Merge "Fixed DragAndDrop for sending dnd events to Source control" into tizen_2.2
[platform/framework/native/uifw.git] / src / ui / FUi_Control.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_Control.cpp
20  * @brief               This is the implementation file for the _Control class.
21  */
22
23 #include <new>
24 #include <execinfo.h>
25 #include <unique_ptr.h>
26 #include <FBaseColLinkedListT.h>
27 #include <FBaseColArrayListT.h>
28 #include <FBaseColHashMapT.h>
29 #include <FBaseSysLog.h>
30 #include <FGrpFloatRectangle.h>
31 #include <FUiAnimVisualElementContentProvider.h>
32 #include <FBase_Log.h>
33 #include <FGrp_BitmapImpl.h>
34 #include <FSys_SystemInfoImpl.h>
35 #include "FUi_Control.h"
36 #include "FUi_ControlManager.h"
37 #include "FUi_FocusManagerImpl.h"
38 #include "FUi_CoordinateSystemUtils.h"
39 #include "FUi_Window.h"
40 #include "FUi_EcoreEvasMgr.h"
41 #include "FUi_EcoreEvas.h"
42 #include "FUi_LayoutLayoutContainer.h"
43 #include "FUi_LayoutAbsoluteLayout.h"
44 #include "FUi_LayoutILayoutItemHandler.h"
45 #include "FUi_TouchManager.h"
46 #include "FUi_DataBindingContext.h"
47 #include "FUi_TouchLongPressGestureDetector.h"
48 #include "FUi_TouchTapGestureDetector.h"
49 #include "FUi_AccessibilityContainer.h"
50 #include "FUi_ResourceManager.h"
51 #include "FUiAnim_ControlVisualElement.h"
52 #include "FUiAnim_Debug.h"
53 #include "FUiAnim_VisualElement.h"
54 #include "FUiAnim_VisualElementImpl.h"
55 #include "FUiCtrl_Form.h"
56 #include "FUiCtrl_Frame.h"
57 #include "FUi_ContainerImpl.h"
58 #include "FUi_DragAndDropItem.h"
59
60 using namespace std;
61 using namespace Tizen::Base;
62 using namespace Tizen::Base::Collection;
63 using namespace Tizen::Base::Runtime;
64 using namespace Tizen::Graphics;
65 using namespace Tizen::Ui;
66 using namespace Tizen::Ui::Animations;
67 using namespace Tizen::Ui::Controls;
68
69 namespace {
70
71 int
72 GetZOrderGroupOfVisualElement(_ControlLayer layer)
73 {
74         switch (layer)
75         {
76         case _CONTROL_LAYER_OVERLAY:
77                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL - 1;
78         case _CONTROL_LAYER_CLIENT_BOTTOM:
79                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL;
80         case _CONTROL_LAYER_NONE:
81                 // fall through
82         case _CONTROL_LAYER_CLIENT_MIDDLE:
83                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 1;
84         case _CONTROL_LAYER_CLIENT_TOP:
85                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 2;
86         case _CONTROL_LAYER_SYSTEM:
87                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 3;
88         default:
89                 SysAssert(false);
90                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 1;
91         }
92 }
93
94 inline bool
95 AdjustSizeToRange(float& width, float& height, const FloatDimension& minDim, const FloatDimension& maxDim)
96 {
97         bool changed = false;
98         if (width < minDim.width)
99         {
100                 width = minDim.width;
101                 changed = true;
102         }
103         if (height < minDim.height)
104         {
105                 height = minDim.height;
106                 changed = true;
107         }
108
109         if (width > maxDim.width)
110         {
111                 width = maxDim.width;
112                 changed = true;
113         }
114         if (height > maxDim.height)
115         {
116                 height = maxDim.height;
117                 changed = true;
118         }
119
120         return changed;
121 }
122
123 inline bool
124 AdjustSizeToRange(FloatDimension& dim, const FloatDimension& minDim, const FloatDimension& maxDim)
125 {
126         return AdjustSizeToRange(dim.width, dim.height, minDim, maxDim);
127 }
128
129 // E_OUT_OF_MEMORY
130 // E_SYSTEM
131 _ControlVisualElement*
132 CreateVisualElementN(void)
133 {
134         ClearLastResult();
135
136         _ControlVisualElement* pVisualElement = new (std::nothrow) _ControlVisualElement;
137         SysTryReturn(NID_UI, pVisualElement, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
138
139         SysTryCatch(NID_UI,
140                 pVisualElement->ConstructControlVisualElement() == E_SUCCESS, ,
141                 E_SYSTEM, "[E_SYSTEM] System error occurred.");
142
143         pVisualElement->SetImplicitAnimationEnabled(false);
144         pVisualElement->SetShowState(true);
145         pVisualElement->SetBackBufferEnabled(true);
146         pVisualElement->SetRedrawOnResizeEnabled(true);
147         pVisualElement->SetSurfaceOpaque(false);
148
149         return pVisualElement;
150
151 CATCH:
152         //delete pVisualElement;
153         pVisualElement->Destroy();
154         return null;
155 }
156
157 _Control::GestureMap*
158 CreateGestureMapN(void)
159 {
160         ClearLastResult();
161
162         _Control::GestureMap* pGestureMap = new (std::nothrow) _Control::GestureMap;
163         SysTryReturn(NID_UI, pGestureMap, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
164
165         SysTryCatch(NID_UI,
166                 pGestureMap->Construct() == E_SUCCESS, ,
167                 E_SYSTEM, "[E_SYSTEM] System error occurred.");
168
169         return pGestureMap;
170
171 CATCH:
172         delete pGestureMap;
173         return null;
174 }
175
176 // E_OUT_OF_MEMORY
177 _Control::ControlList*
178 CreateControlListN(void)
179 {
180         ClearLastResult();
181
182         _Control::ControlList* pControlList = new (std::nothrow) _Control::ControlList;
183         SysTryReturn(NID_UI, pControlList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
184
185         return pControlList;
186 }
187
188 // E_OUT_OF_MEMORY
189 _Control::WindowList*
190 CreateWindowListN(void)
191 {
192         ClearLastResult();
193
194         _Control::WindowList* pWindowList = new (std::nothrow) _Control::WindowList;
195         SysTryReturn(NID_UI, pWindowList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
196
197         return pWindowList;
198 }
199
200 // E_OUT_OF_MEMORY
201 // E_SYSTEM
202 _Layout::LayoutContainer*
203 CreateLayoutContainerN(_Layout::ILayoutItemHandler* pLayoutItemHandler)
204 {
205         ClearLastResult();
206         result r = E_SUCCESS;
207
208         _Layout::LayoutContainer* pLayoutContainer = null;
209         _Layout::AbsoluteLayout* pAbsLayout = null;
210
211         pLayoutContainer = new (std::nothrow) _Layout::LayoutContainer();
212         SysTryReturn(NID_UI, pLayoutContainer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage");
213         if (IsFailed(GetLastResult()))
214         {
215                 goto CATCH;
216         }
217
218         pAbsLayout = new (std::nothrow) _Layout::AbsoluteLayout();
219         SysTryCatch(NID_UI, pAbsLayout, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
220         if (IsFailed(GetLastResult()))
221         {
222                 goto CATCH;
223         }
224
225         pLayoutContainer->SetItemHandler(pLayoutItemHandler);
226
227         r = pLayoutContainer->SetDefaultLayout(*pAbsLayout);
228         SysTryCatch(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
229
230         return pLayoutContainer;
231
232 CATCH:
233         delete pAbsLayout;
234         delete pLayoutContainer;
235
236         return null;
237 }
238
239 } // Anonymous namespace
240
241 namespace Tizen { namespace Ui
242 {
243
244 IMPLEMENT_PROPERTY(_Control);
245
246 class _Control::ControlVisualElementContentProvider
247         : public VisualElementContentProvider
248 {
249
250 private:
251         _Control& __control;
252
253 public:
254         ControlVisualElementContentProvider(_Control& control)
255                 : __control(control)
256         {
257         }
258
259         virtual ~ControlVisualElementContentProvider(void)
260         {
261         }
262
263         virtual bool PrepareDraw(VisualElement& target)
264         {
265                 _ControlVisualElement* pCVE = dynamic_cast <_ControlVisualElement*>(&target);
266                 if (!pCVE)
267                 {
268                         return false;
269                 }
270
271                 Color bgColor = __control.GetBackgroundColor();
272
273                 pCVE->SetBackgroundColor(
274                         _Colorf(
275                                 (float) bgColor.GetRed() / 255.0f,
276                                 (float) bgColor.GetGreen() / 255.0f,
277                                 (float) bgColor.GetBlue() / 255.0f,
278                                 (float) bgColor.GetAlpha() / 255.0f
279                                 )
280                         );
281
282                 __control.GetControlDelegate().OnDraw();
283
284                 target.SetFlushNeeded();
285
286                 return false;
287         }
288
289         virtual HitTestResult HitTest(VisualElement& target, const FloatPoint& point)
290         {
291                 return __control.GetControlDelegate().HitTest(point);
292         }
293
294 private:
295         ControlVisualElementContentProvider(const ControlVisualElementContentProvider& rhs);
296         ControlVisualElementContentProvider& operator =(const ControlVisualElementContentProvider& rhs);
297 };
298
299 class _Control::ControlVisualElementEventListener
300         : public IVisualElementEventListener
301 {
302 public:
303         ControlVisualElementEventListener(_Control& control)
304                 : __control(control)
305         {
306         }
307
308         virtual ~ControlVisualElementEventListener(void)
309         {
310         }
311
312 public:
313         virtual void OnChildAttached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& child)
314         {
315         }
316
317         virtual void OnChildDetached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& child)
318         {
319         }
320
321         virtual void OnAttached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& parent)
322         {
323         }
324
325         virtual void OnDetached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& parent)
326         {
327         }
328
329         virtual result OnTransformChanging(Tizen::Ui::Animations::VisualElement& source, FloatMatrix4& newTransform)
330         {
331                 return E_SUCCESS;
332         }
333
334         virtual void OnTransformChanged(Tizen::Ui::Animations::VisualElement& source, const FloatMatrix4& previousTransform)
335         {
336         }
337
338         virtual result OnChildrenTransformChanging(Tizen::Ui::Animations::VisualElement& source, FloatMatrix4& newTransform)
339         {
340                 for (int i = 0; i < __control.GetChildCount(); ++i)
341                 {
342                         _Control* pChild = __control.GetChild(i);
343                         if (!pChild)
344                         {
345                                 continue;
346                         }
347
348                         if (pChild->GetArea() == _CONTROL_AREA_SYSTEM)
349                         {
350                                 _VisualElement* pVisualElement = pChild->GetVisualElement();
351                                 if (pVisualElement)
352                                 {
353                                         FloatMatrix4 inverseMatrix(newTransform);
354                                         inverseMatrix.Invert();
355
356                                         result r = pVisualElement->SetTransformMatrix(inverseMatrix);
357                                         if (r != E_SUCCESS)
358                                         {
359                                                 continue;
360                                         }
361                                 }
362                         }
363                 }
364
365                 return E_SUCCESS;
366         }
367
368         virtual void OnChildrenTransformChanged(Tizen::Ui::Animations::VisualElement& source, const FloatMatrix4& previousTransform)
369                 {
370         }
371
372         virtual result OnBoundsChanging(Tizen::Ui::Animations::VisualElement& source, FloatRectangle& newBounds)
373         {
374                 return E_SUCCESS;
375         }
376
377         virtual void OnBoundsChanged(Tizen::Ui::Animations::VisualElement& source, const FloatRectangle& previousBounds)
378         {
379         }
380
381         virtual void OnShowStateChanged(Tizen::Ui::Animations::VisualElement& source, bool previousShowState)
382         {
383         }
384
385 private:
386         ControlVisualElementEventListener(const ControlVisualElementEventListener& rhs);
387         ControlVisualElementEventListener& operator =(const ControlVisualElementEventListener& rhs);
388
389 private:
390         _Control& __control;
391 }; // ControlVisualElementEventListener
392
393 // Layout Item Handler
394 class _Control::LayoutItemHandler
395         : public _Layout::ILayoutItemHandler
396 {
397 public:
398         LayoutItemHandler(_Control* pControl)
399                 : __pControl(pControl)
400         {
401                 SysAssert(__pControl);
402         }
403
404         void SetItemVisibleState(bool visible)
405         {
406                 __pControl->SetVisibleState(visible);
407         }
408
409         result SetItemBounds(const FloatRectangle& rect)
410         {
411                 SysAssert(__pControl->IsInSizeRange(FloatDimension(rect.width, rect.height)));
412                 return __pControl->SetBoundsFinal(rect, false, true);
413         }
414
415         FloatRectangle GetItemBounds(void) const
416         {
417                 return __pControl->GetBoundsF();
418         }
419
420         FloatRectangle GetItemClientBoundsFromSize(const FloatDimension& size) const
421         {
422                 FloatRectangle clientBounds(0.0f, 0.0f, 0.0f, 0.0f);
423                 __pControl->UpdateClientBounds(size, clientBounds);
424                 return clientBounds;
425         }
426
427         FloatDimension GetItemContentSize(bool horizontalMode, bool verticalMode) const
428         {
429                 return __pControl->GetControlDelegate().GetContentSizeF(horizontalMode, verticalMode);
430         }
431
432         FloatDimension GetItemMinimumSize(void) const
433         {
434                 return __pControl->GetMinimumSizeF();
435         }
436
437         FloatDimension GetItemMaximumSize(void) const
438         {
439                 return __pControl->GetMaximumSizeF();
440         }
441
442         result OnItemMeasure(float& width, float& height)
443         {
444                 Dimension evaluatedSize(_CoordinateSystemUtils::ConvertToInteger(width), _CoordinateSystemUtils::ConvertToInteger(height));
445                 FloatDimension evaluatedSizeF(width, height);
446
447                 bool changed = __pControl->GetControlDelegate().OnEvaluateSize(evaluatedSizeF);
448                 if (changed)
449                 {
450                         width = evaluatedSizeF.width;
451                         height = evaluatedSizeF.height;
452                 }
453                 else
454                 {
455                         __pControl->GetControlDelegate().OnEvaluateSize(evaluatedSize);
456
457                         if (evaluatedSize.width != (_CoordinateSystemUtils::ConvertToInteger(width)))
458                         {
459                                 width = evaluatedSize.width;
460                         }
461                         if (evaluatedSize.height != (_CoordinateSystemUtils::ConvertToInteger(height)))
462                         {
463                                 height = evaluatedSize.height;
464                         }
465                 }
466
467                 return E_SUCCESS;
468         }
469
470 private:
471         LayoutItemHandler(const LayoutItemHandler& rhs);
472         LayoutItemHandler& operator =(const LayoutItemHandler& rhs);
473
474 private:
475         _Control* __pControl;
476 }; // LayoutItemHandler
477
478 // E_OUT_OF_MEMORY
479 // E_SYSTEM
480 _Control*
481 _Control::CreateControlN(void)
482 {
483         _Control* pControl = new (std::nothrow) _Control;
484         SysTryReturn(NID_UI, pControl, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
485         if (IsFailed(GetLastResult()))
486         {
487                 goto CATCH;
488         }
489
490         pControl->AcquireHandle();
491
492         SysAssert(GetLastResult() == E_SUCCESS);
493         return pControl;
494
495 CATCH:
496         delete pControl;
497         return null;
498 }
499
500 void
501 _Control::ResetEventListeners(void)
502 {
503         __pFocusEventListener = this;
504         __pNotificationEventListener = this;
505 }
506
507 void
508 _Control::SetControlDelegate(_IControlDelegate& delegate)
509 {
510         __pControlDelegate = &delegate;
511 }
512
513 void
514 _Control::ResetControlDelegate(void)
515 {
516         __pControlDelegate = this;
517 }
518
519 _IControlDelegate&
520 _Control::GetControlDelegate(void) const
521 {
522         if (__destroying)
523         {
524                 return const_cast<_Control&>(*this);
525         }
526
527         SysAssert(__pControlDelegate);
528         return *__pControlDelegate;
529 }
530
531 void
532 _Control::SetPropagatedTouchEventListener(_IPropagatedTouchEventListener* pListener)
533 {
534         __pPropagatedTouchEventListener = pListener;
535 }
536
537 _IPropagatedTouchEventListener*
538 _Control::GetPropagatedTouchEventListener(void) const
539 {
540         if (__destroying)
541         {
542                 return const_cast<_Control*>(this);
543         }
544
545         SysAssert(__pPropagatedTouchEventListener);
546         return __pPropagatedTouchEventListener;
547 }
548
549 void
550 _Control::SetPropagatedKeyEventListener(_IPropagatedKeyEventListener* pListener)
551 {
552         __pPropagatedKeyEventListener = pListener;
553 }
554
555 _IPropagatedKeyEventListener*
556 _Control::GetPropagatedKeyEventListener(void) const
557 {
558         if (__destroying)
559         {
560                 return const_cast<_Control*>(this);
561         }
562
563         SysAssert(__pPropagatedKeyEventListener);
564         return __pPropagatedKeyEventListener;
565 }
566
567 bool
568 _Control::OnPreviewKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
569 {
570         return false;
571 }
572
573 bool
574 _Control::OnPreviewKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
575 {
576         return false;
577 }
578
579 bool
580 _Control::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
581 {
582         //SysLog(NID_UI, ">>> [core] OnKeyPressed(%d, %d)", keyInfo.GetKeyCode(), keyInfo.GetKeyModifier());
583         return false;
584 }
585
586 bool
587 _Control::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
588 {
589         //SysLog(NID_UI, ">>> [core] OnKeyReleased(%d, %d)", keyInfo.GetKeyCode(), keyInfo.GetKeyModifier());
590         return false;
591 }
592
593 bool
594 _Control::TranslateKeyEventInfo(const _Control& source, _KeyInfo& keyInfo)
595 {
596         return false;
597 }
598
599 _UiTouchEventDelivery
600 _Control::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
601 {
602         return _UI_TOUCH_EVENT_DELIVERY_YES;
603 }
604
605 _UiTouchEventDelivery
606 _Control::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
607 {
608         return _UI_TOUCH_EVENT_DELIVERY_YES;
609 }
610
611 _UiTouchEventDelivery
612 _Control::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
613 {
614         return _UI_TOUCH_EVENT_DELIVERY_YES;
615 }
616
617 _UiTouchEventDelivery
618 _Control::OnPreviewTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
619 {
620         return _UI_TOUCH_EVENT_DELIVERY_YES;
621 }
622
623 _UiTouchEventDelivery
624 _Control::OnPreviewTouchWheeled(const _Control& source, const _TouchInfo& touchinfo)
625 {
626         return _UI_TOUCH_EVENT_DELIVERY_YES;
627 }
628
629 bool
630 _Control::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
631 {
632         return false;
633 }
634
635 bool
636 _Control::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
637 {
638         return false;
639 }
640
641 bool
642 _Control::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
643 {
644         return false;
645 }
646
647 bool
648 _Control::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
649 {
650         return false;
651 }
652
653 bool
654 _Control::OnTouchWheeled(const _Control& source, const _TouchInfo& touchinfo)
655 {
656         return false;
657 }
658
659 bool
660 _Control::OnFocusGained(const _Control& source)
661 {
662         bool isFocusMode = _FocusManagerImpl::GetInstance()->IsFocusModeStateEnabled();
663         bool isFocusalbeControl = _FocusManagerImpl::GetInstance()->IsFocusable(this);
664         if (isFocusMode && isFocusalbeControl)
665         {
666                 DrawFocus();
667         }
668         return false;
669 }
670
671 bool
672 _Control::OnFocusLost(const _Control& source)
673 {
674     if (__pFocusVisualElement)
675     {
676                 __pFocusVisualElement->SetShowState(false);
677     }
678         return false;
679 }
680
681 bool
682 _Control::OnPreviewNotifiedN(const _Control& source, IList* pArgs)
683 {
684         return false;
685 }
686
687 bool
688 _Control::OnNotifiedN(const _Control& source, IList* pArgs)
689 {
690         return false;
691 }
692
693 bool
694 _Control::IsMovable(void) const
695 {
696         ClearLastResult();
697         return __movable;
698 }
699
700 bool
701 _Control::IsResizable(void) const
702 {
703         ClearLastResult();
704         return __resizable;
705 }
706
707 Dimension
708 _Control::GetContentSize(void) const
709 {
710         ClearLastResult();
711         return Dimension(0, 0);
712 }
713
714 FloatDimension
715 _Control::GetContentSizeF(bool horizontalMode, bool verticalMode) const
716 {
717         ClearLastResult();
718         return FloatDimension(0.0f, 0.0f);
719 }
720
721 HitTestResult
722 _Control::HitTest(const FloatPoint& point)
723 {
724         _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*__pVisualElement);
725
726         if (pVisualElementImpl)
727         {
728                 if (pVisualElementImpl->HitTestI(point) == _VisualElementImpl::HITTEST_MATCH)
729                 {
730                         return HIT_TEST_MATCH;
731                 }
732         }
733
734         return HIT_TEST_NOWHERE;
735 }
736
737 float
738 _Control::GetVerticalScrollPosition(void) const
739 {
740         return 0.0f;
741 }
742
743 float
744 _Control::GetHorizontalScrollPosition(void) const
745 {
746         return 0.0f;
747 }
748
749 _ControlOrientation
750 _Control::GetOrientation(void) const
751 {
752         ClearLastResult();
753         return __orientation;
754 }
755
756 void
757 _Control::OnDraw(void)
758 {
759 }
760
761 Canvas*
762 _Control::OnCanvasRequestedN(const FloatRectangle& bounds)
763 {
764
765         return null;
766 }
767
768 Bitmap*
769 _Control::OnCapturedBitmapRequestedN(void)
770 {
771
772         return null;
773 }
774
775 result
776 _Control::OnAttaching(const _Control* pParent)
777 {
778         return E_SUCCESS;
779 }
780
781 result
782 _Control::OnAttached(void)
783 {
784         return E_SUCCESS;
785 }
786
787 result
788 _Control::OnAttachingToMainTree(const _Control* pParent)
789 {
790         return E_SUCCESS;
791 }
792
793 result
794 _Control::OnPreAttachedToMainTree(void)
795 {
796         return E_SUCCESS;
797 }
798
799 result
800 _Control::OnAttachedToMainTree(void)
801 {
802         return E_SUCCESS;
803 }
804
805 result
806 _Control::OnDetachingFromMainTree(void)
807 {
808         return E_SUCCESS;
809 }
810
811 void
812 _Control::OnAttachingFailed(const _Control& parent)
813 {
814 }
815
816 result
817 _Control::OnDetaching(void)
818 {
819         return E_SUCCESS;
820 }
821
822 result
823 _Control::OnBoundsChanging(const Rectangle& bounds)
824 {
825         return E_SUCCESS;
826 }
827
828 result
829 _Control::OnBoundsChanging(const FloatRectangle& bounds)
830 {
831         return E_SUCCESS;
832 }
833
834 void
835 _Control::OnBoundsChanged(void)
836 {
837
838 }
839
840 void
841 _Control::OnEvaluateSize(Dimension& evaluatedSize)
842 {
843
844 }
845
846 bool
847 _Control::OnEvaluateSize(FloatDimension& evaluatedSize)
848 {
849         return false;
850 }
851
852 void
853 _Control::OnParentBoundsChanged(const _Control& parent)
854 {
855 }
856
857 void
858 _Control::OnChildAttached(const _Control& child)
859 {
860 }
861
862 void
863 _Control::OnChildDetaching(const _Control& child)
864 {
865 }
866
867 void
868 _Control::OnChildDetached(const _Control& child)
869 {
870 }
871
872 void
873 _Control::OnChildBoundsChanged(const _Control& child)
874 {
875 }
876
877 void
878 _Control::OnChildVisibleStateChanged(const _Control& child)
879 {
880 }
881
882 void
883 _Control::OnChangeLayout(_ControlOrientation orientation)
884 {
885 }
886
887 void
888 _Control::OnChangeLayout(_ControlRotation rotation)
889 {
890 }
891
892 void
893 _Control::OnZOrderChanging(_ControlZOrderUpdate zOrderUpdate)
894 {
895 }
896
897 void
898 _Control::OnVisibleStateChanging(void)
899 {
900 }
901
902 void
903 _Control::OnVisibleStateChanged(void)
904 {
905 }
906
907 void
908 _Control::OnAncestorVisibleStateChanged(const _Control& control)
909 {
910         _TouchManager* pTouchManager = _TouchManager::GetInstance();
911         if (pTouchManager && IsVisible() == false && IsAttachedToMainTree() == true)
912         {
913                 if(pTouchManager->GetTouchControlSource() == this)
914                 {
915                         SysLog(NID_UI, "VisibleState changed false, Call SetTouchCanceled");
916                         pTouchManager->SetTouchCanceled(null);
917                 }
918         }
919 }
920
921 void
922 _Control::OnAncestorEnableStateChanged(const _Control& control)
923 {
924 }
925
926 void
927 _Control::OnAncestorInputEnableStateChanged(const _Control& control)
928 {
929 }
930
931 void
932 _Control::OnTouchPressHandled(const _Control& control)
933 {
934 }
935
936 void
937 _Control::OnTouchReleaseHandled(const _Control& control)
938 {
939 }
940
941 void
942 _Control::OnTouchMoveHandled(const _Control& control)
943 {
944 }
945
946 void
947 _Control::OnFontChanged(Tizen::Graphics::Font* pFont)
948 {
949         SetUpdateLayoutState(true);
950 }
951
952 void
953 _Control::OnFontInfoRequested(unsigned long& style, int& size)
954 {
955 }
956
957 void
958 _Control::OnFontInfoRequested(unsigned long& style, float& size)
959 {
960 }
961
962 void
963 _Control::OnBackgroundColorChanged(Color& backgroundColor)
964 {
965 }
966
967 void
968 _Control::OnFocusableStateChanged(bool focusalbeState)
969 {
970 }
971
972 void
973 _Control::OnFocusModeStateChanged(void)
974 {
975 }
976
977 void
978 _Control::OnTouchCancelHandled(const _Control& control)
979 {
980 }
981
982 void
983 _Control::Accept(Visitor& visitor)
984 {
985         ClearLastResult();
986
987         VisitType visitType = visitor.Visit(*this);
988
989         switch (visitType)
990         {
991         case VISIT_STOP:
992                 break;
993
994         case VISIT_DOWNWARD:
995                 for (int i = 0; i < GetChildCount(); ++i) // [Postpone] Keep handle list before iternation.
996                 {
997                         _Control* pChild = GetChild(i);
998                         if (pChild)
999                         {
1000                                 pChild->Accept(visitor);
1001                         }
1002                 }
1003                 break;
1004
1005         case VISIT_UPWARD:
1006                 {
1007                         _Control* pParent = GetParent();
1008                         if (pParent)
1009                         {
1010                                 pParent->Accept(visitor);
1011                         }
1012                 }
1013                 break;
1014
1015         default:
1016                 break;
1017         }
1018 }
1019
1020 void
1021 _Control::Accept(Visitor& visitor) const
1022 {
1023         const_cast <_Control*>(this)->Accept(visitor);
1024 }
1025
1026 void
1027 _Control::InvalidateHierarchyRootWindow(_Control& control)
1028 {
1029         control.__needRecalcRootWindow = true;
1030         control.__pRootWindow = null;
1031
1032         for (int i = 0; i < control.GetChildCount(); ++i)
1033         {
1034                 _Control* pChild = control.GetChild(i);
1035                 if (pChild)
1036                 {
1037                         pChild->InvalidateHierarchyRootWindow(*pChild);
1038                 }
1039         }
1040 }
1041
1042 void
1043 _Control::InvalidateHierarchyAbsoluteBounds(_Control& control)
1044 {
1045         control.__needRecalcAbsBounds = true;
1046         control.__needRecalcAbsBoundsF = true;
1047
1048         for (int i = 0; i < control.GetChildCount(); ++i)
1049         {
1050                 _Control* pChild = control.GetChild(i);
1051                 if (pChild)
1052                 {
1053                         pChild->InvalidateHierarchyAbsoluteBounds(*pChild);
1054                 }
1055         }
1056 }
1057
1058 void
1059 _Control::Draw(bool recursive)
1060 {
1061         ClearLastResult();
1062
1063         Invalidate(recursive);
1064         GetVisualElement()->Draw();
1065 }
1066
1067 void
1068 _Control::Show(void)
1069 {
1070         GetVisualElement()->Flush();
1071         ClearLastResult();
1072
1073         SysAssert(GetLastResult() == E_SUCCESS);
1074 }
1075
1076 void
1077 _Control::ChangeLayout(_ControlOrientation orientation, bool callRotation)
1078 {
1079         ClearLastResult();
1080
1081         struct _Visitor
1082                 : public Visitor
1083         {
1084                 _Visitor(_ControlOrientation orientation)
1085                         : __orientation(orientation){}
1086
1087                 virtual VisitType Visit(_Control& control)
1088                 {
1089                         if (control.__orientation != __orientation)
1090                         {
1091                                 control.__orientation = __orientation;
1092                                 control.GetControlDelegate().OnChangeLayout(__orientation);
1093                                 ClearLastResult();
1094                         }
1095
1096                         return VISIT_DOWNWARD;
1097                 }
1098
1099 private:
1100                 _ControlOrientation __orientation;
1101         };
1102
1103         _Visitor visitor(orientation);
1104         Accept(visitor);
1105
1106         SysAssert(GetLastResult() == E_SUCCESS);
1107
1108         if (callRotation == true)
1109         {
1110                 _ControlManager* pMgr = _ControlManager::GetInstance();
1111                 if (pMgr)
1112                 {
1113                         _ControlRotation rotation = pMgr->GetOrientationStatus();
1114                         ChangeLayout(rotation);
1115                 }
1116         }
1117 }
1118
1119 void
1120 _Control::ChangeLayout(_ControlRotation rotation)
1121 {
1122         ClearLastResult();
1123
1124         struct _Visitor
1125                 : public Visitor
1126         {
1127                 _Visitor(_ControlRotation rotation)
1128                         : __rotation(rotation){}
1129
1130                 virtual VisitType Visit(_Control& control)
1131                 {
1132                         if (control.__rotation != __rotation)
1133                         {
1134                                 control.__rotation = __rotation;
1135                                 control.GetControlDelegate().OnChangeLayout(__rotation);
1136                                 ClearLastResult();
1137                         }
1138
1139                         return VISIT_DOWNWARD;
1140                 }
1141
1142 private:
1143                 _ControlRotation __rotation;
1144         };
1145
1146         _Visitor visitor(rotation);
1147         Accept(visitor);
1148
1149         SysAssert(GetLastResult() == E_SUCCESS);
1150 }
1151
1152 bool
1153 _Control::IsLayoutChangable(void) const
1154 {
1155         return true;
1156 }
1157
1158 bool
1159 _Control::IsOrientationRoot(void) const
1160 {
1161         return false;
1162 }
1163
1164 void
1165 _Control::Invalidate(void)
1166 {
1167         ClearLastResult();
1168         GetVisualElement()->InvalidateRectangle(null);
1169 }
1170
1171 void
1172 _Control::Invalidate(bool recursive)
1173 {
1174         ClearLastResult();
1175
1176         // Update layout
1177         _Layout::Layout* pLayout = GetLayout();
1178         if (pLayout)
1179         {
1180                 pLayout->UpdateLayout();
1181         }
1182
1183         if (recursive == false)
1184         {
1185                 Invalidate();
1186         }
1187         else
1188         {
1189                 Invalidate(*this);
1190         }
1191 }
1192
1193 void
1194 _Control::Invalidate(_Control& control)
1195 {
1196         if (control.GetVisibleState() == false)
1197         {
1198                 return;
1199         }
1200
1201         control.Invalidate();
1202
1203         // Ownee
1204         int owneeCount = control.GetOwneeCount();
1205         for (int i = 0; i < owneeCount; ++i)
1206         {
1207                 _Window* pOwnee = control.GetOwnee(i);
1208                 if (pOwnee)
1209                 {
1210                         pOwnee->Invalidate(true);
1211                 }
1212         }
1213
1214         for (int i = 0; i < control.GetChildCount(); ++i)
1215         {
1216                 _Control* pChild = control.GetChild(i);
1217                 if (pChild)
1218                 {
1219                         pChild->Invalidate(*pChild);
1220                 }
1221         }
1222 }
1223
1224 void
1225 _Control::Invalidate(const Rectangle& rect)
1226 {
1227         ClearLastResult();
1228         FloatRectangle rectf(rect.x, rect.y, rect.width, rect.height);
1229         GetVisualElement()->InvalidateRectangle(&rectf);
1230
1231         __invalidatedBounds = _CoordinateSystemUtils::ConvertToFloat(rect);
1232 }
1233
1234 void
1235 _Control::Invalidate(const FloatRectangle& rect)
1236 {
1237         ClearLastResult();
1238         FloatRectangle rectf(rect.x, rect.y, rect.width, rect.height);
1239         GetVisualElement()->InvalidateRectangle(&rectf);
1240
1241         __invalidatedBounds = rect;
1242 }
1243
1244 bool
1245 _Control::Contains(const Point& point) const
1246 {
1247         ClearLastResult();
1248
1249         Rectangle bounds = GetBounds();
1250         bounds.x = bounds.y = 0;
1251         return bounds.Contains(point);
1252 }
1253
1254 bool
1255 _Control::Contains(const FloatPoint& point) const
1256 {
1257         ClearLastResult();
1258
1259         FloatRectangle bounds = GetBoundsF();
1260         bounds.x = bounds.y = 0;
1261         return bounds.Contains(point);
1262 }
1263
1264
1265 void
1266 _Control::PartialUpdateLayout(void)
1267 {
1268         ClearLastResult();
1269
1270         _Layout::Layout* pLayout = GetLayout();
1271         if (pLayout)
1272         {
1273                 pLayout->PartialUpdateLayout();
1274         }
1275 }
1276
1277 void
1278 _Control::UpdateLayout(void)
1279 {
1280         ClearLastResult();
1281
1282         _Layout::Layout* pLayout = GetLayout();
1283         if (pLayout)
1284         {
1285                 pLayout->UpdateLayout();
1286         }
1287 }
1288
1289 result
1290 _Control::SetChildAlwaysOnTop(_Control& child)
1291 {
1292         ClearLastResult();
1293
1294         SysTryReturn(NID_UI,
1295                                 child.GetParent() == this, E_INVALID_ARG,
1296                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1297
1298         SysTryReturn(NID_UI,
1299                                 child.GetArea() == _CONTROL_AREA_CLIENT, E_SYSTEM,
1300                                 E_SYSTEM, "[E_SYSTEM] The child is not in the client area.");
1301
1302         if (child.GetLayer() == _CONTROL_LAYER_CLIENT_TOP)
1303         {
1304                 return E_SUCCESS;
1305         }
1306
1307         child.SetLayer(_CONTROL_LAYER_CLIENT_TOP);
1308
1309         return E_SUCCESS;
1310 }
1311
1312 result
1313 _Control::SetChildAlwaysAtBottom(_Control& child)
1314 {
1315         ClearLastResult();
1316
1317         SysTryReturn(NID_UI,
1318                                 child.GetParent() == this, E_INVALID_ARG,
1319                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1320
1321         SysTryReturn(NID_UI,
1322                                 child.GetArea() == _CONTROL_AREA_CLIENT, E_SYSTEM,
1323                                 E_SYSTEM, "[E_SYSTEM] The child is not in the client area.");
1324
1325         if (child.GetLayer() == _CONTROL_LAYER_CLIENT_BOTTOM)
1326         {
1327                 return E_SUCCESS;
1328         }
1329
1330         child.SetLayer(_CONTROL_LAYER_CLIENT_BOTTOM);
1331
1332         return E_SUCCESS;
1333 }
1334
1335 result
1336 _Control::ResetChildLayer(_Control& child)
1337 {
1338         ClearLastResult();
1339
1340         SysTryReturn(NID_UI,
1341                                 child.GetParent() == this, E_INVALID_ARG,
1342                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1343
1344         SysTryReturn(NID_UI,
1345                                 child.GetArea() == _CONTROL_AREA_CLIENT, E_SYSTEM,
1346                                 E_SYSTEM, "[E_SYSTEM] The child is not in the client area.");
1347
1348         if (child.GetLayer() == _CONTROL_LAYER_CLIENT_MIDDLE)
1349         {
1350                 return E_SUCCESS;
1351         }
1352
1353         child.SetLayer(_CONTROL_LAYER_CLIENT_MIDDLE);
1354
1355         return E_SUCCESS;
1356 }
1357
1358
1359 result
1360 _Control::AttachSystemChild(_Control& child)
1361 {
1362         ClearLastResult();
1363         result r = E_SUCCESS;
1364
1365         r = StartAttaching(child, _CONTROL_AREA_SYSTEM);
1366         if (IsFailed(r))
1367         {
1368                 return r;
1369         }
1370
1371         ControlList& children = GetChildList();
1372         r = children.Add(&child);
1373         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1374
1375         r = GetVisualElement()->AttachChild(*child.GetVisualElement());
1376         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1377
1378         r = EndAttaching(child);
1379         if (IsFailed(r))
1380         {
1381                 result listResult = children.Remove(&child);
1382                 if (IsFailed(listResult))
1383                 {
1384                         SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
1385                 }
1386                 return r;
1387         }
1388
1389         SysAssert(GetLastResult() == E_SUCCESS);
1390
1391         return E_SUCCESS;
1392 }
1393
1394 result
1395 _Control::DetachSystemChild(_Control& child)
1396 {
1397         return DetachChild(child);
1398 }
1399
1400 bool
1401 _Control::HasParent(void) const
1402 {
1403         return __pParent != null;
1404 }
1405
1406 _ControlArea
1407 _Control::GetArea(void) const
1408 {
1409         ClearLastResult();
1410         return __area;
1411 }
1412
1413 _ControlLayer
1414 _Control::GetLayer(void) const
1415 {
1416         ClearLastResult();
1417         return __layer;
1418 }
1419
1420 void
1421 _Control::SetLayer(_ControlLayer layer)
1422 {
1423         ClearLastResult();
1424         _VisualElementImpl* pImpl = _VisualElementImpl::GetInstance(*GetVisualElement());
1425
1426         result r = pImpl->SetZOrderGroup(GetZOrderGroupOfVisualElement(layer));
1427         __layer = layer;
1428
1429         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1430 }
1431
1432 const _Control::ControlList&
1433 _Control::GetChildList() const
1434 {
1435         return const_cast <_Control*>(this)->GetChildList();
1436 }
1437
1438 _Control::ControlList&
1439 _Control::GetChildList()
1440 {
1441         return *__pChildren;
1442 }
1443
1444 bool
1445 _Control::IsCalledCallAttachingToMainTree(void)
1446 {
1447         return __isCalledCallOnAttachingToMainTree;
1448 }
1449
1450 void
1451 _Control::SetCalledCallAttachingToMainTree(bool isAttaching)
1452 {
1453         __isCalledCallOnAttachingToMainTree = isAttaching;
1454 }
1455
1456 bool
1457 _Control::IsCalledCallPreAttachedToMainTree(void)
1458 {
1459         return __isCalledCallOnPreAttachedToMainTree;
1460 }
1461
1462 bool
1463 _Control::IsCalledCallAttachedToMainTree(void)
1464 {
1465         return __isCalledCallOnAttachedToMainTree;
1466 }
1467
1468 void
1469 _Control::SetCalledCallPreAttachedToMainTree(bool isAttached)
1470 {
1471         __isCalledCallOnPreAttachedToMainTree = isAttached;
1472 }
1473
1474 void
1475 _Control::SetCalledCallAttachedToMainTree(bool isAttached)
1476 {
1477         __isCalledCallOnAttachedToMainTree = isAttached;
1478 }
1479
1480 result
1481 _Control::CallOnAttachingToMainTree(_Control& control)
1482 {
1483         result r = E_SUCCESS;
1484
1485         ControlList& children = control.GetChildList();
1486         _Control* pChild = null;
1487
1488         int childrenCount = children.GetCount();
1489
1490         for (int index = 0; index < childrenCount; index++)
1491         {
1492                 r = children.GetAt(index, pChild);
1493                 if (IsFailed(r))
1494                 {
1495                         SysAssert(r == E_OUT_OF_RANGE);
1496                         SysTryReturn(NID_UI,
1497                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1498                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1499                 }
1500                 if (!pChild->IsCalledCallAttachingToMainTree())
1501                 {
1502                         r = CallOnAttachingToMainTree(*pChild);
1503                         pChild->SetCalledCallAttachingToMainTree(true);
1504                         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1505                 }
1506         }
1507
1508         if (!control.IsCalledCallAttachingToMainTree())
1509         {
1510                 r = control.GetControlDelegate().OnAttachingToMainTree(this);
1511                 control.SetCalledCallAttachingToMainTree(true);
1512                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1513         }
1514
1515         return r;
1516 }
1517
1518 result
1519 _Control::CallOnPreAttachedToMainTree(_Control& control)
1520 {
1521         result r = E_SUCCESS;
1522
1523         ControlList& children = control.GetChildList();
1524         _Control* pChild = null;
1525
1526         int childrenCount = children.GetCount();
1527
1528         for (int index = 0; index < childrenCount; index++)
1529         {
1530                 r = children.GetAt(index, pChild);
1531                 if (IsFailed(r))
1532                 {
1533                         SysAssert(r == E_OUT_OF_RANGE);
1534                         SysTryReturn(NID_UI,
1535                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1536                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1537                 }
1538
1539                 if (!pChild->IsCalledCallPreAttachedToMainTree())
1540                 {
1541                         r = CallOnPreAttachedToMainTree(*pChild);
1542                         pChild->SetCalledCallPreAttachedToMainTree(true);
1543                 }
1544         }
1545
1546         if (!control.IsCalledCallPreAttachedToMainTree())
1547         {
1548                 r = control.GetControlDelegate().OnPreAttachedToMainTree();
1549                 control.SetCalledCallPreAttachedToMainTree(true);
1550         }
1551
1552         return r;
1553 }
1554
1555 result
1556 _Control::CallOnAttachedToMainTree(_Control& control)
1557 {
1558         result r = E_SUCCESS;
1559
1560         ControlList& children = control.GetChildList();
1561         _Control* pChild = null;
1562
1563         int childrenCount = children.GetCount();
1564
1565         for (int index = 0; index < childrenCount; index++)
1566         {
1567                 r = children.GetAt(index, pChild);
1568                 if (IsFailed(r))
1569                 {
1570                         SysAssert(r == E_OUT_OF_RANGE);
1571                         SysTryReturn(NID_UI,
1572                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1573                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1574                 }
1575
1576                 if (!pChild->IsCalledCallAttachedToMainTree())
1577                 {
1578                         r = CallOnAttachedToMainTree(*pChild);
1579                         pChild->SetCalledCallAttachedToMainTree(true);
1580                 }
1581         }
1582
1583         if (!control.IsCalledCallAttachedToMainTree())
1584         {
1585                 r = control.GetControlDelegate().OnAttachedToMainTree();
1586                 control.SetCalledCallAttachedToMainTree(true);
1587         }
1588
1589         return r;
1590 }
1591
1592 result
1593 _Control::CallOnDetachingFromMainTree(_Control& control)
1594 {
1595         result r = E_SUCCESS;
1596
1597         ControlList& children = control.GetChildList();
1598         _Control* pChild = null;
1599
1600         _Window* pTop = control.GetRootWindow();
1601         if (pTop)
1602         {
1603                 _Control* pControl = pTop->GetFocusControl(this);
1604                 if ((&control) == pControl)
1605                 {
1606                         pTop->SetFocusControl(&control, false);
1607                 }
1608                 _Control* pFocusTraversalControl = pTop->GetFocusTraversalControl(this);
1609                 if ((&control) == pFocusTraversalControl)
1610                 {
1611                         pTop->SetFocusTraversalControl(&control, false);
1612                 }
1613         }
1614
1615         if (!__isPostOrderTraversal)
1616         {
1617                 r = control.GetControlDelegate().OnDetachingFromMainTree();
1618                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1619         }
1620
1621         int childrenCount = children.GetCount();
1622
1623         for (int index = 0; index < childrenCount; index++)
1624         {
1625                 r = children.GetAt(index, pChild);
1626                 if (IsFailed(r))
1627                 {
1628                         SysAssert(r == E_OUT_OF_RANGE);
1629                         SysTryReturn(NID_UI,
1630                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1631                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1632                 }
1633                 r = CallOnDetachingFromMainTree(*pChild);
1634                 pChild->SetCalledCallAttachingToMainTree(false);
1635                 pChild->SetCalledCallPreAttachedToMainTree(false);
1636                 pChild->SetCalledCallAttachedToMainTree(false);
1637                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1638         }
1639
1640         if (__isPostOrderTraversal)
1641         {
1642                 r = control.GetControlDelegate().OnDetachingFromMainTree();
1643                 control.SetCalledCallAttachingToMainTree(false);
1644                 control.SetCalledCallPreAttachedToMainTree(false);
1645                 control.SetCalledCallAttachedToMainTree(false);
1646                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1647         }
1648
1649         return r;
1650 }
1651
1652 void
1653 _Control::CallOnAncestorVisibleStateChanged(void)
1654 {
1655         struct _Visitor
1656                 : public Visitor
1657         {
1658                 _Visitor(_Control& parent)
1659                         : __parent(parent){}
1660
1661                 virtual VisitType Visit(_Control& control)
1662                 {
1663                         control.GetControlDelegate().OnAncestorVisibleStateChanged(__parent);
1664                         return VISIT_DOWNWARD;
1665                 }
1666
1667                 _Control& __parent;
1668         };
1669
1670         _Visitor visitor(*this);
1671         Accept(visitor);
1672 }
1673
1674 void
1675 _Control::CallOnAncestorEnableStateChanged(void)
1676 {
1677         struct _Visitor
1678                 : public Visitor
1679         {
1680                 _Visitor(_Control& parent)
1681                         : __parent(parent){}
1682
1683                 virtual VisitType Visit(_Control& control)
1684                 {
1685                         control.GetControlDelegate().OnAncestorEnableStateChanged(__parent);
1686                         return VISIT_DOWNWARD;
1687                 }
1688
1689                 _Control& __parent;
1690         };
1691
1692         _Visitor visitor(*this);
1693         Accept(visitor);
1694 }
1695
1696 void
1697 _Control::CallOnAncestorInputEnableStateChanged(void)
1698 {
1699         struct _Visitor
1700                 : public Visitor
1701         {
1702                 _Visitor(_Control& parent)
1703                         : __parent(parent){}
1704
1705                 virtual VisitType Visit(_Control& control)
1706                 {
1707                         control.GetControlDelegate().OnAncestorInputEnableStateChanged(__parent);
1708                         return VISIT_DOWNWARD;
1709                 }
1710
1711                 _Control& __parent;
1712         };
1713
1714         _Visitor visitor(*this);
1715         Accept(visitor);
1716 }
1717
1718 // E_INVALID_ARG
1719 // E_SYSTEM
1720 // [ToDo] Rollback is difficult.
1721 result
1722 _Control::StartAttaching(_Control& child, _ControlArea area)
1723 {
1724         result r = E_SUCCESS;
1725
1726         _Control* pOldParent = child.GetParent();
1727
1728         SysTryReturn(NID_UI,
1729                                 (pOldParent != this), E_INVALID_ARG,
1730                                 E_INVALID_ARG, "[E_INVALID_ARG] The child control is already attached to this container.");
1731
1732         SysTryReturn(NID_UI,
1733                                 pOldParent == null, E_INVALID_ARG,
1734                                 E_INVALID_ARG, "[E_INVALID_ARG] Unable to add the child which already has another parent.");
1735
1736         r = child.GetControlDelegate().OnAttaching(this);
1737         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1738
1739         if (IsAttachedToMainTree())
1740         {
1741                 r = CallOnAttachingToMainTree(child);
1742                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1743         }
1744
1745         // [ToDo] Add control to layout
1746         // What should we do about non-layoutable controls?
1747         if (area == _CONTROL_AREA_CLIENT)
1748         {
1749                 _ControlManager* pMgr = _ControlManager::GetInstance();
1750                 r = GetLastResult();
1751                 SysTryReturn(NID_UI, pMgr, r, r, "[%s] Propagating.", GetErrorMessage(r));
1752
1753                 r = __pLayoutContainer->AddItem(child.GetLayoutContainer());
1754                 if (IsFailed(r))
1755                 {
1756                         child.GetControlDelegate().OnAttachingFailed(*this);
1757                         SysLogException(NID_UI, E_SYSTEM, "[E_SYSTEM] Failed to apply layout.");
1758                         return E_SYSTEM;
1759                 }
1760         }
1761
1762         if (IsAttachedToMainTree())
1763         {
1764                 if (!(IsOrientationRoot() && child.IsOrientationRoot()))
1765                 {
1766                         child.ChangeLayout(_ControlManager::GetInstance()->GetOrientation());
1767                 }
1768         }
1769
1770         child.__area = area;
1771
1772         if (area == _CONTROL_AREA_CLIENT)
1773         {
1774                 if (child.GetLayer() != _CONTROL_LAYER_CLIENT_TOP && child.GetLayer() != _CONTROL_LAYER_CLIENT_BOTTOM)
1775                 {
1776                         child.SetLayer(_CONTROL_LAYER_CLIENT_MIDDLE);
1777                 }
1778         }
1779         else
1780         {
1781                 child.SetLayer(_CONTROL_LAYER_SYSTEM);
1782         }
1783
1784         SysAssert(GetLastResult() == E_SUCCESS);
1785         return E_SUCCESS;
1786 }
1787
1788 result
1789 _Control::EndAttaching(_Control& child)
1790 {
1791         child.SetParent(this);
1792         InvalidateHierarchyRootWindow(child);
1793         InvalidateHierarchyAbsoluteBounds(child);
1794
1795         FloatRectangle floatBounds(child.GetBoundsF().x, child.GetBoundsF().y, child.GetBoundsF().width, child.GetBoundsF().height);
1796
1797         result r = E_SUCCESS;
1798         if (child.IsLayoutChangable() == false)
1799         {
1800                 r = child.UpdateBoundsOfVisualElement(FloatRectangle(0.0f, 0.0f, floatBounds.width, floatBounds.height));
1801         }
1802         else
1803         {
1804                 r = child.UpdateBoundsOfVisualElement(floatBounds);
1805         }
1806         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1807
1808         r = child.GetControlDelegate().OnAttached();
1809         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1810
1811         if (IsAttachedToMainTree())
1812         {
1813                 r = CallOnPreAttachedToMainTree(child);
1814                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1815
1816                 r = CallOnAttachedToMainTree(child);
1817                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1818         }
1819
1820         ClearLastResult();
1821
1822         GetControlDelegate().OnChildAttached(child);
1823         ClearLastResult();
1824
1825         return E_SUCCESS;
1826 }
1827
1828 // E_INVALID_ARG
1829 // E_OUT_OF_MEMORY
1830 // E_SYSTEM
1831 result
1832 _Control::AttachChild(_Control& child)
1833 {
1834         ClearLastResult();
1835         result r = E_SUCCESS;
1836
1837         SysTryReturn(NID_UI,
1838                                 IsChildAttachable(child), E_INVALID_ARG,
1839                                 E_INVALID_ARG, "[E_INVALID_ARG] %ls cannot be a child of %ls.",
1840                                 child.GetName().GetPointer(), GetName().GetPointer());
1841
1842         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1843         if (IsFailed(r))
1844         {
1845                 return r;
1846         }
1847
1848         ControlList& children = GetChildList();
1849         r = children.Add(&child);
1850         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1851
1852         r = GetVisualElement()->AttachChild(*child.GetVisualElement());
1853
1854         r = EndAttaching(child);
1855         if (IsFailed(r))
1856         {
1857                 result listResult = children.Remove(&child);
1858                 if (IsFailed(listResult))
1859                 {
1860                         SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
1861                 }
1862                 return r;
1863         }
1864
1865         SysAssert(GetLastResult() == E_SUCCESS);
1866         UpdateFocusList();
1867         return E_SUCCESS;
1868 }
1869
1870 void
1871 _Control::UpdateFocusList(void)
1872 {
1873         _Window* pTop = GetRootWindow();
1874         if (pTop)
1875         {
1876                 pTop->ResetFocusList();
1877         }
1878 }
1879
1880 void
1881 _Control::RemoveFocusRing(void)
1882 {
1883         if (__pFocusVisualElement)
1884         {
1885                 __pFocusVisualElement->SetShowState(false);
1886         }
1887 }
1888
1889 bool
1890 _Control::HasFocusRing(void)
1891 {
1892         if (__pFocusVisualElement)
1893         {
1894                 return true;
1895         }
1896
1897         return false;
1898 }
1899
1900 void
1901 _Control::SetFocusNavigateEnabled(bool enable)
1902 {
1903         __isNavigatable = enable;
1904 }
1905
1906 bool
1907 _Control::IsFocusNavigateEnabled(void) const
1908 {
1909         return __isNavigatable;
1910 }
1911
1912 bool
1913 _Control::IsFocusModeStateEnabled(void) const
1914 {
1915         return _FocusManagerImpl::GetInstance()->IsFocusModeStateEnabled();
1916 }
1917
1918 // E_INVALID_ARG
1919 // E_OUT_OF_MEMORY
1920 // E_SYSTEM
1921 result
1922 _Control::InsertChildToBottom(_Control& child)
1923 {
1924         ClearLastResult();
1925         result r = E_SUCCESS;
1926
1927         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1928         if (IsFailed(r))
1929         {
1930                 return r;
1931         }
1932
1933         ControlList& children = GetChildList();
1934         r = children.InsertAt(&child, 0);
1935         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1936
1937         GetVisualElement()->InsertChild(*child.GetVisualElement(), null, false);
1938         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1939
1940         r = EndAttaching(child);
1941         if (IsFailed(r))
1942         {
1943                 result listResult = children.Remove(&child);
1944                 if (IsFailed(listResult))
1945                 {
1946                         SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
1947                 }
1948                 return r;
1949         }
1950
1951         SysAssert(GetLastResult() == E_SUCCESS);
1952         return E_SUCCESS;
1953 }
1954
1955 // E_INVALID_ARG
1956 // E_OUT_OF_MEMORY
1957 // E_SYSTEM
1958 result
1959 _Control::InsertChildAfter(const _Control& targetChild, _Control& child)
1960 {
1961         ClearLastResult();
1962         result r = E_SUCCESS;
1963
1964         SysTryReturn(NID_UI,
1965                                 targetChild.GetParent() == this, E_INVALID_ARG,
1966                                 E_INVALID_ARG, "[E_INVALID_ARG] The target is not my child.");
1967
1968         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1969         if (IsFailed(r))
1970         {
1971                 return r;
1972         }
1973
1974         int targetIndex = GetChildIndex(targetChild);
1975         SysAssert(targetIndex != -1);
1976
1977         ControlList& children = GetChildList();
1978         r = children.InsertAt(&child, targetIndex + 1);
1979         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1980
1981         GetVisualElement()->InsertChild(*child.GetVisualElement(), targetChild.GetVisualElement(), true);
1982         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1983
1984         r = EndAttaching(child);
1985         if (IsFailed(r))
1986         {
1987                 result listResult = children.Remove(&child);
1988                 if (IsFailed(listResult))
1989                 {
1990                         SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
1991                 }
1992                 return r;
1993         }
1994
1995         SysAssert(GetLastResult() == E_SUCCESS);
1996         return E_SUCCESS;
1997 }
1998
1999 // E_INVALID_ARG
2000 // E_OUT_OF_MEMORY
2001 // E_SYSTEM
2002 result
2003 _Control::InsertChildBefore(const _Control& targetChild, _Control& child)
2004 {
2005         ClearLastResult();
2006         result r = E_SUCCESS;
2007
2008         SysTryReturn(NID_UI,
2009                                 targetChild.GetParent() == this, E_INVALID_ARG,
2010                                 E_INVALID_ARG, "[E_INVALID_ARG] The target is not my child.");
2011
2012         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
2013         if (IsFailed(r))
2014         {
2015                 return r;
2016         }
2017
2018         int targetIndex = GetChildIndex(targetChild);
2019         SysAssert(targetIndex != -1);
2020
2021         ControlList& children = GetChildList();
2022         r = children.InsertAt(&child, targetIndex);
2023         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2024
2025         GetVisualElement()->InsertChild(*child.GetVisualElement(), targetChild.GetVisualElement(), false);
2026         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2027
2028         r = EndAttaching(child);
2029         if (IsFailed(r))
2030         {
2031                 result listResult = children.Remove(&child);
2032                 if (IsFailed(listResult))
2033                 {
2034                         SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
2035                 }
2036                 return r;
2037         }
2038
2039         SysAssert(GetLastResult() == E_SUCCESS);
2040         return E_SUCCESS;
2041 }
2042
2043 // E_SYSTEM
2044 result
2045 _Control::DetachChild(_Control& child)
2046 {
2047         ClearLastResult();
2048         result r = E_SUCCESS;
2049
2050         if (child.GetParent() != this)
2051         {
2052                 SetLastResult(E_INVALID_ARG);
2053                 return E_INVALID_ARG;
2054         }
2055
2056         if (IsAttachedToMainTree())
2057         {
2058                 r = CallOnDetachingFromMainTree(child);
2059                 SysTryReturn(NID_UI,
2060                                         r == E_SUCCESS, E_SYSTEM,
2061                                         E_SYSTEM, "[E_SYSTEM] The child cannot detached from parent.");
2062
2063                 SysTryReturn(NID_UI,
2064                                         child.GetControlDelegate().OnDetaching() == E_SUCCESS, E_SYSTEM,
2065                                         E_SYSTEM, "[E_SYSTEM] The child cannot detached from parent.");
2066         }
2067
2068         GetControlDelegate().OnChildDetaching(child);
2069         ClearLastResult();
2070
2071         r = GetVisualElement()->DetachChild(*child.GetVisualElement());
2072         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2073
2074         ControlList& children = GetChildList();
2075         r = children.Remove(&child);
2076         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2077
2078         // Remove control to layout
2079         if (child.__area == _CONTROL_AREA_CLIENT)
2080         {
2081                 _ControlManager* pMgr = _ControlManager::GetInstance();
2082                 r = GetLastResult();
2083                 SysTryReturn(NID_UI, pMgr, r, r, "[%s] Propagating.", GetErrorMessage(r));
2084
2085                 r = __pLayoutContainer->RemoveItem(child.GetLayoutContainer());
2086                 SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2087         }
2088
2089         child.SetParent(null);
2090         child.__area = _CONTROL_AREA_NONE;
2091         child.__layer = _CONTROL_LAYER_NONE;
2092
2093         GetControlDelegate().OnChildDetached(child);
2094         ClearLastResult();
2095
2096         SysAssert(GetLastResult() == E_SUCCESS);
2097         UpdateFocusList();
2098         InvalidateHierarchyRootWindow(child);
2099
2100         return E_SUCCESS;
2101 }
2102
2103 // Always success
2104 void
2105 _Control::DetachAllChildren(bool detachSystemChild, bool recursive)
2106 {
2107         ClearLastResult();
2108         result r = E_SUCCESS;
2109
2110         int childCount = GetChildCount();
2111         int itemIndex = 0;
2112
2113         if (recursive)
2114         {
2115                 while (childCount--)
2116                 {
2117                         _Control* pChild = GetChild(itemIndex);
2118                         if (pChild == null)
2119                         {
2120                                 continue;
2121                         }
2122
2123                         if (!detachSystemChild && (pChild->GetArea() == _CONTROL_AREA_SYSTEM))
2124                         {
2125                                 itemIndex++;
2126                                 continue;
2127                         }
2128
2129                         int childCount = pChild->GetChildCount();
2130                         if (childCount == 0)
2131                         {
2132                                 r = DetachChild(*pChild);
2133                                 if (IsFailed(r))
2134                                 {
2135                                         SysLog(NID_UI, "child is not detached from this container.");
2136                                 }
2137                         }
2138                         else
2139                         {
2140                                 r = DetachChild(*pChild);
2141                                 if (IsFailed(r))
2142                                 {
2143                                         SysLog(NID_UI, "child is not detached from this container.");
2144                                 }
2145                                 pChild->DetachAllChildren(detachSystemChild, true);
2146                         }
2147                 }
2148         }
2149         else
2150         {
2151                 int notDetachedChildCount = 0;
2152                 while (childCount--)
2153                 {
2154                         _Control* pChild = GetChild(itemIndex);
2155                         if (pChild == null)
2156                         {
2157                                 continue;
2158                         }
2159
2160                         if (!detachSystemChild && (pChild->GetArea() == _CONTROL_AREA_SYSTEM))
2161                         {
2162                                 itemIndex++;
2163                                 continue;
2164                         }
2165                         r = DetachChild(*pChild);
2166
2167                         if (IsFailed(r))
2168                         {
2169                                 ++notDetachedChildCount;
2170                         }
2171                 }
2172
2173                 if (notDetachedChildCount > 0)
2174                 {
2175                         SysLog(NID_UI, "%d children are not detached from this container.", notDetachedChildCount);
2176                 }
2177         }
2178
2179         ClearLastResult();
2180 }
2181
2182 // E_INVALID_ARG
2183 result
2184 _Control::MoveChildToTop(const _Control& child)
2185 {
2186         ClearLastResult();
2187         result r = E_SUCCESS;
2188
2189         SysTryReturn(NID_UI,
2190                                 child.GetParent() == this, E_INVALID_ARG,
2191                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
2192
2193         // If already on top,
2194         ControlList& children = GetChildList();
2195         if (GetChildIndex(child) == children.GetCount() - 1)
2196         {
2197                 SysAssert(GetLastResult() == E_SUCCESS);
2198                 return E_SUCCESS;
2199         }
2200
2201         _Control* pChild = const_cast <_Control*>(&child);
2202
2203         r = children.Remove(pChild);
2204         SysAssert(r == E_SUCCESS);
2205
2206         r = children.Add(pChild);
2207         SysAssert(r == E_SUCCESS);
2208
2209         r = child.GetVisualElement()->SetZOrder(null, true);
2210         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2211
2212         SysAssert(GetLastResult() == E_SUCCESS);
2213         return E_SUCCESS;
2214 }
2215
2216 // E_INVALID_ARG
2217 result
2218 _Control::MoveChildToBottom(const _Control& child)
2219 {
2220         ClearLastResult();
2221         result r = E_SUCCESS;
2222
2223         SysTryReturn(NID_UI,
2224                                 child.GetParent() == this, E_INVALID_ARG,
2225                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
2226
2227         ControlList& children = GetChildList();
2228
2229         if (GetChildIndex(child) == 0)
2230         {
2231                 SysAssert(GetLastResult() == E_SUCCESS);
2232                 return E_SUCCESS;
2233         }
2234
2235         _Control* pChild = const_cast <_Control*>(&child);
2236
2237         r = children.Remove(pChild);
2238         SysAssert(r == E_SUCCESS);
2239
2240         r = children.InsertAt(pChild, 0);
2241         SysAssert(r == E_SUCCESS);
2242
2243         r = child.GetVisualElement()->SetZOrder(null, false);
2244         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2245
2246         SysAssert(GetLastResult() == E_SUCCESS);
2247         return E_SUCCESS;
2248 }
2249
2250 // E_INVALID_ARG
2251 result
2252 _Control::MoveChildAfter(const _Control& targetChild, const _Control& child)
2253 {
2254         ClearLastResult();
2255         result r = E_SUCCESS;
2256
2257         SysTryReturn(NID_UI,
2258                                 targetChild.GetParent() == this, E_INVALID_ARG,
2259                                 E_INVALID_ARG, "[E_INVALID_ARG] Target is not my child.");
2260
2261         SysTryReturn(NID_UI,
2262                                 child.GetParent() == this, E_INVALID_ARG,
2263                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
2264
2265         SysTryReturn(NID_UI,
2266                                 &targetChild != &child, E_INVALID_ARG,
2267                                 E_INVALID_ARG, "[E_INVALID_ARG] Cannot move after self.");
2268
2269         ControlList& children = GetChildList();
2270
2271         int targetIndex = GetChildIndex(targetChild);
2272         SysAssert(targetIndex != -1);
2273
2274         if (targetIndex + 1 == GetChildIndex(child))
2275         {
2276                 SysAssert(GetLastResult() == E_SUCCESS);
2277                 return E_SUCCESS;
2278         }
2279
2280         _Control* pChild = const_cast <_Control*>(&child);
2281
2282         r = children.Remove(pChild);
2283         SysAssert(r == E_SUCCESS);
2284
2285         r = children.InsertAt(pChild, targetIndex + 1);
2286         SysAssert(r == E_SUCCESS);
2287
2288         r = child.GetVisualElement()->SetZOrder(targetChild.GetVisualElement(), true);
2289         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2290
2291         SysAssert(GetLastResult() == E_SUCCESS);
2292         return E_SUCCESS;
2293 }
2294
2295 // E_INVALID_ARG
2296 result
2297 _Control::MoveChildBefore(const _Control& targetChild, const _Control& child)
2298 {
2299         ClearLastResult();
2300         result r = E_SUCCESS;
2301
2302         SysTryReturn(NID_UI,
2303                                 targetChild.GetParent() == this, E_INVALID_ARG,
2304                                 E_INVALID_ARG, "[E_INVALID_ARG] Target is not my child.");
2305
2306         SysTryReturn(NID_UI,
2307                                 child.GetParent() == this, E_INVALID_ARG,
2308                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
2309
2310         SysTryReturn(NID_UI,
2311                                 &targetChild != &child, E_INVALID_ARG,
2312                                 E_INVALID_ARG, "[E_INVALID_ARG] Cannot move before self.");
2313
2314         ControlList& children = GetChildList();
2315
2316         int targetIndex = GetChildIndex(targetChild);
2317         SysAssert(targetIndex != -1);
2318
2319         if (targetIndex - 1 == GetChildIndex(child))
2320         {
2321                 SysAssert(GetLastResult() == E_SUCCESS);
2322                 return E_SUCCESS;
2323         }
2324
2325         _Control* pChild = const_cast <_Control*>(&child);
2326
2327         r = children.Remove(pChild);
2328         SysAssert(r == E_SUCCESS);
2329
2330         r = children.InsertAt(pChild, targetIndex);
2331         SysAssert(r == E_SUCCESS);
2332
2333         r = child.GetVisualElement()->SetZOrder(targetChild.GetVisualElement(), false);
2334         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2335
2336         SysAssert(GetLastResult() == E_SUCCESS);
2337         return E_SUCCESS;
2338 }
2339
2340 // E_INVALID_ARG
2341 // E_OBJ_NOT_FOUND
2342 int
2343 _Control::GetChildIndex(const _Control& child) const
2344 {
2345         ClearLastResult();
2346         result r = E_SUCCESS;
2347
2348         SysTryReturn(NID_UI,
2349                                 child.GetParent() == this, -1,
2350                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
2351
2352         const ControlList& children = GetChildList();
2353
2354         int index = -1;
2355         r = children.IndexOf(const_cast<_Control*>(&child), index);
2356         if (IsFailed(r))
2357         {
2358                 SysAssert(r == E_OBJ_NOT_FOUND);
2359                 SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
2360                 return -1;
2361         }
2362
2363         SysAssert(GetLastResult() == E_SUCCESS);
2364         return index;
2365 }
2366
2367 // E_OUT_OF_RANGE
2368 _Control*
2369 _Control::GetChild(int index) const
2370 {
2371         ClearLastResult();
2372         result r = E_SUCCESS;
2373
2374         const ControlList& children = GetChildList();
2375
2376         _Control* pChild = null;
2377         r = children.GetAt(index, pChild);
2378         if (IsFailed(r))
2379         {
2380                 SysAssert(r == E_OUT_OF_RANGE);
2381                 SysLogException(NID_UI, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
2382                 return null;
2383         }
2384
2385         SysAssert(GetLastResult() == E_SUCCESS);
2386         return pChild;
2387 }
2388
2389 int
2390 _Control::GetChildCount(void) const
2391 {
2392         ClearLastResult();
2393         return GetChildList().GetCount();
2394 }
2395
2396 _ControlHandle
2397 _Control::GetHandle(void) const
2398 {
2399         return __controlHandle;
2400 }
2401
2402 void*
2403 _Control::GetUserData(void) const
2404 {
2405         return __pUserData;
2406 }
2407
2408 void
2409 _Control::SetUserData(void* pUserData)
2410 {
2411         __pUserData = pUserData;
2412 }
2413
2414 Variant
2415 _Control::GetPropertyName(void) const
2416 {
2417         ClearLastResult();
2418         return Tizen::Ui::Variant(__name);
2419 }
2420
2421 String
2422 _Control::GetName(void) const
2423 {
2424         Variant name = GetProperty("Name");
2425
2426         return name.ToString();
2427 }
2428
2429 result
2430 _Control::SetPropertyName(const Variant& name)
2431 {
2432         ClearLastResult();
2433         __name = name.ToString();
2434
2435         return E_SUCCESS;
2436 }
2437
2438 void
2439 _Control::SetName(const String& name)
2440 {
2441         SetProperty("Name", Variant(name));
2442 }
2443
2444 _Control*
2445 _Control::GetParent(void) const
2446 {
2447         return __pParent;
2448 }
2449
2450 Canvas*
2451 _Control::GetCanvasN(void) const
2452 {
2453         return GetCanvasN(FloatRectangle(0.0f, 0.0f, __bounds.width, __bounds.height));
2454 }
2455
2456 Canvas*
2457 _Control::GetCanvasN(const Rectangle& bounds) const
2458 {
2459         ClearLastResult();
2460         Canvas *pCanvas = GetControlDelegate().OnCanvasRequestedN(FloatRectangle(bounds.x, bounds.y, bounds.width, bounds.height));
2461         if (pCanvas == null)
2462         {
2463                 GetVisualElement()->SetFlushNeeded();
2464                 pCanvas = GetVisualElement()->GetCanvasN(bounds);
2465                 if (IsFailed(GetLastResult()))
2466                 {
2467                         SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
2468                 }
2469
2470                 if (pCanvas && !__isCalledGetCanvasN)
2471                 {
2472                         pCanvas->SetBackgroundColor(GetBackgroundColor());
2473                         pCanvas->Clear();
2474                         const_cast <_Control*>(this)->__isCalledGetCanvasN = true;
2475                 }
2476         }
2477         return pCanvas;
2478 }
2479
2480 Canvas*
2481 _Control::GetCanvasN(const FloatRectangle& bounds) const
2482 {
2483         ClearLastResult();
2484         Canvas *pCanvas = GetControlDelegate().OnCanvasRequestedN(FloatRectangle(bounds));
2485         if (pCanvas == null)
2486         {
2487                 GetVisualElement()->SetFlushNeeded();
2488                 pCanvas = GetVisualElement()->GetCanvasN(bounds);
2489                 if (IsFailed(GetLastResult()))
2490                 {
2491                         SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
2492                 }
2493
2494                 if (pCanvas && !__isCalledGetCanvasN)
2495                 {
2496                         pCanvas->SetBackgroundColor(GetBackgroundColor());
2497                         pCanvas->Clear();
2498                         const_cast <_Control*>(this)->__isCalledGetCanvasN = true;
2499                 }
2500         }
2501         return pCanvas;
2502 }
2503
2504 bool
2505 _Control::IsCalledGetCanvasN(void) const
2506 {
2507         return __isCalledGetCanvasN;
2508 }
2509
2510 Canvas*
2511 _Control::GetClientCanvasN(void) const
2512 {
2513         return GetCanvasN(GetClientBounds());
2514 }
2515
2516 bool
2517 _Control::IsAncestorOf(const _Control& control) const
2518 {
2519         ClearLastResult();
2520
2521         const _Control* pParent = control.GetParent();
2522         if (!pParent)
2523         {
2524                 return false;
2525         }
2526
2527         struct _Visitor
2528                 : public Visitor
2529         {
2530 private:
2531                 const _Control& __ancestor;
2532
2533 public:
2534                 bool yes;
2535
2536                 _Visitor(const _Control& ancestor)
2537                         : __ancestor(ancestor)
2538                         , yes(false){}
2539
2540                 virtual VisitType Visit(_Control& control)
2541                 {
2542                         if (&__ancestor == &control)
2543                         {
2544                                 yes = true;
2545                                 return VISIT_STOP;
2546                         }
2547                         else
2548                         {
2549                                 return VISIT_UPWARD;
2550                         }
2551                 }
2552         };
2553
2554         _Visitor visitor(*this);
2555         pParent->Accept(visitor);
2556
2557         SysAssert(GetLastResult() == E_SUCCESS);
2558         return visitor.yes;
2559 }
2560
2561 _Window*
2562 _Control::GetRootWindow(void) const
2563 {
2564         ClearLastResult();
2565
2566         if (!__needRecalcRootWindow && __pRootWindow != this)
2567         {
2568                 return __pRootWindow;
2569         }
2570
2571         struct _Visitor
2572                 : public Visitor
2573         {
2574                 _Window* pRoot;
2575
2576                 _Visitor(void)
2577                         : pRoot(null){}
2578
2579                 virtual VisitType Visit(_Control& control)
2580                 {
2581                         pRoot = dynamic_cast <_Window*>(&control);
2582                         if (pRoot != null)
2583                         {
2584                                 return VISIT_STOP;
2585                         }
2586
2587                         return VISIT_UPWARD;
2588                 }
2589         };
2590
2591         _Visitor visitor;
2592         Accept(visitor);
2593
2594         SysAssert(GetLastResult() == E_SUCCESS);
2595
2596         const_cast<_Control*>(this)->__pRootWindow = visitor.pRoot;
2597         const_cast<_Control*>(this)->__needRecalcRootWindow = false;
2598
2599         return visitor.pRoot;
2600 }
2601
2602 bool
2603 _Control::IsAttachedToMainTree(void) const
2604 {
2605         ClearLastResult();
2606
2607         _ControlManager* pMgr = _ControlManager::GetInstance();
2608         if (pMgr == null)
2609         {
2610                 return false;
2611         }
2612
2613         _Window* pRootWindow = GetRootWindow();
2614         if (pRootWindow == null)
2615         {
2616                 return false;
2617         }
2618
2619         return pRootWindow->IsAttached();
2620 }
2621
2622 bool
2623 _Control::IsFocusable(void) const
2624 {
2625         ClearLastResult();
2626         return __focusable;
2627 }
2628
2629 void
2630 _Control::SetFocusable(bool focusable)
2631 {
2632         ClearLastResult();
2633         bool oldState = __focusable;
2634         __focusable = focusable;
2635         if (oldState != __focusable)
2636         {
2637                 GetControlDelegate().OnFocusableStateChanged(focusable);
2638         }
2639 }
2640
2641 bool
2642 _Control::IsNativeObjectFocusable(void) const
2643 {
2644         ClearLastResult();
2645         return __nativeObjectFocusable;
2646 }
2647
2648 void
2649 _Control::SetNativeObjectFocusable(bool focusable)
2650 {
2651         ClearLastResult();
2652         __nativeObjectFocusable = focusable;
2653 }
2654
2655 bool
2656 _Control::IsFocused(void) const
2657 {
2658         ClearLastResult();
2659
2660         _Window* pTop = GetRootWindow();
2661         if (pTop == null)
2662         {
2663                 return false;
2664         }
2665
2666         if (this == pTop->GetFocusControl(const_cast<_Control*>(this)))
2667         {
2668                 return true;
2669         }
2670
2671         return false;
2672 }
2673
2674 void
2675 _Control::SetFocusWindowActivationChecked(bool isChecked)
2676 {
2677         __isFocusWindowActivationChecked = isChecked;
2678 }
2679
2680 bool
2681 _Control::IsFocusWindowActivationChecked(void)
2682 {
2683         return __isFocusWindowActivationChecked;
2684 }
2685
2686 result
2687 _Control::SetFocused(bool on)
2688 {
2689         ClearLastResult();
2690
2691         SysTryReturn(NID_UI,
2692                                 IsAttachedToMainTree(), E_INVALID_OPERATION,
2693                                 E_INVALID_OPERATION, "[E_INVALID_OPERATION] This control should have a parent or be attached to the main tree.");
2694
2695         SysTryReturn(NID_UI,
2696                                 IsFocusable(), E_INVALID_OPERATION,
2697                                 E_INVALID_OPERATION, "[E_INVALID_OPERATION] This Control isn't focusable control.");
2698
2699         _Window* pTop = GetRootWindow();
2700         SysAssert(pTop);
2701         if (on)
2702         {
2703                 pTop->SetFocusControl(this, true);
2704         }
2705         else
2706         {
2707                 pTop->SetFocusControl(this, false);
2708         }
2709
2710         return E_SUCCESS;
2711 }
2712
2713 result
2714 _Control::SetFont(const String& fontName)
2715 {
2716         result r = E_SUCCESS;
2717
2718         if (__fontName.Equals(fontName))
2719         {
2720                 return E_SUCCESS;
2721         }
2722
2723         __isControlFontChanged = true;
2724         __fontName = fontName;
2725         __fontFileName.Clear();
2726
2727         Font* pFont = GetFallbackFont();
2728
2729         if (pFont == null)
2730         {
2731                 r = GetLastResult();
2732                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
2733         }
2734         return E_SUCCESS;
2735 }
2736
2737 Font*
2738 _Control::GetFallbackFont(void)
2739 {
2740         unsigned long style = 0;
2741         int textSize = 0;
2742         float floatTextSize = 0.0f;
2743         result r = E_SUCCESS;
2744         _IControlDelegate& delegate = GetControlDelegate();
2745         delegate.OnFontInfoRequested(style, textSize);
2746         delegate.OnFontInfoRequested(style, floatTextSize);
2747         _ControlManager* pControlManager = _ControlManager::GetInstance();
2748         _FontImpl* pFontImpl  = _FontImpl::GetInstance(*__pFont);
2749         if (!(__isControlFontChanged || pControlManager->IsDefaultFontChanged() || pControlManager->IsSystemFontChanged())
2750                 && __pFont != null
2751                 && ((__pFont->GetSize() == textSize || __pFont->GetSizeF() == floatTextSize))
2752                 && (pFontImpl->GetStyle() == static_cast<int>(style)))
2753         {
2754                 return __pFont;
2755         }
2756         unique_ptr<Font>pTempFont(new(std::nothrow)Font());
2757         SysTryReturn(NID_UI , pTempFont , null , E_OUT_OF_MEMORY, "[%s] Fails to create a __pFont.", GetErrorMessage(r));
2758
2759         if (!__fontName.IsEmpty())
2760         {
2761                 __isControlFontChanged = false;
2762                 _FontImpl* pFontImpl  = _FontImpl::GetInstance(*pTempFont);
2763                 SysAssertf(pFontImpl != null, "Getting _FontImpl instance failed.");
2764
2765                 if (floatTextSize > 0.0f)
2766                 {
2767                         r = pFontImpl->Construct(__fontName, style, floatTextSize, false);
2768                 }
2769                 else
2770                 {
2771                         r = pFontImpl->Construct(__fontName, style, textSize, false);
2772                 }
2773
2774                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2775                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2776         }
2777         else if (!__fontFileName.IsEmpty())
2778         {
2779                 __isControlFontChanged = false;
2780                 _FontImpl* pFontImpl  = _FontImpl::GetInstance(*pTempFont);
2781                 SysAssertf(pFontImpl != null, "Getting _FontImpl instance failed.");
2782
2783                 if (floatTextSize > 0.0f)
2784                 {
2785                         r = pFontImpl->Construct(__fontFileName, style, floatTextSize);
2786                 }
2787                 else
2788                 {
2789                         r = pFontImpl->Construct(__fontFileName, style, textSize);
2790                 }
2791                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2792                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2793
2794         }
2795         else if (!pControlManager->GetDefaultFont().IsEmpty())
2796         {
2797                 _FontImpl* pFontImpl = _FontImpl::GetInstance(*pTempFont);
2798                 SysAssertf(pFontImpl != null, "Getting _FontImpl instance failed.");
2799
2800                 if (floatTextSize > 0.0f)
2801                 {
2802                         r = pFontImpl->Construct(pControlManager->GetDefaultFont(), style, floatTextSize, false);
2803                 }
2804                 else
2805                 {
2806                         r = pFontImpl->Construct(pControlManager->GetDefaultFont(), style, textSize, false);
2807                 }
2808                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2809                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2810         }
2811         else if (!pControlManager->GetDefaultFontFile().IsEmpty())
2812         {
2813                 _FontImpl* pFontImpl = _FontImpl::GetInstance(*pTempFont);
2814                 SysAssertf(pFontImpl != null, "Getting _FontImpl instance failed.");
2815
2816                 if (floatTextSize > 0.0f)
2817                 {
2818                         r = pFontImpl->Construct(pControlManager->GetDefaultFontFile(), style, floatTextSize);
2819                 }
2820                 else
2821                 {
2822                         r = pFontImpl->Construct(pControlManager->GetDefaultFontFile(), style, textSize);
2823                 }
2824                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2825                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2826         }
2827         else
2828         {
2829                 if (floatTextSize > 0.0f)
2830                 {
2831                         r = pTempFont->Construct(style, floatTextSize);
2832                 }
2833                 else
2834                 {
2835                         r = pTempFont->Construct(style, textSize);
2836                 }
2837                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2838                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2839         }
2840
2841         if (__pFont)
2842         {
2843                 delete __pFont;
2844                 __pFont = null;
2845         }
2846         __pFont = pTempFont.release();
2847         delegate.OnFontChanged(__pFont);
2848         return  __pFont;
2849 }
2850
2851 String
2852 _Control::GetFont(void) const
2853 {
2854         return __fontName;
2855 }
2856
2857 bool
2858 _Control::IsEnabled(void) const
2859 {
2860         ClearLastResult();
2861
2862         struct _Visitor
2863                 : public Visitor
2864         {
2865                 bool enabled;
2866
2867                 _Visitor(void)
2868                         : enabled(true){}
2869
2870                 virtual VisitType Visit(_Control& control)
2871                 {
2872                         if (!control.GetEnableState())
2873                         {
2874                                 enabled = false;
2875                                 return VISIT_STOP;
2876                         }
2877
2878                         return VISIT_UPWARD;
2879                 }
2880         };
2881
2882         _Visitor visitor;
2883         Accept(visitor);
2884         return visitor.enabled;
2885 }
2886
2887 bool
2888 _Control::GetEnableState(void) const
2889 {
2890         ClearLastResult();
2891         return __enabledState;
2892 }
2893
2894 void
2895 _Control::SetEnableState(bool enabledState)
2896 {
2897         ClearLastResult();
2898         const bool changed = (__enabledState != enabledState);
2899         if (changed)
2900         {
2901                 __enabledState = enabledState;
2902                 CallOnAncestorEnableStateChanged();
2903         }
2904         __pAccessibilityContainer->SetEnableState(enabledState);
2905 }
2906
2907 bool
2908 _Control::IsInputEventEnabled(void) const
2909 {
2910         ClearLastResult();
2911
2912         struct _Visitor
2913                 : public Visitor
2914         {
2915                 bool inputEnabled;
2916
2917                 _Visitor(void)
2918                         : inputEnabled(true){}
2919
2920                 virtual VisitType Visit(_Control& control)
2921                 {
2922                         if (!control.GetInputEnableState())
2923                         {
2924                                 inputEnabled = false;
2925                                 return VISIT_STOP;
2926                         }
2927
2928                         return VISIT_UPWARD;
2929                 }
2930         };
2931
2932         _Visitor visitor;
2933         Accept(visitor);
2934         return visitor.inputEnabled;
2935 }
2936
2937 bool
2938 _Control::GetInputEnableState(void) const
2939 {
2940         ClearLastResult();
2941
2942         if (__inputLockRefCount != 0)
2943         {
2944                 return false;
2945         }
2946         else
2947         {
2948                 return true;
2949         }
2950 }
2951
2952 void
2953 _Control::LockInputEvent(void)
2954 {
2955         __inputLockRefCount++;
2956         CallOnAncestorInputEnableStateChanged();
2957 }
2958
2959 void
2960 _Control::UnlockInputEvent(void)
2961 {
2962         __inputLockRefCount--;
2963         if (__inputLockRefCount < 0)
2964         {
2965                 __inputLockRefCount = 0;
2966         }
2967 }
2968
2969 bool
2970 _Control::IsVisible(void) const
2971 {
2972         ClearLastResult();
2973
2974         if (IsAttachedToMainTree() == false)
2975         {
2976                 SetLastResult(E_SYSTEM);
2977                 return false;
2978         }
2979
2980         struct _Visitor
2981                 : public Visitor
2982         {
2983                 bool visible;
2984
2985                 _Visitor(void)
2986                         : visible(true){}
2987
2988                 virtual VisitType Visit(_Control& control)
2989                 {
2990                         if (!control.GetVisibleState())
2991                         {
2992                                 visible = false;
2993                                 return VISIT_STOP;
2994                         }
2995
2996                         return VISIT_UPWARD;
2997                 }
2998         };
2999
3000         _Visitor visitor;
3001         Accept(visitor);
3002
3003         SysAssert(GetLastResult() == E_SUCCESS);
3004         return visitor.visible;
3005 }
3006
3007 bool
3008 _Control::GetVisibleState(void) const
3009 {
3010         ClearLastResult();
3011         return __visibleState;
3012 }
3013
3014 void
3015 _Control::SetVisibleState(bool visibleState)
3016 {
3017         ClearLastResult();
3018
3019         const bool changed = (__visibleState != visibleState) || !__initVisibleState;
3020
3021         if (changed)
3022         {
3023                 GetControlDelegate().OnVisibleStateChanging();
3024         }
3025
3026         __visibleState = visibleState;
3027         GetVisualElement()->SetShowState(visibleState);
3028
3029         if (visibleState == false)
3030         {
3031                 int owneeCount = GetOwneeCount();
3032                 for (int i = 0; i < owneeCount; ++i)
3033                 {
3034                         _Window* pWindow = GetOwnee(i);
3035                         if (pWindow)
3036                         {
3037                                 pWindow->SetVisibleState(visibleState);
3038                         }
3039                 }
3040         }
3041
3042         if (changed)
3043         {
3044                 GetControlDelegate().OnVisibleStateChanged();
3045                 CallOnAncestorVisibleStateChanged();
3046                 _Control* pParent = GetParent();
3047                 if (pParent)
3048                 {
3049                         pParent->GetControlDelegate().OnChildVisibleStateChanged(*this);
3050                 }
3051
3052                 ClearLastResult();
3053         }
3054
3055         __initVisibleState = true;
3056 }
3057
3058 bool
3059 _Control::IsLayoutable(void) const
3060 {
3061         ClearLastResult();
3062         return IsMovable();
3063 }
3064
3065 bool
3066 _Control::IsClipToParent(void) const
3067 {
3068         ClearLastResult();
3069 //      SysAssert(GetVisualElement()->IsClipToParent() == __clipToParent);
3070         return __clipToParent;
3071 }
3072
3073 result
3074 _Control::SetClipToParent(bool clipToParent)
3075 {
3076         ClearLastResult();
3077         result r = E_SUCCESS;
3078
3079         __clipToParent = clipToParent;
3080         r =  GetVisualElement()->SetClipToParent(clipToParent);
3081         SysTryReturn(NID_UI, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
3082
3083         return E_SUCCESS;
3084 }
3085
3086 result
3087 _Control::SetClipChildrenEnabled(bool clipChildren)
3088 {
3089         ClearLastResult();
3090         //result r = E_SUCCESS;
3091
3092         GetVisualElement()->SetClipChildrenEnabled(clipChildren);
3093         //SysTryReturn(NID_UI, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
3094
3095         return E_SUCCESS;
3096 }
3097
3098 Rectangle
3099 _Control::GetBounds(void) const
3100 {
3101         ClearLastResult();
3102         return _CoordinateSystemUtils::ConvertToInteger(__bounds);
3103 }
3104
3105 FloatRectangle
3106 _Control::GetBoundsF(void) const
3107 {
3108         ClearLastResult();
3109         return __bounds;
3110 }
3111
3112 Point
3113 _Control::GetPosition(void) const
3114 {
3115         ClearLastResult();
3116         return Point(_CoordinateSystemUtils::ConvertToInteger(__bounds.x), _CoordinateSystemUtils::ConvertToInteger(__bounds.y));
3117 }
3118
3119 FloatPoint
3120 _Control::GetPositionF(void) const
3121 {
3122         ClearLastResult();
3123         return FloatPoint(__bounds.x, __bounds.y);
3124 }
3125
3126 Dimension
3127 _Control::GetSize(void) const
3128 {
3129         ClearLastResult();
3130         return Dimension(_CoordinateSystemUtils::ConvertToInteger(__bounds.width), _CoordinateSystemUtils::ConvertToInteger(__bounds.height));
3131 }
3132
3133 FloatDimension
3134 _Control::GetSizeF(void) const
3135 {
3136         ClearLastResult();
3137         return FloatDimension(__bounds.width, __bounds.height);
3138 }
3139
3140 // E_SYSTEM
3141 result
3142 _Control::UpdateBoundsOfVisualElement(const FloatRectangle& controlBounds)
3143 {
3144         FloatRectangle rect(controlBounds.x, controlBounds.y, controlBounds.width, controlBounds.height);
3145
3146         _Control* pParent = GetParent();
3147         if (__area == _CONTROL_AREA_CLIENT && pParent)
3148         {
3149                 const FloatRectangle clientBounds = pParent->GetClientBoundsF();
3150                 rect.x += clientBounds.x;
3151                 rect.y += clientBounds.y;
3152         }
3153
3154         GetVisualElement()->SetBounds(rect);
3155
3156         return E_SUCCESS;
3157 }
3158
3159 result
3160 _Control::AdjustAbsoluteBounds(void)
3161 {
3162         struct _Visitor
3163                 : public Visitor
3164         {
3165                 _Visitor(_Control* pControl)
3166                         : __pControl(pControl){}
3167
3168                 virtual VisitType Visit(_Control& control)
3169                 {
3170                         result r = E_SUCCESS;
3171
3172                         if (__pControl == &control)
3173                         {
3174                                 return VISIT_DOWNWARD;
3175                         }
3176                         FloatRectangle fbounds = control.GetBoundsF();
3177                         if (control.IsLayoutChangable() == false)
3178                         {
3179                                 r = control.UpdateBoundsOfVisualElement(FloatRectangle(0.0f, 0.0f, fbounds.width, fbounds.height));
3180                         }
3181                         else
3182                         {
3183                                 r = control.UpdateBoundsOfVisualElement(fbounds);
3184                         }
3185
3186                         ControlList& children = control.GetChildList();
3187
3188                         int childrenCount = children.GetCount();
3189                         if (childrenCount <= 0)
3190                         {
3191                                 return VISIT_STOP;
3192                         }
3193                         else
3194                         {
3195                                 return VISIT_DOWNWARD;
3196                         }
3197                 }
3198         private :
3199                 _Control* __pControl;
3200         };
3201
3202         _Visitor visitor(this);
3203         Accept(visitor);
3204
3205         return E_SUCCESS;
3206 }
3207
3208 bool
3209 _Control::IsInSizeRange(const Dimension& size) const
3210 {
3211         return (__minSize.width <= size.width) && (size.width <= __maxSize.width) &&
3212                    (__minSize.height <= size.height) && (size.height <= __maxSize.height);
3213 }
3214
3215 bool
3216 _Control::IsInSizeRange(const FloatDimension& size) const
3217 {
3218         return (__minSize.width <= size.width) && (size.width <= __maxSize.width) &&
3219                    (__minSize.height <= size.height) && (size.height <= __maxSize.height);
3220 }
3221
3222 // Custom Exception: ex) Location::Map
3223 result
3224 _Control::SetBoundsFinal(const FloatRectangle& newBounds, bool changeLayoutBaseRect, bool callBoundsChangeCallbacks)
3225 {
3226         result r = E_SUCCESS;
3227
3228         FloatRectangle bounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height);
3229
3230         _IControlDelegate& delegate = GetControlDelegate();
3231
3232         const bool moved   = (__bounds.x != bounds.x) || (__bounds.y != bounds.y);
3233         const bool resized = (__bounds.width != bounds.width) || (__bounds.height != bounds.height);
3234
3235         if ((moved || resized) && callBoundsChangeCallbacks)
3236         {
3237                 r = delegate.OnBoundsChanging(_CoordinateSystemUtils::ConvertToInteger(bounds));
3238                 if (IsFailed(r))
3239                 {
3240                         SysLogException(NID_UI, r, "[%s] Callback returns exception.", GetErrorMessage(r));
3241                         return r; // Relay the result;
3242                 }
3243                 r = delegate.OnBoundsChanging(bounds);
3244                 if (IsFailed(r))
3245                 {
3246                         SysLogException(NID_UI, r, "[%s] Callback returns exception.", GetErrorMessage(r));
3247                         return r; // Relay the result;
3248                 }
3249         }
3250
3251         if (moved || resized)
3252         {
3253                 const FloatRectangle fbounds(bounds.x, bounds.y, bounds.width, bounds.height);
3254                 if (IsLayoutChangable() == false)
3255                 {
3256                         r = UpdateBoundsOfVisualElement(FloatRectangle(0.0f, 0.0f, fbounds.width, fbounds.height));
3257                 }
3258                 else
3259                 {
3260                         r = UpdateBoundsOfVisualElement(fbounds);
3261                 }
3262         }
3263
3264         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
3265
3266         __bounds = bounds;
3267
3268         if (changeLayoutBaseRect)
3269         {
3270                 __pLayoutContainer->OnChangeBaseRect();
3271         }
3272
3273         if ((moved || resized) && callBoundsChangeCallbacks)
3274         {
3275                 if (IsMovable() && IsResizable())
3276                 {
3277                         SetClientBounds(FloatRectangle(0.0f, 0.0f, GetSizeF().width, GetSizeF().height));
3278                 }
3279                 if (__pFocusVisualElement)
3280                 {
3281                         __pFocusVisualElement->SetBounds(FloatRectangle(0, 0, __bounds.width, __bounds.height));
3282                 }
3283                 delegate.OnBoundsChanged();
3284
3285                 _Control* pParent = GetParent();
3286                 if (pParent)
3287                 {
3288                         pParent->GetControlDelegate().OnChildBoundsChanged(*this);
3289                 }
3290
3291                 ControlList& children = GetChildList();
3292                 _Control* pChild = null;
3293                 int childrenCount = children.GetCount();
3294
3295                 for (int index = 0; index < childrenCount; index++)
3296                 {
3297                         r = children.GetAt(index, pChild);
3298                         if (!IsFailed(r))
3299                         {
3300                                 pChild->GetControlDelegate().OnParentBoundsChanged(*this);
3301                         }
3302                 }
3303         }
3304
3305         if (moved || resized)
3306         {
3307                 InvalidateHierarchyAbsoluteBounds(*this);
3308         }
3309
3310         ClearLastResult();
3311         return E_SUCCESS;
3312 }
3313
3314 // Custom Exception: ex) Location::Map
3315 result
3316 _Control::AdjustSizeToRange(void)
3317 {
3318         ClearLastResult();
3319         result r = E_SUCCESS;
3320
3321         SysAssert(IsResizable());
3322
3323         FloatDimension size = GetSizeF();
3324         bool changed = ::AdjustSizeToRange(size, __minSize, __maxSize);
3325         if (!changed)
3326         {
3327                 return E_SUCCESS;
3328         }
3329
3330         FloatRectangle newBounds(__bounds.x, __bounds.y, size.width, size.height);
3331         r = SetBoundsFinal(newBounds, true, true);
3332         if (IsFailed(r))
3333         {
3334                 return r;
3335         }
3336
3337         return E_SUCCESS;
3338 }
3339
3340 // Custom Exception: ex) Location::Map
3341 // E_INVALID_ARG
3342 result
3343 _Control::SetBoundsInternal(const FloatRectangle& bounds, bool callBoundsChangeCallbacks)
3344 {
3345         SysTryReturn(NID_UI,
3346                                 IsInSizeRange(Dimension(bounds.width, bounds.height)), E_INVALID_ARG,
3347                                 E_INVALID_ARG,
3348                                 "[E_INVALID_ARG] The specified size(%f, %f) is out of range from min size(%d, %d) to max size(%d, %d).",
3349                                 bounds.width, bounds.height, __minSize.width, __minSize.height, __maxSize.width, __maxSize.height);
3350
3351         SetUpdateLayoutState(true);
3352
3353         return SetBoundsFinal(bounds, true, callBoundsChangeCallbacks);
3354 }
3355
3356 // Custom Exception: ex) Location::Map
3357 // E_INVALID_ARG
3358 // E_UNSUPPORTED_OPERATION
3359 result
3360 _Control::SetBounds(const Rectangle& bounds, bool callBoundsChangeCallbacks)
3361 {
3362         ClearLastResult();
3363
3364         if (callBoundsChangeCallbacks)
3365         {
3366                 SysTryReturn(NID_UI,
3367                                         IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
3368                                         E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
3369         }
3370         FloatRectangle floatBounds(bounds.x, bounds.y, bounds.width, bounds.height);
3371
3372         return SetBoundsInternal(floatBounds, callBoundsChangeCallbacks);
3373 }
3374
3375 // Custom Exception: ex) Location::Map
3376 // E_INVALID_ARG
3377 // E_UNSUPPORTED_OPERATION
3378 result
3379 _Control::SetBounds(const FloatRectangle& bounds, bool callBoundsChangeCallbacks)
3380 {
3381         ClearLastResult();
3382
3383         if (callBoundsChangeCallbacks)
3384         {
3385                 SysTryReturn(NID_UI,
3386                                         IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
3387                                         E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
3388         }
3389
3390         return SetBoundsInternal(bounds, callBoundsChangeCallbacks);
3391 }
3392
3393 // A custom Exception can occur. ex) Location::Map
3394 // E_INVALID_ARG
3395 // E_UNSUPPORTED_OPERATION
3396 result
3397 _Control::SetPosition(const Point& position)
3398 {
3399         ClearLastResult();
3400
3401         SysTryReturn(NID_UI,
3402                                 IsMovable(), E_UNSUPPORTED_OPERATION,
3403                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable.");
3404
3405         return SetBoundsInternal(FloatRectangle(position.x, position.y, __bounds.width, __bounds.height), true);
3406 }
3407
3408 result
3409 _Control::SetPosition(const FloatPoint& position)
3410 {
3411         ClearLastResult();
3412
3413         SysTryReturn(NID_UI,
3414                                 IsMovable(), E_UNSUPPORTED_OPERATION,
3415                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable.");
3416
3417         return SetBoundsInternal(FloatRectangle(position.x, position.y, __bounds.width, __bounds.height), true);
3418 }
3419
3420 // Custom Exception: ex) Location::Map
3421 // E_INVALID_ARG
3422 // E_UNSUPPORTED_OPERATION
3423 result
3424 _Control::SetSize(const Dimension& size)
3425 {
3426         ClearLastResult();
3427
3428         SysTryReturn(NID_UI,
3429                                 IsResizable(), E_UNSUPPORTED_OPERATION,
3430                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
3431
3432         return SetBoundsInternal(FloatRectangle(__bounds.x, __bounds.y, size.width, size.height), true);
3433 }
3434
3435 result
3436 _Control::SetSize(const FloatDimension& size)
3437 {
3438         ClearLastResult();
3439
3440         SysTryReturn(NID_UI,
3441                                 IsResizable(), E_UNSUPPORTED_OPERATION,
3442                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
3443
3444         return SetBoundsInternal(FloatRectangle(__bounds.x, __bounds.y, size.width, size.height), true);
3445 }
3446
3447 Dimension
3448 _Control::GetMinimumSize(void) const
3449 {
3450         ClearLastResult();
3451
3452         return _CoordinateSystemUtils::ConvertToInteger(__minSize);
3453 }
3454
3455 FloatDimension
3456 _Control::GetMinimumSizeF(void) const
3457 {
3458         ClearLastResult();
3459
3460         return __minSize;
3461 }
3462
3463 Dimension
3464 _Control::GetMaximumSize(void) const
3465 {
3466         ClearLastResult();
3467
3468         return _CoordinateSystemUtils::ConvertToInteger(__maxSize);
3469 }
3470
3471 FloatDimension
3472 _Control::GetMaximumSizeF(void) const
3473 {
3474         ClearLastResult();
3475
3476         return __maxSize;
3477 }
3478
3479 // Custom Exception: ex) Location::Map
3480 // E_UNSUPPORTED_OPERATION
3481 // E_INVALID_ARG
3482 result
3483 _Control::SetMinimumSize(const Dimension& newMinSize)
3484 {
3485         ClearLastResult();
3486
3487         SysTryReturn(NID_UI,
3488                                 IsResizable(), E_UNSUPPORTED_OPERATION,
3489                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
3490
3491         SysTryReturn(NID_UI,
3492                                 (newMinSize.width >= 0 && newMinSize.height >= 0), E_INVALID_ARG,
3493                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is smaller than 0.");
3494
3495         SysTryReturn(NID_UI,
3496                                 (newMinSize.width <= MAX_LENGTH && newMinSize.height <= MAX_LENGTH), E_INVALID_ARG,
3497                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is greater than %d.", MAX_LENGTH);
3498
3499         if (__maxSize.width < newMinSize.width)
3500         {
3501                 __maxSize.width = newMinSize.width;
3502         }
3503         if (__maxSize.height < newMinSize.height)
3504         {
3505                 __maxSize.height = newMinSize.height;
3506         }
3507
3508         __minSize = _CoordinateSystemUtils::ConvertToFloat(newMinSize);
3509         return AdjustSizeToRange();
3510 }
3511
3512 result
3513 _Control::SetMinimumSize(const FloatDimension& newMinSize)
3514 {
3515         ClearLastResult();
3516
3517         SysTryReturn(NID_UI,
3518                                 IsResizable(), E_UNSUPPORTED_OPERATION,
3519                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
3520
3521         SysTryReturn(NID_UI,
3522                                 (newMinSize.width >= 0 && newMinSize.height >= 0), E_INVALID_ARG,
3523                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is smaller than 0.");
3524
3525         SysTryReturn(NID_UI,
3526                                 (newMinSize.width <= MAX_LENGTH && newMinSize.height <= MAX_LENGTH), E_INVALID_ARG,
3527                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is greater than %d.", MAX_LENGTH);
3528
3529         if (__maxSize.width < newMinSize.width)
3530         {
3531                 __maxSize.width = newMinSize.width;
3532         }
3533         if (__maxSize.height < newMinSize.height)
3534         {
3535                 __maxSize.height = newMinSize.height;
3536         }
3537
3538         __minSize = newMinSize;
3539         return AdjustSizeToRange();
3540 }
3541
3542 // Custom Exception: ex) Location::Map
3543 // E_UNSUPPORTED_OPERATION
3544 // E_INVALID_ARG
3545 result
3546 _Control::SetMaximumSize(const Dimension& newMaxSize)
3547 {
3548         ClearLastResult();
3549
3550         SysTryReturn(NID_UI,
3551                                 IsResizable(), E_UNSUPPORTED_OPERATION,
3552                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
3553
3554         SysTryReturn(NID_UI,
3555                                 (newMaxSize.width >= 0 && newMaxSize.height >= 0), E_INVALID_ARG,
3556                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is smaller than 0.");
3557
3558         SysTryReturn(NID_UI,
3559                                 (newMaxSize.width <= MAX_LENGTH && newMaxSize.height <= MAX_LENGTH), E_INVALID_ARG,
3560                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is greater than %d.", MAX_LENGTH);
3561
3562         if (newMaxSize.width < __minSize.width)
3563         {
3564                 __minSize.width = newMaxSize.width;
3565         }
3566         if (newMaxSize.height < __minSize.height)
3567         {
3568                 __minSize.height = newMaxSize.height;
3569         }
3570
3571         __maxSize = _CoordinateSystemUtils::ConvertToFloat(newMaxSize);
3572         return AdjustSizeToRange();
3573 }
3574
3575 result
3576 _Control::SetMaximumSize(const FloatDimension& newMaxSize)
3577 {
3578         ClearLastResult();
3579
3580         SysTryReturn(NID_UI,
3581                                 IsResizable(), E_UNSUPPORTED_OPERATION,
3582                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
3583
3584         SysTryReturn(NID_UI,
3585                                 (newMaxSize.width >= 0 && newMaxSize.height >= 0), E_INVALID_ARG,
3586                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is smaller than 0.");
3587
3588         SysTryReturn(NID_UI,
3589                                 (newMaxSize.width <= MAX_LENGTH && newMaxSize.height <= MAX_LENGTH), E_INVALID_ARG,
3590                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is greater than %d.", MAX_LENGTH);
3591
3592         if (newMaxSize.width < __minSize.width)
3593         {
3594                 __minSize.width = newMaxSize.width;
3595         }
3596         if (newMaxSize.height < __minSize.height)
3597         {
3598                 __minSize.height = newMaxSize.height;
3599         }
3600
3601         __maxSize = newMaxSize;
3602         return AdjustSizeToRange();
3603 }
3604
3605
3606 Point
3607 _Control::ConvertToControlPosition(const Point& screenPosition) const
3608 {
3609         Point controlPosition;
3610         Rectangle absoluteBounds = GetAbsoluteBounds();
3611
3612         controlPosition.x = screenPosition.x - absoluteBounds.x;
3613         controlPosition.y = screenPosition.y - absoluteBounds.y;
3614
3615         return controlPosition;
3616 }
3617
3618 FloatPoint
3619 _Control::ConvertToControlPosition(const FloatPoint& screenPosition) const
3620 {
3621         FloatPoint controlPosition;
3622         FloatRectangle absoluteBounds = GetAbsoluteBoundsF();
3623
3624         controlPosition.x = screenPosition.x - absoluteBounds.x;
3625         controlPosition.y = screenPosition.y - absoluteBounds.y;
3626
3627         return controlPosition;
3628 }
3629
3630 Point
3631 _Control::ConvertToScreenPosition(const Point& controlPosition) const
3632 {
3633         Point screenPosition;
3634         Rectangle absoluteBounds = GetAbsoluteBounds();
3635
3636         screenPosition.x = controlPosition.x + absoluteBounds.x;
3637         screenPosition.y = controlPosition.y + absoluteBounds.y;
3638
3639         return screenPosition;
3640 }
3641
3642 FloatPoint
3643 _Control::ConvertToScreenPosition(const FloatPoint& controlPosition) const
3644 {
3645         FloatPoint screenPosition;
3646         FloatRectangle absoluteBounds = GetAbsoluteBoundsF();
3647
3648         screenPosition.x = controlPosition.x + absoluteBounds.x;
3649         screenPosition.y = controlPosition.y + absoluteBounds.y;
3650
3651         return screenPosition;
3652 }
3653
3654 Rectangle
3655 _Control::GetClientBounds(void) const
3656 {
3657         if (!__isSetClientBounds)
3658         {
3659                 return Rectangle(0, 0, __bounds.width, __bounds.height);
3660         }
3661
3662         return _CoordinateSystemUtils::ConvertToInteger(__clientBounds);
3663 }
3664
3665 FloatRectangle
3666 _Control::GetClientBoundsF(void) const
3667 {
3668         if (!__isSetClientBounds)
3669         {
3670                 return FloatRectangle(0.0f, 0.0f, __bounds.width, __bounds.height);
3671         }
3672
3673         return __clientBounds;
3674 }
3675
3676
3677 Rectangle
3678 _Control::GetClientBounds(const Tizen::Graphics::Dimension& size) const
3679 {
3680         if (!__isSetClientBounds)
3681         {
3682                 return Rectangle(0, 0, size.width, size.height);
3683         }
3684
3685         return _CoordinateSystemUtils::ConvertToInteger(__clientBounds);
3686 }
3687
3688
3689 FloatRectangle
3690 _Control::GetClientBoundsF(const Tizen::Graphics::FloatDimension& size) const
3691 {
3692         if (!__isSetClientBounds)
3693         {
3694                 return FloatRectangle(0.0f, 0.0f, size.width, size.height);
3695         }
3696
3697         return __clientBounds;
3698 }
3699
3700 Rectangle
3701 _Control::GetAbsoluteBounds(bool recalcAlways) const
3702 {
3703         if (!recalcAlways && !__needRecalcAbsBounds)
3704         {
3705                 return __absoluteBounds;
3706         }
3707
3708         Point accumPoint;
3709         Rectangle absoluteBounds;
3710         Rectangle clientBounds;
3711
3712         const _Control* pSelf = this;
3713         const _Control* pParent = GetParent();
3714
3715         while (pParent)
3716         {
3717                 if ((pSelf->GetArea() == _CONTROL_AREA_CLIENT) && pParent)
3718                 {
3719                         clientBounds = pParent->GetClientBounds();
3720                         accumPoint += Point(clientBounds.x, clientBounds.y);
3721                         accumPoint.x -= pParent->GetHorizontalScrollPosition();
3722                         accumPoint.y -= pParent->GetVerticalScrollPosition();
3723                 }
3724
3725                 accumPoint += pSelf->GetPosition();
3726                 pSelf = pParent;
3727                 pParent = pParent->GetParent();
3728         }
3729
3730         _Window* pWindow = GetRootWindow();
3731
3732         if (pWindow)
3733         {
3734                 Point winPoint = pWindow->GetPosition();
3735
3736                 accumPoint.x += winPoint.x;
3737                 accumPoint.y += winPoint.y;
3738         }
3739
3740         absoluteBounds.x = accumPoint.x;
3741         absoluteBounds.y = accumPoint.y;
3742         absoluteBounds.width = __bounds.width;
3743         absoluteBounds.height = __bounds.height;
3744
3745         const_cast<_Control*>(this)->__absoluteBounds = absoluteBounds;
3746         const_cast<_Control*>(this)->__needRecalcAbsBounds = false;
3747
3748         return absoluteBounds;
3749 }
3750
3751 FloatRectangle
3752 _Control::GetAbsoluteBoundsF(bool recalcAlways) const
3753 {
3754         if (!recalcAlways && !__needRecalcAbsBoundsF)
3755         {
3756                 return __absoluteBoundsF;
3757         }
3758
3759         FloatPoint accumPoint;
3760         FloatRectangle absoluteBounds;
3761         FloatRectangle clientBounds;
3762
3763         const _Control* pSelf = this;
3764         const _Control* pParent = GetParent();
3765
3766         while (pParent)
3767         {
3768                 if ((pSelf->GetArea() == _CONTROL_AREA_CLIENT) && pParent)
3769                 {
3770                         clientBounds = pParent->GetClientBoundsF();
3771                         accumPoint += FloatPoint(clientBounds.x, clientBounds.y);
3772                         accumPoint.x -= pParent->GetHorizontalScrollPosition();
3773                         accumPoint.y -= pParent->GetVerticalScrollPosition();
3774                 }
3775
3776                 accumPoint += pSelf->GetPositionF();
3777                 pSelf = pParent;
3778                 pParent = pParent->GetParent();
3779         }
3780
3781         _Window* pWindow = GetRootWindow();
3782
3783         if (pWindow)
3784         {
3785                 FloatPoint winPoint = pWindow->GetPositionF();
3786
3787                 accumPoint.x += winPoint.x;
3788                 accumPoint.y += winPoint.y;
3789         }
3790
3791         absoluteBounds.x = accumPoint.x;
3792         absoluteBounds.y = accumPoint.y;
3793         absoluteBounds.width = __bounds.width;
3794         absoluteBounds.height = __bounds.height;
3795
3796         const_cast<_Control*>(this)->__absoluteBoundsF = absoluteBounds;
3797         const_cast<_Control*>(this)->__needRecalcAbsBoundsF = false;
3798
3799         return absoluteBounds;
3800 }
3801
3802 result
3803 _Control::SetClientBounds(const Rectangle& bounds)
3804 {
3805         ClearLastResult();
3806
3807         const bool moved   = (__clientBounds.x != _CoordinateSystemUtils::ConvertToFloat(bounds).x) || (__clientBounds.y != _CoordinateSystemUtils::ConvertToFloat(bounds).y);
3808         const bool resized = (__clientBounds.width != _CoordinateSystemUtils::ConvertToFloat(bounds).width) || (__clientBounds.height != _CoordinateSystemUtils::ConvertToFloat(bounds).height);
3809
3810         SysTryReturn(NID_UI,
3811                                 IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
3812                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
3813         __clientBounds = _CoordinateSystemUtils::ConvertToFloat(bounds);
3814         __isSetClientBounds = true;
3815
3816         if (moved || resized)
3817         {
3818                 result r = AdjustAbsoluteBounds();
3819                 SysTryReturn(NID_UI, r == E_SUCCESS, r,         r, "[%s] Propagating.", GetErrorMessage(r));
3820
3821                 SetUpdateLayoutState(true);
3822         }
3823         return E_SUCCESS;
3824 }
3825
3826 result
3827 _Control::SetClientBounds(const FloatRectangle& bounds)
3828 {
3829         ClearLastResult();
3830
3831         const bool moved   = (__clientBounds.x != bounds.x) || (__clientBounds.y != bounds.y);
3832         const bool resized = (__clientBounds.width != bounds.width) || (__clientBounds.height != bounds.height);
3833
3834         SysTryReturn(NID_UI,
3835                                 IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
3836                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
3837         __clientBounds = bounds;
3838         __isSetClientBounds = true;
3839
3840         if (moved || resized)
3841         {
3842                 result r = AdjustAbsoluteBounds();
3843                 SysTryReturn(NID_UI, r == E_SUCCESS, r,         r, "[%s] Propagating.", GetErrorMessage(r));
3844
3845                 SetUpdateLayoutState(true);
3846         }
3847
3848         return E_SUCCESS;
3849 }
3850
3851 bool
3852 _Control::IsCalledSetClientBounds(void)
3853 {
3854         return __isSetClientBounds;
3855 }
3856
3857 void
3858 _Control::UpdateClientBounds(const FloatDimension& size, FloatRectangle& clientBounds)
3859 {
3860         clientBounds.width = size.width;
3861         clientBounds.height = size.height;
3862 }
3863
3864 Color
3865 _Control::GetBackgroundColor(void) const
3866 {
3867         ClearLastResult();
3868         return __backgroundColor;
3869 }
3870
3871 void
3872 _Control::SetBackgroundColor(const Color& color)
3873 {
3874         ClearLastResult();
3875         __backgroundColor = color;
3876
3877         _ControlVisualElement* pCVE = dynamic_cast <_ControlVisualElement*>(GetVisualElement());
3878         if (pCVE)
3879         {
3880                 pCVE->SetBackgroundColor(
3881                         _Colorf(
3882                                 (float)color.GetRed() / 255.0f,
3883                                 (float)color.GetGreen() / 255.0f,
3884                                 (float)color.GetBlue() / 255.0f,
3885                                 (float)color.GetAlpha() / 255.0f
3886                         )
3887                 );
3888         }
3889
3890         GetControlDelegate().OnBackgroundColorChanged(const_cast<Color&>(color));
3891         ClearLastResult();
3892 }
3893
3894 _Control::~_Control(void)
3895 {
3896         __destroying = true;
3897
3898         DoBacktrace(__pDeleteBacktrace.get());
3899
3900         DetachAllChildren();
3901         DetachAllOwnees();
3902         _ControlManager::GetInstance()->TakeFocusFromControl(*this);
3903         DisposeControl();
3904         ReleaseHandle();
3905
3906         if (__pFont)
3907         {
3908                 delete __pFont;
3909                 __pFont = null;
3910         }
3911
3912 //      Dangerous: it clears last result and log in catch block.
3913 //      ClearLastResult();
3914 }
3915
3916 void
3917 _Control::DisposeControl(void)
3918 {
3919         __pControlDelegate = null;
3920
3921         delete __pLayoutItemHandler;
3922         __pLayoutItemHandler = null;
3923
3924         delete __pLayoutContainer;
3925         __pLayoutContainer = null;
3926
3927         delete __pChildren;
3928         __pChildren = null;
3929
3930         delete __pOwnees;
3931         __pOwnees = null;
3932
3933         if (__pVisualElement)
3934         {
3935                 __pVisualElement->Destroy();
3936         }
3937         __pVisualElement = null;
3938
3939         delete __pVisualElementContentProvider;
3940         __pVisualElementContentProvider = null;
3941
3942         delete __pVisualElementEventListener;
3943         __pVisualElementEventListener = null;
3944
3945         delete __pCoreGestureDetectors;
3946         __pCoreGestureDetectors = null;
3947
3948         delete __pDataBindingContext;
3949         __pDataBindingContext = null;
3950
3951         ClearStartedGestureDetectorList();
3952         delete __pDetectStartedGestureMap;
3953         __pDetectStartedGestureMap = null;
3954
3955         delete __pDelayedTouchInfoList;
3956         __pDelayedTouchInfoList = null;
3957
3958         delete __pAccessibilityContainer;
3959         __pAccessibilityContainer = null;
3960
3961         if (__pFocusVisualElement)
3962         {
3963                 __pFocusVisualElement.release();
3964         }
3965 }
3966
3967 // E_OUT_OF_MEMORY
3968 // E_SYSTEM
3969 _Control::_Control(void)
3970         : __needRecalcRootWindow(true)
3971         , __needRecalcAbsBounds(true)
3972         , __needRecalcAbsBoundsF(true)
3973         , __pRootWindow(null)
3974         , __name(L"")
3975         , __pParent(null)
3976         , __pChildren(null)
3977         , __pOwnees(null)
3978         , __bounds(0.0f, 0.0f, 0.0f, 0.0f)
3979         , __contentAreaBounds(0.0f, 0.0f, 0.0f, 0.0f)
3980         , __clientBounds(0.0f, 0.0f, 0.0f, 0.0f)
3981         , __absoluteBounds(0, 0, 0, 0)
3982         , __absoluteBoundsF(0.0f, 0.0f, 0.0f, 0.0f)
3983         , __invalidatedBounds(0.0f, 0.0f, 0.0f, 0.0f)
3984         , __minSize(FloatDimension(0.0f, 0.0f))
3985         , __maxSize(FloatDimension(MAX_LENGTH, MAX_LENGTH))
3986         , __backgroundColor(Color::GetColor(COLOR_ID_BLACK))
3987         , __movable(true)
3988         , __resizable(true)
3989         , __focusable(true)
3990         , __nativeObjectFocusable(true)
3991         , __enabledState(true)
3992         , __visibleState(true)
3993         , __initVisibleState(false)
3994         , __clipToParent(true)
3995         , __multiTouchEnabled(false)
3996         , __dragEnabled(false)
3997         , __dropEnabled(false)
3998         , __drawWhenVisible(true)
3999         , __isPostOrderTraversal(false)
4000         , __isCalledCallOnAttachingToMainTree(false)
4001         , __isCalledCallOnPreAttachedToMainTree(false)
4002         , __isCalledCallOnAttachedToMainTree(false)
4003         , __isSetClientBounds(false)
4004         , __isCalledGetCanvasN(false)
4005         , __isFocusMode(false)
4006         , __isNavigatable(true)
4007         , __isFocusWindowActivationChecked(false)
4008         , __pVisualElementContentProvider(null)
4009         , __pVisualElement(null)
4010         , __pVisualElementEventListener(null)
4011         , __pLayoutItemHandler(null)
4012         , __pPortraitLayout(null)
4013         , __pLandscapeLayout(null)
4014         , __pLayoutContainer(null)
4015         , __area(_CONTROL_AREA_NONE)
4016         , __layer(_CONTROL_LAYER_NONE)
4017         , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
4018         , __rotation(_CONTROL_ROTATION_0)
4019         , __pTouchEventPreviewer(null)
4020         , __pKeyEventPreviewer(null)
4021         , __pNotificationEventPreviewer(null)
4022         , __pKeyEventListener(null)
4023         , __pFocusEventListener(null)
4024         , __pNotificationEventListener(null)
4025         , __pCoreGestureDetectors(null)
4026         , __pDetectStartedGestureMap(null)
4027         , __pDelayedTouchInfoList(null)
4028         , __touchMoveAllowance(3)
4029         , __pressThresHold(0.0f)
4030         , __inputLockRefCount(0)
4031         , __screenDpi(0)
4032         , __isSentDelayedEvent(false)
4033         , __isSendingDelayedEvent(false)
4034         , __isChangingEventTarget(false)
4035         , __pDataBindingContext(null)
4036         , __pControlDelegate(null)
4037         , __pUserData(null)
4038         , __destroying(false)
4039         , __isEventEnableState(true)
4040         , __isEffectSoundEnabled(true)
4041         , __isControlFontChanged(false)
4042         , __pFont(null)
4043         , __fontName(L"")
4044         , __fontFileName(L"")
4045         , __pPreviousFocus(null)
4046         , __pNextFocus(null)
4047         , __pDragWindow(null)
4048         , __isDragAndDropSource(false)
4049 {
4050         ClearLastResult();
4051
4052         std::unique_ptr<Tizen::Base::Collection::ArrayListT<void*> > pNewBacktrace(new (std::nothrow) ArrayListT<void*>);
4053         SysTryReturnVoidResult(NID_SHELL, pNewBacktrace, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4054
4055         result r = pNewBacktrace->Construct();
4056         SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
4057
4058         __pNewBacktrace = move(pNewBacktrace);
4059
4060         DoBacktrace(__pNewBacktrace.get());
4061
4062         std::unique_ptr<Tizen::Base::Collection::ArrayListT<void*> > pDeleteBacktrace(new (std::nothrow) ArrayListT<void*>);
4063         SysTryReturnVoidResult(NID_SHELL, pDeleteBacktrace, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4064
4065         r = pDeleteBacktrace->Construct();
4066         SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
4067
4068         __pDeleteBacktrace = move(pDeleteBacktrace);
4069
4070         SetControlDelegate(*this);
4071         __pAccessibilityContainer = new (std::nothrow) _AccessibilityContainer(*this);
4072         __pAccessibilityContainer->Activate(true);
4073
4074         __pLayoutItemHandler = new (std::nothrow) LayoutItemHandler(this);
4075         if (!__pLayoutItemHandler)
4076         {
4077                 goto CATCH;
4078         }
4079
4080         __pLayoutContainer = CreateLayoutContainerN(__pLayoutItemHandler);
4081         if (!__pLayoutContainer)
4082         {
4083                 goto CATCH;
4084         }
4085
4086         __pChildren = ::CreateControlListN();
4087         if (!__pChildren)
4088         {
4089                 goto CATCH;
4090         }
4091
4092         __pOwnees = ::CreateWindowListN();
4093         if (!__pOwnees)
4094         {
4095                 goto CATCH;
4096         }
4097
4098         __pVisualElement = ::CreateVisualElementN();
4099         if (!__pVisualElement)
4100         {
4101                 goto CATCH;
4102         }
4103
4104         __pVisualElement->SetUserData(this);
4105         __pVisualElement->SetClipChildrenEnabled(true);
4106         __pVisualElement->SetRedrawOnResizeEnabled(true);
4107
4108         __pVisualElementContentProvider = new (std::nothrow) ControlVisualElementContentProvider(*this);
4109         SysTryCatch(NID_UI, __pVisualElementContentProvider, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4110
4111         GetVisualElement()->SetContentProvider(__pVisualElementContentProvider);
4112
4113         __pVisualElementEventListener = new (std::nothrow) ControlVisualElementEventListener(*this);
4114         SysTryCatch(NID_UI, __pVisualElementEventListener, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4115
4116         GetVisualElement()->SetVisualElementEventListener(__pVisualElementEventListener);
4117
4118         __pDetectStartedGestureMap = ::CreateGestureMapN();
4119         if (!__pDetectStartedGestureMap)
4120         {
4121                 goto CATCH;
4122         }
4123
4124         __pCoreGestureDetectors = new (std::nothrow) LinkedListT<_TouchGestureDetector*>;
4125         if (!__pCoreGestureDetectors)
4126         {
4127                 goto CATCH;
4128         }
4129
4130         __pDelayedTouchInfoList = new (std::nothrow) LinkedListT<_TouchInfo*>;
4131         if (!__pDelayedTouchInfoList)
4132         {
4133                 goto CATCH;
4134         }
4135
4136         SetEventPreviewer<_UI_EVENT_TOUCH>(this);
4137         SetEventPreviewer<_UI_EVENT_KEY>(this);
4138         SetEventPreviewer<_UI_EVENT_NOTIFICAITON>(this);
4139
4140         SetEventListener<_UI_EVENT_NOTIFICAITON>(this);
4141         SetEventListener<_UI_EVENT_FOCUS>(this);
4142
4143         SetPropagatedTouchEventListener(this);
4144         SetPropagatedKeyEventListener(this);
4145         GET_COLOR_CONFIG(BASIC::background, __backgroundColor);
4146
4147         if (IsFailed(GetLastResult()))
4148         {
4149                 SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
4150         }
4151
4152         __screenDpi = _ControlManager::GetInstance()->GetScreenDpi();
4153
4154         return;
4155
4156 CATCH:
4157         DisposeControl();
4158         return;
4159 }
4160
4161 void
4162 _Control::AcquireHandle(void)
4163 {
4164         __controlHandle = _ControlManager::GetInstance()->Register(this); // [ToDo] exception?
4165 }
4166
4167 void
4168 _Control::ReleaseHandle(void)
4169 {
4170         _ControlManager::GetInstance()->Release(__controlHandle); // [ToDo] exception?
4171 }
4172
4173 void
4174 _Control::SetDrawWhenVisible(bool draw)
4175 {
4176         __drawWhenVisible = draw;
4177 }
4178
4179 bool
4180 _Control::IsDrawWhenVisible(void)
4181 {
4182         return __drawWhenVisible;
4183 }
4184
4185 void
4186 _Control::SetTerminatingOrder(bool postOrderTraversal)
4187 {
4188         __isPostOrderTraversal = postOrderTraversal;
4189 }
4190
4191 bool
4192 _Control::IsPostOrderTraversal(void)
4193 {
4194         return __isPostOrderTraversal;
4195 }
4196
4197 void
4198 _Control::SetParent(_Control* pParent)
4199 {
4200         ClearLastResult();
4201         __pParent = pParent;
4202 }
4203
4204 // E_OUT_OF_MEMORY
4205 // Only called by _Window::SetOwner(pOwner)
4206 result
4207 _Control::AttachOwnee(_Window& window)
4208 {
4209         ClearLastResult();
4210         result r = E_SUCCESS;
4211
4212         _Control* pOldOwner = window.GetOwner();
4213         if (pOldOwner)
4214         {
4215                 pOldOwner->DetachOwnee(window);
4216         }
4217
4218         r = __pOwnees->Add(&window);
4219         if (IsFailed(r))
4220         {
4221                 SysLogException(NID_UI, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4222                 return E_OUT_OF_MEMORY;
4223         }
4224
4225         return E_SUCCESS;
4226 }
4227
4228 int
4229 _Control::GetOwneeCount(void) const
4230 {
4231         ClearLastResult();
4232         return __pOwnees->GetCount();
4233 }
4234
4235 // E_OUT_OF_RANGE
4236 _Window*
4237 _Control::GetOwnee(int index) const
4238 {
4239         ClearLastResult();
4240         result r = E_SUCCESS;
4241
4242         _Window* pOwnee = null;
4243         r = __pOwnees->GetAt(index, pOwnee);
4244         if (IsFailed(r))
4245         {
4246                 SysLogException(NID_UI, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
4247                 return null;
4248         }
4249
4250         return pOwnee;
4251 }
4252
4253 // Called by _Window::SetOwner(null)
4254 void
4255 _Control::DetachOwnee(_Window& ownee)
4256 {
4257         ClearLastResult();
4258         result r = E_SUCCESS;
4259
4260         _Control* pOwner = ownee.GetOwner();
4261         if (pOwner != this)
4262         {
4263                 return;
4264         }
4265
4266         r = __pOwnees->Remove(&ownee);
4267         if (IsFailed(r))
4268         {
4269                 SysLogException(NID_UI, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
4270         }
4271 }
4272
4273 void
4274 _Control::DetachAllOwnees(void)
4275 {
4276         ClearLastResult();
4277
4278         int owneeCount = GetOwneeCount();
4279         while (owneeCount--)
4280         {
4281                 _Window* pOwnee = GetOwnee(0);
4282                 if (pOwnee == null)
4283                 {
4284                         continue;
4285                 }
4286
4287                 pOwnee->SetOwner(null);
4288         }
4289 }
4290
4291 _Layout::LayoutContainer&
4292 _Control::GetLayoutContainer() const
4293 {
4294         return *__pLayoutContainer;
4295 }
4296
4297 void
4298 _Control::SetMultiTouchEnabled(bool enabled)
4299 {
4300         ClearLastResult();
4301         __multiTouchEnabled = enabled;
4302 }
4303
4304 bool
4305 _Control::IsMultiTouchEnabled(void) const
4306 {
4307         ClearLastResult();
4308         return __multiTouchEnabled;
4309 }
4310
4311 void
4312 _Control::SetMovable(bool movable)
4313 {
4314         ClearLastResult();
4315         __movable = movable;
4316 }
4317
4318 void
4319 _Control::SetResizable(bool resizable)
4320 {
4321         ClearLastResult();
4322
4323         if (!resizable)
4324         {
4325                 __minSize = __maxSize = GetSizeF();
4326         }
4327
4328         if (!__resizable && resizable)
4329         {
4330                 __minSize = FloatDimension(0.0f, 0.0f);
4331                 __maxSize = FloatDimension(MAX_LENGTH, MAX_LENGTH);
4332         }
4333
4334         __resizable = resizable;
4335 }
4336
4337 _Layout::Layout*
4338 _Control::GetLayout(void) const
4339 {
4340         ClearLastResult();
4341         _Layout::Layout* pLayout = __pLayoutContainer->GetLayout();
4342
4343         SysAssert(GetLastResult() == E_SUCCESS);
4344         return pLayout;
4345 }
4346
4347 result
4348 _Control::SetCurrentLayout(_Layout::Layout& layout)
4349 {
4350         ClearLastResult();
4351         return __pLayoutContainer->SetCurrentLayout(layout);
4352 }
4353
4354 result
4355 _Control::AddLayout(_Layout::Layout& layout)
4356 {
4357         ClearLastResult();
4358         return __pLayoutContainer->AddLayout(layout);
4359 }
4360
4361 void
4362 _Control::SetUpdateLayoutState(bool state)
4363 {
4364         _Layout::Layout* pLayout = GetLayout();
4365         if (pLayout)
4366         {
4367                 pLayout->SetUpdateState(state);
4368         }
4369 }
4370
4371 void
4372 _Control::SetEffectSoundEnabled(bool enable)
4373 {
4374         __isEffectSoundEnabled = enable;
4375 }
4376
4377 bool
4378 _Control::IsEffectSoundEnabled(void) const
4379 {
4380         return __isEffectSoundEnabled;
4381 }
4382
4383 _DataBindingContext*
4384 _Control::GetDataBindingContext(void)
4385 {
4386         return __pDataBindingContext;
4387 }
4388 _AccessibilityContainer*
4389 _Control::GetAccessibilityContainer(void)
4390 {
4391         return __pAccessibilityContainer;
4392 }
4393 void
4394 _Control::SetDataBindingContext(_DataBindingContext* pDataBindingContext)
4395 {
4396         __pDataBindingContext = pDataBindingContext;
4397 }
4398
4399 void
4400 _Control::DoBacktrace(Tizen::Base::Collection::ArrayListT<void*>* pBacktraceList)
4401 {
4402         SysTryReturnVoidResult(NID_SHELL, pBacktraceList, E_INVALID_ARG, "[E_INVALID_ARG].");
4403
4404         const static int ADDR_SIZE = 100;
4405         int retSize = 0;
4406         void* pBuffer[ADDR_SIZE];
4407
4408         retSize = backtrace(pBuffer, ADDR_SIZE);
4409         SysSecureLog(NID_SHELL, " %d addresses", retSize);
4410
4411         for (int i = 0; i < retSize; i++)
4412         {
4413                 if (pBuffer[i])
4414                 {
4415                         pBacktraceList->Add(pBuffer[i]);
4416                 }
4417         }
4418 }
4419
4420 Tizen::Base::String
4421 _Control::GetDescription(void) const
4422 {
4423         return String(L"");
4424 }
4425
4426 void
4427 _Control::SetTouchCapture(bool allowOutOfBounds, bool allowOwnerBounds)
4428 {
4429         _TouchManager* pTouchManager = _TouchManager::GetInstance();
4430         SysTryReturnVoidResult(NID_UI, pTouchManager != null, E_SYSTEM, "[E_SYSTEM] TouchManager Instance is null");
4431
4432         pTouchManager->SetCapturedControl(this, allowOutOfBounds, allowOwnerBounds);
4433 }
4434
4435 void
4436 _Control::ReleaseTouchCapture(void)
4437 {
4438         _TouchManager* pTouchManager = _TouchManager::GetInstance();
4439         SysTryReturnVoidResult(NID_UI, pTouchManager != null, E_SYSTEM, "[E_SYSTEM] TouchManager Instance is null");
4440
4441         pTouchManager->SetCapturedControl(null, false, false);
4442 }
4443
4444 _Control*
4445 _Control::GetTopmostChildAt(const Point& point) const
4446 {
4447         _Control* pTouchedControl = null;
4448         FloatPoint ptf((float) point.x, (float) point.y);
4449         _ControlManager* pControlManager = _ControlManager::GetInstance();
4450         SysTryReturn(NID_UI, pControlManager, null, E_SYSTEM, "[E_SYSTEM] _ControlManager does not exist.");
4451
4452         _Window* pRootWindow = GetRootWindow();
4453         SysTryReturn(NID_UI, pRootWindow, null, E_SYSTEM, "[E_SYSTEM] GetRootWindow() is null, this control is detached from the main trree");
4454
4455         _ControlVisualElement* pRootControlElement = dynamic_cast <_ControlVisualElement*>(pRootWindow->GetVisualElement());
4456         SysTryReturn(NID_UI, pRootControlElement, null, E_SYSTEM, "[E_SYSTEM] pRootControlElement is null.");
4457
4458         _ControlVisualElement* pHitTestElm = pRootControlElement->GetControlChildAtPoint(ptf);
4459         if (pHitTestElm == null)
4460         {
4461                 SysSecureLog(NID_UI, "pHitTestElm is null, point(%f, %f)", ptf.x, ptf.y);
4462                 return null;
4463         }
4464
4465         pTouchedControl = static_cast <_Control*>(pHitTestElm->GetUserData());
4466
4467         return pTouchedControl;
4468 }
4469
4470 void
4471 _Control::SetContentAreaBounds(const Rectangle& rect)
4472 {
4473         ClearLastResult();
4474
4475         __contentAreaBounds = _CoordinateSystemUtils::ConvertToFloat(rect);
4476 }
4477
4478 void
4479 _Control::SetContentAreaBounds(const FloatRectangle& rect)
4480 {
4481         ClearLastResult();
4482
4483         __contentAreaBounds = rect;
4484 }
4485
4486 Rectangle
4487 _Control::GetContentAreaBounds(void) const
4488 {
4489         return _CoordinateSystemUtils::ConvertToInteger(__contentAreaBounds);
4490 }
4491
4492 FloatRectangle
4493 _Control::GetContentAreaBoundsF(void) const
4494 {
4495         return __contentAreaBounds;
4496 }
4497
4498 Bitmap*
4499 _Control::GetCapturedBitmapN(bool includeChildren) const
4500 {
4501         result r = E_SUCCESS;
4502
4503         Canvas* pCanvas = null;
4504         Bitmap* pBitmap = null;
4505
4506         pBitmap = GetControlDelegate().OnCapturedBitmapRequestedN();
4507         if (pBitmap == null)
4508         {
4509                 FloatRectangle rect;
4510
4511                 Rectangle boundsInCanvas = GetBounds();
4512                 boundsInCanvas.x = boundsInCanvas.y = 0;
4513
4514                 pCanvas = new (std::nothrow) Canvas;
4515                 SysTryReturn(NID_UI, pCanvas, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4516
4517                 r = pCanvas->Construct(boundsInCanvas);
4518                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
4519
4520                 GetVisualElement()->Draw();
4521                 ClearLastResult();
4522
4523                 rect.SetBounds(boundsInCanvas.x, boundsInCanvas.y, boundsInCanvas.width, boundsInCanvas.height);
4524
4525                 r = GetVisualElement()->Capture(*pCanvas, rect, includeChildren);
4526                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
4527
4528                 pBitmap = new (std::nothrow) Bitmap;
4529                 SysTryCatch(NID_UI, pBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4530
4531                 r = pBitmap->Construct(*pCanvas, boundsInCanvas);
4532                 if (IsFailed(r))
4533                 {
4534                         SysTryCatch(NID_UI, r == E_SUCCESS, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4535                 }
4536
4537                 SysLog(NID_UI, "Bitmap (width:%d, height:%d)", pBitmap->GetWidth(), pBitmap->GetHeight());
4538
4539                 delete pCanvas;
4540         }
4541         _BitmapImpl::ConvertToNonpremultiplied(*pBitmap, true);
4542
4543         return pBitmap;
4544
4545 CATCH:
4546         delete pCanvas;
4547         delete pBitmap;
4548         SetLastResult(r);
4549
4550         return null;
4551 }
4552
4553 Tizen::Graphics::Rectangle
4554 _Control::GetInvalidatedBounds(void) const
4555 {
4556         return _CoordinateSystemUtils::ConvertToInteger(__invalidatedBounds);
4557 }
4558
4559 Tizen::Graphics::FloatRectangle
4560 _Control::GetInvalidatedBoundsF(void) const
4561 {
4562         return __invalidatedBounds;
4563 }
4564
4565 result
4566 _Control::AddGestureDetector(const _TouchGestureDetector& gestureDetector)
4567 {
4568         ClearLastResult();
4569
4570         bool exist = __pCoreGestureDetectors->Contains(const_cast<_TouchGestureDetector*>(&gestureDetector));
4571         SysTryReturnResult(NID_UI, exist == false, E_OBJ_ALREADY_EXIST, "__pCoreGestureDetectors has gesture already");
4572
4573         result r = __pCoreGestureDetectors->Add(const_cast<_TouchGestureDetector*>(&gestureDetector));
4574         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
4575
4576         r = const_cast<_TouchGestureDetector&>(gestureDetector).SetControl(*this);
4577         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "Control handle is not valid.");
4578
4579         const_cast<_TouchGestureDetector&>(gestureDetector).OnTouchGestureDetectorAdded();
4580
4581         return r;
4582 }
4583
4584 result
4585 _Control::RemoveGestureDetector(const _TouchGestureDetector& gestureDetector)
4586 {
4587         ClearLastResult();
4588
4589         result r = __pCoreGestureDetectors->Remove(&(const_cast<_TouchGestureDetector&>(gestureDetector)));
4590         if (r != E_SUCCESS)
4591         {
4592                 return r;
4593         }
4594
4595         const_cast<_TouchGestureDetector&>(gestureDetector).OnTouchGestureDetectorRemoved();
4596
4597         return E_SUCCESS;
4598 }
4599
4600 IListT <_TouchGestureDetector*>*
4601 _Control::GetGestureDetectorList(void) const
4602 {
4603         return __pCoreGestureDetectors;
4604 }
4605
4606 IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>*
4607 _Control::GetStartedGestureDetectorEnumeratorN(void) const
4608 {
4609         return __pDetectStartedGestureMap->GetMapEnumeratorN();
4610 }
4611
4612 IListT <_TouchGestureDetector*>*
4613 _Control::GetStartedGestureDetectorListN(void) const
4614 {
4615         return __pDetectStartedGestureMap->GetKeysN();
4616 }
4617
4618 result
4619 _Control::AddStartedGestureDetector(const _TouchGestureDetector& gestureDetector, _TouchGestureDetectorState state)
4620 {
4621         ClearLastResult();
4622
4623         result r = E_SUCCESS;
4624
4625         bool exist = false;
4626         __pDetectStartedGestureMap->ContainsKey(const_cast<_TouchGestureDetector*>(&gestureDetector), exist);
4627
4628         if (exist == false)
4629         {
4630                 r = __pDetectStartedGestureMap->Add(const_cast<_TouchGestureDetector*>(&gestureDetector), state);
4631         }
4632
4633         return r;
4634 }
4635
4636 result
4637 _Control::SetStartedGestureDetector(const _TouchGestureDetector& gestureDetector, _TouchGestureDetectorState state)
4638 {
4639         ClearLastResult();
4640
4641         result r = E_SUCCESS;
4642
4643         bool exist = false;
4644         __pDetectStartedGestureMap->ContainsKey(const_cast<_TouchGestureDetector*>(&gestureDetector), exist);
4645
4646         if (exist == true)
4647         {
4648                 r = __pDetectStartedGestureMap->SetValue(const_cast<_TouchGestureDetector*>(&gestureDetector), state);
4649         }
4650
4651         return r;
4652 }
4653
4654 result
4655 _Control::ClearStartedGestureDetectorList(void)
4656 {
4657         ClearLastResult();
4658
4659         result r = E_SUCCESS;
4660
4661         IListT<_TouchGestureDetector*>* pList = __pDetectStartedGestureMap->GetKeysN();
4662         if (pList)
4663         {
4664                 IEnumeratorT<_TouchGestureDetector*>* pEnumerator = pList->GetEnumeratorN();
4665                 if (pEnumerator)
4666                 {
4667                         while(pEnumerator->MoveNext() == E_SUCCESS)
4668                         {
4669                                 _TouchGestureDetector* pGestureDetector = null;
4670                                 pEnumerator->GetCurrent(pGestureDetector);
4671
4672                                 if (pGestureDetector == null)
4673                                 {
4674                                         continue;
4675                                 }
4676
4677                                 __pDetectStartedGestureMap->Remove(pGestureDetector);
4678                         }
4679                         delete pEnumerator;
4680                 }
4681                 delete pList;
4682         }
4683
4684         IEnumeratorT<_TouchInfo*>* pEnumerator = __pDelayedTouchInfoList->GetEnumeratorN();
4685         if(pEnumerator)
4686         {
4687                 while(pEnumerator->MoveNext() == E_SUCCESS)
4688                 {
4689                         _TouchInfo* pTouchInfo = null;
4690                         pEnumerator->GetCurrent(pTouchInfo);
4691                         if (pTouchInfo == null)
4692                         {
4693                                 continue;
4694                         }
4695
4696                         delete pTouchInfo;
4697                 }
4698
4699                 __pDelayedTouchInfoList->RemoveAll();
4700                 delete pEnumerator;
4701         }
4702
4703         __isSentDelayedEvent = false;
4704         __isSendingDelayedEvent = false;
4705
4706         return r;
4707 }
4708
4709 bool
4710 _Control::IsDelayedTouchEventEnabled(void) const
4711 {
4712         bool existDelayTouchEventGesture = false;
4713
4714         IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>* pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
4715         if (pMapEnumerator)
4716         {
4717                 while(pMapEnumerator->MoveNext() == E_SUCCESS)
4718                 {
4719                         _TouchGestureDetector* pGestureDetector = null;
4720                         pMapEnumerator->GetKey(pGestureDetector);
4721
4722                         if (pGestureDetector == null)
4723                         {
4724                                 continue;
4725                         }
4726
4727                         _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
4728                         pMapEnumerator->GetValue(state);
4729
4730                         if (pGestureDetector->IsDelayTouchEventEnabled())
4731                         {
4732                                 existDelayTouchEventGesture = true;
4733                         }
4734                 }
4735                 delete pMapEnumerator;
4736         }
4737
4738         bool delayTouchEvent = false;
4739         if (existDelayTouchEventGesture)
4740         {
4741                 pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
4742                 if (pMapEnumerator)
4743                 {
4744                         while(pMapEnumerator->MoveNext() == E_SUCCESS)
4745                         {
4746                                 _TouchGestureDetector* pGestureDetector = null;
4747                                 pMapEnumerator->GetKey(pGestureDetector);
4748
4749                                 if (pGestureDetector == null)
4750                                 {
4751                                         continue;
4752                                 }
4753
4754                                 _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
4755                                 pMapEnumerator->GetValue(state);
4756
4757                                 if (state == _TOUCH_GESTURE_DETECTOR_STATE_STARTED)
4758                                 {
4759                                         delayTouchEvent = true;
4760                                         break;
4761                                 }
4762                         }
4763                         delete pMapEnumerator;
4764                 }
4765
4766                 return delayTouchEvent;
4767         }
4768         else
4769         {
4770                 return false;
4771         }
4772 }
4773
4774 bool
4775 _Control::IsPossibleToSendDelayedTouchEvent(void) const
4776 {
4777         bool existDelayTouchEventGesture = false;
4778
4779         IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>* pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
4780         if (pMapEnumerator)
4781         {
4782                 while(pMapEnumerator->MoveNext() == E_SUCCESS)
4783                 {
4784                         _TouchGestureDetector* pGestureDetector = null;
4785                         pMapEnumerator->GetKey(pGestureDetector);
4786
4787                         if (pGestureDetector == null)
4788                         {
4789                                 continue;
4790                         }
4791
4792                         _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
4793                         pMapEnumerator->GetValue(state);
4794
4795                         if (pGestureDetector->IsDelayTouchEventEnabled())
4796                         {
4797                                 existDelayTouchEventGesture = true;
4798                         }
4799                 }
4800                 delete pMapEnumerator;
4801         }
4802
4803         bool allFailed = true;
4804         if (existDelayTouchEventGesture)
4805         {
4806                 pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
4807                 if (pMapEnumerator)
4808                 {
4809                         while(pMapEnumerator->MoveNext() == E_SUCCESS)
4810                         {
4811                                 _TouchGestureDetector* pGestureDetector = null;
4812                                 pMapEnumerator->GetKey(pGestureDetector);
4813
4814                                 if (pGestureDetector == null)
4815                                 {
4816                                         continue;
4817                                 }
4818
4819                                 _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
4820                                 pMapEnumerator->GetValue(state);
4821
4822                                 if (state == _TOUCH_GESTURE_DETECTOR_STATE_SUCCESS)
4823                                 {
4824                                         allFailed = false;
4825                                         break;
4826                                 }
4827                         }
4828                         delete pMapEnumerator;
4829                 }
4830
4831                 return allFailed;
4832         }
4833         else
4834         {
4835                 return false;
4836         }
4837 }
4838
4839 bool
4840 _Control::IsCancelOnGestureSuccess(void) const
4841 {
4842         _TouchManager* pTouchManager = _TouchManager::GetInstance();
4843         SysAssert(pTouchManager != null);
4844
4845         IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>* pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
4846         if (pMapEnumerator)
4847         {
4848                 while(pMapEnumerator->MoveNext() == E_SUCCESS)
4849                 {
4850                         _TouchGestureDetector* pGestureDetector = null;
4851                         pMapEnumerator->GetKey(pGestureDetector);
4852
4853                         if (pGestureDetector == null)
4854                         {
4855                                 continue;
4856                         }
4857
4858                         _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
4859                         pMapEnumerator->GetValue(state);
4860
4861                         if (pGestureDetector->IsCancelTouchEventOnSuccessEnabled() && state == _TOUCH_GESTURE_DETECTOR_STATE_SUCCESS && !pTouchManager->IsTouchCanceledOnGestureSuccess())
4862                         {
4863                                 delete pMapEnumerator;
4864                                 return true;
4865                         }
4866                 }
4867                 delete pMapEnumerator;
4868         }
4869
4870         return false;
4871 }
4872
4873 bool
4874 _Control::IsSentDelayedEvent(void) const
4875 {
4876         return __isSentDelayedEvent;
4877 }
4878
4879 void
4880 _Control::SetSentDelayedEvent(bool sent)
4881 {
4882         __isSentDelayedEvent = sent;
4883 }
4884
4885 void
4886 _Control::SetSendingDelayedEvent(bool sending)
4887 {
4888         __isSendingDelayedEvent = sending;
4889 }
4890
4891 bool
4892 _Control::IsSendingDelayedEvent(void) const
4893 {
4894         return __isSendingDelayedEvent;
4895 }
4896
4897 void
4898 _Control::AddTouchInfo(const _TouchInfo& touchInfo)
4899 {
4900         _TouchManager* pTouchManager = _TouchManager::GetInstance();
4901         SysTryReturnVoidResult(NID_UI, pTouchManager, E_SYSTEM, "[E_SYSTEM] _TouchManager does not exist.");
4902
4903         _TouchInfo* pTouchInfo = new (std::nothrow) _TouchInfo;
4904         SysTryReturnVoidResult(NID_UI, pTouchInfo, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4905
4906         FloatPoint screenPoint = pTouchManager->GetScreenPoint(touchInfo.GetPointId());
4907         pTouchInfo->SetTouchInfo(touchInfo.GetPointId(), touchInfo.GetTouchStatus(), screenPoint, false, 0);
4908         __pDelayedTouchInfoList->Add(pTouchInfo);
4909 }
4910
4911 IListT<_TouchInfo*>*
4912 _Control::GetTouchInfoList(void)
4913 {
4914         return __pDelayedTouchInfoList;
4915 }
4916
4917 void
4918 _Control::SetVisualElement(_ControlVisualElement* pVisualElement)
4919 {
4920         __pVisualElement = pVisualElement;
4921 }
4922
4923 _VisualElement*
4924 _Control::GetVisualElement(void) const
4925 {
4926         return __pVisualElement;
4927 }
4928
4929 void
4930 _Control::PrintDescription(bool printChildren, int level)
4931 {
4932         int count = PrintDescription(printChildren, 0, level);
4933
4934         SysSecureLog(NID_UI, "%d controls were printed.", count);
4935 }
4936
4937 int
4938 _Control::PrintDescription(bool printChildren, int depth, int level)
4939 {
4940         const int PRINT_CONTROL_VE = 1;
4941         const int PRINT_CONTROL_EVAS = 2;
4942         const int PRINT_CONTROL_VE_EVAS = 3;
4943
4944         String indent(L"");
4945         String format(L"");
4946
4947         format.Format(LOG_LEN_MAX, L"%d", depth);
4948
4949         for (int i = 0; i < depth; i++)
4950         {
4951                 indent.Append(L"   ");
4952         }
4953
4954         indent.Append(format);
4955
4956         String delimiter(L"-------------------------------------------------------------------------------------------");
4957         SysSecureLog(NID_UI, "%ls", delimiter.GetPointer());
4958
4959         // Public
4960         String publicDescription = GetControlDelegate().GetDescription();
4961         if (!publicDescription.IsEmpty())
4962         {
4963                 SysSecureLog(NID_UI, "%ls %ls", indent.GetPointer(), publicDescription.GetPointer());
4964         }
4965
4966         _ControlManager* pControlManager = _ControlManager::GetInstance();
4967         SysTryReturn(NID_UI, pControlManager, null, E_SYSTEM, "[E_SYSTEM] _ControlManager does not exist.");
4968
4969         bool focused = false;
4970         _Control* pFocusedControl = pControlManager->GetFocusControl();
4971         if (pFocusedControl && (pFocusedControl == this))
4972         {
4973                 focused = true;
4974         }
4975
4976         // Core
4977         SysSecureLog(NID_UI, "%ls 0x%x(%d %ls) enable(%d) enableState(%d) visible(%d) visibleState(%d) clip(%d) movable(%d) resizable(%d)",
4978                 indent.GetPointer(), this, __controlHandle.ToInt(), GetName().GetPointer(), IsEnabled(), GetEnableState(), IsVisible(), GetVisibleState(),
4979                 IsClipToParent(), IsMovable(), IsResizable());
4980
4981         SysSecureLog(NID_UI, "%ls  inputEnableState(%d) focusable(%d) focused(%d) prevFocus(0x%x) nextFocus(0x%x) font(0x%x) fontName(%ls) fontFileName(%ls)",
4982                 indent.GetPointer(), GetInputEnableState(), IsFocusable(), focused, __pPreviousFocus, __pNextFocus, __pFont, __fontName.GetPointer(), __fontFileName.GetPointer());
4983
4984         Rectangle bounds = GetBounds();
4985         Dimension min = GetMinimumSize();
4986         Dimension max = GetMaximumSize();
4987         Rectangle clientBounds = GetClientBounds();
4988         Rectangle absoluteBounds = GetAbsoluteBounds();
4989
4990         SysSecureLog(NID_UI, "%ls bounds(%d %d %d %d) min(%d %d) max(%d %d) scrollPos(%.2f %.2f) cbounds(%d %d %d %d) abounds(%d %d %d %d)",
4991                 indent.GetPointer(), bounds.x, bounds.y, bounds.width, bounds.height,
4992                 min.width, min.height, max.width, max.height,
4993                 GetVerticalScrollPosition(), GetHorizontalScrollPosition(),
4994                 clientBounds.x, clientBounds.y, clientBounds.width, clientBounds.height,
4995                 absoluteBounds.x, absoluteBounds.y, absoluteBounds.width, absoluteBounds.height);
4996
4997         SysSecureLog(NID_UI, "%ls bgColor(0x%x) layoutable(%d) orientation(%d) drag(%d) drop(%d) area(%d) layer(%d)",
4998                 indent.GetPointer(), __backgroundColor.GetRGB32(), IsLayoutable(), GetOrientation(), IsDragEnabled(), IsDropEnabled(), GetArea(), GetLayer());
4999
5000         Canvas* pCanvas = GetCanvasN();
5001         if (pCanvas)
5002         {
5003                 Rectangle canvasBounds = pCanvas->GetBounds();
5004                 Color canvasBackgroundColor = pCanvas->GetBackgroundColor();
5005                 Color canvasForegroundColor = pCanvas->GetForegroundColor();
5006
5007                 SysSecureLog(NID_UI, "%ls canvas.bounds(%d %d %d %d) canvas.bgColor(0x%x) canvas.fgColor(0x%x)",
5008                         indent.GetPointer(), canvasBounds.x, canvasBounds.y, canvasBounds.width, canvasBounds.height, canvasBackgroundColor.GetRGB32(), canvasForegroundColor.GetRGB32());
5009
5010                 delete pCanvas;
5011         }
5012
5013         SysSecureLog(NID_UI, "%ls DataBindingContext(0x%x) ControlDelegate(0x%x) UserData(0x%x) destroying(%d)",
5014                 indent.GetPointer(), __pDataBindingContext, __pControlDelegate, __pUserData, __destroying);
5015
5016         // Ownees
5017         String ownees(L"");
5018
5019         for (int i = 0; i < GetOwneeCount(); ++i)
5020         {
5021                 String ownee(L"");
5022                 _Window* pOwnee = GetOwnee(i);
5023                 if (pOwnee)
5024                 {
5025                         ownee.Format(LOG_LEN_MAX, L"0x%x ", pOwnee);
5026                         ownees.Append(ownee);
5027                 }
5028         }
5029
5030         if (!ownees.IsEmpty())
5031         {
5032                 SysSecureLog(NID_UI, "%ls Ownees(%ls)", indent.GetPointer(), ownees.GetPointer());
5033         }
5034
5035         _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*__pVisualElement);
5036
5037         SysSecureLog(NID_UI, "%ls _VisualElement(0x%x) _VisualElementImpl(0x%x) VisualElementContentProvider(0x%x) VisualElementEventListener(0x%x)",
5038                 indent.GetPointer(), __pVisualElement, pVisualElementImpl, __pVisualElementContentProvider, __pVisualElementEventListener);
5039
5040         // Layout
5041         SysSecureLog(NID_UI, "%ls LayoutItemHandler(0x%x) PortraitLayout(0x%x) LandscapeLayout(0x%x) LayoutContainer(0x%x)",
5042                 indent.GetPointer(), __pLayoutItemHandler, __pPortraitLayout, __pLandscapeLayout, __pLayoutContainer);
5043
5044         // Derived class
5045         String description = GetDescription();
5046         if (!description.IsEmpty())
5047         {
5048                 SysSecureLog(NID_UI, "%ls %ls", indent.GetPointer(), description.GetPointer());
5049         }
5050
5051         // Print Gesture List
5052         IListT<_TouchGestureDetector*>* pGestureList = GetGestureDetectorList();
5053         SysTryReturn(NID_UI, pGestureList, 0, E_SYSTEM, "[E_SYSTEM] System error occurred.");
5054
5055         IEnumeratorT<_TouchGestureDetector*>* pEnumerator = pGestureList->GetEnumeratorN();
5056         SysTryReturn(NID_UI, pEnumerator, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
5057
5058         while (pEnumerator->MoveNext() == E_SUCCESS)
5059         {
5060                 _TouchGestureDetector* pGestureDetector = null;
5061                 pEnumerator->GetCurrent(pGestureDetector);
5062                 if (pGestureDetector)
5063                 {
5064                         SysSecureLog(NID_UI, "%ls AddedGesture : %ls", indent.GetPointer(), pGestureDetector->GetDescription().GetPointer());
5065                 }
5066         }
5067
5068         delete pEnumerator;
5069
5070         // Print Started Gesture List
5071         unique_ptr<IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState> > pStartedGestureEnumerator(GetStartedGestureDetectorEnumeratorN());
5072         SysTryReturn(NID_UI, pStartedGestureEnumerator, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
5073
5074         while (pStartedGestureEnumerator->MoveNext() == E_SUCCESS)
5075         {
5076                 _TouchGestureDetector* pStartedGestureDetector = null;
5077                 pStartedGestureEnumerator->GetKey(pStartedGestureDetector);
5078                 if (pStartedGestureDetector)
5079                 {
5080                         SysSecureLog(NID_UI, "%ls StartedGesture : %ls", indent.GetPointer(), pStartedGestureDetector->GetDescription().GetPointer());
5081                 }
5082         }
5083
5084         // new backtrace
5085         unique_ptr<IEnumeratorT<void*> > pNewEnumerator(__pNewBacktrace->GetEnumeratorN());
5086         SysTryReturn(NID_SHELL, pNewEnumerator, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5087
5088         String newBacktrace;
5089
5090         while (pNewEnumerator->MoveNext() == E_SUCCESS)
5091         {
5092                 void* pAddr = null;
5093                 pNewEnumerator->GetCurrent(pAddr);
5094
5095                 String addr(L"");
5096                 addr.Format(LOG_LEN_MAX, L"0x%x ", pAddr);
5097                 newBacktrace.Append(addr);
5098         }
5099
5100         if (!newBacktrace.IsEmpty())
5101         {
5102                 SysSecureLog(NID_UI, "%ls new [%ls]", indent.GetPointer(), newBacktrace.GetPointer());
5103         }
5104
5105         // delete backtrace
5106         unique_ptr<IEnumeratorT<void*> > pDeleteEnumerator(__pDeleteBacktrace->GetEnumeratorN());
5107         SysTryReturn(NID_SHELL, pDeleteEnumerator, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5108
5109         String deleteBacktrace;
5110
5111         while (pDeleteEnumerator->MoveNext() == E_SUCCESS)
5112         {
5113                 void* pAddr = null;
5114                 pDeleteEnumerator->GetCurrent(pAddr);
5115
5116                 String addr(L"");
5117                 addr.Format(LOG_LEN_MAX, L"0x%x ", pAddr);
5118                 deleteBacktrace.Append(addr);
5119         }
5120
5121         if (!deleteBacktrace.IsEmpty())
5122         {
5123                 SysSecureLog(NID_UI, "%ls delete [%ls]", indent.GetPointer(), deleteBacktrace.GetPointer());
5124         }
5125
5126         // Print VE and Evas
5127         switch (level)
5128         {
5129         case PRINT_CONTROL_VE:
5130                 _VeDebug::DumpVeTree(pVisualElementImpl, 0);
5131                 break;
5132
5133         case PRINT_CONTROL_EVAS:
5134                 _VeDebug::DumpEvasTree(pVisualElementImpl, 0);
5135                 break;
5136
5137         case PRINT_CONTROL_VE_EVAS:
5138                 _VeDebug::DumpVeTree(pVisualElementImpl, 0);
5139                 _VeDebug::DumpEvasTree(pVisualElementImpl, 0);
5140                 break;
5141
5142         default:
5143                 break;
5144         }
5145
5146         static int totalCount = 0;
5147
5148         if (depth == 0)
5149         {
5150                 totalCount = 0;
5151         }
5152
5153         if (printChildren)
5154         {
5155                 depth ++;
5156
5157                 int count = GetChildCount();
5158                 totalCount += count;
5159
5160                 for (int i = count - 1; i >= 0; --i)
5161                 {
5162                         _Control* pChild = GetChild(i);
5163                         if (pChild)
5164                         {
5165                                 pChild->PrintDescription(printChildren, depth, level);
5166                         }
5167                 }
5168         }
5169
5170         return totalCount;
5171 }
5172
5173 void
5174 _Control::PrintBacktrace(bool printChildren, bool newBacktrace)
5175 {
5176         int count = PrintBacktrace(printChildren, 0, newBacktrace);
5177
5178         SysSecureLog(NID_UI, "%d controls were printed.", count);
5179 }
5180
5181 int
5182 _Control::PrintBacktrace(bool printChildren, int depth, bool newBacktrace)
5183 {
5184         String indent(L"");
5185         String format(L"");
5186
5187         format.Format(LOG_LEN_MAX, L"%d", depth);
5188
5189         for (int i = 0; i < depth; i++)
5190         {
5191                 indent.Append(L"   ");
5192         }
5193
5194         indent.Append(format);
5195
5196         String delimiter(L"-------------------------------------------------------------------------------------------");
5197         SysSecureLog(NID_UI, "%ls", delimiter.GetPointer());
5198
5199         SysSecureLog(NID_UI, "%ls 0x%x(%d %ls)", indent.GetPointer(), this, __controlHandle.ToInt(), GetName().GetPointer());
5200
5201         Tizen::Base::Collection::ArrayListT<void*>* pBacktraceList = null;
5202
5203         if (newBacktrace)
5204         {
5205                 pBacktraceList = __pNewBacktrace.get();
5206         }
5207         else
5208         {
5209                 pBacktraceList = __pDeleteBacktrace.get();
5210         }
5211
5212         SysTryReturn(NID_UI, pBacktraceList, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5213
5214         int backtraceCount = pBacktraceList->GetCount();
5215         SysTryReturn(NID_UI, backtraceCount > 0, 0, E_INVALID_ARG, "[E_INVALID_ARG].");
5216
5217         void** pBacktrace = new (std::nothrow) void*[backtraceCount];
5218         SysTryReturn(NID_UI, pBacktrace, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5219
5220         unique_ptr<IEnumeratorT<void*> > pEnumerator(pBacktraceList->GetEnumeratorN());
5221         SysTryReturn(NID_UI, pEnumerator, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5222
5223         int i = 0;
5224         while (pEnumerator->MoveNext() == E_SUCCESS)
5225         {
5226                 void* pAddr = null;
5227                 pEnumerator->GetCurrent(pAddr);
5228
5229                 pBacktrace[i++] = pAddr;
5230         }
5231
5232         char** pSymbols = backtrace_symbols(pBacktrace, backtraceCount);
5233         SysTryReturn(NID_UI, pSymbols, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5234
5235         for (int j = 0; j < backtraceCount; j++)
5236         {
5237                 SysSecureLog(NID_UI, "%ls [%s]", indent.GetPointer(), pSymbols[j]);
5238         }
5239
5240         free(pSymbols);
5241         delete [] pBacktrace;
5242
5243         static int totalCount = 0;
5244
5245         if (depth == 0)
5246         {
5247                 totalCount = 0;
5248         }
5249
5250         if (printChildren)
5251         {
5252                 depth ++;
5253
5254                 int count = GetChildCount();
5255                 totalCount += count;
5256
5257                 for (int i = count - 1; i >= 0; --i)
5258                 {
5259                         _Control* pChild = GetChild(i);
5260                         if (pChild)
5261                         {
5262                                 pChild->PrintBacktrace(printChildren, depth, newBacktrace);
5263                         }
5264                 }
5265         }
5266
5267         return totalCount;
5268 }
5269
5270 void
5271 _Control::DragAndDropBegin(const _DragAndDropItem& dragAndDropItem)
5272 {
5273         _Window* pRootWindow = GetRootWindow();
5274         if (!pRootWindow)
5275         {
5276                 return;
5277         }
5278
5279         __isDragAndDropSource = true;
5280
5281         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
5282         if (!pEcoreEvas)
5283         {
5284                 return;
5285         }
5286
5287         pEcoreEvas->SetDragAndDropState(true);
5288         pEcoreEvas->DragAndDropBegin(*pRootWindow, dragAndDropItem.GetData());
5289         
5290         __pDragWindow = GetControlDelegate().OnDragAndDropBeginning();
5291 }
5292
5293 void
5294 _Control::DragAndDropDrop(void)
5295 {
5296         _Window* pRootWindow = GetRootWindow();
5297         if (!pRootWindow)
5298         {
5299                 return;
5300         }
5301         
5302         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
5303         pEcoreEvas->DragAndDropDrop(*pRootWindow);
5304
5305         GetControlDelegate().OnDragAndDropDropping();
5306
5307         // Check self drop.
5308         Point dropPosition = pEcoreEvas->GetDropPosition();
5309         Rectangle bounds = GetRootWindow()->GetBounds();
5310         if (bounds.Contains(dropPosition))
5311         {
5312                 _Control* pDropControl = _ControlManager::GetInstance()->GetTopmostTouchedControl(dropPosition, true);
5313                 if (pDropControl)
5314                 {
5315 // Send dnd events to Source.
5316 #if 0
5317                         if (pDropControl->IsDragAndDropSource() == false)
5318 #endif
5319                         {
5320                                 int pid = pEcoreEvas->GetProcessId(pRootWindow->GetNativeHandle());
5321                                 String dropData = pEcoreEvas->GetDropData();
5322                                 SysLog(NID_UI, "[DND][S:%d] Call OnDragAndDropDropped(%ls) => %ls.", pid, dropData.GetPointer(), pDropControl->GetName().GetPointer());
5323                                 _DragAndDropItem* pDragAndDropItem = _DragAndDropItem::CreateInstanceN(_DRAG_AND_DROP_TYPE_TEXT, dropData);
5324                                 pDropControl->GetControlDelegate().OnDragAndDropDropped(*pDragAndDropItem);
5325                                 delete pDragAndDropItem;
5326                         }
5327                 }
5328         }
5329
5330         __isDragAndDropSource = false;
5331 }
5332
5333 _Window*
5334 _Control::GetDragWindow(void) const
5335 {
5336         return __pDragWindow;
5337 }
5338
5339 bool
5340 _Control::IsDragAndDropSource(void) const
5341 {
5342         return __isDragAndDropSource;
5343 }
5344
5345 _Window*
5346 _Control::OnDragAndDropBeginning(void)
5347 {
5348         return null;
5349 }
5350
5351 void
5352 _Control::OnDragAndDropDropping(void)
5353 {
5354 }
5355
5356 void
5357 _Control::OnDragAndDropEntered(void)
5358 {
5359 }
5360
5361 void
5362 _Control::OnDragAndDropMoved(const FloatPoint& position)
5363 {
5364 }
5365
5366 void
5367 _Control::OnDragAndDropLeft(void)
5368 {
5369 }
5370
5371 void
5372 _Control::OnDragAndDropDropped(const _DragAndDropItem& dragAndDropItem)
5373 {
5374 }
5375
5376 _ITouchEventPreviewer*
5377 _Control::GetEventPreviewer(_IntToType<_UI_EVENT_TOUCH>) const
5378 {
5379         return __pTouchEventPreviewer;
5380 }
5381
5382 _IKeyEventPreviewer*
5383 _Control::GetEventPreviewer(_IntToType<_UI_EVENT_KEY>) const
5384 {
5385         return __pKeyEventPreviewer;
5386 }
5387
5388 _INotificationEventPreviewer*
5389 _Control::GetEventPreviewer(_IntToType<_UI_EVENT_NOTIFICAITON>) const
5390 {
5391         return __pNotificationEventPreviewer;
5392 }
5393
5394 _IKeyEventListener*
5395 _Control::GetEventListener(_IntToType<_UI_EVENT_KEY>) const
5396 {
5397         return __pKeyEventListener;
5398 }
5399
5400 _IFocusEventListener*
5401 _Control::GetEventListener(_IntToType<_UI_EVENT_FOCUS>) const
5402 {
5403         return __pFocusEventListener;
5404 }
5405
5406 _INotificationEventListener*
5407 _Control::GetEventListener(_IntToType<_UI_EVENT_NOTIFICAITON>) const
5408 {
5409         return __pNotificationEventListener;
5410 }
5411
5412 void
5413 _Control::SetEventPreviewer(_IntToType<_UI_EVENT_TOUCH>, _ITouchEventPreviewer* pPreviewer)
5414 {
5415         __pTouchEventPreviewer = pPreviewer;
5416 }
5417
5418 void
5419 _Control::SetEventPreviewer(_IntToType<_UI_EVENT_KEY>, _IKeyEventPreviewer* pPreviewer)
5420 {
5421         __pKeyEventPreviewer = pPreviewer;
5422 }
5423
5424 void
5425 _Control::SetEventPreviewer(_IntToType<_UI_EVENT_NOTIFICAITON>, _INotificationEventPreviewer* pPreviewer)
5426 {
5427         __pNotificationEventPreviewer = pPreviewer;
5428 }
5429
5430 void
5431 _Control::SetEventListener(_IntToType<_UI_EVENT_FOCUS>, _IFocusEventListener* pListener)
5432 {
5433         __pFocusEventListener = pListener;
5434 }
5435
5436 void
5437 _Control::SetEventListener(_IntToType<_UI_EVENT_NOTIFICAITON>, _INotificationEventListener* pListener)
5438 {
5439         __pNotificationEventListener = pListener;
5440 }
5441
5442 bool
5443 _Control::IsDragEnabled(void) const
5444 {
5445         return __dragEnabled;
5446 }
5447
5448 bool
5449 _Control::IsDropEnabled(void) const
5450 {
5451         return __dropEnabled;
5452 }
5453
5454 void
5455 _Control::SetDragEnabled(bool enabled)
5456 {
5457         __dragEnabled = enabled;
5458 }
5459
5460 void
5461 _Control::SetDropEnabled(bool enabled)
5462 {
5463         __dropEnabled = enabled;
5464 }
5465
5466 void
5467 _Control::SetTouchPressThreshold(float distance)
5468 {
5469         __touchMoveAllowance = static_cast<int>(distance * __screenDpi);
5470         __pressThresHold = distance;
5471 }
5472
5473 float
5474 _Control::GetTouchPressThreshold(void) const
5475 {
5476         return __pressThresHold;
5477 }
5478
5479 int
5480 _Control::GetTouchPressThresholdPixel(void) const
5481 {
5482         return __touchMoveAllowance;
5483 }
5484
5485 void
5486 _Control::SetChangingEventTarget(bool isChangingEventTarget)
5487 {
5488         __isChangingEventTarget = isChangingEventTarget;
5489 }
5490
5491 bool
5492 _Control::GetChangingEventTarget(void) const
5493 {
5494         return __isChangingEventTarget;
5495 }
5496
5497 void
5498 _Control::SetEventEnableState(bool enableState)
5499 {
5500         __isEventEnableState = enableState;
5501 }
5502
5503 bool
5504 _Control::IsEventEnabled(void) const
5505 {
5506         return __isEventEnableState;
5507 }
5508
5509 void
5510 _Control::SetPreviousFocus(_Control* pPreviousFocus)
5511 {
5512         __pPreviousFocus = pPreviousFocus;
5513
5514 }
5515 void
5516 _Control::SetNextFocus(_Control* pNextFocus)
5517 {
5518         __pNextFocus = pNextFocus;
5519 }
5520 _Control*
5521 _Control::GetPreviousFocus(void) const
5522 {
5523         return  __pPreviousFocus;
5524 }
5525 _Control*
5526 _Control::GetNextFocus(void) const
5527 {
5528         return __pNextFocus;
5529 }
5530
5531 void
5532 _Control::OnDrawFocus(void)
5533 {
5534         if (__pFocusVisualElement.get() == null)
5535         {
5536                 unique_ptr<VisualElement, _VisualElementDeleter> pFocusVisualElement (new (std::nothrow) VisualElement, _VisualElementDeleter());
5537                 SysTryReturn(NID_UI, pFocusVisualElement, , E_SYSTEM, "[E_SYSTEM] System error");
5538
5539                 result r = pFocusVisualElement->Construct();
5540                 SysTryReturn(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error");
5541
5542                 _VisualElementImpl* pImpl = _VisualElementImpl::GetInstance(*pFocusVisualElement);
5543                 SysTryReturn(NID_UI, pImpl, , E_SYSTEM, "[E_SYSTEM] pImpl System error");
5544                 pImpl->SetZOrderGroup(GetZOrderGroupOfVisualElement(_CONTROL_LAYER_SYSTEM));
5545
5546                 __pFocusVisualElement.reset(pFocusVisualElement.release());
5547                 __pFocusVisualElement->SetImplicitAnimationEnabled(false);
5548
5549                 _VisualElement* pControVisualElement = this->GetVisualElement();
5550                 pControVisualElement->AttachChild(*__pFocusVisualElement);
5551
5552
5553                 if (__pFocusVisualElement)
5554                 {
5555                         Rectangle rectangle = GetBounds();
5556                         __pFocusVisualElement->SetBounds(FloatRectangle(0, 0, rectangle.width, rectangle.height));
5557                         unique_ptr<Canvas>pCanvas(__pFocusVisualElement->GetCanvasN());
5558                         if (pCanvas)
5559                         {
5560                                 pCanvas->SetBackgroundColor(0x55555555);
5561                                 pCanvas->Clear();
5562                         }
5563                         Color contentHighlightedColor;
5564                         GET_COLOR_CONFIG(FOCUSUI::CONTENT_BG_HIGHLIGHTED, contentHighlightedColor);
5565                         Bitmap* pBitmap = null;
5566                         Bitmap* pTempBitmap = null;
5567                         result r = GET_BITMAP_CONFIG_N(FOCUSUI::FOCUS, BITMAP_PIXEL_FORMAT_ARGB8888, pTempBitmap);
5568                         pBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pTempBitmap, Color::GetColor(COLOR_ID_MAGENTA), contentHighlightedColor);
5569
5570                         if (pBitmap)
5571                         {
5572                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBitmap))
5573                                 {
5574                                         if (pCanvas)
5575                                         {
5576                                                 r = pCanvas->DrawNinePatchedBitmap(FloatRectangle(0.0f, 0.0f, rectangle.width, rectangle.height), *pBitmap);
5577                                         }
5578                                 }
5579                                 else
5580                                 {
5581                                         if (pCanvas)
5582                                         {
5583                                                 r = pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, rectangle.width, rectangle.height), *pBitmap);
5584                                         }
5585                                 }
5586                         }
5587
5588                 }
5589         }
5590         __pFocusVisualElement->SetShowState(true);
5591 }
5592
5593 void
5594 _Control::OnChildControlFocusMoved(const _Control& control)
5595 {
5596 }
5597
5598 void
5599 _Control::OnDescendantControlFocusMoved(const _Control& control)
5600 {
5601 }
5602
5603 bool
5604 _Control::IsChildControlFocusManage(void) const
5605 {
5606         return false;
5607 }
5608
5609 result
5610 _Control::SetFontFromFile(const String& fileName)
5611 {
5612         result r = E_SUCCESS;
5613
5614         if (__fontFileName.Equals(fileName))
5615         {
5616                 return E_SUCCESS;
5617         }
5618
5619         __isControlFontChanged = true;
5620         __fontFileName = fileName;
5621         __fontName.Clear();
5622
5623         Font* pFont = GetFallbackFont();
5624
5625         if (pFont == null)
5626         {
5627                 r = GetLastResult();
5628                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
5629         }
5630         return E_SUCCESS;
5631 }
5632
5633 Tizen::Base::String
5634 _Control::GetFontFile(void) const
5635 {
5636         return __fontFileName;
5637 }
5638 void
5639 _Control::DrawFocus(void)
5640 {
5641         _IControlDelegate& delegate = GetControlDelegate();
5642         delegate.OnDrawFocus();
5643 }
5644
5645 void
5646 _Control::MakeFocusList(const _Control* pControl, IListT<_Control*>*  pFocusControlList) const
5647 {
5648         int childCount = pControl->GetChildCount();
5649         for(int i = 0; i < childCount; i++)
5650         {
5651                 _Control* pChildControl = pControl->GetChild(i);
5652                 Rectangle rect = pChildControl->GetAbsoluteBounds();
5653                 unique_ptr<IEnumeratorT<_Control*> > pEnum (pFocusControlList->GetEnumeratorN());
5654                 int index = 0;
5655                 while (pEnum->MoveNext() == E_SUCCESS)
5656                 {
5657                         _Control* pEnumeratorControl = null;
5658                         pEnum->GetCurrent(pEnumeratorControl);
5659                         if (pEnumeratorControl != null)
5660                         {
5661                                 Rectangle enumeratorRect = pEnumeratorControl->GetAbsoluteBounds();
5662                                 if(enumeratorRect.y > rect.y)
5663                                 {
5664                                         break;
5665                                 }
5666                                 else if (enumeratorRect.y == rect.y)
5667                                 {
5668                                         if(enumeratorRect.x > rect.x)
5669                                         {
5670                                                 break;
5671                                         }
5672                                 }
5673
5674                                 index ++;
5675                         }
5676                 }
5677                 pFocusControlList->InsertAt(pChildControl, index);
5678         }
5679 }
5680
5681 void
5682 _Control::MakeChildContainerFocusList(const _Control* pControl, int startIndex , IListT<_Control*>*  pFocusControlList) const
5683 {
5684         unique_ptr<IListT<_Control*> > pTempList (new (std::nothrow) ArrayListT<_Control*>);
5685         SysTryReturnVoidResult(NID_UI_CTRL, pTempList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
5686         MakeFocusList(pControl, pTempList.get());
5687
5688         unique_ptr<IEnumeratorT<_Control*> > pTempEnum(pTempList->GetEnumeratorN());
5689         int index = ++startIndex;
5690         while (pTempEnum->MoveNext() == E_SUCCESS)
5691         {
5692                 _Control* pEnumeratorControl = null;
5693                 pTempEnum->GetCurrent(pEnumeratorControl);
5694                 pFocusControlList->InsertAt(pEnumeratorControl, index);
5695                 index ++;
5696         }
5697 }
5698
5699 Tizen::Base::Collection::IListT<_Control*>*
5700 _Control::GetFocusListN(void) const
5701 {
5702         unique_ptr<IListT<_Control*> > pControlFocusList (new (std::nothrow) ArrayListT<_Control*>);
5703         SysTryReturn(NID_UI_CTRL, pControlFocusList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5704         MakeFocusList(this, pControlFocusList.get());
5705
5706         unique_ptr<IEnumeratorT<_Control*> > pEnum(pControlFocusList->GetEnumeratorN());
5707         SysTryReturn(NID_UI_CTRL, pEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
5708         int i =0;
5709         int nextContainerIndex = -1;
5710         while (pEnum->MoveNext() == E_SUCCESS)
5711         {
5712                 _Control* pEnumeratorControl = null;
5713                 pEnum->GetCurrent(pEnumeratorControl);
5714
5715                 if (nextContainerIndex < i)
5716                 {
5717                         if (pEnumeratorControl->IsChildControlFocusManage() == false)
5718                         {
5719                                 MakeChildContainerFocusList(pEnumeratorControl, i, pControlFocusList.get());
5720                                 nextContainerIndex = i;
5721                                 pEnum.reset(pControlFocusList->GetEnumeratorN());
5722                                 i = -1;
5723                         }
5724                 }
5725                 i++;
5726         }
5727         return pControlFocusList.release();
5728 }
5729
5730 bool
5731 _Control::IsChildAttachable(_Control& child) const
5732 {
5733         if (dynamic_cast <_Window*>(&child) != null)
5734         {
5735                 return false;
5736         }
5737
5738         return true;
5739 }
5740
5741 }} // Tizen::Ui
5742