/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; namespace ElmSharp { /// /// Enumeration for setting genlist item type. /// public enum GenListItemType { /// /// if Normal is set then this item is normal item. /// Normal = 0, /// /// If tree is set then this item is displayed as an item that is able to expand and have child items. /// Tree = (1 << 0), /// /// if Group is set then this item is group index item that is displayed at the top until the next group comes. /// Group = (1 << 1), } /// /// Enumeration for setting genlist's resizing behavior, transverse axis scrolling and items cropping. /// public enum GenListMode { /// /// The genlist won't set any of its size hints to inform how a possible container should resize it. /// Then, if it's not created as a "resize object", it might end with zeroed dimensions. /// The genlist will respect the container's geometry and, if any of its items won't fit into its transverse axis, one won't be able to scroll it in that direction. /// Compress = 0, /// /// 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. /// Scroll, /// /// Sets a minimum size hint on the genlist object, so that containers may respect it (and resize itself to fit the child properly). /// More specifically, a minimum size hint will be set for its transverse axis, so that the largest item in that direction fits well. /// This is naturally bound by the genlist object's maximum size hints, set externally. /// Limit, /// /// Besides setting a minimum size on the transverse axis, just like on Limit, the genlist will set a minimum size on th longitudinal axis, trying to reserve space to all its children to be visible at a time. /// This is naturally bound by the genlist object's maximum size hints, set externally. /// Expand } /// /// It inherits System.EventArgs. /// It contains Item which is type. /// All events of GenList contain GenListItemEventArgs as a parameter. /// public class GenListItemEventArgs : EventArgs { /// /// Gets or sets GenList item. The return type is . /// public GenListItem Item { get; set; } internal static GenListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info) { GenListItem item = ItemObject.GetItemByHandle(info) as GenListItem; return new GenListItemEventArgs() { Item = item }; } } /// /// Enumeration that defines where to position the item in the genlist. /// public enum ScrollToPosition { /// /// Scrolls to nowhere. /// None = 0, /// /// Scrolls to the nearest viewport. /// In = (1 << 0), /// /// Scrolls to the top of the viewport. /// Top = (1 << 1), /// /// Scrolls to the middle of the viewport. /// Middle = (1 << 2), /// /// Scrolls to the bottom of the viewport. /// Bottom = (1 << 3) } /// /// It inherits . /// The GenList is a widget that aims to have more expansive list than the simple in ElmSharp that could have more flexible items and allow many more entries while still being fast and low on memory usage. /// At the same time it was also made to be able to do tree structures. /// But the price to pay is more complexity when it comes to usage. /// If all you want is a simple list with icons and a single text, use the widget. /// public class GenList : Layout { HashSet _children = new HashSet(); SmartEvent _selected; SmartEvent _unselected; SmartEvent _activated; SmartEvent _pressed; SmartEvent _released; SmartEvent _doubleClicked; SmartEvent _expanded; SmartEvent _realized; SmartEvent _unrealized; SmartEvent _longpressed; SmartEvent _moved; SmartEvent _movedAfter; SmartEvent _movedBefore; SmartEvent _scrollAnimationStarted; SmartEvent _scrollAnimationStopped; SmartEvent _changed; /// /// Creates and initializes a new instance of the GenList class. /// /// The parent is a given container which will be attached by GenList as a child. It's type. public GenList(EvasObject parent) : base(parent) { ListMode = GenListMode.Compress; InitializeSmartEvent(); } /// /// Gets or sets whether the homogeneous mode is enabled. /// /// /// If true, the genlist items have same height and width. /// public bool Homogeneous { get { return Interop.Elementary.elm_genlist_homogeneous_get(RealHandle); } set { Interop.Elementary.elm_genlist_homogeneous_set(RealHandle, value); } } /// /// Gets or sets the horizontal stretching mode. This mode used for sizing items horizontally. /// The default value is which means that if items are too wide to fit, the scroller scrolls horizontally. /// If set which means that the item width is fixed (restricted to a minimum of) to the list width when calculating its size in order to allow the height to be calculated based on it. /// If set which means that items are expanded to the viewport width and limited to that size. /// if set which means that genlist try to reserve space to all its items to be visible at a time. /// /// /// Compress makes genlist resize slower as it recalculates every item height again whenever the list width changes. /// The homogeneous mode is so that all items in the genlist are of the same width/height. With Compress, genlist items are initialized fast. /// However, there are no sub-objects in the genlist which can be on the flying resizable (such as TEXTBLOCK). /// If so, then some dynamic resizable objects in the genlist would not be diplayed properly. /// public GenListMode ListMode { get { return (GenListMode)Interop.Elementary.elm_genlist_mode_get(RealHandle); } set { Interop.Elementary.elm_genlist_mode_set(RealHandle, (int)value); } } /// /// Gets the first item in the genlist. /// public GenListItem FirstItem { get { IntPtr handle = Interop.Elementary.elm_genlist_first_item_get(RealHandle); return ItemObject.GetItemByHandle(handle) as GenListItem; } } /// /// Gets the last item in the genlist. /// public GenListItem LastItem { get { IntPtr handle = Interop.Elementary.elm_genlist_last_item_get(RealHandle); return ItemObject.GetItemByHandle(handle) as GenListItem; } } /// /// Gets or sets the reorder mode. /// After turning on the reorder mode, longpress on a normal item triggers reordering of the item. /// You can move the item up and down. However, reordering does not work with group items. /// public bool ReorderMode { get { return Interop.Elementary.elm_genlist_reorder_mode_get(RealHandle); } set { Interop.Elementary.elm_genlist_reorder_mode_set(RealHandle, value); } } /// /// ItemSelected is raised when a new genlist item is selected. /// public event EventHandler ItemSelected; /// /// ItemUnselected is raised when the genlist item is Unselected. /// public event EventHandler ItemUnselected; /// /// ItemPressed is raised when a new genlist item is pressed. /// public event EventHandler ItemPressed; /// /// ItemReleased is raised when a new genlist item is released. /// public event EventHandler ItemReleased; /// /// ItemActivated is raised when a new genlist item is double clicked or pressed (enter|return|spacebar). /// public event EventHandler ItemActivated; /// /// ItemDoubleClicked is raised when a new genlist item is double clicked. /// public event EventHandler ItemDoubleClicked; /// /// ItemExpanded is raised when a new genlist item is indicated to expand. /// public event EventHandler ItemExpanded; /// /// ItemRealized is raised when a new genlist item is created as a real object. /// public event EventHandler ItemRealized; /// /// ItemUnrealized is raised when a new genlist item is unrealized. /// After calling unrealize, the item's content objects are deleted and the item object itself is deleted or is put into a floating cache. /// public event EventHandler ItemUnrealized; /// /// ItemLongPressed is raised when a genlist item is pressed for a certain amount of time. By default it's 1 second. /// public event EventHandler ItemLongPressed; /// /// ItemMoved is raised when a genlist item is moved in the reorder mode. /// public event EventHandler ItemMoved; /// /// ItemMovedAfter is raised when a genlist item is moved after another item in the reorder mode. /// To get the relative previous item, use . /// public event EventHandler ItemMovedAfter; /// /// ItemMovedBefore is raised when a genlist item is moved before another item in the reorder mode. /// To get the relative next item, use . /// public event EventHandler ItemMovedBefore; /// /// Changed is raised when genlist is changed. /// public event EventHandler Changed { add { _changed.On += value; } remove { _changed.On -= value; } } /// /// ScrollAnimationStarted is raised when scrolling animation has started. /// public event EventHandler ScrollAnimationStarted { add { _scrollAnimationStarted.On += value; } remove { _scrollAnimationStarted.On -= value; } } /// /// ScrollAnimationStopped is raised when scrolling animation has stopped. /// public event EventHandler ScrollAnimationStopped { add { _scrollAnimationStopped.On += value; } remove { _scrollAnimationStopped.On -= value; } } /// /// Appends a new item to the end of a given genlist widget. /// /// The itemClass defines how to display the data. /// The item data. /// Return a new added genlist item that contains data and itemClass. /// /// public GenListItem Append(GenItemClass itemClass, object data) { return Append(itemClass, data, GenListItemType.Normal); } /// /// Appends a new item with to the end of a given genlist widget. /// /// The itemClass defines how to display the data. /// The item data. /// The genlist item type. /// Return a new added genlist item that contains data and itemClass. public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type) { return Append(itemClass, data, type, null); } /// /// Appends a new item with to the end of a given genlist widget or the end of the children list if the parent is given. /// /// The itemClass defines how to display the data. /// The item data. /// The genlist item type. /// The parent item, otherwise null if there is no parent item. /// Return a new added genlist item that contains data and itemClass. public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent) { GenListItem item = new GenListItem(data, itemClass); IntPtr handle = Interop.Elementary.elm_genlist_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id); item.Handle = handle; AddInternal(item); return item; } /// /// Prepends a new item to the beginning of a given genlist widget. /// /// The itemClass defines how to display the data. /// The item data. /// Return a new added genlist item that contains data and itemClass. public GenListItem Prepend(GenItemClass itemClass, object data) { return Prepend(itemClass, data, GenListItemType.Normal); } /// /// Prepends a new item with to the beginning of a given genlist widget. /// /// The itemClass defines how to display the data. /// The item data. /// The genlist item type. /// The parent item, otherwise null if there is no parent item. /// Return a new added genlist item that contains data and itemClass. public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type) { return Prepend(itemClass, data, type, null); } /// /// Prepends a new item with to the beginning of a given genlist widget or the beginning of the children list if the parent is given. /// /// The itemClass defines how to display the data. /// The item data. /// The genlist item type. /// The parent item, otherwise null if there is no parent item. /// Return a new added genlist item that contains data and itemClass. public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent) { GenListItem item = new GenListItem(data, itemClass); IntPtr handle = Interop.Elementary.elm_genlist_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id); item.Handle = handle; AddInternal(item); return item; } /// /// Inserts an item before another item in a genlist widget. /// It is the same tree level or group as the item before which it is inserted.???? /// /// The itemClass defines how to display the data. /// The item data. /// The item before which to place this new one. /// Return a new added genlist item that contains data and itemClass. public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before) { return InsertBefore(itemClass, data, before, GenListItemType.Normal); } /// /// Inserts an item with before another item in a genlist widget. /// It is the same tree level or group as the item before which it is inserted.???? /// /// The itemClass defines how to display the data. /// The item data. /// The item before which to place this new one. /// The genlist item type. /// Return a new added genlist item that contains data and itemClass. public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type) { return InsertBefore(itemClass, data, before, type, null); } /// /// Inserts an item with before another item under a parent in a genlist widget. /// /// The itemClass defines how to display the data. /// The item data. /// The item before which to place this new one. /// The genlist item type. /// The parent item, otherwise null if there is no parent item. /// Return a new added genlist item that contains data and itemClass. public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type, GenListItem parent) { GenListItem item = new GenListItem(data, itemClass); // insert before the `before` list item IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before( RealHandle, // genlist handle itemClass.UnmanagedPtr, // item class (IntPtr)item.Id, // data parent, // parent before, // before (int)type, // item type null, // select callback (IntPtr)item.Id); // callback data item.Handle = handle; AddInternal(item); return item; } /// /// Shows the given item with position type in a genlist. /// When animated is true, genlist will jump to the given item and display it (by animatedly scrolling), if it is not fully visible. This may use animation and may take some time. /// When animated is false, genlist will jump to the given item and display it (by jumping to that position), if it is not fully visible. /// /// The item to display. /// The position to show the given item to . /// The animated indicates how to display the item, by scrolling or by jumping. public void ScrollTo(GenListItem item, ScrollToPosition position, bool animated) { if (animated) { Interop.Elementary.elm_genlist_item_bring_in(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position); } else { Interop.Elementary.elm_genlist_item_show(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position); } } /// /// Updates the content of all the realized items. /// This updates all the realized items by calling all the again to get the content, text and states. /// Use this when the original item data has changed and the changes are desired to reflect. /// To update just one item, use . /// /// public void UpdateRealizedItems() { Interop.Elementary.elm_genlist_realized_items_update(RealHandle); } /// /// Removes all items from a given genlist widget. /// This removes (and deletes) all items in obj, making it empty. /// To delete just one item, use . /// /// public void Clear() { Interop.Elementary.elm_genlist_clear(RealHandle); } protected override IntPtr CreateHandle(EvasObject parent) { IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle); Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default"); RealHandle = Interop.Elementary.elm_genlist_add(handle); Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle); return handle; } void InitializeSmartEvent() { _selected = new SmartEvent(this, this.RealHandle, "selected", GenListItemEventArgs.CreateFromSmartEvent); _unselected = new SmartEvent(this, this.RealHandle, "unselected", GenListItemEventArgs.CreateFromSmartEvent); _activated = new SmartEvent(this, this.RealHandle, "activated", GenListItemEventArgs.CreateFromSmartEvent); _pressed = new SmartEvent(this, this.RealHandle, "pressed", GenListItemEventArgs.CreateFromSmartEvent); _released = new SmartEvent(this, this.RealHandle, "released", GenListItemEventArgs.CreateFromSmartEvent); _doubleClicked = new SmartEvent(this, this.RealHandle, "clicked,double", GenListItemEventArgs.CreateFromSmartEvent); _expanded = new SmartEvent(this, this.RealHandle, "expanded", GenListItemEventArgs.CreateFromSmartEvent); _realized = new SmartEvent(this, this.RealHandle, "realized", GenListItemEventArgs.CreateFromSmartEvent); _unrealized = new SmartEvent(this, this.RealHandle, "unrealized", GenListItemEventArgs.CreateFromSmartEvent); _longpressed = new SmartEvent(this, this.RealHandle, "longpressed", GenListItemEventArgs.CreateFromSmartEvent); _moved = new SmartEvent(this, this.RealHandle, "moved", GenListItemEventArgs.CreateFromSmartEvent); _movedAfter = new SmartEvent(this, this.RealHandle, "moved,after", GenListItemEventArgs.CreateFromSmartEvent); _movedBefore = new SmartEvent(this, this.RealHandle, "moved,before", GenListItemEventArgs.CreateFromSmartEvent); _scrollAnimationStarted = new SmartEvent(this, this.RealHandle, "scroll,anim,start"); _scrollAnimationStopped = new SmartEvent(this, this.RealHandle, "scroll,anim,stop"); _changed = new SmartEvent(this, this.RealHandle, "changed"); _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); }; _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); }; _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); }; _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); }; _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); }; _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); }; _expanded.On += (s, e) => { if (e.Item != null) ItemExpanded?.Invoke(this, e); }; _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); }; _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); }; _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); }; _moved.On += (s, e) => { if (e.Item != null) ItemMoved?.Invoke(this, e); }; _movedAfter.On += (s, e) => { if (e.Item != null) ItemMovedAfter?.Invoke(this, e); }; _movedBefore.On += (s, e) => { if (e.Item != null) ItemMovedBefore?.Invoke(this, e); }; } void AddInternal(GenListItem item) { _children.Add(item); item.Deleted += Item_Deleted; } void Item_Deleted(object sender, EventArgs e) { _children.Remove((GenListItem)sender); } } }