2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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)
54 , __textStyle(LABEL_TEXT_STYLE_NORMAL)
61 _LabelPresenter::~_LabelPresenter(void)
77 _LabelPresenter::Construct(const _Label& label)
81 __pLabel = const_cast <_Label*>(&label);
83 __fontStyle = FONT_STYLE_PLAIN;
84 __fontSize = __pLabel->GetTextSizeF();
85 __pFont = __pLabel->GetFallbackFont();
87 SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
89 __pTextObject = new (std::nothrow) TextObject();
90 SysTryCatch(NID_UI_CTRL, __pTextObject != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
92 __pTextObject->Construct();
104 _LabelPresenter::Install(void)
106 result r = E_SUCCESS;
108 __pBase = __pLabel->GetVisualElement();
110 _LabelModel* pModel = new (std::nothrow) _LabelModel();
111 SysTryReturn(NID_UI_CTRL, pModel, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
113 r = SetModel(*pModel);
114 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
116 r = pModel->Construct();
117 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
127 _LabelPresenter::SetModel(const _LabelModel& labelModel)
129 __pLabelModel = const_cast <_LabelModel*>(&labelModel);
135 _LabelPresenter::InitTextObject(void)
137 TextSimple* pSimpleText = null;
139 __pTextObject->RemoveAll(true);
141 pSimpleText = new (std::nothrow) TextSimple(const_cast<wchar_t*>(__pLabel->GetText().GetPointer()), __pLabel->GetText().GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, __pFont);
142 SysTryReturn(NID_UI_CTRL, pSimpleText, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
144 __pTextObject->AppendElement(*pSimpleText);
145 __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
146 __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
152 _LabelPresenter::OnFontChanged(Font* pFont)
157 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
164 _LabelPresenter::SetTextConfig(int size, LabelTextStyle style)
166 float floatSize = _CoordinateSystemUtils::ConvertToFloat(size);
168 return SetTextConfig(floatSize, style);
172 _LabelPresenter::SetTextConfig(float size, LabelTextStyle style)
174 result r = E_SUCCESS;
176 unsigned long fontStyle = FONT_STYLE_PLAIN;
177 bool isStrikeOut = __pFont->IsStrikeOut();
178 bool isUnderLine = __pFont->IsUnderlined();
182 if (__textStyle == LABEL_TEXT_STYLE_ITALIC)
184 fontStyle |= FONT_STYLE_ITALIC;
187 if (__textStyle == LABEL_TEXT_STYLE_BOLD)
189 fontStyle |= FONT_STYLE_BOLD;
192 __fontStyle = fontStyle;
195 __pFont = __pLabel->GetFallbackFont();
197 SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
199 __pFont->SetStrikeOut(isStrikeOut);
200 __pFont->SetUnderline(isUnderLine);
202 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
208 _LabelPresenter::GetTextStyle(void) const
214 _LabelPresenter::SetFontInfo(unsigned long style, int size)
216 float floatSize = _CoordinateSystemUtils::ConvertToFloat(size);
218 return SetFontInfo(style, floatSize);
222 _LabelPresenter::SetFontInfo(unsigned long style, float size)
231 _LabelPresenter::OnFontInfoRequested(unsigned long& style, int& size)
234 size = _CoordinateSystemUtils::ConvertToInteger(__fontSize);
240 _LabelPresenter::OnFontInfoRequested(unsigned long& style, float& size)
249 _LabelPresenter::GetTextObject(void) const
251 return __pTextObject;
255 _LabelPresenter::GetFont(void) const
261 _LabelPresenter::Draw(void)
265 if (!__pLabel->GetText().IsEmpty())
274 _LabelPresenter::DrawBackground(void)
276 Bitmap* pBitmap = __pLabel->GetBackgroundBitmap();
277 Bitmap* pEffectBitmap = __pLabel->GetBackgroundEffectBitmap();
279 FloatRectangle bounds(0.0f, 0.0f, __pLabel->GetBoundsF().width, __pLabel->GetBoundsF().height);
281 Color bgColor = __pLabel->GetBackgroundColor();
283 Canvas* pCanvas = null;
285 String imagePath = L"";
289 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBitmap) == false)
291 imagePath = _BitmapImpl::GetInstance(*pBitmap)->GetFileName();
294 if (imagePath.IsEmpty() == false && __pLabel->GetText() == L"")
296 __pBase->SetBackgroundColor(_Colorf(
297 (float)bgColor.GetRed() / 255, (float)bgColor.GetGreen() / 255, (float)bgColor.GetBlue() / 255, (float)bgColor.GetAlpha() / 255));
299 __pBase->SetImageSource(imagePath);
305 __pBase->SetImageSource(L"");
307 __lazyDecode = false;
309 pCanvas = __pLabel->GetCanvasN();
312 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
317 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
320 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBitmap))
322 pCanvas->DrawNinePatchedBitmap(bounds, *pBitmap);
326 pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height), *pBitmap);
329 if (pEffectBitmap != null)
331 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pEffectBitmap))
333 pCanvas->DrawNinePatchedBitmap(bounds, *pEffectBitmap);
337 pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height), *pEffectBitmap);
342 else // pBitmap is null
344 if (__pLabel->GetText().IsEmpty() && __pLabel->IsCalledGetCanvasN() == false)
346 __pBase->SetBackgroundColor(_Colorf((float)bgColor.GetRed() / 255, (float)bgColor.GetGreen() / 255, (float)bgColor.GetBlue() / 255, (float)bgColor.GetAlpha() / 255));
350 pCanvas = __pLabel->GetCanvasN();
353 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
358 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
360 pCanvas->FillRectangle(__pLabel->GetBackgroundColor(), bounds);
373 _LabelPresenter::DrawText(void)
375 TextObjectAlignment horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
376 TextObjectAlignment verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
378 float leftMargin = __pLabel->GetLeftMargin();
379 float topMargin = __pLabel->GetTopMargin();
380 float rightMargin = __pLabel->GetRightMargin();
381 float bottomMargin = __pLabel->GetBottomMargin();
383 FloatRectangle rect(leftMargin, topMargin, __pLabel->GetBoundsF().width - (leftMargin + rightMargin), __pLabel->GetBoundsF().height - (topMargin + bottomMargin));
385 if (rect.width <= 0 || rect.height <= 0)
387 SysLog(NID_UI_CTRL, "text display rectangle is wrong.");
392 Canvas* pCanvas = __pLabel->GetCanvasN();
395 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
400 __pTextObject->SetBounds(rect);
401 __pTextObject->SetDisplayBoundsExpandEnabled(true);
403 switch (__pLabel->GetTextHorizontalAlignment())
406 horizontalAlign = TEXT_OBJECT_ALIGNMENT_LEFT;
408 case ALIGNMENT_CENTER:
409 horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
412 horizontalAlign = TEXT_OBJECT_ALIGNMENT_RIGHT;
415 switch (__pLabel->GetTextVerticalAlignment())
418 verticalAlign = TEXT_OBJECT_ALIGNMENT_TOP;
420 case ALIGNMENT_MIDDLE:
421 verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
424 verticalAlign = TEXT_OBJECT_ALIGNMENT_BOTTOM;
427 __pTextObject->SetAlignment(horizontalAlign | verticalAlign);
428 __pTextObject->Compose();
429 __pTextObject->SetForegroundColor(__pLabel->GetTextColor(), 0, __pTextObject->GetTextLength());
430 __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
438 _LabelPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
444 _LabelPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
450 _LabelPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
456 _LabelPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
462 _LabelPresenter::OnTouchMoveHandled(const _Control& control)
468 _LabelPresenter::OnCanvasRequestedN(const FloatRectangle& bounds)
470 Canvas* pCanvas = null;
472 if (__lazyDecode == true)
474 Bitmap* pBitmap = null;
476 pBitmap = __pLabel->GetBackgroundBitmap();
480 if (pBitmap->IsNinePatchedBitmap()) // just decode // fix prevent CHECKED_RETURN
488 pCanvas = __pLabel->GetCanvasN();
495 _LabelPresenter::IsEnabledStateChanged(void)
497 bool __currentEnabledState = __pLabel->GetEnableState();
499 if (__currentEnabledState != __previousEnabledState)
501 __previousEnabledState = __currentEnabledState;
510 }}} // Tizen::Ui::Controls