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/public-api/object/type-registry-helper.h>
27 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/public-api/controls/control.h>
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/styling/style-manager.h>
37 const char* LANDSCAPE_QUALIFIER = "landscape";
38 const char* PORTRAIT_QUALIFIER = "portrait";
40 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json";
42 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
43 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
61 BaseHandle handle = StyleManager::Get();
65 SingletonService singletonService( SingletonService::Get() );
66 if ( singletonService )
68 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
69 singletonService.Register( typeid( manager ), manager );
77 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
78 DALI_TYPE_REGISTRATION_END()
82 Toolkit::StyleManager StyleManager::Get()
84 Toolkit::StyleManager manager;
86 SingletonService singletonService( SingletonService::Get() );
87 if ( singletonService )
89 // Check whether the style manager is already created
90 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
93 // If so, downcast the handle of singleton
94 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
101 StyleManager::StyleManager()
102 : mOrientationDegrees( 0 ) // Portrait
104 // Add theme builder constants
105 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
107 RequestDefaultTheme();
109 StyleMonitor styleMonitor( StyleMonitor::Get() );
112 styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
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 // Convert control name to lower case
248 std::string styleName = control.GetTypeName();
249 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
251 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
252 StringList qualifiers;
253 CollectQualifiers( qualifiers );
257 std::string qualifiedStyleName;
258 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
260 // Break if style found or we have tried the root style name (qualifiers is empty)
261 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
266 // Remove the last qualifier in an attempt to find a style that is valid
267 qualifiers.pop_back();
271 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
275 ApplyStyle( mThemeBuilder, control );
279 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
281 bool builderReady = false;
283 // First look in the cache
284 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
291 // Merge theme and style constants
292 Property::Map constants( mThemeBuilderConstants );
293 constants.Merge( mStyleBuilderConstants );
296 builder = CreateBuilder( constants );
298 if( LoadJSON( builder, jsonFileName ) )
300 CacheBuilder( builder, jsonFileName );
305 // Apply the style to the control
308 builder.ApplyStyle( styleName, control );
312 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
314 DALI_ASSERT_DEBUG( 0 != filename.length());
316 std::ifstream in( filename.c_str(), std::ios::in );
319 std::stringstream buffer;
320 buffer << in.rdbuf();
322 stringOut = buffer.str();
332 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
334 return mStyleChangeSignal;
337 void StyleManager::RequestThemeChange( const std::string& themeFile )
339 mThemeFile = themeFile;
341 // need to do style change synchronously as app might create a UI control on the next line
345 void StyleManager::RequestDefaultTheme()
347 RequestThemeChange( DEFAULT_THEME );
350 void StyleManager::SetTheme()
352 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
353 if ( LoadJSON( mThemeBuilder, mThemeFile ) )
356 change.themeChange = true;
357 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
361 mThemeBuilder.Reset();
365 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
367 BuilderMap::iterator builderIt = mBuilderCache.find( key );
368 if( builderIt != mBuilderCache.end() )
370 return builderIt->second;
373 return Toolkit::Builder();
376 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
378 mBuilderCache[ key ] = builder;
381 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
383 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
386 } // namespace Internal
388 } // namespace Toolkit