apply v5.0 gui and fix N_SE-46379,46416
[platform/framework/native/uifw.git] / src / ui / FUi_WindowImpl.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                FUi_WindowImpl.cpp
19  * @brief               This is the implementation file for the _WindowImpl class.
20  */
21
22 #include <new>
23 #include <FBaseSysLog.h>
24 #include <FUiWindow.h>
25 #include "FUi_WindowImpl.h"
26 #include "FUi_Window.h"
27 #include "FUi_ErrorMessages.h"
28 #include "FUi_UiManagerProxy.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Ui::Animations;
34
35
36 namespace Tizen { namespace Ui
37 {
38
39 class _WindowImpl::WindowImplDelegate
40         : public _IWindowDelegate
41 {
42 public:
43         WindowImplDelegate(_WindowImpl& impl)
44                 : __impl(impl)
45                 , __core(impl.GetCore())
46                 , __public(impl.GetPublic())
47         {
48         }
49
50         virtual void OnActivated(void)
51         {
52                 IEnumeratorT <Runtime::IEventListener*>* pEnumerator = __impl.__pPublicWindowEventListeners->GetEnumeratorN();
53                 // 1. public
54                 while (pEnumerator->MoveNext() == E_SUCCESS)
55                 {
56                         Runtime::IEventListener* pListener = null;
57                         pEnumerator->GetCurrent(pListener);
58                         IWindowEventListener* pWindowEventListener = dynamic_cast <IWindowEventListener*>(pListener);
59                         if (!pWindowEventListener)
60                         {
61                                 continue;
62                         }
63
64                         pWindowEventListener->OnWindowActivated(__public);
65                 }
66                 delete pEnumerator;
67
68                 // 2. self
69                 __impl.SetWindowState(WINDOW_STATE_ACTIVATED);
70
71                 // 3. Impl
72                 __impl.OnActivated();
73
74                 // 4. Core
75                 __core.OnActivated();
76         }
77
78         virtual void OnNativeWindowActivated(void)
79         {
80                 __impl.OnNativeWindowActivated();
81                 __core.OnNativeWindowActivated();
82         }
83
84         virtual void OnDeactivated(void)
85         {
86                 IEnumeratorT <Runtime::IEventListener*>* pEnumerator = __impl.__pPublicWindowEventListeners->GetEnumeratorN();
87
88                 // 1. public
89                 while (pEnumerator->MoveNext() == E_SUCCESS)
90                 {
91                         Runtime::IEventListener* pListener = null;
92                         pEnumerator->GetCurrent(pListener);
93                         IWindowEventListener* pWindowEventListener = dynamic_cast <IWindowEventListener*>(pListener);
94                         if (!pWindowEventListener)
95                         {
96                                 continue;
97                         }
98
99                         pWindowEventListener->OnWindowDeactivated(__public);
100                 }
101                 delete pEnumerator;
102
103                 // 2. self
104                 __impl.SetWindowState(WINDOW_STATE_DEACTIVATED);
105
106                 // 3. Impl
107                 __impl.OnDeactivated();
108
109                 // 4. Core
110                 __core.OnDeactivated();
111         }
112
113         virtual void OnOwnerChanged(_Control* pOldOwner)
114         {
115                 __impl.OnOwnerChanged(pOldOwner);
116                 __core.OnOwnerChanged(pOldOwner);
117         }
118
119 private:
120         WindowImplDelegate(const WindowImplDelegate& rhs);
121         WindowImplDelegate& operator =(const WindowImplDelegate& rhs);
122
123 private:
124         _WindowImpl& __impl;
125         _Window& __core;
126         Window& __public;
127 }; // WindowImplDelegate
128
129
130 WindowState
131 _WindowImpl::GetErrorWindowState(void)
132 {
133         return WINDOW_STATE_INITIALIZED; // [ToDo] Not good choice. But no option.
134 }
135
136 _WindowImpl*
137 _WindowImpl::GetInstance(Window& window)
138 {
139         return static_cast <_WindowImpl*> (window._pControlImpl);
140 }
141
142 const _WindowImpl*
143 _WindowImpl::GetInstance(const Window& window)
144 {
145         return static_cast <const _WindowImpl*> (window._pControlImpl);
146 }
147
148 _WindowImpl*
149 _WindowImpl::CreateWindowImplN(Window* pPublic, const Rectangle& bounds,
150                                                                                 const Layout* pPublicPortraitLayout, const Layout* pPublicLandscapeLayout,
151                                                                                 bool resizable, bool movable)
152 {
153         result r = E_SUCCESS;
154
155         _Window* pCore = _Window::CreateWindowN();
156         SysTryReturn(NID_UI, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
157
158         _WindowImpl* pImpl =
159                 new (std::nothrow) _WindowImpl(pPublic, pCore, bounds,
160                                                                                                   pPublicPortraitLayout, pPublicLandscapeLayout,
161                                                                                                   resizable, movable);
162         r = CheckConstruction(pCore, pImpl);
163         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
164
165         ClearLastResult();
166
167         return pImpl;
168 }
169
170 _WindowImpl*
171 _WindowImpl::CreateWindowImplN(Window* pPublic, const FloatRectangle& bounds,
172                                                                                 const Layout* pPublicPortraitLayout, const Layout* pPublicLandscapeLayout,
173                                                                                 bool resizable, bool movable)
174 {
175         result r = E_SUCCESS;
176
177         _Window* pCore = _Window::CreateWindowN();
178         SysTryReturn(NID_UI, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
179
180         _WindowImpl* pImpl =
181                 new (std::nothrow) _WindowImpl(pPublic, pCore, bounds,
182                                                                                                   pPublicPortraitLayout, pPublicLandscapeLayout,
183                                                                                                   resizable, movable);
184         r = CheckConstruction(pCore, pImpl);
185         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
186
187         ClearLastResult();
188
189         return pImpl;
190 }
191
192 _WindowImpl::~_WindowImpl(void)
193 {
194         _WindowImpl::GetCore().ResetWindowDelegate();
195
196         delete __pImplDelegate;
197         __pImplDelegate = null;
198
199         Close();
200
201         delete __pPublicWindowEventListeners;
202         __pPublicWindowEventListeners = null;
203 }
204
205 const char*
206 _WindowImpl::GetPublicClassName(void) const
207 {
208         return "Tizen::Ui::Window";
209 }
210
211 const Window&
212 _WindowImpl::GetPublic(void) const
213 {
214         return static_cast <const Window&>(_ControlImpl::GetPublic());
215 }
216
217 Window&
218 _WindowImpl::GetPublic(void)
219 {
220         return static_cast <Window&>(_ControlImpl::GetPublic());
221 }
222
223 const _Window&
224 _WindowImpl::GetCore(void) const
225 {
226         return static_cast <const _Window&>(_ControlImpl::GetCore());
227 }
228
229 _Window&
230 _WindowImpl::GetCore(void)
231 {
232         return static_cast <_Window&>(_ControlImpl::GetCore());
233 }
234
235 void
236 _WindowImpl::AddWindowEventListener(Tizen::Ui::IWindowEventListener& listener)
237 {
238         ClearLastResult();
239         SysTryReturnVoidResult(NID_UI,
240                 __pPublicWindowEventListeners->Add(&listener) == E_SUCCESS,
241                 E_SYSTEM, _UiError::SYSTEM);
242 }
243
244 void
245 _WindowImpl::RemoveWindowEventListener(Tizen::Ui::IWindowEventListener& listener)
246 {
247         ClearLastResult();
248         SysTryReturnVoidResult(NID_UI,
249                 __pPublicWindowEventListeners->Remove(&listener) == E_SUCCESS,
250                 E_SYSTEM, _UiError::SYSTEM);
251 }
252
253 void
254 _WindowImpl::OnActivated(void)
255 {
256
257 }
258
259 void
260 _WindowImpl::OnNativeWindowActivated(void)
261 {
262
263 }
264
265 void
266 _WindowImpl::OnDeactivated(void)
267 {
268
269 }
270
271 void
272 _WindowImpl::OnOwnerChanged(_Control* pOldOwner)
273 {
274
275 }
276
277 result
278 _WindowImpl::OnBoundsChanging(const Rectangle& bounds)
279 {
280         result r = E_SUCCESS;
281         r = _ControlImpl::OnBoundsChanging(bounds);
282
283         /*
284         if (_WindowImpl::GetInstance(GetPublic()) == null)
285         {
286                 r = CallOnBoundsChanging(bounds);
287         }
288         */
289
290         return r;
291 }
292
293 void
294 _WindowImpl::OnRotated(_ControlRotation rotation)
295 {
296
297 }
298
299 result
300 _WindowImpl::Open(bool drawAndShow)
301 {
302         return GetCore().Open(drawAndShow);
303 }
304
305 void
306 _WindowImpl::Close(void)
307 {
308         GetCore().Close();
309 }
310
311 WindowState
312 _WindowImpl::GetWindowState(void) const
313 {
314         ClearLastResult();
315         return GetCore().GetWindowState();
316 }
317
318 void
319 _WindowImpl::SetWindowState(WindowState windowState)
320 {
321         GetCore().SetWindowState(windowState);
322 }
323
324 DisplayContext*
325 _WindowImpl::GetDisplayContext(void) const
326 {
327         return GetCore().GetDisplayContext();
328 }
329
330 result
331 _WindowImpl::SetZOrderGroup(WindowZOrderGroup windowZOrderGroup)
332 {
333         _UiManagerProxy uiManagerProxy;
334         result r = uiManagerProxy.Construct();
335         SysTryReturnResult(NID_UI, r == E_SUCCESS, r, "Propagating.");
336
337         NativeWindowHandle handle = GetCore().GetNativeHandle();
338         r = uiManagerProxy.SetZOrderGroup(handle, static_cast<int>(windowZOrderGroup));
339         SysTryReturnResult(NID_UI, r == E_SUCCESS, r, "Propagating.");
340
341         return GetCore().SetZOrderGroup(windowZOrderGroup);
342 }
343
344 Control*
345 _WindowImpl::GetOwner(void) const
346 {
347         _Control* pOwner = GetCore().GetOwner();
348         if (pOwner)
349         {
350                 _ControlImpl* pImpl = static_cast<_ControlImpl*>(pOwner->GetUserData());
351                 if (pImpl)
352                 {
353                         return (&pImpl->GetPublic());
354                 }
355         }
356
357         return null;
358 }
359
360 void
361 _WindowImpl::SetOwner(Tizen::Ui::Control *pControl)
362 {
363         _ControlImpl* pImpl = _ControlImpl::GetInstance(*pControl);
364         SysAssertf(pImpl != null, "Not yet constructed. Construct() should be called before use.");
365
366         GetCore().SetOwner(&pImpl->GetCore());
367 }
368
369 NativeWindowHandle
370 _WindowImpl::GetNativeHandle(void) const
371 {
372         return GetCore().GetNativeHandle();
373 }
374
375 result
376 _WindowImpl::Destroy(void)
377 {
378         result r = E_SUCCESS;
379
380         Control* pOwner = GetOwner();
381         if (pOwner)
382         {
383                 _ControlImpl* pImpl = _ControlImpl::GetInstance(*pOwner);
384                 pImpl->GetCore().DetachOwnee(GetCore());
385         }
386         RemoveAllChildren(true, true);
387         Window* pWindow = &GetPublic();
388         delete pWindow;
389         pWindow = null;
390
391         return r;
392 }
393
394 void
395 _WindowImpl::Initialize(Window* pPublic, _Window* pCore, const Layout* pPublicPortraitLayout, const Layout* pPublicLandscapeLayout)
396 {
397         SysAssert(pPublic);
398         SysAssert(pCore);
399
400         // Creation
401         __pImplDelegate = new (std::nothrow) WindowImplDelegate(*this);
402         SysTryReturnVoidResult(NID_UI, __pImplDelegate, E_OUT_OF_MEMORY, _UiError::OUT_OF_MEMORY);
403
404         __pPublicWindowEventListeners = CreatePublicEventListenerListN();
405         result r = GetLastResult();
406         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
407
408         pCore->SetWindowDelegate(*__pImplDelegate);
409         r = GetLastResult();
410         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
411
412         return;
413
414 CATCH:
415         delete __pPublicWindowEventListeners;
416         __pPublicWindowEventListeners = null;
417 }
418
419 _WindowImpl::_WindowImpl(Window* pPublic, _Window* pCore, const Layout* pPublicPortraitLayout, const Layout* pPublicLandscapeLayout)
420         : _ContainerImpl(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout)
421         , __pPublicWindowEventListeners(null)
422         , __pImplDelegate(null)
423 {
424         Initialize(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout);
425 }
426
427 _WindowImpl::_WindowImpl(Window* pPublic, _Window* pCore, const Rectangle& bounds, const Layout* pPublicPortraitLayout, const Layout* pPublicLandscapeLayout, bool resizable, bool movable)
428         : _ContainerImpl(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout)
429 {
430         result r = E_SUCCESS;
431
432         Initialize(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout);
433
434         r = GetLastResult();
435         if (IsFailed(r))
436         {
437                 return;
438         }
439
440         bool allOrNone = (pPublicPortraitLayout && pPublicLandscapeLayout) || (!pPublicPortraitLayout && !pPublicLandscapeLayout);
441         SysAssert(allOrNone);
442
443         r = SetBounds(bounds);
444         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
445
446         pCore->SetResizable(resizable);
447         pCore->SetMovable(movable);
448 }
449
450 _WindowImpl::_WindowImpl(Window* pPublic, _Window* pCore, const FloatRectangle& bounds, const Layout* pPublicPortraitLayout, const Layout* pPublicLandscapeLayout, bool resizable, bool movable)
451         : _ContainerImpl(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout)
452 {
453         result r = E_SUCCESS;
454
455         Initialize(pPublic, pCore, pPublicPortraitLayout, pPublicLandscapeLayout);
456
457         r = GetLastResult();
458         if (IsFailed(r))
459         {
460                 return;
461         }
462
463         bool allOrNone = (pPublicPortraitLayout && pPublicLandscapeLayout) || (!pPublicPortraitLayout && !pPublicLandscapeLayout);
464         SysAssert(allOrNone);
465
466         r = SetBounds(bounds);
467         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
468
469         pCore->SetResizable(resizable);
470         pCore->SetMovable(movable);
471 }
472
473 }} //Tizen::Ui