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