b10d247c98b29befefe292c688af5542cf1d44a8
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_Frame.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_Frame.cpp
19  * @brief               This is the implementation file for the _Frame class.
20  */
21
22 #include <new>
23 #include <FBaseErrorDefine.h>
24 #include <FBaseInteger.h>
25 #include <FBaseSysLog.h>
26 #include <FApp_AppInfo.h>
27 #include "FUi_UiNotificationEvent.h"
28 #include "FUi_UiEventManager.h"
29 #include "FUi_ControlManager.h"
30 #include "FUi_EcoreEvasMgr.h"
31 #include "FUi_EcoreEvas.h"
32 #include "FUi_TouchManager.h"
33 #include "FUi_CoordinateSystemUtils.h"
34 #include "FUiCtrl_Frame.h"
35 #include "FUiCtrl_FramePresenter.h"
36 #include "FUiCtrl_FrameEvent.h"
37 #include "FUiCtrl_IFrameEventListener.h"
38 #include "FUiCtrl_IFormActivationChangeEventListener.h"
39 #include "FUiCtrl_Form.h"
40 #include "FUiAnim_VisualElement.h"
41 #include "FUiAnim_RootVisualElement.h"
42 #include "FUiAnim_EflLayer.h"
43
44 using namespace Tizen::App;
45 using namespace Tizen::Ui::Animations;
46 using namespace Tizen::Ui;
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Graphics;
51
52 namespace Tizen { namespace Ui { namespace Controls {
53
54 const String _ACTIVATE_FRAME = L"ActivateFrame";
55
56 _Frame::_Frame(void)
57         : __pFramePresenter(null)
58         , __pFrameEvent(null)
59         , __floatingBounds(0.0f, 0.0f, 0.0f, 0.0f)
60         , __showMode(FRAME_SHOW_MODE_FULL_SCREEN)
61         , __restore(false)
62         , __minimized(false)
63         , __activated(false)
64         , __constructed(false)
65         , __rotation(false)
66         , __pFormActivationChangeEventListener(null)
67 {
68         _FramePresenter* pPresenter = new (std::nothrow) _FramePresenter(*this);
69         SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
70
71         FloatDimension screen = _ControlManager::GetInstance()->GetScreenSizeF();
72         __floatingBounds.width = screen.width;
73         __floatingBounds.height = screen.height;
74
75         __pFrameEvent = _FrameEvent::CreateInstanceN(*this);
76         SysTryCatch(NID_UI_CTRL, __pFrameEvent, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
77
78         SetPresenter(*pPresenter);
79         SetClipChildrenEnabled(false);
80
81         ClearLastResult();
82
83         return;
84
85 CATCH:
86         delete pPresenter;
87         pPresenter = null;
88 }
89
90 _Frame::~_Frame(void)
91 {
92         if (__pFrameEvent)
93         {
94                 delete __pFrameEvent;
95                 __pFrameEvent = null;
96         }
97
98         delete __pFramePresenter;
99         __pFramePresenter = null;
100
101         ClearLastResult();
102 }
103
104 _Frame*
105 _Frame::CreateFrameN(void)
106 {
107         result r = E_SUCCESS;
108         _RootVisualElement* pRootVE = null;
109         _EflLayer* pLayer = null;
110         int appType = _AppInfo::GetAppType();
111
112         _Frame* pFrame = new (std::nothrow) _Frame;
113         SysTryCatch(NID_UI_CTRL, pFrame, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
114         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
115
116         if (appType & _APP_TYPE_IME_APP)
117         {
118                 SysLog(NID_UI_CTRL, "[Ime Rotation]");
119                 r = pFrame->CreateRootVisualElement(_WINDOW_TYPE_SUB);
120                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
121         }
122         else
123         {
124                 r = pFrame->CreateRootVisualElement();
125                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
126         }
127
128         pRootVE = pFrame->GetRootVisualElement();
129         SysAssert(pRootVE);
130
131         pLayer = static_cast<_EflLayer*>(pRootVE->GetNativeLayer());
132         SysAssert(pLayer);
133
134         pLayer->SetOpacity(1);
135
136         pFrame->AcquireHandle();
137
138         SetLastResult(E_SUCCESS);
139
140         return pFrame;
141
142 CATCH:
143         delete pFrame;
144
145         return null;
146 }
147
148 result
149 _Frame::SetPresenter(const _FramePresenter& framePresenter)
150 {
151         __pFramePresenter = const_cast <_FramePresenter*>(&framePresenter);
152
153         return E_SUCCESS;
154 }
155
156 void
157 _Frame::OnDraw(void)
158 {
159         if (__pFramePresenter)
160         {
161                 __pFramePresenter->Draw();
162         }
163 }
164
165 void
166 _Frame::OnActivated(void)
167 {
168         SysLog(NID_UI, "activated(%d)", __activated);
169
170         if (!__activated)
171         {
172                 return;
173         }
174
175         _Window::OnActivated();
176
177         _Form* pCurrentForm = GetCurrentForm();
178         if (pCurrentForm == null)
179         {
180                 return;
181         }
182
183         _Control* pFocusedControl = pCurrentForm->GetFocused();
184         if (pFocusedControl)
185         {
186                 pFocusedControl->SetFocused();
187         }
188         else
189         {
190                 pCurrentForm->SetFocused();
191         }
192 }
193
194 bool
195 _Frame::OnNotifiedN(const _Control& source, IList* pArgs)
196 {
197         SysTryReturn(NID_UI_CTRL, pArgs, false, E_SYSTEM, "[E_SYSTEM] pArgs is null.")
198
199         String* pType = dynamic_cast <String*>(pArgs->GetAt(0));
200         SysTryReturn(NID_UI_CTRL, pType, false, E_SYSTEM, "[E_SYSTEM] pType is null.")
201
202         if (*pType == L"VisibilityEvent")
203         {
204                 int obscured = 0;
205
206                 Integer* pObscured = dynamic_cast<Integer*>(pArgs->GetAt(1));
207                 if (pObscured == null)
208                 {
209                         pArgs->RemoveAll(true);
210                         delete pArgs;
211
212                         return true;
213                 }
214
215                 obscured = pObscured->ToInt();
216                 if (obscured == 0)
217                 {
218                         __activated = true;
219                         OnFrameActivated();
220                 }
221                 else
222                 {
223                         __activated = false;
224                         OnFrameDeactivated();
225                 }
226
227                 pArgs->RemoveAll(true);
228                 delete pArgs;
229
230                 return true;
231         }
232         else if (*pType == _ACTIVATE_FRAME)
233         {
234                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
235                 if (pEcoreEvas)
236                 {
237                         pEcoreEvas->ActivateWindow(*this);
238                 }
239
240                 pArgs->RemoveAll(true);
241                 delete pArgs;
242
243                 return true;
244         }
245
246         return false;
247 }
248
249 void
250 _Frame::OnFrameActivated(void)
251 {
252         int childcount = GetChildCount();
253
254         for (int i = 0 ; i < childcount ; i++)
255         {
256                 _Control* pChild = GetChild(i);
257                 _Form* pForm = dynamic_cast<_Form*>(pChild);
258                 if (pForm)
259                 {
260                         pForm->MoveOverlayRegion(true);
261                 }
262         }
263
264         // Fire Event.
265         IEventArg* pArg = _FrameEvent::CreateFrameEventArgN(*this, _FRAME_STATUS_ACTIVATED);
266         __pFrameEvent->Fire(*pArg);
267
268         if (GetChildCount() < 1)
269         {
270                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
271                 SysTryReturnVoidResult(NID_UI_CTRL, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] Unable to get evas");
272
273                 if (GetShowMode() == FRAME_SHOW_MODE_FULL_SCREEN)
274                 {
275                         pEcoreEvas->SetIndicatorShowState(*GetRootWindow(), false);
276                 }
277         }
278 }
279
280 void
281 _Frame::OnFrameDeactivated(void)
282 {
283         int childcount = GetChildCount();
284
285         for (int i = 0 ; i < childcount ; i++)
286         {
287                 _Control* pChild = GetChild(i);
288                 _Form* pForm = dynamic_cast<_Form*>(pChild);
289                 if (pForm)
290                 {
291                         pForm->MoveOverlayRegion(false);
292                 }
293         }
294
295         // Fire Event.
296         IEventArg* pArg = _FrameEvent::CreateFrameEventArgN(*this, _FRAME_STATUS_DEACTIVATED);
297         __pFrameEvent->Fire(*pArg);
298
299         _TouchManager* pTouchManager = _TouchManager::GetInstance();
300         if (pTouchManager)
301         {
302                 pTouchManager->SetTouchCanceled(null);
303         }
304 }
305
306 void
307 _Frame::OnFrameMinimized(void)
308 {
309         IEventArg* pArg = _FrameEvent::CreateFrameEventArgN(*this, _FRAME_STATUS_MINIMIZED);
310         __pFrameEvent->Fire(*pArg);
311 }
312
313 void
314 _Frame::OnFrameRestored(void)
315 {
316         __minimized = false;
317                 
318         IEventArg* pArg = _FrameEvent::CreateFrameEventArgN(*this, _FRAME_STATUS_RESTORED);
319         __pFrameEvent->Fire(*pArg);
320 }
321
322 bool
323 _Frame::IsFocusableDescendant(const _Control* pFocus) const
324 {
325         const _Control* pTopChild = GetChild(GetChildCount() - 1); // Find the current Form.
326         const _Control* pTop = pFocus;
327
328         if (pTop == this)
329         {
330                 return true;
331         }
332
333         // 1. Find the Form of the pFocus.
334         // 2. If the Form is the current Form, then return true.
335         while (pTop)
336         {
337                 if (pTop == pTopChild)
338                 {
339                         return true;
340                 }
341                 pTop = pTop->GetParent();
342         }
343
344         return false;
345 }
346
347 void
348 _Frame::SetCurrentForm(const _Form* pForm)
349 {
350         result r = E_SUCCESS;
351
352         SysTryReturnVoidResult(NID_UI_CTRL, pForm != null, E_INVALID_ARG, "[E_INVALID_ARG] Form to become a new current form is null");
353         _Form* pNewForm = const_cast<_Form*>(pForm);
354
355         _Form* pCurrentForm = GetCurrentForm();
356         pNewForm->AddIndicatorObject();
357
358         if (pCurrentForm != null)
359         {
360                 if (pCurrentForm != pForm)
361                 {
362                         // Change order
363                         r = MoveChildToTop(*pForm);
364                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
365                         pNewForm->MoveOverlayRegion(true);
366
367                         pCurrentForm->SetVisibleState(false);
368                         pCurrentForm->MoveOverlayRegion(false);
369
370                         if (__pFormActivationChangeEventListener)
371                                 __pFormActivationChangeEventListener->OnFormDeactivated(*pCurrentForm);
372                 }
373         }
374
375         _Control* pFocus = pNewForm->GetFocused();
376         if (pFocus)
377         {
378                 pFocus->SetFocused();
379         }
380         else
381         {
382                 pNewForm->SetFocused();
383         }
384
385         pNewForm->SetVisibleState(true);
386         pNewForm->SetUpdateLayoutState(true);
387
388         if (__pFormActivationChangeEventListener)
389                 __pFormActivationChangeEventListener->OnFormActivated(*pNewForm);
390
391         SetLastResult(E_SUCCESS);
392
393         return;
394 }
395
396 _Form*
397 _Frame::GetCurrentForm(void) const
398 {
399         _Form* pCurrentForm = null;
400         int controlCount = GetChildCount();
401
402         if (controlCount > 0)
403         {
404                 for (int i = controlCount; i > 0; i--)
405                 {
406                         pCurrentForm = dynamic_cast<_Form*>(GetChild(i - 1));
407                         SysTryReturn(NID_UI_CTRL, pCurrentForm != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
408                         if (pCurrentForm->IsVisible())
409                         {
410                                 break;
411                         }
412                 }
413         }
414
415         return pCurrentForm;
416 }
417
418 bool
419 _Frame::IsOrientationRoot(void) const
420 {
421         return true;
422 }
423
424 void
425 _Frame::SetRotation(bool rotation)
426 {
427         __rotation = rotation;
428 }
429
430 void
431 _Frame::AddFrameEventListener(const _IFrameEventListener& listener)
432 {
433         __constructed = true;
434
435         result r = __pFrameEvent->AddListener(listener);
436         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
437 }
438
439 void
440 _Frame::RemoveFrameEventListener(const _IFrameEventListener& listener)
441 {
442         result r = __pFrameEvent->RemoveListener(listener);
443         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
444 }
445
446 void
447 _Frame::SetFormActivationChangeEventListener(const _IFormActivationChangeEventListener* plistener)
448 {
449         __pFormActivationChangeEventListener = const_cast<_IFormActivationChangeEventListener*>(plistener);
450 }
451
452
453 void
454 _Frame::SetFloatingBounds(const FloatRectangle& rect)
455 {
456         __floatingBounds = rect;
457 }
458
459 void
460 _Frame::SetFloatingBounds(const Rectangle& rect)
461 {
462         __floatingBounds = _CoordinateSystemUtils::ConvertToFloat(rect);
463 }
464
465 result
466 _Frame::SetShowMode(FrameShowMode showMode)
467 {
468         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
469         SysTryReturnResult(NID_UI_CTRL, pEcoreEvas, E_SYSTEM, "The method cannot proceed due to a severe system error.");
470
471         if (showMode == FRAME_SHOW_MODE_MINIMIZED)
472         {
473                 if (__minimized == true)
474                 {
475                         return E_SUCCESS;
476                 }
477
478                 pEcoreEvas->MinimizeWindow(*GetRootWindow());
479                 __minimized = true;
480
481                 return E_SUCCESS;
482         }
483         else
484         {
485                 if (__minimized == true)
486                 {
487                         return E_SYSTEM;
488                 }
489         }
490
491         bool changeMode = true;
492
493         if ((showMode == FRAME_SHOW_MODE_PARTIAL_SCREEN) || (showMode == FRAME_SHOW_MODE_PARTIAL_SCREEN_FLOATING))
494         {
495                 _Form* pCurrentForm = GetCurrentForm();
496
497                 if (pCurrentForm)
498                 {
499                         if (pCurrentForm->GetFormStyle() & _FORM_STYLE_INDICATOR)
500                         {
501                                 changeMode = false;
502                         }
503                 }
504         }
505
506         SysTryReturnResult(NID_UI_CTRL, changeMode == true, E_SYSTEM, "The method cannot proceed due to a severe system error.");
507
508         int oldShowMode = __showMode;
509         __showMode = showMode;
510
511         result r = E_SUCCESS;
512
513         switch (__showMode)
514         {
515         case FRAME_SHOW_MODE_FULL_SCREEN:
516                 if (oldShowMode == FRAME_SHOW_MODE_PARTIAL_SCREEN_FLOATING)
517                 {
518                         r = pEcoreEvas->SetFloatingMode(*GetRootWindow(), false);
519                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
520                 }
521
522                 if (oldShowMode != FRAME_SHOW_MODE_FULL_SCREEN)
523                 {
524                         FloatDimension screen = _ControlManager::GetInstance()->GetScreenSizeF();
525
526                         __restore = true;
527
528                         if (GetOrientation() == _CONTROL_ORIENTATION_PORTRAIT)
529                         {
530                                 SetBounds(FloatRectangle(0.0f, 0.0f, screen.width, screen.height));
531                         }
532                         else
533                         {
534                                 SetBounds(FloatRectangle(0.0f, 0.0f, screen.height, screen.width));
535                         }
536
537                         __restore = false;
538                 }
539
540                 break;
541         case FRAME_SHOW_MODE_PARTIAL_SCREEN:
542                 if (oldShowMode == FRAME_SHOW_MODE_PARTIAL_SCREEN_FLOATING)
543                 {
544                         r = pEcoreEvas->SetFloatingMode(*GetRootWindow(), false);
545                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
546                 }
547
548                 if (oldShowMode == FRAME_SHOW_MODE_FULL_SCREEN)
549                 {
550                         SetBounds(__floatingBounds);
551                 }
552                 else if (oldShowMode == FRAME_SHOW_MODE_PARTIAL_SCREEN_FLOATING)
553                 {
554                         pEcoreEvas->SetWindowBounds(*GetRootWindow(), _CoordinateSystemUtils::ConvertToInteger(__floatingBounds));
555                 }
556
557                 break;
558         case FRAME_SHOW_MODE_PARTIAL_SCREEN_FLOATING:
559                 if (oldShowMode != FRAME_SHOW_MODE_PARTIAL_SCREEN_FLOATING)
560                 {
561                         r = pEcoreEvas->SetFloatingMode(*GetRootWindow(), true);
562                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
563                 }
564
565                 if (oldShowMode == FRAME_SHOW_MODE_FULL_SCREEN)
566                 {
567                         SetBounds(__floatingBounds);
568                 }
569
570                 break;
571         default:
572                 break;
573         }
574
575         return E_SUCCESS;
576 }
577
578 FrameShowMode
579 _Frame::GetShowMode(void) const
580 {
581         if (__minimized == true)
582         {
583                 return FRAME_SHOW_MODE_MINIMIZED;
584         }
585         else
586         {
587                 return __showMode;
588         }
589 }
590
591 bool
592 _Frame::IsActivated(void) const
593 {
594         return __activated;
595 }
596
597 void
598 _Frame::OnChildAttached(const _Control& child)
599 {
600         _Form* pCurrentForm = GetCurrentForm();
601
602         if (pCurrentForm == &child)
603         {
604                 int controlCount = GetChildCount();
605
606                 if (controlCount > 1)
607                 {
608                         _Control* pOldCurrentForm = GetChild(controlCount - 2);
609                         SysTryReturnVoidResult(NID_UI_CTRL, pOldCurrentForm != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
610                         pOldCurrentForm->SetVisibleState(false);
611                 }
612         }
613 }
614
615 void
616 _Frame::OnChildDetached(const _Control& child)
617 {
618         int controlCount = GetChildCount();
619
620         if (controlCount > 0)
621         {
622                 _Control* pCurrentForm = GetChild(controlCount - 1);
623                 SysTryReturnVoidResult(NID_UI_CTRL, pCurrentForm, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
624                 pCurrentForm->SetVisibleState(true);
625         }
626         else
627         {
628                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
629                 SysTryReturnVoidResult(NID_UI_CTRL, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] Unable to get evas");
630                 pEcoreEvas->SetIndicatorShowState(*GetRootWindow(), false);
631         }
632 }
633
634 result
635 _Frame::OnBoundsChanging(const FloatRectangle& bounds)
636 {
637         int appType = _AppInfo::GetAppType();
638         if (appType & _APP_TYPE_IME_APP)
639         {
640                 SysLog(NID_UI_CTRL, "[Ime Rotation]");
641                 return E_SUCCESS;
642         }
643
644         if (__restore == false)
645         {
646                 __floatingBounds = bounds;
647         }
648
649         if ((__showMode == FRAME_SHOW_MODE_FULL_SCREEN) && (__restore == false))
650         {
651                 if (__constructed == false)
652                 {
653                         return E_SUCCESS;
654                 }
655                 else
656                 {
657                         if (__rotation == true)
658                         {
659                                 return E_SUCCESS;
660                         }
661                         else
662                         {
663                                 return E_UNSUPPORTED_OPERATION;
664                         }
665                 }
666         }
667
668         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
669         SysTryReturn(NID_UI, pEcoreEvas, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
670
671         pEcoreEvas->SetWindowBounds(*GetRootWindow(), _CoordinateSystemUtils::ConvertToInteger(bounds));
672         result r = GetLastResult();
673         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
674
675         return r;
676 }
677
678
679 result
680 _Frame::OnAttached(void)
681 {
682         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
683         SysAssert(pEcoreEvas);
684
685         pEcoreEvas->SetWindowVisibleState(*GetRootWindow(), true);
686
687         return E_SUCCESS;
688 }
689
690 void
691 _Frame::OnBackgroundColorChanged(Color& backgroundColor)
692 {
693         _RootVisualElement* pRootVE = GetRootVisualElement();
694         SysAssert(pRootVE);
695
696         _EflLayer* pLayer = static_cast<_EflLayer*>(pRootVE->GetNativeLayer());
697         SysAssert(pLayer);
698
699         byte alpha = backgroundColor.GetAlpha();
700         float opacity = static_cast<float>(alpha) / 255.0f;
701
702         pLayer->SetOpacity(opacity);
703 }
704
705 void
706 _Frame::ResetFocusList(void)
707 {
708         Tizen::Ui::Controls::_Form* pCurrentForm = GetCurrentForm();
709         if (pCurrentForm)
710         {
711                 pCurrentForm->ResetFocusList();
712         }
713 }
714 }}} // Tizen::Ui::Controls