Add Privilege tags to dali.doxy.in
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include "style-manager-impl.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/adaptor-framework/singleton-service.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/adaptor-framework/application.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/control.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/styling/style-manager.h>
31 #include <dali-toolkit/internal/feedback/feedback-style.h>
32
33 namespace
34 {
35
36 const char* LANDSCAPE_QUALIFIER = "landscape";
37 const char* PORTRAIT_QUALIFIER  = "portrait";
38 const char* FONT_SIZE_QUALIFIER = "FontSize";
39
40 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
41
42 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
43 const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH";
44
45 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
46
47 } // namespace
48
49 namespace Dali
50 {
51
52 namespace Toolkit
53 {
54
55 namespace Internal
56 {
57
58 namespace
59 {
60
61 BaseHandle Create()
62 {
63   BaseHandle handle = StyleManager::Get();
64
65   if ( !handle )
66   {
67     SingletonService singletonService( SingletonService::Get() );
68     if ( singletonService )
69     {
70       Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
71       singletonService.Register( typeid( manager ), manager );
72       handle = manager;
73     }
74   }
75
76   return handle;
77 }
78
79 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
80 DALI_TYPE_REGISTRATION_END()
81
82 } // namespace
83
84 Toolkit::StyleManager StyleManager::Get()
85 {
86   Toolkit::StyleManager manager;
87
88   SingletonService singletonService( SingletonService::Get() );
89   if ( singletonService )
90   {
91     // Check whether the style manager is already created
92     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
93     if( handle )
94     {
95       // If so, downcast the handle of singleton
96       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
97     }
98   }
99
100   return manager;
101 }
102
103 StyleManager::StyleManager()
104 : mDefaultFontSize( -1 ),
105   mDefaultFontFamily(""),
106   mFeedbackStyle( NULL )
107 {
108   // Add theme builder constants
109   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
110   mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
111
112   mStyleMonitor = StyleMonitor::Get();
113   if( mStyleMonitor )
114   {
115     mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
116     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
117   }
118
119   // Sound & haptic style
120   mFeedbackStyle = new FeedbackStyle();
121 }
122
123 StyleManager::~StyleManager()
124 {
125   delete mFeedbackStyle;
126 }
127
128 void StyleManager::ApplyTheme( const std::string& themeFile )
129 {
130   SetTheme( themeFile );
131 }
132
133 void StyleManager::ApplyDefaultTheme()
134 {
135   std::string empty;
136   SetTheme( empty );
137 }
138
139 const std::string& StyleManager::GetDefaultFontFamily() const
140 {
141   return mDefaultFontFamily;
142 }
143
144 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
145 {
146   mStyleBuilderConstants[ key ] = value;
147 }
148
149 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
150 {
151   Property::Value* value = mStyleBuilderConstants.Find( key );
152   if( value )
153   {
154     valueOut = *value;
155     return true;
156   }
157
158   return false;
159 }
160
161 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
162 {
163   if( !mThemeBuilder )
164   {
165     ApplyDefaultTheme();
166   }
167
168   if( mThemeBuilder )
169   {
170     ApplyStyle( mThemeBuilder, control );
171   }
172 }
173
174 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
175 {
176   ApplyThemeStyle( control );
177
178   if(mFeedbackStyle)
179   {
180     mFeedbackStyle->ObjectCreated( control );
181   }
182 }
183
184 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
185 {
186   bool builderReady = false;
187
188   // First look in the cache
189   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
190   if( builder )
191   {
192     builderReady = true;
193   }
194   else
195   {
196     // Merge theme and style constants
197     Property::Map constants( mThemeBuilderConstants );
198     constants.Merge( mStyleBuilderConstants );
199
200     // Create it
201     builder = CreateBuilder( constants );
202
203     if( LoadJSON( builder, jsonFileName ) )
204     {
205       CacheBuilder( builder, jsonFileName );
206       builderReady = true;
207     }
208   }
209
210   // Apply the style to the control
211   if( builderReady )
212   {
213     builder.ApplyStyle( styleName, control );
214   }
215 }
216
217 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
218 {
219   return mStyleChangedSignal;
220 }
221
222 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
223 {
224   return mControlStyleChangeSignal;
225 }
226
227 void StyleManager::SetTheme( const std::string& themeFile )
228 {
229   bool themeLoaded = false;
230
231   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
232
233   // Always load the default theme first, then merge in the custom theme if present
234   themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
235
236   if( ! themeFile.empty() )
237   {
238     mThemeFile = themeFile;
239     themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
240   }
241
242   if( themeLoaded )
243   {
244     if(mFeedbackStyle)
245     {
246       mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
247     }
248
249     EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
250   }
251   else
252   {
253     mThemeBuilder.Reset();
254   }
255 }
256
257 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
258 {
259   DALI_ASSERT_DEBUG( 0 != filename.length());
260
261   // as toolkit is platform agnostic, it cannot load files from filesystem
262   // ask style monitor to load the style sheet
263   if( mStyleMonitor )
264   {
265     return mStyleMonitor.LoadThemeFile( filename, stringOut );
266   }
267
268   return false;
269 }
270
271 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
272 {
273   Toolkit::Builder builder = Toolkit::Builder::New();
274   builder.AddConstants( constants );
275
276   return builder;
277 }
278
279 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
280 {
281   std::string fileString;
282   if( LoadFile( jsonFilePath, fileString ) )
283   {
284     builder.LoadFromString( fileString );
285     return true;
286   }
287   else
288   {
289     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
290     return false;
291   }
292 }
293
294 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
295 {
296   // Append the relevant qualifier for orientation
297   int orientation = 0; // Get the orientation from the system
298   switch( orientation )
299   {
300     case 90:
301     case 270:
302     {
303       qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
304       break;
305     }
306     case 180:
307     case 0: // fall through
308     default:
309     {
310       qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
311       break;
312     }
313   }
314 }
315
316 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
317 {
318   qualifiedStyleOut.append( styleName );
319
320   for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
321   {
322     const std::string& str = *it;
323
324     qualifiedStyleOut.append( "-" );
325     qualifiedStyleOut.append( str );
326   }
327 }
328
329 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
330 {
331   std::string styleName = control.GetStyleName();
332
333   if( styleName.empty() )
334   {
335     // Convert control name to lower case
336     styleName = control.GetTypeName();
337     std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
338   }
339
340   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
341   StringList qualifiers;
342   CollectQualifiers( qualifiers );
343
344   while( true )
345   {
346     std::string qualifiedStyleName;
347     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
348
349     // Break if style found or we have tried the root style name (qualifiers is empty)
350     if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
351     {
352       break;
353     }
354
355     // Remove the last qualifier in an attempt to find a style that is valid
356     qualifiers.pop_back();
357   }
358
359   if( mDefaultFontSize >= 0 )
360   {
361     // Apply the style for logical font size
362     std::stringstream fontSizeQualifier;
363     fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
364     builder.ApplyStyle( fontSizeQualifier.str(), control );
365   }
366 }
367
368 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
369 {
370   BuilderMap::iterator builderIt = mBuilderCache.find( key );
371   if( builderIt != mBuilderCache.end() )
372   {
373     return builderIt->second;
374   }
375
376   return Toolkit::Builder();
377 }
378
379 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
380 {
381   mBuilderCache[ key ] = builder;
382 }
383
384 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
385 {
386   switch ( styleChange )
387   {
388     case StyleChange::DEFAULT_FONT_CHANGE:
389     {
390       mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
391       break;
392     }
393
394     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
395     {
396       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
397       break;
398     }
399
400     case StyleChange::THEME_CHANGE:
401     {
402       SetTheme( styleMonitor.GetTheme() );
403       break;
404     }
405   }
406   EmitStyleChangeSignals( styleChange );
407 }
408
409 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
410 {
411   Toolkit::StyleManager styleManager = StyleManager::Get();
412
413   // Update Controls first
414   mControlStyleChangeSignal.Emit( styleManager, styleChange );
415
416   // Inform application last
417   mStyleChangedSignal.Emit( styleManager, styleChange );
418 }
419
420
421 } // namespace Internal
422
423 } // namespace Toolkit
424
425 } // namespace Dali