0386b6bd4e43e43461545977fe4479ed2b946a49
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_Indicator.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                FUiCtrl_Indicator.cpp
19  * @brief               This is the implementation file for the _Form class.
20  */
21
22 #include <FGrpRectangle.h>
23 #include <FGrp_CoordinateSystem.h>
24 #include <FUiAnim_VisualElementSurfaceImpl.h>
25 #include "FUiAnim_VisualElement.h"
26 #include "FUiAnim_VisualElementImpl.h"
27 #include "FUiAnim_EflNode.h"
28 #include "FUi_EcoreEvasMgr.h"
29 #include "FUi_EcoreEvas.h"
30 #include "FUi_ResourceManager.h"
31 #include "FUi_CoordinateSystemUtils.h"
32 #include "FUi_Window.h"
33 #include "FUiCtrl_Form.h"
34 #include "FUiCtrl_Indicator.h"
35 #include "FUiCtrl_IndicatorManager.h"
36
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Ui;
40 using namespace Tizen::Ui::Animations;
41
42 namespace Tizen { namespace Ui { namespace Controls
43 {
44
45 static const char* pPortraitSvcName = "elm_indicator_portrait";
46 static const char* pLandScapeSvcName = "elm_indicator_landscape";
47 static const char* pPortPublicKey = "__Plug_Ecore_Evas_Port";
48 static const char* pLandPublicKey = "__Plug_Ecore_Evas_Land";
49 static const char* pIndicatorKey = "Inidcator_Manager_Object";
50 static const int CONNECTION_INTERVAL = 1000;
51
52 _Indicator*
53 _Indicator::CreateIndicator(void)
54 {
55         _Indicator* pIndicator = new (std::nothrow) _Indicator;
56         SysTryReturn(NID_UI_CTRL, pIndicator, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
57         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
58
59 CATCH:
60         delete pIndicator;
61         return null;
62 }
63
64 _Indicator::_Indicator(void)
65         : __pPortraitIndicatorEvasObject(null)
66         , __pLandscapeIndicatorEvasObject(null)
67         , __pConnectionTimer(null)
68         , __pPortraitVisualElement(null)
69         , __pLandscapeVisualElement(null)
70         , __pCurrentVisualElement(null)
71         , __showstate(false)
72         , __opacity(_INDICATOR_OPACITY_OPAQUE)
73 {
74         SetBackgroundColor(0xff343432);
75
76         AcquireHandle();
77 }
78
79 _Indicator::~_Indicator(void)
80 {
81         if (__pConnectionTimer != null)
82         {
83                 __pConnectionTimer->Cancel();
84                 delete __pConnectionTimer;
85                 __pConnectionTimer = null;
86         }
87 }
88
89 result
90 _Indicator::SetIndicatorShowState(bool state, bool hide)
91 {
92         result r = E_SUCCESS;
93
94         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
95         SysTryReturnResult(NID_UI_CTRL, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] Unable to get evas");
96
97         _Window* pWindow = GetRootWindow();
98
99         if (pWindow)
100         {
101                 if (hide)
102                 {
103                         r = pEcoreEvas->SetIndicatorShowState(*pWindow, true);
104                 }
105                 else
106                 {
107                         r = pEcoreEvas->SetIndicatorShowState(*pWindow, state);
108                 }
109         }
110
111         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
112
113         __showstate = state;
114
115         SetVisibleState(state);
116
117         if (state)
118         {
119                 SetIndicatorOpacity(__opacity);
120         }
121
122         return r;
123 }
124
125 result
126 _Indicator::SetIndicatorOpacity(_IndicatorOpacity opacity)
127 {
128         result r = E_SUCCESS;
129
130         if (opacity == _INDICATOR_OPACITY_OPAQUE)
131         {
132                 SetBackgroundColor(0xff343432);
133         }
134         else if (opacity == _INDICATOR_OPACITY_TRANSLUCENT)
135         {
136                 SetBackgroundColor(0x7f000000);
137         }
138
139         __opacity = opacity;
140
141         return r;
142 }
143
144 bool
145 _Indicator::GetIndicatorShowState(void) const
146 {
147         return __showstate;
148 }
149
150 _IndicatorOpacity
151 _Indicator::GetIndicatorOpacity(void) const
152 {
153         return __opacity;
154 }
155
156 Rectangle
157 _Indicator::GetIndicatorBounds(void) const
158 {
159         const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
160         const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
161
162         _ControlOrientation orientation = GetOrientation();
163
164         Rectangle indicatorbounds(0, 0, 0, 0);
165
166         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
167         {
168                 indicatorbounds.width = portraitSize.width;
169         }
170         else
171         {
172                 indicatorbounds.width = landscapeSize.width;
173         }
174         GET_SHAPE_CONFIG(FORM::INDICATOR_HEIGHT, GetOrientation(), indicatorbounds.height);
175
176         _CoordinateSystem* pCoordSystem = _CoordinateSystem::GetInstance();
177         SysTryReturn(NID_UI_CTRL, pCoordSystem, Rectangle(-1, -1, -1, -1), E_SYSTEM, "[E_SYSTEM] Coordinate system load failed.");
178
179         _ICoordinateSystemTransformer* pXformer = pCoordSystem->GetTransformer();
180         SysTryReturn(NID_UI_CTRL, pXformer, Rectangle(-1, -1, -1, -1), E_SYSTEM, "[E_SYSTEM] Coordinate system load failed.");
181
182         indicatorbounds = pXformer->Transform(indicatorbounds);
183
184         return indicatorbounds;
185 }
186
187 FloatRectangle
188 _Indicator::GetIndicatorBoundsF(void) const
189 {
190         const FloatDimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSizeF();
191         const FloatDimension landscapeSize = FloatDimension(portraitSize.height, portraitSize.width);
192
193         _ControlOrientation orientation = GetOrientation();
194
195         FloatRectangle indicatorbounds(0.0f, 0.0f, 0.0f, 0.0f);
196
197         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
198         {
199                 indicatorbounds.width = portraitSize.width;
200         }
201         else
202         {
203                 indicatorbounds.width = landscapeSize.width;
204         }
205         GET_SHAPE_CONFIG(FORM::INDICATOR_HEIGHT, GetOrientation(), indicatorbounds.height);
206
207         _CoordinateSystem* pCoordSystem = _CoordinateSystem::GetInstance();
208         SysTryReturn(NID_UI_CTRL, pCoordSystem, FloatRectangle(-1.0, -1.0, -1.0, -1.0), E_SYSTEM, "[E_SYSTEM] Coordinate system load failed.");
209
210         _ICoordinateSystemTransformer* pXformer = pCoordSystem->GetTransformer();
211         SysTryReturn(NID_UI_CTRL, pXformer, FloatRectangle(-1.0, -1.0, -1.0, -1.0), E_SYSTEM, "[E_SYSTEM] Coordinate system load failed.");
212
213         indicatorbounds = pXformer->Transform(indicatorbounds);
214
215         return indicatorbounds;
216 }
217
218 result
219 _Indicator::AddIndicatorObject(_Form* pForm)
220 {
221         result r = E_SUCCESS;
222
223         _Window* pWindow = GetRootWindow();
224         SysTryReturn(NID_UI_CTRL, pWindow, null,  E_INVALID_STATE, "[E_INVALID_STATE] Indicator is not attached main tree.");
225
226         __pPortraitVisualElement =      _IndicatorManager::GetInstance()->GetIndicatorVisualElement(pWindow, _INDICATOR_ORIENTATION_PORTRAIT, pForm);
227         SysTryReturn(NID_UI_CTRL, __pPortraitVisualElement, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not create the surface.");
228         __pLandscapeVisualElement = _IndicatorManager::GetInstance()->GetIndicatorVisualElement(pWindow, _INDICATOR_ORIENTATION_LANDSCAPE, pForm);
229         SysTryReturn(NID_UI_CTRL, __pLandscapeVisualElement, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not create the surface.");
230         __pPortraitIndicatorEvasObject = _IndicatorManager::GetInstance()->GetEvasObject(pWindow, pForm, _INDICATOR_ORIENTATION_PORTRAIT);
231         SysTryReturn(NID_UI_CTRL, __pPortraitIndicatorEvasObject, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not get the Evas_Object.");
232         __pLandscapeIndicatorEvasObject = _IndicatorManager::GetInstance()->GetEvasObject(pWindow, pForm, _INDICATOR_ORIENTATION_LANDSCAPE);
233         SysTryReturn(NID_UI_CTRL, __pLandscapeIndicatorEvasObject, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not get the Evas_Object.");
234
235         _ControlOrientation orientation = GetOrientation();
236         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
237         {
238                 r = ChangeCurrentVisualElement(__pPortraitVisualElement);
239         }
240         else
241         {
242                 r = ChangeCurrentVisualElement(__pLandscapeVisualElement);
243         }
244
245         Ecore_Evas *pEe = ecore_evas_object_ecore_evas_get(__pPortraitIndicatorEvasObject);
246         SysTryReturn(NID_UI_CTRL, pEe, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Unable to get evas.");
247
248         ecore_evas_data_set(pEe, pPortPublicKey, __pPortraitIndicatorEvasObject);
249         ecore_evas_data_set(pEe, pLandPublicKey, __pLandscapeIndicatorEvasObject);
250         ecore_evas_data_set(pEe, pIndicatorKey, this);
251         ecore_evas_callback_delete_request_set(pEe, _Indicator::OnDisconnected);
252
253         return r;
254 }
255
256 result
257 _Indicator::DeleteIndicatorObject(void)
258 {
259         result r = E_SUCCESS;
260
261         _Window* pWindow = GetRootWindow();
262         SysTryReturn(NID_UI_CTRL, pWindow, null,  E_INVALID_STATE, "[E_INVALID_STATE] Indicator is not attached main tree.");
263
264         r = _IndicatorManager::GetInstance()->ReleaseIndicatorVisualElement(pWindow, _INDICATOR_ORIENTATION_PORTRAIT, __pPortraitVisualElement);
265         r = _IndicatorManager::GetInstance()->ReleaseIndicatorVisualElement(pWindow, _INDICATOR_ORIENTATION_LANDSCAPE, __pLandscapeVisualElement);
266
267         _VisualElement* pIndicatorVisualElement = GetVisualElement();
268         if (__pPortraitVisualElement)
269         {
270                 r = pIndicatorVisualElement->DetachChild(*__pPortraitVisualElement);
271         }
272         if (__pLandscapeVisualElement)
273         {
274                 r = pIndicatorVisualElement->DetachChild(*__pLandscapeVisualElement);
275         }
276
277         __pPortraitIndicatorEvasObject = null;
278         __pLandscapeIndicatorEvasObject = null;
279         __pPortraitVisualElement = null;
280         __pLandscapeVisualElement = null;
281         __pCurrentVisualElement = null;
282
283         return r;
284 }
285
286 result
287 _Indicator::ChangeCurrentVisualElement(Tizen::Ui::Animations::_VisualElement* pVisualElement)
288 {
289         result r = E_SUCCESS;
290
291         if (!(__pCurrentVisualElement == pVisualElement))
292         {
293                 _VisualElement* pIndicatorVisualElement = GetVisualElement();
294                 r = pIndicatorVisualElement->AttachChild(*pVisualElement);
295                 if (__pCurrentVisualElement)
296                 {
297                         r = pIndicatorVisualElement->DetachChild(*__pCurrentVisualElement);
298                 }
299                 __pCurrentVisualElement = pVisualElement;
300         }
301
302         return r;
303 }
304
305 void
306 _Indicator::OnChangeLayout(_ControlOrientation orientation)
307 {
308         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
309         {
310                 ChangeCurrentVisualElement(__pPortraitVisualElement);
311         }
312         else
313         {
314                 ChangeCurrentVisualElement(__pLandscapeVisualElement);
315         }
316 }
317
318 result
319 _Indicator::OnAttachedToMainTree(void)
320 {
321         result r = E_SUCCESS;
322
323         if (__pCurrentVisualElement)
324         {
325                 r = ChangeCurrentVisualElement(__pCurrentVisualElement);
326         }
327
328         return r;
329 }
330
331 void
332 _Indicator::OnTimerExpired(Timer& timer)
333 {
334         Eina_Bool result = EINA_TRUE;
335
336         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
337         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
338         {
339                 result = ecore_evas_extn_plug_connect(__pPortraitIndicatorEvasObject, pPortraitSvcName, 0, EINA_FALSE);
340         }
341         else
342         {
343                 result = ecore_evas_extn_plug_connect(__pLandscapeIndicatorEvasObject, pLandScapeSvcName, 0, EINA_FALSE);
344         }
345
346         if (result == EINA_TRUE)
347         {
348                 timer.Cancel();
349         }
350         else
351         {
352                 timer.Start(CONNECTION_INTERVAL);
353         }
354 }
355
356 void
357 _Indicator::OnDisconnected(Ecore_Evas *pEe)
358 {
359         _Indicator* pIndicator = (_Indicator*)ecore_evas_data_get(pEe, pIndicatorKey);
360         SysTryReturnVoidResult(NID_UI_CTRL, pIndicator, E_SYSTEM, "[E_SYSTEM] Unable to get Indicator Object");
361
362         if (pIndicator->__pConnectionTimer == null)
363         {
364                 pIndicator->__pConnectionTimer = new (std::nothrow) Timer;
365                 if (pIndicator->__pConnectionTimer == null)
366                 {
367                         return;
368                 }
369
370                 result r = pIndicator->__pConnectionTimer->Construct(*pIndicator);
371                 if (r != E_SUCCESS)
372                 {
373                         delete pIndicator->__pConnectionTimer;
374                         pIndicator->__pConnectionTimer = null;
375
376                         return;
377                 }
378         }
379
380         pIndicator->__pConnectionTimer->Start(CONNECTION_INTERVAL);
381 }
382
383 result
384 _Indicator::SetNotificationTrayOpenEnabled(bool enable)
385 {
386         _Window* pWindow = GetRootWindow();
387         SysTryReturn(NID_UI_CTRL, pWindow, null,  E_INVALID_STATE, "[E_INVALID_STATE] Indicator is not attached main tree.");
388
389         Ecore_X_Window win = (Ecore_X_Window)pWindow->GetNativeHandle();
390
391         Ecore_X_Atom atomPanelScrollable = ecore_x_atom_get("_E_MOVE_PANEL_SCROLLABLE_STATE");
392
393         unsigned int val[3];
394         ecore_x_window_prop_card32_get(win, atomPanelScrollable, val, 3);
395
396         if (enable)
397     {
398                 val[1] = 1;
399     }
400         else
401     {
402                 val[1] = 0;
403     }
404
405         ecore_x_window_prop_card32_set(win, atomPanelScrollable, val, 3);
406
407
408         return E_SUCCESS;
409 }
410
411 bool
412 _Indicator::IsNotificationTrayOpenEnabled(void) const
413 {
414         _Window* pWindow = GetRootWindow();
415         SysTryReturn(NID_UI_CTRL, pWindow, false,  E_INVALID_STATE, "[E_INVALID_STATE] Indicator is not attached main tree.");
416
417         Ecore_X_Window win = (Ecore_X_Window)pWindow->GetNativeHandle();
418
419         Ecore_X_Atom atomPanelScrollable = ecore_x_atom_get("_E_MOVE_PANEL_SCROLLABLE_STATE");
420
421         unsigned int val[3];
422         ecore_x_window_prop_card32_get(win, atomPanelScrollable, val, 3);
423
424         if (val[1] == 1)
425     {
426                 return true;
427     }
428         else
429         {
430                 return false;
431     }
432 }
433
434 }}} // Tizen::Ui::Controls