]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add some editing functions to Entry
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2019 15:53:54 +0000 (18:53 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2019 15:53:54 +0000 (18:53 +0300)
source/entry.cpp
source/entry.h

index af6fcc6d0fd31ed5cfdbdc03f4fda57e9db6c55c..70321dc5a68cf029128f07c52e9b06f44ea38f91 100644 (file)
@@ -57,7 +57,17 @@ void Entry::autosize_special(const Part &part, Geometry &ageom) const
 void Entry::set_text(const string &t)
 {
        text = t;
-       edit_pos = text.size();
+       set_edit_position(text.size());
+}
+
+void Entry::insert(unsigned pos, const string &t)
+{
+       text.insert(pos, t);
+
+       if(edit_pos>=pos)
+               edit_pos += t.size();
+       if(selection_active && selection_pos>=pos)
+               selection_pos += t.size();
 
        if(multiline)
                check_view_range();
@@ -65,6 +75,52 @@ void Entry::set_text(const string &t)
        rebuild();
 }
 
+void Entry::erase(unsigned pos, unsigned len)
+{
+       text.erase(pos, len);
+
+       if(edit_pos>=pos+len)
+               edit_pos -= len;
+       else if(edit_pos>=pos)
+               edit_pos = pos;
+       if(selection_active)
+       {
+               if(selection_pos>=pos+len)
+                       selection_pos -= len;
+               else if(selection_pos>=pos)
+                       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(multiline)
+               check_view_range();
+
+       rebuild();
+}
+
+bool Entry::get_selection(unsigned &start, unsigned &end) const
+{
+       if(!selection_active)
+               return false;
+
+       start = selection_pos;
+       end = edit_pos;
+       if(start>end)
+               swap(start, end);
+
+       return true;
+}
+
 void Entry::set_edit_size(unsigned w, unsigned h)
 {
        edit_width = w;
index a5b03fcdd64e77571237f62a4ec4e81c90111ff9..a350ea8890d2e91b75093c73bf601983eafc2c54 100644 (file)
@@ -59,8 +59,14 @@ private:
 
 public:
        void set_text(const std::string &);
+       void insert(unsigned, const std::string &);
+       void erase(unsigned, unsigned);
        const std::string &get_text() const { return text.get(); }
 
+       void set_edit_position(unsigned);
+       unsigned get_edit_position() const { return edit_pos; }
+       bool get_selection(unsigned &, unsigned &) const;
+
        /** Sets the minimum size of the editing area, in characters and rows.  This
        only affects autosizing. */
        void set_edit_size(unsigned w, unsigned h);