Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / FUi_UiKeyEvent.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FUi_UiKeyEvent.cpp
19  * @brief               This is the implementation file for the _UiKeyEvent class.
20  */
21
22 #include <FBaseResult.h>
23 #include <FBaseSysLog.h>
24 #include <FUiKeyEventInfo.h>
25 #include "FUi_UiKeyEvent.h"
26 #include "FUi_IKeyEventListener.h"
27 #include "FUi_IKeyEventPreviewer.h"
28 #include "FUi_Control.h"
29 #include "FUi_ControlImpl.h"
30
31 namespace Tizen { namespace Ui
32 {
33
34 _KeyInfo::_KeyInfo(KeyState keyState, _KeyCode keyCode, int keyModifier, void* pUserData)
35         : __keyState(keyState)
36         , __keyCode(keyCode)
37         , __keyModifier(keyModifier)
38         , __pUserData(pUserData)
39 {
40 }
41
42 _KeyInfo::_KeyInfo(const _KeyInfo& rhs)
43         : __keyState(rhs.__keyState)
44         , __keyCode(rhs.__keyCode)
45         , __keyModifier(rhs.__keyModifier)
46         , __pUserData(rhs.__pUserData)
47 {
48 }
49
50 _KeyInfo&
51 _KeyInfo::operator =(const _KeyInfo& rhs)
52 {
53         if (this != &rhs)
54         {
55                 __keyState = rhs.__keyState;
56                 __keyCode = rhs.__keyCode;
57                 __keyModifier = rhs.__keyModifier;
58                 __pUserData = rhs.__pUserData;
59         }
60
61         return *this;
62 }
63
64 _KeyInfo::~_KeyInfo(void)
65 {
66 }
67
68 void
69 _KeyInfo::SetKeyState(KeyState keyState)
70 {
71         __keyState = keyState;
72 }
73
74 KeyState
75 _KeyInfo::GetKeyState(void) const
76 {
77         return __keyState;
78 }
79
80 void
81 _KeyInfo::SetKeyCode(_KeyCode keyCode)
82 {
83         __keyCode = keyCode;
84 }
85
86 _KeyCode
87 _KeyInfo::GetKeyCode(void) const
88 {
89         return __keyCode;
90 }
91
92 void
93 _KeyInfo::SetKeyModifier(int keyModifier)
94 {
95         __keyModifier = keyModifier;
96 }
97
98 int
99 _KeyInfo::GetKeyModifier(void) const
100 {
101         return __keyModifier;
102 }
103
104 void
105 _KeyInfo::SetUserData(void* pUserData)
106 {
107         __pUserData = pUserData;
108 }
109
110 void*
111 _KeyInfo::GetUserData(void) const
112 {
113         return __pUserData;
114 }
115
116 _UiKeyEvent::_UiKeyEvent(const _UiObjectHandle& destination, const _KeyInfo& keyInfo, _UiEventRouteType routeType, const _UiObjectHandle& source)
117         : _UiEvent(destination, source, routeType)
118         , __keyInfo(keyInfo)
119 {
120 }
121
122 _UiKeyEvent::~_UiKeyEvent(void)
123 {
124 }
125
126 _UiKeyEvent::_UiKeyEvent(const _UiKeyEvent& rhs)
127         : _UiEvent(rhs)
128         , __keyInfo(rhs.__keyInfo)
129 {
130 }
131
132 _UiKeyEvent&
133 _UiKeyEvent::operator =(const _UiKeyEvent& rhs)
134 {
135         _UiEvent::operator =(rhs);
136
137         if (this != &rhs)
138         {
139                 __keyInfo = rhs.__keyInfo;
140         }
141
142         return *this;
143 }
144
145 _KeyInfo*
146 _UiKeyEvent::GetKeyInfo(void) const
147 {
148         return const_cast<_KeyInfo*>(&__keyInfo);
149 }
150
151 _UiKeyEvent*
152 _UiKeyEvent::CloneN(void) const
153 {
154         return new (std::nothrow) _UiKeyEvent(*this);
155 }
156
157 _UiEventType
158 _UiKeyEvent::GetEventType(void) const
159 {
160         return _UI_EVENT_KEY;
161 }
162
163 void
164 _UiKeyEvent::SetKeyState(const KeyState keyState)
165 {
166         __keyInfo.SetKeyState(keyState);
167 }
168
169 KeyState
170 _UiKeyEvent::GetKeyState(void) const
171 {
172         return __keyInfo.GetKeyState();
173 }
174
175 void
176 _UiKeyEvent::SetKeyCode(const _KeyCode keyCode)
177 {
178         __keyInfo.SetKeyCode(keyCode);
179 }
180
181 _KeyCode
182 _UiKeyEvent::GetKeyCode(void) const
183 {
184         return __keyInfo.GetKeyCode();
185 }
186
187 void
188 _UiKeyEvent::SetKeyModifier(int keyModifier)
189 {
190         __keyInfo.SetKeyModifier(keyModifier);
191 }
192
193 int
194 _UiKeyEvent::GetKeyModifier(void) const
195 {
196         return __keyInfo.GetKeyModifier();
197 }
198
199 bool
200 _UiKeyEvent::IsEventEnabled(const _Control& control) const
201 {
202         if (!_UiEvent::IsEventEnabled(control))
203         {
204                 return false;
205         }
206
207         if (( GetKeyInfo()->GetKeyCode() == _KEY_CONTEXT_MENU )
208                         || ( GetKeyInfo()->GetKeyCode() == _KEY_BACK ))
209         {
210         }
211         else
212         {
213                 if (!control.IsInputEventEnabled())
214                 {
215                         return false;
216                 }
217         }
218
219         return true;
220 }
221
222 result
223 _UiKeyEvent::OnPreviewEventProcessing(const _Control& control, bool& isFiltered)
224 {
225         result r = E_SUCCESS;
226
227         _IKeyEventPreviewer* pKeyEventPreviewer = control.GetPropagatedKeyEventListener();
228         SysTryReturn(NID_UI, pKeyEventPreviewer, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
229
230         const _Control* pTarget = GetControl(GetDestination());
231         SysTryReturn(NID_UI, pTarget, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
232
233         // call a listener method by the key state
234         switch (GetKeyState())
235         {
236         case KEY_PRESSED:
237                 {
238                         isFiltered = pKeyEventPreviewer->OnPreviewKeyPressed(*pTarget, __keyInfo);
239                 }
240                 break;
241
242         case KEY_RELEASED:
243                 {
244                         isFiltered = pKeyEventPreviewer->OnPreviewKeyReleased(*pTarget, __keyInfo);
245                 }
246                 break;
247
248         default:
249                 //SysLogI(NID_UI, "unable to determine the key event type.");
250                 r = E_SYSTEM;
251                 break;
252         }
253
254         return r;
255 }
256
257 result
258 _UiKeyEvent::OnEventProcessing(const _Control& control, bool& isFiltered)
259 {
260         if (isFiltered)
261         {
262                 return E_SUCCESS;
263         }
264
265         _IKeyEventListener* pKeyEventListener = control.GetPropagatedKeyEventListener();
266         SysTryReturn(NID_UI, pKeyEventListener, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
267
268         return FireListener(pKeyEventListener, isFiltered);
269 }
270
271 result
272 _UiKeyEvent::OnListenerProcessing(const _IUiEventListener& listener, bool& isFiltered)
273 {
274         _IKeyEventListener* pKeyEventListener = dynamic_cast <_IKeyEventListener*>(const_cast <_IUiEventListener*>(&listener));
275         SysTryReturn(NID_UI, pKeyEventListener, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
276
277         return FireListener(pKeyEventListener, isFiltered);
278 }
279
280 result
281 _UiKeyEvent::FireListener(const _IKeyEventListener* pListener, bool& isFiltered)
282 {
283         _IKeyEventListener* pKeyEventListener = const_cast <_IKeyEventListener*>(pListener);
284         SysTryReturn(NID_UI, pKeyEventListener, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
285
286         result r = E_SUCCESS;
287
288         const _Control* pTarget = GetControl(GetDestination());
289         SysTryReturn(NID_UI, pTarget, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
290
291         // call a listener method by the key state
292         switch (GetKeyState())
293         {
294         case KEY_PRESSED:
295                 isFiltered = pKeyEventListener->OnKeyPressed(*pTarget, __keyInfo);
296                 break;
297
298         case KEY_RELEASED:
299                 isFiltered = pKeyEventListener->OnKeyReleased(*pTarget, __keyInfo);
300                 break;
301
302         // 2.0 compatibility for key simulate
303         case KEY_LONGPRESSED:
304                 {
305                         _ControlImpl* pTargetImpl = static_cast<_ControlImpl*>(pTarget->GetUserData());
306                         if (pTargetImpl)
307                         {
308                                 pTargetImpl->GenerateKeyEvent(KEY_LONGPRESSED, GetKeyCode());
309                         }
310                 }
311                 break;
312
313         default:
314                 //SysLogI(NID_UI, "unable to determine the key event type.");
315                 r = E_SYSTEM;
316                 break;
317         }
318
319         return r;
320 }
321
322 }} // Tizen::Ui