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"
34 #include "FUi_CoordinateSystemUtils.h"
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Ui::Animations;
38 using namespace Tizen::Base;
39 using namespace Tizen::Ui;
41 namespace Tizen { namespace Ui { namespace Controls {
43 _SearchBarPresenter::_SearchBarPresenter(void)
45 , __pSearchBarModel(null)
46 , __pCancelButton(null)
49 , __searchFieldBounds(FloatRectangle())
50 , __pIconElement(null)
51 , __pReplacedSearchFieldNormalBitmap(null)
52 , __pReplacedSearchFieldDisabledBitmap(null)
53 , __pSearchFieldEffectBitmap(null)
57 _SearchBarPresenter::~_SearchBarPresenter(void)
59 delete __pSearchBarModel;
60 __pSearchBarModel = null;
64 __pIconElement->Destroy();
65 __pIconElement = null;
68 if (__pReplacedSearchFieldNormalBitmap)
70 delete __pReplacedSearchFieldNormalBitmap;
71 __pReplacedSearchFieldNormalBitmap = null;
74 if (__pReplacedSearchFieldDisabledBitmap)
76 delete __pReplacedSearchFieldDisabledBitmap;
77 __pReplacedSearchFieldDisabledBitmap = null;
80 if (__pSearchFieldEffectBitmap)
82 delete __pSearchFieldEffectBitmap;
83 __pSearchFieldEffectBitmap = null;
88 _SearchBarPresenter::Construct(const _SearchBar& searchBar)
91 _VisualElement* pSearchBarElement = null;
93 __pSearchBar = const_cast <_SearchBar*>(&searchBar);
94 SysTryReturn(NID_UI_CTRL, __pSearchBar, E_SYSTEM, E_SYSTEM,
95 "[E_SYSTEM] A system error has occurred. The searchbar instance is null.");
97 __pCancelButton = __pSearchBar->GetSearchBarButton();
98 SysTryReturn(NID_UI_CTRL, __pCancelButton, E_SYSTEM, E_SYSTEM,
99 "[E_SYSTEM] A system error has occurred. The cancel button instance is null.");
101 __pEdit = __pSearchBar->GetSearchField();
102 SysTryReturn(NID_UI_CTRL, __pEdit, E_SYSTEM, E_SYSTEM,
103 "[E_SYSTEM] A system error has occurred. The edit instance is null.");
105 __pContainer = __pSearchBar->GetSearchBarContainer();
106 SysTryReturn(NID_UI_CTRL, __pContainer, E_SYSTEM, E_SYSTEM,
107 " [E_SYSTEM] A system error has occurred. Failed to get the content.");
109 r = LoadSearchFieldIcon();
110 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
112 _VisualElement* pEditElement = __pEdit->GetVisualElement();
113 SysTryReturn(NID_UI_CTRL, pEditElement, E_SYSTEM, E_SYSTEM,
114 "[E_SYSTEM] A system error has occurred. Failed to get edit visual element.");
116 __pIconElement = new (std::nothrow) _VisualElement();
117 SysTryCatch(NID_UI_CTRL, __pIconElement, , E_OUT_OF_MEMORY,
118 "[E_OUT_OF_MEMORY] Memory allocation failed.");
120 r = __pIconElement->Construct();
121 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
123 __pIconElement->SetShowState(true);
125 r = __pIconElement->SetSurfaceOpaque(false);
126 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
128 __pIconElement->SetImplicitAnimationEnabled(false);
130 r = pEditElement->AttachChild(*__pIconElement);
131 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
133 pSearchBarElement = __pSearchBar->GetVisualElement();
134 SysTryCatch(NID_UI_CTRL, pSearchBarElement, , E_SYSTEM,
135 "[E_SYSTEM] A system error has occurred. Failed to get searchbar's visual element.");
142 __pIconElement->Destroy();
143 __pIconElement = null;
150 _SearchBarPresenter::LoadSearchFieldIcon()
152 result r = E_SUCCESS;
153 Color searchFieldIconColor;
154 Bitmap* pSearchFieldBitmap;
157 r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pSearchFieldBitmap);
158 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
160 r = GET_BITMAP_CONFIG_N(SEARCHBAR::ICON_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pSearchFieldEffectBitmap);
161 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r,"[%s] Propagating.", GetErrorMessage(r));
163 GET_COLOR_CONFIG(SEARCHBAR::ICON_BG_NORMAL, searchFieldIconColor);
165 __pReplacedSearchFieldNormalBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSearchFieldBitmap, Color::GetColor(COLOR_ID_MAGENTA), searchFieldIconColor);
166 SysTryCatch(NID_UI_CTRL, __pReplacedSearchFieldNormalBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
167 GetErrorMessage(GetLastResult()));
169 GET_COLOR_CONFIG(SEARCHBAR::ICON_BG_DISABLED, searchFieldIconColor);
171 __pReplacedSearchFieldDisabledBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pSearchFieldBitmap, Color::GetColor(COLOR_ID_MAGENTA), searchFieldIconColor);
172 SysTryCatch(NID_UI_CTRL, __pReplacedSearchFieldDisabledBitmap != null, r = GetLastResult(), GetLastResult(),"[%s] Propagating.",
173 GetErrorMessage(GetLastResult()));
175 delete pSearchFieldBitmap;
176 pSearchFieldBitmap = null;
181 delete pSearchFieldBitmap;
182 pSearchFieldBitmap = null;
184 delete __pSearchFieldEffectBitmap;
185 __pSearchFieldEffectBitmap = null;
187 delete __pReplacedSearchFieldNormalBitmap;
188 __pReplacedSearchFieldNormalBitmap = null;
194 _SearchBarPresenter::Install(void)
196 result r = E_SUCCESS;
198 _SearchBarModel* pModel = new (std::nothrow) _SearchBarModel;
199 SysTryReturn(NID_UI_CTRL, pModel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
201 r = pModel->Construct();
202 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM,
203 "[E_SYSTEM] A system error has occurred. The searchbar model construction failed.");
205 __pSearchBarModel = pModel;
215 _SearchBarPresenter::UpdateContentArea(bool invalidate)
217 SearchBarMode mode = __pSearchBarModel->GetMode();
218 SysTryReturn(NID_UI_CTRL, mode == SEARCH_BAR_MODE_INPUT, E_INVALID_OPERATION, E_INVALID_OPERATION,
219 "[E_INVALID_OPERATION] The searchbar mode cannot be updated.");
221 __pSearchBar->Invalidate(invalidate);
227 _SearchBarPresenter::SetContentAreaVisible(bool visible)
229 return __pSearchBarModel->SetContentAreaVisible(visible);
233 _SearchBarPresenter::IsContentAreaVisible(void) const
235 return __pSearchBarModel->IsContentAreaVisible();
239 _SearchBarPresenter::GetMode(void) const
241 return __pSearchBarModel->GetMode();
245 _SearchBarPresenter::IsModeLocked(void) const
247 return __pSearchBarModel->IsModeLocked();
251 _SearchBarPresenter::SetMode(SearchBarMode mode)
253 SysTryReturn(NID_UI_CTRL, IsModeLocked() == false, E_INVALID_OPERATION, E_INVALID_OPERATION,
254 "[E_INVALID_OPERATION] The searchbar mode is locked.");
256 if (GetMode() == mode)
261 __pSearchBarModel->SetMode(mode);
263 result r = ChangeMode(mode);
264 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
265 "[E_SYSTEM] A system error has occurred. Failed to change the mode of searchbar.");
271 _SearchBarPresenter::SetModeLocked(bool modeLocked)
273 return __pSearchBarModel->SetModeLocked(modeLocked);
277 _SearchBarPresenter::Draw(void)
279 result r = E_SUCCESS;
281 Color bgColor = __pSearchBar->GetColor();
283 FloatRectangle bounds(0, 0, __pSearchBar->GetBoundsF().width, __pSearchBar->GetBoundsF().height);
285 Bitmap* pBackgroundBitmap = __pSearchBar->GetBackgroundBitmap();
287 if (pBackgroundBitmap)
289 Canvas* pCanvas = __pSearchBar->GetCanvasN();
291 SysTryReturn(NID_UI_CTRL, (pCanvas != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
293 pCanvas->SetBackgroundColor(bgColor);
296 if (pBackgroundBitmap->IsNinePatchedBitmap())
298 pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
302 pCanvas->DrawBitmap(Point(0, 0), *pBackgroundBitmap);
309 __pSearchBar->SetBackgroundColor(bgColor);
313 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
319 _SearchBarPresenter::SetCancelButtonVisible(bool visible)
321 if ((__pSearchBar->IsUsableCancelButton() == false) && visible)
326 if (__pCancelButton == null)
331 __pCancelButton->SetVisibleState(visible);
337 _SearchBarPresenter::SetContainerVisible(bool visible)
339 if (__pContainer == null)
344 __pContainer->SetVisibleState(visible);
351 _SearchBarPresenter::DrawIcon(void)
353 SysTryReturn(NID_UI_CTRL, __pEdit, E_SYSTEM, E_SYSTEM,
354 "[E_SYSTEM] A system error has occurred. Failed to get edit instance.");
355 SysTryReturn(NID_UI_CTRL, __pIconElement, E_SYSTEM, E_SYSTEM,
356 "[E_SYSTEM] A system error has occurred. Failed to get edit visual element.");
358 result r = E_SUCCESS;
360 Canvas* pIconCanvas = null;
361 FloatRectangle iconBounds;
363 bool isCustomBitmap = false;
365 isCustomBitmap = IS_CUSTOM_BITMAP(SEARCHBAR::ICON_NORMAL);
370 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
372 GET_SHAPE_CONFIG(SEARCHBAR::ICON_HORIZONTAL_MARGIN, orientation, iconMarginH);
373 GET_SHAPE_CONFIG(SEARCHBAR::ICON_WIDTH, orientation, iconWidth);
374 GET_SHAPE_CONFIG(SEARCHBAR::ICON_HEIGHT, orientation, iconHeight);
376 iconBounds.x = _CoordinateSystemUtils::ConvertToFloat(iconMarginH);
377 iconBounds.y = (__pEdit->GetBoundsF().height - iconWidth) / 2.0f;
378 iconBounds.width = _CoordinateSystemUtils::ConvertToFloat(iconWidth);
379 iconBounds.height = _CoordinateSystemUtils::ConvertToFloat(iconHeight);
381 __pIconElement->SetBounds(FloatRectangle(iconBounds.x, iconBounds.y, iconBounds.width, iconBounds.height));
383 pIconCanvas = __pIconElement->GetCanvasN(FloatRectangle(0, 0, iconBounds.width, iconBounds.height));
384 SysTryReturnResult(NID_UI_CTRL, pIconCanvas != null, GetLastResult(), "Propagating.");
386 pIconCanvas->SetBackgroundColor(Color(0));
387 pIconCanvas->Clear();
388 if (__pSearchBar->IsEnabled())
390 if (__pReplacedSearchFieldNormalBitmap != null)
392 if (__pReplacedSearchFieldNormalBitmap->IsNinePatchedBitmap())
394 pIconCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
398 pIconCanvas->DrawBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldNormalBitmap);
404 if (__pReplacedSearchFieldDisabledBitmap != null)
406 if (__pReplacedSearchFieldDisabledBitmap->IsNinePatchedBitmap())
408 pIconCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
412 pIconCanvas->DrawBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pReplacedSearchFieldDisabledBitmap);
417 if (!isCustomBitmap && __pSearchFieldEffectBitmap != null)
419 if (__pSearchFieldEffectBitmap->IsNinePatchedBitmap())
421 pIconCanvas->DrawNinePatchedBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
425 pIconCanvas->DrawBitmap(FloatRectangle(0, 0, iconBounds.width, iconBounds.height), *__pSearchFieldEffectBitmap);
437 _SearchBarPresenter::ChangeMode(SearchBarMode mode)
439 result r = E_SUCCESS;
441 if (mode == SEARCH_BAR_MODE_NORMAL)
443 InitializeViewModeLayout();
444 SetCancelButtonVisible(false);
445 SetContainerVisible(false);
449 __pEdit->ClearText();
450 __pEdit->SetCursorDisabled(true);
451 __pEdit->SetBounds(__searchFieldBounds);
452 __pEdit->HideKeypad();
455 r = __pSearchBar->SendSearchBarEvent(_SEARCH_BAR_EVENT_MODE_CHANGE);
456 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
457 "[E_SYSTEM] A system error has occurred. Failed to fire the searchbar event.");
461 r = __pSearchBar->SendSearchBarEvent(_SEARCH_BAR_EVENT_MODE_CHANGE);
462 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
463 "[E_SYSTEM] A system error has occurred. Failed to fire the searchbar event.");
465 InitializeInputModeLayout();
466 SetCancelButtonVisible(true);
467 __pSearchBar->SetContentDimming();
471 __pEdit->SetCursorDisabled(false);
473 float buttonWidth = 0;
474 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
475 GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
477 float cancelButtonWidth = 0.0f;
478 cancelButtonWidth = __pCancelButton->GetTextExtentSizeF() + __pCancelButton->GetRightTouchMarginF() + __pCancelButton->GetLeftTouchMarginF() + __pCancelButton->GetRightMarginF() + __pCancelButton->GetLeftMarginF();
480 if (cancelButtonWidth <= buttonWidth)
482 __pEdit->SetBounds(__searchFieldBounds);
485 __pEdit->ShowKeypad();
487 __pSearchBar->SetContentsArea();
489 if (IsContentAreaVisible())
491 SetContainerVisible(true);
494 __pSearchBar->Invalidate();
501 _SearchBarPresenter::InitializeInputModeLayout(void)
503 InitializeViewModeLayout();
505 if (__pSearchBar->IsUsableCancelButton() == false)
510 int horizontalMargin = 0;
511 int buttonRightMargin = 0;
512 int buttonLeftMargin = 0;
514 int searchFieldMinWidth = 0;
515 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
517 GET_SHAPE_CONFIG(SEARCHBAR::HORIZONTAL_MARGIN, orientation, horizontalMargin);
518 GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_LEFT_MARGIN, orientation, buttonLeftMargin);
519 GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_RIGHT_MARGIN, orientation, buttonRightMargin);
520 GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_WIDTH, orientation, searchFieldMinWidth);
521 GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
522 int buttonResizableSearchBarWidth = (horizontalMargin + searchFieldMinWidth +
523 buttonWidth + buttonLeftMargin + buttonRightMargin);
525 if (__pSearchBar->GetBoundsF().width < buttonResizableSearchBarWidth)
527 __searchFieldBounds.width = searchFieldMinWidth;
532 __searchFieldBounds.width = __pSearchBar->GetBoundsF().width - (buttonRightMargin +
533 buttonWidth + buttonLeftMargin + horizontalMargin);
536 __searchFieldBounds.width = (__pSearchBar->GetBoundsF().width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
538 float cancelButtonWidth = 0.0f;
539 cancelButtonWidth = __pCancelButton->GetTextExtentSizeF() + __pCancelButton->GetRightTouchMarginF() + __pCancelButton->GetLeftTouchMarginF() + __pCancelButton->GetRightMarginF() + __pCancelButton->GetLeftMarginF();
542 if (cancelButtonWidth > buttonWidth)
544 __pSearchBar->RecalculateButtonBounds();
551 _SearchBarPresenter::InitializeViewModeLayout(void)
553 int horizontalMargin = 0;
554 int verticalMargin = 0;
555 int searchFieldMinWidth = 0;
556 int searchBarMinHeight = 0;
557 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
559 GET_SHAPE_CONFIG(SEARCHBAR::HORIZONTAL_MARGIN, orientation, horizontalMargin);
560 GET_SHAPE_CONFIG(SEARCHBAR::VERTICAL_MARGIN, orientation, verticalMargin);
561 GET_SHAPE_CONFIG(SEARCHBAR::SEARCH_FIELD_MIN_WIDTH, orientation, searchFieldMinWidth);
562 GET_SHAPE_CONFIG(SEARCHBAR::MIN_HEIGHT, orientation, searchBarMinHeight);
564 int searchFieldMinHeight = searchBarMinHeight - (verticalMargin * 2);
566 __searchFieldBounds.x = _CoordinateSystemUtils::ConvertToFloat(horizontalMargin);
567 __searchFieldBounds.y = _CoordinateSystemUtils::ConvertToFloat(verticalMargin);
568 __searchFieldBounds.width = __pSearchBar->GetBoundsF().width - (horizontalMargin * 2);
569 __searchFieldBounds.height = __pSearchBar->GetBoundsF().height - (verticalMargin * 2);
571 __searchFieldBounds.width = (__searchFieldBounds.width > searchFieldMinWidth) ? __searchFieldBounds.width : searchFieldMinWidth;
572 __searchFieldBounds.height = (__searchFieldBounds.height > searchFieldMinHeight) ? __searchFieldBounds.height : searchFieldMinHeight;
578 _SearchBarPresenter::OnBoundsChanged(void)
580 _Control* pClippedGroupControl = __pSearchBar->GetClippedGroupControl();
582 if (pClippedGroupControl != null)
584 pClippedGroupControl->SetBounds(FloatRectangle(FloatPoint(0.0f, 0.0f), __pSearchBar->GetSizeF()));
587 if (GetMode() == SEARCH_BAR_MODE_NORMAL)
589 InitializeViewModeLayout();
593 InitializeInputModeLayout();
596 result r = E_SUCCESS;
598 if (__pCancelButton != null)
600 r = __pSearchBar->ResizeCancelButton();
601 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
604 r = __pEdit->SetBounds(__searchFieldBounds);
605 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.",GetErrorMessage(r));
607 if (GetMode() == SEARCH_BAR_MODE_INPUT)
609 float buttonWidth = 0;
610 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
611 GET_SHAPE_CONFIG(SEARCHBAR::BUTTON_WIDTH, orientation, buttonWidth);
612 float cancelButtonWidth = 0.0f;
613 cancelButtonWidth = __pCancelButton->GetTextExtentSizeF() + __pCancelButton->GetRightTouchMarginF() + __pCancelButton->GetLeftTouchMarginF() + __pCancelButton->GetRightMarginF() + __pCancelButton->GetLeftMarginF();
614 if (cancelButtonWidth > buttonWidth)
616 __pSearchBar->RecalculateButtonBounds();
620 __pSearchBar->SetContentsArea();
626 _SearchBarPresenter::SetHeaderVisibleState(bool visible)
629 _Control* pControlCore = __pEdit->GetParent();
633 if (pControlCore == null)
635 SysLog(NID_UI_CTRL,"[E_SYSTEM] The parent form is null.");
640 pForm = dynamic_cast<_Form*>(pControlCore);
646 pControlCore = pControlCore->GetParent();
649 _Toolbar* pHeader = pForm->GetHeader();
655 pHeader->SetVisibleState(visible);
660 }}} // Tizen::Ui::Controls