Fixed jira issue.
[platform/framework/native/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 Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0/
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 "FUi_CoordinateSystemUtils.h"
33 #include "FUiCtrl_IndicatorManager.h"
34 #include "FUiCtrl_Form.h"
35 #include "FUiCtrl_Frame.h"
36 #include "FUiCtrl_Popup.h"
37 #include "FUiCtrl_Keypad.h"
38
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Ui;
42 using namespace Tizen::Ui::Animations;
43
44
45 namespace Tizen { namespace Ui { namespace Controls
46 {
47
48 static const char* pPortraitSvcName = "elm_indicator_portrait";
49 static const char* pLandScapeSvcName = "elm_indicator_landscape";
50 const int NUMBER_OF_OBJECT = 2;
51
52 _IndicatorManager* _IndicatorManager::__pInstance = null;
53
54 _IndicatorManager*
55 _IndicatorManager::GetInstance(void)
56 {
57         return __pInstance;
58 }
59
60 void
61 _IndicatorManager::Initialize(void)
62 {
63         if (!__pInstance)
64         {
65                 __pInstance = new (std::nothrow)_IndicatorManager;
66                 SysAssert(__pInstance);
67         }
68 }
69
70 void
71 _IndicatorManager::InitializeInstance(void)
72 {
73         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
74
75         if (!__pInstance)
76         {
77                 pthread_once(&once_block, Initialize);
78         }
79 }
80
81 void
82 _IndicatorManager::ReleaseInstance(void)
83 {
84         if (__pInstance)
85         {
86                 delete __pInstance;
87                 __pInstance = null;
88         }
89 }
90
91 _IndicatorManager::_IndicatorManager(void) // [ToDo] exception check.
92 {
93         __indicatorMap.Construct();
94 }
95
96 _IndicatorManager::~_IndicatorManager(void)
97 {
98         IMapEnumeratorT<_Window*, IndicatorComponent*>* pEnumerator = __indicatorMap.GetMapEnumeratorN();
99         if (pEnumerator)
100         {
101                 while(pEnumerator->MoveNext() == E_SUCCESS)
102                 {
103                         IndicatorComponent* pIndicatorComponent = null;
104                         pEnumerator->GetValue(pIndicatorComponent);
105
106                         if (pIndicatorComponent)
107                         {
108                                 delete pIndicatorComponent[0].pPortraitSurface;
109                                 delete pIndicatorComponent[1].pPortraitSurface;
110                                 delete pIndicatorComponent[0].pLandscapeSurface;
111                                 delete pIndicatorComponent[1].pLandscapeSurface;
112                                 pIndicatorComponent[0].pPortraitVisualElement->Destroy();
113                                 pIndicatorComponent[0].pLandscapeVisualElement->Destroy();
114                                 pIndicatorComponent[1].pPortraitVisualElement->Destroy();
115                                 pIndicatorComponent[1].pLandscapeVisualElement->Destroy();
116
117                                 delete [] pIndicatorComponent;
118                         }
119                 }
120                 delete pEnumerator;
121         }
122
123         __indicatorMap.RemoveAll();
124 }
125
126 _VisualElement*
127 _IndicatorManager::GetIndicatorVisualElement(_Window* pWindow, _IndicatorOrientation orientation, _Control* pControl) const
128 {
129         result r = E_SUCCESS;
130
131         IndicatorComponent* pIndicatorComponentArray = null;
132         r = __indicatorMap.GetValue(pWindow, pIndicatorComponentArray);
133         SysTryReturn(NID_UI_CTRL, pIndicatorComponentArray, null, E_SYSTEM, "[E_SYSTEM] Unable to get Indicator");
134
135
136         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
137         {
138                 if (pControl == pIndicatorComponentArray[i].pControl)
139                 {
140                         if ((orientation == _INDICATOR_ORIENTATION_PORTRAIT) && (pIndicatorComponentArray[i].portraitVisualElementUsed))
141                         {
142                                 return pIndicatorComponentArray[i].pPortraitVisualElement;
143                         }
144                         else if((orientation == _INDICATOR_ORIENTATION_LANDSCAPE) && (pIndicatorComponentArray[i].landscapeVisualElementUsed))
145                         {
146                                 return pIndicatorComponentArray[i].pLandscapeVisualElement;
147                         }
148                 }
149         }
150
151         if (IsFull(pIndicatorComponentArray))
152         {
153                 _Frame* pFrame = dynamic_cast<_Frame*>(pWindow);
154                 if (pFrame)
155                 {
156                         _Form* pCurrentForm = pFrame->GetCurrentForm();
157                         if (pCurrentForm == pControl)
158                         {
159                                 int count = pFrame->GetChildCount();
160                                 pCurrentForm = dynamic_cast<_Form*>(pFrame->GetChild(count-2));
161                         }
162
163                         if (pCurrentForm)
164                         {
165                                 for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
166                                 {
167                                         if (pIndicatorComponentArray[i].pControl != pCurrentForm)
168                                         {
169                                                 _Form* pForm = dynamic_cast<_Form*>(pIndicatorComponentArray[i].pControl);
170                                                 if (pForm)
171                                                 {
172                                                         pForm->DeleteIndicatorObject();
173                                                 }
174                                                 break;
175                                         }
176                                 }
177                         }
178                 }
179         }
180
181         _VisualElement* pVisalElement = null;
182         if (pIndicatorComponentArray)
183         {
184                 for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
185                 {
186                         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
187                         {
188                                 if (!(pIndicatorComponentArray[i].portraitVisualElementUsed))
189                                 {
190                                         pVisalElement = pIndicatorComponentArray[i].pPortraitVisualElement;
191                                         pIndicatorComponentArray[i].portraitVisualElementUsed = true;
192                                         pIndicatorComponentArray[i].pControl = pControl;
193                                         break;
194                                 }
195                         }
196                         else if(orientation == _INDICATOR_ORIENTATION_LANDSCAPE)
197                         {
198                                 if (!(pIndicatorComponentArray[i].landscapeVisualElementUsed))
199                                 {
200                                         pVisalElement = pIndicatorComponentArray[i].pLandscapeVisualElement;
201                                         pIndicatorComponentArray[i].landscapeVisualElementUsed = true;
202                                         pIndicatorComponentArray[i].pControl = pControl;
203                                         break;
204                                 }
205                         }
206                 }
207         }
208         return pVisalElement;
209 }
210
211 result
212 _IndicatorManager::ReleaseIndicatorVisualElement(_Window* pWindow, _IndicatorOrientation orientation, _VisualElement* pVisualElement)
213 {
214         result r = E_SUCCESS;
215         IndicatorComponent* pIndicatorComponentArray = null;
216         r = __indicatorMap.GetValue(pWindow, pIndicatorComponentArray);
217
218         if (pIndicatorComponentArray)
219         {
220                 for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
221                 {
222                         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
223                         {
224                                 if (pIndicatorComponentArray[i].portraitVisualElementUsed)
225                                 {
226                                         if (pVisualElement == pIndicatorComponentArray[i].pPortraitVisualElement)
227                                         {
228                                                 pIndicatorComponentArray[i].portraitVisualElementUsed = false;
229                                                 pIndicatorComponentArray[i].pControl = null;
230                                                 break;
231                                         }
232                                 }
233                         }
234                         else if(orientation == _INDICATOR_ORIENTATION_LANDSCAPE)
235                         {
236                                 if (pIndicatorComponentArray[i].landscapeVisualElementUsed)
237                                 {
238                                         if (pVisualElement == pIndicatorComponentArray[i].pLandscapeVisualElement)
239                                         {
240                                                 pIndicatorComponentArray[i].landscapeVisualElementUsed = false;
241                                                 pIndicatorComponentArray[i].pControl = null;
242                                                 break;
243                                         }
244                                 }
245                         }
246                 }
247         }
248         return r;;
249 }
250
251 result
252 _IndicatorManager::AddWindow(_Window* pWindow)
253 {
254         result r = E_SUCCESS;
255
256         _Frame* pFrame = dynamic_cast<_Frame*>(pWindow);
257         _Popup* pPopup = dynamic_cast<_Popup*>(pWindow);
258         _Keypad* pKeypad = dynamic_cast<_Keypad*>(pWindow);
259
260         if ((pFrame == null) && (pPopup == null) && (pKeypad ==null))
261         {
262                 return E_SUCCESS;
263         }
264
265         IndicatorComponent* pIndicatorComponentArray = MakeIndicatorComponentArrayN(pWindow);
266
267         r = __indicatorMap.Add(pWindow, pIndicatorComponentArray);
268
269         return r;
270 }
271
272 result
273 _IndicatorManager::DeleteWindow(_Window* pWindow)
274 {
275         result r = E_SUCCESS;
276         IndicatorComponent* pIndicatorComponentArray = null;
277
278         _Frame* pFrame = dynamic_cast<_Frame*>(pWindow);
279         _Popup* pPopup = dynamic_cast<_Popup*>(pWindow);
280         _Keypad* pKeypad = dynamic_cast<_Keypad*>(pWindow);
281
282         if ((pFrame == null) && (pPopup == null) && (pKeypad ==null))
283         {
284                 return E_SUCCESS;
285         }
286
287         r = __indicatorMap.GetValue(pWindow, pIndicatorComponentArray);
288         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS && pIndicatorComponentArray, r, r, "[%s] Propagating.", GetErrorMessage(r));
289
290         delete pIndicatorComponentArray[0].pPortraitSurface;
291         delete pIndicatorComponentArray[0].pLandscapeSurface;
292         delete pIndicatorComponentArray[1].pPortraitSurface;
293         delete pIndicatorComponentArray[1].pLandscapeSurface;
294         pIndicatorComponentArray[0].pPortraitVisualElement->Destroy();
295         pIndicatorComponentArray[0].pLandscapeVisualElement->Destroy();
296         pIndicatorComponentArray[1].pPortraitVisualElement->Destroy();
297         pIndicatorComponentArray[1].pLandscapeVisualElement->Destroy();
298
299         delete [] pIndicatorComponentArray;
300         __indicatorMap.Remove(pWindow);
301
302         return r;
303 }
304
305 IndicatorComponent*
306 _IndicatorManager::MakeIndicatorComponentArrayN(_Window* pWindow)
307 {
308         result r = E_SUCCESS;
309         Eina_Bool result = EINA_TRUE;
310         FloatDimension portraitsize(0.0f, 0.0f);
311         FloatDimension landscapesize(0.0f, 0.0f);
312
313         _VisualElement* pLandscapeVisualElement = null;
314         _VisualElement* pPortraitVisualElement = null;
315
316         IndicatorComponent* pIndicatorComponentArray = new IndicatorComponent[NUMBER_OF_OBJECT];
317
318         _RootVisualElement* pRootVisualElement = pWindow->GetRootVisualElement();
319         _EflLayer* pEflLayer = static_cast<_EflLayer*>(pRootVisualElement->GetNativeLayer());
320         Ecore_Evas* pEcore_Evas = pEflLayer->GetEcoreEvas();
321         SysTryCatch(NID_UI_CTRL, pEcore_Evas, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
322
323         portraitsize = GetIndicatorSizeF(_INDICATOR_ORIENTATION_PORTRAIT);
324         landscapesize = GetIndicatorSizeF(_INDICATOR_ORIENTATION_LANDSCAPE);
325
326         //Get indicator Image Object
327         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
328         {
329                 Evas_Object* pPortraitEvasObject = ecore_evas_extn_plug_new(pEcore_Evas);
330                 SysTryCatch(NID_UI_CTRL, pPortraitEvasObject, , E_SYSTEM, "[E_SYSTEM] Unable to get evas object");
331
332                 Evas_Object* pLandscapeEvasObject = ecore_evas_extn_plug_new(pEcore_Evas);
333                 SysTryCatch(NID_UI_CTRL, pLandscapeEvasObject, , E_SYSTEM, "[E_SYSTEM] Unable to get evas object");
334
335                 result = ecore_evas_extn_plug_connect(pPortraitEvasObject, pPortraitSvcName, 0, EINA_FALSE);
336                 result = ecore_evas_extn_plug_connect(pLandscapeEvasObject, pLandScapeSvcName, 0, EINA_FALSE);
337
338                 evas_object_resize(pPortraitEvasObject, portraitsize.width, portraitsize.height);
339                 evas_object_resize(pLandscapeEvasObject, landscapesize.width, landscapesize.height);
340                 evas_object_move(pPortraitEvasObject, 0, 0);
341                 evas_object_move(pLandscapeEvasObject, 0, 0);
342
343                 VisualElementSurface* pPortraitSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pWindow->GetDisplayContext(), reinterpret_cast<Handle>(pPortraitEvasObject), GetIndicatorSize(_INDICATOR_ORIENTATION_PORTRAIT));
344                 VisualElementSurface* pLandScapeSurface = _VisualElementSurfaceImpl::CreateSurfaceUsingExistingObjectN(*pWindow->GetDisplayContext(), reinterpret_cast<Handle>(pLandscapeEvasObject), GetIndicatorSize(_INDICATOR_ORIENTATION_LANDSCAPE));
345
346                 pPortraitVisualElement = new (std::nothrow) _VisualElement;
347                 SysTryCatch(NID_UI_CTRL, pPortraitVisualElement, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
348                 r = pPortraitVisualElement->Construct();
349                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
350                 pPortraitVisualElement->SetImplicitAnimationEnabled(false);
351                 pPortraitVisualElement->SetShowState(true);
352                 pPortraitVisualElement->SetSurface(pPortraitSurface);
353                 pPortraitVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, portraitsize.width, portraitsize.height));
354
355                 pLandscapeVisualElement = new (std::nothrow) _VisualElement;
356                 SysTryCatch(NID_UI_CTRL, pLandscapeVisualElement, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
357                 r = pLandscapeVisualElement->Construct();
358                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
359                 pLandscapeVisualElement->SetImplicitAnimationEnabled(false);
360                 pLandscapeVisualElement->SetShowState(true);
361                 pLandscapeVisualElement->SetSurface(pLandScapeSurface);
362                 pLandscapeVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, landscapesize.width, landscapesize.height));
363
364                 pIndicatorComponentArray[i].pPortraitEvasObject = pPortraitEvasObject;
365                 pIndicatorComponentArray[i].pLandscapeEvasObject = pLandscapeEvasObject;
366                 pIndicatorComponentArray[i].pPortraitSurface = pPortraitSurface;
367                 pIndicatorComponentArray[i].pLandscapeSurface = pLandScapeSurface;
368                 pIndicatorComponentArray[i].pPortraitVisualElement = pPortraitVisualElement;
369                 pIndicatorComponentArray[i].pLandscapeVisualElement = pLandscapeVisualElement;
370                 pIndicatorComponentArray[i].portraitVisualElementUsed = false;
371                 pIndicatorComponentArray[i].landscapeVisualElementUsed = false;
372                 pIndicatorComponentArray[i].pControl = null;
373         }
374
375         return pIndicatorComponentArray;
376
377 CATCH:
378         delete [] pIndicatorComponentArray;
379         if (pPortraitVisualElement)
380         {
381                 pPortraitVisualElement->Destroy();
382         }
383         if (pLandscapeVisualElement)
384         {
385                 pLandscapeVisualElement->Destroy();
386         }
387         return null;
388 }
389
390 bool
391 _IndicatorManager::IsFull(IndicatorComponent* pIndicatorComponentArray) const
392 {
393         if (pIndicatorComponentArray[0].portraitVisualElementUsed && pIndicatorComponentArray[0].landscapeVisualElementUsed
394                          && pIndicatorComponentArray[1].portraitVisualElementUsed && pIndicatorComponentArray[1].landscapeVisualElementUsed)
395         {
396                 return true;
397         }
398
399         return false;
400 }
401
402 Evas_Object*
403 _IndicatorManager::GetEvasObject(_Window* pWindow, _Control* pControl, _IndicatorOrientation orientation) const
404 {
405         Evas_Object* pEvasObject = null;
406
407         result r = E_SUCCESS;
408         IndicatorComponent* pIndicatorComponentArray = null;
409         r = __indicatorMap.GetValue(pWindow, pIndicatorComponentArray);
410         SysTryReturn(NID_UI_CTRL, pIndicatorComponentArray, null, r, "[%s] Propagating.", GetErrorMessage(r));
411
412         for(int i = 0 ; i < NUMBER_OF_OBJECT ; i++)
413         {
414                 if (pControl ==         pIndicatorComponentArray[i].pControl)
415                 {
416                         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
417                         {
418                                 pEvasObject = pIndicatorComponentArray[i].pPortraitEvasObject;
419                         }
420                         else if(orientation == _INDICATOR_ORIENTATION_LANDSCAPE)
421                         {
422                                 pEvasObject = pIndicatorComponentArray[i].pLandscapeEvasObject;
423                         }
424                 }
425         }
426         return pEvasObject;
427 }
428
429 Dimension
430 _IndicatorManager::GetIndicatorSize(_IndicatorOrientation orientation) const
431 {
432         const Dimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSize();
433         const Dimension landscapeSize = Dimension(portraitSize.height, portraitSize.width);
434
435         Rectangle indicatorbounds(0, 0, 0, 0);
436
437         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
438         {
439                 indicatorbounds.width = portraitSize.width;
440         }
441         else
442         {
443                 indicatorbounds.width = landscapeSize.width;
444         }
445         GET_SHAPE_CONFIG(FORM::INDICATOR_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, indicatorbounds.height);
446
447         Dimension size(0, 0);
448         size.width = indicatorbounds.width;
449         size.height = indicatorbounds.height;
450
451         return size;
452 }
453
454 FloatDimension
455 _IndicatorManager::GetIndicatorSizeF(_IndicatorOrientation orientation) const
456 {
457         const FloatDimension portraitSize = _ControlManager::GetInstance()->_ControlManager::GetScreenSizeF();
458         const FloatDimension landscapeSize = FloatDimension(portraitSize.height, portraitSize.width);
459
460         FloatDimension indicatorSize(0.0f, 0.0f);
461
462         if (orientation == _INDICATOR_ORIENTATION_PORTRAIT)
463         {
464                 indicatorSize.width = portraitSize.width;
465         }
466         else
467         {
468                 indicatorSize.width = landscapeSize.width;
469         }
470         GET_SHAPE_CONFIG(FORM::INDICATOR_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, indicatorSize.height);
471
472         return indicatorSize;
473 }
474
475 }}} // Tizen::Ui::Controls