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