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