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