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