Merge "Added New methods to cater for second step initialization" 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/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     styleName = control.GetTypeName();
336   }
337
338   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
339   StringList qualifiers;
340   CollectQualifiers( qualifiers );
341
342   while( true )
343   {
344     std::string qualifiedStyleName;
345     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
346
347     // Break if style found or we have tried the root style name (qualifiers is empty)
348     if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
349     {
350       break;
351     }
352
353     // Remove the last qualifier in an attempt to find a style that is valid
354     qualifiers.pop_back();
355   }
356
357   if( mDefaultFontSize >= 0 )
358   {
359     // Apply the style for logical font size
360     std::stringstream fontSizeQualifier;
361     fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
362     builder.ApplyStyle( fontSizeQualifier.str(), control );
363   }
364 }
365
366 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
367 {
368   BuilderMap::iterator builderIt = mBuilderCache.find( key );
369   if( builderIt != mBuilderCache.end() )
370   {
371     return builderIt->second;
372   }
373
374   return Toolkit::Builder();
375 }
376
377 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
378 {
379   mBuilderCache[ key ] = builder;
380 }
381
382 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
383 {
384   switch ( styleChange )
385   {
386     case StyleChange::DEFAULT_FONT_CHANGE:
387     {
388       mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
389       break;
390     }
391
392     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
393     {
394       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
395       break;
396     }
397
398     case StyleChange::THEME_CHANGE:
399     {
400       SetTheme( styleMonitor.GetTheme() );
401       break;
402     }
403   }
404   EmitStyleChangeSignals( styleChange );
405 }
406
407 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
408 {
409   Toolkit::StyleManager styleManager = StyleManager::Get();
410
411   // Update Controls first
412   mControlStyleChangeSignal.Emit( styleManager, styleChange );
413
414   // Inform application last
415   mStyleChangedSignal.Emit( styleManager, styleChange );
416 }
417
418
419 } // namespace Internal
420
421 } // namespace Toolkit
422
423 } // namespace Dali