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 list's resizing behavior, transverse axis scrolling and items cropping.
28 /// The list won't set any of its size hints to inform how a possible container should resize it.
29 /// Then, if it's not created as a "resize object", it might end with zeroed dimensions.
30 /// 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.
34 /// 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.
38 /// Sets a minimum size hint on the genlist object, so that containers may respect it (and resize itself to fit the child properly).
39 /// More specifically, a minimum size hint will be set for its transverse axis, so that the largest item in that direction fits well.
40 /// This is naturally bound by the list object's maximum size hints, set externally.
44 /// 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.
45 /// This is naturally bound by the list object's maximum size hints, set externally.
51 /// It inherits System.EventArgs.
52 /// It contains Item which is <see cref="ListItem"/> type.
53 /// All events of List contain ListItemEventArgs as a parameter.
55 public class ListItemEventArgs : EventArgs
58 /// Gets or sets List item. The return type is <see cref="ListItem"/>.
60 public ListItem Item { get; set; }
62 internal static ListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
64 ListItem item = ItemObject.GetItemByHandle(info) as ListItem;
65 return new ListItemEventArgs() { Item = item };
70 /// It inherits <see cref="Layout"/>.
71 /// The List is a widget that aims to display simple list item which has 2 icons and 1 text, and can be selected.
72 /// For more robust lists, <see cref="GenList"/> should probably be used.
74 /// <seealso cref="GenList"/>
75 /// <seealso cref="GenGrid"/>
76 public class List : Layout
78 HashSet<ListItem> _children = new HashSet<ListItem>();
79 SmartEvent<ListItemEventArgs> _selected;
80 SmartEvent<ListItemEventArgs> _unselected;
81 SmartEvent<ListItemEventArgs> _doubleClicked;
82 SmartEvent<ListItemEventArgs> _longpressed;
83 SmartEvent<ListItemEventArgs> _activated;
86 /// Creates and initializes a new instance of the List class.
88 /// <param name="parent">The parent is a given container which will be attached by List as a child. It's <see cref="EvasObject"/> type.</param>
89 public List(EvasObject parent) : base(parent)
91 _selected = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "selected", ListItemEventArgs.CreateFromSmartEvent);
92 _unselected = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "unselected", ListItemEventArgs.CreateFromSmartEvent);
93 _doubleClicked = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "clicked,double", ListItemEventArgs.CreateFromSmartEvent);
94 _longpressed = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "longpressed", ListItemEventArgs.CreateFromSmartEvent);
95 _activated = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "activated", ListItemEventArgs.CreateFromSmartEvent);
96 _selected.On += (s, e) => { ItemSelected?.Invoke(this, e); };
97 _unselected.On += (s, e) => { ItemUnselected?.Invoke(this, e); };
98 _doubleClicked.On += (s, e) => { ItemDoubleClicked?.Invoke(this, e); };
99 _longpressed.On += (s, e) => { ItemLongPressed?.Invoke(this, e); };
100 _activated.On += (s, e) => { ItemActivated?.Invoke(this, e); };
104 /// Gets or sets which mode to use for the list.
110 return (ListMode)Interop.Elementary.elm_list_mode_get(RealHandle);
114 Interop.Elementary.elm_list_mode_set(RealHandle, (Interop.Elementary.Elm_List_Mode)value);
119 /// Gets the selected item.
121 public ListItem SelectedItem
125 IntPtr item = Interop.Elementary.elm_list_selected_item_get(RealHandle);
126 return ItemObject.GetItemByHandle(item) as ListItem;
131 /// ItemSelected is raised when a new list item is selected.
133 public event EventHandler<ListItemEventArgs> ItemSelected;
136 /// ItemUnselected is raised when the list item is Unselected.
138 public event EventHandler<ListItemEventArgs> ItemUnselected;
141 /// ItemDoubleClicked is raised when a new list item is double clicked.
143 public event EventHandler<ListItemEventArgs> ItemDoubleClicked;
146 /// ItemLongPressed is raised when a list item is pressed for a certain amount of time. By default it's 1 second.
148 public event EventHandler<ListItemEventArgs> ItemLongPressed;
151 /// ItemActivated is raised when a new list item is double clicked or pressed (enter|return|spacebar).
153 public event EventHandler<ListItemEventArgs> ItemActivated;
157 /// Call before running <see cref="EvasObject.Show"/> on the list object.
158 /// If not called, it won't display the list properly.
162 Interop.Elementary.elm_list_go(RealHandle);
166 /// Appends a new item with a text to the end of a given list widget.
168 /// <param name="label">The text for the item.</param>
169 /// <returns>Return a new added list item that contains a text.</returns>
170 /// <seealso cref="ListItem"/>
171 public ListItem Append(string label)
173 return Append(label, null, null);
177 /// Appends a new item with a text and 2 icons to the end of a given list widget.
179 /// <param name="label">The text for the item.</param>
180 /// <param name="leftIcon">The left icon for the item.</param>
181 /// <param name="rightIcon">The right icon for the item.</param>
182 /// <returns>Return a new added list item that contains a text and 2 icons.</returns>
183 /// <seealso cref="ListItem"/>
184 public ListItem Append(string label, EvasObject leftIcon, EvasObject rightIcon)
186 ListItem item = new ListItem(label, leftIcon, rightIcon);
187 item.Handle = Interop.Elementary.elm_list_item_append(RealHandle, label, leftIcon, rightIcon, null, (IntPtr)item.Id);
193 /// Prepends a new item with a text to the beginning of a given list widget.
195 /// <param name="label">The text for the item.</param>
196 /// <returns>Return a new added list item that contains a text.</returns>
197 public ListItem Prepend(string label)
199 return Prepend(label, null, null);
203 /// Prepends a new item with a text and 2 icons to the beginning of a given list widget.
205 /// <param name="label">The text for the item.</param>
206 /// <param name="leftIcon">The left icon for the item.</param>
207 /// <param name="rigthIcon">The right icon for the item.</param>
208 /// <returns>Return a new added list item that contains a text and 2 icons.</returns>
209 public ListItem Prepend(string label, EvasObject leftIcon, EvasObject rigthIcon)
211 ListItem item = new ListItem(label, leftIcon, rigthIcon);
212 item.Handle = Interop.Elementary.elm_list_item_prepend(RealHandle, label, leftIcon, rigthIcon, null, (IntPtr)item.Id);
218 /// Removes all items from a given list widget.
219 /// To delete just one item, use <see cref="ItemObject.Delete"/>.
223 Interop.Elementary.elm_list_clear(RealHandle);
224 foreach (var item in _children)
226 item.Deleted -= Item_Deleted;
231 protected override IntPtr CreateHandle(EvasObject parent)
233 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
234 Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
236 RealHandle = Interop.Elementary.elm_list_add(handle);
237 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
242 void AddInternal(ListItem item)
245 item.Deleted += Item_Deleted;
248 void Item_Deleted(object sender, EventArgs e)
250 _children.Remove((ListItem)sender);