/* * 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 list's resizing behavior, transverse axis scrolling and items cropping. /// public enum ListMode { /// /// The list 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 list 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 list object's maximum size hints, set externally. /// Limit, /// /// Besides setting a minimum size on the transverse axis, just like on Limit, the list 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 list object's maximum size hints, set externally. /// Expand } /// /// It inherits System.EventArgs. /// It contains Item which is type. /// All events of List contain ListItemEventArgs as a parameter. /// public class ListItemEventArgs : EventArgs { /// /// Gets or sets List item. The return type is . /// public ListItem Item { get; set; } internal static ListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info) { ListItem item = ItemObject.GetItemByHandle(info) as ListItem; return new ListItemEventArgs() { Item = item }; } } /// /// It inherits . /// The List is a widget that aims to display simple list item which has 2 icons and 1 text, and can be selected. /// For more robust lists, should probably be used. /// /// /// public class List : Layout { HashSet _children = new HashSet(); SmartEvent _selected; SmartEvent _unselected; SmartEvent _doubleClicked; SmartEvent _longpressed; SmartEvent _activated; /// /// Creates and initializes a new instance of the List class. /// /// The parent is a given container which will be attached by List as a child. It's type. public List(EvasObject parent) : base(parent) { _selected = new SmartEvent(this, this.RealHandle, "selected", ListItemEventArgs.CreateFromSmartEvent); _unselected = new SmartEvent(this, this.RealHandle, "unselected", ListItemEventArgs.CreateFromSmartEvent); _doubleClicked = new SmartEvent(this, this.RealHandle, "clicked,double", ListItemEventArgs.CreateFromSmartEvent); _longpressed = new SmartEvent(this, this.RealHandle, "longpressed", ListItemEventArgs.CreateFromSmartEvent); _activated = new SmartEvent(this, this.RealHandle, "activated", ListItemEventArgs.CreateFromSmartEvent); _selected.On += (s, e) => { ItemSelected?.Invoke(this, e); }; _unselected.On += (s, e) => { ItemUnselected?.Invoke(this, e); }; _doubleClicked.On += (s, e) => { ItemDoubleClicked?.Invoke(this, e); }; _longpressed.On += (s, e) => { ItemLongPressed?.Invoke(this, e); }; _activated.On += (s, e) => { ItemActivated?.Invoke(this, e); }; } /// /// Gets or sets which mode to use for the list. /// public ListMode Mode { get { return (ListMode)Interop.Elementary.elm_list_mode_get(RealHandle); } set { Interop.Elementary.elm_list_mode_set(RealHandle, (Interop.Elementary.Elm_List_Mode)value); } } /// /// Gets the selected item. /// public ListItem SelectedItem { get { IntPtr item = Interop.Elementary.elm_list_selected_item_get(RealHandle); return ItemObject.GetItemByHandle(item) as ListItem; } } /// /// ItemSelected is raised when a new list item is selected. /// public event EventHandler ItemSelected; /// /// ItemUnselected is raised when the list item is Unselected. /// public event EventHandler ItemUnselected; /// /// ItemDoubleClicked is raised when a new list item is double clicked. /// public event EventHandler ItemDoubleClicked; /// /// ItemLongPressed is raised when a list item is pressed for a certain amount of time. By default it's 1 second. /// public event EventHandler ItemLongPressed; /// /// ItemActivated is raised when a new list item is double clicked or pressed (enter|return|spacebar). /// public event EventHandler ItemActivated; /// /// Starts the list. /// Call before running on the list object. /// If not called, it won't display the list properly. /// public void Update() { Interop.Elementary.elm_list_go(RealHandle); } /// /// Appends a new item with a text to the end of a given list widget. /// /// The text for the item. /// Return a new added list item that contains a text. /// public ListItem Append(string label) { return Append(label, null, null); } /// /// Appends a new item with a text and 2 icons to the end of a given list widget. /// /// The text for the item. /// The left icon for the item. /// The right icon for the item. /// Return a new added list item that contains a text and 2 icons. /// public ListItem Append(string label, EvasObject leftIcon, EvasObject rightIcon) { ListItem item = new ListItem(label, leftIcon, rightIcon); item.Handle = Interop.Elementary.elm_list_item_append(RealHandle, label, leftIcon, rightIcon, null, (IntPtr)item.Id); AddInternal(item); return item; } /// /// Prepends a new item with a text to the beginning of a given list widget. /// /// The text for the item. /// Return a new added list item that contains a text. public ListItem Prepend(string label) { return Prepend(label, null, null); } /// /// Prepends a new item with a text and 2 icons to the beginning of a given list widget. /// /// The text for the item. /// The left icon for the item. /// The right icon for the item. /// Return a new added list item that contains a text and 2 icons. public ListItem Prepend(string label, EvasObject leftIcon, EvasObject rigthIcon) { ListItem item = new ListItem(label, leftIcon, rigthIcon); item.Handle = Interop.Elementary.elm_list_item_prepend(RealHandle, label, leftIcon, rigthIcon, null, (IntPtr)item.Id); AddInternal(item); return item; } /// /// Removes all items from a given list widget. /// To delete just one item, use . /// public void Clear() { Interop.Elementary.elm_list_clear(RealHandle); foreach (var item in _children) { item.Deleted -= Item_Deleted; } _children.Clear(); } 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_list_add(handle); Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle); return handle; } void AddInternal(ListItem item) { _children.Add(item); item.Deleted += Item_Deleted; } void Item_Deleted(object sender, EventArgs e) { _children.Remove((ListItem)sender); } } }