]> git.tdb.fi Git - libs/gltk.git/blob - source/widget.h
8cf67d439b7c7e2a27c2b360623d21b5aa607751
[libs/gltk.git] / source / widget.h
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  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 Panel;
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 public:
30         class Loader: public Msp::DataFile::Loader
31         {
32         protected:
33                 Widget &wdg;
34
35         public:
36                 Loader(Widget &);
37                 Widget &get_object() const { return wdg; }
38         private:
39                 void position(int, int);
40                 void size(unsigned, unsigned);
41                 void style(const std::string &);
42         };
43
44 protected:
45         const Resources &res;
46         Geometry geom;
47         std::string style_name;
48         const Style *style;
49         State state;
50         bool visible;
51         Panel *parent;
52
53         Widget(const Resources &);
54 public:
55         virtual ~Widget();
56         void set_position(int, int);
57         void set_size(unsigned, unsigned);
58         void set_geometry(const Geometry &);
59
60         /**
61         Sets the widget style.  The final style name is constructed by concatenating
62         the widget class and the style name with a dash.
63         */
64         void set_style(const std::string &);
65         const Style &get_style() const { return *style; }
66
67         void set_visible(bool);
68         void set_focus();
69
70         const Geometry &get_geometry() const { return geom; }
71         bool is_visible() const { return visible; }
72
73         void render() const;
74 protected:
75         void render_graphic(const Part &) const;
76         void render_text(const Part &, const std::string &) const;
77         virtual void render_special(const Part &) const { }
78
79 public:
80         // Events
81         virtual void button_press(int, int, unsigned) { }
82         virtual void button_release(int, int, unsigned) { }
83         virtual void pointer_motion(int, int) { }
84         virtual void pointer_enter();
85         virtual void pointer_leave();
86         virtual void key_press(unsigned, unsigned, wchar_t) { }
87         virtual void key_release(unsigned, unsigned) { }
88         virtual void focus_in();
89         virtual void focus_out();
90
91 protected:
92         /**
93         Returns the name of the widget class.  Used for style lookup.
94         */
95         virtual const char *get_class() const { return "widget"; }
96
97         /**
98         Gets a style object from the resource collection based on the class and
99         style names of the widget.
100         */
101         void update_style();
102
103         /**
104         Sets the widget's parent Panel.  The widget must be unparented when calling
105         this function with a nonzero parameter.
106         */
107         void set_parent(Panel *);
108
109         /**
110         A helper function to set the parent of another widget.
111         */
112         void set_parent(Widget &, Panel *);
113
114         // More events
115         virtual void on_geometry_change() { }
116         virtual void on_style_change() { }
117         virtual void on_reparent() { }
118 };
119
120 } // namespace GLtk
121 } // namespace Msp
122
123 #endif