Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_GalleryViewEventHandler.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <FGrpDimension.h>
19 #include <FBaseSysLog.h>
20 #include "FUiCtrl_GalleryImageReader.h"
21 #include "FUiCtrl_GalleryPresenter.h"
22 #include "FUiCtrl_GalleryViewEventInfo.h"
23 #include "FUiCtrl_GalleryViewEventHandler.h"
24 #include "FUiCtrl_IGalleryRenderer.h"
25
26
27 using namespace Tizen::Graphics;
28
29 namespace Tizen { namespace Ui { namespace Controls {
30
31 namespace
32 {
33         const int FLICK_ANIMATION_DURATION = 500;
34         const int ALIGH_ANIMATION_DURATION = 1000;
35         const int ZOOM_ANIMATION_DURATION = 1000;
36         const int PINCH_ZOOM_ANIMATION_DURATION = 500;
37         const int DOUBLE_PRESSE_ZOOM_DISTANCE = 1000;
38         const int FLICK_MOVE_ANIMATION_DURATION = 500;
39         const int FLICK_MOVE_DISTANCE = 200;
40         const float MAX_ZOOM_RATIO = 3.0;
41         const float MIN_ZOOM_RATIO = 0.3;
42         const int NO_CHANGE_ITEM_INDEX = -1;
43 }
44
45 _GalleryViewEventHandler::_GalleryViewEventHandler(_GalleryPresenter& presenter, _IGalleryRenderer& galleryRenderer,
46                                                                                                                                                                 _GalleryImageReader& galleryImageReader)
47         : __galleryPresenter(presenter)
48         , __galleryRenderer(galleryRenderer)
49         , __galleryImageReader(galleryImageReader)
50         , __previousPosition(0, 0)
51         , __startPinchCenterPosition(0, 0)
52         , __flickingDirection(FLICK_DIRECTION_NONE)
53         , __pinchInitialArea(0)
54         , __pressed(false)
55         , __moving(false)
56         , __zoomMode(false)
57         , __zoomAnimationComplete(true)
58         , __pinchMode(false)
59         , __touchPointId(0)
60         , __currentItemIndex(NO_CHANGE_ITEM_INDEX)
61 {
62         // Do nothing.
63 }
64
65 _GalleryViewEventHandler::~_GalleryViewEventHandler(void)
66 {
67         // Do nothing.
68 }
69
70 result
71 _GalleryViewEventHandler::Construct(void)
72 {
73         return __galleryRenderer.AddNotiListener(*this);
74 }
75
76 bool
77 _GalleryViewEventHandler::OnTouchPressed(_GalleryViewEventInfo& eventInfo)
78 {
79         SysTryReturn(NID_UI_CTRL, __pressed == false, true, E_SYSTEM, "[E_SYSTEM] Already pressed state.");
80         result r = SetVisibleCanvas();
81         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
82
83         __pressed = true;
84         __previousPosition.SetPosition(eventInfo.GetCurrentPosition());
85
86         __touchPointId = eventInfo.GetPointId();
87
88         //SetLastResult(E_SUCCESS);
89
90         return true;
91
92 CATCH:
93         SetRollbackCanvas();
94         return true;
95 }
96
97 bool
98 _GalleryViewEventHandler::OnTouchDoublePressed(_GalleryViewEventInfo& eventInfo)
99 {
100         _GalleryAnimationTiming animation = {ZOOM_ANIMATION_DURATION, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
101         Point center;
102         int distance = DOUBLE_PRESSE_ZOOM_DISTANCE;
103         if (__zoomMode == false)
104         {
105                 center = __previousPosition;
106                 __zoomMode = true;
107         }
108         else
109         {
110                 Rectangle viewRect = __galleryRenderer.GetViewRect();
111                 center.SetPosition(viewRect.width / 2, viewRect.height / 2);
112                 __zoomMode = false;
113                 int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(__galleryPresenter.GetCurrentItemIndex());
114                 Rectangle canvasRect = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
115                 distance = canvasRect.width - viewRect.width;
116         }
117         result r = ZoomCanvas(distance, center, __zoomMode, &animation);
118         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
119
120         __zoomAnimationComplete = false;
121         __pressed = false;
122
123         r = RefreshView();
124         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
125
126         //SetLastResult(E_SUCCESS);
127
128         return true;
129 }
130
131 bool
132 _GalleryViewEventHandler::OnTouchMoved(_GalleryViewEventInfo& eventInfo)
133 {
134         if (__pinchMode == true || __touchPointId != eventInfo.GetPointId())
135         {
136                 return true;
137         }
138
139         Dimension distance;
140         distance.width = __previousPosition.x - eventInfo.GetCurrentPosition().x;
141         distance.height = __previousPosition.y - eventInfo.GetCurrentPosition().y;
142
143         result r = MoveCanvas(distance);
144         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
145
146         r = RefreshView();
147         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
148
149         __previousPosition.SetPosition(eventInfo.GetCurrentPosition());
150
151         //SetLastResult(E_SUCCESS);
152
153         return true;
154 }
155
156 bool
157 _GalleryViewEventHandler::OnTouchReleased(_GalleryViewEventInfo& eventInfo)
158 {
159         result r = E_SUCCESS;
160         bool itemClickedSuccess = true;
161         if (__pressed == true)
162         {
163                 if (__moving == true)
164                 {
165                         r = AlignCanvas();
166                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
167                         __moving = false;
168                 }
169                 else
170                 {
171                         itemClickedSuccess = __galleryPresenter.ClickedItem();
172                 }
173                 __pressed = false;
174         }
175
176         if (__pinchMode == true)
177         {
178                 Rectangle viewRect = __galleryRenderer.GetViewRect();
179
180                 int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(__galleryPresenter.GetCurrentItemIndex());
181                 Rectangle canvasRect = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
182                 int distance = canvasRect.width - viewRect.width;
183
184                 if (distance < 0)
185                 {
186                         Point center(viewRect.width / 2, viewRect.height / 2);
187                         _GalleryAnimationTiming animation = {PINCH_ZOOM_ANIMATION_DURATION, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
188                         r = ZoomCanvas(distance, center, false, &animation);
189                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
190                         //__zoomMode = false;
191                 }
192
193                 r = AlignCanvas();
194                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
195                 __pinchInitialArea = 0;
196                 __startPinchCenterPosition.SetPosition(0, 0);
197                 __pinchMode = false;
198         }
199
200         r = RefreshView();
201         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
202         __flickingDirection = FLICK_DIRECTION_NONE;
203
204         //SetLastResult(E_SUCCESS);
205
206         return true;
207
208 CATCH:
209         SetRollbackCanvas();
210         __moving = false;
211         __pressed = false;
212         __pinchInitialArea = 0;
213         __pinchMode = false;
214         __flickingDirection = FLICK_DIRECTION_NONE;
215         return true;
216 }
217
218 bool
219 _GalleryViewEventHandler::OnTouchPinchZoom(_GalleryViewEventInfo& eventInfo)
220 {
221         if (__galleryPresenter.IsZoomingEnabled() == false)
222         {
223                 return true;
224         }
225
226         if (__moving == true)
227         {
228                 return true;
229         }
230
231         Point center = eventInfo.GetPinchCenterPosition();
232         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(__galleryPresenter.GetCurrentItemIndex());
233         Rectangle canvasRect = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
234         Rectangle viewRect = __galleryRenderer.GetViewRect();
235
236         int area = eventInfo.GetPinchArea();
237
238         if (__pinchInitialArea == 0)
239         {
240                 __pinchInitialArea = area;
241                 area = 0;
242         }
243         else
244         {
245                 area -= __pinchInitialArea;
246                 __pinchInitialArea += area;
247         }
248
249         //__zoomMode = true;
250         __pinchMode = true;
251         bool zoomIn = true;
252         if (area < 0)
253         {
254                 zoomIn = false;
255                 area = -area;
256         }
257
258         if (__startPinchCenterPosition.x == 0 && __startPinchCenterPosition.y == 0)
259         {
260                 float factor = (float)canvasRect.width / (float)viewRect.width;
261                 __startPinchCenterPosition = center;
262                 __startPinchCenterPosition.x = CalculateRound((__startPinchCenterPosition.x - canvasRect.x) / factor);
263                 __startPinchCenterPosition.y = CalculateRound((__startPinchCenterPosition.y - canvasRect.y) / factor);
264         }
265
266         //_GalleryAnimationTiming animation = {PINCH_ZOOM_ANIMATION_DURATION, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
267         //result r = ZoomCanvas(area, center, zoomIn, &animation);
268         result r = PinchZoomCanvas(area, center, zoomIn);
269         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
270
271         __pressed = false; //???
272         r = RefreshView();
273         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
274
275         SetLastResult(E_SUCCESS);
276
277         return true;
278 }
279
280 bool
281 _GalleryViewEventHandler::OnTouchFlicked(_GalleryViewEventInfo& eventInfo)
282 {
283         if (__pinchMode == true)
284         {
285                 return true;
286         }
287
288         result r = E_SUCCESS;
289         if (__zoomMode == false)
290         {
291                 if (eventInfo.GetFlickPosition().x > 0)
292                 {
293                         r = FlickToTheRight();
294                 }
295                 else if (eventInfo.GetFlickPosition().x < 0)
296                 {
297                         r = FlickToTheLeft();
298                 }
299                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
300         }
301         else
302         {
303                 _GalleryAnimationTiming animation = {FLICK_MOVE_ANIMATION_DURATION, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
304
305                 int moveX = 0;
306                 int moveY = 0;
307                 if (eventInfo.GetFlickPosition().x > 0)
308                 {
309                         moveX = -FLICK_MOVE_DISTANCE;
310                 }
311                 else
312                 {
313                         moveX = FLICK_MOVE_DISTANCE;
314                 }
315
316                 if (eventInfo.GetFlickPosition().y > 0)
317                 {
318                         moveY = -FLICK_MOVE_DISTANCE;
319                 }
320                 else
321                 {
322                         moveY = FLICK_MOVE_DISTANCE;
323                 }
324
325                 Dimension dim(moveX, moveY);
326                 r = MoveCanvas(dim, &animation);
327                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
328         }
329
330         r = RefreshView();
331         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
332
333         //SetLastResult(E_SUCCESS);
334
335         return true;
336
337 CATCH:
338         SetRollbackCanvas();
339
340         return true;
341 }
342
343 void
344 _GalleryViewEventHandler::OnTransitionCancel(void)
345 {
346 }
347
348 void
349 _GalleryViewEventHandler::OnTransitionCompleted(void)
350 {
351 }
352
353 void
354 _GalleryViewEventHandler::OnCanvasAnimationCancel(void)
355 {
356         __zoomAnimationComplete = true;
357         __flickingDirection = FLICK_DIRECTION_NONE;
358         result r = CallChangeItemIndex();
359         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
360
361         r = RefreshView();
362         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
363
364         //SetLastResult(E_SUCCESS);
365 }
366
367 void
368 _GalleryViewEventHandler::OnCanvasAnimationCompleted(void)
369 {
370         result r = E_SUCCESS;
371
372         if (__zoomAnimationComplete == false)
373         {
374                 r = AlignCanvas();
375                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
376                 __zoomAnimationComplete = true;
377         }
378
379         if (__zoomMode == true)
380         {
381                 r = AlignCanvas(false);
382                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
383
384                 r = __galleryPresenter.SetPartialCanvasImage();
385                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
386         }
387         __flickingDirection = FLICK_DIRECTION_NONE;
388
389         r = CallChangeItemIndex();
390         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
391
392         r = RefreshView();
393         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
394
395         //SetLastResult(E_SUCCESS);
396 }
397
398 void
399 _GalleryViewEventHandler::OnCanvasAnimationStarted(void)
400 {
401
402 }
403
404 void
405 _GalleryViewEventHandler::SetZoomFlag(bool zoomMode)
406 {
407         __zoomMode = zoomMode;
408 }
409
410 bool
411 _GalleryViewEventHandler::IsZoomFlag(void) const
412 {
413         return __zoomMode;
414 }
415
416 result
417 _GalleryViewEventHandler::RefreshView(void)
418 {
419         return __galleryRenderer.RefreshView();
420 }
421
422 result
423 _GalleryViewEventHandler::SetVisibleCanvas(void)
424 {
425         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
426         SysTryReturn(NID_UI_CTRL, currentItemIndex >= 0 && currentItemIndex < __galleryPresenter.GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
427                                 "[E_OUT_OF_RANGE] This value(%d) is out of range(%d ~ %d)", currentItemIndex, 0, __galleryPresenter.GetItemCount());
428
429         Rectangle viewRect = __galleryRenderer.GetViewRect();
430         Rectangle rect = viewRect;
431
432         // current item
433         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
434         result r = E_SUCCESS;
435         if (currentCanvasIndex == NOT_EXIST_CANVAS)
436         {
437                 r = __galleryPresenter.SetCanvasImage(currentItemIndex);
438                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
439         }
440
441         if (__zoomMode == false)
442         {
443                 r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect);
444                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
445         }
446         else
447         {
448                 Rectangle currentCanvasRect = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
449                 r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, currentCanvasRect);
450                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
451         }
452
453         r = __galleryRenderer.SetCanvasVisibility(currentCanvasIndex, true);
454         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
455
456         Rectangle currentCanvasAlignBoundary = __galleryRenderer.GetCanvasAlignBoundary(currentCanvasIndex);
457         // left item
458         if (currentItemIndex - 1 >= 0 && currentItemIndex - 1 < __galleryPresenter.GetItemCount())
459         {
460                 int leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
461                 if (leftCanvasIndex == NOT_EXIST_CANVAS)
462                 {
463                         r = __galleryPresenter.SetCanvasImage(currentItemIndex - 1);
464                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
465                         leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
466                         SysTryReturn(NID_UI_CTRL, leftCanvasIndex != NOT_EXIST_CANVAS, GetLastResult(), GetLastResult(),
467                                         "[%s] Propagating.", GetErrorMessage(GetLastResult()));
468                 }
469                 rect = viewRect;
470                 rect.x = currentCanvasAlignBoundary.x - viewRect.width - __galleryPresenter.GetItemSpacing();
471                 r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect);
472                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
473
474                 r = __galleryRenderer.SetCanvasVisibility(leftCanvasIndex, true);
475                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
476         }
477
478         // right item
479         if (currentItemIndex + 1 >= 0 && currentItemIndex + 1 < __galleryPresenter.GetItemCount())
480         {
481                 int rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
482                 if (rightCanvasIndex == NOT_EXIST_CANVAS)
483                 {
484                         r = __galleryPresenter.SetCanvasImage(currentItemIndex + 1);
485                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
486                         rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
487                         SysTryReturn(NID_UI_CTRL, rightCanvasIndex != NOT_EXIST_CANVAS, GetLastResult(), GetLastResult(),
488                                                 "[%s] Propagating.", GetErrorMessage(GetLastResult()));
489                 }
490                 rect = viewRect;
491                 rect.x = currentCanvasAlignBoundary.x + currentCanvasAlignBoundary.width + __galleryPresenter.GetItemSpacing();
492                 r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect);
493                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
494
495                 r = __galleryRenderer.SetCanvasVisibility(rightCanvasIndex, true);
496                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
497         }
498
499         return E_SUCCESS;
500 }
501
502 result
503 _GalleryViewEventHandler::SetInvisibleCanvas(void)
504 {
505         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
506         SysTryReturn(NID_UI_CTRL, currentItemIndex >= 0 && currentItemIndex < __galleryPresenter.GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
507                         "[E_OUT_OF_RANGE] This value(%d) is out of range(%d ~ %d)", currentItemIndex, 0, __galleryPresenter.GetItemCount());
508
509         int leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
510         int rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
511
512         result r = E_SUCCESS;
513         if (leftCanvasIndex != NOT_EXIST_CANVAS)
514         {
515                 r = __galleryRenderer.SetCanvasVisibility(leftCanvasIndex, false);
516                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
517         }
518         if (rightCanvasIndex != NOT_EXIST_CANVAS)
519         {
520                 r = __galleryRenderer.SetCanvasVisibility(rightCanvasIndex, false);
521                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
522         }
523
524         return E_SUCCESS;
525 }
526
527 result
528 _GalleryViewEventHandler::FlickToTheRight(void)
529 {
530         result r = E_SUCCESS;
531         Rectangle viewRect = __galleryRenderer.GetViewRect();
532         Rectangle rect = viewRect;
533         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
534         int leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
535
536         __flickingDirection = FLICK_DIRECTION_RIGHT;
537         if (leftCanvasIndex == NOT_EXIST_CANVAS)
538         {
539                 if (currentItemIndex - 1 < 0)
540                 {
541                         return E_SUCCESS;
542                 }
543                 else
544                 {
545                         r = __galleryPresenter.SetCanvasImage(currentItemIndex - 1);
546                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
547
548                         leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
549                         r = GetLastResult();
550                         SysTryReturn(NID_UI_CTRL, leftCanvasIndex != NOT_EXIST_CANVAS, r, r, "[%s] Propagating.", GetErrorMessage(r));
551
552                         rect.x = viewRect.x - viewRect.width - __galleryPresenter.GetItemSpacing();
553                         r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect);
554                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
555
556                         r = __galleryRenderer.SetCanvasVisibility(leftCanvasIndex, true);
557                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
558                 }
559         }
560
561         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
562         int rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
563
564         _GalleryAnimationTiming animation = {FLICK_ANIMATION_DURATION, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
565
566         rect.x = viewRect.x + viewRect.width + __galleryPresenter.GetItemSpacing();
567         r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect, &animation);
568         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
569
570         rect.x = viewRect.x;
571         r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect, &animation);
572         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
573
574         if (rightCanvasIndex != NOT_EXIST_CANVAS)
575         {
576                 rect.x = viewRect.x + viewRect.width * 2 + __galleryPresenter.GetItemSpacing();
577                 r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect, &animation);
578                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
579         }
580
581         r = SetCurrentItemIndex(__galleryPresenter.GetCurrentItemIndex() - 1);
582         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
583
584         return E_SUCCESS;
585 }
586
587 result
588 _GalleryViewEventHandler::FlickToTheLeft(void)
589 {
590         result r = E_SUCCESS;
591         Rectangle viewRect = __galleryRenderer.GetViewRect();
592         Rectangle rect = viewRect;
593         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
594         int rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
595
596         __flickingDirection = FLICK_DIRECTION_LEFT;
597         if (rightCanvasIndex == NOT_EXIST_CANVAS)
598         {
599                 if (currentItemIndex + 1 >= __galleryPresenter.GetItemCount())
600                 {
601                         return E_SUCCESS;
602                 }
603                 else
604                 {
605                         r = __galleryPresenter.SetCanvasImage(currentItemIndex + 1);
606                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
607
608                         rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
609                         r = GetLastResult();
610                         SysTryReturn(NID_UI_CTRL, rightCanvasIndex != NOT_EXIST_CANVAS, r, r, "[%s] Propagating.", GetErrorMessage(r));
611
612                         rect.x = viewRect.x + viewRect.width + __galleryPresenter.GetItemSpacing();
613                         r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect);
614                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
615
616                         r = __galleryRenderer.SetCanvasVisibility(rightCanvasIndex, true);
617                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
618                 }
619         }
620
621         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
622         int leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
623         _GalleryAnimationTiming animation = {FLICK_ANIMATION_DURATION, GALLERY_ANIMATION_TIMING_FUNC_EASEOUT};
624
625         rect.x = viewRect.x - viewRect.width - __galleryPresenter.GetItemSpacing();
626         r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect, &animation);
627         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
628
629         rect.x = viewRect.x;
630         r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect, &animation);
631         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
632
633         if (leftCanvasIndex != NOT_EXIST_CANVAS)
634         {
635                 rect.x = viewRect.x - viewRect.width * 2 - __galleryPresenter.GetItemSpacing();
636                 r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect, &animation);
637                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
638         }
639
640         r = SetCurrentItemIndex(__galleryPresenter.GetCurrentItemIndex() + 1);
641         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
642
643         return E_SUCCESS;
644 }
645
646 result
647 _GalleryViewEventHandler::ZoomCanvas(int distance, const Point& center, bool zoomIn, _GalleryAnimationTiming* pAnimation)
648 {
649         if (__galleryPresenter.IsZoomingEnabled() == false || distance == 0)
650         {
651                 __zoomMode = false;
652                 return E_SUCCESS;
653         }
654
655         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
656         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
657         if (__galleryRenderer.IsCanvasVisibility(currentCanvasIndex) == false)
658         {
659                 result r = __galleryRenderer.SetCanvasVisibility(currentCanvasIndex, true);
660                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
661         }
662
663         Rectangle canvasBounds = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
664         Rectangle imageBounds = __galleryRenderer.GetCanvasImageBounds(currentCanvasIndex);
665         Rectangle viewBounds = __galleryRenderer.GetViewRect();
666         float factor = 1.0;
667         if (zoomIn == true)
668         {
669                 factor = (float)(canvasBounds.width + distance) / (float)viewBounds.width;
670                 canvasBounds.width = factor * viewBounds.width;
671                 canvasBounds.height = factor * viewBounds.height;
672                 canvasBounds.x = -(center.x * factor - viewBounds.width / 2);
673                 canvasBounds.y = -(center.y * factor - viewBounds.height/ 2);
674
675                 imageBounds.x *= factor;
676                 imageBounds.y *= factor;
677                 imageBounds.width *= factor;
678                 imageBounds.height *= factor;
679
680                 CorrectCanvasPosition(canvasBounds, imageBounds);
681         }
682         else
683         {
684                 canvasBounds = viewBounds;
685         }
686
687         result r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, canvasBounds, pAnimation);
688         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
689
690         if (canvasBounds.width > viewBounds.width)
691         {
692                 __zoomMode = true;
693         }
694         else
695         {
696                 __zoomMode = false;
697         }
698
699         return E_SUCCESS;
700 }
701
702 result
703 _GalleryViewEventHandler::PinchZoomCanvas(int area, const Point& center, bool zoomIn)
704 {
705         if (__galleryPresenter.IsZoomingEnabled() == false || area == 0 ||
706                 __startPinchCenterPosition.x == 0 || __startPinchCenterPosition.y == 0)
707         {
708                 return E_SUCCESS;
709         }
710
711         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
712         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
713
714         Rectangle bounds = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
715         Rectangle viewBounds = __galleryRenderer.GetViewRect();
716
717         float factor = 1.0f;
718         if (zoomIn == true)
719         {
720                 factor = (float)(bounds.width + area) / (float)viewBounds.width;
721                 if (factor > MAX_ZOOM_RATIO)
722                 {
723                         factor = MAX_ZOOM_RATIO;
724                 }
725         }
726         else
727         {
728                 factor = (float)(bounds.width - area) / (float)viewBounds.width;
729                 if (factor < MIN_ZOOM_RATIO)
730                 {
731                         factor = MIN_ZOOM_RATIO;
732                 }
733         }
734
735         bounds.width = CalculateRound(factor * viewBounds.width);
736         if (bounds.width < 1)
737         {
738                 bounds.width = 1;
739         }
740
741         bounds.height = CalculateRound(factor * viewBounds.height);
742         if (bounds.height < 1)
743         {
744                 bounds.height = 1;
745         }
746
747         if (bounds.width > viewBounds.width || bounds.height > viewBounds.height)
748         {
749                 bounds.x = center.x - CalculateRound(__startPinchCenterPosition.x * factor);
750                 bounds.y = center.y - CalculateRound(__startPinchCenterPosition.y * factor);
751
752                 if (bounds.x > 0)
753                 {
754                         bounds.x = 0;
755                 }
756                 else if (bounds.x < -(bounds.width - viewBounds.width))
757                 {
758                         bounds.x = -(bounds.width - viewBounds.width);
759                 }
760
761                 if (bounds.y > 0)
762                 {
763                         bounds.y = 0;
764                 }
765                 else if (bounds.y < -(bounds.height- viewBounds.height))
766                 {
767                         bounds.y = -(bounds.height - viewBounds.height);
768                 }
769         }
770         else
771         {
772                 bounds.x = (viewBounds.width / 2) - CalculateRound(viewBounds.width / 2 * factor);
773                 bounds.y = (viewBounds.height / 2) - CalculateRound(viewBounds.height / 2 * factor);
774         }
775
776         result r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, bounds);
777         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
778
779         if (bounds.width > viewBounds.width)
780         {
781                 __zoomMode = true;
782         }
783         else
784         {
785                 __zoomMode = false;
786         }
787
788         return E_SUCCESS;
789 }
790
791 result
792 _GalleryViewEventHandler::MoveCanvas(const Dimension& distance, _GalleryAnimationTiming* pAnimation)
793 {
794         SysTryReturn(NID_UI_CTRL, __pressed == true, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] not pressed state.");
795
796         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
797         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
798         int rightCanvasIndex = NOT_EXIST_CANVAS;
799         int leftCanvasIndex = NOT_EXIST_CANVAS;
800
801         if (currentItemIndex != 0)
802         {
803                 leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
804         }
805         if (currentItemIndex < __galleryPresenter.GetItemCount())
806         {
807                 rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
808         }
809
810         Rectangle viewRect = __galleryRenderer.GetViewRect();
811         Rectangle currentCanvasRect = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
812         Rectangle rect = currentCanvasRect;
813
814         Rectangle canvasImageBounds = __galleryRenderer.GetCanvasImageBounds(currentCanvasIndex);
815         if (IsZoomFlag() == true)
816         {
817                 if (canvasImageBounds.y + rect.y > 0)
818                 {
819                         rect.y -= (distance.height / 2);
820                 }
821                 else if (canvasImageBounds.y + canvasImageBounds.height + rect.y < viewRect.height)
822                 {
823                         rect.y -= (distance.height / 2);
824                 }
825                 else
826                 {
827                         rect.y -= distance.height;
828                 }
829
830                 if (leftCanvasIndex == NOT_EXIST_CANVAS && canvasImageBounds.x + rect.x > 0 && distance.width < 0)
831                 {
832                         rect.x -= (distance.width / 2);
833                 }
834                 else if (rightCanvasIndex == NOT_EXIST_CANVAS
835                                 && canvasImageBounds.x + canvasImageBounds.width + rect.x < viewRect.width
836                                 && distance.width > 0)
837                 {
838                         rect.x -= (distance.width / 2);
839                 }
840                 else
841                 {
842                         rect.x -= distance.width;
843                 }
844         }
845         else
846         {
847                 rect.x -= distance.width;
848         }
849
850         if (rect.x > 0 && leftCanvasIndex == NOT_EXIST_CANVAS && __zoomMode == false)
851         {
852                 return E_SUCCESS;
853         }
854         else if (rect.x < 0 && rightCanvasIndex == NOT_EXIST_CANVAS && __zoomMode == false)
855         {
856                 return E_SUCCESS;
857         }
858
859         result r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect, pAnimation);
860         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
861
862         if (rightCanvasIndex != NOT_EXIST_CANVAS)
863         {
864                 rect = __galleryRenderer.GetCanvasBounds(rightCanvasIndex);
865                 //rect.x = currentCanvasRect.x - distance.width + currentCanvasRect.width + __galleryPresenter.GetItemSpacing();
866                 rect.x -= distance.width;
867                 r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect, pAnimation);
868                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
869         }
870
871         if (leftCanvasIndex != NOT_EXIST_CANVAS)
872         {
873                 rect = __galleryRenderer.GetCanvasBounds(leftCanvasIndex);
874                 //rect.x = currentCanvasRect.x - distance.width - rect.width - __galleryPresenter.GetItemSpacing();
875                 rect.x -= distance.width;
876                 r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect, pAnimation);
877                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
878         }
879
880         __moving = true;
881
882         return E_SUCCESS;
883 }
884
885 result
886 _GalleryViewEventHandler::AlignCanvas(bool animation)
887 {
888         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
889         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
890         int leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
891         int rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
892
893         result r = E_SUCCESS;
894         Rectangle viewRect = __galleryRenderer.GetViewRect();
895         Rectangle currentCanvasRect = __galleryRenderer.GetCanvasBounds(currentCanvasIndex);
896         Rectangle canvasImageBounds = __galleryRenderer.GetCanvasImageBounds(currentCanvasIndex);
897
898         Rectangle rect = viewRect;
899         _GalleryAnimationTiming* pAnimationTimingFunction = null;
900         bool immediatePartialCanvasChange = false;
901
902         if (animation == true)
903         {
904                 pAnimationTimingFunction = new(std::nothrow) _GalleryAnimationTiming();
905                 SysTryCatch(NID_UI_CTRL, pAnimationTimingFunction != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
906                 pAnimationTimingFunction->duration_ms = ALIGH_ANIMATION_DURATION;
907                 pAnimationTimingFunction->timingFunction = GALLERY_ANIMATION_TIMING_FUNC_EASEOUT;
908         }
909
910         if (canvasImageBounds.height > viewRect.height) // zoom mode
911         {
912                 if (!(viewRect.y < canvasImageBounds.y && viewRect.height > canvasImageBounds.height + canvasImageBounds.y))
913                 {
914                         immediatePartialCanvasChange = true;
915                 }
916         }
917
918         if ((IsZoomFlag() == true
919                         && currentCanvasRect.x + canvasImageBounds.x > __galleryRenderer.GetViewRect().width / 2
920                         && (leftCanvasIndex != NOT_EXIST_CANVAS))
921                 || (IsZoomFlag() == false
922                         && (canvasImageBounds.width / 2 + canvasImageBounds.x) < canvasImageBounds.x + currentCanvasRect.x)) // ----> align
923         {
924                 r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect, pAnimationTimingFunction);
925                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
926
927                 r = SetCurrentItemIndex(currentItemIndex - 1);
928                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
929
930                 rect = viewRect;
931                 rect.x = viewRect.width + __galleryPresenter.GetItemSpacing();
932                 r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect, pAnimationTimingFunction);
933                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
934         }
935         else if ((IsZoomFlag() == true
936                                 && currentCanvasRect.x + (canvasImageBounds.x + canvasImageBounds.width) < __galleryRenderer.GetViewRect().width / 2
937                                 && (rightCanvasIndex != NOT_EXIST_CANVAS))
938                         ||(IsZoomFlag() == false
939                                 && (canvasImageBounds.width / 2 + canvasImageBounds.x) > currentCanvasRect.x + currentCanvasRect.width)) // <---- align
940         {
941                 r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect, pAnimationTimingFunction);
942                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
943
944                 r = SetCurrentItemIndex(currentItemIndex + 1);
945                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
946
947                 rect = viewRect;
948                 rect.x = -viewRect.width - __galleryPresenter.GetItemSpacing();
949                 r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect, pAnimationTimingFunction);
950                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
951         }
952         else if (currentCanvasRect.x != 0 || currentCanvasRect.width != viewRect.width)// return align (before position)
953         {
954                 if (currentCanvasIndex != NOT_EXIST_CANVAS)
955                 {
956                         rect = currentCanvasRect;
957                         if (__zoomMode == true)
958                         {
959                                 bool corrected = CorrectCanvasPosition(rect, canvasImageBounds);
960                                 if (corrected == false && immediatePartialCanvasChange == true && __zoomAnimationComplete == true)
961                                 {
962                                         r = __galleryPresenter.SetPartialCanvasImage();
963                                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
964                                 }
965                         }
966                         else
967                         {
968                                 rect = viewRect;
969                         }
970
971                         r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, rect, pAnimationTimingFunction);
972                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
973                 }
974                 Rectangle currentCanvasAlignBoundary = __galleryRenderer.GetCanvasAlignBoundary(currentCanvasIndex);
975                 if (rightCanvasIndex != NOT_EXIST_CANVAS)
976                 {
977                         rect = viewRect;
978                         rect.x = currentCanvasAlignBoundary.x + currentCanvasAlignBoundary.width + __galleryPresenter.GetItemSpacing();
979                         r = __galleryRenderer.SetCanvasBounds(rightCanvasIndex, rect, pAnimationTimingFunction);
980                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
981                 }
982                 if (leftCanvasIndex != NOT_EXIST_CANVAS)
983                 {
984                         rect = viewRect;
985                         rect.x = currentCanvasAlignBoundary.x - rect.width - __galleryPresenter.GetItemSpacing();
986                         r = __galleryRenderer.SetCanvasBounds(leftCanvasIndex, rect, pAnimationTimingFunction);
987                         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
988                 }
989         }
990
991         delete pAnimationTimingFunction;
992
993         return E_SUCCESS;
994
995 CATCH:
996         delete pAnimationTimingFunction;
997         return r;
998 }
999
1000 void
1001 _GalleryViewEventHandler::SetRollbackCanvas(void)
1002 {
1003         int currentItemIndex = __galleryPresenter.GetCurrentItemIndex();
1004         int currentCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex);
1005         int leftCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex - 1);
1006         int rightCanvasIndex = __galleryPresenter.SearchCanvasIndex(currentItemIndex + 1);
1007
1008         Rectangle viewRect = __galleryRenderer.GetViewRect();
1009
1010         result r = __galleryRenderer.SetCanvasBounds(currentCanvasIndex, viewRect);
1011         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1012
1013         r = __galleryRenderer.SetCanvasVisibility(leftCanvasIndex, false);
1014         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1015
1016         r = __galleryRenderer.SetCanvasVisibility(rightCanvasIndex, false);
1017         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1018
1019         //SetLastResult(E_SUCCESS);
1020 }
1021
1022 int
1023 _GalleryViewEventHandler::CalculateRound(float value)
1024 {
1025         int returnValue = ((int)((value) * 10)) % 10 > 5 ? value + 0.5 : value;
1026         return returnValue;
1027 }
1028
1029 bool
1030 _GalleryViewEventHandler::CorrectCanvasPosition(Rectangle& canvasBounds, const Rectangle& imageBounds)
1031 {
1032         if (IsZoomFlag() == false)
1033         {
1034                 return false;
1035         }
1036         Rectangle viewBounds = __galleryRenderer.GetViewRect();
1037         bool corrected = false;
1038         if (imageBounds.width > viewBounds.width)
1039         {
1040                 if (viewBounds.x < imageBounds.x + canvasBounds.x)
1041                 {
1042                         canvasBounds.x = -imageBounds.x;
1043                         corrected = true;
1044                 }
1045                 else if (viewBounds.width > imageBounds.x + imageBounds.width + canvasBounds.x)
1046                 {
1047                         canvasBounds.x = (viewBounds.x + viewBounds.width) - (imageBounds.x + imageBounds.width);
1048                         corrected = true;
1049                 }
1050         }
1051         else
1052         {
1053                 canvasBounds.x = viewBounds.width / 2 -((imageBounds.width / 2 + imageBounds.x));
1054                 corrected = true;
1055         }
1056
1057         if (imageBounds.height > viewBounds.height)
1058         {
1059                 if (viewBounds.y < imageBounds.y + canvasBounds.y)
1060                 {
1061                         canvasBounds.y = -imageBounds.y;
1062                         corrected = true;
1063                 }
1064                 else if (viewBounds.height > imageBounds.height + imageBounds.y + canvasBounds.y)
1065                 {
1066                         canvasBounds.y = (viewBounds.y + viewBounds.height) - (imageBounds.y + imageBounds.height);
1067                         corrected = true;
1068                 }
1069         }
1070         else
1071         {
1072                 canvasBounds.y = viewBounds.height / 2 -((imageBounds.height / 2 + imageBounds.y));
1073                 corrected = true;
1074         }
1075
1076         return corrected;
1077 }
1078
1079 result
1080 _GalleryViewEventHandler::SetCurrentItemIndex(int index)
1081 {
1082         __currentItemIndex = index;
1083
1084         return __galleryPresenter.SetCurrentItemIndex(__currentItemIndex, false);
1085 }
1086
1087 result
1088 _GalleryViewEventHandler::CallChangeItemIndex(void)
1089 {
1090         result r = E_SUCCESS;
1091         if (__currentItemIndex != NO_CHANGE_ITEM_INDEX)
1092         {
1093                 r = __galleryPresenter.SetCurrentItemIndex(__currentItemIndex);
1094                 __currentItemIndex = NO_CHANGE_ITEM_INDEX;
1095                 __galleryPresenter.ChangedItem();
1096         }
1097         return r;
1098 }
1099
1100 }}} // Tizen::Ui::Controls