Revert "Adjust the position of the partial Frame"
[platform/framework/native/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 "FUi_CoordinateSystemUtils.h"
26 #include "FUiCtrlForm.h"
27 #include "FUiCtrl_FrameImpl.h"
28 #include "FUiCtrl_FormImpl.h"
29 #include "FUiCtrl_Frame.h"
30 #include "FUiCtrl_PublicFrameEvent.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Base::Runtime;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Animations;
38
39 namespace Tizen { namespace Ui { namespace Controls {
40
41 const String _REQUEST_ORIENTATION_EVENT = L"RequestOrientationEvent";
42
43 const _FrameImpl*
44 _FrameImpl::GetInstance(const Frame& frame)
45 {
46         return static_cast<const _FrameImpl*> (frame._pControlImpl);
47 }
48
49 _FrameImpl*
50 _FrameImpl::GetInstance(Frame& frame)
51 {
52         return static_cast<_FrameImpl*> (frame._pControlImpl);
53 }
54
55 void
56 _FrameImpl::Dispose(void)
57 {
58         GetCore().RemoveFrameEventListener(*this);
59
60         if (__pFrameEvent)
61         {
62                 delete __pFrameEvent;
63                 __pFrameEvent = null;
64         }
65
66         delete __pOrientationAgent;
67         __pOrientationAgent = null;
68
69         delete __pFrameAnimator;
70         __pFrameAnimator = null;
71 }
72
73 _FrameImpl::_FrameImpl(Frame* pPublic, _Frame* pCore)
74         : _WindowImpl(pPublic, pCore)
75         , __pFrameEvent(null)
76         , __pFrameAnimator(null)
77         , __pOrientationAgent(null)
78 {
79         __pFrameEvent = _PublicFrameEvent::CreateInstanceN(*pPublic);
80         SysTryReturnVoidResult(NID_UI_CTRL, __pFrameEvent, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
81
82         __pOrientationAgent = _OrientationAgent::CreateInstanceN(*pPublic);
83         SysTryCatch(NID_UI_CTRL, __pOrientationAgent != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
84
85         return;
86
87 CATCH:
88         Dispose();
89 }
90
91 _FrameImpl::~_FrameImpl(void)
92 {
93         // [ToDo] Wrong point to call OnFrameTerminating().
94         if (__pFrameEvent)
95         {
96                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_TERMINATING);
97                 if (pEventArg)
98                 {
99                         __pFrameEvent->Fire(*pEventArg);
100                 }
101         }
102
103         Dispose();
104         ClearLastResult();
105 }
106
107 const char*
108 _FrameImpl::GetPublicClassName(void) const
109 {
110         return "Tizen::Ui::Controls::Frame";
111 }
112
113 const Frame&
114 _FrameImpl::GetPublic(void) const
115 {
116         return static_cast<const Frame&>(_ControlImpl::GetPublic());
117 }
118
119 Frame&
120 _FrameImpl::GetPublic(void)
121 {
122         return static_cast<Frame&>(_ControlImpl::GetPublic());
123 }
124
125 const _Frame&
126 _FrameImpl::GetCore(void) const
127 {
128         return static_cast<const _Frame&>(_ControlImpl::GetCore());
129 }
130
131 _Frame&
132 _FrameImpl::GetCore(void)
133 {
134         return static_cast<_Frame&>(_ControlImpl::GetCore());
135 }
136
137 result
138 _FrameImpl::SetCurrentForm(const _FormImpl& formImpl)
139 {
140         result r = E_SUCCESS;
141
142         _FormImpl* pNewCurForm = const_cast<_FormImpl*>(&formImpl);
143
144         SysTryReturn(NID_UI_CTRL,
145                 pNewCurForm != null, E_INVALID_ARG,
146                 E_INVALID_ARG, "[E_INVALID_ARG] The Form is not initialized.");
147
148         SysTryReturn(NID_UI_CTRL,
149                 IsAncestorOf(pNewCurForm), E_INVALID_ARG,
150                 E_INVALID_ARG, "[E_INVALID_ARG] The Form is not the child of this Frame.");
151
152         if (GetCore().GetShowMode() != FRAME_SHOW_MODE_FULL_SCREEN)
153         {
154                 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.");
155         }
156
157
158         r = MoveChildToTop(*pNewCurForm);
159         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
160
161         // [ToDo] Change the method to return result. And the arg should be reference.
162         GetCore().SetCurrentForm(&pNewCurForm->GetCore());
163         r = GetLastResult();
164         SysAssert(r == E_SUCCESS); // [ToDo] Exception check.
165
166         pNewCurForm->UpdateOrientationStatus();
167
168         // [ToDo] Check last result
169         SysAssert(GetLastResult() == E_SUCCESS);
170         return E_SUCCESS;
171 }
172
173 result
174 _FrameImpl::SetCurrentForm(_FormImpl* pFormImpl)
175 {
176         result r = E_SUCCESS;
177
178         _FormImpl* pNewCurForm = pFormImpl;
179
180         SysTryReturn(NID_UI_CTRL,
181                 pNewCurForm != null, E_INVALID_ARG,
182                 E_INVALID_ARG, "[E_INVALID_ARG] The Form is not initialized.");
183
184         SysTryReturn(NID_UI_CTRL,
185                 IsAncestorOf(pNewCurForm), E_INVALID_ARG,
186                 E_INVALID_ARG, "[E_INVALID_ARG] The Form is not the child of this Frame.");
187
188         if (GetCore().GetShowMode() != FRAME_SHOW_MODE_FULL_SCREEN)
189         {
190                 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.");
191         }
192
193
194         r = MoveChildToTop(*pNewCurForm);
195         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
196
197         // [ToDo] Change the method to return result. And the arg should be reference.
198         GetCore().SetCurrentForm(&pNewCurForm->GetCore());
199         r = GetLastResult();
200         SysAssert(r == E_SUCCESS); // [ToDo] Exception check.
201
202         pNewCurForm->UpdateOrientationStatus();
203
204         // [ToDo] Check last result
205         SysAssert(GetLastResult() == E_SUCCESS);
206         return E_SUCCESS;
207 }
208
209 // [ToDo] This function should return _FormImpl*.
210 _FormImpl*
211 _FrameImpl::GetCurrentForm(void) const
212 {
213         _Control* pFormCore = GetCore().GetCurrentForm();
214         SysTryReturn(NID_UI_CTRL, pFormCore, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
215
216         _FormImpl* pFormImpl = static_cast<_FormImpl*>(pFormCore->GetUserData());
217
218         return pFormImpl;
219 }
220
221 _FrameImpl*
222 _FrameImpl::CreateFrameImplN(Frame* pPublic)
223 {
224         ClearLastResult();
225         result r = E_SUCCESS;
226
227         _Frame* pCore = _Frame::CreateFrameN();
228         SysTryReturn(NID_UI_CTRL, pCore, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
229
230         _FrameImpl* pImpl = new (std::nothrow) _FrameImpl(pPublic, pCore);
231         r = _ControlImpl::CheckConstruction(pCore, pImpl);
232         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
233
234         pCore->SetSize(_ControlManager::GetInstance()->GetScreenSizeF());
235
236         pCore->AddFrameEventListener(*pImpl);
237
238         SetLastResult(E_SUCCESS);
239
240         return pImpl;
241 }
242
243 void
244 _FrameImpl::AddFrameEventListener(IFrameEventListener& listener)
245 {
246         result r = __pFrameEvent->AddListener(listener);
247         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
248 }
249
250 void
251 _FrameImpl::RemoveFrameEventListener(IFrameEventListener& listener)
252 {
253         result r = __pFrameEvent->RemoveListener(listener);
254         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
255 }
256
257 Animations::FrameAnimator*
258 _FrameImpl::GetFrameAnimator(void) const
259 {
260         if (__pFrameAnimator == null)
261         {
262                 FrameAnimator* pFrameAnimator = new (std::nothrow) FrameAnimator();
263                 SysTryReturn(NID_UI_CTRL,
264                         pFrameAnimator != null, null,
265                         E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
266
267                 result r = pFrameAnimator->Construct(GetPublic());
268                 if (IsFailed(r))
269                 {
270                         SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] FrameAnimator::Construct failed");
271                         delete pFrameAnimator;
272                         return null;
273                 }
274
275                 const_cast<_FrameImpl*>(this)->__pFrameAnimator = pFrameAnimator;
276         }
277
278         SetLastResult(E_SUCCESS);
279
280         return __pFrameAnimator;
281 }
282
283 void
284 _FrameImpl::OnDraw(void)
285 {
286         __pOrientationAgent->FireOrientationEvent();
287
288         _WindowImpl::OnDraw();
289 }
290
291 void
292 _FrameImpl::OnChangeLayout(_ControlOrientation orientation)
293 {
294         // Change layout.
295         _ContainerImpl::OnChangeLayout(orientation);
296         SysTryReturnVoidResult(NID_UI_CTRL, GetLastResult() == E_SUCCESS, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
297
298         bool resizable = IsResizable();
299         SetResizable(true);
300
301         _ControlManager* pCoreManager = _ControlManager::GetInstance();
302         SysAssert(pCoreManager);
303         const FloatDimension& screenSize = pCoreManager->GetScreenSizeF();
304
305         if (GetCore().GetShowMode() == FRAME_SHOW_MODE_FULL_SCREEN)
306         {
307                 if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
308                 {
309                         SetSize(screenSize);
310                 }
311                 else
312                 {
313                         SetSize(FloatDimension(screenSize.height, screenSize.width));
314                 }
315         }
316
317         SetResizable(resizable);
318 }
319
320 void
321 _FrameImpl::OnChildDetaching(const _Control& child)
322 {
323         result r = E_SUCCESS;
324
325         _FormImpl* pFormImpl = static_cast<_FormImpl*>(child.GetUserData());
326
327         _FormImpl* pCurrentForm = GetCurrentForm();
328
329         if (pCurrentForm != null)
330         {
331                 if (pCurrentForm == pFormImpl)
332                 {
333                         // if the given control will be the current form, hide the current form
334                         int count = GetChildCount();
335                         if (count > 1)
336                         {
337                                 _ControlImpl* pControlImpl = GetChild(count - 2);
338                                 if (!pControlImpl)
339                                 {
340                                         return ;
341                                 }
342
343                                 Form* pPrevForm = static_cast<Form*>(&pControlImpl->GetPublic());
344
345                                 if (pPrevForm != null)
346                                 {
347                                         r = pPrevForm->SetShowState(true);
348                                         if (IsFailed(r))
349                                         {
350                                                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
351                                                 return ;
352                                         }
353                                 }
354                         }
355                 }
356         }
357 }
358
359 void
360 _FrameImpl::OnChildDetached(const _Control& child)
361 {
362         _ControlImpl::OnChildDetached(child);
363
364         int childCount = GetCore().GetChildCount();
365         if (childCount == 0)
366         {
367                 UpdateOrientationStatus();
368         }
369         else if (childCount > 0)
370         {
371                 _FormImpl* pCurrentForm = dynamic_cast<_FormImpl*>(GetChild(childCount - 1));
372                 SysTryReturnVoidResult(NID_UI_CTRL, pCurrentForm, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
373                 pCurrentForm->UpdateOrientationStatus(true);
374         }
375 }
376
377 void
378 _FrameImpl::OnFrameActivated(const _Frame& source)
379 {
380         if (__pFrameEvent)
381         {
382                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_ACTIVATED);
383                 if (pEventArg)
384                 {
385                         __pFrameEvent->Fire(*pEventArg);
386                 }
387         }
388 }
389
390 void
391 _FrameImpl::OnFrameDeactivated(const _Frame& source)
392 {
393         if (__pFrameEvent)
394         {
395                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_DEACTIVATED);
396                 if (pEventArg)
397                 {
398                         __pFrameEvent->Fire(*pEventArg);
399                 }
400         }
401 }
402
403 void
404 _FrameImpl::OnFrameMinimized(const _Frame& source)
405 {
406         if (__pFrameEvent)
407         {
408                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_MINIMIZED);
409                 if (pEventArg)
410                 {
411                         __pFrameEvent->Fire(*pEventArg);
412                 }
413         }
414 }
415
416 void
417 _FrameImpl::OnFrameRestored(const _Frame& source)
418 {
419         if (__pFrameEvent)
420         {
421                 IEventArg* pEventArg = _PublicFrameEvent::CreateFrameEventArgN(_FrameImpl::GetPublic(), FRAME_STATE_RESTORED);
422                 if (pEventArg)
423                 {
424                         __pFrameEvent->Fire(*pEventArg);
425                 }
426         }
427 }
428
429 void
430 _FrameImpl::OnBoundsChanged(void)
431 {
432         int childcount = GetChildCount();
433         FloatRectangle bounds = GetBoundsF();
434
435         for (int i = 0 ; i < childcount ; i++)
436         {
437                 _ControlImpl* pChild = GetChild(i);
438                 _FormImpl* pForm = dynamic_cast<_FormImpl*>(pChild);
439                 if (pForm)
440                 {
441                         if (GetShowMode() == FRAME_SHOW_MODE_FULL_SCREEN)
442                         {
443                                 if (pForm->GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT || pForm->GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT_REVERSE)
444                                 {
445                                         pForm->SetBounds(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height));
446                                 }
447                                 else
448                                 {
449                                         pForm->SetBounds(FloatRectangle(0.0f, 0.0f, bounds.height, bounds.width));
450                                 }
451                         }
452                         else
453                         {
454                                 pForm->SetBounds(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height));
455                         }
456
457                 }
458         }
459
460         _ContainerImpl::OnBoundsChanged();
461
462 }
463
464 result
465 _FrameImpl::OnAttachedToMainTree(void)
466 {
467         result r = _WindowImpl::OnAttachedToMainTree();
468         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
469
470         __pOrientationAgent->RequestOrientationEvent();
471
472         return r;
473 }
474
475 bool
476 _FrameImpl::OnNotifiedN(const _ControlImpl& source, IList* pArgs)
477 {
478         String* pString = dynamic_cast <Tizen::Base::String*>(pArgs->GetAt(0));
479         if (pString)
480         {
481                 if (*pString == _REQUEST_ORIENTATION_EVENT)
482                 {
483                         __pOrientationAgent->FireOrientationEvent();
484
485                         pArgs->RemoveAll(true);
486                         delete pArgs;
487
488                         return true;
489                 }
490         }
491
492         return false;
493 }
494
495 void
496 _FrameImpl::AddOrientationEventListener(Tizen::Ui::IOrientationEventListener& listener)
497 {
498         __pOrientationAgent->AddListener(listener);
499 }
500
501 void
502 _FrameImpl::RemoveOrientationEventListener(Tizen::Ui::IOrientationEventListener& listener)
503 {
504         __pOrientationAgent->RemoveListener(listener);
505 }
506
507 void
508 _FrameImpl::SetOrientation(Orientation orientation)
509 {
510         __pOrientationAgent->SetMode(orientation);
511 }
512
513 Orientation
514 _FrameImpl::GetOrientation(void) const
515 {
516         return __pOrientationAgent->GetMode();
517 }
518
519 OrientationStatus
520 _FrameImpl::GetOrientationStatus(void) const
521 {
522         return __pOrientationAgent->GetStatus();
523 }
524
525 bool
526 _FrameImpl::IsChildAttachable(_ControlImpl& child) const
527 {
528         return (dynamic_cast<_FormImpl*>(&child) != null);
529 }
530
531 void
532 _FrameImpl::UpdateOrientationStatus(void)
533 {
534         __pOrientationAgent->Update();
535 }
536
537 #if defined(WINDOW_BASE_ROTATE)
538 void
539 _FrameImpl::UpdateOrientation(void)
540 {
541         __pOrientationAgent->UpdateOrientation();
542 }
543 #endif
544
545 bool
546 _FrameImpl::IsOpaque(void) const
547 {
548         //return true;
549         return false;
550 }
551
552 void
553 _FrameImpl::SetFloatingBounds(const FloatRectangle& rect)
554 {
555         GetCore().SetFloatingBounds(rect);
556 }
557
558 void
559 _FrameImpl::SetFloatingBounds(const Rectangle& rect)
560 {
561         GetCore().SetFloatingBounds(rect);
562 }
563
564 result
565 _FrameImpl::SetShowMode(FrameShowMode showMode)
566 {
567         return GetCore().SetShowMode(showMode);
568 }
569
570 FrameShowMode
571 _FrameImpl::GetShowMode(void) const
572 {
573         return GetCore().GetShowMode();
574 }
575
576 }}} // Tizen::Ui::Controls