[Tizen] Do nothing when setting focus on same actor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.cpp
1 /*
2  * Copyright (c) 2017 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 <cstring> // for strcmp
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/devel-api/adaptor-framework/accessibility-adaptor.h>
25 #include <dali/devel-api/adaptor-framework/singleton-service.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/events/key-event.h>
29 #include <dali/public-api/events/touch-data.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali/public-api/images/resource-image.h>
33 #include <dali/integration-api/debug.h>
34
35 // INTERNAL INCLUDES
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/controls/control-impl.h>
38 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
39 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
40 #include <dali-toolkit/devel-api/controls/control-devel.h>
41
42 namespace Dali
43 {
44
45 namespace Toolkit
46 {
47
48 namespace Internal
49 {
50
51 namespace // Unnamed namespace
52 {
53
54 #if defined(DEBUG_ENABLED)
55 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
56 #endif
57
58 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
59
60 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.9.png";
61
62 BaseHandle Create()
63 {
64   BaseHandle handle = KeyboardFocusManager::Get();
65
66   if ( !handle )
67   {
68     SingletonService singletonService( SingletonService::Get() );
69     if ( singletonService )
70     {
71       Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
72       singletonService.Register( typeid( manager ), manager );
73       handle = manager;
74     }
75   }
76
77   return handle;
78 }
79
80 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
81
82 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange",           SIGNAL_PRE_FOCUS_CHANGE )
83 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged",             SIGNAL_FOCUS_CHANGED )
84 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged",        SIGNAL_FOCUS_GROUP_CHANGED )
85 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey",     SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
86
87 DALI_TYPE_REGISTRATION_END()
88
89 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
90
91 } // unnamed namespace
92
93 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
94 {
95   Toolkit::KeyboardFocusManager manager;
96
97   SingletonService singletonService( SingletonService::Get() );
98   if ( singletonService )
99   {
100     // Check whether the keyboard focus manager is already created
101     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
102     if(handle)
103     {
104       // If so, downcast the handle of singleton to keyboard focus manager
105       manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
106     }
107   }
108
109   return manager;
110 }
111
112 KeyboardFocusManager::KeyboardFocusManager()
113 : mPreFocusChangeSignal(),
114   mFocusChangedSignal(),
115   mFocusGroupChangedSignal(),
116   mFocusedActorEnterKeySignal(),
117   mCurrentFocusActor( 0 ),
118   mFocusIndicatorActor(),
119   mFocusGroupLoopEnabled( false ),
120   mIsFocusIndicatorEnabled( true ),
121   mIsWaitingKeyboardFocusChangeCommit( false ),
122   mFocusHistory(),
123   mSlotDelegate( this ),
124   mCustomAlgorithmInterface(NULL)
125 {
126   // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorEnabled.
127   Stage::GetCurrent().KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
128   Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
129 }
130
131 KeyboardFocusManager::~KeyboardFocusManager()
132 {
133 }
134
135 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
136 {
137   DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
138
139   return DoSetCurrentFocusActor( actor );
140 }
141
142 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
143 {
144   bool success = false;
145
146   Actor currentFocusedActor = GetCurrentFocusActor();
147
148   // If developer set focus on same actor, doing nothing
149   if( actor == currentFocusedActor )
150   {
151     if( !actor )
152     {
153       return false;
154     }
155     return true;
156   }
157
158   // Check whether the actor is in the stage and is keyboard focusable.
159   if( actor && actor.IsKeyboardFocusable() )
160   {
161     if( mIsFocusIndicatorEnabled )
162     {
163       actor.Add( GetFocusIndicatorActor() );
164     }
165
166     // Send notification for the change of focus actor
167     if( !mFocusChangedSignal.Empty() )
168     {
169       mFocusChangedSignal.Emit(currentFocusedActor, actor);
170     }
171
172     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
173     if( currentlyFocusedControl )
174     {
175       // Do we need it to remember if it was previously DISABLED?
176       currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
177       currentlyFocusedControl.ClearKeyInputFocus();
178     }
179
180     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
181
182     // Save the current focused actor
183     mCurrentFocusActor = actor.GetId();
184
185     Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
186     if( newlyFocusedControl )
187     {
188       newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
189       newlyFocusedControl.SetKeyInputFocus();
190     }
191
192     // Push Current Focused Actor to FocusHistory
193     mFocusHistory.PushBack( &actor.GetBaseObject() );
194
195     // Delete first element before add new element when Stack is full.
196     if( mFocusHistory.Count() > MAX_HISTORY_AMOUNT )
197     {
198        FocusStackIterator beginPos = mFocusHistory.Begin();
199        mFocusHistory.Erase( beginPos );
200     }
201
202     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
203     success = true;
204   }
205   else
206   {
207     DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
208   }
209
210   return success;
211 }
212
213 Actor KeyboardFocusManager::GetCurrentFocusActor()
214 {
215   Actor rootActor = Stage::GetCurrent().GetRootLayer();
216   return rootActor.FindChildById(mCurrentFocusActor);
217 }
218
219 Actor KeyboardFocusManager::GetCurrentFocusGroup()
220 {
221   return GetFocusGroup(GetCurrentFocusActor());
222 }
223
224 void KeyboardFocusManager::MoveFocusBackward()
225 {
226   // Find Pre Focused Actor when the list size is more than 1
227   if( mFocusHistory.Count() > 1 )
228   {
229     // Delete current focused actor in history
230     FocusStackIterator endPos = mFocusHistory.End();
231     endPos = mFocusHistory.Erase( --endPos );
232
233     // If pre-focused actors are not on stage, remove them in stack
234     while( !Dali::Actor::DownCast(BaseHandle(mFocusHistory[ mFocusHistory.Count() - 1 ])).OnStage() )
235     {
236       endPos = mFocusHistory.Erase( --endPos );
237     }
238
239     // Get pre focused actor
240     BaseObject* object = mFocusHistory[ mFocusHistory.Count() - 1 ];
241     BaseHandle handle( object );
242     Actor preFocusedActor = Dali::Actor::DownCast( handle );
243
244     // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
245     mFocusHistory.Erase( --endPos );
246
247     SetCurrentFocusActor( preFocusedActor );
248  }
249 }
250
251 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
252 {
253   Toolkit::Control control = Toolkit::Control::DownCast(actor);
254   return control && GetImplementation( control ).IsKeyboardNavigationSupported();
255 }
256
257 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
258 {
259   // Get the actor's parent layout control that supports two dimensional keyboard navigation
260   Actor rootActor = Stage::GetCurrent().GetRootLayer();
261   Actor parent;
262   if(actor)
263   {
264     parent = actor.GetParent();
265   }
266
267   while( parent && !IsLayoutControl(parent) && parent != rootActor )
268   {
269     parent = parent.GetParent();
270   }
271
272   return Toolkit::Control::DownCast(parent);
273 }
274
275 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
276 {
277   Actor currentFocusActor = GetCurrentFocusActor();
278
279   bool succeed = false;
280
281   // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
282   Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
283   while( parentLayoutControl && !succeed )
284   {
285     succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
286     parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
287   }
288
289   if( !succeed )
290   {
291     Actor nextFocusableActor;
292
293     Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
294
295     // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
296     if( currentFocusControl )
297     {
298       int actorId = -1;
299       Property::Index index = Property::INVALID_INDEX;
300       Property::Value value;
301
302       // Find property index based upon focus direction
303       switch ( direction )
304       {
305         case Toolkit::Control::KeyboardFocus::LEFT:
306         {
307           index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
308           break;
309         }
310         case Toolkit::Control::KeyboardFocus::RIGHT:
311         {
312           index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
313           break;
314         }
315         case Toolkit::Control::KeyboardFocus::UP:
316         {
317           index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
318           break;
319         }
320         case Toolkit::Control::KeyboardFocus::DOWN:
321         {
322           index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
323           break;
324         }
325         default:
326           break;
327       }
328
329       // If the focusable property is set then determine next focusable actor
330       if( index != Property::INVALID_INDEX)
331       {
332         value = currentFocusActor.GetProperty( index );
333         actorId = value.Get<int>();
334
335         // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
336         if( actorId != -1 )
337         {
338           if( currentFocusActor.GetParent() )
339           {
340             nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
341           }
342
343           if( !nextFocusableActor )
344           {
345             nextFocusableActor = Stage::GetCurrent().GetRootLayer().FindChildById( actorId );
346           }
347         }
348       }
349     }
350
351     if( !nextFocusableActor )
352     {
353       // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
354       if( mCustomAlgorithmInterface )
355       {
356         mIsWaitingKeyboardFocusChangeCommit = true;
357         nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
358         mIsWaitingKeyboardFocusChangeCommit = false;
359       }
360       else if( !mPreFocusChangeSignal.Empty() )
361       {
362         // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
363         mIsWaitingKeyboardFocusChangeCommit = true;
364         nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
365         mIsWaitingKeyboardFocusChangeCommit = false;
366       }
367     }
368
369     if( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
370     {
371       // Whether the next focusable actor is a layout control
372       if( IsLayoutControl( nextFocusableActor ) )
373       {
374         // If so, move the focus inside it.
375         Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
376         succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
377       }
378       else
379       {
380         // Otherwise, just set focus to the next focusable actor
381         succeed = SetCurrentFocusActor( nextFocusableActor );
382       }
383     }
384   }
385
386   return succeed;
387 }
388
389 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
390 {
391   // Ask the control for the next actor to focus
392   Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
393   if(nextFocusableActor)
394   {
395     if(!nextFocusableActor.IsKeyboardFocusable())
396     {
397       // If the actor is not focusable, ask the same layout control for the next actor to focus
398       return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
399     }
400     else
401     {
402       Actor currentFocusActor = GetCurrentFocusActor();
403       Actor committedFocusActor = nextFocusableActor;
404
405       // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
406       // Signal handler can check the proposed actor and return a different actor if it wishes.
407       if( !mPreFocusChangeSignal.Empty() )
408       {
409         mIsWaitingKeyboardFocusChangeCommit = true;
410         committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
411         mIsWaitingKeyboardFocusChangeCommit = false;
412       }
413
414       if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
415       {
416         // Whether the commited focusable actor is a layout control
417         if(IsLayoutControl(committedFocusActor))
418         {
419           // If so, move the focus inside it.
420           Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
421           return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
422         }
423         else
424         {
425           // Otherwise, just set focus to the next focusable actor
426           if(committedFocusActor == nextFocusableActor)
427           {
428             // If the application hasn't changed our proposed actor, we informs the layout control we will
429             // move the focus to what the control returns. The control might wish to perform some actions
430             // before the focus is actually moved.
431             GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
432           }
433
434           return SetCurrentFocusActor(committedFocusActor);
435         }
436       }
437       else
438       {
439         return false;
440       }
441     }
442   }
443   else
444   {
445     // No more actor can be focused in the given direction within the same layout control.
446     return false;
447   }
448 }
449
450 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
451 {
452   bool succeed = false;
453
454   // Get the parent layout control of the current focus group
455   Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
456
457   while(parentLayoutControl && !succeed)
458   {
459     // If the current focus group has a parent layout control, we can probably automatically
460     // move the focus to the next focus group in the forward or backward direction.
461     Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
462     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
463     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
464   }
465
466   if(!mFocusGroupChangedSignal.Empty())
467   {
468     // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
469     mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
470   }
471
472   return succeed;
473 }
474
475 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
476 {
477   if( actor )
478   {
479     Toolkit::Control control = Toolkit::Control::DownCast( actor );
480     if( control )
481     {
482       // Notify the control that enter has been pressed on it.
483       GetImplementation( control ).KeyboardEnter();
484     }
485
486     // Send a notification for the actor.
487     if( !mFocusedActorEnterKeySignal.Empty() )
488     {
489       mFocusedActorEnterKeySignal.Emit( actor );
490     }
491   }
492 }
493
494 void KeyboardFocusManager::ClearFocus()
495 {
496   Actor actor = GetCurrentFocusActor();
497   if( actor )
498   {
499     if( mFocusIndicatorActor )
500     {
501       actor.Remove( mFocusIndicatorActor );
502     }
503
504     // Send notification for the change of focus actor
505     if( !mFocusChangedSignal.Empty() )
506     {
507       mFocusChangedSignal.Emit( actor, Actor() );
508     }
509
510     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
511     if( currentlyFocusedControl )
512     {
513       currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
514       currentlyFocusedControl.ClearKeyInputFocus();
515     }
516   }
517
518   mCurrentFocusActor = 0;
519   mIsFocusIndicatorEnabled = false;
520 }
521
522 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
523 {
524   mFocusGroupLoopEnabled = enabled;
525 }
526
527 bool KeyboardFocusManager::GetFocusGroupLoop() const
528 {
529   return mFocusGroupLoopEnabled;
530 }
531
532 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
533 {
534   if(actor)
535   {
536     // Create/Set focus group property.
537     actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
538   }
539 }
540
541 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
542 {
543   // Check whether the actor is a focus group
544   bool isFocusGroup = false;
545
546   if(actor)
547   {
548     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
549     if(propertyIsFocusGroup != Property::INVALID_INDEX)
550     {
551       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
552     }
553   }
554
555   return isFocusGroup;
556 }
557
558 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
559 {
560   // Go through the actor's hierarchy to check which focus group the actor belongs to
561   while (actor && !IsFocusGroup(actor))
562   {
563     actor = actor.GetParent();
564   }
565
566   return actor;
567 }
568
569 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
570 {
571   if(mFocusIndicatorActor != indicator)
572   {
573     Actor currentFocusActor = GetCurrentFocusActor();
574     if(currentFocusActor)
575     {
576       // The new focus indicator should be added to the current focused actor immediately
577       if(mFocusIndicatorActor)
578       {
579         currentFocusActor.Remove(mFocusIndicatorActor);
580       }
581
582       if(indicator)
583       {
584         currentFocusActor.Add(indicator);
585       }
586     }
587
588     mFocusIndicatorActor = indicator;
589   }
590 }
591
592 Actor KeyboardFocusManager::GetFocusIndicatorActor()
593 {
594   if( ! mFocusIndicatorActor )
595   {
596     // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
597     mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
598
599     // Apply size constraint to the focus indicator
600     mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
601   }
602
603   mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
604   mFocusIndicatorActor.SetAnchorPoint( AnchorPoint::CENTER );
605   mFocusIndicatorActor.SetPosition(0.0f, 0.0f);
606
607   return mFocusIndicatorActor;
608 }
609
610 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
611 {
612   AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
613   bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
614
615   Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
616
617   std::string keyName = event.keyPressedName;
618
619   bool isFocusStartableKey = false;
620
621   if(event.state == KeyEvent::Down)
622   {
623     if (keyName == "Left")
624     {
625       if(!isAccessibilityEnabled)
626       {
627         if(!mIsFocusIndicatorEnabled)
628         {
629           // Show focus indicator
630           mIsFocusIndicatorEnabled = true;
631         }
632         else
633         {
634           // Move the focus towards left
635           MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
636         }
637
638         isFocusStartableKey = true;
639       }
640       else
641       {
642         // Move the accessibility focus backward
643         accessibilityManager.MoveFocusBackward();
644       }
645     }
646     else if (keyName == "Right")
647     {
648       if(!isAccessibilityEnabled)
649       {
650         if(!mIsFocusIndicatorEnabled)
651         {
652           // Show focus indicator
653           mIsFocusIndicatorEnabled = true;
654         }
655         else
656         {
657           // Move the focus towards right
658           MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
659         }
660       }
661       else
662       {
663         // Move the accessibility focus forward
664         accessibilityManager.MoveFocusForward();
665       }
666
667       isFocusStartableKey = true;
668     }
669     else if (keyName == "Up" && !isAccessibilityEnabled)
670     {
671       if(!mIsFocusIndicatorEnabled)
672       {
673         // Show focus indicator
674         mIsFocusIndicatorEnabled = true;
675       }
676       else
677       {
678         // Move the focus towards up
679         MoveFocus(Toolkit::Control::KeyboardFocus::UP);
680       }
681
682       isFocusStartableKey = true;
683     }
684     else if (keyName == "Down" && !isAccessibilityEnabled)
685     {
686       if(!mIsFocusIndicatorEnabled)
687       {
688         // Show focus indicator
689         mIsFocusIndicatorEnabled = true;
690       }
691       else
692       {
693         // Move the focus towards down
694         MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
695       }
696
697       isFocusStartableKey = true;
698     }
699     else if (keyName == "Prior" && !isAccessibilityEnabled)
700     {
701       if(!mIsFocusIndicatorEnabled)
702       {
703         // Show focus indicator
704         mIsFocusIndicatorEnabled = true;
705       }
706       else
707       {
708         // Move the focus towards the previous page
709         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
710       }
711
712       isFocusStartableKey = true;
713     }
714     else if (keyName == "Next" && !isAccessibilityEnabled)
715     {
716       if(!mIsFocusIndicatorEnabled)
717       {
718         // Show focus indicator
719         mIsFocusIndicatorEnabled = true;
720       }
721       else
722       {
723         // Move the focus towards the next page
724         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
725       }
726
727       isFocusStartableKey = true;
728     }
729     else if (keyName == "Tab" && !isAccessibilityEnabled)
730     {
731       if(!mIsFocusIndicatorEnabled)
732       {
733         // Show focus indicator
734         mIsFocusIndicatorEnabled = true;
735       }
736       else
737       {
738         // "Tab" key changes the focus group in the forward direction and
739         // "Shift-Tab" key changes it in the backward direction.
740         DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
741       }
742
743       isFocusStartableKey = true;
744     }
745     else if (keyName == "space" && !isAccessibilityEnabled)
746     {
747       if(!mIsFocusIndicatorEnabled)
748       {
749         // Show focus indicator
750         mIsFocusIndicatorEnabled = true;
751       }
752
753       isFocusStartableKey = true;
754     }
755     else if (keyName == "" && !isAccessibilityEnabled)
756     {
757       // Check the fake key event for evas-plugin case
758       if(!mIsFocusIndicatorEnabled)
759       {
760         // Show focus indicator
761         mIsFocusIndicatorEnabled = true;
762       }
763
764       isFocusStartableKey = true;
765     }
766     else if (keyName == "Backspace" && !isAccessibilityEnabled)
767     {
768       // Emit signal to go back to the previous view???
769     }
770     else if (keyName == "Escape" && !isAccessibilityEnabled)
771     {
772     }
773   }
774   else if(event.state == KeyEvent::Up)
775   {
776     if (keyName == "Return")
777     {
778       if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
779       {
780         // Show focus indicator
781         mIsFocusIndicatorEnabled = true;
782       }
783       else
784       {
785         // The focused actor has enter pressed on it
786         Actor actor;
787         if( !isAccessibilityEnabled )
788         {
789           actor = GetCurrentFocusActor();
790         }
791         else
792         {
793           actor = accessibilityManager.GetCurrentFocusActor();
794         }
795
796         if( actor )
797         {
798           DoKeyboardEnter( actor );
799         }
800       }
801
802       isFocusStartableKey = true;
803     }
804   }
805
806   if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
807   {
808     Actor actor = GetCurrentFocusActor();
809     if( actor )
810     {
811       // Make sure the focused actor is highlighted
812       actor.Add( GetFocusIndicatorActor() );
813     }
814     else
815     {
816       // No actor is focused but keyboard focus is activated by the key press
817       // Let's try to move the initial focus
818       MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
819     }
820   }
821 }
822
823 void KeyboardFocusManager::OnTouch(const TouchData& touch)
824 {
825   // Clear the focus when user touch the screen.
826   // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
827   if( ( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN ) )
828   {
829     ClearFocus();
830   }
831 }
832
833 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
834 {
835   return mPreFocusChangeSignal;
836 }
837
838 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
839 {
840   return mFocusChangedSignal;
841 }
842
843 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
844 {
845   return mFocusGroupChangedSignal;
846 }
847
848 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
849 {
850   return mFocusedActorEnterKeySignal;
851 }
852
853 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
854 {
855   Dali::BaseHandle handle( object );
856
857   bool connected( true );
858   KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
859
860   if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
861   {
862     manager->PreFocusChangeSignal().Connect( tracker, functor );
863   }
864   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
865   {
866     manager->FocusChangedSignal().Connect( tracker, functor );
867   }
868   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
869   {
870     manager->FocusGroupChangedSignal().Connect( tracker, functor );
871   }
872   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
873   {
874     manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
875   }
876   else
877   {
878     // signalName does not match any signal
879     connected = false;
880   }
881
882   return connected;
883 }
884
885 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
886 {
887   mCustomAlgorithmInterface = &interface;
888 }
889
890 } // namespace Internal
891
892 } // namespace Toolkit
893
894 } // namespace Dali