TextInput Popup has a tail which points to the TextInput
[platform/core/uifw/dali-toolkit.git] / base / 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 // INTERNAL INCLUDES
21 #include <dali-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/styling/style-manager.h>
24 #include <dali-toolkit/internal/styling/util.h>
25 #include <dali/integration-api/debug.h>
26
27 // EXTERNAL INCLUDES
28 #include <fstream>
29 #include <iostream>
30 #include <sstream>
31
32
33 namespace
34 {
35
36 const char* LANDSCAPE_QUALIFIER = "landscape";
37 const char* PORTRAIT_QUALIFIER  = "portrait";
38
39 const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-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 && Adaptor::IsAvailable() )
63   {
64     Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
65     Adaptor::Get().RegisterSingleton( typeid( manager ), manager );
66     handle = manager;
67   }
68
69   return handle;
70 }
71 TypeRegistration STYLE_MANAGER_TYPE( typeid(Dali::Toolkit::StyleManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
72
73 /**
74  * Merge two maps into one
75  */
76 void MergeMaps( const PropertyValueMap& a, const PropertyValueMap& b, PropertyValueMap& out )
77 {
78   out = a;
79   for( PropertyValueMap::const_iterator it = b.begin(), itEnd = b.end(); it != itEnd; ++it )
80   {
81     out[ it->first ] = it->second;
82   }
83 }
84
85 } // namespace
86
87 Toolkit::StyleManager StyleManager::Get()
88 {
89   Toolkit::StyleManager manager;
90
91   if ( Adaptor::IsAvailable() )
92   {
93     // Check whether the style manager is already created
94     Dali::BaseHandle handle = Dali::Adaptor::Get().GetSingleton( typeid( Toolkit::StyleManager ) );
95     if( handle )
96     {
97       // If so, downcast the handle of singleton
98       manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
99     }
100   }
101
102   return manager;
103 }
104
105 StyleManager::StyleManager()
106   : mOrientationDegrees( 0 ),  // Portrait
107     mSetThemeConnection( false )
108 {
109   // Add theme builder constants
110   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
111
112   RequestDefaultTheme();
113
114   if( Adaptor::IsAvailable() )
115   {
116     StyleMonitor::Get().StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
117   }
118 }
119
120 StyleManager::~StyleManager()
121 {
122   // Disconnect from signal
123   SetOrientation( Orientation() );
124 }
125
126 void StyleManager::SetOrientationValue( int orientation )
127 {
128   mOrientationDegrees = orientation;
129
130   Util::ConnectEventProcessingFinishedSignal();
131   mSetThemeConnection = true;
132 }
133
134 int StyleManager::GetOrientationValue()
135 {
136   return mOrientationDegrees;
137 }
138
139 void StyleManager::SetOrientation( Orientation orientation )
140 {
141   if( mOrientation )
142   {
143     mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
144   }
145
146   OnOrientationChanged( orientation );
147
148   if( mOrientation )
149   {
150     mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
151   }
152 }
153
154 Orientation StyleManager::GetOrientation()
155 {
156   return mOrientation;
157 }
158
159 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
160 {
161   mStyleBuilderConstants[ key ] = value;
162 }
163
164 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
165 {
166   Toolkit::PropertyValueMap::iterator valueIt = mStyleBuilderConstants.find( key );
167   if( valueIt != mStyleBuilderConstants.end() )
168   {
169     valueOut = valueIt->second;
170     return true;
171   }
172
173   return false;
174 }
175
176 void StyleManager::OnOrientationChanged( Orientation orientation )
177 {
178   mOrientation = orientation;
179
180   if( mOrientation )
181   {
182     Util::ConnectEventProcessingFinishedSignal();
183     mSetThemeConnection = true;
184   }
185 }
186
187 Toolkit::Builder StyleManager::CreateBuilder( const PropertyValueMap& constants )
188 {
189   Toolkit::Builder builder = Toolkit::Builder::New();
190   builder.AddConstants( constants );
191
192   return builder;
193 }
194
195 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
196 {
197   std::string fileString;
198   if( LoadFile( jsonFilePath, fileString ) )
199   {
200     builder.LoadFromString( fileString );
201     return true;
202   }
203   else
204   {
205     DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
206     return false;
207   }
208 }
209
210 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
211 {
212   // Append the relevant qualifier for orientation
213   int orientation = mOrientationDegrees;
214
215   if( mOrientation )
216   {
217     orientation = mOrientation.GetDegrees();
218   }
219
220   switch( orientation )
221   {
222     case 90:
223     case 270:
224     {
225       qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
226       break;
227     }
228     case 180:
229     case 0: // fall through
230     default:
231     {
232       qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
233       break;
234     }
235   }
236 }
237
238 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
239 {
240   qualifiedStyleOut.append( styleName );
241
242   for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
243   {
244     const std::string& str = *it;
245
246     qualifiedStyleOut.append( "-" );
247     qualifiedStyleOut.append( str );
248   }
249 }
250
251 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
252 {
253   // Convert control name to lower case
254   std::string styleName = control.GetTypeName();
255   std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
256
257   // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
258   StringList qualifiers;
259   CollectQualifiers( qualifiers );
260
261   while( true )
262   {
263     std::string qualifiedStyleName;
264     BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
265
266     // Break if style found or we have tried the root style name (qualifiers is empty)
267     if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
268     {
269       break;
270     }
271
272     // Remove the last qualifier in an attempt to find a style that is valid
273     qualifiers.pop_back();
274   }
275 }
276
277 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
278 {
279   if( mThemeBuilder )
280   {
281     ApplyStyle( mThemeBuilder, control );
282   }
283 }
284
285 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
286 {
287   bool builderReady = false;
288
289   // First look in the cache
290   Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
291   if( builder )
292   {
293     builderReady = true;
294   }
295   else
296   {
297     // Merge theme and style constants
298     PropertyValueMap constants;
299     MergeMaps( mThemeBuilderConstants, mStyleBuilderConstants, constants );
300
301     // Create it
302     builder = CreateBuilder( constants );
303
304     if( LoadJSON( builder, jsonFileName ) )
305     {
306       CacheBuilder( builder, jsonFileName );
307       builderReady = true;
308     }
309   }
310
311   // Apply the style to the control
312   if( builderReady )
313   {
314     builder.ApplyStyle( styleName, control );
315   }
316 }
317
318 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
319 {
320   DALI_ASSERT_DEBUG( 0 != filename.length());
321
322   std::ifstream in( filename.c_str(), std::ios::in );
323   if( in )
324   {
325     std::stringstream buffer;
326     buffer << in.rdbuf();
327
328     stringOut = buffer.str();
329
330     in.close();
331
332     return true;
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   Util::ConnectEventProcessingFinishedSignal();
348   mSetThemeConnection = true;
349 }
350
351 void StyleManager::RequestDefaultTheme()
352 {
353   RequestThemeChange( DEFAULT_THEME );
354 }
355
356 bool StyleManager::IsThemeRequestPending()
357 {
358   return mSetThemeConnection;
359 }
360
361 void StyleManager::SetTheme()
362 {
363   mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
364   LoadJSON( mThemeBuilder, mThemeFile );
365
366   mSetThemeConnection = false;
367
368   StyleChange change;
369   change.themeChange = true;
370   mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change );
371 }
372
373 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
374 {
375   BuilderMap::iterator builderIt = mBuilderCache.find( key );
376   if( builderIt != mBuilderCache.end() )
377   {
378     return builderIt->second;
379   }
380
381   return Toolkit::Builder();
382 }
383
384 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
385 {
386   mBuilderCache[ key ] = builder;
387 }
388
389 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange )
390 {
391   mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
392 }
393
394 } // namespace Internal
395
396 } // namespace Toolkit
397
398 } // namespace Dali