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