Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_ControlManager.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_ControlManager.cpp
19  * @brief               This is the implementation file for the _ControlManager class.
20  */
21
22 #include <new>
23 #include <app.h>
24 #include <runtime_info.h>
25 #if defined(MULTI_WINDOW)
26 #include <Ecore_X.h>
27 #include <Elementary.h>
28 #endif
29 #include <FBaseInt8.h>
30 #include <FBaseSysLog.h>
31 #if defined(MULTI_WINDOW)
32 #include <FGrpRectangle.h>
33 #endif
34 #include <FApp_AppInfo.h>
35 #include <FAppPkg_PackageInfoImpl.h>
36 #include <FGrp_Screen.h>
37 #include <FGrp_FontImpl.h>
38 #include <FSys_SettingInfoImpl.h>
39 #include "FUi_ControlManager.h"
40 #include "FUi_Control.h"
41 #include "FUi_Window.h"
42 #include "FUi_WindowImpl.h"
43 #include "FUi_EcoreEvas.h"
44 #include "FUi_EcoreEvasMgr.h"
45 #include "FUi_EflWindow.h"
46 #include "FUi_ResourceManager.h"
47 #include "FUi_Clipboard.h"
48 #include "FUi_UiFocusEvent.h"
49 #include "FUi_UiEventManager.h"
50 #include "FUi_TouchManager.h"
51 #include "FUi_TouchLongPressGestureDetector.h"
52 #include "FUi_TouchTapGestureDetector.h"
53 #include "FUi_KeyEventManager.h"
54 #include "FUi_CoordinateSystemUtils.h"
55 #include "FUiAnim_RootVisualElement.h"
56 #include "FUiAnim_AnimationManager.h"
57 #include "FUiAnim_DisplayManager.h"
58 #include "FUi_AccessibilityManager.h"
59 #if defined(MULTI_WINDOW)
60 #include "FUiAnim_VisualElement.h"
61 #include "FUiAnim_EflLayer.h"
62 #include "FUiCtrl_FrameImpl.h"
63 #include "FUiCtrl_FormImpl.h"
64 #endif
65 #include "FUiCtrl_Frame.h"
66 // #include "FUiCtrl_NotificationFrame.h"
67 // #include "FUiCtrl_NotificationFrameImpl.h"
68 #include "FUiCtrl_IndicatorManager.h"
69
70 using namespace Tizen::App;
71 using namespace Tizen::App::Package;
72 using namespace Tizen::Base;
73 using namespace Tizen::Base::Collection;
74 using namespace Tizen::Graphics;
75 using namespace Tizen::Ui;
76 using namespace Tizen::Ui::Animations;
77 using namespace Tizen::Ui::Controls;
78 using namespace Tizen::System;
79
80 namespace {
81
82 _ControlRotation Convert(int rotationDegree)
83 {
84         switch (rotationDegree)
85         {
86                 case 0:
87                         return _CONTROL_ROTATION_0;
88                 case 90:
89                         return _CONTROL_ROTATION_90;
90                 case 180:
91                         return _CONTROL_ROTATION_180;
92                 case 270:
93                         return _CONTROL_ROTATION_270;
94                 default:
95                         return _CONTROL_ROTATION_0;
96         }
97 }
98
99 int Convert(_ControlRotation rotation)
100 {
101         switch (rotation)
102         {
103                 case _CONTROL_ROTATION_0:
104                         return 0;
105                 case _CONTROL_ROTATION_90:
106                         return 90;
107                 case _CONTROL_ROTATION_180:
108                         return 180;
109                 case _CONTROL_ROTATION_270:
110                         return 270;
111         }
112 }
113
114 } // Anonymous
115
116 namespace Tizen { namespace Ui
117 {
118
119 class _UiDebug
120 {
121 public:
122         static void PrintControl(const _Control& control, bool printChildren = true);
123         static void PrintTree(void);
124 };
125
126 void
127 _UiDebug::PrintControl(const _Control& control, bool printChildren)
128 {
129         const_cast<_Control&>(control).PrintDescription(printChildren);
130 }
131
132 void
133 _UiDebug::PrintTree(void)
134 {
135         _ControlManager* pControlManager = _ControlManager::GetInstance();
136         SysTryReturnVoidResult(NID_UI, pControlManager, E_SYSTEM, "[E_SYSTEM] System error occurred.");
137
138 #if !defined(MULTI_WINDOW)
139         PrintControl(pControlManager->GetRoot(), true);
140 #else
141         if (pControlManager->GetWindowCount() == 0)
142         {
143                 SysLog(NID_UI, "There is no window.");
144                 return;
145         }
146
147         int count = pControlManager->GetWindowCount();
148         for (int i = 0; i < count; i++)
149         {
150                 _Window* pWindow = pControlManager->GetWindow((count-1) - i);
151                 PrintControl(*pWindow, true);
152         }
153 #endif
154 }
155
156 _ControlManager* _ControlManager::__pInstance = null;
157
158 void
159 _ControlManager::Initialize(void)
160 {
161         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
162
163         if (!__pInstance)
164         {
165                 pthread_once(&once_block, InitInstance);
166         }
167
168         result r = _SettingInfoImpl::AddSettingEventListenerForInternal(*__pInstance);
169         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
170
171         // [ToDo] Is it OK to directly get the device orientation?
172         int degree = app_get_device_orientation();
173         SysLog(NID_UI, "The initial value of device orientation is %d.", degree);
174
175         __pInstance->__screenRotation = ::Convert(degree);
176 }
177
178 void
179 _ControlManager::Release(void)
180 {
181         result  r = _SettingInfoImpl::RemoveSettingEventListenerForInternal(*__pInstance);
182         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
183
184         delete __pInstance;
185         __pInstance = null;
186 }
187
188 _ControlManager*
189 _ControlManager::GetInstance(void)
190 {
191         return __pInstance;
192 }
193
194 void
195 _ControlManager::InitInstance(void)
196 {
197         if (__pInstance)
198         {
199                 return;
200         }
201
202         __pInstance = new (std::nothrow) _ControlManager;
203         SysAssert(__pInstance);
204 }
205
206 _ControlHandle
207 _ControlManager::Register(_Control* pObject)
208 {
209         if (pObject)
210         {
211                 SysLog(NID_UI, "A _Control Registered()");
212         }
213
214         if (pObject == null)
215         {
216                 return _ControlHandle();
217         }
218
219         return __objectManager.Register(*pObject);
220 }
221
222 _Control*
223 _ControlManager::Release(const _ControlHandle& handle)
224 {
225         _Control* pObject = GetObject(handle);
226         if (pObject)
227         {
228                 SysLog(NID_UI, "A _Control Released()");
229         }
230
231         return __objectManager.Unregister(handle);
232 }
233
234 _Control*
235 _ControlManager::GetObject(const _ControlHandle& handle)
236 {
237         return __objectManager.GetObject(handle);
238 }
239
240 const _Control*
241 _ControlManager::GetObject(const _ControlHandle& handle) const
242 {
243         return __objectManager.GetObject(handle);
244 }
245
246 int
247 _ControlManager::GetUsedHandleCount(void) const
248 {
249         return __objectManager.GetObjectCount();
250 }
251
252 result
253 _ControlManager::GetAppCoordinateSystem(bool& isCoordinateSystemLogical, int& logicalCoordinateSystemInt, _BaseScreenSize& logicalBaseScreenSize)
254 {
255         _PackageInfoImpl infoImpl;
256         {
257                 String subAppId(_AppInfo::GetAppId());
258
259                 result r = infoImpl.Construct(subAppId);
260                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
261         }
262
263         String baseScreenSize(null);
264         String coordinateSystem(null);
265         String logicalCoordinate(null);
266
267         result r = infoImpl.GetUiScalabilityInfo(baseScreenSize, coordinateSystem, logicalCoordinate);
268         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
269
270         if (coordinateSystem.Equals(L"Physical", false))
271         {
272                 isCoordinateSystemLogical = false;
273                 logicalCoordinateSystemInt = 0;
274                 logicalBaseScreenSize = BASE_SCREEN_SIZE_DEFAULT;
275
276                 return E_SUCCESS;
277         }
278
279         //Logical Resolution
280         r = Integer::Parse(logicalCoordinate, logicalCoordinateSystemInt);
281         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
282
283         // BaseScreenSize
284         if (baseScreenSize.Equals(L"Large", false))
285         {
286                 logicalBaseScreenSize = BASE_SCREEN_SIZE_LARGE;
287         }
288         else // temp
289         {
290                 logicalBaseScreenSize = BASE_SCREEN_SIZE_NORMAL;
291         }
292
293         return r;
294 }
295
296 bool
297 _ControlManager::IsCoordinateSystemLogical(void) const
298 {
299         return __isCoordinateSystemLogical;
300 }
301
302 int
303 _ControlManager::GetCoordinateSystem(void) const
304 {
305         return __logicalCoordinateSystem;
306 }
307
308 _BaseScreenSize
309 _ControlManager::GetLogicalBaseScreenSize(void) const
310 {
311         return __logicalBaseScreenSize;
312 }
313
314 _ControlManager::_ControlManager(void) // [ToDo] exception check.
315 #if !defined(MULTI_WINDOW)
316         : __pRoot(null)
317 #else
318         : __pWindowList(null)
319 #endif
320         , __isCoordinateSystemLogical(true)
321         , __logicalCoordinateSystem(0)
322         , __logicalBaseScreenSize(BASE_SCREEN_SIZE_NONE)
323         , __pSystemWindowList(null)
324         , __pFocusedControl(null)
325         , __screenRotation(_CONTROL_ROTATION_0)
326         , __orientationStatus(_CONTROL_ROTATION_0)
327         , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
328 #if !defined(MULTI_WINDOW)
329         , __pWindowPi(null)
330 #endif
331         , __pCurrentFrame(null)
332         , __pGestureList(null)
333         , __gestureMaxDuration(0)
334 #if defined(MULTI_WINDOW)
335         , __touchedWindow(0)
336 #endif
337         , __isDefaultFontChanged(false)
338         , __defaultFontName(L"")
339 {
340         result r = GetAppCoordinateSystem(__isCoordinateSystemLogical, __logicalCoordinateSystem, __logicalBaseScreenSize);
341         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] System error occurred.");
342
343         Dimension deviceResolution = CoordinateSystem::GetPhysicalResolution();
344         _BaseScreenSize deviceBaseScreenSize = _CoordinateSystem::GetInstance()->GetPhysicalBaseScreenSize();
345
346         r = _CoordinateSystem::Initialize(__logicalCoordinateSystem, __logicalBaseScreenSize, deviceResolution, deviceBaseScreenSize);
347         SysAssert(r == E_SUCCESS);
348
349 #if !defined(MULTI_WINDOW)
350         _RootVisualElement* pVisualElementRoot = null;
351         _EflWindow* pWindowPi = null;
352         _Control* pRoot = null;
353         _ControlHandle rootHandle;
354
355         // Initialize _UiEventManager
356         _UiEventManager::Initialize();
357
358         // Initialize _TouchManager
359         _TouchManager::Initialize();
360
361         _KeyEventManager::Initialize();
362
363         _AccessibilityManager::CreateInstance();
364
365         r = _AnimationManager::CreateInstance();
366         SysAssertf(r == E_SUCCESS, "Failed to create animation manager!");
367
368         r = _DisplayManager::CreateInstance();
369         SysAssertf(r == E_SUCCESS, "Failed to create display manager!");
370
371         pWindowPi = _EflWindow::CreateInstanceN();
372         SysTryReturnVoidResult(NID_UI, pWindowPi, E_SYSTEM, "[E_SYSTEM] System error occurred.");
373
374         pRoot = new (std::nothrow) _Control;  // The children of pRoot are set not to be clipped to the parent.
375         SysTryCatch(NID_UI, pRoot, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
376
377         r = GetLastResult();
378         SysTryCatch(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
379
380         pVisualElementRoot = pWindowPi->GetRootVisualElement();
381         SysTryCatch(NID_UI, pVisualElementRoot, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
382         pVisualElementRoot->SetName(L"Root");
383
384         r = pVisualElementRoot->AttachChild(*pRoot->GetVisualElement());
385         SysTryCatch(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
386         pRoot->GetVisualElement()->SetClipChildrenEnabled(false);
387         pRoot->GetVisualElement()->SetName(L"RootControl");
388
389         __pSystemWindowList = new (std::nothrow)LinkedListT<_Window*>;
390         SysTryCatch(NID_UI, __pSystemWindowList, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
391
392         __pGestureList = new (std::nothrow)LinkedListT<_TouchGestureDetector*>;
393         SysTryCatch(NID_UI, __pGestureList, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
394
395         rootHandle = Register(pRoot); // [review] exception
396         SysAssert(rootHandle.IsValid());
397
398         __pWindowPi = pWindowPi;
399         __pRoot = pRoot;
400
401         pWindowPi->SetFrame(*pRoot);
402
403         return;
404
405 CATCH:
406         delete pWindowPi;
407         pWindowPi = null;
408
409         delete pRoot;
410         pRoot = null;
411
412         delete __pGestureList;
413         __pGestureList = null;
414
415         delete __pSystemWindowList;
416         __pSystemWindowList = null;
417
418 #else
419         r = _AnimationManager::CreateInstance();
420         SysAssertf(r == E_SUCCESS, "Failed to create animation manager!");
421
422         r = _DisplayManager::CreateInstance();
423         SysAssertf(r == E_SUCCESS, "Failed to create display manager!");
424
425         _EcoreEvas::CreateInstanceN();
426
427         __pWindowList = new (std::nothrow)LinkedListT<_Window*>;
428         SysTryCatch(NID_UI, __pWindowList, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
429
430         __pGestureList = new (std::nothrow)LinkedListT<_TouchGestureDetector*>;
431         SysTryCatch(NID_UI, __pGestureList, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
432
433         _UiEventManager::Initialize();
434         _TouchManager::Initialize();
435         _KeyEventManager::Initialize();
436         _IndicatorManager::InitializeInstance();
437         _AccessibilityManager::CreateInstance();
438
439         SetLastResult(E_SUCCESS);
440
441         return;
442
443 CATCH:
444         _AnimationManager::ReleaseInstance();
445
446         if (__pWindowList)
447         {
448                 delete __pWindowList;
449                 __pWindowList = null;
450         }
451
452         if (__pGestureList)
453         {
454                 delete __pGestureList;
455                 __pGestureList = null;
456         }
457 #endif
458 }
459
460 _ControlManager::~_ControlManager(void)
461 {
462 #if !defined(MULTI_WINDOW)
463         _EflWindow* pWindowPi = static_cast<_EflWindow*>(__pWindowPi);
464
465         // Release frame window
466         if(pWindowPi)
467         {
468                 _RootVisualElement* pRootVE = pWindowPi->GetRootVisualElement();
469                 _VisualElement* pControlRootVE = __pRoot->GetVisualElement();
470                 if (pRootVE && pControlRootVE)
471                 {
472                         pRootVE->DetachChild(*pControlRootVE);
473                 }
474         }
475
476         // Release clip board
477         _Clipboard::ReleaseInstance();
478
479         // Relese others
480         delete __pRoot;
481         delete __pWindowPi;
482         delete __pGestureList;
483         delete __pSystemWindowList;
484
485         _AnimationManager::ReleaseInstance();
486
487         // Release ecore evas manager
488         DestroyEcoreEvasMgr();
489
490         // Check control handle count
491         if (GetUsedHandleCount() != 0)
492         {
493                 SysLog(NID_UI, "[Control Manager] The number of unreleased controls: %d", GetUsedHandleCount());
494         }
495
496         _DisplayManager::ReleaseInstance();
497 #else
498         _Clipboard::ReleaseInstance();
499
500         if (__pWindowList)
501         {
502                 delete __pWindowList;
503                 __pWindowList = null;
504         }
505
506         if (__pGestureList)
507         {
508                 delete __pGestureList;
509                 __pGestureList = null;
510         }
511
512         DestroyEcoreEvasMgr();
513
514         if (GetUsedHandleCount() != 0)
515         {
516                 SysLog(NID_UI, "[Control Manager] The number of unreleased controls: %d", GetUsedHandleCount());
517         }
518
519         _DisplayManager::ReleaseInstance();
520
521         _AnimationManager::ReleaseInstance();
522 #endif
523         _AccessibilityManager::ReleaseInstance();
524
525         _IndicatorManager::ReleaseInstance();
526
527         _KeyEventManager::ReleaseInstance();
528         _TouchManager::ReleaseInstance();
529
530         _UiEventManager::Release();
531 }
532
533 #if !defined(MULTI_WINDOW)
534 const _Control&
535 _ControlManager::GetRoot(void) const
536 {
537         SysAssert(__pRoot);
538         return *__pRoot;
539 }
540
541 _Control&
542 _ControlManager::GetRoot(void)
543 {
544         SysAssert(__pRoot);
545         return *__pRoot;
546 }
547 #endif
548
549 _Window*
550 _ControlManager::GetTopWindow(void) const
551 {
552         ClearLastResult();
553
554         if (GetWindowCount() == 0)
555         {
556                 return null;
557         }
558
559         return GetWindow(GetWindowCount() - 1);
560 }
561
562 #if defined(MULTI_WINDOW)
563 _Window*
564 _ControlManager::GetTopVisibleWindow(void) const
565 {
566         ClearLastResult();
567
568         if (GetWindowCount() == 0)
569         {
570                 return null;
571         }
572
573         int count = GetWindowCount();
574         for (int i = 0; i < count; i++)
575         {
576                 _Window* pWindow = GetWindow((count-1) - i);
577
578                 if (pWindow->GetVisibleState() == true)
579                 {
580                         return pWindow;
581                 }
582         }
583
584         return null;
585 }
586
587 _Window*
588 _ControlManager::GetTopVisibleWindowAt(const Point& point) const
589 {
590         ClearLastResult();
591
592         if (GetWindowCount() == 0)
593         {
594                 return null;
595         }
596
597         int count = GetWindowCount();
598         for (int i = 0; i < count; i++)
599         {
600                 _Window* pWindow = GetWindow((count-1) - i);
601
602                 if (pWindow->GetVisibleState() == false)
603                 {
604                         continue;
605                 }
606
607                 Rectangle winBounds = pWindow->GetBounds();
608                 if (winBounds.Contains(point))
609                 {
610                         return pWindow;
611                 }
612         }
613
614         return null;
615 }
616 #endif
617
618 bool
619 _ControlManager::IsWindowOnTop(const _Window& window) const
620 {
621         return GetTopWindow() == &window;
622 }
623
624 bool
625 _ControlManager::IsWindowAttached(const _Window& window) const
626 {
627 #if !defined(MULTI_WINDOW)
628         if (window.IsSystemWindow())
629         {
630                 return __pSystemWindowList->Contains(const_cast<_Window*>(&window));
631         }
632
633         return window.GetParent() == __pRoot;
634 #else
635         return __pWindowList->Contains(const_cast<_Window*>(&window));
636 #endif
637 }
638
639 // Open a window and bring it to top.
640 result
641 _ControlManager::OpenWindow(_Window& window, bool invalidate)
642 {
643 #if !defined(MULTI_WINDOW)
644         // [Temp]
645         if (dynamic_cast <_Frame*>(&window) != null)
646         {
647                 __pWindowPi->SetFrame(window);
648         }
649 #endif
650
651         if (dynamic_cast <_Frame*>(&window) != null)
652         {
653                 __pCurrentFrame = &window;
654         }
655
656         result r = ActivateWindow(window);
657         SysTryReturn(NID_UI, r != E_INVALID_OPERATION, r, r, "[%s] Propagating.", GetErrorMessage(r));
658         SysTryReturn(NID_UI, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System Error.");
659
660         if (invalidate)
661         {
662                 window.Invalidate(true);
663         }
664
665         return E_SUCCESS;
666 }
667
668 result
669 _ControlManager::ActivateWindow(_Window& window)
670 {
671         ClearLastResult();
672         result r = E_SUCCESS;
673
674 #if !defined(MULTI_WINDOW)
675         // if (dynamic_cast<_NotificationFrame*>(&window))
676         if (window.IsSystemWindow())
677         {
678                 r = window.GetControlDelegate().OnAttaching(null);
679                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
680
681                 r = CallOnAttachingToMainTree(window);
682                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
683
684                 window.Activate();
685
686                 r = window.GetControlDelegate().OnAttached();
687                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
688
689                 r = CallOnAttachedToMainTree(window);
690                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
691         }
692         else
693         {
694                 if (IsWindowOnTop(window))
695                 {
696                         return E_SUCCESS;
697                 }
698
699                 if (window.IsActivatedOnOpen())
700                 {
701                         _Window* pTopWindow = GetTopWindow();
702                         if (pTopWindow)
703                         {
704                                 pTopWindow->Deactivate();
705                         }
706                 }
707
708                 if (IsWindowAttached(window))
709                 {
710                         r = MoveWindowToTop(window); // [ToDo] excpetion
711                         SysAssert(r == E_SUCCESS);
712
713                         return E_SUCCESS;
714                 }
715
716                 r = AttachWindow(window);
717                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
718
719                 if (window.IsActivatedOnOpen())
720                 {
721                         window.Activate();
722                 }
723                 else
724                 {
725                         window.SetWindowState(WINDOW_STATE_DEACTIVATED);
726                 }
727         }
728 #else
729         if (IsWindowOnTop(window))
730         {
731                 return E_SUCCESS;
732         }
733
734         if (window.IsActivationEnabled())
735         {
736                 _Window* pTopWindow = GetTopWindow();
737                 if (pTopWindow)
738                 {
739                         pTopWindow->Deactivate();
740                 }
741         }
742
743         if (IsWindowAttached(window))
744         {
745                 r = MoveWindowToTop(window); // [ToDo] excpetion
746                 SysAssert(r == E_SUCCESS);
747
748                 return E_SUCCESS;
749         }
750
751         r = window.GetControlDelegate().OnAttaching(null);
752         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
753
754         r = CallOnAttachingToMainTree(window);
755         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
756
757         r = AttachWindow(window);
758         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
759
760         if (window.IsActivationEnabled())
761         {
762                 window.Activate();
763         }
764
765         if (window.IsOrientationRoot() == false)
766         {
767                 window.ChangeLayout(GetOrientation());
768
769                 _EcoreEvas* pEcoreEvas = ::GetEcoreEvasMgr()->GetEcoreEvas();
770                 if (pEcoreEvas)
771                 {
772                         pEcoreEvas->RotateWindow(window, ::Convert(__orientationStatus));
773                 }
774         }
775
776         r = window.GetControlDelegate().OnAttached();
777         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
778
779         r = CallOnAttachedToMainTree(window);
780         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
781 #endif
782
783         return E_SUCCESS;
784 }
785
786 result
787 _ControlManager::CallOnAttachingToMainTree(_Control& control)
788 {
789         result r = E_SUCCESS;
790
791         r = control.GetControlDelegate().OnAttachingToMainTree(null);
792         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
793
794         _Control::ControlList& children = control.GetChildList();
795         _Control* pChild = null;
796
797         for (int index = 0; index < children.GetCount(); index++)
798         {
799                 r = children.GetAt(index, pChild);
800                 if (IsFailed(r))
801                 {
802                         SysAssert(r == E_OUT_OF_RANGE);
803                         SysTryReturn(NID_UI,
804                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
805                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
806                 }
807                 r = CallOnAttachingToMainTree(*pChild);
808                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
809         }
810
811         return r;
812 }
813
814 result
815 _ControlManager::CallOnAttachedToMainTree(_Control& control)
816 {
817         result r = E_SUCCESS;
818
819         _Control* pChild = null;
820         _Control::ControlList* pControlList = new (std::nothrow) _Control::ControlList;
821         pControlList->Construct(control.GetChildList());
822
823         r = control.GetControlDelegate().OnAttachedToMainTree();
824         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
825
826         for (int index = 0; index < pControlList->GetCount(); index++)
827         {
828                 r = pControlList->GetAt(index, pChild);
829                 if (IsFailed(r))
830                 {
831                         SysAssert(r == E_OUT_OF_RANGE);
832                         SysTryReturn(NID_UI,
833                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
834                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
835                 }
836                 r = control.CallOnAttachedToMainTree(*pChild);
837                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
838         }
839
840         delete pControlList;
841
842         return r;
843 }
844
845 result
846 _ControlManager::CallOnDetachingFromMainTree(_Control& control)
847 {
848         result r = E_SUCCESS;
849
850         _Control* pChild = null;
851         _Control::ControlList& children = control.GetChildList();
852
853         r = control.GetControlDelegate().OnDetachingFromMainTree();
854         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
855
856         for (int index = 0; index < children.GetCount(); index++)
857         {
858                 r = children.GetAt(index, pChild);
859                 if (IsFailed(r))
860                 {
861                         SysAssert(r == E_OUT_OF_RANGE);
862                         SysTryReturn(NID_UI,
863                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
864                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
865                 }
866                 r = CallOnDetachingFromMainTree(*pChild);
867                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
868         }
869
870         return r;
871 }
872
873 result
874 _ControlManager::CloseWindow(_Window& window) // [ToDo] exception check.
875 {
876         ClearLastResult();
877         result r = E_SUCCESS;
878
879 #if !defined(MULTI_WINDOW)
880         // if (dynamic_cast<_NotificationFrame*>(&window))
881         if (window.IsSystemWindow())
882         {
883                 r = CallOnDetachingFromMainTree(window);
884                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
885
886                 window.GetControlDelegate().OnDetaching();
887
888                 window.Deactivate();
889         }
890         else
891         {
892                 if (IsWindowAttached(window) == false)
893                 {
894                         return E_SUCCESS;
895                 }
896
897                 bool wasWindowOnTop = IsWindowOnTop(window);
898
899                 window.Deactivate();
900
901                 r = DetachWindow(window);
902                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
903
904                 if (wasWindowOnTop)
905                 {
906                         _Window* pNewTopWindow = GetTopWindow();
907                         if (pNewTopWindow && pNewTopWindow->IsActivatedOnOpen())
908                         {
909                                 pNewTopWindow->Activate();
910                         }
911                 }
912
913                 if (dynamic_cast <_Frame*>(&window) != null)
914                 {
915                         // __pWindowPi->SetFrame(null); // ?
916                         __pCurrentFrame = null;
917                 }
918         }
919 #else
920         if (IsWindowAttached(window) == false)
921         {
922                 return E_SUCCESS;
923         }
924
925         bool wasWindowOnTop = IsWindowOnTop(window);
926
927         r = CallOnDetachingFromMainTree(window);
928         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
929
930         window.GetControlDelegate().OnDetaching();
931
932         window.Deactivate();
933
934         r = DetachWindow(window);
935         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
936
937         if (wasWindowOnTop)
938         {
939                 _Window* pNewTopWindow = GetTopWindow();
940                 if (pNewTopWindow && pNewTopWindow->IsActivationEnabled())
941                 {
942                         pNewTopWindow->Activate();
943                 }
944
945                 if (dynamic_cast <_Frame*>(pNewTopWindow) != null)
946                 {
947                         __pCurrentFrame = pNewTopWindow;
948                 }
949         }
950 #endif
951
952         return E_SUCCESS;
953 }
954
955 _Window*
956 _ControlManager::GetWindow(int index) const
957 {
958 #if !defined(MULTI_WINDOW)
959         return dynamic_cast <_Window*>(GetRoot().GetChild(index));
960 #else
961         _Window* pWindow;
962         __pWindowList->GetAt(index, pWindow);
963
964         return pWindow;
965 #endif
966 }
967
968 int
969 _ControlManager::GetWindowCount(void) const
970 {
971 #if !defined(MULTI_WINDOW)
972         return GetRoot().GetChildCount();
973 #else
974         return __pWindowList->GetCount();
975 #endif
976 }
977
978 result
979 _ControlManager::AttachWindow(_Window& window)
980 {
981         _IndicatorManager::GetInstance()->AddWindow(&window);
982
983 #if !defined(MULTI_WINDOW)
984         return GetRoot().AttachChild(window);
985 #else
986         return __pWindowList->Add(&window);
987 #endif
988 }
989
990 result
991 _ControlManager::InsertWindowToBottom(_Window& window)
992 {
993 #if !defined(MULTI_WINDOW)
994         return GetRoot().InsertChildToBottom(window);
995 #else
996         return __pWindowList->InsertAt(&window, 0);
997 #endif
998 }
999
1000 result
1001 _ControlManager::InsertWindowAfter(const _Window& targetWindow, _Window& window)
1002 {
1003 #if !defined(MULTI_WINDOW)
1004         return GetRoot().InsertChildAfter(targetWindow, window);
1005 #else
1006         int index = 0;
1007
1008         result r = __pWindowList->IndexOf(const_cast<_Window*>(&targetWindow), index);
1009         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1010
1011         return __pWindowList->InsertAt(&window, index+1);
1012 #endif
1013 }
1014
1015 result
1016 _ControlManager::InsertWindowBefore(const _Window& targetWindow, _Window& window)
1017 {
1018 #if !defined(MULTI_WINDOW)
1019         return GetRoot().InsertChildBefore(targetWindow, window);
1020 #else
1021         int index = 0;
1022
1023         result r = __pWindowList->IndexOf(const_cast<_Window*>(&targetWindow), index);
1024         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1025
1026         return __pWindowList->InsertAt(&window, index);
1027 #endif
1028 }
1029
1030 result
1031 _ControlManager::DetachWindow(_Window& window)
1032 {
1033         _IndicatorManager::GetInstance()->DeleteWindow(&window);
1034
1035 #if !defined(MULTI_WINDOW)
1036         return GetRoot().DetachChild(window);
1037 #else
1038         return __pWindowList->Remove(&window);
1039 #endif
1040 }
1041
1042 void
1043 _ControlManager::DetachAllWindows(void)
1044 {
1045 #if !defined(MULTI_WINDOW)
1046         GetRoot().DetachAllChildren();
1047 #else
1048         __pWindowList->RemoveAll();
1049 #endif
1050 }
1051
1052 result
1053 _ControlManager::MoveWindowToTop(const _Window& window)
1054 {
1055 #if !defined(MULTI_WINDOW)
1056         return GetRoot().MoveChildToTop(window);
1057 #else
1058         result r = __pWindowList->Remove(const_cast<_Window*>(&window));
1059         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1060
1061         return __pWindowList->Add(const_cast<_Window*>(&window));
1062 #endif
1063 }
1064
1065 result
1066 _ControlManager::MoveWindowToBottom(const _Window& window)
1067 {
1068 #if !defined(MULTI_WINDOW)
1069         return GetRoot().MoveChildToBottom(window);
1070 #else
1071         result r = __pWindowList->Remove(const_cast<_Window*>(&window));
1072         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1073
1074         return __pWindowList->InsertAt(const_cast<_Window*>(&window), 0);
1075 #endif
1076 }
1077
1078 result
1079 _ControlManager::MoveWindowAfter(const _Window& targetWindow, const _Window& window)
1080 {
1081 #if !defined(MULTI_WINDOW)
1082         return GetRoot().MoveChildAfter(targetWindow, window);
1083 #else
1084         result r = __pWindowList->Remove(const_cast<_Window*>(&window));
1085         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1086
1087         int index = 0;
1088
1089         r = __pWindowList->IndexOf(const_cast<_Window*>(&targetWindow), index);
1090         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1091
1092         return __pWindowList->InsertAt(const_cast<_Window*>(&window), index+1);
1093 #endif
1094 }
1095
1096 result
1097 _ControlManager::MoveWindowBefore(const _Window& targetWindow, const _Window& window)
1098 {
1099 #if !defined(MULTI_WINDOW)
1100         return GetRoot().MoveChildBefore(targetWindow, window);
1101 #else
1102         result r = __pWindowList->Remove(const_cast<_Window*>(&window));
1103         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1104
1105         int index = 0;
1106
1107         r = __pWindowList->IndexOf(const_cast<_Window*>(&targetWindow), index);
1108         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1109
1110         return __pWindowList->InsertAt(const_cast<_Window*>(&window), index);
1111 #endif
1112 }
1113
1114 _ControlOrientation
1115 _ControlManager::GetOrientation(void) const
1116 {
1117         return __orientation;
1118 }
1119
1120 void
1121 _ControlManager::SetOrientation(_ControlOrientation orientation)
1122 {
1123         __orientation = orientation;
1124 }
1125
1126 _ControlRotation
1127 _ControlManager::GetScreenRotation(void) const
1128 {
1129         bool autoRotate = true;
1130         int ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, &autoRotate);
1131
1132         if (ret == RUNTIME_INFO_ERROR_NONE)
1133         {
1134                 SysLog(NID_UI, "The flag of auto-rotate is %d.", autoRotate);
1135         }
1136         else
1137         {
1138                 SysLog(NID_UI, "It's failed to get the flag of auto-rotate.");
1139         }
1140
1141         if (autoRotate == false)
1142         {
1143                 return _CONTROL_ROTATION_0;
1144         }
1145         else
1146         {
1147                 return __screenRotation;
1148         }
1149 }
1150
1151 void
1152 _ControlManager::OnScreenRotated(int rotation)
1153 {
1154         SysLog(NID_UI, "The angle of Emul or Target is %d.", rotation);
1155         __screenRotation = ::Convert(rotation);
1156
1157 #if defined(MULTI_WINDOW)
1158         int count = GetWindowCount();
1159
1160         for (int i = 0; i < count; i++)
1161         {
1162                 _Window* pWindow = GetWindow((count-1) - i);
1163
1164                 _Frame* pFrame = dynamic_cast<_Frame*>(pWindow);
1165
1166                 if (pFrame)
1167                 {
1168                         Controls::_FrameImpl* pFrameImpl = static_cast<Controls::_FrameImpl*>(pFrame->GetUserData());
1169                         if (pFrameImpl == null)
1170                         {
1171                                 continue;
1172                         }
1173
1174                         Controls::_FormImpl* pCurrentFormImpl = pFrameImpl->GetCurrentForm();
1175                         if (pCurrentFormImpl)
1176                         {
1177                                 pCurrentFormImpl->UpdateOrientationStatus(true);
1178                         }
1179                         else
1180                         {
1181                                 pFrameImpl->UpdateOrientationStatus();
1182                         }
1183                 }
1184         }
1185 #endif
1186 }
1187
1188 #if defined(MULTI_WINDOW)
1189 void
1190 _ControlManager::SetTouchedWindow(unsigned int window)
1191 {
1192         __touchedWindow = window;
1193 }
1194
1195 _Window*
1196 _ControlManager::GetTouchedWindow(void) const
1197 {
1198         ClearLastResult();
1199
1200         if (GetWindowCount() == 0)
1201         {
1202                 return null;
1203         }
1204
1205         int count = GetWindowCount();
1206         for (int i = 0; i < count; i++)
1207         {
1208                 _Window* pWindow = GetWindow((count-1) - i);
1209
1210                 _RootVisualElement* pRootVE = pWindow->GetRootVisualElement();
1211                 _EflLayer* pLayer = static_cast<_EflLayer*>(pRootVE->GetNativeLayer());
1212
1213                 Ecore_Evas* pEcoreEvas = pLayer->GetEcoreEvas();
1214                 Ecore_X_Window win = (Ecore_X_Window) ecore_evas_window_get(pEcoreEvas);
1215
1216                 if (win == __touchedWindow)
1217                 {
1218                         return pWindow;
1219                 }
1220         }
1221
1222         return null;
1223 }
1224 #endif
1225
1226 void
1227 _ControlManager::SetOrientationStatus(_ControlRotation orientationStatus)
1228 {
1229         __orientationStatus = orientationStatus;
1230 }
1231
1232 void
1233 _ControlManager::RotateScreen(_ControlRotation screenRotation)
1234 {
1235         _EcoreEvas* pEcoreEvas = ::GetEcoreEvasMgr()->GetEcoreEvas();
1236         SysAssert(pEcoreEvas);
1237         if (pEcoreEvas == null)
1238         {
1239                 return;
1240         }
1241
1242         pEcoreEvas->RotateWindow(::Convert(screenRotation));
1243
1244         _TouchManager* pTouchManager = _TouchManager::GetInstance();
1245         if (pTouchManager)
1246         {
1247                 pTouchManager->SetTouchCanceled(true);
1248         }
1249 }
1250
1251 void
1252 _ControlManager::RotateScreen(const _Control& control, _ControlRotation screenRotation)
1253 {
1254         _EcoreEvas* pEcoreEvas = ::GetEcoreEvasMgr()->GetEcoreEvas();
1255         SysAssert(pEcoreEvas);
1256         if (pEcoreEvas == null)
1257         {
1258                 return;
1259         }
1260
1261         // Rotate root window.
1262         _Window* pRootWindow = control.GetRootWindow();
1263         if (pRootWindow)
1264         {
1265                 pEcoreEvas->RotateWindow(*pRootWindow, ::Convert(screenRotation));
1266         }
1267
1268 #if defined(MULTI_WINDOW)
1269         // Rotate Ownees.
1270         int owneeCount = control.GetOwneeCount();
1271         for (int i = 0; i < owneeCount; i++)
1272         {
1273                 _Window* pOwnee = control.GetOwnee(i);
1274                 if (pOwnee)
1275                 {
1276                         pEcoreEvas->RotateWindow(*pOwnee, ::Convert(screenRotation));
1277                 }
1278         }
1279 #endif
1280
1281         _TouchManager* pTouchManager = _TouchManager::GetInstance();
1282         if (pTouchManager)
1283         {
1284                 pTouchManager->SetTouchCanceled(true);
1285         }
1286 }
1287
1288 void
1289 _ControlManager::OnWindowRotated(int rotation)
1290 {
1291 #if !defined(MULTI_WINDOW)
1292         IEnumeratorT<_Window*>* pEnumerator = __pSystemWindowList->GetEnumeratorN();
1293         SysTryReturnVoidResult(NID_UI, pEnumerator, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
1294
1295         while (pEnumerator->MoveNext() == E_SUCCESS)
1296         {
1297                 _Window* pWindow = null;
1298                 pEnumerator->GetCurrent(pWindow);
1299                 if (pWindow)
1300                 {
1301                         _WindowImpl* pWindowImpl = static_cast<_WindowImpl*>(pWindow->GetUserData());
1302                         if (pWindowImpl)
1303                         {
1304                                 pWindowImpl->Rotate(Convert(rotation));
1305                         }
1306                 }
1307         }
1308 #else
1309         // For non-ownees
1310         IEnumeratorT<_Window*>* pEnumerator = __pWindowList->GetEnumeratorN();
1311         SysTryReturnVoidResult(NID_UI, pEnumerator, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
1312
1313         while (pEnumerator->MoveNext() == E_SUCCESS)
1314         {
1315                 _Window* pWindow = null;
1316                 pEnumerator->GetCurrent(pWindow);
1317
1318                 if (pWindow->GetOwner() == null)
1319                 {
1320                         void* pUserData = pWindow->GetUserData();
1321                         if (pUserData)
1322                         {
1323                                 _WindowImpl* pWindowImpl = static_cast<_WindowImpl*>(pUserData);
1324                                 pWindowImpl->OnRotated(Convert(rotation));
1325                         }
1326                 }
1327         }
1328 #endif
1329
1330         delete pEnumerator;
1331 }
1332
1333 void
1334 _ControlManager::SetFocusedControl(const _Control& control)
1335 {
1336         if (__pFocusedControl == &control)
1337         {
1338                 return;
1339         }
1340
1341         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
1342         SysTryReturnVoidResult(NID_UI, pEcoreEvas, E_SYSTEM, "[E_SYSTEM] System error occurred.");
1343
1344         _Control *pPreviousFocusedControl = __pFocusedControl;
1345         __pFocusedControl = const_cast<_Control*>(&control);
1346
1347         if (pPreviousFocusedControl)
1348         {
1349                 // [review] make SetFocus() return result and use the returned result here.
1350                 pEcoreEvas->SetFocus(*pPreviousFocusedControl, false);
1351                 if (GetLastResult() == E_SUCCESS)
1352                 {
1353                         if (pPreviousFocusedControl)
1354                         {
1355                                 pPreviousFocusedControl->SetFocusOff(pPreviousFocusedControl);
1356                         }
1357                         _UiFocusEvent event(pPreviousFocusedControl->GetHandle(), FOCUS_LOST);
1358                         _UiEventManager::GetInstance()->SendEvent(event);
1359                 }
1360         }
1361
1362         // [review] make SetFocus() return result and use the returned result here.
1363         if (control.IsNativeObjectFocusable())
1364         {
1365                 pEcoreEvas->SetFocus(control, true);
1366         }
1367
1368         if (GetLastResult() == E_SUCCESS)
1369         {
1370                 _UiFocusEvent event(control.GetHandle(), FOCUS_GAINED);
1371                 _UiEventManager::GetInstance()->SendEvent(event);
1372                 const_cast<_Control*>(&control)->SetFocusOn();
1373         }
1374 }
1375
1376 // [review] called in ~_Control and virtual OnFocusLost is called.
1377 // _Control::Release() instead of ~_Control.
1378 bool
1379 _ControlManager::TakeFocusFromControl(const _Control& control)
1380 {
1381         if (__pFocusedControl == &control)
1382         {
1383                 _UiFocusEvent event(__pFocusedControl->GetHandle(), FOCUS_LOST);
1384                 _UiEventManager::GetInstance()->SendEvent(event);
1385                 __pFocusedControl = null;
1386                 return true;
1387         }
1388
1389         return false;
1390 }
1391
1392 _Control*
1393 _ControlManager::GetFocusedControl(void) const
1394 {
1395         return __pFocusedControl;
1396 }
1397
1398 _Control*
1399 _ControlManager::GetTopmostTouchedControl(const Point& point)
1400 {
1401         _Control* pControl = null;
1402         _Window* pTopWindow = null;
1403
1404 #if !defined(MULTI_WINDOW)
1405         pTopWindow = GetTopWindow();
1406 #else
1407         int count = GetWindowCount();
1408
1409         if (count != 0)
1410         {
1411                 for (int i = 0; i < count; i++)
1412                 {
1413                         _Window* pWindow = GetWindow((count-1) - i);
1414
1415                         if (pWindow->GetVisibleState() == false)
1416                         {
1417                                 continue;
1418                         }
1419
1420                         _RootVisualElement* pRootVE = pWindow->GetRootVisualElement();
1421                         _EflLayer* pLayer = static_cast<_EflLayer*>(pRootVE->GetNativeLayer());
1422
1423                         Ecore_Evas* pEcoreEvas = pLayer->GetEcoreEvas();
1424                         Ecore_X_Window win = (Ecore_X_Window)ecore_evas_window_get(pEcoreEvas);
1425
1426                         Rectangle winDeviceBounds(0, 0, 0, 0);
1427                         ecore_x_window_geometry_get(win, &winDeviceBounds.x, &winDeviceBounds.y, &winDeviceBounds.width, &winDeviceBounds.height);
1428
1429                         Point winDevicePoint = _CoordinateSystemUtils::Transform(pWindow->GetPosition());
1430
1431                         Point devicePoint = _CoordinateSystemUtils::Transform(point);
1432                         int x = devicePoint.x;
1433                         int y = devicePoint.y;
1434
1435                         int rootW = 0;
1436                         int rootH = 0;
1437                         ecore_x_window_size_get(ecore_x_window_root_get(win), &rootW, &rootH);
1438
1439                         int rotation = ecore_evas_rotation_get(pEcoreEvas);
1440                         switch (rotation)
1441                         {
1442                         case 270:
1443                                 devicePoint.x = rootW - y;
1444                                 devicePoint.y = x;
1445                                 winDeviceBounds.x = rootW - winDevicePoint.y - winDeviceBounds.width;
1446                                 winDeviceBounds.y = winDevicePoint.x;
1447                                 break;
1448                         case 180:
1449                                 devicePoint.x = rootW - x;
1450                                 devicePoint.y = rootH - y;
1451                                 winDeviceBounds.x = rootW - winDevicePoint.x - winDeviceBounds.width;
1452                                 winDeviceBounds.y = rootH - winDevicePoint.y - winDeviceBounds.height;
1453                                 break;
1454                         case 90:
1455                                 devicePoint.x = y;
1456                                 devicePoint.y = rootH - x;
1457                                 winDeviceBounds.x = winDevicePoint.y;
1458                                 winDeviceBounds.y = rootH - winDevicePoint.x - winDeviceBounds.height;
1459                                 break;
1460                         default:
1461                                 devicePoint.x = x;
1462                                 devicePoint.y = y;
1463                                 winDeviceBounds.x = winDevicePoint.x;
1464                                 winDeviceBounds.y = winDevicePoint.y;
1465                                 break;
1466                         }
1467
1468                         if (winDeviceBounds.Contains(devicePoint))
1469                         {
1470                                 pTopWindow = pWindow;
1471                                 break;
1472                         }
1473                 }
1474         }
1475 #endif
1476
1477         if (pTopWindow != null)
1478         {
1479                 Point winPos(0, 0);
1480
1481 #if !defined(MULTI_WINDOW)
1482                 _Frame* pFrame = dynamic_cast<_Frame*>(pTopWindow);
1483                 if (pFrame)
1484                 {
1485                         winPos = pFrame->GetPosition();
1486                 }
1487
1488                 Point relPos(point.x - winPos.x, point.y - winPos.y);
1489                 pControl = GetRoot().GetTopmostChildAt(relPos);
1490 #else
1491                 winPos = pTopWindow->GetPosition();
1492
1493                 Point relPos(point.x - winPos.x, point.y - winPos.y);
1494                 pControl = pTopWindow->GetTopmostChildAt(relPos);
1495 #endif
1496                 if (pControl != null)
1497                 {
1498                         if (pControl->GetRootWindow() == pTopWindow)
1499                         {
1500                                 return pControl;
1501                         }
1502                 }
1503         }
1504
1505         return null;
1506 }
1507
1508 // [review] rename __InvXformer
1509 Dimension
1510 _ControlManager::GetScreenSize(void) const
1511 {
1512         return _CoordinateSystemUtils::InverseTransform(Dimension(_Screen::GetWidth(), _Screen::GetHeight()));
1513 }
1514
1515 _Window*
1516 _ControlManager::GetCurrentFrame(void) const
1517 {
1518         return __pCurrentFrame;
1519 }
1520
1521 result
1522 _ControlManager::SetDefaultFont(const String& appFontName)
1523 {
1524
1525         if(appFontName.Equals(__defaultFontName))
1526         {
1527                 return E_SUCCESS;
1528         }
1529
1530         __isDefaultFontChanged = true;
1531         __defaultFontName = appFontName;
1532
1533         struct _Visitor
1534                 : public _Control::Visitor
1535         {
1536                  virtual _Control::VisitType Visit(_Control& control)
1537                 {
1538                         if (control.__fontName.IsEmpty())
1539                         {
1540                                 control.GetFallbackFont();
1541                                 _IControlDelegate& delegate = control.GetControlDelegate();
1542                                 delegate.OnFontChanged(control.__pFont);
1543                         }
1544                         return _Control::VISIT_DOWNWARD;
1545                 }
1546         };
1547
1548         _Visitor visitor;
1549
1550 #if !defined(MULTI_WINDOW)
1551         _Control& control = GetRoot();
1552         control.Accept(visitor);
1553 #else
1554         int count = GetWindowCount();
1555
1556         for (int j = 0; j < count; j++)
1557         {
1558                 _Window* pWindow = GetWindow((count-1) - j);
1559                 pWindow->Accept(visitor);
1560         }
1561 #endif
1562
1563         SetDefaultFontChangeState(false);
1564
1565         return E_SUCCESS;
1566 }
1567
1568 Tizen::Base::String
1569 _ControlManager::GetDefaultFont(void)
1570 {
1571         return __defaultFontName;
1572 }
1573
1574 bool
1575 _ControlManager::IsDefaultFontChanged(void)
1576 {
1577         return __isDefaultFontChanged;
1578 }
1579
1580 void
1581 _ControlManager::SetDefaultFontChangeState(bool isDefaultFontChanged)
1582 {
1583         __isDefaultFontChanged = isDefaultFontChanged;
1584 }
1585
1586 bool
1587 _ControlManager::IsFrameActivated(void) const
1588 {
1589         _Frame* pFrame = dynamic_cast<_Frame*>(__pCurrentFrame);
1590         SysTryReturn(NID_UI, pFrame, false, E_SYSTEM, "[E_SYSTEM] System error occurred. ");
1591
1592         return pFrame->IsActivated();
1593 }
1594
1595 // [review] refactoring
1596 result
1597 _ControlManager::AddGestureDetector(const _TouchGestureDetector& gesture)
1598 {
1599         ClearLastResult();
1600
1601         SysTryReturn(NID_UI, __pGestureList != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred. ");
1602
1603         bool exist = __pGestureList->Contains(const_cast<_TouchGestureDetector*>(&gesture));
1604         SysTryReturnResult(NID_UI, exist == false, E_OBJ_ALREADY_EXIST, "[E_OBJ_ALREADY_EXIST]__pGestureList has gesture already");
1605
1606         result r = __pGestureList->Add(const_cast<_TouchGestureDetector*>(&gesture));
1607         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1608
1609         switch (gesture.GetDetectorType())
1610         {
1611                 case _TOUCH_GESTURE_DETECTOR_TYPE_TAP:
1612                 {
1613                         _TouchTapGestureDetector* pGestureTap = dynamic_cast<_TouchTapGestureDetector*>(const_cast<_TouchGestureDetector*>(&gesture));
1614                         SysTryReturnResult(NID_UI, pGestureTap, E_SYSTEM, "[E_SYSTEM]system error occurred.");
1615
1616                         if (pGestureTap->GetTapInterval() > __gestureMaxDuration)
1617                         {
1618                                 __gestureMaxDuration = pGestureTap->GetTapInterval();
1619                         }
1620                 }
1621                 break;
1622                 case _TOUCH_GESTURE_DETECTOR_TYPE_LONG_PRESS:
1623                 {
1624                         _TouchLongPressGestureDetector* pGestureLongPress= dynamic_cast<_TouchLongPressGestureDetector*>(const_cast<_TouchGestureDetector*>(&gesture));
1625                         SysTryReturnResult(NID_UI, pGestureLongPress, E_SYSTEM, "[E_SYSTEM]system error occurred.");
1626
1627                         if (pGestureLongPress->GetDuration() > __gestureMaxDuration)
1628                         {
1629                                 __gestureMaxDuration = pGestureLongPress->GetDuration();
1630                         }
1631                 }
1632                 break;
1633                 default:
1634                         break;
1635         }
1636
1637         return E_SUCCESS;
1638 }
1639
1640 result
1641 _ControlManager::RemoveGestureDetector(const _TouchGestureDetector& gesture)
1642 {
1643         ClearLastResult();
1644
1645         result r = __pGestureList->Remove(&(const_cast<_TouchGestureDetector&>(gesture)));
1646         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1647
1648         return E_SUCCESS;
1649 }
1650
1651 IListT <_TouchGestureDetector*>*
1652 _ControlManager::GetGestureDetectorList(void) const
1653 {
1654         return __pGestureList;
1655 }
1656
1657 int
1658 _ControlManager::GetGestureMaxTimeDuration(void) const
1659 {
1660         return __gestureMaxDuration;
1661 }
1662
1663 void
1664 _ControlManager::OnSettingChanged(Tizen::Base::String& key)
1665 {
1666         const wchar_t* FONT_TYPE = L"http://tizen.org/setting/font.type";
1667         const wchar_t* LOCALE_COUNTRY = L"http://tizen.org/setting/locale.country";
1668         const wchar_t* LOCALE_LANGUAGE = L"http://tizen.org/setting/locale.language";
1669
1670          if (key == FONT_TYPE || key == LOCALE_COUNTRY || key == LOCALE_LANGUAGE)
1671         {
1672                 _FontImpl::UpdateDefaultFont(key);
1673
1674                 int count = GetWindowCount();
1675
1676                 for(int index = 0; index < count ; index++)
1677                 {
1678                         _Window* pWindow = GetWindow(index);
1679                         pWindow->Invalidate(true);
1680                 }
1681         }
1682 }
1683
1684 }} // Tizen::Ui