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/public-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 = "FontSize";
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 : mDefaultFontSize( -1 ),
102 mDefaultFontFamily(""),
103 mFeedbackStyle( NULL )
105 // Add theme builder constants
106 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
108 mStyleMonitor = StyleMonitor::Get();
111 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
113 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
116 // Sound & haptic style
117 mFeedbackStyle = new FeedbackStyle();
120 StyleManager::~StyleManager()
122 delete mFeedbackStyle;
125 void StyleManager::ApplyTheme( const std::string& themeFile )
127 SetTheme( themeFile );
130 void StyleManager::ApplyDefaultTheme()
136 const std::string& StyleManager::GetDefaultFontFamily() const
138 return mDefaultFontFamily;
141 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
143 mStyleBuilderConstants[ key ] = value;
146 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
148 Property::Value* value = mStyleBuilderConstants.Find( key );
158 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
167 ApplyStyle( mThemeBuilder, control );
171 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
173 ApplyThemeStyle( control );
177 mFeedbackStyle->ObjectCreated( control );
181 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
183 bool builderReady = false;
185 // First look in the cache
186 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
193 // Merge theme and style constants
194 Property::Map constants( mThemeBuilderConstants );
195 constants.Merge( mStyleBuilderConstants );
198 builder = CreateBuilder( constants );
200 if( LoadJSON( builder, jsonFileName ) )
202 CacheBuilder( builder, jsonFileName );
207 // Apply the style to the control
210 builder.ApplyStyle( styleName, control );
214 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
216 return mStyleChangedSignal;
219 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
221 return mControlStyleChangeSignal;
224 void StyleManager::SetTheme( const std::string& themeFile )
226 bool themeLoaded = false;
228 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
230 // Always load the default theme first, then merge in the custom theme if present
231 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
233 if( ! themeFile.empty() )
235 mThemeFile = themeFile;
236 themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
243 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
246 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
250 mThemeBuilder.Reset();
254 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
256 DALI_ASSERT_DEBUG( 0 != filename.length());
258 // as toolkit is platform agnostic, it cannot load files from filesystem
259 // ask style monitor to load the style sheet
262 return mStyleMonitor.LoadThemeFile( filename, stringOut );
268 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
270 Toolkit::Builder builder = Toolkit::Builder::New();
271 builder.AddConstants( constants );
276 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
278 std::string fileString;
279 if( LoadFile( jsonFilePath, fileString ) )
281 builder.LoadFromString( fileString );
286 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
291 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
293 // Append the relevant qualifier for orientation
294 int orientation = 0; // Get the orientation from the system
295 switch( orientation )
300 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
304 case 0: // fall through
307 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
313 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
315 qualifiedStyleOut.append( styleName );
317 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
319 const std::string& str = *it;
321 qualifiedStyleOut.append( "-" );
322 qualifiedStyleOut.append( str );
326 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
328 std::string styleName = control.GetStyleName();
330 if( styleName.empty() )
332 // Convert control name to lower case
333 styleName = control.GetTypeName();
334 std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
337 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
338 StringList qualifiers;
339 CollectQualifiers( qualifiers );
343 std::string qualifiedStyleName;
344 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
346 // Break if style found or we have tried the root style name (qualifiers is empty)
347 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
352 // Remove the last qualifier in an attempt to find a style that is valid
353 qualifiers.pop_back();
356 if( mDefaultFontSize >= 0 )
358 // Apply the style for logical font size
359 std::stringstream fontSizeQualifier;
360 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
361 builder.ApplyStyle( fontSizeQualifier.str(), control );
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::Type styleChange )
383 switch ( styleChange )
385 case StyleChange::DEFAULT_FONT_CHANGE:
387 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
391 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
393 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
397 case StyleChange::THEME_CHANGE:
399 SetTheme( styleMonitor.GetTheme() );
403 EmitStyleChangeSignals( styleChange );
406 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
408 Toolkit::StyleManager styleManager = StyleManager::Get();
410 // Update Controls first
411 mControlStyleChangeSignal.Emit( styleManager, styleChange );
413 // Inform application last
414 mStyleChangedSignal.Emit( styleManager, styleChange );
418 } // namespace Internal
420 } // namespace Toolkit