Author: mordante
Date: Wed Aug 27 18:43:45 2008
New Revision: 29029
URL: http://svn.gna.org/viewcvs/wesnoth?rev=29029&view=rev
Log:
Extended the class.
Added some helper functions and also keep track of the length in a cache
variable.
Modified:
trunk/src/text.cpp
trunk/src/text.hpp
Modified: trunk/src/text.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/text.cpp?rev=29029&r1=29028&r2=29029&view=diff
==============================================================================
--- trunk/src/text.cpp (original)
+++ trunk/src/text.cpp Wed Aug 27 18:43:45 2008
@@ -75,6 +75,7 @@
maximum_height_(-1),
ellipse_mode_(PANGO_ELLIPSIZE_END),
calculation_dirty_(true),
+ length_(0),
surface_dirty_(true),
surface_buffer_(NULL)
{
@@ -126,6 +127,32 @@
recalculate();
return (pango_layout_is_ellipsized(layout_) == TRUE);
+}
+
+unsigned ttext::insert_text(const unsigned offset, const std::string& text)
+{
+ if(text.empty()) {
+ return 0;
+ }
+
+ wide_string wtext = utils::string_to_wstring(text_);
+ insert_unicode(offset, wtext);
+ return wtext.size();
+}
+
+void ttext::insert_unicode(const unsigned offset, const wchar_t unicode)
+{
+ insert_unicode(offset, wide_string(1, unicode));
+}
+
+void ttext::insert_unicode(const unsigned offset, const wide_string& unicode)
+{
+ assert(offset <= length_);
+
+ wide_string tmp = utils::string_to_wstring(text_);
+ tmp.insert(tmp.begin() + offset, unicode.begin(), unicode.end());
+
+ set_text(utils::wstring_to_string(tmp), false);
}
gui2::tpoint ttext::get_cursor_position(
@@ -199,11 +226,6 @@
}
}
-size_t ttext::get_length() const
-{
- return utils::string_to_wstring(text_).size();
-}
-
ttext& ttext::set_text(const std::string& text, const bool markedup)
{
if(markedup != markedup_text_ || text != text_) {
@@ -212,7 +234,8 @@
} else {
pango_layout_set_text(layout_, text.c_str(),
text.size());
}
- text_ = text;
+ text_ = text;
+ length_ = utils::string_to_wstring(text_).size();
markedup_text_ = markedup;
calculation_dirty_ = true;
surface_dirty_ = true;
Modified: trunk/src/text.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/text.hpp?rev=29029&r1=29028&r2=29029&view=diff
==============================================================================
--- trunk/src/text.hpp (original)
+++ trunk/src/text.hpp Wed Aug 27 18:43:45 2008
@@ -65,6 +65,34 @@
/** Has the text been truncated? */
bool is_truncated() const;
+ /**
+ * Inserts utf 8 text.
+ *
+ * @param offset The position to insert the text.
+ * @param text The utf-8 text to insert.
+ *
+ * @returns The number of characters inserted.
+ */
+ unsigned insert_text(const unsigned offset, const std::string& text);
+
+ /**
+ * Inserts a unicode char.
+ *
+ * @param offset The position to insert the char.
+ * @param unicode The character to insert.
+ *
+ */
+ void insert_unicode(const unsigned offset, const wchar_t unicode);
+
+ /**
+ * Inserts unicode text.
+ *
+ * @param offset The position to insert the text.
+ * @param unicode Vector with characters to insert.
+ */
+ void insert_unicode(
+ const unsigned offset, const std::vector<wchar_t>& unicode);
+
/***** ***** ***** ***** Font flags ***** ***** ***** *****/
/**
@@ -110,11 +138,12 @@
* The text set is utf-8 so the length of the string might not be the
length
* of the text.
*/
- size_t get_length() const;
-
- /***** ***** ***** ***** Setters ***** ***** ***** *****/
+ size_t get_length() const { return length_; }
+
+ /***** ***** ***** ***** Setters / getters ***** ***** ***** *****/
ttext& set_text(const std::string& text, const bool markedup);
+ const std::string& text() const { return text_; }
ttext& set_font_size(const unsigned font_size);
@@ -172,6 +201,9 @@
/** The dirty state of the calculations. */
mutable bool calculation_dirty_;
+ /** Length of the text. */
+ mutable size_t length_;
+
/**
* Recalculates the text.
*
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits