41f711ac9f1b9138ee3b23f7e3cabaacb957fd6a
[platform/framework/native/uifw.git] / src / ui / FUi_Window.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 /**
19  * @file                FUi_Window.cpp
20  * @brief               This is the implementation file for the _Window class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FUiAnimDisplayContext.h>
25 #include <FUiWindow.h>
26 #include <FBase_Log.h>
27 #include "FUi_Window.h"
28 #include "FUi_ControlManager.h"
29 #include "FUi_TouchManager.h"
30 #include "FUiAnim_DisplayContextImpl.h"
31 #include "FUiAnim_RootVisualElement.h"
32 #include "FUiAnim_VisualElement.h"
33 #include "FUi_EcoreEvasMgr.h"
34 #include "FUi_EcoreEvas.h"
35 #include "FUiAnim_DisplayManager.h"
36 #include "FUiAnim_EflLayer.h"
37 #include "FUiCtrl_Form.h"
38 #include "FUiCtrl_FormImpl.h"
39 #include "FUiCtrl_FrameImpl.h"
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Ui::Animations;
45 using namespace Tizen::Ui::Controls;
46
47 namespace Tizen { namespace Ui
48 {
49
50 _Window*
51 _Window::CreateWindowN(void)
52 {
53         _Window* pWindow = new (std::nothrow) _Window;
54         SysTryReturn(NID_UI, pWindow, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
55
56         result r = E_SUCCESS;
57
58         if (IsFailed(GetLastResult()))
59         {
60                 goto CATCH;
61         }
62
63         r = pWindow->CreateRootVisualElement();
64         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
65
66         pWindow->AcquireHandle();
67
68         return pWindow;
69
70 CATCH:
71         delete pWindow;
72         return null;
73 }
74
75 _Window::~_Window(void)
76 {
77         __destroying = true;
78         Close();
79         SetOwner(null);
80
81         if (__pDisplayContext)
82         {
83                 _DisplayContextImpl::DestroyPublicInstance(*__pDisplayContext);
84                 __pDisplayContext = null;
85         }
86
87         if(__pLayer)
88         {
89                 //_EflLayer* pLayer = static_cast<_EflLayer*>(__pLayer);
90                 //Ecore_X_Window win = (Ecore_X_Window) ecore_evas_window_get(pLayer->GetEcoreEvas());
91                 //SysLog(NID_UI, "[Multi_Window] destroy x window(0x%x)", win);
92
93                 if(__pRootVisualElement)
94                 {
95                         __pRootVisualElement->DetachChild(*GetVisualElement());
96                 }
97                 delete __pLayer;
98                 __pLayer = NULL;
99         }
100
101         __pRootVisualElement =NULL;
102 }
103
104 _IWindowDelegate&
105 _Window::GetWindowDelegate(void) const
106 {
107         if (__destroying)
108         {
109                 return const_cast<_Window&>(*this);
110         }
111
112         SysAssert(__pWindowDelegate);
113         return *__pWindowDelegate;
114 }
115
116 String
117 _Window::GetDescription(void) const
118 {
119         String description = _Control::GetDescription();
120         String descriptionTemp(L"");
121
122         descriptionTemp.Format(LOG_LEN_MAX, L"_Window: xid(0x%x) owner(0x%x) delegate(0x%x) activated(%d) destroying(%d) displayContext(0x%x)",
123                 GetNativeHandle(), __pOwner, __pWindowDelegate, __activated, __destroying, __pDisplayContext);
124
125         description.Append(descriptionTemp);
126
127         return description;
128 }
129
130 void
131 _Window::SetRootVisualElement(const Tizen::Ui::Animations::_RootVisualElement& rootVisualElement)
132 {
133         _RootVisualElement* pRootVE = const_cast<_RootVisualElement*>(&rootVisualElement);
134
135         __pRootVisualElement = pRootVE;
136 }
137
138 void
139 _Window::SetLayer(const Tizen::Ui::Animations::_NativeLayer& layer)
140 {
141         _NativeLayer* pLayer = const_cast<_NativeLayer*>(&layer);
142
143         __pLayer = pLayer;
144 }
145
146 bool
147 _Window::IsActivatedOnOpen(void) const
148 {
149         return true;
150 }
151
152 bool
153 _Window::IsActivated(void) const
154 {
155         return __activated;
156 }
157
158 void
159 _Window::SetWindowDelegate(_IWindowDelegate& delegate)
160 {
161         ClearLastResult();
162         __pWindowDelegate = &delegate;
163 }
164
165 void
166 _Window::ResetWindowDelegate(void)
167 {
168         ClearLastResult();
169         __pWindowDelegate = this;
170 }
171
172 bool
173 _Window::IsAttached(void) const
174 {
175         return _ControlManager::GetInstance()->IsWindowAttached(*this);
176 }
177
178 result
179 _Window::Open(bool drawAndShow)
180 {
181         bool visibleState = GetVisibleState();
182         if (!__isOpened && visibleState)
183         {
184                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
185                 SysTryReturn(NID_UI, pEcoreEvas, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
186                 pEcoreEvas->SetWindowVisibleState(*GetRootWindow(), visibleState);
187                 __isOpened = true;
188                 __isInitialized = true;
189                 return _ControlManager::GetInstance()->OpenWindow(*this, drawAndShow);
190         }
191
192         return E_SUCCESS;
193 }
194
195 void
196 _Window::Close(void)
197 {
198         if (__isOpened)
199         {
200                 bool visibleState = GetVisibleState();
201                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
202                 SysTryReturnVoidResult(NID_UI, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
203                 pEcoreEvas->SetWindowVisibleState(*GetRootWindow(), visibleState);
204
205                 _ControlManager::GetInstance()->CloseWindow(*this);
206                 __isOpened = false;
207         }
208 }
209
210 _Control*
211 _Window::GetOwner(void) const
212 {
213         ClearLastResult();
214         return __pOwner;
215 }
216
217 void
218 _Window::SetOwner(_Control* pOwner)
219 {
220         ClearLastResult();
221
222         if (pOwner)
223         {
224                 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
225                 if (pEcoreEvas)
226                 {
227                         if (__transient == true)
228                         {
229                                 pEcoreEvas->SetOwner(*this, *pOwner);
230                         }
231
232                         __transient = true;
233                 }
234         }
235
236         if (pOwner == __pOwner)
237         {
238                 return;
239         }
240
241         if (__pOwner)
242         {
243                 __pOwner->DetachOwnee(*this);
244         }
245
246         if (pOwner)
247         {
248                 pOwner->AttachOwnee(*this);
249         }
250         GetWindowDelegate().OnOwnerChanged(__pOwner);
251         __pOwner = pOwner;
252 }
253
254 result
255 _Window::CreateRootVisualElement(void)
256 {
257         result r = CreateLayer();
258         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
259
260         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
261         SysAssert(pEcoreEvas);
262 #if !defined(WINDOW_BASE_ROTATE)
263         pEcoreEvas->SetWindowOrientationEnabled(*this, false);
264 #else
265         __rotation = pEcoreEvas->GetWindowRotation(*this);
266         pEcoreEvas->RegisterWindowStateCallback(*this);
267 #endif
268
269         // Get visual element.
270         _VisualElement* pVisualElement = GetVisualElement();
271         SysAssert(pVisualElement);
272
273         pVisualElement->SetName(L"Window");
274         pVisualElement->SetClipChildrenEnabled(false);
275         pVisualElement->SetRenderOperation(_VisualElement::RENDER_OPERATION_COPY);
276
277         // Attach visual element to root visual element.
278         __pRootVisualElement->AttachChild(*GetVisualElement());
279
280         return E_SUCCESS;
281 }
282
283 result
284 _Window::CreateLayer(void)
285 {
286         _DisplayManager* pDisplayManager = _DisplayManager::GetInstance();
287         SysAssert(pDisplayManager);
288
289         __pLayer = _NativeLayer::CreateInstanceN();
290         SysAssert(__pLayer);
291         __pRootVisualElement = __pLayer->GetRootVisualElement();
292
293         _EflLayer* pLayer = static_cast<_EflLayer*>(__pLayer);
294         SysAssert(pLayer);
295
296         int rootWindowW = 0;
297         int rootWindowH = 0;
298         ecore_x_window_size_get(ecore_x_window_root_get((Ecore_X_Window)ecore_evas_window_get(pLayer->GetEcoreEvas())), &rootWindowW, &rootWindowH);
299
300         pLayer->SetBounds(FloatRectangle(0.0f, 0.0f, (float)rootWindowW, (float)rootWindowH));
301         pLayer->SetOpacity(0);
302
303         __pRootVisualElement->SetName(L"Root");
304
305         return E_SUCCESS;
306 }
307
308 bool
309 _Window::IsFocusableDescendant(const _Control* pFocus) const
310 {
311         return true;
312 }
313
314 void
315 _Window::SetActivationEnabled(bool enable)
316 {
317         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
318         SysTryReturnVoidResult(NID_UI, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
319
320         pEcoreEvas->SetWindowActivationEnabled(*GetRootWindow(), enable);
321
322         SetLastResult(E_SUCCESS);
323 }
324
325 bool
326 _Window::IsActivationEnabled(void)
327 {
328         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
329         SysTryReturn(NID_UI, pEcoreEvas, true, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
330
331         bool enable = pEcoreEvas->IsWindowActivationEnabled(*GetRootWindow());
332
333         SetLastResult(E_SUCCESS);
334
335         return enable;
336 }
337
338 #if !defined(WINDOW_BASE_ROTATE)
339 void
340 _Window::SetOrientationEnabled(bool enable)
341 {
342         __orientationEnabled = enable;
343 }
344
345 bool
346 _Window::IsOrientationEnabled(void)
347 {
348         return __orientationEnabled;
349 }
350 #endif
351
352 _RootVisualElement*
353 _Window::GetRootVisualElement(void) const
354 {
355         return __pRootVisualElement;
356 }
357
358 NativeWindowHandle
359 _Window::GetNativeHandle(void) const
360 {
361         _RootVisualElement* pRootVE = GetRootVisualElement();
362         SysAssert(pRootVE);
363
364         _EflLayer* pLayer = static_cast<_EflLayer*>(pRootVE->GetNativeLayer());
365         SysAssert(pLayer);
366
367         Ecore_Evas* pEcoreEvas = pLayer->GetEcoreEvas();
368         Ecore_X_Window win = (Ecore_X_Window) ecore_evas_window_get(pEcoreEvas);
369
370         return win;
371 }
372
373 void
374 _Window::OnActivated(void)
375 {
376         bool isFrameActivated = _ControlManager::GetInstance()->IsFrameActivated();
377         SysLog(NID_UI, "frameActivated(%d)", isFrameActivated);
378
379         _Control* focusedControl = GetFocusedControl();
380
381         if (focusedControl)
382         {
383                 focusedControl->SetFocused();
384         }
385
386         if (!isFrameActivated)
387         {
388                 return;
389         }
390
391         _Control* pFocus = GetFocused();
392         if (pFocus)
393         {
394                 pFocus->SetFocused();
395         }
396         else
397         {
398                 SetFocused();
399         }
400
401         _TouchManager* pTouchMgr = _TouchManager::GetInstance();
402         if (pTouchMgr)
403         {
404                 pTouchMgr->SetTouchCanceled(true);
405         }
406 }
407
408 void
409 _Window::OnNativeWindowActivated(void)
410 {
411
412 }
413
414 bool
415 _Window::OnNotifiedN(const _Control& source, IList* pArgs)
416 {
417         SysTryReturn(NID_UI_CTRL, pArgs, false, E_SYSTEM, "[E_SYSTEM] pArgs is null.")
418
419         String* pType = dynamic_cast <String*>(pArgs->GetAt(0));
420         SysTryReturn(NID_UI_CTRL, pType, false, E_SYSTEM, "[E_SYSTEM] pType is null.")
421
422         if (*pType == L"VisibilityEvent")
423         {
424                 int obscured = 0;
425
426                 Integer* pObscured = dynamic_cast<Integer*>(pArgs->GetAt(1));
427                 if (pObscured == null)
428                 {
429                         pArgs->RemoveAll(true);
430                         delete pArgs;
431
432                         return true;
433                 }
434
435                 obscured = pObscured->ToInt();
436                 if (obscured == 0)
437                 {
438                         GetWindowDelegate().OnNativeWindowActivated();
439                 }
440
441                 pArgs->RemoveAll(true);
442                 delete pArgs;
443
444                 return true;
445         }
446
447         return false;
448 }
449
450 void
451 _Window::OnDeactivated(void)
452 {
453         bool isFrameActivated = _ControlManager::GetInstance()->IsFrameActivated();
454         SysLog(NID_UI, "frameActivated(%d)", isFrameActivated);
455 }
456
457 void
458 _Window::OnOwnerChanged(_Control* pOldOwner)
459 {
460
461 }
462
463 result
464 _Window::OnBoundsChanging(const Rectangle& bounds)
465 {
466         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
467         SysTryReturn(NID_UI, pEcoreEvas, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
468
469         pEcoreEvas->SetWindowBounds(*GetRootWindow(), bounds);
470         result r = GetLastResult();
471         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
472
473         return r;
474 }
475
476 void
477 _Window::OnVisibleStateChanged(void)
478 {
479         ClearLastResult();
480
481         bool visibleState = GetVisibleState();
482         if (visibleState == false)
483         {
484                 Close();
485         }
486
487         result r = GetLastResult();
488         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
489
490         SetLastResult(E_SUCCESS);
491 }
492
493 result
494 _Window::OnAttachingToMainTree(const _Control* pParent)
495 {
496         _EflLayer* pLayer = static_cast<_EflLayer*>(__pRootVisualElement->GetNativeLayer());
497         SysAssert(pLayer);
498         SetVisibleState(true);
499         pLayer->SetShowState(true);
500
501         return E_SUCCESS;
502 }
503
504 result
505 _Window::OnDetachingFromMainTree(void)
506 {
507         _EflLayer* pLayer = static_cast<_EflLayer*>(__pRootVisualElement->GetNativeLayer());
508         SysAssert(pLayer);
509         pLayer->SetShowState(false);
510
511         return E_SUCCESS;
512 }
513
514 #if defined(WINDOW_BASE_ROTATE)
515 void
516 _Window::OnWindowStateChanged(void)
517 {
518         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
519         if (!pEcoreEvas)
520         {
521                 return;
522         }
523
524         int rotation = pEcoreEvas->GetWindowRotation(*this);
525
526         Rectangle bounds = GetBounds();
527         SysLog(NID_UI, "[Window-based Rotation][Window : 0x%x, %d, %d, %d, %d] OnWindowStateChanged is called with prev rotation(__rotation) and cur rotation(rotation)", 
528                 GetNativeHandle(), bounds.x, bounds.y, bounds.width, bounds.height, __rotation, rotation);
529
530         if (__rotation != rotation)
531         {
532                 __rotation = rotation;
533
534                 int childCount = GetChildCount();
535                 for (int i = childCount; i > 0; i--)
536                 {
537                         _Control* pChild = GetChild(i - 1);
538                         
539                         _Form* pForm = dynamic_cast<_Form*>(pChild);
540                         if (pForm)
541                         {
542                                 if (pForm->IsVisible())
543                                 {
544                                         // Current Form
545                                         _FormImpl* pFormImpl = static_cast<_FormImpl*>(pForm->GetUserData());
546                                         if (pFormImpl)
547                                         {
548                                                 pFormImpl->UpdateOrientation();
549                                         }
550
551                                         return;
552                                 }
553                         }
554                 }
555
556                 if (IsOrientationRoot())
557                 {
558                         _FrameImpl* pFrameImpl = static_cast<_FrameImpl*>(GetUserData());
559                         if (pFrameImpl)
560                         {
561                                 pFrameImpl->UpdateOrientation();
562                         }
563                 }
564                 else
565                 {
566                         _ControlOrientation controlOrientation = (rotation == 0 || rotation == 180) ? _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
567                         ChangeLayout(controlOrientation);
568                         pEcoreEvas->UpdateWindowBounds(*this);
569                 }
570         }
571 }
572 #endif
573
574 bool
575 _Window::IsLayoutChangable(void) const
576 {
577         return false;
578 }
579
580 void
581 _Window::OnChangeLayout(_ControlOrientation orientation)
582 {
583         SysLog(NID_UI, "[Test] _Window::OnChangeLayout()");
584
585         // if window, change its size specially for full-screen(Form, Frame, etc.).
586         _Control::OnChangeLayout(orientation); // if called, layout will be changed.
587 }
588
589 WindowState
590 _Window::GetWindowState(void) const
591 {
592         ClearLastResult();
593         return __windowState;
594 }
595
596 void
597 _Window::SetWindowState(WindowState windowState)
598 {
599         __windowState = windowState;
600 }
601
602 DisplayContext*
603 _Window::GetDisplayContext(void) const
604 {
605         if (!__pDisplayContext)
606         {
607                 SysTryReturn(NID_UI, __pLayer, null, E_SYSTEM, "[E_SYSTEM] Cannot gain the DisplayContext. This window is not completed.");
608
609                 __pDisplayContext = _DisplayContextImpl::CreatePublicInstance(*__pLayer);
610         }
611
612         result r = GetLastResult();
613         SysTryReturn(NID_UI, __pDisplayContext, null, r, "[%s] Propagating.", GetErrorMessage(r));
614
615         return __pDisplayContext;
616 }
617
618 result
619 _Window::SetZOrderGroup(int windowZOrderGroup)
620 {
621         __transient = false;
622
623         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
624         bool visible = pEcoreEvas->IsWindowVisible(*this);
625         bool activationEnabled = pEcoreEvas->IsWindowActivationEnabled(*this);
626
627         if ((visible == true) && (activationEnabled == true))
628         {
629                 pEcoreEvas->ActivateWindow(*this);
630         }
631
632         return E_SUCCESS;
633 }
634
635 _Window::_Window()
636         : __windowState(WINDOW_STATE_INITIALIZED)
637         , __pOwner(null)
638         , __pWindowDelegate(null)
639         , __activated(false)
640         , __destroying(false)
641         , __pRootVisualElement(null)
642         , __pLayer(null)
643         , __pDisplayContext(null)
644         , __systemWindow(false)
645         , __isOpened(false)
646         , __isInitialized(false)
647         , __pFocusedControl(null)
648         , __transient(true)
649 #if !defined(WINDOW_BASE_ROTATE)
650         , __orientationEnabled(false)
651 #else
652         , __rotation(0)
653 #endif
654 {
655         SetControlDelegate(*this);
656         SetWindowDelegate(*this);
657         SetClipToParent(false); // [ToDo] exception check.
658 }
659
660 void
661 _Window::Activate(void)
662 {
663         if (__activated)
664         {
665                 return;
666         }
667
668         __activated = true;
669         GetWindowDelegate().OnActivated();
670 }
671
672 void
673 _Window::Deactivate(void)
674 {
675         __activated = false;
676
677         if (__destroying == false)
678         {
679                 GetWindowDelegate().OnDeactivated();
680         }
681 }
682
683 void
684 _Window::SetSystemWindow(bool systemWindow)
685 {
686         __systemWindow = systemWindow;
687 }
688
689 bool
690 _Window::IsSystemWindow(void) const
691 {
692         return __systemWindow;
693 }
694
695 void
696 _Window::SetFocusedControl(const _Control* pControl)
697 {
698         __pFocusedControl = const_cast<_Control*>(pControl);
699 }
700
701 _Control*
702 _Window::GetFocusedControl(void) const
703 {
704         return __pFocusedControl;
705 }
706 }} // Tizen::Ui