Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ToolbarPresenter.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_ToolbarPresenter.cpp
19  * @brief               This is the implementation file for the _ToolbarPresenter class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <FGrp_BitmapImpl.h>
24 #include <FGrp_CanvasImpl.h>
25 #include <FGrp_FontImpl.h>
26 #include <FGrp_TextCommon.h>
27 #include <FGrp_TextTextSimple.h>
28 #include "FUi_UiTouchEvent.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUiCtrl_Label.h"
31 #include "FUiCtrl_ToolbarPresenter.h"
32 #include "FUiCtrl_ToolbarModel.h"
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 _ToolbarPresenter::_ToolbarPresenter(void)
43         : __pToolbar(null)
44         , __pToolbarModel(null)
45         , __initialDraw(true)
46         , __tabEditEnabled(false)
47         , __beingEdited(false)
48         , __touchInitiatedInToolbar(false)
49         , __editItemIndex(-1)
50         , __initialPressedItemIndex(-1)
51         , __initialPressesItemStatus(_BUTTON_STATUS_NORMAL)
52         , __pEditItem(null)
53         , __pTitleBackgroundBitmap(null)
54         , __pTitleSlideTimer(null)
55         , __titleRect(Rectangle(0, 0, 0, 0))
56         , __descriptionRect(Rectangle(0, 0, 0, 0))
57         , __titleText(L"")
58         , __pTitleTextFont(null)
59         , __pTextFont(null)
60         , __pDescriptionTextObject(null)
61         , __pTitleTextObject(null)
62         , __titleSlidingAction(TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT)
63         , __fontStyle(0)
64         , __fontSize(0)
65         , __touchMoveHandled(false)
66 {
67         __currentTouchPosition = Point(0, 0);
68 }
69
70 _ToolbarPresenter::~_ToolbarPresenter(void)
71 {
72         if (__pToolbarModel)
73         {
74                 delete __pToolbarModel;
75                 __pToolbarModel = null;
76         }
77
78         if (__pEditItem)
79         {
80                 __pToolbar->DetachChild(*__pEditItem);
81                 delete __pEditItem;
82                 __pEditItem = null;
83         }
84
85         if (__pTitleBackgroundBitmap)
86         {
87                 delete __pTitleBackgroundBitmap;
88                 __pTitleBackgroundBitmap = null;
89         }
90
91         if (__pTitleSlideTimer)
92         {
93                 delete __pTitleSlideTimer;
94                 __pTitleSlideTimer = null;
95         }
96
97         if (__pDescriptionTextObject)
98         {
99                 delete __pDescriptionTextObject;
100                 __pDescriptionTextObject = null;
101         }
102
103         if (__pTitleTextObject)
104         {
105                 delete __pTitleTextObject;
106                 __pTitleTextObject = null;
107         }
108 }
109
110 result
111 _ToolbarPresenter::Construct(const _Toolbar& toolbar)
112 {
113         __pToolbar = const_cast<_Toolbar*>(&toolbar);
114
115         __fontStyle = FONT_STYLE_BOLD;
116
117         return E_SUCCESS;
118 }
119
120 result
121 _ToolbarPresenter::Install(void)
122 {
123         _ToolbarModel* pModel = new (std::nothrow) _ToolbarModel();
124
125         _SetModel(*pModel);
126
127         result r = pModel->Construct();
128
129         return r;
130 }
131
132 result
133 _ToolbarPresenter::Draw(void)
134 {
135         result r = E_SUCCESS;
136
137         ToolbarStyle toolbarStyle = __pToolbar->GetStyle();
138
139         DrawBackground();
140
141         DrawItems();
142
143         Canvas* pCanvas = __pToolbar->GetCanvasN();
144
145         if (pCanvas == null)
146         {
147                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
148
149                 return E_SYSTEM;
150         }
151
152         int buttonItemGap = 0;
153         int headerButtonItemHeight = 0;
154         int separatorHeight = 0;
155         int rightMargin = 0;
156
157         int footerButtonItemWidth = 0;
158         int footerButtonItemHeight = 0;
159         int fontSize;
160
161         Rectangle bounds(0, 0, __pToolbar->GetBounds().width, __pToolbar->GetBounds().height);
162
163         const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
164         const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
165
166         if (__pToolbar->GetOrientation() == _CONTROL_ORIENTATION_PORTRAIT)
167         {
168                 if (bounds.width > portraitSize.width)
169                 {
170                         bounds.width = portraitSize.width;
171                 }
172         }
173         else
174         {
175                 if (bounds.width > landscapeSize.width)
176                 {
177                         bounds.width = landscapeSize.width;
178                 }
179         }
180
181         int segmentedTitleTextAreaHeight = 0;
182
183         if (toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE || toolbarStyle == TOOLBAR_TAB_WITH_TITLE)
184         {
185                 GET_SHAPE_CONFIG(HEADER::SEGMENTED_ITEM_HEIGHT, __pToolbar->GetOrientation(), headerButtonItemHeight);
186                 GET_SHAPE_CONFIG(HEADER::TITLE_HEIGHT_WITH_SEGMENTED_ITEM, __pToolbar->GetOrientation(), segmentedTitleTextAreaHeight);
187         }
188         else
189         {
190                 GET_SHAPE_CONFIG(HEADER::BUTTON_ITEM_HEIGHT, __pToolbar->GetOrientation(), headerButtonItemHeight);
191                 GET_SHAPE_CONFIG(HEADER::BUTTON_ITEM_TOP_MARGIN, __pToolbar->GetOrientation(), segmentedTitleTextAreaHeight);
192         }
193
194         GET_SHAPE_CONFIG(HEADER::SEPARATOR_WIDTH, __pToolbar->GetOrientation(), buttonItemGap);
195         GET_SHAPE_CONFIG(HEADER::SEPARATOR_HEIGHT, __pToolbar->GetOrientation(), separatorHeight);
196         GET_SHAPE_CONFIG(HEADER::RIGHT_MARGIN, __pToolbar->GetOrientation(), rightMargin);
197
198         GET_SHAPE_CONFIG(FOOTER::BUTTON_ITEM_WIDTH, __pToolbar->GetOrientation(), footerButtonItemWidth);
199         GET_SHAPE_CONFIG(FOOTER::BUTTON_ITEM_HEIGHT, __pToolbar->GetOrientation(), footerButtonItemHeight);
200
201         if ((toolbarStyle == TOOLBAR_TITLE || toolbarStyle == TOOLBAR_HEADER_SEGMENTED || toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE
202                         || toolbarStyle == TOOLBAR_TAB || toolbarStyle == TOOLBAR_TAB_WITH_TITLE || toolbarStyle == TOOLBAR_TEXT))
203         {
204                 if (__pTitleBackgroundBitmap)
205                 {
206                         delete __pTitleBackgroundBitmap;
207                         __pTitleBackgroundBitmap = null;
208                 }
209
210                 if (toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE || toolbarStyle == TOOLBAR_TAB_WITH_TITLE)
211                 {
212                         GET_SHAPE_CONFIG(HEADER::TITLE_FONT_SIZE_WITH_SEGMENTED, __pToolbar->GetOrientation(), fontSize);
213
214                         if (!__titleText.IsEmpty())
215                         {
216                                 __fontSize = fontSize;
217
218                                 __pTitleTextFont = __pToolbar->GetFallbackFont();
219                                 r = GetLastResult();
220                                 SysTryReturn(NID_UI_CTRL, __pTitleTextFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
221
222                                 __pTitleTextObject->SetFont(__pTitleTextFont, 0, __pTitleTextObject->GetTextLength());
223                         }
224                 }
225                 else if (toolbarStyle == TOOLBAR_TITLE && __pToolbar->GetDescriptionText() != L""
226                                 && __pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) == ANIMATION_STOPPED)
227                 {
228                         GET_SHAPE_CONFIG(HEADER::TITLE_TOP_MARGIN_WITH_DESCRIPTION, __pToolbar->GetOrientation(), __titleRect.y);
229                         GET_SHAPE_CONFIG(HEADER::TITLE_DISPLAY_HEIGHT_WITH_DESCRIPTION, __pToolbar->GetOrientation(), __titleRect.height);
230                         GET_SHAPE_CONFIG(HEADER::TITLE_FONT_SIZE_WITH_DESCRIPTION, __pToolbar->GetOrientation(), fontSize);
231
232                         DrawDescriptionText();
233
234                         if(!__titleText.IsEmpty())
235                         {
236                                 __fontSize = fontSize;
237
238                                 __pTitleTextFont = __pToolbar->GetFallbackFont();
239                                 r = GetLastResult();
240                                 SysTryReturn(NID_UI_CTRL, __pTitleTextFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
241
242                                 __pTitleTextObject->SetFont(__pTitleTextFont, 0, __pTitleTextObject->GetTextLength());
243                         }
244                 }
245
246                 __pTitleBackgroundBitmap = new (std::nothrow) Bitmap();
247                 __pTitleBackgroundBitmap->Construct(*pCanvas, __titleRect);
248
249                 DrawTitleText(pCanvas);
250         }
251
252         if (toolbarStyle == TOOLBAR_TITLE || toolbarStyle == TOOLBAR_HEADER_SEGMENTED || toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE)
253         {
254                 if (__pToolbar->GetButton(RIGHT_BUTTON))
255                 {
256                         if (__pToolbar->GetButton(LEFT_BUTTON))
257                         {
258                                 if (__pToolbar->GetButton(LEFT_BUTTON)->GetText() != L"" && __pToolbar->GetButton(RIGHT_BUTTON)->GetText() != L"")
259                                 {
260                                         DrawSeparator(TOOLBAR_SEPARATOR_HEADER_BUTTON, Point(bounds.width - __pToolbar->GetButton(RIGHT_BUTTON)->GetBounds().width - buttonItemGap - rightMargin,
261                                                         segmentedTitleTextAreaHeight + (headerButtonItemHeight - separatorHeight) / 2), pCanvas);
262                                 }
263                         }
264                 }
265         }
266
267
268         int itemCount = __pToolbar->GetItemCount();
269
270         if (itemCount == 1)
271         {
272                         //DrawSeparator(TOOLBAR_SEPARATOR_FOOTER_BUTTON, Point(__pToolbar->GetItem(0)->GetBounds().x + __pToolbar->GetItem(0)->GetBounds().width,
273                         //              (__pToolbar->GetBounds().height - separatorHeight) / 2), pCanvas);
274         }
275         else
276         {
277                 if (toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE || toolbarStyle == TOOLBAR_TAB_WITH_TITLE)
278                 {
279                         for (int i = 0; i < itemCount - 1 ; i++)
280                         {
281                                 DrawSeparator(TOOLBAR_SEPARATOR_FOOTER_BUTTON, Point(__pToolbar->GetItem(i)->GetBounds().x + __pToolbar->GetItem(i)->GetBounds().width,
282                                                 segmentedTitleTextAreaHeight + (headerButtonItemHeight - separatorHeight) / 2), pCanvas);
283                         }
284                 }
285                 else
286                 {
287                         for (int i = 0; i < itemCount - 1 ; i++)
288                         {
289                                 if (!(toolbarStyle == TOOLBAR_TEXT || toolbarStyle == TOOLBAR_ICON || toolbarStyle == TOOLBAR_ICON_TEXT))
290                                 {
291                                         DrawSeparator(TOOLBAR_SEPARATOR_FOOTER_BUTTON, Point(__pToolbar->GetItem(i)->GetBounds().x + __pToolbar->GetItem(i)->GetBounds().width,
292                                                 (__pToolbar->GetBounds().height - separatorHeight) / 2), pCanvas);
293                                 }
294                         }
295                 }
296         }
297
298
299         if (__initialDraw)
300         {
301                 if ((toolbarStyle == TOOLBAR_TAB || toolbarStyle == TOOLBAR_TAB_WITH_TITLE
302                                 || toolbarStyle == TOOLBAR_SEGMENTED || toolbarStyle == TOOLBAR_HEADER_SEGMENTED || toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE)
303                                 && __pToolbar->GetItemCount() > 0 && __pToolbarModel->GetSelectedItemIndex() == -1)
304                 {
305                         int firstEnabledItemIndex = __pToolbar->GetFirstEnabledItemIndex();
306
307                         if(firstEnabledItemIndex != -1)
308                         {
309                                 __pToolbar->SetItemSelected(firstEnabledItemIndex, true);
310                         }
311                 }
312
313                 __initialDraw = false;
314         }
315
316         delete pCanvas;
317
318         return E_SUCCESS;
319 }
320
321 void
322 _ToolbarPresenter::DrawBackground(void)
323 {
324         Canvas* pCanvas = __pToolbar->GetCanvasN();
325
326         if (pCanvas == null)
327         {
328                 SysLog(NID_UI_CTRL, "Cannot get a canvas.");
329
330                 return;
331         }
332
333         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
334         pCanvas->Clear();
335
336         ToolbarStyle style = __pToolbar->GetStyle();
337
338         if (style == TOOLBAR_SOFTKEY)
339         {
340                 delete pCanvas;
341
342                 return;
343         }
344
345         Bitmap* pReplacementColorBackgroundBitmap = null;
346         Bitmap* pBackgroundBitmap = null;
347
348         Rectangle bounds(0, 0, __pToolbar->GetBounds().width, __pToolbar->GetBounds().height);
349
350         const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
351         const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
352
353         if (__pToolbar->GetOrientation() == _CONTROL_ORIENTATION_PORTRAIT)
354         {
355                 if (bounds.width > portraitSize.width)
356                 {
357                         bounds.width = portraitSize.width;
358                 }
359         }
360         else
361         {
362                 if (bounds.width > landscapeSize.width)
363                 {
364                         bounds.width = landscapeSize.width;
365                 }
366         }
367
368         pBackgroundBitmap = __pToolbar->GetBackgroundBitmap();
369
370         pReplacementColorBackgroundBitmap =
371                                 _BitmapImpl::GetColorReplacedBitmapN(*pBackgroundBitmap, Color::GetColor(COLOR_ID_MAGENTA), __pToolbar->GetColor());
372
373         if (__pToolbar->IsUserBackgroundBitmap())
374         {
375                 if (pBackgroundBitmap)
376                 {
377                         if (pBackgroundBitmap->IsNinePatchedBitmap())
378                         {
379                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
380                         }
381                         else
382                         {
383                                 pCanvas->DrawBitmap(bounds, *pBackgroundBitmap, Rectangle(0, 0, pBackgroundBitmap->GetWidth(), pBackgroundBitmap->GetHeight()));
384                         }
385                 }
386                 else
387                 {
388                         pCanvas->FillRectangle(__pToolbar->GetColor(), bounds);
389                 }
390         }
391         else
392         {
393                 if (pReplacementColorBackgroundBitmap)
394                 {
395                         if (pReplacementColorBackgroundBitmap->IsNinePatchedBitmap())
396                         {
397                                 pCanvas->DrawNinePatchedBitmap(bounds, *pReplacementColorBackgroundBitmap);
398                         }
399                         else
400                         {
401                                 pCanvas->DrawBitmap(bounds, *pReplacementColorBackgroundBitmap, Rectangle(0, 0, pReplacementColorBackgroundBitmap->GetWidth(),
402                                                         pReplacementColorBackgroundBitmap->GetHeight()));
403                         }
404                 }
405                 else
406                 {
407                         pCanvas->FillRectangle(__pToolbar->GetColor(), bounds);
408                 }
409         }
410
411         delete pCanvas;
412         delete pReplacementColorBackgroundBitmap;
413
414         return;
415 }
416
417 void
418 _ToolbarPresenter::DrawDescriptionText(void)
419 {
420         result r = E_SUCCESS;
421
422         int textAreaHeight = 0;
423         int fontSize = 0;
424         int titleIconWidth = 0;
425
426         GET_SHAPE_CONFIG(HEADER::DESCRIPTION_TEXT_HEIGHT, __pToolbar->GetOrientation(), textAreaHeight);
427         GET_SHAPE_CONFIG(HEADER::DESCRIPTION_TEXT_FONT_SIZE, __pToolbar->GetOrientation(), fontSize);
428         GET_SHAPE_CONFIG(HEADER::ICON_WIDTH, __pToolbar->GetOrientation(), titleIconWidth);
429
430         __fontSize = fontSize;
431
432         __pTextFont = __pToolbar->GetFallbackFont();
433         r = GetLastResult();
434         SysTryReturnVoidResult(NID_UI_CTRL, __pTextFont, r, "[%s] Propagating.", GetErrorMessage(r));
435
436         if (__pToolbar->GetTitleIcon() != null)
437         {
438                 __descriptionRect.x = __toolbarUsableArea.x + titleIconWidth + __toolbarUsableArea.x;
439         }
440         else
441         {
442                 __descriptionRect.x = __toolbarUsableArea.x;
443         }
444
445         __descriptionRect.y = __titleRect.y + __titleRect.height;
446
447         if(__titleText.IsEmpty())
448         {
449                 __descriptionRect.width = __toolbarUsableArea.width;
450         }
451         else
452         {
453                 __descriptionRect.width = __titleRect.width;
454         }
455
456         __descriptionRect.height = textAreaHeight;
457
458         Canvas* pCanvas = __pToolbar->GetCanvasN();
459
460         if (__pDescriptionTextObject)
461         {
462                 __pDescriptionTextObject->SetFont(__pTextFont, 0, __pDescriptionTextObject->GetTextLength());
463                 __pDescriptionTextObject->SetForegroundColor(__pToolbar->GetDescriptionTextColor(), 0, __pDescriptionTextObject->GetTextLength());
464                 __pDescriptionTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
465                 __pDescriptionTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT);
466                 __pDescriptionTextObject->SetBounds(__descriptionRect);
467                 __pDescriptionTextObject->Compose();
468                 __pDescriptionTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
469         }
470
471         delete pCanvas;
472
473         return;
474 }
475
476 void
477 _ToolbarPresenter::DrawEditItem(const Point& point)
478 {
479         if (__pEditItem == null )
480         {
481                 return;
482         }
483
484         Point anchor = Point(0,0);
485
486         anchor.x = point.x - (__pEditItem->GetSize().width / 2);
487         anchor.y = point.y - (__pEditItem->GetSize().width / 2);
488
489         __pEditItem->SetPosition(anchor);
490
491         __pToolbar->Invalidate(true);
492
493         return;
494 }
495
496 void
497 _ToolbarPresenter::DrawSeparator(ToolbarSeparatorType type, const Point& point, Canvas* pCanvas)
498 {
499         if (__pToolbar->GetStyle() == TOOLBAR_SOFTKEY)
500         {
501                 return ;
502         }
503
504         int separatorWidth = 0;
505         int separatorHeight = 0;
506
507         GET_SHAPE_CONFIG(HEADER::SEPARATOR_WIDTH, __pToolbar->GetOrientation(), separatorWidth);
508         GET_SHAPE_CONFIG(HEADER::SEPARATOR_HEIGHT, __pToolbar->GetOrientation(), separatorHeight);
509
510         if (!__pToolbar->IsTransparent())
511         {
512                 Bitmap* pSeparatorBitmap = null;
513                 GET_BITMAP_CONFIG_N(HEADER::DIVIDER_LINE_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pSeparatorBitmap);
514
515                 if (pSeparatorBitmap)
516                 {
517                         if (pSeparatorBitmap->IsNinePatchedBitmap())
518                         {
519                                 pCanvas->DrawNinePatchedBitmap(Rectangle(point.x, point.y, separatorWidth, separatorHeight), *pSeparatorBitmap);
520                         }
521                         else
522                         {
523                                 pCanvas->DrawBitmap(Point(point.x, point.y), *pSeparatorBitmap);
524                         }
525
526                         delete pSeparatorBitmap;
527                 }
528         }
529         else
530         {
531                 Bitmap* pTranslucentSeparatorBitmap = null;
532                 GET_BITMAP_CONFIG_N(HEADER::DIVIDER_LINE_TRANSLUCENT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pTranslucentSeparatorBitmap);
533
534                 if (pTranslucentSeparatorBitmap)
535                 {
536                         if (pTranslucentSeparatorBitmap->IsNinePatchedBitmap())
537                         {
538                                 pCanvas->DrawNinePatchedBitmap(Rectangle(point.x, point.y, separatorWidth, separatorHeight), *pTranslucentSeparatorBitmap);
539                         }
540                         else
541                         {
542                                 pCanvas->DrawBitmap(Point(point.x, point.y), *pTranslucentSeparatorBitmap);
543                         }
544
545                         delete pTranslucentSeparatorBitmap;
546                 }
547         }
548
549         return;
550 }
551
552 void
553 _ToolbarPresenter::DrawItems(void)
554 {
555         result r = E_SUCCESS;
556
557         Canvas* pCanvas = __pToolbar->GetCanvasN();
558
559         SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, E_INVALID_STATE, "[E_INVALID_STATE] System error occurred.");
560
561         Rectangle usableArea = __toolbarUsableArea;
562
563         Bitmap* pTitleIcon = __pToolbar->GetTitleIcon();
564
565         int iconWidth = 0;
566         int titleDisplayHeight = 0;
567         int iconAreaHeight = 0;
568         int leftMargin = 0;
569         int headerTopMargin = 0;
570         int processingAnimationIconSize = 0;
571         int processingAnimationIconGap = 0;
572         int titleCharacterCount = 0;
573         int fontSize = 0;
574         Dimension titleTextDimension(0, 0);
575         int totalWidth = 0;
576         int titleHeight = 0;
577
578         ToolbarStyle toolbarStyle = __pToolbar->GetStyle();
579
580         GET_SHAPE_CONFIG(HEADER::LEFT_MARGIN, __pToolbar->GetOrientation(), leftMargin); // 16
581         GET_SHAPE_CONFIG(HEADER::PROCESSING_ANIMATION_ICON_SIZE, __pToolbar->GetOrientation(), processingAnimationIconSize); // 32
582         GET_SHAPE_CONFIG(HEADER::PROCESSING_ANIMATION_ICON_GAP, __pToolbar->GetOrientation(), processingAnimationIconGap); // 32
583
584         if (__pToolbar->GetStyle() == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE || __pToolbar->GetStyle() == TOOLBAR_TAB_WITH_TITLE)
585         {
586                 GET_SHAPE_CONFIG(HEADER::TITLE_TOP_MARGIN_WITH_SEGMENTED_ITEM, __pToolbar->GetOrientation(), headerTopMargin); // 18, 17
587                 GET_SHAPE_CONFIG(HEADER::TITLE_DISPLAY_HEIGHT_WITH_SEGMENTED_ITEM, __pToolbar->GetOrientation(), titleDisplayHeight); // 60
588         }
589         else
590         {
591                 GET_SHAPE_CONFIG(HEADER::TOP_MARGIN, __pToolbar->GetOrientation(), headerTopMargin); // 34, 17
592                 GET_SHAPE_CONFIG(HEADER::TITLE_DISPLAY_HEIGHT, __pToolbar->GetOrientation(), titleDisplayHeight); //64, 64
593         }
594
595         GET_SHAPE_CONFIG(HEADER::ICON_WIDTH, __pToolbar->GetOrientation(), iconWidth); // 38
596
597         GET_SHAPE_CONFIG(HEADER::TITLE_DISPLAY_HEIGHT_WITH_DESCRIPTION, __pToolbar->GetOrientation(), iconAreaHeight);//50, 42
598
599
600         if (!__titleText.IsEmpty())
601         {
602                 titleCharacterCount = __titleText.GetLength(); // alphabet count
603
604                 if (toolbarStyle == TOOLBAR_HEADER_SEGMENTED_WITH_TITLE || toolbarStyle == TOOLBAR_TAB_WITH_TITLE)
605                 {
606                         r = GET_SHAPE_CONFIG(HEADER::TITLE_FONT_SIZE_WITH_SEGMENTED, __pToolbar->GetOrientation(), fontSize);
607                 }
608                 else if (toolbarStyle == TOOLBAR_TITLE && __pToolbar->GetDescriptionText() != L""
609                                 && __pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) == ANIMATION_STOPPED)
610                 {
611                         r = GET_SHAPE_CONFIG(HEADER::TITLE_FONT_SIZE_WITH_DESCRIPTION, __pToolbar->GetOrientation(), fontSize);
612                 }
613                 else
614                 {
615                         r = GET_SHAPE_CONFIG(HEADER::TITLE_FONT_SIZE, __pToolbar->GetOrientation(), fontSize);
616                 }
617
618                 if (IsFailed(r))
619                 {
620                         delete pCanvas;
621
622                         return;
623                 }
624
625                 __fontSize = fontSize;
626
627                 __pTitleTextFont = __pToolbar->GetFallbackFont();
628                 r = GetLastResult();
629                 SysTryReturnVoidResult(NID_UI_CTRL, __pTitleTextFont, r, "[%s] Propagating.", GetErrorMessage(r));
630
631                 __pTitleTextObject->SetFont(__pTitleTextFont, 0, __pTitleTextObject->GetTextLength());
632
633                 __pTitleTextFont->GetTextExtent(__titleText, titleCharacterCount, titleTextDimension);
634
635                 totalWidth = titleTextDimension.width; // pixel
636                 titleHeight = titleTextDimension.height; // pixel
637
638                 __titleRect.height = _FontImpl::GetInstance(*__pTitleTextFont)->GetLeading();
639
640
641                 if (pTitleIcon)
642                 {
643                         pTitleIcon->Scale(Dimension(iconWidth, iconWidth));
644                 }
645
646                 if (pTitleIcon)
647                 {
648                         totalWidth += (pTitleIcon->GetWidth() + usableArea.x);
649                 }
650
651                 if ((__pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE)) != ANIMATION_STOPPED)
652                 {
653                         totalWidth += processingAnimationIconSize + processingAnimationIconGap;
654                 }
655
656                 if (totalWidth > usableArea.width)
657                 {
658                         __titleRect.width = titleTextDimension.width - (totalWidth - usableArea.width);;
659
660                         if (pTitleIcon)
661                         {
662                                 pCanvas->DrawBitmap(Point(usableArea.x, headerTopMargin + (titleDisplayHeight - pTitleIcon->GetHeight()) / 2), *(pTitleIcon));
663
664                                 __titleRect.x = usableArea.x + pTitleIcon->GetWidth() + usableArea.x;
665                                 __titleRect.y = headerTopMargin + (titleDisplayHeight - titleHeight) / 2;
666
667                                 if (__pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) != ANIMATION_STOPPED)
668                                 {
669                                         __pToolbar->SetWaitingAnimationPosition(TOOLBAR_ANIMATION_POSITION_TITLE,
670                                                         usableArea.x + (pTitleIcon->GetWidth() + processingAnimationIconGap) + __titleRect.width + processingAnimationIconGap,
671                                                         headerTopMargin + (titleDisplayHeight - processingAnimationIconSize) / 2);
672                                 }
673                         }
674                         else
675                         {
676                                 __titleRect.x = usableArea.x;
677                                 __titleRect.y = headerTopMargin + (titleDisplayHeight - titleHeight) / 2;
678
679                                 if (__pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) != ANIMATION_STOPPED)
680                                 {
681                                         __pToolbar->SetWaitingAnimationPosition(TOOLBAR_ANIMATION_POSITION_TITLE,
682                                                         usableArea.x + __titleRect.width + processingAnimationIconGap,
683                                                         headerTopMargin + (titleDisplayHeight - processingAnimationIconSize) / 2);
684                                 }
685                         }
686                 }
687                 else
688                 {
689                         __titleRect.width = usableArea.width;
690
691                         if (pTitleIcon)
692                         {
693                                 __titleRect.x = leftMargin + pTitleIcon->GetWidth() + leftMargin;
694                                 __titleRect.y = headerTopMargin + (titleDisplayHeight - titleHeight) / 2;
695
696                                 if(__pTitleTextObject->GetAlignment() & TEXT_OBJECT_ALIGNMENT_CENTER)
697                                 {
698                                         leftMargin = (__titleRect.width - titleTextDimension.width) / 2;
699                                 }
700                                 else if(__pTitleTextObject->GetAlignment() & TEXT_OBJECT_ALIGNMENT_RIGHT)
701                                 {
702                                         leftMargin = __titleRect.width - titleTextDimension.width - leftMargin * 2;
703                                 }
704
705                                 pCanvas->DrawBitmap(Point(leftMargin, headerTopMargin + (titleDisplayHeight - pTitleIcon->GetHeight()) / 2), *(pTitleIcon));
706
707                                 if (__pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) != ANIMATION_STOPPED)
708                                 {
709                                         __pToolbar->SetWaitingAnimationPosition(TOOLBAR_ANIMATION_POSITION_TITLE,
710                                                         usableArea.x + (pTitleIcon->GetWidth() + processingAnimationIconGap) + titleTextDimension.width + processingAnimationIconGap,
711                                                         headerTopMargin + (titleDisplayHeight - processingAnimationIconSize) / 2);
712                                 }
713
714                                 __titleRect.width -= (pTitleIcon->GetWidth() + usableArea.x);
715                         }
716                         else
717                         {
718                                 __titleRect.x = leftMargin;
719                                 __titleRect.y = headerTopMargin + (titleDisplayHeight - titleHeight) / 2;
720
721                                 if (__pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) != ANIMATION_STOPPED)
722                                 {
723                                         __pToolbar->SetWaitingAnimationPosition(TOOLBAR_ANIMATION_POSITION_TITLE,
724                                                         leftMargin + titleTextDimension.width + processingAnimationIconGap,
725                                                         headerTopMargin + (titleDisplayHeight - processingAnimationIconSize) / 2);
726                                 }
727                         }
728                 }
729         }
730         else // empty title
731         {
732                 if (pTitleIcon && __pToolbar->GetWaitingAnimationStatus(TOOLBAR_ANIMATION_POSITION_TITLE) != ANIMATION_STOPPED)
733                 {
734                         pTitleIcon->Scale(Dimension(iconWidth, iconWidth));
735
736                         if(__pToolbar->GetTitleTextHorizontalAlignment() == ALIGNMENT_CENTER)
737                         {
738                                 leftMargin = (leftMargin + usableArea.width - (pTitleIcon->GetWidth() + processingAnimationIconGap + processingAnimationIconSize)) / 2;
739                         }
740                         else if(__pToolbar->GetTitleTextHorizontalAlignment() == ALIGNMENT_RIGHT)
741                         {
742                                 leftMargin = leftMargin + usableArea.width - (pTitleIcon->GetWidth() + processingAnimationIconGap + processingAnimationIconSize);
743                         }
744
745                         pCanvas->DrawBitmap(Point(leftMargin, headerTopMargin + (titleDisplayHeight - pTitleIcon->GetHeight()) / 2), *(pTitleIcon));
746
747                         __pToolbar->SetWaitingAnimationPosition(TOOLBAR_ANIMATION_POSITION_TITLE,
748                                         leftMargin + processingAnimationIconGap + processingAnimationIconSize,
749                                         headerTopMargin + (titleDisplayHeight - processingAnimationIconSize) / 2);
750                 }
751                 else if (pTitleIcon)
752                 {
753                         pTitleIcon->Scale(Dimension(iconWidth, iconWidth));
754
755                         if(__pToolbar->GetTitleTextHorizontalAlignment() == ALIGNMENT_CENTER)
756                         {
757                                 leftMargin = usableArea.width / 2;
758                         }
759                         else if(__pToolbar->GetTitleTextHorizontalAlignment() == ALIGNMENT_RIGHT)
760                         {
761                                 leftMargin = usableArea.width - pTitleIcon->GetWidth();
762                         }
763
764                         pCanvas->DrawBitmap(Point(leftMargin, headerTopMargin + (titleDisplayHeight - pTitleIcon->GetHeight()) / 2), *(pTitleIcon));
765                 }
766                 else
767                 {
768                         if(__pToolbar->GetTitleTextHorizontalAlignment() == ALIGNMENT_CENTER)
769                         {
770                                 leftMargin = (usableArea.width - (processingAnimationIconGap + processingAnimationIconSize))/ 2;
771                         }
772                         else if(__pToolbar->GetTitleTextHorizontalAlignment() == ALIGNMENT_RIGHT)
773                         {
774                                 leftMargin = usableArea.width - (processingAnimationIconGap + processingAnimationIconSize);
775                         }
776
777                         __pToolbar->SetWaitingAnimationPosition(TOOLBAR_ANIMATION_POSITION_TITLE,
778                                         leftMargin, headerTopMargin + (titleDisplayHeight - processingAnimationIconSize) / 2);
779                 }
780         }
781
782         delete pCanvas;
783
784         return;
785 }
786
787 result
788 _ToolbarPresenter::DrawTitleText(Canvas* pCanvas)
789 {
790         TimerForTitleSlideInit();
791
792         if (__titleText.IsEmpty())
793         {
794                 return E_SUCCESS;
795         }
796
797         __pTitleTextObject->SetForegroundColor(__pToolbar->GetTitleTextColor(), 0, __pTitleTextObject->GetTextLength());
798         __pTitleTextObject->SetBounds(__titleRect);
799         __pTitleTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
800         __pTitleTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT);
801         __pTitleTextObject->Compose();
802         __pTitleTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
803
804         if (__pTitleTextObject->IsActionOn() == true)
805         {
806                 TimerForTitleSlideStart();
807         }
808
809         return E_SUCCESS;
810 }
811
812 int
813 _ToolbarPresenter::GetSelectedItemIndex(void) const
814 {
815         return __pToolbarModel->GetSelectedItemIndex();
816 }
817
818 bool
819 _ToolbarPresenter::IsInitialDraw(void) const
820 {
821         return __initialDraw;
822 }
823
824 bool
825 _ToolbarPresenter::IsTabEditModeEnabled(void) const
826 {
827         return __tabEditEnabled;
828 }
829
830 result
831 _ToolbarPresenter::SetDescriptionText(const String& description)
832 {
833         result r = E_SUCCESS;
834
835         int length = description.GetLength();
836         int fontSize = 0;
837
838         wchar_t* tempString = const_cast<wchar_t*>(description.GetPointer());
839
840         // description text
841         if (__pDescriptionTextObject != null)
842         {
843                 delete __pDescriptionTextObject;
844                 __pDescriptionTextObject = null;
845         }
846
847         __pDescriptionTextObject = new (std::nothrow) TextObject();
848
849         if (__pDescriptionTextObject == null)
850         {
851                 return E_OUT_OF_MEMORY;
852         }
853         __pDescriptionTextObject->Construct();
854         TextSimple* pSimpleText = new (std::nothrow) TextSimple(tempString, length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
855         __pDescriptionTextObject->AppendElement(*pSimpleText);
856         __pDescriptionTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
857         __pDescriptionTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER);
858
859         GET_SHAPE_CONFIG(HEADER::DESCRIPTION_TEXT_FONT_SIZE, __pToolbar->GetOrientation(), fontSize);
860
861         __fontSize = fontSize;
862
863         __pTextFont = __pToolbar->GetFallbackFont();
864         r = GetLastResult();
865         SysTryReturn(NID_UI_CTRL, __pTextFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
866
867         __pDescriptionTextObject->SetFont(__pTextFont, 0, __pDescriptionTextObject->GetTextLength());
868
869         return r;
870 }
871
872 result
873 _ToolbarPresenter::SetItemSelected(int itemIndex)
874 {
875         return __pToolbarModel->SetSelectedItemIndex(itemIndex);
876 }
877
878 result
879 _ToolbarPresenter::SetInitialDrawState(bool state)
880 {
881         DrawBackground();
882         __initialDraw = state;
883         __titleText = L"";
884
885         return E_SUCCESS;
886 }
887
888 result
889 _ToolbarPresenter::SetTabEditModeEnabled(bool enable)
890 {
891         __tabEditEnabled = enable;
892
893         return E_SUCCESS;
894 }
895
896 result
897 _ToolbarPresenter::SetTitleText(const String& title, HorizontalAlignment alignment)
898 {
899         result r = E_SUCCESS;
900
901         int length = title.GetLength();
902
903         __titleText = String(title);
904         wchar_t* tempString = const_cast<wchar_t*>(__titleText.GetPointer());
905
906         // title text
907         if (__pTitleTextObject != null)
908         {
909                 delete __pTitleTextObject;
910                 __pTitleTextObject = null;
911         }
912
913         __pTitleTextObject = new (std::nothrow) TextObject();
914
915         if (__pTitleTextObject == null)
916         {
917                 return E_OUT_OF_MEMORY;
918         }
919         __pTitleTextObject->Construct();
920         TextSimple* pSimpleText = new (std::nothrow) TextSimple(tempString, length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
921         __pTitleTextObject->AppendElement(*pSimpleText);
922
923         __pTitleTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT);
924
925         switch (alignment)
926         {
927                 case ALIGNMENT_LEFT:
928                         __pTitleTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT);
929                         break;
930                 case ALIGNMENT_CENTER:
931                         __pTitleTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER);
932                         break;
933                 case ALIGNMENT_RIGHT:
934                         __pTitleTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_RIGHT);
935                         break;
936                 default:
937                         __pTitleTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT);
938                         break;
939         }
940
941         int fontSize;
942
943         r = GET_SHAPE_CONFIG(HEADER::TITLE_FONT_SIZE, __pToolbar->GetOrientation(), fontSize);
944
945         __fontSize = fontSize;
946
947         __pTitleTextFont = __pToolbar->GetFallbackFont();
948         r = GetLastResult();
949         SysTryReturn(NID_UI_CTRL, __pTitleTextFont, r, r, "[%s] Propagating.", GetErrorMessage(r));
950
951         __pTitleTextObject->SetFont(__pTitleTextFont, 0, __pTitleTextObject->GetTextLength());
952
953         return r;
954 }
955
956 result
957 _ToolbarPresenter::SetUsableAreaBounds(const Rectangle& rect)
958 {
959         __toolbarUsableArea = rect;
960
961         return E_SUCCESS;
962 }
963
964 bool
965 _ToolbarPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
966 {
967         __touchInitiatedInToolbar = true;
968
969         __currentTouchPosition = touchinfo.GetCurrentPosition();
970
971         __initialPressedItemIndex = __pToolbar->GetItemIndexFromPosition(__currentTouchPosition);
972
973         if (__initialPressedItemIndex == -1)
974         {
975                 return true;
976         }
977         else
978         {
979                 _Button* pPressedButton = __pToolbar->GetItem(__initialPressedItemIndex);
980
981                 __initialPressesItemStatus = pPressedButton->GetButtonStatus();
982
983                 if (__initialPressesItemStatus == _BUTTON_STATUS_DISABLED)
984                 {
985                         return true;
986                 }
987
988                 pPressedButton->SetButtonStatus(_BUTTON_STATUS_PRESSED);
989         }
990
991         __pToolbar->Invalidate(true);
992
993         return true;
994 }
995
996 bool
997 _ToolbarPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
998 {
999         if (!__touchInitiatedInToolbar)
1000         {
1001                 return false;
1002         }
1003
1004         __touchInitiatedInToolbar = false;
1005
1006         Point touchPoint = touchinfo.GetCurrentPosition();
1007
1008         int currentItemIndex = __pToolbar->GetItemIndexFromPosition(touchPoint);
1009
1010         if ((__pToolbar->GetStyle() == TOOLBAR_TAB || __pToolbar->GetStyle() == TOOLBAR_TAB_WITH_TITLE)&& __beingEdited == true)
1011         {
1012                 __beingEdited = false;
1013
1014                 if (currentItemIndex == -1)
1015                 {
1016                         if (__pEditItem)
1017                         {
1018                                 __pToolbar->DetachChild(*__pEditItem);
1019                                 delete __pEditItem;
1020                                 __pEditItem = null;
1021                         }
1022
1023                         if (__initialPressedItemIndex != -1)
1024                         {
1025                                 __pToolbar->GetItem(__initialPressedItemIndex)->SetButtonStatus(__initialPressesItemStatus);
1026                         }
1027                 }
1028                 else
1029                 {
1030                         __pToolbar->MoveItem(__editItemIndex, currentItemIndex);
1031
1032                         if (__pEditItem)
1033                         {
1034                                 __pToolbar->DetachChild(*__pEditItem);
1035                                 delete __pEditItem;
1036                                 __pEditItem = null;
1037                         }
1038                 }
1039         }
1040         else
1041         {
1042                 if ((__pToolbar->GetStyle() == TOOLBAR_TAB || __pToolbar->GetStyle() == TOOLBAR_TAB_WITH_TITLE)
1043                                 && __pToolbar->GetItemCount() > 4)// && pMoreButton != null)
1044                 {
1045                         if (__initialPressedItemIndex != -1)
1046                         {
1047                                 if (currentItemIndex == __initialPressedItemIndex)
1048                                 {
1049                                         if (__initialPressesItemStatus != _BUTTON_STATUS_DISABLED && __touchMoveHandled != true)
1050                                         {
1051                                                 __pToolbar->SetItemSelected(currentItemIndex, true);
1052                                         }
1053                                         else
1054                                         {
1055                                                 __pToolbar->GetItem(__initialPressedItemIndex)->SetButtonStatus(__initialPressesItemStatus, false);
1056                                         }
1057                                 }
1058                                 else
1059                                 {
1060                                         __pToolbar->GetItem(__initialPressedItemIndex)->SetButtonStatus(__initialPressesItemStatus, false);
1061                                 }
1062                         }
1063                 }
1064                 else
1065                 {
1066                         if (__initialPressedItemIndex != -1 && (currentItemIndex != __initialPressedItemIndex))
1067                         {
1068                                 __pToolbar->GetItem(__initialPressedItemIndex)->SetButtonStatus(__initialPressesItemStatus, false);
1069                         }
1070                         else
1071                         {
1072                                 if (__initialPressedItemIndex != -1 && __initialPressesItemStatus != _BUTTON_STATUS_DISABLED)
1073                                 {
1074                                         __pToolbar->SetItemSelected(currentItemIndex, true);
1075                                 }
1076
1077                                 return true;
1078                         }
1079                 }
1080         }
1081
1082         __touchMoveHandled = false;
1083
1084         // Restore status of other buttons to _BUTTON_STATUS_NORMAL             // this can be removed
1085         int itemCount = __pToolbar->GetItemCount();
1086
1087         for (int j = 0; j < itemCount; j++)
1088         {
1089                 if (__pToolbar->GetItem(j) != null && __pToolbar->GetItem(j)->GetButtonStatus() == _BUTTON_STATUS_PRESSED)
1090                 {
1091                         __pToolbar->GetItem(j)->SetButtonStatus(_BUTTON_STATUS_NORMAL);
1092                 }
1093         }
1094
1095         // Update children
1096         __pToolbar->Invalidate(true);
1097
1098         return true;
1099 }
1100
1101 bool
1102 _ToolbarPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1103 {
1104         if (!__touchInitiatedInToolbar)
1105         {
1106                 return false;
1107         }
1108
1109         if ((__pToolbar->GetStyle() == TOOLBAR_TAB || __pToolbar->GetStyle() == TOOLBAR_TAB_WITH_TITLE)
1110                                 && __pToolbar->GetItemCount() > 4)
1111         {
1112                 __touchMoveHandled = true;
1113         }
1114
1115         Point touchPoint = touchinfo.GetCurrentPosition();
1116
1117         if ((__pToolbar->GetStyle() == TOOLBAR_TAB || __pToolbar->GetStyle() == TOOLBAR_TAB_WITH_TITLE)
1118                         && __beingEdited == true)
1119         {
1120                 DrawEditItem(touchinfo.GetCurrentPosition());
1121         }
1122         else if (__initialPressedItemIndex != -1)
1123         {
1124                 if (__initialPressesItemStatus == _BUTTON_STATUS_DISABLED)
1125                 {
1126                         return false;
1127                 }
1128
1129                 _Button* pCurrentButton = __pToolbar->GetItem(__initialPressedItemIndex);
1130
1131                 if (pCurrentButton == null)
1132                 {
1133                         return false;
1134                 }
1135
1136                 Rectangle itemBounds = pCurrentButton->GetBounds();
1137
1138                 if (__touchMoveHandled)
1139                 {
1140                         pCurrentButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
1141                 }
1142                 else if (itemBounds.Contains(touchPoint))
1143                 {
1144                         pCurrentButton->SetButtonStatus(_BUTTON_STATUS_PRESSED);
1145                 }
1146                 else
1147                 {
1148                         pCurrentButton->SetButtonStatus(__initialPressesItemStatus, false);
1149                 }
1150
1151                 pCurrentButton->Invalidate(false);
1152         }
1153
1154         if ((__pToolbar->GetStyle() == TOOLBAR_TAB || __pToolbar->GetStyle() == TOOLBAR_TAB_WITH_TITLE)
1155                         && __pToolbar->GetItemCount() > 4)
1156         {
1157                 int distance = (touchinfo.GetCurrentPosition()).x - __currentTouchPosition.x;
1158                 int sideMargin = 0;
1159
1160                 GET_SHAPE_CONFIG(HEADER::TAB_LEFT_MARGIN, __pToolbar->GetOrientation(), sideMargin);
1161
1162                 Rectangle clientBounds = __pToolbar->GetBounds();
1163
1164                 const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
1165                 const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
1166
1167                 if (__pToolbar->GetOrientation() == _CONTROL_ORIENTATION_PORTRAIT)
1168                 {
1169                         if (clientBounds.width > portraitSize.width)
1170                         {
1171                                 clientBounds.width = portraitSize.width;
1172                         }
1173                 }
1174                 else
1175                 {
1176                         if (clientBounds.width > landscapeSize.width)
1177                         {
1178                                 clientBounds.width = landscapeSize.width;
1179                         }
1180                 }
1181
1182                 __currentTouchPosition = touchinfo.GetCurrentPosition();
1183
1184                 _Button* pItem = null;
1185                 Rectangle itemBounds;
1186                 int itemCount = __pToolbar->GetItemCount();
1187
1188                 if (distance > 0) // right move
1189                 {
1190                         pItem = __pToolbar->GetItem(0);
1191                         if (pItem == null)
1192                         {
1193                                 return true;
1194                         }
1195
1196                         itemBounds = pItem->GetBounds();
1197
1198                         if (itemBounds.x == 0)
1199                         {
1200                                 return true;
1201                         }
1202
1203                         if (itemBounds.x + distance >= sideMargin)
1204                         {
1205                                 distance = sideMargin - itemBounds.x;
1206                         }
1207                 }
1208                 else
1209                 {
1210                         pItem = __pToolbar->GetItem(itemCount - 1);
1211
1212                         if (pItem == null)
1213                         {
1214                                 return true;
1215                         }
1216
1217                         itemBounds = pItem->GetBounds();
1218
1219                         if (itemBounds.x + itemBounds.width == clientBounds.width - sideMargin)
1220                         {
1221                                 return true;
1222                         }
1223
1224                         if (itemBounds.x + itemBounds.width + distance <= clientBounds.width - sideMargin)
1225                         {
1226                                 distance = (clientBounds.width - sideMargin) - (itemBounds.x + itemBounds.width);
1227                         }
1228                 }
1229
1230                 AdjustItemPositionX(distance);
1231
1232                 Draw();
1233         }
1234
1235         return false;
1236 }
1237
1238 bool
1239 _ToolbarPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
1240 {
1241         __touchInitiatedInToolbar = false;
1242
1243         // Restore status of other buttons to _BUTTON_STATUS_NORMAL             // this can be removed
1244         int itemCount = __pToolbar->GetItemCount();
1245
1246         for (int i = 0; i < itemCount; i++)
1247         {
1248                 if (__pToolbar->GetItem(i) != null && __pToolbar->GetItem(i)->GetButtonStatus() == _BUTTON_STATUS_PRESSED)
1249                 {
1250                         __pToolbar->GetItem(i)->SetButtonStatus(_BUTTON_STATUS_NORMAL);
1251                 }
1252         }
1253
1254         // Update children
1255         __pToolbar->Invalidate(true);
1256
1257         return true;
1258 }
1259
1260 bool
1261 _ToolbarPresenter::OnLongPressGestureDetected(void)
1262 {
1263         if (__pToolbar->IsTabEditModeEnabled() == true)
1264         {
1265                 int longPressedItemIndex = __pToolbar->GetItemIndexFromPosition(__currentTouchPosition);
1266
1267                 if (longPressedItemIndex == -1)
1268                 {
1269                         return false;
1270                 }
1271
1272                 __editItemIndex = longPressedItemIndex;
1273
1274                 __beingEdited = true;
1275
1276                 _Button* pButton = null;
1277                 pButton = __pToolbar->GetItem(__editItemIndex);
1278                 SysTryReturn(NID_UI_CTRL, pButton, true, E_INVALID_STATE, "[E_INVALID_STATE] Unable to retrieve a button");
1279
1280                 pButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
1281
1282                 Rectangle itemBounds = pButton->GetBounds();
1283
1284                 if (__pEditItem)
1285                 {
1286                         __pToolbar->DetachChild(*__pEditItem);
1287                         delete __pEditItem;
1288                         __pEditItem = null;
1289                 }
1290
1291                 __pEditItem = _Label::CreateLabelN();
1292                 SysTryCatch(NID_UI_CTRL, __pEditItem, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1293
1294                 Canvas* pCanvas = pButton->GetCanvasN();
1295                 SysTryCatch(NID_UI_CTRL, pCanvas, , E_INVALID_STATE, "[E_INVALID_STATE] Unable to retrieve a Canvas.");
1296
1297                 Bitmap* pEditItemBitmap = new (std::nothrow) Bitmap();
1298                 result r = pEditItemBitmap->Construct(*pCanvas, Rectangle(0, 0, itemBounds.width, itemBounds.height));
1299
1300                 if (IsFailed(r))
1301                 {
1302                         delete pEditItemBitmap;
1303                         pEditItemBitmap = null;
1304
1305                         delete pCanvas;
1306
1307                         goto CATCH;
1308                 }
1309
1310                 __pEditItem->SetBounds(itemBounds);
1311                 __pEditItem->SetBackgroundBitmap(*pEditItemBitmap);
1312
1313                 delete pCanvas;
1314                 delete pEditItemBitmap;
1315
1316                 __pToolbar->AttachChild(*__pEditItem);
1317                 __pToolbar->SetChildAlwaysOnTop(*__pEditItem);
1318
1319                 __pToolbar->Draw();
1320         }
1321
1322     return true;
1323
1324 CATCH:
1325         if (__pEditItem)
1326         {
1327                 delete __pEditItem;
1328                 __pEditItem = null;
1329         }
1330
1331         return true;
1332 }
1333
1334 void
1335 _ToolbarPresenter::OnTimerExpired(Timer& timer)
1336 {
1337         Timer* onTimer = &timer;
1338
1339         if (onTimer == __pTitleSlideTimer)
1340         {
1341                 TimerForTitleSlideTimeout();
1342         }
1343
1344         return;
1345 }
1346
1347 result
1348 _ToolbarPresenter::TimerForTitleSlideInit(void)
1349 {
1350         result r = E_SUCCESS;
1351
1352         if (__pTitleSlideTimer != null)
1353         {
1354                 delete __pTitleSlideTimer;
1355                 __pTitleSlideTimer = null;
1356         }
1357
1358         return r;
1359 }
1360
1361 result
1362 _ToolbarPresenter::TimerForTitleSlideStart(void)
1363 {
1364         result r = E_SUCCESS;
1365
1366         if (__pTitleSlideTimer == null)
1367         {
1368                 __pTitleSlideTimer = new (std::nothrow) Timer();
1369                 if (__pTitleSlideTimer == null)
1370                 {
1371                         r = E_OUT_OF_MEMORY;
1372                         goto CATCH;
1373                 }
1374
1375                 r = __pTitleSlideTimer->Construct(*this);
1376                 if (IsFailed(r))
1377                 {
1378                         delete __pTitleSlideTimer;
1379                         goto CATCH;
1380                 }
1381         }
1382
1383         r = __pTitleSlideTimer->Start(100);
1384         if (IsFailed(r))
1385         {
1386                 goto CATCH;
1387         }
1388
1389 CATCH:
1390         return r;
1391 }
1392
1393 result
1394 _ToolbarPresenter::TimerForTitleSlideTimeout(void)
1395 {
1396         result r = E_SUCCESS;
1397
1398         Canvas* pCanvas = __pToolbar->GetCanvasN();
1399         SysTryReturnResult(NID_UI_CTRL, pCanvas, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pCanvas is invalid!");
1400
1401         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1402         pCanvas->Clear(__titleRect);
1403
1404         if (__pTitleBackgroundBitmap)
1405         {
1406                 pCanvas->DrawBitmap(__titleRect, *__pTitleBackgroundBitmap);
1407         }
1408
1409         if (__pTitleTextObject->IsChanged())
1410         {
1411                 DrawTitleText(pCanvas);
1412         }
1413
1414         __pTitleTextObject->DrawWithOffset(*_CanvasImpl::GetInstance(*pCanvas));
1415
1416         delete pCanvas;
1417
1418         r = TimerForTitleSlideStart();
1419
1420         return r;
1421 }
1422
1423 result
1424 _ToolbarPresenter::AdjustItemPositionX(int distance)
1425 {
1426         int index = 0;
1427         int itemCount = __pToolbar->GetItemCount();
1428         _Button* pItem = __pToolbar->GetItem(index);
1429
1430         while (pItem != null)
1431         {
1432                 Rectangle bounds = pItem->GetBounds();
1433                 bounds.x += distance;
1434                 pItem->SetBounds(bounds);
1435                 index++;
1436
1437                 if (index < itemCount)
1438                 {
1439                         pItem = __pToolbar->GetItem(index);
1440                 }
1441                 else
1442                 {
1443                         break;
1444                 }
1445         }
1446
1447         return E_SUCCESS;
1448 }
1449
1450 Rectangle
1451 _ToolbarPresenter::GetTitleTextBounds(void) const
1452 {
1453         return __titleRect;
1454 }
1455
1456 Rectangle
1457 _ToolbarPresenter::GetDescriptionBounds(void) const
1458 {
1459         return __descriptionRect;
1460 }
1461
1462 result
1463 _ToolbarPresenter::_SetModel(const _ToolbarModel& toolbarModel)
1464 {
1465         __pToolbarModel = const_cast<_ToolbarModel*>(&toolbarModel);
1466
1467         return E_SUCCESS;
1468 }
1469
1470 void
1471 _ToolbarPresenter::OnFontChanged(Font* pFont)
1472 {
1473         __pTextFont = pFont;
1474         __pTitleTextFont = pFont;
1475
1476         return;
1477 }
1478
1479 void
1480 _ToolbarPresenter::OnFontInfoRequested(unsigned long& style, int& size)
1481 {
1482         style = __fontStyle;
1483         size =  __fontSize;
1484
1485         return;
1486 }
1487
1488 void
1489 _ToolbarPresenter::SetFontInfo(unsigned long style, int size)
1490 {
1491         __fontStyle = style;
1492         __fontSize = size;
1493
1494         return;
1495 }
1496
1497 }}} // Tizen::Ui::Controls