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