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