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>
25 #include <dali/devel-api/common/stage-devel.h>
26 #include <dali/integration-api/adaptors/adaptor.h>
27 #include <dali/integration-api/adaptors/scene-holder.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali/integration-api/debug.h>
47 const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
51 KeyInputFocusManager::KeyInputFocusManager()
52 : mSlotDelegate( this ),
53 mCurrentFocusControl()
55 // Retrieve all the existing widnows
56 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
57 for( auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter )
59 ( *iter ).KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
62 // Get notified when any new scene holder is created afterwards
63 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnSceneHolderCreated );
66 KeyInputFocusManager::~KeyInputFocusManager()
70 void KeyInputFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
72 sceneHolder.KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
75 void KeyInputFocusManager::SetFocus( Toolkit::Control control )
83 if( control == mCurrentFocusControl )
85 // Control already has focus
89 control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
91 Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
92 if( previousFocusControl )
94 // Notify the control that it has lost key input focus
95 GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
98 // Set control to currentFocusControl
99 mCurrentFocusControl = control;
101 // Tell the new actor that it has gained focus.
102 GetImplementation( control ).OnKeyInputFocusGained();
104 // Emit the signal to inform focus change to the application.
105 if ( !mKeyInputFocusChangedSignal.Empty() )
107 mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
111 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
113 if( control == mCurrentFocusControl )
115 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
117 // Notify the control that it has lost key input focus
118 GetImplementation( control ).OnKeyInputFocusLost();
120 mCurrentFocusControl.Reset();
124 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
126 return mCurrentFocusControl;
129 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
131 return mKeyInputFocusChangedSignal;
134 bool KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
136 bool consumed = false;
138 Toolkit::Control control = GetCurrentFocusControl();
141 // Notify the control about the key event
142 consumed = EmitKeyEventSignal( control, event );
148 bool KeyInputFocusManager::EmitKeyEventSignal( Toolkit::Control control, const KeyEvent& event )
150 bool consumed = false;
154 consumed = GetImplementation( control ).EmitKeyEventSignal( event );
156 // if control doesn't consume KeyEvent, give KeyEvent to its parent.
159 Toolkit::Control parent = Toolkit::Control::DownCast( control.GetParent() );
163 consumed = EmitKeyEventSignal( parent, event );
171 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
173 RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
177 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
179 bool connected( true );
180 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
184 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
186 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
190 // signalName does not match any signal
198 } // namespace Internal
200 } // namespace Toolkit