Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_GalleryCanvas.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 #include <FBaseColArrayList.h>
18 #include <FGrpCanvas.h>
19 #include <FGrpFloatRectangle.h>
20 #include <FGrpFloatPoint.h>
21 #include <FUiAnimVisualElementPropertyAnimation.h>
22 #include <FBaseSysLog.h>
23 #include "FUiAnim_VisualElement.h"
24 #include "FUiCtrl_GalleryCanvas.h"
25 #include "FUiCtrl_GalleryBitmap.h"
26
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Ui;
31 using namespace Tizen::Ui::Animations;
32
33 namespace Tizen { namespace Ui { namespace Controls
34 {
35
36 _GalleryAnimationProvider::_GalleryAnimationProvider(void)
37         : __pAnimationDelegator(null)
38 {
39         __animationTiming.duration_ms = 0;
40         __animationTiming.timingFunction = GALLERY_ANIMATION_TIMING_FUNC_LINEAR;
41 }
42
43 _GalleryAnimationProvider::~_GalleryAnimationProvider(void)
44 {
45         // Do nothing
46 }
47
48 VisualElementAnimation*
49 _GalleryAnimationProvider::CreateAnimationForProperty(VisualElement& source, const String& property)
50 {
51         const IVisualElementAnimationTimingFunction* pTimingFunction = GetTimingFunc(__animationTiming.timingFunction);
52
53         VisualElementAnimation* pAnimation = VisualElementAnimationProvider::CreateAnimationForProperty(source, property);//dynamic_cast<VisualElementPropertyAnimation*>(visualElement.CreateAnimationForPropertyN(visualElement, VEPropTransform));
54         if (pAnimation != null)
55         {
56                 result r = pAnimation->SetDuration(__animationTiming.duration_ms);
57                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
58
59                 pAnimation->SetTimingFunction(pTimingFunction);
60                 pAnimation->SetVisualElementAnimationStatusEventListener(__pAnimationDelegator);
61         }
62
63         SetLastResult(E_SUCCESS);
64
65         return pAnimation;
66
67 CATCH:
68         delete pAnimation;
69
70         return null;
71 }
72
73 void
74 _GalleryAnimationProvider::SetAnimationDelegator(IVisualElementAnimationStatusEventListener* pAnimationDelegator)
75 {
76         __pAnimationDelegator = pAnimationDelegator;
77         return;
78 }
79
80 void
81 _GalleryAnimationProvider::SetGalleryAnimationTiming(_GalleryAnimationTiming animationTiming)
82 {
83         __animationTiming = animationTiming;
84         return;
85 }
86
87 const IVisualElementAnimationTimingFunction*
88 _GalleryAnimationProvider::GetTimingFunc(_GalleryAnimationTimingFunction timingFunction) const
89 {
90         const IVisualElementAnimationTimingFunction* pTimingFunc = null;
91         switch (timingFunction)
92         {
93         case GALLERY_ANIMATION_TIMING_FUNC_LINEAR:
94                 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("Linear");
95                 break;
96         case GALLERY_ANIMATION_TIMING_FUNC_EASEIN:
97                 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("EaseIn");
98                 break;
99         case GALLERY_ANIMATION_TIMING_FUNC_EASEOUT:
100                 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("EaseOut");
101                 break;
102         case GALLERY_ANIMATION_TIMING_FUNC_EASEINOUT:
103                 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("EaseInOut");
104                 break;
105         default:
106                 break;
107         }
108
109         return pTimingFunc;
110 }
111
112
113 // _GalleryCanvas
114 _GalleryCanvas*
115 _GalleryCanvas::CreateGalleryCanvasN(FloatRectangle& canvasBounds)
116 {
117         result r = E_SUCCESS;
118
119         _GalleryCanvas* pCanvas = new (std::nothrow) _GalleryCanvas();
120         SysTryCatch(NID_UI_CTRL, pCanvas != null, , E_OUT_OF_MEMORY,
121                                         "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _GalleryCanvas.");
122
123         r = pCanvas->Construct(canvasBounds);
124         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
125
126         SetLastResult(E_SUCCESS);
127
128         return pCanvas;
129
130 CATCH:
131         delete pCanvas;
132
133         return null;
134 }
135
136 result
137 _GalleryCanvas::SetImage(const _GalleryBitmap* pImage, const FloatRectangle& viewBounds, _GalleryFittingType fittingType)
138 {
139         if (pImage == null)
140         {
141                 return E_SUCCESS;
142         }
143
144         FloatRectangle canvasBounds = GetBounds();
145         canvasBounds.width = viewBounds.width;
146         canvasBounds.height = viewBounds.height;
147         result r = SetBounds(canvasBounds);
148         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
149
150         _GalleryBitmap* pCanvasImage = _GalleryBitmap::CloneN(const_cast<_GalleryBitmap*>(pImage));
151         r = GetLastResult();
152         SysTryReturn(NID_UI_CTRL, pCanvasImage != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
153
154         delete __pImage;
155         __pImage = pCanvasImage;
156
157         if (__pImage->IsInitialize() == false || __visualElementBoundsChanged == true)
158         {
159                 result r = __pImage->Initialize(__pRootCanvas->GetControlSize(), fittingType);
160                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
161
162                 FloatDimension imageSize = GetScaledBitmapSize(*__pImage, __pRootCanvas->GetControlSize(), fittingType);
163                 FloatPoint position(0,0);
164                 if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_MIDDLE)
165                 {
166                         position.y = (__bounds.height - imageSize.height) / 2;
167                 }
168                 else if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_BOTTOM)
169                 {
170                         position.y = (__bounds.height - imageSize.height);
171                 }
172                 if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_CENTER)
173                 {
174                         position.x = (__bounds.width - imageSize.width) / 2;
175                 }
176                 else if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_RIGHT)
177                 {
178                         position.x = (__bounds.width - imageSize.width);
179                 }
180
181                 FloatRectangle visualElementBounds(position.x, position.y, imageSize.width, imageSize.height);
182                 r = SetVisualElementBounds(visualElementBounds);
183                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
184
185                 __originalImageBounds = visualElementBounds;
186                 SetImageBounds(GetBounds());
187
188                 __visualElementBoundsChanged = false;
189         }
190
191         SetNeedUpdateImage(true);
192
193         return E_SUCCESS;
194 }
195
196 _GalleryBitmap*
197 _GalleryCanvas::GetImage(void) const
198 {
199         return __pImage;
200 }
201
202 result
203 _GalleryCanvas::SetBounds(const FloatRectangle& bounds)
204 {
205         SysTryReturnResult(NID_UI_CTRL, bounds.width >= 0.0 && bounds.height >= 0.0, E_INVALID_ARG,
206                         "The bounds width or height is negative value.");
207
208         __bounds = bounds;
209         if (GetFrameVisualElement().IsClipChildrenEnabled() == true)
210         {
211                 GetFrameVisualElement().SetBounds(bounds);
212         }
213         return E_SUCCESS;
214 }
215
216 const FloatRectangle&
217 _GalleryCanvas::GetBounds(void) const
218 {
219         return __bounds;
220 }
221
222 result
223 _GalleryCanvas::SetVisibility(bool visibility)
224 {
225         if (IsVisibility() == visibility)
226         {
227                 return E_SUCCESS;
228         }
229
230         GetImageVisualElement().SetImplicitAnimationEnabled(false);
231         GetImageVisualElement().SetShowState(visibility);
232         GetFrameVisualElement().SetImplicitAnimationEnabled(false);
233         GetFrameVisualElement().SetShowState(visibility);
234
235         return E_SUCCESS;
236 }
237
238 bool
239 _GalleryCanvas::IsVisibility(void) const
240 {
241         return GetImageVisualElement().GetShowState();
242 }
243
244 result
245 _GalleryCanvas::RefreshCanvas(void)
246 {
247         return GetImageVisualElement().InvalidateRectangle(null);
248 }
249
250 result
251 _GalleryCanvas::LoadImage(void)
252 {
253         Canvas* pCanvas = null;
254         result r = E_SUCCESS;
255         Bitmap* pScaledImage = null;
256
257         if (IsNeedUpdateImage() == true)
258         {
259                 _GalleryBitmap* pImage = GetImage();
260                 if (pImage != null)
261                 {
262                         FloatRectangle rect = GetImageVisualElementBounds();
263                         rect.x = 0.0;
264                         rect.y = 0.0;
265                         pCanvas = GetImageVisualElement().GetCanvasN();
266                         r = GetLastResult();
267                         SysTryCatch(NID_UI_CTRL, pCanvas != null, , r, "[%s] Propagating.", GetErrorMessage(r));
268
269                         pCanvas->SetBackgroundColor(__pRootCanvas->GetBackgroundColor());
270                         r = pCanvas->Clear();
271                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
272
273                         pScaledImage = ScaledBitmapN(pImage->GetInternalBitmap(), Dimension(rect.width, rect.height));
274                         r = GetLastResult();
275                         SysTryCatch(NID_UI_CTRL, pScaledImage != null, , r, "[%s] Propagating.", GetErrorMessage(r));
276
277                         r = pCanvas->DrawBitmap(Point(0,0), *pScaledImage);
278                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
279
280                         r = GetImageVisualElement().SetFlushNeeded();
281                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
282
283                         delete pScaledImage;
284                         delete pCanvas;
285                 }
286                 SetNeedUpdateImage(false);
287         }
288
289         return E_SUCCESS;
290
291 CATCH:
292         delete pScaledImage;
293         delete pCanvas;
294         return r;
295 }
296
297 _GalleryCanvas::_GalleryCanvas(void)
298         : __pImageVisualElement(null)
299         , __pFrameVisualElement(null)
300         , __pAnimation(null)
301         , __bounds(0, 0, 0, 0)
302         , __pImage(null)
303         , __visableRegionOfImage(0, 0, 0, 0)
304         , __needUpdateImage(false)
305         , __visualElementBoundsChanged(false)
306         , __imageBounds(0, 0, 0, 0)
307         , __originalImageBounds(0, 0, 0, 0)
308         , __pRootCanvas(null)
309         , __factor(1.0)
310         , __imageAnchor(0, 0)
311         , __imageVerticalAlignment(GALLERY_VERTICAL_ALIGN_MIDDLE)
312         , __imageHorizontalAlignment(GALLERY_HORIZONTAL_ALIGN_CENTER)
313 {
314         // Do nothing
315 }
316
317 _GalleryCanvas::~_GalleryCanvas(void)
318 {
319         GetImageVisualElement().RemoveAllAnimations();
320
321         delete __pImage;
322         __pImage = null;
323
324         if (__pImageVisualElement != null)
325         {
326                 __pImageVisualElement->Destroy();
327         }
328
329         if (__pFrameVisualElement != null)
330         {
331                 __pFrameVisualElement->Destroy();
332         }
333 }
334
335 result
336 _GalleryCanvas::Construct(FloatRectangle& canvasBounds)
337 {
338         result r = E_SUCCESS;
339         _VisualElement* pImageVisualElement = null;
340         _VisualElement* pFrameVisualElement = null;
341
342         pImageVisualElement = new (std::nothrow) _VisualElement();
343         SysTryCatch(NID_UI_CTRL, pImageVisualElement != null, , E_OUT_OF_MEMORY,
344                                         "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _VisualElement.");
345
346         r = pImageVisualElement->Construct();
347         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
348
349         pImageVisualElement->SetImplicitAnimationEnabled(false);
350         pImageVisualElement->SetBounds(canvasBounds);
351
352         pImageVisualElement->SetShowState(true);
353
354         r = pImageVisualElement->SetSurfaceOpaque(false);
355         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
356
357         SetImageVisualElement(*pImageVisualElement);
358
359         pFrameVisualElement = new (std::nothrow) _VisualElement();
360         SysTryCatch(NID_UI_CTRL, pFrameVisualElement != null, , E_OUT_OF_MEMORY,
361                                         "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _VisualElement.");
362         r = pFrameVisualElement->Construct();
363         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
364
365         pFrameVisualElement->SetImplicitAnimationEnabled(false);
366         pFrameVisualElement->SetBounds(canvasBounds);
367
368         pFrameVisualElement->SetShowState(true);
369
370         SetFrameVisualElement(*pFrameVisualElement);
371
372         r = GetFrameVisualElement().AttachChild(GetImageVisualElement());
373         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
374
375         GetFrameVisualElement().SetClipChildrenEnabled(true);
376
377         return E_SUCCESS;
378
379 CATCH:
380         if (pImageVisualElement != null)
381         {
382                 pImageVisualElement->Destroy();
383         }
384
385         if (pFrameVisualElement != null)
386         {
387                 pFrameVisualElement->Destroy();
388         }
389         __pImageVisualElement = null;
390         __pFrameVisualElement = null;
391
392         return r;
393 }
394
395 void
396 _GalleryCanvas::SetImageVisualElement(_VisualElement& visualElement)
397 {
398         __pImageVisualElement = &visualElement;
399         return;
400 }
401
402 _VisualElement&
403 _GalleryCanvas::GetImageVisualElement(void) const
404 {
405         return *__pImageVisualElement;
406 }
407
408 void
409 _GalleryCanvas::SetFrameVisualElement(_VisualElement& visualElement)
410 {
411         __pFrameVisualElement = &visualElement;
412         return;
413 }
414
415 _VisualElement&
416 _GalleryCanvas::GetFrameVisualElement(void) const
417 {
418         return *__pFrameVisualElement;
419 }
420
421 void
422 _GalleryCanvas::SetAnimation(VisualElementPropertyAnimation& animation)
423 {
424         __pAnimation = &animation;
425         return;
426 }
427
428 VisualElementPropertyAnimation&
429 _GalleryCanvas::GetAnimation(void) const
430 {
431         return *__pAnimation;
432 }
433
434 void
435 _GalleryCanvas::SetNeedUpdateImage(bool needUpdate)
436 {
437         __needUpdateImage = needUpdate;
438         return;
439 }
440
441 bool
442 _GalleryCanvas::IsNeedUpdateImage(void) const
443 {
444         return __needUpdateImage;
445 }
446
447 void
448 _GalleryCanvas::SetCanvasFactor(float factor)
449 {
450         __factor = factor;
451         return;
452 }
453
454 float
455 _GalleryCanvas::GetCanvasFactor(void) const
456 {
457         return __factor;
458 }
459
460 result
461 _GalleryCanvas::SetVisualElementBounds(const FloatRectangle& bounds)
462 {
463         if (GetImageVisualElementBounds() == bounds)
464         {
465                 return E_SUCCESS;
466         }
467
468         __visualElementBoundsChanged = true;
469         return GetImageVisualElement().SetBounds(bounds);
470 }
471
472 FloatRectangle
473 _GalleryCanvas::GetImageVisualElementBounds(void) const
474 {
475         return GetImageVisualElement().GetBounds();
476 }
477
478 void
479 _GalleryCanvas::SetImageBounds(const FloatRectangle& canvasBounds)
480 {
481         float factor = GetCanvasFactor();
482         if (__visualElementBoundsChanged == true)
483         {
484                 factor = 1.0f;
485         }
486         __imageBounds.width = __originalImageBounds.width * factor;
487         __imageBounds.height = __originalImageBounds.height * factor;
488
489         if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_MIDDLE)
490         {
491                 __imageBounds.y = (canvasBounds.height - __imageBounds.height) / 2;
492         }
493         else if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_BOTTOM)
494         {
495                 __imageBounds.y = (canvasBounds.height - __imageBounds.height);
496         }
497         if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_CENTER)
498         {
499                 __imageBounds.x = (canvasBounds.width - __imageBounds.width) / 2;
500         }
501         else if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_RIGHT)
502         {
503                 __imageBounds.x = (canvasBounds.width - __imageBounds.width);
504         }
505
506         return;
507 }
508
509 FloatRectangle
510 _GalleryCanvas::GetOriginalImageBounds(void) const
511 {
512         return __originalImageBounds;
513 }
514
515 FloatRectangle
516 _GalleryCanvas::GetImageBounds(void) const
517 {
518         return __imageBounds;
519 }
520
521 result
522 _GalleryCanvas::SetAnimationDelegator(_GalleryAnimationProvider* pVEDelegator)
523 {
524         if (GetImageVisualElement().GetAnimationProvider() == pVEDelegator
525                 && GetFrameVisualElement().GetAnimationProvider() == pVEDelegator)
526         {
527                 return E_SUCCESS;
528         }
529
530         result r = GetImageVisualElement().SetAnimationProvider(pVEDelegator);
531         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
532
533         r = GetFrameVisualElement().SetAnimationProvider(pVEDelegator);
534         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
535
536         return E_SUCCESS;
537 }
538
539 void
540 _GalleryCanvas::SetRootCanvas(_GalleryRootCanvas& rootCanvas)
541 {
542         __pRootCanvas = &rootCanvas;
543         return;
544 }
545
546 void
547 _GalleryCanvas::SetImageAnchor(Tizen::Graphics::FloatPoint anchor)
548 {
549         __imageAnchor = anchor;
550         return;
551 }
552
553 FloatPoint
554 _GalleryCanvas::GetImageAnchor(void) const
555 {
556         return __imageAnchor;
557 }
558
559 FloatRectangle
560 _GalleryCanvas::GetAlignBoundary(void) const
561 {
562         FloatRectangle imageBounds = GetImageBounds();
563         if (__originalImageBounds.width != imageBounds.width || __originalImageBounds.height != imageBounds.height)
564         {
565                 imageBounds.x += GetBounds().x;
566                 imageBounds.y += GetBounds().y;
567                 return imageBounds;
568         }
569         else
570         {
571                 return FloatRectangle(-1, -1, -1, -1);
572         }
573 }
574
575 void
576 _GalleryCanvas::SetClippingOfFrameVisualElement(bool clipping)
577 {
578         GetFrameVisualElement().SetClipChildrenEnabled(clipping);
579 }
580
581 void
582 _GalleryCanvas::SetImageAlignment(_GalleryVerticalAlignment imageVerticalAlignment
583                                                                 , _GalleryHorizontalAlignment imageHorizontalAlignment)
584 {
585         __imageVerticalAlignment = imageVerticalAlignment;
586         __imageHorizontalAlignment = imageHorizontalAlignment;
587 }
588
589 void
590 _GalleryCanvas::GetImageAlignment(_GalleryVerticalAlignment& imageVerticalAlignment
591                                                                 , _GalleryHorizontalAlignment& imageHorizontalAlignment) const
592 {
593         imageVerticalAlignment = __imageVerticalAlignment;
594         imageHorizontalAlignment = __imageHorizontalAlignment;
595 }
596
597 FloatDimension
598 _GalleryCanvas::GetScaledBitmapSize(_GalleryBitmap& galleryBitmap, const FloatDimension& size, _GalleryFittingType fittingType) const
599 {
600         FloatDimension scaledSize = size;
601         if (fittingType == GALLERY_FITTING_TYPE_NONE)
602         {
603                 return scaledSize;
604         }
605
606         if (fittingType != GALLERY_FITTING_TYPE_FIT_XY)
607         {
608                 float width = galleryBitmap.GetSize().width;
609                 float height = galleryBitmap.GetSize().height;
610
611                 float widthFactor = size.width / width;
612                 float heightFactor = size.height / height;
613
614                 if (fittingType == GALLERY_FITTING_TYPE_STRECTABLE)
615                 {
616                         if (widthFactor < 1.0f && heightFactor < 1.0f)
617                         {
618                                 return scaledSize;
619                         }
620                 }
621                 else if (fittingType == GALLERY_FITTING_TYPE_SHRINKABLE)
622                 {
623                         if (widthFactor > 1.0f && heightFactor > 1.0f)
624                         {
625                                 return scaledSize;
626                         }
627                 }
628
629                 if (widthFactor > 1.0f && heightFactor > 1.0f)
630                 {
631                         scaledSize.SetSize(width, height);
632                 }
633                 else
634                 {
635                         if (widthFactor > heightFactor)
636                         {
637                                 scaledSize.width = width * heightFactor;
638                         }
639                         else
640                         {
641                                 scaledSize.height = height * widthFactor;
642                         }
643                 }
644         }
645         return scaledSize;
646 }
647
648 Bitmap*
649 _GalleryCanvas::ScaledBitmapN(Bitmap* pSourceBitmap, Dimension scaledSize)
650 {
651         Bitmap* pScaledBitmap = new (std::nothrow) Bitmap();
652         SysTryReturn(NID_UI_CTRL, pScaledBitmap != null, null, E_OUT_OF_MEMORY,
653                                         "[E_OUT_OF_MEMORY] Failed to allocate the memory for the Bitmap.");
654
655         FloatRectangle rect(0, 0, pSourceBitmap->GetWidthF(), pSourceBitmap->GetHeightF());
656         int share = 1;
657         int widthShare = rect.width / scaledSize.width;
658         int heightShare = rect.height / scaledSize.height;
659         int power = 0;
660         Dimension realScaleSize = scaledSize;
661
662         result r = pScaledBitmap->Construct(*pSourceBitmap, rect);
663         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
664
665         if (widthShare > heightShare)
666         {
667                 share = widthShare;
668         }
669         else
670         {
671                 share = heightShare;
672         }
673
674
675         while (share > pow(2, (power + 1)))
676         {
677                 power++;
678         }
679
680         r = pScaledBitmap->SetScalingQuality(BITMAP_SCALING_QUALITY_HIGH);
681         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
682
683         for (; power >= 0; power--)
684         {
685                 realScaleSize.width = scaledSize.width * pow(2, power);
686                 realScaleSize.height = scaledSize.height * pow(2, power);
687                 r = pScaledBitmap->Scale(realScaleSize);
688                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
689         }
690
691         return pScaledBitmap;
692
693 CATCH:
694         delete pScaledBitmap;
695
696         return null;
697 }
698
699
700 // _GalleryRootCanvas
701 _GalleryRootCanvas*
702 _GalleryRootCanvas::CreateGalleryRootCanvasN(_Control& control)
703 {
704         _GalleryRootCanvas* pRootCanvas = new (std::nothrow) _GalleryRootCanvas(control);
705         SysTryReturn(NID_UI_CTRL, pRootCanvas != null, null, E_OUT_OF_MEMORY,
706                                         "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _GalleryRootCanvas.");
707
708         SetLastResult(E_SUCCESS);
709
710         return pRootCanvas;
711 }
712
713 result
714 _GalleryRootCanvas::SetBackgroundImage(const _GalleryBitmap* pImage)
715 {
716         __pBackgroundImage = const_cast <_GalleryBitmap*>(pImage);
717         return E_SUCCESS;
718 }
719
720 _GalleryBitmap*
721 _GalleryRootCanvas::GetBackgroundImage(void) const
722 {
723         return __pBackgroundImage;
724 }
725
726 Color
727 _GalleryRootCanvas::GetBackgroundColor(void) const
728 {
729         return GetControl().GetBackgroundColor();
730 }
731
732 result
733 _GalleryRootCanvas::SetBounds(const FloatRectangle& bounds)
734 {
735         if (GetBounds() == bounds)
736         {
737                 return E_SUCCESS;
738         }
739
740         return GetControl().SetBounds(bounds);
741 }
742
743 FloatRectangle
744 _GalleryRootCanvas::GetBounds(void) const
745 {
746         return GetControl().GetBoundsF();
747 }
748
749 FloatDimension
750 _GalleryRootCanvas::GetControlSize(void) const
751 {
752         return GetControl().GetSizeF();
753 }
754
755 result
756 _GalleryRootCanvas::SetVisibility(bool visibility)
757 {
758         if (IsVisibility() == visibility)
759         {
760                 return E_SUCCESS;
761         }
762
763         GetControl().SetVisibleState(visibility);
764
765         return E_SUCCESS;
766 }
767
768 bool
769 _GalleryRootCanvas::IsVisibility(void) const
770 {
771         return GetControl().GetVisibleState();
772 }
773
774 result
775 _GalleryRootCanvas::AddChildCanvas(_GalleryCanvas& canvas)
776 {
777         _VisualElement& childVisualElement = canvas.GetFrameVisualElement();
778         _VisualElement* pRootVisualElement = GetImageVisualElement();
779         SysTryReturn(NID_UI_CTRL, pRootVisualElement != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Root VisualElement is null.");
780
781         result r = pRootVisualElement->AttachChild(childVisualElement);
782         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
783
784         canvas.SetRootCanvas(*this);
785
786         return E_SUCCESS;
787 }
788
789 result
790 _GalleryRootCanvas::RemoveChildCanvas(_GalleryCanvas& canvas)
791 {
792         _VisualElement& childVisualElement = canvas.GetFrameVisualElement();
793         _VisualElement* pRootVisualElement = GetImageVisualElement();
794         SysTryReturn(NID_UI_CTRL, pRootVisualElement != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Root VisualElement is null.");
795
796         result r = pRootVisualElement->DetachChild(childVisualElement);
797         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
798
799         return E_SUCCESS;
800 }
801
802 result
803 _GalleryRootCanvas::RefreshCanvas(void)
804 {
805         GetControl().Invalidate();
806         return E_SUCCESS;
807 }
808
809 _GalleryRootCanvas::_GalleryRootCanvas(_Control& control)
810         : __pControl(null)
811         , __bounds(0, 0, 0, 0)
812         , __pBackgroundImage(null)
813         , __needUpdateBackgroundImage(false)
814 {
815         SetControl(control);
816 }
817
818 _GalleryRootCanvas::~_GalleryRootCanvas(void)
819 {
820         // Do nothing
821 }
822
823 void
824 _GalleryRootCanvas::SetControl(_Control& control)
825 {
826         __pControl = &control;
827         return;
828 }
829
830 _Control&
831 _GalleryRootCanvas::GetControl(void) const
832 {
833         return *__pControl;
834 }
835
836 _VisualElement*
837 _GalleryRootCanvas::GetImageVisualElement(void) const
838 {
839         return GetControl().GetVisualElement();
840 }
841
842 }}} // Tizen::Ui::Controls