Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_TouchGestureDetectorImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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_TouchGestureDetectorImpl.cpp
19  * @brief               This is the implementation file for %_TouchGestureDetectorImpl class
20  * @version             2.0
21  *
22  * This file contains the implementation of %_TouchGestureDetectorImpl class.
23  *
24  */
25
26 #include <FUiTouchEventInfo.h>
27 #include <FBaseSysLog.h>
28 #include "FUi_TouchGestureDetectorImpl.h"
29 #include "FUi_ITouchEventListener.h"
30 #include "FUi_TouchEventArg.h"
31 #include "FUi_TouchManager.h"
32
33 using namespace Tizen::Base::Collection;
34
35 namespace Tizen { namespace Ui
36 {
37
38 _TouchGestureDetectorImpl::_TouchGestureDetectorImpl(TouchGestureDetector* pPublic, _TouchGestureDetector* pCore)
39         : __pPublicGestureEventListenerList(null)
40 {
41         __pPublicGestureDetector = pPublic;
42         __pCoreGestureDetector = pCore;
43
44         __pPublicGestureEventListenerList = new (std::nothrow) LinkedListT<ITouchGestureEventListener*>;
45         SysTryReturnVoidResult(NID_UI, __pPublicGestureEventListenerList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
46
47         pCore->SetUserData(this);
48         pCore->SetDelegate(*this);
49
50         ClearLastResult();
51 }
52
53 _TouchGestureDetectorImpl*
54 _TouchGestureDetectorImpl::CreateInstanceN(TouchGestureDetector* pPublic)
55 {
56         ClearLastResult();
57
58         _TouchGestureDetector* pCore = null;
59         pCore = new (std::nothrow) _TouchGestureDetector;
60         SysTryReturn(NID_UI, pCore, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
61
62         _TouchGestureDetectorImpl* pImpl = new (std::nothrow) _TouchGestureDetectorImpl(pPublic, pCore);
63         SysTryCatch(NID_UI, pImpl, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
64
65         ClearLastResult();
66
67         return pImpl;
68
69 CATCH:
70         delete pCore;
71
72         return null;
73 }
74
75 _TouchGestureDetectorImpl::~_TouchGestureDetectorImpl(void)
76 {
77         delete __pPublicGestureEventListenerList;
78         __pPublicGestureEventListenerList = null;
79
80         delete __pCoreGestureDetector;
81         __pCoreGestureDetector = null;
82
83         ClearLastResult();
84 }
85
86 const _TouchGestureDetector&
87 _TouchGestureDetectorImpl::GetCore(void) const
88 {
89         return *__pCoreGestureDetector;
90 }
91
92 _TouchGestureDetector&
93 _TouchGestureDetectorImpl::GetCore(void)
94 {
95         return *__pCoreGestureDetector;
96 }
97
98 const TouchGestureDetector&
99 _TouchGestureDetectorImpl::GetPublic(void) const
100 {
101         return *__pPublicGestureDetector;
102 }
103
104 TouchGestureDetector&
105 _TouchGestureDetectorImpl::GetPublic(void)
106 {
107         return *__pPublicGestureDetector;
108 }
109
110 _TouchGestureDetectorImpl*
111 _TouchGestureDetectorImpl::GetInstance(TouchGestureDetector& gestureDetector)
112 {
113         return gestureDetector.__pTouchGestureDetectorImpl;
114 }
115
116 const _TouchGestureDetectorImpl*
117 _TouchGestureDetectorImpl::GetInstance(const TouchGestureDetector& gestureDetector)
118 {
119         return gestureDetector.__pTouchGestureDetectorImpl;
120 }
121
122 result
123 _TouchGestureDetectorImpl::AddGestureEventListener(ITouchGestureEventListener& listener)
124 {
125         bool exist = __pPublicGestureEventListenerList->Contains(const_cast<ITouchGestureEventListener*>(&listener));
126         if (exist)
127         {
128                 return E_OBJ_ALREADY_EXIST;
129         }
130
131         result r = __pPublicGestureEventListenerList->Add(&listener);
132         SysTryReturnResult(NID_UI,  r == E_SUCCESS, r, "%s Propagating", GetErrorMessage(r));
133
134         return E_SUCCESS;
135 }
136
137 result
138 _TouchGestureDetectorImpl::RemoveGestureEventListener(ITouchGestureEventListener& listener)
139 {
140         result r =  __pPublicGestureEventListenerList->Remove(&listener);
141         SysTryReturnResult(NID_UI, r == E_SUCCESS, r, "%s Propagating", GetErrorMessage(r));
142
143         return E_SUCCESS;
144 }
145
146 IListT <ITouchGestureEventListener*>*
147 _TouchGestureDetectorImpl::GetGestureEventListenerList(void) const
148 {
149         return __pPublicGestureEventListenerList;
150 }
151
152 result
153 _TouchGestureDetectorImpl::SetDetectorState(TouchGestureDetectorState state)
154 {
155         GetCore().SetDetectorState(static_cast<_TouchGestureDetectorState>(state));
156
157         return E_SUCCESS;
158 }
159
160 TouchGestureDetectorState
161 _TouchGestureDetectorImpl::GetDetectorState(void) const
162 {
163         ClearLastResult();
164
165         return static_cast<TouchGestureDetectorState>(GetCore().GetDetectorState());
166 }
167
168 Control*
169 _TouchGestureDetectorImpl::GetControl(void) const
170 {
171         _Control* pControl = GetCore().GetControl();
172         SysTryReturn(NID_UI, pControl, null, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
173
174         ClearLastResult();
175
176         _ControlImpl* pControlImpl = static_cast<_ControlImpl*>(pControl->GetUserData());
177         return &(pControlImpl->GetPublic());
178 }
179
180 result
181 _TouchGestureDetectorImpl::SetDelayTouchEventEnabled(bool enable)
182 {
183         GetCore().SetDelayTouchEventEnabled(enable);
184
185         return E_SUCCESS;
186 }
187
188 bool
189 _TouchGestureDetectorImpl::IsDelayTouchEventEnabled(void) const
190 {
191         ClearLastResult();
192
193         return GetCore().IsDelayTouchEventEnabled();
194 }
195
196 result
197 _TouchGestureDetectorImpl::SetCancelTouchEventOnSuccessEnabled(bool enable)
198 {
199         GetCore().SetCancelTouchEventOnSuccessEnabled(enable);
200
201         return E_SUCCESS;
202 }
203
204 bool
205 _TouchGestureDetectorImpl::IsCancelTouchEventOnSuccessEnabled(void) const
206 {
207         ClearLastResult();
208
209         return GetCore().IsCancelTouchEventOnSuccessEnabled();
210 }
211
212 result
213 _TouchGestureDetectorImpl::StartOnFailureOf(const TouchGestureDetector& gesture)
214 {
215         const _TouchGestureDetectorImpl* pImpl = GetInstance(gesture);
216         SysTryReturnResult(NID_UI, pImpl, E_INVALID_ARG, "[E_INVALID_ARG] This instance is invalid.");
217
218         result r = GetCore().StartOnFailureOf(pImpl->GetCore());
219         SysTryLog(NID_UI, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
220
221         return r;
222 }
223
224 bool
225 _TouchGestureDetectorImpl::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
226 {
227         //public
228         TouchGestureDetector& publicDetector = GetPublic();
229         Control* pControl = static_cast<Control*>(source.GetUserData());
230         _TouchEventArg* pEventArg = null;
231
232         if (pControl)
233         {
234                 TouchEventInfo publicTouchInfo;
235
236                 pEventArg = GetTouchEventArgN(*pControl, touchinfo);
237                 SysTryReturn(NID_UI, pEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
238
239                 publicTouchInfo.Construct(*pEventArg);
240
241                 publicDetector.OnTouchPressed(*pControl, publicTouchInfo);
242         }
243
244         //core
245         _TouchGestureDetector& coreDetector = GetCore();
246
247         coreDetector.OnTouchPressed(source, touchinfo);
248
249         delete pEventArg;
250         return false;
251 }
252
253 bool
254 _TouchGestureDetectorImpl::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
255 {
256         //public
257         TouchGestureDetector& publicDetector = GetPublic();
258         Control* pControl = static_cast<Control*>(source.GetUserData());
259         _TouchEventArg* pEventArg = null;
260
261         if (pControl)
262         {
263                 TouchEventInfo publicTouchInfo;
264
265                 pEventArg = GetTouchEventArgN(*pControl, touchinfo);
266                 SysTryReturn(NID_UI, pEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
267
268                 publicTouchInfo.Construct(*pEventArg);
269
270                 publicDetector.OnTouchMoved(*pControl, publicTouchInfo);
271         }
272
273         //core
274         _TouchGestureDetector& coreDetector = GetCore();
275
276         coreDetector.OnTouchMoved(source, touchinfo);
277
278         delete pEventArg;
279         return false;
280 }
281
282 bool
283 _TouchGestureDetectorImpl::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
284 {
285         //public
286         TouchGestureDetector& publicDetector = GetPublic();
287         Control* pControl = static_cast<Control*>(source.GetUserData());
288         _TouchEventArg* pEventArg = null;
289
290         if (pControl)
291         {
292                 TouchEventInfo publicTouchInfo;
293
294                 pEventArg = GetTouchEventArgN(*pControl, touchinfo);
295                 SysTryReturn(NID_UI, pEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
296
297                 publicTouchInfo.Construct(*pEventArg);
298
299                 publicDetector.OnTouchReleased(*pControl, publicTouchInfo);
300         }
301
302         //core
303         _TouchGestureDetector& coreDetector = GetCore();
304
305         coreDetector.OnTouchReleased(source, touchinfo);
306
307         delete pEventArg;
308         return false;
309 }
310
311 bool
312 _TouchGestureDetectorImpl::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
313 {
314         //public
315         TouchGestureDetector& publicDetector = GetPublic();
316         Control* pControl = static_cast<Control*>(source.GetUserData());
317         _TouchEventArg* pEventArg = null;
318
319         if (pControl)
320         {
321                 TouchEventInfo publicTouchInfo;
322
323                 pEventArg = GetTouchEventArgN(*pControl, touchinfo);
324                 SysTryReturn(NID_UI, pEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
325
326                 publicTouchInfo.Construct(*pEventArg);
327
328                 publicDetector.OnTouchCanceled(*pControl, publicTouchInfo);
329         }
330
331         //core
332         _TouchGestureDetector& coreDetector = GetCore();
333         coreDetector.OnTouchCanceled(source, touchinfo);
334
335         delete pEventArg;
336         return false;
337 }
338
339
340 _TouchEventArg*
341 _TouchGestureDetectorImpl::GetTouchEventArgN(const Control& source, const _TouchInfo& touchInfo)
342 {
343         ClearLastResult();
344
345         _TouchManager* pTouchManager = _TouchManager::GetInstance();
346         SysTryReturn(NID_UI, pTouchManager, null, E_SYSTEM, "[E_SYSTEM] _TouchManager does not exist.");
347
348         _TouchEventArg* pEventArg = new (std::nothrow) _TouchEventArg(source, touchInfo.GetTouchStatus());
349         SysTryReturn(NID_UI, pEventArg, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
350
351         pEventArg->SetTouchPosition(touchInfo.GetPointId(), pTouchManager->GetStartPoint(touchInfo.GetPointId()).x,
352                         pTouchManager->GetStartPoint(touchInfo.GetPointId()).y,
353                 touchInfo.GetCurrentPosition().x, touchInfo.GetCurrentPosition().y);
354
355         return pEventArg;
356 }
357
358 } }  //Tizen::Ui