Revert " modify license, permission and remove ^M char"
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_Label.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_Label.cpp
19  * @brief               This is the implementation file for the _Label class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include <FGrp_BitmapImpl.h>
25 #include "FUi_AccessibilityContainer.h"
26 #include "FUi_AccessibilityElement.h"
27 #include "FUi_AccessibilityManager.h"
28 #include "FUi_CoordinateSystemUtils.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUi_EflWindow.h"
31 #include "FUiCtrl_Label.h"
32 #include "FUiCtrl_LabelPresenter.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui::Animations;
37 using namespace Tizen::Graphics::_Text;
38
39 namespace Tizen { namespace Ui { namespace Controls
40 {
41
42 IMPLEMENT_PROPERTY(_Label);
43
44 _Label::_Label(void)
45         : __pLabelPresenter(null)
46         , __text(L"")
47         , __textSize(0.0f)
48         , __horizontalAlignment(ALIGNMENT_CENTER)
49         , __verticalAlignment(ALIGNMENT_MIDDLE)
50         , __pBackgroundBitmap(null)
51         , __pBackgroundEffectBitmap(null)
52         , __leftMargin(0.0f)
53         , __topMargin(0.0f)
54         , __rightMargin(0.0f)
55         , __bottomMargin(0.0f)
56         , __pTextElement(null)
57 {
58         result r = E_SUCCESS;
59
60         _AccessibilityContainer* pContainer = null;
61
62         GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __textSize);
63
64         _LabelPresenter* pPresenter = new (std::nothrow) _LabelPresenter();
65         SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
66
67         r = SetPresenter(*pPresenter);
68         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
69
70         r = pPresenter->Construct(*this);
71         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
72
73         r = pPresenter->Install();
74         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
75
76         GET_COLOR_CONFIG(LABEL::TEXT_NORMAL, __textColor);
77
78         GET_SHAPE_CONFIG(LABEL::LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
79         GET_SHAPE_CONFIG(LABEL::TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __topMargin);
80         GET_SHAPE_CONFIG(LABEL::RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __rightMargin);
81         GET_SHAPE_CONFIG(LABEL::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __bottomMargin);
82
83         SetBackgroundColor(Color(0, 0, 0, 0)); // for transparent
84         SetFocusable(false);
85
86         pContainer = GetAccessibilityContainer();
87
88         if(pContainer)
89         {
90                 pContainer->Activate(true);
91                 InitializeAccessibilityElement();
92         }
93
94         ClearLastResult();
95
96         return ;
97
98 CATCH:
99         delete pPresenter;
100 }
101
102 _Label*
103 _Label::CreateLabelN(void)
104 {
105         _Label* pLabel = new (std::nothrow) _Label();
106         SysTryReturn(NID_UI_CTRL, pLabel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
107         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
108
109         pLabel->AcquireHandle();
110
111         pLabel->SetTouchPressThreshold(SENSITIVE);
112
113         return pLabel;
114
115 CATCH:
116         delete pLabel;
117         return null;
118 }
119
120 _Label::~_Label(void)
121 {
122         if (__pLabelPresenter)
123         {
124                 delete __pLabelPresenter;
125                 __pLabelPresenter = null;
126         }
127
128         if (__pBackgroundBitmap)
129         {
130                 delete __pBackgroundBitmap;
131                 __pBackgroundBitmap = null;
132         }
133
134         if (__pBackgroundEffectBitmap)
135         {
136                 delete __pBackgroundEffectBitmap;
137                 __pBackgroundEffectBitmap = null;
138         }
139         if (__pTextElement)
140         {
141                 __pTextElement->Activate(false);
142                 __pTextElement = null;
143         }
144         ClearLastResult();
145 }
146
147 result
148 _Label::SetPresenter(const _LabelPresenter& labelPresenter)
149 {
150         __pLabelPresenter = const_cast <_LabelPresenter*>(&labelPresenter);
151
152         return E_SUCCESS;
153 }
154
155 void
156 _Label::OnDraw(void)
157 {
158         __pLabelPresenter->Draw();
159
160         return;
161 }
162
163 result
164 _Label::OnAttachedToMainTree(void)
165 {
166         if(__pTextElement)
167         {
168                 __pTextElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
169         }
170
171         return E_SUCCESS;
172 }
173
174 void
175 _Label::InitializeAccessibilityElement(void)
176 {
177         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
178
179         __pTextElement = new (std::nothrow) _AccessibilityElement(true);
180         SysTryReturnVoidResult(NID_UI_CTRL, __pTextElement, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
181         __pTextElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
182         __pTextElement->SetLabel(GetText());
183         __pTextElement->SetTrait(ACCESSIBILITY_TRAITS_LABEL);
184         __pTextElement->SetName(L"LabelText");
185
186                 pContainer->AddElement(*__pTextElement);
187
188         return;
189 }
190
191 bool
192 _Label::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
193 {
194         return __pLabelPresenter->OnTouchPressed(source, touchinfo);
195 }
196
197 bool
198 _Label::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
199 {
200         return __pLabelPresenter->OnTouchReleased(source, touchinfo);
201 }
202
203 bool
204 _Label::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
205 {
206         return __pLabelPresenter->OnTouchMoved(source, touchinfo);
207 }
208
209 bool
210 _Label::OnTouchCanceled(const _Control & source, const _TouchInfo & touchinfo)
211 {
212         return __pLabelPresenter->OnTouchCanceled(source, touchinfo);
213 }
214
215 void
216 _Label::OnTouchMoveHandled(const _Control& control)
217 {
218         __pLabelPresenter->OnTouchMoveHandled(control);
219
220         return;
221 }
222
223 void
224 _Label::OnBoundsChanged(void)
225 {
226         if(__pTextElement)
227         {
228                 __pTextElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
229         }
230
231         return;
232 }
233
234 void
235 _Label::OnFontChanged(Font* pFont)
236 {
237         __pLabelPresenter->OnFontChanged(pFont);
238
239         return;
240 }
241
242 void
243 _Label::OnFontInfoRequested(unsigned long& style, int& size)
244 {
245         __pLabelPresenter->OnFontInfoRequested(style, size);
246
247         return;
248 }
249
250 result
251 _Label::SetText(const String& text)
252 {
253         return SetProperty(L"text", Variant(text));
254 }
255
256 result
257 _Label::SetPropertyText(const Variant& text)
258 {
259         __text = text.ToString();
260
261         if(__pTextElement)
262         {
263                 __pTextElement->SetLabel(__text);
264         }
265
266         result r = __pLabelPresenter->InitTextObject();
267         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
268
269         return E_SUCCESS;
270 }
271
272 result
273 _Label::SetTextColor(const Color& color)
274 {
275         return SetProperty(L"textColor", Variant(color));
276 }
277
278 result
279 _Label::SetPropertyTextColor(const Variant& color)
280 {
281         __textColor = color.ToColor();
282         return E_SUCCESS;
283 }
284
285 result
286 _Label::SetBackgroundBitmap(const Bitmap& bitmap)
287 {
288         result r = E_SYSTEM;
289
290         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
291
292         if (pClonedBitmap)
293         {
294                 if (__pBackgroundBitmap != null)
295                 {
296                         delete __pBackgroundBitmap;
297                 }
298
299                 __pBackgroundBitmap = pClonedBitmap;
300
301                 r = E_SUCCESS;
302         }
303
304         return r;
305 }
306
307 Bitmap*
308 _Label::GetBackgroundBitmap(void) const
309 {
310         return __pBackgroundBitmap;
311 }
312
313 result
314 _Label::SetBackgroundEffectBitmap(const Bitmap& bitmap)
315 {
316         result r = E_SYSTEM;
317
318         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
319
320         if (pClonedBitmap)
321         {
322                 if (__pBackgroundEffectBitmap != null)
323                 {
324                         delete __pBackgroundEffectBitmap;
325                 }
326
327                 __pBackgroundEffectBitmap = pClonedBitmap;
328
329                 r = E_SUCCESS;
330         }
331
332         return r;
333 }
334
335 Bitmap*
336 _Label::GetBackgroundEffectBitmap(void) const
337 {
338         return __pBackgroundEffectBitmap;
339 }
340
341
342 result
343 _Label::SetTextHorizontalAlignment(HorizontalAlignment alignment)
344 {
345         __horizontalAlignment = alignment;
346         return E_SUCCESS;
347 }
348
349 result
350 _Label::SetTextVerticalAlignment(VerticalAlignment alignment)
351 {
352         __verticalAlignment = alignment;
353         return E_SUCCESS;
354 }
355
356 result
357 _Label::SetTextConfig(int size, LabelTextStyle style)
358 {
359         float floatSize = _CoordinateSystemUtils::ConvertToFloat(size);
360
361         __pLabelPresenter->SetTextConfig(floatSize, style);
362
363         return SetProperty(L"textSize", Variant(floatSize));
364 }
365
366 result
367 _Label::SetTextConfig(float size, LabelTextStyle style)
368 {
369         __pLabelPresenter->SetTextConfig(size, style);
370
371         return SetProperty(L"textSize", Variant(size));
372 }
373
374 result
375 _Label::SetPropertyTextSize(const Variant& textSize)
376 {
377         __textSize = textSize.ToFloat();
378
379         return E_SUCCESS;
380 }
381
382 String
383 _Label::GetText(void) const
384 {
385         Variant text = GetProperty(L"text");
386
387         return text.ToString();
388 }
389
390 Variant
391 _Label::GetPropertyText(void) const
392 {
393         return Variant(__text);
394 }
395
396 HorizontalAlignment
397 _Label::GetTextHorizontalAlignment(void) const
398 {
399         return __horizontalAlignment;
400 }
401
402 VerticalAlignment
403 _Label::GetTextVerticalAlignment(void) const
404 {
405         return __verticalAlignment;
406 }
407
408 Color
409 _Label::GetTextColor(void) const
410 {
411         Variant color = GetProperty(L"textColor");
412
413         return color.ToColor();
414 }
415
416 Variant
417 _Label::GetPropertyTextColor(void) const
418 {
419         return Variant(__textColor);
420 }
421
422 int
423 _Label::GetTextSize(void) const
424 {
425         return _CoordinateSystemUtils::ConvertToInteger(GetTextSizeF());
426 }
427
428 float
429 _Label::GetTextSizeF(void) const
430 {
431         Variant size = GetProperty(L"textSize");
432
433         return size.ToFloat();
434 }
435
436 Variant
437 _Label::GetPropertyTextSize(void) const
438 {
439         return Variant(__textSize);
440 }
441
442 LabelTextStyle
443 _Label::GetTextStyle(void) const
444 {
445         return __pLabelPresenter->GetTextStyle();
446 }
447
448 result
449 _Label::SetMargin(int topMargin, int leftMargin)
450 {
451         float floatTopMargin = _CoordinateSystemUtils::ConvertToFloat(topMargin);
452         float floatLeftMargin = _CoordinateSystemUtils::ConvertToFloat(leftMargin);
453
454         return SetMargin(floatTopMargin, floatLeftMargin);
455 }
456
457 result
458 _Label::SetMargin(float topMargin, float leftMargin)
459 {
460         __topMargin = topMargin;
461         __leftMargin = leftMargin;
462
463         return E_SUCCESS;
464 }
465
466 result
467 _Label::SetMargin(int leftMargin, int topMargin, int rightMargin, int bottomMargin)
468 {
469         float floatLeftMargin = _CoordinateSystemUtils::ConvertToFloat(leftMargin);
470         float floatTopMargin  = _CoordinateSystemUtils::ConvertToFloat(topMargin);
471         float floatRightMargin  = _CoordinateSystemUtils::ConvertToFloat(rightMargin);
472         float floatBottomMargin  = _CoordinateSystemUtils::ConvertToFloat(bottomMargin);
473
474         return SetMargin(floatLeftMargin, floatTopMargin, floatRightMargin, floatBottomMargin);
475 }
476
477 result
478 _Label::SetMargin(float leftMargin, float topMargin, float rightMargin, float bottomMargin)
479 {
480         __leftMargin = leftMargin;
481         __topMargin = topMargin;
482         __rightMargin = rightMargin;
483         __bottomMargin = bottomMargin;
484
485         return E_SUCCESS;
486 }
487
488 int
489 _Label::GetLeftMargin(void) const
490 {
491         return _CoordinateSystemUtils::ConvertToInteger(GetLeftMarginF());
492 }
493
494 float
495 _Label::GetLeftMarginF(void) const
496 {
497         return __leftMargin;
498 }
499
500 int
501 _Label::GetTopMargin(void) const
502 {
503         return _CoordinateSystemUtils::ConvertToInteger(GetTopMarginF());
504 }
505
506 float
507 _Label::GetTopMarginF(void) const
508 {
509         return __topMargin;
510 }
511
512 int
513 _Label::GetRightMargin(void) const
514 {
515         return _CoordinateSystemUtils::ConvertToInteger(GetRightMarginF());
516 }
517
518 float
519 _Label::GetRightMarginF(void) const
520 {
521         return __rightMargin;
522 }
523
524 int
525 _Label::GetBottomMargin(void) const
526 {
527         return _CoordinateSystemUtils::ConvertToInteger(GetBottomMarginF());
528 }
529
530 float
531 _Label::GetBottomMarginF(void) const
532 {
533         return __bottomMargin;
534 }
535
536 Dimension
537 _Label::GetContentSize(void) const
538 {
539         return _CoordinateSystemUtils::ConvertToInteger(GetContentSizeInternalF());
540 }
541
542 FloatDimension
543 _Label::GetContentSizeF(void) const
544 {
545         return GetContentSizeInternalF();
546 }
547
548 FloatDimension
549 _Label::GetContentSizeInternalF(void) const
550 {
551         if (__text.IsEmpty() && __pBackgroundBitmap == null)
552         {
553                 return FloatDimension(GetBoundsF().width, GetBoundsF().height);
554         }
555
556         FloatDimension dimension(0.0f, 0.0f);
557         FloatRectangle contentRect(0.0f, 0.0f, 0.0f , 0.0f);
558
559         TextObject* pTextObject = __pLabelPresenter->GetTextObject();
560
561         TextObjectActionType previousActionType = pTextObject->GetAction();
562         TextObjectWrapType previousWrapType = pTextObject->GetWrap();
563         FloatRectangle previousRect = pTextObject->GetBoundsF();
564
565         pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
566         pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
567         pTextObject->Compose();
568         dimension = pTextObject->GetTextExtentF(0, pTextObject->GetTextLength());
569
570         contentRect.width = _ControlManager::GetInstance()->GetScreenSizeF().width;
571
572         if (dimension.width > contentRect.width - GetBoundsF().x)
573         {
574                 dimension.width = contentRect.width - GetBoundsF().x;
575
576                 pTextObject->SetBounds(FloatRectangle(previousRect.x, previousRect.y, dimension.width, previousRect.height));
577                 pTextObject->Compose();
578         }
579
580         dimension.height = pTextObject->GetTotalHeightF();
581
582         pTextObject->SetBounds(previousRect);
583         pTextObject->SetAction(previousActionType);
584         pTextObject->SetWrap(previousWrapType);
585         pTextObject->Compose();
586
587         dimension.width += __leftMargin * 2;
588         dimension.height += __topMargin * 2;
589
590         if (__pBackgroundBitmap != null)
591         {
592                 dimension.width = GetBoundsF().width;
593                 dimension.height = GetBoundsF().height;
594         }
595
596         return dimension;
597 }
598
599 }}} // Tizen::Ui::Controls
600