TGUI  0.7.8
Theme.hpp
1
2//
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
36namespace 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 bool m_resourcePathLock = false;
303 std::map<Widget*, std::string> m_widgets; // Map widget to class name
304 std::map<std::string, std::string> m_widgetTypes; // Map class name to type
305 std::map<std::string, std::map<std::string, std::string>> m_widgetProperties; // Map class name to property-value pairs
306
307 friend class ThemeTest;
308 };
309
311}
312
314
315#endif // TGUI_THEME_HPP
Base class for the Theme classes.
Definition: Theme.hpp:43
static std::map< std::string, std::function< Widget::Ptr()> > m_constructors
Widget creator functions.
Definition: Theme.hpp:122
virtual void initWidget(Widget *widget, std::string primary, std::string secondary)=0
Virtual function called by the widget to finish its initialization.
virtual ~BaseTheme()=default
Virtual destructor.
virtual WidgetConverter internalLoad(const std::string &primary, const std::string &secondary)=0
This function can be used inside a widget to load other widgets without access to the derived theme c...
static std::shared_ptr< BaseThemeLoader > m_themeLoader
Theme loading functions, they read the theme file.
Definition: Theme.hpp:123
static void setConstructFunction(const std::string &type, const std::function< Widget::Ptr()> &constructor)
Changes the construct function of a specific widget type.
virtual void widgetAttached(Widget *widget)
Virtual function that gets called when a widget is connected to this theme.
virtual void widgetDetached(Widget *widget)
Virtual function that gets called when a widget is disconnected from this theme.
static void setThemeLoader(const std::shared_ptr< BaseThemeLoader > &themeLoader)
Change the function that will load the widget theme data.
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
Default Theme class.
Definition: Theme.hpp:132
virtual void widgetDetached(Widget *widget) override
Function that gets called when a widget is disconnected from this theme.
Theme(const std::string &filename="")
Construct the theme class, with an optional theme file to load.
virtual void initWidget(Widget *widget, std::string filename, std::string className) override
Function called by the widget to finish its initialization.
void reload(const std::string &filename)
Reload the theme with a different filename.
WidgetConverter load(std::string className)
Load the widget from the theme.
std::shared_ptr< Theme > Ptr
Shared theme pointer.
Definition: Theme.hpp:135
static Theme::Ptr create(const std::string &filename="")
Creates a new theme instance.
void setProperty(std::string className, const std::string &property, const std::string &value)
Change a property of the renderer of all widgets that were loaded with a certain class name.
void setProperty(std::string className, const std::string &property, ObjectConverter &&value)
Change a property of the renderer of all widgets that were loaded with a certain class name.
std::string getProperty(std::string className, std::string property) const
Retrieve the serialized value of a certain property of widgets that use a certain class name.
void reload(Widget::Ptr widget, std::string className)
Reload a specific widget.
void reload(std::string oldClassName, std::string newClassName)
Reload all widgets that were loaded with a certain class name.
std::map< std::string, std::string > getPropertyValuePairs(std::string className) const
Get a map with all properties and their values from widgets loaded with a certain class name.
virtual WidgetConverter internalLoad(const std::string &filename, const std::string &className) override
This function can be used inside a widget to load other widgets without access to the derived theme c...
std::shared_ptr< Theme > clone() const
Clone the theme without its connected widgets.
Implicit converter for widgets returned by the load function of the theme.
Definition: WidgetConverter.hpp:41
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34