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>
34 const char* LANDSCAPE_QUALIFIER = "landscape";
35 const char* PORTRAIT_QUALIFIER = "portrait";
36 const char* FONT_SIZE_QUALIFIER = "font-size-";
38 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
40 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
41 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
59 BaseHandle handle = StyleManager::Get();
63 SingletonService singletonService( SingletonService::Get() );
64 if ( singletonService )
66 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
67 singletonService.Register( typeid( manager ), manager );
75 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
76 DALI_TYPE_REGISTRATION_END()
80 Toolkit::StyleManager StyleManager::Get()
82 Toolkit::StyleManager manager;
84 SingletonService singletonService( SingletonService::Get() );
85 if ( singletonService )
87 // Check whether the style manager is already created
88 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
91 // If so, downcast the handle of singleton
92 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
99 StyleManager::StyleManager()
100 : mOrientationDegrees( 0 ), // Portrait
101 mDefaultFontSize( -1 ),
102 mThemeFile( DEFAULT_THEME )
104 // Add theme builder constants
105 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
107 mStyleMonitor = StyleMonitor::Get();
110 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
112 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
116 StyleManager::~StyleManager()
120 void StyleManager::SetOrientationValue( int orientation )
122 if( orientation != mOrientationDegrees )
124 mOrientationDegrees = orientation;
125 // TODO: if orientation changed, apply the new style to all controls
126 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
131 int StyleManager::GetOrientationValue()
133 return mOrientationDegrees;
136 void StyleManager::SetOrientation( Orientation orientation )
140 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
143 OnOrientationChanged( orientation );
147 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
151 Orientation StyleManager::GetOrientation()
156 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
158 mStyleBuilderConstants[ key ] = value;
161 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
163 Property::Value* value = mStyleBuilderConstants.Find( key );
173 void StyleManager::OnOrientationChanged( Orientation orientation )
175 mOrientation = orientation;
176 // TODO: if orientation changed, apply the new style to all controls
177 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
181 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
183 Toolkit::Builder builder = Toolkit::Builder::New();
184 builder.AddConstants( constants );
189 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
191 std::string fileString;
192 if( LoadFile( jsonFilePath, fileString ) )
194 builder.LoadFromString( fileString );
199 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
204 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
206 // Append the relevant qualifier for orientation
207 int orientation = mOrientationDegrees;
211 orientation = mOrientation.GetDegrees();
214 switch( orientation )
219 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
223 case 0: // fall through
226 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
232 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
234 qualifiedStyleOut.append( styleName );
236 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
238 const std::string& str = *it;
240 qualifiedStyleOut.append( "-" );
241 qualifiedStyleOut.append( str );
245 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
247 std::string styleName = control.GetStyleName();
248 if( styleName.empty() )
250 // Convert control name to lower case
251 styleName = control.GetTypeName();
252 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
255 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
256 StringList qualifiers;
257 CollectQualifiers( qualifiers );
261 std::string qualifiedStyleName;
262 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
264 // Break if style found or we have tried the root style name (qualifiers is empty)
265 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
270 // Remove the last qualifier in an attempt to find a style that is valid
271 qualifiers.pop_back();
274 if( mDefaultFontSize >= 0 )
276 // Apply the style for logical font size
277 std::stringstream fontSizeQualifier;
278 fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
279 builder.ApplyStyle( fontSizeQualifier.str(), control );
283 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
287 RequestDefaultTheme();
292 ApplyStyle( mThemeBuilder, control );
296 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
298 bool builderReady = false;
300 // First look in the cache
301 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
308 // Merge theme and style constants
309 Property::Map constants( mThemeBuilderConstants );
310 constants.Merge( mStyleBuilderConstants );
313 builder = CreateBuilder( constants );
315 if( LoadJSON( builder, jsonFileName ) )
317 CacheBuilder( builder, jsonFileName );
322 // Apply the style to the control
325 builder.ApplyStyle( styleName, control );
329 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
331 DALI_ASSERT_DEBUG( 0 != filename.length());
333 // as toolkit is platform agnostic, it cannot load files from filesystem
334 // ask style monitor to load the style sheet
337 return mStyleMonitor.LoadThemeFile( filename, stringOut );
343 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
345 return mStyleChangeSignal;
348 void StyleManager::RequestThemeChange( const std::string& themeFile )
350 mThemeFile = themeFile;
352 // need to do style change synchronously as app might create a UI control on the next line
356 void StyleManager::RequestDefaultTheme()
358 RequestThemeChange( DEFAULT_THEME );
361 void StyleManager::SetTheme()
363 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
364 if ( LoadJSON( mThemeBuilder, mThemeFile ) )
366 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
370 mThemeBuilder.Reset();
374 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
376 BuilderMap::iterator builderIt = mBuilderCache.find( key );
377 if( builderIt != mBuilderCache.end() )
379 return builderIt->second;
382 return Toolkit::Builder();
385 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
387 mBuilderCache[ key ] = builder;
390 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
392 switch ( styleChange )
394 case StyleChange::DEFAULT_FONT_CHANGE:
399 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
401 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
405 case StyleChange::THEME_CHANGE:
407 const std::string& newTheme = styleMonitor.GetTheme();
408 if( ! newTheme.empty() )
410 mThemeFile = newTheme;
414 mThemeFile = DEFAULT_THEME;
422 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
425 } // namespace Internal
427 } // namespace Toolkit