[dali_2.1.25] Merge branch 'devel/master'
[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 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41 class FeedbackStyle;
42
43 /**
44  * @copydoc Toolkit::StyleManager
45  */
46 class StyleManager : public Dali::BaseObject, public ConnectionTracker
47 {
48 public:
49   /**
50    * Singleton access
51    *
52    * @return The StyleManager object
53    */
54   static Toolkit::StyleManager Get();
55
56   /**
57    * Construct a new StyleManager.
58    */
59   StyleManager();
60
61 protected:
62   /**
63    * @brief Destructor
64    */
65   virtual ~StyleManager();
66
67 public: // Public API
68   /**
69    * @copydoc Toolkit::StyleManager::ApplyTheme
70    */
71   void ApplyTheme(const std::string& themeFile);
72
73   /**
74    * @copydoc Toolkit::StyleManager::ApplyDefaultTheme
75    */
76   void ApplyDefaultTheme();
77
78   /**
79    * @copydoc Toolkit::StyleManager::GetDefaultFontFamily
80    */
81   const std::string& GetDefaultFontFamily() const;
82
83   /**
84    * @copydoc Toolkit::StyleManager::SetStyleConstant
85    */
86   void SetStyleConstant(const std::string& key, const Property::Value& value);
87
88   /**
89    * @copydoc Toolkit::StyleManager::GetStyleConstant
90    */
91   bool GetStyleConstant(const std::string& key, Property::Value& valueOut);
92
93   /**
94    * @copydoc Toolkit::StyleManager::GetConfigurations
95    */
96   const Property::Map GetConfigurations();
97
98   /**
99    * @copydoc Toolkit::DevelStyleManager::SetBrokenImageUrl
100    */
101   void SetBrokenImageUrl(DevelStyleManager::BrokenImageType brokenImageType, const std::string& brokenImageUrl);
102
103   /**
104    * @copydoc Toolkit::DevelStyleManager::GetBrokenImageUrl
105    */
106   std::string GetBrokenImageUrl(DevelStyleManager::BrokenImageType brokenImageType);
107
108   /**
109    * @copydoc Toolkit::DevelStyleManager::GetBrokenImageUrlList
110    */
111   std::vector<std::string> GetBrokenImageUrlList();
112
113   /**
114    * @brief Apply the theme style to a control.
115    *
116    * @param[in] control The control to apply style.
117    */
118   void ApplyThemeStyle(Toolkit::Control control);
119
120   /**
121    * @brief Apply the theme style to a control at initialization.
122    *
123    * @param[in] control The control to apply style.
124    */
125   void ApplyThemeStyleAtInit(Toolkit::Control control);
126
127   /**
128    * @copydoc Toolkit::StyleManager::ApplyStyle
129    */
130   void ApplyStyle(Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName);
131
132   /**
133    * Get the state/style information for the given control
134    * @param[in] control The control to get state information for
135    * @return The style information (or empty ptr if not found)
136    */
137   const StylePtr GetRecordedStyle(Toolkit::Control control);
138
139 public:
140   // SIGNALS
141
142   /**
143    * @copydoc Toolkit::StyleManager::StyleChangeSignal
144    * This signal is sent after all the controls have been updated
145    * due to style change
146    */
147   Toolkit::StyleManager::StyleChangedSignalType& StyleChangedSignal();
148
149   /**
150    * This signal is sent to the controls following a style change.
151    * It should not be exposed in the public API
152    */
153   Toolkit::StyleManager::StyleChangedSignalType& ControlStyleChangeSignal();
154
155   /**
156    * This signal is sent to the visual factory following a broken image change.
157    * It should not be exposed in the public API
158    */
159   Toolkit::DevelStyleManager::BrokenImageChangedSignalType& BrokenImageChangedSignal();
160
161 private:
162   typedef std::vector<std::string> StringList;
163
164   /**
165    * @brief Set the current theme. Called only once per event processing cycle.
166    * @param[in] themeFile The name of the theme file to read.
167    */
168   void SetTheme(const std::string& themeFile);
169
170   /**
171    * @brief Internal helper method to read a file from file system.
172    * @param filename The name of the file to read.
173    * @param[out] stringOut The string to return the file in
174    *
175    * @param Return true if file was found
176    */
177   bool LoadFile(const std::string& filename, std::string& stringOut);
178
179   /**
180    * @brief Create a new builder.
181    *
182    * @param[in] constants A map of constants to be used by the builder
183    *
184    * @return Return the newly created builder
185    */
186   Toolkit::Builder CreateBuilder(const Property::Map& constants);
187
188   /**
189    * @brief Load a JSON file into given builder
190    *
191    * @param[in] builder The builder object to load the theme file
192    * @param[in] jsonFileName The name of the JSON file to load
193    * @return Return true if file was loaded
194    */
195   bool LoadJSON(Toolkit::Builder builder, const std::string& jsonFileName);
196
197   /**
198    * @brief Apply a style to the control using the given builder
199    *
200    * @param[in] builder The builder to apply the style from
201    * @param[in] control The control to apply the style to
202    */
203   void ApplyStyle(Toolkit::Builder builder, Toolkit::Control control);
204
205   /**
206    * Search for a builder in the cache
207    *
208    * @param[in] key The key the builder was cached under
209    * @return Return the cached builder if found or an empty builder object if not found
210    */
211   Toolkit::Builder FindCachedBuilder(const std::string& key);
212
213   /**
214    * Store a given builder in the cache keyed to the given key
215    *
216    * @param[in] builder The builder object to store
217    * @param[in] key The key to store the builder under
218    */
219   void CacheBuilder(Toolkit::Builder builder, const std::string& key);
220
221   /**
222    * Callback for when style monitor raises a signal
223    *
224    * @param[in] styleMonitor The style monitor object
225    * @param[in] styleChange The style change type
226    */
227   void StyleMonitorChange(StyleMonitor styleMonitor, StyleChange::Type styleChange);
228
229   /**
230    * Emit signals to controls first, app second
231    */
232   void EmitStyleChangeSignals(StyleChange::Type styleChange);
233
234   // Undefined
235   StyleManager(const StyleManager&);
236
237   StyleManager& operator=(const StyleManager& rhs);
238
239 private:
240   // Map to store builders keyed by JSON file name
241   typedef std::map<std::string, Toolkit::Builder> BuilderMap;
242
243   Toolkit::Builder mThemeBuilder; ///< Builder for all default theme properties
244   StyleMonitor     mStyleMonitor; ///< Style monitor handle
245
246   int         mDefaultFontSize; ///< Logical size, not a point-size
247   std::string mDefaultFontFamily;
248   std::string mDefaultThemeFilePath; ///< The full path of the default theme file
249   std::string mThemeFile;            ///< The full path of the current theme file
250
251   Property::Map mThemeBuilderConstants; ///< Contants to give the theme builder
252   Property::Map mStyleBuilderConstants; ///< Constants specific to building styles
253
254   BuilderMap mBuilderCache; ///< Cache of builders keyed by JSON file name
255
256   Toolkit::Internal::FeedbackStyle* mFeedbackStyle; ///< Feedback style
257
258   std::vector<std::string> mBrokenImageUrls;    ///< Broken Image Urls received from user
259
260   // Signals
261   Toolkit::StyleManager::StyleChangedSignalType            mControlStyleChangeSignal; ///< Emitted when the style( theme/font ) changes for the controls to style themselves
262   Toolkit::StyleManager::StyleChangedSignalType            mStyleChangedSignal;       ///< Emitted after the controls have been styled
263   Toolkit::DevelStyleManager::BrokenImageChangedSignalType mBrokenImageChangedSignal; ///< Emitted after brokenImageChangedSignal
264 };
265
266 } // namespace Internal
267
268 inline Internal::StyleManager& GetImpl(Dali::Toolkit::StyleManager& obj)
269 {
270   DALI_ASSERT_ALWAYS(obj);
271
272   Dali::BaseObject& handle = obj.GetBaseObject();
273
274   return static_cast<Internal::StyleManager&>(handle);
275 }
276
277 inline const Internal::StyleManager& GetImpl(const Dali::Toolkit::StyleManager& obj)
278 {
279   DALI_ASSERT_ALWAYS(obj);
280
281   const Dali::BaseObject& handle = obj.GetBaseObject();
282
283   return static_cast<const Internal::StyleManager&>(handle);
284 }
285
286 } // namespace Toolkit
287
288 } // namespace Dali
289
290 #endif // DALI_TOOLKIT_INTERNAL_STYLE_MANAGER_H