Merge "DALi Version 1.2.49" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.cpp
1 /*
2  * Copyright (c) 2016 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/internal/builder/builder-impl.h>
29 #include <dali-toolkit/public-api/controls/control.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali-toolkit/public-api/styling/style-manager.h>
32 #include <dali-toolkit/internal/feedback/feedback-style.h>
33
34 namespace
35 {
36
37 //const char* LANDSCAPE_QUALIFIER = "landscape";
38 const char* PORTRAIT_QUALIFIER  = "portrait";
39 const char* FONT_SIZE_QUALIFIER = "fontsize";
40
41 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
42
43 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
44 const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH";
45
46 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
47
48 } // namespace
49
50 namespace Dali
51 {
52
53 namespace Toolkit
54 {
55
56 namespace Internal
57 {
58
59 namespace
60 {
61
62 BaseHandle Create()
63 {
64   BaseHandle handle = StyleManager::Get();
65
66   if ( !handle )
67   {
68     SingletonService singletonService( SingletonService::Get() );
69     if ( singletonService )
70     {
71       Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
72       singletonService.Register( typeid( manager ), manager );
73       handle = manager;
74     }
75   }
76
77   return handle;
78 }
79
80 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
81 DALI_TYPE_REGISTRATION_END()
82
83 } // namespace
84
85 Toolkit::StyleManager StyleManager::Get()
86 {
87   Toolkit::StyleManager manager;
88
89   SingletonService singletonService( SingletonService::Get() );
90   if ( singletonService )
91   {
92     // Check whether the style manager is already created
93     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
94     if( handle )
95     {
96       // If so, downcast the handle of singleton
97       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
98     }
99   }
100
101   return manager;
102 }
103
104 StyleManager::StyleManager()
105 : mDefaultFontSize( -1 ),
106   mDefaultFontFamily(""),
107   mFeedbackStyle( NULL )
108 {
109   // Add theme builder constants
110   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
111   mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
112
113   mStyleMonitor = StyleMonitor::Get();
114   if( mStyleMonitor )
115   {
116     mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
117     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
118   }
119
120   // Sound & haptic style
121   mFeedbackStyle = new FeedbackStyle();
122 }
123
124 StyleManager::~StyleManager()
125 {
126   delete mFeedbackStyle;
127 }
128
129 void StyleManager::ApplyTheme( const std::string& themeFile )
130 {
131   SetTheme( themeFile );
132 }
133
134 void StyleManager::ApplyDefaultTheme()
135 {
136   SetTheme( DEFAULT_THEME );
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   if( mThemeFile.compare(DEFAULT_THEME) == 0 && mThemeBuilder )
232   {
233     // We have already loaded the default theme into mThemeBuilder
234   }
235   else
236   {
237     // Reload the default theme
238     mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
239     themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
240   }
241
242   if( themeFile.compare(DEFAULT_THEME) != 0 )
243   {
244     // The theme is different to the default: Merge it
245     themeLoaded = LoadJSON( mThemeBuilder, themeFile );
246     mThemeFile = themeFile;
247   }
248
249   if( themeLoaded )
250   {
251     if(mFeedbackStyle)
252     {
253       mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
254     }
255
256     EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
257   }
258   else
259   {
260     mThemeBuilder.Reset();
261   }
262 }
263
264 const Property::Map StyleManager::GetConfigurations()
265 {
266   Property::Map result;
267   if( mThemeBuilder )
268   {
269     result = mThemeBuilder.GetConfigurations();
270   }
271   else
272   {
273     bool themeLoaded = false;
274
275     mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
276
277     // Load default theme because this is first try to load stylesheet.
278     themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
279     mThemeFile = DEFAULT_THEME;
280
281     if( themeLoaded )
282     {
283       result = mThemeBuilder.GetConfigurations();
284     }
285   }
286
287   return result;
288 }
289
290 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
291 {
292   DALI_ASSERT_DEBUG( 0 != filename.length());
293
294   // as toolkit is platform agnostic, it cannot load files from filesystem
295   // ask style monitor to load the style sheet
296   if( mStyleMonitor )
297   {
298     return mStyleMonitor.LoadThemeFile( filename, stringOut );
299   }
300
301   return false;
302 }
303
304 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
305 {
306   Toolkit::Builder builder = Toolkit::Builder::New();
307   builder.AddConstants( constants );
308
309   return builder;
310 }
311
312 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
313 {
314   std::string fileString;
315   if( LoadFile( jsonFilePath, fileString ) )
316   {
317     builder.LoadFromString( fileString );
318     return true;
319   }
320   else
321   {
322     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
323     return false;
324   }
325 }
326
327 static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
328 {
329   // Append the relevant qualifier for orientation
330   // int orientation = 0; // Get the orientation from the system
331   /*
332   //// To Do /////
333   Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
334   orientation  0, 180 : PORTRAIT_QUALIFIER (default)
335   orientation 90, 270 : LANDSCAPE_QUALIFIER
336   */
337
338   qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
339
340 }
341
342 /**
343  * @brief Construct a qualified style name out of qualifiers
344  *
345  * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
346  *
347  * @param[in] styleName The root name of the style
348  * @param[in] qualifiers List of qualifier names
349  * @param[out] qualifiedStyleOut The qualified style name
350  */
351 static void BuildQualifiedStyleName(
352   const std::string& styleName,
353   const std::vector<std::string>& qualifiers,
354   std::string& qualifiedStyleOut )
355 {
356   qualifiedStyleOut.append( styleName );
357
358   for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
359          itEnd = qualifiers.end(); it != itEnd; ++it )
360   {
361     const std::string& str = *it;
362
363     qualifiedStyleOut.append( "-" );
364     qualifiedStyleOut.append( str );
365   }
366 }
367
368 static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
369 {
370   styleName = control.GetStyleName();
371
372   if( styleName.empty() )
373   {
374     styleName = control.GetTypeName();
375   }
376
377   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
378   std::vector<std::string> qualifiers;
379   CollectQualifiers( qualifiers );
380
381   bool found = 0;
382   std::string qualifiedStyleName;
383   do
384   {
385     qualifiedStyleName.clear();
386     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
387
388     // Break if style found or we have tried the root style name (qualifiers is empty)
389     if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
390     {
391       found = true;
392       break;
393     }
394     if( qualifiers.size() == 0 )
395     {
396       break;
397     }
398     // Remove the last qualifier in an attempt to find a style that is valid
399     qualifiers.pop_back();
400   } while (!found);
401
402   if(found)
403   {
404     styleName = qualifiedStyleName;
405   }
406   return found;
407 }
408
409 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
410 {
411   std::string styleName = control.GetStyleName();
412   if( GetStyleNameForControl( builder, control, styleName ) )
413   {
414     builder.ApplyStyle( styleName, control );
415   }
416
417   if( mDefaultFontSize >= 0 )
418   {
419     // Apply the style for logical font size
420     std::stringstream fontSizeQualifier;
421     fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
422     builder.ApplyStyle( fontSizeQualifier.str(), control );
423   }
424 }
425
426 const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control )
427 {
428   if( mThemeBuilder )
429   {
430     std::string styleName = control.GetStyleName();
431
432     if( GetStyleNameForControl( mThemeBuilder, control, styleName ) )
433     {
434       const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName );
435       return style;
436     }
437   }
438   return StylePtr(NULL);
439 }
440
441 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
442 {
443   BuilderMap::iterator builderIt = mBuilderCache.find( key );
444   if( builderIt != mBuilderCache.end() )
445   {
446     return builderIt->second;
447   }
448
449   return Toolkit::Builder();
450 }
451
452 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
453 {
454   mBuilderCache[ key ] = builder;
455 }
456
457 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
458 {
459   switch ( styleChange )
460   {
461     case StyleChange::DEFAULT_FONT_CHANGE:
462     {
463       mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
464       break;
465     }
466
467     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
468     {
469       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
470       break;
471     }
472
473     case StyleChange::THEME_CHANGE:
474     {
475       SetTheme( styleMonitor.GetTheme() );
476       break;
477     }
478   }
479   EmitStyleChangeSignals( styleChange );
480 }
481
482 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
483 {
484   Toolkit::StyleManager styleManager = StyleManager::Get();
485
486   // Update Controls first
487   mControlStyleChangeSignal.Emit( styleManager, styleChange );
488
489   // Inform application last
490   mStyleChangedSignal.Emit( styleManager, styleChange );
491 }
492
493
494 } // namespace Internal
495
496 } // namespace Toolkit
497
498 } // namespace Dali