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