Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_LabelPresenter.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_LabelPresenter.cpp
19  * @brief               This is the implementation file for the _LabelPresenter class.
20  */
21
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_ResourceManager.h"
31 #include "FUiAnim_VisualElement.h"
32 #include "FUiCtrl_LabelPresenter.h"
33 #include "FUiCtrl_LabelModel.h"
34 #include "FUiCtrl_Label.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Ui::Animations;
39 using namespace Tizen::Graphics::_Text;
40 using namespace Tizen::App;
41
42 namespace Tizen { namespace Ui { namespace Controls
43 {
44
45 _LabelPresenter::_LabelPresenter(void)
46         : __pLabel(null)
47         , __pLabelModel(null)
48         , __previousEnabledState(false)
49         , __pFont(null)
50         , __pTextObject(null)
51         , __pBase(null)
52         , __textStyle(LABEL_TEXT_STYLE_NORMAL)
53         , __fontStyle(0)
54         , __fontSize(0)
55 {
56
57 }
58
59 _LabelPresenter::~_LabelPresenter(void)
60 {
61         if (__pLabelModel)
62         {
63                 delete __pLabelModel;
64                 __pLabelModel = null;
65         }
66
67         if (__pTextObject)
68         {
69                 delete __pTextObject;
70                 __pTextObject = null;
71         }
72 }
73
74 result
75 _LabelPresenter::Construct(const _Label& label)
76 {
77         result r = E_SUCCESS;
78
79         __pLabel = const_cast <_Label*>(&label);
80
81         __fontStyle = FONT_STYLE_PLAIN;
82         __fontSize =  __pLabel->GetTextSize();
83         __pFont = __pLabel->GetFallbackFont();
84         r = GetLastResult();
85         SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
86
87         __pTextObject = new (std::nothrow) TextObject();
88         SysTryCatch(NID_UI_CTRL, __pTextObject != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
89
90         return E_SUCCESS;
91
92 CATCH:
93         delete __pTextObject;
94         __pTextObject = null;
95
96         return r;
97 }
98
99 result
100 _LabelPresenter::Install(void)
101 {
102         result r = E_SUCCESS;
103
104         __pBase = __pLabel->GetVisualElement();
105
106         _LabelModel* pModel = new (std::nothrow) _LabelModel();
107         SysTryReturn(NID_UI_CTRL, pModel, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
108
109         r = SetModel(*pModel);
110         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
111
112         r = pModel->Construct();
113         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
114
115         return E_SUCCESS;
116
117 CATCH:
118         delete pModel;
119         return r;
120 }
121
122 result
123 _LabelPresenter::SetModel(const _LabelModel& labelModel)
124 {
125         __pLabelModel = const_cast <_LabelModel*>(&labelModel);
126
127         return E_SUCCESS;
128 }
129
130 result
131 _LabelPresenter::InitTextObject(void)
132 {
133         TextSimple* pSimpleText = null;
134
135         pSimpleText = new (std::nothrow) TextSimple(const_cast<wchar_t*>(__pLabel->GetText().GetPointer()), __pLabel->GetText().GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
136         SysTryReturn(NID_UI_CTRL, pSimpleText, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
137
138         __pTextObject->Construct();
139         __pTextObject->AppendElement(*pSimpleText);
140         __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
141         __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
142         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
143         __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
144         __pTextObject->SetBounds(Rectangle(__pLabel->GetLeftMargin(), __pLabel->GetTopMargin(),
145                         __pLabel->GetBounds().width - __pLabel->GetLeftMargin() * 2, __pLabel->GetBounds().height - __pLabel->GetTopMargin() * 2));
146         __pTextObject->Compose();
147
148         return E_SUCCESS;
149 }
150
151 void
152 _LabelPresenter::OnFontChanged(Font* pFont)
153 {
154         __pFont = pFont;
155
156         return;
157 }
158
159 result
160 _LabelPresenter::SetTextConfig(int size, LabelTextStyle style)
161 {
162         result r = E_SUCCESS;
163
164         unsigned long fontStyle = FONT_STYLE_PLAIN;
165         bool isStrikeOut = __pFont->IsStrikeOut();
166         bool isUnderLine = __pFont->IsUnderlined();
167
168         __textStyle = style;
169
170         if (__textStyle == LABEL_TEXT_STYLE_ITALIC)
171         {
172                 fontStyle |= FONT_STYLE_ITALIC;
173         }
174
175         if (__textStyle == LABEL_TEXT_STYLE_BOLD)
176         {
177                 fontStyle |= FONT_STYLE_BOLD;
178         }
179
180         __fontStyle = fontStyle;
181         __fontSize = size;
182
183         __pFont = __pLabel->GetFallbackFont();
184         r = GetLastResult();
185         SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
186
187         __pFont->SetStrikeOut(isStrikeOut);
188         __pFont->SetUnderline(isUnderLine);
189
190         return E_SUCCESS;
191 }
192
193 LabelTextStyle
194 _LabelPresenter::GetTextStyle(void) const
195 {
196         return __textStyle;
197 }
198
199 void
200 _LabelPresenter::SetFontInfo(unsigned long style, int size)
201 {
202         __fontStyle = style;
203         __fontSize = size;
204
205         return;
206 }
207
208 void
209 _LabelPresenter::OnFontInfoRequested(unsigned long& style, int& size)
210 {
211         style = __fontStyle;
212         size =  __fontSize;
213
214         return;
215 }
216
217 TextObject*
218 _LabelPresenter::GetTextObject(void) const
219 {
220         return __pTextObject;
221 }
222
223 void
224 _LabelPresenter::Draw(void)
225 {
226         DrawBackground();
227         DrawText();
228
229         return;
230 }
231
232 void
233 _LabelPresenter::DrawBackground(void)
234 {
235         Bitmap* pBitmap = __pLabel->GetBackgroundBitmap();
236         Bitmap* pEffectBitmap = __pLabel->GetBackgroundEffectBitmap();
237
238         Rectangle bounds(0, 0, __pLabel->GetBounds().width, __pLabel->GetBounds().height);
239
240         Color bgColor = __pLabel->GetBackgroundColor();
241
242         Canvas* pCanvas = null;
243
244         if (pBitmap != null)
245         {
246                 pCanvas = __pLabel->GetCanvasN();
247                 if (pCanvas == null)
248                 {
249                         SysLog(NID_UI_CTRL, "Cannot get a canvas.");
250
251                         return;
252                 }
253
254                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
255                 pCanvas->Clear();
256
257                 if (pBitmap->IsNinePatchedBitmap())
258                 {
259                         pCanvas->DrawNinePatchedBitmap(bounds, *pBitmap);
260                 }
261                 else
262                 {
263                         pCanvas->DrawBitmap(Rectangle(0, 0, bounds.width, bounds.height), *pBitmap);
264                 }
265
266                 if (pEffectBitmap != null)
267                 {
268                         if (pEffectBitmap->IsNinePatchedBitmap())
269                         {
270                                 pCanvas->DrawNinePatchedBitmap(bounds, *pEffectBitmap);
271                         }
272                         else
273                         {
274                                 pCanvas->DrawBitmap(Rectangle(0, 0, bounds.width, bounds.height), *pEffectBitmap);
275                         }
276                 }
277         }
278         else
279         {
280                 if (__pLabel->GetText().IsEmpty() && __pLabel->IsCalledGetCanvasN() == false)
281                 {
282                         __pBase->SetBackgroundColor(_Colorf((float)bgColor.GetRed() / 255, (float)bgColor.GetGreen() / 255, (float)bgColor.GetBlue() / 255, (float)bgColor.GetAlpha() / 255));
283                 }
284                 else
285                 {
286                         pCanvas = __pLabel->GetCanvasN();
287                         if (pCanvas == null)
288                         {
289                                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
290
291                                 return;
292                         }
293
294                         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
295                         pCanvas->Clear();
296                         pCanvas->FillRectangle(__pLabel->GetBackgroundColor(), bounds);
297                 }
298         }
299
300         if (pCanvas)
301         {
302                 delete pCanvas;
303         }
304
305         return;
306 }
307
308 void
309 _LabelPresenter::DrawText(void)
310 {
311         if (__pLabel->GetText().IsEmpty())
312         {
313                 SysLog(NID_UI_CTRL, "Text is empty.");
314
315                 return;
316         }
317
318         TextSimple* pSimpleText = null;
319         TextObjectAlignment horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
320         TextObjectAlignment verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
321
322         int topMargin = __pLabel->GetTopMargin();
323         int leftMargin = __pLabel->GetLeftMargin();
324         Rectangle rect(leftMargin, topMargin, __pLabel->GetBounds().width - leftMargin * 2, __pLabel->GetBounds().height - topMargin * 2 );
325         if (rect.width <= 0 || rect.height <= 0)
326         {
327                 SysLog(NID_UI_CTRL, "text display rectangle is wrong.");
328
329                 return;
330         }
331
332         Canvas* pCanvas = __pLabel->GetCanvasN();
333         if (pCanvas == null)
334         {
335                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
336
337                 return;
338         }
339
340         __pTextObject->RemoveAll();
341         pSimpleText = new (std::nothrow) TextSimple(const_cast<wchar_t*>(__pLabel->GetText().GetPointer()), __pLabel->GetText().GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
342         __pTextObject->AppendElement(*pSimpleText);
343
344         __pTextObject->SetForegroundColor(__pLabel->GetTextColor(), 0, __pTextObject->GetTextLength());
345         __pTextObject->SetBounds(rect);
346         __pTextObject->Compose();
347
348         switch (__pLabel->GetTextHorizontalAlignment())
349         {
350         case ALIGNMENT_LEFT:
351                 horizontalAlign = TEXT_OBJECT_ALIGNMENT_LEFT;
352                 break;
353         case ALIGNMENT_CENTER:
354                 horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
355                 break;
356         default:
357                 horizontalAlign = TEXT_OBJECT_ALIGNMENT_RIGHT;
358         }
359
360         switch (__pLabel->GetTextVerticalAlignment())
361         {
362         case ALIGNMENT_TOP:
363                 verticalAlign = TEXT_OBJECT_ALIGNMENT_TOP;
364                 break;
365         case ALIGNMENT_MIDDLE:
366                 verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
367                 break;
368         default:
369                 verticalAlign = TEXT_OBJECT_ALIGNMENT_BOTTOM;
370         }
371
372         __pTextObject->SetAlignment(horizontalAlign | verticalAlign);
373         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
374         __pTextObject->SetBounds(rect);
375         __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
376
377         delete pCanvas;
378
379         return;
380 }
381
382 bool
383 _LabelPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
384 {
385         return false;
386 }
387
388 bool
389 _LabelPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
390 {
391         return false;
392 }
393
394 bool
395 _LabelPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
396 {
397         return false;
398 }
399
400 bool
401 _LabelPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
402 {
403         return false;
404 }
405
406 void
407 _LabelPresenter::OnTouchMoveHandled(const _Control& control)
408 {
409         return;
410 }
411
412 bool
413 _LabelPresenter::IsEnabledStateChanged(void)
414 {
415         bool __currentEnabledState = __pLabel->GetEnableState();
416
417         if (__currentEnabledState != __previousEnabledState)
418         {
419                 __previousEnabledState = __currentEnabledState;
420                 return true;
421         }
422         else
423         {
424                 return false;
425         }
426 }
427
428 }}} // Tizen::Ui::Controls