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/devel-api/adaptor-framework/singleton-service.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/devel-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/devel-api/styling/style-manager.h>
37 const char* LANDSCAPE_QUALIFIER = "landscape";
38 const char* PORTRAIT_QUALIFIER = "portrait";
39 const char* FONT_SIZE_QUALIFIER = "font-size-";
41 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json";
43 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
44 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
62 BaseHandle handle = StyleManager::Get();
66 SingletonService singletonService( SingletonService::Get() );
67 if ( singletonService )
69 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
70 singletonService.Register( typeid( manager ), manager );
78 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
79 DALI_TYPE_REGISTRATION_END()
83 Toolkit::StyleManager StyleManager::Get()
85 Toolkit::StyleManager manager;
87 SingletonService singletonService( SingletonService::Get() );
88 if ( singletonService )
90 // Check whether the style manager is already created
91 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
94 // If so, downcast the handle of singleton
95 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
102 StyleManager::StyleManager()
103 : mOrientationDegrees( 0 ), // Portrait
104 mDefaultFontSize( -1 ),
105 mThemeFile( DEFAULT_THEME )
107 // Add theme builder constants
108 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
110 StyleMonitor styleMonitor( StyleMonitor::Get() );
113 styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
115 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
119 StyleManager::~StyleManager()
123 void StyleManager::SetOrientationValue( int orientation )
125 if( orientation != mOrientationDegrees )
127 mOrientationDegrees = orientation;
128 // TODO: if orientation changed, apply the new style to all controls
129 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
134 int StyleManager::GetOrientationValue()
136 return mOrientationDegrees;
139 void StyleManager::SetOrientation( Orientation orientation )
143 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
146 OnOrientationChanged( orientation );
150 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
154 Orientation StyleManager::GetOrientation()
159 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
161 mStyleBuilderConstants[ key ] = value;
164 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
166 Property::Value* value = mStyleBuilderConstants.Find( key );
176 void StyleManager::OnOrientationChanged( Orientation orientation )
178 mOrientation = orientation;
179 // TODO: if orientation changed, apply the new style to all controls
180 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
184 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
186 Toolkit::Builder builder = Toolkit::Builder::New();
187 builder.AddConstants( constants );
192 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
194 std::string fileString;
195 if( LoadFile( jsonFilePath, fileString ) )
197 builder.LoadFromString( fileString );
202 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
207 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
209 // Append the relevant qualifier for orientation
210 int orientation = mOrientationDegrees;
214 orientation = mOrientation.GetDegrees();
217 switch( orientation )
222 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
226 case 0: // fall through
229 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
235 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
237 qualifiedStyleOut.append( styleName );
239 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
241 const std::string& str = *it;
243 qualifiedStyleOut.append( "-" );
244 qualifiedStyleOut.append( str );
248 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
250 std::string styleName = control.GetStyleName();
251 if( styleName.empty() )
253 // Convert control name to lower case
254 styleName = control.GetTypeName();
255 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
258 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
259 StringList qualifiers;
260 CollectQualifiers( qualifiers );
264 std::string qualifiedStyleName;
265 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
267 // Break if style found or we have tried the root style name (qualifiers is empty)
268 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
273 // Remove the last qualifier in an attempt to find a style that is valid
274 qualifiers.pop_back();
277 if( mDefaultFontSize >= 0 )
279 // Apply the style for logical font size
280 std::stringstream fontSizeQualifier;
281 fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
282 builder.ApplyStyle( fontSizeQualifier.str(), control );
286 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
290 ApplyStyle( mThemeBuilder, control );
294 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
296 bool builderReady = false;
298 // First look in the cache
299 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
306 // Merge theme and style constants
307 Property::Map constants( mThemeBuilderConstants );
308 constants.Merge( mStyleBuilderConstants );
311 builder = CreateBuilder( constants );
313 if( LoadJSON( builder, jsonFileName ) )
315 CacheBuilder( builder, jsonFileName );
320 // Apply the style to the control
323 builder.ApplyStyle( styleName, control );
327 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
329 DALI_ASSERT_DEBUG( 0 != filename.length());
331 std::ifstream in( filename.c_str(), std::ios::in );
334 std::stringstream buffer;
335 buffer << in.rdbuf();
337 stringOut = buffer.str();
347 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
349 return mStyleChangeSignal;
352 void StyleManager::RequestThemeChange( const std::string& themeFile )
354 mThemeFile = themeFile;
356 // need to do style change synchronously as app might create a UI control on the next line
360 void StyleManager::RequestDefaultTheme()
362 RequestThemeChange( DEFAULT_THEME );
365 void StyleManager::SetTheme()
367 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
368 if ( LoadJSON( mThemeBuilder, mThemeFile ) )
371 change.themeChange = true;
372 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
376 mThemeBuilder.Reset();
380 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
382 BuilderMap::iterator builderIt = mBuilderCache.find( key );
383 if( builderIt != mBuilderCache.end() )
385 return builderIt->second;
388 return Toolkit::Builder();
391 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
393 mBuilderCache[ key ] = builder;
396 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
398 if( styleChange.defaultFontSizeChange )
400 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
403 if( styleChange.themeChange )
405 if( ! styleChange.themeFilePath.empty() )
407 mThemeFile = styleChange.themeFilePath;
411 mThemeFile = DEFAULT_THEME;
417 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
420 } // namespace Internal
422 } // namespace Toolkit