Changed indicator bg color.
[platform/framework/native/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 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  * @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 <FBase_LocalizedNumParser.h>
25 #include <FUiAccessibilityContainer.h>
26 #include <FUiAccessibilityElement.h>
27 #include "FUi_ResourceSizeInfo.h"
28 #include "FUi_ResourceManager.h"
29 #include "FUi_UiBuilder.h"
30 #include "FUiCtrl_LabelImpl.h"
31 #include "FUiCtrl_Label.h"
32 #include "FUi_CoordinateSystemUtils.h"
33
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Graphics;
38
39 namespace Tizen { namespace Ui { namespace Controls
40 {
41
42 Dimension
43 _LabelImpl::LabelSizeInfo::GetDefaultMinimumSize(_ControlOrientation orientation) const
44 {
45         result r = E_SUCCESS;
46         Dimension dimension(0, 0);
47
48         r = GET_DIMENSION_CONFIG(LABEL::MIN_SIZE, orientation, dimension);
49         SysTryReturn(NID_UI, r == E_SUCCESS, dimension, r, "[%s] A system error occurred.", GetErrorMessage(r));
50
51         SetLastResult(r);
52
53         return dimension;
54 }
55
56 FloatDimension
57 _LabelImpl::LabelSizeInfo::GetDefaultMinimumSizeF(_ControlOrientation orientation) const
58 {
59         result r = E_SUCCESS;
60         FloatDimension dimension(0.0f, 0.0f);
61
62         r = GET_DIMENSION_CONFIG(LABEL::MIN_SIZE, orientation, dimension);
63         SysTryReturn(NID_UI, r == E_SUCCESS, dimension, r, "[%s] A system error occurred.", GetErrorMessage(r));
64
65         SetLastResult(r);
66
67         return dimension;
68 }
69
70 _LabelImpl*
71 _LabelImpl::GetInstance(Label& label)
72 {
73         return static_cast<_LabelImpl*> (label._pControlImpl);
74 }
75
76 const _LabelImpl*
77 _LabelImpl::GetInstance(const Label& label)
78 {
79         return static_cast<const _LabelImpl*> (label._pControlImpl);
80 }
81
82 _LabelImpl::_LabelImpl(Label* pPublic, _Label* pCore)
83         : _ControlImpl(pPublic, pCore)
84 {
85         ClearLastResult();
86         SetFocusableChangable(false);
87 }
88
89 _LabelImpl::~_LabelImpl(void)
90 {
91         ClearLastResult();
92 }
93
94 _LabelImpl*
95 _LabelImpl::CreateLabelImplN(Label* pControl, const Rectangle& bounds)
96 {
97         result r = E_SUCCESS;
98         r = GET_SIZE_INFO(Label).CheckInitialSizeValid(Dimension(bounds.width, bounds.height), _CONTROL_ORIENTATION_PORTRAIT);
99         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] The given size is not valid.");
100
101         _Label* pCore = _Label::CreateLabelN();
102         SysTryReturn(NID_UI_CTRL, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
103
104         _LabelImpl* pImpl = new (std::nothrow) _LabelImpl(pControl, pCore);
105         r = _ControlImpl::CheckConstruction(pCore, pImpl);
106         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
107
108         r = pImpl->InitializeBoundsProperties(GET_SIZE_INFO(Label), bounds, pCore->GetOrientation());
109         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
110
111         ClearLastResult();
112
113         return pImpl;
114
115 CATCH:
116         delete pImpl;
117         return null;
118 }
119
120 _LabelImpl*
121 _LabelImpl::CreateLabelImplN(Label* pControl, const FloatRectangle& bounds)
122 {
123         result r = E_SUCCESS;
124         r = GET_SIZE_INFO(Label).CheckInitialSizeValidF(FloatDimension(bounds.width, bounds.height), _CONTROL_ORIENTATION_PORTRAIT);
125         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] The given size is not valid.");
126
127         _Label* pCore = _Label::CreateLabelN();
128         SysTryReturn(NID_UI_CTRL, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
129
130         _LabelImpl* pImpl = new (std::nothrow) _LabelImpl(pControl, pCore);
131         r = _ControlImpl::CheckConstruction(pCore, pImpl);
132         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
133
134         r = pImpl->InitializeBoundsPropertiesF(GET_SIZE_INFO(Label), bounds, pCore->GetOrientation());
135         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
136
137         pImpl->SetBackgroundColor(Color(0, 0, 0, 0)); // for transparent
138
139         ClearLastResult();
140
141         return pImpl;
142
143 CATCH:
144         delete pImpl;
145         return null;
146 }
147
148 Color
149 _LabelImpl::GetColorOnError(void)
150 {
151         return Color(0XFFFFFFFF);
152 }
153
154 const char*
155 _LabelImpl::GetPublicClassName(void) const
156 {
157         return "Tizen::Ui::Controls::Label";
158 }
159
160 const Label&
161 _LabelImpl::GetPublic(void) const
162 {
163         return static_cast <const Label&>(_ControlImpl::GetPublic());
164 }
165
166 Label&
167 _LabelImpl::GetPublic(void)
168 {
169         return static_cast <Label&>(_ControlImpl::GetPublic());
170 }
171
172 const _Label&
173 _LabelImpl::GetCore(void) const
174 {
175         return static_cast <const _Label&>(_ControlImpl::GetCore());
176 }
177
178 _Label&
179 _LabelImpl::GetCore(void)
180 {
181         return static_cast <_Label&>(_ControlImpl::GetCore());
182 }
183
184 result
185 _LabelImpl::OnBoundsChanged(const Rectangle& oldRect, const Rectangle& newRect)
186 {
187         return E_SUCCESS;
188 }
189
190 result
191 _LabelImpl::OnBoundsChanged(const FloatRectangle& oldRect, const FloatRectangle& newRect)
192 {
193         return E_SUCCESS;
194 }
195
196 Tizen::Graphics::FloatDimension
197 _LabelImpl::GetContentSizeF(bool horizontalMode, bool verticalMode) const
198 {
199         return GetCore().GetContentSizeF(horizontalMode, verticalMode);
200 }
201
202 result
203 _LabelImpl::SetText(const String& text)
204 {
205         Variant var(text);
206         result r = GetCore().SetPropertyText(var);
207         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
208
209         return r;
210 }
211
212 result
213 _LabelImpl::SetTextColor(const Color& color)
214 {
215         result r = GetCore().SetTextColor(color);
216         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
217
218         return r;
219 }
220
221 result
222 _LabelImpl::SetBackgroundBitmap(const Bitmap& bitmap)
223 {
224         result r = GetCore().SetBackgroundBitmap(bitmap);
225         SetLastResultReturn(r);
226 }
227
228 result
229 _LabelImpl::SetTextHorizontalAlignment(HorizontalAlignment alignment)
230 {
231         result r = GetCore().SetTextHorizontalAlignment(alignment);
232         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
233
234         return r;
235 }
236
237 result
238 _LabelImpl::SetTextVerticalAlignment(VerticalAlignment alignment)
239 {
240         result r = GetCore().SetTextVerticalAlignment(alignment);
241         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
242
243         return r;
244 }
245
246 result
247 _LabelImpl::SetTextConfig(int size, LabelTextStyle style)
248 {
249         result r = GetCore().SetTextConfig(size, style);
250         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
251
252         return r;
253 }
254
255 result
256 _LabelImpl::SetTextConfig(float size, LabelTextStyle style)
257 {
258         result r = GetCore().SetTextConfig(size, style);
259         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
260
261         return r;
262 }
263
264 result
265 _LabelImpl::SetMargin(int topMargin, int leftMargin)
266 {
267         result r = GetCore().SetMargin(topMargin, leftMargin);
268         SetLastResultReturn(r);
269 }
270
271 result
272 _LabelImpl::SetMargin(float topMargin, float leftMargin)
273 {
274         result r = GetCore().SetMargin(topMargin, leftMargin);
275         SetLastResultReturn(r);
276 }
277
278 String
279 _LabelImpl::GetText(void) const
280 {
281         ClearLastResult();
282
283         return GetCore().GetPropertyText().ToString();
284 }
285
286 HorizontalAlignment
287 _LabelImpl::GetTextHorizontalAlignment(void) const
288 {
289         ClearLastResult();
290
291         return GetCore().GetTextHorizontalAlignment();
292 }
293
294 VerticalAlignment
295 _LabelImpl::GetTextVerticalAlignment(void) const
296 {
297         ClearLastResult();
298
299         return GetCore().GetTextVerticalAlignment();
300 }
301
302 Color
303 _LabelImpl::GetTextColor(void) const
304 {
305         ClearLastResult();
306
307         return GetCore().GetTextColor();
308 }
309
310 int
311 _LabelImpl::GetTextSize(void) const
312 {
313         ClearLastResult();
314
315         return GetCore().GetTextSize();
316 }
317
318 float
319 _LabelImpl::GetTextSizeF(void) const
320 {
321         ClearLastResult();
322
323         return GetCore().GetTextSizeF();
324 }
325
326 LabelTextStyle
327 _LabelImpl::GetTextStyle(void) const
328 {
329         ClearLastResult();
330
331         return GetCore().GetTextStyle();
332 }
333
334 int
335 _LabelImpl::GetTopMargin(void) const
336 {
337         ClearLastResult();
338
339         return GetCore().GetTopMargin();
340 }
341
342 float
343 _LabelImpl::GetTopMarginF(void) const
344 {
345         ClearLastResult();
346
347         return GetCore().GetTopMarginF();
348 }
349
350 int
351 _LabelImpl::GetLeftMargin(void) const
352 {
353         ClearLastResult();
354
355         return GetCore().GetLeftMargin();
356 }
357
358 float
359 _LabelImpl::GetLeftMarginF(void) const
360 {
361         ClearLastResult();
362
363         return GetCore().GetLeftMarginF();
364 }
365
366 class _LabelMaker
367         : public _UiBuilderControlMaker
368 {
369 public:
370         _LabelMaker(_UiBuilder* uibuilder)
371                 : _UiBuilderControlMaker(uibuilder){};
372         virtual ~_LabelMaker(){};
373         static _UiBuilderControlMaker* GetInstance(_UiBuilder* uibuilder)
374         {
375                 _LabelMaker* pLabelMaker = new (std::nothrow) _LabelMaker(uibuilder);
376                 return static_cast<_UiBuilderControlMaker*>(pLabelMaker);
377         };
378 protected:
379         virtual Control* Make(_UiBuilderControl* pControl)
380         {
381 //              bool b = false;
382 //              bool bText = false;
383 //              bool bSoftkeyCount = false;
384                 result r = E_SYSTEM;
385                 _UiBuilderControlLayout* pControlProperty = null;
386                 Label* pLabel = null;
387                 FloatRectangle rect;
388
389                 HorizontalAlignment horizontalAlignment;
390                 VerticalAlignment verticalAlignment;
391                 Tizen::Base::String elementString;
392                 int opacity = 0;
393                 float size = 0.0f;
394                 float leftMargin = 0.0f;
395                 float topMargin = 0.0f;
396                 LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
397                 Color color;
398
399                 GetProperty(pControl, &pControlProperty);
400                 if (pControlProperty == null)
401                 {
402                         return null;
403                 }
404
405                 pLabel = new (std::nothrow) Label();
406                 rect = pControlProperty->GetRectF();
407
408                 //Construct
409                 if (pControl->GetElement(L"text", elementString))
410                 {
411                         r = pLabel->Construct(rect, elementString);
412                 }
413                 else
414                 {
415                         r = pLabel->Construct(rect, String(L""));
416                 }
417
418                 if (r != E_SUCCESS)
419                 {
420                         delete pLabel;
421                         return null;
422                 }
423
424                 //setting
425                 if (pControl->GetElement(L"horizontalAlign", elementString) || pControl->GetElement(L"hAlign", elementString))
426                 {
427                         if (ConvertHAlignToHorizontalAlignment(elementString, horizontalAlignment))
428                         {
429                                 pLabel->SetTextHorizontalAlignment(horizontalAlignment);
430                         }
431                 }
432
433                 if (pControl->GetElement(L"verticalAlign", elementString) || pControl->GetElement(L"vAlign", elementString))
434                 {
435                         if (ConvertVAlignToVerticalAlignment(elementString, verticalAlignment))
436                         {
437                                 pLabel->SetTextVerticalAlignment(verticalAlignment);
438                         }
439                 }
440
441                 if (pControl->GetElement(L"backgroundColorOpacity", elementString) || pControl->GetElement(L"BGColorOpacity", elementString))
442                 {
443                         Base::Integer::Parse(elementString, opacity);
444                 }
445
446                 if (pControl->GetElement(L"backgroundColor", elementString) || pControl->GetElement(L"BGColor", elementString))
447                 {
448                         ConvertStringToColor32(elementString, opacity, color);
449                         pLabel->SetBackgroundColor(color);
450                 }
451                 else
452                 {
453                         color = pLabel->GetBackgroundColor();
454                         color.SetAlpha(ConvertOpacity100to255(opacity));
455                         pLabel->SetBackgroundColor(color);
456                 }
457
458                 if (pControl->GetElement(L"textColor", elementString))
459                 {
460                         ConvertStringToColor(elementString, color);
461                         pLabel->SetTextColor(color);
462                 }
463
464                 if (pControl->GetElement(L"backgroundBitmapPath", elementString) || pControl->GetElement(L"BGBitmapPath", elementString))
465                 {
466                         Bitmap* pBackgroundBitmap = null;
467                         pBackgroundBitmap = LoadBitmapN(elementString);
468
469                         if (pBackgroundBitmap != null)
470                         {
471                                 pLabel->SetBackgroundBitmap(*pBackgroundBitmap);
472                                 delete pBackgroundBitmap;
473                         }
474                 }
475
476                 if (pControl->GetElement(L"textSize", elementString))
477                 {
478                         size = _LocalizedNumParser::ToDouble(elementString, "C");
479                         _ICoordinateSystemTransformer* pTransform = GetTransformer();
480                         if (pTransform)
481                         {
482                                 size = pTransform->Transform(size);
483                         }
484                 }
485
486                 if (pControl->GetElement(L"textStyle", elementString))
487                 {
488                         if (elementString.Equals(L"LABEL_TEXT_STYLE_NORMAL", false))
489                         {
490                                 style = LABEL_TEXT_STYLE_NORMAL;
491                         }
492                         else if (elementString.Equals(L"LABEL_TEXT_STYLE_BOLD", false))
493                         {
494                                 style = LABEL_TEXT_STYLE_BOLD;
495                         }
496                         else if (elementString.Equals(L"LABEL_TEXT_STYLE_ITALIC", false))
497                         {
498                                 style = LABEL_TEXT_STYLE_ITALIC;
499                         }
500                 }
501
502                 if (size > 0.0f)
503                 {
504                         pLabel->SetTextConfig(size, style);
505                 }
506                 else
507                 {
508                         pLabel->SetTextConfig(pLabel->GetTextSize(), style);
509                 }
510
511                 if (pControl->GetElement(L"LeftMargin", elementString))
512                 {
513                         Base::Float::Parse(elementString, leftMargin);
514                         _ICoordinateSystemTransformer* pTransform = GetTransformer();
515                         if (pTransform)
516                         {
517                                 leftMargin = pTransform->Transform(leftMargin);
518                         }
519                 }
520
521                 if (pControl->GetElement(L"TopMargin", elementString))
522                 {
523                         Base::Float::Parse(elementString, topMargin);
524                         _ICoordinateSystemTransformer* pTransform = GetTransformer();
525                         if (pTransform)
526                         {
527                                 topMargin = pTransform->Transform(topMargin);
528                         }
529                 }
530
531                 pLabel->SetMargin(topMargin, leftMargin);
532
533                 //-------safety for Minimum size-------------------
534                 rect = (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT))->GetRectF();
535                 if (rect.width < 0.0f) // LABEL_MIN_WIDTH = 0
536                 {
537                         rect.width = 0.0f;
538                 }
539
540                 if (rect.height < 0.0f)
541                 {
542                         rect.height = 0.0f;
543                 }
544
545                 (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT))->SetRect(rect.x, rect.y, rect.width, rect.height);
546
547                 rect = (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE))->GetRectF();
548                 if (rect.width < 0.0f)
549                 {
550                         rect.width = 0.0f;
551                 }
552
553                 if (rect.height < 0.0f) // LABEL_MIN_HEIGHT = 0
554                 {
555                         rect.height = 0.0f;
556                 }
557                 (pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE))->SetRect(rect.x, rect.y, rect.width, rect.height);
558                 //---------end safety code------------------------
559
560                 if (pControl->GetElement(L"accessibilityHint", elementString))
561                 {
562                         AccessibilityContainer* pContainer = pLabel->GetAccessibilityContainer();
563                         if (pContainer)
564                         {
565                                 AccessibilityElement* pElement = pContainer->GetElement(L"LabelText");
566                                 if (pElement)
567                                 {
568                                         pElement->SetHint(elementString);
569                                 }
570                         }
571                 }
572
573                 return pLabel;
574         }
575 private:
576 }; // _LabelMaker
577
578 _LabelRegister::_LabelRegister()
579 {
580           _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
581           pUiBuilderControlTableManager->RegisterControl(L"Label", _LabelMaker::GetInstance);
582 }
583 _LabelRegister::~_LabelRegister()
584 {
585           _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
586           pUiBuilderControlTableManager->UnregisterControl(L"Label");
587 }
588 static _LabelRegister LabelRegisterToUiBuilder;
589 }}} // Tizen::Ui::Controls