Merge "DALi Version 1.0.47" into devel/master
[platform/core/uifw/dali-demo.git] / examples / item-view / item-view-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <sstream>
19 #include <iostream>
20 #include <vector>
21 #include <string>
22 #include <algorithm>
23 #include <cstdlib> // rand
24 #include "shared/view.h"
25
26 #include <dali/dali.h>
27 #include <dali-toolkit/dali-toolkit.h>
28 #include <dali/devel-api/images/atlas.h>
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 namespace
34 {
35
36 enum AllImagesLayouts
37 {
38   SPIRAL_LAYOUT,
39   DEPTH_LAYOUT,
40   GRID_LAYOUT,
41
42   NUMBER_OF_LAYOUTS
43 };
44
45 const char* IMAGE_PATHS[] = {
46     DALI_IMAGE_DIR "gallery-medium-1.jpg",
47     DALI_IMAGE_DIR "gallery-medium-2.jpg",
48     DALI_IMAGE_DIR "gallery-medium-3.jpg",
49     DALI_IMAGE_DIR "gallery-medium-4.jpg",
50     DALI_IMAGE_DIR "gallery-medium-5.jpg",
51     DALI_IMAGE_DIR "gallery-medium-6.jpg",
52     DALI_IMAGE_DIR "gallery-medium-7.jpg",
53     DALI_IMAGE_DIR "gallery-medium-8.jpg",
54     DALI_IMAGE_DIR "gallery-medium-9.jpg",
55     DALI_IMAGE_DIR "gallery-medium-10.jpg",
56     DALI_IMAGE_DIR "gallery-medium-11.jpg",
57     DALI_IMAGE_DIR "gallery-medium-12.jpg",
58     DALI_IMAGE_DIR "gallery-medium-13.jpg",
59     DALI_IMAGE_DIR "gallery-medium-14.jpg",
60     DALI_IMAGE_DIR "gallery-medium-15.jpg",
61     DALI_IMAGE_DIR "gallery-medium-16.jpg",
62     DALI_IMAGE_DIR "gallery-medium-17.jpg",
63     DALI_IMAGE_DIR "gallery-medium-18.jpg",
64     DALI_IMAGE_DIR "gallery-medium-19.jpg",
65     DALI_IMAGE_DIR "gallery-medium-20.jpg",
66     DALI_IMAGE_DIR "gallery-medium-21.jpg",
67     DALI_IMAGE_DIR "gallery-medium-22.jpg",
68     DALI_IMAGE_DIR "gallery-medium-23.jpg",
69     DALI_IMAGE_DIR "gallery-medium-24.jpg",
70     DALI_IMAGE_DIR "gallery-medium-25.jpg",
71     DALI_IMAGE_DIR "gallery-medium-26.jpg",
72     DALI_IMAGE_DIR "gallery-medium-27.jpg",
73     DALI_IMAGE_DIR "gallery-medium-28.jpg",
74     DALI_IMAGE_DIR "gallery-medium-29.jpg",
75     DALI_IMAGE_DIR "gallery-medium-30.jpg",
76     DALI_IMAGE_DIR "gallery-medium-31.jpg",
77     DALI_IMAGE_DIR "gallery-medium-32.jpg",
78     DALI_IMAGE_DIR "gallery-medium-33.jpg",
79     DALI_IMAGE_DIR "gallery-medium-34.jpg",
80     DALI_IMAGE_DIR "gallery-medium-35.jpg",
81     DALI_IMAGE_DIR "gallery-medium-36.jpg",
82     DALI_IMAGE_DIR "gallery-medium-37.jpg",
83     DALI_IMAGE_DIR "gallery-medium-38.jpg",
84     DALI_IMAGE_DIR "gallery-medium-39.jpg",
85     DALI_IMAGE_DIR "gallery-medium-40.jpg",
86     DALI_IMAGE_DIR "gallery-medium-41.jpg",
87     DALI_IMAGE_DIR "gallery-medium-42.jpg",
88     DALI_IMAGE_DIR "gallery-medium-43.jpg",
89     DALI_IMAGE_DIR "gallery-medium-44.jpg",
90     DALI_IMAGE_DIR "gallery-medium-45.jpg",
91     DALI_IMAGE_DIR "gallery-medium-46.jpg",
92     DALI_IMAGE_DIR "gallery-medium-47.jpg",
93     DALI_IMAGE_DIR "gallery-medium-48.jpg",
94     DALI_IMAGE_DIR "gallery-medium-49.jpg",
95     DALI_IMAGE_DIR "gallery-medium-50.jpg",
96     DALI_IMAGE_DIR "gallery-medium-51.jpg",
97     DALI_IMAGE_DIR "gallery-medium-52.jpg",
98     DALI_IMAGE_DIR "gallery-medium-53.jpg",
99 };
100
101 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
102
103 const unsigned int IMAGE_WIDTH = 256;
104 const unsigned int IMAGE_HEIGHT = 256;
105 const unsigned int NUM_IMAGE_PER_ROW_IN_ATLAS = 8;
106
107 const char* BACKGROUND_IMAGE( "" );
108 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
109 const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-edit.png" );
110 const char* EDIT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-edit-selected.png" );
111 const char* SPIRAL_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png" );
112 const char* SPIRAL_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-spiral-selected.png" );
113 const char* GRID_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-grid.png" );
114 const char* GRID_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-grid-selected.png" );
115 const char* DEPTH_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-depth.png" );
116 const char* DEPTH_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-depth-selected.png" );
117 const char* DELETE_IMAGE( DALI_IMAGE_DIR "icon-delete.png" );
118 const char* DELETE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-delete-selected.png" );
119 const char* REPLACE_IMAGE( DALI_IMAGE_DIR "icon-replace.png" );
120 const char* REPLACE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-replace-selected.png" );
121 const char* INSERT_IMAGE( DALI_IMAGE_DIR "icon-insert.png" );
122 const char* INSERT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-insert-selected.png" );
123 const char* SELECTED_IMAGE( DALI_IMAGE_DIR "item-select-check.png" );
124 const char* APPLICATION_TITLE( "ItemView" );
125
126 const char* SPIRAL_LABEL("Spiral");
127 const char* GRID_LABEL("Grid");
128 const char* DEPTH_LABEL("Depth");
129
130 const char* ITEM_BORDER_IMAGE_PATH( DALI_IMAGE_DIR "frame-128x128.png" );
131 const Vector3 ITEM_BORDER_MARGIN_SIZE(24, 24, 0);
132
133 // These values depend on the border image
134 const float ITEM_IMAGE_BORDER_LEFT   = 13.0f;
135 const float ITEM_IMAGE_BORDER_RIGHT  = 13.0f;
136 const float ITEM_IMAGE_BORDER_TOP    = 13.0f;
137 const float ITEM_IMAGE_BORDER_BOTTOM = 13.0f;
138
139 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
140 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
141 const float DEPTH_LAYOUT_COLUMNS = 3.0f;
142
143 const float MIN_SWIPE_DISTANCE = 15.0f;
144 const float MIN_SWIPE_SPEED = 5.0f;
145
146 const float SELECTION_BORDER_WIDTH = 3.0f;
147 const float BUTTON_BORDER = -10.0f;
148 const float MENU_OPTION_HEIGHT(140.0f);
149 const float LABEL_TEXT_SIZE_Y = 20.0f;
150
151 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
152
153 static Vector3 DepthLayoutItemSizeFunctionPortrait( float layoutWidth )
154 {
155   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
156
157   // 1x1 aspect ratio
158   return Vector3(width, width, width);
159 }
160
161 static Vector3 DepthLayoutItemSizeFunctionLandscape( float layoutWidth )
162 {
163   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
164
165   // 1x1 aspect ratio
166   return Vector3(width, width, width);
167 }
168
169 } // unnamed namespace
170
171 /**
172  * This example shows how to use ItemView UI control.
173  * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
174  * There is one button in the upper-left corner for quitting the application and
175  * another button in the upper-right corner for switching between different layouts.
176  */
177 class ItemViewExample : public ConnectionTracker, public ItemFactory
178 {
179 public:
180
181   enum Mode
182   {
183     MODE_NORMAL,
184     MODE_REMOVE,
185     MODE_REMOVE_MANY,
186     MODE_INSERT,
187     MODE_INSERT_MANY,
188     MODE_REPLACE,
189     MODE_REPLACE_MANY,
190     MODE_LAST
191   };
192
193   /**
194    * Constructor
195    * @param application class, stored as reference
196    */
197   ItemViewExample( Application& application )
198   : mApplication( application ),
199     mMode( MODE_NORMAL ),
200     mOrientation( 0 ),
201     mCurrentLayout( SPIRAL_LAYOUT ),
202     mDurationSeconds( 0.25f )
203   {
204     // Connect to the Application's Init signal
205     mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
206   }
207
208   /**
209    * This method gets called once the main loop of application is up and running
210    */
211   void OnInit(Application& app)
212   {
213     Stage stage = Dali::Stage::GetCurrent();
214     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
215
216     Vector2 stageSize = Stage::GetCurrent().GetSize();
217
218     // Create a border image shared by all the item actors
219     mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
220
221     // Creates a default view with a default tool bar.
222     // The view is added to the stage.
223     Layer contents = DemoHelper::CreateView( mApplication,
224                                              mView,
225                                              mToolBar,
226                                              BACKGROUND_IMAGE,
227                                              TOOLBAR_IMAGE,
228                                              "" );
229
230     //app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
231
232     // Create an edit mode button. (left of toolbar)
233     Toolkit::PushButton editButton = Toolkit::PushButton::New();
234     editButton.SetButtonImage( ResourceImage::New( EDIT_IMAGE ) );
235     editButton.SetSelectedImage( ResourceImage::New( EDIT_IMAGE_SELECTED ) );
236     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
237     editButton.SetLeaveRequired( true );
238     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
239
240     // Create a layout toggle button. (right of toolbar)
241     mLayoutButton = Toolkit::PushButton::New();
242     mLayoutButton.SetButtonImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
243     mLayoutButton.SetSelectedImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE_SELECTED ) );
244     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
245     mLayoutButton.SetLeaveRequired( true );
246     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
247
248     // Create a delete button (bottom right of screen)
249     mDeleteButton = Toolkit::PushButton::New();
250     mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
251     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
252     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
253     mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
254     mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
255     mDeleteButton.SetSelectedImage( ResourceImage::New( DELETE_IMAGE_SELECTED ) );
256     mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
257     mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
258     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
259     mDeleteButton.SetLeaveRequired( true );
260     mDeleteButton.SetVisible( false );
261     stage.Add( mDeleteButton );
262
263     // Create an insert button (bottom right of screen)
264     mInsertButton = Toolkit::PushButton::New();
265     mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
266     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
267     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
268     mInsertButton.SetDrawMode( DrawMode::OVERLAY );
269     mInsertButton.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
270     mInsertButton.SetSelectedImage( ResourceImage::New( INSERT_IMAGE_SELECTED ) );
271     mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
272     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
273     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
274     mInsertButton.SetLeaveRequired( true );
275     mInsertButton.SetVisible( false );
276     stage.Add( mInsertButton );
277
278     // Create an replace button (bottom right of screen)
279     mReplaceButton = Toolkit::PushButton::New();
280     mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
281     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
282     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
283     mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
284     mReplaceButton.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
285     mReplaceButton.SetSelectedImage( ResourceImage::New( REPLACE_IMAGE_SELECTED ) );
286     mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
287     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
288     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
289     mReplaceButton.SetLeaveRequired( true );
290     mReplaceButton.SetVisible( false );
291     stage.Add( mReplaceButton );
292
293     // Create the item view actor
294     mImageAtlas = CreateImageAtlas();
295     mItemView = ItemView::New(*this);
296     mItemView.SetParentOrigin(ParentOrigin::CENTER);
297     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
298
299     // Display item view on the stage
300     stage.Add( mItemView );
301
302     // Create the layouts
303     mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
304     mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
305     mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
306
307     // Add the layouts to item view
308     mItemView.AddLayout(*mSpiralLayout);
309     mItemView.AddLayout(*mDepthLayout);
310     mItemView.AddLayout(*mGridLayout);
311
312     mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
313     mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
314
315     // Activate the spiral layout
316     SetLayout( mCurrentLayout );
317     mItemView.SetKeyboardFocusable( true );
318     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
319
320     // Set the title and icon to the current layout
321     SetLayoutTitle();
322     SetLayoutImage();
323
324     // Store one 1x1 white image for multiple items to share for backgrounds:
325     mWhiteImage = BufferImage::WHITE();
326   }
327
328   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
329   {
330     if ( !current && !proposed  )
331     {
332       return mItemView;
333     }
334
335     return proposed;
336   }
337
338   /**
339    * Animate to a different layout
340    */
341   void ChangeLayout()
342   {
343     Animation animation = Animation::New( mDurationSeconds );
344     animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
345     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
346     animation.Play();
347   }
348
349   void AnimationFinished( Animation& )
350   {
351     SetLayout( mCurrentLayout );
352
353     Animation animation = Animation::New( mDurationSeconds );
354     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
355     animation.Play();
356   }
357
358   /**
359    * Switch to a different item view layout
360    */
361   void SetLayout( int layoutId )
362   {
363     // Set the new orientation to the layout
364     mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
365
366     Vector2 stageSize = Stage::GetCurrent().GetSize();
367
368     if(layoutId == DEPTH_LAYOUT)
369     {
370       // Set up the depth layout according to the new orientation
371       if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
372       {
373         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
374       }
375       else
376       {
377         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
378       }
379     }
380
381     // Enable anchoring for depth layout only
382     mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
383
384     // Activate the layout
385     mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
386   }
387
388   /**
389    * Orientation changed signal callback
390    * @param orientation
391    */
392   void OnOrientationChanged( Orientation orientation )
393   {
394     const unsigned int angle = orientation.GetDegrees();
395
396     // If orientation really changed
397     if( mOrientation != angle )
398     {
399       // Remember orientation
400       mOrientation = angle;
401
402       SetLayout( mCurrentLayout );
403     }
404   }
405
406   bool OnLayoutButtonClicked( Toolkit::Button button )
407   {
408     // Switch to the next layout
409     mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
410
411     ChangeLayout();
412
413     SetLayoutTitle();
414     SetLayoutImage();
415
416     return true;
417   }
418
419   bool OnModeButtonClicked( Toolkit::Button button )
420   {
421     SwitchToNextMode();
422
423     return true;
424   }
425
426   void SwitchToNextMode()
427   {
428     switch( mMode )
429     {
430       case MODE_REMOVE:
431       {
432         ExitRemoveMode();
433         mMode = MODE_REMOVE_MANY;
434         EnterRemoveManyMode();
435         break;
436       }
437
438       case MODE_REMOVE_MANY:
439       {
440         ExitRemoveManyMode();
441         mMode = MODE_INSERT;
442         EnterInsertMode();
443         break;
444       }
445
446       case MODE_INSERT:
447       {
448         ExitInsertMode();
449         mMode = MODE_INSERT_MANY;
450         EnterInsertManyMode();
451         break;
452       }
453
454       case MODE_INSERT_MANY:
455       {
456         ExitInsertManyMode();
457         mMode = MODE_REPLACE;
458         EnterReplaceMode();
459         break;
460       }
461
462       case MODE_REPLACE:
463       {
464         ExitReplaceMode();
465         mMode = MODE_REPLACE_MANY;
466         EnterReplaceManyMode();
467         break;
468       }
469
470       case MODE_REPLACE_MANY:
471       {
472         ExitReplaceManyMode();
473         mMode = MODE_NORMAL;
474         SetLayoutTitle();
475         break;
476       }
477
478       case MODE_NORMAL:
479       default:
480       {
481         mMode = MODE_REMOVE;
482         EnterRemoveMode();
483         break;
484       }
485     }
486   }
487
488   void EnterRemoveMode()
489   {
490     SetTitle("Edit: Remove");
491
492     mTapDetector = TapGestureDetector::New();
493
494     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
495     {
496       mTapDetector.Attach(mItemView.GetChildAt(i));
497     }
498
499     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
500   }
501
502   void ExitRemoveMode()
503   {
504     mTapDetector.Reset();
505   }
506
507   void RemoveOnTap( Actor actor, const TapGesture& tap )
508   {
509     mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
510   }
511
512   void EnterRemoveManyMode()
513   {
514     SetTitle("Edit: Remove Many");
515
516     mDeleteButton.SetVisible( true );
517
518     mTapDetector = TapGestureDetector::New();
519
520     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
521     {
522       Actor child = mItemView.GetChildAt( i );
523       Actor box = child.FindChildByName( "CheckBox" );
524
525       if( box )
526       {
527         mTapDetector.Attach( child );
528         box.SetVisible( true );
529       }
530     }
531
532     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
533   }
534
535   void ExitRemoveManyMode()
536   {
537     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
538     {
539       Actor child = mItemView.GetChildAt( i );
540       Actor box = child.FindChildByName( "CheckBox" );
541
542       if( box )
543       {
544         box.SetVisible( false );
545
546         Actor tick = box.FindChildByName( "Tick" );
547         if( tick )
548         {
549           tick.SetVisible( false );
550         }
551       }
552     }
553
554     mTapDetector.Reset();
555
556     mDeleteButton.SetVisible( false );
557   }
558
559   void SelectOnTap( Actor actor, const TapGesture& tap )
560   {
561     Actor tick = actor.FindChildByName( "Tick" );
562     if( tick )
563     {
564       tick.SetVisible( !tick.IsVisible() );
565     }
566   }
567
568   bool OnDeleteButtonClicked( Toolkit::Button button )
569   {
570     ItemIdContainer removeList;
571
572     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
573     {
574       Actor child = mItemView.GetChildAt( i );
575       Actor tick = child.FindChildByName( "Tick" );
576
577       if( tick && tick.IsVisible() )
578       {
579         removeList.push_back( mItemView.GetItemId(child) );
580       }
581     }
582
583     if( ! removeList.empty() )
584     {
585       mItemView.RemoveItems( removeList, 0.5f );
586     }
587
588     return true;
589   }
590
591   void EnterInsertMode()
592   {
593     SetTitle("Edit: Insert");
594
595     mTapDetector = TapGestureDetector::New();
596
597     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
598     {
599       mTapDetector.Attach( mItemView.GetChildAt(i) );
600     }
601
602     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
603   }
604
605   void ExitInsertMode()
606   {
607     mTapDetector.Reset();
608   }
609
610   void InsertOnTap( Actor actor, const TapGesture& tap )
611   {
612     ItemId id = mItemView.GetItemId( actor );
613
614     Actor newActor = NewItem( rand() );
615
616     mItemView.InsertItem( Item(id,newActor), 0.5f );
617   }
618
619   void EnterInsertManyMode()
620   {
621     SetTitle("Edit: Insert Many");
622
623     mInsertButton.SetVisible( true );
624
625     mTapDetector = TapGestureDetector::New();
626
627     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
628     {
629       Actor child = mItemView.GetChildAt( i );
630       Actor box = child.FindChildByName( "CheckBox" );
631
632       if( box )
633       {
634         mTapDetector.Attach( child );
635         box.SetVisible( true );
636       }
637     }
638
639     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
640   }
641
642   void ExitInsertManyMode()
643   {
644     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
645     {
646       Actor child = mItemView.GetChildAt( i );
647       Actor box = child.FindChildByName( "CheckBox" );
648
649       if( box )
650       {
651         box.SetVisible( false );
652
653         Actor tick = box.FindChildByName( "Tick" );
654         if( tick )
655         {
656           tick.SetVisible( false );
657         }
658       }
659     }
660
661     mTapDetector.Reset();
662
663     mInsertButton.SetVisible( false );
664   }
665
666   bool OnInsertButtonClicked( Toolkit::Button button )
667   {
668     ItemContainer insertList;
669
670     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
671     {
672       Actor child = mItemView.GetChildAt( i );
673       Actor tick = child.FindChildByName( "Tick" );
674
675       if( tick && tick.IsVisible() )
676       {
677         insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
678       }
679     }
680
681     if( ! insertList.empty() )
682     {
683       mItemView.InsertItems( insertList, 0.5f );
684     }
685
686     return true;
687   }
688
689   void EnterReplaceMode()
690   {
691     SetTitle("Edit: Replace");
692
693     mTapDetector = TapGestureDetector::New();
694
695     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
696     {
697       mTapDetector.Attach(mItemView.GetChildAt(i));
698     }
699
700     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
701   }
702
703   void ReplaceOnTap( Actor actor, const TapGesture& tap )
704   {
705     mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
706   }
707
708   void ExitReplaceMode()
709   {
710     mTapDetector.Reset();
711   }
712
713   void EnterReplaceManyMode()
714   {
715     SetTitle("Edit: Replace Many");
716
717     mReplaceButton.SetVisible( true );
718
719     mTapDetector = TapGestureDetector::New();
720
721     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
722     {
723       Actor child = mItemView.GetChildAt( i );
724       Actor box = child.FindChildByName( "CheckBox" );
725
726       if( box )
727       {
728         mTapDetector.Attach( child );
729         box.SetVisible( true );
730       }
731     }
732
733     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
734   }
735
736   void ExitReplaceManyMode()
737   {
738     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
739     {
740       Actor child = mItemView.GetChildAt( i );
741       Actor box = child.FindChildByName( "CheckBox" );
742
743       if( box )
744       {
745         box.SetVisible( false );
746
747         Actor tick = box.FindChildByName( "Tick" );
748         if( tick )
749         {
750           tick.SetVisible( false );
751         }
752       }
753     }
754
755     mTapDetector.Reset();
756
757     mReplaceButton.SetVisible( false );
758   }
759
760   bool OnReplaceButtonClicked( Toolkit::Button button )
761   {
762     ItemContainer replaceList;
763
764     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
765     {
766       Actor child = mItemView.GetChildAt( i );
767       Actor tick = child.FindChildByName( "Tick" );
768
769       if( tick && tick.IsVisible() )
770       {
771         replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
772       }
773     }
774
775     if( ! replaceList.empty() )
776     {
777       mItemView.ReplaceItems( replaceList, 0.5f );
778     }
779
780     return true;
781   }
782
783   void SetLayoutTitle()
784   {
785     if( MODE_NORMAL == mMode )
786     {
787       std::stringstream ss(APPLICATION_TITLE);
788       switch(mCurrentLayout)
789       {
790       case SPIRAL_LAYOUT:
791         ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
792         break;
793       case GRID_LAYOUT:
794         ss << APPLICATION_TITLE << ": " << GRID_LABEL;
795         break;
796       case DEPTH_LAYOUT:
797         ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
798         break;
799       default:
800         break;
801       }
802       SetTitle(ss.str());
803     }
804   }
805
806   void SetLayoutImage()
807   {
808     if( mLayoutButton )
809     {
810       switch( mCurrentLayout )
811       {
812         case SPIRAL_LAYOUT:
813         {
814           mLayoutButton.SetButtonImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
815           mLayoutButton.SetSelectedImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE_SELECTED ) );
816           break;
817         }
818
819         case GRID_LAYOUT:
820         {
821           mLayoutButton.SetButtonImage( ResourceImage::New( GRID_LAYOUT_IMAGE ) );
822           mLayoutButton.SetSelectedImage( ResourceImage::New( GRID_LAYOUT_IMAGE_SELECTED ) );
823           break;
824         }
825
826         case DEPTH_LAYOUT:
827         {
828           mLayoutButton.SetButtonImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE ) );
829           mLayoutButton.SetSelectedImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE_SELECTED ) );
830           break;
831         }
832
833         default:
834           break;
835       }
836     }
837   }
838
839 public: // From ItemFactory
840
841   /**
842    * Query the number of items available from the factory.
843    * The maximum available item has an ID of GetNumberOfItems() - 1.
844    */
845   virtual unsigned int GetNumberOfItems()
846   {
847     return NUM_IMAGES * 10;
848   }
849
850   /**
851    * Create an Actor to represent a visible item.
852    * @param itemId
853    * @return the created actor.
854    */
855   virtual Actor NewItem(unsigned int itemId)
856   {
857     // Create an image actor for this item
858     unsigned int imageId = itemId % NUM_IMAGES;
859     ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
860                                      (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
861                                       IMAGE_WIDTH,
862                                       IMAGE_HEIGHT );
863     Actor actor = ImageActor::New(mImageAtlas, pixelArea);
864     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
865
866     // Add a border image child actor
867     ImageActor borderActor = ImageActor::New(mBorderImage);
868     borderActor.SetParentOrigin( ParentOrigin::CENTER );
869     borderActor.SetAnchorPoint( AnchorPoint::CENTER );
870     borderActor.SetPosition( 0.f, 0.f, 1.f );
871     borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
872     borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
873     borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
874     borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
875     borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
876     actor.Add(borderActor);
877     actor.SetKeyboardFocusable( true );
878
879     Vector3 spiralItemSize;
880     static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
881
882     // Add a checkbox child actor; invisible until edit-mode is enabled
883
884     ImageActor checkbox = ImageActor::New( mWhiteImage );
885     checkbox.SetName( "CheckBox" );
886     checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
887     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
888     checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
889     checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
890     checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
891     checkbox.SetZ( 1.0f );
892     if( MODE_REMOVE_MANY  != mMode &&
893         MODE_INSERT_MANY  != mMode &&
894         MODE_REPLACE_MANY != mMode )
895     {
896       checkbox.SetVisible( false );
897     }
898     actor.Add( checkbox );
899
900     ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
901     tick.SetColorMode( USE_OWN_COLOR );
902     tick.SetName( "Tick" );
903     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
904     tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
905     tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
906     tick.SetZ( 1.0f );
907     tick.SetVisible( false );
908     checkbox.Add( tick );
909
910     // Connect new items for various editing modes
911     if( mTapDetector )
912     {
913       mTapDetector.Attach( actor );
914     }
915
916     return actor;
917   }
918
919 private:
920
921   /**
922    * Create an Atlas to tile the images inside.
923    */
924   Atlas CreateImageAtlas()
925   {
926     const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
927     const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
928     Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
929
930     for( unsigned int i = 0; i < NUM_IMAGES; i++ )
931     {
932       atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
933     }
934
935     return atlas;
936   }
937
938   /**
939    * Sets/Updates the title of the View
940    * @param[in] title The new title for the view.
941    */
942   void SetTitle(const std::string& title)
943   {
944     if(!mTitleActor)
945     {
946       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
947       // Add title to the tool bar.
948       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
949     }
950
951     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
952   }
953
954   /**
955    * Main key event handler
956    */
957   void OnKeyEvent(const KeyEvent& event)
958   {
959     if(event.state == KeyEvent::Down)
960     {
961       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
962       {
963         mApplication.Quit();
964       }
965     }
966   }
967
968 private:
969
970   Application& mApplication;
971   Mode mMode;
972
973   Toolkit::Control mView;
974   unsigned int mOrientation;
975
976   Toolkit::ToolBar mToolBar;
977   TextLabel mTitleActor;             ///< The Toolbar's Title.
978
979   ItemView mItemView;
980   Image mBorderImage;
981   Atlas mImageAtlas;
982   unsigned int mCurrentLayout;
983   float mDurationSeconds;
984
985   ItemLayoutPtr mSpiralLayout;
986   ItemLayoutPtr mDepthLayout;
987   ItemLayoutPtr mGridLayout;
988
989   TapGestureDetector mTapDetector;
990   Toolkit::PushButton mLayoutButton;
991   Toolkit::PushButton mDeleteButton;
992   Toolkit::PushButton mInsertButton;
993   Toolkit::PushButton mReplaceButton;
994
995   BufferImage mWhiteImage;
996 };
997
998 void RunTest(Application& app)
999 {
1000   ItemViewExample test(app);
1001
1002   app.MainLoop();
1003 }
1004
1005 int main(int argc, char **argv)
1006 {
1007   Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);
1008
1009   RunTest(app);
1010
1011   return 0;
1012 }