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