]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.h
Add protected functions for manipulating widget state
[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 "state.h"
8
9 namespace Msp {
10 namespace GLtk {
11
12 class Container;
13 class Part;
14 class Resources;
15 class Style;
16
17 /**
18 Base class for all widgets.  Derived classes should call update_style in 
19 constructor, because it can't be done correctly in the Widget constructor.
20 */
21 class Widget
22 {
23         friend class Container;
24
25 public:
26         class Loader: public DataFile::ObjectLoader<Widget>
27         {
28         public:
29                 Loader(Widget &);
30         private:
31                 void position(int, int);
32                 void size(unsigned, unsigned);
33                 void style(const std::string &);
34         };
35
36         sigc::signal<void, bool> signal_visibility_changed;
37         sigc::signal<void> signal_autosize_changed;
38         sigc::signal<void> signal_request_focus;
39         sigc::signal<void> signal_grab_pointer;
40         sigc::signal<void> signal_ungrab_pointer;
41
42 protected:
43         Geometry geom;
44         std::string style_name;
45         const Style *style;
46         State state;
47         bool visible;
48         bool focusable;
49         Container *parent;
50         std::string tooltip;
51
52         Widget();
53 public:
54         virtual ~Widget();
55
56         /**
57         Returns the name of the widget class.  Used for style lookup.
58         */
59         virtual const char *get_class() const { return "widget"; }
60
61         void set_position(int, int);
62         void set_size(unsigned, unsigned);
63         virtual void autosize();
64         void set_geometry(const Geometry &);
65         const Geometry &get_geometry() const { return geom; }
66
67 protected:
68         /**
69         Sets the widget's parent Container.  The widget must be unparented when
70         calling this function with a non-null parameter.
71         */
72         void set_parent(Container *);
73 public:
74         Container *get_parent() const { return parent; }
75
76         /**
77         Sets the widget style.  The final style name is constructed by concatenating
78         the widget class and the style name with a dash.
79         */
80         void set_style(const std::string &);
81         const Style &get_style() const { return *style; }
82
83 protected:
84         /**
85         Gets a style object from the resource collection based on the class and
86         style names of the widget.
87         */
88         void update_style();
89
90 public:
91         void set_tooltip(const std::string &);
92         const std::string &get_tooltip() const { return tooltip; }
93
94         void set_visible(bool);
95         bool is_visible() const { return visible; }
96         void set_focusable(bool);
97         bool is_focusable() const { return focusable; }
98         void set_focus();
99
100 protected:
101         void set_state(State s) { set_state(s, s); }
102         void clear_state(State s) { set_state(s, NORMAL); }
103         void set_state(State, State);
104
105 public:
106         void render() const;
107 protected:
108         virtual void render_special(const Part &) const { }
109
110 public:
111         // Events
112         virtual void button_press(int, int, unsigned) { }
113         virtual void button_release(int, int, unsigned) { }
114         virtual void pointer_motion(int, int) { }
115         virtual void pointer_enter();
116         virtual void pointer_leave();
117         virtual void key_press(unsigned, unsigned) { }
118         virtual void key_release(unsigned, unsigned) { }
119         virtual void character(wchar_t) { }
120         virtual void focus_in();
121         virtual void focus_out();
122 protected:
123         virtual void on_geometry_change() { }
124         virtual void on_style_change() { }
125         virtual void on_reparent() { }
126 };
127
128 } // namespace GLtk
129 } // namespace Msp
130
131 #endif