Fix for N_SE-40699
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_SearchBar.cpp
index 5f45a33..311cb6c 100644 (file)
@@ -9,7 +9,7 @@
 //     http://www.apache.org/licenses/LICENSE-2.0/
 //
 // Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an ”AS IS” BASIS,
+// distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 #include "FUi_AccessibilityElement.h"
 #include "FUi_AccessibilityManager.h"
 #include "FUi_CoordinateSystemUtils.h"
+#include "FUiCtrl_SearchBarImpl.h"
+#include "FUiCtrl_EditFieldImpl.h"
+#include "FUiCtrl_ButtonImpl.h"
+#include <FSys_SettingInfoImpl.h>
+
 
 using namespace Tizen::Graphics;
 using namespace Tizen::Ui;
@@ -47,6 +52,7 @@ using namespace Tizen::Ui::Animations;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Runtime;
 using namespace Tizen::Locales;
+using namespace Tizen::System;
 
 namespace Tizen { namespace Ui { namespace Controls
 {
@@ -69,6 +75,9 @@ _SearchBar::_SearchBar(void)
        , __isUserGuideTextColor(false)
        , __isKeypadOpening(false)
        , __isupdateContentBounds(false)
+       , __isKeyPressed(false)
+       , __isFocusCallbackToBeFired(true)
+       , __isButtonTextChangedByApp(false)
        , __keypadAction(CORE_KEYPAD_ACTION_SEARCH)
        , __pBackgroundBitmap(null)
        , __backgroundColor(Color())
@@ -103,28 +112,11 @@ _SearchBar::_SearchBar(void)
 
 _SearchBar::~_SearchBar(void)
 {
+       SettingInfo::RemoveSettingEventListener(*this);
 
        delete __pSearchBarPresenter;
        __pSearchBarPresenter = null;
 
-       if (__pEdit)
-       {
-               __pEdit->HideKeypad();
-
-               __pClippedGroupControl->DetachChild(*__pEdit);
-
-               delete __pEdit;
-               __pEdit = null;
-       }
-
-       if (__pCancelButton)
-       {
-               __pClippedGroupControl->DetachChild(*__pCancelButton);
-
-               delete __pCancelButton;
-               __pCancelButton = null;
-       }
-
        if (__pClippedGroupControl)
        {
                DetachChild(*__pClippedGroupControl);
@@ -272,6 +264,10 @@ _SearchBar::Initialize(bool enableSearchBarButton, CoreKeypadAction keypadAction
        __pSearchBarPresenter = pPresenter;
 
        CreateAccessibilityElement();
+
+       r = _SettingInfoImpl::AddSettingEventListener(*this);
+       SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
+
        return r;
 
 CATCH:
@@ -328,13 +324,11 @@ _SearchBar::CreateSearchField(void)
        editBounds.width = (editBounds.width > searchBarMinWidthModeNormal) ? editBounds.width : searchBarMinWidthModeNormal;
        editBounds.height = (editBounds.height > searchFieldMinHeight) ? editBounds.height : searchFieldMinHeight;
 
-       __pEdit = _Edit::CreateEditN();
-       r = GetLastResult();
-       SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
+       _SearchBarImpl* pSearchBarImpl = static_cast<_SearchBarImpl*>(GetUserData());
+       __pEdit = &(pSearchBarImpl->GetEditFieldImpl()->GetCore());
 
-       r = __pEdit->Initialize(EDIT_STYLE_NORMAL | EDIT_STYLE_SINGLE_LINE | EDIT_STYLE_CLEAR | EDIT_STYLE_NOSCROLL, INPUT_STYLE_OVERLAY,
-                                                       SEARCHBAR_TEXT_LENGTH_MAX);
-       SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
+       SysTryReturnResult(NID_UI_CTRL, __pEdit != null, E_SYSTEM,
+                          "[E_SYSTEM] A system error has occured. Failed to get _Edit instance");
 
        __pEdit->SetKeypadCommandButtonVisible(false);
        __pEdit->SetBounds(editBounds);
@@ -386,7 +380,12 @@ _SearchBar::CreateCancelButton(void)
 
        _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
 
-       __pCancelButton = _Button::CreateButtonN();
+       _SearchBarImpl* pSearchBarImpl = static_cast<_SearchBarImpl*>(GetUserData());
+       __pCancelButton = &(pSearchBarImpl->GetButtonImpl()->GetCore());
+
+       SysTryReturnResult(NID_UI_CTRL, __pCancelButton != null, E_SYSTEM,
+                          "[E_SYSTEM] A system error has occured. Failed to get _Edit instance");
+
        r = GetLastResult();
        SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
 
@@ -580,6 +579,16 @@ _SearchBar::SetMode(SearchBarMode mode)
 result
 _SearchBar::SetModeLocked(bool modeLocked)
 {
+       if (modeLocked == true && GetMode() == SEARCH_BAR_MODE_NORMAL)
+       {
+               __pEdit->SetViewModeEnabled(true);
+       }
+
+       if (modeLocked == false && __pEdit->IsViewModeEnabled())
+       {
+               __pEdit->SetViewModeEnabled(false);
+       }
+
        return __pSearchBarPresenter->SetModeLocked(modeLocked);
 }
 
@@ -671,6 +680,8 @@ _SearchBar::SetButtonText(const String& text)
        SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
                     "[E_SYSTEM] A system error has occurred. Failed to set text.");
 
+       __isButtonTextChangedByApp = true;
+
        return r;
 }
 
@@ -1579,8 +1590,6 @@ _SearchBar::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& tou
                return _UI_TOUCH_EVENT_DELIVERY_YES;
        }
 
-       SetMode(SEARCH_BAR_MODE_INPUT);
-
        return _UI_TOUCH_EVENT_DELIVERY_YES;
 }
 
@@ -1654,6 +1663,8 @@ _SearchBar::OnKeypadClosed(void)
                __pKeypadEvent->Fire(*pEventArg);
        }
 
+       __isKeypadOpening = false;
+
        SetContentsArea();
        return;
 }
@@ -1763,15 +1774,28 @@ _SearchBar::OnFontInfoRequested(unsigned long& style, int& size)
 bool
 _SearchBar::OnFocusGained(const _Control& source)
 {
-       SetMode(SEARCH_BAR_MODE_INPUT);
-       __pEdit->SetFocused();
-       return false;
+       if (__isKeyPressed)
+       {
+               __isKeyPressed = false;
+               _Control::OnDrawFocus();
+
+               return true;
+       }
+
+       if (GetVisibleState() == true)
+       {
+               __pEdit->SetFocused();
+       }
+
+       return true;
 }
 
 bool
-_SearchBar::OnFocusLost(const _Controlsource)
+_SearchBar::OnFocusLost(const _Control &source)
 {
-       return false;
+       __pEdit->SetSearchFieldFocus(false);
+       _Control::OnFocusLost(source);
+       return true;
 }
 
 bool
@@ -1782,49 +1806,103 @@ _SearchBar::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
                return false;
        }
 
+       if (!__pEdit->IsSearchFieldFocused())
+       {
+               return false;
+       }
+
        _KeyCode keyCode = keyInfo.GetKeyCode();
 
-       if (keyCode == _KEY_RIGHT)
+       switch (keyCode)
        {
-               if (__isButtonEnabled)
-               {
-                       __pEdit->SetFocused(false);
-                       __pCancelButton->SetButtonStatus(_BUTTON_STATUS_HIGHLIGHTED);
-                       __pCancelButton->SetFocused();
-                       __pCancelButton->Invalidate(true);
-               }
+               case _KEY_RIGHT:
+                       SetFocusCallbackParameter(false);
+                       if (&source == __pEdit)
+                       {
+                               if ((!__isButtonEnabled || !__isUsableCancelButton) && __pContentControl && __pContentControl->GetVisibleState() && __pContentControl->GetEnableState())
+                               {
+                                       __pContentControl->SetFocused();
+                                       __pContentControl->OnDrawFocus();
 
-               return true;
-       }
+                                       return true;
+                               }
+                               else if (__isButtonEnabled && __isUsableCancelButton)
+                               {
+                                       __pCancelButton->SetFocused();
+                               }
 
-       if (keyCode == _KEY_LEFT)
-       {
-               if (__pCancelButton->GetButtonStatus() == _BUTTON_STATUS_HIGHLIGHTED)
-               {
-                       __pCancelButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
-                       __pCancelButton->SetFocused(false);
-                       __pCancelButton->Invalidate();
+                               return true;
+                       }
 
-               }
+                       if (&source == __pCancelButton && __pContentControl && __pContentControl->GetVisibleState() && __pContentControl->GetEnableState())
+                       {
+                               __pContentControl->SetFocused();
+                               __pContentControl->OnDrawFocus();
 
-               if (__pCancelButton->GetButtonStatus() == _BUTTON_STATUS_DISABLED)      //Searchbar Button is disabled, left arrow key is pressed
-               {
-                       __pCancelButton->SetButtonStatus(_BUTTON_STATUS_NORMAL);
-                       SetButtonEnabled(false);
-               }
+                               return true;
+                       }
 
-               __pEdit->SetFocused();
+                       break;
 
-               return true;
+               case _KEY_LEFT:
+                       if (&source == __pCancelButton)
+                       {
+                               __pEdit->SetFocused();
+                               return true;
+                       }
+
+                       if (&source == __pContentControl)
+                       {
+                               if ((__isButtonEnabled && __isUsableCancelButton))
+                               {
+                                       __pCancelButton->SetFocused();
+                               }
+                               else
+                               {
+                                       __pEdit->SetFocused();
+                               }
+                               return true;
+                       }
+
+                       break;
+
+               case _KEY_ENTER:
+                       if (&source == __pCancelButton)
+                       {
+                               SetMode(SEARCH_BAR_MODE_NORMAL);
+                               __isKeyPressed = true;
+                               SetFocused();
+                               _Control::OnDrawFocus();
+
+                               return true;
+                       }
+
+                       break;
+
+               default:
+                       return false;
        }
 
        return false;
 }
 
-bool
-_SearchBar::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
+void
+_SearchBar::OnSettingChanged(Tizen::Base::String& key)
 {
-       return false;
+       if (__isButtonTextChangedByApp || !__isUsableCancelButton)
+       {
+               return;
+       }
+
+       if (key.Equals(L"http://tizen.org/setting/locale.language", false))
+       {
+               String cancelText;
+               GET_STRING_CONFIG(IDS_COM_SK_CANCEL, cancelText);
+               __pCancelButton->SetText(cancelText);
+               __pCancelButton->Invalidate();
+       }
+
+       return;
 }
 
 _Control*
@@ -2463,7 +2541,7 @@ _SearchBar::CreateAccessibilityElement(void)
                __pAccessibilitySearchBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
                __pAccessibilitySearchBarElement->SetName("SearchBar");
                pContainer->AddElement(*__pAccessibilitySearchBarElement);
-        }
+       }
 
        return;
 }
@@ -2486,6 +2564,13 @@ _SearchBar::SetContentDimming(void)
        return;
 }
 
+void
+_SearchBar::OnDrawFocus(void)
+{
+       __pEdit->SetSearchFieldFocus(true);
+       return;
+}
+
 bool
 _SearchBar::IsContentAttachable(const _Control* pContent)
 {
@@ -2691,4 +2776,16 @@ _SearchBar::SendOpaqueCommand(const String& command)
        return;
 }
 
+void
+_SearchBar::SetFocusCallbackParameter(bool state)
+{
+       __isFocusCallbackToBeFired = state;
+       return;
+}
+
+bool
+_SearchBar::IsFocusCallbackToBeFired(void) const
+{
+       return __isFocusCallbackToBeFired;
+}
 }}} // Tizen::Ui::Controls