Merge "Resolve cases for strikethrough when using multiple <s> tags" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "keyboard-focus-manager-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
24 #include <dali/devel-api/common/singleton-service.h>
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/adaptor-framework/scene-holder.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/actors/layer.h>
29 #include <dali/public-api/animation/constraints.h>
30 #include <dali/public-api/events/key-event.h>
31 #include <dali/public-api/events/touch-event.h>
32 #include <dali/public-api/events/wheel-event.h>
33 #include <dali/public-api/object/property-map.h>
34 #include <dali/public-api/object/type-registry-helper.h>
35 #include <dali/public-api/object/type-registry.h>
36 #include <cstring> // for strcmp
37
38 // INTERNAL INCLUDES
39 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
40 #include <dali-toolkit/devel-api/controls/control-devel.h>
41 #include <dali-toolkit/devel-api/focus-manager/focus-finder.h>
42 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
43 #include <dali-toolkit/public-api/controls/control-impl.h>
44 #include <dali-toolkit/public-api/controls/control.h>
45 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
46 #include <dali-toolkit/public-api/styling/style-manager.h>
47 #include <dali/devel-api/adaptor-framework/accessibility.h>
48
49 namespace Dali
50 {
51 namespace Toolkit
52 {
53 namespace Internal
54 {
55 namespace // Unnamed namespace
56 {
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
59 #endif
60
61 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
62
63 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
64
65 BaseHandle Create()
66 {
67   BaseHandle handle = KeyboardFocusManager::Get();
68
69   if(!handle)
70   {
71     SingletonService singletonService(SingletonService::Get());
72     if(singletonService)
73     {
74       Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager(new Internal::KeyboardFocusManager());
75       singletonService.Register(typeid(manager), manager);
76       handle = manager;
77     }
78   }
79
80   return handle;
81 }
82
83 DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true)
84
85 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE)
86 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED)
87 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED)
88 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY)
89
90 DALI_TYPE_REGISTRATION_END()
91
92 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
93
94 } // unnamed namespace
95
96 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
97 {
98   Toolkit::KeyboardFocusManager manager;
99
100   SingletonService singletonService(SingletonService::Get());
101   if(singletonService)
102   {
103     // Check whether the keyboard focus manager is already created
104     Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::KeyboardFocusManager));
105     if(handle)
106     {
107       // If so, downcast the handle of singleton to keyboard focus manager
108       manager = Toolkit::KeyboardFocusManager(dynamic_cast<KeyboardFocusManager*>(handle.GetObjectPtr()));
109     }
110   }
111
112   return manager;
113 }
114
115 KeyboardFocusManager::KeyboardFocusManager()
116 : mPreFocusChangeSignal(),
117   mFocusChangedSignal(),
118   mFocusGroupChangedSignal(),
119   mFocusedActorEnterKeySignal(),
120   mCurrentFocusActor(),
121   mFocusIndicatorActor(),
122   mFocusHistory(),
123   mSlotDelegate(this),
124   mCustomAlgorithmInterface(NULL),
125   mCurrentFocusedWindow(),
126   mIsFocusIndicatorShown(UNKNOWN),
127   mEnableFocusIndicator(ENABLE),
128   mAlwaysShowIndicator(ALWAYS_SHOW),
129   mFocusGroupLoopEnabled(false),
130   mIsWaitingKeyboardFocusChangeCommit(false),
131   mClearFocusOnTouch(true),
132   mEnableDefaultAlgorithm(false)
133 {
134   // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
135
136   LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
137 }
138
139 void KeyboardFocusManager::OnAdaptorInit()
140 {
141   if(Adaptor::IsAvailable())
142   {
143     // Retrieve all the existing scene holders
144     Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
145     for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
146     {
147       (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
148       (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
149       (*iter).WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnCustomWheelEvent);
150       (*iter).WheelEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
151       Dali::Window window = DevelWindow::DownCast(*iter);
152       if(window)
153       {
154         window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
155       }
156     }
157
158     // Get notified when any new scene holder is created afterwards
159     Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
160   }
161 }
162
163 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
164 {
165   sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
166   sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
167   sceneHolder.WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnCustomWheelEvent);
168   sceneHolder.WheelEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
169   Dali::Window window = DevelWindow::DownCast(sceneHolder);
170   if(window)
171   {
172     window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
173   }
174 }
175
176 KeyboardFocusManager::~KeyboardFocusManager()
177 {
178 }
179
180 void KeyboardFocusManager::GetConfigurationFromStyleManger()
181 {
182   Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
183   if(styleManager)
184   {
185     Property::Map config   = Toolkit::DevelStyleManager::GetConfigurations(styleManager);
186     mAlwaysShowIndicator   = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
187     mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
188     mClearFocusOnTouch     = (mIsFocusIndicatorShown == SHOW) ? false : true;
189   }
190 }
191
192 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
193 {
194   DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
195
196   if(mIsFocusIndicatorShown == UNKNOWN)
197   {
198     GetConfigurationFromStyleManger();
199   }
200
201   return DoSetCurrentFocusActor(actor);
202 }
203
204 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
205 {
206   bool success = false;
207
208   // If the parent's KEYBOARD_FOCUSABLE_CHILDREN is false, it cannot have focus.
209   if(actor)
210   {
211     Actor parent = actor.GetParent();
212     while(parent)
213     {
214       if(!parent.GetProperty<bool>(DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN))
215       {
216         DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Parent Actor has KEYBOARD_FOCUSABLE_CHILDREN false,\n", __FUNCTION__, __LINE__);
217         return false;
218       }
219       parent = parent.GetParent();
220     }
221   }
222
223   if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
224   {
225     Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
226
227     if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
228     {
229       Layer rootLayer       = currentWindow.GetRootLayer();
230       mCurrentFocusedWindow = rootLayer;
231     }
232   }
233
234   Actor currentFocusedActor = GetCurrentFocusActor();
235
236   // If developer set focus on same actor, doing nothing
237   if(actor == currentFocusedActor)
238   {
239     if(!actor)
240     {
241       return false;
242     }
243     return true;
244   }
245
246   // Check whether the actor is in the stage and is keyboard focusable.
247   if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
248   {
249     if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
250     {
251       actor.Add(GetFocusIndicatorActor());
252     }
253
254     // Send notification for the change of focus actor
255     if(!mFocusChangedSignal.Empty())
256     {
257       mFocusChangedSignal.Emit(currentFocusedActor, actor);
258     }
259
260     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
261     if(currentlyFocusedControl)
262     {
263       // Do we need it to remember if it was previously DISABLED?
264       currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
265       currentlyFocusedControl.ClearKeyInputFocus();
266     }
267
268     DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
269
270     // Save the current focused actor
271     mCurrentFocusActor = actor;
272
273     bool focusedWindowFound = false;
274     for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
275     {
276       if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
277       {
278         mCurrentFocusActors[i].second = actor;
279         focusedWindowFound            = true;
280         break;
281       }
282     }
283     if(!focusedWindowFound)
284     {
285       // A new window gains the focus, so store the focused actor in that window.
286       mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
287     }
288
289     Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
290     if(newlyFocusedControl)
291     {
292       newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
293       newlyFocusedControl.SetKeyInputFocus();
294     }
295
296     // Push Current Focused Actor to FocusHistory
297     mFocusHistory.push_back(actor);
298
299     // Delete first element before add new element when Stack is full.
300     if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
301     {
302       FocusStackIterator beginPos = mFocusHistory.begin();
303       mFocusHistory.erase(beginPos);
304     }
305
306     DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
307     success = true;
308   }
309   else
310   {
311     DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
312   }
313
314   return success;
315 }
316
317 Actor KeyboardFocusManager::GetCurrentFocusActor()
318 {
319   Actor actor = mCurrentFocusActor.GetHandle();
320
321   if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
322   {
323     // If the actor has been removed from the stage, then it should not be focused
324     actor.Reset();
325     mCurrentFocusActor.Reset();
326   }
327   return actor;
328 }
329
330 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
331 {
332   Actor        actor;
333   unsigned int index;
334   for(index = 0; index < mCurrentFocusActors.size(); index++)
335   {
336     if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
337     {
338       actor = mCurrentFocusActors[index].second.GetHandle();
339       break;
340     }
341   }
342
343   if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
344   {
345     // If the actor has been removed from the window, then the window doesn't have any focused actor
346     actor.Reset();
347     mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
348   }
349
350   return actor;
351 }
352
353 Actor KeyboardFocusManager::GetCurrentFocusGroup()
354 {
355   return GetFocusGroup(GetCurrentFocusActor());
356 }
357
358 void KeyboardFocusManager::MoveFocusBackward()
359 {
360   // Find Pre Focused Actor when the list size is more than 1
361   if(mFocusHistory.size() > 1)
362   {
363     // Delete current focused actor in history
364     mFocusHistory.pop_back();
365
366     // If pre-focused actors are not on stage or deleted, remove them in stack
367     while(mFocusHistory.size() > 0)
368     {
369       // Get pre focused actor
370       Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
371
372       // Impl of Actor is not null
373       if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
374       {
375         // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
376         mFocusHistory.pop_back();
377         SetCurrentFocusActor(target);
378         break;
379       }
380       else
381       {
382         // Target is empty handle or off stage. Erase from queue
383         mFocusHistory.pop_back();
384       }
385     }
386
387     // if there is no actor which can get focus, then push current focus actor in stack again
388     if(mFocusHistory.size() == 0)
389     {
390       Actor currentFocusedActor = GetCurrentFocusActor();
391       mFocusHistory.push_back(currentFocusedActor);
392     }
393   }
394 }
395
396 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
397 {
398   Toolkit::Control control = Toolkit::Control::DownCast(actor);
399   return control && GetImplementation(control).IsKeyboardNavigationSupported();
400 }
401
402 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
403 {
404   // Get the actor's parent layout control that supports two dimensional keyboard navigation
405   Actor rootActor;
406   Actor parent;
407   if(actor)
408   {
409     Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
410     if(window)
411     {
412       rootActor = window.GetRootLayer();
413     }
414
415     parent = actor.GetParent();
416   }
417
418   while(parent && !IsLayoutControl(parent) && parent != rootActor)
419   {
420     parent = parent.GetParent();
421   }
422
423   return Toolkit::Control::DownCast(parent);
424 }
425
426 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction, const std::string& deviceName)
427 {
428   Actor currentFocusActor = GetCurrentFocusActor();
429
430   bool succeed = false;
431
432   // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
433   Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
434   while(parentLayoutControl && !succeed)
435   {
436     succeed             = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
437     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
438   }
439
440   if(!succeed)
441   {
442     Actor nextFocusableActor;
443
444     Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
445
446     // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
447     if(currentFocusControl)
448     {
449       int             actorId = -1;
450       Property::Index index   = Property::INVALID_INDEX;
451       Property::Value value;
452
453       // Find property index based upon focus direction
454       switch(direction)
455       {
456         case Toolkit::Control::KeyboardFocus::LEFT:
457         {
458           index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
459           break;
460         }
461         case Toolkit::Control::KeyboardFocus::RIGHT:
462         {
463           index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
464           break;
465         }
466         case Toolkit::Control::KeyboardFocus::UP:
467         {
468           index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
469           break;
470         }
471         case Toolkit::Control::KeyboardFocus::DOWN:
472         {
473           index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
474           break;
475         }
476         case Toolkit::Control::KeyboardFocus::CLOCKWISE:
477         {
478           index = Toolkit::DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID;
479           break;
480         }
481         case Toolkit::Control::KeyboardFocus::COUNTER_CLOCKWISE:
482         {
483           index = Toolkit::DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID;
484           break;
485         }
486         default:
487           break;
488       }
489
490       // If the focusable property is set then determine next focusable actor
491       if(index != Property::INVALID_INDEX)
492       {
493         value   = currentFocusActor.GetProperty(index);
494         actorId = value.Get<int>();
495
496         // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
497         if(actorId != -1)
498         {
499           if(currentFocusActor.GetParent())
500           {
501             nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
502           }
503
504           if(!nextFocusableActor)
505           {
506             Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
507             if(window)
508             {
509               nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
510             }
511           }
512         }
513       }
514     }
515
516     if(!nextFocusableActor)
517     {
518       // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
519       if(mCustomAlgorithmInterface)
520       {
521         mIsWaitingKeyboardFocusChangeCommit = true;
522         nextFocusableActor                  = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction, deviceName);
523         mIsWaitingKeyboardFocusChangeCommit = false;
524       }
525       else if(!mPreFocusChangeSignal.Empty())
526       {
527         // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
528         mIsWaitingKeyboardFocusChangeCommit = true;
529         nextFocusableActor                  = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
530         mIsWaitingKeyboardFocusChangeCommit = false;
531       }
532       else if (mEnableDefaultAlgorithm)
533       {
534         Layer rootLayer;
535         if (currentFocusActor)
536         {
537           // Find the window of the focused actor.
538           Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
539           if (window)
540           {
541             rootLayer = window.GetRootLayer();
542           }
543         }
544         else
545         {
546           // Searches from the currently focused window.
547           rootLayer = mCurrentFocusedWindow.GetHandle();
548         }
549         if (rootLayer)
550         {
551           // We should find it among the actors nearby.
552           nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(rootLayer, currentFocusActor, direction);
553         }
554       }
555     }
556
557     if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
558     {
559       // Whether the next focusable actor is a layout control
560       if(IsLayoutControl(nextFocusableActor))
561       {
562         // If so, move the focus inside it.
563         Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
564         succeed                        = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
565       }
566       else
567       {
568         // Otherwise, just set focus to the next focusable actor
569         succeed = SetCurrentFocusActor(nextFocusableActor);
570       }
571     }
572   }
573
574   return succeed;
575 }
576
577 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
578 {
579   // Ask the control for the next actor to focus
580   Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
581   if(nextFocusableActor)
582   {
583     if(!nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
584     {
585       // If the actor is not focusable, ask the same layout control for the next actor to focus
586       return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
587     }
588     else
589     {
590       Actor currentFocusActor   = GetCurrentFocusActor();
591       Actor committedFocusActor = nextFocusableActor;
592
593       // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
594       // Signal handler can check the proposed actor and return a different actor if it wishes.
595       if(!mPreFocusChangeSignal.Empty())
596       {
597         mIsWaitingKeyboardFocusChangeCommit = true;
598         committedFocusActor                 = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
599         mIsWaitingKeyboardFocusChangeCommit = false;
600       }
601
602       if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
603       {
604         // Whether the commited focusable actor is a layout control
605         if(IsLayoutControl(committedFocusActor))
606         {
607           // If so, move the focus inside it.
608           Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
609           return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
610         }
611         else
612         {
613           // Otherwise, just set focus to the next focusable actor
614           if(committedFocusActor == nextFocusableActor)
615           {
616             // If the application hasn't changed our proposed actor, we informs the layout control we will
617             // move the focus to what the control returns. The control might wish to perform some actions
618             // before the focus is actually moved.
619             GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
620           }
621
622           return SetCurrentFocusActor(committedFocusActor);
623         }
624       }
625       else
626       {
627         return false;
628       }
629     }
630   }
631   else
632   {
633     // No more actor can be focused in the given direction within the same layout control.
634     return false;
635   }
636 }
637
638 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
639 {
640   bool succeed = false;
641
642   // Get the parent layout control of the current focus group
643   Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
644
645   while(parentLayoutControl && !succeed)
646   {
647     // If the current focus group has a parent layout control, we can probably automatically
648     // move the focus to the next focus group in the forward or backward direction.
649     Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
650     succeed                                              = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
651     parentLayoutControl                                  = GetParentLayoutControl(parentLayoutControl);
652   }
653
654   if(!mFocusGroupChangedSignal.Empty())
655   {
656     // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
657     mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
658   }
659
660   return succeed;
661 }
662
663 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
664 {
665   if(actor)
666   {
667     Toolkit::Control control = Toolkit::Control::DownCast(actor);
668     if(control)
669     {
670       // Notify the control that enter has been pressed on it.
671       GetImplementation(control).KeyboardEnter();
672     }
673
674     // Send a notification for the actor.
675     if(!mFocusedActorEnterKeySignal.Empty())
676     {
677       mFocusedActorEnterKeySignal.Emit(actor);
678     }
679   }
680 }
681
682 void KeyboardFocusManager::ClearFocus()
683 {
684   Actor actor = GetCurrentFocusActor();
685   if(actor)
686   {
687     if(mFocusIndicatorActor)
688     {
689       actor.Remove(mFocusIndicatorActor);
690     }
691
692     // Send notification for the change of focus actor
693     if(!mFocusChangedSignal.Empty())
694     {
695       mFocusChangedSignal.Emit(actor, Actor());
696     }
697
698     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
699     if(currentlyFocusedControl)
700     {
701       currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
702       currentlyFocusedControl.ClearKeyInputFocus();
703     }
704   }
705
706   mCurrentFocusActor.Reset();
707   mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
708 }
709
710 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
711 {
712   mFocusGroupLoopEnabled = enabled;
713 }
714
715 bool KeyboardFocusManager::GetFocusGroupLoop() const
716 {
717   return mFocusGroupLoopEnabled;
718 }
719
720 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
721 {
722   if(actor)
723   {
724     // Create/Set focus group property.
725     actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
726   }
727 }
728
729 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
730 {
731   // Check whether the actor is a focus group
732   bool isFocusGroup = false;
733
734   if(actor)
735   {
736     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
737     if(propertyIsFocusGroup != Property::INVALID_INDEX)
738     {
739       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
740     }
741   }
742
743   return isFocusGroup;
744 }
745
746 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
747 {
748   // Go through the actor's hierarchy to check which focus group the actor belongs to
749   while(actor && !IsFocusGroup(actor))
750   {
751     actor = actor.GetParent();
752   }
753
754   return actor;
755 }
756
757 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
758 {
759   if(mFocusIndicatorActor != indicator)
760   {
761     Actor currentFocusActor = GetCurrentFocusActor();
762     if(currentFocusActor)
763     {
764       // The new focus indicator should be added to the current focused actor immediately
765       if(mFocusIndicatorActor)
766       {
767         currentFocusActor.Remove(mFocusIndicatorActor);
768       }
769
770       if(indicator)
771       {
772         currentFocusActor.Add(indicator);
773       }
774     }
775
776     mFocusIndicatorActor = indicator;
777   }
778 }
779
780 Actor KeyboardFocusManager::GetFocusIndicatorActor()
781 {
782   if(!mFocusIndicatorActor)
783   {
784     // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
785     const std::string imageDirPath = AssetManager::GetDaliImagePath();
786     mFocusIndicatorActor           = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
787
788     // Apply size constraint to the focus indicator
789     mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
790   }
791
792   mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
793   mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
794   mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
795
796   return mFocusIndicatorActor;
797 }
798
799 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
800 {
801   const std::string& keyName = event.GetKeyName();
802   const std::string& deviceName = event.GetDeviceName();
803
804   if(mIsFocusIndicatorShown == UNKNOWN)
805   {
806     GetConfigurationFromStyleManger();
807   }
808
809   bool isFocusStartableKey = false;
810
811   if(event.GetState() == KeyEvent::DOWN)
812   {
813     if(keyName == "Left")
814     {
815       if(mIsFocusIndicatorShown == HIDE)
816       {
817         // Show focus indicator
818         mIsFocusIndicatorShown = SHOW;
819       }
820       else
821       {
822         // Move the focus towards left
823         MoveFocus(Toolkit::Control::KeyboardFocus::LEFT, deviceName);
824       }
825
826       isFocusStartableKey = true;
827     }
828     else if(keyName == "Right")
829     {
830       if(mIsFocusIndicatorShown == HIDE)
831       {
832         // Show focus indicator
833         mIsFocusIndicatorShown = SHOW;
834       }
835       else
836       {
837         // Move the focus towards right
838         MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT, deviceName);
839       }
840
841       isFocusStartableKey = true;
842     }
843     else if(keyName == "Up")
844     {
845       if(mIsFocusIndicatorShown == HIDE)
846       {
847         // Show focus indicator
848         mIsFocusIndicatorShown = SHOW;
849       }
850       else
851       {
852         // Move the focus towards up
853         MoveFocus(Toolkit::Control::KeyboardFocus::UP, deviceName);
854       }
855
856       isFocusStartableKey = true;
857     }
858     else if(keyName == "Down")
859     {
860       if(mIsFocusIndicatorShown == HIDE)
861       {
862         // Show focus indicator
863         mIsFocusIndicatorShown = SHOW;
864       }
865       else
866       {
867         // Move the focus towards down
868         MoveFocus(Toolkit::Control::KeyboardFocus::DOWN, deviceName);
869       }
870
871       isFocusStartableKey = true;
872     }
873     else if(keyName == "Prior")
874     {
875       if(mIsFocusIndicatorShown == HIDE)
876       {
877         // Show focus indicator
878         mIsFocusIndicatorShown = SHOW;
879       }
880       else
881       {
882         // Move the focus towards the previous page
883         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP, deviceName);
884       }
885
886       isFocusStartableKey = true;
887     }
888     else if(keyName == "Next")
889     {
890       if(mIsFocusIndicatorShown == HIDE)
891       {
892         // Show focus indicator
893         mIsFocusIndicatorShown = SHOW;
894       }
895       else
896       {
897         // Move the focus towards the next page
898         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN, deviceName);
899       }
900
901       isFocusStartableKey = true;
902     }
903     else if(keyName == "Tab")
904     {
905       if(mIsFocusIndicatorShown == HIDE)
906       {
907         // Show focus indicator
908         mIsFocusIndicatorShown = SHOW;
909       }
910       else
911       {
912         // "Tab" key changes the focus group in the forward direction and
913         // "Shift-Tab" key changes it in the backward direction.
914         if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
915         {
916           // If the focus group is not changed, Move the focus towards forward, "Shift-Tap" key moves the focus towards backward.
917           MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::BACKWARD : Toolkit::Control::KeyboardFocus::FORWARD, deviceName);
918         }
919       }
920
921       isFocusStartableKey = true;
922     }
923     else if(keyName == "space")
924     {
925       if(mIsFocusIndicatorShown == HIDE)
926       {
927         // Show focus indicator
928         mIsFocusIndicatorShown = SHOW;
929       }
930
931       isFocusStartableKey = true;
932     }
933     else if(keyName == "")
934     {
935       // Check the fake key event for evas-plugin case
936       if(mIsFocusIndicatorShown == HIDE)
937       {
938         // Show focus indicator
939         mIsFocusIndicatorShown = SHOW;
940       }
941
942       isFocusStartableKey = true;
943     }
944     else if(keyName == "Backspace")
945     {
946       // Emit signal to go back to the previous view???
947     }
948     else if(keyName == "Escape")
949     {
950     }
951   }
952   else if(event.GetState() == KeyEvent::UP)
953   {
954     if(keyName == "Return")
955     {
956       if(mIsFocusIndicatorShown == HIDE)
957       {
958         // Show focus indicator
959         mIsFocusIndicatorShown = SHOW;
960       }
961       else
962       {
963         // The focused actor has enter pressed on it
964         Actor actor = GetCurrentFocusActor();
965         if(actor)
966         {
967           DoKeyboardEnter(actor);
968         }
969       }
970
971       isFocusStartableKey = true;
972     }
973   }
974
975   if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
976   {
977     Actor actor = GetCurrentFocusActor();
978     if(actor)
979     {
980       if(mEnableFocusIndicator == ENABLE)
981       {
982         // Make sure the focused actor is highlighted
983         actor.Add(GetFocusIndicatorActor());
984       }
985     }
986     else
987     {
988       // No actor is focused but keyboard focus is activated by the key press
989       // Let's try to move the initial focus
990       MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT, deviceName);
991     }
992   }
993 }
994
995 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
996 {
997   // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
998   // Try to load configuration.
999   if(mIsFocusIndicatorShown == UNKNOWN)
1000   {
1001     GetConfigurationFromStyleManger();
1002   }
1003
1004   // Clear the focus when user touch the screen.
1005   // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
1006   if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
1007   {
1008     // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
1009     if(mClearFocusOnTouch)
1010     {
1011       ClearFocus();
1012     }
1013
1014     // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
1015     Actor hitActor = touch.GetHitActor(0);
1016     if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
1017     {
1018       SetCurrentFocusActor(hitActor);
1019     }
1020   }
1021 }
1022
1023 void KeyboardFocusManager::OnWheelEvent(const WheelEvent& event)
1024 {
1025   if(event.GetType() == Dali::WheelEvent::CUSTOM_WHEEL)
1026   {
1027     Toolkit::Control::KeyboardFocus::Direction direction = (event.GetDelta() > 0) ? Toolkit::Control::KeyboardFocus::CLOCKWISE : Toolkit::Control::KeyboardFocus::COUNTER_CLOCKWISE;
1028     // Move the focus
1029     MoveFocus(direction);
1030   }
1031 }
1032
1033 bool KeyboardFocusManager::OnCustomWheelEvent(const WheelEvent& event)
1034 {
1035   bool consumed = false;
1036   Actor actor = GetCurrentFocusActor();
1037   if(actor)
1038   {
1039     // Notify the actor about the wheel event
1040     consumed = EmitCustomWheelSignals(actor, event);
1041   }
1042   return consumed;
1043 }
1044
1045 bool KeyboardFocusManager::EmitCustomWheelSignals(Actor actor, const WheelEvent& event)
1046 {
1047   bool consumed = false;
1048
1049   if(actor)
1050   {
1051     Dali::Actor oldParent(actor.GetParent());
1052
1053     // Only do the conversion and emit the signal if the actor's wheel signal has connections.
1054     if(!actor.WheelEventSignal().Empty())
1055     {
1056       // Emit the signal to the parent
1057       consumed = actor.WheelEventSignal().Emit(actor, event);
1058     }
1059     // if actor doesn't consume WheelEvent, give WheelEvent to its parent.
1060     if(!consumed)
1061     {
1062       // The actor may have been removed/reparented during the signal callbacks.
1063       Dali::Actor parent = actor.GetParent();
1064
1065       if(parent &&
1066          (parent == oldParent))
1067       {
1068         consumed = EmitCustomWheelSignals(parent, event);
1069       }
1070     }
1071   }
1072
1073   return consumed;
1074 }
1075
1076 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
1077 {
1078   if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
1079   {
1080     // Change Current Focused Window
1081     Layer rootLayer       = window.GetRootLayer();
1082     mCurrentFocusedWindow = rootLayer;
1083
1084     // Get Current Focused Actor from window
1085     Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1086     SetCurrentFocusActor(currentFocusedActor);
1087
1088     if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
1089     {
1090       // Make sure the focused actor is highlighted
1091       currentFocusedActor.Add(GetFocusIndicatorActor());
1092       mIsFocusIndicatorShown = SHOW;
1093     }
1094   }
1095 }
1096
1097 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1098 {
1099   return mPreFocusChangeSignal;
1100 }
1101
1102 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1103 {
1104   return mFocusChangedSignal;
1105 }
1106
1107 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1108 {
1109   return mFocusGroupChangedSignal;
1110 }
1111
1112 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1113 {
1114   return mFocusedActorEnterKeySignal;
1115 }
1116
1117 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1118 {
1119   Dali::BaseHandle handle(object);
1120
1121   bool                  connected(true);
1122   KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1123
1124   if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1125   {
1126     manager->PreFocusChangeSignal().Connect(tracker, functor);
1127   }
1128   else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1129   {
1130     manager->FocusChangedSignal().Connect(tracker, functor);
1131   }
1132   else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1133   {
1134     manager->FocusGroupChangedSignal().Connect(tracker, functor);
1135   }
1136   else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1137   {
1138     manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1139   }
1140   else
1141   {
1142     // signalName does not match any signal
1143     connected = false;
1144   }
1145
1146   return connected;
1147 }
1148
1149 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1150 {
1151   mCustomAlgorithmInterface = &interface;
1152 }
1153
1154 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1155 {
1156   if(!enable && mFocusIndicatorActor)
1157   {
1158     mFocusIndicatorActor.Unparent();
1159   }
1160
1161   mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1162 }
1163
1164 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1165 {
1166   return (mEnableFocusIndicator == ENABLE);
1167 }
1168
1169 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1170 {
1171   mEnableDefaultAlgorithm = enable;
1172 }
1173
1174 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1175 {
1176   return mEnableDefaultAlgorithm;
1177 }
1178
1179 } // namespace Internal
1180
1181 } // namespace Toolkit
1182
1183 } // namespace Dali