Fix for flick gesture in Screen Reader mode
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / focus-manager / focus-manager-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include "focus-manager-impl.h"
19
20 // INTERNAL INCLUDES
21 #include <dali-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali/integration-api/debug.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 namespace // unnamed namespace
35 {
36
37 #if defined(DEBUG_ENABLED)
38 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_FOCUS_MANAGER");
39 #endif
40
41 const char * const ACTOR_FOCUSABLE("focusable");
42 const char * const IS_FOCUS_GROUP("is-focus-group");
43
44 const char* FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "B16-8_TTS_focus.png";
45 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
46
47 const char* FOCUS_SOUND_FILE = DALI_SOUND_DIR "Focus.ogg";
48 const char* FOCUS_CHAIN_END_SOUND_FILE = DALI_SOUND_DIR "End_of_List.ogg";
49
50 /**
51  * The function to be used in the hit-test algorithm to check whether the actor is hittable.
52  */
53 bool IsActorFocusableFunction(Actor actor, Dali::HitTestAlgorithm::TraverseType type)
54 {
55   bool hittable = false;
56
57   switch (type)
58   {
59     case Dali::HitTestAlgorithm::CHECK_ACTOR:
60     {
61       // Check whether the actor is visible and not fully transparent.
62       if( actor.IsVisible()
63        && actor.GetCurrentWorldColor().a > 0.01f) // not FULLY_TRANSPARENT
64       {
65         // Check whether the actor is focusable
66         Property::Index propertyActorFocusable = actor.GetPropertyIndex(ACTOR_FOCUSABLE);
67         if(propertyActorFocusable != Property::INVALID_INDEX)
68         {
69           hittable = actor.GetProperty<bool>(propertyActorFocusable);
70         }
71       }
72       break;
73     }
74     case Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE:
75     {
76       if( actor.IsVisible() ) // Actor is visible, if not visible then none of its children are visible.
77       {
78         hittable = true;
79       }
80       break;
81     }
82     default:
83     {
84       break;
85     }
86   }
87
88   return hittable;
89 };
90
91 }
92
93 FocusManager::FocusManager()
94 : mIsWrapped(false),
95   mIsFocusWithinGroup(false),
96   mIsEndcapFeedbackEnabled(true),
97   mIsEndcapFeedbackPlayed(false),
98   mCurrentFocusActor(FocusIDPair(0, 0)),
99   mFocusIndicatorActor(Actor()),
100   mRecursiveFocusMoveCounter(0),
101   mIsAccessibilityTtsEnabled(false),
102   mIsFocusIndicatorEnabled(false)
103 {
104   CreateDefaultFocusIndicatorActor();
105
106   AccessibilityManager manager = AccessibilityManager::Get();
107   manager.SetActionHandler(*this);
108   manager.SetGestureHandler(*this);
109
110   ChangeAccessibilityStatus();
111 }
112
113 FocusManager::~FocusManager()
114 {
115 }
116
117 FocusManager::ActorAdditionalInfo FocusManager::GetActorAdditionalInfo(const unsigned int actorID) const
118 {
119   ActorAdditionalInfo data;
120   IDAdditionalInfoConstIter iter = mIDAdditionalInfoContainer.find(actorID);
121   if(iter != mIDAdditionalInfoContainer.end())
122   {
123     data = (*iter).second;
124   }
125
126   return data;
127 }
128
129 void FocusManager::SynchronizeActorAdditionalInfo(const unsigned int actorID, const unsigned int order)
130 {
131   ActorAdditionalInfo actorInfo = GetActorAdditionalInfo(actorID);
132   actorInfo.mFocusOrder = order;
133   mIDAdditionalInfoContainer.erase(actorID);
134   mIDAdditionalInfoContainer.insert(IDAdditionalInfoPair(actorID, actorInfo));
135 }
136
137 void FocusManager::SetAccessibilityAttribute(Actor actor, Toolkit::FocusManager::AccessibilityAttribute type, const std::string& text)
138 {
139   if(actor)
140   {
141     unsigned int actorID = actor.GetId();
142
143     ActorAdditionalInfo info = GetActorAdditionalInfo(actorID);
144     info.mAccessibilityAttributes[type] = text;
145
146     mIDAdditionalInfoContainer.erase(actorID);
147     mIDAdditionalInfoContainer.insert(IDAdditionalInfoPair(actorID, info));
148   }
149 }
150
151 std::string FocusManager::GetAccessibilityAttribute(Actor actor, Toolkit::FocusManager::AccessibilityAttribute type) const
152 {
153   std::string text;
154
155   if(actor)
156   {
157     ActorAdditionalInfo data = GetActorAdditionalInfo(actor.GetId());
158     text = data.mAccessibilityAttributes[type];
159   }
160
161   return text;
162 }
163
164 void FocusManager::SetFocusOrder(Actor actor, const unsigned int order)
165 {
166   // Do nothing if the focus order of the actor is not changed.
167   if(actor && GetFocusOrder(actor) != order)
168   {
169     // Firstly delete the actor from the focus chain if it's already there with a different focus order.
170     mFocusIDContainer.erase(GetFocusOrder(actor));
171
172     // Create actor focusable property if not already created.
173     Property::Index propertyActorFocusable = actor.GetPropertyIndex(ACTOR_FOCUSABLE);
174     if(propertyActorFocusable == Property::INVALID_INDEX)
175     {
176       propertyActorFocusable = actor.RegisterProperty(ACTOR_FOCUSABLE, true);
177     }
178
179     if(order == 0)
180     {
181       // The actor is not focusable without a defined focus order.
182       actor.SetProperty(propertyActorFocusable, false);
183
184       // If the actor is currently being focused, it should clear the focus
185       if(actor == GetCurrentFocusActor())
186       {
187         ClearFocus();
188       }
189     }
190     else // Insert the actor to the focus chain
191     {
192       // Check whether there is another actor in the focus chain with the same focus order already.
193       FocusIDIter focusIDIter = mFocusIDContainer.find(order);
194       if(focusIDIter != mFocusIDContainer.end())
195       {
196         // We need to increase the focus order of that actor and all the actors followed it
197         // in the focus chain.
198         FocusIDIter lastIter = mFocusIDContainer.end();
199         --lastIter;//We want forward iterator to the last element here
200         mFocusIDContainer.insert(FocusIDPair((*lastIter).first + 1, (*lastIter).second));
201
202         // Update the actor's focus order in its additional data
203         SynchronizeActorAdditionalInfo((*lastIter).second, (*lastIter).first + 1);
204
205         for(FocusIDIter iter = lastIter; iter != focusIDIter; iter--)
206         {
207           FocusIDIter previousIter = iter;
208           --previousIter;//We want forward iterator to the previous element here
209           unsigned int actorID = (*previousIter).second;
210           (*iter).second = actorID;
211
212           // Update the actor's focus order in its additional data
213           SynchronizeActorAdditionalInfo(actorID, (*iter).first);
214         }
215
216         mFocusIDContainer.erase(order);
217       }
218
219       // The actor is focusable
220       actor.SetProperty(propertyActorFocusable, true);
221
222       // Now we insert the actor into the focus chain with the specified focus order
223       mFocusIDContainer.insert(FocusIDPair(order, actor.GetId()));
224     }
225
226     // Update the actor's focus order in its additional data
227     SynchronizeActorAdditionalInfo(actor.GetId(), order);
228   }
229 }
230
231 unsigned int FocusManager::GetFocusOrder(Actor actor) const
232 {
233   unsigned int focusOrder = 0;
234
235   if(actor)
236   {
237     ActorAdditionalInfo data = GetActorAdditionalInfo(actor.GetId());
238     focusOrder = data.mFocusOrder;
239   }
240
241   return focusOrder;
242 }
243
244 unsigned int FocusManager::GenerateNewFocusOrder() const
245 {
246   unsigned int order = 1;
247   FocusIDContainer::const_reverse_iterator iter = mFocusIDContainer.rbegin();
248
249   if(iter != mFocusIDContainer.rend())
250   {
251     order = (*iter).first + 1;
252   }
253
254   return order;
255 }
256
257 Actor FocusManager::GetActorByFocusOrder(const unsigned int order)
258 {
259   Actor actor = Actor();
260
261   FocusIDIter focusIDIter = mFocusIDContainer.find(order);
262   if(focusIDIter != mFocusIDContainer.end())
263   {
264     Actor rootActor = Stage::GetCurrent().GetRootLayer();
265     actor = rootActor.FindChildById(mFocusIDContainer[order]);
266   }
267
268   return actor;
269 }
270
271 bool FocusManager::SetCurrentFocusActor(Actor actor)
272 {
273   if(actor)
274   {
275     return DoSetCurrentFocusActor(actor.GetId());
276   }
277
278   return false;
279 }
280
281 bool FocusManager::DoSetCurrentFocusActor(const unsigned int actorID)
282 {
283   Actor rootActor = Stage::GetCurrent().GetRootLayer();
284
285   // If the group mode is enabled, check which focus group the current focused actor belongs to
286   Actor focusGroup;
287   if(mIsFocusWithinGroup)
288   {
289     focusGroup = GetFocusGroup(GetCurrentFocusActor());
290   }
291
292   if(!focusGroup)
293   {
294     focusGroup = rootActor;
295   }
296
297   Actor actor = focusGroup.FindChildById(actorID);
298
299   // Check whether the actor is in the stage
300   if(actor)
301   {
302     // Check whether the actor is focusable
303     bool actorFocusable = false;
304     Property::Index propertyActorFocusable = actor.GetPropertyIndex(ACTOR_FOCUSABLE);
305     if(propertyActorFocusable != Property::INVALID_INDEX)
306     {
307       actorFocusable = actor.GetProperty<bool>(propertyActorFocusable);
308     }
309
310     // Go through the actor's hierarchy to check whether the actor is visible
311     bool actorVisible = actor.IsVisible();
312     Actor parent = actor.GetParent();
313     while (actorVisible && parent && parent != rootActor)
314     {
315       actorVisible = parent.IsVisible();
316       parent = parent.GetParent();
317     }
318
319     // Check whether the actor is fully transparent
320     bool actorOpaque = actor.GetCurrentWorldColor().a > 0.01f;
321
322     // Set the focus only when the actor is focusable and visible and not fully transparent
323     if(actorVisible && actorFocusable && actorOpaque)
324     {
325       // Draw the focus indicator upon the focused actor
326       if(mIsFocusIndicatorEnabled && mFocusIndicatorActor)
327       {
328         actor.Add(mFocusIndicatorActor);
329       }
330
331       // Send notification for the change of focus actor
332       mFocusChangedSignalV2.Emit( GetCurrentFocusActor(), actor );
333
334       // Save the current focused actor
335       mCurrentFocusActor = FocusIDPair(GetFocusOrder(actor), actorID);
336
337       if(mIsAccessibilityTtsEnabled)
338       {
339         Dali::SoundPlayer soundPlayer = Dali::SoundPlayer::Get();
340         soundPlayer.PlaySound(FOCUS_SOUND_FILE);
341
342         // Play the accessibility attributes with the TTS player.
343         Dali::TtsPlayer player = Dali::TtsPlayer::Get(Dali::TtsPlayer::SCREEN_READER);
344
345         // Combine attribute texts to one text
346         std::string informationText;
347         for(int i = 0; i < Toolkit::FocusManager::ACCESSIBILITY_ATTRIBUTE_NUM; i++)
348         {
349           if(!GetActorAdditionalInfo(actorID).mAccessibilityAttributes[i].empty())
350           {
351             if( i > 0 )
352             {
353               informationText += ", "; // for space time between each information
354             }
355             informationText += GetActorAdditionalInfo(actorID).mAccessibilityAttributes[i];
356           }
357         }
358         player.Play(informationText);
359       }
360
361       return true;
362     }
363   }
364
365   DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
366   return false;
367 }
368
369 Actor FocusManager::GetCurrentFocusActor()
370 {
371   Actor rootActor = Stage::GetCurrent().GetRootLayer();
372   return rootActor.FindChildById(mCurrentFocusActor.second);
373 }
374
375 Actor FocusManager::GetCurrentFocusGroup()
376 {
377   return GetFocusGroup(GetCurrentFocusActor());
378 }
379
380 unsigned int FocusManager::GetCurrentFocusOrder()
381 {
382   return mCurrentFocusActor.first;
383 }
384
385 bool FocusManager::MoveFocusForward()
386 {
387   bool ret = false;
388   mRecursiveFocusMoveCounter = 0;
389
390   FocusIDIter focusIDIter = mFocusIDContainer.find(mCurrentFocusActor.first);
391   if(focusIDIter != mFocusIDContainer.end())
392   {
393     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] focus order : %d\n", __FUNCTION__, __LINE__, (*focusIDIter).first);
394     ret = DoMoveFocus(focusIDIter, true, mIsWrapped);
395   }
396   else
397   {
398     // TODO: if there is not focused actor, move first actor
399     if(!mFocusIDContainer.empty())
400     {
401       //if there is not focused actor, move 1st actor
402       focusIDIter = mFocusIDContainer.begin(); // TODO: I'm not sure it was sorted.
403       DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] focus order : %d\n", __FUNCTION__, __LINE__, (*focusIDIter).first);
404       ret = DoSetCurrentFocusActor((*focusIDIter).second);
405     }
406   }
407
408   DALI_LOG_INFO( gLogFilter, Debug::General, "[%s] %s\n", __FUNCTION__, ret?"SUCCEED!!!":"FAILED!!!");
409
410   return ret;
411 }
412
413 bool FocusManager::MoveFocusBackward()
414 {
415   bool ret = false;
416   mRecursiveFocusMoveCounter = 0;
417
418   FocusIDIter focusIDIter = mFocusIDContainer.find(mCurrentFocusActor.first);
419   if(focusIDIter != mFocusIDContainer.end())
420   {
421     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] focus order : %d\n", __FUNCTION__, __LINE__, (*focusIDIter).first);
422     ret = DoMoveFocus(focusIDIter, false, mIsWrapped);
423   }
424   else
425   {
426     // TODO: if there is not focused actor, move last actor
427     if(!mFocusIDContainer.empty())
428     {
429       //if there is not focused actor, move last actor
430       focusIDIter = mFocusIDContainer.end();
431       --focusIDIter;//We want forward iterator to the last element here
432       DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] focus order : %d\n", __FUNCTION__, __LINE__, (*focusIDIter).first);
433       ret = DoSetCurrentFocusActor((*focusIDIter).second);
434     }
435   }
436
437   DALI_LOG_INFO( gLogFilter, Debug::General, "[%s] %s\n", __FUNCTION__, ret?"SUCCEED!!!":"FAILED!!!");
438
439   return ret;
440 }
441
442 void FocusManager::DoActivate(Actor actor)
443 {
444   if(actor)
445   {
446     Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
447     if(control)
448     {
449       // Notify the control that it is activated
450       control.GetImplementation().OnActivated();
451     }
452
453     // Send notification for the activation of focused actor
454     mFocusedActorActivatedSignalV2.Emit(actor);
455   }
456 }
457
458 void FocusManager::ClearFocus()
459 {
460   Actor actor = GetCurrentFocusActor();
461   if(actor)
462   {
463     actor.Remove(mFocusIndicatorActor);
464   }
465
466   mCurrentFocusActor = FocusIDPair(0, 0);
467
468   // Send notification for the change of focus actor
469   mFocusChangedSignalV2.Emit(actor, Actor());
470
471   if(mIsAccessibilityTtsEnabled)
472   {
473     // Stop the TTS playing if any
474     Dali::TtsPlayer player = Dali::TtsPlayer::Get(Dali::TtsPlayer::SCREEN_READER);
475     player.Stop();
476   }
477 }
478
479 void FocusManager::Reset()
480 {
481   ClearFocus();
482   mFocusIDContainer.clear();
483   mIDAdditionalInfoContainer.clear();
484 }
485
486 void FocusManager::SetFocusGroup(Actor actor, bool isFocusGroup)
487 {
488   if(actor)
489   {
490     // Create focus group property if not already created.
491     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP);
492     if(propertyIsFocusGroup == Property::INVALID_INDEX)
493     {
494       propertyIsFocusGroup = actor.RegisterProperty(IS_FOCUS_GROUP, isFocusGroup);
495     }
496     else
497     {
498       actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
499     }
500   }
501 }
502
503 bool FocusManager::IsFocusGroup(Actor actor) const
504 {
505   // Check whether the actor is a focus group
506   bool isFocusGroup = false;
507
508   if(actor)
509   {
510     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP);
511     if(propertyIsFocusGroup != Property::INVALID_INDEX)
512     {
513       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
514     }
515   }
516
517   return isFocusGroup;
518 }
519
520 Actor FocusManager::GetFocusGroup(Actor actor)
521 {
522   // Go through the actor's hierarchy to check which focus group the actor belongs to
523   while (actor && !IsFocusGroup(actor))
524   {
525     actor = actor.GetParent();
526   }
527
528   return actor;
529 }
530
531 void FocusManager::SetGroupMode(bool enabled)
532 {
533   mIsFocusWithinGroup = enabled;
534 }
535
536 bool FocusManager::GetGroupMode() const
537 {
538   return mIsFocusWithinGroup;
539 }
540
541 void FocusManager::SetWrapMode(bool wrapped)
542 {
543   mIsWrapped = wrapped;
544 }
545
546 bool FocusManager::GetWrapMode() const
547 {
548   return mIsWrapped;
549 }
550
551 void FocusManager::SetEndCapFeedbackEnabled(bool enabled)
552 {
553   mIsEndcapFeedbackEnabled = enabled;
554 }
555
556 bool FocusManager::GetEndCapFeedbackEnabled() const
557 {
558   return mIsEndcapFeedbackEnabled;
559 }
560
561 void FocusManager::SetFocusIndicatorActor(Actor indicator)
562 {
563   mFocusIndicatorActor = indicator;
564 }
565
566 Actor FocusManager::GetFocusIndicatorActor()
567 {
568   return mFocusIndicatorActor;
569 }
570
571 bool FocusManager::DoMoveFocus(FocusIDIter focusIDIter, bool forward, bool wrapped)
572 {
573   DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] %d focusable actors\n", __FUNCTION__, __LINE__, mFocusIDContainer.size());
574   DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] focus order : %d\n", __FUNCTION__, __LINE__, (*focusIDIter).first);
575
576   if( (forward && ++focusIDIter == mFocusIDContainer.end())
577     || (!forward && focusIDIter-- == mFocusIDContainer.begin()) )
578   {
579     if(wrapped)
580     {
581       if(mIsEndcapFeedbackEnabled)
582       {
583         if(mIsEndcapFeedbackPlayed == false)
584         {
585           // play sound & skip to move once
586           Dali::SoundPlayer soundPlayer = Dali::SoundPlayer::Get();
587           soundPlayer.PlaySound(FOCUS_CHAIN_END_SOUND_FILE);
588
589           mIsEndcapFeedbackPlayed = true;
590           return true;
591         }
592         mIsEndcapFeedbackPlayed = false;
593       }
594
595       if(forward)
596       {
597         focusIDIter = mFocusIDContainer.begin();
598       }
599       else
600       {
601         focusIDIter = mFocusIDContainer.end();
602         --focusIDIter;//We want forward iterator to the last element here
603       }
604     }
605     else
606     {
607       if(mIsEndcapFeedbackEnabled)
608       {
609         Dali::SoundPlayer soundPlayer = Dali::SoundPlayer::Get();
610         soundPlayer.PlaySound(FOCUS_CHAIN_END_SOUND_FILE);
611       }
612
613       DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Overshot\n", __FUNCTION__, __LINE__);
614       // Send notification for handling overshooted situation
615       mFocusOvershotSignalV2.Emit(GetCurrentFocusActor(), forward ? Toolkit::FocusManager::OVERSHOT_NEXT : Toolkit::FocusManager::OVERSHOT_PREVIOUS);
616
617       return false; // Try to move the focus out of the scope
618     }
619   }
620
621   if((focusIDIter != mFocusIDContainer.end()) && !DoSetCurrentFocusActor((*focusIDIter).second))
622   {
623     mRecursiveFocusMoveCounter++;
624     if(mRecursiveFocusMoveCounter > mFocusIDContainer.size())
625     {
626       // We've attempted to focus all the actors in the whole focus chain and no actor
627       // can be focused successfully.
628
629       DALI_LOG_WARNING("[%s] There is no more focusable actor in %d focus chains\n", __FUNCTION__, mRecursiveFocusMoveCounter);
630
631       return false;
632     }
633     else
634     {
635       return DoMoveFocus(focusIDIter, forward, wrapped);
636     }
637   }
638
639   return true;
640 }
641
642 void FocusManager::SetFocusable(Actor actor, bool focusable)
643 {
644   if(actor)
645   {
646     // Create actor focusable property if not already created.
647     Property::Index propertyActorFocusable = actor.GetPropertyIndex(ACTOR_FOCUSABLE);
648     if(propertyActorFocusable == Property::INVALID_INDEX)
649     {
650       propertyActorFocusable = actor.RegisterProperty(ACTOR_FOCUSABLE, focusable);
651     }
652     else
653     {
654       actor.SetProperty(propertyActorFocusable, focusable);
655     }
656   }
657 }
658
659 void FocusManager::CreateDefaultFocusIndicatorActor()
660 {
661   // Create a focus indicator actor shared by all the focusable actors
662   Image borderImage = Image::New(FOCUS_BORDER_IMAGE_PATH);
663
664   ImageActor focusIndicator = ImageActor::New(borderImage);
665   focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
666   focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
667   focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
668   focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
669
670   // Apply size constraint to the focus indicator
671   Constraint constraint = Constraint::New<Vector3>(Actor::SIZE,
672                                                    ParentSource(Actor::SIZE),
673                                                    EqualToConstraint());
674   focusIndicator.ApplyConstraint(constraint);
675
676   SetFocusIndicatorActor(focusIndicator);
677 }
678
679 bool FocusManager::ChangeAccessibilityStatus()
680 {
681   AccessibilityManager manager = AccessibilityManager::Get();
682   mIsAccessibilityTtsEnabled = manager.IsEnabled();
683
684   if(mIsAccessibilityTtsEnabled)
685   {
686     // Show indicator when tts turned on if there is focused actor.
687     Actor actor = GetCurrentFocusActor();
688     if(actor)
689     {
690       if(mFocusIndicatorActor)
691       {
692         actor.Add(mFocusIndicatorActor);
693       }
694     }
695     mIsFocusIndicatorEnabled = true;
696   }
697   else
698   {
699     // Hide indicator when tts turned off
700     Actor actor = GetCurrentFocusActor();
701     if(actor)
702     {
703       actor.Remove(mFocusIndicatorActor);
704     }
705     mIsFocusIndicatorEnabled = false;
706   }
707
708   return true;
709 }
710
711 bool FocusManager::AccessibilityActionNext()
712 {
713   if(mIsAccessibilityTtsEnabled)
714   {
715     return MoveFocusForward();
716   }
717   else
718   {
719     return false;
720   }
721 }
722
723 bool FocusManager::AccessibilityActionPrevious()
724 {
725   if(mIsAccessibilityTtsEnabled)
726   {
727     return MoveFocusBackward();
728   }
729   else
730   {
731     return false;
732   }
733 }
734
735 bool FocusManager::AccessibilityActionActivate()
736 {
737   bool ret = false;
738
739   Actor actor = GetCurrentFocusActor();
740   if(actor)
741   {
742     DoActivate(actor);
743     ret = true;
744   }
745
746   return ret;
747 }
748
749 bool FocusManager::AccessibilityActionRead(bool allowReadAgain)
750 {
751   bool ret = false;
752
753   if(mIsAccessibilityTtsEnabled)
754   {
755     // Find the focusable actor at the read position
756     AccessibilityManager manager = AccessibilityManager::Get();
757     Dali::HitTestAlgorithm::Results results;
758     Dali::HitTestAlgorithm::HitTest( Stage::GetCurrent(), manager.GetReadPosition(), results, IsActorFocusableFunction );
759
760     FocusIDIter focusIDIter = mFocusIDContainer.find(GetFocusOrder(results.actor));
761     if(focusIDIter != mFocusIDContainer.end())
762     {
763       if( allowReadAgain || (results.actor != GetCurrentFocusActor()) )
764       {
765         // Move the focus to the actor
766         ret = SetCurrentFocusActor(results.actor);
767         DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SetCurrentFocusActor returns %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
768       }
769     }
770   }
771
772   return ret;
773 }
774
775 bool FocusManager::AccessibilityActionReadNext()
776 {
777   if(mIsAccessibilityTtsEnabled)
778   {
779     return MoveFocusForward();
780   }
781   else
782   {
783     return false;
784   }
785 }
786
787 bool FocusManager::AccessibilityActionReadPrevious()
788 {
789   if(mIsAccessibilityTtsEnabled)
790   {
791     return MoveFocusBackward();
792   }
793   else
794   {
795     return false;
796   }
797 }
798
799 bool FocusManager::AccessibilityActionUp()
800 {
801   bool ret = false;
802
803   if(mIsAccessibilityTtsEnabled)
804   {
805     Actor actor = GetCurrentFocusActor();
806     if(actor)
807     {
808       Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
809       if(control)
810       {
811         // Notify the control that it is activated
812         ret = control.GetImplementation().OnAccessibilityValueChange(true);
813       }
814     }
815   }
816
817   return ret;
818 }
819
820 bool FocusManager::AccessibilityActionDown()
821 {
822   bool ret = false;
823
824   if(mIsAccessibilityTtsEnabled)
825   {
826     Actor actor = GetCurrentFocusActor();
827     if(actor)
828     {
829       Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
830       if(control)
831       {
832         // Notify the control that it is activated
833         ret = control.GetImplementation().OnAccessibilityValueChange(false);
834       }
835     }
836   }
837
838   return ret;
839 }
840
841 bool FocusManager::ClearAccessibilityFocus()
842 {
843   if(mIsAccessibilityTtsEnabled)
844   {
845     ClearFocus();
846     return true;
847   }
848   else
849   {
850     return false;
851   }
852 }
853
854 bool FocusManager::AccessibilityActionBack()
855 {
856   // TODO: Back to previous view
857
858   return mIsAccessibilityTtsEnabled;
859 }
860
861 bool FocusManager::HandlePanGesture(const Integration::PanGestureEvent& panEvent)
862 {
863   bool handled = false;
864
865   if( panEvent.state == Gesture::Started )
866   {
867     // Find the focusable actor at the event position
868     Dali::HitTestAlgorithm::Results results;
869     AccessibilityManager manager = AccessibilityManager::Get();
870
871     Dali::HitTestAlgorithm::HitTest( Stage::GetCurrent(), panEvent.currentPosition, results, IsActorFocusableFunction );
872     mCurrentGesturedActor = results.actor;
873
874     if(!mCurrentGesturedActor)
875     {
876       DALI_LOG_ERROR("Gesture detected, but no hit actor");
877     }
878   }
879
880   // Gesture::Finished (Up) events are delivered with previous (Motion) event position
881   // Use the real previous position; otherwise we may incorrectly get a ZERO velocity
882   if ( Gesture::Finished != panEvent.state )
883   {
884     // Store the previous position for next Gesture::Finished iteration.
885     mPreviousPosition = panEvent.previousPosition;
886   }
887
888   Actor rootActor = Stage::GetCurrent().GetRootLayer();
889
890   Dali::PanGesture pan(panEvent.state);
891   pan.time = panEvent.time;
892   pan.numberOfTouches = panEvent.numberOfTouches;
893   pan.screenPosition = panEvent.currentPosition;
894   pan.screenDisplacement = mPreviousPosition - panEvent.currentPosition;
895   pan.screenVelocity.x = pan.screenDisplacement.x / panEvent.timeDelta;
896   pan.screenVelocity.y = pan.screenDisplacement.y / panEvent.timeDelta;
897
898   // Only handle the pan gesture when the current focused actor is scrollable or within a scrollable actor
899   while(mCurrentGesturedActor && mCurrentGesturedActor != rootActor && !handled)
900   {
901     Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(mCurrentGesturedActor);
902     if(control)
903     {
904       Vector2 localCurrent;
905       control.ScreenToLocal( localCurrent.x, localCurrent.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
906       pan.position = localCurrent;
907
908       Vector2 localPrevious;
909       control.ScreenToLocal( localPrevious.x, localPrevious.y, mPreviousPosition.x, mPreviousPosition.y );
910
911       pan.displacement = localCurrent - localPrevious;
912       pan.velocity.x = pan.displacement.x / panEvent.timeDelta;
913       pan.velocity.y = pan.displacement.y / panEvent.timeDelta;
914
915       handled = control.GetImplementation().OnAccessibilityPan(pan);
916     }
917
918     // If the gesture is not handled by the control, check its parent
919     if(!handled)
920     {
921       mCurrentGesturedActor = mCurrentGesturedActor.GetParent();
922
923       if(!mCurrentGesturedActor)
924       {
925         DALI_LOG_ERROR("no more gestured actor");
926       }
927     }
928     else
929     {
930       // If handled, then update the pan gesture properties
931       PanGestureDetector::SetPanGestureProperties( pan );
932     }
933   }
934
935   return handled;
936 }
937
938 Toolkit::FocusManager::FocusChangedSignalV2& FocusManager::FocusChangedSignal()
939 {
940   return mFocusChangedSignalV2;
941 }
942
943 Toolkit::FocusManager::FocusOvershotSignalV2& FocusManager::FocusOvershotSignal()
944 {
945   return mFocusOvershotSignalV2;
946 }
947
948 Toolkit::FocusManager::FocusedActorActivatedSignalV2& FocusManager::FocusedActorActivatedSignal()
949 {
950   return mFocusedActorActivatedSignalV2;
951 }
952
953 bool FocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
954 {
955   Dali::BaseHandle handle( object );
956
957   bool connected( true );
958   FocusManager* manager = dynamic_cast<FocusManager*>(object);
959
960   if( Dali::Toolkit::FocusManager::SIGNAL_FOCUS_CHANGED == signalName )
961   {
962     manager->FocusChangedSignal().Connect( tracker, functor );
963   }
964   else if( Dali::Toolkit::FocusManager::SIGNAL_FOCUS_OVERSHOT == signalName )
965   {
966     manager->FocusOvershotSignal().Connect( tracker, functor );
967   }
968   else if( Dali::Toolkit::FocusManager::SIGNAL_FOCUSED_ACTOR_ACTIVATED== signalName )
969   {
970     manager->FocusedActorActivatedSignal().Connect( tracker, functor );
971   }
972   else
973   {
974     // signalName does not match any signal
975     connected = false;
976   }
977
978   return connected;
979 }
980
981 } // namespace Internal
982
983 } // namespace Toolkit
984
985 } // namespace Dali