From: Mikko Rasa Date: Fri, 25 Dec 2009 17:29:37 +0000 (+0000) Subject: Handle line data when inserting/erasing text X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=62417b367503a9b26510c75e2332db21af1497bf;p=libs%2Fgltk.git Handle line data when inserting/erasing text --- diff --git a/source/text.cpp b/source/text.cpp index e4ec329..e33eb80 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -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(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::iterator i; + for(i=lines.begin(); (i!=lines.end() && i->start+i->lengthi->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::iterator i; + for(i=lines.begin(); (i!=lines.end() && i->start+i->lengthlength+=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(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 diff --git a/source/text.h b/source/text.h index 46bd18f..5daeb9b 100644 --- a/source/text.h +++ b/source/text.h @@ -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