X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fentry.cpp;h=54c5fb7cbcdd49fc48f06dae0761c9843fd6b0de;hb=bbc49b78e8b766fc695ab90fdbe00f579882fc99;hp=70321dc5a68cf029128f07c52e9b06f44ea38f91;hpb=8a829f00f36517625d57b002e3a479755c0eae6d;p=libs%2Fgltk.git diff --git a/source/entry.cpp b/source/entry.cpp index 70321dc..54c5fb7 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -64,11 +64,15 @@ void Entry::insert(unsigned pos, const string &t) { text.insert(pos, t); + unsigned old_edit_pos = edit_pos; if(edit_pos>=pos) edit_pos += t.size(); if(selection_active && selection_pos>=pos) selection_pos += t.size(); + if(edit_pos!=old_edit_pos) + signal_edit_position_changed.emit(edit_pos); + if(multiline) check_view_range(); @@ -79,6 +83,7 @@ void Entry::erase(unsigned pos, unsigned len) { text.erase(pos, len); + unsigned old_edit_pos = edit_pos; if(edit_pos>=pos+len) edit_pos -= len; else if(edit_pos>=pos) @@ -91,16 +96,8 @@ void Entry::erase(unsigned pos, unsigned len) selection_pos = pos; } - if(multiline) - check_view_range(); - - rebuild(); -} - -void Entry::set_edit_position(unsigned pos) -{ - edit_pos = min(pos, text.size()); - selection_active = false; + if(edit_pos!=old_edit_pos) + signal_edit_position_changed.emit(edit_pos); if(multiline) check_view_range(); @@ -121,6 +118,16 @@ bool Entry::get_selection(unsigned &start, unsigned &end) const return true; } +void Entry::translate_position(unsigned pos, unsigned &row, unsigned &col) const +{ + text.offset_to_coords(pos, row, col); +} + +unsigned Entry::translate_position(unsigned row, unsigned col) const +{ + return text.coords_to_offset(row, col); +} + void Entry::set_edit_size(unsigned w, unsigned h) { edit_width = w; @@ -177,10 +184,8 @@ void Entry::rebuild_special(const Part &part) if(!text_part || !graphic || !graphic->get_texture()) return; - unsigned start = selection_pos; - unsigned end = edit_pos; - if(start>end) - swap(start, end); + unsigned start, end; + get_selection(start, end); unsigned row, col; text.offset_to_coords(start, row, col); @@ -252,25 +257,17 @@ bool Entry::key_press(unsigned key, unsigned mod) if(selection_active) erase_selection(); else if(edit_pos>0) - { - text.erase(--edit_pos, 1); - check_view_range(); - rebuild(); - } + erase(edit_pos-1, 1); } else if(key==Input::KEY_DELETE) { if(selection_active) erase_selection(); else - text.erase(edit_pos, 1); + erase(edit_pos, 1); } else if(key==Input::KEY_ENTER && multiline) - { - text.insert(edit_pos++, "\n"); - check_view_range(); - rebuild(); - } + insert(edit_pos, "\n"); else if(key==Input::KEY_END) { unsigned row, col; @@ -303,9 +300,7 @@ bool Entry::character(wchar_t ch) { if(selection_active) erase_selection(); - text.insert(edit_pos, StringCodec::encode(StringCodec::ustring(1, ch))); - ++edit_pos; - rebuild(); + insert(edit_pos, StringCodec::encode(StringCodec::ustring(1, ch))); return true; } @@ -400,21 +395,23 @@ void Entry::set_edit_position(unsigned ep, bool select) selection_pos = edit_pos; selection_active = select; - edit_pos = ep; - check_view_range(); + unsigned old_edit_pos = edit_pos; + edit_pos = min(ep, text.size()); + + if(edit_pos!=old_edit_pos) + signal_edit_position_changed.emit(edit_pos); + + if(multiline) + check_view_range(); rebuild(); } void Entry::erase_selection() { - if(!selection_active) + unsigned start, end; + if(!get_selection(start, end)) return; - unsigned start = selection_pos; - unsigned end = edit_pos; - if(start>end) - swap(start, end); - text.erase(start, end-start); set_edit_position(start, false); } @@ -440,20 +437,20 @@ 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); + unsigned old_first_row = first_row; if(first_row>row) first_row = row; else if(row>=first_row+visible_rows) first_row = row+1-visible_rows; + if(first_row!=old_first_row) + signal_scroll_position_changed.emit(first_row); + unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows; slider->set_range(0, scroll); slider->set_value(scroll-first_row); @@ -462,7 +459,12 @@ void Entry::check_view_range() void Entry::slider_value_changed(double value) { if(text.get_n_lines()>visible_rows) + { + unsigned old_first_row = first_row; first_row = text.get_n_lines()-visible_rows-static_cast(value); + if(first_row!=old_first_row) + signal_scroll_position_changed.emit(first_row); + } }