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