TGUI  v0.7.5
Theme.hpp
1 //
3 // TGUI - Texus's Graphical User Interface
4 // Copyright (C) 2012-2017 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_THEME_HPP
27 #define TGUI_THEME_HPP
28 
29 
30 #include <TGUI/Widget.hpp>
31 #include <TGUI/Loading/WidgetConverter.hpp>
32 #include <TGUI/Loading/ThemeLoader.hpp>
33 
35 
36 namespace tgui
37 {
42  class TGUI_API BaseTheme : public std::enable_shared_from_this<BaseTheme>
43  {
44  public:
45 
50  virtual ~BaseTheme() = default;
51 
52 
59  virtual void widgetAttached(Widget* widget);
60 
61 
68  virtual void widgetDetached(Widget* widget);
69 
70 
78  virtual WidgetConverter internalLoad(const std::string& primary, const std::string& secondary) = 0;
79 
80 
89  virtual void initWidget(Widget* widget, std::string primary, std::string secondary) = 0;
90 
91 
99  static void setConstructFunction(const std::string& type, const std::function<Widget::Ptr()>& constructor);
100 
101 
108  static void setThemeLoader(const std::shared_ptr<BaseThemeLoader>& themeLoader);
109 
110 
112  protected:
113 
115  // Calls the protected widget->reload(primary, secondary, force) function
117  void widgetReload(Widget* widget, const std::string& primary = "", const std::string& secondary = "", bool force = false);
118 
119 
121  protected:
122  static std::map<std::string, std::function<Widget::Ptr()>> m_constructors;
123  static std::shared_ptr<BaseThemeLoader> m_themeLoader;
124  };
125 
126 
131  class TGUI_API Theme : public BaseTheme
132  {
133  public:
134 
135  typedef std::shared_ptr<Theme> Ptr;
136 
137 
146  Theme(const std::string& filename = "");
147 
148 
155  static Theme::Ptr create(const std::string& filename = "");
156 
157 
167  WidgetConverter load(std::string className);
168 
169 
181  void reload(const std::string& filename);
182 
183 
194  void reload(std::string oldClassName, std::string newClassName);
195 
196 
207  void reload(Widget::Ptr widget, std::string className);
208 
209 
220  void setProperty(std::string className, const std::string& property, const std::string& value);
221 
222 
234  void setProperty(std::string className, const std::string& property, ObjectConverter&& value);
235 
236 
246  std::string getProperty(std::string className, std::string property) const;
247 
248 
257  std::map<std::string, std::string> getPropertyValuePairs(std::string className) const;
258 
259 
266  std::shared_ptr<Theme> clone() const;
267 
268 
275  virtual void widgetDetached(Widget* widget) override;
276 
277 
285  virtual WidgetConverter internalLoad(const std::string& filename, const std::string& className) override;
286 
287 
296  virtual void initWidget(Widget* widget, std::string filename, std::string className) override;
297 
298 
300  private:
301  std::string m_filename;
302  std::string m_resourcePath;
303  bool m_resourcePathLock = false;
304  std::map<Widget*, std::string> m_widgets; // Map widget to class name
305  std::map<std::string, std::string> m_widgetTypes; // Map class name to type
306  std::map<std::string, std::map<std::string, std::string>> m_widgetProperties; // Map class name to property-value pairs
307 
308  friend class ThemeTest;
309  };
310 
312 }
313 
315 
316 #endif // TGUI_THEME_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
static std::map< std::string, std::function< Widget::Ptr()> > m_constructors
Widget creator functions.
Definition: Theme.hpp:122
The parent class for every widget.
Definition: Widget.hpp:71
std::shared_ptr< Theme > Ptr
Shared theme pointer.
Definition: Theme.hpp:135
Default Theme class.
Definition: Theme.hpp:131
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
Implicit converter for widgets returned by the load function of the theme.
Definition: WidgetConverter.hpp:40
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Base class for the Theme classes.
Definition: Theme.hpp:42
static std::shared_ptr< BaseThemeLoader > m_themeLoader
Theme loading functions, they read the theme file.
Definition: Theme.hpp:123