Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_Button.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_Button.cpp
19  * @brief               This is the implementation file for the _Button 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_UiTouchEvent.h"
30 #include "FUiCtrl_Button.h"
31 #include "FUiCtrl_ButtonPresenter.h"
32
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Graphics::_Text;
38
39 namespace Tizen { namespace Ui { namespace Controls
40 {
41
42 IMPLEMENT_PROPERTY(_Button);
43
44 _Button::_Button()
45         : __pButtonPresenter(null)
46         , __pActionEvent(null)
47         , __actionId(0)
48         , __text(L"")
49         , __horizontalAlignment(ALIGNMENT_CENTER)
50         , __verticalAlignment(ALIGNMENT_MIDDLE)
51         , __buttonStatus(_BUTTON_STATUS_NORMAL)
52         , __buttonStyle(_BUTTON_STYLE_NORMAL)
53         , __textSize(0)
54         , __textMaxLine(2)
55         , __parentMoveDistance(0)
56         , __previousTouchArea(false)
57         , __userDefinedText(false)
58         , __topMargin(0)
59         , __leftMargin(0)
60         , __drawInner(false)
61         , __userDefinedTextArea(0,0,0,0)
62         , __pTextElement(null)
63 {
64         result r = E_SUCCESS;
65
66         _AccessibilityContainer* pContainer = null;
67
68         GET_SHAPE_CONFIG(BUTTON::DEFAULT_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __textSize);
69
70         _ButtonPresenter* pPresenter = new (std::nothrow) _ButtonPresenter();
71         SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
72
73         r = SetPresenter(*pPresenter);
74         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
75
76         r = pPresenter->Construct(*this);
77         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
78
79         r = pPresenter->Install();
80         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
81
82         for (int i = 0; i < NUMBER_OF_BUTTON_STATUS; i++)
83         {
84                 __pBitmap[i] = null;
85                 __pToolbarItemBackgroundBitmap[i] = null;
86                 __pBackgroundBitmap[i] = null;
87                 __pBackgroundEffectBitmap[i] = null;
88                 __isUserBackgroundBitmap[i] = false;
89                 __isUserBackgroundEffectBitmap[i] = false;
90         }
91
92         __pToolbarSelectedBitmap = null;
93
94         GET_COLOR_CONFIG(BUTTON::BG_NORMAL, __color[_BUTTON_STATUS_NORMAL]);
95         GET_COLOR_CONFIG(BUTTON::BG_DISABLED, __color[_BUTTON_STATUS_DISABLED]);
96         GET_COLOR_CONFIG(BUTTON::BG_PRESSED, __color[_BUTTON_STATUS_PRESSED]);
97         GET_COLOR_CONFIG(BUTTON::BG_HIGHLIGHTED, __color[_BUTTON_STATUS_HIGHLIGHTED]);
98         GET_COLOR_CONFIG(BUTTON::BG_PRESSED, __color[_BUTTON_STATUS_SELECTED]);
99
100         GET_COLOR_CONFIG(BUTTON::TEXT_NORMAL, __textColor[_BUTTON_STATUS_NORMAL]);
101         GET_COLOR_CONFIG(BUTTON::TEXT_DISABLED, __textColor[_BUTTON_STATUS_DISABLED]);
102         GET_COLOR_CONFIG(BUTTON::TEXT_PRESSED, __textColor[_BUTTON_STATUS_PRESSED]);
103         GET_COLOR_CONFIG(BUTTON::TEXT_HIGHLIGHTED, __textColor[_BUTTON_STATUS_HIGHLIGHTED]);
104         GET_COLOR_CONFIG(BUTTON::TEXT_PRESSED, __textColor[_BUTTON_STATUS_SELECTED]);
105
106         GET_SHAPE_CONFIG(BUTTON::TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __topMargin);
107         GET_SHAPE_CONFIG(BUTTON::LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
108
109         AddPropertyChangeEventListener(*this);
110
111         pContainer = GetAccessibilityContainer();
112
113         if(pContainer)
114         {
115                 pContainer->Activate(true);
116         }
117
118         ClearLastResult();
119
120         return;
121
122 CATCH:
123         delete pPresenter;
124 }
125
126 _Button*
127 _Button::CreateButtonN(void)
128 {
129         _Button* pButton = new (std::nothrow) _Button();
130         SysTryReturn(NID_UI_CTRL, pButton, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
131         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
132
133         pButton->AcquireHandle();
134
135         return pButton;
136
137 CATCH:
138         delete pButton;
139         return null;
140 }
141
142 _Button::~_Button(void)
143 {
144         if (__pButtonPresenter)
145         {
146                 delete __pButtonPresenter;
147                 __pButtonPresenter = null;
148         }
149
150         if (__pActionEvent)
151         {
152                 delete __pActionEvent;
153                 __pActionEvent = null;
154         }
155
156         for (int i = 0; i < NUMBER_OF_BUTTON_STATUS; i++)
157         {
158                 if (__pBitmap[i])
159                 {
160                         delete __pBitmap[i];
161                         __pBitmap[i] = null;
162                 }
163
164                 if (__pToolbarItemBackgroundBitmap[i])
165                 {
166                         delete __pToolbarItemBackgroundBitmap[i];
167                         __pToolbarItemBackgroundBitmap[i] = null;
168                 }
169
170                 if (__pBackgroundBitmap[i])
171                 {
172                         delete __pBackgroundBitmap[i];
173                         __pBackgroundBitmap[i] = null;
174                 }
175
176                 if (__pBackgroundEffectBitmap[i])
177                 {
178                         delete __pBackgroundEffectBitmap[i];
179                         __pBackgroundEffectBitmap[i] = null;
180                 }
181         }
182
183         if (__pToolbarSelectedBitmap)
184         {
185                 delete __pToolbarSelectedBitmap;
186                 __pToolbarSelectedBitmap = null;
187         }
188
189         if (__pTextElement)
190         {
191                 __pTextElement->Activate(false);
192                 __pTextElement = null;
193         }
194
195         ClearLastResult();
196 }
197
198 result
199 _Button::SetPresenter(const _ButtonPresenter& buttonPresenter)
200 {
201         __pButtonPresenter = const_cast<_ButtonPresenter*>(&buttonPresenter);
202
203         return E_SUCCESS;
204 }
205
206 void
207 _Button::OnDraw(void)
208 {
209         __pButtonPresenter->Draw();
210
211         return;
212 }
213
214 result
215 _Button::OnAttachedToMainTree(void)
216 {
217         InitializeAccessibilityElement();
218
219         return E_SUCCESS;
220 }
221
222 void
223 _Button::InitializeAccessibilityElement(void)
224 {
225         if(__pTextElement)
226         {
227                 return;
228         }
229
230         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
231
232         if(pContainer)
233         {
234                 __pTextElement = new (std::nothrow) _AccessibilityElement(true);
235                 SysTryReturnVoidResult(NID_UI_CTRL, __pTextElement, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
236
237                 __pTextElement->SetBounds(Rectangle(0,0, GetBounds().width, GetBounds().height));
238                 __pTextElement->SetLabel(GetText());
239                 __pTextElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
240                 __pTextElement->SetName(L"ButtonText");
241
242                 pContainer->AddElement(*__pTextElement);
243         }
244
245         return;
246 }
247
248 void
249 _Button::OnPropertyChanging(_PropertyBase& source, const String& name, const Variant& oldValue, const Variant& newValue)
250 {
251         return;
252 }
253
254 void
255 _Button::OnPropertyChanged(_PropertyBase& source, const String& name, const Variant& oldValue, const Variant& newValue)
256 {
257         return;
258 }
259
260 bool
261 _Button::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
262 {
263         return __pButtonPresenter->OnTouchPressed(source, touchinfo);
264 }
265
266 bool
267 _Button::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
268 {
269         return __pButtonPresenter->OnTouchReleased(source, touchinfo);
270 }
271
272 bool
273 _Button::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
274 {
275         return __pButtonPresenter->OnTouchMoved(source, touchinfo);
276 }
277
278 bool
279 _Button::OnTouchCanceled(const _Control & source, const _TouchInfo & touchinfo)
280 {
281         return __pButtonPresenter->OnTouchCanceled(source, touchinfo);
282 }
283
284 void
285 _Button::OnTouchMoveHandled(const _Control& control)
286 {
287         __pButtonPresenter->OnTouchMoveHandled(control);
288
289         return;
290 }
291
292 void
293 _Button::OnBoundsChanged(void)
294 {
295         if(__pTextElement)
296         {
297                 __pTextElement->SetBounds(Rectangle(0,0, GetBounds().width, GetBounds().height));
298         }
299
300         return;
301 }
302
303 void
304 _Button::OnFontChanged(Font* pFont)
305 {
306         __pButtonPresenter->OnFontChanged(pFont);
307
308         return;
309 }
310
311 void
312 _Button::OnFontInfoRequested(unsigned long& style, int& size)
313 {
314         __pButtonPresenter->OnFontInfoRequested(style, size);
315
316         return;
317 }
318
319 result
320 _Button::SetText(const String& text)
321 {
322         return SetProperty("text", Variant(text));
323 }
324
325 result
326 _Button::SetPropertyText(const Variant& text)
327 {
328         __text = text.ToString();
329
330         if(__pTextElement)
331         {
332                 __pTextElement->SetLabel(__text);
333         }
334
335         result r = __pButtonPresenter->InitTextObject();
336         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
337
338         return E_SUCCESS;
339 }
340
341 String
342 _Button::GetText(void) const
343 {
344         Variant text = GetProperty("text");
345
346         return text.ToString();
347 }
348
349 Variant
350 _Button::GetPropertyText(void) const
351 {
352         return Variant(__text);
353 }
354
355 result
356 _Button::SetColor(_ButtonStatus status, const Color& color)
357 {
358         result r = E_SUCCESS;
359
360         switch (status)
361         {
362         case _BUTTON_STATUS_NORMAL:
363                 r = SetProperty("normalColor", Variant(color));
364                 break;
365         case _BUTTON_STATUS_DISABLED:
366                 r = SetProperty("disabledColor", Variant(color));
367                 break;
368         case _BUTTON_STATUS_PRESSED:
369                 r = SetProperty("pressedColor", Variant(color));
370                 break;
371         case _BUTTON_STATUS_HIGHLIGHTED:
372                 r = SetProperty("highlightedColor", Variant(color));
373                 break;
374         default:
375                 r = SetProperty("selectedColor", Variant(color));
376         }
377
378         return r;
379 }
380
381 result
382 _Button::SetPropertyNormalColor(const Variant& color)
383 {
384         __color[_BUTTON_STATUS_NORMAL] = color.ToColor();
385
386         return E_SUCCESS;
387 }
388
389 result
390 _Button::SetPropertyDisabledColor(const Variant& color)
391 {
392         __color[_BUTTON_STATUS_DISABLED] = color.ToColor();
393
394         return E_SUCCESS;
395 }
396
397 result
398 _Button::SetPropertyPressedColor(const Variant& color)
399 {
400         __color[_BUTTON_STATUS_PRESSED] = color.ToColor();
401
402         return E_SUCCESS;
403 }
404
405 result
406 _Button::SetPropertyHighlightedColor(const Variant& color)
407 {
408         __color[_BUTTON_STATUS_HIGHLIGHTED] = color.ToColor();
409
410         return E_SUCCESS;
411 }
412
413 result
414 _Button::SetPropertySelectedColor(const Variant& color)
415 {
416         __color[_BUTTON_STATUS_SELECTED] = color.ToColor();
417
418         return E_SUCCESS;
419 }
420
421 Color
422 _Button::GetColor(_ButtonStatus status) const
423 {
424         Variant color;
425
426         switch (status)
427         {
428         case _BUTTON_STATUS_NORMAL:
429                  color = GetProperty("normalColor");
430                  break;
431         case _BUTTON_STATUS_DISABLED:
432                  color = GetProperty("disabledColor");
433                  break;
434         case _BUTTON_STATUS_PRESSED:
435                  color = GetProperty("pressedColor");
436                  break;;
437         case _BUTTON_STATUS_HIGHLIGHTED:
438                  color = GetProperty("highlightedColor");
439                  break;
440         default:
441                  color = GetProperty("selectedColor");
442         }
443
444         return color.ToColor();
445 }
446
447 Variant
448 _Button::GetPropertyNormalColor(void) const
449 {
450         return Variant(__color[_BUTTON_STATUS_NORMAL]);
451 }
452
453 Variant
454 _Button::GetPropertyDisabledColor(void) const
455 {
456         return Variant(__color[_BUTTON_STATUS_DISABLED]);
457 }
458
459 Variant
460 _Button::GetPropertyPressedColor(void) const
461 {
462         return Variant(__color[_BUTTON_STATUS_PRESSED]);
463 }
464
465 Variant
466 _Button::GetPropertyHighlightedColor(void) const
467 {
468         return Variant(__color[_BUTTON_STATUS_HIGHLIGHTED]);
469 }
470
471 Variant
472 _Button::GetPropertySelectedColor(void) const
473 {
474         return Variant(__color[_BUTTON_STATUS_SELECTED]);
475 }
476
477 result
478 _Button::AddActionEventListener(const _IActionEventListener& listener)
479 {
480         if (__pActionEvent == null)
481         {
482                 __pActionEvent = _ActionEvent::CreateInstanceN(*this);
483                 SysTryReturn(NID_UI_CTRL, __pActionEvent, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
484         }
485
486         result r = __pActionEvent->AddListener(listener);
487         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
488
489         return E_SUCCESS;
490 }
491
492 result
493 _Button::RemoveActionEventListener(const _IActionEventListener& listener)
494 {
495         result r = E_OBJ_NOT_FOUND;
496
497         if (__pActionEvent)
498         {
499                 r = __pActionEvent->RemoveListener(listener);
500         }
501
502         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
503
504         return E_SUCCESS;
505 }
506
507 result
508 _Button::SetActionId(int actionId)
509 {
510         return SetProperty("actionId", Variant(actionId));
511 }
512
513 result
514 _Button::SetPropertyActionId(const Variant& actionId)
515 {
516         __actionId = actionId.ToInt();
517
518         return E_SUCCESS;
519 }
520
521 int
522 _Button::GetActionId(void) const
523 {
524         Variant actionId = GetProperty("actionId");
525
526         return actionId.ToInt();
527 }
528
529 Variant
530 _Button::GetPropertyActionId(void) const
531 {
532         return Variant(__actionId);
533 }
534
535 result
536 _Button::SetTextHorizontalAlignment(HorizontalAlignment alignment)
537 {
538         __horizontalAlignment = alignment;
539
540         return E_SUCCESS;
541 }
542
543 HorizontalAlignment
544 _Button::GetTextHorizontalAlignment(void) const
545 {
546         return __horizontalAlignment;
547 }
548
549 result
550 _Button::SetTextVerticalAlignment(VerticalAlignment alignment)
551 {
552         __verticalAlignment = alignment;
553
554         return E_SUCCESS;
555 }
556
557 VerticalAlignment
558 _Button::GetTextVerticalAlignment(void) const
559 {
560         return __verticalAlignment;
561 }
562
563 result
564 _Button::SetTextColor(_ButtonStatus status, const Color& color)
565 {
566         result r = E_SUCCESS;
567
568         switch (status)
569         {
570         case _BUTTON_STATUS_NORMAL:
571                 r = SetProperty("normalTextColor", Variant(color));
572                 break;
573         case _BUTTON_STATUS_DISABLED:
574                 r = SetProperty("disabledTextColor", Variant(color));
575                 break;
576         case _BUTTON_STATUS_PRESSED:
577                 r = SetProperty("pressedTextColor", Variant(color));
578                 break;
579         case _BUTTON_STATUS_HIGHLIGHTED:
580                 r = SetProperty("highlightedTextColor", Variant(color));
581                 break;
582         default:
583                 r = SetProperty("selectedTextColor", Variant(color));
584         }
585
586         return r;
587 }
588
589 result
590 _Button::SetPropertyNormalTextColor(const Variant& color)
591 {
592         __textColor[_BUTTON_STATUS_NORMAL] = color.ToColor();
593
594         return E_SUCCESS;
595 }
596
597 result
598 _Button::SetPropertyDisabledTextColor(const Variant& color)
599 {
600         __textColor[_BUTTON_STATUS_DISABLED] = color.ToColor();
601
602         return E_SUCCESS;
603 }
604
605 result
606 _Button::SetPropertyPressedTextColor(const Variant& color)
607 {
608         __textColor[_BUTTON_STATUS_PRESSED] = color.ToColor();
609
610         return E_SUCCESS;
611 }
612
613 result
614 _Button::SetPropertyHighlightedTextColor(const Variant& color)
615 {
616         __textColor[_BUTTON_STATUS_HIGHLIGHTED] = color.ToColor();
617
618         return E_SUCCESS;
619 }
620
621 result
622 _Button::SetPropertySelectedTextColor(const Variant& color)
623 {
624         __textColor[_BUTTON_STATUS_SELECTED] = color.ToColor();
625
626         return E_SUCCESS;
627 }
628
629 Color
630 _Button::GetTextColor(_ButtonStatus status) const
631 {
632         Variant color;
633
634         switch (status)
635         {
636         case _BUTTON_STATUS_NORMAL:
637                  color = GetProperty("normalTextColor");
638                  break;
639         case _BUTTON_STATUS_DISABLED:
640                  color = GetProperty("disabledTextColor");
641                  break;
642         case _BUTTON_STATUS_PRESSED:
643                  color = GetProperty("pressedTextColor");
644                  break;
645         case _BUTTON_STATUS_HIGHLIGHTED:
646                  color = GetProperty("highlightedTextColor");
647                  break;
648         default:
649                  color = GetProperty("selectedTextColor");
650         }
651
652         return color.ToColor();
653 }
654
655 Variant
656 _Button::GetPropertyNormalTextColor(void) const
657 {
658         return Variant(__textColor[_BUTTON_STATUS_NORMAL]);
659 }
660
661 Variant
662 _Button::GetPropertyDisabledTextColor(void) const
663 {
664         return Variant(__textColor[_BUTTON_STATUS_DISABLED]);
665 }
666
667 Variant
668 _Button::GetPropertyPressedTextColor(void) const
669 {
670         return Variant(__textColor[_BUTTON_STATUS_PRESSED]);
671 }
672
673 Variant
674 _Button::GetPropertyHighlightedTextColor(void) const
675 {
676         return Variant(__textColor[_BUTTON_STATUS_HIGHLIGHTED]);
677 }
678
679 Variant
680 _Button::GetPropertySelectedTextColor(void) const
681 {
682         return Variant(__textColor[_BUTTON_STATUS_SELECTED]);
683 }
684
685 result
686 _Button::SetBitmap(_ButtonStatus status, const Point& position, const Bitmap& bitmap)
687 {
688         result r = E_SYSTEM;
689
690         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
691
692         if (pClonedBitmap)
693         {
694                 __bitmapPosition[status] = position;
695
696                 if (__pBitmap[status] != null)
697                 {
698                         delete __pBitmap[status];
699                 }
700
701                 __pBitmap[status] = pClonedBitmap;
702
703                 r = E_SUCCESS;
704         }
705
706         return r;
707 }
708
709 Bitmap*
710 _Button::GetBitmap(_ButtonStatus status) const
711 {
712         return __pBitmap[status];
713 }
714
715 result
716 _Button::SetToolbarItemBackgroundBitmap(_ButtonStatus status, const Bitmap& bitmap)
717 {
718         result r = E_SYSTEM;
719
720         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
721
722         if (pClonedBitmap)
723         {
724                 if (__pToolbarItemBackgroundBitmap[status] != null)
725                 {
726                         delete __pToolbarItemBackgroundBitmap[status];
727                 }
728
729                 __pToolbarItemBackgroundBitmap[status] = pClonedBitmap;
730
731                 r = E_SUCCESS;
732         }
733
734         return r;
735 }
736
737 Bitmap*
738 _Button::GetToolbarItemBackgroundBitmap(_ButtonStatus status) const
739 {
740         return __pToolbarItemBackgroundBitmap[status];
741 }
742
743 result
744 _Button::SetToolbarSelectedBitmap(const Tizen::Graphics::Bitmap& bitmap, bool drawInner)
745 {
746         result r = E_SYSTEM;
747
748         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
749
750         if (pClonedBitmap)
751         {
752                 if (__pToolbarSelectedBitmap != null)
753                 {
754                         delete __pToolbarSelectedBitmap;
755                 }
756
757                 __pToolbarSelectedBitmap = pClonedBitmap;
758
759                 r = E_SUCCESS;
760         }
761
762         __drawInner = drawInner;
763
764         return r;
765 }
766
767 bool
768 _Button::IsDrawInner(void) const
769 {
770         return __drawInner;
771 }
772
773 Tizen::Graphics::Bitmap*
774 _Button::GetToolbarSelectedBitmap(void) const
775 {
776         return __pToolbarSelectedBitmap;
777 }
778
779 Point
780 _Button::GetBitmapPosition(_ButtonStatus status) const
781 {
782         return __bitmapPosition[status];
783 }
784
785 result
786 _Button::SetBackgroundBitmap(_ButtonStatus status, const Bitmap& bitmap)
787 {
788         result r = E_SYSTEM;
789
790         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
791
792         if (pClonedBitmap)
793         {
794                 if (__pBackgroundBitmap[status] != null)
795                 {
796                         delete __pBackgroundBitmap[status];
797                 }
798
799                 __pBackgroundBitmap[status] = pClonedBitmap;
800                 __isUserBackgroundBitmap[status] = true;
801
802                 r = E_SUCCESS;
803         }
804
805         return r;
806 }
807
808 Bitmap*
809 _Button::GetBackgroundBitmap(_ButtonStatus status) const
810 {
811         result r = E_SYSTEM;
812
813         _Button* pButton = const_cast<_Button*>(this);
814
815         if (!pButton->__pBackgroundBitmap[status])
816         {
817                 switch(status)
818                 {
819                 case _BUTTON_STATUS_NORMAL:
820                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundBitmap[_BUTTON_STATUS_NORMAL]);
821                         break;
822                 case _BUTTON_STATUS_DISABLED:
823                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundBitmap[_BUTTON_STATUS_DISABLED]);
824                         break;
825                 case _BUTTON_STATUS_PRESSED:
826                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundBitmap[_BUTTON_STATUS_PRESSED]);
827                         break;
828                 case _BUTTON_STATUS_HIGHLIGHTED:
829                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundBitmap[_BUTTON_STATUS_HIGHLIGHTED]);
830                         break;
831                 default:
832                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundBitmap[_BUTTON_STATUS_SELECTED]);
833                         break;
834                 }
835                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
836         }
837
838         return pButton->__pBackgroundBitmap[status];
839 }
840
841 result
842 _Button::SetBackgroundEffectBitmap(_ButtonStatus status, const Bitmap& bitmap)
843 {
844         result r = E_SYSTEM;
845
846         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(bitmap);
847
848         if (pClonedBitmap)
849         {
850                 if (__pBackgroundEffectBitmap[status] != null)
851                 {
852                         delete __pBackgroundEffectBitmap[status];
853                 }
854
855                 __pBackgroundEffectBitmap[status] = pClonedBitmap;
856                 __isUserBackgroundEffectBitmap[status] = true;
857
858                 r= E_SUCCESS;
859         }
860
861         return r;
862 }
863
864 Bitmap*
865 _Button::GetBackgroundEffectBitmap(_ButtonStatus status) const
866 {
867         result r = E_SYSTEM;
868
869         _Button* pButton = const_cast<_Button*>(this);
870
871         if (!pButton->__pBackgroundEffectBitmap[status])
872         {
873                 switch(status)
874                 {
875                 case _BUTTON_STATUS_NORMAL:
876                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundEffectBitmap[_BUTTON_STATUS_NORMAL]);
877                         break;
878                 case _BUTTON_STATUS_DISABLED:
879                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_EFFECT_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundEffectBitmap[_BUTTON_STATUS_DISABLED]);
880                         break;
881                 case _BUTTON_STATUS_PRESSED:
882                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_EFFECT_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundEffectBitmap[_BUTTON_STATUS_PRESSED]);
883                         break;
884                 case _BUTTON_STATUS_HIGHLIGHTED:
885                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_EFFECT_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundEffectBitmap[_BUTTON_STATUS_HIGHLIGHTED]);
886                         break;
887                 default:
888                         r = GET_BITMAP_CONFIG_N(BUTTON::BG_EFFECT_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pButton->__pBackgroundEffectBitmap[_BUTTON_STATUS_SELECTED]);
889                         break;
890                 }
891                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
892         }
893
894         return pButton->__pBackgroundEffectBitmap[status];
895 }
896
897 result
898 _Button::SetTextSize(int size, unsigned long fontStyle)
899 {
900         __pButtonPresenter->SetTextSize(size, fontStyle);
901
902         return SetProperty("textSize", Variant(size));
903 }
904
905 result
906 _Button::SetPropertyTextSize(const Variant& textSize)
907 {
908         result r = E_SUCCESS;
909
910         if (textSize.ToInt() > 0)
911         {
912                 __textSize = textSize.ToInt();
913         }
914         else
915         {
916                 r = E_INVALID_ARG;
917                 SysLogException(NID_UI_CTRL, r, "[E_INVALID_ARG] Text size is no greater than 0.");
918         }
919
920         return r;
921 }
922
923 int
924 _Button::GetTextSize(void) const
925 {
926         Variant size = GetProperty("textSize");
927
928         return size.ToInt();
929 }
930
931 Variant
932 _Button::GetPropertyTextSize(void) const
933 {
934         return Variant(__textSize);
935 }
936
937 result
938 _Button::SetButtonStyle(_ButtonStyle buttonStyle)
939 {
940         __buttonStyle = buttonStyle;
941
942         return E_SUCCESS;
943 }
944
945 _ButtonStyle
946 _Button::GetButtonStyle(void) const
947 {
948         return __buttonStyle;
949 }
950
951 result
952 _Button::SetButtonStatus(_ButtonStatus buttonStatus, bool fire)
953 {
954         result r = E_SUCCESS;
955
956         if (__buttonStatus != buttonStatus)
957         {
958                 __buttonStatus = buttonStatus;
959         }
960
961         if (_BUTTON_STATUS_DISABLED == __buttonStatus)
962         {
963                 SetEnableState(false);
964         }
965         else
966         {
967                 SetEnableState(true);
968         }
969
970         if (_BUTTON_STATUS_SELECTED == __buttonStatus && fire)
971         {
972                 r = FireActionEvent();
973                 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
974         }
975         if(__pTextElement)
976         {
977                 if(buttonStatus == _BUTTON_STATUS_SELECTED )
978                 {
979                         __pTextElement->SetStatus(L"Selected");
980                 }
981                 else
982                 {
983                         __pTextElement->SetStatus(L"");
984                 }
985         }
986         return r;
987 }
988
989 _ButtonStatus
990 _Button::GetButtonStatus(void) const
991 {
992         _ButtonStatus status = __buttonStatus;
993
994         if (!IsEnabled())
995         {
996                 status = _BUTTON_STATUS_DISABLED;
997         }
998
999         return status;
1000 }
1001
1002 result
1003 _Button::SetTextMaxLine(int textMaxLine)
1004 {
1005         __textMaxLine = textMaxLine;
1006
1007         return E_SUCCESS;
1008 }
1009
1010 int
1011 _Button::GetTextMaxLine(void) const
1012 {
1013         return __textMaxLine;
1014 }
1015
1016 result
1017 _Button::UnloadBackgroundBitmap(_ButtonStatus status)
1018 {
1019         if (__pBackgroundBitmap[status] != null)
1020         {
1021                 delete __pBackgroundBitmap[status];
1022                 __pBackgroundBitmap[status] = null;
1023         }
1024         return E_SUCCESS;
1025 }
1026
1027 result
1028 _Button::UnloadBackgroundEffectBitmap(_ButtonStatus status)
1029 {
1030         if (__pBackgroundEffectBitmap[status] != null)
1031         {
1032                 delete __pBackgroundEffectBitmap[status];
1033                 __pBackgroundEffectBitmap[status] = null;
1034         }
1035         return E_SUCCESS;
1036 }
1037
1038 bool
1039 _Button::IsUserBackgroundBitmap(_ButtonStatus status) const
1040 {
1041         return __isUserBackgroundBitmap[status];
1042 }
1043
1044 bool
1045 _Button::IsUserBackgroundEffectBitmap(_ButtonStatus status) const
1046 {
1047         return __isUserBackgroundEffectBitmap[status];
1048 }
1049
1050 bool
1051 _Button::IsTouchAreaChanged(bool currentButtonArea)
1052 {
1053         if (__previousTouchArea != currentButtonArea)
1054         {
1055                 __previousTouchArea = currentButtonArea;
1056                 return true;
1057         }
1058         else
1059         {
1060                 return false;
1061         }
1062 }
1063
1064 result
1065 _Button::FireActionEvent(void)
1066 {
1067         result r = E_SUCCESS;
1068
1069         if (__pActionEvent)
1070         {
1071                 IEventArg* pEventArg = _ActionEvent::CreateActionEventArgN(__actionId);
1072                 SysTryReturn(NID_UI_CTRL, pEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
1073
1074                 __pActionEvent->Fire(*pEventArg);
1075         }
1076
1077         return r;
1078 }
1079
1080 result
1081 _Button::SetUserDefinedTextArea(const Rectangle& bounds)
1082 {
1083         __userDefinedText = true;
1084         __userDefinedTextArea = bounds;
1085
1086         return E_SUCCESS;
1087 }
1088
1089 Rectangle
1090 _Button::GetUserDefinedTextArea(void) const
1091 {
1092         return __userDefinedTextArea;
1093 }
1094
1095 bool
1096 _Button::UserDefinedText(void) const
1097 {
1098         return __userDefinedText;
1099 }
1100
1101 result
1102 _Button::SetMargin(int topMargin, int leftMargin)
1103 {
1104         __topMargin = topMargin;
1105         __leftMargin = leftMargin;
1106
1107         return E_SUCCESS;
1108 }
1109
1110 int
1111 _Button::GetTopMargin(void) const
1112 {
1113         return __topMargin;
1114 }
1115
1116 int
1117 _Button::GetLeftMargin(void) const
1118 {
1119         return __leftMargin;
1120 }
1121
1122 Tizen::Graphics::Dimension
1123 _Button::GetContentSize(void) const
1124 {
1125         return GetContentSizeInternal();
1126 }
1127
1128 Tizen::Graphics::Dimension
1129 _Button::GetContentSizeInternal(void) const
1130 {
1131         for (int i = 0; i < NUMBER_OF_BUTTON_STATUS; i++)
1132         {
1133                 if (__text.IsEmpty() && __pBitmap[i] == null && __isUserBackgroundBitmap[i] == false)
1134                 {
1135                         return Dimension(GetBounds().width, GetBounds().height);
1136                 }
1137         }
1138
1139         Dimension dimension(0,0);
1140         Rectangle contentRect(0,0,0,0);
1141
1142         TextObject* pTextObject = __pButtonPresenter->GetTextObject();
1143
1144         TextObjectActionType previousActionType = pTextObject->GetAction();
1145         TextObjectWrapType previousWrapType = pTextObject->GetWrap();
1146         Rectangle previousRect = pTextObject->GetBounds();
1147
1148         pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
1149         pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
1150         pTextObject->Compose();
1151         dimension = pTextObject->GetTextExtent(0, pTextObject->GetTextLength());
1152
1153         contentRect.width = _ControlManager::GetInstance()->GetScreenSize().width;
1154
1155         if (dimension.width > contentRect.width - GetBounds().x)
1156         {
1157                 dimension.width = contentRect.width - GetBounds().x;
1158
1159                 pTextObject->SetBounds(Rectangle(previousRect.x, previousRect.y, dimension.width, previousRect.height));
1160                 pTextObject->Compose();
1161         }
1162
1163         dimension.height = pTextObject->GetTotalHeight();
1164
1165         pTextObject->SetBounds(previousRect);
1166         pTextObject->SetAction(previousActionType);
1167         pTextObject->SetWrap(previousWrapType);
1168         pTextObject->Compose();
1169
1170         dimension.width += __leftMargin * 4;
1171         dimension.height += __topMargin * 4;
1172
1173         for (int i = 0; i < NUMBER_OF_BUTTON_STATUS; i++)
1174         {
1175                 if (__pBitmap[i] != null || __isUserBackgroundBitmap[i] == true)
1176                 {
1177                         dimension.width = GetBounds().width;
1178                         dimension.height = GetBounds().height;
1179                 }
1180         }
1181
1182         return dimension;
1183 }
1184
1185 int
1186 _Button::GetTextExtentSize(void) const
1187 {
1188         int textExtentSize = 0;
1189
1190         TextObject* pTextObject = __pButtonPresenter->GetTextObject();
1191
1192         TextObjectActionType previousActionType = pTextObject->GetAction();
1193         TextObjectWrapType previousWrapType = pTextObject->GetWrap();
1194         Rectangle previousRect = pTextObject->GetBounds();
1195
1196         pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
1197         pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
1198         pTextObject->Compose();
1199
1200         textExtentSize = pTextObject->GetTextExtent(0, pTextObject->GetTextLength()).width;
1201
1202         pTextObject->SetBounds(previousRect);
1203         pTextObject->SetAction(previousActionType);
1204         pTextObject->SetWrap(previousWrapType);
1205         pTextObject->Compose();
1206
1207         return textExtentSize;
1208 }
1209
1210 }}} // Tizen::Ui::Controls