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