2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
18 * @file FUiCtrl_Label.cpp
19 * @brief This is the implementation file for the _Label class.
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"
34 using namespace Tizen::Base;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui::Animations;
37 using namespace Tizen::Graphics::_Text;
39 namespace Tizen { namespace Ui { namespace Controls
42 IMPLEMENT_PROPERTY(_Label);
45 : __pLabelPresenter(null)
48 , __horizontalAlignment(ALIGNMENT_CENTER)
49 , __verticalAlignment(ALIGNMENT_MIDDLE)
50 , __pBackgroundBitmap(null)
51 , __pBackgroundEffectBitmap(null)
55 , __bottomMargin(0.0f)
56 , __pTextElement(null)
60 _AccessibilityContainer* pContainer = null;
62 GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __textSize);
64 _LabelPresenter* pPresenter = new (std::nothrow) _LabelPresenter();
65 SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
67 r = SetPresenter(*pPresenter);
68 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
70 r = pPresenter->Construct(*this);
71 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
73 r = pPresenter->Install();
74 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
76 GET_COLOR_CONFIG(LABEL::TEXT_NORMAL, __textColor);
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);
83 SetBackgroundColor(Color(0, 0, 0, 0)); // for transparent
86 pContainer = GetAccessibilityContainer();
90 pContainer->Activate(true);
91 InitializeAccessibilityElement();
103 _Label::CreateLabelN(void)
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.");
109 pLabel->AcquireHandle();
111 pLabel->SetTouchPressThreshold(SENSITIVE);
120 _Label::~_Label(void)
122 if (__pLabelPresenter)
124 delete __pLabelPresenter;
125 __pLabelPresenter = null;
128 if (__pBackgroundBitmap)
130 delete __pBackgroundBitmap;
131 __pBackgroundBitmap = null;
134 if (__pBackgroundEffectBitmap)
136 delete __pBackgroundEffectBitmap;
137 __pBackgroundEffectBitmap = null;
141 __pTextElement->Activate(false);
142 __pTextElement = null;
148 _Label::SetPresenter(const _LabelPresenter& labelPresenter)
150 __pLabelPresenter = const_cast <_LabelPresenter*>(&labelPresenter);
158 __pLabelPresenter->Draw();
164 _Label::OnAttachedToMainTree(void)
168 __pTextElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
175 _Label::InitializeAccessibilityElement(void)
177 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
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");
186 pContainer->AddElement(*__pTextElement);
192 _Label::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
194 return __pLabelPresenter->OnTouchPressed(source, touchinfo);
198 _Label::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
200 return __pLabelPresenter->OnTouchReleased(source, touchinfo);
204 _Label::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
206 return __pLabelPresenter->OnTouchMoved(source, touchinfo);
210 _Label::OnTouchCanceled(const _Control & source, const _TouchInfo & touchinfo)
212 return __pLabelPresenter->OnTouchCanceled(source, touchinfo);
216 _Label::OnTouchMoveHandled(const _Control& control)
218 __pLabelPresenter->OnTouchMoveHandled(control);
224 _Label::OnBoundsChanged(void)
228 __pTextElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
235 _Label::OnFontChanged(Font* pFont)
237 __pLabelPresenter->OnFontChanged(pFont);
243 _Label::OnFontInfoRequested(unsigned long& style, int& size)
245 __pLabelPresenter->OnFontInfoRequested(style, size);
251 _Label::SetText(const String& text)
253 return SetProperty(L"text", Variant(text));
257 _Label::SetPropertyText(const Variant& text)
259 __text = text.ToString();
263 __pTextElement->SetLabel(__text);
266 result r = __pLabelPresenter->InitTextObject();
267 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
273 _Label::SetTextColor(const Color& color)
275 return SetProperty(L"textColor", Variant(color));
279 _Label::SetPropertyTextColor(const Variant& color)
281 __textColor = color.ToColor();
286 _Label::SetBackgroundBitmap(const Bitmap& bitmap)
290 Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
294 if (__pBackgroundBitmap != null)
296 delete __pBackgroundBitmap;
299 __pBackgroundBitmap = pClonedBitmap;
308 _Label::GetBackgroundBitmap(void) const
310 return __pBackgroundBitmap;
314 _Label::SetBackgroundEffectBitmap(const Bitmap& bitmap)
318 Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
322 if (__pBackgroundEffectBitmap != null)
324 delete __pBackgroundEffectBitmap;
327 __pBackgroundEffectBitmap = pClonedBitmap;
336 _Label::GetBackgroundEffectBitmap(void) const
338 return __pBackgroundEffectBitmap;
343 _Label::SetTextHorizontalAlignment(HorizontalAlignment alignment)
345 __horizontalAlignment = alignment;
350 _Label::SetTextVerticalAlignment(VerticalAlignment alignment)
352 __verticalAlignment = alignment;
357 _Label::SetTextConfig(int size, LabelTextStyle style)
359 float floatSize = _CoordinateSystemUtils::ConvertToFloat(size);
361 __pLabelPresenter->SetTextConfig(floatSize, style);
363 return SetProperty(L"textSize", Variant(floatSize));
367 _Label::SetTextConfig(float size, LabelTextStyle style)
369 __pLabelPresenter->SetTextConfig(size, style);
371 return SetProperty(L"textSize", Variant(size));
375 _Label::SetPropertyTextSize(const Variant& textSize)
377 __textSize = textSize.ToFloat();
383 _Label::GetText(void) const
385 Variant text = GetProperty(L"text");
387 return text.ToString();
391 _Label::GetPropertyText(void) const
393 return Variant(__text);
397 _Label::GetTextHorizontalAlignment(void) const
399 return __horizontalAlignment;
403 _Label::GetTextVerticalAlignment(void) const
405 return __verticalAlignment;
409 _Label::GetTextColor(void) const
411 Variant color = GetProperty(L"textColor");
413 return color.ToColor();
417 _Label::GetPropertyTextColor(void) const
419 return Variant(__textColor);
423 _Label::GetTextSize(void) const
425 return _CoordinateSystemUtils::ConvertToInteger(GetTextSizeF());
429 _Label::GetTextSizeF(void) const
431 Variant size = GetProperty(L"textSize");
433 return size.ToFloat();
437 _Label::GetPropertyTextSize(void) const
439 return Variant(__textSize);
443 _Label::GetTextStyle(void) const
445 return __pLabelPresenter->GetTextStyle();
449 _Label::SetMargin(int topMargin, int leftMargin)
451 float floatTopMargin = _CoordinateSystemUtils::ConvertToFloat(topMargin);
452 float floatLeftMargin = _CoordinateSystemUtils::ConvertToFloat(leftMargin);
454 return SetMargin(floatTopMargin, floatLeftMargin);
458 _Label::SetMargin(float topMargin, float leftMargin)
460 __topMargin = topMargin;
461 __leftMargin = leftMargin;
467 _Label::SetMargin(int leftMargin, int topMargin, int rightMargin, int bottomMargin)
469 float floatLeftMargin = _CoordinateSystemUtils::ConvertToFloat(leftMargin);
470 float floatTopMargin = _CoordinateSystemUtils::ConvertToFloat(topMargin);
471 float floatRightMargin = _CoordinateSystemUtils::ConvertToFloat(rightMargin);
472 float floatBottomMargin = _CoordinateSystemUtils::ConvertToFloat(bottomMargin);
474 return SetMargin(floatLeftMargin, floatTopMargin, floatRightMargin, floatBottomMargin);
478 _Label::SetMargin(float leftMargin, float topMargin, float rightMargin, float bottomMargin)
480 __leftMargin = leftMargin;
481 __topMargin = topMargin;
482 __rightMargin = rightMargin;
483 __bottomMargin = bottomMargin;
489 _Label::GetLeftMargin(void) const
491 return _CoordinateSystemUtils::ConvertToInteger(GetLeftMarginF());
495 _Label::GetLeftMarginF(void) const
501 _Label::GetTopMargin(void) const
503 return _CoordinateSystemUtils::ConvertToInteger(GetTopMarginF());
507 _Label::GetTopMarginF(void) const
513 _Label::GetRightMargin(void) const
515 return _CoordinateSystemUtils::ConvertToInteger(GetRightMarginF());
519 _Label::GetRightMarginF(void) const
521 return __rightMargin;
525 _Label::GetBottomMargin(void) const
527 return _CoordinateSystemUtils::ConvertToInteger(GetBottomMarginF());
531 _Label::GetBottomMarginF(void) const
533 return __bottomMargin;
537 _Label::GetContentSize(void) const
539 return _CoordinateSystemUtils::ConvertToInteger(GetContentSizeInternalF());
543 _Label::GetContentSizeF(void) const
545 return GetContentSizeInternalF();
549 _Label::GetContentSizeInternalF(void) const
551 if (__text.IsEmpty() && __pBackgroundBitmap == null)
553 return FloatDimension(GetBoundsF().width, GetBoundsF().height);
556 FloatDimension dimension(0.0f, 0.0f);
557 FloatRectangle contentRect(0.0f, 0.0f, 0.0f , 0.0f);
559 TextObject* pTextObject = __pLabelPresenter->GetTextObject();
561 TextObjectActionType previousActionType = pTextObject->GetAction();
562 TextObjectWrapType previousWrapType = pTextObject->GetWrap();
563 FloatRectangle previousRect = pTextObject->GetBoundsF();
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());
570 contentRect.width = _ControlManager::GetInstance()->GetScreenSizeF().width;
572 if (dimension.width > contentRect.width - GetBoundsF().x)
574 dimension.width = contentRect.width - GetBoundsF().x;
576 pTextObject->SetBounds(FloatRectangle(previousRect.x, previousRect.y, dimension.width, previousRect.height));
577 pTextObject->Compose();
580 dimension.height = pTextObject->GetTotalHeightF();
582 pTextObject->SetBounds(previousRect);
583 pTextObject->SetAction(previousActionType);
584 pTextObject->SetWrap(previousWrapType);
585 pTextObject->Compose();
587 dimension.width += __leftMargin * 2;
588 dimension.height += __topMargin * 2;
590 if (__pBackgroundBitmap != null)
592 dimension.width = GetBoundsF().width;
593 dimension.height = GetBoundsF().height;
599 }}} // Tizen::Ui::Controls