]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.h
Cache widget parts in meshes
[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 "geometry.h"
7 #include "partcache.h"
8 #include "state.h"
9
10 namespace Msp {
11 namespace GLtk {
12
13 class Container;
14 class Part;
15 class Resources;
16 class Style;
17
18 /**
19 Base class for all widgets.  Derived classes should call update_style in 
20 constructor, because it can't be done correctly in the Widget constructor.
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         std::list<CachedPart> cached_parts;
53
54         Widget();
55 public:
56         virtual ~Widget();
57
58         /**
59         Returns the name of the widget class.  Used for style lookup.
60         */
61         virtual const char *get_class() const { return "widget"; }
62
63         void set_position(int, int);
64         void set_size(unsigned, unsigned);
65         virtual void autosize();
66         void set_geometry(const Geometry &);
67         const Geometry &get_geometry() const { return geom; }
68
69 protected:
70         /**
71         Sets the widget's parent Container.  The widget must be unparented when
72         calling this function with a non-null parameter.
73         */
74         void set_parent(Container *);
75 public:
76         Container *get_parent() const { return parent; }
77
78         /**
79         Sets the widget style.  The final style name is constructed by concatenating
80         the widget class and the style name with a dash.
81         */
82         void set_style(const std::string &);
83         const Style &get_style() const { return *style; }
84
85 protected:
86         /**
87         Gets a style object from the resource collection based on the class and
88         style names of the widget.
89         */
90         void update_style();
91
92 public:
93         void set_tooltip(const std::string &);
94         const std::string &get_tooltip() const { return tooltip; }
95
96         void set_visible(bool);
97         bool is_visible() const { return visible; }
98         void set_focusable(bool);
99         bool is_focusable() const { return focusable; }
100         void set_focus();
101
102 protected:
103         void set_state(State s) { set_state(s, s); }
104         void clear_state(State s) { set_state(s, NORMAL); }
105         void set_state(State, State);
106
107         void rebuild();
108         virtual void rebuild_special(const Part &, CachedPart &) { }
109
110 public:
111         void render() const;
112 protected:
113         virtual void render_special(const Part &) const { }
114
115 public:
116         // Events
117         virtual void button_press(int, int, unsigned) { }
118         virtual void button_release(int, int, unsigned) { }
119         virtual void pointer_motion(int, int) { }
120         virtual void pointer_enter();
121         virtual void pointer_leave();
122         virtual void key_press(unsigned, unsigned) { }
123         virtual void key_release(unsigned, unsigned) { }
124         virtual void character(wchar_t) { }
125         virtual void focus_in();
126         virtual void focus_out();
127 protected:
128         virtual void on_geometry_change() { }
129         virtual void on_style_change() { }
130         virtual void on_reparent() { }
131 };
132
133 } // namespace GLtk
134 } // namespace Msp
135
136 #endif