68162002cb956a3c56c7632841bfa2531f562f3c
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H__
2 #define __DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H__
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/base-object.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
26 #include <dali-toolkit/devel-api/focus-manager/keyboard-focus-manager-devel.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 /**
38  * @copydoc Toolkit::KeyboardFocusManager
39  */
40 class KeyboardFocusManager : public Dali::BaseObject
41 {
42 public:
43
44   typedef Toolkit::DevelKeyboardFocusManager::CustomAlgorithmInterface CustomAlgorithmInterface;
45
46   /**
47    * @copydoc Toolkit::KeyboardFocusManager::Get
48    */
49   static Toolkit::KeyboardFocusManager Get();
50
51   /**
52    * Construct a new KeyboardFocusManager.
53    */
54   KeyboardFocusManager();
55
56   /**
57    * @copydoc Toolkit::KeyboardFocusManager::SetCurrentFocusActor
58    */
59   bool SetCurrentFocusActor(Actor actor);
60
61   /**
62    * @copydoc Toolkit::KeyboardFocusManager::GetCurrentFocusActor
63    */
64   Actor GetCurrentFocusActor();
65
66   /**
67    * @copydoc Toolkit::KeyboardFocusManager::MoveFocus
68    */
69   bool MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction);
70
71   /**
72    * @copydoc Toolkit::KeyboardFocusManager::ClearFocus
73    */
74   void ClearFocus();
75
76   /**
77    * @copydoc Toolkit::KeyboardFocusManager::SetAsFocusGroup
78    */
79   void SetAsFocusGroup(Actor actor, bool isFocusGroup);
80
81   /**
82    * @copydoc Toolkit::KeyboardFocusManager::IsFocusGroup
83    */
84   bool IsFocusGroup(Actor actor) const;
85
86   /**
87    * @copydoc Toolkit::KeyboardFocusManager::GetFocusGroup
88    */
89   Actor GetFocusGroup(Actor actor);
90
91   /**
92    * @copydoc Toolkit::KeyboardFocusManager::SetFocusGroupLoop
93    */
94   void SetFocusGroupLoop(bool enabled);
95
96   /**
97    * @copydoc Toolkit::KeyboardFocusManager::GetFocusGroupLoop
98    */
99   bool GetFocusGroupLoop() const;
100
101   /**
102    * @copydoc Toolkit::KeyboardFocusManager::SetFocusIndicatorActor
103    */
104   void SetFocusIndicatorActor(Actor indicator);
105
106   /**
107    * @copydoc Toolkit::KeyboardFocusManager::GetFocusIndicatorActor
108    */
109   Actor GetFocusIndicatorActor();
110
111   /**
112    * Move current focus to backward
113    */
114   void MoveFocusBackward();
115
116   /**
117    * @copydoc Toolkit::DevelKeyboardFocusManager::SetCustomAlgorithm
118    */
119   void SetCustomAlgorithm(CustomAlgorithmInterface& interface);
120
121 public:
122
123   /**
124    * @copydoc Toolkit::KeyboardFocusManager::PreFocusChangeSignal()
125    */
126   Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& PreFocusChangeSignal();
127
128   /**
129    * @copydoc Toolkit::KeyboardFocusManager::FocusChangedSignal()
130    */
131   Toolkit::KeyboardFocusManager::FocusChangedSignalType& FocusChangedSignal();
132
133   /**
134    * @copydoc Toolkit::KeyboardFocusManager::FocusGroupChangedSignal()
135    */
136   Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& FocusGroupChangedSignal();
137
138   /**
139    * @copydoc Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignal()
140    */
141   Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& FocusedActorEnterKeySignal();
142
143   /**
144    * Connects a callback function with the object's signals.
145    * @param[in] object The object providing the signal.
146    * @param[in] tracker Used to disconnect the signal.
147    * @param[in] signalName The signal to connect to.
148    * @param[in] functor A newly allocated FunctorDelegate.
149    * @return True if the signal was connected.
150    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
151    */
152   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
153
154 protected:
155
156   /**
157    * Destructor
158    */
159   virtual ~KeyboardFocusManager();
160
161 private:
162
163   typedef Dali::Vector< Dali::BaseObject* > FocusStack; ///< Define Dali::Vector< Dali::BaseObject* > as FocusStack to contain focus history
164   typedef FocusStack::Iterator FocusStackIterator; ///< Define FocusStack::Iterator as FocusStackIterator to navigate FocusStack
165
166   /**
167    * Get the focus group of current focused actor.
168    * @pre The FocusManager has been initialized.
169    * @return A handle to the parent of the current focused actor which is a focus group,
170    * or an empty handle if no actor is focused.
171    */
172   Actor GetCurrentFocusGroup();
173
174   /**
175    * Move the focus to the specified actor and send notification for the focus change.
176    * @param actor The actor to be queried
177    * @return Whether the focus is successful or not
178    */
179   bool DoSetCurrentFocusActor(Actor actor);
180
181   /**
182    * Move the focus to the next actor towards the specified direction within the layout control
183    * @param control The layout control to move the focus in
184    * @param actor The current focused actor
185    * @param direction The direction of focus movement
186    * @return Whether the focus is successful or not
187    */
188   bool DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction);
189
190   /**
191    * Move the focus to the first focusable actor in the next focus group in the forward
192    * or backward direction. The "Tab" key changes the focus group in the forward direction
193    * and the "Shift-Tab" key changes it in the backward direction.
194    * @param forward Whether the direction of focus group change is forward or backward
195    * @return Whether the focus group change is successful or not
196    */
197   bool DoMoveFocusToNextFocusGroup(bool forward);
198
199   /**
200    * Enter has been pressed on the actor. If the actor is control, call the OnKeybaordEnter virtual function.
201    * This function will emit FocusedActorEnterKeySignal.
202    * @param actor The actor to notify
203    */
204   void DoKeyboardEnter( Actor actor );
205
206   /**
207    * Check whether the actor is a layout control that supports two dimensional keyboard navigation.
208    * The layout control needs to internally set the focus order for the child actor and be able to
209    * tell KeyboardFocusmanager the next focusable actor in the given direction.
210    * @pre The KeyboardFocusManager has been initialized.
211    * @pre The Actor has been initialized.
212    * @param actor The actor to be checked.
213    * @return Whether the actor is a layout control or not.
214    */
215   bool IsLayoutControl(Actor actor) const;
216
217   /**
218    * Returns the closest ancestor of the given actor that is a layout control.
219    * @param actor The actor to be checked for its parent layout control
220    * @return The parent layout control the given actor belongs to or an empty handle if the given actor doesn't belong to a layout control
221    */
222  Toolkit::Control GetParentLayoutControl(Actor actor) const;
223
224   /**
225    * Callback for the key event when no actor in the stage has gained the key input focus
226    * @param[in] event The KeyEvent event.
227    */
228   void OnKeyEvent(const KeyEvent& event);
229
230   /**
231    * Callback for the touch event when the screen is touched and when the touch ends
232    * (i.e. the down & up touch events only).
233    * @param[in] touch The touch information
234    */
235   void OnTouch( const TouchData& touch );
236
237 private:
238
239   // Undefined
240   KeyboardFocusManager(const KeyboardFocusManager&);
241
242   KeyboardFocusManager& operator=(const KeyboardFocusManager& rhs);
243
244 private:
245
246   Toolkit::KeyboardFocusManager::PreFocusChangeSignalType mPreFocusChangeSignal; ///< The signal to notify the focus will be changed
247   Toolkit::KeyboardFocusManager::FocusChangedSignalType mFocusChangedSignal; ///< The signal to notify the focus change
248   Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType mFocusGroupChangedSignal; ///< The signal to notify the focus group change
249   Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType mFocusedActorEnterKeySignal; ///< The signal to notify that enter has been pressed on the focused actor
250
251   unsigned int mCurrentFocusActor; ///< The actor ID of current focused actor
252
253   Actor mFocusIndicatorActor; ///< The focus indicator actor shared by all the keyboard focusable actors for highlight
254
255   bool mFocusGroupLoopEnabled:1; ///< Whether the focus movement is looped within the same focus group
256
257   bool mIsFocusIndicatorEnabled:1; ///< Whether indicator should be shown / hidden. It could be enabled when keyboard focus feature enabled and navigation keys or 'Tab' key pressed.
258
259   bool mIsWaitingKeyboardFocusChangeCommit:1; /// A flag to indicate PreFocusChangeSignal emitted but the proposed focus actor is not commited by the application yet.
260
261   FocusStack mFocusHistory; ///< Stack to contain pre-focused actor's BaseObject*
262
263   SlotDelegate< KeyboardFocusManager > mSlotDelegate;
264
265   CustomAlgorithmInterface* mCustomAlgorithmInterface; ///< The user's (application / toolkit) implementation of CustomAlgorithmInterface
266
267 };
268
269 } // namespace Internal
270
271 inline Internal::KeyboardFocusManager& GetImpl(Dali::Toolkit::KeyboardFocusManager& obj)
272 {
273   DALI_ASSERT_ALWAYS(obj);
274
275   Dali::BaseObject& handle = obj.GetBaseObject();
276
277   return static_cast<Internal::KeyboardFocusManager&>(handle);
278 }
279
280 inline const Internal::KeyboardFocusManager& GetImpl(const Dali::Toolkit::KeyboardFocusManager& obj)
281 {
282   DALI_ASSERT_ALWAYS(obj);
283
284   const Dali::BaseObject& handle = obj.GetBaseObject();
285
286   return static_cast<const Internal::KeyboardFocusManager&>(handle);
287 }
288
289 } // namespace Toolkit
290
291 } // namespace Dali
292
293 #endif // __DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H__