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