Merge "[Usb] Fix crashes while disposing"
[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             ListMode = GenListMode.Compress;
159             InitializeSmartEvent();
160         }
161
162         /// <summary>
163         /// Gets or sets whether the homogeneous mode is enabled.
164         /// </summary>
165         /// <remarks>
166         /// If true, the genlist items have same height and width.
167         /// </remarks>
168         public bool Homogeneous
169         {
170             get
171             {
172                 return Interop.Elementary.elm_genlist_homogeneous_get(RealHandle);
173             }
174             set
175             {
176                 Interop.Elementary.elm_genlist_homogeneous_set(RealHandle, value);
177             }
178         }
179
180         /// <summary>
181         /// Gets or sets the horizontal stretching mode. This mode used for sizing items horizontally.
182         /// The default value is <see cref="GenListMode.Scroll"/> which means that if items are too wide to fit, the scroller scrolls horizontally.
183         /// 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.
184         /// If set <see cref="GenListMode.Limit"/> which means that items are expanded to the viewport width and limited to that size.
185         /// if set <see cref="GenListMode.Expand"/> which means that genlist try to reserve space to all its items to be visible at a time.
186         /// </summary>
187         /// <remarks>
188         /// Compress makes genlist resize slower as it recalculates every item height again whenever the list width changes.
189         /// The homogeneous mode is so that all items in the genlist are of the same width/height. With Compress, genlist items are initialized fast.
190         /// However, there are no sub-objects in the genlist which can be on the flying resizable (such as TEXTBLOCK).
191         /// If so, then some dynamic resizable objects in the genlist would not be diplayed properly.
192         /// </remarks>
193         public GenListMode ListMode
194         {
195             get
196             {
197                 return (GenListMode)Interop.Elementary.elm_genlist_mode_get(RealHandle);
198             }
199             set
200             {
201                 Interop.Elementary.elm_genlist_mode_set(RealHandle, (int)value);
202             }
203         }
204
205         /// <summary>
206         /// Gets the first item in the genlist.
207         /// </summary>
208         public GenListItem FirstItem
209         {
210             get
211             {
212                 IntPtr handle = Interop.Elementary.elm_genlist_first_item_get(RealHandle);
213                 return ItemObject.GetItemByHandle(handle) as GenListItem;
214             }
215         }
216
217         /// <summary>
218         /// Gets the last item in the genlist.
219         /// </summary>
220         public GenListItem LastItem
221         {
222             get
223             {
224                 IntPtr handle = Interop.Elementary.elm_genlist_last_item_get(RealHandle);
225                 return ItemObject.GetItemByHandle(handle) as GenListItem;
226             }
227         }
228
229         /// <summary>
230         /// Gets or sets the reorder mode.
231         /// After turning on the reorder mode, longpress on a normal item triggers reordering of the item.
232         /// You can move the item up and down. However, reordering does not work with group items.
233         /// </summary>
234         public bool ReorderMode
235         {
236             get
237             {
238                 return Interop.Elementary.elm_genlist_reorder_mode_get(RealHandle);
239             }
240             set
241             {
242                 Interop.Elementary.elm_genlist_reorder_mode_set(RealHandle, value);
243             }
244         }
245
246         /// <summary>
247         /// Gets or set the maximum number of items within an item block.
248         /// </summary>
249         public int BlockCount
250         {
251             get
252             {
253                 return Interop.Elementary.elm_genlist_block_count_get(RealHandle);
254             }
255             set
256             {
257                 Interop.Elementary.elm_genlist_block_count_set(RealHandle, value);
258             }
259         }
260
261         /// <summary>
262         /// Gets or sets whether the genlist items should be highlighted when an item is selected.
263         /// </summary>
264         public bool IsHighlight
265         {
266             get
267             {
268                 return Interop.Elementary.elm_genlist_highlight_mode_get(RealHandle);
269             }
270             set
271             {
272                 Interop.Elementary.elm_genlist_highlight_mode_set(RealHandle, value);
273             }
274         }
275
276         /// <summary>
277         /// Gets or sets the timeout in seconds for the longpress event.
278         /// </summary>
279         public double LongPressTimeout
280         {
281             get
282             {
283                 return Interop.Elementary.elm_genlist_longpress_timeout_get(RealHandle);
284             }
285             set
286             {
287                 Interop.Elementary.elm_genlist_longpress_timeout_set(RealHandle, value);
288             }
289         }
290
291         /// <summary>
292         /// Gets or Sets focus upon items selection mode.
293         /// </summary>
294         /// <remarks>
295         /// When enabled, every selection of an item inside the <see cref="GenList"/> will automatically set focus to its first focusable widget from the left.
296         /// 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.
297         /// Clicking on a focusable widget inside an item will couse this particular item to get focus as usual.
298         /// </remarks>
299         public bool FocusOnSelection
300         {
301             get
302             {
303                 return Interop.Elementary.elm_genlist_focus_on_selection_get(RealHandle);
304             }
305             set
306             {
307                 Interop.Elementary.elm_genlist_focus_on_selection_set(RealHandle, value);
308             }
309         }
310
311         /// <summary>
312         /// Gets or sets whether enable multi-selection in the genlist.
313         /// </summary>
314         public bool IsMultiSelection
315         {
316             get
317             {
318                 return Interop.Elementary.elm_genlist_multi_select_get(RealHandle);
319             }
320             set
321             {
322                 Interop.Elementary.elm_genlist_multi_select_set(RealHandle, value);
323             }
324         }
325
326         /// <summary>
327         /// Gets the selected item in a given genlist widget.
328         /// </summary>
329         public GenListItem SelectedItem
330         {
331             get
332             {
333                 IntPtr handle = Interop.Elementary.elm_genlist_selected_item_get(RealHandle);
334                 return ItemObject.GetItemByHandle(handle) as GenListItem;
335             }
336         }
337
338         /// <summary>
339         /// Gets or sets the genlist select mode by <see cref="GenItemSelectionMode"/>.
340         /// </summary>
341         public GenItemSelectionMode SelectionMode
342         {
343             get
344             {
345                 return (GenItemSelectionMode)Interop.Elementary.elm_genlist_select_mode_get(RealHandle);
346             }
347             set
348             {
349                 Interop.Elementary.elm_genlist_select_mode_set(RealHandle, (int)value);
350             }
351         }
352
353         /// <summary>
354         /// ItemSelected is raised when a new genlist item is selected.
355         /// </summary>
356         public event EventHandler<GenListItemEventArgs> ItemSelected;
357
358         /// <summary>
359         /// ItemUnselected is raised when the genlist item is Unselected.
360         /// </summary>
361         public event EventHandler<GenListItemEventArgs> ItemUnselected;
362
363         /// <summary>
364         /// ItemPressed is raised when a new genlist item is pressed.
365         /// </summary>
366         public event EventHandler<GenListItemEventArgs> ItemPressed;
367
368         /// <summary>
369         /// ItemReleased is raised when a new genlist item is released.
370         /// </summary>
371         public event EventHandler<GenListItemEventArgs> ItemReleased;
372
373         /// <summary>
374         /// ItemActivated is raised when a new genlist item is double clicked or pressed (enter|return|spacebar).
375         /// </summary>
376         public event EventHandler<GenListItemEventArgs> ItemActivated;
377
378         /// <summary>
379         /// ItemDoubleClicked is raised when a new genlist item is double clicked.
380         /// </summary>
381         public event EventHandler<GenListItemEventArgs> ItemDoubleClicked;
382
383         /// <summary>
384         /// ItemExpanded is raised when a new genlist item is indicated to expand.
385         /// </summary>
386         public event EventHandler<GenListItemEventArgs> ItemExpanded;
387
388         /// <summary>
389         /// ItemRealized is raised when a new genlist item is created as a real object.
390         /// </summary>
391         public event EventHandler<GenListItemEventArgs> ItemRealized;
392
393         /// <summary>
394         /// ItemUnrealized is raised when a new genlist item is unrealized.
395         /// After calling unrealize, the item's content objects are deleted and the item object itself is deleted or is put into a floating cache.
396         /// </summary>
397         public event EventHandler<GenListItemEventArgs> ItemUnrealized;
398
399         /// <summary>
400         /// ItemLongPressed is raised when a genlist item is pressed for a certain amount of time. By default it's 1 second.
401         /// </summary>
402         public event EventHandler<GenListItemEventArgs> ItemLongPressed;
403
404         /// <summary>
405         /// ItemMoved is raised when a genlist item is moved in the reorder mode.
406         /// </summary>
407         public event EventHandler<GenListItemEventArgs> ItemMoved;
408
409         /// <summary>
410         /// ItemMovedAfter is raised when a genlist item is moved after another item in the reorder mode.
411         /// To get the relative previous item, use <see cref="GenListItem.Previous"/>.
412         /// </summary>
413         public event EventHandler<GenListItemEventArgs> ItemMovedAfter;
414
415         /// <summary>
416         /// ItemMovedBefore is raised when a genlist item is moved before another item in the reorder mode.
417         /// To get the relative next item, use <see cref="GenListItem.Next"/>.
418         /// </summary>
419         public event EventHandler<GenListItemEventArgs> ItemMovedBefore;
420
421         /// <summary>
422         /// Changed is raised when genlist is changed.
423         /// </summary>
424         public event EventHandler Changed
425         {
426             add { _changed.On += value; }
427             remove { _changed.On -= value; }
428         }
429
430         /// <summary>
431         /// ScrollAnimationStarted is raised when scrolling animation has started.
432         /// </summary>
433         public event EventHandler ScrollAnimationStarted
434         {
435             add { _scrollAnimationStarted.On += value; }
436             remove { _scrollAnimationStarted.On -= value; }
437         }
438
439         /// <summary>
440         /// ScrollAnimationStopped is raised when scrolling animation has stopped.
441         /// </summary>
442         public event EventHandler ScrollAnimationStopped
443         {
444             add { _scrollAnimationStopped.On += value; }
445             remove { _scrollAnimationStopped.On -= value; }
446         }
447
448         /// <summary>
449         /// Appends a new item to the end of a given genlist widget.
450         /// </summary>
451         /// <param name="itemClass">The itemClass defines how to display the data.</param>
452         /// <param name="data">The item data.</param>
453         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
454         /// <seealso cref="GenItemClass"/>
455         /// <seealso cref="GenListItem"/>
456         public GenListItem Append(GenItemClass itemClass, object data)
457         {
458             return Append(itemClass, data, GenListItemType.Normal);
459         }
460
461         /// <summary>
462         /// Appends a new item with <see cref="GenListItemType"/> to the end of a given genlist widget.
463         /// </summary>
464         /// <param name="itemClass">The itemClass defines how to display the data.</param>
465         /// <param name="data">The item data.</param>
466         /// <param name="type">The genlist item type.</param>
467         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
468         public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type)
469         {
470             return Append(itemClass, data, type, null);
471         }
472
473         /// <summary>
474         /// 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.
475         /// </summary>
476         /// <param name="itemClass">The itemClass defines how to display the data.</param>
477         /// <param name="data">The item data.</param>
478         /// <param name="type">The genlist item type.</param>
479         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
480         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
481         public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
482         {
483             GenListItem item = new GenListItem(data, itemClass);
484             IntPtr handle = Interop.Elementary.elm_genlist_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
485             item.Handle = handle;
486             AddInternal(item);
487             return item;
488         }
489
490         /// <summary>
491         /// Prepends a new item to the beginning of a given genlist widget.
492         /// </summary>
493         /// <param name="itemClass">The itemClass defines how to display the data.</param>
494         /// <param name="data">The item data.</param>
495         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
496         public GenListItem Prepend(GenItemClass itemClass, object data)
497         {
498             return Prepend(itemClass, data, GenListItemType.Normal);
499         }
500
501         /// <summary>
502         /// Prepends a new item with <see cref="GenListItemType"/> to the beginning of a given genlist widget.
503         /// </summary>
504         /// <param name="itemClass">The itemClass defines how to display the data.</param>
505         /// <param name="data">The item data.</param>
506         /// <param name="type">The genlist item type.</param>
507         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
508         public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type)
509         {
510             return Prepend(itemClass, data, type, null);
511         }
512
513         /// <summary>
514         /// 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.
515         /// </summary>
516         /// <param name="itemClass">The itemClass defines how to display the data.</param>
517         /// <param name="data">The item data.</param>
518         /// <param name="type">The genlist item type.</param>
519         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
520         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
521         public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
522         {
523             GenListItem item = new GenListItem(data, itemClass);
524             IntPtr handle = Interop.Elementary.elm_genlist_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
525             item.Handle = handle;
526             AddInternal(item);
527             return item;
528         }
529
530         /// <summary>
531         /// Inserts an item before another item in a genlist widget.
532         /// It is the same tree level or group as the item before which it is inserted.????
533         /// </summary>
534         /// <param name="itemClass">The itemClass defines how to display the data.</param>
535         /// <param name="data">The item data.</param>
536         /// <param name="before">The item before which to place this new one.</param>
537         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
538         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before)
539         {
540             return InsertBefore(itemClass, data, before, GenListItemType.Normal);
541         }
542
543         /// <summary>
544         /// Inserts an item with <see cref="GenListItemType"/> before another item in a genlist widget.
545         /// It is the same tree level or group as the item before which it is inserted.????
546         /// </summary>
547         /// <param name="itemClass">The itemClass defines how to display the data.</param>
548         /// <param name="data">The item data.</param>
549         /// <param name="before">The item before which to place this new one.</param>
550         /// <param name="type">The genlist item type.</param>
551         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
552         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type)
553         {
554             return InsertBefore(itemClass, data, before, type, null);
555         }
556
557         /// <summary>
558         /// Inserts an item with <see cref="GenListItemType"/> before another item under a parent in a genlist widget.
559         /// </summary>
560         /// <param name="itemClass">The itemClass defines how to display the data.</param>
561         /// <param name="data">The item data.</param>
562         /// <param name="before">The item before which to place this new one.</param>
563         /// <param name="type">The genlist item type.</param>
564         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
565         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
566         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type, GenListItem parent)
567         {
568             GenListItem item = new GenListItem(data, itemClass);
569             // insert before the `before` list item
570             IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
571                 RealHandle, // genlist handle
572                 itemClass.UnmanagedPtr, // item class
573                 (IntPtr)item.Id, // data
574                 parent, // parent
575                 before, // before
576                 (int)type, // item type
577                 null, // select callback
578                 (IntPtr)item.Id); // callback data
579             item.Handle = handle;
580             AddInternal(item);
581             return item;
582         }
583
584         /// <summary>
585         /// Inserts an item with <see cref="GenListItemType"/> after another item under a parent in a genlist widget.
586         /// </summary>
587         /// <param name="itemClass">The itemClass defines how to display the data.</param>
588         /// <param name="data">The item data.</param>
589         /// <param name="after">The item after which to place this new one.</param>
590         /// <param name="type">The genlist item type.</param>
591         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
592         /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
593         public GenListItem InsertAfter(GenItemClass itemClass, object data, GenListItem after, GenListItemType type, GenListItem parent)
594         {
595             GenListItem item = new GenListItem(data, itemClass);
596             // insert before the `before` list item
597             IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
598                 RealHandle, // genlist handle
599                 itemClass.UnmanagedPtr, // item class
600                 (IntPtr)item.Id, // data
601                 parent, // parent
602                 after, // after
603                 (int)type, // item type
604                 null, // select callback
605                 (IntPtr)item.Id); // callback data
606             item.Handle = handle;
607             AddInternal(item);
608             return item;
609         }
610
611         /// <summary>
612         /// Insert an item in a genlist widget using a user-defined sort function.
613         /// </summary>
614         /// <param name="itemClass">The itemClass defines how to display the data.</param>
615         /// <param name="data">The item data.</param>
616         /// <param name="comparison">User defined comparison function that defines the sort order based on genlist item and its data.</param>
617         /// <param name="type">The genlist item type.</param>
618         /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
619         /// <returns>Return a genlist item that contains data and itemClass.</returns>
620         public GenListItem InsertSorted(GenItemClass itemClass, object data, Comparison<object> comparison, GenListItemType type, GenListItem parent)
621         {
622             GenListItem item = new GenListItem(data, itemClass);
623
624             Interop.Elementary.Eina_Compare_Cb compareCallback = (handle1, handle2) =>
625             {
626                 GenListItem first = (ItemObject.GetItemByHandle(handle1) as GenListItem) ?? item;
627                 GenListItem second = (ItemObject.GetItemByHandle(handle2) as GenListItem) ?? item;
628                 return comparison(first.Data, second.Data);
629             };
630
631             IntPtr handle = Interop.Elementary.elm_genlist_item_sorted_insert(
632                 RealHandle, // genlist handle
633                 itemClass.UnmanagedPtr, // item clas
634                 (IntPtr)item.Id, // data
635                 parent, // parent
636                 (int)type, // item type
637                 compareCallback, // compare callback
638                 null, //select callback
639                 (IntPtr)item.Id); // callback data
640             item.Handle = handle;
641             AddInternal(item);
642             return item;
643         }
644
645         /// <summary>
646         /// Shows the given item with position type in a genlist.
647         /// 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.
648         /// 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.
649         /// </summary>
650         /// <param name="item">The item to display.</param>
651         /// <param name="position">The position to show the given item to <see cref="ScrollToPosition"/>.</param>
652         /// <param name="animated">The animated indicates how to display the item, by scrolling or by jumping.</param>
653         public void ScrollTo(GenListItem item, ScrollToPosition position, bool animated)
654         {
655             if (animated)
656             {
657                 Interop.Elementary.elm_genlist_item_bring_in(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
658             }
659             else
660             {
661                 Interop.Elementary.elm_genlist_item_show(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
662             }
663         }
664
665         /// <summary>
666         /// Updates the content of all the realized items.
667         /// This updates all the realized items by calling all the <see cref="GenItemClass"/> again to get the content, text and states.
668         /// Use this when the original item data has changed and the changes are desired to reflect.
669         /// To update just one item, use <see cref="GenListItem.Update"/>.
670         /// </summary>
671         /// <seealso cref="GenListItem.Update"/>
672         public void UpdateRealizedItems()
673         {
674             Interop.Elementary.elm_genlist_realized_items_update(RealHandle);
675         }
676
677         /// <summary>
678         /// Removes all items from a given genlist widget.
679         /// This removes (and deletes) all items in obj, making it empty.
680         /// To delete just one item, use <see cref="ItemObject.Delete"/>.
681         /// </summary>
682         /// <seealso cref="ItemObject.Delete"/>
683         public void Clear()
684         {
685             Interop.Elementary.elm_genlist_clear(RealHandle);
686         }
687
688         /// <summary>
689         /// Get the item that is at the x, y canvas coords.
690         /// </summary>
691         /// <param name="x">The input x coordinate</param>
692         /// <param name="y">The input y coordinate</param>
693         /// <param name="pos">The position relative to the item returned here
694         ///  -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).
695         /// </param>
696         /// <returns></returns>
697         public GenListItem GetItemByPosition(int x, int y, out int pos)
698         {
699             IntPtr handle = Interop.Elementary.elm_genlist_at_xy_item_get(RealHandle, x, y, out pos);
700             return ItemObject.GetItemByHandle(handle) as GenListItem;
701         }
702
703         /// <summary>
704         /// Gets the nth item in a given genlist widget, placed at position nth, in its internal items list.
705         /// </summary>
706         /// <param name="index">The number of the item to grab (0 being the first)</param>
707         /// <returns></returns>
708         public GenListItem GetItemByIndex(int index)
709         {
710             IntPtr handle = Interop.Elementary.elm_genlist_nth_item_get(RealHandle, index);
711             return ItemObject.GetItemByHandle(handle) as GenListItem;
712         }
713
714         /// <summary>
715         /// Creates a widget handle.
716         /// </summary>
717         /// <param name="parent">Parent EvasObject</param>
718         /// <returns>Handle IntPtr</returns>
719         protected override IntPtr CreateHandle(EvasObject parent)
720         {
721             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
722             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
723
724             RealHandle = Interop.Elementary.elm_genlist_add(handle);
725             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
726
727             return handle;
728         }
729
730         void InitializeSmartEvent()
731         {
732             _selected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "selected", GenListItemEventArgs.CreateFromSmartEvent);
733             _unselected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unselected", GenListItemEventArgs.CreateFromSmartEvent);
734             _activated = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "activated", GenListItemEventArgs.CreateFromSmartEvent);
735             _pressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "pressed", GenListItemEventArgs.CreateFromSmartEvent);
736             _released = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "released", GenListItemEventArgs.CreateFromSmartEvent);
737             _doubleClicked = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "clicked,double", GenListItemEventArgs.CreateFromSmartEvent);
738             _expanded = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "expanded", GenListItemEventArgs.CreateFromSmartEvent);
739             _realized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "realized", GenListItemEventArgs.CreateFromSmartEvent);
740             _unrealized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unrealized", GenListItemEventArgs.CreateFromSmartEvent);
741             _longpressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "longpressed", GenListItemEventArgs.CreateFromSmartEvent);
742             _moved = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved", GenListItemEventArgs.CreateFromSmartEvent);
743             _movedAfter = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,after", GenListItemEventArgs.CreateFromSmartEvent);
744             _movedBefore = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,before", GenListItemEventArgs.CreateFromSmartEvent);
745             _scrollAnimationStarted = new SmartEvent(this, this.RealHandle, "scroll,anim,start");
746             _scrollAnimationStopped = new SmartEvent(this, this.RealHandle, "scroll,anim,stop");
747             _changed = new SmartEvent(this, this.RealHandle, "changed");
748
749             _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
750             _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
751             _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
752             _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
753             _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
754             _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
755             _expanded.On += (s, e) => { if (e.Item != null) ItemExpanded?.Invoke(this, e); };
756             _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
757             _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
758             _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
759             _moved.On += (s, e) => { if (e.Item != null) ItemMoved?.Invoke(this, e); };
760             _movedAfter.On += (s, e) => { if (e.Item != null) ItemMovedAfter?.Invoke(this, e); };
761             _movedBefore.On += (s, e) => { if (e.Item != null) ItemMovedBefore?.Invoke(this, e); };
762         }
763
764         void AddInternal(GenListItem item)
765         {
766             _children.Add(item);
767             item.Deleted += Item_Deleted;
768         }
769
770         void Item_Deleted(object sender, EventArgs e)
771         {
772             _children.Remove((GenListItem)sender);
773         }
774     }
775 }