Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ButtonPresenter.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_ButtonPresenter.cpp
19  * @brief               This is the implementation file for the _ButtonPresenter class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include <FGrp_TextTextSimple.h>
25 #include <FGrp_BitmapImpl.h>
26 #include <FGrp_CanvasImpl.h>
27 #include "FUi_ResourceManager.h"
28 #include "FUi_UiTouchEvent.h"
29 #include "FUiAnim_VisualElement.h"
30 #include "FUiCtrl_ButtonPresenter.h"
31 #include "FUiCtrl_ButtonModel.h"
32 #include "FUiCtrl_Button.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Graphics::_Text;
37 using namespace Tizen::App;
38
39 namespace Tizen { namespace Ui { namespace Controls
40 {
41 _ButtonPresenter::_ButtonPresenter(void)
42         : __pButton(null)
43         , __pButtonModel(null)
44         , __previousEnabledState(false)
45         , __touchMoveHandled(false)
46         , __pFont(null)
47         , __pTextObject(null)
48         , __pBase(null)
49         , __fontStyle(0)
50         , __fontSize(0)
51 {
52
53 }
54
55 _ButtonPresenter::~_ButtonPresenter(void)
56 {
57         if (__pButtonModel)
58         {
59                 delete __pButtonModel;
60                 __pButtonModel = null;
61         }
62
63         if (__pTextObject)
64         {
65                 delete __pTextObject;
66                 __pTextObject = null;
67         }
68 }
69
70 result
71 _ButtonPresenter::Construct(const _Button& button)
72 {
73         result r = E_SUCCESS;
74
75         __pButton = const_cast <_Button*>(&button);
76         __fontStyle = FONT_STYLE_PLAIN;
77         __fontSize =  __pButton->GetTextSize();
78         __pFont = __pButton->GetFallbackFont();
79         r = GetLastResult();
80         SysTryReturn(NID_UI_CTRL, __pFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
81
82         __pTextObject = new (std::nothrow) TextObject();
83         SysTryCatch(NID_UI_CTRL, __pTextObject != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
84
85         return E_SUCCESS;
86
87 CATCH:
88
89         delete __pTextObject;
90         __pTextObject = null;
91
92         return r;
93 }
94
95 result
96 _ButtonPresenter::Install(void)
97 {
98         result r = E_SUCCESS;
99
100         __pBase = __pButton->GetVisualElement();
101
102         _ButtonModel* pModel = new (std::nothrow) _ButtonModel();
103         SysTryReturn(NID_UI_CTRL, pModel, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
104
105         r = SetModel(*pModel);
106         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
107
108         r = pModel->Construct();
109         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
110
111         return E_SUCCESS;
112
113 CATCH:
114         delete pModel;
115         return r;
116 }
117
118 result
119 _ButtonPresenter::SetModel(const _ButtonModel& buttonModel)
120 {
121         __pButtonModel = const_cast <_ButtonModel*>(&buttonModel);
122
123         return E_SUCCESS;
124 }
125
126 result
127 _ButtonPresenter::InitTextObject(void)
128 {
129         TextSimple* pSimpleText = null;
130
131         pSimpleText = new (std::nothrow) TextSimple(const_cast<wchar_t*>(__pButton->GetText().GetPointer()), __pButton->GetText().GetLength());
132         SysTryReturn(NID_UI_CTRL, pSimpleText, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
133
134         __pTextObject->Construct();
135         __pTextObject->AppendElement(*pSimpleText);
136         __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
137         __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
138         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
139         __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
140         __pTextObject->SetBounds(Rectangle(__pButton->GetLeftMargin(), __pButton->GetTopMargin(),
141                         __pButton->GetBounds().width - __pButton->GetLeftMargin() * 2, __pButton->GetBounds().height - __pButton->GetTopMargin() * 2));
142         __pTextObject->Compose();
143
144         return E_SUCCESS;
145 }
146
147 void
148 _ButtonPresenter::OnFontChanged(Font* pFont)
149 {
150         __pFont = pFont;
151
152         return;
153 }
154
155 void
156 _ButtonPresenter::OnFontInfoRequested(unsigned long& style, int& size)
157 {
158         style = __fontStyle;
159         size =  __fontSize;
160
161         return;
162 }
163
164 void
165 _ButtonPresenter::SetTextSize(int size, unsigned long fontStyle)
166 {
167         result r = E_SUCCESS;
168
169         bool isStrikeOut = __pFont->IsStrikeOut();
170         bool isUnderLine = __pFont->IsUnderlined();
171
172         if (__pFont->IsItalic())
173         {
174                 fontStyle |= FONT_STYLE_ITALIC;
175         }
176
177         if (__pFont->IsBold())
178         {
179                 fontStyle |= FONT_STYLE_BOLD;
180         }
181
182         __fontStyle = fontStyle;
183         __fontSize = size;
184
185         __pFont = __pButton->GetFallbackFont();
186         r = GetLastResult();
187         SysTryReturnVoidResult(NID_UI_CTRL, __pFont, r, "[%s] Propagating.", GetErrorMessage(r));
188
189         __pFont->SetStrikeOut(isStrikeOut);
190         __pFont->SetUnderline(isUnderLine);
191
192         return;
193 }
194
195 void
196 _ButtonPresenter::SetFontInfo(unsigned long style, int size)
197 {
198         __fontStyle = style;
199         __fontSize = size;
200
201         return;
202 }
203
204 TextObject*
205 _ButtonPresenter::GetTextObject(void) const
206 {
207         return __pTextObject;
208 }
209
210 void
211 _ButtonPresenter::Draw(void)
212 {
213         _ButtonStatus status = __pButton->GetButtonStatus();
214         Bitmap* pToolbarItemBackgroundBitmap = __pButton->GetToolbarItemBackgroundBitmap(status);
215
216         if (pToolbarItemBackgroundBitmap)
217         {
218                 DrawToolbarItemBackground();
219         }
220         else
221         {
222                 DrawBackground();
223         }
224
225         DrawBitmap();
226         DrawText();
227
228         if (status == _BUTTON_STATUS_SELECTED)
229         {
230                 DrawToolbarSelectedBitmap();
231         }
232
233         return;
234 }
235
236 void
237 _ButtonPresenter::DrawBackground(void)
238 {
239         Canvas* pCanvas = null;
240
241         Bitmap* pReplacementColorBackgroundBitmap = null;
242         Bitmap* pBackgroundBitmap = null;
243         Bitmap* pBackgroundEffectBitmap = null;
244
245         Rectangle bounds(0, 0, __pButton->GetBounds().width, __pButton->GetBounds().height);
246
247         _ButtonStatus status = __pButton->GetButtonStatus();
248
249         if (__pButton->IsUserBackgroundBitmap(status))
250         {
251                 pBackgroundBitmap = __pButton->GetBackgroundBitmap(status);
252                 pBackgroundEffectBitmap = __pButton->GetBackgroundEffectBitmap(status);
253
254                 if (pBackgroundBitmap)
255                 {
256                         pCanvas = __pButton->GetCanvasN();
257                         if (pCanvas == null)
258                         {
259                                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
260                                 return;
261                         }
262                         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
263                         pCanvas->Clear();
264
265                         if (pBackgroundBitmap->IsNinePatchedBitmap()) //ninepatch
266                         {
267                                 if (pBackgroundEffectBitmap)
268                                 {
269                                         if (pBackgroundEffectBitmap->IsNinePatchedBitmap())
270                                         {
271                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
272                                                 if (__pButton->IsUserBackgroundEffectBitmap(status))
273                                                 {
274                                                         pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundEffectBitmap);
275                                                 }
276                                         }
277                                         else
278                                         {
279                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
280                                                 if (__pButton->IsUserBackgroundEffectBitmap(status))
281                                                 {
282                                                         pCanvas->DrawBitmap(bounds, *pBackgroundEffectBitmap, Rectangle(0, 0, pBackgroundEffectBitmap->GetWidth(), pBackgroundEffectBitmap->GetHeight()));
283                                                 }
284                                         }
285                                 }
286                                 else
287                                 {
288                                         pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
289                                 }
290                         }
291                         else // not ninepatch
292                         {
293                                 if (pBackgroundEffectBitmap)
294                                 {
295                                         if (pBackgroundEffectBitmap->IsNinePatchedBitmap())
296                                         {
297                                                 pCanvas->DrawBitmap(bounds, *pBackgroundBitmap, Rectangle(0, 0, pBackgroundBitmap->GetWidth(), pBackgroundBitmap->GetHeight()));
298                                                 if (__pButton->IsUserBackgroundEffectBitmap(status))
299                                                 {
300                                                         pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundEffectBitmap);
301                                                 }
302                                         }
303                                         else
304                                         {
305                                                 pCanvas->DrawBitmap(bounds, *pBackgroundBitmap, Rectangle(0, 0, pBackgroundBitmap->GetWidth(), pBackgroundBitmap->GetHeight()));
306                                                 if (__pButton->IsUserBackgroundEffectBitmap(status))
307                                                 {
308                                                         pCanvas->DrawBitmap(bounds, *pBackgroundEffectBitmap, Rectangle(0, 0, pBackgroundEffectBitmap->GetWidth(), pBackgroundEffectBitmap->GetHeight()));
309                                                 }
310                                         }
311                                 }
312                                 else
313                                 {
314                                         pCanvas->DrawBitmap(bounds, *pBackgroundBitmap, Rectangle(0, 0, pBackgroundBitmap->GetWidth(), pBackgroundBitmap->GetHeight()));
315                                 }
316                         }
317                 }
318                 else
319                 {
320                         pCanvas = __pButton->GetCanvasN();
321                         if (pCanvas == null)
322                         {
323                                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
324                                 return;
325                         }
326                         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
327                         pCanvas->Clear();
328
329                         if (pBackgroundEffectBitmap)
330                         {
331                                 if (pBackgroundEffectBitmap->IsNinePatchedBitmap())
332                                 {
333                                         if (__pButton->IsUserBackgroundEffectBitmap(status))
334                                         {
335                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundEffectBitmap);
336                                         }
337                                 }
338                                 else
339                                 {
340                                         if (__pButton->IsUserBackgroundEffectBitmap(status))
341                                         {
342                                                 pCanvas->DrawBitmap(bounds, *pBackgroundEffectBitmap, Rectangle(0, 0, pBackgroundEffectBitmap->GetWidth(), pBackgroundEffectBitmap->GetHeight()));
343                                         }
344                                 }
345                         }
346                         else
347                         {
348                                 pCanvas->FillRectangle(__pButton->GetColor(status), bounds);
349                         }
350                 }
351         }
352         else // nobody set bitmap (default bitmap draw)
353         {
354                 pCanvas = __pButton->GetCanvasN();
355                 if (pCanvas == null)
356                 {
357                         SysLog(NID_UI_CTRL, "Cannot get a canvas.");
358                         return;
359                 }
360                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
361                 pCanvas->Clear();
362
363                 if (status == _BUTTON_STATUS_DISABLED)
364                 {
365                         pBackgroundBitmap = __pButton->GetBackgroundBitmap(_BUTTON_STATUS_DISABLED);
366
367                         if(!__pButton->IsUserBackgroundBitmap(_BUTTON_STATUS_NORMAL))
368                         {
369                                 pBackgroundEffectBitmap = __pButton->GetBackgroundEffectBitmap(_BUTTON_STATUS_NORMAL);
370                         }
371
372                         pReplacementColorBackgroundBitmap =
373                                         _BitmapImpl::GetColorReplacedBitmapN(*pBackgroundBitmap, Color::GetColor(COLOR_ID_MAGENTA), __pButton->GetColor(_BUTTON_STATUS_DISABLED));
374                 }
375                 else
376                 {
377                         pBackgroundBitmap = __pButton->GetBackgroundBitmap(status);
378                         pBackgroundEffectBitmap = __pButton->GetBackgroundEffectBitmap(status);
379                         pReplacementColorBackgroundBitmap =
380                                         _BitmapImpl::GetColorReplacedBitmapN(*pBackgroundBitmap, Color::GetColor(COLOR_ID_MAGENTA), __pButton->GetColor(status));
381                 }
382
383                 if (pReplacementColorBackgroundBitmap)
384                 {
385                         if (pReplacementColorBackgroundBitmap->IsNinePatchedBitmap())
386                         {
387                                 if (pBackgroundEffectBitmap)
388                                 {
389                                         if (pBackgroundEffectBitmap->IsNinePatchedBitmap())
390                                         {
391                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pReplacementColorBackgroundBitmap);
392                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundEffectBitmap);
393                                         }
394                                         else
395                                         {
396                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pReplacementColorBackgroundBitmap);
397                                                 pCanvas->DrawBitmap(bounds, *pBackgroundEffectBitmap, Rectangle(0, 0, pBackgroundEffectBitmap->GetWidth(), pBackgroundEffectBitmap->GetHeight()));
398                                         }
399                                 }
400                                 else
401                                 {
402                                         pCanvas->DrawNinePatchedBitmap(bounds, *pReplacementColorBackgroundBitmap);
403                                 }
404                         }
405                         else
406                         {
407                                 if (pBackgroundEffectBitmap)
408                                 {
409                                         if (pBackgroundEffectBitmap->IsNinePatchedBitmap())
410                                         {
411                                                 pCanvas->DrawBitmap(bounds, *pReplacementColorBackgroundBitmap, Rectangle(0, 0, pReplacementColorBackgroundBitmap->GetWidth(),
412                                                                 pReplacementColorBackgroundBitmap->GetHeight()));
413                                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundEffectBitmap);
414                                         }
415                                         else
416                                         {
417                                                 pCanvas->DrawBitmap(bounds, *pReplacementColorBackgroundBitmap, Rectangle(0, 0, pReplacementColorBackgroundBitmap->GetWidth(),
418                                                                 pReplacementColorBackgroundBitmap->GetHeight()));
419                                                 pCanvas->DrawBitmap(bounds, *pBackgroundEffectBitmap, Rectangle(0, 0, pBackgroundEffectBitmap->GetWidth(), pBackgroundEffectBitmap->GetHeight()));
420                                         }
421                                 }
422                                 else
423                                 {
424                                         pCanvas->DrawBitmap(bounds, *pReplacementColorBackgroundBitmap, Rectangle(0, 0, pReplacementColorBackgroundBitmap->GetWidth(),
425                                                         pReplacementColorBackgroundBitmap->GetHeight()));
426                                 }
427                         }
428                 }
429                 else
430                 {
431                         if (pBackgroundEffectBitmap)
432                         {
433                                 if (pBackgroundEffectBitmap->IsNinePatchedBitmap())
434                                 {
435                                         pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundEffectBitmap);
436                                 }
437                                 else
438                                 {
439                                         pCanvas->DrawBitmap(bounds, *pBackgroundEffectBitmap, Rectangle(0, 0, pBackgroundEffectBitmap->GetWidth(), pBackgroundEffectBitmap->GetHeight()));
440                                 }
441                         }
442                         else
443                         {
444                                 if (status == _BUTTON_STATUS_DISABLED)
445                                 {
446                                         pCanvas->FillRectangle(__pButton->GetColor(_BUTTON_STATUS_NORMAL), bounds);
447                                 }
448                                 else
449                                 {
450                                         pCanvas->FillRectangle(__pButton->GetColor(status), bounds);
451                                 }
452                         }
453                 }
454         }
455
456         if (!__pButton->IsUserBackgroundBitmap(status))
457         {
458                 __pButton->UnloadBackgroundBitmap(status);
459         }
460
461         if (!__pButton->IsUserBackgroundEffectBitmap(status))
462         {
463                 __pButton->UnloadBackgroundEffectBitmap(status);
464         }
465
466         if (pCanvas)
467         {
468                 delete pCanvas;
469         }
470
471         delete pReplacementColorBackgroundBitmap;
472
473         return;
474 }
475
476 void
477 _ButtonPresenter::DrawToolbarItemBackground(void)
478 {
479         _ButtonStatus status = __pButton->GetButtonStatus();
480         Bitmap* pToolbarItemBackgroundBitmap = __pButton->GetToolbarItemBackgroundBitmap(status);
481
482         if (pToolbarItemBackgroundBitmap)
483         {
484                 Canvas* pCanvas = __pButton->GetCanvasN();
485
486                 if (pCanvas == null)
487                 {
488                         SysLog(NID_UI_CTRL, "Cannot get a canvas.");
489                         return;
490                 }
491
492                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
493                 pCanvas->Clear();
494
495                 Rectangle bounds(0, 0, __pButton->GetBounds().width, __pButton->GetBounds().height);
496
497                 if (pToolbarItemBackgroundBitmap->IsNinePatchedBitmap())
498                 {
499                         pCanvas->DrawNinePatchedBitmap(bounds, *pToolbarItemBackgroundBitmap);
500                 }
501                 else
502                 {
503                         pCanvas->DrawBitmap(bounds, *pToolbarItemBackgroundBitmap, Rectangle(0, 0, pToolbarItemBackgroundBitmap->GetWidth(), pToolbarItemBackgroundBitmap->GetHeight()));
504                 }
505
506                 delete pCanvas;
507         }
508
509         return;
510 }
511
512 void
513 _ButtonPresenter::DrawBitmap(void)
514 {
515         Canvas* pCanvas = null;
516
517         _ButtonStatus status = __pButton->GetButtonStatus();
518
519         Bitmap* pBitmap = __pButton->GetBitmap(status);
520         Bitmap* pNormalBitmap = __pButton->GetBitmap(_BUTTON_STATUS_NORMAL);
521
522         Rectangle bounds(0, 0, __pButton->GetBounds().width, __pButton->GetBounds().height);
523
524         if (pBitmap)
525         {
526                 pCanvas = __pButton->GetCanvasN();
527
528                 if (pCanvas == null)
529                 {
530                         SysLog(NID_UI_CTRL, "Cannot get a canvas.");
531                         return;
532                 }
533
534                 if (__pButton->GetSize().width - __pButton->GetBitmapPosition(status).x < pBitmap->GetWidth()
535                                 || __pButton->GetSize().height - __pButton->GetBitmapPosition(status).y < pBitmap->GetHeight())
536                 {
537                         pCanvas->DrawBitmap(bounds, *pBitmap, Rectangle(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight()));
538                 }
539                 else
540                 {
541                         pCanvas->DrawBitmap(__pButton->GetBitmapPosition(status), *pBitmap);
542                 }
543         }
544         else if (pNormalBitmap)
545         {
546                 pCanvas = __pButton->GetCanvasN();
547
548                 if (pCanvas == null)
549                 {
550                         SysLog(NID_UI_CTRL, "Cannot get a canvas.");
551                         return;
552                 }
553
554                 if (__pButton->GetSize().width - __pButton->GetBitmapPosition(_BUTTON_STATUS_NORMAL).x < pNormalBitmap->GetWidth()
555                                 || __pButton->GetSize().height - __pButton->GetBitmapPosition(_BUTTON_STATUS_NORMAL).y < pNormalBitmap->GetHeight())
556                 {
557                         pCanvas->DrawBitmap(bounds, *pNormalBitmap, Rectangle(0, 0, pNormalBitmap->GetWidth(), pNormalBitmap->GetHeight()));
558                 }
559                 else
560                 {
561                         pCanvas->DrawBitmap(__pButton->GetBitmapPosition(_BUTTON_STATUS_NORMAL), *pNormalBitmap);
562                 }
563         }
564
565         delete pCanvas;
566
567         return;
568 }
569
570 void
571 _ButtonPresenter::DrawText(void)
572 {
573         TextSimple* pSimpleText = null;
574         TextObjectAlignment horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
575         TextObjectAlignment verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
576
577         int buttonTopMargin = __pButton->GetTopMargin();
578         int buttonLeftMargin = __pButton->GetLeftMargin();
579         int multilineFontSize = 0;
580
581         Canvas* pCanvas = __pButton->GetCanvasN();
582         if (pCanvas == null)
583         {
584                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
585                 return;
586         }
587
588         switch (__pButton->GetTextHorizontalAlignment())
589         {
590         case ALIGNMENT_LEFT:
591                 horizontalAlign = TEXT_OBJECT_ALIGNMENT_LEFT;
592                 break;
593         case ALIGNMENT_CENTER:
594                 horizontalAlign = TEXT_OBJECT_ALIGNMENT_CENTER;
595                 break;
596         default:
597                 horizontalAlign = TEXT_OBJECT_ALIGNMENT_RIGHT;
598         }
599
600         switch (__pButton->GetTextVerticalAlignment())
601         {
602         case ALIGNMENT_TOP:
603                 verticalAlign = TEXT_OBJECT_ALIGNMENT_TOP;
604                 break;
605         case ALIGNMENT_MIDDLE:
606                 verticalAlign = TEXT_OBJECT_ALIGNMENT_MIDDLE;
607                 break;
608         default:
609                 verticalAlign = TEXT_OBJECT_ALIGNMENT_BOTTOM;
610         }
611
612         __pTextObject->RemoveAll();
613         pSimpleText = new (std::nothrow) TextSimple(const_cast<wchar_t*>(__pButton->GetText().GetPointer()), __pButton->GetText().GetLength());
614         __pTextObject->AppendElement(*pSimpleText);
615
616         if (__pButton->UserDefinedText())
617         {
618                 __pTextObject->SetBounds(__pButton->GetUserDefinedTextArea());
619         }
620         else
621         {
622                 __pTextObject->SetBounds(Rectangle(buttonLeftMargin, buttonTopMargin,
623                                 __pButton->GetBounds().width - buttonLeftMargin * 2, __pButton->GetBounds().height - buttonTopMargin * 2));
624         }
625         __pTextObject->SetAlignment(horizontalAlign | verticalAlign);
626
627         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
628         __pTextObject->Compose();
629
630         if (__pTextObject->GetTotalLineCount() >= 2)
631         {
632                 GET_SHAPE_CONFIG(BUTTON::MULTILINE_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, multilineFontSize);
633
634                 if (multilineFontSize < __fontSize)
635                 {
636                         __fontSize = multilineFontSize;
637                         SetTextSize(__fontSize);
638
639                         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
640                         __pTextObject->Compose();
641                 }
642         }
643
644         __pTextObject->SetForegroundColor(__pButton->GetTextColor(__pButton->GetButtonStatus()), 0, __pTextObject->GetTextLength());
645         __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
646
647         delete pCanvas;
648
649         return;
650 }
651
652 void
653 _ButtonPresenter::DrawToolbarSelectedBitmap(void)
654 {
655         int selectedBitmapMargin = 0;
656         int selectedBitmapHeight = 0;
657         int segmentedHeight = 0;
658
659         GET_SHAPE_CONFIG(HEADER::HEADER_ITEM_SELECTED_BITMAP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, selectedBitmapMargin);
660         GET_SHAPE_CONFIG(HEADER::HEADER_ITEM_SELECTED_BITMAP_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, selectedBitmapHeight);
661         GET_SHAPE_CONFIG(HEADER::SEGMENTED_ITEM_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, segmentedHeight);
662
663         Bitmap* pToolbarSelectedBitmap = __pButton->GetToolbarSelectedBitmap();
664
665         bool isDrawInner = __pButton->IsDrawInner();
666
667         if (pToolbarSelectedBitmap)
668         {
669                 Canvas* pCanvas = __pButton->GetCanvasN();
670
671                 if (pCanvas == null)
672                 {
673                         SysLog(NID_UI_CTRL, "Cannot get a canvas.");
674                         return;
675                 }
676
677                 Rectangle bounds(selectedBitmapMargin, __pButton->GetBounds().height - (__pButton->GetBounds().height - segmentedHeight) / 2,
678                                 __pButton->GetBounds().width - selectedBitmapMargin * 2, selectedBitmapHeight);
679
680                 if (isDrawInner)
681                 {
682                         bounds.y -= selectedBitmapHeight;
683                 }
684
685                 if (pToolbarSelectedBitmap->IsNinePatchedBitmap())
686                 {
687                         pCanvas->DrawNinePatchedBitmap(bounds, *pToolbarSelectedBitmap);
688                 }
689                 else
690                 {
691                         pCanvas->DrawBitmap(bounds, *pToolbarSelectedBitmap, Rectangle(0, 0, pToolbarSelectedBitmap->GetWidth(), pToolbarSelectedBitmap->GetHeight()));
692                 }
693
694                 delete pCanvas;
695         }
696
697         return;
698 }
699
700 bool
701 _ButtonPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
702 {
703         if (&source != __pButton)
704         {
705                 return false;
706         }
707
708         if (!__pButton->IsEnabled())
709         {
710                 return true;
711         }
712
713         __touchMoveHandled = false;
714         _ButtonStatus status = __pButton->GetButtonStatus();
715
716         if (status != _BUTTON_STATUS_SELECTED)
717         {
718                 __pButton->SetButtonStatus(_BUTTON_STATUS_PRESSED);
719         }
720
721         __pButton->Invalidate();
722
723         if (__pButton->GetButtonStyle() == _BUTTON_STYLE_SEGMENT)
724         {
725                 return false;
726         }
727
728         return true;
729 }
730
731 bool
732 _ButtonPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
733 {
734         if (&source != __pButton)
735         {
736                 return false;
737         }
738
739         if (!__pButton->IsEnabled())
740         {
741                 return true;
742         }
743
744         _ButtonStatus status = __pButton->GetButtonStatus();
745
746         if (__pButton->GetButtonStyle() == _BUTTON_STYLE_SEGMENT)
747         {
748                 return false;
749         }
750
751         if (status == _BUTTON_STATUS_NORMAL)
752         {
753                 return true;
754         }
755
756         Point touchPoint = touchinfo.GetCurrentPosition();
757
758         touchPoint.x += source.GetClientBounds().x;
759         touchPoint.y += source.GetClientBounds().y;
760
761         Rectangle bounds = __pButton->GetClientBounds();
762
763         __pButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
764         __pButton->Invalidate();
765
766         if (bounds.Contains(touchPoint))
767         {
768                 __pButton->FireActionEvent();
769         }
770
771         return true;
772 }
773
774 bool
775 _ButtonPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
776 {
777         if (&source != __pButton)
778         {
779                 return false;
780         }
781
782         if (!__pButton->IsEnabled())
783         {
784                 return true;
785         }
786
787         if (__pButton->GetButtonStyle() == _BUTTON_STYLE_SEGMENT)
788         {
789                 return false;
790         }
791
792         Point touchPoint = touchinfo.GetCurrentPosition();
793
794         touchPoint.x += source.GetClientBounds().x;
795         touchPoint.y += source.GetClientBounds().y;
796
797         Rectangle bounds = __pButton->GetClientBounds();
798         bool isInButtonArea = bounds.Contains(touchPoint);
799         _ButtonStatus oldStatus = __pButton->GetButtonStatus();
800
801         if (isInButtonArea && (__touchMoveHandled != true))
802         {
803                 __pButton->SetButtonStatus(_BUTTON_STATUS_PRESSED);
804         }
805         else
806         {
807                 __pButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
808         }
809
810         if (__pButton->GetButtonStatus() != oldStatus)
811         {
812                 __pButton->Invalidate();
813         }
814
815         return false;
816 }
817
818 bool
819 _ButtonPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
820 {
821         if (&source != __pButton)
822         {
823                 return false;
824         }
825
826         if (!__pButton->IsEnabled())
827         {
828                 return true;
829         }
830
831         if (__pButton->GetButtonStyle() == _BUTTON_STYLE_SEGMENT)
832         {
833                 return false;
834         }
835
836         __pButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
837         __pButton->Invalidate();
838
839         return true;
840 }
841
842 void
843 _ButtonPresenter::OnTouchMoveHandled(const _Control& control)
844 {
845         if (__touchMoveHandled == false)
846         {
847                 __touchMoveHandled = true;
848                 __pButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
849                 __pButton->Invalidate();
850         }
851
852         return;
853 }
854
855 bool
856 _ButtonPresenter::IsEnabledStateChanged(void)
857 {
858         bool __currentEnabledState = __pButton->GetEnableState();
859         bool ret = false;
860
861         if (__currentEnabledState != __previousEnabledState)
862         {
863                 __previousEnabledState = __currentEnabledState;
864                 ret = true;
865         }
866
867         return ret;
868 }
869
870
871
872 }}} // Tizen::Ui::Controls