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/devel-api/adaptor-framework/singleton-service.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/object/type-registry-helper.h>
24 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/public-api/controls/control.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/devel-api/styling/style-manager.h>
30 #include <dali-toolkit/internal/feedback/feedback-style.h>
35 const char* LANDSCAPE_QUALIFIER = "landscape";
36 const char* PORTRAIT_QUALIFIER = "portrait";
37 const char* FONT_SIZE_QUALIFIER = "font-size-";
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-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 );
76 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
77 DALI_TYPE_REGISTRATION_END()
81 Toolkit::StyleManager StyleManager::Get()
83 Toolkit::StyleManager manager;
85 SingletonService singletonService( SingletonService::Get() );
86 if ( singletonService )
88 // Check whether the style manager is already created
89 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
92 // If so, downcast the handle of singleton
93 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
100 StyleManager::StyleManager()
101 : mOrientationDegrees( 0 ), // Portrait
102 mDefaultFontSize( -1 ),
103 mDefaultFontFamily(""),
104 mThemeFile( DEFAULT_THEME ),
105 mFeedbackStyle( NULL )
107 // Add theme builder constants
108 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
110 mStyleMonitor = StyleMonitor::Get();
113 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
115 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
118 // Sound & haptic style
119 mFeedbackStyle = new FeedbackStyle();
123 StyleManager::~StyleManager()
125 delete mFeedbackStyle;
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 std::string StyleManager::GetDefaultFontFamily() const
161 return mDefaultFontFamily;
164 Orientation StyleManager::GetOrientation()
169 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
171 mStyleBuilderConstants[ key ] = value;
174 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
176 Property::Value* value = mStyleBuilderConstants.Find( key );
186 void StyleManager::OnOrientationChanged( Orientation orientation )
188 mOrientation = orientation;
189 // TODO: if orientation changed, apply the new style to all controls
190 // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
194 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
196 Toolkit::Builder builder = Toolkit::Builder::New();
197 builder.AddConstants( constants );
202 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
204 std::string fileString;
205 if( LoadFile( jsonFilePath, fileString ) )
207 builder.LoadFromString( fileString );
212 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
217 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
219 // Append the relevant qualifier for orientation
220 int orientation = mOrientationDegrees;
224 orientation = mOrientation.GetDegrees();
227 switch( orientation )
232 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
236 case 0: // fall through
239 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
245 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
247 qualifiedStyleOut.append( styleName );
249 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
251 const std::string& str = *it;
253 qualifiedStyleOut.append( "-" );
254 qualifiedStyleOut.append( str );
258 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
260 std::string styleName = control.GetStyleName();
262 if( styleName.empty() )
264 // Convert control name to lower case
265 styleName = control.GetTypeName();
266 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
269 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
270 StringList qualifiers;
271 CollectQualifiers( qualifiers );
275 std::string qualifiedStyleName;
276 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
278 // Break if style found or we have tried the root style name (qualifiers is empty)
279 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
284 // Remove the last qualifier in an attempt to find a style that is valid
285 qualifiers.pop_back();
288 if( mDefaultFontSize >= 0 )
290 // Apply the style for logical font size
291 std::stringstream fontSizeQualifier;
292 fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
293 builder.ApplyStyle( fontSizeQualifier.str(), control );
297 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
301 RequestDefaultTheme();
306 ApplyStyle( mThemeBuilder, control );
310 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
312 ApplyThemeStyle( control );
316 mFeedbackStyle->ObjectCreated( control );
320 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
322 bool builderReady = false;
324 // First look in the cache
325 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
332 // Merge theme and style constants
333 Property::Map constants( mThemeBuilderConstants );
334 constants.Merge( mStyleBuilderConstants );
337 builder = CreateBuilder( constants );
339 if( LoadJSON( builder, jsonFileName ) )
341 CacheBuilder( builder, jsonFileName );
346 // Apply the style to the control
349 builder.ApplyStyle( styleName, control );
353 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
355 DALI_ASSERT_DEBUG( 0 != filename.length());
357 // as toolkit is platform agnostic, it cannot load files from filesystem
358 // ask style monitor to load the style sheet
361 return mStyleMonitor.LoadThemeFile( filename, stringOut );
367 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
369 return mStyleChangeSignal;
372 void StyleManager::RequestThemeChange( const std::string& themeFile )
374 mThemeFile = themeFile;
376 // need to do style change synchronously as app might create a UI control on the next line
380 void StyleManager::RequestDefaultTheme()
382 RequestThemeChange( DEFAULT_THEME );
385 void StyleManager::SetTheme()
387 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
389 if( LoadJSON( mThemeBuilder, mThemeFile ) )
393 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
396 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
400 mThemeBuilder.Reset();
404 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
406 BuilderMap::iterator builderIt = mBuilderCache.find( key );
407 if( builderIt != mBuilderCache.end() )
409 return builderIt->second;
412 return Toolkit::Builder();
415 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
417 mBuilderCache[ key ] = builder;
420 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
422 switch ( styleChange )
424 case StyleChange::DEFAULT_FONT_CHANGE:
426 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
430 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
432 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
436 case StyleChange::THEME_CHANGE:
438 const std::string& newTheme = styleMonitor.GetTheme();
439 if( ! newTheme.empty() )
441 mThemeFile = newTheme;
445 mThemeFile = DEFAULT_THEME;
453 mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
456 } // namespace Internal
458 } // namespace Toolkit