Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_GalleryImpl.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 <FUiCtrlGallery.h>
18 #include <FUiCtrlIGalleryItemProvider.h>
19 #include <FUiCtrlIGalleryEventListener.h>
20 #include <FGrp_BitmapImpl.h>
21 #include "FUi_UiBuilder.h"
22 #include "FUiCtrl_Gallery.h"
23 #include "FUiCtrl_GalleryImpl.h"
24 #include "FUiCtrl_GalleryBitmap.h"
25 #include "FUiCtrl_GalleryItemProviderAdaptorImpl.h"
26 #include "FUiCtrl_GalleryCoreEventListener.h"
27 #include "FUiCtrl_GalleryImplEvent.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Ui;
32 using namespace Tizen::Ui::Effects;
33
34 namespace Tizen { namespace Ui { namespace Controls {
35
36 namespace
37 {
38
39 _GallerySlideShowType
40 ConvertEnum(GalleryAnimation var)
41 {
42         switch (var)
43         {
44         case GALLERY_ANIMATION_PAGE:
45                 return GALLERY_SLIDESHOW_PAGE;
46
47         case GALLERY_ANIMATION_DISSOLVE:
48                 return GALLERY_SLIDESHOW_DISSOLVE;
49
50         case GALLERY_ANIMATION_ZOOM:
51                 return GALLERY_SLIDESHOW_ZOOM;
52
53         default:
54                 return (_GallerySlideShowType)-1;
55         }
56 }
57
58 GalleryAnimation
59 ConvertEnum(_GallerySlideShowType var)
60 {
61         switch (var)
62         {
63         case GALLERY_SLIDESHOW_PAGE:
64                 return GALLERY_ANIMATION_PAGE;
65
66         case GALLERY_SLIDESHOW_DISSOLVE:
67                 return GALLERY_ANIMATION_DISSOLVE;
68
69         case GALLERY_SLIDESHOW_ZOOM:
70                 return GALLERY_ANIMATION_ZOOM;
71
72         default:
73                 return (GalleryAnimation)-1;
74         }
75 }
76
77 } // unnamed namespace
78
79 _GalleryImpl::_GalleryImpl(Gallery* pGallery, _Gallery* pCore, _GalleryImplEvent* pImplEvent,
80                                                                         _GalleryCoreEventListener* pCoreEventListener, _GalleryItemProviderAdaptorImpl* pGalleryItemProviderAdaptor)
81         : _ControlImpl(pGallery, pCore)
82         , __pImplEvent(pImplEvent)
83         , __pCoreEventListener(pCoreEventListener)
84         , __pGalleryItemProviderAdaptor(pGalleryItemProviderAdaptor)
85 {
86 }
87
88 _GalleryImpl::~_GalleryImpl(void)
89 {
90         RequestToUnloadAllItems();
91
92         delete __pImplEvent;
93         delete __pCoreEventListener;
94         delete __pGalleryItemProviderAdaptor;
95 }
96
97 _GalleryImpl*
98 _GalleryImpl::CreateGalleryImplN(Gallery* pGallery)
99 {
100         SysTryReturn(NID_UI_CTRL, pGallery != null, null, E_INVALID_ARG, "[E_INVALID_ARG] pGallery is null.");
101
102         _GalleryCoreEventListener* pCoreEventListener = null;
103         _GalleryItemProviderAdaptorImpl* pGalleryItemProviderAdaptor = null;
104         _Gallery* pCore = null;
105         _GalleryImpl* pImpl = null;
106         result r = E_SUCCESS;
107
108         _GalleryImplEvent* pImplEvent = new (std::nothrow) _GalleryImplEvent();
109         SysTryCatch(NID_UI_CTRL, pImplEvent != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
110
111         pCoreEventListener = new (std::nothrow) _GalleryCoreEventListener(*pImplEvent);
112         SysTryCatch(NID_UI_CTRL, pCoreEventListener != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
113
114         pGalleryItemProviderAdaptor = new (std::nothrow) _GalleryItemProviderAdaptorImpl();
115         SysTryCatch(NID_UI_CTRL, pGalleryItemProviderAdaptor != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
116
117         pCore = _Gallery::CreateGalleryN(pGalleryItemProviderAdaptor);
118         SysTryCatch(NID_UI_CTRL, pCore != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
119
120         r = pCore->SetCoreEventListener(*pCoreEventListener);
121         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
122
123         pImpl = new (std::nothrow) _GalleryImpl(pGallery, pCore, pImplEvent, pCoreEventListener, pGalleryItemProviderAdaptor);
124         SysTryCatch(NID_UI_CTRL, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
125         //pCore = null;
126
127         SetLastResult(E_SUCCESS);
128         return pImpl;
129
130 CATCH:
131         delete pCoreEventListener;
132         delete pImplEvent;
133         delete pCore;
134         //delete pImpl;
135
136         return null;
137 }
138
139 const char*
140 _GalleryImpl::GetPublicClassName(void) const
141 {
142         return "Control::Gallery";
143 }
144
145 const Gallery&
146 _GalleryImpl::GetPublic(void) const
147 {
148         return static_cast<const Gallery&>(_ControlImpl::GetPublic());
149 }
150
151 Gallery&
152 _GalleryImpl::GetPublic(void)
153 {
154         return static_cast<Gallery&>(_ControlImpl::GetPublic());
155 }
156
157 const _Gallery&
158 _GalleryImpl::GetCore(void) const
159 {
160         return static_cast<const _Gallery&>(_ControlImpl::GetCore());
161 }
162
163 _Gallery&
164 _GalleryImpl::GetCore(void)
165 {
166         return static_cast<_Gallery&>(_ControlImpl::GetCore());
167 }
168
169 void
170 _GalleryImpl::AddGalleryEventListener(IGalleryEventListener& listener)
171 {
172         result r = __pImplEvent->AddListener(listener);
173         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
174
175         SetLastResult(E_SUCCESS);
176 }
177
178 void
179 _GalleryImpl::RemoveGalleryEventListener(IGalleryEventListener& listener)
180 {
181         result r = __pImplEvent->RemoveListener(listener);
182         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
183
184         SetLastResult(E_SUCCESS);
185 }
186
187 result
188 _GalleryImpl::SetItemProvider(IGalleryItemProvider& provider)
189 {
190         __pGalleryItemProviderAdaptor->SetGalleryItemProvider(provider);
191
192         return GetCore().RequestToUpdateAllItems();
193 }
194
195 result
196 _GalleryImpl::SetCurrentItemIndex(int index)
197 {
198         SysTryReturn(NID_UI_CTRL, __pGalleryItemProviderAdaptor->HasGalleryItemProvider() == true, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
199                                         "[E_OUT_OF_RANGE] Item provider is null");
200
201         return GetCore().SetCurrentItemIndex(index);
202 }
203
204 int
205 _GalleryImpl::GetCurrentItemIndex(void) const
206 {
207         return GetCore().GetCurrentItemIndex();
208 }
209
210 result
211 _GalleryImpl::SetZoomingEnabled(bool enable)
212 {
213         return GetCore().SetZoomingEnabled(enable);
214 }
215
216 bool
217 _GalleryImpl::IsZoomingEnabled(void) const
218 {
219         return GetCore().IsZoomingEnabled();
220 }
221
222 result
223 _GalleryImpl::SetTextOfEmptyGallery(const String& text)
224 {
225         return GetCore().SetTextOfEmptyGallery(text);
226 }
227
228 String
229 _GalleryImpl::GetTextOfEmptyGallery(void) const
230 {
231         return GetCore().GetTextOfEmptyGallery();
232 }
233
234 result
235 _GalleryImpl::SetBitmapOfEmptyGallery(const Bitmap* pBitmap)
236 {
237         _GalleryBitmap* pGalleryBitmap = _GalleryBitmap::CreateGalleryBitmapN(const_cast<Bitmap*>(pBitmap));
238         SysTryReturn(NID_UI_CTRL, pGalleryBitmap != null, GetLastResult(), GetLastResult(),
239                                 "[%s] Propagating.", GetErrorMessage(GetLastResult()));
240
241         return GetCore().SetBitmapOfEmptyGallery(pGalleryBitmap);
242 }
243
244 result
245 _GalleryImpl::SetSlideShowAnimation(GalleryAnimation animation)
246 {
247         _GallerySlideShowType slideShowType = ConvertEnum(animation);
248
249         SysTryReturn(NID_UI_CTRL, slideShowType != -1, E_INVALID_ARG, E_INVALID_ARG,
250                                         "[E_INVALID_ARG] The animation argument(%d) is invalid argument.", animation);
251
252         return GetCore().SetSlideShowType(slideShowType);
253 }
254
255 GalleryAnimation
256 _GalleryImpl::GetSlideShowAnimation(void) const
257 {
258         return ConvertEnum(GetCore().GetSlideShowType());
259 }
260
261 result
262 _GalleryImpl::SetSlideShowAnimationDuration(int duration)
263 {
264         return GetCore().SetSlideShowAnimationDuration(duration);
265 }
266
267 int
268 _GalleryImpl::GetSlideShowAnimationDuration(void) const
269 {
270         return GetCore().GetSlideShowAnimationDuration();
271 }
272
273 result
274 _GalleryImpl::SetSlideShowViewDuration(int duration)
275 {
276         return GetCore().SetSlideShowViewDuration(duration);
277 }
278
279 int
280 _GalleryImpl::GetSlideShowViewDuration(void) const
281 {
282         return GetCore().GetSlideShowViewDuration();
283 }
284
285 result
286 _GalleryImpl::StartSlideShow(bool repeat)
287 {
288         return GetCore().StartSlideShow(repeat);
289 }
290
291 result
292 _GalleryImpl::StopSlideShow(void) const
293 {
294         _Gallery* pCore = const_cast<_Gallery*>(&GetCore()); // ???
295
296         return pCore->StopSlideShow();
297 }
298
299 bool
300 _GalleryImpl::IsSlideShowStarted(void) const
301 {
302         return GetCore().IsSlideShowStarted();
303 }
304
305 int
306 _GalleryImpl::GetItemCount(void) const
307 {
308         return GetCore().GetItemCount();
309 }
310
311 result
312 _GalleryImpl::RefreshGallery(int itemIndex, GalleryRefreshType type)
313 {
314         SysTryReturn(NID_UI_CTRL, __pGalleryItemProviderAdaptor->HasGalleryItemProvider() == true, E_INVALID_OPERATION, E_INVALID_OPERATION,
315                                         "[E_INVALID_OPERATION] Item provider is null");
316
317         switch (type)
318         {
319         case GALLERY_REFRESH_TYPE_ITEM_ADD:
320                 return GetCore().RequestToLoadItem(itemIndex);
321         case GALLERY_REFRESH_TYPE_ITEM_REMOVE:
322                 return GetCore().RequestToUnloadItem(itemIndex);
323         case GALLERY_REFRESH_TYPE_ITEM_MODIFY:
324                 return GetCore().RequestToUpdateItem(itemIndex);
325         default:
326                 SysTryReturn(NID_UI_CTRL, false, E_INVALID_ARG, E_INVALID_ARG,
327                                         "[E_INVALID_ARG] The refresh type(%d) is invalid type.", type);
328                 break;
329         }
330
331         return E_SUCCESS;
332 }
333
334 result
335 _GalleryImpl::UpdateGallery(void)
336 {
337         SysTryReturn(NID_UI_CTRL, __pGalleryItemProviderAdaptor->HasGalleryItemProvider() == true, E_INVALID_OPERATION, E_INVALID_OPERATION,
338                         "[E_INVALID_OPERATION] Item provider is null");
339
340         result r = GetCore().RequestToUpdateAllItems();
341         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
342
343         return E_SUCCESS;
344 }
345
346 void
347 _GalleryImpl::RequestToUnloadAllItems(void)
348 {
349         result r = GetCore().RequestToUnloadAllItems();
350         SysAssert(r == E_SUCCESS);
351 }
352
353 void
354 _GalleryImpl::SetEffect(Effect* pEffect)
355 {
356         GetCore().SetEffect(pEffect);
357 }
358
359 Effect*
360 _GalleryImpl::GetEffect(void)
361 {
362         return GetCore().GetEffect();
363 }
364
365 _GalleryImpl*
366 _GalleryImpl::GetInstance(Gallery& gallery)
367 {
368         return static_cast<_GalleryImpl*> (gallery._pControlImpl);
369 }
370
371 const _GalleryImpl*
372 _GalleryImpl::GetInstance(const Gallery& gallery)
373 {
374         return static_cast<const _GalleryImpl*> (gallery._pControlImpl);
375 }
376
377 // Ui Builder
378 class _GalleryMaker
379         : public _UiBuilderControlMaker
380 {
381 public:
382         _GalleryMaker(_UiBuilder* uibuilder)
383                 : _UiBuilderControlMaker(uibuilder) {};
384         virtual ~_GalleryMaker() {};
385         static _UiBuilderControlMaker*
386         GetInstance(_UiBuilder* uibuilder)
387         {
388                 _GalleryMaker* pListViewMaker = new (std::nothrow) _GalleryMaker(uibuilder);
389                 return pListViewMaker;
390         };
391 protected:
392         virtual Control*
393         Make(_UiBuilderControl* pControl)
394         {
395                 result r = E_SYSTEM;
396                 _UiBuilderControlLayout* pControlProperty = null;
397                 Gallery* pGallery = null;
398                 Rectangle rect;
399                 Tizen::Base::String elementString;
400                 int currentIndex = 0;
401                 int duration = 0;
402                 Color color;
403                 bool isZoomingEnabled = true;
404
405                 GetProperty(pControl, &pControlProperty);
406                 if (pControlProperty == null)
407                 {
408                         return null;
409                 }
410
411                 pGallery = new (std::nothrow) Gallery();
412                 if (pGallery == null)
413                 {
414                         return null;
415                 }
416
417                 rect = pControlProperty->GetRect();
418                 //Construct
419                 r = pGallery->Construct(rect);
420                 if (r != E_SUCCESS)
421                 {
422                         delete pGallery;
423                         return null;
424                 }
425
426                 if (pControl->GetElement(L"backgroundColor", elementString) || pControl->GetElement(L"BGColor", elementString))
427                 {
428                         ConvertStringToColor32(elementString, 100, color);
429                         r = pGallery->SetBackgroundColor(color);
430                         if (r != E_SUCCESS)
431                         {
432                                 delete pGallery;
433                                 return null;
434                         }
435                 }
436
437                 if (pControl->GetElement(L"bitmapPathOfEmptyGallery", elementString))
438                 {
439                         Bitmap* pBackgroundBitmap = null;
440                         pBackgroundBitmap = LoadBitmapN(elementString); //__image->DecodeN(path,BITMAP_PIXEL_FORMAT_RGB565);
441                         if (pBackgroundBitmap != null)
442                         {
443                                 r = pGallery->SetBitmapOfEmptyGallery(pBackgroundBitmap);
444                                 if (r != E_SUCCESS)
445                                 {
446                                         delete pBackgroundBitmap;
447                                         delete pGallery;
448                                         return null;
449                                 }
450                                 delete pBackgroundBitmap;
451                         }
452                 }
453
454                 if (pControl->GetElement(L"currentItemIndex", elementString))
455                 {
456                         Base::Integer::Parse(elementString, currentIndex);
457                         r = pGallery->SetCurrentItemIndex(currentIndex);
458                         if (r != E_SUCCESS)
459                         {
460                                 delete pGallery;
461                                 return null;
462                         }
463                 }
464
465                 if (pControl->GetElement(L"slideShowAnimation", elementString))
466                 {
467                         GalleryAnimation animation = GALLERY_ANIMATION_DISSOLVE;
468                         if (elementString.Equals(L"GALLERY_ANIMATION_PAGE", false))
469                         {
470                                 animation = GALLERY_ANIMATION_PAGE;
471                         }
472                         else if (elementString.Equals(L"GALLERY_ANIMATION_ZOOM", false))
473                         {
474                                 animation = GALLERY_ANIMATION_ZOOM;
475                         }
476
477                         r = pGallery->SetSlideShowAnimation(animation);
478                         if (r != E_SUCCESS)
479                         {
480                                 delete pGallery;
481                                 return null;
482                         }
483                 }
484
485                 if (pControl->GetElement(L"slideShowAnimationDuration", elementString))
486                 {
487                         Base::Integer::Parse(elementString, duration);
488                         r = pGallery->SetSlideShowAnimationDuration(duration);
489                         if (r != E_SUCCESS)
490                         {
491                                 delete pGallery;
492                                 return null;
493                         }
494                 }
495
496                 if (pControl->GetElement(L"slideShowViewDuration", elementString))
497                 {
498                         Base::Integer::Parse(elementString, duration);
499                         r = pGallery->SetSlideShowViewDuration(duration);
500                         if (r != E_SUCCESS)
501                         {
502                                 delete pGallery;
503                                 return null;
504                         }
505                 }
506
507                 if (pControl->GetElement(L"textOfEmptyGallery", elementString))
508                 {
509                         r = pGallery->SetTextOfEmptyGallery(elementString);
510                         if (r != E_SUCCESS)
511                         {
512                                 delete pGallery;
513                                 return null;
514                         }
515                 }
516
517                 if (pControl->GetElement(L"zoomingEabled", elementString))
518                 {
519                         {
520                                 if (elementString.Equals(L"true", false))
521                                 {
522                                         isZoomingEnabled = true;
523                                 }
524                                 else
525                                 {
526                                         isZoomingEnabled = false;
527                                 }
528                         }
529                         r = pGallery->SetZoomingEnabled(isZoomingEnabled);
530                         if (r != E_SUCCESS)
531                         {
532                                 delete pGallery;
533                                 return null;
534                         }
535                 }
536
537                 return pGallery;
538         }
539
540 private:
541 };
542
543 _GalleryRegister::_GalleryRegister(void)
544 {
545         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
546         pUiBuilderControlTableManager->RegisterControl(L"Gallery", _GalleryMaker::GetInstance);
547 }
548 _GalleryRegister::~_GalleryRegister(void)
549 {
550         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
551         pUiBuilderControlTableManager->UnregisterControl(L"Gallery");
552 }
553 static _GalleryRegister GalleryRegisterToUiBuilder;
554 }}} // Controls