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 = "font-size-";
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 mThemeFile( DEFAULT_THEME ),
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();
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 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
165 mStyleBuilderConstants[ key ] = value;
168 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
170 Property::Value* value = mStyleBuilderConstants.Find( key );
180 void StyleManager::OnOrientationChanged( Orientation orientation )
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
188 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
190 Toolkit::Builder builder = Toolkit::Builder::New();
191 builder.AddConstants( constants );
196 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
198 std::string fileString;
199 if( LoadFile( jsonFilePath, fileString ) )
201 builder.LoadFromString( fileString );
206 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
211 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
213 // Append the relevant qualifier for orientation
214 int orientation = mOrientationDegrees;
218 orientation = mOrientation.GetDegrees();
221 switch( orientation )
226 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
230 case 0: // fall through
233 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
239 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
241 qualifiedStyleOut.append( styleName );
243 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
245 const std::string& str = *it;
247 qualifiedStyleOut.append( "-" );
248 qualifiedStyleOut.append( str );
252 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
254 std::string styleName = control.GetStyleName();
256 if( styleName.empty() )
258 // Convert control name to lower case
259 styleName = control.GetTypeName();
260 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
263 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
264 StringList qualifiers;
265 CollectQualifiers( qualifiers );
269 std::string qualifiedStyleName;
270 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
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 )
278 // Remove the last qualifier in an attempt to find a style that is valid
279 qualifiers.pop_back();
282 if( mDefaultFontSize >= 0 )
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 );
291 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
295 RequestDefaultTheme();
300 ApplyStyle( mThemeBuilder, control );
304 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
308 ApplyStyle( mThemeBuilder, control );
313 mFeedbackStyle->ObjectCreated( control );
317 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
319 bool builderReady = false;
321 // First look in the cache
322 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
329 // Merge theme and style constants
330 Property::Map constants( mThemeBuilderConstants );
331 constants.Merge( mStyleBuilderConstants );
334 builder = CreateBuilder( constants );
336 if( LoadJSON( builder, jsonFileName ) )
338 CacheBuilder( builder, jsonFileName );
343 // Apply the style to the control
346 builder.ApplyStyle( styleName, control );
350 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
352 DALI_ASSERT_DEBUG( 0 != filename.length());
354 // as toolkit is platform agnostic, it cannot load files from filesystem
355 // ask style monitor to load the style sheet
358 return mStyleMonitor.LoadThemeFile( filename, stringOut );
364 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
366 return mStyleChangeSignal;
369 void StyleManager::RequestThemeChange( const std::string& themeFile )
371 mThemeFile = themeFile;
373 // need to do style change synchronously as app might create a UI control on the next line
377 void StyleManager::RequestDefaultTheme()
379 RequestThemeChange( DEFAULT_THEME );
382 void StyleManager::SetTheme()
384 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
386 if( LoadJSON( mThemeBuilder, mThemeFile ) )
390 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
393 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
397 mThemeBuilder.Reset();
401 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
403 BuilderMap::iterator builderIt = mBuilderCache.find( key );
404 if( builderIt != mBuilderCache.end() )
406 return builderIt->second;
409 return Toolkit::Builder();
412 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
414 mBuilderCache[ key ] = builder;
417 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
419 switch ( styleChange )
421 case StyleChange::DEFAULT_FONT_CHANGE:
426 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
428 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
432 case StyleChange::THEME_CHANGE:
434 const std::string& newTheme = styleMonitor.GetTheme();
435 if( ! newTheme.empty() )
437 mThemeFile = newTheme;
441 mThemeFile = DEFAULT_THEME;
449 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
452 } // namespace Internal
454 } // namespace Toolkit