Merge "Fix the texture bleeding with wrapping in atlas" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include "style-manager-impl.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/adaptor-framework/singleton-service.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
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>
31
32 namespace
33 {
34
35 const char* LANDSCAPE_QUALIFIER = "landscape";
36 const char* PORTRAIT_QUALIFIER  = "portrait";
37 const char* FONT_SIZE_QUALIFIER = "FontSize";
38
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
40
41 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
42 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
43
44 } // namespace
45
46 namespace Dali
47 {
48
49 namespace Toolkit
50 {
51
52 namespace Internal
53 {
54
55 namespace
56 {
57
58 BaseHandle Create()
59 {
60   BaseHandle handle = StyleManager::Get();
61
62   if ( !handle )
63   {
64     SingletonService singletonService( SingletonService::Get() );
65     if ( singletonService )
66     {
67       Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
68       singletonService.Register( typeid( manager ), manager );
69       handle = manager;
70     }
71   }
72
73   return handle;
74 }
75
76 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
77 DALI_TYPE_REGISTRATION_END()
78
79 } // namespace
80
81 Toolkit::StyleManager StyleManager::Get()
82 {
83   Toolkit::StyleManager manager;
84
85   SingletonService singletonService( SingletonService::Get() );
86   if ( singletonService )
87   {
88     // Check whether the style manager is already created
89     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
90     if( handle )
91     {
92       // If so, downcast the handle of singleton
93       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
94     }
95   }
96
97   return manager;
98 }
99
100 StyleManager::StyleManager()
101 : mDefaultFontSize( -1 ),
102   mDefaultFontFamily(""),
103   mFeedbackStyle( NULL )
104 {
105   // Add theme builder constants
106   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
107
108   mStyleMonitor = StyleMonitor::Get();
109   if( mStyleMonitor )
110   {
111     mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
112
113     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
114   }
115
116   // Sound & haptic style
117   mFeedbackStyle = new FeedbackStyle();
118 }
119
120 StyleManager::~StyleManager()
121 {
122   delete mFeedbackStyle;
123 }
124
125 void StyleManager::ApplyTheme( const std::string& themeFile )
126 {
127   SetTheme( themeFile );
128 }
129
130 void StyleManager::ApplyDefaultTheme()
131 {
132   std::string empty;
133   SetTheme( empty );
134 }
135
136 const std::string& StyleManager::GetDefaultFontFamily() const
137 {
138   return mDefaultFontFamily;
139 }
140
141 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
142 {
143   mStyleBuilderConstants[ key ] = value;
144 }
145
146 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
147 {
148   Property::Value* value = mStyleBuilderConstants.Find( key );
149   if( value )
150   {
151     valueOut = *value;
152     return true;
153   }
154
155   return false;
156 }
157
158 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
159 {
160   if( !mThemeBuilder )
161   {
162     ApplyDefaultTheme();
163   }
164
165   if( mThemeBuilder )
166   {
167     ApplyStyle( mThemeBuilder, control );
168   }
169 }
170
171 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
172 {
173   ApplyThemeStyle( control );
174
175   if(mFeedbackStyle)
176   {
177     mFeedbackStyle->ObjectCreated( control );
178   }
179 }
180
181 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
182 {
183   bool builderReady = false;
184
185   // First look in the cache
186   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
187   if( builder )
188   {
189     builderReady = true;
190   }
191   else
192   {
193     // Merge theme and style constants
194     Property::Map constants( mThemeBuilderConstants );
195     constants.Merge( mStyleBuilderConstants );
196
197     // Create it
198     builder = CreateBuilder( constants );
199
200     if( LoadJSON( builder, jsonFileName ) )
201     {
202       CacheBuilder( builder, jsonFileName );
203       builderReady = true;
204     }
205   }
206
207   // Apply the style to the control
208   if( builderReady )
209   {
210     builder.ApplyStyle( styleName, control );
211   }
212 }
213
214 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
215 {
216   return mStyleChangedSignal;
217 }
218
219 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
220 {
221   return mControlStyleChangeSignal;
222 }
223
224 void StyleManager::SetTheme( const std::string& themeFile )
225 {
226   bool themeLoaded = false;
227
228   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
229
230   // Always load the default theme first, then merge in the custom theme if present
231   themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
232
233   if( ! themeFile.empty() )
234   {
235     mThemeFile = themeFile;
236     themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
237   }
238
239   if( themeLoaded )
240   {
241     if(mFeedbackStyle)
242     {
243       mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
244     }
245
246     EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
247   }
248   else
249   {
250     mThemeBuilder.Reset();
251   }
252 }
253
254 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
255 {
256   DALI_ASSERT_DEBUG( 0 != filename.length());
257
258   // as toolkit is platform agnostic, it cannot load files from filesystem
259   // ask style monitor to load the style sheet
260   if( mStyleMonitor )
261   {
262     return mStyleMonitor.LoadThemeFile( filename, stringOut );
263   }
264
265   return false;
266 }
267
268 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
269 {
270   Toolkit::Builder builder = Toolkit::Builder::New();
271   builder.AddConstants( constants );
272
273   return builder;
274 }
275
276 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
277 {
278   std::string fileString;
279   if( LoadFile( jsonFilePath, fileString ) )
280   {
281     builder.LoadFromString( fileString );
282     return true;
283   }
284   else
285   {
286     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
287     return false;
288   }
289 }
290
291 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
292 {
293   // Append the relevant qualifier for orientation
294   int orientation = 0; // Get the orientation from the system
295   switch( orientation )
296   {
297     case 90:
298     case 270:
299     {
300       qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
301       break;
302     }
303     case 180:
304     case 0: // fall through
305     default:
306     {
307       qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
308       break;
309     }
310   }
311 }
312
313 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
314 {
315   qualifiedStyleOut.append( styleName );
316
317   for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
318   {
319     const std::string& str = *it;
320
321     qualifiedStyleOut.append( "-" );
322     qualifiedStyleOut.append( str );
323   }
324 }
325
326 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
327 {
328   std::string styleName = control.GetStyleName();
329
330   if( styleName.empty() )
331   {
332     // Convert control name to lower case
333     styleName = control.GetTypeName();
334     std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
335   }
336
337   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
338   StringList qualifiers;
339   CollectQualifiers( qualifiers );
340
341   while( true )
342   {
343     std::string qualifiedStyleName;
344     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
345
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 )
348     {
349       break;
350     }
351
352     // Remove the last qualifier in an attempt to find a style that is valid
353     qualifiers.pop_back();
354   }
355
356   if( mDefaultFontSize >= 0 )
357   {
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 );
362   }
363 }
364
365 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
366 {
367   BuilderMap::iterator builderIt = mBuilderCache.find( key );
368   if( builderIt != mBuilderCache.end() )
369   {
370     return builderIt->second;
371   }
372
373   return Toolkit::Builder();
374 }
375
376 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
377 {
378   mBuilderCache[ key ] = builder;
379 }
380
381 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
382 {
383   switch ( styleChange )
384   {
385     case StyleChange::DEFAULT_FONT_CHANGE:
386     {
387       mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
388       break;
389     }
390
391     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
392     {
393       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
394       break;
395     }
396
397     case StyleChange::THEME_CHANGE:
398     {
399       SetTheme( styleMonitor.GetTheme() );
400       break;
401     }
402   }
403   EmitStyleChangeSignals( styleChange );
404 }
405
406 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
407 {
408   Toolkit::StyleManager styleManager = StyleManager::Get();
409
410   // Update Controls first
411   mControlStyleChangeSignal.Emit( styleManager, styleChange );
412
413   // Inform application last
414   mStyleChangedSignal.Emit( styleManager, styleChange );
415 }
416
417
418 } // namespace Internal
419
420 } // namespace Toolkit
421
422 } // namespace Dali