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