2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "keyinput-focus-manager-impl.h"
22 #include <cstring> // for strcmp
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/public-api/common/stage.h>
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali/integration-api/debug.h>
44 const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
45 const char* const SIGNAL_UNHANDLED_KEY_EVENT = "unhandledKeyEvent";
49 KeyInputFocusManager::KeyInputFocusManager()
50 : mSlotDelegate( this )
52 Stage::GetCurrent().KeyEventSignal().Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
53 mObjectRegistry = Dali::Stage::GetCurrent().GetObjectRegistry();
54 mObjectRegistry.ObjectDestroyedSignal().Connect( this, &KeyInputFocusManager::OnObjectDestroyed );
57 KeyInputFocusManager::~KeyInputFocusManager()
61 void KeyInputFocusManager::SetFocus( Toolkit::Control control )
69 FocusStackIterator pos = FindFocusControlInStack( control );
71 if( ( mFocusStack.Count() != 0 ) && ( pos == mFocusStack.End()-1 ) )
73 // Control already in front, so No-op
77 if( pos != mFocusStack.End() )
79 // A previously focused control wants to regain focus
80 mFocusStack.Erase( pos );
84 control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
87 Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
88 if( previousFocusControl )
90 // Notify the control that it has lost key input focus
91 GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
94 mFocusStack.PushBack( &control.GetBaseObject() );
96 // Tell the new actor that it has gained focus.
97 GetImplementation( control ).OnKeyInputFocusGained();
99 // Emit the signal to inform focus change to the application.
100 if ( !mKeyInputFocusChangedSignal.Empty() )
102 mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
106 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
110 FocusStackIterator pos = FindFocusControlInStack( control );
111 if( pos != mFocusStack.End() )
113 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
115 // Notify the control that it has lost key input focus
116 GetImplementation( control ).OnKeyInputFocusLost();
118 // If this is the top-most actor, pop it and change focus to the previous control
119 if( pos == mFocusStack.End() - 1 )
121 mFocusStack.Erase( pos );
123 Toolkit::Control previouslyFocusedControl = GetCurrentFocusControl();
124 if( previouslyFocusedControl )
126 // Tell the control that it has gained focus.
127 GetImplementation( previouslyFocusedControl ).OnKeyInputFocusGained();
132 // If the removed control is not currently focused, then no need to emit signal.
133 mFocusStack.Erase( pos );
139 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
141 Toolkit::Control currentControl;
143 FocusStack::SizeType count = mFocusStack.Count();
146 BaseObject* object = mFocusStack[ count - 1 ];
147 BaseHandle handle( object );
148 currentControl = Dali::Toolkit::Control::DownCast( handle );
150 return currentControl;
153 bool KeyInputFocusManager::IsKeyboardListener( Toolkit::Control control ) const
157 if( FindFocusControlInStack( control ) != mFocusStack.End() )
165 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
167 return mKeyInputFocusChangedSignal;
170 Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalType& KeyInputFocusManager::UnhandledKeyEventSignal()
172 return mUnhandledKeyEventSignal;
175 KeyInputFocusManager::FocusStackIterator KeyInputFocusManager::FindFocusControlInStack( Toolkit::Control control ) const
177 BaseObject* controlObject = &control.GetBaseObject();
178 return std::find( mFocusStack.Begin(), mFocusStack.End(), controlObject );
181 void KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
183 bool consumed = false;
185 if( mFocusStack.Count() > 0 )
187 FocusStack::SizeType index = mFocusStack.Count();
188 while( mFocusStack.Count() != 0 && !consumed && index > 0 )
191 BaseObject* object = mFocusStack[ index ];
192 BaseHandle handle( object );
193 Toolkit::Control control = Toolkit::Control::DownCast( object );
196 // Notify the control about the key event
197 consumed = GetImplementation( control ).EmitKeyEventSignal( event );
204 // Emit signal to inform that a key event is not consumed.
205 if( !mUnhandledKeyEventSignal.Empty() )
207 mUnhandledKeyEventSignal.Emit(event);
212 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
214 RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
217 void KeyInputFocusManager::OnObjectDestroyed( const Dali::RefObject* object )
219 // The object is already destroyed. Don't create handles to it, or try sending
220 // signals to it. Remove it's pointer from the stack.
221 const BaseObject* baseObject = static_cast<const BaseObject*>( object );
222 FocusStackIterator pos = std::find( mFocusStack.Begin(), mFocusStack.End(), baseObject );
223 if( pos != mFocusStack.End() )
225 mFocusStack.Erase( pos );
229 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
231 bool connected( true );
232 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
236 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
238 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
240 else if( 0 == strcmp( signalName.c_str(), SIGNAL_UNHANDLED_KEY_EVENT ) )
242 manager->UnhandledKeyEventSignal().Connect( tracker, functor );
246 // signalName does not match any signal
254 } // namespace Internal
256 } // namespace Toolkit