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 )
106 // Add theme builder constants
107 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
109 RequestDefaultTheme();
111 StyleMonitor styleMonitor( StyleMonitor::Get() );
114 styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
116 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
120 StyleManager::~StyleManager()
124 void StyleManager::SetOrientationValue( int orientation )
126 if( orientation != mOrientationDegrees )
128 mOrientationDegrees = orientation;
129 // TODO: if orientation changed, apply the new style to all controls
130 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
135 int StyleManager::GetOrientationValue()
137 return mOrientationDegrees;
140 void StyleManager::SetOrientation( Orientation orientation )
144 mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
147 OnOrientationChanged( orientation );
151 mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
155 Orientation StyleManager::GetOrientation()
160 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
162 mStyleBuilderConstants[ key ] = value;
165 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
167 Property::Value* value = mStyleBuilderConstants.Find( key );
177 void StyleManager::OnOrientationChanged( Orientation orientation )
179 mOrientation = orientation;
180 // TODO: if orientation changed, apply the new style to all controls
181 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
185 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
187 Toolkit::Builder builder = Toolkit::Builder::New();
188 builder.AddConstants( constants );
193 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
195 std::string fileString;
196 if( LoadFile( jsonFilePath, fileString ) )
198 builder.LoadFromString( fileString );
203 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
208 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
210 // Append the relevant qualifier for orientation
211 int orientation = mOrientationDegrees;
215 orientation = mOrientation.GetDegrees();
218 switch( orientation )
223 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
227 case 0: // fall through
230 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
236 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
238 qualifiedStyleOut.append( styleName );
240 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
242 const std::string& str = *it;
244 qualifiedStyleOut.append( "-" );
245 qualifiedStyleOut.append( str );
249 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
251 std::string styleName = control.GetStyleName();
252 if( styleName.empty() )
254 // Convert control name to lower case
255 styleName = control.GetTypeName();
256 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();
278 if( mDefaultFontSize >= 0 )
280 // Apply the style for logical font size
281 std::stringstream fontSizeQualifier;
282 fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
283 builder.ApplyStyle( fontSizeQualifier.str(), control );
287 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
291 ApplyStyle( mThemeBuilder, control );
295 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
297 bool builderReady = false;
299 // First look in the cache
300 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
307 // Merge theme and style constants
308 Property::Map constants( mThemeBuilderConstants );
309 constants.Merge( mStyleBuilderConstants );
312 builder = CreateBuilder( constants );
314 if( LoadJSON( builder, jsonFileName ) )
316 CacheBuilder( builder, jsonFileName );
321 // Apply the style to the control
324 builder.ApplyStyle( styleName, control );
328 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
330 DALI_ASSERT_DEBUG( 0 != filename.length());
332 std::ifstream in( filename.c_str(), std::ios::in );
335 std::stringstream buffer;
336 buffer << in.rdbuf();
338 stringOut = buffer.str();
348 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
350 return mStyleChangeSignal;
353 void StyleManager::RequestThemeChange( const std::string& themeFile )
355 mThemeFile = themeFile;
357 // need to do style change synchronously as app might create a UI control on the next line
361 void StyleManager::RequestDefaultTheme()
363 RequestThemeChange( DEFAULT_THEME );
366 void StyleManager::SetTheme()
368 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
369 if ( LoadJSON( mThemeBuilder, mThemeFile ) )
372 change.themeChange = true;
373 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
377 mThemeBuilder.Reset();
381 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
383 BuilderMap::iterator builderIt = mBuilderCache.find( key );
384 if( builderIt != mBuilderCache.end() )
386 return builderIt->second;
389 return Toolkit::Builder();
392 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
394 mBuilderCache[ key ] = builder;
397 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
399 if( styleChange.defaultFontSizeChange )
401 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
404 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
407 } // namespace Internal
409 } // namespace Toolkit