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-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/styling/style-manager.h>
24 #include <dali/integration-api/debug.h>
35 const char* LANDSCAPE_QUALIFIER = "landscape";
36 const char* PORTRAIT_QUALIFIER = "portrait";
38 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-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 );
74 TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
78 Toolkit::StyleManager StyleManager::Get()
80 Toolkit::StyleManager manager;
82 SingletonService singletonService( SingletonService::Get() );
83 if ( singletonService )
85 // Check whether the style manager is already created
86 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
89 // If so, downcast the handle of singleton
90 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
97 StyleManager::StyleManager()
98 : mOrientationDegrees( 0 ) // Portrait
100 // Add theme builder constants
101 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
103 RequestDefaultTheme();
105 StyleMonitor styleMonitor( StyleMonitor::Get() );
108 styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
112 StyleManager::~StyleManager()
116 void StyleManager::SetOrientationValue( int orientation )
118 if( orientation != mOrientationDegrees )
120 mOrientationDegrees = orientation;
121 // TODO: if orientation changed, apply the new style to all controls
122 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
127 int StyleManager::GetOrientationValue()
129 return mOrientationDegrees;
132 void StyleManager::SetOrientation( Orientation orientation )
136 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
139 OnOrientationChanged( orientation );
143 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
147 Orientation StyleManager::GetOrientation()
152 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
154 mStyleBuilderConstants[ key ] = value;
157 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
159 Property::Value* value = mStyleBuilderConstants.Find( key );
169 void StyleManager::OnOrientationChanged( Orientation orientation )
171 mOrientation = orientation;
172 // TODO: if orientation changed, apply the new style to all controls
173 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
177 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
179 Toolkit::Builder builder = Toolkit::Builder::New();
180 builder.AddConstants( constants );
185 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
187 std::string fileString;
188 if( LoadFile( jsonFilePath, fileString ) )
190 builder.LoadFromString( fileString );
195 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
200 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
202 // Append the relevant qualifier for orientation
203 int orientation = mOrientationDegrees;
207 orientation = mOrientation.GetDegrees();
210 switch( orientation )
215 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
219 case 0: // fall through
222 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
228 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
230 qualifiedStyleOut.append( styleName );
232 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
234 const std::string& str = *it;
236 qualifiedStyleOut.append( "-" );
237 qualifiedStyleOut.append( str );
241 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
243 // Convert control name to lower case
244 std::string styleName = control.GetTypeName();
245 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
247 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
248 StringList qualifiers;
249 CollectQualifiers( qualifiers );
253 std::string qualifiedStyleName;
254 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
256 // Break if style found or we have tried the root style name (qualifiers is empty)
257 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
262 // Remove the last qualifier in an attempt to find a style that is valid
263 qualifiers.pop_back();
267 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
271 ApplyStyle( mThemeBuilder, control );
275 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
277 bool builderReady = false;
279 // First look in the cache
280 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
287 // Merge theme and style constants
288 Property::Map constants( mThemeBuilderConstants );
289 constants.Merge( mStyleBuilderConstants );
292 builder = CreateBuilder( constants );
294 if( LoadJSON( builder, jsonFileName ) )
296 CacheBuilder( builder, jsonFileName );
301 // Apply the style to the control
304 builder.ApplyStyle( styleName, control );
308 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
310 DALI_ASSERT_DEBUG( 0 != filename.length());
312 std::ifstream in( filename.c_str(), std::ios::in );
315 std::stringstream buffer;
316 buffer << in.rdbuf();
318 stringOut = buffer.str();
328 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
330 return mStyleChangeSignal;
333 void StyleManager::RequestThemeChange( const std::string& themeFile )
335 mThemeFile = themeFile;
337 // need to do style change synchronously as app might create a UI control on the next line
341 void StyleManager::RequestDefaultTheme()
343 RequestThemeChange( DEFAULT_THEME );
346 void StyleManager::SetTheme()
348 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
349 LoadJSON( mThemeBuilder, mThemeFile );
352 change.themeChange = true;
353 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
356 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
358 BuilderMap::iterator builderIt = mBuilderCache.find( key );
359 if( builderIt != mBuilderCache.end() )
361 return builderIt->second;
364 return Toolkit::Builder();
367 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
369 mBuilderCache[ key ] = builder;
372 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
374 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
377 } // namespace Internal
379 } // namespace Toolkit