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