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