]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.h
Rework how widget ownership works in Container
[libs/gltk.git] / source / entry.h
index 2c9c31448cd4f2c6c3b6403297956109427b412e..3c7ab63548aa35fda07df38eeca8a105b9788bc0 100644 (file)
 #ifndef MSP_GLTK_ENTRY_H_
 #define MSP_GLTK_ENTRY_H_
 
+#include "container.h"
+#include "mspgltk_api.h"
+#include "text.h"
 #include "widget.h"
 
 namespace Msp {
 namespace GLtk {
 
-class Entry: public Widget
+class VSlider;
+
+/**
+Text entry field.
+
+Special parts:
+
+  text       The current text of the widget.  Graphics are ignored.
+  cursor     Indicates the current input position.  Fill and align are taken
+             from the text part.
+  selection  Highlights the selected text.  Fill and align are taken from the
+             text part.
+  slider     A vertical slider for multiline entries.
+*/
+class MSPGLTK_API Entry: virtual public Widget, private Container
 {
 public:
-       Entry(const Resources &, const std::string & =std::string());
+       class MSPGLTK_API Loader: public DataFile::DerivedObjectLoader<Entry, Widget::Loader>
+       {
+       public:
+               Loader(Entry &);
+
+       private:
+               void multiline(bool);
+       };
+
+       sigc::signal<void, unsigned> signal_edit_position_changed;
+       sigc::signal<void, unsigned> signal_scroll_position_changed;
+       sigc::signal<void, unsigned, unsigned> signal_selection_changed;
+       sigc::signal<void, const std::string &> signal_text_changed;
+
+private:
+       Text text;
+       bool multiline = false;
+       unsigned edit_width = 10;
+       unsigned edit_height = 1;
+       std::size_t edit_pos = 0;
+       std::size_t first_row = 0;
+       unsigned visible_rows = 1;
+       const Part *text_part = nullptr;
+       VSlider *slider = nullptr;
+       bool got_key_press = false;
+       bool cursor_blink = true;
+       bool selection_active = false;
+       std::size_t selection_pos = 0;
+
+public:
+       Entry(const std::string & = std::string());
+
+       const char *get_class() const override { return "entry"; }
+
+private:
+       void autosize_special(const Part &, Geometry &) const override;
+
+public:
        void set_text(const std::string &);
-       const std::string &get_text() const { return text; }
-       void key_press(unsigned, unsigned, wchar_t);
-       void focus_in();
-       void focus_out();
+       void insert(std::size_t, const std::string &);
+       void erase(std::size_t, std::size_t);
+       const std::string &get_text() const { return text.get(); }
+
+       void set_edit_position(std::size_t p) { set_edit_position(p, false); }
+       std::size_t 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(std::size_t &, std::size_t &) const;
+       void translate_position(std::size_t, std::size_t &, std::size_t &) const;
+       std::size_t translate_position(std::size_t, std::size_t) 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);
+
+       void set_multiline(bool);
+       bool is_multiline() const { return multiline; }
+
+private:
+       void rebuild_special(const Part &) override;
+       void render_special(const Part &, GL::Renderer &) const override;
+
+public:
+       void touch_press(int, int, unsigned) override;
+       bool key_press(unsigned, unsigned) override;
+       bool character(wchar_t) override;
+       void focus_in() override;
+       void focus_out() override;
+       bool navigate(Navigation) override;
+       void animate(const Time::TimeDelta &) override;
 private:
-       std::string text;
-       unsigned edit_pos;
+       void on_size_change() override;
+       void on_style_change() override;
 
-       const char *get_class() const { return "entry"; }
-       void render_part(const Part &) const;
+       void move_edit_position(Navigation, bool);
+       void adjust_edit_position_for_change(std::size_t, std::ptrdiff_t);
+       void set_edit_position(std::size_t, bool);
+       void erase_selection(bool);
+       void check_cursor_blink();
+       void check_view_range();
+       void slider_value_changed(double);
 };
 
 } // namespace GLtk