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