New propagation rule for KeyEvent
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyinput-focus-manager-impl.cpp
index dc8cefa..f188281 100644 (file)
 #include "keyinput-focus-manager-impl.h"
 
 // EXTERNAL INCLUDES
+#include <cstring> // for strcmp
 #include <dali/public-api/actors/layer.h>
 #include <dali/public-api/common/stage.h>
+#include <dali/devel-api/common/stage-devel.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/control-impl.h>
@@ -35,12 +37,20 @@ namespace Toolkit
 namespace Internal
 {
 
+namespace
+{
+
+// Signals
+
+const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
+
+}
+
 KeyInputFocusManager::KeyInputFocusManager()
-: mSlotDelegate( this )
+: mSlotDelegate( this ),
+  mCurrentFocusControl()
 {
-  Stage::GetCurrent().KeyEventSignal().Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
-  mObjectRegistry = Dali::Stage::GetCurrent().GetObjectRegistry();
-  mObjectRegistry.ObjectDestroyedSignal().Connect( this, &KeyInputFocusManager::OnObjectDestroyed );
+  DevelStage::KeyEventGeneratedSignal( Stage::GetCurrent() ).Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
 }
 
 KeyInputFocusManager::~KeyInputFocusManager()
@@ -55,35 +65,26 @@ void KeyInputFocusManager::SetFocus( Toolkit::Control control )
     return;
   }
 
-  FocusStackIterator pos = FindFocusControlInStack( control );
-
-  if( ( mFocusStack.Count() != 0 ) && ( pos == mFocusStack.End()-1 ) )
+  if( control == mCurrentFocusControl )
   {
-    // Control already in front, so No-op
+    // Control already has focus
     return;
   }
 
-  if( pos != mFocusStack.End() )
-  {
-    // A previously focused control wants to regain focus
-    mFocusStack.Erase( pos );
-  }
-  else
-  {
-    control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
-  }
+  control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
 
   Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
   if( previousFocusControl )
   {
     // Notify the control that it has lost key input focus
-    previousFocusControl.GetImplementation().OnKeyInputFocusLost();
+    GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
   }
 
-  mFocusStack.PushBack( &control.GetBaseObject() );
+  // Set control to currentFocusControl
+  mCurrentFocusControl = control;
 
   // Tell the new actor that it has gained focus.
-  control.GetImplementation().OnKeyInputFocusGained();
+  GetImplementation( control ).OnKeyInputFocusGained();
 
   // Emit the signal to inform focus change to the application.
   if ( !mKeyInputFocusChangedSignal.Empty() )
@@ -94,61 +95,20 @@ void KeyInputFocusManager::SetFocus( Toolkit::Control control )
 
 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
 {
-  if( control )
+  if( control == mCurrentFocusControl )
   {
-    FocusStackIterator pos = FindFocusControlInStack( control );
-    if( pos != mFocusStack.End() )
-    {
-      control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
+    control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
 
-      // Notify the control that it has lost key input focus
-      control.GetImplementation().OnKeyInputFocusLost();
+    // Notify the control that it has lost key input focus
+    GetImplementation( control ).OnKeyInputFocusLost();
 
-      // If this is the top-most actor, pop it and change focus to the previous control
-      if( pos == mFocusStack.End() - 1 )
-      {
-        mFocusStack.Erase( pos );
-
-        Toolkit::Control previouslyFocusedControl = GetCurrentFocusControl();
-        if( previouslyFocusedControl )
-        {
-          // Tell the control that it has gained focus.
-          previouslyFocusedControl.GetImplementation().OnKeyInputFocusGained();
-        }
-      }
-      else
-      {
-        // If the removed control is not currently focused, then no need to emit signal.
-        mFocusStack.Erase( pos );
-      }
-    }
+    mCurrentFocusControl.Reset();
   }
 }
 
 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
 {
-  Toolkit::Control currentControl;
-
-  FocusStack::SizeType count = mFocusStack.Count();
-  if( count != 0 )
-  {
-    BaseObject* object = mFocusStack[ count - 1 ];
-    BaseHandle handle( object );
-    currentControl = Dali::Toolkit::Control::DownCast( handle );
-  }
-  return currentControl;
-}
-
-bool KeyInputFocusManager::IsKeyboardListener( Toolkit::Control control ) const
-{
-  bool result = false;
-
-  if( FindFocusControlInStack( control ) != mFocusStack.End() )
-  {
-    result = true;
-  }
-
-  return result;
+  return mCurrentFocusControl;
 }
 
 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
@@ -156,46 +116,41 @@ Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusMana
   return mKeyInputFocusChangedSignal;
 }
 
-Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalType& KeyInputFocusManager::UnhandledKeyEventSignal()
+bool KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
 {
-  return mUnhandledKeyEventSignal;
-}
+  bool consumed = false;
 
-KeyInputFocusManager::FocusStackIterator KeyInputFocusManager::FindFocusControlInStack( Toolkit::Control control ) const
-{
-  BaseObject* controlObject = &control.GetBaseObject();
-  return std::find( mFocusStack.Begin(), mFocusStack.End(), controlObject );
+  Toolkit::Control control = GetCurrentFocusControl();
+  if( control )
+  {
+    // Notify the control about the key event
+    consumed = EmitKeyEventSignal( control, event );
+  }
+
+  return consumed;
 }
 
-void KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
+bool KeyInputFocusManager::EmitKeyEventSignal( Toolkit::Control control, const KeyEvent& event )
 {
   bool consumed = false;
 
-  if( mFocusStack.Count() > 0 )
+  if( control )
   {
-    FocusStack::SizeType index = mFocusStack.Count();
-    while( mFocusStack.Count() != 0 && !consumed && index > 0 )
+    consumed = GetImplementation( control ).EmitKeyEventSignal( event );
+
+    // if control doesn't consume KeyEvent, give KeyEvent to its parent.
+    if( !consumed )
     {
-      --index;
-      BaseObject* object = mFocusStack[ index ];
-      BaseHandle handle( object );
-      Toolkit::Control control = Toolkit::Control::DownCast( object );
-      if( control )
+      Toolkit::Control parent = Toolkit::Control::DownCast( control.GetParent() );
+
+      if( parent )
       {
-        // Notify the control about the key event
-        consumed = control.GetImplementation().EmitKeyEventSignal( event );
+        consumed = EmitKeyEventSignal( parent, event );
       }
     }
   }
 
-  if( !consumed )
-  {
-    // Emit signal to inform that a key event is not consumed.
-    if( !mUnhandledKeyEventSignal.Empty() )
-    {
-      mUnhandledKeyEventSignal.Emit(event);
-    }
-  }
+  return consumed;
 }
 
 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
@@ -203,33 +158,23 @@ void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
   RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
 }
 
-void KeyInputFocusManager::OnObjectDestroyed( const Dali::RefObject* object )
-{
-  // The object is already destroyed. Don't create handles to it, or try sending
-  // signals to it. Remove it's pointer from the stack.
-  const BaseObject* baseObject = static_cast<const BaseObject*>( object );
-  FocusStackIterator pos = std::find( mFocusStack.Begin(), mFocusStack.End(), baseObject );
-  if( pos != mFocusStack.End() )
-  {
-    mFocusStack.Erase( pos );
-  }
-}
 
 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
 {
-  Dali::BaseHandle handle( object );
-
   bool connected( true );
-  KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>(object);
+  KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
 
-  if( Dali::Toolkit::KeyInputFocusManager::SIGNAL_KEY_INPUT_FOCUS_CHANGED == signalName )
+  if( manager )
   {
-    manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
-  }
-  else
-  {
-    // signalName does not match any signal
-    connected = false;
+    if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
+    {
+      manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
+    }
+    else
+    {
+      // signalName does not match any signal
+      connected = false;
+    }
   }
 
   return connected;