Tizen 2.1 base
[framework/osp/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 Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FUi_Control.cpp
19  * @brief               This is the implementation file for the _Control class.
20  */
21
22 #include <new>
23 #include <unique_ptr.h>
24 #include <FBaseColLinkedListT.h>
25 #include <FBaseColArrayListT.h>
26 #include <FBaseColHashMapT.h>
27 #include <FBaseSysLog.h>
28 #include <FGrpFloatRectangle.h>
29 #include <FUiAnimVisualElementContentProvider.h>
30 #include <FBase_Log.h>
31 #include "FUi_Control.h"
32 #include "FUi_ControlManager.h"
33 #include "FUi_Window.h"
34 #include "FUi_EcoreEvasMgr.h"
35 #include "FUi_EcoreEvas.h"
36 #include "FUi_LayoutLayoutContainer.h"
37 #include "FUi_LayoutAbsoluteLayout.h"
38 #include "FUi_LayoutILayoutItemHandler.h"
39 #include "FUi_TouchManager.h"
40 #include "FUi_DataBindingContext.h"
41 #include "FUi_TouchLongPressGestureDetector.h"
42 #include "FUi_TouchTapGestureDetector.h"
43 #include "FUi_AccessibilityContainer.h"
44 #include "FUi_ResourceManager.h"
45 #include "FUiAnim_VisualElement.h"
46 #include "FUiAnim_ControlVisualElement.h"
47 #include "FUiAnim_VisualElementImpl.h"
48 #include "FUiCtrl_Form.h"
49 #include "FUiCtrl_Frame.h"
50
51 using namespace std;
52 using namespace Tizen::Base;
53 using namespace Tizen::Base::Collection;
54 using namespace Tizen::Base::Runtime;
55 using namespace Tizen::Graphics;
56 using namespace Tizen::Ui;
57 using namespace Tizen::Ui::Animations;
58
59 namespace {
60
61 int
62 GetZOrderGroupOfVisualElement(_ControlLayer layer)
63 {
64         switch (layer)
65         {
66         case _CONTROL_LAYER_OVERLAY:
67                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL - 1;
68         case _CONTROL_LAYER_CLIENT_BOTTOM:
69                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL;
70         case _CONTROL_LAYER_NONE:
71                 // fall through
72         case _CONTROL_LAYER_CLIENT_MIDDLE:
73                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 1;
74         case _CONTROL_LAYER_CLIENT_TOP:
75                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 2;
76         case _CONTROL_LAYER_SYSTEM:
77                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 3;
78         default:
79                 SysAssert(false);
80                 return _ControlVisualElement::Z_ORDER_GROUP_CONTROL + 1;
81         }
82 }
83
84 inline bool
85 AdjustSizeToRange(int& width, int& height, const Dimension& minDim, const Dimension& maxDim)
86 {
87         bool changed = false;
88         if (width < minDim.width)
89         {
90                 width = minDim.width;
91                 changed = true;
92         }
93         if (height < minDim.height)
94         {
95                 height = minDim.height;
96                 changed = true;
97         }
98
99         if (width > maxDim.width)
100         {
101                 width = maxDim.width;
102                 changed = true;
103         }
104         if (height > maxDim.height)
105         {
106                 height = maxDim.height;
107                 changed = true;
108         }
109
110         return changed;
111 }
112
113 inline bool
114 AdjustSizeToRange(Dimension& dim, const Dimension& minDim, const Dimension& maxDim)
115 {
116         return AdjustSizeToRange(dim.width, dim.height, minDim, maxDim);
117 }
118
119 // E_OUT_OF_MEMORY
120 // E_SYSTEM
121 _ControlVisualElement*
122 CreateVisualElementN(void)
123 {
124         ClearLastResult();
125
126         _ControlVisualElement* pVisualElement = new (std::nothrow) _ControlVisualElement;
127         SysTryReturn(NID_UI, pVisualElement, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
128
129         SysTryCatch(NID_UI,
130                 pVisualElement->ConstructControlVisualElement() == E_SUCCESS, ,
131                 E_SYSTEM, "[E_SYSTEM] System error occurred.");
132
133         pVisualElement->SetImplicitAnimationEnabled(false);
134         pVisualElement->SetShowState(true);
135         pVisualElement->SetBackBufferEnabled(true);
136         pVisualElement->SetRedrawOnResizeEnabled(true);
137         pVisualElement->SetSurfaceOpaque(false);
138
139         return pVisualElement;
140
141 CATCH:
142         //delete pVisualElement;
143         pVisualElement->Destroy();
144         return null;
145 }
146
147 _Control::GestureMap*
148 CreateGestureMapN(void)
149 {
150         ClearLastResult();
151
152         _Control::GestureMap* pGestureMap = new (std::nothrow) _Control::GestureMap;
153         SysTryReturn(NID_UI, pGestureMap, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
154
155         SysTryCatch(NID_UI,
156                 pGestureMap->Construct() == E_SUCCESS, ,
157                 E_SYSTEM, "[E_SYSTEM] System error occurred.");
158
159         return pGestureMap;
160
161 CATCH:
162         delete pGestureMap;
163         return null;
164 }
165
166 // E_OUT_OF_MEMORY
167 _Control::ControlList*
168 CreateControlListN(void)
169 {
170         ClearLastResult();
171
172         _Control::ControlList* pControlList = new (std::nothrow) _Control::ControlList;
173         SysTryReturn(NID_UI, pControlList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
174
175         return pControlList;
176 }
177
178 // E_OUT_OF_MEMORY
179 _Control::WindowList*
180 CreateWindowListN(void)
181 {
182         ClearLastResult();
183
184         _Control::WindowList* pWindowList = new (std::nothrow) _Control::WindowList;
185         SysTryReturn(NID_UI, pWindowList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
186
187         return pWindowList;
188 }
189
190 // E_OUT_OF_MEMORY
191 // E_SYSTEM
192 _Layout::LayoutContainer*
193 CreateLayoutContainerN(_Layout::ILayoutItemHandler* pLayoutItemHandler)
194 {
195         ClearLastResult();
196         result r = E_SUCCESS;
197
198         _Layout::LayoutContainer* pLayoutContainer = null;
199         _Layout::AbsoluteLayout* pAbsLayout = null;
200
201         pLayoutContainer = new (std::nothrow) _Layout::LayoutContainer();
202         SysTryReturn(NID_UI, pLayoutContainer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage");
203         if (IsFailed(GetLastResult()))
204         {
205                 goto CATCH;
206         }
207
208         pAbsLayout = new (std::nothrow) _Layout::AbsoluteLayout();
209         SysTryCatch(NID_UI, pAbsLayout, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
210         if (IsFailed(GetLastResult()))
211         {
212                 goto CATCH;
213         }
214
215         pLayoutContainer->SetItemHandler(pLayoutItemHandler);
216
217         r = pLayoutContainer->SetDefaultLayout(*pAbsLayout);
218         SysTryCatch(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] System error occurred.");
219
220         return pLayoutContainer;
221
222 CATCH:
223         delete pAbsLayout;
224         delete pLayoutContainer;
225
226         return null;
227 }
228
229 } // Anonymous namespace
230
231 namespace Tizen { namespace Ui
232 {
233
234 IMPLEMENT_PROPERTY(_Control);
235
236 class _Control::ControlVisualElementContentProvider
237         : public VisualElementContentProvider
238 {
239
240 private:
241         _Control& __control;
242
243 public:
244         ControlVisualElementContentProvider(_Control& control)
245                 : __control(control)
246         {
247         }
248
249         virtual ~ControlVisualElementContentProvider(void)
250         {
251         }
252
253         virtual bool PrepareDraw(VisualElement& target)
254         {
255                 _ControlVisualElement* pCVE = dynamic_cast <_ControlVisualElement*>(&target);
256                 if (!pCVE)
257                 {
258                         return false;
259                 }
260
261                 Color bgColor = __control.GetBackgroundColor();
262
263                 pCVE->SetBackgroundColor(
264                         _Colorf(
265                                 (float) bgColor.GetRed() / 255.0f,
266                                 (float) bgColor.GetGreen() / 255.0f,
267                                 (float) bgColor.GetBlue() / 255.0f,
268                                 (float) bgColor.GetAlpha() / 255.0f
269                                 )
270                         );
271
272                 __control.GetControlDelegate().OnDraw();
273
274                 target.SetFlushNeeded();
275
276                 return false;
277         }
278
279         virtual HitTestResult HitTest(VisualElement& target, const FloatPoint& point)
280         {
281                 return __control.GetControlDelegate().HitTest(point);
282         }
283
284 private:
285         ControlVisualElementContentProvider(const ControlVisualElementContentProvider& rhs);
286         ControlVisualElementContentProvider& operator =(const ControlVisualElementContentProvider& rhs);
287 };
288
289 class _Control::ControlVisualElementEventListener
290         : public IVisualElementEventListener
291 {
292 public:
293         ControlVisualElementEventListener(_Control& control)
294                 : __control(control)
295         {
296         }
297
298         virtual ~ControlVisualElementEventListener(void)
299         {
300         }
301
302 public:
303         virtual void OnChildAttached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& child)
304         {
305         }
306
307         virtual void OnChildDetached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& child)
308         {
309         }
310
311         virtual void OnAttached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& parent)
312         {
313         }
314
315         virtual void OnDetached(Tizen::Ui::Animations::VisualElement& source, Tizen::Ui::Animations::VisualElement& parent)
316         {
317         }
318
319         virtual result OnTransformChanging(Tizen::Ui::Animations::VisualElement& source, FloatMatrix4& newTransform)
320         {
321                 return E_SUCCESS;
322         }
323
324         virtual void OnTransformChanged(Tizen::Ui::Animations::VisualElement& source, const FloatMatrix4& previousTransform)
325         {
326         }
327
328         virtual result OnChildrenTransformChanging(Tizen::Ui::Animations::VisualElement& source, FloatMatrix4& newTransform)
329         {
330                 for (int i = 0; i < __control.GetChildCount(); ++i)
331                 {
332                         _Control* pChild = __control.GetChild(i);
333                         if (!pChild)
334                         {
335                                 continue;
336                         }
337
338                         if (pChild->GetArea() == _CONTROL_AREA_SYSTEM)
339                         {
340                                 _VisualElement* pVisualElement = pChild->GetVisualElement();
341                                 if (pVisualElement)
342                                 {
343                                         FloatMatrix4 inverseMatrix(newTransform);
344                                         inverseMatrix.Invert();
345
346                                         result r = pVisualElement->SetTransformMatrix(inverseMatrix);
347                                         if (r != E_SUCCESS)
348                                         {
349                                                 continue;
350                                         }
351                                 }
352                         }
353                 }
354
355                 return E_SUCCESS;
356         }
357
358         virtual void OnChildrenTransformChanged(Tizen::Ui::Animations::VisualElement& source, const FloatMatrix4& previousTransform)
359                 {
360         }
361
362         virtual result OnBoundsChanging(Tizen::Ui::Animations::VisualElement& source, FloatRectangle& newBounds)
363         {
364                 return E_SUCCESS;
365         }
366
367         virtual void OnBoundsChanged(Tizen::Ui::Animations::VisualElement& source, const FloatRectangle& previousBounds)
368         {
369         }
370
371         virtual void OnShowStateChanged(Tizen::Ui::Animations::VisualElement& source, bool previousShowState)
372         {
373         }
374
375 private:
376         ControlVisualElementEventListener(const ControlVisualElementEventListener& rhs);
377         ControlVisualElementEventListener& operator =(const ControlVisualElementEventListener& rhs);
378
379 private:
380         _Control& __control;
381 }; // ControlVisualElementEventListener
382
383 // Layout Item Handler
384 class _Control::LayoutItemHandler
385         : public _Layout::ILayoutItemHandler
386 {
387 public:
388         LayoutItemHandler(_Control* pControl)
389                 : __pControl(pControl)
390         {
391                 SysAssert(__pControl);
392         }
393
394         void SetItemVisibleState(bool visible)
395         {
396                 __pControl->SetVisibleState(visible);
397         }
398
399         result SetItemBounds(const Rectangle& rect)
400         {
401                 SysAssert(__pControl->IsInSizeRange(Dimension(rect.width, rect.height)));
402                 FloatRectangle floatBounds(rect.x, rect.y, rect.width, rect.height);
403                 return __pControl->SetBoundsFinal(floatBounds, false, true);
404         }
405
406         Rectangle GetItemBounds(void) const
407         {
408                 return __pControl->GetBounds();
409         }
410
411         Rectangle GetItemClientBoundsFromSize(const Dimension& size) const
412         {
413                 return __pControl->GetClientBounds(size);
414         }
415
416         Dimension GetItemContentSize(void) const
417         {
418                 return __pControl->GetControlDelegate().GetContentSize();
419         }
420
421         Dimension GetItemMinimumSize(void) const
422         {
423                 return __pControl->GetMinimumSize();
424         }
425
426         Dimension GetItemMaximumSize(void) const
427         {
428                 return __pControl->GetMaximumSize();
429         }
430
431         result OnItemMeasure(int& width, int& height)
432         {
433                 Dimension evaluatedSize(width, height);
434                 __pControl->GetControlDelegate().OnEvaluateSize(evaluatedSize);
435
436                 width = evaluatedSize.width;
437                 height = evaluatedSize.height;
438
439                 return E_SUCCESS;
440         }
441
442 private:
443         LayoutItemHandler(const LayoutItemHandler& rhs);
444         LayoutItemHandler& operator =(const LayoutItemHandler& rhs);
445
446 private:
447         _Control* __pControl;
448 }; // LayoutItemHandler
449
450 // E_OUT_OF_MEMORY
451 // E_SYSTEM
452 _Control*
453 _Control::CreateControlN(void)
454 {
455         _Control* pControl = new (std::nothrow) _Control;
456         SysTryReturn(NID_UI, pControl, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
457         if (IsFailed(GetLastResult()))
458         {
459                 goto CATCH;
460         }
461
462         pControl->AcquireHandle();
463
464         SysAssert(GetLastResult() == E_SUCCESS);
465         return pControl;
466
467 CATCH:
468         delete pControl;
469         return null;
470 }
471
472 void
473 _Control::ResetEventListeners(void)
474 {
475         __pFocusEventListener = this;
476         __pNotificationEventListener = this;
477 }
478
479 void
480 _Control::SetControlDelegate(_IControlDelegate& delegate)
481 {
482         __pControlDelegate = &delegate;
483 }
484
485 void
486 _Control::ResetControlDelegate(void)
487 {
488         __pControlDelegate = this;
489 }
490
491 _IControlDelegate&
492 _Control::GetControlDelegate(void) const
493 {
494         if (__destroying)
495         {
496                 return const_cast<_Control&>(*this);
497         }
498
499         SysAssert(__pControlDelegate);
500         return *__pControlDelegate;
501 }
502
503 void
504 _Control::SetPropagatedTouchEventListener(_IPropagatedTouchEventListener* pListener)
505 {
506         __pPropagatedTouchEventListener = pListener;
507 }
508
509 _IPropagatedTouchEventListener*
510 _Control::GetPropagatedTouchEventListener(void) const
511 {
512         if (__destroying)
513         {
514                 return const_cast<_Control*>(this);
515         }
516
517         SysAssert(__pPropagatedTouchEventListener);
518         return __pPropagatedTouchEventListener;
519 }
520
521 void
522 _Control::SetPropagatedKeyEventListener(_IPropagatedKeyEventListener* pListener)
523 {
524         __pPropagatedKeyEventListener = pListener;
525 }
526
527 _IPropagatedKeyEventListener*
528 _Control::GetPropagatedKeyEventListener(void) const
529 {
530         if (__destroying)
531         {
532                 return const_cast<_Control*>(this);
533         }
534
535         SysAssert(__pPropagatedKeyEventListener);
536         return __pPropagatedKeyEventListener;
537 }
538
539 bool
540 _Control::OnPreviewKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
541 {
542         return false;
543 }
544
545 bool
546 _Control::OnPreviewKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
547 {
548         return false;
549 }
550
551 bool
552 _Control::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
553 {
554         //SysLog(NID_UI, ">>> [core] OnKeyPressed(%d, %d)", keyInfo.GetKeyCode(), keyInfo.GetKeyModifier());
555         return false;
556 }
557
558 bool
559 _Control::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
560 {
561         //SysLog(NID_UI, ">>> [core] OnKeyReleased(%d, %d)", keyInfo.GetKeyCode(), keyInfo.GetKeyModifier());
562         return false;
563 }
564
565 bool
566 _Control::TranslateKeyEventInfo(const _Control& source, _KeyInfo& keyInfo)
567 {
568         return false;
569 }
570
571 _UiTouchEventDelivery
572 _Control::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
573 {
574         return _UI_TOUCH_EVENT_DELIVERY_YES;
575 }
576
577 _UiTouchEventDelivery
578 _Control::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
579 {
580         return _UI_TOUCH_EVENT_DELIVERY_YES;
581 }
582
583 _UiTouchEventDelivery
584 _Control::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
585 {
586         return _UI_TOUCH_EVENT_DELIVERY_YES;
587 }
588
589 _UiTouchEventDelivery
590 _Control::OnPreviewTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
591 {
592         return _UI_TOUCH_EVENT_DELIVERY_YES;
593 }
594
595 bool
596 _Control::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
597 {
598         return false;
599 }
600
601 bool
602 _Control::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
603 {
604         return false;
605 }
606
607 bool
608 _Control::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
609 {
610         return false;
611 }
612
613 bool
614 _Control::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
615 {
616         return false;
617 }
618
619 bool
620 _Control::OnFocusGained(const _Control& source)
621 {
622         return false;
623 }
624
625 bool
626 _Control::OnFocusLost(const _Control& source)
627 {
628         return false;
629 }
630
631 bool
632 _Control::OnPreviewNotifiedN(const _Control& source, IList* pArgs)
633 {
634         return false;
635 }
636
637 bool
638 _Control::OnNotifiedN(const _Control& source, IList* pArgs)
639 {
640         return false;
641 }
642
643 bool
644 _Control::IsMovable(void) const
645 {
646         ClearLastResult();
647         return __movable;
648 }
649
650 bool
651 _Control::IsResizable(void) const
652 {
653         ClearLastResult();
654         return __resizable;
655 }
656
657 Dimension
658 _Control::GetContentSize(void) const
659 {
660         ClearLastResult();
661         return GetSize();
662 }
663
664 HitTestResult
665 _Control::HitTest(const FloatPoint& point)
666 {
667         _VisualElementImpl* pVisualElementImpl = _VisualElementImpl::GetInstance(*__pVisualElement);
668
669         if (pVisualElementImpl)
670         {
671                 if (pVisualElementImpl->HitTestI(point) == _VisualElementImpl::HITTEST_MATCH)
672                 {
673                         return HIT_TEST_MATCH;
674                 }
675         }
676
677         return HIT_TEST_NOWHERE;
678 }
679
680 int
681 _Control::GetVerticalScrollPosition(void) const
682 {
683         return 0;
684 }
685
686 int
687 _Control::GetHorizontalScrollPosition(void) const
688 {
689         return 0;
690 }
691
692 _ControlOrientation
693 _Control::GetOrientation(void) const
694 {
695         ClearLastResult();
696         return __orientation;
697 }
698
699 void
700 _Control::OnDraw(void)
701 {
702 }
703
704 Canvas*
705 _Control::OnCanvasRequestedN(const Dimension& size)
706 {
707
708         return null;
709 }
710
711 Bitmap*
712 _Control::OnCapturedBitmapRequestedN(void)
713 {
714
715         return null;
716 }
717
718 result
719 _Control::OnAttaching(const _Control* pParent)
720 {
721         return E_SUCCESS;
722 }
723
724 result
725 _Control::OnAttached(void)
726 {
727         return E_SUCCESS;
728 }
729
730 result
731 _Control::OnAttachingToMainTree(const _Control* pParent)
732 {
733         return E_SUCCESS;
734 }
735
736 result
737 _Control::OnAttachedToMainTree(void)
738 {
739         return E_SUCCESS;
740 }
741
742 result
743 _Control::OnDetachingFromMainTree(void)
744 {
745         return E_SUCCESS;
746 }
747
748 void
749 _Control::OnAttachingFailed(const _Control& parent)
750 {
751 }
752
753 result
754 _Control::OnDetaching(void)
755 {
756         return E_SUCCESS;
757 }
758
759 result
760 _Control::OnBoundsChanging(const Rectangle& bounds)
761 {
762         return E_SUCCESS;
763 }
764
765 void
766 _Control::OnBoundsChanged(void)
767 {
768
769 }
770
771 void
772 _Control::OnEvaluateSize(Dimension& evaluatedSize)
773 {
774 }
775
776 void
777 _Control::OnParentBoundsChanged(const _Control& parent)
778 {
779 }
780
781
782 void
783 _Control::OnChildAttached(const _Control& child)
784 {
785 }
786
787 void
788 _Control::OnChildDetaching(const _Control& child)
789 {
790 }
791
792 void
793 _Control::OnChildDetached(const _Control& child)
794 {
795 }
796
797 void
798 _Control::OnChildBoundsChanged(const _Control& child)
799 {
800 }
801
802 void
803 _Control::OnChildVisibleStateChanged(const _Control& child)
804 {
805 }
806
807 void
808 _Control::OnChangeLayout(_ControlOrientation orientation)
809 {
810 }
811
812 void
813 _Control::OnZOrderChanging(_ControlZOrderUpdate zOrderUpdate)
814 {
815 }
816
817 void
818 _Control::OnVisibleStateChanging(void)
819 {
820 }
821
822 void
823 _Control::OnVisibleStateChanged(void)
824 {
825 }
826
827 void
828 _Control::OnAncestorVisibleStateChanged(const _Control& control)
829 {
830 }
831
832 void
833 _Control::OnAncestorEnableStateChanged(const _Control& control)
834 {
835 }
836
837 void
838 _Control::OnTouchPressHandled(const _Control& control)
839 {
840 }
841
842 void
843 _Control::OnTouchReleaseHandled(const _Control& control)
844 {
845 }
846
847 void
848 _Control::OnTouchMoveHandled(const _Control& control)
849 {
850 }
851
852 void
853 _Control::OnFontChanged(Tizen::Graphics::Font* pFont)
854 {
855 }
856
857 void
858 _Control::OnFontInfoRequested(unsigned long& style, int& size)
859 {
860 }
861
862 void
863 _Control::OnTouchCancelHandled(const _Control& control)
864 {
865 }
866
867 void
868 _Control::Accept(Visitor& visitor)
869 {
870         ClearLastResult();
871
872         VisitType visitType = visitor.Visit(*this);
873
874         switch (visitType)
875         {
876         case VISIT_STOP:
877                 break;
878
879         case VISIT_DOWNWARD:
880                 for (int i = 0; i < GetChildCount(); ++i) // [Postpone] Keep handle list before iternation.
881                 {
882                         _Control* pChild = GetChild(i);
883                         if (pChild)
884                         {
885                                 pChild->Accept(visitor);
886                         }
887                 }
888                 break;
889
890         case VISIT_UPWARD:
891                 {
892                         _Control* pParent = GetParent();
893                         if (pParent)
894                         {
895                                 pParent->Accept(visitor);
896                         }
897                 }
898                 break;
899
900         default:
901                 break;
902         }
903 }
904
905 void
906 _Control::Accept(Visitor& visitor) const
907 {
908         const_cast <_Control*>(this)->Accept(visitor);
909 }
910
911 void
912 _Control::Draw(bool recursive)
913 {
914         ClearLastResult();
915
916         Invalidate(recursive);
917         GetVisualElement()->Draw();
918 }
919
920 void
921 _Control::Show(void)
922 {
923         GetVisualElement()->Flush();
924         ClearLastResult();
925
926         SysAssert(GetLastResult() == E_SUCCESS);
927 }
928
929 void
930 _Control::ChangeLayout(_ControlOrientation orientation)
931 {
932         ClearLastResult();
933
934         struct _Visitor
935                 : public Visitor
936         {
937                 _Visitor(_ControlOrientation orientation)
938                         : __orientation(orientation){}
939
940                 virtual VisitType Visit(_Control& control)
941                 {
942                         if (control.__orientation != __orientation)
943                         {
944                                 control.__orientation = __orientation;
945                                 control.GetControlDelegate().OnChangeLayout(__orientation);
946                                 ClearLastResult();
947                         }
948
949                         return VISIT_DOWNWARD;
950                 }
951
952 private:
953                 _ControlOrientation __orientation;
954         };
955
956         _Visitor visitor(orientation);
957         Accept(visitor);
958
959         SysAssert(GetLastResult() == E_SUCCESS);
960 }
961
962 bool
963 _Control::IsLayoutChangable(void) const
964 {
965         return true;
966 }
967
968 bool
969 _Control::IsOrientationRoot(void) const
970 {
971         return false;
972 }
973
974 void
975 _Control::Invalidate(void)
976 {
977         ClearLastResult();
978         GetVisualElement()->InvalidateRectangle(null);
979 }
980
981 void
982 _Control::Invalidate(bool recursive)
983 {
984         ClearLastResult();
985
986         struct _Visitor
987                 : public Visitor
988         {
989                 virtual VisitType Visit(_Control& control)
990                 {
991                         if (control.GetVisibleState() == false)
992                         {
993                                 return VISIT_STOP;
994                         }
995
996                         control.Invalidate();
997
998                         // Ownee
999                         int owneeCount = control.GetOwneeCount();
1000                         for (int i = 0; i < owneeCount; ++i)
1001                         {
1002                                 _Window* pOwnee = control.GetOwnee(i);
1003                                 if (pOwnee)
1004                                 {
1005                                         pOwnee->Invalidate(true);
1006                                 }
1007                         }
1008
1009                         return VISIT_DOWNWARD;
1010                 }
1011         };
1012
1013         // Update layout
1014         _Layout::Layout* pLayout = GetLayout();
1015         if (pLayout)
1016         {
1017                 pLayout->UpdateLayout();
1018         }
1019
1020         if (recursive == false)
1021         {
1022                 Invalidate();
1023         }
1024         else
1025         {
1026                 _Visitor visitor;
1027                 Accept(visitor);
1028         }
1029 }
1030
1031 void
1032 _Control::Invalidate(const Rectangle& rect)
1033 {
1034         ClearLastResult();
1035         FloatRectangle rectf(rect.x, rect.y, rect.width, rect.height);
1036         GetVisualElement()->InvalidateRectangle(&rectf);
1037
1038         __invalidatedBounds = rect;
1039 }
1040
1041 bool
1042 _Control::Contains(const Point& point) const
1043 {
1044         ClearLastResult();
1045
1046         Rectangle bounds = GetBounds();
1047         bounds.x = bounds.y = 0;
1048         return bounds.Contains(point);
1049 }
1050
1051 void
1052 _Control::PartialUpdateLayout(void)
1053 {
1054         ClearLastResult();
1055
1056         _Layout::Layout* pLayout = GetLayout();
1057         if (pLayout)
1058         {
1059                 pLayout->PartialUpdateLayout();
1060         }
1061 }
1062
1063 void
1064 _Control::UpdateLayout(void)
1065 {
1066         ClearLastResult();
1067
1068         _Layout::Layout* pLayout = GetLayout();
1069         if (pLayout)
1070         {
1071                 pLayout->UpdateLayout();
1072         }
1073 }
1074
1075 result
1076 _Control::SetChildAlwaysOnTop(_Control& child)
1077 {
1078         ClearLastResult();
1079
1080         SysTryReturn(NID_UI,
1081                                 child.GetParent() == this, E_INVALID_ARG,
1082                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1083
1084         SysTryReturn(NID_UI,
1085                                 child.GetArea() == _CONTROL_AREA_CLIENT, E_SYSTEM,
1086                                 E_SYSTEM, "[E_SYSTEM] The child is not in the client area.");
1087
1088         if (child.GetLayer() == _CONTROL_LAYER_CLIENT_TOP)
1089         {
1090                 return E_SUCCESS;
1091         }
1092
1093         child.SetLayer(_CONTROL_LAYER_CLIENT_TOP);
1094
1095         SysAssert(GetLastResult() == E_SUCCESS);
1096         return E_SUCCESS;
1097 }
1098
1099 result
1100 _Control::SetChildAlwaysAtBottom(_Control& child)
1101 {
1102         ClearLastResult();
1103
1104         SysTryReturn(NID_UI,
1105                                 child.GetParent() == this, E_INVALID_ARG,
1106                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1107
1108         SysTryReturn(NID_UI,
1109                                 child.GetArea() == _CONTROL_AREA_CLIENT, E_SYSTEM,
1110                                 E_SYSTEM, "[E_SYSTEM] The child is not in the client area.");
1111
1112         if (child.GetLayer() == _CONTROL_LAYER_CLIENT_BOTTOM)
1113         {
1114                 return E_SUCCESS;
1115         }
1116
1117         child.SetLayer(_CONTROL_LAYER_CLIENT_BOTTOM);
1118
1119         SysAssert(GetLastResult() == E_SUCCESS);
1120         return E_SUCCESS;
1121 }
1122
1123 result
1124 _Control::ResetChildLayer(_Control& child)
1125 {
1126         ClearLastResult();
1127
1128         SysTryReturn(NID_UI,
1129                                 child.GetParent() == this, E_INVALID_ARG,
1130                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1131
1132         SysTryReturn(NID_UI,
1133                                 child.GetArea() == _CONTROL_AREA_CLIENT, E_SYSTEM,
1134                                 E_SYSTEM, "[E_SYSTEM] The child is not in the client area.");
1135
1136         if (child.GetLayer() == _CONTROL_LAYER_CLIENT_MIDDLE)
1137         {
1138                 return E_SUCCESS;
1139         }
1140
1141         child.SetLayer(_CONTROL_LAYER_CLIENT_MIDDLE);
1142
1143         SysAssert(GetLastResult() == E_SUCCESS);
1144         return E_SUCCESS;
1145 }
1146
1147
1148 result
1149 _Control::AttachSystemChild(_Control& child)
1150 {
1151         ClearLastResult();
1152         result r = E_SUCCESS;
1153
1154         r = StartAttaching(child, _CONTROL_AREA_SYSTEM);
1155         if (IsFailed(r))
1156         {
1157                 return r;
1158         }
1159
1160         ControlList& children = GetChildList();
1161         r = children.Add(&child);
1162         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1163
1164         r = GetVisualElement()->AttachChild(*child.GetVisualElement());
1165         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1166
1167         r = EndAttaching(child);
1168         if (IsFailed(r))
1169         {
1170                 return r;
1171         }
1172
1173         SysAssert(GetLastResult() == E_SUCCESS);
1174
1175         return E_SUCCESS;
1176 }
1177
1178 result
1179 _Control::DetachSystemChild(_Control& child)
1180 {
1181         return DetachChild(child);
1182 }
1183
1184 bool
1185 _Control::HasParent(void) const
1186 {
1187         return __pParent != null;
1188 }
1189
1190 _ControlArea
1191 _Control::GetArea(void) const
1192 {
1193         ClearLastResult();
1194         return __area;
1195 }
1196
1197 _ControlLayer
1198 _Control::GetLayer(void) const
1199 {
1200         ClearLastResult();
1201         return __layer;
1202 }
1203
1204 void
1205 _Control::SetLayer(_ControlLayer layer)
1206 {
1207         ClearLastResult();
1208         _VisualElementImpl* pImpl = _VisualElementImpl::GetInstance(*GetVisualElement());
1209
1210         result r = pImpl->SetZOrderGroup(::GetZOrderGroupOfVisualElement(layer));
1211         __layer = layer;
1212
1213         SysAssert(r == E_SUCCESS);
1214         ClearLastResult(); // [Temp] Will be removed.
1215
1216         SysAssert(r == E_SUCCESS);
1217 }
1218
1219 const _Control::ControlList&
1220 _Control::GetChildList() const
1221 {
1222         return const_cast <_Control*>(this)->GetChildList();
1223 }
1224
1225 _Control::ControlList&
1226 _Control::GetChildList()
1227 {
1228         return *__pChildren;
1229 }
1230
1231 bool
1232 _Control::IsCalledCallAttachingToMainTree(void)
1233 {
1234         return __isCalledCallOnAttachingToMainTree;
1235 }
1236
1237 void
1238 _Control::SetCalledCallAttachingToMainTree(bool isAttaching)
1239 {
1240         __isCalledCallOnAttachingToMainTree = isAttaching;
1241 }
1242
1243 bool
1244 _Control::IsCalledCallAttachedToMainTree(void)
1245 {
1246         return __isCalledCallOnAttachedToMainTree;
1247 }
1248
1249 void
1250 _Control::SetCalledCallAttachedToMainTree(bool isAttached)
1251 {
1252         __isCalledCallOnAttachedToMainTree = isAttached;
1253 }
1254
1255 result
1256 _Control::CallOnAttachingToMainTree(_Control& control)
1257 {
1258         result r = E_SUCCESS;
1259
1260         ControlList& children = control.GetChildList();
1261         _Control* pChild = null;
1262
1263         int childrenCount = children.GetCount();
1264
1265         for (int index = 0; index < childrenCount; index++)
1266         {
1267                 r = children.GetAt(index, pChild);
1268                 if (IsFailed(r))
1269                 {
1270                         SysAssert(r == E_OUT_OF_RANGE);
1271                         SysTryReturn(NID_UI,
1272                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1273                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1274                 }
1275                 if (!pChild->IsCalledCallAttachingToMainTree())
1276                 {
1277                         r = CallOnAttachingToMainTree(*pChild);
1278                         pChild->SetCalledCallAttachingToMainTree(true);
1279                         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1280                 }
1281         }
1282
1283         if (!control.IsCalledCallAttachingToMainTree())
1284         {
1285                 r = control.GetControlDelegate().OnAttachingToMainTree(this);
1286                 control.SetCalledCallAttachingToMainTree(true);
1287                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1288         }
1289
1290         return r;
1291 }
1292
1293 result
1294 _Control::CallOnAttachedToMainTree(_Control& control)
1295 {
1296         result r = E_SUCCESS;
1297
1298         ControlList& children = control.GetChildList();
1299         _Control* pChild = null;
1300
1301         int childrenCount = children.GetCount();
1302
1303         for (int index = 0; index < childrenCount; index++)
1304         {
1305                 r = children.GetAt(index, pChild);
1306                 if (IsFailed(r))
1307                 {
1308                         SysAssert(r == E_OUT_OF_RANGE);
1309                         SysTryReturn(NID_UI,
1310                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1311                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1312                 }
1313
1314                 if (!pChild->IsCalledCallAttachedToMainTree())
1315                 {
1316                         r = CallOnAttachedToMainTree(*pChild);
1317                         pChild->SetCalledCallAttachedToMainTree(true);
1318                 }
1319         }
1320
1321         if (!control.IsCalledCallAttachedToMainTree())
1322         {
1323                 r = control.GetControlDelegate().OnAttachedToMainTree();
1324                 control.SetCalledCallAttachedToMainTree(true);
1325         }
1326
1327         return r;
1328 }
1329 result
1330 _Control::CallOnDetachingFromMainTree(_Control& control)
1331 {
1332         result r = E_SUCCESS;
1333
1334         ControlList& children = control.GetChildList();
1335         _Control* pChild = null;
1336
1337         if (!__isPostOrderTraversal)
1338         {
1339                 r = control.GetControlDelegate().OnDetachingFromMainTree();
1340                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1341         }
1342
1343         int childrenCount = children.GetCount();
1344
1345         for (int index = 0; index < childrenCount; index++)
1346         {
1347                 r = children.GetAt(index, pChild);
1348                 if (IsFailed(r))
1349                 {
1350                         SysAssert(r == E_OUT_OF_RANGE);
1351                         SysTryReturn(NID_UI,
1352                                 (r != E_OUT_OF_RANGE), E_OUT_OF_RANGE,
1353                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1354                 }
1355                 r = CallOnDetachingFromMainTree(*pChild);
1356                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1357         }
1358
1359         if (__isPostOrderTraversal)
1360         {
1361                 r = control.GetControlDelegate().OnDetachingFromMainTree();
1362                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1363         }
1364
1365         return r;
1366 }
1367
1368 void
1369 _Control::CallOnAncestorVisibleStateChanged(void)
1370 {
1371         struct _Visitor
1372                 : public Visitor
1373         {
1374                 _Visitor(_Control& parent)
1375                         : __parent(parent){}
1376
1377                 virtual VisitType Visit(_Control& control)
1378                 {
1379                         control.GetControlDelegate().OnAncestorVisibleStateChanged(__parent);
1380                         return VISIT_DOWNWARD;
1381                 }
1382
1383                 _Control& __parent;
1384         };
1385
1386         _Visitor visitor(*this);
1387         Accept(visitor);
1388 }
1389
1390 void
1391 _Control::CallOnAncestorEnableStateChanged(void)
1392 {
1393         struct _Visitor
1394                 : public Visitor
1395         {
1396                 _Visitor(_Control& parent)
1397                         : __parent(parent){}
1398
1399                 virtual VisitType Visit(_Control& control)
1400                 {
1401                         control.GetControlDelegate().OnAncestorEnableStateChanged(__parent);
1402                         return VISIT_DOWNWARD;
1403                 }
1404
1405                 _Control& __parent;
1406         };
1407
1408         _Visitor visitor(*this);
1409         Accept(visitor);
1410 }
1411
1412 // E_INVALID_ARG
1413 // E_SYSTEM
1414 // [ToDo] Rollback is difficult.
1415 result
1416 _Control::StartAttaching(_Control& child, _ControlArea area)
1417 {
1418         result r = E_SUCCESS;
1419
1420         _Control* pOldParent = child.GetParent();
1421
1422         SysTryReturn(NID_UI,
1423                                 (pOldParent != this), E_INVALID_ARG,
1424                                 E_INVALID_ARG, "[E_INVALID_ARG] The child control is already attached to this container.");
1425
1426         SysTryReturn(NID_UI,
1427                                 pOldParent == null, E_INVALID_ARG,
1428                                 E_INVALID_ARG, "[E_INVALID_ARG] Unable to add the child which already has another parent.");
1429
1430         r = child.GetControlDelegate().OnAttaching(this);
1431         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1432
1433         if (IsAttachedToMainTree())
1434         {
1435                 r = CallOnAttachingToMainTree(child);
1436                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1437         }
1438
1439         // [ToDo] Add control to layout
1440         // What should we do about non-layoutable controls?
1441         if (area == _CONTROL_AREA_CLIENT)
1442         {
1443                 _ControlManager* pMgr = _ControlManager::GetInstance();
1444                 r = GetLastResult();
1445                 SysTryReturn(NID_UI, pMgr, r, r, "[%s] Propagating.", GetErrorMessage(r));
1446
1447 #if !defined(MULTI_WINDOW)
1448                 if (this != &(pMgr->GetRoot()))
1449 #endif
1450                 {
1451                         r = __pLayoutContainer->AddItem(child.GetLayoutContainer());
1452                 }
1453
1454                 if (IsFailed(r))
1455                 {
1456                         child.GetControlDelegate().OnAttachingFailed(*this);
1457                         SysLogException(NID_UI, E_SYSTEM, "[E_SYSTEM] Failed to apply layout.");
1458                         return E_SYSTEM;
1459                 }
1460         }
1461
1462         if (IsAttachedToMainTree())
1463         {
1464 #if defined(MULTI_WINDOW)
1465                 if (!(IsOrientationRoot() && child.IsOrientationRoot()))
1466 #else
1467                 if (IsLayoutChangable() && child.IsLayoutChangable())
1468 #endif
1469                 {
1470                         child.ChangeLayout(_ControlManager::GetInstance()->GetOrientation());
1471                 }
1472         }
1473
1474         child.__area = area;
1475
1476         if (area == _CONTROL_AREA_CLIENT)
1477         {
1478                 if (child.GetLayer() != _CONTROL_LAYER_CLIENT_TOP && child.GetLayer() != _CONTROL_LAYER_CLIENT_BOTTOM)
1479                 {
1480                         child.SetLayer(_CONTROL_LAYER_CLIENT_MIDDLE);
1481                 }
1482         }
1483         else
1484         {
1485                 child.SetLayer(_CONTROL_LAYER_SYSTEM);
1486         }
1487
1488         SysAssert(GetLastResult() == E_SUCCESS);
1489         return E_SUCCESS;
1490 }
1491
1492 result
1493 _Control::EndAttaching(_Control& child)
1494 {
1495         child.SetParent(this);
1496
1497         FloatRectangle floatBounds(child.GetBounds().x, child.GetBounds().y, child.GetBounds().width, child.GetBounds().height);
1498
1499         result r = E_SUCCESS;
1500         if (child.IsLayoutChangable() == false)
1501         {
1502                 r = child.UpdateBoundsOfVisualElement(FloatRectangle(0, 0, floatBounds.width, floatBounds.height));
1503         }
1504         else
1505         {
1506                 r = child.UpdateBoundsOfVisualElement(floatBounds);
1507         }
1508         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1509
1510         r = child.GetControlDelegate().OnAttached();
1511         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1512
1513         if (IsAttachedToMainTree())
1514         {
1515                 r = CallOnAttachedToMainTree(child);
1516                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1517         }
1518
1519         ClearLastResult();
1520
1521         GetControlDelegate().OnChildAttached(child);
1522         ClearLastResult();
1523
1524         return E_SUCCESS;
1525 }
1526
1527 // E_INVALID_ARG
1528 // E_OUT_OF_MEMORY
1529 // E_SYSTEM
1530 result
1531 _Control::AttachChild(_Control& child)
1532 {
1533         ClearLastResult();
1534         result r = E_SUCCESS;
1535
1536         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1537         if (IsFailed(r))
1538         {
1539                 return r;
1540         }
1541
1542         ControlList& children = GetChildList();
1543         r = children.Add(&child);
1544         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1545
1546         r = GetVisualElement()->AttachChild(*child.GetVisualElement());
1547
1548         r = EndAttaching(child);
1549         if (IsFailed(r))
1550         {
1551                 return r;
1552         }
1553
1554         SysAssert(GetLastResult() == E_SUCCESS);
1555         return E_SUCCESS;
1556 }
1557
1558 // E_INVALID_ARG
1559 // E_OUT_OF_MEMORY
1560 // E_SYSTEM
1561 result
1562 _Control::InsertChildToBottom(_Control& child)
1563 {
1564         ClearLastResult();
1565         result r = E_SUCCESS;
1566
1567         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1568         if (IsFailed(r))
1569         {
1570                 return r;
1571         }
1572
1573         ControlList& children = GetChildList();
1574         r = children.InsertAt(&child, 0);
1575         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1576
1577         GetVisualElement()->InsertChild(*child.GetVisualElement(), null, false);
1578         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1579
1580         r = EndAttaching(child);
1581         if (IsFailed(r))
1582         {
1583                 return r;
1584         }
1585
1586         SysAssert(GetLastResult() == E_SUCCESS);
1587         return E_SUCCESS;
1588 }
1589
1590 // E_INVALID_ARG
1591 // E_OUT_OF_MEMORY
1592 // E_SYSTEM
1593 result
1594 _Control::InsertChildAfter(const _Control& targetChild, _Control& child)
1595 {
1596         ClearLastResult();
1597         result r = E_SUCCESS;
1598
1599         SysTryReturn(NID_UI,
1600                                 targetChild.GetParent() == this, E_INVALID_ARG,
1601                                 E_INVALID_ARG, "[E_INVALID_ARG] The target is not my child.");
1602
1603         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1604         if (IsFailed(r))
1605         {
1606                 return r;
1607         }
1608
1609         int targetIndex = GetChildIndex(targetChild);
1610         SysAssert(targetIndex != -1);
1611
1612         ControlList& children = GetChildList();
1613         r = children.InsertAt(&child, targetIndex + 1);
1614         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1615
1616         GetVisualElement()->InsertChild(*child.GetVisualElement(), targetChild.GetVisualElement(), true);
1617         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1618
1619         r = EndAttaching(child);
1620         if (IsFailed(r))
1621         {
1622                 return r;
1623         }
1624
1625         SysAssert(GetLastResult() == E_SUCCESS);
1626         return E_SUCCESS;
1627 }
1628
1629 // E_INVALID_ARG
1630 // E_OUT_OF_MEMORY
1631 // E_SYSTEM
1632 result
1633 _Control::InsertChildBefore(const _Control& targetChild, _Control& child)
1634 {
1635         ClearLastResult();
1636         result r = E_SUCCESS;
1637
1638         SysTryReturn(NID_UI,
1639                                 targetChild.GetParent() == this, E_INVALID_ARG,
1640                                 E_INVALID_ARG, "[E_INVALID_ARG] The target is not my child.");
1641
1642         r = StartAttaching(child, _CONTROL_AREA_CLIENT);
1643         if (IsFailed(r))
1644         {
1645                 return r;
1646         }
1647
1648         int targetIndex = GetChildIndex(targetChild);
1649         SysAssert(targetIndex != -1);
1650
1651         ControlList& children = GetChildList();
1652         r = children.InsertAt(&child, targetIndex);
1653         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1654
1655         GetVisualElement()->InsertChild(*child.GetVisualElement(), targetChild.GetVisualElement(), false);
1656         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1657
1658         r = EndAttaching(child);
1659         if (IsFailed(r))
1660         {
1661                 return r;
1662         }
1663
1664         SysAssert(GetLastResult() == E_SUCCESS);
1665         return E_SUCCESS;
1666 }
1667
1668 // E_SYSTEM
1669 result
1670 _Control::DetachChild(_Control& child)
1671 {
1672         ClearLastResult();
1673         result r = E_SUCCESS;
1674
1675         SysTryReturn(NID_UI,
1676                                 child.GetParent() == this, E_INVALID_ARG,
1677                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1678
1679         if (IsAttachedToMainTree())
1680         {
1681                 r = CallOnDetachingFromMainTree(child);
1682                 SysTryReturn(NID_UI,
1683                                         r == E_SUCCESS, E_SYSTEM,
1684                                         E_SYSTEM, "[E_SYSTEM] The child cannot detached from parent.");
1685
1686                 SysTryReturn(NID_UI,
1687                                         child.GetControlDelegate().OnDetaching() == E_SUCCESS, E_SYSTEM,
1688                                         E_SYSTEM, "[E_SYSTEM] The child cannot detached from parent.");
1689         }
1690
1691         GetControlDelegate().OnChildDetaching(child);
1692         ClearLastResult();
1693
1694         r = GetVisualElement()->DetachChild(*child.GetVisualElement());
1695         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1696
1697         ControlList& children = GetChildList();
1698         r = children.Remove(&child);
1699         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1700
1701         // Remove control to layout
1702         if (child.__area == _CONTROL_AREA_CLIENT)
1703         {
1704                 _ControlManager* pMgr = _ControlManager::GetInstance();
1705                 r = GetLastResult();
1706                 SysTryReturn(NID_UI, pMgr, r, r, "[%s] Propagating.", GetErrorMessage(r));
1707
1708 #if !defined(MULTI_WINDOW)
1709                 if (this != &(pMgr->GetRoot()))
1710 #endif
1711                 {
1712                         r = __pLayoutContainer->RemoveItem(child.GetLayoutContainer());
1713                         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1714                 }
1715         }
1716
1717         child.SetParent(null);
1718         child.__area = _CONTROL_AREA_NONE;
1719         child.__layer = _CONTROL_LAYER_NONE;
1720
1721         GetControlDelegate().OnChildDetached(child);
1722         ClearLastResult();
1723
1724         SysAssert(GetLastResult() == E_SUCCESS);
1725         return E_SUCCESS;
1726 }
1727
1728 // Always success
1729 void
1730 _Control::DetachAllChildren(bool detachSystemChild)
1731 {
1732         ClearLastResult();
1733         result r = E_SUCCESS;
1734
1735         int notDetachedChildCount = 0;
1736
1737         int childCount = GetChildCount();
1738         int itemIndex = 0;
1739
1740         while (childCount--)
1741         {
1742                 _Control* pChild = GetChild(itemIndex);
1743                 if (pChild == null)
1744                 {
1745                         continue;
1746                 }
1747
1748                 if (!detachSystemChild && (pChild->GetArea() == _CONTROL_AREA_SYSTEM))
1749                 {
1750                         itemIndex++;
1751                         continue;
1752                 }
1753                 r = DetachChild(*pChild);
1754
1755                 if (IsFailed(r))
1756                 {
1757                         ++notDetachedChildCount;
1758                 }
1759         }
1760
1761         SysLog(NID_UI, "%d children are not detached from this container.", notDetachedChildCount);
1762         ClearLastResult();
1763 }
1764
1765 // E_INVALID_ARG
1766 result
1767 _Control::MoveChildToTop(const _Control& child)
1768 {
1769         ClearLastResult();
1770         result r = E_SUCCESS;
1771
1772         SysTryReturn(NID_UI,
1773                                 child.GetParent() == this, E_INVALID_ARG,
1774                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1775
1776         // If already on top,
1777         ControlList& children = GetChildList();
1778         if (GetChildIndex(child) == children.GetCount() - 1)
1779         {
1780                 SysAssert(GetLastResult() == E_SUCCESS);
1781                 return E_SUCCESS;
1782         }
1783
1784         _Control* pChild = const_cast <_Control*>(&child);
1785
1786         r = children.Remove(pChild);
1787         SysAssert(r == E_SUCCESS);
1788
1789         r = children.Add(pChild);
1790         SysAssert(r == E_SUCCESS);
1791
1792         r = child.GetVisualElement()->SetZOrder(null, true);
1793         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1794
1795         SysAssert(GetLastResult() == E_SUCCESS);
1796         return E_SUCCESS;
1797 }
1798
1799 // E_INVALID_ARG
1800 result
1801 _Control::MoveChildToBottom(const _Control& child)
1802 {
1803         ClearLastResult();
1804         result r = E_SUCCESS;
1805
1806         SysTryReturn(NID_UI,
1807                                 child.GetParent() == this, E_INVALID_ARG,
1808                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1809
1810         ControlList& children = GetChildList();
1811
1812         if (GetChildIndex(child) == 0)
1813         {
1814                 SysAssert(GetLastResult() == E_SUCCESS);
1815                 return E_SUCCESS;
1816         }
1817
1818         _Control* pChild = const_cast <_Control*>(&child);
1819
1820         r = children.Remove(pChild);
1821         SysAssert(r == E_SUCCESS);
1822
1823         r = children.InsertAt(pChild, 0);
1824         SysAssert(r == E_SUCCESS);
1825
1826         r = child.GetVisualElement()->SetZOrder(null, false);
1827         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1828
1829         SysAssert(GetLastResult() == E_SUCCESS);
1830         return E_SUCCESS;
1831 }
1832
1833 // E_INVALID_ARG
1834 result
1835 _Control::MoveChildAfter(const _Control& targetChild, const _Control& child)
1836 {
1837         ClearLastResult();
1838         result r = E_SUCCESS;
1839
1840         SysTryReturn(NID_UI,
1841                                 targetChild.GetParent() == this, E_INVALID_ARG,
1842                                 E_INVALID_ARG, "[E_INVALID_ARG] Target is not my child.");
1843
1844         SysTryReturn(NID_UI,
1845                                 child.GetParent() == this, E_INVALID_ARG,
1846                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1847
1848         SysTryReturn(NID_UI,
1849                                 &targetChild != &child, E_INVALID_ARG,
1850                                 E_INVALID_ARG, "[E_INVALID_ARG] Cannot move after self.");
1851
1852         ControlList& children = GetChildList();
1853
1854         int targetIndex = GetChildIndex(targetChild);
1855         SysAssert(targetIndex != -1);
1856
1857         if (targetIndex + 1 == GetChildIndex(child))
1858         {
1859                 SysAssert(GetLastResult() == E_SUCCESS);
1860                 return E_SUCCESS;
1861         }
1862
1863         _Control* pChild = const_cast <_Control*>(&child);
1864
1865         r = children.Remove(pChild);
1866         SysAssert(r == E_SUCCESS);
1867
1868         r = children.InsertAt(pChild, targetIndex + 1);
1869         SysAssert(r == E_SUCCESS);
1870
1871         r = child.GetVisualElement()->SetZOrder(targetChild.GetVisualElement(), true);
1872         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1873
1874         SysAssert(GetLastResult() == E_SUCCESS);
1875         return E_SUCCESS;
1876 }
1877
1878 // E_INVALID_ARG
1879 result
1880 _Control::MoveChildBefore(const _Control& targetChild, const _Control& child)
1881 {
1882         ClearLastResult();
1883         result r = E_SUCCESS;
1884
1885         SysTryReturn(NID_UI,
1886                                 targetChild.GetParent() == this, E_INVALID_ARG,
1887                                 E_INVALID_ARG, "[E_INVALID_ARG] Target is not my child.");
1888
1889         SysTryReturn(NID_UI,
1890                                 child.GetParent() == this, E_INVALID_ARG,
1891                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1892
1893         SysTryReturn(NID_UI,
1894                                 &targetChild != &child, E_INVALID_ARG,
1895                                 E_INVALID_ARG, "[E_INVALID_ARG] Cannot move before self.");
1896
1897         ControlList& children = GetChildList();
1898
1899         int targetIndex = GetChildIndex(targetChild);
1900         SysAssert(targetIndex != -1);
1901
1902         if (targetIndex - 1 == GetChildIndex(child))
1903         {
1904                 SysAssert(GetLastResult() == E_SUCCESS);
1905                 return E_SUCCESS;
1906         }
1907
1908         _Control* pChild = const_cast <_Control*>(&child);
1909
1910         r = children.Remove(pChild);
1911         SysAssert(r == E_SUCCESS);
1912
1913         r = children.InsertAt(pChild, targetIndex);
1914         SysAssert(r == E_SUCCESS);
1915
1916         r = child.GetVisualElement()->SetZOrder(targetChild.GetVisualElement(), false);
1917         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
1918
1919         SysAssert(GetLastResult() == E_SUCCESS);
1920         return E_SUCCESS;
1921 }
1922
1923 // E_INVALID_ARG
1924 // E_OBJ_NOT_FOUND
1925 int
1926 _Control::GetChildIndex(const _Control& child) const
1927 {
1928         ClearLastResult();
1929         result r = E_SUCCESS;
1930
1931         SysTryReturn(NID_UI,
1932                                 child.GetParent() == this, -1,
1933                                 E_INVALID_ARG, "[E_INVALID_ARG] Not my child.");
1934
1935         const ControlList& children = GetChildList();
1936
1937         int index = -1;
1938         r = children.IndexOf(const_cast<_Control*>(&child), index);
1939         if (IsFailed(r))
1940         {
1941                 SysAssert(r == E_OBJ_NOT_FOUND);
1942                 SysLogException(NID_UI, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Unable to find the specified child.");
1943                 return -1;
1944         }
1945
1946         SysAssert(GetLastResult() == E_SUCCESS);
1947         return index;
1948 }
1949
1950 // E_OUT_OF_RANGE
1951 _Control*
1952 _Control::GetChild(int index) const
1953 {
1954         ClearLastResult();
1955         result r = E_SUCCESS;
1956
1957         const ControlList& children = GetChildList();
1958
1959         _Control* pChild = null;
1960         r = children.GetAt(index, pChild);
1961         if (IsFailed(r))
1962         {
1963                 SysAssert(r == E_OUT_OF_RANGE);
1964                 SysLogException(NID_UI, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
1965                 return null;
1966         }
1967
1968         SysAssert(GetLastResult() == E_SUCCESS);
1969         return pChild;
1970 }
1971
1972 int
1973 _Control::GetChildCount(void) const
1974 {
1975         ClearLastResult();
1976         return GetChildList().GetCount();
1977 }
1978
1979 _ControlHandle
1980 _Control::GetHandle(void) const
1981 {
1982         return __controlHandle;
1983 }
1984
1985 void*
1986 _Control::GetUserData(void) const
1987 {
1988         return __pUserData;
1989 }
1990
1991 void
1992 _Control::SetUserData(void* pUserData)
1993 {
1994         __pUserData = pUserData;
1995 }
1996
1997 Variant
1998 _Control::GetPropertyName(void) const
1999 {
2000         ClearLastResult();
2001         return Tizen::Ui::Variant(__name);
2002 }
2003
2004 String
2005 _Control::GetName(void) const
2006 {
2007         Variant name = GetProperty("Name");
2008
2009         return name.ToString();
2010 }
2011
2012 result
2013 _Control::SetPropertyName(const Variant& name)
2014 {
2015         ClearLastResult();
2016         __name = name.ToString();
2017
2018         return E_SUCCESS;
2019 }
2020
2021 void
2022 _Control::SetName(const String& name)
2023 {
2024         SetProperty("Name", Variant(name));
2025 }
2026
2027 _Control*
2028 _Control::GetParent(void) const
2029 {
2030         return __pParent;
2031 }
2032
2033 Canvas*
2034 _Control::GetCanvasN(void) const
2035 {
2036         return GetCanvasN(Rectangle(0, 0, __bounds.width, __bounds.height));
2037 }
2038
2039 Canvas*
2040 _Control::GetCanvasN(const Rectangle& bounds) const
2041 {
2042         ClearLastResult();
2043         Canvas *pCanvas = GetControlDelegate().OnCanvasRequestedN(Dimension(bounds.width, bounds.height));
2044         if (pCanvas == null)
2045         {
2046                 GetVisualElement()->SetFlushNeeded();
2047                 pCanvas = GetVisualElement()->GetCanvasN(bounds);
2048                 if (IsFailed(GetLastResult()))
2049                 {
2050                         SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
2051                 }
2052
2053                 if (pCanvas && !__isCalledGetCanvasN)
2054                 {
2055                         pCanvas->SetBackgroundColor(GetBackgroundColor());
2056                         pCanvas->Clear();
2057                         const_cast <_Control*>(this)->__isCalledGetCanvasN = true;
2058                 }
2059         }
2060         return pCanvas;
2061 }
2062
2063 bool
2064 _Control::IsCalledGetCanvasN(void) const
2065 {
2066         return __isCalledGetCanvasN;
2067 }
2068
2069 Canvas*
2070 _Control::GetClientCanvasN(void) const
2071 {
2072         return GetCanvasN(GetClientBounds());
2073 }
2074
2075 bool
2076 _Control::IsAncestorOf(const _Control& control) const
2077 {
2078         ClearLastResult();
2079
2080         const _Control* pParent = control.GetParent();
2081         if (!pParent)
2082         {
2083                 return false;
2084         }
2085
2086         struct _Visitor
2087                 : public Visitor
2088         {
2089 private:
2090                 const _Control& __ancestor;
2091
2092 public:
2093                 bool yes;
2094
2095                 _Visitor(const _Control& ancestor)
2096                         : __ancestor(ancestor)
2097                         , yes(false){}
2098
2099                 virtual VisitType Visit(_Control& control)
2100                 {
2101                         if (&__ancestor == &control)
2102                         {
2103                                 yes = true;
2104                                 return VISIT_STOP;
2105                         }
2106                         else
2107                         {
2108                                 return VISIT_UPWARD;
2109                         }
2110                 }
2111         };
2112
2113         _Visitor visitor(*this);
2114         pParent->Accept(visitor);
2115
2116         SysAssert(GetLastResult() == E_SUCCESS);
2117         return visitor.yes;
2118 }
2119
2120 _Window*
2121 _Control::GetRootWindow(void) const
2122 {
2123         ClearLastResult();
2124
2125         struct _Visitor
2126                 : public Visitor
2127         {
2128                 _Window* pRoot;
2129
2130                 _Visitor(void)
2131                         : pRoot(null){}
2132
2133                 virtual VisitType Visit(_Control& control)
2134                 {
2135                         pRoot = dynamic_cast <_Window*>(&control);
2136                         if (pRoot != null)
2137                         {
2138                                 return VISIT_STOP;
2139                         }
2140
2141                         return VISIT_UPWARD;
2142                 }
2143         };
2144
2145         _Visitor visitor;
2146         Accept(visitor);
2147
2148         SysAssert(GetLastResult() == E_SUCCESS);
2149         return visitor.pRoot;
2150 }
2151
2152 bool
2153 _Control::IsAttachedToMainTree(void) const
2154 {
2155         ClearLastResult();
2156
2157         _ControlManager* pMgr = _ControlManager::GetInstance();
2158         if (pMgr == null)
2159         {
2160                 return false;
2161         }
2162
2163 #if !defined(MULTI_WINDOW)
2164         if (this == &(pMgr->GetRoot()))
2165         {
2166                 return true;
2167         }
2168 #endif
2169
2170         _Window* pRootWindow = GetRootWindow();
2171         if (pRootWindow == null)
2172         {
2173                 return false;
2174         }
2175
2176         return pRootWindow->IsAttached();
2177 }
2178
2179 bool
2180 _Control::IsFocusable(void) const
2181 {
2182         ClearLastResult();
2183         return __focusable;
2184 }
2185
2186 void
2187 _Control::SetFocusable(bool focusable)
2188 {
2189         ClearLastResult();
2190         __focusable = focusable;
2191 }
2192
2193 bool
2194 _Control::IsNativeObjectFocusable(void) const
2195 {
2196         ClearLastResult();
2197         return __nativeObjectFocusable;
2198 }
2199
2200 void
2201 _Control::SetNativeObjectFocusable(bool focusable)
2202 {
2203         ClearLastResult();
2204         __nativeObjectFocusable = focusable;
2205 }
2206
2207 bool
2208 _Control::IsFocused(void) const
2209 {
2210         ClearLastResult();
2211
2212         if (!__focusSet)
2213         {
2214                 return false;
2215         }
2216
2217         for (int i = 0; i < GetChildCount(); ++i)
2218         {
2219                 _Control* pChild = GetChild(i);
2220
2221                 SysAssert(pChild);
2222                 if (pChild == null)
2223                 {
2224                         continue;
2225                 }
2226
2227                 if (pChild->__focusSet)
2228                 {
2229                         return false;
2230                 }
2231         }
2232
2233         return true;
2234 }
2235
2236 _Control*
2237 _Control::GetFocused(void) const
2238 {
2239         for (int i = GetChildCount() - 1; i >= 0; --i)
2240         {
2241                 _Control* pChild = GetChild(i);
2242
2243                 SysAssert(pChild);
2244                 if (pChild == null)
2245                 {
2246                         continue;
2247                 }
2248
2249                 if (pChild->__focusSet == true)
2250                 {
2251                         return pChild->GetFocused();
2252                 }
2253         }
2254
2255         if (__focusSet == true)
2256         {
2257                 return const_cast <_Control*>(this);
2258         }
2259         else
2260         {
2261                 return null;
2262         }
2263
2264 }
2265
2266 result
2267 _Control::SetFocused(void)
2268 {
2269         ClearLastResult();
2270
2271         SysTryReturn(NID_UI,
2272                                 IsAttachedToMainTree(), E_INVALID_OPERATION,
2273                                 E_INVALID_OPERATION, "[E_INVALID_OPERATION] This control should have a parent or be attached to the main tree.");
2274
2275         SysTryReturn(NID_UI,
2276                                 IsFocusable(), E_INVALID_OPERATION,
2277                                 E_INVALID_OPERATION, "[E_INVALID_OPERATION] This Control isn't focusable control.");
2278
2279         _Window* pTop = GetRootWindow();
2280         SysAssert(pTop);
2281
2282         pTop->SetFocusOff(this);
2283         SetFocusOn();
2284
2285         _ControlManager* pControlMgr = _ControlManager::GetInstance();
2286         SysAssert(pControlMgr);
2287
2288         if (pTop->IsActivated() && pTop->IsFocusableDescendant(this))
2289         {
2290                 pControlMgr->SetFocusedControl(*this); // Dangerous if _Control::Manager::SetFocusedControl() is called by User.
2291         }
2292
2293         if (IsFailed(GetLastResult()))
2294         {
2295                 SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
2296         }
2297
2298         return E_SUCCESS;
2299 }
2300
2301
2302 // [ToDo] Assume the control is attached to the main tree.
2303 void
2304 _Control::SetFocusOn(void)
2305 {
2306         ClearLastResult();
2307
2308         __focusSet = true;
2309         _Control* pParent = GetParent();
2310         if (pParent)
2311         {
2312                 pParent->SetFocusOn();
2313         }
2314 }
2315
2316 // [ToDo] Find the focused descendent and clear flags upwards.
2317 void
2318 _Control::SetFocusOff(_Control* pFocus)
2319 {
2320         ClearLastResult();
2321
2322         __focusSet = false;
2323         for (int i = 0; i < GetChildCount(); ++i)
2324         {
2325                 _Control* pChild = GetChild(i);
2326
2327                 SysAssert(pChild);
2328                 if (pChild == null)
2329                 {
2330                         continue;
2331                 }
2332
2333                 if (pChild->__focusSet == true)
2334                 {
2335                         pChild->SetFocusOff(pFocus);
2336                         return;
2337                 }
2338         }
2339 }
2340
2341 result
2342 _Control::SetFont(const String& fontName)
2343 {
2344         result r = E_SUCCESS;
2345
2346         if (__fontName.Equals(fontName))
2347         {
2348                 return E_SUCCESS;
2349         }
2350
2351         __isControlFontChanged = true;
2352         __fontName = fontName;
2353
2354         Font* pFont = GetFallbackFont();
2355
2356         if (pFont == null)
2357         {
2358                 r = GetLastResult();
2359                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
2360         }
2361         return E_SUCCESS;
2362 }
2363
2364 Font*
2365 _Control::GetFallbackFont(void)
2366 {
2367         unsigned long style;
2368         int textSize;
2369         result r = E_SUCCESS;
2370         _IControlDelegate& delegate = GetControlDelegate();
2371         delegate.OnFontInfoRequested(style, textSize);
2372         _ControlManager* pControlManager = _ControlManager::GetInstance();
2373
2374         if (!(__isControlFontChanged || pControlManager->IsDefaultFontChanged()) && __pFont != null && __pFont->GetSize() == textSize)
2375         {
2376                 return __pFont;
2377         }
2378         unique_ptr<Font>pTempFont(new(std::nothrow)Font());
2379         SysTryReturn(NID_UI , pTempFont , null , E_OUT_OF_MEMORY, "[%s] Fails to create a __pFont.", GetErrorMessage(r));
2380
2381         if (!__fontName.IsEmpty())
2382         {
2383                 __isControlFontChanged = false;
2384                 _FontImpl* pFontImpl  = _FontImpl::GetInstance(*pTempFont);
2385                 SysAssertf(pFontImpl != null, "Getting _FontImpl instance failed.");
2386
2387                 r = pFontImpl->Construct(__fontName, style, textSize, false);
2388                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2389                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2390         }
2391         else if (!pControlManager->GetDefaultFont().IsEmpty())
2392         {
2393                 _FontImpl* pFontImpl = _FontImpl::GetInstance(*pTempFont);
2394                 SysAssertf(pFontImpl != null, "Getting _FontImpl instance failed.");
2395
2396                 r = pFontImpl->Construct(pControlManager->GetDefaultFont(), style, textSize, false);
2397                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2398                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2399         }
2400         else
2401         {
2402                 r = pTempFont->Construct(style, textSize);
2403                 SysTryReturn(NID_UI, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "%s.", GetErrorMessage(r));
2404                 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_FILE_NOT_FOUND, "%s.", GetErrorMessage(r));
2405         }
2406
2407         if (__pFont)
2408         {
2409                 delete __pFont;
2410                 __pFont = null;
2411         }
2412         __pFont = pTempFont.release();
2413         delegate.OnFontChanged(__pFont);
2414         return  __pFont;
2415 }
2416
2417 String
2418 _Control::GetFont(void) const
2419 {
2420         return __fontName;
2421 }
2422
2423 bool
2424 _Control::IsEnabled(void) const
2425 {
2426         ClearLastResult();
2427
2428         struct _Visitor
2429                 : public Visitor
2430         {
2431                 bool enabled;
2432
2433                 _Visitor(void)
2434                         : enabled(true){}
2435
2436                 virtual VisitType Visit(_Control& control)
2437                 {
2438                         if (!control.GetEnableState())
2439                         {
2440                                 enabled = false;
2441                                 return VISIT_STOP;
2442                         }
2443
2444                         return VISIT_UPWARD;
2445                 }
2446         };
2447
2448         _Visitor visitor;
2449         Accept(visitor);
2450         return visitor.enabled;
2451 }
2452
2453 bool
2454 _Control::GetEnableState(void) const
2455 {
2456         ClearLastResult();
2457         return __enabledState;
2458 }
2459
2460 void
2461 _Control::SetEnableState(bool enabledState)
2462 {
2463         ClearLastResult();
2464         const bool changed = (__enabledState != enabledState);
2465         __enabledState = enabledState;
2466         if (changed)
2467         {
2468                 CallOnAncestorEnableStateChanged();
2469         }
2470         __pAccessibilityContainer->SetEnableState(enabledState);
2471 }
2472
2473 bool
2474 _Control::IsInputEnabled(void) const
2475 {
2476         ClearLastResult();
2477
2478         struct _Visitor
2479                 : public Visitor
2480         {
2481                 bool inputEnabled;
2482
2483                 _Visitor(void)
2484                         : inputEnabled(true){}
2485
2486                 virtual VisitType Visit(_Control& control)
2487                 {
2488                         if (!control.GetInputEnableState())
2489                         {
2490                                 inputEnabled = false;
2491                                 return VISIT_STOP;
2492                         }
2493
2494                         return VISIT_UPWARD;
2495                 }
2496         };
2497
2498         _Visitor visitor;
2499         Accept(visitor);
2500         return visitor.inputEnabled;
2501 }
2502
2503 bool
2504 _Control::GetInputEnableState(void) const
2505 {
2506         ClearLastResult();
2507         return __inputEnabledState;
2508 }
2509
2510 void
2511 _Control::SetInputEnableState(bool inputEnabledState)
2512 {
2513         ClearLastResult();
2514         __inputEnabledState = inputEnabledState;
2515 }
2516
2517 bool
2518 _Control::IsVisible(void) const
2519 {
2520         ClearLastResult();
2521
2522         SysTryReturn(NID_UI, IsAttachedToMainTree(), false, E_SYSTEM, "[E_SYSTEM] This control should be attached to the main tree.");
2523
2524         struct _Visitor
2525                 : public Visitor
2526         {
2527                 bool visible;
2528
2529                 _Visitor(void)
2530                         : visible(true){}
2531
2532                 virtual VisitType Visit(_Control& control)
2533                 {
2534                         if (!control.GetVisibleState())
2535                         {
2536                                 visible = false;
2537                                 return VISIT_STOP;
2538                         }
2539
2540                         return VISIT_UPWARD;
2541                 }
2542         };
2543
2544         _Visitor visitor;
2545         Accept(visitor);
2546
2547         SysAssert(GetLastResult() == E_SUCCESS);
2548         return visitor.visible;
2549 }
2550
2551 bool
2552 _Control::GetVisibleState(void) const
2553 {
2554         ClearLastResult();
2555         return __visibleState;
2556 }
2557
2558 void
2559 _Control::SetVisibleState(bool visibleState)
2560 {
2561         ClearLastResult();
2562
2563         const bool changed = (__visibleState != visibleState) || !__initVisibleState;
2564
2565         if (changed)
2566         {
2567                 GetControlDelegate().OnVisibleStateChanging();
2568         }
2569
2570         __visibleState = visibleState;
2571         GetVisualElement()->SetShowState(visibleState);
2572
2573         if (visibleState == false)
2574         {
2575                 int owneeCount = GetOwneeCount();
2576                 for (int i = 0; i < owneeCount; ++i)
2577                 {
2578                         _Window* pWindow = GetOwnee(i);
2579                         if (pWindow)
2580                         {
2581                                 pWindow->SetVisibleState(visibleState);
2582                         }
2583                 }
2584         }
2585
2586         if (changed)
2587         {
2588                 GetControlDelegate().OnVisibleStateChanged();
2589                 CallOnAncestorVisibleStateChanged();
2590
2591                 _Control* pParent = GetParent();
2592                 if (pParent)
2593                 {
2594                         pParent->GetControlDelegate().OnChildVisibleStateChanged(*this);
2595                 }
2596
2597                 ClearLastResult();
2598         }
2599
2600         __initVisibleState = true;
2601 }
2602
2603 bool
2604 _Control::IsLayoutable(void) const
2605 {
2606         ClearLastResult();
2607         return IsMovable();
2608 }
2609
2610 bool
2611 _Control::IsClipToParent(void) const
2612 {
2613         ClearLastResult();
2614 //      SysAssert(GetVisualElement()->IsClipToParent() == __clipToParent);
2615         return __clipToParent;
2616 }
2617
2618 result
2619 _Control::SetClipToParent(bool clipToParent)
2620 {
2621         ClearLastResult();
2622         result r = E_SUCCESS;
2623
2624         __clipToParent = clipToParent;
2625         r =  GetVisualElement()->SetClipToParent(clipToParent);
2626         SysTryReturn(NID_UI, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
2627
2628         return E_SUCCESS;
2629 }
2630
2631 result
2632 _Control::SetClipChildrenEnabled(bool clipChildren)
2633 {
2634         ClearLastResult();
2635         //result r = E_SUCCESS;
2636
2637         GetVisualElement()->SetClipChildrenEnabled(clipChildren);
2638         //SysTryReturn(NID_UI, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occurred.");
2639
2640         return E_SUCCESS;
2641 }
2642
2643 Rectangle
2644 _Control::GetBounds(void) const
2645 {
2646         ClearLastResult();
2647         return __bounds;
2648 }
2649
2650 Point
2651 _Control::GetPosition(void) const
2652 {
2653         ClearLastResult();
2654         return Point(__bounds.x, __bounds.y);
2655 }
2656
2657 Dimension
2658 _Control::GetSize(void) const
2659 {
2660         ClearLastResult();
2661         return Dimension(__bounds.width, __bounds.height);
2662 }
2663
2664 // E_SYSTEM
2665 result
2666 _Control::UpdateBoundsOfVisualElement(const FloatRectangle& controlBounds)
2667 {
2668         FloatRectangle rect(controlBounds.x, controlBounds.y, controlBounds.width, controlBounds.height);
2669
2670         _Control* pParent = GetParent();
2671         if (__area == _CONTROL_AREA_CLIENT && pParent)
2672         {
2673                 const Rectangle clientBounds = pParent->GetClientBounds();
2674                 rect.x += (float)clientBounds.x;
2675                 rect.y += (float)clientBounds.y;
2676         }
2677
2678         GetVisualElement()->SetBounds(rect);
2679
2680         return E_SUCCESS;
2681 }
2682
2683 bool
2684 _Control::IsInSizeRange(const Dimension& size)
2685 {
2686         return (__minSize.width <= size.width) && (size.width <= __maxSize.width) &&
2687                    (__minSize.height <= size.height) && (size.height <= __maxSize.height);
2688 }
2689
2690 // Custom Exception: ex) Location::Map
2691 result
2692 _Control::SetBoundsFinal(const FloatRectangle& newBounds, bool changeLayoutBaseRect, bool callBoundsChangeCallbacks)
2693 {
2694         result r = E_SUCCESS;
2695
2696         Rectangle bounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height);
2697
2698         _IControlDelegate& delegate = GetControlDelegate();
2699
2700         const bool moved   = (__bounds.x != bounds.x) || (__bounds.y != bounds.y);
2701         const bool resized = (__bounds.width != bounds.width) || (__bounds.height != bounds.height);
2702
2703         if ((moved || resized) && callBoundsChangeCallbacks)
2704         {
2705                 r = delegate.OnBoundsChanging(bounds);
2706                 if (IsFailed(r))
2707                 {
2708                         SysLogException(NID_UI, r, "[%s] Callback returns exception.", GetErrorMessage(r));
2709                         return r; // Relay the result;
2710                 }
2711         }
2712
2713         if (moved || resized)
2714         {
2715                 const FloatRectangle fbounds(bounds.x, bounds.y, bounds.width, bounds.height);
2716                 if (IsLayoutChangable() == false)
2717                 {
2718                         r = UpdateBoundsOfVisualElement(FloatRectangle(0, 0, fbounds.width, fbounds.height));
2719                 }
2720                 else
2721                 {
2722                         r = UpdateBoundsOfVisualElement(fbounds);
2723                 }
2724         }
2725
2726         SysAssert(r == E_SUCCESS); // [ToDo] Exception check and rollback.
2727
2728         __bounds = bounds;
2729
2730         if (changeLayoutBaseRect)
2731         {
2732                 __pLayoutContainer->OnChangeBaseRect();
2733         }
2734
2735         if ((moved || resized) && callBoundsChangeCallbacks)
2736         {
2737                 if (IsMovable() && IsResizable())
2738                 {
2739                         SetClientBounds(Rectangle(0, 0, GetSize().width, GetSize().height));
2740                 }
2741                 delegate.OnBoundsChanged();
2742
2743                 _Control* pParent = GetParent();
2744                 if (pParent)
2745                 {
2746                         pParent->GetControlDelegate().OnChildBoundsChanged(*this);
2747                 }
2748
2749                 ControlList& children = GetChildList();
2750                 _Control* pChild = null;
2751                 int childrenCount = children.GetCount();
2752
2753                 for (int index = 0; index < childrenCount; index++)
2754                 {
2755                         r = children.GetAt(index, pChild);
2756                         if (!IsFailed(r))
2757                         {
2758                                 pChild->GetControlDelegate().OnParentBoundsChanged(*this);
2759                         }
2760                 }
2761         }
2762
2763         ClearLastResult();
2764         return E_SUCCESS;
2765 }
2766
2767 // Custom Exception: ex) Location::Map
2768 result
2769 _Control::AdjustSizeToRange(void)
2770 {
2771         ClearLastResult();
2772         result r = E_SUCCESS;
2773
2774         SysAssert(IsResizable());
2775
2776         Dimension size = GetSize();
2777         bool changed = ::AdjustSizeToRange(size, __minSize, __maxSize);
2778         if (!changed)
2779         {
2780                 return E_SUCCESS;
2781         }
2782
2783         FloatRectangle newBounds(__bounds.x, __bounds.y, size.width, size.height);
2784         r = SetBoundsFinal(newBounds, true, true);
2785         if (IsFailed(r))
2786         {
2787                 return r;
2788         }
2789
2790         return E_SUCCESS;
2791 }
2792
2793 // Custom Exception: ex) Location::Map
2794 // E_INVALID_ARG
2795 result
2796 _Control::SetBoundsInternal(const FloatRectangle& bounds, bool callBoundsChangeCallbacks)
2797 {
2798         SysTryReturn(NID_UI,
2799                                 IsInSizeRange(Dimension(bounds.width, bounds.height)), E_INVALID_ARG,
2800                                 E_INVALID_ARG,
2801                                 "[E_INVALID_ARG] The specified size(%f, %f) is out of range from min size(%d, %d) to max size(%d, %d).",
2802                                 bounds.width, bounds.height, __minSize.width, __minSize.height, __maxSize.width, __maxSize.height);
2803
2804         SetUpdateLayoutState(true);
2805
2806         return SetBoundsFinal(bounds, true, callBoundsChangeCallbacks);
2807 }
2808
2809 // Custom Exception: ex) Location::Map
2810 // E_INVALID_ARG
2811 // E_UNSUPPORTED_OPERATION
2812 result
2813 _Control::SetBounds(const Rectangle& bounds, bool callBoundsChangeCallbacks)
2814 {
2815         ClearLastResult();
2816
2817         if (callBoundsChangeCallbacks)
2818         {
2819                 SysTryReturn(NID_UI,
2820                                         IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
2821                                         E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
2822         }
2823         FloatRectangle floatBounds(bounds.x, bounds.y, bounds.width, bounds.height);
2824
2825         return SetBoundsInternal(floatBounds, callBoundsChangeCallbacks);
2826 }
2827
2828 // Custom Exception: ex) Location::Map
2829 // E_INVALID_ARG
2830 // E_UNSUPPORTED_OPERATION
2831 result
2832 _Control::SetBounds(const FloatRectangle& bounds, bool callBoundsChangeCallbacks)
2833 {
2834         ClearLastResult();
2835
2836         if (callBoundsChangeCallbacks)
2837         {
2838                 SysTryReturn(NID_UI,
2839                                         IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
2840                                         E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
2841         }
2842
2843         return SetBoundsInternal(bounds, callBoundsChangeCallbacks);
2844 }
2845
2846 // A custom Exception can occur. ex) Location::Map
2847 // E_INVALID_ARG
2848 // E_UNSUPPORTED_OPERATION
2849 result
2850 _Control::SetPosition(const Point& position)
2851 {
2852         ClearLastResult();
2853
2854         SysTryReturn(NID_UI,
2855                                 IsMovable(), E_UNSUPPORTED_OPERATION,
2856                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable.");
2857
2858         return SetBoundsInternal(FloatRectangle(position.x, position.y, __bounds.width, __bounds.height), true);
2859 }
2860
2861 // Custom Exception: ex) Location::Map
2862 // E_INVALID_ARG
2863 // E_UNSUPPORTED_OPERATION
2864 result
2865 _Control::SetSize(const Dimension& size)
2866 {
2867         ClearLastResult();
2868
2869         SysTryReturn(NID_UI,
2870                                 IsResizable(), E_UNSUPPORTED_OPERATION,
2871                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
2872
2873         return SetBoundsInternal(FloatRectangle(__bounds.x, __bounds.y, size.width, size.height), true);
2874 }
2875
2876 Dimension
2877 _Control::GetMinimumSize(void) const
2878 {
2879         ClearLastResult();
2880
2881         return __minSize;
2882 }
2883
2884 Dimension
2885 _Control::GetMaximumSize(void) const
2886 {
2887         ClearLastResult();
2888
2889         return __maxSize;
2890 }
2891
2892 // Custom Exception: ex) Location::Map
2893 // E_UNSUPPORTED_OPERATION
2894 // E_INVALID_ARG
2895 result
2896 _Control::SetMinimumSize(const Dimension& newMinSize)
2897 {
2898         ClearLastResult();
2899
2900         SysTryReturn(NID_UI,
2901                                 IsResizable(), E_UNSUPPORTED_OPERATION,
2902                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
2903
2904         SysTryReturn(NID_UI,
2905                                 (newMinSize.width >= 0 && newMinSize.height >= 0), E_INVALID_ARG,
2906                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is smaller than 0.");
2907
2908         SysTryReturn(NID_UI,
2909                                 (newMinSize.width <= MAX_LENGTH && newMinSize.height <= MAX_LENGTH), E_INVALID_ARG,
2910                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is greater than %d.", MAX_LENGTH);
2911
2912         if (__maxSize.width < newMinSize.width)
2913         {
2914                 __maxSize.width = newMinSize.width;
2915         }
2916         if (__maxSize.height < newMinSize.height)
2917         {
2918                 __maxSize.height = newMinSize.height;
2919         }
2920
2921         __minSize = newMinSize;
2922         return AdjustSizeToRange();
2923 }
2924
2925 // Custom Exception: ex) Location::Map
2926 // E_UNSUPPORTED_OPERATION
2927 // E_INVALID_ARG
2928 result
2929 _Control::SetMaximumSize(const Dimension& newMaxSize)
2930 {
2931         ClearLastResult();
2932
2933         SysTryReturn(NID_UI,
2934                                 IsResizable(), E_UNSUPPORTED_OPERATION,
2935                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not resizable.");
2936
2937         SysTryReturn(NID_UI,
2938                                 (newMaxSize.width >= 0 && newMaxSize.height >= 0), E_INVALID_ARG,
2939                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is smaller than 0.");
2940
2941         SysTryReturn(NID_UI,
2942                                 (newMaxSize.width <= MAX_LENGTH && newMaxSize.height <= MAX_LENGTH), E_INVALID_ARG,
2943                                 E_INVALID_ARG, "[E_INVALID_ARG] The width or height is greater than %d.", MAX_LENGTH);
2944
2945         if (newMaxSize.width < __minSize.width)
2946         {
2947                 __minSize.width = newMaxSize.width;
2948         }
2949         if (newMaxSize.height < __minSize.height)
2950         {
2951                 __minSize.height = newMaxSize.height;
2952         }
2953
2954         __maxSize = newMaxSize;
2955         return AdjustSizeToRange();
2956 }
2957
2958 Point
2959 _Control::ConvertToControlPosition(const Point& screenPosition) const
2960 {
2961         Point controlPosition;
2962         Rectangle absoluteBounds = GetAbsoluteBounds();
2963
2964         controlPosition.x = screenPosition.x - absoluteBounds.x;
2965         controlPosition.y = screenPosition.y - absoluteBounds.y;
2966
2967         return controlPosition;
2968 }
2969
2970 Point
2971 _Control::ConvertToScreenPosition(const Point& controlPosition) const
2972 {
2973         Point screenPosition;
2974         Rectangle absoluteBounds = GetAbsoluteBounds();
2975
2976         screenPosition.x = controlPosition.x + absoluteBounds.x;
2977         screenPosition.y = controlPosition.y + absoluteBounds.y;
2978
2979         return screenPosition;
2980 }
2981
2982 Rectangle
2983 _Control::GetClientBounds(void) const
2984 {
2985         if (!__isSetClientBounds)
2986         {
2987                 return Rectangle(0, 0, __bounds.width, __bounds.height);
2988         }
2989
2990         return __clientBounds;
2991 }
2992
2993 Rectangle
2994 _Control::GetClientBounds(const Dimension& size) const
2995 {
2996         if (!__isSetClientBounds)
2997         {
2998                 return Rectangle(0, 0, size.width, size.height);
2999         }
3000
3001         return __clientBounds;
3002 }
3003
3004 Rectangle
3005 _Control::GetAbsoluteBounds(void) const
3006 {
3007 #if !defined(MULTI_WINDOW)
3008         Point accumPoint;
3009         Rectangle absoluteBounds;
3010         Rectangle clientBounds;
3011
3012         const _Control* pSelf = this;
3013         const _Control* pParent = GetParent();
3014         if (!pParent)
3015         {
3016                 accumPoint += pSelf->GetPosition();
3017         }
3018
3019         while (pParent)
3020         {
3021                 if ((pSelf->GetArea() == _CONTROL_AREA_CLIENT) && pParent)
3022                 {
3023                         clientBounds = pParent->GetClientBounds();
3024                         accumPoint += Point(clientBounds.x, clientBounds.y);
3025                         accumPoint.x -= pParent->GetHorizontalScrollPosition();
3026                         accumPoint.y -= pParent->GetVerticalScrollPosition();
3027                 }
3028
3029                 accumPoint += pSelf->GetPosition();
3030                 pSelf = pParent;
3031                 pParent = pParent->GetParent();
3032         }
3033
3034         absoluteBounds.x = accumPoint.x;
3035         absoluteBounds.y = accumPoint.y;
3036         absoluteBounds.width = __bounds.width;
3037         absoluteBounds.height = __bounds.height;
3038
3039         return absoluteBounds;
3040 #else
3041         Point accumPoint;
3042         Rectangle absoluteBounds;
3043         Rectangle clientBounds;
3044
3045         const _Control* pSelf = this;
3046         const _Control* pParent = GetParent();
3047
3048         while (pParent)
3049         {
3050                 if ((pSelf->GetArea() == _CONTROL_AREA_CLIENT) && pParent)
3051                 {
3052                         clientBounds = pParent->GetClientBounds();
3053                         accumPoint += Point(clientBounds.x, clientBounds.y);
3054                         accumPoint.x -= pParent->GetHorizontalScrollPosition();
3055                         accumPoint.y -= pParent->GetVerticalScrollPosition();
3056                 }
3057
3058                 accumPoint += pSelf->GetPosition();
3059                 pSelf = pParent;
3060                 pParent = pParent->GetParent();
3061         }
3062
3063         _Window* pWindow = GetRootWindow();
3064
3065         if (pWindow)
3066         {
3067                 Point winPoint = pWindow->GetPosition();
3068
3069                 accumPoint.x += winPoint.x;
3070                 accumPoint.y += winPoint.y;
3071         }
3072
3073         absoluteBounds.x = accumPoint.x;
3074         absoluteBounds.y = accumPoint.y;
3075         absoluteBounds.width = __bounds.width;
3076         absoluteBounds.height = __bounds.height;
3077
3078         return absoluteBounds;
3079 #endif
3080 }
3081
3082 result
3083 _Control::SetClientBounds(const Rectangle& bounds)
3084 {
3085         ClearLastResult();
3086
3087         SysTryReturn(NID_UI,
3088                                 IsMovable() && IsResizable(), E_UNSUPPORTED_OPERATION,
3089                                 E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Not movable nor resizable.");
3090         __clientBounds = bounds;
3091         __isSetClientBounds = true;
3092
3093         SetUpdateLayoutState(true);
3094
3095         return E_SUCCESS;
3096 }
3097
3098 bool
3099 _Control::IsCalledSetClientBounds(void)
3100 {
3101         return __isSetClientBounds;
3102 }
3103
3104 Color
3105 _Control::GetBackgroundColor(void) const
3106 {
3107         ClearLastResult();
3108         return __backgroundColor;
3109 }
3110
3111 void
3112 _Control::SetBackgroundColor(const Color& color)
3113 {
3114         ClearLastResult();
3115         __backgroundColor = color;
3116
3117         _ControlVisualElement* pCVE = dynamic_cast <_ControlVisualElement*>(GetVisualElement());
3118         if (pCVE)
3119         {
3120                 pCVE->SetBackgroundColor(
3121                         _Colorf(
3122                                 (float)color.GetRed() / 255.0f,
3123                                 (float)color.GetGreen() / 255.0f,
3124                                 (float)color.GetBlue() / 255.0f,
3125                                 (float)color.GetAlpha() / 255.0f
3126                         )
3127                 );
3128         }
3129 }
3130
3131 _Control::~_Control(void)
3132 {
3133         __destroying = true;
3134
3135         DetachAllChildren();
3136         DetachAllOwnees();
3137         _ControlManager::GetInstance()->TakeFocusFromControl(*this);
3138         DisposeControl();
3139         ReleaseHandle();
3140
3141         if (__pFont)
3142         {
3143                 delete __pFont;
3144                 __pFont = null;
3145         }
3146
3147 //      Dangerous: it clears last result and log in catch block.
3148 //      ClearLastResult();
3149 }
3150
3151 void
3152 _Control::DisposeControl(void)
3153 {
3154         __pControlDelegate = null;
3155
3156         delete __pLayoutItemHandler;
3157         __pLayoutItemHandler = null;
3158
3159         delete __pLayoutContainer;
3160         __pLayoutContainer = null;
3161
3162         delete __pChildren;
3163         __pChildren = null;
3164
3165         delete __pOwnees;
3166         __pOwnees = null;
3167
3168         if (__pVisualElement)
3169         {
3170                 __pVisualElement->Destroy();
3171         }
3172         __pVisualElement = null;
3173
3174         delete __pVisualElementContentProvider;
3175         __pVisualElementContentProvider = null;
3176
3177         delete __pVisualElementEventListener;
3178         __pVisualElementEventListener = null;
3179
3180         delete __pCoreGestureDetectors;
3181         __pCoreGestureDetectors = null;
3182
3183         delete __pDataBindingContext;
3184         __pDataBindingContext = null;
3185
3186         ClearStartedGestureDetectorList();
3187         delete __pDetectStartedGestureMap;
3188         __pDetectStartedGestureMap = null;
3189
3190         delete __pDelayedTouchInfoList;
3191         __pDelayedTouchInfoList = null;
3192
3193         delete __pAccessibilityContainer;
3194         __pAccessibilityContainer = null;
3195 }
3196
3197 // E_OUT_OF_MEMORY
3198 // E_SYSTEM
3199 _Control::_Control(void)
3200         : __name(L"")
3201         , __pParent(null)
3202         , __pChildren(null)
3203         , __pOwnees(null)
3204         , __bounds(0, 0, 0, 0)
3205         , __clientBounds(0, 0, 0, 0)
3206         , __absoluteBounds(0, 0, 0, 0)
3207         , __invalidatedBounds(0, 0, 0, 0)
3208         , __minSize(Dimension(0, 0))
3209         , __maxSize(Dimension(MAX_LENGTH, MAX_LENGTH))
3210         , __backgroundColor(Color::GetColor(COLOR_ID_BLACK))
3211         , __movable(true)
3212         , __resizable(true)
3213         , __focusable(true)
3214         , __nativeObjectFocusable(true)
3215         , __enabledState(true)
3216         , __inputEnabledState(true)
3217         , __visibleState(true)
3218         , __initVisibleState(false)
3219         , __clipToParent(true)
3220         , __focusSet(false)
3221         , __multiTouchEnabled(false)
3222         , __dragEnabled(false)
3223         , __dropEnabled(false)
3224         , __drawWhenVisible(true)
3225         , __isPostOrderTraversal(false)
3226         , __isCalledCallOnAttachingToMainTree(false)
3227         , __isCalledCallOnAttachedToMainTree(false)
3228         , __isSetClientBounds(false)
3229         , __isCalledGetCanvasN(false)
3230         , __pVisualElementContentProvider(null)
3231         , __pVisualElement(null)
3232         , __pVisualElementEventListener(null)
3233         , __pLayoutItemHandler(null)
3234         , __pPortraitLayout(null)
3235         , __pLandscapeLayout(null)
3236         , __pLayoutContainer(null)
3237         , __area(_CONTROL_AREA_NONE)
3238         , __layer(_CONTROL_LAYER_NONE)
3239         , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
3240         , __pTouchEventPreviewer(null)
3241         , __pKeyEventPreviewer(null)
3242         , __pNotificationEventPreviewer(null)
3243         , __pKeyEventListener(null)
3244         , __pFocusEventListener(null)
3245         , __pNotificationEventListener(null)
3246         , __pCoreGestureDetectors(null)
3247         , __pDetectStartedGestureMap(null)
3248         , __pDelayedTouchInfoList(null)
3249         ,__touchMoveAllowance(TOUCH_MOVE_ALLOWANCE_SENSITIVE)
3250         , __isSentDelayedEvent(false)
3251         , __isSendingDelayedEvent(false)
3252         , __isChangingEventTarget(false)
3253         , __pDataBindingContext(null)
3254         , __pControlDelegate(null)
3255         , __pUserData(null)
3256         , __destroying(false)
3257         , __isEventReceivable(true)
3258         , __isControlFontChanged(false)
3259         , __pFont(null)
3260         , __fontName(L"")
3261 {
3262         ClearLastResult();
3263
3264         SetControlDelegate(*this);
3265         __pAccessibilityContainer = new (std::nothrow) _AccessibilityContainer(*this);
3266
3267         __pLayoutItemHandler = new (std::nothrow) LayoutItemHandler(this);
3268         if (!__pLayoutItemHandler)
3269         {
3270                 goto CATCH;
3271         }
3272
3273         __pLayoutContainer = CreateLayoutContainerN(__pLayoutItemHandler);
3274         if (!__pLayoutContainer)
3275         {
3276                 goto CATCH;
3277         }
3278
3279         __pChildren = ::CreateControlListN();
3280         if (!__pChildren)
3281         {
3282                 goto CATCH;
3283         }
3284
3285         __pOwnees = ::CreateWindowListN();
3286         if (!__pOwnees)
3287         {
3288                 goto CATCH;
3289         }
3290
3291         __pVisualElement = ::CreateVisualElementN();
3292         if (!__pVisualElement)
3293         {
3294                 goto CATCH;
3295         }
3296
3297         __pVisualElement->SetUserData(this);
3298         __pVisualElement->SetClipChildrenEnabled(true);
3299         __pVisualElement->SetRedrawOnResizeEnabled(true);
3300
3301         __pVisualElementContentProvider = new (std::nothrow) ControlVisualElementContentProvider(*this);
3302         SysTryCatch(NID_UI, __pVisualElementContentProvider, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
3303
3304         GetVisualElement()->SetContentProvider(__pVisualElementContentProvider);
3305
3306         __pVisualElementEventListener = new (std::nothrow) ControlVisualElementEventListener(*this);
3307         SysTryCatch(NID_UI, __pVisualElementEventListener, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
3308
3309         GetVisualElement()->SetVisualElementEventListener(__pVisualElementEventListener);
3310
3311         __pDetectStartedGestureMap = ::CreateGestureMapN();
3312         if (!__pDetectStartedGestureMap)
3313         {
3314                 goto CATCH;
3315         }
3316
3317         __pCoreGestureDetectors = new (std::nothrow) LinkedListT<_TouchGestureDetector*>;
3318         if (!__pCoreGestureDetectors)
3319         {
3320                 goto CATCH;
3321         }
3322
3323         __pDelayedTouchInfoList = new (std::nothrow) LinkedListT<_TouchInfo*>;
3324         if (!__pDelayedTouchInfoList)
3325         {
3326                 goto CATCH;
3327         }
3328
3329         SetEventPreviewer<_UI_EVENT_TOUCH>(this);
3330         SetEventPreviewer<_UI_EVENT_KEY>(this);
3331         SetEventPreviewer<_UI_EVENT_NOTIFICAITON>(this);
3332
3333         SetEventListener<_UI_EVENT_NOTIFICAITON>(this);
3334         SetEventListener<_UI_EVENT_FOCUS>(this);
3335
3336         SetPropagatedTouchEventListener(this);
3337         SetPropagatedKeyEventListener(this);
3338         _ResourceManager::GetInstance()->GetColor(L"DEFAULTCOLORTABLE::background", __backgroundColor);
3339
3340         if (IsFailed(GetLastResult()))
3341         {
3342                 SysLog(NID_UI_CTRL, "[%s] Propagated", GetErrorMessage(GetLastResult()));
3343         }
3344
3345         return;
3346
3347 CATCH:
3348         DisposeControl();
3349         return;
3350 }
3351
3352 void
3353 _Control::AcquireHandle(void)
3354 {
3355         __controlHandle = _ControlManager::GetInstance()->Register(this); // [ToDo] exception?
3356 }
3357
3358 void
3359 _Control::ReleaseHandle(void)
3360 {
3361         _ControlManager::GetInstance()->Release(__controlHandle); // [ToDo] exception?
3362 }
3363
3364 void
3365 _Control::SetDrawWhenVisible(bool draw)
3366 {
3367         __drawWhenVisible = draw;
3368 }
3369
3370 bool
3371 _Control::IsDrawWhenVisible(void)
3372 {
3373         return __drawWhenVisible;
3374 }
3375
3376 void
3377 _Control::SetTerminatingOrder(bool postOrderTraversal)
3378 {
3379         __isPostOrderTraversal = postOrderTraversal;
3380 }
3381
3382 void
3383 _Control::SetParent(_Control* pParent)
3384 {
3385         ClearLastResult();
3386         __pParent = pParent;
3387 }
3388
3389 // E_OUT_OF_MEMORY
3390 // Only called by _Window::SetOwner(pOwner)
3391 result
3392 _Control::AttachOwnee(_Window& window)
3393 {
3394         ClearLastResult();
3395         result r = E_SUCCESS;
3396
3397         _Control* pOldOwner = window.GetOwner();
3398         if (pOldOwner)
3399         {
3400                 pOldOwner->DetachOwnee(window);
3401         }
3402
3403         r = __pOwnees->Add(&window);
3404         if (IsFailed(r))
3405         {
3406                 SysAssert(r == E_OUT_OF_MEMORY);
3407                 SysLogException(NID_UI, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
3408                 return E_OUT_OF_MEMORY;
3409         }
3410
3411         return E_SUCCESS;
3412 }
3413
3414 int
3415 _Control::GetOwneeCount(void) const
3416 {
3417         ClearLastResult();
3418         return __pOwnees->GetCount();
3419 }
3420
3421 // E_OUT_OF_RANGE
3422 _Window*
3423 _Control::GetOwnee(int index) const
3424 {
3425         ClearLastResult();
3426         result r = E_SUCCESS;
3427
3428         _Window* pOwnee = null;
3429         r = __pOwnees->GetAt(index, pOwnee);
3430         if (IsFailed(r))
3431         {
3432                 SysAssert(r == E_OUT_OF_RANGE);
3433                 SysLogException(NID_UI, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is out of range.");
3434                 return null;
3435         }
3436
3437         return pOwnee;
3438 }
3439
3440 // Called by _Window::SetOwner(null)
3441 void
3442 _Control::DetachOwnee(_Window& ownee)
3443 {
3444         ClearLastResult();
3445         result r = E_SUCCESS;
3446
3447         _Control* pOwner = ownee.GetOwner();
3448         if (pOwner != this)
3449         {
3450                 return;
3451         }
3452
3453         r = __pOwnees->Remove(&ownee);
3454         SysAssert(r == E_SUCCESS);
3455 }
3456
3457 void
3458 _Control::DetachAllOwnees(void)
3459 {
3460         ClearLastResult();
3461
3462         int owneeCount = GetOwneeCount();
3463         while (owneeCount--)
3464         {
3465                 _Window* pOwnee = GetOwnee(0);
3466
3467                 SysAssert(pOwnee);
3468                 if (pOwnee == null)
3469                 {
3470                         continue;
3471                 }
3472
3473                 pOwnee->SetOwner(null);
3474         }
3475 }
3476
3477 _Layout::LayoutContainer&
3478 _Control::GetLayoutContainer() const
3479 {
3480         return *__pLayoutContainer;
3481 }
3482
3483 void
3484 _Control::SetMultiTouchEnabled(bool enabled)
3485 {
3486         ClearLastResult();
3487         __multiTouchEnabled = enabled;
3488 }
3489
3490 bool
3491 _Control::IsMultiTouchEnabled(void) const
3492 {
3493         ClearLastResult();
3494         return __multiTouchEnabled;
3495 }
3496
3497 void
3498 _Control::SetMovable(bool movable)
3499 {
3500         ClearLastResult();
3501         __movable = movable;
3502 }
3503
3504 void
3505 _Control::SetResizable(bool resizable)
3506 {
3507         ClearLastResult();
3508
3509         if (!resizable)
3510         {
3511                 __minSize = __maxSize = GetSize();
3512         }
3513
3514         if (!__resizable && resizable)
3515         {
3516                 __minSize = Dimension(0, 0);
3517                 __maxSize = Dimension(MAX_LENGTH, MAX_LENGTH);
3518         }
3519
3520         __resizable = resizable;
3521 }
3522
3523 _Layout::Layout*
3524 _Control::GetLayout(void) const
3525 {
3526         ClearLastResult();
3527         _Layout::Layout* pLayout = __pLayoutContainer->GetLayout();
3528
3529         SysAssert(GetLastResult() == E_SUCCESS);
3530         return pLayout;
3531 }
3532
3533 result
3534 _Control::SetCurrentLayout(_Layout::Layout& layout)
3535 {
3536         ClearLastResult();
3537         return __pLayoutContainer->SetCurrentLayout(layout);
3538 }
3539
3540 result
3541 _Control::AddLayout(_Layout::Layout& layout)
3542 {
3543         ClearLastResult();
3544         return __pLayoutContainer->AddLayout(layout);
3545 }
3546
3547 void
3548 _Control::SetUpdateLayoutState(bool state)
3549 {
3550         _Layout::Layout* pLayout = GetLayout();
3551         if (pLayout)
3552         {
3553                 pLayout->SetUpdateState(state);
3554         }
3555 }
3556
3557 _DataBindingContext*
3558 _Control::GetDataBindingContext(void)
3559 {
3560         return __pDataBindingContext;
3561 }
3562 _AccessibilityContainer*
3563 _Control::GetAccessibilityContainer(void)
3564 {
3565         return __pAccessibilityContainer;
3566 }
3567 void
3568 _Control::SetDataBindingContext(_DataBindingContext* pDataBindingContext)
3569 {
3570         __pDataBindingContext = pDataBindingContext;
3571 }
3572
3573 Tizen::Base::String
3574 _Control::GetDescription(void) const
3575 {
3576         return String(L"");
3577 }
3578
3579 void
3580 _Control::SetTouchCapture(bool allowOutOfBounds)
3581 {
3582         _TouchManager* pTouchManager = _TouchManager::GetInstance();
3583         SysTryReturnVoidResult(NID_UI, pTouchManager != null, E_SYSTEM, "[E_SYSTEM] TouchManager Instance is null");
3584
3585         pTouchManager->SetCapturedControl(this, allowOutOfBounds);
3586 }
3587
3588 void
3589 _Control::ReleaseTouchCapture(void)
3590 {
3591         _TouchManager* pTouchManager = _TouchManager::GetInstance();
3592         SysTryReturnVoidResult(NID_UI, pTouchManager != null, E_SYSTEM, "[E_SYSTEM] TouchManager Instance is null");
3593
3594         pTouchManager->SetCapturedControl(null, false);
3595 }
3596
3597 _Control*
3598 _Control::GetTopmostChildAt(const Point& point) const
3599 {
3600         _Control* pTouchedControl = null;
3601         FloatPoint ptf((float) point.x, (float) point.y);
3602         _ControlManager* pControlManager = _ControlManager::GetInstance();
3603         SysTryReturn(NID_UI, pControlManager, null, E_SYSTEM, "[E_SYSTEM] _ControlManager does not exist.");
3604
3605 #if !defined(MULTI_WINDOW)
3606         _ControlVisualElement* pRootControlElement =
3607                                 dynamic_cast <_ControlVisualElement*>(pControlManager->GetRoot().GetVisualElement());
3608 #else
3609         _Window* pRootWindow = GetRootWindow();
3610         SysTryReturn(NID_UI, pRootWindow, null, E_SYSTEM, "[E_SYSTEM] GetRootWindow() is null, this control is detached from the main trree");
3611
3612         _ControlVisualElement* pRootControlElement =
3613                                 dynamic_cast <_ControlVisualElement*>(pRootWindow->GetVisualElement());
3614 #endif
3615
3616         SysTryReturn(NID_UI, pRootControlElement, null, E_SYSTEM, "[E_SYSTEM] pRootControlElement is null.");
3617
3618         _ControlVisualElement* pHitTestElm = pRootControlElement->GetControlChildAtPoint(ptf);
3619         SysTryReturn(NID_UI, pHitTestElm, null, E_SYSTEM, "[E_SYSTEM] pHitTestElm is null.");
3620
3621         pTouchedControl = static_cast <_Control*>(pHitTestElm->GetUserData());
3622
3623         return pTouchedControl;
3624 }
3625
3626 Bitmap*
3627 _Control::GetCapturedBitmapN(bool includeChildren) const
3628 {
3629         result r = E_SUCCESS;
3630
3631         Canvas* pCanvas = null;
3632         Bitmap* pBitmap = null;
3633
3634         pBitmap = GetControlDelegate().OnCapturedBitmapRequestedN();
3635         if (pBitmap == null)
3636         {
3637                 FloatRectangle rect;
3638
3639                 Rectangle boundsInCanvas = GetBounds();
3640                 boundsInCanvas.x = boundsInCanvas.y = 0;
3641
3642                 pCanvas = new (std::nothrow) Canvas;
3643                 SysTryReturn(NID_UI, pCanvas, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
3644
3645                 r = pCanvas->Construct(boundsInCanvas);
3646                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
3647
3648                 GetVisualElement()->Draw();
3649                 ClearLastResult(); // [ToDo] Temp.
3650
3651                 rect.SetBounds(boundsInCanvas.x, boundsInCanvas.y, boundsInCanvas.width, boundsInCanvas.height);
3652
3653                 r = GetVisualElement()->Capture(*pCanvas, rect, includeChildren);
3654                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
3655
3656                 pBitmap = new (std::nothrow) Bitmap;
3657                 SysTryCatch(NID_UI, pBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
3658
3659                 r = pBitmap->Construct(*pCanvas, boundsInCanvas);
3660                 if (IsFailed(r))
3661                 {
3662                         SysAssert(r == E_OUT_OF_MEMORY);
3663                         SysTryCatch(NID_UI, r == E_SUCCESS, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
3664                 }
3665
3666                 SysLog(NID_UI, "Bitmap (width:%d, height:%d)", pBitmap->GetWidth(), pBitmap->GetHeight());
3667
3668                 delete pCanvas;
3669         }
3670
3671         return pBitmap;
3672
3673 CATCH:
3674         delete pCanvas;
3675         delete pBitmap;
3676         SetLastResult(r);
3677
3678         return null;
3679 }
3680
3681 Tizen::Graphics::Rectangle
3682 _Control::GetInvalidatedBounds(void) const
3683 {
3684         return __invalidatedBounds;
3685 }
3686
3687 result
3688 _Control::AddGestureDetector(const _TouchGestureDetector& gestureDetector)
3689 {
3690         ClearLastResult();
3691
3692         bool exist = __pCoreGestureDetectors->Contains(const_cast<_TouchGestureDetector*>(&gestureDetector));
3693         SysTryReturnResult(NID_UI, exist == false, E_OBJ_ALREADY_EXIST, "[E_OBJ_ALREADY_EXIST]__pCoreGestureDetectors has gesture already");
3694
3695         result r = __pCoreGestureDetectors->Add(const_cast<_TouchGestureDetector*>(&gestureDetector));
3696         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
3697
3698         r = const_cast<_TouchGestureDetector&>(gestureDetector).SetControl(*this);
3699         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "Control handle is not valid.");
3700
3701         return r;
3702 }
3703
3704 result
3705 _Control::RemoveGestureDetector(const _TouchGestureDetector& gestureDetector)
3706 {
3707         ClearLastResult();
3708
3709         result r = __pCoreGestureDetectors->Remove(&(const_cast<_TouchGestureDetector&>(gestureDetector)));
3710         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
3711
3712         return E_SUCCESS;
3713 }
3714
3715 IListT <_TouchGestureDetector*>*
3716 _Control::GetGestureDetectorList(void) const
3717 {
3718         return __pCoreGestureDetectors;
3719 }
3720
3721 IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>*
3722 _Control::GetStartedGestureDetectorEnumeratorN(void) const
3723 {
3724         return __pDetectStartedGestureMap->GetMapEnumeratorN();
3725 }
3726
3727 result
3728 _Control::AddStartedGestureDetector(const _TouchGestureDetector& gestureDetector, _TouchGestureDetectorState state)
3729 {
3730         ClearLastResult();
3731
3732         result r = E_SUCCESS;
3733
3734         bool exist = false;
3735         __pDetectStartedGestureMap->ContainsKey(const_cast<_TouchGestureDetector*>(&gestureDetector), exist);
3736
3737         if (exist == false)
3738         {
3739                 r = __pDetectStartedGestureMap->Add(const_cast<_TouchGestureDetector*>(&gestureDetector), state);
3740         }
3741
3742         return r;
3743 }
3744
3745 result
3746 _Control::SetStartedGestureDetector(const _TouchGestureDetector& gestureDetector, _TouchGestureDetectorState state)
3747 {
3748         ClearLastResult();
3749
3750         result r = E_SUCCESS;
3751
3752         bool exist = false;
3753         __pDetectStartedGestureMap->ContainsKey(const_cast<_TouchGestureDetector*>(&gestureDetector), exist);
3754
3755         if (exist == true)
3756         {
3757                 r = __pDetectStartedGestureMap->SetValue(const_cast<_TouchGestureDetector*>(&gestureDetector), state);
3758         }
3759
3760         return r;
3761 }
3762
3763 result
3764 _Control::ClearStartedGestureDetectorList(void)
3765 {
3766         ClearLastResult();
3767
3768         result r = E_SUCCESS;
3769
3770         IListT<_TouchGestureDetector*>* pList = __pDetectStartedGestureMap->GetKeysN();
3771         if (pList)
3772         {
3773                 IEnumeratorT<_TouchGestureDetector*>* pEnumerator = pList->GetEnumeratorN();
3774                 if (pEnumerator)
3775                 {
3776                         while(pEnumerator->MoveNext() == E_SUCCESS)
3777                         {
3778                                 _TouchGestureDetector* pGestureDetector = null;
3779                                 pEnumerator->GetCurrent(pGestureDetector);
3780
3781                                 if (pGestureDetector == null)
3782                                 {
3783                                         continue;
3784                                 }
3785
3786                                 __pDetectStartedGestureMap->Remove(pGestureDetector);
3787                         }
3788                         delete pEnumerator;
3789                 }
3790                 delete pList;
3791         }
3792
3793         IEnumeratorT<_TouchInfo*>* pEnumerator = __pDelayedTouchInfoList->GetEnumeratorN();
3794         if(pEnumerator)
3795         {
3796                 while(pEnumerator->MoveNext() == E_SUCCESS)
3797                 {
3798                         _TouchInfo* pTouchInfo = null;
3799                         pEnumerator->GetCurrent(pTouchInfo);
3800                         if (pTouchInfo == null)
3801                         {
3802                                 continue;
3803                         }
3804
3805                         delete pTouchInfo;
3806                 }
3807
3808                 __pDelayedTouchInfoList->RemoveAll();
3809                 delete pEnumerator;
3810         }
3811
3812         __isSentDelayedEvent = false;
3813         __isSendingDelayedEvent = false;
3814
3815         return r;
3816 }
3817
3818 bool
3819 _Control::IsDelayedTouchEventEnabled(void) const
3820 {
3821         bool existDelayTouchEventGesture = false;
3822
3823         IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>* pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
3824         if (pMapEnumerator)
3825         {
3826                 while(pMapEnumerator->MoveNext() == E_SUCCESS)
3827                 {
3828                         _TouchGestureDetector* pGestureDetector = null;
3829                         pMapEnumerator->GetKey(pGestureDetector);
3830
3831                         if (pGestureDetector == null)
3832                         {
3833                                 continue;
3834                         }
3835
3836                         _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
3837                         pMapEnumerator->GetValue(state);
3838
3839                         if (pGestureDetector->IsDelayTouchEventEnabled())
3840                         {
3841                                 existDelayTouchEventGesture = true;
3842                         }
3843                 }
3844                 delete pMapEnumerator;
3845         }
3846
3847         bool delayTouchEvent = false;
3848         if (existDelayTouchEventGesture)
3849         {
3850                 pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
3851                 if (pMapEnumerator)
3852                 {
3853                         while(pMapEnumerator->MoveNext() == E_SUCCESS)
3854                         {
3855                                 _TouchGestureDetector* pGestureDetector = null;
3856                                 pMapEnumerator->GetKey(pGestureDetector);
3857
3858                                 if (pGestureDetector == null)
3859                                 {
3860                                         continue;
3861                                 }
3862
3863                                 _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
3864                                 pMapEnumerator->GetValue(state);
3865
3866                                 if (state == _TOUCH_GESTURE_DETECTOR_STATE_STARTED)
3867                                 {
3868                                         delayTouchEvent = true;
3869                                         break;
3870                                 }
3871                         }
3872                         delete pMapEnumerator;
3873                 }
3874
3875                 return delayTouchEvent;
3876         }
3877         else
3878         {
3879                 return false;
3880         }
3881 }
3882
3883 bool
3884 _Control::IsPossibleToSendDelayedTouchEvent(void) const
3885 {
3886         bool existDelayTouchEventGesture = false;
3887
3888         IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>* pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
3889         if (pMapEnumerator)
3890         {
3891                 while(pMapEnumerator->MoveNext() == E_SUCCESS)
3892                 {
3893                         _TouchGestureDetector* pGestureDetector = null;
3894                         pMapEnumerator->GetKey(pGestureDetector);
3895
3896                         if (pGestureDetector == null)
3897                         {
3898                                 continue;
3899                         }
3900
3901                         _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
3902                         pMapEnumerator->GetValue(state);
3903
3904                         if (pGestureDetector->IsDelayTouchEventEnabled())
3905                         {
3906                                 existDelayTouchEventGesture = true;
3907                         }
3908                 }
3909                 delete pMapEnumerator;
3910         }
3911
3912         bool allFailed = true;
3913         if (existDelayTouchEventGesture)
3914         {
3915                 pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
3916                 if (pMapEnumerator)
3917                 {
3918                         while(pMapEnumerator->MoveNext() == E_SUCCESS)
3919                         {
3920                                 _TouchGestureDetector* pGestureDetector = null;
3921                                 pMapEnumerator->GetKey(pGestureDetector);
3922
3923                                 if (pGestureDetector == null)
3924                                 {
3925                                         continue;
3926                                 }
3927
3928                                 _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
3929                                 pMapEnumerator->GetValue(state);
3930
3931                                 if (state == _TOUCH_GESTURE_DETECTOR_STATE_SUCCESS)
3932                                 {
3933                                         allFailed = false;
3934                                         break;
3935                                 }
3936                         }
3937                         delete pMapEnumerator;
3938                 }
3939
3940                 return allFailed;
3941         }
3942         else
3943         {
3944                 return false;
3945         }
3946 }
3947
3948 bool
3949 _Control::IsCancelOnGestureSuccess(void) const
3950 {
3951         _TouchManager* pTouchManager = _TouchManager::GetInstance();
3952         SysAssert(pTouchManager != null);
3953
3954         IMapEnumeratorT <_TouchGestureDetector*, _TouchGestureDetectorState>* pMapEnumerator = __pDetectStartedGestureMap->GetMapEnumeratorN();
3955         if (pMapEnumerator)
3956         {
3957                 while(pMapEnumerator->MoveNext() == E_SUCCESS)
3958                 {
3959                         _TouchGestureDetector* pGestureDetector = null;
3960                         pMapEnumerator->GetKey(pGestureDetector);
3961
3962                         if (pGestureDetector == null)
3963                         {
3964                                 continue;
3965                         }
3966
3967                         _TouchGestureDetectorState state = _TOUCH_GESTURE_DETECTOR_STATE_READY;
3968                         pMapEnumerator->GetValue(state);
3969
3970                         if (pGestureDetector->IsCancelTouchEventOnSuccessEnabled() && state == _TOUCH_GESTURE_DETECTOR_STATE_SUCCESS && !pTouchManager->IsTouchCanceledOnGestureSuccess())
3971                         {
3972                                 delete pMapEnumerator;
3973                                 return true;
3974                         }
3975                 }
3976                 delete pMapEnumerator;
3977         }
3978
3979         return false;
3980 }
3981
3982 bool
3983 _Control::IsSentDelayedEvent(void) const
3984 {
3985         return __isSentDelayedEvent;
3986 }
3987
3988 void
3989 _Control::SetSentDelayedEvent(bool sent)
3990 {
3991         __isSentDelayedEvent = sent;
3992 }
3993
3994 void
3995 _Control::SetSendingDelayedEvent(bool sending)
3996 {
3997         __isSendingDelayedEvent = sending;
3998 }
3999
4000 bool
4001 _Control::IsSendingDelayedEvent(void) const
4002 {
4003         return __isSendingDelayedEvent;
4004 }
4005
4006 void
4007 _Control::AddTouchInfo(const _TouchInfo& touchInfo)
4008 {
4009         _TouchManager* pTouchManager = _TouchManager::GetInstance();
4010         SysTryReturnVoidResult(NID_UI, pTouchManager, E_SYSTEM, "[E_SYSTEM] _TouchManager does not exist.");
4011
4012         _TouchInfo* pTouchInfo = new (std::nothrow) _TouchInfo;
4013         SysTryReturnVoidResult(NID_UI, pTouchInfo, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4014
4015         Point screenPoint = pTouchManager->GetScreenPoint(touchInfo.GetPointId());
4016         pTouchInfo->SetTouchInfo(touchInfo.GetPointId(), touchInfo.GetTouchStatus(), screenPoint, false, 0);
4017         __pDelayedTouchInfoList->Add(pTouchInfo);
4018 }
4019
4020 IListT<_TouchInfo*>*
4021 _Control::GetTouchInfoList(void)
4022 {
4023         return __pDelayedTouchInfoList;
4024 }
4025
4026 _VisualElement*
4027 _Control::GetVisualElement(void) const
4028 {
4029         SysAssert(__pVisualElement);
4030         return __pVisualElement;
4031 }
4032
4033 void
4034 _Control::PrintDescription(bool printChildren)
4035 {
4036         int count = PrintDescription(printChildren, 0);
4037
4038         SysLog(NID_UI, "%d controls were printed.", count);
4039 }
4040
4041 int
4042 _Control::PrintDescription(bool printChildren, int depth)
4043 {
4044         String indent(L"");
4045         String format(L"");
4046
4047         format.Format(LOG_LEN_MAX, L"%d", depth);
4048
4049         for (int i = 0; i < depth; i++)
4050         {
4051                 indent.Append(L"   ");
4052         }
4053
4054         indent.Append(format);
4055
4056         String delimiter(L"-------------------------------------------------------------------------------------------");
4057         SysLog(NID_UI, "%ls", delimiter.GetPointer());
4058
4059         // Public
4060         String publicDescription = GetControlDelegate().GetDescription();
4061         if (!publicDescription.IsEmpty())
4062         {
4063                 SysLog(NID_UI, "%ls %ls", indent.GetPointer(), publicDescription.GetPointer());
4064         }
4065
4066         // Core
4067         SysLog(NID_UI, "%ls 0x%x(%d %ls) enable(%d) enableState(%d) visible(%d) visibleState(%d) focusable(%d) clip(%d) movable(%d) resizable(%d)",
4068                 indent.GetPointer(), this, __controlHandle.ToInt(), GetName().GetPointer(), IsEnabled(), GetEnableState(), IsVisible(), GetVisibleState(),
4069                 IsFocusable(), IsClipToParent(), IsMovable(), IsResizable());
4070
4071         Rectangle bounds = GetBounds();
4072         Dimension min = GetMinimumSize();
4073         Dimension max = GetMaximumSize();
4074         Rectangle clientBounds = GetClientBounds();
4075         Rectangle absoluteBounds = GetAbsoluteBounds();
4076
4077         SysLog(NID_UI, "%ls bounds(%d %d %d %d) min(%d %d) max(%d %d) scrollPos(%d %d) cbounds(%d %d %d %d) abounds(%d %d %d %d)",
4078                 indent.GetPointer(), bounds.x, bounds.y, bounds.width, bounds.height,
4079                 min.width, min.height, max.width, max.height,
4080                 GetVerticalScrollPosition(), GetHorizontalScrollPosition(),
4081                 clientBounds.x, clientBounds.y, clientBounds.width, clientBounds.height,
4082                 absoluteBounds.x, absoluteBounds.y, absoluteBounds.width, absoluteBounds.height);
4083
4084         SysLog(NID_UI, "%ls bgColor(0x%x) layoutable(%d) orientation(%d) drag(%d) drop(%d) area(%d) layer(%d)",
4085                 indent.GetPointer(), __backgroundColor.GetRGB32(), IsLayoutable(), GetOrientation(), IsDragEnabled(), IsDropEnabled(), GetArea(), GetLayer());
4086
4087         Canvas* pCanvas = GetCanvasN();
4088         if (pCanvas)
4089         {
4090                 Rectangle canvasBounds = pCanvas->GetBounds();
4091                 Color canvasBackgroundColor = pCanvas->GetBackgroundColor();
4092                 Color canvasForegroundColor = pCanvas->GetForegroundColor();
4093
4094                 SysLog(NID_UI, "%ls canvas.bounds(%d %d %d %d) canvas.bgColor(0x%x) canvas.fgColor(0x%x)",
4095                         indent.GetPointer(), canvasBounds.x, canvasBounds.y, canvasBounds.width, canvasBounds.height, canvasBackgroundColor.GetRGB32(), canvasForegroundColor.GetRGB32());
4096
4097                 delete pCanvas;
4098         }
4099
4100         SysLog(NID_UI, "%ls DataBindingContext(0x%x) ControlDelegate(0x%x) UserData(0x%x) destroying(%d)",
4101                 indent.GetPointer(), __pDataBindingContext, __pControlDelegate, __pUserData, __destroying);
4102
4103         // Ownees
4104         String ownees(L"");
4105
4106         for (int i = 0; i < GetOwneeCount(); ++i)
4107         {
4108                 String ownee(L"");
4109                 _Window* pOwnee = GetOwnee(i);
4110                 if (pOwnee)
4111                 {
4112                         ownee.Format(LOG_LEN_MAX, L"0x%x ", pOwnee);
4113                         ownees.Append(ownee);
4114                 }
4115         }
4116
4117         if (!ownees.IsEmpty())
4118         {
4119                 SysLog(NID_UI, "%ls Ownees(%ls)", indent.GetPointer(), ownees.GetPointer());
4120         }
4121
4122         SysLog(NID_UI, "%ls VisualElement(0x%x) VisualElementContentProvider(0x%x) VisualElementEventListener(0x%x)",
4123                 indent.GetPointer(), GetVisualElement(), __pVisualElementContentProvider, __pVisualElementEventListener);
4124
4125         // Layout
4126         SysLog(NID_UI, "%ls LayoutItemHandler(0x%x) PortraitLayout(0x%x) LandscapeLayout(0x%x) LayoutContainer(0x%x)",
4127                 indent.GetPointer(), __pLayoutItemHandler, __pPortraitLayout, __pLandscapeLayout, __pLayoutContainer);
4128
4129         // Derived class
4130         String description = GetDescription();
4131         if (!description.IsEmpty())
4132         {
4133                 SysLog(NID_UI, "%ls %ls", indent.GetPointer(), description.GetPointer());
4134         }
4135
4136         // Print Gesture List
4137         IListT<_TouchGestureDetector*>* pGestureList = GetGestureDetectorList();
4138         SysTryReturn(NID_UI, pGestureList, 0, E_SYSTEM, "[E_SYSTEM] System error occurred.");
4139
4140         IEnumeratorT<_TouchGestureDetector*>* pEnumerator = pGestureList->GetEnumeratorN();
4141         SysTryReturn(NID_UI, pEnumerator, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
4142
4143         while (pEnumerator->MoveNext() == E_SUCCESS)
4144         {
4145                 _TouchGestureDetector* pGestureDetector = null;
4146                 pEnumerator->GetCurrent(pGestureDetector);
4147                 if (pGestureDetector)
4148                 {
4149                         SysLog(NID_UI, "%ls %ls", indent.GetPointer(), pGestureDetector->GetDescription().GetPointer());
4150                 }
4151         }
4152
4153         delete pEnumerator;
4154
4155
4156         static int totalCount = 0;
4157
4158         if (depth == 0)
4159         {
4160                 totalCount = 0;
4161         }
4162
4163         if (printChildren)
4164         {
4165                 depth ++;
4166
4167                 int count = GetChildCount();
4168                 totalCount += count;
4169
4170                 for (int i = count - 1; i >= 0; --i)
4171                 {
4172                         _Control* pChild = GetChild(i);
4173                         if (pChild)
4174                         {
4175                                 pChild->PrintDescription(printChildren, depth);
4176                         }
4177                 }
4178         }
4179
4180         return totalCount;
4181 }
4182
4183 _ITouchEventPreviewer*
4184 _Control::GetEventPreviewer(_IntToType<_UI_EVENT_TOUCH>) const
4185 {
4186         return __pTouchEventPreviewer;
4187 }
4188
4189 _IKeyEventPreviewer*
4190 _Control::GetEventPreviewer(_IntToType<_UI_EVENT_KEY>) const
4191 {
4192         return __pKeyEventPreviewer;
4193 }
4194
4195 _INotificationEventPreviewer*
4196 _Control::GetEventPreviewer(_IntToType<_UI_EVENT_NOTIFICAITON>) const
4197 {
4198         return __pNotificationEventPreviewer;
4199 }
4200
4201 _IKeyEventListener*
4202 _Control::GetEventListener(_IntToType<_UI_EVENT_KEY>) const
4203 {
4204         return __pKeyEventListener;
4205 }
4206
4207 _IFocusEventListener*
4208 _Control::GetEventListener(_IntToType<_UI_EVENT_FOCUS>) const
4209 {
4210         return __pFocusEventListener;
4211 }
4212
4213 _INotificationEventListener*
4214 _Control::GetEventListener(_IntToType<_UI_EVENT_NOTIFICAITON>) const
4215 {
4216         return __pNotificationEventListener;
4217 }
4218
4219 void
4220 _Control::SetEventPreviewer(_IntToType<_UI_EVENT_TOUCH>, _ITouchEventPreviewer* pPreviewer)
4221 {
4222         __pTouchEventPreviewer = pPreviewer;
4223 }
4224
4225 void
4226 _Control::SetEventPreviewer(_IntToType<_UI_EVENT_KEY>, _IKeyEventPreviewer* pPreviewer)
4227 {
4228         __pKeyEventPreviewer = pPreviewer;
4229 }
4230
4231 void
4232 _Control::SetEventPreviewer(_IntToType<_UI_EVENT_NOTIFICAITON>, _INotificationEventPreviewer* pPreviewer)
4233 {
4234         __pNotificationEventPreviewer = pPreviewer;
4235 }
4236
4237 void
4238 _Control::SetEventListener(_IntToType<_UI_EVENT_FOCUS>, _IFocusEventListener* pListener)
4239 {
4240         __pFocusEventListener = pListener;
4241 }
4242
4243 void
4244 _Control::SetEventListener(_IntToType<_UI_EVENT_NOTIFICAITON>, _INotificationEventListener* pListener)
4245 {
4246         __pNotificationEventListener = pListener;
4247 }
4248
4249 bool
4250 _Control::IsDragEnabled(void) const
4251 {
4252         return __dragEnabled;
4253 }
4254
4255 bool
4256 _Control::IsDropEnabled(void) const
4257 {
4258         return __dropEnabled;
4259 }
4260
4261 void
4262 _Control::SetDragEnabled(bool enabled)
4263 {
4264         __dragEnabled = enabled;
4265 }
4266
4267 void
4268 _Control::SetDropEnabled(bool enabled)
4269 {
4270         __dropEnabled = enabled;
4271 }
4272
4273 void
4274 _Control::SetTouchMoveAllowance(_TouchMoveAllowance touchMoveAllowanceType)
4275 {
4276         __touchMoveAllowance = touchMoveAllowanceType;
4277 }
4278
4279 _TouchMoveAllowance
4280 _Control::GetTouchMoveAllowance(void) const
4281 {
4282         return __touchMoveAllowance;
4283 }
4284
4285 void
4286 _Control::SetChangingEventTarget(bool isChangingEventTarget)
4287 {
4288         __isChangingEventTarget = isChangingEventTarget;
4289 }
4290
4291 bool
4292 _Control::GetChangingEventTarget(void) const
4293 {
4294         return __isChangingEventTarget;
4295 }
4296
4297 void
4298 _Control::SetEventReceivable(bool receivable)
4299 {
4300         __isEventReceivable = receivable;
4301 }
4302
4303 bool
4304 _Control::IsEventReceivable(void) const
4305 {
4306         return __isEventReceivable;
4307 }
4308
4309 }} // Tizen::Ui