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