]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/text.cpp
Store the Resources reference only in Root widget
[libs/gltk.git] / source / text.cpp
index c173a8a5edb3a603351661dca1f22347270d85b6..c8c6f55ea3d6597ca348b7dbc911672d93d0ed6e 100644 (file)
@@ -42,9 +42,12 @@ void Text::set_style(const Style *s)
 {
        style = s;
 
-       float font_size = style->get_font()->get_default_size();
-       for(vector<Line>::iterator i=lines.begin(); i!=lines.end(); ++i)
-               i->width = static_cast<unsigned>(style->get_font()->get_string_width(text.substr(i->start, i->length))*font_size);
+       if(style)
+       {
+               float font_size = style->get_font()->get_default_size();
+               for(vector<Line>::iterator i=lines.begin(); i!=lines.end(); ++i)
+                       i->width = static_cast<unsigned>(style->get_font()->get_string_width(text.substr(i->start, i->length))*font_size);
+       }
 }
 
 unsigned Text::get_width() const
@@ -57,6 +60,9 @@ unsigned Text::get_width() const
 
 unsigned Text::get_height() const
 {
+       if(!style)
+               return lines.size();
+
        const GL::Font *font = style->get_font();
        float font_size = font->get_default_size();
        unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
@@ -158,7 +164,7 @@ Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsi
 
 void Text::render(const Part &part, const Geometry &parent, unsigned first_row) const
 {
-       if(lines.empty())
+       if(!style || lines.empty())
                return;
 
        const GL::Color &color = style->get_font_color();
@@ -180,7 +186,7 @@ Text &Text::operator=(const string &t)
 void Text::find_lines()
 {
        lines.clear();
-       float font_size = style->get_font()->get_default_size();
+       float font_size = (style ? style->get_font()->get_default_size() : 1);
        string::size_type start = 0;
        while(1)
        {
@@ -189,7 +195,12 @@ void Text::find_lines()
                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);
+               line.width = line.length;
+               if(style)
+               {
+                       string str = text.substr(line.start, line.length);
+                       line.width = static_cast<unsigned>(style->get_font()->get_string_width(str)*font_size);
+               }
                lines.push_back(line);
 
                if(newline==string::npos)
@@ -201,6 +212,9 @@ void Text::find_lines()
 template<typename T, void (Text::*func)(unsigned, const Geometry &, T &) const>
 void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const
 {
+       if(!style)
+               return;
+
        const GL::Font *font = style->get_font();
        float font_size = font->get_default_size();
        unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
@@ -209,7 +223,7 @@ void Text::process_lines(const Part &part, const Geometry &parent, unsigned firs
        int y_offset = static_cast<int>(-font->get_descent()*font_size);
 
        const Sides &margin = part.get_margin();
-       unsigned n_lines = max(min(lines.size(), (parent.h-margin.top-margin.bottom)/line_spacing), 1U);
+       unsigned n_lines = min(lines.size(), max((parent.h-margin.top-margin.bottom)/line_spacing, 1U));
        first_row = min(first_row, lines.size()-n_lines);
 
        for(unsigned i=0; i<n_lines; ++i)