2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "style-manager-impl.h"
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>
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>
35 const char* LANDSCAPE_QUALIFIER = "landscape";
36 const char* PORTRAIT_QUALIFIER = "portrait";
37 const char* FONT_SIZE_QUALIFIER = "FontSize";
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
41 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
42 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
60 BaseHandle handle = StyleManager::Get();
64 SingletonService singletonService( SingletonService::Get() );
65 if ( singletonService )
67 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
68 singletonService.Register( typeid( manager ), manager );
76 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
77 DALI_TYPE_REGISTRATION_END()
81 Toolkit::StyleManager StyleManager::Get()
83 Toolkit::StyleManager manager;
85 SingletonService singletonService( SingletonService::Get() );
86 if ( singletonService )
88 // Check whether the style manager is already created
89 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
92 // If so, downcast the handle of singleton
93 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
100 StyleManager::StyleManager()
101 : mOrientationDegrees( 0 ), // Portrait
102 mDefaultFontSize( -1 ),
103 mDefaultFontFamily(""),
104 mFeedbackStyle( NULL )
106 // Add theme builder constants
107 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
109 mStyleMonitor = StyleMonitor::Get();
112 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
114 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
117 // Sound & haptic style
118 mFeedbackStyle = new FeedbackStyle();
121 StyleManager::~StyleManager()
123 delete mFeedbackStyle;
126 void StyleManager::SetOrientationValue( int orientation )
128 if( orientation != mOrientationDegrees )
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 );
137 int StyleManager::GetOrientationValue()
139 return mOrientationDegrees;
142 void StyleManager::SetOrientation( Orientation orientation )
146 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
149 OnOrientationChanged( orientation );
153 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
157 Orientation StyleManager::GetOrientation()
162 std::string StyleManager::GetDefaultFontFamily() const
164 return mDefaultFontFamily;
167 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
169 mStyleBuilderConstants[ key ] = value;
172 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
174 Property::Value* value = mStyleBuilderConstants.Find( key );
184 void StyleManager::RequestThemeChange( const std::string& themeFile )
186 SetTheme( themeFile );
189 void StyleManager::RequestDefaultTheme()
195 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
199 RequestDefaultTheme();
204 ApplyStyle( mThemeBuilder, control );
208 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
210 ApplyThemeStyle( control );
214 mFeedbackStyle->ObjectCreated( control );
218 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
220 bool builderReady = false;
222 // First look in the cache
223 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
230 // Merge theme and style constants
231 Property::Map constants( mThemeBuilderConstants );
232 constants.Merge( mStyleBuilderConstants );
235 builder = CreateBuilder( constants );
237 if( LoadJSON( builder, jsonFileName ) )
239 CacheBuilder( builder, jsonFileName );
244 // Apply the style to the control
247 builder.ApplyStyle( styleName, control );
251 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
253 return mStyleChangedSignal;
256 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
258 return mControlStyleChangeSignal;
261 void StyleManager::SetTheme( const std::string& themeFile )
263 bool themeLoaded = false;
265 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
267 // Always load the default theme first, then merge in the custom theme if present
268 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
270 if( ! themeFile.empty() )
272 mThemeFile = themeFile;
273 themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
280 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
283 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
287 mThemeBuilder.Reset();
291 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
293 DALI_ASSERT_DEBUG( 0 != filename.length());
295 // as toolkit is platform agnostic, it cannot load files from filesystem
296 // ask style monitor to load the style sheet
299 return mStyleMonitor.LoadThemeFile( filename, stringOut );
305 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
307 Toolkit::Builder builder = Toolkit::Builder::New();
308 builder.AddConstants( constants );
313 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
315 std::string fileString;
316 if( LoadFile( jsonFilePath, fileString ) )
318 builder.LoadFromString( fileString );
323 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
328 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
330 // Append the relevant qualifier for orientation
331 int orientation = mOrientationDegrees;
335 orientation = mOrientation.GetDegrees();
338 switch( orientation )
343 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
347 case 0: // fall through
350 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
356 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
358 qualifiedStyleOut.append( styleName );
360 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
362 const std::string& str = *it;
364 qualifiedStyleOut.append( "-" );
365 qualifiedStyleOut.append( str );
369 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
371 std::string styleName = control.GetStyleName();
373 if( styleName.empty() )
375 // Convert control name to lower case
376 styleName = control.GetTypeName();
377 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
380 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
381 StringList qualifiers;
382 CollectQualifiers( qualifiers );
386 std::string qualifiedStyleName;
387 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
389 // Break if style found or we have tried the root style name (qualifiers is empty)
390 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
395 // Remove the last qualifier in an attempt to find a style that is valid
396 qualifiers.pop_back();
399 if( mDefaultFontSize >= 0 )
401 // Apply the style for logical font size
402 std::stringstream fontSizeQualifier;
403 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
404 builder.ApplyStyle( fontSizeQualifier.str(), control );
408 void StyleManager::OnOrientationChanged( Orientation orientation )
410 mOrientation = orientation;
411 // TODO: if orientation changed, apply the new style to all controls
412 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
413 SetTheme( mThemeFile );
417 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
419 BuilderMap::iterator builderIt = mBuilderCache.find( key );
420 if( builderIt != mBuilderCache.end() )
422 return builderIt->second;
425 return Toolkit::Builder();
428 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
430 mBuilderCache[ key ] = builder;
433 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
435 switch ( styleChange )
437 case StyleChange::DEFAULT_FONT_CHANGE:
439 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
443 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
445 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
449 case StyleChange::THEME_CHANGE:
451 SetTheme( styleMonitor.GetTheme() );
455 EmitStyleChangeSignals( styleChange );
458 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
460 Toolkit::StyleManager styleManager = StyleManager::Get();
462 // Update Controls first
463 mControlStyleChangeSignal.Emit( styleManager, styleChange );
465 // Inform application last
466 mStyleChangedSignal.Emit( styleManager, styleChange );
470 } // namespace Internal
472 } // namespace Toolkit