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