Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_Label.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_Label.cpp
19  * @brief               This is the implementation file for the _Label class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include <FGrp_BitmapImpl.h>
25 #include "FUi_AccessibilityContainer.h"
26 #include "FUi_AccessibilityElement.h"
27 #include "FUi_AccessibilityManager.h"
28 #include "FUi_ResourceManager.h"
29 #include "FUi_EflWindow.h"
30 #include "FUiCtrl_Label.h"
31 #include "FUiCtrl_LabelPresenter.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui::Animations;
36 using namespace Tizen::Graphics::_Text;
37
38 namespace Tizen { namespace Ui { namespace Controls
39 {
40
41 IMPLEMENT_PROPERTY(_Label);
42
43 _Label::_Label(void)
44         : __pLabelPresenter(null)
45         , __text(L"")
46         , __textSize(0)
47         , __horizontalAlignment(ALIGNMENT_CENTER)
48         , __verticalAlignment(ALIGNMENT_MIDDLE)
49         , __pBackgroundBitmap(null)
50         , __pBackgroundEffectBitmap(null)
51         , __topMargin(0)
52         , __leftMargin(0)
53         , __pTextElement(null)
54 {
55         result r = E_SUCCESS;
56
57         _AccessibilityContainer* pContainer = null;
58
59         GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __textSize);
60
61         _LabelPresenter* pPresenter = new (std::nothrow) _LabelPresenter();
62         SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
63
64         r = SetPresenter(*pPresenter);
65         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
66
67         r = pPresenter->Construct(*this);
68         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
69
70         r = pPresenter->Install();
71         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
72
73         GET_COLOR_CONFIG(LABEL::TEXT_NORMAL, __textColor);
74
75         GET_SHAPE_CONFIG(LABEL::TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __topMargin);
76         GET_SHAPE_CONFIG(LABEL::LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
77
78         SetFocusable(false);
79
80         pContainer = GetAccessibilityContainer();
81
82         if(pContainer)
83         {
84                 pContainer->Activate(true);
85         }
86
87         ClearLastResult();
88
89         return ;
90
91 CATCH:
92         delete pPresenter;
93 }
94
95 _Label*
96 _Label::CreateLabelN(void)
97 {
98         _Label* pLabel = new (std::nothrow) _Label();
99         SysTryReturn(NID_UI_CTRL, pLabel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
100         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
101
102         pLabel->AcquireHandle();
103
104         return pLabel;
105
106 CATCH:
107         delete pLabel;
108         return null;
109 }
110
111 _Label::~_Label(void)
112 {
113         if (__pLabelPresenter)
114         {
115                 delete __pLabelPresenter;
116                 __pLabelPresenter = null;
117         }
118
119         if (__pBackgroundBitmap)
120         {
121                 delete __pBackgroundBitmap;
122                 __pBackgroundBitmap = null;
123         }
124
125         if (__pBackgroundEffectBitmap)
126         {
127                 delete __pBackgroundEffectBitmap;
128                 __pBackgroundEffectBitmap = null;
129         }
130         if (__pTextElement)
131         {
132                 __pTextElement->Activate(false);
133                 __pTextElement = null;
134         }
135         ClearLastResult();
136 }
137
138 result
139 _Label::SetPresenter(const _LabelPresenter& labelPresenter)
140 {
141         __pLabelPresenter = const_cast <_LabelPresenter*>(&labelPresenter);
142
143         return E_SUCCESS;
144 }
145
146 void
147 _Label::OnDraw(void)
148 {
149         __pLabelPresenter->Draw();
150
151         return;
152 }
153
154 result
155 _Label::OnAttachedToMainTree(void)
156 {
157         InitializeAccessibilityElement();
158
159         return E_SUCCESS;
160 }
161
162 void
163 _Label::InitializeAccessibilityElement(void)
164 {
165         if(__pTextElement)
166         {
167                 return;
168         }
169
170         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
171
172         if(pContainer)
173         {
174                 __pTextElement = new (std::nothrow) _AccessibilityElement(true);
175                 SysTryReturnVoidResult(NID_UI_CTRL, __pTextElement, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
176
177                 __pTextElement->SetBounds(Rectangle(0,0, GetBounds().width, GetBounds().height));
178                 __pTextElement->SetLabel(GetText());
179                 __pTextElement->SetTrait(ACCESSIBILITY_TRAITS_LABEL);
180                 __pTextElement->SetName(L"LabelText");
181
182                 pContainer->AddElement(*__pTextElement);
183         }
184
185         return;
186 }
187
188 bool
189 _Label::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
190 {
191         return __pLabelPresenter->OnTouchPressed(source, touchinfo);
192 }
193
194 bool
195 _Label::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
196 {
197         return __pLabelPresenter->OnTouchReleased(source, touchinfo);
198 }
199
200 bool
201 _Label::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
202 {
203         return __pLabelPresenter->OnTouchMoved(source, touchinfo);
204 }
205
206 bool
207 _Label::OnTouchCanceled(const _Control & source, const _TouchInfo & touchinfo)
208 {
209         return __pLabelPresenter->OnTouchCanceled(source, touchinfo);
210 }
211
212 void
213 _Label::OnTouchMoveHandled(const _Control& control)
214 {
215         __pLabelPresenter->OnTouchMoveHandled(control);
216
217         return;
218 }
219
220 void
221 _Label::OnBoundsChanged(void)
222 {
223         if(__pTextElement)
224         {
225                 __pTextElement->SetBounds(Rectangle(0,0, GetBounds().width, GetBounds().height));
226         }
227
228         return;
229 }
230
231 void
232 _Label::OnFontChanged(Font* pFont)
233 {
234         __pLabelPresenter->OnFontChanged(pFont);
235
236         return;
237 }
238
239 void
240 _Label::OnFontInfoRequested(unsigned long& style, int& size)
241 {
242         __pLabelPresenter->OnFontInfoRequested(style, size);
243
244         return;
245 }
246
247 result
248 _Label::SetText(const String& text)
249 {
250         return SetProperty("text", Variant(text));
251 }
252
253 result
254 _Label::SetPropertyText(const Variant& text)
255 {
256         __text = text.ToString();
257
258         if(__pTextElement)
259         {
260                 __pTextElement->SetLabel(__text);
261         }
262
263         result r = __pLabelPresenter->InitTextObject();
264         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
265
266         return E_SUCCESS;
267 }
268
269 result
270 _Label::SetTextColor(const Color& color)
271 {
272         return SetProperty("textColor", Variant(color));
273 }
274
275 result
276 _Label::SetPropertyTextColor(const Variant& color)
277 {
278         __textColor = color.ToColor();
279         return E_SUCCESS;
280 }
281
282 result
283 _Label::SetBackgroundBitmap(const Bitmap& bitmap)
284 {
285         result r = E_SYSTEM;
286
287         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
288
289         if (pClonedBitmap)
290         {
291                 if (__pBackgroundBitmap != null)
292                 {
293                         delete __pBackgroundBitmap;
294                 }
295
296                 __pBackgroundBitmap = pClonedBitmap;
297
298                 r = E_SUCCESS;
299         }
300
301         return r;
302 }
303
304 Bitmap*
305 _Label::GetBackgroundBitmap(void) const
306 {
307         return __pBackgroundBitmap;
308 }
309
310 result
311 _Label::SetBackgroundEffectBitmap(const Bitmap& bitmap)
312 {
313         result r = E_SYSTEM;
314
315         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
316
317         if (pClonedBitmap)
318         {
319                 if (__pBackgroundEffectBitmap != null)
320                 {
321                         delete __pBackgroundEffectBitmap;
322                 }
323
324                 __pBackgroundEffectBitmap = pClonedBitmap;
325
326                 r = E_SUCCESS;
327         }
328
329         return r;
330 }
331
332 Bitmap*
333 _Label::GetBackgroundEffectBitmap(void) const
334 {
335         return __pBackgroundEffectBitmap;
336 }
337
338
339 result
340 _Label::SetTextHorizontalAlignment(HorizontalAlignment alignment)
341 {
342         __horizontalAlignment = alignment;
343         return E_SUCCESS;
344 }
345
346 result
347 _Label::SetTextVerticalAlignment(VerticalAlignment alignment)
348 {
349         __verticalAlignment = alignment;
350         return E_SUCCESS;
351 }
352
353 result
354 _Label::SetTextConfig(int size, LabelTextStyle style)
355 {
356         __pLabelPresenter->SetTextConfig(size, style);
357
358         return SetProperty("textSize", Variant(size));
359 }
360
361 result
362 _Label::SetPropertyTextSize(const Variant& textSize)
363 {
364         __textSize = textSize.ToInt();
365
366         return E_SUCCESS;
367 }
368
369 String
370 _Label::GetText(void) const
371 {
372         Variant text = GetProperty("text");
373
374         return text.ToString();
375 }
376
377 Variant
378 _Label::GetPropertyText(void) const
379 {
380         return Variant(__text);
381 }
382
383 HorizontalAlignment
384 _Label::GetTextHorizontalAlignment(void) const
385 {
386         return __horizontalAlignment;
387 }
388
389 VerticalAlignment
390 _Label::GetTextVerticalAlignment(void) const
391 {
392         return __verticalAlignment;
393 }
394
395 Color
396 _Label::GetTextColor(void) const
397 {
398         Variant color = GetProperty("textColor");
399
400         return color.ToColor();
401 }
402
403 Variant
404 _Label::GetPropertyTextColor(void) const
405 {
406         return Variant(__textColor);
407 }
408
409 int
410 _Label::GetTextSize(void) const
411 {
412         Variant size = GetProperty("textSize");
413
414         return size.ToInt();
415 }
416
417 Variant
418 _Label::GetPropertyTextSize(void) const
419 {
420         return Variant(__textSize);
421 }
422
423 LabelTextStyle
424 _Label::GetTextStyle(void) const
425 {
426         return __pLabelPresenter->GetTextStyle();
427 }
428
429 result
430 _Label::SetMargin(int topMargin, int leftMargin)
431 {
432         __topMargin = topMargin;
433         __leftMargin = leftMargin;
434
435         return E_SUCCESS;
436 }
437
438 int
439 _Label::GetTopMargin(void) const
440 {
441         return __topMargin;
442 }
443
444 int
445 _Label::GetLeftMargin(void) const
446 {
447         return __leftMargin;
448 }
449
450 Tizen::Graphics::Dimension
451 _Label::GetContentSize(void) const
452 {
453         return GetContentSizeInternal();
454 }
455
456 Tizen::Graphics::Dimension
457 _Label::GetContentSizeInternal(void) const
458 {
459         if (__text.IsEmpty() && __pBackgroundBitmap == null)
460         {
461                 return Dimension(GetBounds().width, GetBounds().height);
462         }
463
464         Dimension dimension(0,0);
465         Rectangle contentRect(0,0,0,0);
466
467         TextObject* pTextObject = __pLabelPresenter->GetTextObject();
468
469         TextObjectActionType previousActionType = pTextObject->GetAction();
470         TextObjectWrapType previousWrapType = pTextObject->GetWrap();
471         Rectangle previousRect = pTextObject->GetBounds();
472
473         pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
474         pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
475         pTextObject->Compose();
476         dimension = pTextObject->GetTextExtent(0, pTextObject->GetTextLength());
477
478         contentRect.width = _ControlManager::GetInstance()->GetScreenSize().width;
479
480         if (dimension.width > contentRect.width - GetBounds().x)
481         {
482                 dimension.width = contentRect.width - GetBounds().x;
483
484                 pTextObject->SetBounds(Rectangle(previousRect.x, previousRect.y, dimension.width, previousRect.height));
485                 pTextObject->Compose();
486         }
487
488         dimension.height = pTextObject->GetTotalHeight();
489
490         pTextObject->SetBounds(previousRect);
491         pTextObject->SetAction(previousActionType);
492         pTextObject->SetWrap(previousWrapType);
493         pTextObject->Compose();
494
495         dimension.width += __leftMargin * 2;
496         dimension.height += __topMargin * 2;
497
498         if (__pBackgroundBitmap != null)
499         {
500                 dimension.width = GetBounds().width;
501                 dimension.height = GetBounds().height;
502         }
503
504         return dimension;
505 }
506
507 }}} // Tizen::Ui::Controls
508