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