fixed bug (TapGesture improvement)
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_GalleryPresenter.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 <FBaseErrors.h>
18 #include <FBaseRtTimer.h>
19 #include <FBaseSysLog.h>
20 #include "FUi_Math.h"
21 #include "FUi_ResourceManager.h"
22 #include "FUiCtrl_Gallery.h"
23 #include "FUiCtrl_GalleryPresenter.h"
24 #include "FUiCtrl_GalleryModel.h"
25 #include "FUiCtrl_GalleryItem.h"
26 #include "FUiCtrl_GalleryItemProvider.h"
27 #include "FUiCtrl_GalleryCoreEvent.h"
28 #include "FUiCtrl_GalleryCoreEventArg.h"
29 #include "FUiCtrl_GalleryImageReader.h"
30 #include "FUiCtrl_GalleryViewEventHandler.h"
31 #include "FUiCtrl_IGalleryCoreEventListener.h"
32 #include "FUiCtrl_IGalleryRenderer.h"
33 #include "FUiCtrl_GalleryBitmap.h"
34 #include "FUiCtrl_GalleryViewEvent.h"
35 #include "FUiAnim_VisualElement.h"
36
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Ui;
41
42 namespace Tizen { namespace Ui { namespace Controls {
43
44 namespace
45 {
46
47 const int DEFAULT_ITEM_SPACING = 30;
48
49 const int DEFAULT_ANIMATION_DURATION = 3000;
50 const int MIN_ANIMATION_DURATION = 300;
51 const int MAX_ANIMATION_DURATION = 20000;
52 const int DEFAULT_VIEW_DURATION = 10;
53 const int MIN_VIEW_DURATION = 10;
54 const int FIRST_SLIDE_SHOW_DURATION = 1;
55
56 const int PARTIAL_CANVAS = 0;
57 } // unnamed namespace
58
59 IMPLEMENT_PROPERTY(_GalleryPresenter);
60
61 _GalleryPresenter::_GalleryPresenter(_IGalleryRenderer& galleryRenderer)
62         : __galleryRenderer(galleryRenderer)
63         , __pGalleryViewEventHandler(null)
64         , __pGalleryCoreEvent(null)
65         , __pGalleryItemProvider(null)
66         , __pGalleryModel(null)
67         , __pGalleryImageReader(null)
68         , __pEmptyGalleryImage(null)
69         , __emptyText(L"")
70         , __slideShowAnimationDuration(DEFAULT_ANIMATION_DURATION)
71         , __slideShowViewDuration(DEFAULT_VIEW_DURATION)
72         , __gallerySlideShowType(GALLERY_SLIDESHOW_DISSOLVE)
73         , __currentItemIndex(NO_CURRENT_IMAGE)
74         , __slideShowRepeat(false)
75         , __slideShowStarted(false)
76         , __zoomingEnabled(true)
77         , __pSlideShowTimer(null)
78         , __slideShowPlayCount(0)
79         , __itemSpacing(DEFAULT_ITEM_SPACING)
80         , __pItemToCanvas(null)
81         , __fittingType(GALLERY_FITTING_TYPE_FIT)
82         , __verticalAlignment(GALLERY_VERTICAL_ALIGN_MIDDLE)
83         , __horizontalAlignment(GALLERY_HORIZONTAL_ALIGN_CENTER)
84         , __emptyFontSize(0)
85         , __emptyFontStyle(FONT_STYLE_PLAIN)
86         , __pGalleryView(null)
87 {
88         AddPropertyChangeEventListener(*this);
89 }
90
91 _GalleryPresenter::~_GalleryPresenter(void)
92 {
93         delete __pGalleryViewEventHandler;
94         delete __pGalleryImageReader;
95         delete __pGalleryModel;
96         delete __pGalleryItemProvider;
97         delete __pGalleryCoreEvent;
98         delete __pEmptyGalleryImage;
99         delete[] __pItemToCanvas;
100         delete __pSlideShowTimer;
101         __pSlideShowTimer = null;
102 }
103
104 _GalleryPresenter*
105 _GalleryPresenter::CreateGalleryPresenterN(_IGalleryRenderer& galleryRenderer,
106                 _GalleryItemProviderAdaptorImpl* pGalleryItemProviderAdaptor, _Gallery* pGalleryView)
107 {
108         _GalleryPresenter* pPresenter = new (std::nothrow) _GalleryPresenter(galleryRenderer);
109         SysTryReturn(NID_UI_CTRL, pPresenter != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
110
111         result r = pPresenter->Construct(pGalleryItemProviderAdaptor, pGalleryView);
112         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
113
114         SetLastResult(E_SUCCESS);
115         return pPresenter;
116
117 CATCH:
118         delete pPresenter;
119
120         return null;
121 }
122
123 result
124 _GalleryPresenter::Construct(_GalleryItemProviderAdaptorImpl* pGalleryItemProviderAdaptor, _Gallery* pGalleryView)
125 {
126         SetLastResult(E_SUCCESS);
127
128         result r = E_SUCCESS;
129
130         __pGalleryCoreEvent = new (std::nothrow) _GalleryCoreEvent();
131         SysTryCatch(NID_UI_CTRL, __pGalleryCoreEvent != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
132
133         __pGalleryItemProvider = new (std::nothrow) _GalleryItemProvider(*__pGalleryCoreEvent, pGalleryItemProviderAdaptor);
134         SysTryCatch(NID_UI_CTRL, __pGalleryItemProvider != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
135
136         __pGalleryModel = _GalleryModel::CreateGalleryModelN(*__pGalleryItemProvider);
137         SysTryCatch(NID_UI_CTRL, __pGalleryModel != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
138
139         __pGalleryImageReader = new (std::nothrow) _GalleryImageReader(*__pGalleryModel);
140         SysTryCatch(NID_UI_CTRL, __pGalleryImageReader != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
141
142         __pItemToCanvas = new(std::nothrow) int[MAX_CANVAS_COUNT];
143         SysTryCatch(NID_UI_CTRL, __pItemToCanvas != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
144         memset(__pItemToCanvas, -1, sizeof(int) * MAX_CANVAS_COUNT);
145
146         r = __galleryRenderer.SetCanvasMaxCount(MAX_CANVAS_COUNT);
147         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
148
149         r = __galleryRenderer.EnableEmptyView();
150         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
151
152         GET_STRING_CONFIG(IDS_COM_BODY_NO_IMAGES, __emptyText);
153         r = __galleryRenderer.SetEmptyText(__emptyText);
154         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
155
156         __pGalleryViewEventHandler = new (std::nothrow) _GalleryViewEventHandler(*this, __galleryRenderer, *__pGalleryImageReader);
157         SysTryCatch(NID_UI_CTRL, __pGalleryViewEventHandler != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
158
159         r = __pGalleryViewEventHandler->Construct();
160         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
161
162         SetFontStyle(FONT_STYLE_PLAIN);
163         SetFontSize(32);
164
165         __pGalleryView = pGalleryView;
166
167         return E_SUCCESS;
168
169 CATCH:
170         delete __pGalleryViewEventHandler;
171         delete __pGalleryImageReader;
172         delete __pGalleryModel;
173         delete __pGalleryItemProvider;
174         delete __pGalleryCoreEvent;
175         delete[] __pItemToCanvas;
176
177         return r;
178 }
179
180 int
181 _GalleryPresenter::GetCurrentItemIndex(void) const
182 {
183         return __currentItemIndex;
184 }
185
186 result
187 _GalleryPresenter::SetCurrentItemIndex(int index, bool eventFire)
188 {
189         SysTryReturn(NID_UI_CTRL, index < __pGalleryModel->GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
190                         "[E_OUT_OF_RANGE] The argument(%d) is out of range.", index);
191
192         if (__currentItemIndex != NO_CURRENT_IMAGE && __currentItemIndex != index)
193         {
194                 __currentItemIndex = index;
195                 if (eventFire == true)
196                 {
197                         ChangedItem();
198                 }
199         }
200         else
201         {
202                 __currentItemIndex = index;
203         }
204
205         __pGalleryViewEventHandler->SetZoomFlag(false);
206
207         return E_SUCCESS;
208 }
209
210 result
211 _GalleryPresenter::SetCurrentItem(int index)
212 {
213         SysTryReturn(NID_UI_CTRL, index >=0 && index < __pGalleryModel->GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
214                                         "[E_OUT_OF_RANGE] The argument(%d) is out of range.", index);
215
216         int oldCurrentCanvasIndex = 0;
217         result r = E_SUCCESS;
218
219         if (index == __currentItemIndex)
220         {
221                 return E_SUCCESS;
222         }
223
224         if (__currentItemIndex != NO_CURRENT_IMAGE)
225         {
226                 oldCurrentCanvasIndex = SearchCanvasIndex(__currentItemIndex);
227                 SysTryReturn(NID_UI_CTRL, oldCurrentCanvasIndex != NOT_EXIST_CANVAS, E_SYSTEM, E_SYSTEM,
228                                         "[E_SYSTEM] The canvas of %d item is not exist.", oldCurrentCanvasIndex);
229
230                 if (__currentItemIndex > index)
231                 {
232                         FloatRectangle rect = __galleryRenderer.GetViewRect();
233                         rect.x = rect.width;
234                         r = __galleryRenderer.SetCanvasBounds(oldCurrentCanvasIndex, rect);
235                 }
236                 else if (__currentItemIndex < index)
237                 {
238                         FloatRectangle rect = __galleryRenderer.GetViewRect();
239                         rect.x = -rect.width;
240                         r = __galleryRenderer.SetCanvasBounds(oldCurrentCanvasIndex, rect);
241                 }
242
243                 r = __galleryRenderer.SetCanvasVisibility(oldCurrentCanvasIndex, false);
244                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
245         }
246
247         int newCurrentCanvasIndex = SearchCanvasIndex(index);
248         if (newCurrentCanvasIndex == NOT_EXIST_CANVAS)
249         {
250                 r = SetCanvasImage(index);
251                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
252                 newCurrentCanvasIndex = SearchCanvasIndex(index);
253         }
254         r = __galleryRenderer.SetCanvasBounds(newCurrentCanvasIndex, __galleryRenderer.GetViewRect());
255         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
256
257         r = __galleryRenderer.SetCanvasVisibility(newCurrentCanvasIndex, true);
258         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
259
260         r = SetCurrentItemIndex(index);
261         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
262
263         //RefreshView();
264
265         return E_SUCCESS;
266
267 CATCH:
268         result resultValue = __galleryRenderer.SetCanvasVisibility(oldCurrentCanvasIndex, true);
269         if (resultValue != E_SUCCESS)
270         {
271                 SysLog(NID_UI_CTRL, "[%s] Failed to setting of canvas visibility.", GetErrorMessage(resultValue));
272         }
273
274         return r;
275 }
276
277 int
278 _GalleryPresenter::GetItemCount(void) const
279 {
280         return __pGalleryModel->GetItemCount();
281 }
282
283 void
284 _GalleryPresenter::SetItemSpacing(int spacing)
285 {
286         __itemSpacing = spacing;
287 }
288 int
289 _GalleryPresenter::GetItemSpacing(void) const
290 {
291         return __itemSpacing;
292 }
293
294 _GallerySlideShowType
295 _GalleryPresenter::GetSlideShowType(void) const
296 {
297         return __gallerySlideShowType;
298 }
299
300 result
301 _GalleryPresenter::SetSlideShowType(_GallerySlideShowType slideShowType)
302 {
303         SysTryReturn(NID_UI_CTRL, slideShowType >= GALLERY_SLIDESHOW_DISSOLVE && slideShowType <= GALLERY_SLIDESHOW_PAGE,
304                                 E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The animation argument(%d) is out of range", slideShowType);
305
306         __gallerySlideShowType = slideShowType;
307
308         return E_SUCCESS;
309 }
310
311 int
312 _GalleryPresenter::GetSlideShowAnimationDuration(void) const
313 {
314         Variant variant = GetProperty("slideShowAnimationDuration");
315         return variant.ToInt();
316 }
317
318 result
319 _GalleryPresenter::SetSlideShowAnimationDuration(int duration)
320 {
321         SysTryReturn(NID_UI_CTRL, duration >= MIN_ANIMATION_DURATION && duration <= MAX_ANIMATION_DURATION,
322                         E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The argument(%d) is out of range.", duration);
323
324         return SetProperty("slideShowAnimationDuration", Variant(duration));
325 }
326
327 int
328 _GalleryPresenter::GetSlideShowViewDuration(void) const
329 {
330         Variant variant = GetProperty("slideShowViewDuration");
331         return variant.ToInt();
332 }
333
334 result
335 _GalleryPresenter::SetSlideShowViewDuration(int duration)
336 {
337         SysTryReturn(NID_UI_CTRL, duration >= MIN_VIEW_DURATION, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
338                         "[E_OUT_OF_RANGE] The argument(%d) is out of range.", duration);
339
340         return SetProperty("slideShowViewDuration", Variant(duration));
341 }
342
343 result
344 _GalleryPresenter::StartSlideShow(bool repeat)
345 {
346         SysTryReturn(NID_UI_CTRL, __pSlideShowTimer == null, E_INVALID_OPERATION, E_INVALID_OPERATION,
347                         "[E_INVALID_OPERATION] Slide show is already started");
348
349         if (GetItemCount() == 0)
350         {
351                 return E_SUCCESS;
352         }
353
354         __slideShowRepeat = repeat;
355         __slideShowPlayCount = GetItemCount() - 1;
356
357         result r = StartSlideShowTimer(GetSlideShowViewDuration());
358         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
359
360         _GalleryCoreEventArg* pArg = new(std::nothrow) _GalleryCoreEventArg(GALLERY_CORE_EVENT_SLIDE_SHOW_STARTED);
361         __pGalleryCoreEvent->Fire(*pArg);
362
363         return E_SUCCESS;
364 }
365
366 result
367 _GalleryPresenter::StopSlideShow(void)
368 {
369         SysTryReturn(NID_UI_CTRL, __pSlideShowTimer != null, E_INVALID_OPERATION, E_INVALID_OPERATION,
370                         "[E_INVALID_OPERATION] Slide show is already stopped");
371
372         StopSlideShowTimer();
373         __galleryRenderer.StopAllCanvasAnimation();
374
375         result r = __pGalleryViewEventHandler->SetVisibleCanvas();
376         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
377
378         _GalleryCoreEventArg* pArg = new(std::nothrow) _GalleryCoreEventArg(GALLERY_CORE_EVENT_SLIDE_SHOW_STOPPED);
379         __pGalleryCoreEvent->Fire(*pArg);
380
381         r = __galleryRenderer.RefreshView();
382         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
383
384         return E_SUCCESS;
385 }
386
387 String
388 _GalleryPresenter::GetTextOfEmptyGallery(void) const
389 {
390         Variant variant = GetProperty("textOfEmptyGallery");
391         return variant.ToString();
392 }
393
394 result
395 _GalleryPresenter::SetTextOfEmptyGallery(const String& text)
396 {
397         result r = __galleryRenderer.SetEmptyText(text);
398         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
399
400         return SetProperty("textOfEmptyGallery", Variant(text));
401 }
402
403 result
404 _GalleryPresenter::SetBitmapOfEmptyGallery(const _GalleryBitmap* pBitmap)
405 {
406         result r = __galleryRenderer.SetEmptyImage(pBitmap);
407         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
408
409         __pEmptyGalleryImage = const_cast <_GalleryBitmap*>(pBitmap);
410
411         return E_SUCCESS;
412 }
413
414 bool
415 _GalleryPresenter::IsSlideShowStarted(void) const
416 {
417         return __pSlideShowTimer != null ? true : false;
418 }
419
420 result
421 _GalleryPresenter::SetZoomingEnabled(bool enable)
422 {
423         return SetProperty("zoomingEnabled", Variant(enable));
424 }
425
426 bool
427 _GalleryPresenter::IsZoomingEnabled(void) const
428 {
429         Variant variant = GetProperty("zoomingEnabled");
430
431         return variant.ToBool();
432 }
433
434 bool
435 _GalleryPresenter::PostEvent(_GalleryViewEvent& event)
436 {
437         bool slideShowStarted = false;
438         if (__pSlideShowTimer != null && event.GetEventType() != GALLERY_VIEW_EVENT_BOUNDS_CHANGED)
439         {
440                 result r = StopSlideShow();
441                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
442                 slideShowStarted = true;
443         }
444
445         if (GetItemCount() == 0)
446         {
447                 if (event.GetEventType() == GALLERY_VIEW_EVENT_BOUNDS_CHANGED)
448                 {
449                         return OnBoundsChanged();
450                 }
451                 //SetLastResult(E_SUCCESS);
452                 return true;
453         }
454
455         switch (event.GetEventType())
456         {
457         case GALLERY_VIEW_EVENT_TOUCH_PRESSED:
458         {
459                 if (slideShowStarted == true)
460                 {
461                         result r = __pGalleryViewEventHandler->SetVisibleCanvas();
462                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
463                         return true;
464                 }
465                 return __pGalleryViewEventHandler->OnTouchPressed(*(event.GetEventInfo()));
466         }
467
468         case GALLERY_VIEW_EVENT_TOUCH_RELEASED:
469                 return __pGalleryViewEventHandler->OnTouchReleased(*(event.GetEventInfo()));
470
471         case GALLERY_VIEW_EVENT_TOUCH_DOUBLE_PRESSED:
472                 __galleryRenderer.SetCanvasVisibility(PARTIAL_CANVAS, false);
473                 return __pGalleryViewEventHandler->OnTouchDoublePressed(*(event.GetEventInfo()));
474
475         case GALLERY_VIEW_EVENT_TOUCH_MOVED:
476                 __galleryRenderer.SetCanvasVisibility(PARTIAL_CANVAS, false);
477                 return __pGalleryViewEventHandler->OnTouchMoved(*(event.GetEventInfo()));
478
479         case GALLERY_VIEW_EVENT_TOUCH_PINCH_ZOOM:
480                 return __pGalleryViewEventHandler->OnTouchPinchZoom(*(event.GetEventInfo()));
481
482         case GALLERY_VIEW_EVENT_TOUCH_FLICK:
483                 //__galleryRenderer.SetCanvasVisibility(PARTIAL_CANVAS, false);
484                 return __pGalleryViewEventHandler->OnTouchFlicked(*(event.GetEventInfo()));
485
486         case GALLERY_VIEW_EVENT_BOUNDS_CHANGED:
487                 return OnBoundsChanged();
488
489         default:
490                 SysTryReturn(NID_UI_CTRL, false, false, E_INVALID_ARG,
491                                         "[E_INVALID_ARG] The event type(%d) is invalid type", event.GetEventType());
492         }
493
494         //SetLastResult(E_SUCCESS);
495         return true;
496 }
497
498 bool
499 _GalleryPresenter::OnBoundsChanged(void)
500 {
501         int itemIndex = -1;
502         result r = E_SUCCESS;
503
504         if (__pSlideShowTimer != null && GetSlideShowType() == GALLERY_SLIDESHOW_PAGE)
505         {
506                 int slideShowPlayCount = __slideShowPlayCount;
507                 bool slideShowRepeat = __slideShowRepeat;
508                 __galleryRenderer.StopAllCanvasAnimation();
509                 __slideShowPlayCount = slideShowPlayCount;
510                 __slideShowRepeat = slideShowRepeat;
511                 r = StartSlideShowTimer(GetSlideShowViewDuration());
512                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
513         }
514
515         r = __galleryRenderer.InitializeCanvasBounds();
516         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
517
518         if (GetItemCount() > 0)
519         {
520                 for (int canvasIndex = PARTIAL_CANVAS + 1; canvasIndex < MAX_CANVAS_COUNT; canvasIndex++)
521                 {
522                         itemIndex = __pItemToCanvas[canvasIndex];
523                         if (itemIndex != NOT_EXIST_CANVAS)
524                         {
525                                 r = __galleryRenderer.SetCanvasImage(canvasIndex, __pGalleryImageReader->GetItemImage(itemIndex)
526                                                 , __verticalAlignment, __horizontalAlignment, __fittingType);
527                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
528                         }
529                 }
530         }
531         else
532         {
533                 r = __galleryRenderer.EnableEmptyView();
534                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
535                 SetLastResult(E_SUCCESS);
536                 return true;
537         }
538
539         int currentItemIndex = GetCurrentItemIndex();
540         int currentCanvasIndex = SearchCanvasIndex(currentItemIndex);
541         int nextItemIndex = GetCurrentItemIndex() + 1;
542         if (nextItemIndex >= GetItemCount())
543         {
544                 nextItemIndex = 0;
545         }
546         int nextCanvasIndex = SearchCanvasIndex(nextItemIndex);
547
548         r = __galleryRenderer.SetCanvasShowState(currentCanvasIndex, true);
549         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
550
551         if (__pSlideShowTimer != null && __slideShowStarted == true)
552         {
553                 r = __galleryRenderer.SetCanvasShowState(nextCanvasIndex, true);
554                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
555         }
556         else
557         {
558                 __galleryRenderer.GalleryBoundsChanged(currentCanvasIndex);
559                 FloatRectangle canvasBounds = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
560                 FloatRectangle imageBounds = __galleryRenderer.GetCanvasImageBounds(currentCanvasIndex);
561                 __pGalleryViewEventHandler->CorrectCanvasPosition(canvasBounds, imageBounds);
562                 r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, canvasBounds);
563                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
564
565                 __pGalleryViewEventHandler->ResetTouch();
566
567                 r = __pGalleryViewEventHandler->AlignCanvas(false);
568                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
569         }
570
571         SetLastResult(E_SUCCESS);
572
573         return true;
574 }
575
576 void
577 _GalleryPresenter::OnDraw(void)
578 {
579         result r = E_SUCCESS;
580
581         if (GetItemCount() > 0 && GetCurrentItemIndex() == NO_CURRENT_IMAGE)
582         {
583                 r = SetCurrentItem(0);
584                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
585         }
586
587         r = __galleryRenderer.Draw();
588         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
589
590         SetLastResult(E_SUCCESS);
591 }
592
593 result
594 _GalleryPresenter::SetCoreEventListener(const _IGalleryCoreEventListener& coreEventListener)
595 {
596         return __pGalleryCoreEvent->AddListener(coreEventListener);
597 }
598
599 result
600 _GalleryPresenter::RemoveCoreEventListener(const _IGalleryCoreEventListener& coreEventListener)
601 {
602         return __pGalleryCoreEvent->RemoveListener(coreEventListener);
603 }
604
605 bool
606 _GalleryPresenter::ClickedItem(void)
607 {
608         _GalleryCoreEventArg* pArg = new(std::nothrow) _GalleryCoreEventArg(GALLERY_CORE_EVENT_ITEM_CLICKED, __currentItemIndex);
609         return __pGalleryCoreEvent->Fire(*pArg);
610 }
611
612 bool
613 _GalleryPresenter::ChangedItem(void)
614 {
615         if (__currentItemIndex == NO_CURRENT_IMAGE)
616         {
617                 return true;
618         }
619
620         __pGalleryView->ResizeGalleryAccessibilityElement();
621
622         _GalleryCoreEventArg* pArg = new(std::nothrow) _GalleryCoreEventArg(GALLERY_CORE_EVENT_CURRENT_ITEM_CHANGED, __currentItemIndex);
623         return __pGalleryCoreEvent->Fire(*pArg);
624 }
625
626 result
627 _GalleryPresenter::RequestToLoadItem(int itemIndex)
628 {
629         result r = __pGalleryModel->RequestToLoadItem(itemIndex);
630         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
631         SortItemToCanvasIndex(itemIndex, true);
632
633         if (GetItemCount() == 1)
634         {
635                 r = __galleryRenderer.DisableEmptyView();
636                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
637
638                 r = SetCurrentItem(itemIndex);
639                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
640         }
641
642         r = __galleryRenderer.RefreshView();
643         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
644
645         return E_SUCCESS;
646 }
647
648 result
649 _GalleryPresenter::RequestToUnloadItem(int itemIndex)
650 {
651         result r = E_SUCCESS;
652
653         int canvasIndex = SearchCanvasIndex(itemIndex);
654         if (canvasIndex != NOT_EXIST_CANVAS)
655         {
656                 __galleryRenderer.SetCanvasVisibility(canvasIndex, false);
657                 __pItemToCanvas[canvasIndex] = NOT_USED_CANVAS;
658         }
659
660         if (GetItemCount() == 1)
661         {
662                 r = SetCurrentItemIndex(NO_CURRENT_IMAGE);
663                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
664
665                 r = __galleryRenderer.EnableEmptyView();
666                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
667         }
668         else
669         {
670                 if (itemIndex == __currentItemIndex)
671                 {
672                         __pGalleryImageReader->ResetLoadedPartialImageIndex();
673                         r = __galleryRenderer.SetCanvasVisibility(PARTIAL_CANVAS, false);
674                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
675
676                         __pGalleryViewEventHandler->SetZoomFlag(false);
677
678                         int canvasIndex = NOT_EXIST_CANVAS;
679                         if (itemIndex == GetItemCount() - 1)
680                         {
681                                 r = SetCanvasImage(itemIndex - 1);
682                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
683
684                                 canvasIndex = SearchCanvasIndex(itemIndex - 1);
685                                 SysTryReturn(NID_UI_CTRL, canvasIndex != NOT_EXIST_CANVAS, E_SYSTEM, E_SYSTEM,
686                                                         "[E_SYSTEM] Failed to search canvas");
687                                 __currentItemIndex--;
688                         }
689                         else
690                         {
691                                 r = SetCanvasImage(itemIndex + 1);
692                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
693
694                                 canvasIndex = SearchCanvasIndex(itemIndex + 1);
695                                 SysTryReturn(NID_UI_CTRL, canvasIndex != NOT_EXIST_CANVAS, E_SYSTEM, E_SYSTEM,
696                                                         "[E_SYSTEM] Failed to search canvas");
697                         }
698
699                         r = __galleryRenderer.SetCanvasVisibility(canvasIndex, true);
700                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
701
702                         r = __galleryRenderer.SetCanvasBounds(canvasIndex, __galleryRenderer.GetViewRect());
703                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
704
705                         if(ChangedItem() == false)
706                         {
707                                 r = GetLastResult();
708                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
709                         }
710                 }
711                 else if (itemIndex < __currentItemIndex)
712                 {
713                         __currentItemIndex--;
714                 }
715         }
716
717         r = __pGalleryModel->RequestToUnloadItem(itemIndex);
718         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
719
720         SortItemToCanvasIndex(itemIndex, false);
721
722         r = __galleryRenderer.RefreshView();
723         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
724
725         return E_SUCCESS;
726 }
727
728 result
729 _GalleryPresenter::RequestToUnloadAllItems(void)
730 {
731         result r = __pGalleryModel->RequestToUnloadAllItems();
732         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
733
734         for (int i = 0; i < MAX_CANVAS_COUNT; i++)
735         {
736                 __pItemToCanvas[i] = NOT_USED_CANVAS;
737         }
738
739         r = SetCurrentItemIndex(NO_CURRENT_IMAGE);
740         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
741
742         r = __galleryRenderer.EnableEmptyView();
743         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
744
745         //r = __galleryRenderer.RefreshView();
746         //SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
747
748         return E_SUCCESS;
749 }
750
751 result
752 _GalleryPresenter::RequestToUpdateItem(int itemIndex)
753 {
754         result r = __pGalleryModel->RequestToUpdateItem(itemIndex);
755         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
756
757         int canvasIndex = SearchCanvasIndex(itemIndex);
758         if (itemIndex == GetCurrentItemIndex())
759         {
760                 _GalleryBitmap* pImage = __pGalleryImageReader->GetItemImage(itemIndex);
761                 r = GetLastResult();
762                 SysTryReturn(NID_UI_CTRL, pImage != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
763
764                 r = __galleryRenderer.SetCanvasImage(canvasIndex, (const _GalleryBitmap*)pImage
765                                 , __verticalAlignment, __horizontalAlignment, __fittingType);
766                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
767
768                 __galleryRenderer.GalleryBoundsChanged(canvasIndex);
769                 FloatRectangle canvasBounds = __galleryRenderer.GetCanvasBounds(canvasIndex);
770                 FloatRectangle imageBounds = __galleryRenderer.GetCanvasImageBounds(canvasIndex);
771                 __pGalleryViewEventHandler->CorrectCanvasPosition(canvasBounds, imageBounds);
772                 r = __galleryRenderer.SetCanvasBounds(canvasIndex, canvasBounds);
773                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
774
775                 r = __pGalleryViewEventHandler->AlignCanvas(false);
776                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
777         }
778         else if (canvasIndex != NOT_EXIST_CANVAS)
779         {
780                 __pItemToCanvas[canvasIndex] = NOT_USED_CANVAS;
781         }
782
783         r = __galleryRenderer.RefreshView();
784         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
785
786         return E_SUCCESS;
787 }
788
789 result
790 _GalleryPresenter::RequestToUpdateAllItems(void)
791 {
792         result r = __pGalleryModel->RequestToUpdateAllItems();
793         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
794
795         r = __galleryRenderer.ResetAllCanvas();
796         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
797
798         for (int i = 0; i < MAX_CANVAS_COUNT; i++)
799         {
800                 __pItemToCanvas[i] = NOT_USED_CANVAS;
801         }
802
803         int itemCount = GetItemCount();
804         if (itemCount > 0)
805         {
806                 r = __galleryRenderer.DisableEmptyView();
807                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
808
809                 if (GetCurrentItemIndex() != NO_CURRENT_IMAGE)
810                 {
811                         r = SetCanvasImage(GetCurrentItemIndex());
812                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
813
814                         r = __galleryRenderer.SetCanvasVisibility(SearchCanvasIndex(GetCurrentItemIndex()), true);
815                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
816                 }
817                 else
818                 {
819                         r = SetCurrentItem(0);
820                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
821
822                         r = __galleryRenderer.SetCanvasVisibility(SearchCanvasIndex(0), true);
823                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
824                 }
825         }
826         else
827         {
828                 r = __galleryRenderer.EnableEmptyView();
829                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
830         }
831
832         return E_SUCCESS;
833 }
834
835 result
836 _GalleryPresenter::RequestToUpdateItemCount(void)
837 {
838         __pGalleryModel->UpdateItemCount();
839
840         return E_SUCCESS;
841 }
842
843 void
844 _GalleryPresenter::OnTimerExpired(Runtime::Timer& timer)
845 {
846         result r = E_SUCCESS;
847         if (&timer == __pSlideShowTimer)
848         {
849                 r = PlaySlideShow();
850                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
851         }
852         SetLastResult(E_SUCCESS);
853 }
854
855 void
856 _GalleryPresenter::OnTransitionCancel(void)
857 {
858         __slideShowStarted = false;
859         int endItemIndex = __currentItemIndex + 1;
860         if (GetItemCount() > 0)
861         {
862                 endItemIndex = endItemIndex % GetItemCount();
863         }
864         else
865         {
866                 endItemIndex = 0;
867         }
868
869         result r = SetCurrentItemIndex(endItemIndex);
870         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
871
872         StopSlideShowTimer();
873 }
874
875 void
876 _GalleryPresenter::OnTransitionCompleted(void)
877 {
878         int endItemIndex = __currentItemIndex + 1;
879         if (GetItemCount() > 0)
880         {
881                 endItemIndex = endItemIndex % GetItemCount();
882         }
883         else
884         {
885                 endItemIndex = 0;
886         }
887
888         result r = SetCurrentItemIndex(endItemIndex);
889         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
890
891         __slideShowStarted = false;
892         if (__slideShowPlayCount > 0 || __slideShowRepeat == true)
893         {
894                 StartSlideShowTimer(GetSlideShowViewDuration());
895         }
896         else
897         {
898                 r = StopSlideShow();
899                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
900         }
901
902         SetLastResult(E_SUCCESS);
903 }
904
905 void
906 _GalleryPresenter::OnCanvasAnimationCancel(void)
907 {
908         __slideShowStarted = false;
909 }
910
911 void
912 _GalleryPresenter::OnCanvasAnimationCompleted(void)
913 {
914         __slideShowStarted = false;
915 }
916
917 void
918 _GalleryPresenter::OnCanvasAnimationStarted(void)
919 {
920         __slideShowStarted = true;
921 }
922
923 result
924 _GalleryPresenter::SetCanvasImage(int itemIndex)
925 {
926         SysTryReturn(NID_UI_CTRL, itemIndex >= 0, E_INVALID_ARG, E_INVALID_ARG,
927                                         "[E_INVALID_ARG] The argument(%d) is negative value.", itemIndex);
928         SysTryReturn(NID_UI_CTRL, itemIndex >=0 && itemIndex < __pGalleryModel->GetItemCount(),
929                                         E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The argument(%d) is out of range.", itemIndex);
930
931         if (SearchCanvasIndex(itemIndex) != NOT_EXIST_CANVAS)
932         {
933                 return E_SUCCESS;
934         }
935
936         int emptyCanvasIndex = GetEmptyCanvasIndex();
937         if (emptyCanvasIndex == NOT_EXIST_CANVAS)
938         {
939                 emptyCanvasIndex = ClearCanvasIndex(__currentItemIndex);
940         }
941
942         _GalleryBitmap* pImage = __pGalleryImageReader->GetItemImage(itemIndex);
943         result r = GetLastResult();
944         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
945
946         __pItemToCanvas[emptyCanvasIndex] = itemIndex;
947         r = __galleryRenderer.SetCanvasImage(emptyCanvasIndex, (const _GalleryBitmap*)pImage
948                         , __verticalAlignment, __horizontalAlignment, __fittingType);
949         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
950
951         FloatRectangle bounds = __galleryRenderer.GetViewRect();
952         r = __galleryRenderer.SetCanvasBounds(emptyCanvasIndex, bounds);
953         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
954
955         r = __galleryRenderer.SetCanvasVisibility(emptyCanvasIndex, false);
956         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
957
958         return E_SUCCESS;
959 }
960
961 result
962 _GalleryPresenter::SetPartialCanvasImage(void)
963 {
964         SysTryReturn(NID_UI_CTRL, __currentItemIndex >= 0, E_INVALID_ARG, E_INVALID_ARG,
965                                 "[E_INVALID_ARG] The argument(%d) is negative value.", __currentItemIndex);
966
967         _GalleryItem* pGalleryItem = __pGalleryModel->GetItem(__currentItemIndex);
968         if (pGalleryItem->GetGalleryItemFilePath() == L"")
969         {
970                 return E_SUCCESS;
971         }
972
973         int currentCanvasIndex = SearchCanvasIndex(__currentItemIndex);
974
975         FloatRectangle canvasBounds = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
976         FloatRectangle imageBounds = __galleryRenderer.GetCanvasImageBounds(currentCanvasIndex);
977         FloatRectangle viewBounds = __galleryRenderer.GetViewRect();
978
979         if (imageBounds.width < viewBounds.width)
980         {
981                 imageBounds.x -= (viewBounds.width - imageBounds.width) / 2;
982         }
983
984         if (imageBounds.height < viewBounds.height)
985         {
986                 imageBounds.y -= (viewBounds.height - imageBounds.height) / 2;
987         }
988
989         canvasBounds.x = _Abs(-canvasBounds.x - imageBounds.x);
990         canvasBounds.y = _Abs(-canvasBounds.y - imageBounds.y);
991         canvasBounds.width = viewBounds.width < imageBounds.width ? viewBounds.width : imageBounds.width;
992         canvasBounds.height = viewBounds.height < imageBounds.height ? viewBounds.height : imageBounds.height;
993
994         FloatDimension size(imageBounds.width, imageBounds.height);
995
996         _GalleryBitmap* pImage = __pGalleryImageReader->GetPartialImageFromFileN(__currentItemIndex, canvasBounds, size);
997         SysTryReturn(NID_UI_CTRL, pImage != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
998
999         __pItemToCanvas[PARTIAL_CANVAS] = __currentItemIndex;
1000         result r = __galleryRenderer.SetCanvasImage(PARTIAL_CANVAS, (const _GalleryBitmap*)pImage
1001                         , GALLERY_VERTICAL_ALIGN_MIDDLE, GALLERY_HORIZONTAL_ALIGN_CENTER,  __fittingType);
1002         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1003
1004         r = __galleryRenderer.SetCanvasBounds(PARTIAL_CANVAS, viewBounds);
1005         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1006
1007         r = __galleryRenderer.SetCanvasVisibility(PARTIAL_CANVAS, true);
1008         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1009
1010         delete pImage;
1011
1012         return E_SUCCESS;
1013 CATCH:
1014         delete pImage;
1015
1016         return r;
1017 }
1018
1019 int
1020 _GalleryPresenter::SearchCanvasIndex(int itemIndex)
1021 {
1022         if (itemIndex == NO_CURRENT_IMAGE)
1023         {
1024                 return NOT_EXIST_CANVAS;
1025         }
1026
1027         for (int i = 1; i < MAX_CANVAS_COUNT; i++)
1028         {
1029                 if (__pItemToCanvas[i] == itemIndex)
1030                 {
1031                         return i;
1032                 }
1033         }
1034
1035         return NOT_EXIST_CANVAS;
1036 }
1037
1038 int
1039 _GalleryPresenter::GetEmptyCanvasIndex(void) const
1040 {
1041         for (int i = 1; i < MAX_CANVAS_COUNT; i++)
1042         {
1043                 if (__pItemToCanvas[i] == NOT_USED_CANVAS)
1044                 {
1045                         return i;
1046                 }
1047         }
1048         return NOT_EXIST_CANVAS;
1049 }
1050
1051 result
1052 _GalleryPresenter::SetCanvasIndex(int canvasIndex, int itemIndex)
1053 {
1054         SysTryReturn(NID_UI_CTRL, canvasIndex >= 0, E_INVALID_ARG, E_INVALID_ARG,
1055                                         "[E_INVALID_ARG] The argument(%d) is negative value.", canvasIndex);
1056         SysTryReturn(NID_UI_CTRL, canvasIndex > 0 && canvasIndex < MAX_CANVAS_COUNT, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
1057                                         "[E_OUT_OF_RANGE] The canvas index(%d) is out of range", canvasIndex);
1058         SysTryReturn(NID_UI_CTRL, itemIndex >= 0, E_INVALID_ARG, E_INVALID_ARG,
1059                                         "[E_INVALID_ARG] The argument(%d) is negative value.", itemIndex);
1060         SysTryReturn(NID_UI_CTRL, itemIndex >=0 && itemIndex < __pGalleryModel->GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
1061                         "[E_OUT_OF_RANGE] The argument(%d) is out of range.", itemIndex);
1062
1063         if (__pItemToCanvas[canvasIndex] == NOT_USED_CANVAS)
1064         {
1065                 __pItemToCanvas[canvasIndex] = itemIndex;
1066         }
1067         else
1068         {
1069                 SysTryReturn(NID_UI_CTRL, false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The canvas index(%d) is used.", canvasIndex);
1070         }
1071
1072         return E_SUCCESS;
1073 }
1074
1075 int
1076 _GalleryPresenter::ClearCanvasIndex(int currentItemIndex)
1077 {
1078         SysTryReturn(NID_UI_CTRL, currentItemIndex >= 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument(%d) is negative value.", currentItemIndex);
1079
1080         int clearCanvasIndex = 1;
1081         int maxAbsValue = 0;
1082         int absValue = 0;
1083
1084         for (int i = 1; i < MAX_CANVAS_COUNT; i++)
1085         {
1086                 absValue = _Abs(currentItemIndex - __pItemToCanvas[i]);
1087                 if (absValue > maxAbsValue)
1088                 {
1089                         maxAbsValue = absValue;
1090                         clearCanvasIndex = i;
1091                 }
1092         }
1093         __pItemToCanvas[clearCanvasIndex] = NOT_USED_CANVAS;
1094
1095         SetLastResult(E_SUCCESS);
1096         return clearCanvasIndex;
1097 }
1098
1099 void
1100 _GalleryPresenter::SortItemToCanvasIndex(int itemIndex, bool add)
1101 {
1102         if (add == true)
1103         {
1104                 for (int i = 0; i < MAX_CANVAS_COUNT; i++)
1105                 {
1106                         if (__pItemToCanvas[i] >= itemIndex)
1107                         {
1108                                 __pItemToCanvas[i]++;
1109                         }
1110                 }
1111         }
1112         else
1113         {
1114                 for (int i = 0; i < MAX_CANVAS_COUNT; i++)
1115                 {
1116                         if (__pItemToCanvas[i] > itemIndex)
1117                         {
1118                                 __pItemToCanvas[i]--;
1119                         }
1120                 }
1121         }
1122 }
1123
1124 result
1125 _GalleryPresenter::StartSlideShowTimer(int duration)
1126 {
1127         result r = E_SUCCESS;
1128         if (__pSlideShowTimer != null)
1129         {
1130                 delete __pSlideShowTimer;
1131                 __pSlideShowTimer = null;
1132         }
1133
1134         __pSlideShowTimer = new(std::nothrow) Runtime::Timer();
1135         SysTryCatch(NID_UI_CTRL, __pSlideShowTimer != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1136
1137         r = __pSlideShowTimer->Construct(*this);
1138         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1139
1140         __pSlideShowTimer->Start(duration);
1141         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1142
1143         return E_SUCCESS;
1144
1145 CATCH:
1146         delete __pSlideShowTimer;
1147         __pSlideShowTimer = null;
1148
1149         return GetLastResult();
1150 }
1151
1152 void
1153 _GalleryPresenter::StopSlideShowTimer(void)
1154 {
1155         delete __pSlideShowTimer;
1156         __pSlideShowTimer = null;
1157
1158         __slideShowRepeat = false;
1159         __slideShowPlayCount = 0;
1160 }
1161
1162 result
1163 _GalleryPresenter::PlaySlideShow(void)
1164 {
1165         int startCanvasIndex = SearchCanvasIndex(__currentItemIndex);
1166         SysTryReturn(NID_UI_CTRL, startCanvasIndex != NOT_EXIST_CANVAS, E_SYSTEM, E_SYSTEM,
1167                                 "[E_SYSTEM] The current item not exist.");
1168
1169         int endItemIndex = __currentItemIndex + 1;
1170         if (endItemIndex >= GetItemCount())
1171         {
1172                 endItemIndex = 0;
1173         }
1174
1175         result r = __galleryRenderer.SetCanvasVisibility(PARTIAL_CANVAS, false);
1176         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1177
1178         r = __galleryRenderer.SetCanvasVisibility(startCanvasIndex, true);
1179         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1180
1181         int endCanvasIndex = SearchCanvasIndex(endItemIndex);
1182         if (endCanvasIndex == NOT_EXIST_CANVAS)
1183         {
1184                 r = SetCanvasImage(endItemIndex);
1185                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1186                 endCanvasIndex = SearchCanvasIndex(endItemIndex);
1187         }
1188
1189         _GalleryTransitionType transition = static_cast<_GalleryTransitionType>(GetSlideShowType());
1190         _GalleryAnimationTiming animation = {__slideShowAnimationDuration, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
1191         r = __galleryRenderer.SetCanvasVisibility(endCanvasIndex, true);
1192         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1193
1194         r = __galleryRenderer.RunCanvasTransition(startCanvasIndex, endCanvasIndex, transition, &animation);
1195         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1196
1197         if (__slideShowRepeat == false)
1198         {
1199                 __slideShowPlayCount--;
1200         }
1201
1202         r = __galleryRenderer.RefreshView();
1203         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1204
1205         return E_SUCCESS;
1206 }
1207
1208 // property access
1209 Variant
1210 _GalleryPresenter::GetPropertySlideShowViewDuration(void) const
1211 {
1212         return Variant(__slideShowViewDuration);
1213 }
1214
1215 result
1216 _GalleryPresenter::SetPropertySlideShowViewDuration(const Variant& variant)
1217 {
1218         __slideShowViewDuration = variant.ToInt();
1219
1220         return E_SUCCESS;
1221 }
1222
1223 Variant
1224 _GalleryPresenter::GetPropertySlideShowAnimationDuration(void) const
1225 {
1226         return Variant(__slideShowAnimationDuration);
1227 }
1228
1229 result
1230 _GalleryPresenter::SetPropertySlideShowAnimationDuration(const Variant& variant)
1231 {
1232         __slideShowAnimationDuration = variant.ToInt();
1233
1234         return E_SUCCESS;
1235 }
1236
1237 Variant
1238 _GalleryPresenter::GetPropertyTextOfEmptyGallery(void) const
1239 {
1240         return Variant(__emptyText);
1241 }
1242
1243 result
1244 _GalleryPresenter::SetPropertyTextOfEmptyGallery(const Variant& variant)
1245 {
1246         __emptyText = variant.ToString();
1247
1248         return E_SUCCESS;
1249 }
1250
1251 Variant
1252 _GalleryPresenter::GetPropertyZoomingEnabled(void) const
1253 {
1254         return Variant(__zoomingEnabled);
1255 }
1256
1257 result
1258 _GalleryPresenter::SetPropertyZoomingEnabled(const Variant& variant)
1259 {
1260         __zoomingEnabled = variant.ToBool();
1261
1262         return E_SUCCESS;
1263 }
1264
1265 void
1266 _GalleryPresenter::OnPropertyChanging(_PropertyBase& source, const String& name,
1267                                                                                                                         const Variant& oldProperty, const Variant& newProperty)
1268 {
1269 }
1270
1271 void
1272 _GalleryPresenter::OnPropertyChanged(_PropertyBase& source, const String& name,
1273                                                                                                                         const Variant& oldProperty, const Variant& newProperty)
1274 {
1275 }
1276
1277 void
1278 _GalleryPresenter::SetFontSize(int size)
1279 {
1280         __emptyFontSize = size;
1281 }
1282
1283 int
1284 _GalleryPresenter::GetFontSize(void) const
1285 {
1286         return __emptyFontSize;
1287 }
1288
1289 void
1290 _GalleryPresenter::SetFontStyle(FontStyle style)
1291 {
1292         __emptyFontStyle = style;
1293 }
1294
1295 FontStyle
1296 _GalleryPresenter::GetFontStyle(void) const
1297 {
1298         return __emptyFontStyle;
1299 }
1300
1301 void
1302 _GalleryPresenter::OnFontInfoRequested(unsigned long& style, int& size)
1303 {
1304         style = GetFontStyle();
1305         size = GetFontSize();
1306 }
1307
1308 void
1309 _GalleryPresenter::OnFontChanged(void)
1310 {
1311         if (GetItemCount() == 0)
1312         {
1313                 result r = __galleryRenderer.EnableEmptyView();
1314                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1315         }
1316 }
1317
1318 }}} // Tizen::Ui::Controls