600c57b96c50ef4680b03aa038d8b4bede3b9a36
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / GenList.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.Collections.Generic;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// Enumeration for setting genlist item type.
24     /// </summary>
25     public enum GenListItemType
26     {
27         /// <summary>
28         /// if Normal is set then this item is normal item.
29         /// </summary>
30         Normal = 0,
31
32         /// <summary>
33         /// If tree is set then this item is displayed as an item that is able to expand and have child items.
34         /// </summary>
35         Tree = (1 << 0),
36
37         /// <summary>
38         /// if Group is set then this item is group index item that is displayed at the top until the next group comes.
39         /// </summary>
40         Group = (1 << 1),
41     }
42
43     /// <summary>
44     /// Enumeration for setting genlist's resizing behavior, transverse axis scrolling and items cropping.
45     /// </summary>
46     public enum GenListMode
47     {
48         /// <summary>
49         /// The genlist won't set any of its size hints to inform how a possible container should resize it.
50         /// Then, if it's not created as a "resize object", it might end with zeroed dimensions.
51         /// 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.
52         /// </summary>
53         Compress = 0,
54
55         /// <summary>
56         /// 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.
57         /// </summary>
58         Scroll,
59
60         /// <summary>
61         /// Sets a minimum size hint on the genlist object, so that containers may respect it (and resize itself to fit the child properly).
62         /// More specifically, a minimum size hint will be set for its transverse axis, so that the largest item in that direction fits well.
63         /// This is naturally bound by the genlist object's maximum size hints, set externally.
64         /// </summary>
65         Limit,
66
67         /// <summary>
68         /// 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.
69         /// This is naturally bound by the genlist object's maximum size hints, set externally.
70         /// </summary>
71         Expand
72     }
73
74     /// <summary>
75     /// It inherits System.EventArgs.
76     /// It contains Item which is <see cref="GenListItem"/> type.
77     /// All events of GenList contain GenListItemEventArgs as a parameter.
78     /// </summary>
79     public class GenListItemEventArgs : EventArgs
80     {
81         /// <summary>
82         /// Gets or sets GenList item. The return type is <see cref="GenListItem"/>.
83         /// </summary>
84         public GenListItem Item { get; set; }
85
86         internal static GenListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
87         {
88             GenListItem item = ItemObject.GetItemByHandle(info) as GenListItem;
89             return new GenListItemEventArgs() { Item = item };
90         }
91     }
92
93     /// <summary>
94     /// Enumeration that defines where to position the item in the genlist.
95     /// </summary>
96     public enum ScrollToPosition
97     {
98         /// <summary>
99         /// Scrolls to nowhere.
100         /// </summary>
101         None = 0,
102
103         /// <summary>
104         /// Scrolls to the nearest viewport.
105         /// </summary>
106         In = (1 << 0),
107
108         /// <summary>
109         /// Scrolls to the top of the viewport.
110         /// </summary>
111         Top = (1 << 1),
112
113         /// <summary>
114         /// Scrolls to the middle of the viewport.
115         /// </summary>
116         Middle = (1 << 2),
117
118         /// <summary>
119         /// Scrolls to the bottom of the viewport.
120         /// </summary>
121         Bottom = (1 << 3)
122     }
123
124     /// <summary>
125     /// It inherits <see cref="Layout"/>.
126     /// 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.
127     /// At the same time it was also made to be able to do tree structures.
128     /// But the price to pay is more complexity when it comes to usage.
129     /// If all you want is a simple list with icons and a single text, use the <see cref="List"/> widget.
130     /// </summary>
131     public class GenList : Layout
132     {
133         HashSet<GenListItem> _children = new HashSet<GenListItem>();
134
135         SmartEvent<GenListItemEventArgs> _selected;
136         SmartEvent<GenListItemEventArgs> _unselected;
137         SmartEvent<GenListItemEventArgs> _activated;
138         SmartEvent<GenListItemEventArgs> _pressed;
139         SmartEvent<GenListItemEventArgs> _released;
140         SmartEvent<GenListItemEventArgs> _doubleClicked;
141         SmartEvent<GenListItemEventArgs> _expanded;
142         SmartEvent<GenListItemEventArgs> _realized;
143         SmartEvent<GenListItemEventArgs> _unrealized;
144         SmartEvent<GenListItemEventArgs> _longpressed;
145         SmartEvent<GenListItemEventArgs> _moved;
146         SmartEvent<GenListItemEventArgs> _movedAfter;
147         SmartEvent<GenListItemEventArgs> _movedBefore;
148         SmartEvent _scrollAnimationStarted;
149         SmartEvent _scrollAnimationStopped;
150         SmartEvent _changed;
151
152         /// <summary>
153         /// Creates and initializes a new instance of the GenList class.
154         /// </summary>
155         /// <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>
156         public GenList(EvasObject parent) : base(parent)
157         {
158         }
159
160         /// <summary>
161         /// Creates and initializes a new instance of GenList class.
162         /// </summary>
163         protected GenList() : base()
164         {
165         }
166
167         /// <summary>
168         /// Gets or sets whether the homogeneous mode is enabled.
169         /// </summary>
170         /// <remarks>
171         /// If true, the genlist items have same height and width.
172         /// </remarks>
173         public bool Homogeneous
174         {
175             get
176             {
177                 return Interop.Elementary.elm_genlist_homogeneous_get(RealHandle);
178             }
179             set
180             {
181                 Interop.Elementary.elm_genlist_homogeneous_set(RealHandle, value);
182             }
183         }
184
185         /// <summary>
186         /// Gets or sets the horizontal stretching mode. This mode used for sizing items horizontally.
187         /// The default value is <see cref="GenListMode.Scroll"/> which means that if items are too wide to fit, the scroller scrolls horizontally.
188         /// 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.
189         /// If set <see cref="GenListMode.Limit"/> which means that items are expanded to the viewport width and limited to that size.
190         /// if set <see cref="GenListMode.Expand"/> which means that genlist try to reserve space to all its items to be visible at a time.
191         /// </summary>
192         /// <remarks>
193         /// Compress makes genlist resize slower as it recalculates every item height again whenever the list width changes.
194         /// The homogeneous mode is so that all items in the genlist are of the same width/height. With Compress, genlist items are initialized fast.
195         /// However, there are no sub-objects in the genlist which can be on the flying resizable (such as TEXTBLOCK).
196         /// If so, then some dynamic resizable objects in the genlist would not be diplayed properly.
197         /// </remarks>
198         public GenListMode ListMode
199         {
200             get
201             {
202                 return (GenListMode)Interop.Elementary.elm_genlist_mode_get(RealHandle);
203             }
204             set
205             {
206                 Interop.Elementary.elm_genlist_mode_set(RealHandle, (int)value);
207             }
208         }
209
210         /// <summary>
211         /// Gets the first item in the genlist.
212         /// </summary>
213         public GenListItem FirstItem
214         {
215             get
216             {
217                 IntPtr handle = Interop.Elementary.elm_genlist_first_item_get(RealHandle);
218                 return ItemObject.GetItemByHandle(handle) as GenListItem;
219             }
220         }
221
222         /// <summary>
223         /// Gets the last item in the genlist.
224         /// </summary>
225         public GenListItem LastItem
226         {
227             get
228             {
229                 IntPtr handle = Interop.Elementary.elm_genlist_last_item_get(RealHandle);
230                 return ItemObject.GetItemByHandle(handle) as GenListItem;
231             }
232         }
233
234         /// <summary>
235         /// Gets or sets the reorder mode.
236         /// After turning on the reorder mode, longpress on a normal item triggers reordering of the item.
237         /// You can move the item up and down. However, reordering does not work with group items.
238         /// </summary>
239         public bool ReorderMode
240         {
241             get
242             {
243                 return Interop.Elementary.elm_genlist_reorder_mode_get(RealHandle);
244             }
245             set
246             {
247                 Interop.Elementary.elm_genlist_reorder_mode_set(RealHandle, value);
248             }
249         }
250
251         /// <summary>
252         /// Gets or set the maximum number of items within an item block.
253         /// </summary>
254         public int BlockCount
255         {
256             get
257             {
258                 return Interop.Elementary.elm_genlist_block_count_get(RealHandle);
259             }
260             set
261             {
262                 Interop.Elementary.elm_genlist_block_count_set(RealHandle, value);
263             }
264         }
265
266         /// <summary>
267         /// Gets or sets whether the genlist items should be highlighted when an item is selected.
268         /// </summary>
269         public bool IsHighlight
270         {
271             get
272             {
273                 return Interop.Elementary.elm_genlist_highlight_mode_get(RealHandle);
274             }
275             set
276             {
277                 Interop.Elementary.elm_genlist_highlight_mode_set(RealHandle, value);
278             }
279         }
280
281         /// <summary>
282         /// Gets or sets the timeout in seconds for the longpress event.
283         /// </summary>
284         public double LongPressTimeout
285         {
286             get
287             {
288                 return Interop.Elementary.elm_genlist_longpress_timeout_get(RealHandle);
289             }
290             set
291             {
292                 Interop.Elementary.elm_genlist_longpress_timeout_set(RealHandle, value);
293             }
294         }
295
296         /// <summary>
297         /// Gets or Sets focus upon items selection mode.
298         /// </summary>
299         /// <remarks>
300         /// When enabled, every selection of an item inside the <see cref="GenList"/> will automatically set focus to its first focusable widget from the left.
301         /// This is true of course if the selection was made by clicking an unfocusable area in an item or selecting it with a key movement.
302         /// Clicking on a focusable widget inside an item will couse this particular item to get focus as usual.
303         /// </remarks>
304         public bool FocusOnSelection
305         {
306             get
307             {
308                 return Interop.Elementary.elm_genlist_focus_on_selection_get(RealHandle);
309             }
310             set
311             {
312                 Interop.Elementary.elm_genlist_focus_on_selection_set(RealHandle, value);
313             }
314         }
315
316         /// <summary>
317         /// Gets or sets whether enable multi-selection in the genlist.
318         /// </summary>
319         public bool IsMultiSelection
320         {
321             get
322             {
323                 return Interop.Elementary.elm_genlist_multi_select_get(RealHandle);
324             }
325             set
326             {
327                 Interop.Elementary.elm_genlist_multi_select_set(RealHandle, value);
328             }
329         }
330
331         /// <summary>
332         /// Gets the selected item in a given genlist widget.
333         /// </summary>
334         public GenListItem SelectedItem
335         {
336             get
337             {
338                 IntPtr handle = Interop.Elementary.elm_genlist_selected_item_get(RealHandle);
339                 return ItemObject.GetItemByHandle(handle) as GenListItem;
340             }
341         }
342
343         /// <summary>
344         /// Gets or sets the genlist select mode by <see cref="GenItemSelectionMode"/>.
345         /// </summary>
346         public GenItemSelectionMode SelectionMode
347         {
348             get
349             {
350                 return (GenItemSelectionMode)Interop.Elementary.elm_genlist_select_mode_get(RealHandle);
351             }
352             set
353             {
354                 Interop.Elementary.elm_genlist_select_mode_set(RealHandle, (int)value);
355             }
356         }
357
358         /// <summary>
359         /// Gets count of items in a this genlist widget
360         /// </summary>
361         public int Count
362         {
363             get
364             {
365                 return Interop.Elementary.elm_genlist_items_count(RealHandle);
366             }
367         }
368
369         /// <summary>
370         /// ItemSelected is raised when a new genlist item is selected.
371         /// </summary>
372         public event EventHandler<GenListItemEventArgs> ItemSelected;
373
374         /// <summary>
375         /// ItemUnselected is raised when the genlist item is Unselected.
376         /// </summary>
377         public event EventHandler<GenListItemEventArgs> ItemUnselected;
378
379         /// <summary>
380         /// ItemPressed is raised when a new genlist item is pressed.
381         /// </summary>
382         public event EventHandler<GenListItemEventArgs> ItemPressed;
383
384         /// <summary>
385         /// ItemReleased is raised when a new genlist item is released.
386         /// </summary>
387         public event EventHandler<GenListItemEventArgs> ItemReleased;
388
389         /// <summary>
390         /// ItemActivated is raised when a new genlist item is double clicked or pressed (enter|return|spacebar).
391         /// </summary>
392         public event EventHandler<GenListItemEventArgs> ItemActivated;
393
394         /// <summary>
395         /// ItemDoubleClicked is raised when a new genlist item is double clicked.
396         /// </summary>
397         public event EventHandler<GenListItemEventArgs> ItemDoubleClicked;
398
399         /// <summary>
400         /// ItemExpanded is raised when a new genlist item is indicated to expand.
401         /// </summary>
402         public event EventHandler<GenListItemEventArgs> ItemExpanded;
403
404         /// <summary>
405         /// ItemRealized is raised when a new genlist item is created as a real object.
406         /// </summary>
407         public event EventHandler<GenListItemEventArgs> ItemRealized;
408
409         /// <summary>
410         /// ItemUnrealized is raised when a new genlist item is unrealized.
411         /// After calling unrealize, the item's content objects are deleted and the item object itself is deleted or is put into a floating cache.
412         /// </summary>
413         public event EventHandler<GenListItemEventArgs> ItemUnrealized;
414
415         /// <summary>
416         /// ItemLongPressed is raised when a genlist item is pressed for a certain amount of time. By default it's 1 second.
417         /// </summary>
418         public event EventHandler<GenListItemEventArgs> ItemLongPressed;
419
420         /// <summary>
421         /// ItemMoved is raised when a genlist item is moved in the reorder mode.
422         /// </summary>
423         public event EventHandler<GenListItemEventArgs> ItemMoved;
424
425         /// <summary>
426         /// ItemMovedAfter is raised when a genlist item is moved after another item in the reorder mode.
427         /// To get the relative previous item, use <see cref="GenListItem.Previous"/>.
428         /// </summary>
429         public event EventHandler<GenListItemEventArgs> ItemMovedAfter;
430
431         /// <summary>
432         /// ItemMovedBefore is raised when a genlist item is moved before another item in the reorder mode.
433         /// To get the relative next item, use <see cref="GenListItem.Next"/>.
434         /// </summary>
435         public event EventHandler<GenListItemEventArgs> ItemMovedBefore;
436
437         /// <summary>
438         /// Changed is raised when genlist is changed.
439         /// </summary>
440         public event EventHandler Changed
441         {
442             add { _changed.On += value; }
443             remove { _changed.On -= value; }
444         }
445
446         /// <summary>
447         /// ScrollAnimationStarted is raised when scrolling animation has started.
448         /// </summary>
449         public event EventHandler ScrollAnimationStarted
450         {
451             add { _scrollAnimationStarted.On += value; }
452             remove { _scrollAnimationStarted.On -= value; }
453         }
454
455         /// <summary>
456         /// ScrollAnimationStopped is raised when scrolling animation has stopped.
457         /// </summary>
458         public event EventHandler ScrollAnimationStopped
459         {
460             add { _scrollAnimationStopped.On += value; }
461             remove { _scrollAnimationStopped.On -= value; }
462         }
463
464         /// <summary>
465         /// Appends a new item to the end of a given genlist widget.
466         /// </summary>
467         /// <param name="itemClass">The itemClass defines how to display the data.</param>
468         /// <param name="data">The item data.</param>
469         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
470         /// <seealso cref="GenItemClass"/>
471         /// <seealso cref="GenListItem"/>
472         public GenListItem Append(GenItemClass itemClass, object data)
473         {
474             return Append(itemClass, data, GenListItemType.Normal);
475         }
476
477         /// <summary>
478         /// Appends a new item with <see cref="GenListItemType"/> to the end of a given genlist widget.
479         /// </summary>
480         /// <param name="itemClass">The itemClass defines how to display the data.</param>
481         /// <param name="data">The item data.</param>
482         /// <param name="type">The genlist item type.</param>
483         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
484         public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type)
485         {
486             return Append(itemClass, data, type, null);
487         }
488
489         /// <summary>
490         /// 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.
491         /// </summary>
492         /// <param name="itemClass">The itemClass defines how to display the data.</param>
493         /// <param name="data">The item data.</param>
494         /// <param name="type">The genlist item type.</param>
495         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
496         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
497         public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
498         {
499             GenListItem item = new GenListItem(data, itemClass);
500             IntPtr handle = Interop.Elementary.elm_genlist_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
501             item.Handle = handle;
502             AddInternal(item);
503             return item;
504         }
505
506         /// <summary>
507         /// Prepends a new item to the beginning of a given genlist widget.
508         /// </summary>
509         /// <param name="itemClass">The itemClass defines how to display the data.</param>
510         /// <param name="data">The item data.</param>
511         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
512         public GenListItem Prepend(GenItemClass itemClass, object data)
513         {
514             return Prepend(itemClass, data, GenListItemType.Normal);
515         }
516
517         /// <summary>
518         /// Prepends a new item with <see cref="GenListItemType"/> to the beginning of a given genlist widget.
519         /// </summary>
520         /// <param name="itemClass">The itemClass defines how to display the data.</param>
521         /// <param name="data">The item data.</param>
522         /// <param name="type">The genlist item type.</param>
523         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
524         public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type)
525         {
526             return Prepend(itemClass, data, type, null);
527         }
528
529         /// <summary>
530         /// 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.
531         /// </summary>
532         /// <param name="itemClass">The itemClass defines how to display the data.</param>
533         /// <param name="data">The item data.</param>
534         /// <param name="type">The genlist item type.</param>
535         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
536         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
537         public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
538         {
539             GenListItem item = new GenListItem(data, itemClass);
540             IntPtr handle = Interop.Elementary.elm_genlist_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
541             item.Handle = handle;
542             AddInternal(item);
543             return item;
544         }
545
546         /// <summary>
547         /// Inserts an item before another item in a genlist widget.
548         /// It is the same tree level or group as the item before which it is inserted.????
549         /// </summary>
550         /// <param name="itemClass">The itemClass defines how to display the data.</param>
551         /// <param name="data">The item data.</param>
552         /// <param name="before">The item before which to place this new one.</param>
553         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
554         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before)
555         {
556             return InsertBefore(itemClass, data, before, GenListItemType.Normal);
557         }
558
559         /// <summary>
560         /// Inserts an item with <see cref="GenListItemType"/> before another item in a genlist widget.
561         /// It is the same tree level or group as the item before which it is inserted.????
562         /// </summary>
563         /// <param name="itemClass">The itemClass defines how to display the data.</param>
564         /// <param name="data">The item data.</param>
565         /// <param name="before">The item before which to place this new one.</param>
566         /// <param name="type">The genlist item type.</param>
567         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
568         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type)
569         {
570             return InsertBefore(itemClass, data, before, type, null);
571         }
572
573         /// <summary>
574         /// Inserts an item with <see cref="GenListItemType"/> before another item under a parent in a genlist widget.
575         /// </summary>
576         /// <param name="itemClass">The itemClass defines how to display the data.</param>
577         /// <param name="data">The item data.</param>
578         /// <param name="before">The item before which to place this new one.</param>
579         /// <param name="type">The genlist item type.</param>
580         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
581         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
582         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type, GenListItem parent)
583         {
584             GenListItem item = new GenListItem(data, itemClass);
585             // insert before the `before` list item
586             IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
587                 RealHandle, // genlist handle
588                 itemClass.UnmanagedPtr, // item class
589                 (IntPtr)item.Id, // data
590                 parent, // parent
591                 before, // before
592                 (int)type, // item type
593                 null, // select callback
594                 (IntPtr)item.Id); // callback data
595             item.Handle = handle;
596             AddInternal(item);
597             return item;
598         }
599
600         /// <summary>
601         /// Inserts an item with <see cref="GenListItemType"/> after another item under a parent in a genlist widget.
602         /// </summary>
603         /// <param name="itemClass">The itemClass defines how to display the data.</param>
604         /// <param name="data">The item data.</param>
605         /// <param name="after">The item after which to place this new one.</param>
606         /// <param name="type">The genlist item type.</param>
607         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
608         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
609         public GenListItem InsertAfter(GenItemClass itemClass, object data, GenListItem after, GenListItemType type, GenListItem parent)
610         {
611             GenListItem item = new GenListItem(data, itemClass);
612             // insert before the `before` list item
613             IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
614                 RealHandle, // genlist handle
615                 itemClass.UnmanagedPtr, // item class
616                 (IntPtr)item.Id, // data
617                 parent, // parent
618                 after, // after
619                 (int)type, // item type
620                 null, // select callback
621                 (IntPtr)item.Id); // callback data
622             item.Handle = handle;
623             AddInternal(item);
624             return item;
625         }
626
627         /// <summary>
628         /// Insert an item in a genlist widget using a user-defined sort function.
629         /// </summary>
630         /// <param name="itemClass">The itemClass defines how to display the data.</param>
631         /// <param name="data">The item data.</param>
632         /// <param name="comparison">User defined comparison function that defines the sort order based on genlist item and its data.</param>
633         /// <param name="type">The genlist item type.</param>
634         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
635         /// <returns>Return a genlist item that contains data and itemClass.</returns>
636         public GenListItem InsertSorted(GenItemClass itemClass, object data, Comparison<object> comparison, GenListItemType type, GenListItem parent)
637         {
638             GenListItem item = new GenListItem(data, itemClass);
639
640             Interop.Elementary.Eina_Compare_Cb compareCallback = (handle1, handle2) =>
641             {
642                 GenListItem first = (ItemObject.GetItemByHandle(handle1) as GenListItem) ?? item;
643                 GenListItem second = (ItemObject.GetItemByHandle(handle2) as GenListItem) ?? item;
644                 return comparison(first.Data, second.Data);
645             };
646
647             IntPtr handle = Interop.Elementary.elm_genlist_item_sorted_insert(
648                 RealHandle, // genlist handle
649                 itemClass.UnmanagedPtr, // item clas
650                 (IntPtr)item.Id, // data
651                 parent, // parent
652                 (int)type, // item type
653                 compareCallback, // compare callback
654                 null, //select callback
655                 (IntPtr)item.Id); // callback data
656             item.Handle = handle;
657             AddInternal(item);
658             return item;
659         }
660
661         /// <summary>
662         /// Shows the given item with position type in a genlist.
663         /// 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.
664         /// 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.
665         /// </summary>
666         /// <param name="item">The item to display.</param>
667         /// <param name="position">The position to show the given item to <see cref="ScrollToPosition"/>.</param>
668         /// <param name="animated">The animated indicates how to display the item, by scrolling or by jumping.</param>
669         public void ScrollTo(GenListItem item, ScrollToPosition position, bool animated)
670         {
671             if (animated)
672             {
673                 Interop.Elementary.elm_genlist_item_bring_in(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
674             }
675             else
676             {
677                 Interop.Elementary.elm_genlist_item_show(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
678             }
679         }
680
681         /// <summary>
682         /// Updates the content of all the realized items.
683         /// This updates all the realized items by calling all the <see cref="GenItemClass"/> again to get the content, text and states.
684         /// Use this when the original item data has changed and the changes are desired to reflect.
685         /// To update just one item, use <see cref="GenListItem.Update"/>.
686         /// </summary>
687         /// <seealso cref="GenListItem.Update"/>
688         public void UpdateRealizedItems()
689         {
690             Interop.Elementary.elm_genlist_realized_items_update(RealHandle);
691         }
692
693         /// <summary>
694         /// Removes all items from a given genlist widget.
695         /// This removes (and deletes) all items in obj, making it empty.
696         /// To delete just one item, use <see cref="ItemObject.Delete"/>.
697         /// </summary>
698         /// <seealso cref="ItemObject.Delete"/>
699         public void Clear()
700         {
701             Interop.Elementary.elm_genlist_clear(RealHandle);
702         }
703
704         /// <summary>
705         /// Get the item that is at the x, y canvas coords.
706         /// </summary>
707         /// <param name="x">The input x coordinate</param>
708         /// <param name="y">The input y coordinate</param>
709         /// <param name="pos">The position relative to the item returned here
710         ///  -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).
711         /// </param>
712         /// <returns></returns>
713         public GenListItem GetItemByPosition(int x, int y, out int pos)
714         {
715             IntPtr handle = Interop.Elementary.elm_genlist_at_xy_item_get(RealHandle, x, y, out pos);
716             return ItemObject.GetItemByHandle(handle) as GenListItem;
717         }
718
719         /// <summary>
720         /// Gets the nth item in a given genlist widget, placed at position nth, in its internal items list.
721         /// </summary>
722         /// <param name="index">The number of the item to grab (0 being the first)</param>
723         /// <returns></returns>
724         public GenListItem GetItemByIndex(int index)
725         {
726             IntPtr handle = Interop.Elementary.elm_genlist_nth_item_get(RealHandle, index);
727             return ItemObject.GetItemByHandle(handle) as GenListItem;
728         }
729
730         /// <summary>
731         /// The callback of Unrealize Event
732         /// </summary>
733         protected override void OnRealized()
734         {
735             base.OnRealized();
736             ListMode = GenListMode.Compress;
737             InitializeSmartEvent();
738         }
739
740         /// <summary>
741         /// Creates a widget handle.
742         /// </summary>
743         /// <param name="parent">Parent EvasObject</param>
744         /// <returns>Handle IntPtr</returns>
745         protected override IntPtr CreateHandle(EvasObject parent)
746         {
747             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
748             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
749
750             RealHandle = Interop.Elementary.elm_genlist_add(handle);
751             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
752
753             return handle;
754         }
755
756         void InitializeSmartEvent()
757         {
758             _selected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "selected", GenListItemEventArgs.CreateFromSmartEvent);
759             _unselected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unselected", GenListItemEventArgs.CreateFromSmartEvent);
760             _activated = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "activated", GenListItemEventArgs.CreateFromSmartEvent);
761             _pressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "pressed", GenListItemEventArgs.CreateFromSmartEvent);
762             _released = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "released", GenListItemEventArgs.CreateFromSmartEvent);
763             _doubleClicked = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "clicked,double", GenListItemEventArgs.CreateFromSmartEvent);
764             _expanded = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "expanded", GenListItemEventArgs.CreateFromSmartEvent);
765             _realized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "realized", GenListItemEventArgs.CreateFromSmartEvent);
766             _unrealized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unrealized", GenListItemEventArgs.CreateFromSmartEvent);
767             _longpressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "longpressed", GenListItemEventArgs.CreateFromSmartEvent);
768             _moved = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved", GenListItemEventArgs.CreateFromSmartEvent);
769             _movedAfter = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,after", GenListItemEventArgs.CreateFromSmartEvent);
770             _movedBefore = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,before", GenListItemEventArgs.CreateFromSmartEvent);
771             _scrollAnimationStarted = new SmartEvent(this, this.RealHandle, "scroll,anim,start");
772             _scrollAnimationStopped = new SmartEvent(this, this.RealHandle, "scroll,anim,stop");
773             _changed = new SmartEvent(this, this.RealHandle, "changed");
774
775             _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
776             _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
777             _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
778             _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
779             _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
780             _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
781             _expanded.On += (s, e) => { if (e.Item != null) ItemExpanded?.Invoke(this, e); };
782             _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
783             _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
784             _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
785             _moved.On += (s, e) => { if (e.Item != null) ItemMoved?.Invoke(this, e); };
786             _movedAfter.On += (s, e) => { if (e.Item != null) ItemMovedAfter?.Invoke(this, e); };
787             _movedBefore.On += (s, e) => { if (e.Item != null) ItemMovedBefore?.Invoke(this, e); };
788         }
789
790         void AddInternal(GenListItem item)
791         {
792             _children.Add(item);
793             item.Deleted += Item_Deleted;
794         }
795
796         void Item_Deleted(object sender, EventArgs e)
797         {
798             _children.Remove((GenListItem)sender);
799         }
800     }
801 }