Support scene holder instead of window in keyinput/keyboard focus
[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) 2019 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 #include <dali/public-api/object/weak-handle.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
28 #include <dali-toolkit/devel-api/focus-manager/keyboard-focus-manager-devel.h>
29
30 namespace Dali
31 {
32
33 namespace Integration
34 {
35
36 class SceneHolder;
37
38 } // namespace Integration
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45
46 /**
47  * @copydoc Toolkit::KeyboardFocusManager
48  */
49 class KeyboardFocusManager : public Dali::BaseObject, public ConnectionTracker
50 {
51 public:
52
53   typedef Toolkit::DevelKeyboardFocusManager::CustomAlgorithmInterface CustomAlgorithmInterface;
54
55   /**
56    * @copydoc Toolkit::KeyboardFocusManager::Get
57    */
58   static Toolkit::KeyboardFocusManager Get();
59
60   /**
61    * Construct a new KeyboardFocusManager.
62    */
63   KeyboardFocusManager();
64
65   /**
66    * @copydoc Toolkit::KeyboardFocusManager::SetCurrentFocusActor
67    */
68   bool SetCurrentFocusActor(Actor actor);
69
70   /**
71    * @copydoc Toolkit::KeyboardFocusManager::GetCurrentFocusActor
72    */
73   Actor GetCurrentFocusActor();
74
75   /**
76    * @copydoc Toolkit::KeyboardFocusManager::MoveFocus
77    */
78   bool MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction);
79
80   /**
81    * @copydoc Toolkit::KeyboardFocusManager::ClearFocus
82    */
83   void ClearFocus();
84
85   /**
86    * @copydoc Toolkit::KeyboardFocusManager::SetAsFocusGroup
87    */
88   void SetAsFocusGroup(Actor actor, bool isFocusGroup);
89
90   /**
91    * @copydoc Toolkit::KeyboardFocusManager::IsFocusGroup
92    */
93   bool IsFocusGroup(Actor actor) const;
94
95   /**
96    * @copydoc Toolkit::KeyboardFocusManager::GetFocusGroup
97    */
98   Actor GetFocusGroup(Actor actor);
99
100   /**
101    * @copydoc Toolkit::KeyboardFocusManager::SetFocusGroupLoop
102    */
103   void SetFocusGroupLoop(bool enabled);
104
105   /**
106    * @copydoc Toolkit::KeyboardFocusManager::GetFocusGroupLoop
107    */
108   bool GetFocusGroupLoop() const;
109
110   /**
111    * @copydoc Toolkit::KeyboardFocusManager::SetFocusIndicatorActor
112    */
113   void SetFocusIndicatorActor(Actor indicator);
114
115   /**
116    * @copydoc Toolkit::KeyboardFocusManager::GetFocusIndicatorActor
117    */
118   Actor GetFocusIndicatorActor();
119
120   /**
121    * Move current focus to backward
122    */
123   void MoveFocusBackward();
124
125   /**
126    * @copydoc Toolkit::DevelKeyboardFocusManager::SetCustomAlgorithm
127    */
128   void SetCustomAlgorithm(CustomAlgorithmInterface& interface);
129
130   /**
131    * @copydoc Toolkit::DevelKeyboardFocusManager::UseFocusIndicator
132    */
133   void EnableFocusIndicator(bool enable);
134
135   /**
136    * @copydoc Toolkit::DevelKeyboardFocusManager::UseFocusIndicator
137    */
138   bool IsFocusIndicatorEnabled() const;
139
140 public:
141
142   /**
143    * @copydoc Toolkit::KeyboardFocusManager::PreFocusChangeSignal()
144    */
145   Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& PreFocusChangeSignal();
146
147   /**
148    * @copydoc Toolkit::KeyboardFocusManager::FocusChangedSignal()
149    */
150   Toolkit::KeyboardFocusManager::FocusChangedSignalType& FocusChangedSignal();
151
152   /**
153    * @copydoc Toolkit::KeyboardFocusManager::FocusGroupChangedSignal()
154    */
155   Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& FocusGroupChangedSignal();
156
157   /**
158    * @copydoc Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignal()
159    */
160   Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& FocusedActorEnterKeySignal();
161
162   /**
163    * Connects a callback function with the object's signals.
164    * @param[in] object The object providing the signal.
165    * @param[in] tracker Used to disconnect the signal.
166    * @param[in] signalName The signal to connect to.
167    * @param[in] functor A newly allocated FunctorDelegate.
168    * @return True if the signal was connected.
169    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
170    */
171   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
172
173 protected:
174
175   /**
176    * Destructor
177    */
178   virtual ~KeyboardFocusManager();
179
180 private:
181
182   typedef std::vector< WeakHandle< Actor > > FocusStack; ///< Define Dali::Vector< Dali::BaseObject* > as FocusStack to contain focus history
183   typedef FocusStack::iterator FocusStackIterator; ///< Define FocusStack::Iterator as FocusStackIterator to navigate FocusStack
184
185   /**
186    * This will be called when the adaptor is initialized
187    */
188   void OnAdaptorInit();
189
190   /**
191    * This will be called when a new scene holder is created
192    * @param sceneHolder The new scene holder
193    */
194   void OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder );
195
196   /**
197    * Get configuration from StyleManager.
198    */
199   void GetConfigurationFromStyleManger();
200
201   /**
202    * Get the focus group of current focused actor.
203    * @pre The FocusManager has been initialized.
204    * @return A handle to the parent of the current focused actor which is a focus group,
205    * or an empty handle if no actor is focused.
206    */
207   Actor GetCurrentFocusGroup();
208
209   /**
210    * Move the focus to the specified actor and send notification for the focus change.
211    * @param actor The actor to be queried
212    * @return Whether the focus is successful or not
213    */
214   bool DoSetCurrentFocusActor(Actor actor);
215
216   /**
217    * Move the focus to the next actor towards the specified direction within the layout control
218    * @param control The layout control to move the focus in
219    * @param actor The current focused actor
220    * @param direction The direction of focus movement
221    * @return Whether the focus is successful or not
222    */
223   bool DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction);
224
225   /**
226    * Move the focus to the first focusable actor in the next focus group in the forward
227    * or backward direction. The "Tab" key changes the focus group in the forward direction
228    * and the "Shift-Tab" key changes it in the backward direction.
229    * @param forward Whether the direction of focus group change is forward or backward
230    * @return Whether the focus group change is successful or not
231    */
232   bool DoMoveFocusToNextFocusGroup(bool forward);
233
234   /**
235    * Enter has been pressed on the actor. If the actor is control, call the OnKeybaordEnter virtual function.
236    * This function will emit FocusedActorEnterKeySignal.
237    * @param actor The actor to notify
238    */
239   void DoKeyboardEnter( Actor actor );
240
241   /**
242    * Check whether the actor is a layout control that supports two dimensional keyboard navigation.
243    * The layout control needs to internally set the focus order for the child actor and be able to
244    * tell KeyboardFocusmanager the next focusable actor in the given direction.
245    * @pre The KeyboardFocusManager has been initialized.
246    * @pre The Actor has been initialized.
247    * @param actor The actor to be checked.
248    * @return Whether the actor is a layout control or not.
249    */
250   bool IsLayoutControl(Actor actor) const;
251
252   /**
253    * Returns the closest ancestor of the given actor that is a layout control.
254    * @param actor The actor to be checked for its parent layout control
255    * @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
256    */
257  Toolkit::Control GetParentLayoutControl(Actor actor) const;
258
259   /**
260    * Callback for the key event when no actor in the stage has gained the key input focus
261    * @param[in] event The KeyEvent event.
262    */
263   void OnKeyEvent(const KeyEvent& event);
264
265   /**
266    * Callback for the touch event when the screen is touched and when the touch ends
267    * (i.e. the down & up touch events only).
268    * @param[in] touch The touch information
269    */
270   void OnTouch( const TouchData& touch );
271
272 private:
273
274   // Undefined
275   KeyboardFocusManager(const KeyboardFocusManager&);
276
277   KeyboardFocusManager& operator=(const KeyboardFocusManager& rhs);
278
279 private:
280
281   Toolkit::KeyboardFocusManager::PreFocusChangeSignalType mPreFocusChangeSignal; ///< The signal to notify the focus will be changed
282   Toolkit::KeyboardFocusManager::FocusChangedSignalType mFocusChangedSignal; ///< The signal to notify the focus change
283   Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType mFocusGroupChangedSignal; ///< The signal to notify the focus group change
284   Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType mFocusedActorEnterKeySignal; ///< The signal to notify that enter has been pressed on the focused actor
285
286   WeakHandle< Actor > mCurrentFocusActor; ///< A weak handle to the current focused actor
287
288   Actor mFocusIndicatorActor; ///< The focus indicator actor shared by all the keyboard focusable actors for highlight
289
290   int mIsFocusIndicatorShown; ///< Whether indicator should be shown / hidden when getting focus. It could be enabled when keyboard focus feature is enabled and navigation keys or 'Tab' key are pressed.
291
292   bool mFocusGroupLoopEnabled:1; ///< Whether the focus movement is looped within the same focus group
293
294   bool mIsWaitingKeyboardFocusChangeCommit:1; /// A flag to indicate PreFocusChangeSignal emitted but the proposed focus actor is not commited by the application yet.
295
296   bool mClearFocusOnTouch:1; ///< Whether clear focus on touch.
297
298   bool mEnableFocusIndicator;  ///< Whether use focus indicator
299
300   bool mAlwaysShowIndicator; ///< Whether always show indicator. If true, the indicator would be directly shown when focused.
301
302   FocusStack mFocusHistory; ///< Stack to contain pre-focused actor's BaseObject*
303
304   SlotDelegate< KeyboardFocusManager > mSlotDelegate;
305
306   CustomAlgorithmInterface* mCustomAlgorithmInterface; ///< The user's (application / toolkit) implementation of CustomAlgorithmInterface
307
308 };
309
310 } // namespace Internal
311
312 inline Internal::KeyboardFocusManager& GetImpl(Dali::Toolkit::KeyboardFocusManager& obj)
313 {
314   DALI_ASSERT_ALWAYS(obj);
315
316   Dali::BaseObject& handle = obj.GetBaseObject();
317
318   return static_cast<Internal::KeyboardFocusManager&>(handle);
319 }
320
321 inline const Internal::KeyboardFocusManager& GetImpl(const Dali::Toolkit::KeyboardFocusManager& obj)
322 {
323   DALI_ASSERT_ALWAYS(obj);
324
325   const Dali::BaseObject& handle = obj.GetBaseObject();
326
327   return static_cast<const Internal::KeyboardFocusManager&>(handle);
328 }
329
330 } // namespace Toolkit
331
332 } // namespace Dali
333
334 #endif // DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H