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>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali/integration-api/debug.h>
45 const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
49 KeyInputFocusManager::KeyInputFocusManager()
50 : mSlotDelegate( this ),
51 mCurrentFocusControl()
53 DevelStage::KeyEventGeneratedSignal( Stage::GetCurrent() ).Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
56 KeyInputFocusManager::~KeyInputFocusManager()
60 void KeyInputFocusManager::SetFocus( Toolkit::Control control )
68 if( control == mCurrentFocusControl )
70 // Control already has focus
74 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 GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
83 // Set control to currentFocusControl
84 mCurrentFocusControl = control;
86 // Tell the new actor that it has gained focus.
87 GetImplementation( control ).OnKeyInputFocusGained();
89 // Emit the signal to inform focus change to the application.
90 if ( !mKeyInputFocusChangedSignal.Empty() )
92 mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
96 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
98 if( control == mCurrentFocusControl )
100 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
102 // Notify the control that it has lost key input focus
103 GetImplementation( control ).OnKeyInputFocusLost();
105 mCurrentFocusControl.Reset();
109 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
111 return mCurrentFocusControl;
114 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
116 return mKeyInputFocusChangedSignal;
119 bool KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
121 bool consumed = false;
123 Toolkit::Control control = GetCurrentFocusControl();
126 // Notify the control about the key event
127 consumed = EmitKeyEventSignal( control, event );
133 bool KeyInputFocusManager::EmitKeyEventSignal( Toolkit::Control control, const KeyEvent& event )
135 bool consumed = false;
139 consumed = GetImplementation( control ).EmitKeyEventSignal( event );
141 // if control doesn't consume KeyEvent, give KeyEvent to its parent.
144 Toolkit::Control parent = Toolkit::Control::DownCast( control.GetParent() );
148 consumed = EmitKeyEventSignal( parent, event );
156 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
158 RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
162 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
164 bool connected( true );
165 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
169 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
171 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
175 // signalName does not match any signal
183 } // namespace Internal
185 } // namespace Toolkit