Tizen 2.1 base
[framework/osp/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_Window.h"
32 #include "FUiCtrl_Form.h"
33 #include "FUiCtrl_Indicator.h"
34 #include "FUiCtrl_IndicatorManager.h"
35
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Animations;
40
41 namespace Tizen { namespace Ui { namespace Controls
42 {
43
44 static const char* pPortraitSvcName = "elm_indicator_portrait";
45 static const char* pLandScapeSvcName = "elm_indicator_landscape";
46 static const char* pPortPublicKey = "__Plug_Ecore_Evas_Port";
47 static const char* pLandPublicKey = "__Plug_Ecore_Evas_Land";
48 static const char* pIndicatorKey = "Inidcator_Manager_Object";
49 static const int CONNECTION_INTERVAL = 1000;
50
51 _Indicator*
52 _Indicator::CreateIndicator(void)
53 {
54         _Indicator* pIndicator = new (std::nothrow) _Indicator;
55         SysTryReturn(NID_UI_CTRL, pIndicator, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
56         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
57
58 CATCH:
59         delete pIndicator;
60         return null;
61 }
62
63 _Indicator::_Indicator(void)
64         : __pPortraitIndicatorEvasObject(null)
65         , __pLandscapeIndicatorEvasObject(null)
66         , __pConnectionTimer(null)
67         , __pPortraitSurface(null)
68         , __pLandScapeSurface(null)
69         , __pCurrentSurface(null)
70         , __pVisualElement(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         __pPortraitSurface = null;
89         __pLandScapeSurface = null;
90         __pCurrentSurface = null;
91 }
92
93 result
94 _Indicator::SetIndicatorShowState(bool state)
95 {
96         result r = E_SUCCESS;
97
98         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
99         SysTryReturnResult(NID_UI_CTRL, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] Unable to get evas");
100
101         _Window* pWindow = GetRootWindow();
102
103         if (pWindow)
104         {
105                 r = pEcoreEvas->SetIndicatorShowState(*pWindow, state);
106         }
107
108         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
109
110         __showstate = state;
111
112         SetVisibleState(state);
113
114         if (state)
115         {
116                 SetIndicatorOpacity(__opacity);
117         }
118
119         return r;
120 }
121
122 result
123 _Indicator::SetIndicatorOpacity(_IndicatorOpacity opacity)
124 {
125         result r = E_SUCCESS;
126
127         if (opacity == _INDICATOR_OPACITY_OPAQUE)
128         {
129                 SetBackgroundColor(0xff343432);
130         }
131         else if (opacity == _INDICATOR_OPACITY_TRANSLUCENT)
132         {
133                 SetBackgroundColor(0x7f000000);
134         }
135
136         __opacity = opacity;
137
138         return r;
139 }
140
141 bool
142 _Indicator::GetIndicatorShowState(void) const
143 {
144         return __showstate;
145 }
146
147 _IndicatorOpacity
148 _Indicator::GetIndicatorOpacity(void) const
149 {
150         return __opacity;
151 }
152
153 Rectangle
154 _Indicator::GetIndicatorBounds(void) const
155 {
156         const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
157         const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
158
159         _ControlOrientation orientation = GetOrientation();
160
161         Rectangle indicatorbounds(0, 0, 0, 0);
162
163         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
164         {
165                 indicatorbounds.width = portraitSize.width;
166         }
167         else
168         {
169                 indicatorbounds.width = landscapeSize.width;
170         }
171         GET_SHAPE_CONFIG(FORM::INDICATOR_HEIGHT, GetOrientation(), indicatorbounds.height);
172
173         _CoordinateSystem* pCoordSystem = _CoordinateSystem::GetInstance();
174         SysTryReturn(NID_UI_CTRL, pCoordSystem, Rectangle(-1, -1, -1, -1), E_SYSTEM, "[E_SYSTEM] Coordinate system load failed.");
175
176         _ICoordinateSystemTransformer* pXformer = pCoordSystem->GetTransformer();
177         SysTryReturn(NID_UI_CTRL, pXformer, Rectangle(-1, -1, -1, -1), E_SYSTEM, "[E_SYSTEM] Coordinate system load failed.");
178
179         indicatorbounds = pXformer->Transform(indicatorbounds);
180
181         return indicatorbounds;
182 }
183
184 result
185 _Indicator::AddEvasObject(_Form* pForm)
186 {
187         result r = E_SUCCESS;
188
189         _Window* pWindow = GetRootWindow();
190         SysTryReturn(NID_UI_CTRL, pWindow, null,  E_INVALID_STATE, "[E_INVALID_STATE] Indicator is not attached main tree.");
191
192         __pPortraitSurface =    _IndicatorManager::GetInstance()->GetSurface(pWindow, _INDICATOR_ORIENTATION_PORTRAIT, pForm);
193         SysTryReturn(NID_UI_CTRL, __pPortraitSurface, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not create the surface.");
194         __pLandScapeSurface = _IndicatorManager::GetInstance()->GetSurface(pWindow, _INDICATOR_ORIENTATION_LANDSCAPE, pForm);
195         SysTryReturn(NID_UI_CTRL, __pLandScapeSurface, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not create the surface.");
196         __pPortraitIndicatorEvasObject = _IndicatorManager::GetInstance()->GetEvasObject(pWindow, pForm, _INDICATOR_ORIENTATION_PORTRAIT);
197         SysTryReturn(NID_UI_CTRL, __pPortraitIndicatorEvasObject, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not get the Evas_Object.");
198         __pLandscapeIndicatorEvasObject = _IndicatorManager::GetInstance()->GetEvasObject(pWindow, pForm, _INDICATOR_ORIENTATION_LANDSCAPE);
199         SysTryReturn(NID_UI_CTRL, __pLandscapeIndicatorEvasObject, null,  E_SYSTEM, "[E_SYSTEM] Indicator can not get the Evas_Object.");
200
201         _ControlOrientation orientation = GetOrientation();
202         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
203         {
204                 __pCurrentSurface = __pPortraitSurface;
205         }
206         else
207         {
208                 __pCurrentSurface = __pLandScapeSurface;
209         }
210
211         if (!__pVisualElement)
212         {
213                 __pVisualElement = new (std::nothrow) _VisualElement;
214                 SysTryReturn(NID_UI_CTRL, __pVisualElement, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
215                 r = __pVisualElement->Construct();
216                 SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), null, E_SYSTEM, "[E_SYSTEM] System error occurred.");
217                 __pVisualElement->SetImplicitAnimationEnabled(false);
218                 __pVisualElement->SetShowState(true);
219
220                 _VisualElement* pIndicatorVisualElement = GetVisualElement();
221                 pIndicatorVisualElement->AttachChild(*__pVisualElement);
222         }
223
224         r = SetSurface(__pCurrentSurface);
225         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, GetErrorMessage(r));
226
227         Ecore_Evas *pEe = ecore_evas_object_ecore_evas_get(__pPortraitIndicatorEvasObject);
228         SysTryReturn(NID_UI_CTRL, pEe, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Unable to get evas.");
229
230         ecore_evas_data_set(pEe, pPortPublicKey, __pPortraitIndicatorEvasObject);
231         ecore_evas_data_set(pEe, pLandPublicKey, __pLandscapeIndicatorEvasObject);
232         ecore_evas_data_set(pEe, pIndicatorKey, this);
233         ecore_evas_callback_delete_request_set(pEe, _Indicator::OnDisconnected);
234
235         return r;
236 }
237
238 result
239 _Indicator::DeleteEvasObject(void)
240 {
241         result r = E_SUCCESS;
242
243         _Window* pWindow = GetRootWindow();
244         SysTryReturn(NID_UI_CTRL, pWindow, null,  E_INVALID_STATE, "[E_INVALID_STATE] Indicator is not attached main tree.");
245
246         r = _IndicatorManager::GetInstance()->ReleaseSurface(pWindow, _INDICATOR_ORIENTATION_PORTRAIT, __pPortraitSurface);
247         r = _IndicatorManager::GetInstance()->ReleaseSurface(pWindow, _INDICATOR_ORIENTATION_LANDSCAPE, __pLandScapeSurface);
248
249         r = SetSurface(null);
250
251         __pPortraitIndicatorEvasObject = null;
252         __pLandscapeIndicatorEvasObject = null;
253         __pPortraitSurface = null;
254         __pLandScapeSurface = null;
255         __pCurrentSurface = null;
256
257         return r;
258 }
259
260 result
261 _Indicator::SetSurface(VisualElementSurface* pSurface)
262 {
263         result r = E_SUCCESS;
264
265         if (__pVisualElement)
266         {
267                 r = __pVisualElement->SetBounds(FloatRectangle(0, 0, GetSize().width, GetSize().height));
268                 r = __pVisualElement->SetSurface(pSurface);
269         }
270
271         return r;
272 }
273
274 void
275 _Indicator::OnChangeLayout(_ControlOrientation orientation)
276 {
277         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
278         {
279                 __pCurrentSurface = __pPortraitSurface;
280                 SetSurface(__pCurrentSurface);
281
282                 evas_object_smart_member_del(__pLandscapeIndicatorEvasObject);
283                 evas_object_show(__pPortraitIndicatorEvasObject);
284                 evas_object_hide(__pLandscapeIndicatorEvasObject);
285         }
286         else
287         {
288                 __pCurrentSurface = __pLandScapeSurface;
289                 SetSurface(__pCurrentSurface);
290
291                 evas_object_smart_member_del(__pPortraitIndicatorEvasObject);
292                 evas_object_show(__pLandscapeIndicatorEvasObject);
293                 evas_object_hide(__pPortraitIndicatorEvasObject);
294         }
295 }
296
297 result
298 _Indicator::OnAttachedToMainTree(void)
299 {
300         result r = E_SUCCESS;
301
302         if (__pCurrentSurface)
303         {
304                 r = SetSurface(__pCurrentSurface);
305         }
306
307         return r;
308 }
309
310 void
311 _Indicator::OnTimerExpired(Timer& timer)
312 {
313         Eina_Bool result = EINA_TRUE;
314
315         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
316         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
317         {
318                 result = ecore_evas_extn_plug_connect(__pPortraitIndicatorEvasObject, pPortraitSvcName, 0, EINA_FALSE);
319         }
320         else
321         {
322                 result = ecore_evas_extn_plug_connect(__pLandscapeIndicatorEvasObject, pLandScapeSvcName, 0, EINA_FALSE);
323         }
324
325         if (result == EINA_TRUE)
326         {
327                 timer.Cancel();
328         }
329         else
330         {
331                 timer.Start(CONNECTION_INTERVAL);
332         }
333 }
334
335 void
336 _Indicator::OnDisconnected(Ecore_Evas *pEe)
337 {
338         _Indicator* pIndicator = (_Indicator*)ecore_evas_data_get(pEe, pIndicatorKey);
339         SysTryReturnVoidResult(NID_UI_CTRL, pIndicator, E_SYSTEM, "[E_SYSTEM] Unable to get Indicator Object");
340
341         if (pIndicator->__pConnectionTimer == null)
342         {
343                 pIndicator->__pConnectionTimer = new (std::nothrow) Timer;
344                 if (pIndicator->__pConnectionTimer == null)
345                 {
346                         return;
347                 }
348
349                 result r = pIndicator->__pConnectionTimer->Construct(*pIndicator);
350                 if (r != E_SUCCESS)
351                 {
352                         delete pIndicator->__pConnectionTimer;
353                         pIndicator->__pConnectionTimer = null;
354
355                         return;
356                 }
357         }
358
359         pIndicator->__pConnectionTimer->Start(CONNECTION_INTERVAL);
360 }
361
362 }}} // Tizen::Ui::Controls