Tizen 2.1 base
[framework/osp/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 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 /**
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
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui::Animations;
37 using namespace Tizen::Base;
38
39 namespace Tizen { namespace Ui { namespace Controls {
40
41 _SearchBarPresenter::_SearchBarPresenter(void)
42         : __pSearchBar(null)
43         , __pSearchBarModel(null)
44         , __pCancelButton(null)
45         , __pEdit(null)
46         , __pContainer(null)
47         , __searchFieldBounds(Rectangle())
48         , __pIconElement(null)
49         , __pReplacedSearchFieldNormalBitmap(null)
50         , __pReplacedSearchFieldDisabledBitmap(null)
51         , __pSearchFieldEffectBitmap(null)
52 {
53 }
54
55 _SearchBarPresenter::~_SearchBarPresenter(void)
56 {
57         delete __pSearchBarModel;
58         __pSearchBarModel = null;
59
60         if (__pIconElement)
61         {
62                 __pIconElement->Destroy();
63                 __pIconElement = null;
64         }
65
66         if (__pReplacedSearchFieldNormalBitmap)
67         {
68                 delete __pReplacedSearchFieldNormalBitmap;
69                 __pReplacedSearchFieldNormalBitmap = null;
70         }
71
72         if (__pReplacedSearchFieldDisabledBitmap)
73         {
74                 delete __pReplacedSearchFieldDisabledBitmap;
75                 __pReplacedSearchFieldDisabledBitmap = null;
76         }
77
78         if (__pSearchFieldEffectBitmap)
79         {
80                 delete __pSearchFieldEffectBitmap;
81                 __pSearchFieldEffectBitmap = null;
82         }
83 }
84
85 result
86 _SearchBarPresenter::Construct(const _SearchBar& searchBar)
87 {
88         result r = E_SUCCESS;
89         _VisualElement* pSearchBarElement = null;
90
91         __pSearchBar = const_cast <_SearchBar*>(&searchBar);
92         SysTryReturn(NID_UI_CTRL, __pSearchBar, E_SYSTEM, E_SYSTEM,
93                      "[E_SYSTEM] A system error has occurred. The searchbar instance is null.");
94
95         __pCancelButton = __pSearchBar->GetSearchBarButton();
96         SysTryReturn(NID_UI_CTRL, __pCancelButton, E_SYSTEM, E_SYSTEM,
97                      "[E_SYSTEM] A system error has occurred. The cancel button instance is null.");
98
99         __pEdit = __pSearchBar->GetSearchField();
100         SysTryReturn(NID_UI_CTRL, __pEdit, E_SYSTEM, E_SYSTEM,
101                      "[E_SYSTEM] A system error has occurred. The edit instance is null.");
102
103         __pContainer = __pSearchBar->GetSearchBarContainer();
104         SysTryReturn(NID_UI_CTRL, __pContainer, E_SYSTEM, E_SYSTEM,
105                      " [E_SYSTEM] A system error has occurred. Failed to get the content.");
106
107         r = LoadSearchFieldIcon();
108         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
109
110         _VisualElement* pEditElement = __pEdit->GetVisualElement();
111         SysTryReturn(NID_UI_CTRL, pEditElement, E_SYSTEM, E_SYSTEM,
112                      "[E_SYSTEM] A system error has occurred. Failed to get edit visual element.");
113
114         __pIconElement = new (std::nothrow) _VisualElement();
115         SysTryCatch(NID_UI_CTRL, __pIconElement, , E_OUT_OF_MEMORY,
116                     "[E_OUT_OF_MEMORY] Memory allocation failed.");
117
118         r = __pIconElement->Construct();
119         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
120
121         __pIconElement->SetShowState(true);
122
123         r = __pIconElement->SetSurfaceOpaque(false);
124         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
125
126         __pIconElement->SetImplicitAnimationEnabled(false);
127
128         r = pEditElement->AttachChild(*__pIconElement);
129         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
130
131         pSearchBarElement = __pSearchBar->GetVisualElement();
132         SysTryCatch(NID_UI_CTRL, pSearchBarElement, , E_SYSTEM,
133                     "[E_SYSTEM] A system error has occurred. Failed to get searchbar's visual element.");
134
135         return r;
136
137 CATCH:
138         if (__pIconElement)
139         {
140                 __pIconElement->Destroy();
141                 __pIconElement = null;
142         }
143
144         return r;
145 }
146
147 result
148 _SearchBarPresenter::LoadSearchFieldIcon()
149 {
150         result r = E_SUCCESS;
151         Color searchFieldIconColor;
152         Bitmap* pSearchFieldBitmap;
153
154
155         r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pSearchFieldBitmap);
156         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
157
158         r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pSearchFieldEffectBitmap);
159         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r,"[%s] Propagating.", GetErrorMessage(r));
160
161         GET_COLOR_CONFIG(SEARCHBAR::ICON_COLOR_NORMAL, searchFieldIconColor);
162
163         __pReplacedSearchFieldNormalBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSearchFieldBitmap, Color::GetColor(COLOR_ID_MAGENTA), searchFieldIconColor);
164         SysTryCatch(NID_UI_CTRL, __pReplacedSearchFieldNormalBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
165                 GetErrorMessage(GetLastResult()));
166
167         GET_COLOR_CONFIG(SEARCHBAR::ICON_COLOR_DISABLED, searchFieldIconColor);
168
169         __pReplacedSearchFieldDisabledBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSearchFieldBitmap, Color::GetColor(COLOR_ID_MAGENTA), searchFieldIconColor);
170         SysTryCatch(NID_UI_CTRL, __pReplacedSearchFieldDisabledBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
171                 GetErrorMessage(GetLastResult()));
172
173         delete pSearchFieldBitmap;
174         pSearchFieldBitmap = null;
175
176         return r;
177
178 CATCH:
179         delete pSearchFieldBitmap;
180         pSearchFieldBitmap = null;
181
182         delete __pSearchFieldEffectBitmap;
183         __pSearchFieldEffectBitmap = null;
184
185         delete __pReplacedSearchFieldNormalBitmap;
186         __pReplacedSearchFieldNormalBitmap = null;
187
188         return r;
189 }
190
191 result
192 _SearchBarPresenter::Install(void)
193 {
194         result r = E_SUCCESS;
195
196         _SearchBarModel* pModel = new (std::nothrow) _SearchBarModel;
197         SysTryReturn(NID_UI_CTRL, pModel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
198
199         r = pModel->Construct();
200         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM,
201                     "[E_SYSTEM] A system error has occurred. The searchbar model construction failed.");
202
203         __pSearchBarModel = pModel;
204
205         return E_SUCCESS;
206
207 CATCH:
208         delete pModel;
209         return E_SYSTEM;
210 }
211
212 result
213 _SearchBarPresenter::UpdateContentArea(bool invalidate)
214 {
215         SearchBarMode mode = __pSearchBarModel->GetMode();
216         SysTryReturn(NID_UI_CTRL, mode == SEARCH_BAR_MODE_INPUT, E_INVALID_OPERATION, E_INVALID_OPERATION,
217                      "[E_INVALID_OPERATION] The searchbar mode cannot be updated.");
218
219         __pSearchBar->Invalidate(invalidate);
220
221         return E_SUCCESS;
222 }
223
224 result
225 _SearchBarPresenter::SetContentAreaVisible(bool visible)
226 {
227         return __pSearchBarModel->SetContentAreaVisible(visible);
228 }
229
230 bool
231 _SearchBarPresenter::IsContentAreaVisible(void) const
232 {
233         return __pSearchBarModel->IsContentAreaVisible();
234 }
235
236 SearchBarMode
237 _SearchBarPresenter::GetMode(void) const
238 {
239         return __pSearchBarModel->GetMode();
240 }
241
242 bool
243 _SearchBarPresenter::IsModeLocked(void) const
244 {
245         return __pSearchBarModel->IsModeLocked();
246 }
247
248 result
249 _SearchBarPresenter::SetMode(SearchBarMode mode)
250 {
251         SysTryReturn(NID_UI_CTRL, IsModeLocked() == false, E_INVALID_OPERATION, E_INVALID_OPERATION,
252                      "[E_INVALID_OPERATION] The searchbar mode is locked.");
253
254         if (GetMode() == mode)
255         {
256                 return E_SUCCESS;
257         }
258
259         __pSearchBarModel->SetMode(mode);
260
261         result r = ChangeMode(mode);
262         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
263                      "[E_SYSTEM] A system error has occurred. Failed to change the mode of searchbar.");
264
265         return r;
266 }
267
268 result
269 _SearchBarPresenter::SetModeLocked(bool modeLocked)
270 {
271         return __pSearchBarModel->SetModeLocked(modeLocked);
272 }
273
274 result
275 _SearchBarPresenter::Draw(void)
276 {
277         result r = E_SUCCESS;
278
279         Color bgColor = __pSearchBar->GetColor();
280
281         Rectangle bounds(0, 0, __pSearchBar->GetBounds().width, __pSearchBar->GetBounds().height);
282         __pSearchBar->SetBackgroundColor(__pSearchBar->GetColor());
283
284         Bitmap* pBackgroundBitmap = __pSearchBar->GetBackgroundBitmap();
285
286         if (pBackgroundBitmap)
287         {
288                 Canvas* pCanvas = __pSearchBar->GetCanvasN();
289                 r = GetLastResult();
290                 SysTryReturn(NID_UI_CTRL, (pCanvas != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
291
292                 if (pBackgroundBitmap->IsNinePatchedBitmap())
293                 {
294                         pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
295                 }
296                 else
297                 {
298                         pCanvas->DrawBitmap(Point(0, 0), *pBackgroundBitmap);
299                 }
300
301                 delete pCanvas;
302         }
303
304         r = DrawIcon();
305         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
306
307         return r;
308 }
309
310 void
311 _SearchBarPresenter::SetCancelButtonVisible(bool visible)
312 {
313         if ((__pSearchBar->IsUsableCancelButton() == false) && visible)
314         {
315                 return;
316         }
317
318         if (__pCancelButton == null)
319         {
320                 return;
321         }
322
323         __pCancelButton->SetVisibleState(visible);
324
325         return;
326 }
327
328 void
329 _SearchBarPresenter::SetContainerVisible(bool visible)
330 {
331         if (__pContainer == null)
332         {
333                 return;
334         }
335
336         __pContainer->SetVisibleState(visible);
337
338         return;
339 }
340
341
342 result
343 _SearchBarPresenter::DrawIcon(void)
344 {
345         SysTryReturn(NID_UI_CTRL, __pEdit, E_SYSTEM, E_SYSTEM,
346                      "[E_SYSTEM] A system error has occurred. Failed to get edit instance.");
347         SysTryReturn(NID_UI_CTRL, __pIconElement, E_SYSTEM, E_SYSTEM,
348                      "[E_SYSTEM] A system error has occurred. Failed to get edit visual element.");
349
350         result r = E_SUCCESS;
351
352         Canvas* pIconCanvas = null;
353         Rectangle iconBounds;
354
355         int iconMarginH = 0;
356         int iconWidth = 0;
357         int iconHeight = 0;
358         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
359
360         GET_SHAPE_CONFIG(SEARCHBAR::ICON_HORIZONTAL_MARGIN, orientation, iconMarginH);
361         GET_SHAPE_CONFIG(SEARCHBAR::ICON_WIDTH, orientation, iconWidth);
362         GET_SHAPE_CONFIG(SEARCHBAR::ICON_HEIGHT, orientation, iconHeight);
363
364         iconBounds.x = iconMarginH;
365         iconBounds.y = (__pEdit->GetBounds().height - iconWidth) / 2;
366         iconBounds.width = iconWidth;
367         iconBounds.height = iconHeight;
368
369         __pIconElement->SetBounds(FloatRectangle(iconBounds.x, iconBounds.y, iconBounds.width, iconBounds.height));
370
371         pIconCanvas = __pIconElement->GetCanvasN(Rectangle(0, 0, iconBounds.width, iconBounds.height));
372         SysTryReturnResult(NID_UI_CTRL, pIconCanvas != null, GetLastResult(), "Propagating.");
373
374         pIconCanvas->SetBackgroundColor(Color(0));
375         pIconCanvas->Clear();
376         if(__pSearchBar->IsEnabled())
377         {
378                 if (__pReplacedSearchFieldNormalBitmap != null)
379                 {
380                         if (__pReplacedSearchFieldNormalBitmap->IsNinePatchedBitmap())
381                         {
382                                 pIconCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
383                         }
384                         else
385                         {
386                                 pIconCanvas->DrawBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
387                         }
388                 }
389         }
390         else
391         {
392                 if (__pReplacedSearchFieldDisabledBitmap != null)
393                 {
394                         if (__pReplacedSearchFieldDisabledBitmap->IsNinePatchedBitmap())
395                         {
396                                 pIconCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
397                         }
398                         else
399                         {
400                                 pIconCanvas->DrawBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
401                         }
402                 }
403         }
404
405         if (__pSearchFieldEffectBitmap != null)
406         {
407                 if (__pSearchFieldEffectBitmap->IsNinePatchedBitmap())
408                 {
409                         pIconCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
410                 }
411                 else
412                 {
413                         pIconCanvas->DrawBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
414                 }
415         }
416
417         delete pIconCanvas;
418         pIconCanvas = null;
419
420         return r;
421 }
422
423
424 result
425 _SearchBarPresenter::ChangeMode(SearchBarMode mode)
426 {
427         result r = E_SUCCESS;
428
429         if (mode == SEARCH_BAR_MODE_NORMAL)
430         {
431                 InitializeViewModeLayout();
432                 SetCancelButtonVisible(false);
433                 SetContainerVisible(false);
434
435                 if (__pEdit)
436                 {
437                         __pEdit->ClearText();
438                         __pEdit->SetCursorDisabled(true);
439                         __pEdit->SetBounds(__searchFieldBounds);
440                         __pEdit->HideKeypad();
441                 }
442
443                 r = __pSearchBar->SendSearchBarEvent(_SEARCH_BAR_EVENT_MODE_CHANGE);
444                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
445                              "[E_SYSTEM] A system error has occurred. Failed to fire the searchbar event.");
446         }
447         else
448         {
449                 r = __pSearchBar->SendSearchBarEvent(_SEARCH_BAR_EVENT_MODE_CHANGE);
450                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
451                              "[E_SYSTEM] A system error has occurred. Failed to fire the searchbar event.");
452
453                 InitializeInputModeLayout();
454                 SetCancelButtonVisible(true);
455                 __pSearchBar->SetContentDimming();
456                 __pSearchBar->SetContentsArea();
457                 SetContainerVisible(true);
458
459                 __pSearchBar->Invalidate();
460
461                 if (__pEdit)
462                 {
463                         __pEdit->SetCursorDisabled(false);
464                         __pEdit->SetBounds(__searchFieldBounds);
465                         __pEdit->ShowKeypad();
466                 }
467         }
468
469         return E_SUCCESS;
470 }
471
472 void
473 _SearchBarPresenter::InitializeInputModeLayout(void)
474 {
475         InitializeViewModeLayout();
476
477         if (__pSearchBar->IsUsableCancelButton() == false)
478         {
479                 return;
480         }
481
482         int horizontalMargin = 0;
483         int buttonRightMargin = 0;
484         int buttonLeftMargin = 0;
485         int buttonWidth = 0;
486         int searchFieldMinWidth = 0;
487         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
488
489         GET_SHAPE_CONFIG(SEARCHBAR::HORIZONTAL_MARGIN, orientation, horizontalMargin);
490         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_LEFT_MARGIN, orientation, buttonLeftMargin);
491         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_RIGHT_MARGIN, orientation, buttonRightMargin);
492         GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_WIDTH, orientation, searchFieldMinWidth);
493         GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
494         int buttonResizableSearchBarWidth = (horizontalMargin + searchFieldMinWidth +
495                                              buttonWidth + buttonLeftMargin + buttonRightMargin);
496
497         if(__pSearchBar->GetBounds().width < buttonResizableSearchBarWidth)
498         {
499                 __searchFieldBounds.width = searchFieldMinWidth;
500         }
501
502         else
503         {
504                 __searchFieldBounds.width = __pSearchBar->GetBounds().width - (buttonRightMargin +
505                                                                                buttonWidth + buttonLeftMargin + horizontalMargin);
506         }
507
508         __searchFieldBounds.width = (__pSearchBar->GetBounds().width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
509
510         return;
511 }
512
513 void
514 _SearchBarPresenter::InitializeViewModeLayout(void)
515 {
516         int horizontalMargin = 0;
517         int verticalMargin = 0;
518         int searchFieldMinWidth = 0;
519         int searchFieldMinHeight = 0;
520         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
521
522         GET_SHAPE_CONFIG(SEARCHBAR::HORIZONTAL_MARGIN, orientation, horizontalMargin);
523         GET_SHAPE_CONFIG(SEARCHBAR::VERTICAL_MARGIN, orientation, verticalMargin);
524         GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_WIDTH, orientation, searchFieldMinWidth);
525         GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_HEIGHT, orientation, searchFieldMinHeight);
526
527         __searchFieldBounds.x = horizontalMargin;
528         __searchFieldBounds.y = verticalMargin;
529         __searchFieldBounds.width = __pSearchBar->GetBounds().width - (horizontalMargin * 2);
530         __searchFieldBounds.height = __pSearchBar->GetBounds().height - (verticalMargin * 2);
531
532         __searchFieldBounds.width = (__searchFieldBounds.width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
533         __searchFieldBounds.height = (__searchFieldBounds.height > searchFieldMinHeight) ? __searchFieldBounds.height : searchFieldMinHeight;
534
535         return;
536 }
537
538 void
539 _SearchBarPresenter::OnBoundsChanged(void)
540 {
541         _Control* pClippedGroupControl = __pSearchBar->GetClippedGroupControl();
542
543         if(pClippedGroupControl != null)
544         {
545                 pClippedGroupControl->SetBounds(Rectangle(Point(0, 0), __pSearchBar->GetSize()));
546         }
547
548         if (GetMode() == SEARCH_BAR_MODE_NORMAL)
549         {
550                 InitializeViewModeLayout();
551         }
552         else
553         {
554                 InitializeInputModeLayout();
555         }
556
557         result r = E_SUCCESS;
558
559         if (__pCancelButton != null)
560         {
561                 r = __pSearchBar->ResizeCancelButton();
562                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
563         }
564
565         r = __pEdit->SetBounds(__searchFieldBounds);
566         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
567
568         __pSearchBar->SetContentsArea();
569
570         return;
571 }
572
573 void
574 _SearchBarPresenter::SetHeaderVisibleState(bool visible)
575 {
576         _Form* pForm = null;
577         _Control* pControlCore = __pEdit->GetParent();
578
579         while (true)
580         {
581                 if (pControlCore == null)
582                 {
583                         SysLog(NID_UI_CTRL,"[E_SYSTEM] The parent form is null.");
584
585                         return;
586                 }
587
588                 pForm = dynamic_cast<_Form*>(pControlCore);
589                 if (pForm != null)
590                 {
591                         break;
592                 }
593
594                 pControlCore = pControlCore->GetParent();
595         }
596
597         _Toolbar* pHeader = pForm->GetHeader();
598         if (pHeader == null)
599         {
600                 return;
601         }
602
603         pHeader->SetVisibleState(visible);
604
605         return;
606 }
607
608 }}} // Tizen::Ui::Controls