]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.h
7c601d2b5860668b426e3b8466ef316ab92fb2c3
[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.  Derived classes should call update_style in 
21 constructor, because it can't be done correctly in the Widget constructor.
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
44 protected:
45         Geometry geom;
46         std::string style_name;
47         const Style *style;
48         State state;
49         bool visible;
50         bool focusable;
51         Container *parent;
52         std::string tooltip;
53         PartCache part_cache;
54
55         Widget();
56 public:
57         virtual ~Widget();
58
59         /// Returns the name of the widget class.  Used for style lookup.
60         virtual const char *get_class() const { return "widget"; }
61
62         void set_position(int, int);
63         void set_size(unsigned, unsigned);
64         void autosize();
65 protected:
66         virtual void autosize_special(const Part &, Geometry &) { }
67 public:
68         void set_geometry(const Geometry &);
69         const Geometry &get_geometry() const { return geom; }
70
71         void map_coords_to_ancestor(int &, int &, const Widget &) const;
72
73 protected:
74         /** Sets the widget's parent Container.  The widget must be unparented when
75         calling this function with a non-null parameter. */
76         void set_parent(Container *);
77 public:
78         Container *get_parent() const { return parent; }
79
80         /** Finds the closest ancestor of a specific type. */
81         template<typename T>
82         T *find_ancestor() const
83         {
84                 for(Widget *w=parent; w; w=w->get_parent())
85                         if(T *tw = dynamic_cast<T *>(w))
86                                 return tw;
87                 return 0;
88         }
89
90         /** Sets the widget style.  The name of the resource to be looked up is
91         constructed by concatenating the widget class and the style name with a
92         dash. */
93         void set_style(const std::string &);
94         const Style &get_style() const { return *style; }
95
96 protected:
97         /** Gets a style object from the resource collection based on the class and
98         style names of the widget. */
99         void update_style();
100
101 public:
102         void set_tooltip(const std::string &);
103         const std::string &get_tooltip() const { return tooltip; }
104
105         void set_visible(bool);
106         bool is_visible() const { return visible; }
107         void set_focusable(bool);
108         bool is_focusable() const { return focusable; }
109         void set_focus();
110
111 protected:
112         void set_state(State s) { set_state(s, s); }
113         void clear_state(State s) { set_state(s, NORMAL); }
114         void set_state(State, State);
115 public:
116         State get_state() const { return state; }
117
118 protected:
119         void rebuild();
120         virtual void rebuild_special(const Part &);
121
122 public:
123         void render(GL::Renderer &) const;
124 protected:
125         virtual void render_special(const Part &, GL::Renderer &) const { }
126
127 public:
128         // Events
129         virtual void button_press(int, int, unsigned) { }
130         virtual void button_release(int, int, unsigned) { }
131         virtual void pointer_motion(int, int) { }
132         virtual void pointer_enter();
133         virtual void pointer_leave();
134         virtual void key_press(unsigned, unsigned) { }
135         virtual void key_release(unsigned, unsigned) { }
136         virtual void character(wchar_t) { }
137         virtual void focus_in();
138         virtual void focus_out();
139 protected:
140         virtual void on_geometry_change() { }
141         virtual void on_style_change() { }
142         virtual void on_reparent() { }
143 };
144
145 } // namespace GLtk
146 } // namespace Msp
147
148 #endif