Changes after adaptor integration-api folder move
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyinput-focus-manager-impl.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "keyinput-focus-manager-impl.h"
20
21 // EXTERNAL INCLUDES
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-toolkit/public-api/controls/control-impl.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/adaptor-framework/adaptor.h>
29 #include <dali/integration-api/adaptor-framework/scene-holder.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
43 // Signals
44
45 const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
46
47 }
48
49 KeyInputFocusManager::KeyInputFocusManager()
50 : mSlotDelegate( this ),
51   mCurrentFocusControl()
52 {
53   // Retrieve all the existing widnows
54   Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
55   for( auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter )
56   {
57     ( *iter ).KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
58   }
59
60   // Get notified when any new scene holder is created afterwards
61   Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnSceneHolderCreated );
62 }
63
64 KeyInputFocusManager::~KeyInputFocusManager()
65 {
66 }
67
68 void KeyInputFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
69 {
70   sceneHolder.KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
71 }
72
73 void KeyInputFocusManager::SetFocus( Toolkit::Control control )
74 {
75   if( !control )
76   {
77     // No-op
78     return;
79   }
80
81   if( control == mCurrentFocusControl )
82   {
83     // Control already has focus
84     return;
85   }
86
87   control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
88
89   Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
90   if( previousFocusControl )
91   {
92     // Notify the control that it has lost key input focus
93     GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
94   }
95
96   // Set control to currentFocusControl
97   mCurrentFocusControl = control;
98
99   // Tell the new actor that it has gained focus.
100   GetImplementation( control ).OnKeyInputFocusGained();
101
102   // Emit the signal to inform focus change to the application.
103   if ( !mKeyInputFocusChangedSignal.Empty() )
104   {
105     mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
106   }
107 }
108
109 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
110 {
111   if( control == mCurrentFocusControl )
112   {
113     control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
114
115     // Notify the control that it has lost key input focus
116     GetImplementation( control ).OnKeyInputFocusLost();
117
118     mCurrentFocusControl.Reset();
119   }
120 }
121
122 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
123 {
124   return mCurrentFocusControl;
125 }
126
127 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
128 {
129   return mKeyInputFocusChangedSignal;
130 }
131
132 bool KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
133 {
134   bool consumed = false;
135
136   Toolkit::Control control = GetCurrentFocusControl();
137   if( control )
138   {
139     // Notify the control about the key event
140     consumed = EmitKeyEventSignal( control, event );
141   }
142
143   return consumed;
144 }
145
146 bool KeyInputFocusManager::EmitKeyEventSignal( Toolkit::Control control, const KeyEvent& event )
147 {
148   bool consumed = false;
149
150   if( control )
151   {
152     consumed = GetImplementation( control ).EmitKeyEventSignal( event );
153
154     // if control doesn't consume KeyEvent, give KeyEvent to its parent.
155     if( !consumed )
156     {
157       Toolkit::Control parent = Toolkit::Control::DownCast( control.GetParent() );
158
159       if( parent )
160       {
161         consumed = EmitKeyEventSignal( parent, event );
162       }
163     }
164   }
165
166   return consumed;
167 }
168
169 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
170 {
171   RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
172 }
173
174
175 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
176 {
177   bool connected( true );
178   KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
179
180   if( manager )
181   {
182     if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
183     {
184       manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
185     }
186     else
187     {
188       // signalName does not match any signal
189       connected = false;
190     }
191   }
192
193   return connected;
194 }
195
196 } // namespace Internal
197
198 } // namespace Toolkit
199
200 } // namespace Dali