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