Merge remote-tracking branch 'origin/tizen' into new_text
[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 <list>
23 #include <dali/public-api/adaptor-framework/style-monitor.h>
24 #include <dali/public-api/common/map-wrapper.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/public-api/builder/builder.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41 /**
42  * @copydoc Toolkit::StyleManager
43  */
44 class StyleManager : public Dali::BaseObject, public ConnectionTracker
45 {
46 public:
47
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   /**
61    * @copydoc Toolkit::StyleManager::SetOrientationValue
62    */
63   void SetOrientationValue( int orientation );
64
65   /**
66    * @copydoc Toolkit::StyleManager::GetOrientationValue
67    */
68   int GetOrientationValue();
69
70   /**
71    * @copydoc Toolkit::StyleManager::SetOrientation( Orientation orientation )
72    */
73   void SetOrientation( Orientation orientation );
74
75   /**
76    * @copydoc Toolkit::StyleManager::GetOrientation
77    */
78   Orientation GetOrientation();
79
80   /**
81    * @copydoc Toolkit::StyleManager::SetStyleConstant
82    */
83   void SetStyleConstant( const std::string& key, const Property::Value& value );
84
85   /**
86    * @copydoc Toolkit::StyleManager::GetStyleConstant
87    */
88   bool GetStyleConstant( const std::string& key, Property::Value& valueOut );
89
90   /**
91    * @copydoc Toolkit::StyleManager::RequestThemeChange
92    */
93   void RequestThemeChange( const std::string& themeFile );
94
95   /**
96    * @copydoc Toolkit::StyleManager::RequestDefaultTheme
97    */
98   void RequestDefaultTheme();
99
100   /**
101    * Determine if a theme change has been requested
102    * @return Whether a theme request is pending
103    */
104   bool IsThemeRequestPending();
105
106   /**
107    * @brief Apply the theme style to a control.
108    *
109    * @param[in] control The control to apply style.
110    */
111   void ApplyThemeStyle( Toolkit::Control control );
112
113   /**
114    * @copydoc Toolkit::StyleManager::ApplyStyle
115    */
116   void ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName );
117
118 public:
119   // SIGNALS
120
121   /**
122    * @copydoc Toolkit::StyleManager::StyleChangeSignal
123    */
124   Toolkit::StyleManager::StyleChangeSignalType& StyleChangeSignal();
125
126 protected:
127
128   /**
129    * @brief Destructor
130    */
131   virtual ~StyleManager();
132
133
134 public:
135
136   /**
137    * @brief Set the current theme. Called only once per event processing cycle.
138    */
139   void SetTheme();
140
141 private:
142
143   typedef std::list<std::string> StringList;
144
145   /**
146    * @brief Internal helper method to read a file from file system.
147    * @param filename The name of the file to read.
148    * @param[out] stringOut The string to return the file in
149    *
150    * @param Return true if file was found
151    */
152   bool LoadFile(const std::string& filename, std::string& stringOut);
153
154   /**
155    * @brief Create a new builder.
156    *
157    * @param[in] constants A map of constants to be used by the builder
158    *
159    * @return Return the newly created builder
160    */
161   Toolkit::Builder CreateBuilder( const Property::Map& constants );
162
163   /**
164    * @brief Load a JSON file into given builder
165    *
166    * @param[in] builder The builder object to load the theme file
167    * @param[in] jsonFileName The name of the JSON file to load
168    * @return Return true if file was loaded
169    */
170   bool LoadJSON( Toolkit::Builder builder, const std::string& jsonFileName );
171
172   /**
173    * @brief Collect qualifiers (e.g. Landscape, portrait etc) for a given style
174    *
175    * @param[in,out] qualifiersOut The list to populate with qualifiers
176    */
177   void CollectQualifiers( StringList& qualifiersOut );
178
179   /**
180    * @brief Construct a qualified style name out of qualifiers
181    *
182    * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
183    *
184    * @param[in] styleName The root name of the style
185    * @param[in] qualifiers List of qualifier names
186    * @param[out] qualifiedStyleOut The qualified style name
187    */
188   void BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut );
189
190   /**
191    * @brief Apply a style to the control using the given builder
192    *
193    * @param[in] builder The builder to apply the style from
194    * @param[in] control The control to apply the style to
195    */
196   void ApplyStyle( Toolkit::Builder builder, Toolkit::Control control );
197
198   /**
199    * @brief Callback for orientation changes
200    *
201    * @param[in] orientation The orientation object
202    */
203   void OnOrientationChanged( Orientation orientation );
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 data
226    */
227   void StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange );
228
229   // Undefined
230   StyleManager(const StyleManager&);
231
232   StyleManager& operator=(const StyleManager& rhs);
233
234 private:
235
236   // Map to store builders keyed by JSON file name
237   typedef std::map< std::string, Toolkit::Builder > BuilderMap;
238
239   Toolkit::Builder mThemeBuilder;     ///< Builder for all default theme properties
240
241   Orientation mOrientation;           ///< Handle to application orientation object
242   int mOrientationDegrees;            ///< Directly set value of orientation
243
244   int mDefaultFontSize;               ///< Logical size, not a point-size
245
246   std::string mThemeFile;             ///< The full path of the current theme file
247
248   Property::Map mThemeBuilderConstants;   ///< Contants to give the theme builder
249   Property::Map mStyleBuilderConstants;   ///< Constants specific to building styles
250
251   BuilderMap mBuilderCache;           ///< Cache of builders keyed by JSON file name
252
253   // Signals
254   Toolkit::StyleManager::StyleChangeSignalType       mStyleChangeSignal;         ///< Emitted when the style( theme/font ) changes
255 };
256
257 } // namespace Internal
258
259 inline Internal::StyleManager& GetImpl( Dali::Toolkit::StyleManager& obj )
260 {
261   DALI_ASSERT_ALWAYS( obj );
262
263   Dali::BaseObject& handle = obj.GetBaseObject();
264
265   return static_cast< Internal::StyleManager& >( handle );
266 }
267
268 inline const Internal::StyleManager& GetImpl( const Dali::Toolkit::StyleManager& obj )
269 {
270   DALI_ASSERT_ALWAYS( obj );
271
272   const Dali::BaseObject& handle = obj.GetBaseObject();
273
274   return static_cast< const Internal::StyleManager& >( handle );
275 }
276
277 } // namespace Toolkit
278
279 } // namespace Dali
280
281 #endif // __DALI_TOOLKIT_INTERNAL_STYLE_MANAGER_H__
282