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 */ );
77 * Merge two maps into one
79 void MergeMaps( const PropertyValueMap& a, const PropertyValueMap& b, PropertyValueMap& out )
82 for( PropertyValueMap::const_iterator it = b.begin(), itEnd = b.end(); it != itEnd; ++it )
84 out[ it->first ] = it->second;
90 Toolkit::StyleManager StyleManager::Get()
92 Toolkit::StyleManager manager;
94 SingletonService singletonService( SingletonService::Get() );
95 if ( singletonService )
97 // Check whether the style manager is already created
98 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
101 // If so, downcast the handle of singleton
102 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
109 StyleManager::StyleManager()
110 : mOrientationDegrees( 0 ) // Portrait
112 // Add theme builder constants
113 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
115 RequestDefaultTheme();
117 StyleMonitor styleMonitor( StyleMonitor::Get() );
120 styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
124 StyleManager::~StyleManager()
128 void StyleManager::SetOrientationValue( int orientation )
130 if( orientation != mOrientationDegrees )
132 mOrientationDegrees = orientation;
133 // TODO: if orientation changed, apply the new style to all controls
134 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
139 int StyleManager::GetOrientationValue()
141 return mOrientationDegrees;
144 void StyleManager::SetOrientation( Orientation orientation )
148 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
151 OnOrientationChanged( orientation );
155 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
159 Orientation StyleManager::GetOrientation()
164 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
166 mStyleBuilderConstants[ key ] = value;
169 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
171 Toolkit::PropertyValueMap::iterator valueIt = mStyleBuilderConstants.find( key );
172 if( valueIt != mStyleBuilderConstants.end() )
174 valueOut = valueIt->second;
181 void StyleManager::OnOrientationChanged( Orientation orientation )
183 mOrientation = orientation;
184 // TODO: if orientation changed, apply the new style to all controls
185 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
189 Toolkit::Builder StyleManager::CreateBuilder( const PropertyValueMap& constants )
191 Toolkit::Builder builder = Toolkit::Builder::New();
192 builder.AddConstants( constants );
197 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
199 std::string fileString;
200 if( LoadFile( jsonFilePath, fileString ) )
202 builder.LoadFromString( fileString );
207 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
212 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
214 // Append the relevant qualifier for orientation
215 int orientation = mOrientationDegrees;
219 orientation = mOrientation.GetDegrees();
222 switch( orientation )
227 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
231 case 0: // fall through
234 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
240 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
242 qualifiedStyleOut.append( styleName );
244 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
246 const std::string& str = *it;
248 qualifiedStyleOut.append( "-" );
249 qualifiedStyleOut.append( str );
253 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
255 // Convert control name to lower case
256 std::string styleName = control.GetTypeName();
257 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
259 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
260 StringList qualifiers;
261 CollectQualifiers( qualifiers );
265 std::string qualifiedStyleName;
266 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
268 // Break if style found or we have tried the root style name (qualifiers is empty)
269 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
274 // Remove the last qualifier in an attempt to find a style that is valid
275 qualifiers.pop_back();
279 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
283 ApplyStyle( mThemeBuilder, control );
287 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
289 bool builderReady = false;
291 // First look in the cache
292 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
299 // Merge theme and style constants
300 PropertyValueMap constants;
301 MergeMaps( mThemeBuilderConstants, mStyleBuilderConstants, constants );
304 builder = CreateBuilder( constants );
306 if( LoadJSON( builder, jsonFileName ) )
308 CacheBuilder( builder, jsonFileName );
313 // Apply the style to the control
316 builder.ApplyStyle( styleName, control );
320 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
322 DALI_ASSERT_DEBUG( 0 != filename.length());
324 std::ifstream in( filename.c_str(), std::ios::in );
327 std::stringstream buffer;
328 buffer << in.rdbuf();
330 stringOut = buffer.str();
340 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
342 return mStyleChangeSignal;
345 void StyleManager::RequestThemeChange( const std::string& themeFile )
347 mThemeFile = themeFile;
349 // need to do style change synchronously as app might create a UI control on the next line
353 void StyleManager::RequestDefaultTheme()
355 RequestThemeChange( DEFAULT_THEME );
358 void StyleManager::SetTheme()
360 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
361 LoadJSON( mThemeBuilder, mThemeFile );
364 change.themeChange = true;
365 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
368 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
370 BuilderMap::iterator builderIt = mBuilderCache.find( key );
371 if( builderIt != mBuilderCache.end() )
373 return builderIt->second;
376 return Toolkit::Builder();
379 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
381 mBuilderCache[ key ] = builder;
384 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
386 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
389 } // namespace Internal
391 } // namespace Toolkit