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