Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_LabelImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @file                        FUiCtrl_LabelImpl.cpp
19  * @brief               This is the implementation file for the _LabelImpl class.
20  */
21
22 #include <FAppApplication.h>
23 #include <FBaseSysLog.h>
24 #include "FUi_ResourceSizeInfo.h"
25 #include "FUi_ResourceManager.h"
26 #include "FUi_UiBuilder.h"
27 #include "FUiCtrl_LabelImpl.h"
28 #include "FUiCtrl_Label.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Graphics;
33
34 namespace Tizen { namespace Ui { namespace Controls
35 {
36
37 Dimension
38 _LabelImpl::LabelSizeInfo::GetDefaultMinimumSize(_ControlOrientation orientation) const
39 {
40         result r = E_SUCCESS;
41         Dimension dimension(0, 0);
42
43         r = GET_DIMENSION_CONFIG(LABEL::MIN_SIZE, orientation, dimension);
44         SysTryReturn(NID_UI, r == E_SUCCESS, dimension, r, "[%s] A system error occurred.", GetErrorMessage(r));
45
46         SetLastResult(r);
47
48         return dimension;
49 }
50
51 _LabelImpl*
52 _LabelImpl::GetInstance(Label& label)
53 {
54         return static_cast<_LabelImpl*> (label._pControlImpl);
55 }
56
57 const _LabelImpl*
58 _LabelImpl::GetInstance(const Label& label)
59 {
60         return static_cast<const _LabelImpl*> (label._pControlImpl);
61 }
62
63 _LabelImpl::_LabelImpl(Label* pPublic, _Label* pCore)
64         : _ControlImpl(pPublic, pCore)
65 {
66         ClearLastResult();
67         SetFocusableChangable(false);
68 }
69
70 _LabelImpl::~_LabelImpl(void)
71 {
72         ClearLastResult();
73 }
74
75 _LabelImpl*
76 _LabelImpl::CreateLabelImplN(Label* pControl, const Rectangle& bounds)
77 {
78         result r = E_SUCCESS;
79         r = GET_SIZE_INFO(Label).CheckInitialSizeValid(Dimension(bounds.width, bounds.height), _CONTROL_ORIENTATION_PORTRAIT);
80         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] The given size is not valid.");
81
82         _Label* pCore = _Label::CreateLabelN();
83         SysTryReturn(NID_UI_CTRL, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
84
85         _LabelImpl* pImpl = new (std::nothrow) _LabelImpl(pControl, pCore);
86         r = _ControlImpl::CheckConstruction(pCore, pImpl);
87         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
88
89         r = pImpl->InitializeBoundsProperties(GET_SIZE_INFO(Label), bounds, pCore->GetOrientation());
90         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
91
92         pImpl->SetBackgroundColor(Color(0, 0, 0, 0)); // for transparent
93
94         ClearLastResult();
95
96         return pImpl;
97
98 CATCH:
99         delete pImpl;
100         return null;
101 }
102
103 Color
104 _LabelImpl::GetColorOnError(void)
105 {
106         return Color(0XFFFFFFFF);
107 }
108
109 const char*
110 _LabelImpl::GetPublicClassName(void) const
111 {
112         return "Tizen::Ui::Controls::Label";
113 }
114
115 const Label&
116 _LabelImpl::GetPublic(void) const
117 {
118         return static_cast <const Label&>(_ControlImpl::GetPublic());
119 }
120
121 Label&
122 _LabelImpl::GetPublic(void)
123 {
124         return static_cast <Label&>(_ControlImpl::GetPublic());
125 }
126
127 const _Label&
128 _LabelImpl::GetCore(void) const
129 {
130         return static_cast <const _Label&>(_ControlImpl::GetCore());
131 }
132
133 _Label&
134 _LabelImpl::GetCore(void)
135 {
136         return static_cast <_Label&>(_ControlImpl::GetCore());
137 }
138
139 result
140 _LabelImpl::OnBoundsChanged(const Rectangle& oldRect, const Rectangle& newRect)
141 {
142         return E_SUCCESS;
143 }
144
145 Tizen::Graphics::Dimension
146 _LabelImpl::GetContentSize(void) const
147 {
148         return GetCore().GetContentSizeInternal();
149 }
150
151 result
152 _LabelImpl::SetText(const String& text)
153 {
154         Variant var(text);
155         result r = GetCore().SetPropertyText(var);
156         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
157
158         return r;
159 }
160
161 result
162 _LabelImpl::SetTextColor(const Color& color)
163 {
164         result r = GetCore().SetTextColor(color);
165         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
166
167         return r;
168 }
169
170 result
171 _LabelImpl::SetBackgroundBitmap(const Bitmap& bitmap)
172 {
173         result r = GetCore().SetBackgroundBitmap(bitmap);
174         SetLastResultReturn(r);
175 }
176
177 result
178 _LabelImpl::SetTextHorizontalAlignment(HorizontalAlignment alignment)
179 {
180         result r = GetCore().SetTextHorizontalAlignment(alignment);
181         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
182
183         return r;
184 }
185
186 result
187 _LabelImpl::SetTextVerticalAlignment(VerticalAlignment alignment)
188 {
189         result r = GetCore().SetTextVerticalAlignment(alignment);
190         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
191
192         return r;
193 }
194
195 result
196 _LabelImpl::SetTextConfig(int size, LabelTextStyle style)
197 {
198         result r = GetCore().SetTextConfig(size, style);
199         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
200
201         return r;
202 }
203
204 result
205 _LabelImpl::SetMargin(int topMargin, int leftMargin)
206 {
207         result r = GetCore().SetMargin(topMargin, leftMargin);
208         SetLastResultReturn(r);
209 }
210
211 String
212 _LabelImpl::GetText(void) const
213 {
214         ClearLastResult();
215
216         return GetCore().GetPropertyText().ToString();
217 }
218
219 HorizontalAlignment
220 _LabelImpl::GetTextHorizontalAlignment(void) const
221 {
222         ClearLastResult();
223
224         return GetCore().GetTextHorizontalAlignment();
225 }
226
227 VerticalAlignment
228 _LabelImpl::GetTextVerticalAlignment(void) const
229 {
230         ClearLastResult();
231
232         return GetCore().GetTextVerticalAlignment();
233 }
234
235 Color
236 _LabelImpl::GetTextColor(void) const
237 {
238         ClearLastResult();
239
240         return GetCore().GetTextColor();
241 }
242
243 int
244 _LabelImpl::GetTextSize(void) const
245 {
246         ClearLastResult();
247
248         return GetCore().GetTextSize();
249 }
250
251 LabelTextStyle
252 _LabelImpl::GetTextStyle(void) const
253 {
254         ClearLastResult();
255
256         return GetCore().GetTextStyle();
257 }
258
259 int
260 _LabelImpl::GetTopMargin(void) const
261 {
262         ClearLastResult();
263
264         return GetCore().GetTopMargin();
265 }
266
267 int
268 _LabelImpl::GetLeftMargin(void) const
269 {
270         ClearLastResult();
271
272         return GetCore().GetLeftMargin();
273 }
274
275 class _LabelMaker
276         : public _UiBuilderControlMaker
277 {
278 public:
279         _LabelMaker(_UiBuilder* uibuilder)
280                 : _UiBuilderControlMaker(uibuilder){};
281         virtual ~_LabelMaker(){};
282         static _UiBuilderControlMaker* GetInstance(_UiBuilder* uibuilder)
283         {
284                 _LabelMaker* pLabelMaker = new (std::nothrow) _LabelMaker(uibuilder);
285                 return static_cast<_UiBuilderControlMaker*>(pLabelMaker);
286         };
287 protected:
288         virtual Control* Make(_UiBuilderControl* pControl)
289         {
290 //              bool b = false;
291 //              bool bText = false;
292 //              bool bSoftkeyCount = false;
293                 result r = E_SYSTEM;
294                 _UiBuilderControlLayout* pControlProperty = null;
295                 Label* pLabel = null;
296                 Rectangle rect;
297
298                 AppResource* pAppResource = null;
299                 UiApp* pUiApp = UiApp::GetInstance();
300                 if (pUiApp)
301                 {
302                         pAppResource = UiApp::GetInstance()->GetAppResource();
303                 }
304
305                 HorizontalAlignment horizontalAlignment;
306                 VerticalAlignment verticalAlignment;
307                 Tizen::Base::String elementString;
308                 int opacity = 0;
309                 int size = 0;
310                 LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
311                 Color color;
312
313                 GetProperty(pControl, &pControlProperty);
314                 if (pControlProperty == null)
315                 {
316                         return null;
317                 }
318
319                 pLabel = new (std::nothrow) Label();
320                 rect = pControlProperty->GetRect();
321
322                 //Construct
323                 if (pControl->GetElement(L"text", elementString))
324                 {
325                         r = pLabel->Construct(rect, elementString);
326                 }
327                 else
328                 {
329                         r = pLabel->Construct(rect, String(L""));
330                 }
331
332                 if (r != E_SUCCESS)
333                 {
334                         delete pLabel;
335                         return null;
336                 }
337
338                 //setting
339                 if (pControl->GetElement(L"horizontalAlign", elementString) || pControl->GetElement(L"hAlign", elementString))
340                 {
341                         if (ConvertHAlignToHorizontalAlignment(elementString, horizontalAlignment))
342                         {
343                                 pLabel->SetTextHorizontalAlignment(horizontalAlignment);
344                         }
345                 }
346
347                 if (pControl->GetElement(L"verticalAlign", elementString) || pControl->GetElement(L"vAlign", elementString))
348                 {
349                         if (ConvertVAlignToVerticalAlignment(elementString, verticalAlignment))
350                         {
351                                 pLabel->SetTextVerticalAlignment(verticalAlignment);
352                         }
353                 }
354
355                 if (pControl->GetElement(L"backgroundColorOpacity", elementString) || pControl->GetElement(L"BGColorOpacity", elementString))
356                 {
357                         Base::Integer::Parse(elementString, opacity);
358                 }
359
360                 if (pControl->GetElement(L"backgroundColor", elementString) || pControl->GetElement(L"BGColor", elementString))
361                 {
362                         ConvertStringToColor32(elementString, opacity, color);
363                         pLabel->SetBackgroundColor(color);
364                 }
365                 else
366                 {
367                         color = pLabel->GetBackgroundColor();
368                         color.SetAlpha(ConvertOpacity100to255(opacity));
369                         pLabel->SetBackgroundColor(color);
370                 }
371
372                 if (pControl->GetElement(L"textColor", elementString))
373                 {
374                         ConvertStringToColor(elementString, color);
375                         pLabel->SetTextColor(color);
376                 }
377
378                 if (pControl->GetElement(L"backgroundBitmapPath", elementString) || pControl->GetElement(L"BGBitmapPath", elementString))
379                 {
380                         Bitmap* pBackgroundBitmap = null;
381                         pBackgroundBitmap = pAppResource->GetBitmapN(elementString);
382                         if (pBackgroundBitmap != null)
383                         {
384                                 pLabel->SetBackgroundBitmap(*pBackgroundBitmap);
385                                 delete pBackgroundBitmap;
386                         }
387                 }
388
389                 if (pControl->GetElement(L"textSize", elementString))
390                 {
391                         Base::Integer::Parse(elementString, size);
392                         _ICoordinateSystemTransformer* pTransform = GetTransformer();
393                         if (pTransform)
394                         {
395                                 size = pTransform->Transform(size);
396                         }
397
398                 }
399
400                 if (pControl->GetElement(L"textStyle", elementString))
401                 {
402                         if (elementString.Equals(L"LABEL_TEXT_STYLE_NORMAL", false))
403                         {
404                                 style = LABEL_TEXT_STYLE_NORMAL;
405                         }
406                         else if (elementString.Equals(L"LABEL_TEXT_STYLE_BOLD", false))
407                         {
408                                 style = LABEL_TEXT_STYLE_BOLD;
409                         }
410                         else if (elementString.Equals(L"LABEL_TEXT_STYLE_ITALIC", false))
411                         {
412                                 style = LABEL_TEXT_STYLE_ITALIC;
413                         }
414                 }
415
416                 if (size != 0)
417                 {
418                         pLabel->SetTextConfig(size, style);
419                 }
420                 else
421                 {
422                         pLabel->SetTextConfig(pLabel->GetTextSize(), style);
423                 }
424
425                 //-------safety for Minimum size-------------------
426                 rect = (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT))->GetRect();
427                 if (rect.width < 0) // LABEL_MIN_WIDTH = 0
428                 {
429                         rect.width = 0;
430                 }
431
432                 if (rect.height < 0)
433                 {
434                         rect.height = 0;
435                 }
436
437                 (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT))->SetRect(rect.x, rect.y, rect.width, rect.height);
438
439                 rect = (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE))->GetRect();
440                 if (rect.width < 0)
441                 {
442                         rect.width = 0;
443                 }
444
445                 if (rect.height < 0) // LABEL_MIN_HEIGHT = 0
446                 {
447                         rect.height = 0;
448                 }
449                 (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE))->SetRect(rect.x, rect.y, rect.width, rect.height);
450                 //---------end safety code------------------------
451
452                 return pLabel;
453         }
454 private:
455 }; // _LabelMaker
456
457 _LabelRegister::_LabelRegister()
458 {
459           _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
460           pUiBuilderControlTableManager->RegisterControl(L"Label", _LabelMaker::GetInstance);
461 }
462 _LabelRegister::~_LabelRegister()
463 {
464           _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
465           pUiBuilderControlTableManager->UnregisterControl(L"Label");
466 }
467 static _LabelRegister LabelRegisterToUiBuilder;
468 }}} // Tizen::Ui::Controls