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::StyleChangeSignalType& StyleManager::StyleChangeSignal()
253 return mStyleChangeSignal;
256 void StyleManager::SetTheme( const std::string& themeFile )
258 bool themeLoaded = false;
260 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
262 // Always load the default theme first, then merge in the custom theme if present
263 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
265 if( ! themeFile.empty() )
267 mThemeFile = themeFile;
268 themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
275 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
278 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
282 mThemeBuilder.Reset();
286 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
288 DALI_ASSERT_DEBUG( 0 != filename.length());
290 // as toolkit is platform agnostic, it cannot load files from filesystem
291 // ask style monitor to load the style sheet
294 return mStyleMonitor.LoadThemeFile( filename, stringOut );
300 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
302 Toolkit::Builder builder = Toolkit::Builder::New();
303 builder.AddConstants( constants );
308 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
310 std::string fileString;
311 if( LoadFile( jsonFilePath, fileString ) )
313 builder.LoadFromString( fileString );
318 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
323 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
325 // Append the relevant qualifier for orientation
326 int orientation = mOrientationDegrees;
330 orientation = mOrientation.GetDegrees();
333 switch( orientation )
338 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
342 case 0: // fall through
345 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
351 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
353 qualifiedStyleOut.append( styleName );
355 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
357 const std::string& str = *it;
359 qualifiedStyleOut.append( "-" );
360 qualifiedStyleOut.append( str );
364 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
366 std::string styleName = control.GetStyleName();
368 if( styleName.empty() )
370 // Convert control name to lower case
371 styleName = control.GetTypeName();
372 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
375 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
376 StringList qualifiers;
377 CollectQualifiers( qualifiers );
381 std::string qualifiedStyleName;
382 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
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 )
390 // Remove the last qualifier in an attempt to find a style that is valid
391 qualifiers.pop_back();
394 if( mDefaultFontSize >= 0 )
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 );
403 void StyleManager::OnOrientationChanged( Orientation orientation )
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 );
412 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
414 BuilderMap::iterator builderIt = mBuilderCache.find( key );
415 if( builderIt != mBuilderCache.end() )
417 return builderIt->second;
420 return Toolkit::Builder();
423 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
425 mBuilderCache[ key ] = builder;
428 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
430 switch ( styleChange )
432 case StyleChange::DEFAULT_FONT_CHANGE:
434 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
438 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
440 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
444 case StyleChange::THEME_CHANGE:
446 SetTheme( styleMonitor.GetTheme() );
451 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
454 } // namespace Internal
456 } // namespace Toolkit