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