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