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