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