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