X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftext.cpp;h=e33eb80714fb572185c6a31c9127501fc1621bb7;hb=c72566cd7f8252eb386c753ceeafa8a324d1120b;hp=e4ec3290ac91ee551931d6444e27f4aeb86d64ef;hpb=525351c67a74fc3434fd1fb993bb9fa78b681f1f;p=libs%2Fgltk.git 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