Removed unnecessary codes
[framework/osp/shell.git] / src / FShell_AppWidgetPopup.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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  * @file                FShell_AppWidgetPopup.cpp
18  * @brief               This is the implementation file for the _AppWidgetPopup class.
19  */
20
21 #include <new>
22 #include <Evas.h>
23 #include <Ecore_Evas.h>
24 #include <provider.h>
25 #include <provider_buffer.h>
26 #include <FBaseSysLog.h>
27 #include <FSysSystemTime.h>
28 #include <FBase_StringConverter.h>
29 #include "FUi_UiKeyEvent.h"
30 #include "FUi_IUiEventManager.h"
31 #include "FUi_UiEventManager.h"
32 #include "FUi_ResourceManager.h"
33 #include "FUiAnim_DisplayManager.h"
34 #include "FUiAnim_RootVisualElement.h"
35 #include "FShell_AppWidgetLayer.h"
36 #include "FShell_AppWidgetPopup.h"
37 #include "FShell_AppWidgetPopupPresenter.h"
38 #include "FShell_AppWidgetProviderManagerImpl.h"
39
40 using namespace std;
41 using namespace Tizen::Base;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::System;
44 using namespace Tizen::Ui;
45 using namespace Tizen::Ui::Animations;
46 using namespace Tizen::Ui::Controls;
47
48 namespace Tizen { namespace Shell
49 {
50
51 class _AppWidgetPopupLayer
52         : public _AppWidgetLayer
53 {
54 public:
55         _AppWidgetPopupLayer(const Tizen::Base::String& providerId, const FloatDimension& size);
56         virtual ~_AppWidgetPopupLayer(void);
57
58 private:
59         _AppWidgetPopupLayer(const _AppWidgetPopupLayer& rhs);
60         _AppWidgetPopupLayer& operator =(const _AppWidgetPopupLayer&  rhs);
61
62         virtual result RegisterTouchEventListener(void);
63         virtual int AcquirePixmap(void);
64         virtual result Sync(const FloatDimension& size);
65         virtual void ReleasePixmap(void);
66 };
67
68 _AppWidgetPopupLayer::_AppWidgetPopupLayer(const Tizen::Base::String& providerId, const FloatDimension& size)
69         : _AppWidgetLayer(providerId, size)
70 {
71 }
72
73 _AppWidgetPopupLayer::~_AppWidgetPopupLayer(void)
74 {
75         if (!__isReleased)
76         {
77                 ReleasePixmap();
78                 __isReleased = true;
79         }
80 }
81
82 result
83 _AppWidgetPopupLayer::RegisterTouchEventListener(void)
84 {
85         result r = _AppWidgetProviderManagerImpl::GetInstance()->SetAppWidgetPopupEventListener(*this);
86         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
87
88         return r;
89 }
90
91 int
92 _AppWidgetPopupLayer::AcquirePixmap(void)
93 {
94         long long ticks = 0;
95         SystemTime::GetTicks(ticks);
96         SysLog(NID_SHELL, "%lld", ticks);
97
98         int pixmapId = -1;
99
100         result r = _AppWidgetProviderManagerImpl::GetInstance()->RequestSharedMemoryIdForPD(GetProviderId(), __size.width, __size.height, pixmapId);
101         SysTryReturn(NID_SHELL, r == E_SUCCESS, -1, r, "[%s] Propagating.", GetErrorMessage(r));
102
103         SystemTime::GetTicks(ticks);
104         SysLog(NID_SHELL, "%lld", ticks);
105
106         SysLog(NID_SHELL, "pixmapId(%d) size(%f %f)", pixmapId, __size.width, __size.height);
107
108         return pixmapId;
109 }
110
111 result
112 _AppWidgetPopupLayer::Sync(const FloatDimension& size)
113 {
114         result r = _AppWidgetProviderManagerImpl::GetInstance()->RequestSyncSharedMemoryForPD(GetProviderId());
115         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
116
117         return r;
118 }
119
120 void
121 _AppWidgetPopupLayer::ReleasePixmap(void)
122 {
123         _AppWidgetProviderManagerImpl::GetInstance()->RequestReleaseSharedMemoryForPD(GetProviderId());
124 }
125
126 _AppWidgetPopup::_AppWidgetPopup(void)
127         : __pAppWidgetPopupPresenter(new (std::nothrow) _AppWidgetPopupPresenter(*this))
128         , __pAppWidgetPopupLayer(null)
129         , __pAppWidgetPopupRootVisualElement(null)
130         , __pEventManager(null)
131 {
132         SysTryReturnVoidResult(NID_SHELL, __pAppWidgetPopupPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
133
134         _UiEventManager* pEventManager = _UiEventManager::GetInstance();
135         SysTryReturnVoidResult(NID_SHELL, pEventManager, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
136
137         __pEventManager = pEventManager->GetEventManager();
138         SysTryReturnVoidResult(NID_SHELL, __pEventManager, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
139 }
140
141 _AppWidgetPopup::~_AppWidgetPopup(void)
142 {
143         if (__pEventManager)
144         {
145                 __pEventManager->UnregisterTouchEventHandler(*this);
146         }
147
148         __pAppWidgetPopupLayer = null;
149         __pAppWidgetPopupRootVisualElement = null;
150 }
151
152 _AppWidgetPopup*
153 _AppWidgetPopup::CreateAppWidgetPopupN(void)
154 {
155         unique_ptr<_AppWidgetPopup> pAppWidgetPopup(new (std::nothrow) _AppWidgetPopup());
156         SysTryReturn(NID_SHELL, pAppWidgetPopup, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
157
158         result r = GetLastResult();
159         SysTryReturn(NID_SHELL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
160
161         pAppWidgetPopup->AcquireHandle();
162
163         SetLastResult(E_SUCCESS);
164
165         return pAppWidgetPopup.release();
166 }
167
168 result
169 _AppWidgetPopup::Initialize(const FloatDimension& size)
170 {
171         long long ticks = 0;
172         SystemTime::GetTicks(ticks);
173
174         SysLog(NID_SHELL, "(%d %d) %lld", size.width, size.height, ticks);
175
176         result r = E_SUCCESS;
177
178         __appwidgetSize = size;
179
180         SetSystemWindow(true);
181
182         r = __pAppWidgetPopupPresenter->Initialize();
183         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
184
185         r = CreateRootVisualElement();
186         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
187
188         FloatRectangle bounds(0.0f, 0.0f, size.width, size.height);
189         r = SetBounds(bounds);
190         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
191
192         __pAppWidgetPopupLayer->SetLayerBounds(FloatRectangle(0.0f, 0.0f, 1.0f, 1.0f));
193         __pAppWidgetPopupLayer->SetShowState(false);
194
195         __pAppWidgetPopupRootVisualElement->SetImplicitAnimationEnabled(false);
196         __pAppWidgetPopupRootVisualElement->SetName(L"_AppWidgetPopup");
197         __pAppWidgetPopupRootVisualElement->SetBounds(bounds);
198         __pAppWidgetPopupRootVisualElement->SetShowState(false);
199
200         SetBackgroundColor(Color(0, 0, 0, 0));
201
202         __pEventManager->RegisterTouchEventHandler(*this);
203         r = GetLastResult();
204         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
205
206         return r;
207 }
208
209 result
210 _AppWidgetPopup::SetProviderId(const String& providerId)
211 {
212         __pAppWidgetPopupPresenter->SetProviderId(providerId);
213
214         result r = __pAppWidgetPopupLayer->SetProviderId(providerId);
215         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
216
217         FloatRectangle bounds(0.0f, 0.0f, __appwidgetSize.width, __appwidgetSize.height);
218
219         __pAppWidgetPopupLayer->SetLayerBounds(bounds);
220
221         return r;
222 }
223
224 String
225 _AppWidgetPopup::GetProviderId(void) const
226 {
227         return __pAppWidgetPopupPresenter->GetProviderId();
228 }
229
230 void
231 _AppWidgetPopup::SetAnchorPosition(const FloatPoint& position)
232 {
233         return __pAppWidgetPopupPresenter->SetAnchorPosition(position);
234 }
235
236 FloatPoint
237 _AppWidgetPopup::GetAnchorPosition(void) const
238 {
239         return __pAppWidgetPopupPresenter->GetAnchorPosition();
240 }
241
242 Canvas*
243 _AppWidgetPopup::GetClientAreaCanvasN(void) const
244 {
245         Canvas* pCanvas = GetCanvasN(GetClientBoundsF());
246         result r = GetLastResult();
247         SysTryReturn(NID_SHELL, pCanvas, null, r, "[%s] Propagating.", GetErrorMessage(r));
248
249         return pCanvas;
250 }
251
252 FloatPoint
253 _AppWidgetPopup::TranslateToClientAreaPosition(const FloatPoint& controlPosition) const
254 {
255         FloatRectangle leftTop = GetClientBoundsF();
256
257         return FloatPoint(controlPosition.x - leftTop.x, controlPosition.y - leftTop.y);
258 }
259
260 FloatPoint
261 _AppWidgetPopup::TranslateToControlPosition(const FloatPoint& clientPosition) const
262 {
263         FloatRectangle leftTop = GetClientBoundsF();
264
265         return FloatPoint(clientPosition.x + leftTop.x, clientPosition.y + leftTop.y);
266 }
267
268 result
269 _AppWidgetPopup::SetLayerShowState(bool showState)
270 {
271         result r = E_SUCCESS;
272
273         if (__pAppWidgetPopupRootVisualElement)
274         {
275                 _AppWidgetPopupLayer* pLayer = dynamic_cast<_AppWidgetPopupLayer*>(__pAppWidgetPopupRootVisualElement->GetNativeLayer());
276                 SysTryReturn(NID_SHELL, pLayer, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
277
278                 r = pLayer->SetShowState(showState);
279                 SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
280
281                 __pAppWidgetPopupRootVisualElement->SetShowState(showState);
282         }
283
284         return r;
285 }
286
287 result
288 _AppWidgetPopup::SetLayerBounds(const FloatRectangle& bounds)
289 {
290         result r = E_SUCCESS;
291
292         if (__pAppWidgetPopupRootVisualElement && !GetProviderId().IsEmpty())
293         {
294                 _AppWidgetPopupLayer* pLayer = dynamic_cast<_AppWidgetPopupLayer*>(__pAppWidgetPopupRootVisualElement->GetNativeLayer());
295                 r = GetLastResult();
296                 SysTryReturn(NID_SHELL, pLayer, r, r, "[%s] Propagating.", GetErrorMessage(r));
297
298                 pLayer->SetLayerBounds(bounds);
299                 __pAppWidgetPopupRootVisualElement->SetBounds(bounds);
300         }
301
302         return r;
303 }
304
305 FloatRectangle
306 _AppWidgetPopup::GetAppWidgetPopupClientBounds(void) const
307 {
308         int leftMargin = 0;
309         int rightMargin = 0;
310         int topMargin = 0;
311         int bottomMargin = 0;
312         _ControlOrientation orientation = GetOrientation();
313
314         GET_SHAPE_CONFIG(APPWIDGET::POPUP_LEFT_MARGIN, orientation, leftMargin);
315         GET_SHAPE_CONFIG(APPWIDGET::POPUP_RIGHT_MARGIN, orientation, rightMargin);
316         GET_SHAPE_CONFIG(APPWIDGET::POPUP_TOP_MARGIN, orientation, topMargin);
317         GET_SHAPE_CONFIG(APPWIDGET::POPUP_BOTTOM_MARGIN, orientation, bottomMargin);
318
319         FloatRectangle bounds = GetBoundsF();
320
321         FloatRectangle clientBounds(bounds.x + leftMargin, bounds.y + topMargin, bounds.width - leftMargin - rightMargin, bounds.height - topMargin - bottomMargin);
322
323         SysLog(NID_SHELL, "[%d %d %d %d]", clientBounds.x, clientBounds.y, clientBounds.width, clientBounds.height);
324
325         return clientBounds;
326 }
327
328 void
329 _AppWidgetPopup::UpdateClientBounds(const FloatDimension& size, FloatRectangle& clientBounds)
330 {
331         clientBounds = GetAppWidgetPopupClientBounds();
332 }
333
334 result
335 _AppWidgetPopup::CreateLayer(void)
336 {
337         unique_ptr<_AppWidgetPopupLayer> pLayer(new (std::nothrow) _AppWidgetPopupLayer(GetProviderId(), FloatDimension(1.0f, 1.0f)));
338         SysTryReturn(NID_SHELL, pLayer, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
339
340         result r = pLayer->Construct();
341         SysTryReturn(NID_SHELL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
342
343         __pAppWidgetPopupLayer = pLayer.release();
344         __pAppWidgetPopupRootVisualElement = __pAppWidgetPopupLayer->GetRootVisualElement();
345
346         SetLayer(*__pAppWidgetPopupLayer);
347         SetRootVisualElement(*__pAppWidgetPopupRootVisualElement);
348
349         return r;
350 }
351
352 void
353 _AppWidgetPopup::OnActivated(void)
354 {
355         result r = SetLayerShowState(true);
356         SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
357 }
358
359 void
360 _AppWidgetPopup::OnBoundsChanged(void)
361 {
362         SetLayerBounds(GetBoundsF());
363
364         SetClientBounds(GetAppWidgetPopupClientBounds());
365 }
366
367 void
368 _AppWidgetPopup::OnDraw(void)
369 {
370         if (__pAppWidgetPopupPresenter)
371         {
372                 __pAppWidgetPopupPresenter->Draw();
373         }
374 }
375
376 }} // Tizen::Shell