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