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