Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ScrollPresenter.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUiCtrl_ScrollPresenter.cpp
20  * @brief               This is the implementation file for the %_ScrollPresenter class.
21  */
22
23 #include <FBaseErrors.h>
24 #include <FBaseSysLog.h>
25 #include <FGrpColor.h>
26 #include <FGrpRectangle.h>
27 #include <FGrpCanvas.h>
28 #include <FGrp_BitmapImpl.h>
29 #include <FUiAnimVisualElementAnimation.h>
30 #include <FUiAnimVisualElementAnimationProvider.h>
31 #include "FUi_ResourceManager.h"
32 #include "FUi_UiTouchEvent.h"
33 #include "FUiAnim_TransformMatrix3Df.h"
34 #include "FUiAnim_MatrixUtil.h"
35 #include "FUiAnim_VisualElement.h"
36 #include "FUiCtrl_ScrollPresenter.h"
37 #include "FUiCtrl_UiScrollEvent.h"
38 #include "FUiCtrl_UiScrollEventArg.h"
39
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Ui;
44 using namespace Tizen::Ui::Animations;
45
46 namespace
47 {
48 const float SCROLL_OPACITY_ON = 1.0f;
49 const float SCROLL_OPACITY_DARK_DIM =0.6f;
50 const float SCROLL_OPACITY_LIGHT_DIM = 0.3f;
51 const float SCROLL_OPACITY_OFF = 0.0f;
52 const float SCROLLING_EFFECT_MAX_POSITION = 100.0f;
53 const float SCROLLING_EFFECT_MIN_SCALE_RATE = 0.95f;
54 }
55
56 namespace Tizen { namespace Ui { namespace Controls
57 {
58
59 #ifndef MAX
60 #define MAX(a, b) ((a) > (b) ? (a) : (b))
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 #endif
63
64 _ScrollPresenter::_ScrollPresenter(void)
65         : __pParentCtrl(null)
66         , __pScrollCtrl(null)
67         , __scrollDirection(SCROLL_DIRECTION_VERTICAL)
68         , __enableFadeEffect(false)
69         , __enableJumpToTop(false)
70         , __enableHandler(false)
71         , __enableScrollingEffect(false)
72         , __enableOverScrollingEffect(false)
73         , __parentUsingViewport(true)
74         , __positionMin(0)
75         , __positionMax(0)
76         , __viewRange(0)
77         , __scrollRange(0)
78         , __scrollPositionCurr(0)
79         , __scrollPositionPrev(0)
80         , __fadeIn(false)
81         , __fadeOut(false)
82         , __scrollVisibility(false)
83         , __scrollingEffectVisibility(false)
84         , __parentBouncingRate(0.0f)
85         , __fadeEffectDuration_ms(5000)
86         , __needUpdateThumb(false)
87         , __needUpdateJumpTopTop(false)
88         , __needUpdateHandler(false)
89         , __needUpdateScrollingEffect(false)
90         , __handlerTouched(false)
91         , __handlerTouchedPosition(0)
92         , __jumpToTopPressed(false)
93         , __pScrollEvent(null)
94         , __pCtrlVe(null)
95         , __pThumbVe(null)
96         , __pJumpToTopVe(null)
97         , __pHandlerVe(null)
98         , __pFrontScrollingEffectVe(null)
99         , __pReerScrollingEffectVe(null)
100         , __thumbColor(0x00000000)
101         , __jumpToTopBgColor(0x00000000)
102         , __jumpToTopColor(0x00000000)
103         , __jumpToTopColorPressed(0x00000000)
104         , __buttonColor(0x00000000)
105         , __buttonColorPressed(0x00000000)
106         , __pThumbBitmap(null)
107         , __pThumbEffectBitmap(null)
108         , __pJumpToTopBitmap(null)
109         , __pJumpToTopEfBitmap(null)
110         , __pJumpToTopLeftBitmap(null)
111         , __pJumpToTopLeftEfBitmap(null)
112         , __pButtonBitmap(null)
113         , __pButtonEfBitmap(null)
114         , __pButtonPressBitmap(null)
115         , __pHandlerBgBitmap(null)
116         , __pHandlerBgEffectBitmap(null)
117         , __pHandlerBitmap(null)
118         , __pFrontScrollingEffectBitmap(null)
119         , __pReerScrollingEffectBitmap(null)
120         , __thumbSizeMin(0, 0)
121         , __thumbMargin(0)
122         , __jumpToTopSize(0, 0)
123         , __jumpToTopMarginRight(0)
124         , __jumpToTopMarginTop(0)
125         , __handlerSizeMin(0, 0)
126         , __handlerMarginLeft(0)
127         , __handlerMarginRight(0)
128         , __handlerMarginTop(0)
129         , __handlerMarginBottom(0)
130         , __scrollingEffectWidthLeft(0)
131         , __scrollingEffectWidthRight(0)
132         , __scrollingEffectHeightTop(0)
133         , __scrollingEffectHeightBottom(0)
134         , __beforeBouncingMatrixHash()
135 {
136 }
137
138 _ScrollPresenter::~_ScrollPresenter(void)
139 {
140         delete __pScrollEvent;
141         __pScrollEvent = null;
142
143         __beforeBouncingMatrixHash.RemoveAll(true);
144
145         if (__pThumbVe != null)
146         {
147                 __pThumbVe->Destroy();
148                 __pThumbVe = null;
149         }
150
151         if (__pJumpToTopVe != null)
152         {
153                 __pJumpToTopVe->Destroy();
154                 __pJumpToTopVe = null;
155         }
156
157         if (__pHandlerVe != null)
158         {
159                 __pHandlerVe->Destroy();
160                 __pHandlerVe = null;
161         }
162
163         if (__pFrontScrollingEffectVe != null)
164         {
165                 __pFrontScrollingEffectVe->RemoveAllAnimations();
166                 __pFrontScrollingEffectVe->Destroy();
167                 __pFrontScrollingEffectVe = null;
168         }
169
170         if (__pReerScrollingEffectVe != null)
171         {
172                 __pReerScrollingEffectVe->RemoveAllAnimations();
173                 __pReerScrollingEffectVe->Destroy();
174                 __pReerScrollingEffectVe = null;
175         }
176
177         if (__pCtrlVe != null)
178         {
179                 __pCtrlVe->RemoveAllAnimations();
180                 __pCtrlVe->SetAnimationProvider(null);
181                 __pCtrlVe->Destroy();
182                 __pCtrlVe = null;
183         }
184
185         ReleaseBitmapResources();
186 }
187
188 _ScrollPresenter*
189 _ScrollPresenter::CreateScrollPresenterN(_Control& parentCtrl,
190                                                                                                         _Scroll& scrollCtrl,
191                                                                                                         _ScrollDirection scrollDirection,
192                                                                                                         bool enableFadeEffect,
193                                                                                                         bool enableJumpToTop,
194                                                                                                         bool enableHandler,
195                                                                                                         bool visibility,
196                                                                                                         int viewRange,
197                                                                                                         int scrollRange,
198                                                                                                         int scrollPosition)
199 {
200         SysTryReturn(NID_UI_CTRL, ((scrollRange >= viewRange) && (scrollRange != 0)), null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid arguments");
201         SysTryReturn(NID_UI_CTRL, scrollPosition >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid arguments");
202
203         _ScrollPresenter* pPresenter = new (std::nothrow) _ScrollPresenter();
204         SysTryReturn(NID_UI_CTRL, pPresenter, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
205
206         result r = pPresenter->Construct(parentCtrl,
207                                                                                 scrollCtrl,
208                                                                                 scrollDirection,
209                                                                                 enableFadeEffect,
210                                                                                 enableJumpToTop,
211                                                                                 enableHandler,
212                                                                                 visibility,
213                                                                                 viewRange,
214                                                                                 scrollRange,
215                                                                                 scrollPosition);
216
217         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
218
219         SetLastResult(E_SUCCESS);
220         return pPresenter;
221
222 CATCH:
223         delete pPresenter;
224
225         return null;
226 }
227
228 result
229 _ScrollPresenter::SetScrollDirection(_ScrollDirection scrollDirection)
230 {
231         result r = E_SUCCESS;
232
233         // load color/shape configuration and bitmap resources
234         r = LoadResources(scrollDirection, _CONTROL_ORIENTATION_PORTRAIT);
235         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to load resources.");
236
237         // reset attributes
238         __scrollDirection = scrollDirection;
239
240         // reset the bounds of scroll control
241         Rectangle bounds = GetParentCtrl().GetBounds();
242         bounds.SetPosition(0, 0);
243         r = GetControl().SetBounds(bounds);
244         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set the bounds of _Scroll.");
245
246         // relayout scroll children.
247         r = RelayoutScrollChildren(__scrollDirection);
248         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to relayout scroll chilren");
249
250         return E_SUCCESS;
251 }
252
253 _ScrollDirection
254 _ScrollPresenter::GetScrollDirection(void) const
255 {
256         return __scrollDirection;
257 }
258
259 result
260 _ScrollPresenter::SetScrollRange(int viewRange, int scrollRange)
261 {
262         SysTryReturn(NID_UI_CTRL, ((scrollRange >= viewRange) && (scrollRange != 0)), E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid arguments");
263
264         result r = E_SUCCESS;
265
266         // reset scroll range.
267         __viewRange = viewRange;
268         __scrollRange = scrollRange;
269
270         // relayout scroll children.
271         r = RelayoutScrollChildren(__scrollDirection);
272         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to relayout scroll chilren");
273
274         return E_SUCCESS;
275
276 }
277
278 void
279 _ScrollPresenter::GetScrollRange(int* pViewRange, int* pScrollRange) const
280 {
281         SysTryReturnVoidResult(NID_UI_CTRL,
282                                         !((pViewRange == null) && (pScrollRange == null)),
283                                         E_INVALID_ARG,"[E_INVALID_ARG] Input parameter is invalid.");
284
285         *pViewRange = __viewRange;
286         *pScrollRange = __scrollRange;
287
288         return;
289 }
290
291 result
292 _ScrollPresenter::SetScrollPosition(int scrollPosition)
293 {
294         result r = E_SUCCESS;
295
296         if (scrollPosition != __scrollPositionCurr)
297         {
298
299                 if (!__enableScrollingEffect)
300                 {
301                         if (scrollPosition < 0)
302                         {
303                                 scrollPosition = 0;
304                         }
305                         else if (scrollPosition > (__scrollRange - __viewRange))
306                         {
307                                 scrollPosition = (__scrollRange -__viewRange);
308                         }
309                 }
310                 __scrollPositionPrev = __scrollPositionCurr;
311                 __scrollPositionCurr = scrollPosition;
312
313                 if (__enableJumpToTop)
314                 {
315                         if ((__scrollPositionCurr <= 0) && (__pJumpToTopVe->GetOpacity() > 0.0f))
316                         {
317                                 __pCtrlVe->SetImplicitAnimationEnabled(true);
318                                 __pJumpToTopVe->SetImplicitAnimationEnabled(true);
319
320                                 __pJumpToTopVe->SetOpacity(SCROLL_OPACITY_OFF);
321
322                                 __pJumpToTopVe->SetImplicitAnimationEnabled(false);
323                                 __pCtrlVe->SetImplicitAnimationEnabled(false);
324                         }
325                         else if ((__scrollPositionCurr > 0) && (__pJumpToTopVe->GetOpacity() < 1.0f))
326                         {
327                                 __pCtrlVe->SetImplicitAnimationEnabled(true);
328                                 __pJumpToTopVe->SetImplicitAnimationEnabled(true);
329
330                                 __pJumpToTopVe->SetOpacity(SCROLL_OPACITY_ON);
331
332                                 __pJumpToTopVe->SetImplicitAnimationEnabled(false);
333                                 __pCtrlVe->SetImplicitAnimationEnabled(false);
334                         }
335                         // else keep up current visibility.
336
337                 }
338
339                 if (__enableHandler)
340                 {
341                         Point handlerPosition = CalculateHandlerPosition(__scrollDirection);
342                         Rectangle handlerBounds(handlerPosition.x, handlerPosition.y, __pHandlerVe->GetBounds().width, __pHandlerVe->GetBounds().height);
343                         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
344                         if (__scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
345                         {
346                                 if (handlerPosition.x < 0)
347                                 {
348                                         handlerBounds.x = 0;
349                                 }
350                                 else if (handlerPosition.x > scrollCtrlBounds.width - handlerBounds.width)
351                                 {
352                                         handlerBounds.x =scrollCtrlBounds.width - handlerBounds.width;
353                                 }
354                         }
355                         else
356                         {
357                                 if (handlerPosition.y < 0)
358                                 {
359                                         handlerBounds.y = 0;
360                                 }
361                                 else if (handlerPosition.y > scrollCtrlBounds.height - handlerBounds.height)
362                                 {
363                                         handlerBounds.y = scrollCtrlBounds.height - handlerBounds.height;
364                                 }
365                         }
366
367                         r = SetScrollChildBounds(*__pHandlerVe, handlerBounds);
368                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set the bounds of the __pHandlerVe");
369                 }
370                 else
371                 {
372                         Point thumbPosition = CalculateThumbPosition(__scrollDirection);
373                         Rectangle thumbBounds(thumbPosition.x, thumbPosition.y, __pThumbVe->GetBounds().width, __pThumbVe->GetBounds().height);
374                         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
375                         if (__scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
376                         {
377                                 if (thumbPosition.x < 0)
378                                 {
379                                         thumbBounds.x = 0;
380                                 }
381                                 else if (thumbPosition.x > scrollCtrlBounds.width - thumbBounds.width)
382                                 {
383                                         thumbBounds.x = scrollCtrlBounds.width - thumbBounds.width;
384                                 }
385                         }
386                         else
387                         {
388                                 if (thumbPosition.y < 0)
389                                 {
390                                         thumbBounds.y = 0;
391                                 }
392                                 else if (thumbPosition.y > scrollCtrlBounds.height - thumbBounds.height)
393                                 {
394                                         thumbBounds.y = scrollCtrlBounds.height - thumbBounds.height;
395                                 }
396                         }
397
398                         r = SetScrollChildBounds(*__pThumbVe, thumbBounds);
399                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set the bounds of the __pThumbVe");
400                 }
401
402                 if (__enableScrollingEffect || __enableOverScrollingEffect)
403                 {
404                         float ratio = 0.0f;
405                         if (__scrollPositionCurr < 0)
406                         {
407                                 ratio = -__scrollPositionCurr / SCROLLING_EFFECT_MAX_POSITION;
408                                 ratio = ratio > 1.0f ? 1.0f : ratio;
409
410                                 if (__enableScrollingEffect)
411                                 {
412                                         SetScrollingEffectOpacity(*__pFrontScrollingEffectVe, true, ratio);
413                                         SetScrollingEffectOpacity(*__pReerScrollingEffectVe, false, ratio);
414                                 }
415
416                                 __scrollingEffectVisibility = true;
417                         }
418                         else if (__scrollPositionCurr > (__scrollRange - __viewRange))
419                         {
420                                 ratio = (__scrollPositionCurr - (__scrollRange - __viewRange)) / SCROLLING_EFFECT_MAX_POSITION;
421                                 ratio = ratio > 1.0f ? 1.0f : ratio;
422
423                                 if (__enableScrollingEffect)
424                                 {
425                                         SetScrollingEffectOpacity(*__pFrontScrollingEffectVe, false, ratio);
426                                         SetScrollingEffectOpacity(*__pReerScrollingEffectVe, true, ratio);
427                                 }
428
429                                 __scrollingEffectVisibility = true;
430                         }
431
432                         if (__enableOverScrollingEffect)
433                         {
434                                 r = SetParentBouncing(ratio);
435                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
436                         }
437                 }
438         }
439
440         return r;
441 }
442
443 int
444 _ScrollPresenter::GetScrollPosition(void) const
445 {
446         return __scrollPositionCurr;
447 }
448
449 void
450 _ScrollPresenter::EnableFadeEffect(bool enable)
451 {
452         __enableFadeEffect = enable;
453
454         return;
455 }
456
457 bool
458 _ScrollPresenter::IsEnabledFadeEffect(void) const
459 {
460         return __enableFadeEffect;
461 }
462
463 bool
464 _ScrollPresenter::IsOnFadeEffect(void) const
465 {
466         return (__fadeIn || __fadeOut);
467 }
468
469 void
470 _ScrollPresenter::EnableScrollingEffect(bool enable)
471 {
472         __enableScrollingEffect = enable;
473 }
474
475 bool
476 _ScrollPresenter::IsEnabledScrollingEffect(void) const
477 {
478         return __enableScrollingEffect;
479 }
480
481 void
482 _ScrollPresenter::EnableOverScrollingEffect(bool enable)
483 {
484         __enableOverScrollingEffect = enable;
485 }
486
487 bool
488 _ScrollPresenter::IsEnabledOverScrollingEffect(void) const
489 {
490         return __enableOverScrollingEffect;
491 }
492
493 void
494 _ScrollPresenter::SetParentUsingViewport(bool useViewPort)
495 {
496         __parentUsingViewport = useViewPort;
497 }
498
499 bool
500 _ScrollPresenter::IsParentUsingViewport(void) const
501 {
502         return __parentUsingViewport;
503 }
504
505 result
506 _ScrollPresenter::SetScrollVisibility(bool visibility)
507 {
508         if (GetScrollVisibility() == visibility)
509         {
510                 // keep up current visibility.
511                 return E_SUCCESS;
512         }
513
514         __pCtrlVe->RemoveAllAnimations();
515         if (visibility)
516         {
517                 if (__enableFadeEffect)
518                 {
519                         __fadeIn = true;
520                         __fadeOut = false;
521
522                         __pCtrlVe->SetImplicitAnimationEnabled(true);
523                 }
524
525                 __pCtrlVe->SetOpacity(SCROLL_OPACITY_ON);
526
527                 if (__enableFadeEffect)
528                 {
529                         __pCtrlVe->SetImplicitAnimationEnabled(false);
530                 }
531                 else
532                 {
533                         __fadeIn = false;
534                         __fadeOut = false;
535                 }
536         }
537         else
538         {
539                 if (__enableFadeEffect)
540                 {
541                         __fadeIn = false;
542                         __fadeOut = true;
543
544                         __pCtrlVe->SetImplicitAnimationEnabled(true);
545                 }
546
547                 __pCtrlVe->SetOpacity(SCROLL_OPACITY_OFF);
548
549                 if (__enableFadeEffect)
550                 {
551                         __pCtrlVe->SetImplicitAnimationEnabled(false);
552                 }
553                 else
554                 {
555                         __fadeIn = false;
556                         __fadeOut = false;
557                 }
558         }
559
560         __scrollVisibility = visibility;
561
562         return E_SUCCESS;
563 }
564
565 bool
566 _ScrollPresenter::GetScrollVisibility(void) const
567 {
568         if (__enableFadeEffect)
569         {
570                 if (__scrollVisibility)
571                 {
572                         return true;
573                 }
574         }
575         else if (__pCtrlVe->GetOpacity() > SCROLL_OPACITY_OFF)
576         {
577                 return true;
578         }
579
580         return false;
581 }
582
583 void
584 _ScrollPresenter::CancelFadeEffect(void)
585 {
586         if (__fadeIn)
587         {
588                 __fadeIn = false;
589                 __scrollVisibility = false;
590                 __pCtrlVe->SetOpacity(SCROLL_OPACITY_OFF);
591         }
592         else if (__fadeOut)
593         {
594                 __fadeOut = false;
595                 __scrollVisibility = true;
596                 __pCtrlVe->SetOpacity(SCROLL_OPACITY_ON);
597         }
598
599         return;
600 }
601
602 result
603 _ScrollPresenter::EnableJumpToTop(bool enable)
604 {
605         result r = E_SUCCESS;
606
607         if (__enableJumpToTop != enable)
608         {
609                 if (enable)
610                 {
611                         if (__enableFadeEffect)
612                         {
613                                 __pCtrlVe->SetImplicitAnimationEnabled(true);
614                         }
615
616                         r = AttachScrollChild(&__pJumpToTopVe, CalculateJumpToTopBounds(__scrollDirection), true);
617                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to attach the __pJumpToTopVe to _Scroll");
618
619                         if (__scrollPositionCurr != 0)
620                         {
621                                 __pJumpToTopVe->SetOpacity(SCROLL_OPACITY_ON);
622                         }
623                         else
624                         {
625                                 __pJumpToTopVe->SetOpacity(SCROLL_OPACITY_OFF);
626                         }
627
628                         if (__enableFadeEffect)
629                         {
630                                 __pCtrlVe->SetImplicitAnimationEnabled(false);
631                         }
632                 }
633                 else
634                 {
635                         DetachScrollChild(&__pJumpToTopVe, true);
636                 }
637
638                 __enableJumpToTop = enable;
639         }
640
641         return E_SUCCESS;
642
643 }
644
645 bool
646 _ScrollPresenter::IsEnabledJumpToTop(void) const
647 {
648         return __enableJumpToTop;
649 }
650
651 result
652 _ScrollPresenter::EnableHandler(bool enable)
653 {
654         result r = E_SUCCESS;
655
656         if (__enableHandler != enable)
657         {
658                 if (__enableFadeEffect)
659                 {
660                         __pCtrlVe->SetImplicitAnimationEnabled(true);
661                 }
662
663                 if (enable)
664                 {
665                         r = AttachScrollChild(&__pHandlerVe, CalculateHandlerBounds(__scrollDirection), true);
666                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to attach the __pHandlerVe to _Scroll");
667
668                         DetachScrollChild(&__pThumbVe, true);
669                 }
670                 else
671                 {
672                         r = AttachScrollChild(&__pThumbVe, CalculateThumbBounds(__scrollDirection), true);
673                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to attach the __pThumbVe to _Scroll");
674
675                         DetachScrollChild(&__pHandlerVe, true);
676                 }
677                 __enableHandler = enable;
678
679                 if (__enableFadeEffect)
680                 {
681                         __pCtrlVe->SetImplicitAnimationEnabled(false);
682                 }
683         }
684
685         return E_SUCCESS;
686 }
687
688 bool
689 _ScrollPresenter::IsEnabledHandler(void) const
690 {
691         return __enableHandler;
692 }
693
694 result
695 _ScrollPresenter::SetScrollingEffectVisibility(bool visibility)
696 {
697         if (!__enableScrollingEffect)
698         {
699                 visibility = false;
700         }
701
702         if (__scrollingEffectVisibility == visibility)
703         {
704                 // keep up current visibility.
705                 return E_SUCCESS;
706         }
707
708         if (__pFrontScrollingEffectVe == null || __pReerScrollingEffectVe == null)
709         {
710                 return E_SUCCESS;
711         }
712
713         __pFrontScrollingEffectVe->RemoveAllAnimations();
714         __pReerScrollingEffectVe->RemoveAllAnimations();
715
716         if (visibility)
717         {
718                 if (__enableFadeEffect)
719                 {
720                         __pFrontScrollingEffectVe->SetImplicitAnimationEnabled(true);
721                         __pReerScrollingEffectVe->SetImplicitAnimationEnabled(true);
722                 }
723
724                 SetScrollingEffectOpacity(SCROLL_OPACITY_DARK_DIM);
725
726                 if (__enableFadeEffect)
727                 {
728                         __pFrontScrollingEffectVe->SetImplicitAnimationEnabled(false);
729                         __pReerScrollingEffectVe->SetImplicitAnimationEnabled(false);
730                 }
731         }
732         else
733         {
734                 if (__enableFadeEffect)
735                 {
736                         __pFrontScrollingEffectVe->SetImplicitAnimationEnabled(true);
737                         __pReerScrollingEffectVe->SetImplicitAnimationEnabled(true);
738                 }
739
740                 SetScrollingEffectOpacity(SCROLL_OPACITY_OFF);
741
742                 if (__enableFadeEffect)
743                 {
744                         __pFrontScrollingEffectVe->SetImplicitAnimationEnabled(false);
745                         __pReerScrollingEffectVe->SetImplicitAnimationEnabled(false);
746                 }
747         }
748
749         __scrollingEffectVisibility = visibility;
750
751         return E_SUCCESS;
752 }
753
754 void
755 _ScrollPresenter::SetScrollingEffectOpacity(float opacity)
756 {
757         __pFrontScrollingEffectVe->SetOpacity(opacity);
758         __pReerScrollingEffectVe->SetOpacity(opacity);
759 }
760
761 void
762 _ScrollPresenter::SetScrollingEffectOpacity(_VisualElement& Ve, bool isActive, float ratio)
763 {
764         float baseOpacity = (isActive) ? SCROLL_OPACITY_ON : SCROLL_OPACITY_LIGHT_DIM;
765         Ve.SetOpacity(SCROLL_OPACITY_DARK_DIM + ((baseOpacity - SCROLL_OPACITY_DARK_DIM) * ratio));
766 }
767
768 bool
769 _ScrollPresenter::IsScrollVisible(void) const
770 {
771         return (__fadeIn || __fadeOut || __scrollVisibility);
772 }
773
774 void
775 _ScrollPresenter::AddScrollEventListener(const Controls::_IScrollEventListener& listener)
776 {
777         if (__pScrollEvent == null)
778         {
779                 __pScrollEvent = new (std::nothrow) _UiScrollEvent();
780                 SysTryReturnVoidResult(NID_UI_CTRL, __pScrollEvent, E_OUT_OF_MEMORY,
781                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
782                 __pScrollEvent->Construct(GetControl());
783         }
784
785         __pScrollEvent->AddListener(listener);
786
787         return;
788 }
789
790 void
791 _ScrollPresenter::RemoveScrollEventListener(const Controls::_IScrollEventListener& listener)
792 {
793         ClearLastResult();
794         SysTryReturnVoidResult(NID_UI_CTRL, __pScrollEvent, E_SYSTEM, "[E_SYSTEM] __pScrollEvent must not be null.");
795
796         __pScrollEvent->RemoveListener(listener);
797
798         return;
799 }
800
801 void
802 _ScrollPresenter::OnParentBoundsChanged(void)
803 {
804         // reset the bounds of scroll control
805         Rectangle bounds = GetParentCtrl().GetBounds();
806         bounds.SetPosition(0, 0);
807         GetControl().SetBounds(bounds);
808
809         // relayout scroll children.
810         RelayoutScrollChildren(__scrollDirection);
811
812         return;
813 }
814
815 void
816 _ScrollPresenter::OnDraw(void)
817 {
818         DrawThumb();
819         DrawJumpToTop();
820         DrawHandler();
821         DrawScrollingEffect();
822
823         return;
824 }
825
826 bool
827 _ScrollPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
828 {
829         bool ret = false;
830
831         ret |= OnTouchPressedHandler(source, touchinfo);
832         ret |= OnTouchPressedJumpToTop(source, touchinfo);
833
834         return ret;
835 }
836
837 bool
838 _ScrollPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
839 {
840         bool ret = false;
841
842         ret |= OnTouchReleasedHandler(source, touchinfo);
843         ret |= OnTouchReleasedJumpToTop(source, touchinfo);
844
845         return ret;
846 }
847
848 bool
849 _ScrollPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
850 {
851         bool ret = false;
852
853         ret |= OnTouchMovedHandler(source, touchinfo);
854         ret |= OnTouchMovedJumpToTop(source, touchinfo);
855
856         return ret;
857 }
858
859 HitTestResult
860 _ScrollPresenter::HitTest(const FloatPoint& point)
861 {
862         FloatRectangle ctrlBounds = __pCtrlVe->GetBounds();
863
864         if (__enableJumpToTop && IsScrollVisible())
865         {
866                 Rectangle jumpToTopBounds = GetScrollChildBounds(*__pJumpToTopVe);
867                 jumpToTopBounds.x += ctrlBounds.x;
868                 jumpToTopBounds.y += ctrlBounds.y;
869                 if (jumpToTopBounds.Contains(Point(point.x, point.y)))
870                 {
871                         return HIT_TEST_MATCH;
872                 }
873         }
874
875         if (__enableHandler)
876         {
877                 Rectangle handlerBounds = GetScrollChildBounds(*__pHandlerVe);
878                 handlerBounds.x += ctrlBounds.x;
879                 handlerBounds.y += ctrlBounds.y;
880                 if (handlerBounds.Contains(Point(point.x, point.y)))
881                 {
882                         return HIT_TEST_MATCH;
883                 }
884         }
885
886         return HIT_TEST_NOWHERE;
887 }
888
889 VisualElementAnimation*
890 _ScrollPresenter::CreateAnimationForProperty(VisualElement& target, const String& property)
891 {
892         VisualElementAnimation* pAnimation = VisualElementAnimationProvider::CreateAnimationForProperty(target, property);
893         SysTryReturn(NID_UI_CTRL, pAnimation, null, GetLastResult(), "[%s] Failed to create a VisualElementAnimation.", GetErrorMessage(GetLastResult()));
894
895         pAnimation->SetVisualElementAnimationStatusEventListener(this);
896         SetLastResult(E_SUCCESS);
897
898         return pAnimation;
899 }
900
901 void
902 _ScrollPresenter::OnVisualElementAnimationStarted(const VisualElementAnimation& animation, const String& keyName, VisualElement& target)
903 {
904         return;
905 }
906
907 void
908 _ScrollPresenter::OnVisualElementAnimationRepeated(const VisualElementAnimation& animation, const String& keyName, VisualElement& target, long currentRepeatCount)
909 {
910         return;
911 }
912
913 void
914 _ScrollPresenter::OnVisualElementAnimationFinished(const VisualElementAnimation& animation, const String& keyName, VisualElement& target, bool completedNormally)
915 {
916         if (__pCtrlVe == (static_cast <_VisualElement*>(&target)))
917         {
918                 __fadeIn = false;
919                 __fadeOut = false;
920         }
921
922         return;
923 }
924
925 result
926 _ScrollPresenter::Construct(_Control& parentCtrl,
927                                                                 _Scroll& scrollCtrl,
928                                                                 _ScrollDirection scrollDirection,
929                                                                 bool enableFadeEffect,
930                                                                 bool enableJumpToTop,
931                                                                 bool enableHandler,
932                                                                 bool visibility,
933                                                                 int viewRange,
934                                                                 int scrollRange,
935                                                                 int scrollPosition)
936 {
937         result r = E_SUCCESS;
938
939         // load color/shape configuration and bitmap resources
940         r = LoadResources(scrollDirection, _CONTROL_ORIENTATION_PORTRAIT);
941         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to load resources.");
942
943         // set attributes
944         SetParentCtrl(parentCtrl);
945         SetControl(scrollCtrl);
946         __scrollDirection = scrollDirection;
947         __enableFadeEffect = enableFadeEffect;
948         __enableJumpToTop = enableJumpToTop;
949         __enableHandler = enableHandler;
950         __viewRange = viewRange;
951         __scrollRange = scrollRange;
952         __scrollPositionCurr = scrollPosition;
953
954         _VisualElement* pVe = null;
955
956         // initialize the scroll control
957         Rectangle bounds = GetParentCtrl().GetBounds();
958         bounds.SetPosition(0, 0);
959         GetControl().SetBounds(bounds);
960         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set the bounds of _Scroll.", GetErrorMessage(r));
961
962         scrollCtrl.SetBackgroundColor(Color(0, 0, 0, 0));
963
964         // initialize visual elements
965         pVe = scrollCtrl.GetVisualElement();
966         SysTryCatch(NID_UI_CTRL, pVe, , GetLastResult(), "[%s] Failed to get VisualElement of the _Scroll.", GetErrorMessage(GetLastResult()));
967
968         __pCtrlVe = new (std::nothrow) _VisualElement();
969         SysTryReturnResult(NID_UI_CTRL, __pCtrlVe != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
970
971         r = __pCtrlVe->Construct();
972         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct Visual Element.", GetErrorMessage(r));
973
974         r = __pCtrlVe->SetSurfaceOpaque(false);
975         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set surface opaque.", GetErrorMessage(r));
976
977         __pCtrlVe->SetImplicitAnimationEnabled(false);
978
979         r = pVe->AttachChild(*__pCtrlVe);
980         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach child to the control VisualElement.", GetErrorMessage(r));
981
982         r = SetScrollChildBounds(*__pCtrlVe, CalculateScrollCtrlBounds(scrollDirection));
983         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
984
985         __pCtrlVe->SetAnimationProvider(this);
986         __pCtrlVe->SetShowState(true);
987
988         if (visibility)
989         {
990                 __pCtrlVe->SetOpacity(SCROLL_OPACITY_ON);
991         }
992         else
993         {
994                 __pCtrlVe->SetOpacity(SCROLL_OPACITY_OFF);
995         }
996
997         // initialize jumpToTopVe
998         if (enableJumpToTop)
999         {
1000                 r = AttachScrollChild(&__pJumpToTopVe, CalculateJumpToTopBounds(scrollDirection), true);
1001                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach the __pJumpToTopVe to _Scroll.", GetErrorMessage(r));
1002
1003                 if ((scrollDirection != SCROLL_DIRECTION_VERTICAL) || (scrollPosition == 0))
1004                 {
1005                         __pJumpToTopVe->SetOpacity(SCROLL_OPACITY_OFF);
1006                 }
1007
1008                 __needUpdateJumpTopTop = true;
1009         }
1010
1011         // initialize thumbVe and handlerVe
1012         if (enableHandler)
1013         {
1014                 r = AttachScrollChild(&__pHandlerVe, CalculateHandlerBounds(scrollDirection), true);
1015                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach the __pHandlerVe to _Scroll.", GetErrorMessage(r));
1016
1017                 DetachScrollChild(&__pThumbVe, true);
1018
1019                 __needUpdateHandler = true;
1020         }
1021         else
1022         {
1023                 r = AttachScrollChild(&__pThumbVe, CalculateThumbBounds(scrollDirection), true);
1024                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach the __pThumbVe to _Scroll.", GetErrorMessage(r));
1025
1026                 DetachScrollChild(&__pHandlerVe, true);
1027
1028                 __needUpdateThumb = true;
1029         }
1030
1031         // initialize scrolling effect front VE
1032         __pFrontScrollingEffectVe = new (std::nothrow) _VisualElement();
1033         SysTryCatch(NID_UI_CTRL, __pFrontScrollingEffectVe, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1034
1035         r = __pFrontScrollingEffectVe->Construct();
1036         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct pFrontScrollingEffectVe.", GetErrorMessage(r));
1037
1038         r = __pFrontScrollingEffectVe->SetSurfaceOpaque(false);
1039         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set surface opaque.", GetErrorMessage(r));
1040
1041         __pFrontScrollingEffectVe->SetImplicitAnimationEnabled(false);
1042
1043         r = pVe->AttachChild(*__pFrontScrollingEffectVe);
1044         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach child to the control VisualElement.", GetErrorMessage(r));
1045
1046         r = SetScrollChildBounds(*__pFrontScrollingEffectVe, CalculateScrollingEffectFrontBounds(scrollDirection));
1047         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1048
1049         __pFrontScrollingEffectVe->SetShowState(true);
1050         __pFrontScrollingEffectVe->SetOpacity(SCROLL_OPACITY_OFF);
1051
1052         // initialize scrolling effect reer VE
1053         __pReerScrollingEffectVe = new (std::nothrow) _VisualElement();
1054         SysTryCatch(NID_UI_CTRL, __pReerScrollingEffectVe, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1055
1056         r = __pReerScrollingEffectVe->Construct();
1057         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct pReerScrollingEffectVe.", GetErrorMessage(r));
1058
1059         r = __pReerScrollingEffectVe->SetSurfaceOpaque(false);
1060         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set surface opaque.", GetErrorMessage(r));
1061
1062         __pReerScrollingEffectVe->SetImplicitAnimationEnabled(false);
1063
1064         r = pVe->AttachChild(*__pReerScrollingEffectVe);
1065         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach child to the control VisualElement.", GetErrorMessage(r));
1066
1067         r = SetScrollChildBounds(*__pReerScrollingEffectVe, CalculateScrollCtrlBounds(scrollDirection));
1068         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1069
1070         __pReerScrollingEffectVe->SetShowState(true);
1071         __pReerScrollingEffectVe->SetOpacity(SCROLL_OPACITY_OFF);
1072
1073         __needUpdateScrollingEffect = true;
1074
1075         __scrollVisibility = visibility;
1076
1077         r = __beforeBouncingMatrixHash.Construct();
1078         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1079
1080         return E_SUCCESS;
1081
1082 CATCH:
1083         // set attributes
1084         __pParentCtrl = null;
1085         __pScrollCtrl = null;
1086         __scrollDirection = SCROLL_DIRECTION_VERTICAL;
1087         __enableFadeEffect = false;
1088         __enableJumpToTop = false;
1089         __enableHandler = false;
1090         __enableScrollingEffect = false;
1091         __enableOverScrollingEffect = false;
1092
1093         __positionMin = 0;
1094         __positionMax = 0;
1095         __viewRange = 0;
1096         __scrollRange = 0;
1097
1098         __scrollPositionCurr = 0;
1099         __needUpdateThumb = false;
1100         __needUpdateJumpTopTop = false;
1101         __needUpdateHandler = false;
1102         __needUpdateScrollingEffect = false;
1103         __jumpToTopPressed = false;
1104
1105         __thumbColor = 0x00000000;
1106         __jumpToTopBgColor = 0x00000000;
1107         __jumpToTopColor = 0x00000000;
1108         __jumpToTopColorPressed = 0x00000000;
1109         __buttonColor= 0x00000000;
1110         __buttonColorPressed = 0x00000000;
1111
1112         ReleaseBitmapResources();
1113
1114         __thumbSizeMin.SetSize(0, 0);
1115         __thumbMargin = 0;
1116         __jumpToTopSize.SetSize(0, 0);
1117         __jumpToTopMarginRight = 0;
1118         __jumpToTopMarginTop = 0;
1119         __handlerSizeMin.SetSize(0, 0);
1120         __handlerMarginLeft = 0;
1121         __handlerMarginRight = 0;
1122         __handlerMarginTop = 0;
1123         __handlerMarginBottom = 0;
1124         __scrollingEffectWidthLeft = 0;
1125         __scrollingEffectWidthRight = 0;
1126         __scrollingEffectHeightTop = 0;
1127         __scrollingEffectHeightBottom = 0;
1128
1129         if (__pThumbVe != null)
1130         {
1131                 __pThumbVe->Destroy();
1132                 __pThumbVe = null;
1133         }
1134
1135         if (__pJumpToTopVe != null)
1136         {
1137                 __pJumpToTopVe->Destroy();
1138                 __pJumpToTopVe = null;
1139         }
1140
1141         if (__pHandlerVe != null)
1142         {
1143                 __pHandlerVe->Destroy();
1144                 __pHandlerVe = null;
1145         }
1146
1147         if (__pFrontScrollingEffectVe != null)
1148         {
1149                 __pFrontScrollingEffectVe->RemoveAllAnimations();
1150                 __pFrontScrollingEffectVe->Destroy();
1151                 __pFrontScrollingEffectVe = null;
1152         }
1153
1154         if (__pReerScrollingEffectVe != null)
1155         {
1156                 __pReerScrollingEffectVe->RemoveAllAnimations();
1157                 __pReerScrollingEffectVe->Destroy();
1158                 __pReerScrollingEffectVe = null;
1159         }
1160
1161         if (__pCtrlVe != null)
1162         {
1163                 __pCtrlVe->RemoveAllAnimations();
1164                 __pCtrlVe->SetAnimationProvider(null);
1165                 __pCtrlVe->Destroy();
1166                 __pCtrlVe = null;
1167         }
1168
1169         return r;
1170 }
1171
1172 result
1173 _ScrollPresenter::LoadResources(_ScrollDirection scrollDirection, _ControlOrientation orientation)
1174 {
1175         result r = E_SUCCESS;
1176
1177         r = LoadColorConfig();
1178         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get resource.", GetErrorMessage(r));
1179
1180         r = LoadBitmapResources(scrollDirection);
1181         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get resource.", GetErrorMessage(r));
1182
1183         r = LoadShapeConfig(scrollDirection, orientation);
1184         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get resource.", GetErrorMessage(r));
1185
1186         return E_SUCCESS;
1187
1188 CATCH:
1189         __thumbColor = 0x00000000;
1190         __jumpToTopBgColor = 0x00000000;
1191         __jumpToTopColor = 0x00000000;
1192         __jumpToTopColorPressed = 0x00000000;
1193         __buttonColor= 0x00000000;
1194         __buttonColorPressed = 0x00000000;
1195
1196         ReleaseBitmapResources();
1197
1198         __thumbSizeMin.SetSize(0, 0);
1199         __thumbMargin = 0;
1200         __jumpToTopSize.SetSize(0, 0);
1201         __jumpToTopMarginRight = 0;
1202         __jumpToTopMarginTop = 0;
1203         __handlerSizeMin.SetSize(0, 0);
1204         __handlerMarginLeft = 0;
1205         __handlerMarginRight = 0;
1206         __handlerMarginTop = 0;
1207         __handlerMarginBottom = 0;
1208         __scrollingEffectWidthLeft = 0;
1209         __scrollingEffectWidthRight = 0;
1210         __scrollingEffectHeightTop = 0;
1211         __scrollingEffectHeightBottom = 0;
1212
1213         return r;
1214
1215 }
1216
1217 result
1218 _ScrollPresenter::LoadColorConfig(void)
1219 {
1220         result r = E_SUCCESS;
1221
1222         // load color configuration
1223         r = GET_COLOR_CONFIG(SCROLL::THUMB, __thumbColor);
1224         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1225
1226         r = GET_COLOR_CONFIG(SCROLL::JUMP_TO_TOP_BG, __jumpToTopBgColor);
1227         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1228
1229         r = GET_COLOR_CONFIG(SCROLL::JUMP_TO_TOP_NORMAL, __jumpToTopColor);
1230         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1231
1232         r = GET_COLOR_CONFIG(SCROLL::JUMP_TO_TOP_PRESSED, __jumpToTopColorPressed);
1233         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1234
1235         r = GET_COLOR_CONFIG(SCROLL::BUTTON_BG, __buttonColor);
1236         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1237
1238         r = GET_COLOR_CONFIG(SCROLL::BUTTON_BG_PRESSED, __buttonColorPressed);
1239         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1240
1241         return E_SUCCESS;
1242
1243 CATCH:
1244         __thumbColor = 0x00000000;
1245         __jumpToTopBgColor = 0x00000000;
1246         __jumpToTopColor = 0x00000000;
1247         __jumpToTopColorPressed = 0x00000000;
1248         __buttonColor= 0x00000000;
1249         __buttonColorPressed = 0x00000000;
1250
1251         return E_SYSTEM;
1252
1253 }
1254
1255 result
1256 _ScrollPresenter::LoadBitmapResources(_ScrollDirection scrollDirection)
1257 {
1258         result r = E_SUCCESS;
1259
1260         // release allocated resources.
1261         ReleaseBitmapResources();
1262
1263         // load bitmap resources
1264         r = GET_BITMAP_CONFIG_N(SCROLL::THUMB, BITMAP_PIXEL_FORMAT_ARGB8888, __pThumbBitmap);
1265         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1266
1267         r = GET_BITMAP_CONFIG_N(SCROLL::THUMB_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pThumbEffectBitmap);
1268         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1269
1270         r = GET_BITMAP_CONFIG_N(SCROLL::JUMP_TO_TOP, BITMAP_PIXEL_FORMAT_ARGB8888, __pJumpToTopBitmap);
1271         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1272
1273         GET_BITMAP_CONFIG_N(SCROLL::JUMP_TO_TOP_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pJumpToTopEfBitmap);
1274         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1275
1276         r = GET_BITMAP_CONFIG_N(SCROLL::JUMP_TO_TOP_LEFT, BITMAP_PIXEL_FORMAT_ARGB8888, __pJumpToTopLeftBitmap);
1277         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1278
1279         GET_BITMAP_CONFIG_N(SCROLL::JUMP_TO_TOP_LEFT_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pJumpToTopLeftEfBitmap);
1280         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1281
1282         r = GET_BITMAP_CONFIG_N(SCROLL::BUTTON_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pButtonBitmap);
1283         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1284
1285         r = GET_BITMAP_CONFIG_N(SCROLL::BUTTON_BG_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pButtonEfBitmap);
1286         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1287
1288         r = GET_BITMAP_CONFIG_N(SCROLL::BUTTON_BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, __pButtonPressBitmap);
1289         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1290
1291         r = GET_BITMAP_CONFIG_N(SCROLL::HANDLER_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBgBitmap);
1292         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1293
1294         r = GET_BITMAP_CONFIG_N(SCROLL::HANDLER_BG_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBgEffectBitmap);
1295         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1296
1297         if (scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
1298         {
1299                 r = GET_BITMAP_CONFIG_N(SCROLL::HANDLER_HORIZONTAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBitmap);
1300                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1301
1302                 r = GET_BITMAP_CONFIG_N(SCROLL::OVERSCROLLING_LEFT_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pFrontScrollingEffectBitmap);
1303                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1304
1305                 r = GET_BITMAP_CONFIG_N(SCROLL::OVERSCROLLING_RIGHT_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pReerScrollingEffectBitmap);
1306                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1307         }
1308         else // if (__scrollDirection == SCROLL_DIRECTION_VERTICAL)
1309         {
1310                 r = GET_BITMAP_CONFIG_N(SCROLL::HANDLER_VERTICAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pHandlerBitmap);
1311                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1312
1313                 r = GET_BITMAP_CONFIG_N(SCROLL::OVERSCROLLING_TOP_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pFrontScrollingEffectBitmap);
1314                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1315
1316                 r = GET_BITMAP_CONFIG_N(SCROLL::OVERSCROLLING_BOTTOM_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pReerScrollingEffectBitmap);
1317                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] failed to load image.");
1318         }
1319
1320         return E_SUCCESS;
1321
1322 CATCH:
1323         ReleaseBitmapResources();
1324
1325         return E_SYSTEM;
1326 }
1327
1328 result
1329 _ScrollPresenter::LoadShapeConfig(_ScrollDirection scrollDirection, _ControlOrientation orientation)
1330 {
1331         result r = E_SUCCESS;
1332
1333         // load shape configuration
1334         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1335         {
1336                 r = GET_SHAPE_CONFIG(SCROLL::VERTICAL_THUMB_MIN_WIDTH, orientation, __thumbSizeMin.width);
1337                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1338
1339                 r = GET_SHAPE_CONFIG(SCROLL::VERTICAL_THUMB_MIN_HEIGHT, orientation, __thumbSizeMin.height);
1340                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1341
1342                 r = GET_SHAPE_CONFIG(SCROLL::VERTICAL_THUMB_MARGIN, orientation, __thumbMargin);
1343                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1344         }
1345         else
1346         {
1347                 r = GET_SHAPE_CONFIG(SCROLL::HORIZONTAL_THUMB_MIN_WIDTH, orientation, __thumbSizeMin.width);
1348                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1349
1350                 r = GET_SHAPE_CONFIG(SCROLL::HORIZONTAL_THUMB_MIN_HEIGHT, orientation, __thumbSizeMin.height);
1351                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1352
1353                 r = GET_SHAPE_CONFIG(SCROLL::HORIZONTAL_THUMB_MARGIN, orientation, __thumbMargin);
1354                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1355         }
1356
1357         r = GET_SHAPE_CONFIG(SCROLL::JUMP_TO_TOP_RIGHT_MARGIN, orientation, __jumpToTopMarginRight);
1358         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1359
1360         r = GET_SHAPE_CONFIG(SCROLL::JUMP_TO_TOP_TOP_MARGIN, orientation, __jumpToTopMarginTop);
1361         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1362
1363         r = GET_FIXED_VALUE_CONFIG(SCROLL::JUMP_TO_TOP_WIDTH, orientation, __jumpToTopSize.width);
1364         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1365
1366         r = GET_FIXED_VALUE_CONFIG(SCROLL::JUMP_TO_TOP_HEIGHT, orientation, __jumpToTopSize.height);
1367         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1368
1369         r = GET_SHAPE_CONFIG(SCROLL::HANDLER_MARGIN, orientation, __handlerMarginLeft);
1370         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1371
1372         r = GET_SHAPE_CONFIG(SCROLL::HANDLER_MIN_WIDTH, orientation, __handlerSizeMin.width);
1373         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1374
1375         r = GET_SHAPE_CONFIG(SCROLL::HANDLER_MIN_HEIGHT, orientation, __handlerSizeMin.height);
1376         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1377
1378         GET_SHAPE_CONFIG(SCROLL::OVERSCROLLING_LEFT_WIDTH, orientation, __scrollingEffectWidthLeft);
1379         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1380
1381         GET_SHAPE_CONFIG(SCROLL::OVERSCROLLING_RIGHT_WIDTH, orientation, __scrollingEffectWidthRight);
1382         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1383
1384         GET_SHAPE_CONFIG(SCROLL::OVERSCROLLING_TOP_HEIGHT, orientation, __scrollingEffectHeightTop);
1385         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1386
1387         GET_SHAPE_CONFIG(SCROLL::OVERSCROLLING_BOTTOM_HEIGHT, orientation, __scrollingEffectHeightBottom);
1388         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to get resource.");
1389
1390         __handlerMarginRight = __handlerMarginLeft;
1391         __handlerMarginTop = __handlerMarginLeft;
1392         __handlerMarginBottom = __handlerMarginLeft;
1393
1394         return E_SUCCESS;
1395
1396 CATCH:
1397         __thumbSizeMin.SetSize(0, 0);
1398         __thumbMargin = 0;
1399         __jumpToTopSize.SetSize(0, 0);
1400         __jumpToTopMarginRight = 0;
1401         __jumpToTopMarginTop = 0;
1402         __handlerSizeMin.SetSize(0, 0);
1403         __handlerMarginLeft = 0;
1404         __handlerMarginRight = 0;
1405         __handlerMarginTop = 0;
1406         __handlerMarginBottom = 0;
1407         __scrollingEffectWidthLeft = 0;
1408         __scrollingEffectWidthRight = 0;
1409         __scrollingEffectHeightTop = 0;
1410         __scrollingEffectHeightBottom = 0;
1411
1412         return E_SYSTEM;
1413
1414 }
1415
1416 void
1417 _ScrollPresenter::ReleaseBitmapResources(void)
1418 {
1419         delete __pThumbBitmap;
1420         __pThumbBitmap = null;
1421
1422         delete __pThumbEffectBitmap;
1423         __pThumbEffectBitmap = null;
1424
1425         delete __pJumpToTopBitmap;
1426         __pJumpToTopBitmap = null;
1427
1428         delete __pJumpToTopEfBitmap;
1429         __pJumpToTopEfBitmap = null;
1430
1431         delete __pJumpToTopLeftBitmap;
1432         __pJumpToTopLeftBitmap = null;
1433
1434         delete __pJumpToTopLeftEfBitmap;
1435         __pJumpToTopLeftEfBitmap = null;
1436
1437         delete __pButtonBitmap;
1438         __pButtonBitmap = null;
1439
1440         delete __pButtonEfBitmap;
1441         __pButtonEfBitmap = null;
1442
1443         delete __pButtonPressBitmap;
1444         __pButtonPressBitmap = null;
1445
1446         delete __pHandlerBgBitmap;
1447         __pHandlerBgBitmap = null;
1448
1449         delete __pHandlerBgEffectBitmap;
1450         __pHandlerBgEffectBitmap = null;
1451
1452         delete __pHandlerBitmap;
1453         __pHandlerBitmap = null;
1454
1455         delete __pFrontScrollingEffectBitmap;
1456         __pFrontScrollingEffectBitmap = null;
1457
1458         delete __pReerScrollingEffectBitmap;
1459         __pReerScrollingEffectBitmap = null;
1460
1461         return;
1462 }
1463
1464 Rectangle
1465 _ScrollPresenter::CalculateScrollCtrlBounds(_ScrollDirection scrollDirection)
1466 {
1467         Rectangle scrollCtrlBounds;
1468         Rectangle parentBounds = GetParentCtrl().GetBounds();
1469
1470         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1471         {
1472                 scrollCtrlBounds.width = MAX(scrollCtrlBounds.width, (__thumbSizeMin.width + __thumbMargin));
1473                 scrollCtrlBounds.width = MAX(scrollCtrlBounds.width, (__handlerSizeMin.width + __handlerMarginRight + __handlerMarginLeft));
1474                 scrollCtrlBounds.width = MAX(scrollCtrlBounds.width, (__jumpToTopSize.width + __jumpToTopMarginRight));
1475                 scrollCtrlBounds.width = MIN(scrollCtrlBounds.width, (parentBounds.width));
1476                 scrollCtrlBounds.height = parentBounds.height;
1477         }
1478         else
1479         {
1480                 scrollCtrlBounds.height = MAX(scrollCtrlBounds.height, (__thumbSizeMin.width + __thumbMargin));
1481                 scrollCtrlBounds.height = MAX(scrollCtrlBounds.height, (__handlerSizeMin.width + __handlerMarginRight + __handlerMarginLeft));
1482                 scrollCtrlBounds.height = MAX(scrollCtrlBounds.height, (__jumpToTopSize.width + __jumpToTopMarginRight));
1483                 scrollCtrlBounds.height = MIN(scrollCtrlBounds.height, (parentBounds.height));
1484                 scrollCtrlBounds.width = parentBounds.width;
1485         }
1486
1487         scrollCtrlBounds.x = parentBounds.width - scrollCtrlBounds.width;
1488         scrollCtrlBounds.y = parentBounds.height - scrollCtrlBounds.height;
1489
1490         return scrollCtrlBounds;
1491 }
1492
1493 Rectangle
1494 _ScrollPresenter::CalculateThumbBounds(_ScrollDirection scrollDirection)
1495 {
1496         Dimension thumbSize = CalculateThumbSize(scrollDirection);
1497         Point thumbPosition = CalculateThumbPosition(scrollDirection);
1498
1499         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1500         if (scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
1501         {
1502                 if (thumbPosition.x < 0)
1503                 {
1504                         thumbPosition.x = 0;
1505                 }
1506                 else if (thumbPosition.x > scrollCtrlBounds.width - thumbSize.width)
1507                 {
1508                         thumbPosition.x = scrollCtrlBounds.width - thumbSize.width;
1509                 }
1510         }
1511         else
1512         {
1513                 if (thumbPosition.y < 0)
1514                 {
1515                         thumbPosition.y = 0;
1516                 }
1517                 else if (thumbPosition.y > scrollCtrlBounds.height - thumbSize.height)
1518                 {
1519                         thumbPosition.y = scrollCtrlBounds.height - thumbSize.height;
1520                 }
1521         }
1522
1523         return Rectangle(thumbPosition.x, thumbPosition.y, thumbSize.width, thumbSize.height);
1524 }
1525
1526 Point
1527 _ScrollPresenter::CalculateThumbPosition(_ScrollDirection scrollDirection)
1528 {
1529         Point thumbPosition;
1530         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1531
1532         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1533         {
1534                 thumbPosition.x = scrollCtrlBounds.width - (__thumbSizeMin.width + __thumbMargin);
1535                 if (thumbPosition.x < 0)
1536                 {
1537                         thumbPosition.x = 0;
1538                 }
1539
1540                 if (__scrollRange > 0)
1541                 {
1542                         thumbPosition.y = (int)((scrollCtrlBounds.height * __scrollPositionCurr) / __scrollRange);
1543                         if (__scrollPositionCurr < 0)
1544                         {
1545                                 thumbPosition.y = 0;
1546                         }
1547                 }
1548                 else
1549                 {
1550                         thumbPosition.y = 0;
1551                 }
1552         }
1553         else
1554         {
1555                 thumbPosition.y = scrollCtrlBounds.height - (__thumbSizeMin.height + __thumbMargin);
1556                 if (thumbPosition.y < 0)
1557                 {
1558                         thumbPosition.y = 0;
1559                 }
1560
1561                 if (__scrollRange > 0)
1562                 {
1563                         thumbPosition.x = (int)((scrollCtrlBounds.width * __scrollPositionCurr) / __scrollRange);
1564                         if (__scrollPositionCurr < 0)
1565                         {
1566                                 thumbPosition.x = 0;
1567                         }
1568                 }
1569                 else
1570                 {
1571                         thumbPosition.x = 0;
1572                 }
1573         }
1574
1575         return thumbPosition;
1576 }
1577
1578 Dimension
1579 _ScrollPresenter::CalculateThumbSize(_ScrollDirection scrollDirection)
1580 {
1581         Dimension thumbSize;
1582         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1583
1584         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1585         {
1586                 thumbSize.width = MIN(__thumbSizeMin.width, scrollCtrlBounds.width);
1587                 if (__scrollRange > 0)
1588                 {
1589                         thumbSize.height = (int)((scrollCtrlBounds.height * __viewRange) / __scrollRange);
1590                         thumbSize.height = MAX(__thumbSizeMin.height, thumbSize.height);
1591                         thumbSize.height = MIN(scrollCtrlBounds.height, thumbSize.height);
1592                 }
1593                 else
1594                 {
1595                         thumbSize.height = 0;
1596                 }
1597         }
1598         else
1599         {
1600                 thumbSize.height = MIN(__thumbSizeMin.height, scrollCtrlBounds.height);
1601                 if (__scrollRange > 0)
1602                 {
1603                         thumbSize.width = (int)((scrollCtrlBounds.width * __viewRange) / __scrollRange);
1604                         thumbSize.width = MAX(__thumbSizeMin.width, thumbSize.width);
1605                         thumbSize.width = MIN(scrollCtrlBounds.width, thumbSize.width);
1606                 }
1607                 else
1608                 {
1609                         thumbSize.width = 0;
1610                 }
1611         }
1612
1613         return thumbSize;
1614 }
1615
1616 Rectangle
1617 _ScrollPresenter::CalculateHandlerBounds(_ScrollDirection scrollDirection)
1618 {
1619         Dimension handlerSize = CalculateHandlerSize(scrollDirection);
1620         Point handlerPosition = CalculateHandlerPosition(scrollDirection);
1621
1622         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1623         if (scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
1624         {
1625                 if (handlerPosition.x < 0)
1626                 {
1627                         handlerPosition.x = 0;
1628                 }
1629                 else if (handlerPosition.x > scrollCtrlBounds.width - handlerSize.width)
1630                 {
1631                         handlerPosition.x =scrollCtrlBounds.width - handlerSize.width;
1632                 }
1633         }
1634         else
1635         {
1636                 if (handlerPosition.y < 0)
1637                 {
1638                         handlerPosition.y = 0;
1639                 }
1640                 else if (handlerPosition.y > scrollCtrlBounds.height - handlerSize.height)
1641                 {
1642                         handlerPosition.y = scrollCtrlBounds.height - handlerSize.height;
1643                 }
1644         }
1645
1646         return Rectangle(handlerPosition.x, handlerPosition.y, handlerSize.width, handlerSize.height);
1647 }
1648
1649 Point
1650 _ScrollPresenter::CalculateHandlerPosition(_ScrollDirection scrollDirection)
1651 {
1652         Point handlerPosition;
1653         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1654
1655         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1656         {
1657                 handlerPosition.x = scrollCtrlBounds.width - (__handlerSizeMin.width + __handlerMarginRight);
1658                 if (handlerPosition.x < 0)
1659                 {
1660                         handlerPosition.x = 0;
1661                 }
1662                 if (__scrollRange > 0)
1663                 {
1664                         handlerPosition.y = (int)((scrollCtrlBounds.height * __scrollPositionCurr) / __scrollRange);
1665                 }
1666                 else
1667                 {
1668                         handlerPosition.y = 0;
1669                 }
1670         }
1671         else
1672         {
1673                 handlerPosition.y = scrollCtrlBounds.height - (__handlerSizeMin.width + __handlerMarginRight);
1674                 if (handlerPosition.y < 0)
1675                 {
1676                         handlerPosition.y = 0;
1677                 }
1678                 if (__scrollRange > 0)
1679                 {
1680                         handlerPosition.x = (int)((scrollCtrlBounds.width * __scrollPositionCurr) / __scrollRange);
1681                 }
1682                 else
1683                 {
1684                         handlerPosition.x = 0;
1685                 }
1686         }
1687
1688         return handlerPosition;
1689 }
1690
1691 Dimension
1692 _ScrollPresenter::CalculateHandlerSize(_ScrollDirection scrollDirection)
1693 {
1694         Dimension handlerSize;
1695         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1696
1697         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1698         {
1699                 handlerSize.width = MIN(__handlerSizeMin.width, scrollCtrlBounds.width);
1700                 if (__scrollRange > 0)
1701                 {
1702                         handlerSize.height = (int)((scrollCtrlBounds.height * __viewRange) / __scrollRange);
1703                 }
1704                 else
1705                 {
1706                         handlerSize.height = 0;
1707                 }
1708         }
1709         else
1710         {
1711                 handlerSize.height = MIN(__handlerSizeMin.width, scrollCtrlBounds.height);
1712                 if (__scrollRange > 0)
1713                 {
1714                         handlerSize.width = (int)((scrollCtrlBounds.width * __viewRange) / __scrollRange);
1715                 }
1716                 else
1717                 {
1718                         handlerSize.width = 0;
1719                 }
1720         }
1721
1722         return handlerSize;
1723 }
1724
1725 Rectangle
1726 _ScrollPresenter::CalculateJumpToTopBounds(_ScrollDirection scrollDirection)
1727 {
1728         FloatRectangle scrollCtrlBounds = __pCtrlVe->GetBounds();
1729         Rectangle jumpToTopBounds(0, 0, 0, 0);
1730
1731         if (scrollDirection == SCROLL_DIRECTION_VERTICAL)
1732         {
1733                 jumpToTopBounds.width = MIN(__jumpToTopSize.width, scrollCtrlBounds.width);
1734                 jumpToTopBounds.height = MIN(__jumpToTopSize.height, scrollCtrlBounds.height);
1735                 jumpToTopBounds.x = scrollCtrlBounds.width - (__jumpToTopSize.width + __jumpToTopMarginRight);
1736                 if (jumpToTopBounds.x < 0)
1737                 {
1738                         jumpToTopBounds.x = 0;
1739                 }
1740                 jumpToTopBounds.y = __jumpToTopMarginTop;
1741         }
1742         else
1743         {
1744                 jumpToTopBounds.width = MIN(__jumpToTopSize.width, scrollCtrlBounds.width);
1745                 jumpToTopBounds.height = MIN(__jumpToTopSize.height, scrollCtrlBounds.height);
1746                 jumpToTopBounds.y = scrollCtrlBounds.height - (__jumpToTopSize.height + __jumpToTopMarginRight);
1747                 if (jumpToTopBounds.y < 0)
1748                 {
1749                         jumpToTopBounds.y = 0;
1750                 }
1751                 jumpToTopBounds.x = __jumpToTopMarginTop;
1752         }
1753
1754         return jumpToTopBounds;
1755 }
1756
1757 Rectangle
1758 _ScrollPresenter::CalculateScrollingEffectFrontBounds(_ScrollDirection scrollDirection)
1759 {
1760         Rectangle scrollCtrlBounds = GetControl().GetBounds();
1761         Rectangle scrollEffectFrontBounds(0, 0, 0, 0);
1762
1763         if (scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
1764         {
1765                 scrollEffectFrontBounds.width = __scrollingEffectWidthLeft;
1766                 scrollEffectFrontBounds.height = scrollCtrlBounds.height;
1767         }
1768         else
1769         {
1770                 scrollEffectFrontBounds.width = scrollCtrlBounds.width;
1771                 scrollEffectFrontBounds.height = __scrollingEffectHeightTop;
1772         }
1773
1774         return scrollEffectFrontBounds;
1775 }
1776
1777 Rectangle
1778 _ScrollPresenter::CalculateScrollingEffectReerBounds(_ScrollDirection scrollDirection)
1779 {
1780         Rectangle scrollCtrlBounds = GetControl().GetBounds();
1781         Rectangle scrollEffectReerBounds(0, 0, 0, 0);
1782
1783         if (scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
1784         {
1785                 scrollEffectReerBounds.x = scrollCtrlBounds.width - __scrollingEffectWidthRight;
1786                 scrollEffectReerBounds.width = __scrollingEffectWidthRight;
1787                 scrollEffectReerBounds.height = scrollCtrlBounds.height;
1788         }
1789         else
1790         {
1791                 scrollEffectReerBounds.y = scrollCtrlBounds.height - __scrollingEffectHeightBottom;
1792                 scrollEffectReerBounds.width = scrollCtrlBounds.width;
1793                 scrollEffectReerBounds.height = __scrollingEffectHeightBottom;
1794         }
1795
1796         return scrollEffectReerBounds;
1797 }
1798
1799 result
1800 _ScrollPresenter::AttachScrollChild(_VisualElement** ppScrollVe, const Rectangle& bounds, bool visibility)
1801 {
1802         result r = E_SUCCESS;
1803         _VisualElement* pScrollVe = *ppScrollVe;
1804
1805         if (pScrollVe == null)
1806         {
1807                 pScrollVe = new (std::nothrow) _VisualElement();
1808                 SysTryReturnResult(NID_UI_CTRL, pScrollVe, E_OUT_OF_MEMORY, "Memory allocation failed.");
1809
1810                 r = pScrollVe->Construct();
1811                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct pScrollVe.", GetErrorMessage(r));
1812
1813                 r = pScrollVe->SetSurfaceOpaque(false);
1814                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set surface opaque.", GetErrorMessage(r));
1815
1816                 pScrollVe->SetImplicitAnimationEnabled(false);
1817
1818                 r = __pCtrlVe->AttachChild(*pScrollVe);
1819                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach child to the __pCtrlVe.", GetErrorMessage(r));
1820
1821                 *ppScrollVe = pScrollVe;
1822         }
1823
1824         r = SetScrollChildBounds(*pScrollVe, bounds);
1825         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1826
1827         pScrollVe->SetShowState(visibility);
1828
1829         return E_SUCCESS;
1830
1831 CATCH:
1832         if (pScrollVe)
1833         {
1834                 pScrollVe->Destroy();
1835                 pScrollVe = null;
1836                 *ppScrollVe = pScrollVe;
1837         }
1838
1839         return r;
1840 }
1841
1842 void
1843 _ScrollPresenter::DetachScrollChild(_VisualElement** ppScrollVe, bool destroy)
1844 {
1845         _VisualElement* pScrollVe = *ppScrollVe;
1846
1847         if (pScrollVe)
1848         {
1849                 if (destroy)
1850                 {
1851                         pScrollVe->Destroy();
1852                         pScrollVe = null;
1853                         *ppScrollVe = null;
1854                 }
1855                 else
1856                 {
1857                         pScrollVe->SetShowState(false);
1858                 }
1859         }
1860 }
1861
1862 Rectangle
1863 _ScrollPresenter::GetScrollChildBounds(_VisualElement& scrollVe) const
1864 {
1865         FloatRectangle veBounds = scrollVe.GetBounds();
1866         return Rectangle(veBounds.x, veBounds.y, veBounds.width, veBounds.height);
1867 }
1868
1869 result
1870 _ScrollPresenter::SetScrollChildBounds(_VisualElement& scrollVe, const Rectangle& bounds)
1871 {
1872         FloatRectangle veBounds(bounds.x, bounds.y, bounds.width, bounds.height);
1873         return scrollVe.SetBounds(veBounds);
1874 }
1875
1876 result
1877 _ScrollPresenter::RelayoutScrollChildren(_ScrollDirection scrollDirection)
1878 {
1879         result r = E_SUCCESS;
1880
1881         r = SetScrollChildBounds(*__pCtrlVe, CalculateScrollCtrlBounds(scrollDirection));
1882         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds the __pCtrlVe.");
1883
1884         if (__enableJumpToTop)
1885         {
1886                 r = SetScrollChildBounds(*__pJumpToTopVe, CalculateJumpToTopBounds(scrollDirection));
1887                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds the __pJumpToTopVe.");
1888
1889                 __needUpdateJumpTopTop = true;
1890         }
1891
1892         if (__enableHandler)
1893         {
1894                 r = SetScrollChildBounds(*__pHandlerVe, CalculateHandlerBounds(scrollDirection));
1895                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds the __pHandlerVe.");
1896
1897                 __needUpdateHandler = true;
1898         }
1899         else
1900         {
1901                 r = SetScrollChildBounds(*__pThumbVe, CalculateThumbBounds(scrollDirection));
1902                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds the __pThumbVe.");
1903
1904                 __needUpdateThumb = true;
1905         }
1906
1907         r = SetScrollChildBounds(*__pFrontScrollingEffectVe, CalculateScrollingEffectFrontBounds(scrollDirection));
1908         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds the __pFrontScrollingEffectVe.");
1909
1910         r = SetScrollChildBounds(*__pReerScrollingEffectVe, CalculateScrollingEffectReerBounds(scrollDirection));
1911         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds the __pReerScrollingEffectVe.");
1912
1913         __needUpdateScrollingEffect = true;
1914
1915         r = (GetControl().GetVisualElement())->InvalidateRectangle(null);
1916         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to invalidate Control Ve.");
1917
1918         return E_SUCCESS;
1919 }
1920
1921 result
1922 _ScrollPresenter::SetParentBouncing(float rate)
1923 {
1924         if (rate == __parentBouncingRate)
1925         {
1926                 return E_SUCCESS;
1927         }
1928
1929         _VisualElement* pParentVe = __pParentCtrl->GetVisualElement();
1930         IList* pChildrenVeList = null;
1931         result r = E_SUCCESS;
1932
1933         if (__parentBouncingRate <= 0.0f)
1934         {
1935                 __beforeBouncingMatrixHash.RemoveAll(true);
1936
1937                 pChildrenVeList = pParentVe->GetChildrenN();
1938                 r = GetLastResult();
1939                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1940
1941                 for (int i = 0; i < pChildrenVeList->GetCount(); i++)
1942                 {
1943                         VisualElement* pChildVe = static_cast<VisualElement*>(pChildrenVeList->GetAt(i));
1944                         bool isSystemChild = false;
1945                         for (int j = 0; j < __pParentCtrl->GetChildCount(); j++)
1946                         {
1947                                 _Control* pChild = __pParentCtrl->GetChild(j);
1948                                 _VisualElement* pChildControlVe = pChild->GetVisualElement();
1949
1950                                 if (pChildControlVe == pChildVe && pChild->GetArea() == _CONTROL_AREA_SYSTEM)
1951                                 {
1952                                         isSystemChild = true;
1953                                         break;
1954                                 }
1955                         }
1956
1957                         if (isSystemChild)
1958                         {
1959                                 continue;
1960                         }
1961
1962                         Integer* pChildHashCode = new (std::nothrow) Integer(pChildVe->GetHashCode());
1963                         SysTryCatch(NID_UI_CTRL, pChildHashCode, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1964
1965                         FloatMatrix4* pOriginChildMatrix = new (std::nothrow) FloatMatrix4;
1966                         SysTryCatch(NID_UI_CTRL, pOriginChildMatrix, delete pChildHashCode, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1967
1968                         *pOriginChildMatrix = pChildVe->GetTransformMatrix();
1969
1970                         r = __beforeBouncingMatrixHash.Add(*pChildHashCode, *pOriginChildMatrix);
1971                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, (delete pChildHashCode, delete pOriginChildMatrix), r, "[%s] Propagating.",
1972                                         GetErrorMessage(r));
1973                 }
1974                 delete pChildrenVeList;
1975                 pChildrenVeList = null;
1976         }
1977
1978         __parentBouncingRate = rate;
1979
1980         if (rate > 0.0f)
1981         {
1982                 float scaleRate = 1.0f - (rate * (1.0f - SCROLLING_EFFECT_MIN_SCALE_RATE));
1983                 float centerX = 0.0f;
1984                 float centerY = 0.0f;
1985                 Rectangle parentBounds = __pParentCtrl->GetBounds();
1986                 int fixedScrollPosition = __scrollPositionCurr;
1987
1988                 if (__parentUsingViewport)
1989                 {
1990                         if (fixedScrollPosition < 0)
1991                         {
1992                                 fixedScrollPosition = 0;
1993                         }
1994                         else if (fixedScrollPosition > (__scrollRange - __viewRange))
1995                         {
1996                                 fixedScrollPosition = (__scrollRange -__viewRange);
1997                         }
1998                 }
1999                 else
2000                 {
2001                         fixedScrollPosition = 0;
2002                 }
2003
2004                 if (__scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
2005                 {
2006                         centerX = parentBounds.width / 2.0f + fixedScrollPosition;
2007                         centerY = parentBounds.height / 2.0f;
2008                 }
2009                 else
2010                 {
2011                         centerX = parentBounds.width / 2.0f;
2012                         centerY = parentBounds.height / 2.0f + fixedScrollPosition;
2013                 }
2014
2015                 pChildrenVeList = pParentVe->GetChildrenN();
2016                 r = GetLastResult();
2017                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
2018
2019                 for (int i = 0; i < pChildrenVeList->GetCount(); i++)
2020                 {
2021                         VisualElement* pChildVe = static_cast<VisualElement*>(pChildrenVeList->GetAt(i));
2022                         bool isSystemChild = false;
2023
2024                         for (int j = 0; j < __pParentCtrl->GetChildCount(); j++)
2025                         {
2026                                 _Control* pChild = __pParentCtrl->GetChild(j);
2027                                 _VisualElement* pChildControlVe = pChild->GetVisualElement();
2028
2029                                 if (pChildControlVe == pChildVe && pChild->GetArea() == _CONTROL_AREA_SYSTEM)
2030                                 {
2031                                         isSystemChild = true;
2032                                         break;
2033                                 }
2034                         }
2035
2036                         if (isSystemChild)
2037                         {
2038                                 continue;
2039                         }
2040
2041                         Integer* pChildHashCode = new (std::nothrow) Integer(pChildVe->GetHashCode());
2042                         SysTryCatch(NID_UI_CTRL, pChildHashCode, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2043
2044                         Object* pMatrixObject = __beforeBouncingMatrixHash.GetValue(*pChildHashCode);
2045                         delete pChildHashCode;
2046                         if (pMatrixObject == null)
2047                         {
2048                                 continue;
2049                         }
2050                         FloatMatrix4* pOriginChildMatrix = static_cast<FloatMatrix4*>(pMatrixObject);
2051
2052                         FloatRectangle childBounds = pChildVe->GetBounds();
2053                         _TransformMatrix3Df transformMatrix;
2054                         transformMatrix.SetTransformMatrix(*pOriginChildMatrix);
2055                         transformMatrix.SetScaleAnchor(centerX - (childBounds.x + (childBounds.width / 2.0f)), centerY - (childBounds.y + (childBounds.height / 2.0f)), 0.0f);
2056                         transformMatrix.SetScaleFactors(scaleRate, scaleRate, 1.0f);
2057
2058                         pChildVe->SetTransformMatrix(transformMatrix.GetTransformMatrix());
2059                 }
2060                 delete pChildrenVeList;
2061                 pChildrenVeList = null;
2062         }
2063         else
2064         {
2065                 pChildrenVeList = pParentVe->GetChildrenN();
2066                 r = GetLastResult();
2067                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
2068
2069                 for (int i = 0; i < pChildrenVeList->GetCount(); i++)
2070                 {
2071                         VisualElement* pChildVe = static_cast<VisualElement*>(pChildrenVeList->GetAt(i));
2072                         bool isSystemChild = false;
2073                         for (int j = 0; j < __pParentCtrl->GetChildCount(); j++)
2074                         {
2075                                 _Control* pChild = __pParentCtrl->GetChild(j);
2076                                 _VisualElement* pChildControlVe = pChild->GetVisualElement();
2077
2078                                 if (pChildControlVe == pChildVe && pChild->GetArea() == _CONTROL_AREA_SYSTEM)
2079                                 {
2080                                         isSystemChild = true;
2081                                         break;
2082                                 }
2083                         }
2084
2085                         if (isSystemChild)
2086                         {
2087                                 continue;
2088                         }
2089
2090                         Integer* pChildHashCode = new (std::nothrow) Integer(pChildVe->GetHashCode());
2091                         SysTryCatch(NID_UI_CTRL, pChildHashCode != null, , E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2092
2093                         Object* pMatrixObject = __beforeBouncingMatrixHash.GetValue(*pChildHashCode);
2094                         delete pChildHashCode;
2095                         if (pMatrixObject == null)
2096                         {
2097                                 continue;
2098                         }
2099                         FloatMatrix4* pOriginChildMatrix = static_cast<FloatMatrix4*>(pMatrixObject);
2100
2101                         pChildVe->SetTransformMatrix(*pOriginChildMatrix);
2102                 }
2103                 delete pChildrenVeList;
2104                 pChildrenVeList = null;
2105         }
2106
2107         return r;
2108
2109 CATCH:
2110         delete pChildrenVeList;
2111
2112         return r;
2113 }
2114
2115 void
2116 _ScrollPresenter::DrawThumb(void)
2117 {
2118         if (__needUpdateThumb != true)
2119         {
2120                 return;
2121         }
2122
2123         Canvas* pCanvas = null;
2124         Bitmap* pThumbBitmap = null;
2125         result r = E_SUCCESS;
2126
2127         if (__enableHandler != true)
2128         {
2129                 pCanvas = __pThumbVe->GetCanvasN();
2130                 SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "Failed to get the canvas of __pThumbVe");
2131
2132                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
2133                 pCanvas->Clear();
2134
2135                 pThumbBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pThumbBitmap,
2136                                                                                                                         Color::GetColor(COLOR_ID_MAGENTA),
2137                                                                                                                         __thumbColor);
2138                 SysTryCatch(NID_UI_CTRL, pThumbBitmap, , GetLastResult(), "[%s] Failed to draw bitmap.",
2139                                         GetErrorMessage(GetLastResult()));
2140
2141                 Rectangle thumbBounds = GetScrollChildBounds(*__pThumbVe);
2142                 thumbBounds.SetPosition(0, 0);
2143                 r = DrawBitmap(*pCanvas, thumbBounds, *__pThumbEffectBitmap);
2144                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2145
2146                 r = DrawBitmap(*pCanvas, thumbBounds, *pThumbBitmap);
2147                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2148         }
2149
2150         __needUpdateThumb = false;
2151         //fall through
2152 CATCH:
2153         delete pCanvas;
2154         delete pThumbBitmap;
2155
2156         return;
2157 }
2158
2159 void
2160 _ScrollPresenter::DrawJumpToTop(void)
2161 {
2162         if (__needUpdateJumpTopTop != true)
2163         {
2164                 return;
2165         }
2166
2167         Canvas* pCanvas = null;
2168         Bitmap* pColorReplacedBitmap = null;
2169         result r = E_SUCCESS;
2170
2171         if (__enableJumpToTop)
2172         {
2173                 Bitmap* pBitmap = __pJumpToTopBitmap;
2174                 Bitmap* pEfBitmap = __pJumpToTopEfBitmap;
2175                 if (__scrollDirection == SCROLL_DIRECTION_HORIZONTAL)
2176                 {
2177                         pBitmap = __pJumpToTopLeftBitmap;
2178                         pEfBitmap = __pJumpToTopLeftEfBitmap;
2179                 }
2180
2181                 Color jumpToTopColor = (__jumpToTopPressed) ? __jumpToTopColorPressed : __jumpToTopColor;
2182                 Color buttonColor = (__jumpToTopPressed) ? __buttonColorPressed : __buttonColor;
2183                 Bitmap* pButtonEfBitmap = (__jumpToTopPressed) ? __pButtonPressBitmap : __pButtonEfBitmap;
2184
2185                 pCanvas = __pJumpToTopVe->GetCanvasN();
2186                 SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "Failed to get the canvas of __pJumpToTopVe");
2187
2188                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
2189                 pCanvas->Clear();
2190
2191                 Rectangle jumpToTopBounds = GetScrollChildBounds(*__pJumpToTopVe);
2192                 jumpToTopBounds.SetPosition(0, 0);
2193
2194                 //Draw Button
2195                 r = DrawBitmap(*pCanvas, jumpToTopBounds, *pButtonEfBitmap);
2196                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2197
2198                 pColorReplacedBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pButtonBitmap,
2199                                                                                                                                         Color::GetColor(COLOR_ID_MAGENTA),
2200                                                                                                                                         buttonColor);
2201                 SysTryCatch(NID_UI_CTRL, pColorReplacedBitmap, , GetLastResult(), "[%s] Failed to draw bitmap.", GetErrorMessage(GetLastResult()));
2202
2203                 r = DrawBitmap(*pCanvas, jumpToTopBounds, *pColorReplacedBitmap);
2204                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2205
2206                 r = DrawBitmap(*pCanvas, jumpToTopBounds, *pEfBitmap);
2207                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2208
2209                 pColorReplacedBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap,
2210                                                                                                                                         Color::GetColor(COLOR_ID_MAGENTA),
2211                                                                                                                                         jumpToTopColor);
2212                 SysTryCatch(NID_UI_CTRL, pColorReplacedBitmap, , GetLastResult(), "[%s] Failed to draw bitmap.", GetErrorMessage(GetLastResult()));
2213
2214                 r = DrawBitmap(*pCanvas, jumpToTopBounds, *pColorReplacedBitmap);
2215                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2216         }
2217
2218         __needUpdateJumpTopTop = false;
2219         //fall through
2220 CATCH:
2221         delete pCanvas;
2222         delete pColorReplacedBitmap;
2223
2224         return;
2225 }
2226
2227 void
2228 _ScrollPresenter::DrawHandler(void)
2229 {
2230         if (__needUpdateHandler != true)
2231         {
2232                 return;
2233         }
2234
2235         Canvas* pCanvas = null;
2236         Bitmap* pHandlerBgBitmap = null;
2237         result r = E_SUCCESS;
2238
2239         if (__enableHandler)
2240         {
2241                 // handler bounds setting
2242                 pCanvas = __pHandlerVe->GetCanvasN();
2243                 SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "Failed to get the canvas of __pHandlerVe");
2244
2245                 // clear background
2246                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
2247                 pCanvas->Clear();
2248
2249                 // draw handler bg bitmap
2250                 pHandlerBgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pHandlerBgBitmap,
2251                                                                                                                                 Color::GetColor(COLOR_ID_MAGENTA),
2252                                                                                                                                 __thumbColor);
2253                 SysTryCatch(NID_UI_CTRL, pHandlerBgBitmap, , GetLastResult(), "[%s] Failed to draw bitmap.", GetErrorMessage(GetLastResult()));
2254
2255                 Rectangle handlerBounds = GetScrollChildBounds(*__pHandlerVe);
2256                 handlerBounds.SetPosition(0, 0);
2257
2258                 r = DrawBitmap(*pCanvas, handlerBounds, *__pHandlerBgEffectBitmap);
2259                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2260
2261                 r = DrawBitmap(*pCanvas, handlerBounds, *pHandlerBgBitmap);
2262                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2263
2264                 r = DrawBitmap(*pCanvas, handlerBounds, *__pHandlerBitmap);
2265                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2266         }
2267
2268         __needUpdateHandler = false;
2269         //fall through
2270 CATCH:
2271         delete pCanvas;
2272         delete pHandlerBgBitmap;
2273
2274         return;
2275 }
2276
2277 void
2278 _ScrollPresenter::DrawScrollingEffect(void)
2279 {
2280         if (__needUpdateScrollingEffect != true)
2281         {
2282                 return;
2283         }
2284
2285         Canvas* pCanvas = null;
2286         result r = E_SUCCESS;
2287
2288         if (__pFrontScrollingEffectBitmap != null)
2289         {
2290                 pCanvas = __pFrontScrollingEffectVe->GetCanvasN();
2291                 SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "[%s] Failed to get the canvas of __pFrontScrollingEffectVe", GetErrorMessage(GetLastResult()));
2292
2293                 // clear background
2294                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
2295                 pCanvas->Clear();
2296
2297                 Rectangle effectBounds = GetScrollChildBounds(*__pFrontScrollingEffectVe);
2298                 effectBounds.SetPosition(0, 0);
2299                 r = DrawBitmap(*pCanvas, effectBounds, *__pFrontScrollingEffectBitmap);
2300                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2301
2302                 delete pCanvas;
2303         }
2304
2305         if (__pReerScrollingEffectBitmap != null)
2306         {
2307                 pCanvas = __pReerScrollingEffectVe->GetCanvasN();
2308                 SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "[%s] Failed to get the canvas of __pReerScrollingEffectVe", GetErrorMessage(GetLastResult()));
2309
2310                 // clear background
2311                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
2312                 pCanvas->Clear();
2313
2314                 Rectangle effectBounds = GetScrollChildBounds(*__pReerScrollingEffectVe);
2315                 effectBounds.SetPosition(0, 0);
2316                 r = DrawBitmap(*pCanvas, effectBounds, *__pReerScrollingEffectBitmap);
2317                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw bitmap.", GetErrorMessage(r));
2318
2319                 delete pCanvas;
2320         }
2321
2322         __needUpdateScrollingEffect = false;
2323
2324         return;
2325
2326 CATCH:
2327         delete pCanvas;
2328 }
2329
2330 result
2331 _ScrollPresenter::DrawBitmap(Canvas& canvas, const Rectangle& bounds, const Bitmap& bitmap)
2332 {
2333         result r = E_SUCCESS;
2334         if (bitmap.IsNinePatchedBitmap())
2335         {
2336                 r = canvas.DrawNinePatchedBitmap(bounds, bitmap);
2337                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to draw ninepathced bitmap");
2338         }
2339         else
2340         {
2341                 Point pos((bounds.width - bitmap.GetWidth()) / 2,       (bounds.height - bitmap.GetHeight()) / 2);
2342                 r = canvas.DrawBitmap(pos, bitmap);
2343                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to draw bitmap");
2344         }
2345
2346         return E_SUCCESS;
2347 }
2348
2349 bool
2350 _ScrollPresenter::OnTouchPressedJumpToTop(const _Control& source, const _TouchInfo& touchinfo)
2351 {
2352         if (__enableJumpToTop && IsScrollVisible())
2353         {
2354                 FloatRectangle ctrlBounds = __pCtrlVe->GetBounds();
2355                 Rectangle jumpToTopBounds = GetScrollChildBounds(*__pJumpToTopVe);
2356                 jumpToTopBounds.x += ctrlBounds.x;
2357                 jumpToTopBounds.y += ctrlBounds.y;
2358                 if (jumpToTopBounds.Contains(touchinfo.GetCurrentPosition()))
2359                 {
2360                         __needUpdateJumpTopTop = true;
2361                         __jumpToTopPressed = true;
2362                         (GetControl().GetVisualElement())->InvalidateRectangle(null);
2363
2364                         if (__pScrollEvent)
2365                         {
2366                                 _UiScrollEventArg* pEventArg = _UiScrollEventArg::GetScrollEventArgN(*__pScrollCtrl, 0);
2367                                 SysTryReturn(NID_UI_CTRL, pEventArg, true, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2368
2369                                 __pScrollEvent->Fire(*pEventArg);
2370                         }
2371
2372                         return true;
2373                 }
2374         }
2375
2376         return false;
2377 }
2378
2379 bool
2380 _ScrollPresenter::OnTouchReleasedJumpToTop(const _Control& source, const _TouchInfo& touchinfo)
2381 {
2382         if (__jumpToTopPressed)
2383         {
2384                 __needUpdateJumpTopTop = true;
2385         }
2386
2387         __jumpToTopPressed = false;
2388         (GetControl().GetVisualElement())->InvalidateRectangle(null);
2389         return false;
2390 }
2391
2392 bool
2393 _ScrollPresenter::OnTouchMovedJumpToTop(const _Control& source, const _TouchInfo& touchinfo)
2394 {
2395         if (__jumpToTopPressed)
2396         {
2397                 FloatRectangle ctrlBounds = __pCtrlVe->GetBounds();
2398                 Rectangle jumpToTopBounds = GetScrollChildBounds(*__pJumpToTopVe);
2399                 jumpToTopBounds.x += ctrlBounds.x;
2400                 jumpToTopBounds.y += ctrlBounds.y;
2401                 if (!(jumpToTopBounds.Contains(touchinfo.GetCurrentPosition())))
2402                 {
2403                         __needUpdateJumpTopTop = true;
2404                         __jumpToTopPressed = false;
2405                         (GetControl().GetVisualElement())->InvalidateRectangle(null);
2406                 }
2407         }
2408
2409         return false;
2410 }
2411
2412 bool
2413 _ScrollPresenter::OnTouchPressedHandler(const _Control& source, const _TouchInfo& touchinfo)
2414 {
2415         if (__enableHandler)
2416         {
2417                 FloatRectangle ctrlBounds = __pCtrlVe->GetBounds();
2418                 Rectangle handlerBounds = GetScrollChildBounds(*__pHandlerVe);
2419                 handlerBounds.x += ctrlBounds.x;
2420                 handlerBounds.y += ctrlBounds.y;
2421                 if (handlerBounds.Contains(touchinfo.GetCurrentPosition()))
2422                 {
2423                         __handlerTouched = true;
2424                         if (__scrollDirection == SCROLL_DIRECTION_VERTICAL)
2425                         {
2426                                 __handlerTouchedPosition = touchinfo.GetCurrentPosition().y;
2427                         }
2428                         else
2429                         {
2430                                 __handlerTouchedPosition = touchinfo.GetCurrentPosition().x;
2431                         }
2432                         return true;
2433                 }
2434         }
2435
2436         return false;
2437 }
2438
2439 bool
2440 _ScrollPresenter::OnTouchReleasedHandler(const _Control& source, const _TouchInfo& touchinfo)
2441 {
2442         if (__enableHandler)
2443         {
2444                 __handlerTouched = false;
2445                 __handlerTouchedPosition = 0;
2446
2447                 return true;
2448         }
2449
2450         // forwarding OnTouchReleased event to parent control
2451         return false;
2452 }
2453
2454 bool
2455 _ScrollPresenter::OnTouchMovedHandler(const _Control& source, const _TouchInfo& touchinfo)
2456 {
2457         if (__enableHandler && __handlerTouched)
2458         {
2459                 int scrollPosition = GetScrollPosition();
2460                 Rectangle scrollCtrlBounds = GetControl().GetBounds();
2461                 int touchPosition = 0;
2462                 int scrollBoundsRange = 0;
2463
2464                 if (__scrollDirection == SCROLL_DIRECTION_VERTICAL)
2465                 {
2466                         touchPosition = touchinfo.GetCurrentPosition().y;
2467                         scrollBoundsRange = scrollCtrlBounds.height;
2468                 }
2469                 else
2470                 {
2471                         touchPosition = touchinfo.GetCurrentPosition().x;
2472                         scrollBoundsRange = scrollCtrlBounds.width;
2473                 }
2474
2475                 //1. move handler
2476                 int touchDistance = touchPosition - __handlerTouchedPosition;
2477                 int scrollDistance = (int)(( __scrollRange * touchDistance) / scrollBoundsRange);
2478
2479                 scrollPosition += scrollDistance;
2480
2481                 int scrollType = -1;
2482                 if (scrollPosition <= 0)
2483                 {
2484                         scrollType = (__scrollDirection == SCROLL_DIRECTION_VERTICAL) ? SCROLL_END_EVENT_END_TOP : SCROLL_END_EVENT_END_LEFT;
2485                         scrollPosition = 0;
2486                 }
2487                 else if (scrollPosition >= __scrollRange - __viewRange)
2488                 {
2489                         scrollType = (__scrollDirection == SCROLL_DIRECTION_VERTICAL) ? SCROLL_END_EVENT_END_BOTTOM :
2490                                 SCROLL_END_EVENT_END_RIGHT;
2491                         scrollPosition = __scrollRange - __viewRange;
2492                 }
2493
2494                 result r = SetScrollPosition(scrollPosition);
2495                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Failed to set scroll position.", GetErrorMessage(r));
2496
2497                 __handlerTouchedPosition = touchPosition;
2498
2499                 //2. fire event
2500                 if (__pScrollEvent)
2501                 {
2502                         _UiScrollEventArg* pEventArg = null;
2503                         if (scrollType >= 0)
2504                         {
2505                                 pEventArg = _UiScrollEventArg::GetScrollEventArgN(*__pScrollCtrl, (ScrollEndEvent)scrollType);
2506                                 SysTryReturn(NID_UI_CTRL, pEventArg, true, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2507                         }
2508                         else
2509                         {
2510                                 pEventArg = _UiScrollEventArg::GetScrollEventArgN(*__pScrollCtrl, GetScrollPosition());
2511                                 SysTryReturn(NID_UI_CTRL, pEventArg, true, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2512                         }
2513
2514                         __pScrollEvent->Fire(*pEventArg);
2515                 }
2516
2517                 return true;
2518         }
2519
2520         // forwarding OnTouchMoved event to parent control
2521         return false;
2522 }
2523
2524 void
2525 _ScrollPresenter::SetParentCtrl(_Control& parentCtrl)
2526 {
2527         __pParentCtrl = &parentCtrl;
2528 }
2529
2530 _Control&
2531 _ScrollPresenter::GetParentCtrl(void) const
2532 {
2533         return *__pParentCtrl;
2534 }
2535
2536 void
2537 _ScrollPresenter::SetControl(_Scroll& scrollCtrl)
2538 {
2539         __pScrollCtrl = &scrollCtrl;
2540 }
2541
2542 _Scroll&
2543 _ScrollPresenter::GetControl(void) const
2544 {
2545         return *__pScrollCtrl;
2546 }
2547
2548 }}} // Tizen::Ui::Controls