]> git.tdb.fi Git - libs/gltk.git/blob - source/userinterface.h
Rework how widget ownership works in Container
[libs/gltk.git] / source / userinterface.h
1 #ifndef MSP_GLTK_USERINTERFACE_H_
2 #define MSP_GLTK_USERINTERFACE_H_
3
4 #include <msp/core/maputils.h>
5 #include "logic.h"
6 #include "root.h"
7
8 namespace Msp {
9 namespace GLtk {
10
11 /**
12 Encapsulates a Root widget and Logic associated with it.  Allows looking up
13 widgets by name.
14 */
15 class UserInterface
16 {
17 public:
18         class Loader: public DataFile::Loader
19         {
20         private:
21                 UserInterface &ui;
22
23         public:
24                 Loader(UserInterface &);
25         private:
26                 void logic();
27                 void root();
28         };
29
30         typedef std::map<std::string, Widget *> WidgetMap;
31
32 private:
33         WidgetMap widgets;
34         Root root;
35         Logic logic;
36
37 public:
38         UserInterface(Resources &, Graphics::Window &);
39         Root &get_root() { return root; }
40         const Logic &get_logic() const { return logic; }
41
42         template<typename W>
43         W &get_widget(const std::string &n) const
44         {
45                 return dynamic_cast<W &>(*get_item(widgets, n));
46         }
47
48         template<typename W>
49         void get_widget(const std::string &n, W *&w) const
50         {
51                 w = &get_widget<W>(n);
52         }
53 };
54
55 } // namespace GLtk
56 } // namespace Msp
57
58 #endif