summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/GenList.cs
blob: 74b7202628443abf48f603e3a481e4b64278f0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;

namespace ElmSharp
{
    /// <summary>
    /// Enumeration for setting genlist item type.
    /// </summary>
    public enum GenListItemType
    {
        /// <summary>
        /// if Normal is set then this item is normal item.
        /// </summary>
        Normal = 0,

        /// <summary>
        /// If tree is set then this item is displayed as an item that is able to expand and have child items.
        /// </summary>
        Tree = (1 << 0),

        /// <summary>
        /// if Group is set then this item is group index item that is displayed at the top until the next group comes.
        /// </summary>
        Group = (1 << 1),
    }

    /// <summary>
    /// Enumeration for setting genlist's resizing behavior, transverse axis scrolling and items cropping.
    /// </summary>
    public enum GenListMode
    {
        /// <summary>
        /// The genlist won't set any of its size hints to inform how a possible container should resize it.
        /// Then, if it's not created as a "resize object", it might end with zeroed dimensions.
        /// The genlist will respect the container's geometry and, if any of its items won't fit into its transverse axis, one won't be able to scroll it in that direction.
        /// </summary>
        Compress = 0,

        /// <summary>
        /// This is the same as Compress, with the exception that if any of its items won't fit into its transverse axis, one will be able to scroll it in that direction.
        /// </summary>
        Scroll,

        /// <summary>
        /// Sets a minimum size hint on the genlist object, so that containers may respect it (and resize itself to fit the child properly).
        /// More specifically, a minimum size hint will be set for its transverse axis, so that the largest item in that direction fits well.
        /// This is naturally bound by the genlist object's maximum size hints, set externally.
        /// </summary>
        Limit,

        /// <summary>
        /// Besides setting a minimum size on the transverse axis, just like on Limit, the genlist will set a minimum size on th longitudinal axis, trying to reserve space to all its children to be visible at a time.
        /// This is naturally bound by the genlist object's maximum size hints, set externally.
        /// </summary>
        Expand
    }

    /// <summary>
    /// It inherits System.EventArgs.
    /// It contains Item which is <see cref="GenListItem"/> type.
    /// All events of GenList contain GenListItemEventArgs as a parameter.
    /// </summary>
    public class GenListItemEventArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets GenList item. The return type is <see cref="GenListItem"/>.
        /// </summary>
        public GenListItem Item { get; set; }

        internal static GenListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
        {
            GenListItem item = ItemObject.GetItemByHandle(info) as GenListItem;
            return new GenListItemEventArgs() { Item = item };
        }
    }

    /// <summary>
    /// Enumeration that defines where to position the item in the genlist.
    /// </summary>
    public enum ScrollToPosition
    {
        /// <summary>
        /// Scrolls to nowhere.
        /// </summary>
        None = 0,

        /// <summary>
        /// Scrolls to the nearest viewport.
        /// </summary>
        In = (1 << 0),

        /// <summary>
        /// Scrolls to the top of the viewport.
        /// </summary>
        Top = (1 << 1),

        /// <summary>
        /// Scrolls to the middle of the viewport.
        /// </summary>
        Middle = (1 << 2),

        /// <summary>
        /// Scrolls to the bottom of the viewport.
        /// </summary>
        Bottom = (1 << 3)
    }

    /// <summary>
    /// It inherits <see cref="Layout"/>.
    /// The GenList is a widget that aims to have more expansive list than the simple <see cref="List"/> in ElmSharp that could have more flexible items and allow many more entries while still being fast and low on memory usage.
    /// At the same time it was also made to be able to do tree structures.
    /// But the price to pay is more complexity when it comes to usage.
    /// If all you want is a simple list with icons and a single text, use the <see cref="List"/> widget.
    /// </summary>
    public class GenList : Layout
    {
        HashSet<GenListItem> _children = new HashSet<GenListItem>();

        SmartEvent<GenListItemEventArgs> _selected;
        SmartEvent<GenListItemEventArgs> _unselected;
        SmartEvent<GenListItemEventArgs> _activated;
        SmartEvent<GenListItemEventArgs> _pressed;
        SmartEvent<GenListItemEventArgs> _released;
        SmartEvent<GenListItemEventArgs> _doubleClicked;
        SmartEvent<GenListItemEventArgs> _expanded;
        SmartEvent<GenListItemEventArgs> _realized;
        SmartEvent<GenListItemEventArgs> _unrealized;
        SmartEvent<GenListItemEventArgs> _longpressed;
        SmartEvent<GenListItemEventArgs> _moved;
        SmartEvent<GenListItemEventArgs> _movedAfter;
        SmartEvent<GenListItemEventArgs> _movedBefore;
        SmartEvent _scrollAnimationStarted;
        SmartEvent _scrollAnimationStopped;
        SmartEvent _changed;

        /// <summary>
        /// Creates and initializes a new instance of the GenList class.
        /// </summary>
        /// <param name="parent">The parent is a given container which will be attached by GenList as a child. It's <see cref="EvasObject"/> type.</param>
        public GenList(EvasObject parent) : base(parent)
        {
            ListMode = GenListMode.Compress;
            InitializeSmartEvent();
        }

        /// <summary>
        /// Gets or sets whether the homogeneous mode is enabled.
        /// </summary>
        /// <remarks>
        /// If true, the genlist items have same height and width.
        /// </remarks>
        public bool Homogeneous
        {
            get
            {
                return Interop.Elementary.elm_genlist_homogeneous_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_homogeneous_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets or sets the horizontal stretching mode. This mode used for sizing items horizontally.
        /// The default value is <see cref="GenListMode.Scroll"/> which means that if items are too wide to fit, the scroller scrolls horizontally.
        /// If set <see cref="GenListMode.Compress"/> which means that the item width is fixed (restricted to a minimum of) to the list width when calculating its size in order to allow the height to be calculated based on it.
        /// If set <see cref="GenListMode.Limit"/> which means that items are expanded to the viewport width and limited to that size.
        /// if set <see cref="GenListMode.Expand"/> which means that genlist try to reserve space to all its items to be visible at a time.
        /// </summary>
        /// <remarks>
        /// Compress makes genlist resize slower as it recalculates every item height again whenever the list width changes.
        /// The homogeneous mode is so that all items in the genlist are of the same width/height. With Compress, genlist items are initialized fast.
        /// However, there are no sub-objects in the genlist which can be on the flying resizable (such as TEXTBLOCK).
        /// If so, then some dynamic resizable objects in the genlist would not be diplayed properly.
        /// </remarks>
        public GenListMode ListMode
        {
            get
            {
                return (GenListMode)Interop.Elementary.elm_genlist_mode_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_mode_set(RealHandle, (int)value);
            }
        }

        /// <summary>
        /// Gets the first item in the genlist.
        /// </summary>
        public GenListItem FirstItem
        {
            get
            {
                IntPtr handle = Interop.Elementary.elm_genlist_first_item_get(RealHandle);
                return ItemObject.GetItemByHandle(handle) as GenListItem;
            }
        }

        /// <summary>
        /// Gets the last item in the genlist.
        /// </summary>
        public GenListItem LastItem
        {
            get
            {
                IntPtr handle = Interop.Elementary.elm_genlist_last_item_get(RealHandle);
                return ItemObject.GetItemByHandle(handle) as GenListItem;
            }
        }

        /// <summary>
        /// Gets or sets the reorder mode.
        /// After turning on the reorder mode, longpress on a normal item triggers reordering of the item.
        /// You can move the item up and down. However, reordering does not work with group items.
        /// </summary>
        public bool ReorderMode
        {
            get
            {
                return Interop.Elementary.elm_genlist_reorder_mode_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_reorder_mode_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets or set the maximum number of items within an item block.
        /// </summary>
        public int BlockCount
        {
            get
            {
                return Interop.Elementary.elm_genlist_block_count_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_block_count_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets or sets whether the genlist items should be highlighted when an item is selected.
        /// </summary>
        public bool IsHighlight
        {
            get
            {
                return Interop.Elementary.elm_genlist_highlight_mode_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_highlight_mode_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets the depth of expanded item.
        /// </summary>
        public int ExpandedItemDepth
        {
            get
            {
                return Interop.Elementary.elm_genlist_item_expanded_depth_get(RealHandle);
            }
        }

        /// <summary>
        /// Gets or sets the timeout in seconds for the longpress event.
        /// </summary>
        public double LongPressTimeout
        {
            get
            {
                return Interop.Elementary.elm_genlist_longpress_timeout_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_longpress_timeout_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets or sets whether enable multi-selection in the genlist.
        /// </summary>
        public bool IsMultiSelection
        {
            get
            {
                return Interop.Elementary.elm_genlist_multi_select_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_multi_select_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets the selected item in a given genlist widget.
        /// </summary>
        public GenListItem SelectedItem
        {
            get
            {
                IntPtr handle = Interop.Elementary.elm_genlist_selected_item_get(RealHandle);
                return ItemObject.GetItemByHandle(handle) as GenListItem;
            }
        }

        /// <summary>
        /// Gets or sets the genlist select mode by <see cref="GenItemSelectionMode"/>.
        /// </summary>
        public GenItemSelectionMode SelectionMode
        {
            get
            {
                return (GenItemSelectionMode)Interop.Elementary.elm_genlist_select_mode_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_genlist_select_mode_set(RealHandle, (int)value);
            }
        }

        /// <summary>
        /// ItemSelected is raised when a new genlist item is selected.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemSelected;

        /// <summary>
        /// ItemUnselected is raised when the genlist item is Unselected.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemUnselected;

        /// <summary>
        /// ItemPressed is raised when a new genlist item is pressed.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemPressed;

        /// <summary>
        /// ItemReleased is raised when a new genlist item is released.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemReleased;

        /// <summary>
        /// ItemActivated is raised when a new genlist item is double clicked or pressed (enter|return|spacebar).
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemActivated;

        /// <summary>
        /// ItemDoubleClicked is raised when a new genlist item is double clicked.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemDoubleClicked;

        /// <summary>
        /// ItemExpanded is raised when a new genlist item is indicated to expand.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemExpanded;

        /// <summary>
        /// ItemRealized is raised when a new genlist item is created as a real object.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemRealized;

        /// <summary>
        /// ItemUnrealized is raised when a new genlist item is unrealized.
        /// After calling unrealize, the item's content objects are deleted and the item object itself is deleted or is put into a floating cache.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemUnrealized;

        /// <summary>
        /// ItemLongPressed is raised when a genlist item is pressed for a certain amount of time. By default it's 1 second.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemLongPressed;

        /// <summary>
        /// ItemMoved is raised when a genlist item is moved in the reorder mode.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemMoved;

        /// <summary>
        /// ItemMovedAfter is raised when a genlist item is moved after another item in the reorder mode.
        /// To get the relative previous item, use <see cref="GenListItem.Previous"/>.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemMovedAfter;

        /// <summary>
        /// ItemMovedBefore is raised when a genlist item is moved before another item in the reorder mode.
        /// To get the relative next item, use <see cref="GenListItem.Next"/>.
        /// </summary>
        public event EventHandler<GenListItemEventArgs> ItemMovedBefore;

        /// <summary>
        /// Changed is raised when genlist is changed.
        /// </summary>
        public event EventHandler Changed
        {
            add { _changed.On += value; }
            remove { _changed.On -= value; }
        }

        /// <summary>
        /// ScrollAnimationStarted is raised when scrolling animation has started.
        /// </summary>
        public event EventHandler ScrollAnimationStarted
        {
            add { _scrollAnimationStarted.On += value; }
            remove { _scrollAnimationStarted.On -= value; }
        }

        /// <summary>
        /// ScrollAnimationStopped is raised when scrolling animation has stopped.
        /// </summary>
        public event EventHandler ScrollAnimationStopped
        {
            add { _scrollAnimationStopped.On += value; }
            remove { _scrollAnimationStopped.On -= value; }
        }

        /// <summary>
        /// Appends a new item to the end of a given genlist widget.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        /// <seealso cref="GenItemClass"/>
        /// <seealso cref="GenListItem"/>
        public GenListItem Append(GenItemClass itemClass, object data)
        {
            return Append(itemClass, data, GenListItemType.Normal);
        }

        /// <summary>
        /// Appends a new item with <see cref="GenListItemType"/> to the end of a given genlist widget.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="type">The genlist item type.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type)
        {
            return Append(itemClass, data, type, null);
        }

        /// <summary>
        /// Appends a new item with <see cref="GenListItemType"/> to the end of a given genlist widget or the end of the children list if the parent is given.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="type">The genlist item type.</param>
        /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
        {
            GenListItem item = new GenListItem(data, itemClass);
            IntPtr handle = Interop.Elementary.elm_genlist_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
            item.Handle = handle;
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Prepends a new item to the beginning of a given genlist widget.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem Prepend(GenItemClass itemClass, object data)
        {
            return Prepend(itemClass, data, GenListItemType.Normal);
        }

        /// <summary>
        /// Prepends a new item with <see cref="GenListItemType"/> to the beginning of a given genlist widget.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="type">The genlist item type.</param>
        /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type)
        {
            return Prepend(itemClass, data, type, null);
        }

        /// <summary>
        /// Prepends a new item with <see cref="GenListItemType"/> to the beginning of a given genlist widget or the beginning of the children list if the parent is given.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="type">The genlist item type.</param>
        /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
        {
            GenListItem item = new GenListItem(data, itemClass);
            IntPtr handle = Interop.Elementary.elm_genlist_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
            item.Handle = handle;
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Inserts an item before another item in a genlist widget.
        /// It is the same tree level or group as the item before which it is inserted.????
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="before">The item before which to place this new one.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before)
        {
            return InsertBefore(itemClass, data, before, GenListItemType.Normal);
        }

        /// <summary>
        /// Inserts an item with <see cref="GenListItemType"/> before another item in a genlist widget.
        /// It is the same tree level or group as the item before which it is inserted.????
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="before">The item before which to place this new one.</param>
        /// <param name="type">The genlist item type.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type)
        {
            return InsertBefore(itemClass, data, before, type, null);
        }

        /// <summary>
        /// Inserts an item with <see cref="GenListItemType"/> before another item under a parent in a genlist widget.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="before">The item before which to place this new one.</param>
        /// <param name="type">The genlist item type.</param>
        /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type, GenListItem parent)
        {
            GenListItem item = new GenListItem(data, itemClass);
            // insert before the `before` list item
            IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
                RealHandle, // genlist handle
                itemClass.UnmanagedPtr, // item class
                (IntPtr)item.Id, // data
                parent, // parent
                before, // before
                (int)type, // item type
                null, // select callback
                (IntPtr)item.Id); // callback data
            item.Handle = handle;
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Inserts an item with <see cref="GenListItemType"/> after another item under a parent in a genlist widget.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="before">The item after which to place this new one.</param>
        /// <param name="type">The genlist item type.</param>
        /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
        /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
        public GenListItem InsertAfter(GenItemClass itemClass, object data, GenListItem after, GenListItemType type, GenListItem parent)
        {
            GenListItem item = new GenListItem(data, itemClass);
            // insert before the `before` list item
            IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
                RealHandle, // genlist handle
                itemClass.UnmanagedPtr, // item class
                (IntPtr)item.Id, // data
                parent, // parent
                after, // after
                (int)type, // item type
                null, // select callback
                (IntPtr)item.Id); // callback data
            item.Handle = handle;
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Insert an item in a genlist widget using a user-defined sort function.
        /// </summary>
        /// <param name="itemClass">The itemClass defines how to display the data.</param>
        /// <param name="data">The item data.</param>
        /// <param name="func">User defined comparison function that defines the sort order based on genlist item and its data.</param>
        /// <param name="type">The genlist item type.</param>
        /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
        /// <returns>Return a genlist item that contains data and itemClass.</returns>
        public GenListItem InsertSorted(GenItemClass itemClass, object data, Comparison<GenListItem> comparison, GenListItemType type, GenListItem parent)
        {
            Interop.Elementary.Eina_Compare_Cb compareCallback = (handle1, handle2) =>
            {
                GenListItem item1 = ItemObject.GetItemByHandle(handle1) as GenListItem;
                GenListItem item2 = ItemObject.GetItemByHandle(handle2) as GenListItem;
                return comparison(item1, item2);
            };

            GenListItem item = new GenListItem(data, itemClass);

            IntPtr handle = Interop.Elementary.elm_genlist_item_sorted_insert(
                RealHandle, // genlist handle
                itemClass.UnmanagedPtr, // item clas
                (IntPtr)item.Id, // data
                parent, // parent
                (int)type, // item type
                compareCallback, // compare callback
                null, //select callback
                (IntPtr)item.Id); // callback data
            item.Handle = handle;
            AddInternal(item);
            return item;
        }

        /// <summary>
        /// Shows the given item with position type in a genlist.
        /// When animated is true, genlist will jump to the given item and display it (by animatedly scrolling), if it is not fully visible. This may use animation and may take some time.
        /// When animated is false, genlist will jump to the given item and display it (by jumping to that position), if it is not fully visible.
        /// </summary>
        /// <param name="item">The item to display.</param>
        /// <param name="position">The position to show the given item to <see cref="ScrollToPosition"/>.</param>
        /// <param name="animated">The animated indicates how to display the item, by scrolling or by jumping.</param>
        public void ScrollTo(GenListItem item, ScrollToPosition position, bool animated)
        {
            if (animated)
            {
                Interop.Elementary.elm_genlist_item_bring_in(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
            }
            else
            {
                Interop.Elementary.elm_genlist_item_show(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
            }
        }

        /// <summary>
        /// Updates the content of all the realized items.
        /// This updates all the realized items by calling all the <see cref="GenItemClass"/> again to get the content, text and states.
        /// Use this when the original item data has changed and the changes are desired to reflect.
        /// To update just one item, use <see cref="GenListItem.Update"/>.
        /// </summary>
        /// <seealso cref="GenListItem.Update"/>
        public void UpdateRealizedItems()
        {
            Interop.Elementary.elm_genlist_realized_items_update(RealHandle);
        }

        /// <summary>
        /// Removes all items from a given genlist widget.
        /// This removes (and deletes) all items in obj, making it empty.
        /// To delete just one item, use <see cref="ItemObject.Delete"/>.
        /// </summary>
        /// <seealso cref="ItemObject.Delete"/>
        public void Clear()
        {
            Interop.Elementary.elm_genlist_clear(RealHandle);
        }

        /// <summary>
        /// Get the item that is at the x, y canvas coords.
        /// </summary>
        /// <param name="x">The input x coordinate</param>
        /// <param name="y">The input y coordinate</param>
        /// <param name="pos">The position relative to the item returned here
        ///  -1, 0, or 1, depending on whether the coordinate is on the upper portion of that item (-1), in the middle section (0), or on the lower part (1).
        /// </param>
        /// <returns></returns>
        public GenListItem GetItemByPosition(int x, int y, out int pos)
        {
            IntPtr handle = Interop.Elementary.elm_genlist_at_xy_item_get(RealHandle, x, y, out pos);
            return ItemObject.GetItemByHandle(handle) as GenListItem;
        }

        /// <summary>
        /// Gets the nth item in a given genlist widget, placed at position nth, in its internal items list.
        /// </summary>
        /// <param name="index">The number of the item to grab (0 being the first)</param>
        /// <returns></returns>
        public GenListItem GetItemByIndex(int index)
        {
            IntPtr handle = Interop.Elementary.elm_genlist_nth_item_get(RealHandle, index);
            return ItemObject.GetItemByHandle(handle) as GenListItem;
        }

        protected override IntPtr CreateHandle(EvasObject parent)
        {
            IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
            Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");

            RealHandle = Interop.Elementary.elm_genlist_add(handle);
            Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);

            return handle;
        }

        void InitializeSmartEvent()
        {
            _selected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "selected", GenListItemEventArgs.CreateFromSmartEvent);
            _unselected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unselected", GenListItemEventArgs.CreateFromSmartEvent);
            _activated = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "activated", GenListItemEventArgs.CreateFromSmartEvent);
            _pressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "pressed", GenListItemEventArgs.CreateFromSmartEvent);
            _released = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "released", GenListItemEventArgs.CreateFromSmartEvent);
            _doubleClicked = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "clicked,double", GenListItemEventArgs.CreateFromSmartEvent);
            _expanded = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "expanded", GenListItemEventArgs.CreateFromSmartEvent);
            _realized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "realized", GenListItemEventArgs.CreateFromSmartEvent);
            _unrealized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unrealized", GenListItemEventArgs.CreateFromSmartEvent);
            _longpressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "longpressed", GenListItemEventArgs.CreateFromSmartEvent);
            _moved = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved", GenListItemEventArgs.CreateFromSmartEvent);
            _movedAfter = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,after", GenListItemEventArgs.CreateFromSmartEvent);
            _movedBefore = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,before", GenListItemEventArgs.CreateFromSmartEvent);
            _scrollAnimationStarted = new SmartEvent(this, this.RealHandle, "scroll,anim,start");
            _scrollAnimationStopped = new SmartEvent(this, this.RealHandle, "scroll,anim,stop");
            _changed = new SmartEvent(this, this.RealHandle, "changed");

            _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
            _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
            _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
            _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
            _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
            _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
            _expanded.On += (s, e) => { if (e.Item != null) ItemExpanded?.Invoke(this, e); };
            _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
            _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
            _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
            _moved.On += (s, e) => { if (e.Item != null) ItemMoved?.Invoke(this, e); };
            _movedAfter.On += (s, e) => { if (e.Item != null) ItemMovedAfter?.Invoke(this, e); };
            _movedBefore.On += (s, e) => { if (e.Item != null) ItemMovedBefore?.Invoke(this, e); };
        }

        void AddInternal(GenListItem item)
        {
            _children.Add(item);
            item.Deleted += Item_Deleted;
        }

        void Item_Deleted(object sender, EventArgs e)
        {
            _children.Remove((GenListItem)sender);
        }
    }
}