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