modify license, permission and remove ^M char
[platform/framework/native/uifw.git] / src / ui / FUi_UiBuilderControlMaker.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an ”AS IS” BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19 * @file         FUi_UiBuilderControlMaker.cpp
20 * @brief                This is the implementation file for _UiBuilderControlMaker class.
21 */
22
23 #include <app.h>
24 #include <runtime_info.h>
25 #include <FAppApplication.h>
26 #include <FAppApp.h>
27 #include <FUiContainer.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseInteger.h>
30 #include <FBase_StringConverter.h>
31 #include <FGrp_NonScale.h>
32 #include <FGrp_BitmapImpl.h>
33 #include "FUi_UiBuilderControlMaker.h"
34 #include "FUi_UiBuilder.h"
35 #include "FUi_LayoutLayoutMaker.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Controls;
40 using namespace Tizen::App;
41 using namespace Tizen::Graphics;
42
43 namespace Tizen { namespace Ui
44 {
45 _UiBuilderControlMaker::_UiBuilderControlMaker(_UiBuilder* pUibuilder)
46         : __pLayoutMaker(new(std::nothrow)_LayoutMaker(pUibuilder))
47         , __pUiBuilder(pUibuilder)
48 {
49         SysTryReturnVoidResult(NID_UI, __pLayoutMaker != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
50 }
51 _UiBuilderControlMaker::_UiBuilderControlMaker(void)
52         : __pUiBuilder(null)
53 {
54 }
55 _UiBuilderControlMaker::~_UiBuilderControlMaker(void)
56 {
57 }
58
59 result
60 _UiBuilderControlMaker::MakeControl(_UiBuilderControl* pUiBuilderControl)
61 {
62         Tizen::Ui::Control* pControl = null;
63
64         if (pUiBuilderControl != null)
65         {
66                 pControl = Make(pUiBuilderControl);
67                 if ((pUiBuilderControl->GetType()).Equals(L"Header", false) || (pUiBuilderControl->GetType()).Equals(L"Footer", false))
68                 {
69                         return E_SUCCESS;
70                 }
71                 if (pControl != null)
72                 {
73                         pControl->SetName(pUiBuilderControl->GetId());
74                         SetLayoutOrientation(pUiBuilderControl, pControl);
75                         if (!(pUiBuilderControl->GetType().Equals(L"Form", false) || pUiBuilderControl->GetType().Equals(L"Popup", false)))
76                         {
77                                 if (pUiBuilderControl->GetType().Equals(L"Panel", false) || pUiBuilderControl->GetType().Equals(L"ScrollPanel", false))
78                                 {
79                                         if (pUiBuilderControl->GetParentWin().IsEmpty())
80                                         {
81                                                 return E_SUCCESS;
82                                         }
83
84                                 }
85                                 AddControl(pUiBuilderControl, pControl);
86                                 result r = __pLayoutMaker->SetLayoutProperty(pUiBuilderControl, pControl);
87                                 if (r != E_SUCCESS)
88                                 {
89                                         return E_OPERATION_FAILED;
90                                 }
91                         }
92                         return E_SUCCESS;
93                 }
94         }
95
96         return E_OPERATION_FAILED;
97 }
98
99 void
100 _UiBuilderControlMaker::ConvertStringToColor(const Tizen::Base::String& colorString, Color& color)
101 {
102         int index = 0;
103         int len = 0;
104         int gap = 0;
105         const wchar_t* colorStringPointer = colorString.GetPointer();
106         char ch;
107         unsigned long temp = 0;
108
109         len = colorString.GetLength();
110
111         if (len < 1)
112         {
113                 return;
114         }
115         color = 0;
116
117         for (index = 0; index < len + 1; index++)
118         {
119                 ch = colorStringPointer[index];
120                 if ((ch >= '0') && (ch <= '9'))
121                 {
122                         temp = temp << 4;
123                         gap = ch - '0';
124                         temp |= gap;
125
126                 }
127                 else if ((ch >= 'A') && (ch <= 'F'))
128                 {
129                         temp = temp << 4;
130                         gap = ch - 'A' + 10;
131                         temp |= gap;
132
133                 }
134                 else if ((ch >= 'a') && (ch <= 'f'))
135                 {
136                         temp = temp << 4;
137                         gap = ch - 'a' + 10;
138                         temp |= gap;
139                 }
140         }
141         color.SetRGB32(temp, false);
142 }
143
144 void
145 _UiBuilderControlMaker::ConvertStringToColor32(const Tizen::Base::String& colorString, int opacity, Color& color)
146 {
147         int index = 0;
148         int len = 0;
149         int gap = 0;
150         int alpha = 0;
151         const wchar_t* colorStringPointer = colorString.GetPointer();
152         char ch;
153         unsigned long temp = 0;
154
155         len = colorString.GetLength();
156
157         if (len < 1)
158         {
159                 SysLog(NID_UI, "String is empty");
160                 return;
161         }
162
163         color = 0;
164
165         for (index = 0; index < len + 1; index++)
166         {
167                 ch = colorStringPointer[index];
168                 if ((ch >= '0') && (ch <= '9'))
169                 {
170                         temp = temp << 4;
171                         gap = ch - '0';
172                         temp |= gap;
173
174                 }
175                 else if ((ch >= 'A') && (ch <= 'F'))
176                 {
177                         temp = temp << 4;
178                         gap = ch - 'A' + 10;
179                         temp |= gap;
180
181                 }
182                 else if ((ch >= 'a') && (ch <= 'f'))
183                 {
184                         temp = temp << 4;
185                         gap = ch - 'a' + 10;
186                         temp |= gap;
187                 }
188         }
189
190         color.SetRGB32(temp, true);
191         alpha = ConvertOpacity100to255(opacity);
192         color.SetAlpha(alpha);
193 }
194
195 int
196 _UiBuilderControlMaker::ConvertOpacity100to255(int opacity)
197 {
198         return 255 * opacity / 100;
199 }
200
201 bool
202 _UiBuilderControlMaker::ConvertHAlignToHorizontalAlignment(const Tizen::Base::String& align, HorizontalAlignment& alignment)
203 {
204         if (align.Equals(L"ALIGN_LEFT", false))
205         {
206                 alignment = ALIGNMENT_LEFT;
207         }
208         else if (align.Equals(L"ALIGN_CENTER", false))
209         {
210                 alignment = ALIGNMENT_CENTER;
211         }
212         else if (align.Equals(L"ALIGN_RIGHT", false))
213         {
214                 alignment = ALIGNMENT_RIGHT;
215         }
216         else
217         {
218                 return false;
219         }
220
221         return true;
222 }
223
224 bool
225 _UiBuilderControlMaker::ConvertVAlignToVerticalAlignment(const Tizen::Base::String& align, VerticalAlignment& alignment)
226 {
227         if (align.Equals(L"ALIGN_TOP", false))
228         {
229                 alignment = ALIGNMENT_TOP;
230         }
231         else if (align.Equals(L"ALIGN_MIDDLE", false))
232         {
233                 alignment = ALIGNMENT_MIDDLE;
234         }
235         else if (align.Equals(L"ALIGN_BOTTOM", false))
236         {
237                 alignment = ALIGNMENT_BOTTOM;
238         }
239         else
240         {
241                 return false;
242         }
243
244         return true;
245 }
246
247 bool
248 _UiBuilderControlMaker::ConvertSlideShowAnimation(const Tizen::Base::String& animationType, GalleryAnimation& animation)
249 {
250         if (animationType.Equals(L"GALLERY_ANIMATION_PAGE", false))
251         {
252                 animation = GALLERY_ANIMATION_PAGE;
253         }
254         else if (animationType.Equals(L"GALLERY_ANIMATION_DISSOLVE", false))
255         {
256                 animation = GALLERY_ANIMATION_DISSOLVE;
257         }
258         else if (animationType.Equals(L"GALLERY_ANIMATION_ZOOM", false))
259         {
260                 animation = GALLERY_ANIMATION_ZOOM;
261         }
262         else
263         {
264                 return false;
265         }
266
267         return true;
268 }
269
270 Tizen::Graphics::Bitmap*
271 _UiBuilderControlMaker::LoadBitmapN(const Tizen::Base::String& imagePath)
272 {
273     AppResource* pAppResource = Tizen::App::App::GetInstance()->GetAppResource();
274     if (pAppResource != null)
275     {
276         Tizen::Graphics::Bitmap* pBitmap = pAppResource->GetBitmapN(imagePath);
277         return pBitmap;
278     }
279
280         return null;
281 }
282 void
283 _UiBuilderControlMaker::GetProperty(_UiBuilderControl* pControl, _UiBuilderControlLayout** pControlProperty) const
284 {
285         // Form's orientation mode is AUTOMATIC
286         if (__pUiBuilder->GetUiBuilderRotateState() == UIBUIDER_SCREEN_NONE)
287         {
288                 // Get device orientation
289
290                 bool autoRotateLock = false;
291                 int ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, &autoRotateLock);
292
293                 if (ret == RUNTIME_INFO_ERROR_NONE)
294                 {
295                         SysLog(NID_UI, "The flag of auto-rotate lock is %d.", autoRotateLock);
296                 }
297                 else
298                 {
299                         autoRotateLock = false;
300                         SysLog(NID_UI, "It's failed to get the flag of auto-rotate.");
301                 }
302
303                 app_device_orientation_e deviceOrientation = app_get_device_orientation();
304                 bool isHorizontal = (deviceOrientation == APP_DEVICE_ORIENTATION_90) || (deviceOrientation == APP_DEVICE_ORIENTATION_270);
305
306                 if (isHorizontal && autoRotateLock == true)
307                 {
308                         __pUiBuilder->SetUiBuilderRotateState(UIBUIDER_SCREEN_HORIZONTAL);
309                 }
310                 else
311                 {
312                         __pUiBuilder->SetUiBuilderRotateState(UIBUIDER_SCREEN_VERTICAL);
313                 }
314         }
315
316         if (__pUiBuilder->GetUiBuilderRotateState() == UIBUIDER_SCREEN_HORIZONTAL)
317         {
318                 *pControlProperty = pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE);
319         }
320         else
321         {
322                 *pControlProperty = pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT);
323         }
324
325         return;
326 }
327
328 void
329 _UiBuilderControlMaker::AddControl(_UiBuilderControl* pUiBuilderControl, Tizen::Ui::Control* pControl)
330 {
331         Tizen::Base::String panePosition;
332         Tizen::Ui::Container* pContainer = GetContainer();
333         Tizen::Ui::Control* pFindControl = null;
334
335
336         //Find Container
337         if (pContainer->GetName().Equals(pUiBuilderControl->GetParentWin()))
338         {
339                 pFindControl = pContainer;
340         }
341         else
342         {
343                 pFindControl = pContainer->GetControl(Tizen::Base::String(pUiBuilderControl->GetParentWin()), true);
344         }
345
346         //Add SplitPanel child
347         if (pUiBuilderControl->GetElement(L"controlPosition", panePosition))
348         {
349
350                 Tizen::Ui::Controls::SplitPanel *pSplitPanel = dynamic_cast<SplitPanel*>(pFindControl);
351                 if (pSplitPanel)
352                 {
353                         if(panePosition.Equals(L"first", false))
354                         {
355                                 pSplitPanel->SetPane(pControl, SPLIT_PANEL_PANE_ORDER_FIRST);
356                         }
357                         else
358                         {
359                                 pSplitPanel->SetPane(pControl, SPLIT_PANEL_PANE_ORDER_SECOND);
360                         }
361                 }
362         }
363
364         //Add Container chid
365         Tizen::Ui::Container* pParent = dynamic_cast<Tizen::Ui::Container*>(pFindControl);
366
367         if (pParent != null)
368         {
369                 pParent->AddControl(*pControl);
370         }
371 }
372
373 Tizen::Ui::Container*
374 _UiBuilderControlMaker::GetContainer(void) const
375 {
376         return __pUiBuilder->GetContainer();
377 }
378
379 void
380 _UiBuilderControlMaker::SetLayoutOrientation(_UiBuilderControl* pUiBuilderCOntrol, Tizen::Ui::Control* pControl)
381 {
382         __pUiBuilder->SetLayoutOrientation(pUiBuilderCOntrol, pControl);
383 }
384
385 void
386 _UiBuilderControlMaker::SetUiBuilderRotateState(_UiBuilderScreenRotate rotate)
387 {
388         return __pUiBuilder->SetUiBuilderRotateState(rotate);
389 }
390
391 _UiBuilderScreenRotate
392 _UiBuilderControlMaker::GetUiBuilderRotateState(void)
393 {
394         return __pUiBuilder->GetUiBuilderRotateState();
395 }
396
397 Tizen::Graphics::_ICoordinateSystemTransformer*
398 _UiBuilderControlMaker::GetTransformer(void) const
399 {
400         return __pUiBuilder->GetTransformer();
401 }
402 } }  // Tizen::Ui