]> git.tdb.fi Git - libs/gltk.git/blob - source/userinterface.h
Add various constructors to Sides (semantics inspired by CSS margins)
[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         typedef std::map<std::string, Widget *> WidgetMap;
37
38 private:
39         WidgetMap widgets;
40         Root root;
41         Logic logic;
42
43 public:
44         UserInterface(Resources &, Graphics::Window &);
45         Root &get_root() { return root; }
46         const Logic &get_logic() const { return logic; }
47
48         template<typename W>
49         W &get_widget(const std::string &n) const
50         {
51                 WidgetMap::const_iterator i = widgets.find(n);
52                 if(i==widgets.end())
53                         throw KeyError("Unknown widget", n);
54
55                 W *w = dynamic_cast<W *>(i->second);
56                 if(!w)
57                         throw Exception("Widget type mismatch");
58
59                 return *w;
60         }
61
62         template<typename W>
63         void get_widget(const std::string &n, W *&w) const
64         {
65                 w = &get_widget<W>(n);
66         }
67 };
68
69 } // namespace GLtk
70 } // namespace Msp
71
72 #endif