Updated all header files to new format
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_STYLE_MANAGER_H
2 #define DALI_TOOLKIT_INTERNAL_STYLE_MANAGER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/adaptor-framework/style-monitor.h>
22 #include <dali/devel-api/common/map-wrapper.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/object/property-map.h>
26 #include <dali/public-api/signals/connection-tracker.h>
27 #include <string>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/builder/builder.h>
31 #include <dali-toolkit/internal/builder/style.h>
32 #include <dali-toolkit/public-api/styling/style-manager.h>
33
34 namespace Dali
35 {
36 namespace Toolkit
37 {
38 namespace Internal
39 {
40 class FeedbackStyle;
41
42 /**
43  * @copydoc Toolkit::StyleManager
44  */
45 class StyleManager : public Dali::BaseObject, public ConnectionTracker
46 {
47 public:
48   /**
49    * Singleton access
50    *
51    * @return The StyleManager object
52    */
53   static Toolkit::StyleManager Get();
54
55   /**
56    * Construct a new StyleManager.
57    */
58   StyleManager();
59
60 protected:
61   /**
62    * @brief Destructor
63    */
64   virtual ~StyleManager();
65
66 public: // Public API
67   /**
68    * @copydoc Toolkit::StyleManager::ApplyTheme
69    */
70   void ApplyTheme(const std::string& themeFile);
71
72   /**
73    * @copydoc Toolkit::StyleManager::ApplyDefaultTheme
74    */
75   void ApplyDefaultTheme();
76
77   /**
78    * @copydoc Toolkit::StyleManager::GetDefaultFontFamily
79    */
80   const std::string& GetDefaultFontFamily() const;
81
82   /**
83    * @copydoc Toolkit::StyleManager::SetStyleConstant
84    */
85   void SetStyleConstant(const std::string& key, const Property::Value& value);
86
87   /**
88    * @copydoc Toolkit::StyleManager::GetStyleConstant
89    */
90   bool GetStyleConstant(const std::string& key, Property::Value& valueOut);
91
92   /**
93    * @copydoc Toolkit::StyleManager::GetConfigurations
94    */
95   const Property::Map GetConfigurations();
96
97   /**
98    * @brief Apply the theme style to a control.
99    *
100    * @param[in] control The control to apply style.
101    */
102   void ApplyThemeStyle(Toolkit::Control control);
103
104   /**
105    * @brief Apply the theme style to a control at initialization.
106    *
107    * @param[in] control The control to apply style.
108    */
109   void ApplyThemeStyleAtInit(Toolkit::Control control);
110
111   /**
112    * @copydoc Toolkit::StyleManager::ApplyStyle
113    */
114   void ApplyStyle(Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName);
115
116   /**
117    * Get the state/style information for the given control
118    * @param[in] control The control to get state information for
119    * @return The style information (or empty ptr if not found)
120    */
121   const StylePtr GetRecordedStyle(Toolkit::Control control);
122
123 public:
124   // SIGNALS
125
126   /**
127    * @copydoc Toolkit::StyleManager::StyleChangeSignal
128    * This signal is sent after all the controls have been updated
129    * due to style change
130    */
131   Toolkit::StyleManager::StyleChangedSignalType& StyleChangedSignal();
132
133   /**
134    * This signal is sent to the controls following a style change.
135    * It should not be exposed in the public API
136    */
137   Toolkit::StyleManager::StyleChangedSignalType& ControlStyleChangeSignal();
138
139 private:
140   typedef std::vector<std::string> StringList;
141
142   /**
143    * @brief Set the current theme. Called only once per event processing cycle.
144    * @param[in] themeFile The name of the theme file to read.
145    */
146   void SetTheme(const std::string& themeFile);
147
148   /**
149    * @brief Internal helper method to read a file from file system.
150    * @param filename The name of the file to read.
151    * @param[out] stringOut The string to return the file in
152    *
153    * @param Return true if file was found
154    */
155   bool LoadFile(const std::string& filename, std::string& stringOut);
156
157   /**
158    * @brief Create a new builder.
159    *
160    * @param[in] constants A map of constants to be used by the builder
161    *
162    * @return Return the newly created builder
163    */
164   Toolkit::Builder CreateBuilder(const Property::Map& constants);
165
166   /**
167    * @brief Load a JSON file into given builder
168    *
169    * @param[in] builder The builder object to load the theme file
170    * @param[in] jsonFileName The name of the JSON file to load
171    * @return Return true if file was loaded
172    */
173   bool LoadJSON(Toolkit::Builder builder, const std::string& jsonFileName);
174
175   /**
176    * @brief Apply a style to the control using the given builder
177    *
178    * @param[in] builder The builder to apply the style from
179    * @param[in] control The control to apply the style to
180    */
181   void ApplyStyle(Toolkit::Builder builder, Toolkit::Control control);
182
183   /**
184    * Search for a builder in the cache
185    *
186    * @param[in] key The key the builder was cached under
187    * @return Return the cached builder if found or an empty builder object if not found
188    */
189   Toolkit::Builder FindCachedBuilder(const std::string& key);
190
191   /**
192    * Store a given builder in the cache keyed to the given key
193    *
194    * @param[in] builder The builder object to store
195    * @param[in] key The key to store the builder under
196    */
197   void CacheBuilder(Toolkit::Builder builder, const std::string& key);
198
199   /**
200    * Callback for when style monitor raises a signal
201    *
202    * @param[in] styleMonitor The style monitor object
203    * @param[in] styleChange The style change type
204    */
205   void StyleMonitorChange(StyleMonitor styleMonitor, StyleChange::Type styleChange);
206
207   /**
208    * Emit signals to controls first, app second
209    */
210   void EmitStyleChangeSignals(StyleChange::Type styleChange);
211
212   // Undefined
213   StyleManager(const StyleManager&);
214
215   StyleManager& operator=(const StyleManager& rhs);
216
217 private:
218   // Map to store builders keyed by JSON file name
219   typedef std::map<std::string, Toolkit::Builder> BuilderMap;
220
221   Toolkit::Builder mThemeBuilder; ///< Builder for all default theme properties
222   StyleMonitor     mStyleMonitor; ///< Style monitor handle
223
224   int         mDefaultFontSize; ///< Logical size, not a point-size
225   std::string mDefaultFontFamily;
226   std::string mDefaultThemeFilePath; ///< The full path of the default theme file
227   std::string mThemeFile;            ///< The full path of the current theme file
228
229   Property::Map mThemeBuilderConstants; ///< Contants to give the theme builder
230   Property::Map mStyleBuilderConstants; ///< Constants specific to building styles
231
232   BuilderMap mBuilderCache; ///< Cache of builders keyed by JSON file name
233
234   Toolkit::Internal::FeedbackStyle* mFeedbackStyle; ///< Feedback style
235
236   // Signals
237   Toolkit::StyleManager::StyleChangedSignalType mControlStyleChangeSignal; ///< Emitted when the style( theme/font ) changes for the controls to style themselves
238   Toolkit::StyleManager::StyleChangedSignalType mStyleChangedSignal;       ///< Emitted after the controls have been styled
239 };
240
241 } // namespace Internal
242
243 inline Internal::StyleManager& GetImpl(Dali::Toolkit::StyleManager& obj)
244 {
245   DALI_ASSERT_ALWAYS(obj);
246
247   Dali::BaseObject& handle = obj.GetBaseObject();
248
249   return static_cast<Internal::StyleManager&>(handle);
250 }
251
252 inline const Internal::StyleManager& GetImpl(const Dali::Toolkit::StyleManager& obj)
253 {
254   DALI_ASSERT_ALWAYS(obj);
255
256   const Dali::BaseObject& handle = obj.GetBaseObject();
257
258   return static_cast<const Internal::StyleManager&>(handle);
259 }
260
261 } // namespace Toolkit
262
263 } // namespace Dali
264
265 #endif // DALI_TOOLKIT_INTERNAL_STYLE_MANAGER_H