]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor visible height calculation in Entry and Text
authorMikko Rasa <tdb@tdb.fi>
Thu, 12 Sep 2019 10:55:11 +0000 (13:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 12 Sep 2019 11:01:12 +0000 (14:01 +0300)
It's now contained in a common function to ensure it's calculated the
same way in both.

source/entry.cpp
source/text.cpp
source/text.h

index 70321dc5a68cf029128f07c52e9b06f44ea38f91..6d13ccdd0377eaaff11b29db0c27981d5b9cacc0 100644 (file)
@@ -440,11 +440,7 @@ void Entry::check_view_range()
        if(!multiline || !text_part)
                return;
 
-       float font_size = style->get_font_size();
-       unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
-
-       const Sides &margin = text_part->get_margin();
-       visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
+       visible_rows = text.get_visible_lines(*text_part, geom, 0);
 
        unsigned row, col;
        text.offset_to_coords(edit_pos, row, col);
index cdaae859abb481f301ad5a610ae42241f45b5e36..1d824e0ddb9ca590f551748921503e626bcdd931 100644 (file)
@@ -115,6 +115,22 @@ void Text::insert(unsigned pos, const string &s)
        }
 }
 
+unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const
+{
+       const GL::Font &font = style->get_font();
+       float font_size = style->get_font_size();
+       unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
+       unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
+
+       const Sides &margin = part.get_margin();
+       unsigned vmargin = margin.top+margin.bottom+line_height-line_spacing;
+       unsigned n_lines = min<unsigned>(lines.size(), max((max(parent.h, vmargin)-vmargin)/line_spacing, 1U));
+       if(fit_height)
+               *fit_height = line_height+(n_lines-1)*line_spacing;
+
+       return n_lines;
+}
+
 unsigned Text::get_line_length(unsigned i) const
 {
        if(i>=lines.size())
@@ -225,14 +241,11 @@ void Text::process_lines(const Part &part, const Geometry &parent, unsigned firs
 
        const GL::Font &font = style->get_font();
        float font_size = style->get_font_size();
-       unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
        unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
        int y_offset = static_cast<int>(-font.get_descent()*font_size);
 
-       const Sides &margin = part.get_margin();
-       unsigned fit_height = parent.h-margin.top-margin.bottom+line_spacing-line_height;
-       unsigned n_lines = min<unsigned>(lines.size(), max(fit_height/line_spacing, 1U));
-       fit_height = line_height+(n_lines-1)*line_spacing;
+       unsigned fit_height;
+       unsigned n_lines = get_visible_lines(part, parent, &fit_height);
        first_row = min<unsigned>(first_row, lines.size()-n_lines);
 
        for(unsigned i=0; i<n_lines; ++i)
index c49b167a844945d3a55cab385e9bbb5fa7c2f502..243c3a617f6cf2806a2272caf71cf4a44f45121e 100644 (file)
@@ -49,6 +49,7 @@ public:
        const std::string &get() const { return text; }
        unsigned size() const { return text.size(); }
        unsigned get_n_lines() const { return lines.size(); }
+       unsigned get_visible_lines(const Part &, const Geometry &, unsigned *) const;
        unsigned get_line_length(unsigned) const;
        void offset_to_coords(unsigned, unsigned &, unsigned &) const;
        unsigned coords_to_offset(unsigned, unsigned) const;