]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.h
fc517989364c899a4d315585ad2de04552653eaa
[libs/gltk.git] / source / widget.h
1 #ifndef MSP_GLTK_WIDGET_H_
2 #define MSP_GLTK_WIDGET_H_
3
4 #include <string>
5 #include <msp/core/noncopyable.h>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/gl/renderer.h>
8 #include "geometry.h"
9 #include "inputmethod.h"
10 #include "mspgltk_api.h"
11 #include "partcache.h"
12 #include "state.h"
13
14 namespace Msp {
15 namespace GLtk {
16
17 class Container;
18 class Part;
19 class Resources;
20 class Style;
21
22 /**
23 Base class for all widgets.
24 */
25 class MSPGLTK_API Widget: public NonCopyable
26 {
27         friend class Container;
28
29 public:
30         class Loader: public DataFile::ObjectLoader<Widget>
31         {
32         public:
33                 Loader(Widget &);
34         private:
35                 void position(int, int);
36                 void size(unsigned, unsigned);
37                 void style(const std::string &);
38         };
39
40         sigc::signal<void, bool> signal_visibility_changed;
41         sigc::signal<void> signal_autosize_changed;
42         sigc::signal<void> signal_request_focus;
43         sigc::signal<void> signal_grab_pointer;
44         sigc::signal<void> signal_ungrab_pointer;
45         sigc::signal<void, const Time::TimeDelta &> signal_request_animation;
46         sigc::signal<void> signal_rebuild_needed;
47
48 protected:
49         Geometry geom;
50         std::string style_name;
51         const Style *style = nullptr;
52         State state = NORMAL;
53         bool visible = true;
54         InputType input_type = INPUT_NONE;
55         Container *parent = nullptr;
56         std::string tooltip;
57         PartCache part_cache;
58         bool rebuild_needed = false;
59         Time::TimeDelta anim_interval;
60
61         Widget() = default;
62 public:
63         virtual ~Widget();
64
65         /// Returns the name of the widget class.  Used for style lookup.
66         virtual const char *get_class() const { return "widget"; }
67
68         void set_position(int, int);
69         void set_size(unsigned, unsigned);
70         void autosize();
71         void autosize(Geometry &) const;
72 protected:
73         virtual void autosize_special(const Part &, Geometry &) const { };
74 public:
75         void set_geometry(const Geometry &);
76         const Geometry &get_geometry() const { return geom; }
77
78         void map_coords_to_ancestor(int &, int &, const Widget &) const;
79         void map_coords_to_root(int &, int &) const;
80
81 protected:
82         /** Sets the widget's parent Container.  The widget must be unparented when
83         calling this function with a non-null parameter. */
84         void set_parent(Container *);
85 public:
86         Container *get_parent() const { return parent; }
87
88         /** Finds the closest ancestor of a specific type. */
89         template<typename T>
90         T *find_ancestor() const
91         {
92                 for(Widget *w=parent; w; w=w->get_parent())
93                         if(T *tw = dynamic_cast<T *>(w))
94                                 return tw;
95                 return 0;
96         }
97
98         /** Sets the widget style.  The name of the resource to be looked up is
99         constructed by concatenating the widget class and the style name with a
100         dash. */
101         void set_style(const std::string &);
102         const Style &get_style() const { return *style; }
103
104 protected:
105         /** Gets a style object from the resource collection based on the class and
106         style names of the widget. */
107         void update_style();
108
109 public:
110         void set_tooltip(const std::string &);
111         const std::string &get_tooltip() const { return tooltip; }
112
113         void set_visible(bool);
114         bool is_visible() const { return visible; }
115         InputType get_input_type() const { return input_type; }
116         bool is_focusable() const { return visible && input_type!=INPUT_NONE; }
117         void set_focus();
118         void set_enabled(bool);
119         bool is_enabled() const { return !(state&DISABLED); }
120
121 protected:
122         void set_state(State s) { set_state(s, s); }
123         void clear_state(State s) { set_state(s, NORMAL); }
124         void set_state(State, State);
125 public:
126         State get_state() const { return state; }
127
128 protected:
129         void set_animation_interval(const Time::TimeDelta &);
130         void stop_animation();
131 public:
132         const Time::TimeDelta &get_animation_interval() const { return anim_interval; }
133
134 protected:
135         void mark_rebuild();
136         virtual void rebuild_hierarchy();
137         void rebuild();
138         virtual void rebuild_special(const Part &);
139
140 public:
141         void render(GL::Renderer &) const;
142 protected:
143         virtual void render_special(const Part &, GL::Renderer &) const { }
144
145 public:
146         // Events
147         virtual void button_press(int, int, unsigned) { }
148         virtual void button_release(int, int, unsigned) { }
149         virtual void pointer_motion(int, int) { }
150         virtual void pointer_enter();
151         virtual void pointer_leave();
152         virtual void touch_press(int, int, unsigned);
153         virtual void touch_release(int, int, unsigned);
154         virtual void touch_motion(int, int, unsigned);
155         virtual bool key_press(unsigned, unsigned) { return false; }
156         virtual bool key_release(unsigned, unsigned) { return false; }
157         virtual bool character(wchar_t) { return false; }
158         virtual void focus_in();
159         virtual void focus_out();
160         virtual bool navigate(Navigation) { return false; }
161         virtual void animate(const Time::TimeDelta &) { }
162 protected:
163         virtual void on_size_change() { }
164         virtual void on_style_change() { }
165         virtual void on_reparent() { }
166 };
167
168 } // namespace GLtk
169 } // namespace Msp
170
171 #endif