Initialize member variable in FocusMgr
[framework/osp/uifw.git] / src / ui / FUi_FocusManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
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 * @file             FUi_FocusManagerImpl.cpp
19 * @brief                This is the implementation file for _FocusManagerImpl class.
20 * @version              2.0
21 *
22 */
23
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"
35
36 using namespace std;
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;
42
43 namespace Tizen { namespace Ui
44 {
45 ////////////////////////////////////////////////////////////////////////////////
46 /// _FocusManagerImpl class Lifecycle
47 _FocusManagerImpl* _FocusManagerImpl::__pInstance = null;
48
49 _FocusManagerImpl::_FocusManagerImpl(void)
50         : __pCurrentFocusUiControl(null)
51 {
52         result r = _UiEventManager::GetInstance()->AddPostKeyEventListener(*this);
53         SysTryReturnVoidResult(NID_UI,  r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
54         
55         r = _UiEventManager::GetInstance()->AddTouchEventListener(*this);
56         SysTryReturnVoidResult(NID_UI,  r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
57 }
58
59
60 _FocusManagerImpl::~_FocusManagerImpl(void)
61 {
62         // NOTHING
63 }
64
65 void
66 _FocusManagerImpl::Initialize(void)
67 {
68         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
69
70         if (__pInstance == null)
71         {
72                 pthread_once(&once_block, InitializeInstance);
73         }
74 }
75
76 _FocusManagerImpl*
77 _FocusManagerImpl::GetInstance(void)
78 {
79         return __pInstance;
80 }
81
82 void
83 _FocusManagerImpl::InitializeInstance(void)
84 {
85         ClearLastResult();
86
87         if (__pInstance == null)
88         {
89                 __pInstance = new (std::nothrow) _FocusManagerImpl;
90                 SysTryReturnVoidResult(NID_UI, __pInstance != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
91         }
92 }
93
94 void
95 _FocusManagerImpl::ReleaseInstance(void)
96 {
97         if (__pInstance)
98         {
99                 delete __pInstance;
100                 __pInstance = null;
101         }
102 }
103
104 ////////////////////////////////////////////////////////////////////////////////
105 /// _FocusManagerImpl class Operation
106
107 _ControlImpl*
108 _FocusManagerImpl::GetCurrentFocusOwner(void) const
109 {
110         _Control* pFocus = _ControlManager::GetInstance()->GetFocusedControl();
111         if(pFocus == null)
112         {
113                 return null;
114         }
115
116         _ControlImpl* pFocusImpl = static_cast <_ControlImpl*>(pFocus->GetUserData());
117         SysAssert(pFocusImpl);
118
119         return pFocusImpl;
120 }
121
122
123 _WindowImpl*
124 _FocusManagerImpl::GetCurrentFocusedWindow(void) const
125 {
126         _Control* pFocus = _ControlManager::GetInstance()->GetFocusedControl();
127         if(pFocus == null)
128         {
129                 return null;
130         }
131         _Window* pWindow = pFocus->GetRootWindow();
132         SysAssert(pWindow);
133
134         _WindowImpl* pWindowImpl = static_cast <_WindowImpl*>(static_cast <_ControlImpl*>(pWindow->GetUserData()));
135         SysAssert(pWindowImpl);
136
137         return pWindowImpl;
138 }
139
140 int
141 _FocusManagerImpl::GetNextIndex(int currentIndex, FocusDirection focusDirection, const IListT<_Control*>* pFocusControlList) const
142 {
143         int nextIndex = currentIndex;
144         if (focusDirection == FOCUS_DIRECTION_DOWNWARD)
145         {
146                 ++nextIndex;
147                 if (nextIndex == pFocusControlList->GetCount())
148                 {
149                         nextIndex = 0;
150                 }
151         }
152         else
153         {
154                 --nextIndex;
155                 if (nextIndex  < 0)
156                 {
157                         nextIndex = pFocusControlList->GetCount() - 1;
158                 }
159         }
160         return nextIndex;
161
162
163 }
164
165 bool 
166 _FocusManagerImpl::IsFocusable(_Control* pControl) const
167 {
168         bool isFocusalbe = pControl->IsFocusable();
169         bool enableState = pControl->GetEnableState();
170         bool visibleState = pControl->GetVisibleState();        
171         if (enableState  && visibleState  && isFocusalbe)
172         {
173                 return true;
174         }
175         return false;
176 }
177
178 void
179 _FocusManagerImpl::StartFocusTraversal(_Control* pControl, FocusDirection focusDirection)
180 {
181         bool focusMode = pControl->IsFocusModeStateEnabled();
182         if (focusMode == false && pControl->IsFocused())
183         {
184                 if (IsFocusable(pControl))
185                 {
186                         __pCurrentFocusUiControl = pControl;
187                         pControl->DrawFocus();
188                         pControl->SetFocusModeStateEnabled(true);
189                         return;
190                 }
191         }
192
193         if (focusDirection == FOCUS_DIRECTION_DOWNWARD)
194         {
195                 _Control* pFocusControl =  pControl->GetNextFocus();
196                 if (pFocusControl)
197                 {
198                         pFocusControl->SetFocused();
199                         pFocusControl->DrawFocus();
200                         return;
201                 }
202         }
203         else
204         {
205                 _Control* pFocusControl =  pControl->GetPreviousFocus();
206                 if (pFocusControl)
207                 {
208                         pFocusControl->SetFocused();
209                         pFocusControl->DrawFocus();
210                         return;
211                 }
212         }
213
214         IListT<_Control*>* pFocusControlList =  null;
215         _Window* pTop = pControl->GetRootWindow();
216         if (pTop)
217         {
218                 _Frame* pFrame = dynamic_cast<_Frame*>(pTop);
219                 if (pFrame)
220                 {
221                         _Form* pForm = pFrame->GetCurrentForm();
222                         pFocusControlList = pForm->GetFocusList();
223                 }
224                 else
225                 {
226                         pFocusControlList = pTop->GetFocusList();
227                 }
228         }
229
230         if (pFocusControlList)
231         {
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.");
234                 int index = 0;
235                 bool findNextFocusControl = false;
236                 _Control* pNextFocusControl = null;
237                 while (pEnum->MoveNext() == E_SUCCESS)
238                 {
239                         _Control* pEnumeratorControl = null;
240                         pEnum->GetCurrent(pEnumeratorControl);
241
242                         //Find current focused control
243                         if (pControl == pEnumeratorControl)
244                         {
245                         //Find next focusable control.
246                         index = GetNextIndex(index, focusDirection, pFocusControlList);
247                         
248                                 while (pFocusControlList->GetAt(index, pNextFocusControl) == E_SUCCESS)
249                                 {
250                                         if (IsFocusable(pNextFocusControl))
251                                         {
252                                                 if (pNextFocusControl != pControl)
253                                                 {
254                                                     pNextFocusControl->SetFocused();
255                                                     pNextFocusControl->SetFocusModeStateEnabled(true);
256                                                     pControl->SetFocusModeStateEnabled(false);
257                                                 }
258                                                 findNextFocusControl = true;
259                                                 break;
260                                         }
261                                         else
262                                         {
263                                                 index = GetNextIndex(index, focusDirection, pFocusControlList);
264                                         }
265                                 }
266                         }
267
268                         if (findNextFocusControl == true)
269                         {
270                                 break;
271                         }
272
273                         index ++;
274                 }
275
276                 __pCurrentFocusUiControl = pNextFocusControl;
277                 if (pNextFocusControl && findNextFocusControl == true)
278                 {
279                         _Control* pParentControl =  pNextFocusControl->GetParent();
280                         if (pParentControl)
281                         {
282                                 pParentControl->OnChildControlFocusMoved(*pNextFocusControl);
283                         }
284                         pNextFocusControl->DrawFocus();
285                 }
286         }
287 }
288
289 bool
290 _FocusManagerImpl::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
291 {
292         _KeyCode keyCode = keyInfo.GetKeyCode();
293
294         if(keyCode == _KEY_TAB)
295         {
296                 FocusDirection focusDirection = FOCUS_DIRECTION_DOWNWARD;
297                 if (keyInfo.GetKeyModifier() & _KEY_MODIFIER_SHIFT)
298                 {
299                         focusDirection = FOCUS_DIRECTION_UPWARD;
300                 }
301
302                 _Control* pControl = const_cast<_Control*> (&source);
303                 StartFocusTraversal(pControl, focusDirection);
304         }
305         return false;
306 }
307 bool
308 _FocusManagerImpl::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
309 {
310         return false;
311 }
312
313 bool 
314 _FocusManagerImpl::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
315 {       
316         if (__pCurrentFocusUiControl)
317         {
318                 __pCurrentFocusUiControl->RemoveFocusRing();
319                 __pCurrentFocusUiControl->SetFocusModeStateEnabled(false);
320                 __pCurrentFocusUiControl = null;
321         }
322         return false;
323 }
324
325 bool 
326 _FocusManagerImpl::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo) 
327 {
328         return false;
329 }
330
331 bool
332 _FocusManagerImpl:: OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
333 {       
334         return false;
335 }
336
337 bool 
338 _FocusManagerImpl::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
339 {       
340         return false;
341 }
342 } } //Tizen::Ui