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