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"
24 #include <dali/public-api/adaptor-framework/singleton-service.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/public-api/controls/control.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali-toolkit/public-api/styling/style-manager.h>
36 const char* LANDSCAPE_QUALIFIER = "landscape";
37 const char* PORTRAIT_QUALIFIER = "portrait";
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-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 );
75 TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
79 Toolkit::StyleManager StyleManager::Get()
81 Toolkit::StyleManager manager;
83 SingletonService singletonService( SingletonService::Get() );
84 if ( singletonService )
86 // Check whether the style manager is already created
87 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
90 // If so, downcast the handle of singleton
91 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
98 StyleManager::StyleManager()
99 : mOrientationDegrees( 0 ) // Portrait
101 // Add theme builder constants
102 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
104 RequestDefaultTheme();
106 StyleMonitor styleMonitor( StyleMonitor::Get() );
109 styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
113 StyleManager::~StyleManager()
117 void StyleManager::SetOrientationValue( int orientation )
119 if( orientation != mOrientationDegrees )
121 mOrientationDegrees = orientation;
122 // TODO: if orientation changed, apply the new style to all controls
123 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
128 int StyleManager::GetOrientationValue()
130 return mOrientationDegrees;
133 void StyleManager::SetOrientation( Orientation orientation )
137 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
140 OnOrientationChanged( orientation );
144 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
148 Orientation StyleManager::GetOrientation()
153 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
155 mStyleBuilderConstants[ key ] = value;
158 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
160 Property::Value* value = mStyleBuilderConstants.Find( key );
170 void StyleManager::OnOrientationChanged( Orientation orientation )
172 mOrientation = orientation;
173 // TODO: if orientation changed, apply the new style to all controls
174 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
178 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
180 Toolkit::Builder builder = Toolkit::Builder::New();
181 builder.AddConstants( constants );
186 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
188 std::string fileString;
189 if( LoadFile( jsonFilePath, fileString ) )
191 builder.LoadFromString( fileString );
196 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
201 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
203 // Append the relevant qualifier for orientation
204 int orientation = mOrientationDegrees;
208 orientation = mOrientation.GetDegrees();
211 switch( orientation )
216 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
220 case 0: // fall through
223 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
229 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
231 qualifiedStyleOut.append( styleName );
233 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
235 const std::string& str = *it;
237 qualifiedStyleOut.append( "-" );
238 qualifiedStyleOut.append( str );
242 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
244 // Convert control name to lower case
245 std::string styleName = control.GetTypeName();
246 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
248 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
249 StringList qualifiers;
250 CollectQualifiers( qualifiers );
254 std::string qualifiedStyleName;
255 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
257 // Break if style found or we have tried the root style name (qualifiers is empty)
258 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
263 // Remove the last qualifier in an attempt to find a style that is valid
264 qualifiers.pop_back();
268 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
272 ApplyStyle( mThemeBuilder, control );
276 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
278 bool builderReady = false;
280 // First look in the cache
281 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
288 // Merge theme and style constants
289 Property::Map constants( mThemeBuilderConstants );
290 constants.Merge( mStyleBuilderConstants );
293 builder = CreateBuilder( constants );
295 if( LoadJSON( builder, jsonFileName ) )
297 CacheBuilder( builder, jsonFileName );
302 // Apply the style to the control
305 builder.ApplyStyle( styleName, control );
309 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
311 DALI_ASSERT_DEBUG( 0 != filename.length());
313 std::ifstream in( filename.c_str(), std::ios::in );
316 std::stringstream buffer;
317 buffer << in.rdbuf();
319 stringOut = buffer.str();
329 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
331 return mStyleChangeSignal;
334 void StyleManager::RequestThemeChange( const std::string& themeFile )
336 mThemeFile = themeFile;
338 // need to do style change synchronously as app might create a UI control on the next line
342 void StyleManager::RequestDefaultTheme()
344 RequestThemeChange( DEFAULT_THEME );
347 void StyleManager::SetTheme()
349 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
350 if ( LoadJSON( mThemeBuilder, mThemeFile ) )
353 change.themeChange = true;
354 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
358 mThemeBuilder.Reset();
362 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
364 BuilderMap::iterator builderIt = mBuilderCache.find( key );
365 if( builderIt != mBuilderCache.end() )
367 return builderIt->second;
370 return Toolkit::Builder();
373 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
375 mBuilderCache[ key ] = builder;
378 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
380 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
383 } // namespace Internal
385 } // namespace Toolkit