From: Mikko Rasa Date: Thu, 12 Sep 2019 10:55:11 +0000 (+0300) Subject: Refactor visible height calculation in Entry and Text X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7c1b1b44dc3726d6cdb4681f9d44189eb4102a5a;p=libs%2Fgltk.git Refactor visible height calculation in Entry and Text It's now contained in a common function to ensure it's calculated the same way in both. --- diff --git a/source/entry.cpp b/source/entry.cpp index 70321dc..6d13ccd 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -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(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); diff --git a/source/text.cpp b/source/text.cpp index cdaae85..1d824e0 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -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((font.get_ascent()-font.get_descent())*font_size); + unsigned line_spacing = static_cast(font_size*6/5); + + const Sides &margin = part.get_margin(); + unsigned vmargin = margin.top+margin.bottom+line_height-line_spacing; + unsigned n_lines = min(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((font.get_ascent()-font.get_descent())*font_size); unsigned line_spacing = static_cast(font_size*6/5); int y_offset = static_cast(-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(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(first_row, lines.size()-n_lines); for(unsigned i=0; i