2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
23 /// Enumeration for setting genlist item type.
25 public enum GenListItemType
28 /// if Normal is set then this item is normal item.
32 /// If tree is set then this item is displayed as an item that is able to expand and have child items.
36 /// if Group is set then this item is group index item that is displayed at the top until the next group comes.
42 /// Enumeration for setting genlist's resizing behavior, transverse axis scrolling and items cropping.
44 public enum GenListMode
47 /// The genlist won't set any of its size hints to inform how a possible container should resize it.
48 /// Then, if it's not created as a "resize object", it might end with zeroed dimensions.
49 /// 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.
53 /// 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 /// Sets a minimum size hint on the genlist object, so that containers may respect it (and resize itself to fit the child properly).
58 /// More specifically, a minimum size hint will be set for its transverse axis, so that the largest item in that direction fits well.
59 /// This is naturally bound by the genlist object's maximum size hints, set externally.
63 /// 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.
64 /// This is naturally bound by the genlist object's maximum size hints, set externally.
70 /// It inherits System.EventArgs.
71 /// It contains Item which is <see cref="GenListItem"/> type.
72 /// All events of GenList contain GenListItemEventArgs as a parameter.
74 public class GenListItemEventArgs : EventArgs
77 /// Gets or sets GenList item. The return type is <see cref="GenListItem"/>.
79 public GenListItem Item { get; set; }
81 internal static GenListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
83 GenListItem item = ItemObject.GetItemByHandle(info) as GenListItem;
84 return new GenListItemEventArgs() { Item = item };
89 /// Enumeration that defines where to position the item in the genlist.
91 public enum ScrollToPosition
94 /// Scrolls to nowhere.
98 /// Scrolls to the nearest viewport.
102 /// Scrolls to the top of the viewport.
106 /// Scrolls to the middle of the viewport.
110 /// Scrolls to the bottom of the viewport.
116 /// It inherits <see cref="Layout"/>.
117 /// 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.
118 /// At the same time it was also made to be able to do tree structures.
119 /// But the price to pay is more complexity when it comes to usage.
120 /// If all you want is a simple list with icons and a single text, use the <see cref="List"/> widget.
122 public class GenList : Layout
124 HashSet<GenListItem> _children = new HashSet<GenListItem>();
126 SmartEvent<GenListItemEventArgs> _selected;
127 SmartEvent<GenListItemEventArgs> _unselected;
128 SmartEvent<GenListItemEventArgs> _activated;
129 SmartEvent<GenListItemEventArgs> _pressed;
130 SmartEvent<GenListItemEventArgs> _released;
131 SmartEvent<GenListItemEventArgs> _doubleClicked;
132 SmartEvent<GenListItemEventArgs> _expanded;
133 SmartEvent<GenListItemEventArgs> _realized;
134 SmartEvent<GenListItemEventArgs> _unrealized;
135 SmartEvent<GenListItemEventArgs> _longpressed;
136 SmartEvent<GenListItemEventArgs> _moved;
137 SmartEvent<GenListItemEventArgs> _movedAfter;
138 SmartEvent<GenListItemEventArgs> _movedBefore;
139 SmartEvent _scrollAnimationStarted;
140 SmartEvent _scrollAnimationStopped;
144 /// Creates and initializes a new instance of the GenList class.
146 /// <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>
147 public GenList(EvasObject parent) : base(parent)
149 InitializeSmartEvent();
153 /// Gets or sets whether the homogeneous mode is enabled.
156 /// If true, the genlist items have same height and width.
158 public bool Homogeneous
162 return Interop.Elementary.elm_genlist_homogeneous_get(RealHandle);
166 Interop.Elementary.elm_genlist_homogeneous_set(RealHandle, value);
171 /// Gets or sets the horizontal stretching mode. This mode used for sizing items horizontally.
172 /// The default value is <see cref="GenListMode.Scroll"/> which means that if items are too wide to fit, the scroller scrolls horizontally.
173 /// 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.
174 /// If set <see cref="GenListMode.Limit"/> which means that items are expanded to the viewport width and limited to that size.
175 /// if set <see cref="GenListMode.Expand"/> which means that genlist try to reserve space to all its items to be visible at a time.
178 /// Compress makes genlist resize slower as it recalculates every item height again whenever the list width changes.
179 /// The homogeneous mode is so that all items in the genlist are of the same width/height. With Compress, genlist items are initialized fast.
180 /// However, there are no sub-objects in the genlist which can be on the flying resizable (such as TEXTBLOCK).
181 /// If so, then some dynamic resizable objects in the genlist would not be diplayed properly.
183 public GenListMode ListMode
187 return (GenListMode)Interop.Elementary.elm_genlist_mode_get(RealHandle);
191 Interop.Elementary.elm_genlist_mode_set(RealHandle, (int)value);
196 /// Gets the first item in the genlist.
198 public GenListItem FirstItem
202 IntPtr handle = Interop.Elementary.elm_genlist_first_item_get(RealHandle);
203 return ItemObject.GetItemByHandle(handle) as GenListItem;
208 /// Gets the last item in the genlist.
210 public GenListItem LastItem
214 IntPtr handle = Interop.Elementary.elm_genlist_last_item_get(RealHandle);
215 return ItemObject.GetItemByHandle(handle) as GenListItem;
220 /// Gets or sets the reorder mode.
221 /// After turning on the reorder mode, longpress on a normal item triggers reordering of the item.
222 /// You can move the item up and down. However, reordering does not work with group items.
224 public bool ReorderMode
228 return Interop.Elementary.elm_genlist_reorder_mode_get(RealHandle);
232 Interop.Elementary.elm_genlist_reorder_mode_set(RealHandle, value);
237 /// ItemSelected is raised when a new genlist item is selected.
239 public event EventHandler<GenListItemEventArgs> ItemSelected;
242 /// ItemUnselected is raised when the genlist item is Unselected.
244 public event EventHandler<GenListItemEventArgs> ItemUnselected;
247 /// ItemPressed is raised when a new genlist item is pressed.
249 public event EventHandler<GenListItemEventArgs> ItemPressed;
252 /// ItemReleased is raised when a new genlist item is released.
254 public event EventHandler<GenListItemEventArgs> ItemReleased;
257 /// ItemActivated is raised when a new genlist item is double clicked or pressed (enter|return|spacebar).
259 public event EventHandler<GenListItemEventArgs> ItemActivated;
262 /// ItemDoubleClicked is raised when a new genlist item is double clicked.
264 public event EventHandler<GenListItemEventArgs> ItemDoubleClicked;
267 /// ItemExpanded is raised when a new genlist item is indicated to expand.
269 public event EventHandler<GenListItemEventArgs> ItemExpanded;
272 /// ItemRealized is raised when a new genlist item is created as a real object.
274 public event EventHandler<GenListItemEventArgs> ItemRealized;
277 /// ItemUnrealized is raised when a new genlist item is unrealized.
278 /// After calling unrealize, the item's content objects are deleted and the item object itself is deleted or is put into a floating cache.
280 public event EventHandler<GenListItemEventArgs> ItemUnrealized;
283 /// ItemLongPressed is raised when a genlist item is pressed for a certain amount of time. By default it's 1 second.
285 public event EventHandler<GenListItemEventArgs> ItemLongPressed;
288 /// ItemMoved is raised when a genlist item is moved in the reorder mode.
290 public event EventHandler<GenListItemEventArgs> ItemMoved;
293 /// ItemMovedAfter is raised when a genlist item is moved after another item in the reorder mode.
294 /// To get the relative previous item, use <see cref="GenListItem.Previous"/>.
296 public event EventHandler<GenListItemEventArgs> ItemMovedAfter;
299 /// ItemMovedBefore is raised when a genlist item is moved before another item in the reorder mode.
300 /// To get the relative next item, use <see cref="GenListItem.Next"/>.
302 public event EventHandler<GenListItemEventArgs> ItemMovedBefore;
305 /// Changed is raised when genlist is changed.
307 public event EventHandler Changed
309 add { _changed.On += value; }
310 remove { _changed.On -= value; }
314 /// ScrollAnimationStarted is raised when scrolling animation has started.
316 public event EventHandler ScrollAnimationStarted
318 add { _scrollAnimationStarted.On += value; }
319 remove { _scrollAnimationStarted.On -= value; }
323 /// ScrollAnimationStopped is raised when scrolling animation has stopped.
325 public event EventHandler ScrollAnimationStopped
327 add { _scrollAnimationStopped.On += value; }
328 remove { _scrollAnimationStopped.On -= value; }
332 /// Appends a new item to the end of a given genlist widget.
334 /// <param name="itemClass">The itemClass defines how to display the data.</param>
335 /// <param name="data">The item data.</param>
336 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
337 /// <seealso cref="GenItemClass"/>
338 /// <seealso cref="GenListItem"/>
339 public GenListItem Append(GenItemClass itemClass, object data)
341 return Append(itemClass, data, GenListItemType.Normal);
345 /// Appends a new item with <see cref="GenListItemType"/> to the end of a given genlist widget.
347 /// <param name="itemClass">The itemClass defines how to display the data.</param>
348 /// <param name="data">The item data.</param>
349 /// <param name="type">The genlist item type.</param>
350 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
351 public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type)
353 return Append(itemClass, data, type, null);
357 /// 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.
359 /// <param name="itemClass">The itemClass defines how to display the data.</param>
360 /// <param name="data">The item data.</param>
361 /// <param name="type">The genlist item type.</param>
362 /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
363 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
364 public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
366 GenListItem item = new GenListItem(data, itemClass);
367 IntPtr handle = Interop.Elementary.elm_genlist_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
368 item.Handle = handle;
374 /// Prepends a new item to the beginning of a given genlist widget.
376 /// <param name="itemClass">The itemClass defines how to display the data.</param>
377 /// <param name="data">The item data.</param>
378 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
379 public GenListItem Prepend(GenItemClass itemClass, object data)
381 return Prepend(itemClass, data, GenListItemType.Normal);
385 /// Prepends a new item with <see cref="GenListItemType"/> to the beginning of a given genlist widget.
387 /// <param name="itemClass">The itemClass defines how to display the data.</param>
388 /// <param name="data">The item data.</param>
389 /// <param name="type">The genlist item type.</param>
390 /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
391 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
392 public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type)
394 return Prepend(itemClass, data, type, null);
398 /// 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.
400 /// <param name="itemClass">The itemClass defines how to display the data.</param>
401 /// <param name="data">The item data.</param>
402 /// <param name="type">The genlist item type.</param>
403 /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
404 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
405 public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
407 GenListItem item = new GenListItem(data, itemClass);
408 IntPtr handle = Interop.Elementary.elm_genlist_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
409 item.Handle = handle;
415 /// Inserts an item before another item in a genlist widget.
416 /// It is the same tree level or group as the item before which it is inserted.????
418 /// <param name="itemClass">The itemClass defines how to display the data.</param>
419 /// <param name="data">The item data.</param>
420 /// <param name="before">The item before which to place this new one.</param>
421 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
422 public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before)
424 return InsertBefore(itemClass, data, before, GenListItemType.Normal);
428 /// Inserts an item with <see cref="GenListItemType"/> before another item in a genlist widget.
429 /// It is the same tree level or group as the item before which it is inserted.????
431 /// <param name="itemClass">The itemClass defines how to display the data.</param>
432 /// <param name="data">The item data.</param>
433 /// <param name="before">The item before which to place this new one.</param>
434 /// <param name="type">The genlist item type.</param>
435 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
436 public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type)
438 return InsertBefore(itemClass, data, before, type, null);
442 /// Inserts an item with <see cref="GenListItemType"/> before another item under a parent in a genlist widget.
444 /// <param name="itemClass">The itemClass defines how to display the data.</param>
445 /// <param name="data">The item data.</param>
446 /// <param name="before">The item before which to place this new one.</param>
447 /// <param name="type">The genlist item type.</param>
448 /// <param name="parent">The parent item, otherwise null if there is no parent item.</param>
449 /// <returns>Return a new added genlist item that contains data and itemClass.</returns>
450 public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type, GenListItem parent)
452 GenListItem item = new GenListItem(data, itemClass);
453 // insert before the `before` list item
454 IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
455 RealHandle, // genlist handle
456 itemClass.UnmanagedPtr, // item class
457 (IntPtr)item.Id, // data
460 (int)type, // item type
461 null, // select callback
462 (IntPtr)item.Id); // callback data
463 item.Handle = handle;
469 /// Shows the given item with position type in a genlist.
470 /// 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.
471 /// 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.
473 /// <param name="item">The item to display.</param>
474 /// <param name="position">The position to show the given item to <see cref="ScrollToPosition"/>.</param>
475 /// <param name="animated">The animated indicates how to display the item, by scrolling or by jumping.</param>
476 public void ScrollTo(GenListItem item, ScrollToPosition position, bool animated)
480 Interop.Elementary.elm_genlist_item_bring_in(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
484 Interop.Elementary.elm_genlist_item_show(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
489 /// Updates the content of all the realized items.
490 /// This updates all the realized items by calling all the <see cref="GenItemClass"/> again to get the content, text and states.
491 /// Use this when the original item data has changed and the changes are desired to reflect.
492 /// To update just one item, use <see cref="GenListItem.Update"/>.
494 /// <seealso cref="GenListItem.Update"/>
495 public void UpdateRealizedItems()
497 Interop.Elementary.elm_genlist_realized_items_update(RealHandle);
501 /// Removes all items from a given genlist widget.
502 /// This removes (and deletes) all items in obj, making it empty.
503 /// To delete just one item, use <see cref="ItemObject.Delete"/>.
505 /// <seealso cref="ItemObject.Delete"/>
508 Interop.Elementary.elm_genlist_clear(RealHandle);
511 protected override IntPtr CreateHandle(EvasObject parent)
513 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
514 Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
516 RealHandle = Interop.Elementary.elm_genlist_add(handle);
517 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
522 void InitializeSmartEvent()
524 _selected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "selected", GenListItemEventArgs.CreateFromSmartEvent);
525 _unselected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unselected", GenListItemEventArgs.CreateFromSmartEvent);
526 _activated = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "activated", GenListItemEventArgs.CreateFromSmartEvent);
527 _pressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "pressed", GenListItemEventArgs.CreateFromSmartEvent);
528 _released = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "released", GenListItemEventArgs.CreateFromSmartEvent);
529 _doubleClicked = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "clicked,double", GenListItemEventArgs.CreateFromSmartEvent);
530 _expanded = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "expanded", GenListItemEventArgs.CreateFromSmartEvent);
531 _realized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "realized", GenListItemEventArgs.CreateFromSmartEvent);
532 _unrealized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unrealized", GenListItemEventArgs.CreateFromSmartEvent);
533 _longpressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "longpressed", GenListItemEventArgs.CreateFromSmartEvent);
534 _moved = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved", GenListItemEventArgs.CreateFromSmartEvent);
535 _movedAfter = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,after", GenListItemEventArgs.CreateFromSmartEvent);
536 _movedBefore = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,before", GenListItemEventArgs.CreateFromSmartEvent);
537 _scrollAnimationStarted = new SmartEvent(this, this.RealHandle, "scroll,anim,start");
538 _scrollAnimationStopped = new SmartEvent(this, this.RealHandle, "scroll,anim,stop");
539 _changed = new SmartEvent(this, this.RealHandle, "changed");
541 _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
542 _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
543 _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
544 _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
545 _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
546 _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
547 _expanded.On += (s, e) => { if (e.Item != null) ItemExpanded?.Invoke(this, e); };
548 _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
549 _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
550 _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
551 _moved.On += (s, e) => { if (e.Item != null) ItemMoved?.Invoke(this, e); };
552 _movedAfter.On += (s, e) => { if (e.Item != null) ItemMovedAfter?.Invoke(this, e); };
553 _movedBefore.On += (s, e) => { if (e.Item != null) ItemMovedBefore?.Invoke(this, e); };
556 void AddInternal(GenListItem item)
559 item.Deleted += Item_Deleted;
562 void Item_Deleted(object sender, EventArgs e)
564 _children.Remove((GenListItem)sender);