Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_FrameImpl.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 #include <new>
19 #include <FUiCtrlForm.h>
20 #include <FUiAnimFrameAnimator.h>
21 #include <FUiCtrlIFrameEventListener.h>
22 #include <FBaseSysLog.h>
23 #include "FUi_ControlManager.h"
24 #include "FUi_OrientationAgent.h"
25 #include "FUiCtrlForm.h"
26 #include "FUiCtrl_FrameImpl.h"
27 #include "FUiCtrl_FormImpl.h"
28 #include "FUiCtrl_Frame.h"
29 #include "FUiCtrl_PublicFrameEvent.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Runtime;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Ui::Animations;
36
37 namespace Tizen { namespace Ui { namespace Controls {
38
39 const _FrameImpl*
40 _FrameImpl::GetInstance(const Frame& frame)
41 {
42         return static_cast<const _FrameImpl*> (frame._pControlImpl);
43 }
44
45 _FrameImpl*
46 _FrameImpl::GetInstance(Frame& frame)
47 {
48         return static_cast<_FrameImpl*> (frame._pControlImpl);
49 }
50
51 void
52 _FrameImpl::Dispose(void)
53 {
54         GetCore().RemoveFrameEventListener(*this);
55
56         if (__pFrameEvent)
57         {
58                 delete __pFrameEvent;
59                 __pFrameEvent = null;
60         }
61
62         delete __pOrientationAgent;
63         __pOrientationAgent = null;
64
65         delete __pFrameAnimator;
66         __pFrameAnimator = null;
67 }
68
69 _FrameImpl::_FrameImpl(Frame* pPublic, _Frame* pCore)
70         : _WindowImpl(pPublic, pCore)
71         , __pFrameEvent(null)
72         , __pFrameAnimator(null)
73         , __pOrientationAgent(null)
74 {
75         __pFrameEvent = _PublicFrameEvent::CreateInstanceN(*pPublic);
76         SysTryReturnVoidResult(NID_UI_CTRL, __pFrameEvent, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
77
78         __pOrientationAgent = _OrientationAgent::CreateInstanceN(*pPublic);
79         SysTryCatch(NID_UI_CTRL, __pOrientationAgent != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
80
81         return;
82
83 CATCH:
84         Dispose();
85 }
86
87 _FrameImpl::~_FrameImpl(void)
88 {
89         // [ToDo] Wrong point to call OnFrameTerminating().
90         if (__pFrameEvent)
91         {
92                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_TERMINATING);
93                 if (pEventArg)
94                 {
95                         __pFrameEvent->Fire(*pEventArg);
96                 }
97         }
98
99         Dispose();
100         ClearLastResult();
101 }
102
103 const char*
104 _FrameImpl::GetPublicClassName(void) const
105 {
106         return "Tizen::Ui::Controls::Frame";
107 }
108
109 const Frame&
110 _FrameImpl::GetPublic(void) const
111 {
112         return static_cast<const Frame&>(_ControlImpl::GetPublic());
113 }
114
115 Frame&
116 _FrameImpl::GetPublic(void)
117 {
118         return static_cast<Frame&>(_ControlImpl::GetPublic());
119 }
120
121 const _Frame&
122 _FrameImpl::GetCore(void) const
123 {
124         return static_cast<const _Frame&>(_ControlImpl::GetCore());
125 }
126
127 _Frame&
128 _FrameImpl::GetCore(void)
129 {
130         return static_cast<_Frame&>(_ControlImpl::GetCore());
131 }
132
133 result
134 _FrameImpl::SetCurrentForm(const _FormImpl& formImpl)
135 {
136         result r = E_SUCCESS;
137
138         _FormImpl* pNewCurForm = const_cast<_FormImpl*>(&formImpl);
139
140         SysTryReturn(NID_UI_CTRL,
141                 pNewCurForm != null, E_INVALID_ARG,
142                 E_INVALID_ARG, "[E_INVALID_ARG] The Form is not initialized.");
143
144         SysTryReturn(NID_UI_CTRL,
145                 IsAncestorOf(pNewCurForm), E_INVALID_ARG,
146                 E_INVALID_ARG, "[E_INVALID_ARG] The Form is not the child of this Frame.");
147
148         if (GetCore().GetShowMode() != FRAME_SHOW_MODE_FULL_SCREEN)
149         {
150                 SysTryReturn(NID_UI_CTRL, !(pNewCurForm->GetFormStyle() & FORM_STYLE_INDICATOR), E_INVALID_ARG, E_INVALID_ARG, "Only Frame whose show mode is @c FRAME_SHOW_MODE_FULL_SCREEN can set a Form which has the style of @c FORM_STYLE_INDICATOR as the current form.");
151         }
152
153
154         r = MoveChildToTop(*pNewCurForm);
155         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
156
157         // [ToDo] Change the method to return result. And the arg should be reference.
158         GetCore().SetCurrentForm(&pNewCurForm->GetCore());
159         r = GetLastResult();
160         SysAssert(r == E_SUCCESS); // [ToDo] Exception check.
161
162         pNewCurForm->UpdateOrientationStatus();
163
164         // [ToDo] Check last result
165         SysAssert(GetLastResult() == E_SUCCESS);
166         return E_SUCCESS;
167 }
168
169 // [ToDo] This function should return _FormImpl*.
170 _FormImpl*
171 _FrameImpl::GetCurrentForm(void) const
172 {
173         _Control* pFormCore = GetCore().GetCurrentForm();
174         SysTryReturn(NID_UI_CTRL, pFormCore, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
175
176         _FormImpl* pFormImpl = static_cast<_FormImpl*>(pFormCore->GetUserData());
177
178         return pFormImpl;
179 }
180
181 _FrameImpl*
182 _FrameImpl::CreateFrameImplN(Frame* pPublic)
183 {
184         ClearLastResult();
185         result r = E_SUCCESS;
186
187         _Frame* pCore = _Frame::CreateFrameN();
188         SysTryReturn(NID_UI_CTRL, pCore, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
189
190         _FrameImpl* pImpl = new (std::nothrow) _FrameImpl(pPublic, pCore);
191         r = _ControlImpl::CheckConstruction(pCore, pImpl);
192         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
193
194         pCore->SetSize(_ControlManager::GetInstance()->GetScreenSize());
195
196         pCore->AddFrameEventListener(*pImpl);
197
198         SetLastResult(E_SUCCESS);
199
200         return pImpl;
201 }
202
203 void
204 _FrameImpl::AddFrameEventListener(IFrameEventListener& listener)
205 {
206         result r = __pFrameEvent->AddListener(listener);
207         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
208 }
209
210 void
211 _FrameImpl::RemoveFrameEventListener(IFrameEventListener& listener)
212 {
213         result r = __pFrameEvent->RemoveListener(listener);
214         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
215 }
216
217 Animations::FrameAnimator*
218 _FrameImpl::GetFrameAnimator(void) const
219 {
220         if (__pFrameAnimator == null)
221         {
222                 FrameAnimator* pFrameAnimator = new (std::nothrow) FrameAnimator();
223                 SysTryReturn(NID_UI_CTRL,
224                         pFrameAnimator != null, null,
225                         E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
226
227                 result r = pFrameAnimator->Construct(GetPublic());
228                 if (IsFailed(r))
229                 {
230                         SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] FrameAnimator::Construct failed");
231                         delete pFrameAnimator;
232                         return null;
233                 }
234
235                 const_cast<_FrameImpl*>(this)->__pFrameAnimator = pFrameAnimator;
236         }
237
238         SetLastResult(E_SUCCESS);
239
240         return __pFrameAnimator;
241 }
242
243 void
244 _FrameImpl::OnDraw(void)
245 {
246         __pOrientationAgent->FireOrientationEvent();
247
248         _WindowImpl::OnDraw();
249 }
250
251 void
252 _FrameImpl::OnChangeLayout(_ControlOrientation orientation)
253 {
254         // Change layout.
255         _ContainerImpl::OnChangeLayout(orientation);
256         SysTryReturnVoidResult(NID_UI_CTRL, GetLastResult() == E_SUCCESS, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
257
258         bool resizable = IsResizable();
259         SetResizable(true);
260
261         _ControlManager* pCoreManager = _ControlManager::GetInstance();
262         SysAssert(pCoreManager);
263         const Dimension& screenSize = pCoreManager->GetScreenSize();
264
265 #if !defined(MULTI_WINDOW)
266         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
267         {
268                 SetSize(screenSize);
269         }
270         else
271         {
272                 SetSize(Dimension(screenSize.height, screenSize.width));
273         }
274 #else
275         if (GetCore().GetShowMode() == FRAME_SHOW_MODE_FULL_SCREEN)
276         {
277                 if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
278                 {
279                         SetSize(screenSize);
280                 }
281                 else
282                 {
283                         SetSize(Dimension(screenSize.height, screenSize.width));
284                 }
285         }
286 #endif
287
288         SetResizable(resizable);
289 }
290
291 void
292 _FrameImpl::OnChildDetaching(const _Control& child)
293 {
294         result r = E_SUCCESS;
295
296         _FormImpl* pFormImpl = static_cast<_FormImpl*>(child.GetUserData());
297
298         _FormImpl* pCurrentForm = GetCurrentForm();
299
300         if (pCurrentForm != null)
301         {
302                 if (pCurrentForm == pFormImpl)
303                 {
304                         // if the given control will be the current form, hide the current form
305                         int count = GetChildCount();
306                         if (count > 1)
307                         {
308                                 _ControlImpl* pControlImpl = GetChild(count - 2);
309                                 if (!pControlImpl)
310                                 {
311                                         return ;
312                                 }
313
314                                 Form* pPrevForm = static_cast<Form*>(&pControlImpl->GetPublic());
315
316                                 if (pPrevForm != null)
317                                 {
318                                         r = pPrevForm->SetShowState(true);
319                                         if (IsFailed(r))
320                                         {
321                                                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
322                                                 return ;
323                                         }
324                                 }
325                         }
326                 }
327         }
328 }
329
330 void
331 _FrameImpl::OnChildDetached(const _Control& child)
332 {
333         _ControlImpl::OnChildDetached(child);
334
335         int childCount = GetCore().GetChildCount();
336         if (childCount == 0)
337         {
338                 UpdateOrientationStatus();
339         }
340 }
341
342 void
343 _FrameImpl::OnFrameActivated(const _Frame& source)
344 {
345         if (__pFrameEvent)
346         {
347                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_ACTIVATED);
348                 if (pEventArg)
349                 {
350                         __pFrameEvent->Fire(*pEventArg);
351                 }
352         }
353 }
354
355 void
356 _FrameImpl::OnFrameDeactivated(const _Frame& source)
357 {
358         if (__pFrameEvent)
359         {
360                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_DEACTIVATED);
361                 if (pEventArg)
362                 {
363                         __pFrameEvent->Fire(*pEventArg);
364                 }
365         }
366 }
367
368 void
369 _FrameImpl::OnBoundsChanged(void)
370 {
371         int childcount = GetChildCount();
372         Rectangle bounds = GetBounds();
373
374         for (int i = 0 ; i < childcount ; i++)
375         {
376                 _ControlImpl* pChild = GetChild(i);
377                 _FormImpl* pForm = dynamic_cast<_FormImpl*>(pChild);
378                 if (pForm)
379                 {
380                         if (GetShowMode() == FRAME_SHOW_MODE_FULL_SCREEN)
381                         {
382                                 if (pForm->GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT || pForm->GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT_REVERSE)
383                                 {
384                                         pForm->SetBounds(Rectangle(0, 0, GetBounds().width, GetBounds().height));
385                                 }
386                                 else
387                                 {
388                                         pForm->SetBounds(Rectangle(0, 0, GetBounds().height, GetBounds().width));
389                                 }
390                         }
391                         else
392                         {
393                                 pForm->SetBounds(Rectangle(0, 0, GetBounds().width, GetBounds().height));
394                         }
395
396                 }
397         }
398
399         _ContainerImpl::OnBoundsChanged();
400
401 }
402
403
404 void
405 _FrameImpl::AddOrientationEventListener(Tizen::Ui::IOrientationEventListener& listener)
406 {
407         __pOrientationAgent->AddListener(listener);
408 }
409
410 void
411 _FrameImpl::RemoveOrientationEventListener(Tizen::Ui::IOrientationEventListener& listener)
412 {
413         __pOrientationAgent->RemoveListener(listener);
414 }
415
416 void
417 _FrameImpl::SetOrientation(Orientation orientation)
418 {
419         __pOrientationAgent->SetMode(orientation);
420 }
421
422 Orientation
423 _FrameImpl::GetOrientation(void) const
424 {
425         return __pOrientationAgent->GetMode();
426 }
427
428 OrientationStatus
429 _FrameImpl::GetOrientationStatus(void) const
430 {
431         return __pOrientationAgent->GetStatus();
432 }
433
434 bool
435 _FrameImpl::IsChildAttachable(_ControlImpl& child) const
436 {
437         return (dynamic_cast<_FormImpl*>(&child) != null);
438 }
439
440 void
441 _FrameImpl::UpdateOrientationStatus(void)
442 {
443         __pOrientationAgent->Update();
444 }
445
446 bool
447 _FrameImpl::IsOpaque(void) const
448 {
449         return true;
450 }
451
452 void
453 _FrameImpl::SetFloatingBounds(const Rectangle& rect)
454 {
455         GetCore().SetFloatingBounds(rect);
456 }
457
458 result
459 _FrameImpl::SetShowMode(FrameShowMode showMode)
460 {
461         return GetCore().SetShowMode(showMode);
462 }
463
464 FrameShowMode
465 _FrameImpl::GetShowMode(void) const
466 {
467         return GetCore().GetShowMode();
468 }
469
470 }}} // Tizen::Ui::Controls