]> git.tdb.fi Git - libs/gltk.git/commitdiff
Some more interfaces to support integrating Entry with other components
authorMikko Rasa <tdb@tdb.fi>
Thu, 12 Sep 2019 13:46:07 +0000 (16:46 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 12 Sep 2019 14:07:14 +0000 (17:07 +0300)
source/entry.cpp
source/entry.h

index fec27397eaf8a445f068e8edf19b2d6f0decb882..54c5fb7cbcdd49fc48f06dae0761c9843fd6b0de 100644 (file)
@@ -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,8 +96,12 @@ void Entry::erase(unsigned pos, unsigned len)
                        selection_pos = pos;
        }
 
+       if(edit_pos!=old_edit_pos)
+               signal_edit_position_changed.emit(edit_pos);
+
        if(multiline)
                check_view_range();
+
        rebuild();
 }
 
@@ -109,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;
@@ -376,7 +395,12 @@ void Entry::set_edit_position(unsigned ep, bool select)
                selection_pos = edit_pos;
        selection_active = select;
 
+       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();
@@ -418,11 +442,15 @@ void Entry::check_view_range()
        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);
@@ -431,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<unsigned>(value);
+               if(first_row!=old_first_row)
+                       signal_scroll_position_changed.emit(first_row);
+       }
 }
 
 
index b6b4998662a91cba957fe43396ec90316e34a15e..02d1f2e50e5a49596864ed1d41f4d0667476f267 100644 (file)
@@ -31,6 +31,9 @@ public:
                void multiline(bool);
        };
 
+       sigc::signal<void, unsigned> signal_edit_position_changed;
+       sigc::signal<void, unsigned> signal_scroll_position_changed;
+
        // Deprecated
        sigc::signal<void> signal_enter;
 
@@ -65,7 +68,11 @@ public:
 
        void set_edit_position(unsigned p) { set_edit_position(p, false); }
        unsigned get_edit_position() const { return edit_pos; }
+       unsigned get_scroll_position() const { return first_row; }
+       unsigned get_visible_rows() const { return visible_rows; }
        bool get_selection(unsigned &, unsigned &) const;
+       void translate_position(unsigned, unsigned &, unsigned &) const;
+       unsigned translate_position(unsigned, unsigned) const;
 
        /** Sets the minimum size of the editing area, in characters and rows.  This
        only affects autosizing. */