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 mThemeFile( DEFAULT_THEME ),
105 mFeedbackStyle( NULL )
107 // Add theme builder constants
108 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
110 mStyleMonitor = StyleMonitor::Get();
113 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
115 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
118 // Sound & haptic style
119 mFeedbackStyle = new FeedbackStyle();
122 StyleManager::~StyleManager()
124 delete mFeedbackStyle;
127 void StyleManager::SetOrientationValue( int orientation )
129 if( orientation != mOrientationDegrees )
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
138 int StyleManager::GetOrientationValue()
140 return mOrientationDegrees;
143 void StyleManager::SetOrientation( Orientation orientation )
147 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
150 OnOrientationChanged( orientation );
154 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
158 Orientation StyleManager::GetOrientation()
163 std::string StyleManager::GetDefaultFontFamily() const
165 return mDefaultFontFamily;
168 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
170 mStyleBuilderConstants[ key ] = value;
173 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
175 Property::Value* value = mStyleBuilderConstants.Find( key );
185 void StyleManager::RequestThemeChange( const std::string& themeFile )
187 mThemeFile = themeFile;
189 // need to do style change synchronously as app might create a UI control on the next line
193 void StyleManager::RequestDefaultTheme()
195 RequestThemeChange( DEFAULT_THEME );
198 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
202 RequestDefaultTheme();
207 ApplyStyle( mThemeBuilder, control );
211 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
213 ApplyThemeStyle( control );
217 mFeedbackStyle->ObjectCreated( control );
221 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
223 bool builderReady = false;
225 // First look in the cache
226 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
233 // Merge theme and style constants
234 Property::Map constants( mThemeBuilderConstants );
235 constants.Merge( mStyleBuilderConstants );
238 builder = CreateBuilder( constants );
240 if( LoadJSON( builder, jsonFileName ) )
242 CacheBuilder( builder, jsonFileName );
247 // Apply the style to the control
250 builder.ApplyStyle( styleName, control );
254 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
256 return mStyleChangeSignal;
260 void StyleManager::SetTheme()
262 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
264 if( LoadJSON( mThemeBuilder, mThemeFile ) )
268 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
271 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
275 mThemeBuilder.Reset();
279 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
281 DALI_ASSERT_DEBUG( 0 != filename.length());
283 // as toolkit is platform agnostic, it cannot load files from filesystem
284 // ask style monitor to load the style sheet
287 return mStyleMonitor.LoadThemeFile( filename, stringOut );
293 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
295 Toolkit::Builder builder = Toolkit::Builder::New();
296 builder.AddConstants( constants );
301 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
303 std::string fileString;
304 if( LoadFile( jsonFilePath, fileString ) )
306 builder.LoadFromString( fileString );
311 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
316 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
318 // Append the relevant qualifier for orientation
319 int orientation = mOrientationDegrees;
323 orientation = mOrientation.GetDegrees();
326 switch( orientation )
331 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
335 case 0: // fall through
338 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
344 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
346 qualifiedStyleOut.append( styleName );
348 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
350 const std::string& str = *it;
352 qualifiedStyleOut.append( "-" );
353 qualifiedStyleOut.append( str );
357 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
359 std::string styleName = control.GetStyleName();
361 if( styleName.empty() )
363 // Convert control name to lower case
364 styleName = control.GetTypeName();
365 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
368 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
369 StringList qualifiers;
370 CollectQualifiers( qualifiers );
374 std::string qualifiedStyleName;
375 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
377 // Break if style found or we have tried the root style name (qualifiers is empty)
378 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
383 // Remove the last qualifier in an attempt to find a style that is valid
384 qualifiers.pop_back();
387 if( mDefaultFontSize >= 0 )
389 // Apply the style for logical font size
390 std::stringstream fontSizeQualifier;
391 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
392 builder.ApplyStyle( fontSizeQualifier.str(), control );
396 void StyleManager::OnOrientationChanged( Orientation orientation )
398 mOrientation = orientation;
399 // TODO: if orientation changed, apply the new style to all controls
400 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
405 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
407 BuilderMap::iterator builderIt = mBuilderCache.find( key );
408 if( builderIt != mBuilderCache.end() )
410 return builderIt->second;
413 return Toolkit::Builder();
416 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
418 mBuilderCache[ key ] = builder;
421 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
423 switch ( styleChange )
425 case StyleChange::DEFAULT_FONT_CHANGE:
427 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
431 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
433 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
437 case StyleChange::THEME_CHANGE:
439 const std::string& newTheme = styleMonitor.GetTheme();
440 if( ! newTheme.empty() )
442 mThemeFile = newTheme;
446 mThemeFile = DEFAULT_THEME;
454 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
457 } // namespace Internal
459 } // namespace Toolkit