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_ResourceManager.h"
29 #include "FUi_EflWindow.h"
30 #include "FUiCtrl_Label.h"
31 #include "FUiCtrl_LabelPresenter.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui::Animations;
36 using namespace Tizen::Graphics::_Text;
38 namespace Tizen { namespace Ui { namespace Controls
41 IMPLEMENT_PROPERTY(_Label);
44 : __pLabelPresenter(null)
47 , __horizontalAlignment(ALIGNMENT_CENTER)
48 , __verticalAlignment(ALIGNMENT_MIDDLE)
49 , __pBackgroundBitmap(null)
50 , __pBackgroundEffectBitmap(null)
53 , __pTextElement(null)
57 _AccessibilityContainer* pContainer = null;
59 GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __textSize);
61 _LabelPresenter* pPresenter = new (std::nothrow) _LabelPresenter();
62 SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
64 r = SetPresenter(*pPresenter);
65 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
67 r = pPresenter->Construct(*this);
68 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
70 r = pPresenter->Install();
71 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
73 GET_COLOR_CONFIG(LABEL::TEXT_NORMAL, __textColor);
75 GET_SHAPE_CONFIG(LABEL::TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __topMargin);
76 GET_SHAPE_CONFIG(LABEL::LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
80 pContainer = GetAccessibilityContainer();
84 pContainer->Activate(true);
96 _Label::CreateLabelN(void)
98 _Label* pLabel = new (std::nothrow) _Label();
99 SysTryReturn(NID_UI_CTRL, pLabel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
100 SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
102 pLabel->AcquireHandle();
111 _Label::~_Label(void)
113 if (__pLabelPresenter)
115 delete __pLabelPresenter;
116 __pLabelPresenter = null;
119 if (__pBackgroundBitmap)
121 delete __pBackgroundBitmap;
122 __pBackgroundBitmap = null;
125 if (__pBackgroundEffectBitmap)
127 delete __pBackgroundEffectBitmap;
128 __pBackgroundEffectBitmap = null;
132 __pTextElement->Activate(false);
133 __pTextElement = null;
139 _Label::SetPresenter(const _LabelPresenter& labelPresenter)
141 __pLabelPresenter = const_cast <_LabelPresenter*>(&labelPresenter);
149 __pLabelPresenter->Draw();
155 _Label::OnAttachedToMainTree(void)
157 InitializeAccessibilityElement();
163 _Label::InitializeAccessibilityElement(void)
170 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
174 __pTextElement = new (std::nothrow) _AccessibilityElement(true);
175 SysTryReturnVoidResult(NID_UI_CTRL, __pTextElement, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
177 __pTextElement->SetBounds(Rectangle(0,0, GetBounds().width, GetBounds().height));
178 __pTextElement->SetLabel(GetText());
179 __pTextElement->SetTrait(ACCESSIBILITY_TRAITS_LABEL);
180 __pTextElement->SetName(L"LabelText");
182 pContainer->AddElement(*__pTextElement);
189 _Label::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
191 return __pLabelPresenter->OnTouchPressed(source, touchinfo);
195 _Label::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
197 return __pLabelPresenter->OnTouchReleased(source, touchinfo);
201 _Label::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
203 return __pLabelPresenter->OnTouchMoved(source, touchinfo);
207 _Label::OnTouchCanceled(const _Control & source, const _TouchInfo & touchinfo)
209 return __pLabelPresenter->OnTouchCanceled(source, touchinfo);
213 _Label::OnTouchMoveHandled(const _Control& control)
215 __pLabelPresenter->OnTouchMoveHandled(control);
221 _Label::OnBoundsChanged(void)
225 __pTextElement->SetBounds(Rectangle(0,0, GetBounds().width, GetBounds().height));
232 _Label::OnFontChanged(Font* pFont)
234 __pLabelPresenter->OnFontChanged(pFont);
240 _Label::OnFontInfoRequested(unsigned long& style, int& size)
242 __pLabelPresenter->OnFontInfoRequested(style, size);
248 _Label::SetText(const String& text)
250 return SetProperty("text", Variant(text));
254 _Label::SetPropertyText(const Variant& text)
256 __text = text.ToString();
260 __pTextElement->SetLabel(__text);
263 result r = __pLabelPresenter->InitTextObject();
264 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
270 _Label::SetTextColor(const Color& color)
272 return SetProperty("textColor", Variant(color));
276 _Label::SetPropertyTextColor(const Variant& color)
278 __textColor = color.ToColor();
283 _Label::SetBackgroundBitmap(const Bitmap& bitmap)
287 Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
291 if (__pBackgroundBitmap != null)
293 delete __pBackgroundBitmap;
296 __pBackgroundBitmap = pClonedBitmap;
305 _Label::GetBackgroundBitmap(void) const
307 return __pBackgroundBitmap;
311 _Label::SetBackgroundEffectBitmap(const Bitmap& bitmap)
315 Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
319 if (__pBackgroundEffectBitmap != null)
321 delete __pBackgroundEffectBitmap;
324 __pBackgroundEffectBitmap = pClonedBitmap;
333 _Label::GetBackgroundEffectBitmap(void) const
335 return __pBackgroundEffectBitmap;
340 _Label::SetTextHorizontalAlignment(HorizontalAlignment alignment)
342 __horizontalAlignment = alignment;
347 _Label::SetTextVerticalAlignment(VerticalAlignment alignment)
349 __verticalAlignment = alignment;
354 _Label::SetTextConfig(int size, LabelTextStyle style)
356 __pLabelPresenter->SetTextConfig(size, style);
358 return SetProperty("textSize", Variant(size));
362 _Label::SetPropertyTextSize(const Variant& textSize)
364 __textSize = textSize.ToInt();
370 _Label::GetText(void) const
372 Variant text = GetProperty("text");
374 return text.ToString();
378 _Label::GetPropertyText(void) const
380 return Variant(__text);
384 _Label::GetTextHorizontalAlignment(void) const
386 return __horizontalAlignment;
390 _Label::GetTextVerticalAlignment(void) const
392 return __verticalAlignment;
396 _Label::GetTextColor(void) const
398 Variant color = GetProperty("textColor");
400 return color.ToColor();
404 _Label::GetPropertyTextColor(void) const
406 return Variant(__textColor);
410 _Label::GetTextSize(void) const
412 Variant size = GetProperty("textSize");
418 _Label::GetPropertyTextSize(void) const
420 return Variant(__textSize);
424 _Label::GetTextStyle(void) const
426 return __pLabelPresenter->GetTextStyle();
430 _Label::SetMargin(int topMargin, int leftMargin)
432 __topMargin = topMargin;
433 __leftMargin = leftMargin;
439 _Label::GetTopMargin(void) const
445 _Label::GetLeftMargin(void) const
450 Tizen::Graphics::Dimension
451 _Label::GetContentSize(void) const
453 return GetContentSizeInternal();
456 Tizen::Graphics::Dimension
457 _Label::GetContentSizeInternal(void) const
459 if (__text.IsEmpty() && __pBackgroundBitmap == null)
461 return Dimension(GetBounds().width, GetBounds().height);
464 Dimension dimension(0,0);
465 Rectangle contentRect(0,0,0,0);
467 TextObject* pTextObject = __pLabelPresenter->GetTextObject();
469 TextObjectActionType previousActionType = pTextObject->GetAction();
470 TextObjectWrapType previousWrapType = pTextObject->GetWrap();
471 Rectangle previousRect = pTextObject->GetBounds();
473 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
474 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
475 pTextObject->Compose();
476 dimension = pTextObject->GetTextExtent(0, pTextObject->GetTextLength());
478 contentRect.width = _ControlManager::GetInstance()->GetScreenSize().width;
480 if (dimension.width > contentRect.width - GetBounds().x)
482 dimension.width = contentRect.width - GetBounds().x;
484 pTextObject->SetBounds(Rectangle(previousRect.x, previousRect.y, dimension.width, previousRect.height));
485 pTextObject->Compose();
488 dimension.height = pTextObject->GetTotalHeight();
490 pTextObject->SetBounds(previousRect);
491 pTextObject->SetAction(previousActionType);
492 pTextObject->SetWrap(previousWrapType);
493 pTextObject->Compose();
495 dimension.width += __leftMargin * 2;
496 dimension.height += __topMargin * 2;
498 if (__pBackgroundBitmap != null)
500 dimension.width = GetBounds().width;
501 dimension.height = GetBounds().height;
507 }}} // Tizen::Ui::Controls