2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
18 * @file FUi_FocusManagerImpl.cpp
19 * @brief This is the implementation file for _FocusManagerImpl class.
24 #include <unique_ptr.h>
25 #include "FUi_FocusManagerImpl.h"
26 #include "FUi_ControlImpl.h"
27 #include "FUi_WindowImpl.h"
28 #include "FUi_ControlManager.h"
29 #include "FUiCtrl_Form.h"
30 #include "FUi_UiEventManager.h"
31 #include "FUi_ResourceManager.h"
32 #include "FUi_UiKeyEvent.h"
33 #include "FUi_Window.h"
34 #include "FUiCtrl_Frame.h"
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Ui;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Ui::Animations;
43 namespace Tizen { namespace Ui
45 ////////////////////////////////////////////////////////////////////////////////
46 /// _FocusManagerImpl class Lifecycle
47 _FocusManagerImpl* _FocusManagerImpl::__pInstance = null;
49 _FocusManagerImpl::_FocusManagerImpl(void)
50 : __pCurrentFocusUiControl(null)
52 result r = _UiEventManager::GetInstance()->AddPostKeyEventListener(*this);
53 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
55 r = _UiEventManager::GetInstance()->AddTouchEventListener(*this);
56 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
60 _FocusManagerImpl::~_FocusManagerImpl(void)
66 _FocusManagerImpl::Initialize(void)
68 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
70 if (__pInstance == null)
72 pthread_once(&once_block, InitializeInstance);
77 _FocusManagerImpl::GetInstance(void)
83 _FocusManagerImpl::InitializeInstance(void)
87 if (__pInstance == null)
89 __pInstance = new (std::nothrow) _FocusManagerImpl;
90 SysTryReturnVoidResult(NID_UI, __pInstance != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
95 _FocusManagerImpl::ReleaseInstance(void)
104 ////////////////////////////////////////////////////////////////////////////////
105 /// _FocusManagerImpl class Operation
108 _FocusManagerImpl::GetCurrentFocusOwner(void) const
110 _Control* pFocus = _ControlManager::GetInstance()->GetFocusedControl();
116 _ControlImpl* pFocusImpl = static_cast <_ControlImpl*>(pFocus->GetUserData());
117 SysAssert(pFocusImpl);
124 _FocusManagerImpl::GetCurrentFocusedWindow(void) const
126 _Control* pFocus = _ControlManager::GetInstance()->GetFocusedControl();
131 _Window* pWindow = pFocus->GetRootWindow();
134 _WindowImpl* pWindowImpl = static_cast <_WindowImpl*>(static_cast <_ControlImpl*>(pWindow->GetUserData()));
135 SysAssert(pWindowImpl);
141 _FocusManagerImpl::GetNextIndex(int currentIndex, FocusDirection focusDirection, const IListT<_Control*>* pFocusControlList) const
143 int nextIndex = currentIndex;
144 if (focusDirection == FOCUS_DIRECTION_DOWNWARD)
147 if (nextIndex == pFocusControlList->GetCount())
157 nextIndex = pFocusControlList->GetCount() - 1;
166 _FocusManagerImpl::IsFocusable(_Control* pControl) const
168 bool isFocusalbe = pControl->IsFocusable();
169 bool enableState = pControl->GetEnableState();
170 bool visibleState = pControl->GetVisibleState();
171 if (enableState && visibleState && isFocusalbe)
179 _FocusManagerImpl::StartFocusTraversal(_Control* pControl, FocusDirection focusDirection)
181 bool focusMode = pControl->IsFocusModeStateEnabled();
182 if (focusMode == false && pControl->IsFocused())
184 if (IsFocusable(pControl))
186 __pCurrentFocusUiControl = pControl;
187 pControl->DrawFocus();
188 pControl->SetFocusModeStateEnabled(true);
193 if (focusDirection == FOCUS_DIRECTION_DOWNWARD)
195 _Control* pFocusControl = pControl->GetNextFocus();
198 pFocusControl->SetFocused();
199 pFocusControl->DrawFocus();
205 _Control* pFocusControl = pControl->GetPreviousFocus();
208 pFocusControl->SetFocused();
209 pFocusControl->DrawFocus();
214 IListT<_Control*>* pFocusControlList = null;
215 _Window* pTop = pControl->GetRootWindow();
218 _Frame* pFrame = dynamic_cast<_Frame*>(pTop);
221 _Form* pForm = pFrame->GetCurrentForm();
222 pFocusControlList = pForm->GetFocusList();
226 pFocusControlList = pTop->GetFocusList();
230 if (pFocusControlList)
232 unique_ptr<IEnumeratorT<_Control*> > pEnum (pFocusControlList->GetEnumeratorN());
233 SysTryReturnVoidResult(NID_UI_CTRL, pEnum, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
235 bool findNextFocusControl = false;
236 _Control* pNextFocusControl = null;
237 while (pEnum->MoveNext() == E_SUCCESS)
239 _Control* pEnumeratorControl = null;
240 pEnum->GetCurrent(pEnumeratorControl);
242 //Find current focused control
243 if (pControl == pEnumeratorControl)
245 //Find next focusable control.
246 index = GetNextIndex(index, focusDirection, pFocusControlList);
248 while (pFocusControlList->GetAt(index, pNextFocusControl) == E_SUCCESS)
250 if (IsFocusable(pNextFocusControl))
252 if (pNextFocusControl != pControl)
254 pNextFocusControl->SetFocused();
255 pNextFocusControl->SetFocusModeStateEnabled(true);
256 pControl->SetFocusModeStateEnabled(false);
258 findNextFocusControl = true;
263 index = GetNextIndex(index, focusDirection, pFocusControlList);
268 if (findNextFocusControl == true)
276 __pCurrentFocusUiControl = pNextFocusControl;
277 if (pNextFocusControl && findNextFocusControl == true)
279 _Control* pParentControl = pNextFocusControl->GetParent();
282 pParentControl->OnChildControlFocusMoved(*pNextFocusControl);
284 pNextFocusControl->DrawFocus();
290 _FocusManagerImpl::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
292 _KeyCode keyCode = keyInfo.GetKeyCode();
294 if(keyCode == _KEY_TAB)
296 FocusDirection focusDirection = FOCUS_DIRECTION_DOWNWARD;
297 if (keyInfo.GetKeyModifier() & _KEY_MODIFIER_SHIFT)
299 focusDirection = FOCUS_DIRECTION_UPWARD;
302 _Control* pControl = const_cast<_Control*> (&source);
303 StartFocusTraversal(pControl, focusDirection);
308 _FocusManagerImpl::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
314 _FocusManagerImpl::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
316 if (__pCurrentFocusUiControl)
318 __pCurrentFocusUiControl->RemoveFocusRing();
319 __pCurrentFocusUiControl->SetFocusModeStateEnabled(false);
320 __pCurrentFocusUiControl = null;
326 _FocusManagerImpl::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
332 _FocusManagerImpl:: OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
338 _FocusManagerImpl::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)