2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 * @file FUiCtrl_SearchBarPresenter.cpp
20 * @brief This is the implementation file for the _SearchBarPresenter class.
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"
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui::Animations;
37 using namespace Tizen::Base;
39 namespace Tizen { namespace Ui { namespace Controls {
41 _SearchBarPresenter::_SearchBarPresenter(void)
43 , __pSearchBarModel(null)
44 , __pCancelButton(null)
47 , __searchFieldBounds(Rectangle())
48 , __pIconElement(null)
49 , __pReplacedSearchFieldNormalBitmap(null)
50 , __pReplacedSearchFieldDisabledBitmap(null)
51 , __pSearchFieldEffectBitmap(null)
55 _SearchBarPresenter::~_SearchBarPresenter(void)
57 delete __pSearchBarModel;
58 __pSearchBarModel = null;
62 __pIconElement->Destroy();
63 __pIconElement = null;
66 if (__pReplacedSearchFieldNormalBitmap)
68 delete __pReplacedSearchFieldNormalBitmap;
69 __pReplacedSearchFieldNormalBitmap = null;
72 if (__pReplacedSearchFieldDisabledBitmap)
74 delete __pReplacedSearchFieldDisabledBitmap;
75 __pReplacedSearchFieldDisabledBitmap = null;
78 if (__pSearchFieldEffectBitmap)
80 delete __pSearchFieldEffectBitmap;
81 __pSearchFieldEffectBitmap = null;
86 _SearchBarPresenter::Construct(const _SearchBar& searchBar)
89 _VisualElement* pSearchBarElement = null;
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.");
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.");
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.");
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.");
107 r = LoadSearchFieldIcon();
108 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
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.");
114 __pIconElement = new (std::nothrow) _VisualElement();
115 SysTryCatch(NID_UI_CTRL, __pIconElement, , E_OUT_OF_MEMORY,
116 "[E_OUT_OF_MEMORY] Memory allocation failed.");
118 r = __pIconElement->Construct();
119 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
121 __pIconElement->SetShowState(true);
123 r = __pIconElement->SetSurfaceOpaque(false);
124 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
126 __pIconElement->SetImplicitAnimationEnabled(false);
128 r = pEditElement->AttachChild(*__pIconElement);
129 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
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.");
140 __pIconElement->Destroy();
141 __pIconElement = null;
148 _SearchBarPresenter::LoadSearchFieldIcon()
150 result r = E_SUCCESS;
151 Color searchFieldIconColor;
152 Bitmap* pSearchFieldBitmap;
155 r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pSearchFieldBitmap);
156 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
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));
161 GET_COLOR_CONFIG(SEARCHBAR::ICON_COLOR_NORMAL, searchFieldIconColor);
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()));
167 GET_COLOR_CONFIG(SEARCHBAR::ICON_COLOR_DISABLED, searchFieldIconColor);
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()));
173 delete pSearchFieldBitmap;
174 pSearchFieldBitmap = null;
179 delete pSearchFieldBitmap;
180 pSearchFieldBitmap = null;
182 delete __pSearchFieldEffectBitmap;
183 __pSearchFieldEffectBitmap = null;
185 delete __pReplacedSearchFieldNormalBitmap;
186 __pReplacedSearchFieldNormalBitmap = null;
192 _SearchBarPresenter::Install(void)
194 result r = E_SUCCESS;
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.");
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.");
203 __pSearchBarModel = pModel;
213 _SearchBarPresenter::UpdateContentArea(bool invalidate)
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.");
219 __pSearchBar->Invalidate(invalidate);
225 _SearchBarPresenter::SetContentAreaVisible(bool visible)
227 return __pSearchBarModel->SetContentAreaVisible(visible);
231 _SearchBarPresenter::IsContentAreaVisible(void) const
233 return __pSearchBarModel->IsContentAreaVisible();
237 _SearchBarPresenter::GetMode(void) const
239 return __pSearchBarModel->GetMode();
243 _SearchBarPresenter::IsModeLocked(void) const
245 return __pSearchBarModel->IsModeLocked();
249 _SearchBarPresenter::SetMode(SearchBarMode mode)
251 SysTryReturn(NID_UI_CTRL, IsModeLocked() == false, E_INVALID_OPERATION, E_INVALID_OPERATION,
252 "[E_INVALID_OPERATION] The searchbar mode is locked.");
254 if (GetMode() == mode)
259 __pSearchBarModel->SetMode(mode);
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.");
269 _SearchBarPresenter::SetModeLocked(bool modeLocked)
271 return __pSearchBarModel->SetModeLocked(modeLocked);
275 _SearchBarPresenter::Draw(void)
277 result r = E_SUCCESS;
279 Color bgColor = __pSearchBar->GetColor();
281 Rectangle bounds(0, 0, __pSearchBar->GetBounds().width, __pSearchBar->GetBounds().height);
282 __pSearchBar->SetBackgroundColor(__pSearchBar->GetColor());
284 Bitmap* pBackgroundBitmap = __pSearchBar->GetBackgroundBitmap();
286 if (pBackgroundBitmap)
288 Canvas* pCanvas = __pSearchBar->GetCanvasN();
290 SysTryReturn(NID_UI_CTRL, (pCanvas != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
292 if (pBackgroundBitmap->IsNinePatchedBitmap())
294 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
298 pCanvas->DrawBitmap(Point(0, 0), *pBackgroundBitmap);
305 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
311 _SearchBarPresenter::SetCancelButtonVisible(bool visible)
313 if ((__pSearchBar->IsUsableCancelButton() == false) && visible)
318 if (__pCancelButton == null)
323 __pCancelButton->SetVisibleState(visible);
329 _SearchBarPresenter::SetContainerVisible(bool visible)
331 if (__pContainer == null)
336 __pContainer->SetVisibleState(visible);
343 _SearchBarPresenter::DrawIcon(void)
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.");
350 result r = E_SUCCESS;
352 Canvas* pIconCanvas = null;
353 Rectangle iconBounds;
358 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
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);
364 iconBounds.x = iconMarginH;
365 iconBounds.y = (__pEdit->GetBounds().height - iconWidth) / 2;
366 iconBounds.width = iconWidth;
367 iconBounds.height = iconHeight;
369 __pIconElement->SetBounds(FloatRectangle(iconBounds.x, iconBounds.y, iconBounds.width, iconBounds.height));
371 pIconCanvas = __pIconElement->GetCanvasN(Rectangle(0, 0, iconBounds.width, iconBounds.height));
372 SysTryReturnResult(NID_UI_CTRL, pIconCanvas != null, GetLastResult(), "Propagating.");
374 pIconCanvas->SetBackgroundColor(Color(0));
375 pIconCanvas->Clear();
376 if(__pSearchBar->IsEnabled())
378 if (__pReplacedSearchFieldNormalBitmap != null)
380 if (__pReplacedSearchFieldNormalBitmap->IsNinePatchedBitmap())
382 pIconCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
386 pIconCanvas->DrawBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
392 if (__pReplacedSearchFieldDisabledBitmap != null)
394 if (__pReplacedSearchFieldDisabledBitmap->IsNinePatchedBitmap())
396 pIconCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
400 pIconCanvas->DrawBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
405 if (__pSearchFieldEffectBitmap != null)
407 if (__pSearchFieldEffectBitmap->IsNinePatchedBitmap())
409 pIconCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
413 pIconCanvas->DrawBitmap(Rectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
425 _SearchBarPresenter::ChangeMode(SearchBarMode mode)
427 result r = E_SUCCESS;
429 if (mode == SEARCH_BAR_MODE_NORMAL)
431 InitializeViewModeLayout();
432 SetCancelButtonVisible(false);
433 SetContainerVisible(false);
437 __pEdit->ClearText();
438 __pEdit->SetCursorDisabled(true);
439 __pEdit->SetBounds(__searchFieldBounds);
440 __pEdit->HideKeypad();
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.");
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.");
453 InitializeInputModeLayout();
454 SetCancelButtonVisible(true);
455 __pSearchBar->SetContentDimming();
456 __pSearchBar->SetContentsArea();
457 SetContainerVisible(true);
459 __pSearchBar->Invalidate();
463 __pEdit->SetCursorDisabled(false);
464 __pEdit->SetBounds(__searchFieldBounds);
465 __pEdit->ShowKeypad();
473 _SearchBarPresenter::InitializeInputModeLayout(void)
475 InitializeViewModeLayout();
477 if (__pSearchBar->IsUsableCancelButton() == false)
482 int horizontalMargin = 0;
483 int buttonRightMargin = 0;
484 int buttonLeftMargin = 0;
486 int searchFieldMinWidth = 0;
487 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
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);
497 if(__pSearchBar->GetBounds().width < buttonResizableSearchBarWidth)
499 __searchFieldBounds.width = searchFieldMinWidth;
504 __searchFieldBounds.width = __pSearchBar->GetBounds().width - (buttonRightMargin +
505 buttonWidth + buttonLeftMargin + horizontalMargin);
508 __searchFieldBounds.width = (__pSearchBar->GetBounds().width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
514 _SearchBarPresenter::InitializeViewModeLayout(void)
516 int horizontalMargin = 0;
517 int verticalMargin = 0;
518 int searchFieldMinWidth = 0;
519 int searchFieldMinHeight = 0;
520 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
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);
527 __searchFieldBounds.x = horizontalMargin;
528 __searchFieldBounds.y = verticalMargin;
529 __searchFieldBounds.width = __pSearchBar->GetBounds().width - (horizontalMargin * 2);
530 __searchFieldBounds.height = __pSearchBar->GetBounds().height - (verticalMargin * 2);
532 __searchFieldBounds.width = (__searchFieldBounds.width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
533 __searchFieldBounds.height = (__searchFieldBounds.height > searchFieldMinHeight) ? __searchFieldBounds.height : searchFieldMinHeight;
539 _SearchBarPresenter::OnBoundsChanged(void)
541 _Control* pClippedGroupControl = __pSearchBar->GetClippedGroupControl();
543 if(pClippedGroupControl != null)
545 pClippedGroupControl->SetBounds(Rectangle(Point(0, 0), __pSearchBar->GetSize()));
548 if (GetMode() == SEARCH_BAR_MODE_NORMAL)
550 InitializeViewModeLayout();
554 InitializeInputModeLayout();
557 result r = E_SUCCESS;
559 if (__pCancelButton != null)
561 r = __pSearchBar->ResizeCancelButton();
562 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
565 r = __pEdit->SetBounds(__searchFieldBounds);
566 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
568 __pSearchBar->SetContentsArea();
574 _SearchBarPresenter::SetHeaderVisibleState(bool visible)
577 _Control* pControlCore = __pEdit->GetParent();
581 if (pControlCore == null)
583 SysLog(NID_UI_CTRL,"[E_SYSTEM] The parent form is null.");
588 pForm = dynamic_cast<_Form*>(pControlCore);
594 pControlCore = pControlCore->GetParent();
597 _Toolbar* pHeader = pForm->GetHeader();
603 pHeader->SetVisibleState(visible);
608 }}} // Tizen::Ui::Controls