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
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