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_LabelPresenter.cpp
19 * @brief This is the implementation file for the _LabelPresenter class.
22 #include <FBaseErrorDefine.h>
23 #include <FGrpCanvas.h>
24 #include <FBaseSysLog.h>
25 #include <FGrp_CanvasImpl.h>
26 #include <FGrp_FontImpl.h>
27 #include <FGrp_BitmapImpl.h>
28 #include <FGrp_TextTextSimple.h>
29 #include <FAppApplication.h>
30 #include "FUi_CoordinateSystemUtils.h"
31 #include "FUi_ResourceManager.h"
32 #include "FUiAnim_VisualElement.h"
33 #include "FUiCtrl_LabelPresenter.h"
34 #include "FUiCtrl_LabelModel.h"
35 #include "FUiCtrl_Label.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Ui::Animations;
40 using namespace Tizen::Graphics::_Text;
41 using namespace Tizen::App;
43 namespace Tizen { namespace Ui { namespace Controls
46 _LabelPresenter::_LabelPresenter(void)
49 , __previousEnabledState(false)
53 , __textStyle(LABEL_TEXT_STYLE_NORMAL)
60 _LabelPresenter::~_LabelPresenter(void)
76 _LabelPresenter::Construct(const _Label& label)
80 __pLabel = const_cast <_Label*>(&label);
82 __fontStyle = FONT_STYLE_PLAIN;
83 __fontSize = __pLabel->GetTextSizeF();
84 __pFont = __pLabel->GetFallbackFont();
86 SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
88 __pTextObject = new (std::nothrow) TextObject();
89 SysTryCatch(NID_UI_CTRL, __pTextObject != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
101 _LabelPresenter::Install(void)
103 result r = E_SUCCESS;
105 __pBase = __pLabel->GetVisualElement();
107 _LabelModel* pModel = new (std::nothrow) _LabelModel();
108 SysTryReturn(NID_UI_CTRL, pModel, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
110 r = SetModel(*pModel);
111 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
113 r = pModel->Construct();
114 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
124 _LabelPresenter::SetModel(const _LabelModel& labelModel)
126 __pLabelModel = const_cast <_LabelModel*>(&labelModel);
132 _LabelPresenter::InitTextObject(void)
134 TextSimple* pSimpleText = null;
136 pSimpleText = new (std::nothrow) TextSimple(const_cast<wchar_t*>(__pLabel->GetText().GetPointer()), __pLabel->GetText().GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, __pFont);
137 SysTryReturn(NID_UI_CTRL, pSimpleText, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
139 __pTextObject->Construct();
140 __pTextObject->AppendElement(*pSimpleText);
141 __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
142 __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
148 _LabelPresenter::OnFontChanged(Font* pFont)
153 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
159 _LabelPresenter::SetTextConfig(int size, LabelTextStyle style)
161 float floatSize = _CoordinateSystemUtils::ConvertToFloat(size);
163 return SetTextConfig(floatSize, style);
167 _LabelPresenter::SetTextConfig(float size, LabelTextStyle style)
169 result r = E_SUCCESS;
171 unsigned long fontStyle = FONT_STYLE_PLAIN;
172 bool isStrikeOut = __pFont->IsStrikeOut();
173 bool isUnderLine = __pFont->IsUnderlined();
177 if (__textStyle == LABEL_TEXT_STYLE_ITALIC)
179 fontStyle |= FONT_STYLE_ITALIC;
182 if (__textStyle == LABEL_TEXT_STYLE_BOLD)
184 fontStyle |= FONT_STYLE_BOLD;
187 __fontStyle = fontStyle;
190 __pFont = __pLabel->GetFallbackFont();
192 SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
194 __pFont->SetStrikeOut(isStrikeOut);
195 __pFont->SetUnderline(isUnderLine);
197 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
203 _LabelPresenter::GetTextStyle(void) const
209 _LabelPresenter::SetFontInfo(unsigned long style, int size)
211 float floatSize = _CoordinateSystemUtils::ConvertToFloat(size);
213 return SetFontInfo(style, floatSize);
217 _LabelPresenter::SetFontInfo(unsigned long style, float size)
226 _LabelPresenter::OnFontInfoRequested(unsigned long& style, int& size)
229 size = _CoordinateSystemUtils::ConvertToInteger(__fontSize);
235 _LabelPresenter::OnFontInfoRequested(unsigned long& style, float& size)
244 _LabelPresenter::GetTextObject(void) const
246 return __pTextObject;
250 _LabelPresenter::Draw(void)
259 _LabelPresenter::DrawBackground(void)
261 Bitmap* pBitmap = __pLabel->GetBackgroundBitmap();
262 Bitmap* pEffectBitmap = __pLabel->GetBackgroundEffectBitmap();
264 FloatRectangle bounds(0.0f, 0.0f, __pLabel->GetBoundsF().width, __pLabel->GetBoundsF().height);
266 Color bgColor = __pLabel->GetBackgroundColor();
268 Canvas* pCanvas = null;
272 pCanvas = __pLabel->GetCanvasN();
275 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
280 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
283 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBitmap))
285 pCanvas->DrawNinePatchedBitmap(bounds, *pBitmap);
289 pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height), *pBitmap);
292 if (pEffectBitmap != null)
294 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pEffectBitmap))
296 pCanvas->DrawNinePatchedBitmap(bounds, *pEffectBitmap);
300 pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height), *pEffectBitmap);
306 if (__pLabel->GetText().IsEmpty() && __pLabel->IsCalledGetCanvasN() == false)
308 __pBase->SetBackgroundColor(_Colorf((float)bgColor.GetRed() / 255, (float)bgColor.GetGreen() / 255, (float)bgColor.GetBlue() / 255, (float)bgColor.GetAlpha() / 255));
312 pCanvas = __pLabel->GetCanvasN();
315 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
320 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
322 pCanvas->FillRectangle(__pLabel->GetBackgroundColor(), bounds);
335 _LabelPresenter::DrawText(void)
337 if (__pLabel->GetText().IsEmpty())
339 SysLog(NID_UI_CTRL, "Text is empty.");
344 TextObjectAlignment horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
345 TextObjectAlignment verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
347 float leftMargin = __pLabel->GetLeftMargin();
348 float topMargin = __pLabel->GetTopMargin();
349 float rightMargin = __pLabel->GetRightMargin();
350 float bottomMargin = __pLabel->GetBottomMargin();
352 FloatRectangle rect(leftMargin, topMargin, __pLabel->GetBoundsF().width - (leftMargin + rightMargin), __pLabel->GetBoundsF().height - (topMargin + bottomMargin));
354 if (rect.width <= 0 || rect.height <= 0)
356 SysLog(NID_UI_CTRL, "text display rectangle is wrong.");
361 Canvas* pCanvas = __pLabel->GetCanvasN();
364 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
369 __pTextObject->SetBounds(rect);
371 switch (__pLabel->GetTextHorizontalAlignment())
374 horizontalAlign = TEXT_OBJECT_ALIGNMENT_LEFT;
376 case ALIGNMENT_CENTER:
377 horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
380 horizontalAlign = TEXT_OBJECT_ALIGNMENT_RIGHT;
383 switch (__pLabel->GetTextVerticalAlignment())
386 verticalAlign = TEXT_OBJECT_ALIGNMENT_TOP;
388 case ALIGNMENT_MIDDLE:
389 verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
392 verticalAlign = TEXT_OBJECT_ALIGNMENT_BOTTOM;
395 __pTextObject->SetAlignment(horizontalAlign | verticalAlign);
396 __pTextObject->Compose();
397 __pTextObject->SetForegroundColor(__pLabel->GetTextColor(), 0, __pTextObject->GetTextLength());
398 __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
406 _LabelPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
412 _LabelPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
418 _LabelPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
424 _LabelPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
430 _LabelPresenter::OnTouchMoveHandled(const _Control& control)
436 _LabelPresenter::IsEnabledStateChanged(void)
438 bool __currentEnabledState = __pLabel->GetEnableState();
440 if (__currentEnabledState != __previousEnabledState)
442 __previousEnabledState = __currentEnabledState;
451 }}} // Tizen::Ui::Controls