]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.h
3c7ab63548aa35fda07df38eeca8a105b9788bc0
[libs/gltk.git] / source / entry.h
1 #ifndef MSP_GLTK_ENTRY_H_
2 #define MSP_GLTK_ENTRY_H_
3
4 #include "container.h"
5 #include "mspgltk_api.h"
6 #include "text.h"
7 #include "widget.h"
8
9 namespace Msp {
10 namespace GLtk {
11
12 class VSlider;
13
14 /**
15 Text entry field.
16
17 Special parts:
18
19   text       The current text of the widget.  Graphics are ignored.
20   cursor     Indicates the current input position.  Fill and align are taken
21              from the text part.
22   selection  Highlights the selected text.  Fill and align are taken from the
23              text part.
24   slider     A vertical slider for multiline entries.
25 */
26 class MSPGLTK_API Entry: virtual public Widget, private Container
27 {
28 public:
29         class MSPGLTK_API Loader: public DataFile::DerivedObjectLoader<Entry, Widget::Loader>
30         {
31         public:
32                 Loader(Entry &);
33
34         private:
35                 void multiline(bool);
36         };
37
38         sigc::signal<void, unsigned> signal_edit_position_changed;
39         sigc::signal<void, unsigned> signal_scroll_position_changed;
40         sigc::signal<void, unsigned, unsigned> signal_selection_changed;
41         sigc::signal<void, const std::string &> signal_text_changed;
42
43 private:
44         Text text;
45         bool multiline = false;
46         unsigned edit_width = 10;
47         unsigned edit_height = 1;
48         std::size_t edit_pos = 0;
49         std::size_t first_row = 0;
50         unsigned visible_rows = 1;
51         const Part *text_part = nullptr;
52         VSlider *slider = nullptr;
53         bool got_key_press = false;
54         bool cursor_blink = true;
55         bool selection_active = false;
56         std::size_t selection_pos = 0;
57
58 public:
59         Entry(const std::string & = std::string());
60
61         const char *get_class() const override { return "entry"; }
62
63 private:
64         void autosize_special(const Part &, Geometry &) const override;
65
66 public:
67         void set_text(const std::string &);
68         void insert(std::size_t, const std::string &);
69         void erase(std::size_t, std::size_t);
70         const std::string &get_text() const { return text.get(); }
71
72         void set_edit_position(std::size_t p) { set_edit_position(p, false); }
73         std::size_t get_edit_position() const { return edit_pos; }
74         unsigned get_scroll_position() const { return first_row; }
75         unsigned get_visible_rows() const { return visible_rows; }
76         bool get_selection(std::size_t &, std::size_t &) const;
77         void translate_position(std::size_t, std::size_t &, std::size_t &) const;
78         std::size_t translate_position(std::size_t, std::size_t) const;
79
80         /** Sets the minimum size of the editing area, in characters and rows.  This
81         only affects autosizing. */
82         void set_edit_size(unsigned w, unsigned h);
83
84         void set_multiline(bool);
85         bool is_multiline() const { return multiline; }
86
87 private:
88         void rebuild_special(const Part &) override;
89         void render_special(const Part &, GL::Renderer &) const override;
90
91 public:
92         void touch_press(int, int, unsigned) override;
93         bool key_press(unsigned, unsigned) override;
94         bool character(wchar_t) override;
95         void focus_in() override;
96         void focus_out() override;
97         bool navigate(Navigation) override;
98         void animate(const Time::TimeDelta &) override;
99 private:
100         void on_size_change() override;
101         void on_style_change() override;
102
103         void move_edit_position(Navigation, bool);
104         void adjust_edit_position_for_change(std::size_t, std::ptrdiff_t);
105         void set_edit_position(std::size_t, bool);
106         void erase_selection(bool);
107         void check_cursor_blink();
108         void check_view_range();
109         void slider_value_changed(double);
110 };
111
112 } // namespace GLtk
113 } // namespace Msp
114
115 #endif
116