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-toolkit/public-api/controls/control-impl.h>
23 #include <dali/integration-api/debug.h>
34 KeyInputFocusManager::KeyInputFocusManager()
35 : mSlotDelegate( this )
37 Stage::GetCurrent().KeyEventSignal().Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
40 KeyInputFocusManager::~KeyInputFocusManager()
44 void KeyInputFocusManager::SetFocus(Toolkit::Control control)
52 unsigned int actorID = control.GetId();
54 ActorQueueIterator pos = std::find( mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorID);
56 if((!mFocusActorsQueue.empty()) && (pos == mFocusActorsQueue.begin()))
58 //Actor allready in front, so No-op
62 if(pos != mFocusActorsQueue.end())
64 //A previously focused actor wants to regain focus
65 mFocusActorsQueue.erase(pos);
69 control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusActorStageDisconnection );
72 Dali::Toolkit::Control previousFocusControl;
73 if(!mFocusActorsQueue.empty())
75 previousFocusControl = Dali::Toolkit::Control::DownCast(Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front()));
76 if(previousFocusControl)
78 // Notify the control that it has lost key input focus
79 previousFocusControl.GetImplementation().OnKeyInputFocusLost();
83 mFocusActorsQueue.push_front(actorID);
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 ( !mKeyInputFocusChangedSignalV2.Empty() )
91 mKeyInputFocusChangedSignalV2.Emit( control, previousFocusControl );
95 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
97 Toolkit::Control currentFocusControl;
99 if(!mFocusActorsQueue.empty())
101 currentFocusControl = Dali::Toolkit::Control::DownCast(Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front()));
104 return currentFocusControl;
107 void KeyInputFocusManager::RemoveFocus(Toolkit::Control control)
111 unsigned int actorId = control.GetId();
112 ActorQueueIterator pos = std::find( mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
114 if(pos != mFocusActorsQueue.end())
116 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusActorStageDisconnection );
118 // Notify the control that it has lost key input focus
119 control.GetImplementation().OnKeyInputFocusLost();
121 if(pos == mFocusActorsQueue.begin())
123 Actor previousFocusActor;
125 mFocusActorsQueue.erase(pos);
126 if(!mFocusActorsQueue.empty())
128 previousFocusActor = Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front());
131 Dali::Toolkit::Control previouscontrol = Dali::Toolkit::Control::DownCast(previousFocusActor);
134 // Tell the new actor that it has gained focus.
135 previouscontrol.GetImplementation().OnKeyInputFocusGained();
140 //If the removed actor is not currently focused, then no need to emit signal.
141 mFocusActorsQueue.erase(pos);
148 bool KeyInputFocusManager::IsKeyboardListener(Toolkit::Control control) const
152 if(!mFocusActorsQueue.empty())
154 unsigned int actorId = control.GetId();
155 ActorQueueConstIterator pos = std::find(mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
157 if(pos != mFocusActorsQueue.end())
166 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalV2& KeyInputFocusManager::KeyInputFocusChangedSignal()
168 return mKeyInputFocusChangedSignalV2;
171 Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalV2& KeyInputFocusManager::UnhandledKeyEventSignal()
173 return mUnhandledKeyEventSignalV2;
176 void KeyInputFocusManager::OnKeyEvent(const KeyEvent& event)
178 bool consumed = false;
180 ActorQueueIterator iter = mFocusActorsQueue.begin();
182 Layer rootLayer = Stage::GetCurrent().GetRootLayer();
184 while(!mFocusActorsQueue.empty() && !consumed && (iter != mFocusActorsQueue.end()))
186 Actor actor = rootLayer.FindChildById(*iter);
187 Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
190 // Notify the control about the key event
191 consumed = control.GetImplementation().EmitKeyEventSignal(event);
198 // Emit signal to inform that a key event is not consumed.
199 if( !mUnhandledKeyEventSignalV2.Empty() )
201 mUnhandledKeyEventSignalV2.Emit(event);
206 void KeyInputFocusManager::OnFocusActorStageDisconnection( Dali::Actor actor )
208 RemoveFocus(Dali::Toolkit::Control::DownCast(actor));
211 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
213 Dali::BaseHandle handle( object );
215 bool connected( true );
216 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>(object);
218 if( Dali::Toolkit::KeyInputFocusManager::SIGNAL_KEY_INPUT_FOCUS_CHANGED == signalName )
220 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
224 // signalName does not match any signal
231 } // namespace Internal
233 } // namespace Toolkit