Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_SearchBarPresenter.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUiCtrl_SearchBarPresenter.cpp
20  * @brief               This is the implementation file for the _SearchBarPresenter class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FBaseErrorDefine.h>
25 #include <FGrp_BitmapImpl.h>
26 #include "FUi_ResourceManager.h"
27 #include "FUiAnim_VisualElement.h"
28 #include "FUiCtrl_Edit.h"
29 #include "FUiCtrl_Form.h"
30 #include "FUiCtrl_SearchBar.h"
31 #include "FUiCtrl_SearchBarModel.h"
32 #include "FUiCtrl_SearchBarPresenter.h"
33 #include "FUiCtrl_Toolbar.h"
34 #include "FUi_CoordinateSystemUtils.h"
35
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Ui::Animations;
38 using namespace Tizen::Base;
39 using namespace Tizen::Ui;
40
41 namespace Tizen { namespace Ui { namespace Controls {
42
43 _SearchBarPresenter::_SearchBarPresenter(void)
44         : __pSearchBar(null)
45         , __pSearchBarModel(null)
46         , __pCancelButton(null)
47         , __pEdit(null)
48         , __pContainer(null)
49         , __searchFieldBounds(FloatRectangle())
50         , __pIconElement(null)
51         , __pReplacedSearchFieldNormalBitmap(null)
52         , __pReplacedSearchFieldDisabledBitmap(null)
53         , __pReplacedSearchBarBitmap(null)
54         , __pSearchBarBitmap(null)
55         , __pSearchFieldEffectBitmap(null)
56         , __pSearchBarEffectBitmap(null)
57         , __currentBgColor(Color())
58         , __lazyDecode(false)
59 {
60 }
61
62 _SearchBarPresenter::~_SearchBarPresenter(void)
63 {
64         delete __pSearchBarModel;
65         __pSearchBarModel = null;
66
67         if (__pReplacedSearchFieldNormalBitmap)
68         {
69                 delete __pReplacedSearchFieldNormalBitmap;
70                 __pReplacedSearchFieldNormalBitmap = null;
71         }
72
73         if (__pReplacedSearchFieldDisabledBitmap)
74         {
75                 delete __pReplacedSearchFieldDisabledBitmap;
76                 __pReplacedSearchFieldDisabledBitmap = null;
77         }
78
79         if (__pReplacedSearchBarBitmap)
80         {
81                 delete __pReplacedSearchBarBitmap;
82                 __pReplacedSearchBarBitmap = null;
83         }
84
85         if (__pSearchBarBitmap)
86         {
87                 delete __pSearchBarBitmap;
88                 __pSearchBarBitmap = null;
89         }
90
91         if (__pSearchFieldEffectBitmap)
92         {
93                 delete __pSearchFieldEffectBitmap;
94                 __pSearchFieldEffectBitmap = null;
95         }
96
97         if (__pSearchBarEffectBitmap)
98         {
99                 delete __pSearchBarEffectBitmap;
100                 __pSearchBarEffectBitmap = null;
101         }
102 }
103
104 result
105 _SearchBarPresenter::Construct(const _SearchBar& searchBar)
106 {
107         result r = E_SUCCESS;
108         _VisualElement* pSearchBarElement = null;
109
110         __pSearchBar = const_cast <_SearchBar*>(&searchBar);
111         SysTryReturn(NID_UI_CTRL, __pSearchBar, E_SYSTEM, E_SYSTEM,
112                      "[E_SYSTEM] A system error has occurred. The searchbar instance is null.");
113
114         __pCancelButton = __pSearchBar->GetSearchBarButton();
115         SysTryReturn(NID_UI_CTRL, __pCancelButton, E_SYSTEM, E_SYSTEM,
116                      "[E_SYSTEM] A system error has occurred. The cancel button instance is null.");
117
118         __pEdit = __pSearchBar->GetSearchField();
119         SysTryReturn(NID_UI_CTRL, __pEdit, E_SYSTEM, E_SYSTEM,
120                      "[E_SYSTEM] A system error has occurred. The edit instance is null.");
121
122         __pContainer = __pSearchBar->GetSearchBarContainer();
123         SysTryReturn(NID_UI_CTRL, __pContainer, E_SYSTEM, E_SYSTEM,
124                      " [E_SYSTEM] A system error has occurred. Failed to get the content.");
125
126         r = LoadSearchBarBitmaps();
127         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
128
129         _VisualElement* pEditElement = __pEdit->GetVisualElement();
130         SysTryReturn(NID_UI_CTRL, pEditElement, E_SYSTEM, E_SYSTEM,
131                      "[E_SYSTEM] A system error has occurred. Failed to get edit visual element.");
132
133         __pIconElement = new (std::nothrow) _VisualElement();
134         SysTryCatch(NID_UI_CTRL, __pIconElement, , E_OUT_OF_MEMORY,
135                     "[E_OUT_OF_MEMORY] Memory allocation failed.");
136
137         r = __pIconElement->Construct();
138         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
139
140         __pIconElement->SetShowState(true);
141
142         r = __pIconElement->SetSurfaceOpaque(false);
143         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
144
145         __pIconElement->SetImplicitAnimationEnabled(false);
146
147         r = pEditElement->AttachChild(*__pIconElement);
148         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
149
150         pSearchBarElement = __pSearchBar->GetVisualElement();
151         SysTryCatch(NID_UI_CTRL, pSearchBarElement, , E_SYSTEM,
152                     "[E_SYSTEM] A system error has occurred. Failed to get searchbar's visual element.");
153
154         return r;
155
156 CATCH:
157         if (__pIconElement)
158         {
159                 __pIconElement->Destroy();
160                 __pIconElement = null;
161         }
162
163         return r;
164 }
165
166 result
167 _SearchBarPresenter::LoadSearchBarBitmaps()
168 {
169         result r = E_SUCCESS;
170         Color searchFieldIconColor;
171         Bitmap* pSearchFieldBitmap;
172
173         r = GET_BITMAP_CONFIG_N(SEARCHBAR::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pSearchBarBitmap);
174         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
175
176         r = GET_BITMAP_CONFIG_N(SEARCHBAR::BG_NORMAL_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pSearchBarEffectBitmap);
177         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r,"[%s] Propagating.", GetErrorMessage(r));
178
179         __currentBgColor = __pSearchBar->GetColor();
180
181         __pReplacedSearchBarBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pSearchBarBitmap, Color::GetColor(COLOR_ID_MAGENTA), __currentBgColor);
182         SysTryCatch(NID_UI_CTRL, __pReplacedSearchBarBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
183                 GetErrorMessage(GetLastResult()));
184
185         r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pSearchFieldBitmap);
186         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
187
188         r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pSearchFieldEffectBitmap);
189         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r,"[%s] Propagating.", GetErrorMessage(r));
190
191         GET_COLOR_CONFIG(SEARCHBAR::ICON_BG_NORMAL, searchFieldIconColor);
192
193         __pReplacedSearchFieldNormalBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSearchFieldBitmap, Color::GetColor(COLOR_ID_MAGENTA), searchFieldIconColor);
194         SysTryCatch(NID_UI_CTRL, __pReplacedSearchFieldNormalBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
195                 GetErrorMessage(GetLastResult()));
196
197         delete pSearchFieldBitmap;
198         pSearchFieldBitmap = null;
199
200         r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, pSearchFieldBitmap);
201         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r,"[%s] Propagating.", GetErrorMessage(r));
202
203         GET_COLOR_CONFIG(SEARCHBAR::ICON_BG_DISABLED, searchFieldIconColor);
204
205         __pReplacedSearchFieldDisabledBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSearchFieldBitmap, Color::GetColor(COLOR_ID_MAGENTA), searchFieldIconColor);
206         SysTryCatch(NID_UI_CTRL, __pReplacedSearchFieldDisabledBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
207                 GetErrorMessage(GetLastResult()));
208
209         delete pSearchFieldBitmap;
210         pSearchFieldBitmap = null;
211
212         return r;
213
214 CATCH:
215         delete __pSearchBarBitmap;
216         __pSearchBarBitmap = null;
217
218         delete __pSearchBarEffectBitmap;
219         __pSearchBarEffectBitmap = null;
220
221         delete __pReplacedSearchBarBitmap;
222         __pReplacedSearchBarBitmap = null;
223
224         delete pSearchFieldBitmap;
225         pSearchFieldBitmap = null;
226
227         delete __pSearchFieldEffectBitmap;
228         __pSearchFieldEffectBitmap = null;
229
230         delete __pReplacedSearchFieldNormalBitmap;
231         __pReplacedSearchFieldNormalBitmap = null;
232
233         return r;
234 }
235
236 result
237 _SearchBarPresenter::Install(void)
238 {
239         result r = E_SUCCESS;
240
241         _SearchBarModel* pModel = new (std::nothrow) _SearchBarModel;
242         SysTryReturn(NID_UI_CTRL, pModel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
243
244         r = pModel->Construct();
245         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM,
246                     "[E_SYSTEM] A system error has occurred. The searchbar model construction failed.");
247
248         __pSearchBarModel = pModel;
249
250         return E_SUCCESS;
251
252 CATCH:
253         delete pModel;
254         return E_SYSTEM;
255 }
256
257 result
258 _SearchBarPresenter::UpdateContentArea(bool invalidate)
259 {
260         SearchBarMode mode = __pSearchBarModel->GetMode();
261         SysTryReturn(NID_UI_CTRL, mode == SEARCH_BAR_MODE_INPUT, E_INVALID_OPERATION, E_INVALID_OPERATION,
262                      "[E_INVALID_OPERATION] The searchbar mode cannot be updated.");
263
264         __pSearchBar->Invalidate(invalidate);
265
266         return E_SUCCESS;
267 }
268
269 result
270 _SearchBarPresenter::SetContentAreaVisible(bool visible)
271 {
272         return __pSearchBarModel->SetContentAreaVisible(visible);
273 }
274
275 bool
276 _SearchBarPresenter::IsContentAreaVisible(void) const
277 {
278         return __pSearchBarModel->IsContentAreaVisible();
279 }
280
281 SearchBarMode
282 _SearchBarPresenter::GetMode(void) const
283 {
284         return __pSearchBarModel->GetMode();
285 }
286
287 bool
288 _SearchBarPresenter::IsModeLocked(void) const
289 {
290         return __pSearchBarModel->IsModeLocked();
291 }
292
293 result
294 _SearchBarPresenter::SetMode(SearchBarMode mode)
295 {
296         SysTryReturn(NID_UI_CTRL, IsModeLocked() == false, E_INVALID_OPERATION, E_INVALID_OPERATION,
297                      "[E_INVALID_OPERATION] The searchbar mode is locked.");
298
299         if (GetMode() == mode)
300         {
301                 return E_SUCCESS;
302         }
303
304         __pSearchBarModel->SetMode(mode);
305
306         result r = ChangeMode(mode);
307         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
308                      "[E_SYSTEM] A system error has occurred. Failed to change the mode of searchbar.");
309
310         return r;
311 }
312
313 result
314 _SearchBarPresenter::SetModeLocked(bool modeLocked)
315 {
316         return __pSearchBarModel->SetModeLocked(modeLocked);
317 }
318
319 result
320 _SearchBarPresenter::Draw(void)
321 {
322         result r = E_SUCCESS;
323
324         Color bgColor = __pSearchBar->GetColor();
325
326         FloatRectangle bounds(0.0f, 0.0f, __pSearchBar->GetBoundsF().width, __pSearchBar->GetBoundsF().height);
327
328         Bitmap* pBackgroundBitmap = __pSearchBar->GetBackgroundBitmap();
329
330         if (!pBackgroundBitmap)
331         {
332                 r = DrawSearchBarDefaultBitmap();
333                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
334         }
335         else
336         {
337                 String imagePath = L"";
338
339                 if (!_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBackgroundBitmap))
340                 {
341                         imagePath = _BitmapImpl::GetInstance(*pBackgroundBitmap)->GetFileName();
342                 }
343
344                 if(!imagePath.IsEmpty())
345                 {
346                         __pSearchBar->GetVisualElement()->SetBackgroundColor(_Colorf((float)bgColor.GetRed() / 255, (float)bgColor.GetGreen() / 255, (float)bgColor.GetBlue() / 255, (float)bgColor.GetAlpha() / 255));
347                         __pSearchBar->GetVisualElement()->SetImageSource(imagePath);
348                         __lazyDecode = true;
349                 }
350                 else
351                 {
352                         __pSearchBar->GetVisualElement()->SetImageSource(L"");
353                         __lazyDecode = false;
354                         Canvas* pCanvas = __pSearchBar->GetCanvasN();
355                         r = GetLastResult();
356                         SysTryReturn(NID_UI_CTRL, (pCanvas != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
357
358                         pCanvas->SetBackgroundColor(bgColor);
359                         pCanvas->Clear();
360
361                         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBackgroundBitmap))
362                         {
363                                 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
364                         }
365                         else
366                         {
367                                 pCanvas->DrawBitmap(bounds, *pBackgroundBitmap, FloatRectangle(0.0f, 0.0f, pBackgroundBitmap->GetWidthF(), pBackgroundBitmap->GetHeightF()));
368                         }
369
370                         delete pCanvas;
371                 }
372         }
373
374         r = DrawIcon();
375         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
376
377         return r;
378 }
379
380 result
381 _SearchBarPresenter::DrawSearchBarDefaultBitmap(void)
382 {
383         result r = E_SUCCESS;
384         Canvas* pCanvas = null;
385         bool isCustomBitmap = false;
386         FloatRectangle bounds = __pSearchBar->GetBoundsF();
387
388         Color bgColor = __pSearchBar->GetColor();
389
390         if (bgColor != __currentBgColor)
391         {
392                 delete __pReplacedSearchBarBitmap;
393                 __pReplacedSearchBarBitmap = null;
394
395                 __pReplacedSearchBarBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pSearchBarBitmap, Color::GetColor(COLOR_ID_MAGENTA), bgColor);
396                 r = GetLastResult();
397                 SysTryReturn(NID_UI_CTRL, (__pReplacedSearchBarBitmap != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
398                 __currentBgColor = bgColor;
399         }
400
401         isCustomBitmap = IS_CUSTOM_BITMAP(SEARCHBAR::BG_NORMAL);
402
403         pCanvas = __pSearchBar->GetCanvasN();
404         r = GetLastResult();
405         SysTryReturn(NID_UI_CTRL, (pCanvas != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
406
407         pCanvas->SetBackgroundColor(Color(0));
408         pCanvas->Clear();
409
410         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pReplacedSearchBarBitmap))
411         {
412                 pCanvas->DrawNinePatchedBitmap(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height), *__pReplacedSearchBarBitmap);
413         }
414         else
415         {
416                 pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height), *__pReplacedSearchBarBitmap);
417         }
418
419         if (!isCustomBitmap && __pSearchBarEffectBitmap != null)
420         {
421                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pSearchBarEffectBitmap))
422                 {
423                         pCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, bounds.width, bounds.height), *__pSearchBarEffectBitmap);
424                 }
425                 else
426                 {
427                         pCanvas->DrawBitmap(FloatRectangle(0, 0, bounds.width, bounds.height), *__pSearchBarEffectBitmap);
428                 }
429         }
430
431         delete pCanvas;
432         return r;
433 }
434
435 void
436 _SearchBarPresenter::SetCancelButtonVisible(bool visible)
437 {
438         if ((__pSearchBar->IsUsableCancelButton() == false) && visible)
439         {
440                 return;
441         }
442
443         if (__pCancelButton == null)
444         {
445                 return;
446         }
447
448         __pCancelButton->SetVisibleState(visible);
449
450         return;
451 }
452
453 void
454 _SearchBarPresenter::SetContainerVisible(bool visible)
455 {
456         if (__pContainer == null)
457         {
458                 return;
459         }
460
461         __pContainer->SetVisibleState(visible);
462
463         return;
464 }
465
466
467 result
468 _SearchBarPresenter::DrawIcon(void)
469 {
470         SysTryReturn(NID_UI_CTRL, __pEdit, E_SYSTEM, E_SYSTEM,
471                      "[E_SYSTEM] A system error has occurred. Failed to get edit instance.");
472         SysTryReturn(NID_UI_CTRL, __pIconElement, E_SYSTEM, E_SYSTEM,
473                      "[E_SYSTEM] A system error has occurred. Failed to get edit visual element.");
474
475         result r = E_SUCCESS;
476
477         Canvas* pIconCanvas = null;
478         FloatRectangle iconBounds;
479
480         bool isCustomBitmap = false;
481
482         isCustomBitmap = IS_CUSTOM_BITMAP(SEARCHBAR::ICON_NORMAL);
483
484         float iconMarginH = 0.0f;
485         float iconWidth = 0.0f;
486         float iconHeight = 0.0f;
487         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
488
489         GET_SHAPE_CONFIG(SEARCHBAR::ICON_HORIZONTAL_MARGIN, orientation, iconMarginH);
490         GET_SHAPE_CONFIG(SEARCHBAR::ICON_WIDTH, orientation, iconWidth);
491         GET_SHAPE_CONFIG(SEARCHBAR::ICON_HEIGHT, orientation, iconHeight);
492
493         iconBounds.x = iconMarginH;
494         iconBounds.y = (__pEdit->GetBoundsF().height - iconHeight) / 2.0f;
495         iconBounds.width = iconWidth;
496         iconBounds.height = iconHeight;
497
498         __pIconElement->SetBounds(FloatRectangle(iconBounds.x, iconBounds.y, iconBounds.width, iconBounds.height));
499
500         pIconCanvas = __pIconElement->GetCanvasN(FloatRectangle(0.0f, 0.0f, iconBounds.width, iconBounds.height));
501         SysTryReturnResult(NID_UI_CTRL, pIconCanvas != null, GetLastResult(), "Propagating.");
502
503         pIconCanvas->SetBackgroundColor(Color(0));
504         pIconCanvas->Clear();
505         if (__pSearchBar->IsEnabled())
506         {
507                 if (__pReplacedSearchFieldNormalBitmap != null)
508                 {
509                         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pReplacedSearchFieldNormalBitmap))
510                         {
511                                 pIconCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
512                         }
513                         else
514                         {
515                                 pIconCanvas->DrawBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
516                         }
517                 }
518         }
519         else
520         {
521                 if (__pReplacedSearchFieldDisabledBitmap != null)
522                 {
523                         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pReplacedSearchFieldDisabledBitmap))
524                         {
525                                 pIconCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
526                         }
527                         else
528                         {
529                                 pIconCanvas->DrawBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
530                         }
531                 }
532         }
533
534         if (!isCustomBitmap && __pSearchFieldEffectBitmap != null)
535         {
536                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pSearchFieldEffectBitmap))
537                 {
538                         pIconCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
539                 }
540                 else
541                 {
542                         pIconCanvas->DrawBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
543                 }
544         }
545
546         delete pIconCanvas;
547         pIconCanvas = null;
548
549         return r;
550 }
551
552
553 result
554 _SearchBarPresenter::ChangeMode(SearchBarMode mode)
555 {
556         result r = E_SUCCESS;
557
558         if (mode == SEARCH_BAR_MODE_NORMAL)
559         {
560                 __pSearchBar->SetFocusCallbackParameter(true);
561                 InitializeViewModeLayout();
562                 SetCancelButtonVisible(false);
563                 SetContainerVisible(false);
564
565                 if (__pEdit)
566                 {
567                         __pEdit->ClearText();
568                         __pEdit->SetBounds(__searchFieldBounds);
569                         if (__pEdit->IsFocused())
570                         {
571                                 __pEdit->HideKeypad();
572                         }
573                 }
574
575                 r = __pSearchBar->SendSearchBarEvent(_SEARCH_BAR_EVENT_MODE_CHANGE);
576                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
577                              "[E_SYSTEM] A system error has occurred. Failed to fire the searchbar event.");
578         }
579         else
580         {
581                 r = __pSearchBar->SendSearchBarEvent(_SEARCH_BAR_EVENT_MODE_CHANGE);
582                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
583                              "[E_SYSTEM] A system error has occurred. Failed to fire the searchbar event.");
584
585                 InitializeInputModeLayout();
586                 SetCancelButtonVisible(true);
587                 __pSearchBar->SetContentDimming();
588
589                 if (__pEdit)
590                 {
591                         float buttonWidth = 0.0f;
592                         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
593                         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
594
595                         float cancelButtonWidth = 0.0f;
596                         cancelButtonWidth = __pCancelButton->GetTextExtentSizeF() + __pCancelButton->GetRightTouchMarginF() + __pCancelButton->GetLeftTouchMarginF() + __pCancelButton->GetRightMarginF() + __pCancelButton->GetLeftMarginF();
597
598                         if (cancelButtonWidth <= buttonWidth)
599                         {
600                                 __pEdit->SetBounds(__searchFieldBounds);
601                         }
602                         __pEdit->ShowKeypad();
603                 }
604                 __pSearchBar->SetContentsArea();
605
606                 if (IsContentAreaVisible())
607                 {
608                         SetContainerVisible(true);
609                 }
610
611                 __pSearchBar->Invalidate();
612         }
613
614         return E_SUCCESS;
615 }
616
617 void
618 _SearchBarPresenter::InitializeInputModeLayout(void)
619 {
620         InitializeViewModeLayout();
621
622         if (__pSearchBar->IsUsableCancelButton() == false)
623         {
624                 return;
625         }
626
627         float horizontalMargin = 0.0f;
628         float buttonRightMargin = 0.0f;
629         float buttonLeftMargin = 0.0f;
630         float buttonWidth = 0.0f;
631         float searchFieldMinWidth = 0.0f;
632
633         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
634
635         GET_SHAPE_CONFIG(SEARCHBAR::HORIZONTAL_MARGIN, orientation, horizontalMargin);
636         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_LEFT_MARGIN, orientation, buttonLeftMargin);
637         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_RIGHT_MARGIN, orientation, buttonRightMargin);
638         GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_WIDTH, orientation, searchFieldMinWidth);
639         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
640
641         float buttonResizableSearchBarWidth = (horizontalMargin + searchFieldMinWidth +
642                                              buttonWidth + buttonLeftMargin + buttonRightMargin);
643
644         if (__pSearchBar->GetBoundsF().width < buttonResizableSearchBarWidth)
645         {
646                 __searchFieldBounds.width = searchFieldMinWidth;
647         }
648
649         else
650         {
651                 __searchFieldBounds.width = __pSearchBar->GetBoundsF().width - (buttonRightMargin +
652                                                                                buttonWidth + buttonLeftMargin + horizontalMargin);
653         }
654
655         __searchFieldBounds.width = (__pSearchBar->GetBoundsF().width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
656
657         float cancelButtonWidth = 0.0f;
658         cancelButtonWidth = __pCancelButton->GetTextExtentSizeF() + __pCancelButton->GetRightTouchMarginF() + __pCancelButton->GetLeftTouchMarginF() + __pCancelButton->GetRightMarginF() + __pCancelButton->GetLeftMarginF();
659
660         if (cancelButtonWidth > buttonWidth)
661         {
662                 __pSearchBar->RecalculateButtonBounds();
663         }
664
665         return;
666 }
667
668 void
669 _SearchBarPresenter::InitializeViewModeLayout(void)
670 {
671         float horizontalMargin = 0.0f;
672         float verticalMargin = 0.0f;
673         float searchBarMinWidthModeNormal = 0.0f;
674         float searchBarMinHeight = 0.0f;
675         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
676
677         GET_SHAPE_CONFIG(SEARCHBAR::HORIZONTAL_MARGIN, orientation, horizontalMargin);
678         GET_SHAPE_CONFIG(SEARCHBAR::VERTICAL_MARGIN, orientation, verticalMargin);
679         GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_WIDTH_NORMAL_MODE, orientation, searchBarMinWidthModeNormal);
680         GET_SHAPE_CONFIG(SEARCHBAR::MIN_HEIGHT, orientation, searchBarMinHeight);
681
682         float searchFieldMinHeight = searchBarMinHeight - (verticalMargin * 2.0f);
683
684         __searchFieldBounds.x = horizontalMargin;
685         if (__pSearchBar->GetBoundsF().height < searchBarMinHeight)
686         {
687                 verticalMargin = (__pSearchBar->GetBoundsF().height - searchFieldMinHeight)/2.0f;
688                 if (verticalMargin < 0.0f)
689                 {
690                         verticalMargin = 0.0f;
691                 }
692         }
693         __searchFieldBounds.y = verticalMargin;
694         __searchFieldBounds.width = __pSearchBar->GetBoundsF().width - (horizontalMargin * 2.0f);
695         __searchFieldBounds.height = __pSearchBar->GetBoundsF().height - (verticalMargin * 2.0f);
696
697         __searchFieldBounds.width = (__searchFieldBounds.width > searchBarMinWidthModeNormal) ? __searchFieldBounds.width : searchBarMinWidthModeNormal;
698         __searchFieldBounds.height = (__searchFieldBounds.height > searchFieldMinHeight) ? __searchFieldBounds.height : searchFieldMinHeight;
699
700         return;
701 }
702
703 void
704 _SearchBarPresenter::OnBoundsChanged(void)
705 {
706         _Control* pClippedGroupControl = __pSearchBar->GetClippedGroupControl();
707
708         if (pClippedGroupControl != null)
709         {
710                 pClippedGroupControl->SetBounds(FloatRectangle(FloatPoint(0.0f, 0.0f), __pSearchBar->GetSizeF()));
711         }
712
713         if (GetMode() == SEARCH_BAR_MODE_NORMAL)
714         {
715                 InitializeViewModeLayout();
716         }
717         else
718         {
719                 InitializeInputModeLayout();
720         }
721
722         result r = E_SUCCESS;
723         float cancelButtonWidth = 0.0f;
724
725         if (__pCancelButton != null)
726         {
727                 r = __pSearchBar->ResizeCancelButton();
728                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
729
730                 cancelButtonWidth = __pCancelButton->GetTextExtentSizeF() + __pCancelButton->GetRightTouchMarginF() + __pCancelButton->GetLeftTouchMarginF() + __pCancelButton->GetRightMarginF() + __pCancelButton->GetLeftMarginF();
731         }
732
733         r = __pEdit->SetBounds(__searchFieldBounds);
734         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
735
736         if (__pSearchBar->IsUsableCancelButton() && GetMode() == SEARCH_BAR_MODE_INPUT)
737         {
738                 float buttonWidth = 0.0f;
739                 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
740                 GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
741
742                 if (cancelButtonWidth > buttonWidth)
743                 {
744                         __pSearchBar->RecalculateButtonBounds();
745                 }
746         }
747
748         __pSearchBar->SetContentsArea();
749         __pSearchBar->Invalidate(true);
750
751         return;
752 }
753
754 Canvas*
755 _SearchBarPresenter::OnCanvasRequestedN(const FloatRectangle& bounds)
756 {
757         Canvas* pCanvas = null;
758
759         if (__lazyDecode == true)
760         {
761                 Bitmap* pBackgroundBitmap = __pSearchBar->GetBackgroundBitmap();
762                 SysTryReturn(NID_UI_CTRL, pBackgroundBitmap != null, null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the background bitmap.");
763
764                 //Decode the bitmap explicitly
765                 pBackgroundBitmap->IsNinePatchedBitmap();
766                 Draw();
767                 pCanvas = __pSearchBar->GetCanvasN();
768         }
769
770         return pCanvas;
771 }
772
773 void
774 _SearchBarPresenter::SetHeaderVisibleState(bool visible)
775 {
776         _Form* pForm = null;
777         _Control* pControlCore = __pEdit->GetParent();
778
779         while (true)
780         {
781                 if (pControlCore == null)
782                 {
783                         SysLog(NID_UI_CTRL,"[E_SYSTEM] The parent form is null.");
784
785                         return;
786                 }
787
788                 pForm = dynamic_cast<_Form*>(pControlCore);
789                 if (pForm != null)
790                 {
791                         break;
792                 }
793
794                 pControlCore = pControlCore->GetParent();
795         }
796
797         _Toolbar* pHeader = pForm->GetHeader();
798         if (pHeader == null)
799         {
800                 return;
801         }
802
803         pHeader->SetVisibleState(visible);
804
805         return;
806 }
807
808 }}} // Tizen::Ui::Controls