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