2861611eb80c0e2085f27a94b2d562514535e9c2
[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/devel-api/object/type-registry-helper.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/control.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/devel-api/styling/style-manager.h>
30 #include <dali-toolkit/internal/feedback/feedback-style.h>
31
32 namespace
33 {
34
35 const char* LANDSCAPE_QUALIFIER = "landscape";
36 const char* PORTRAIT_QUALIFIER  = "portrait";
37 const char* FONT_SIZE_QUALIFIER = "FontSize";
38
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
40
41 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
42 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
43
44 } // namespace
45
46 namespace Dali
47 {
48
49 namespace Toolkit
50 {
51
52 namespace Internal
53 {
54
55 namespace
56 {
57
58 BaseHandle Create()
59 {
60   BaseHandle handle = StyleManager::Get();
61
62   if ( !handle )
63   {
64     SingletonService singletonService( SingletonService::Get() );
65     if ( singletonService )
66     {
67       Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
68       singletonService.Register( typeid( manager ), manager );
69       handle = manager;
70     }
71   }
72
73   return handle;
74 }
75
76 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
77 DALI_TYPE_REGISTRATION_END()
78
79 } // namespace
80
81 Toolkit::StyleManager StyleManager::Get()
82 {
83   Toolkit::StyleManager manager;
84
85   SingletonService singletonService( SingletonService::Get() );
86   if ( singletonService )
87   {
88     // Check whether the style manager is already created
89     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
90     if( handle )
91     {
92       // If so, downcast the handle of singleton
93       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
94     }
95   }
96
97   return manager;
98 }
99
100 StyleManager::StyleManager()
101 : mOrientationDegrees( 0 ),  // Portrait
102   mDefaultFontSize( -1 ),
103   mDefaultFontFamily(""),
104   mThemeFile( DEFAULT_THEME ),
105   mFeedbackStyle( NULL )
106 {
107   // Add theme builder constants
108   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
109
110   mStyleMonitor = StyleMonitor::Get();
111   if( mStyleMonitor )
112   {
113     mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
114
115     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
116   }
117
118   // Sound & haptic style
119   mFeedbackStyle = new FeedbackStyle();
120
121 }
122
123 StyleManager::~StyleManager()
124 {
125   delete mFeedbackStyle;
126 }
127
128 void StyleManager::SetOrientationValue( int orientation )
129 {
130   if( orientation !=  mOrientationDegrees )
131   {
132     mOrientationDegrees = orientation;
133     // TODO: if orientation changed, apply the new style to all controls
134     // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
135     SetTheme();
136   }
137 }
138
139 int StyleManager::GetOrientationValue()
140 {
141   return mOrientationDegrees;
142 }
143
144 void StyleManager::SetOrientation( Orientation orientation )
145 {
146   if( mOrientation )
147   {
148     mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
149   }
150
151   OnOrientationChanged( orientation );
152
153   if( mOrientation )
154   {
155     mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
156   }
157 }
158
159 std::string StyleManager::GetDefaultFontFamily() const
160 {
161   return mDefaultFontFamily;
162 }
163
164 Orientation StyleManager::GetOrientation()
165 {
166   return mOrientation;
167 }
168
169 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
170 {
171   mStyleBuilderConstants[ key ] = value;
172 }
173
174 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
175 {
176   Property::Value* value = mStyleBuilderConstants.Find( key );
177   if( value )
178   {
179     valueOut = *value;
180     return true;
181   }
182
183   return false;
184 }
185
186 void StyleManager::OnOrientationChanged( Orientation orientation )
187 {
188   mOrientation = orientation;
189   // TODO: if orientation changed, apply the new style to all controls
190   // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
191   SetTheme();
192 }
193
194 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
195 {
196   Toolkit::Builder builder = Toolkit::Builder::New();
197   builder.AddConstants( constants );
198
199   return builder;
200 }
201
202 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
203 {
204   std::string fileString;
205   if( LoadFile( jsonFilePath, fileString ) )
206   {
207     builder.LoadFromString( fileString );
208     return true;
209   }
210   else
211   {
212     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
213     return false;
214   }
215 }
216
217 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
218 {
219   // Append the relevant qualifier for orientation
220   int orientation = mOrientationDegrees;
221
222   if( mOrientation )
223   {
224     orientation = mOrientation.GetDegrees();
225   }
226
227   switch( orientation )
228   {
229     case 90:
230     case 270:
231     {
232       qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
233       break;
234     }
235     case 180:
236     case 0: // fall through
237     default:
238     {
239       qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
240       break;
241     }
242   }
243 }
244
245 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
246 {
247   qualifiedStyleOut.append( styleName );
248
249   for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
250   {
251     const std::string& str = *it;
252
253     qualifiedStyleOut.append( "-" );
254     qualifiedStyleOut.append( str );
255   }
256 }
257
258 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
259 {
260   std::string styleName = control.GetStyleName();
261
262   if( styleName.empty() )
263   {
264     // Convert control name to lower case
265     styleName = control.GetTypeName();
266     std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
267   }
268
269   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
270   StringList qualifiers;
271   CollectQualifiers( qualifiers );
272
273   while( true )
274   {
275     std::string qualifiedStyleName;
276     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
277
278     // Break if style found or we have tried the root style name (qualifiers is empty)
279     if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
280     {
281       break;
282     }
283
284     // Remove the last qualifier in an attempt to find a style that is valid
285     qualifiers.pop_back();
286   }
287
288   if( mDefaultFontSize >= 0 )
289   {
290     // Apply the style for logical font size
291     std::stringstream fontSizeQualifier;
292     fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
293     builder.ApplyStyle( fontSizeQualifier.str(), control );
294   }
295 }
296
297 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
298 {
299   if( !mThemeBuilder )
300   {
301     RequestDefaultTheme();
302   }
303
304   if( mThemeBuilder )
305   {
306     ApplyStyle( mThemeBuilder, control );
307   }
308 }
309
310 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
311 {
312   ApplyThemeStyle( control );
313
314   if(mFeedbackStyle)
315   {
316     mFeedbackStyle->ObjectCreated( control );
317   }
318 }
319
320 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
321 {
322   bool builderReady = false;
323
324   // First look in the cache
325   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
326   if( builder )
327   {
328     builderReady = true;
329   }
330   else
331   {
332     // Merge theme and style constants
333     Property::Map constants( mThemeBuilderConstants );
334     constants.Merge( mStyleBuilderConstants );
335
336     // Create it
337     builder = CreateBuilder( constants );
338
339     if( LoadJSON( builder, jsonFileName ) )
340     {
341       CacheBuilder( builder, jsonFileName );
342       builderReady = true;
343     }
344   }
345
346   // Apply the style to the control
347   if( builderReady )
348   {
349     builder.ApplyStyle( styleName, control );
350   }
351 }
352
353 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
354 {
355   DALI_ASSERT_DEBUG( 0 != filename.length());
356
357   // as toolkit is platform agnostic, it cannot load files from filesystem
358   // ask style monitor to load the style sheet
359   if( mStyleMonitor )
360   {
361     return mStyleMonitor.LoadThemeFile( filename, stringOut );
362   }
363
364   return false;
365 }
366
367 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
368 {
369   return mStyleChangeSignal;
370 }
371
372 void StyleManager::RequestThemeChange( const std::string& themeFile )
373 {
374   mThemeFile = themeFile;
375
376   // need to do style change synchronously as app might create a UI control on the next line
377   SetTheme();
378 }
379
380 void StyleManager::RequestDefaultTheme()
381 {
382   RequestThemeChange( DEFAULT_THEME );
383 }
384
385 void StyleManager::SetTheme()
386 {
387   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
388
389   if( LoadJSON( mThemeBuilder, mThemeFile ) )
390   {
391     if(mFeedbackStyle)
392     {
393       mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
394     }
395
396     mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
397   }
398   else
399   {
400     mThemeBuilder.Reset();
401   }
402 }
403
404 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
405 {
406   BuilderMap::iterator builderIt = mBuilderCache.find( key );
407   if( builderIt != mBuilderCache.end() )
408   {
409     return builderIt->second;
410   }
411
412   return Toolkit::Builder();
413 }
414
415 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
416 {
417   mBuilderCache[ key ] = builder;
418 }
419
420 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
421 {
422   switch ( styleChange )
423   {
424     case StyleChange::DEFAULT_FONT_CHANGE:
425     {
426       mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
427       break;
428     }
429
430     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
431     {
432       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
433       break;
434     }
435
436     case StyleChange::THEME_CHANGE:
437     {
438       const std::string& newTheme = styleMonitor.GetTheme();
439       if( ! newTheme.empty() )
440       {
441         mThemeFile = newTheme;
442       }
443       else
444       {
445         mThemeFile = DEFAULT_THEME;
446       }
447
448       SetTheme();
449       break;
450     }
451   }
452
453   mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
454 }
455
456 } // namespace Internal
457
458 } // namespace Toolkit
459
460 } // namespace Dali