Text - FONT_STYLE and INPUT_FONT_STYLE properties refactor.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-style-monitor.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
18 // HEADER
19 #include "toolkit-style-monitor.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/signals/dali-signal.h>
26
27 namespace
28 {
29 const char* DEFAULT_THEME=
30   "{\"styles\":{\n"
31   "  \"textlabel\":\n"
32   "    {\n"
33   "      \"fontStyle\":{\"weight\":\"normal\"},\n"
34   "      \"pointSize\":18\n"
35   "    }\n"
36   "  }\n"
37   "}\n";
38
39 struct NamedTheme
40 {
41   NamedTheme( const std::string& name, const std::string& theme )
42   : name(name), theme(theme)
43   {
44   }
45
46   std::string name;
47   std::string theme;
48 };
49 typedef std::vector< NamedTheme > NamedThemes;
50 NamedThemes gThemes;
51
52 std::string gTheme;
53 std::string gFontFamily("LucidaSans");
54 std::string gFontStyle("Regular");
55 int         gFontSize(1);
56 }
57
58 namespace Dali
59 {
60 namespace Internal
61 {
62 namespace Adaptor
63 {
64 /**
65  * Stub for the StyleMonitor
66  */
67 class StyleMonitor : public BaseObject
68 {
69 public: // Creation & Destruction
70   static Dali::StyleMonitor Get();
71   StyleMonitor();
72   ~StyleMonitor();
73
74 public: // Style Information
75   std::string GetDefaultFontFamily() const;
76   std::string GetDefaultFontStyle() const;
77   float GetDefaultFontSize() const;
78   const std::string& GetTheme() const;
79   void SetTheme(std::string theme);
80   bool LoadThemeFile( const std::string& filename, std::string& output );
81
82 public: // Signals
83   Dali::StyleMonitor::StyleChangeSignalType& StyleChangeSignal();
84
85   void EmitStyleChangeSignal(StyleChange::Type styleChange)
86   {
87     mStyleChangeSignal.Emit(Dali::StyleMonitor(this), styleChange);
88   }
89
90 private:
91   Dali::StyleMonitor::StyleChangeSignalType mStyleChangeSignal;
92   static Dali::StyleMonitor mToolkitStyleMonitor;
93
94   std::string mTheme;  ///<< Current theme name
95 };
96
97 Dali::StyleMonitor StyleMonitor::mToolkitStyleMonitor;
98
99 Dali::StyleMonitor StyleMonitor::Get()
100 {
101   if( ! mToolkitStyleMonitor )
102   {
103     mToolkitStyleMonitor = Dali::StyleMonitor( new Dali::Internal::Adaptor::StyleMonitor() );
104   }
105   return mToolkitStyleMonitor;
106 }
107
108 StyleMonitor::StyleMonitor()
109 : mTheme("default")
110 {
111 }
112
113 StyleMonitor::~StyleMonitor()
114 {
115 }
116
117 std::string StyleMonitor::GetDefaultFontFamily() const
118 {
119   return gFontFamily;
120 }
121
122 std::string StyleMonitor::GetDefaultFontStyle() const
123 {
124   return gFontStyle;
125 }
126
127 float StyleMonitor::GetDefaultFontSize() const
128 {
129   return gFontSize;
130 }
131
132 const std::string& StyleMonitor::GetTheme() const
133 {
134   return mTheme;
135 }
136
137 void StyleMonitor::SetTheme(std::string path)
138 {
139   mTheme = path;
140   EmitStyleChangeSignal( StyleChange::THEME_CHANGE );
141 }
142
143 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
144 {
145   for( NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter )
146   {
147     NamedTheme& theme = *iter;
148     if( theme.name == filename )
149     {
150       output = theme.theme;
151       return true;
152     }
153   }
154
155   if( !gTheme.empty() )
156   {
157     output = gTheme;
158   }
159   else
160   {
161     output = DEFAULT_THEME;
162   }
163   return true;
164 }
165
166 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
167 {
168   return mStyleChangeSignal;
169 }
170
171 } // namespace Adaptor
172 } // namespace Internal
173
174 ////////////////////////////////////////////////////////////////////////////////////////////////////
175
176 Internal::Adaptor::StyleMonitor& GetImplementation(Dali::StyleMonitor& monitor)
177 {
178   BaseObject& object = monitor.GetBaseObject();
179   return static_cast<Internal::Adaptor::StyleMonitor&>(object);
180 }
181 const Internal::Adaptor::StyleMonitor& GetImplementation(const Dali::StyleMonitor& monitor)
182 {
183   const BaseObject& object = monitor.GetBaseObject();
184   return static_cast<const Internal::Adaptor::StyleMonitor&>(object);
185 }
186
187 StyleMonitor::StyleMonitor()
188 {
189 }
190
191 StyleMonitor::StyleMonitor(const StyleMonitor& monitor)
192 : BaseHandle(monitor)
193 {
194 }
195
196 StyleMonitor StyleMonitor::StyleMonitor::Get()
197 {
198   return Internal::Adaptor::StyleMonitor::Get();
199 }
200
201 StyleMonitor::~StyleMonitor()
202 {
203 }
204
205 std::string StyleMonitor::GetDefaultFontFamily() const
206 {
207   return GetImplementation(*this).GetDefaultFontFamily();
208 }
209
210 std::string StyleMonitor::GetDefaultFontStyle() const
211 {
212   return GetImplementation(*this).GetDefaultFontStyle();
213 }
214
215 int StyleMonitor::GetDefaultFontSize() const
216 {
217   return GetImplementation(*this).GetDefaultFontSize();
218 }
219
220 const std::string& StyleMonitor::GetTheme() const
221 {
222   return GetImplementation(*this).GetTheme();
223 }
224
225 void StyleMonitor::SetTheme(const std::string& themeFilePath)
226 {
227   GetImplementation(*this).SetTheme(themeFilePath);
228 }
229
230 StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
231 {
232   return GetImplementation(*this).StyleChangeSignal();
233 }
234
235 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
236 {
237   return GetImplementation(*this).LoadThemeFile(filename, output);
238 }
239
240 StyleMonitor& StyleMonitor::operator=(const StyleMonitor& monitor)
241 {
242   if( *this != monitor )
243   {
244     BaseHandle::operator=(monitor);
245   }
246   return *this;
247 }
248
249 StyleMonitor::StyleMonitor(Internal::Adaptor::StyleMonitor* internal)
250 : BaseHandle(internal)
251 {
252 }
253
254 } // namespace Dali
255
256 namespace Test
257 {
258 namespace StyleMonitor
259 {
260
261 void SetThemeFileOutput( const std::string& name, const std::string& output )
262 {
263   for( NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter )
264   {
265     NamedTheme& theme = *iter;
266     if( theme.name == name )
267     {
268       theme.theme = output;
269       return;
270     }
271   }
272
273   gThemes.push_back( NamedTheme( name, output ) );
274 }
275
276 void SetDefaultFontFamily(const std::string& family)
277 {
278   gFontFamily = family;
279 }
280
281 void SetDefaultFontStyle(const std::string& style)
282 {
283   gFontStyle = style;
284 }
285
286 void SetDefaultFontSize( float size )
287 {
288   gFontSize = size;
289 }
290
291 } // StyleMonitor
292 } // Test