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 <dali/public-api/actors/layer.h>
23 #include <dali/public-api/common/stage.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include <dali/integration-api/debug.h>
38 KeyInputFocusManager::KeyInputFocusManager()
39 : mSlotDelegate( this )
41 Stage::GetCurrent().KeyEventSignal().Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
42 mObjectRegistry = Dali::Stage::GetCurrent().GetObjectRegistry();
43 mObjectRegistry.ObjectDestroyedSignal().Connect( this, &KeyInputFocusManager::OnObjectDestroyed );
46 KeyInputFocusManager::~KeyInputFocusManager()
50 void KeyInputFocusManager::SetFocus( Toolkit::Control control )
58 FocusStackIterator pos = FindFocusControlInStack( control );
60 if( ( mFocusStack.Count() != 0 ) && ( pos == mFocusStack.End()-1 ) )
62 // Control already in front, so No-op
66 if( pos != mFocusStack.End() )
68 // A previously focused control wants to regain focus
69 mFocusStack.Erase( pos );
73 control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
76 Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
77 if( previousFocusControl )
79 // Notify the control that it has lost key input focus
80 previousFocusControl.GetImplementation().OnKeyInputFocusLost();
83 mFocusStack.PushBack( &control.GetBaseObject() );
85 // Tell the new actor that it has gained focus.
86 control.GetImplementation().OnKeyInputFocusGained();
88 // Emit the signal to inform focus change to the application.
89 if ( !mKeyInputFocusChangedSignal.Empty() )
91 mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
95 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
99 FocusStackIterator pos = FindFocusControlInStack( control );
100 if( pos != mFocusStack.End() )
102 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
104 // Notify the control that it has lost key input focus
105 control.GetImplementation().OnKeyInputFocusLost();
107 // If this is the top-most actor, pop it and change focus to the previous control
108 if( pos == mFocusStack.End() - 1 )
110 mFocusStack.Erase( pos );
112 Toolkit::Control previouslyFocusedControl = GetCurrentFocusControl();
113 if( previouslyFocusedControl )
115 // Tell the control that it has gained focus.
116 previouslyFocusedControl.GetImplementation().OnKeyInputFocusGained();
121 // If the removed control is not currently focused, then no need to emit signal.
122 mFocusStack.Erase( pos );
128 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
130 Toolkit::Control currentControl;
132 FocusStack::SizeType count = mFocusStack.Count();
135 BaseObject* object = mFocusStack[ count - 1 ];
136 BaseHandle handle( object );
137 currentControl = Dali::Toolkit::Control::DownCast( handle );
139 return currentControl;
142 bool KeyInputFocusManager::IsKeyboardListener( Toolkit::Control control ) const
146 if( FindFocusControlInStack( control ) != mFocusStack.End() )
154 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
156 return mKeyInputFocusChangedSignal;
159 Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalType& KeyInputFocusManager::UnhandledKeyEventSignal()
161 return mUnhandledKeyEventSignal;
164 KeyInputFocusManager::FocusStackIterator KeyInputFocusManager::FindFocusControlInStack( Toolkit::Control control ) const
166 BaseObject* controlObject = &control.GetBaseObject();
167 return std::find( mFocusStack.Begin(), mFocusStack.End(), controlObject );
170 void KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
172 bool consumed = false;
174 if( mFocusStack.Count() > 0 )
176 FocusStack::SizeType index = mFocusStack.Count();
177 while( mFocusStack.Count() != 0 && !consumed && index > 0 )
180 BaseObject* object = mFocusStack[ index ];
181 BaseHandle handle( object );
182 Toolkit::Control control = Toolkit::Control::DownCast( object );
185 // Notify the control about the key event
186 consumed = control.GetImplementation().EmitKeyEventSignal( event );
193 // Emit signal to inform that a key event is not consumed.
194 if( !mUnhandledKeyEventSignal.Empty() )
196 mUnhandledKeyEventSignal.Emit(event);
201 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
203 RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
206 void KeyInputFocusManager::OnObjectDestroyed( const Dali::RefObject* object )
208 // The object is already destroyed. Don't create handles to it, or try sending
209 // signals to it. Remove it's pointer from the stack.
210 const BaseObject* baseObject = static_cast<const BaseObject*>( object );
211 FocusStackIterator pos = std::find( mFocusStack.Begin(), mFocusStack.End(), baseObject );
212 if( pos != mFocusStack.End() )
214 mFocusStack.Erase( pos );
218 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
220 Dali::BaseHandle handle( object );
222 bool connected( true );
223 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>(object);
225 if( Dali::Toolkit::KeyInputFocusManager::SIGNAL_KEY_INPUT_FOCUS_CHANGED == signalName )
227 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
231 // signalName does not match any signal
238 } // namespace Internal
240 } // namespace Toolkit