Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_IndicatorManager.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_IndicatorManager.cpp
19  * @brief               This is the implementation file for the _IndicatorManager class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <FBaseColHashMapT.h>
24 #include <FUiAnim_VisualElementSurfaceImpl.h>
25 #include "FUiAnim_RootVisualElement.h"
26 #include "FUiAnim_EflLayer.h"
27 #include "FUi_EcoreEvas.h"
28 #include "FUi_EcoreEvasMgr.h"
29 #include "FUi_Window.h"
30 #include "FUi_ControlManager.h"
31 #include "FUi_ResourceManager.h"
32 #include "FUiCtrl_IndicatorManager.h"
33 #include "FUiCtrl_Form.h"
34 #include "FUiCtrl_Frame.h"
35
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Animations;
40
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 const int NUMBER_OF_OBJECT = 2;
48
49 _IndicatorManager* _IndicatorManager::__pInstance = null;
50
51 _IndicatorManager*
52 _IndicatorManager::GetInstance(void)
53 {
54         return __pInstance;
55 }
56
57 void
58 _IndicatorManager::Initialize(void)
59 {
60         if (!__pInstance)
61         {
62                 __pInstance = new (std::nothrow)_IndicatorManager;
63                 SysAssert(__pInstance);
64         }
65 }
66
67 void
68 _IndicatorManager::InitializeInstance(void)
69 {
70         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
71
72         if (!__pInstance)
73         {
74                 pthread_once(&once_block, Initialize);
75         }
76 }
77
78 void
79 _IndicatorManager::ReleaseInstance(void)
80 {
81         if (__pInstance)
82         {
83                 delete __pInstance;
84                 __pInstance = null;
85         }
86 }
87
88 _IndicatorManager::_IndicatorManager(void) // [ToDo] exception check.
89 {
90         __indicatorMap.Construct();
91 }
92
93 _IndicatorManager::~_IndicatorManager(void)
94 {
95         IMapEnumeratorT<_Window*, IndicatorSurface*>* pEnumerator = __indicatorMap.GetMapEnumeratorN();
96         if (pEnumerator)
97         {
98                 while(pEnumerator->MoveNext() == E_SUCCESS)
99                 {
100                         IndicatorSurface* pIndicatorSurface = null;
101                         pEnumerator->GetValue(pIndicatorSurface);
102
103                         if (pIndicatorSurface)
104                         {
105                                 delete pIndicatorSurface[0].pPortraitSurface;
106                                 delete pIndicatorSurface[1].pPortraitSurface;
107                                 delete pIndicatorSurface[0].pLandscapeSurface;
108                                 delete pIndicatorSurface[1].pLandscapeSurface;
109                                 delete [] pIndicatorSurface;
110                         }
111                 }
112                 delete pEnumerator;
113         }
114
115         __indicatorMap.RemoveAll();
116 }
117
118 VisualElementSurface*
119 _IndicatorManager::GetSurface(_Window* pWindow, _IndicatorOrientation orientation, _Form* pForm) const
120 {
121         result r = E_SUCCESS;
122
123         IndicatorSurface* pIndicatorSurfaceArray = null;
124         r = __indicatorMap.GetValue(pWindow, pIndicatorSurfaceArray);
125         SysTryReturn(NID_UI_CTRL, pIndicatorSurfaceArray, null, E_SYSTEM, "[E_SYSTEM] Unable to get Indicator");
126
127
128         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
129         {
130                 if (pForm == pIndicatorSurfaceArray[i].pForm)
131                 {
132                         if ((orientation == _INDICATOR_ORIENTATION_PORTRAIT) && (pIndicatorSurfaceArray[i].portraitSurfaceUsed))
133                         {
134                                 return pIndicatorSurfaceArray[i].pPortraitSurface;
135                         }
136                         else if((orientation == _INDICATOR_ORIENTATION_LANDSCAPE) && (pIndicatorSurfaceArray[i].landscapeSurfaceUsed))
137                         {
138                                 return pIndicatorSurfaceArray[i].pLandscapeSurface;
139                         }
140                 }
141         }
142
143         if (IsFull(pIndicatorSurfaceArray))
144         {
145                 _Frame* pFrame = dynamic_cast<_Frame*>(pWindow);
146                 if (pFrame)
147                 {
148                         _Form* pCurrentForm = pFrame->GetCurrentForm();
149                         if (pCurrentForm == pForm)
150                         {
151                                 int count = pFrame->GetChildCount();
152                                 pCurrentForm = dynamic_cast<_Form*>(pFrame->GetChild(count-2));
153                         }
154
155                         if (pCurrentForm)
156                         {
157                                 for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
158                                 {
159                                         if (pIndicatorSurfaceArray[i].pForm != pCurrentForm)
160                                         {
161                                                 pIndicatorSurfaceArray[i].pForm->DeleteIndicatorObject();
162                                                 break;
163                                         }
164                                 }
165                         }
166                 }
167         }
168
169         VisualElementSurface* pSurface = null;
170         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
171         {
172                 if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
173                 {
174                         if (!(pIndicatorSurfaceArray[i].portraitSurfaceUsed))
175                         {
176                                 pSurface = pIndicatorSurfaceArray[i].pPortraitSurface;
177                                 pIndicatorSurfaceArray[i].portraitSurfaceUsed = true;
178                                 pIndicatorSurfaceArray[i].pForm = pForm;
179                                 break;
180                         }
181                 }
182                 else if(orientation == _INDICATOR_ORIENTATION_LANDSCAPE)
183                 {
184                         if (!(pIndicatorSurfaceArray[i].landscapeSurfaceUsed))
185                         {
186                                 pSurface = pIndicatorSurfaceArray[i].pLandscapeSurface;
187                                 pIndicatorSurfaceArray[i].landscapeSurfaceUsed = true;
188                                 pIndicatorSurfaceArray[i].pForm = pForm;
189                                 break;
190                         }
191                 }
192         }
193
194         return pSurface;
195 }
196
197 result
198 _IndicatorManager::ReleaseSurface(_Window* pWindow, _IndicatorOrientation orientation, VisualElementSurface* pSurface)
199 {
200         result r = E_SUCCESS;
201         IndicatorSurface* pIndicatorSurfaceArray = null;
202         r = __indicatorMap.GetValue(pWindow, pIndicatorSurfaceArray);
203
204         if (pIndicatorSurfaceArray)
205         {
206                 for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
207                 {
208                         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
209                         {
210                                 if (pIndicatorSurfaceArray[i].portraitSurfaceUsed)
211                                 {
212                                         if (pSurface == pIndicatorSurfaceArray[i].pPortraitSurface)
213                                         {
214                                                 pIndicatorSurfaceArray[i].portraitSurfaceUsed = false;
215                                                 pIndicatorSurfaceArray[i].pForm = null;
216                                                 break;
217                                         }
218                                 }
219                         }
220                         else if(orientation == _INDICATOR_ORIENTATION_LANDSCAPE)
221                         {
222                                 if (pSurface == pIndicatorSurfaceArray[i].pLandscapeSurface)
223                                 {
224                                         pIndicatorSurfaceArray[i].landscapeSurfaceUsed = false;
225                                         pIndicatorSurfaceArray[i].pForm = null;
226                                         break;
227                                 }
228                         }
229                 }
230         }
231         return r;;
232 }
233
234 result
235 _IndicatorManager::AddWindow(_Window* pWindow)
236 {
237         result r = E_SYSTEM;
238
239         IndicatorSurface* pIndicatorSurfaceArray = MakeEvasSurfaceArrayN(pWindow);
240
241         r = __indicatorMap.Add(pWindow, pIndicatorSurfaceArray);
242
243         return r;
244 }
245
246 result
247 _IndicatorManager::DeleteWindow(_Window* pWindow)
248 {
249         result r = E_SUCCESS;
250         IndicatorSurface* pIndicatorSurfaceArray = null;
251
252         r = __indicatorMap.GetValue(pWindow, pIndicatorSurfaceArray);
253         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS && pIndicatorSurfaceArray, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The given binding ID is not registered.");
254
255         delete [] pIndicatorSurfaceArray;
256         __indicatorMap.Remove(pWindow);
257
258         return r;
259 }
260
261 IndicatorSurface*
262 _IndicatorManager::MakeEvasSurfaceArrayN(_Window* pWindow)
263 {
264         Eina_Bool result = EINA_TRUE;
265
266         _Frame* pFrame = dynamic_cast<_Frame*>(pWindow);
267         if (!pFrame)
268         {
269                 return null;
270         }
271
272         IndicatorSurface* pIndicatorSurfaceArray = new IndicatorSurface[NUMBER_OF_OBJECT];
273
274         Dimension portraitsize = GetIndicatorSize(_INDICATOR_ORIENTATION_PORTRAIT);
275         Dimension landscapesize = GetIndicatorSize(_INDICATOR_ORIENTATION_LANDSCAPE);
276
277         _RootVisualElement* pRootVisualElement = pWindow->GetRootVisualElement();
278         _EflLayer* pEflLayer = static_cast<_EflLayer*>(pRootVisualElement->GetNativeLayer());
279         Ecore_Evas* pEcore_Evas = pEflLayer->GetEcoreEvas();
280         SysTryCatch(NID_UI_CTRL, pEcore_Evas, , E_SYSTEM, "[E_SYSTEM] Unable to get Ecore_Evas object");
281
282         //Get indicator Image Object
283         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
284         {
285                 Evas_Object* pPortraitEvasObject = ecore_evas_extn_plug_new(pEcore_Evas);
286                 SysTryCatch(NID_UI_CTRL, pPortraitEvasObject, , E_SYSTEM, "[E_SYSTEM] Unable to get evas object");
287
288                 Evas_Object* pLandscapeEvasObject = ecore_evas_extn_plug_new(pEcore_Evas);
289                 SysTryCatch(NID_UI_CTRL, pLandscapeEvasObject, , E_SYSTEM, "[E_SYSTEM] Unable to get evas object");
290
291                 result = ecore_evas_extn_plug_connect(pPortraitEvasObject, pPortraitSvcName, 0, EINA_FALSE);
292                 result = ecore_evas_extn_plug_connect(pLandscapeEvasObject, pLandScapeSvcName, 0, EINA_FALSE);
293
294                 evas_object_resize(pPortraitEvasObject, portraitsize.width, portraitsize.height);
295                 evas_object_resize(pLandscapeEvasObject, landscapesize.width, landscapesize.height);
296                 evas_object_move(pPortraitEvasObject, 0, 0);
297                 evas_object_move(pLandscapeEvasObject, 0, 0);
298
299                 VisualElementSurface* pPortraitSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pWindow->GetDisplayContext(), reinterpret_cast<Handle>(pPortraitEvasObject), portraitsize);
300                 VisualElementSurface* pLandScapeSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pWindow->GetDisplayContext(), reinterpret_cast<Handle>(pLandscapeEvasObject), landscapesize);
301
302                 pIndicatorSurfaceArray[i].pPortraitEvasObject = pPortraitEvasObject;
303                 pIndicatorSurfaceArray[i].pLandscapeEvasObject = pLandscapeEvasObject;
304                 pIndicatorSurfaceArray[i].pPortraitSurface = pPortraitSurface;
305                 pIndicatorSurfaceArray[i].pLandscapeSurface = pLandScapeSurface;
306                 pIndicatorSurfaceArray[i].portraitSurfaceUsed = false;
307                 pIndicatorSurfaceArray[i].landscapeSurfaceUsed = false;
308                 pIndicatorSurfaceArray[i].pForm = null;
309         }
310
311         return pIndicatorSurfaceArray;
312
313 CATCH:
314         delete [] pIndicatorSurfaceArray;
315         return null;
316 }
317
318 bool
319 _IndicatorManager::IsFull(IndicatorSurface* pIndicatorSurfaceArray) const
320 {
321         if (pIndicatorSurfaceArray[0].portraitSurfaceUsed && pIndicatorSurfaceArray[0].landscapeSurfaceUsed
322                          && pIndicatorSurfaceArray[1].portraitSurfaceUsed && pIndicatorSurfaceArray[1].landscapeSurfaceUsed)
323         {
324                 return true;
325         }
326
327         return false;
328 }
329
330 Evas_Object*
331 _IndicatorManager::GetEvasObject(_Window* pWindow, _Form* pForm, _IndicatorOrientation orientation) const
332 {
333         Evas_Object* pEvasObject = null;
334
335         result r = E_SUCCESS;
336         IndicatorSurface* pIndicatorSurfaceArray = null;
337         r = __indicatorMap.GetValue(pWindow, pIndicatorSurfaceArray);
338         SysTryReturn(NID_UI_CTRL, pIndicatorSurfaceArray, null, r, "[%s] Propagating.", GetErrorMessage(r));
339
340         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
341         {
342                 if (pForm ==    pIndicatorSurfaceArray[i].pForm)
343                 {
344                         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
345                         {
346                                 pEvasObject = pIndicatorSurfaceArray[i].pPortraitEvasObject;
347                         }
348                         else if(orientation == _INDICATOR_ORIENTATION_LANDSCAPE)
349                         {
350                                 pEvasObject = pIndicatorSurfaceArray[i].pLandscapeEvasObject;
351                         }
352                 }
353         }
354         return pEvasObject;
355 }
356
357 Dimension
358 _IndicatorManager::GetIndicatorSize(_IndicatorOrientation orientation) const
359 {
360         const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
361         const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
362
363         Rectangle indicatorbounds(0, 0, 0, 0);
364
365         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
366         {
367                 indicatorbounds.width = portraitSize.width;
368         }
369         else
370         {
371                 indicatorbounds.width = landscapeSize.width;
372         }
373         GET_SHAPE_CONFIG(FORM::INDICATOR_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, indicatorbounds.height);
374
375         Dimension size(0, 0);
376         size.width = indicatorbounds.width;
377         size.height = indicatorbounds.height;
378
379         return size;
380 }
381
382 }}} // Tizen::Ui::Controls