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