Merge "TextSelectionPopup to show Paste in data in Clipboard" 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/devel-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/devel-api/styling/style-manager.h>
30
31 namespace
32 {
33
34 const char* LANDSCAPE_QUALIFIER = "landscape";
35 const char* PORTRAIT_QUALIFIER  = "portrait";
36 const char* FONT_SIZE_QUALIFIER = "font-size-";
37
38 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
39
40 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
41 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
42
43 } // namespace
44
45 namespace Dali
46 {
47
48 namespace Toolkit
49 {
50
51 namespace Internal
52 {
53
54 namespace
55 {
56
57 BaseHandle Create()
58 {
59   BaseHandle handle = StyleManager::Get();
60
61   if ( !handle )
62   {
63     SingletonService singletonService( SingletonService::Get() );
64     if ( singletonService )
65     {
66       Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
67       singletonService.Register( typeid( manager ), manager );
68       handle = manager;
69     }
70   }
71
72   return handle;
73 }
74
75 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
76 DALI_TYPE_REGISTRATION_END()
77
78 } // namespace
79
80 Toolkit::StyleManager StyleManager::Get()
81 {
82   Toolkit::StyleManager manager;
83
84   SingletonService singletonService( SingletonService::Get() );
85   if ( singletonService )
86   {
87     // Check whether the style manager is already created
88     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
89     if( handle )
90     {
91       // If so, downcast the handle of singleton
92       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
93     }
94   }
95
96   return manager;
97 }
98
99 StyleManager::StyleManager()
100 : mOrientationDegrees( 0 ),  // Portrait
101   mDefaultFontSize( -1 ),
102   mThemeFile( DEFAULT_THEME )
103 {
104   // Add theme builder constants
105   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
106
107   mStyleMonitor = StyleMonitor::Get();
108   if( mStyleMonitor )
109   {
110     mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
111
112     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
113   }
114 }
115
116 StyleManager::~StyleManager()
117 {
118 }
119
120 void StyleManager::SetOrientationValue( int orientation )
121 {
122   if( orientation !=  mOrientationDegrees )
123   {
124     mOrientationDegrees = orientation;
125     // TODO: if orientation changed, apply the new style to all controls
126     // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
127     SetTheme();
128   }
129 }
130
131 int StyleManager::GetOrientationValue()
132 {
133   return mOrientationDegrees;
134 }
135
136 void StyleManager::SetOrientation( Orientation orientation )
137 {
138   if( mOrientation )
139   {
140     mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
141   }
142
143   OnOrientationChanged( orientation );
144
145   if( mOrientation )
146   {
147     mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
148   }
149 }
150
151 Orientation StyleManager::GetOrientation()
152 {
153   return mOrientation;
154 }
155
156 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
157 {
158   mStyleBuilderConstants[ key ] = value;
159 }
160
161 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
162 {
163   Property::Value* value = mStyleBuilderConstants.Find( key );
164   if( value )
165   {
166     valueOut = *value;
167     return true;
168   }
169
170   return false;
171 }
172
173 void StyleManager::OnOrientationChanged( Orientation orientation )
174 {
175   mOrientation = orientation;
176   // TODO: if orientation changed, apply the new style to all controls
177   // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
178   SetTheme();
179 }
180
181 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
182 {
183   Toolkit::Builder builder = Toolkit::Builder::New();
184   builder.AddConstants( constants );
185
186   return builder;
187 }
188
189 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
190 {
191   std::string fileString;
192   if( LoadFile( jsonFilePath, fileString ) )
193   {
194     builder.LoadFromString( fileString );
195     return true;
196   }
197   else
198   {
199     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
200     return false;
201   }
202 }
203
204 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
205 {
206   // Append the relevant qualifier for orientation
207   int orientation = mOrientationDegrees;
208
209   if( mOrientation )
210   {
211     orientation = mOrientation.GetDegrees();
212   }
213
214   switch( orientation )
215   {
216     case 90:
217     case 270:
218     {
219       qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
220       break;
221     }
222     case 180:
223     case 0: // fall through
224     default:
225     {
226       qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
227       break;
228     }
229   }
230 }
231
232 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
233 {
234   qualifiedStyleOut.append( styleName );
235
236   for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
237   {
238     const std::string& str = *it;
239
240     qualifiedStyleOut.append( "-" );
241     qualifiedStyleOut.append( str );
242   }
243 }
244
245 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
246 {
247   std::string styleName = control.GetStyleName();
248   if( styleName.empty() )
249   {
250     // Convert control name to lower case
251     styleName = control.GetTypeName();
252     std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
253   }
254
255   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
256   StringList qualifiers;
257   CollectQualifiers( qualifiers );
258
259   while( true )
260   {
261     std::string qualifiedStyleName;
262     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
263
264     // Break if style found or we have tried the root style name (qualifiers is empty)
265     if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
266     {
267       break;
268     }
269
270     // Remove the last qualifier in an attempt to find a style that is valid
271     qualifiers.pop_back();
272   }
273
274   if( mDefaultFontSize >= 0 )
275   {
276     // Apply the style for logical font size
277     std::stringstream fontSizeQualifier;
278     fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
279     builder.ApplyStyle( fontSizeQualifier.str(), control );
280   }
281 }
282
283 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
284 {
285   if( mThemeBuilder )
286   {
287     ApplyStyle( mThemeBuilder, control );
288   }
289 }
290
291 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
292 {
293   bool builderReady = false;
294
295   // First look in the cache
296   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
297   if( builder )
298   {
299     builderReady = true;
300   }
301   else
302   {
303     // Merge theme and style constants
304     Property::Map constants( mThemeBuilderConstants );
305     constants.Merge( mStyleBuilderConstants );
306
307     // Create it
308     builder = CreateBuilder( constants );
309
310     if( LoadJSON( builder, jsonFileName ) )
311     {
312       CacheBuilder( builder, jsonFileName );
313       builderReady = true;
314     }
315   }
316
317   // Apply the style to the control
318   if( builderReady )
319   {
320     builder.ApplyStyle( styleName, control );
321   }
322 }
323
324 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
325 {
326   DALI_ASSERT_DEBUG( 0 != filename.length());
327
328   // as toolkit is platform agnostic, it cannot load files from filesystem
329   // ask style monitor to load the style sheet
330   if( mStyleMonitor )
331   {
332     return mStyleMonitor.LoadThemeFile( filename, stringOut );
333   }
334
335   return false;
336 }
337
338 Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
339 {
340   return mStyleChangeSignal;
341 }
342
343 void StyleManager::RequestThemeChange( const std::string& themeFile )
344 {
345   mThemeFile = themeFile;
346
347   // need to do style change synchronously as app might create a UI control on the next line
348   SetTheme();
349 }
350
351 void StyleManager::RequestDefaultTheme()
352 {
353   RequestThemeChange( DEFAULT_THEME );
354 }
355
356 void StyleManager::SetTheme()
357 {
358   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
359   if ( LoadJSON( mThemeBuilder, mThemeFile ) )
360   {
361     mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
362   }
363   else
364   {
365     mThemeBuilder.Reset();
366   }
367 }
368
369 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
370 {
371   BuilderMap::iterator builderIt = mBuilderCache.find( key );
372   if( builderIt != mBuilderCache.end() )
373   {
374     return builderIt->second;
375   }
376
377   return Toolkit::Builder();
378 }
379
380 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
381 {
382   mBuilderCache[ key ] = builder;
383 }
384
385 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
386 {
387   switch ( styleChange )
388   {
389     case StyleChange::DEFAULT_FONT_CHANGE:
390     {
391       break;
392     }
393
394     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
395     {
396       mDefaultFontSize = styleMonitor.GetDefaultFontSize();
397       break;
398     }
399
400     case StyleChange::THEME_CHANGE:
401     {
402       const std::string& newTheme = styleMonitor.GetTheme();
403       if( ! newTheme.empty() )
404       {
405         mThemeFile = newTheme;
406       }
407       else
408       {
409         mThemeFile = DEFAULT_THEME;
410       }
411
412       SetTheme();
413       break;
414     }
415   }
416
417   mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
418 }
419
420 } // namespace Internal
421
422 } // namespace Toolkit
423
424 } // namespace Dali