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