]> git.tdb.fi Git - libs/gltk.git/commitdiff
Handle line data when inserting/erasing text
authorMikko Rasa <tdb@tdb.fi>
Fri, 25 Dec 2009 17:29:37 +0000 (17:29 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 25 Dec 2009 17:29:37 +0000 (17:29 +0000)
source/text.cpp
source/text.h

index e4ec3290ac91ee551931d6444e27f4aeb86d64ef..e33eb80714fb572185c6a31c9127501fc1621bb7 100644 (file)
@@ -38,33 +38,43 @@ unsigned Text::get_height() const
 void Text::set(const string &t)
 {
        text=t;
-       lines.clear();
-       float font_size=style->get_font()->get_default_size();
-       string::size_type start=0;
-       while(1)
-       {
-               string::size_type newline=text.find('\n', start);
-
-               Line line;
-               line.start=start;
-               line.length=(newline==string::npos ? text.size() : newline)-start;
-               line.width=static_cast<unsigned>(style->get_font()->get_string_width(text.substr(line.start, line.length))*font_size);
-               lines.push_back(line);
-
-               if(newline==string::npos)
-                       break;
-               start=newline+1;
-       }
+       find_lines();
 }
 
 void Text::erase(unsigned pos, unsigned len)
 {
        text.erase(pos, len);
+
+       vector<Line>::iterator i;
+       for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
+
+       if(pos+len>i->start+i->length)
+               find_lines();
+       else
+       {
+               i->length-=len;
+
+               for(++i; i!=lines.end(); ++i)
+                       i->start-=len;
+       }
 }
 
 void Text::insert(unsigned pos, const string &s)
 {
        text.insert(pos, s);
+
+       if(s.find('\n')!=string::npos)
+               find_lines();
+       else
+       {
+               vector<Line>::iterator i;
+               for(i=lines.begin(); (i!=lines.end() && i->start+i->length<pos); ++i) ;
+
+               i->length+=s.size();
+
+               for(++i; i!=lines.end(); ++i)
+                       i->start+=s.size();
+       }
 }
 
 void Text::render(const Part &part, const Geometry &geom) const
@@ -108,5 +118,26 @@ Text &Text::operator=(const string &t)
        return *this;
 }
 
+void Text::find_lines()
+{
+       lines.clear();
+       float font_size=style->get_font()->get_default_size();
+       string::size_type start=0;
+       while(1)
+       {
+               string::size_type newline=text.find('\n', start);
+
+               Line line;
+               line.start=start;
+               line.length=(newline==string::npos ? text.size() : newline)-start;
+               line.width=static_cast<unsigned>(style->get_font()->get_string_width(text.substr(line.start, line.length))*font_size);
+               lines.push_back(line);
+
+               if(newline==string::npos)
+                       break;
+               start=newline+1;
+       }
+}
+
 } // namespace GLtk
 } // namespace Msp
index 46bd18f9075844cf7401bd7193119e817e0fd8ff..5daeb9bae70db324014f330ee04b5edf76adba34 100644 (file)
@@ -44,7 +44,8 @@ public:
        void render(const Part &, const Geometry &) const;
 
        Text &operator=(const std::string &);
-       //operator const std::string &() const { return text; }
+private:
+       void find_lines();
 };
 
 } // namespace GLtk