]> git.tdb.fi Git - libs/gltk.git/blob - source/userinterface.h
Strip copyright messages and id tags from individual files
[libs/gltk.git] / source / userinterface.h
1 #ifndef MSP_GLTK_USERINTERFACE_H_
2 #define MSP_GLTK_USERINTERFACE_H_
3
4 #include "logic.h"
5 #include "root.h"
6
7 namespace Msp {
8 namespace GLtk {
9
10 /**
11 Encapsulates a Root widget and Logic associated with it.  Allows looking up
12 widgets by name.
13 */
14 class UserInterface
15 {
16 public:
17         class Loader: public DataFile::Loader
18         {
19         private:
20                 UserInterface &ui;
21
22         public:
23                 Loader(UserInterface &);
24         private:
25                 void logic();
26                 void root();
27         };
28
29         typedef std::map<std::string, Widget *> WidgetMap;
30
31 private:
32         WidgetMap widgets;
33         Root root;
34         Logic logic;
35
36 public:
37         UserInterface(Resources &, Graphics::Window &);
38         Root &get_root() { return root; }
39         const Logic &get_logic() const { return logic; }
40
41         template<typename W>
42         W &get_widget(const std::string &n) const
43         {
44                 WidgetMap::const_iterator i = widgets.find(n);
45                 if(i==widgets.end())
46                         throw KeyError("Unknown widget", n);
47
48                 W *w = dynamic_cast<W *>(i->second);
49                 if(!w)
50                         throw Exception("Widget type mismatch");
51
52                 return *w;
53         }
54
55         template<typename W>
56         void get_widget(const std::string &n, W *&w) const
57         {
58                 w = &get_widget<W>(n);
59         }
60 };
61
62 } // namespace GLtk
63 } // namespace Msp
64
65 #endif