Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / List.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 list's resizing behavior, transverse axis scrolling and items cropping.
24     /// </summary>
25     public enum ListMode
26     {
27         /// <summary>
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.
31         /// </summary>
32         Compress = 0,
33         /// <summary>
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.
35         /// </summary>
36         Scroll,
37         /// <summary>
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.
41         /// </summary>
42         Limit,
43         /// <summary>
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.
46         /// </summary>
47         Expand
48     }
49
50     /// <summary>
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.
54     /// </summary>
55     public class ListItemEventArgs : EventArgs
56     {
57         /// <summary>
58         /// Gets or sets List item. The return type is <see cref="ListItem"/>.
59         /// </summary>
60         public ListItem Item { get; set; }
61
62         internal static ListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
63         {
64             ListItem item = ItemObject.GetItemByHandle(info) as ListItem;
65             return new ListItemEventArgs() { Item = item };
66         }
67     }
68
69     /// <summary>
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.
73     /// </summary>
74     /// <seealso cref="GenList"/>
75     /// <seealso cref="GenGrid"/>
76     public class List : Layout
77     {
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;
84
85         /// <summary>
86         /// Creates and initializes a new instance of the List class.
87         /// </summary>
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)
90         {
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); };
101         }
102
103         /// <summary>
104         /// Gets or sets which mode to use for the list.
105         /// </summary>
106         public ListMode Mode
107         {
108             get
109             {
110                 return (ListMode)Interop.Elementary.elm_list_mode_get(RealHandle);
111             }
112             set
113             {
114                 Interop.Elementary.elm_list_mode_set(RealHandle, (Interop.Elementary.Elm_List_Mode)value);
115             }
116         }
117
118         /// <summary>
119         /// Gets the selected item.
120         /// </summary>
121         public ListItem SelectedItem
122         {
123             get
124             {
125                 IntPtr item = Interop.Elementary.elm_list_selected_item_get(RealHandle);
126                 return ItemObject.GetItemByHandle(item) as ListItem;
127             }
128         }
129
130         /// <summary>
131         /// ItemSelected is raised when a new list item is selected.
132         /// </summary>
133         public event EventHandler<ListItemEventArgs> ItemSelected;
134
135         /// <summary>
136         /// ItemUnselected is raised when the list item is Unselected.
137         /// </summary>
138         public event EventHandler<ListItemEventArgs> ItemUnselected;
139
140         /// <summary>
141         /// ItemDoubleClicked is raised when a new list item is double clicked.
142         /// </summary>
143         public event EventHandler<ListItemEventArgs> ItemDoubleClicked;
144
145         /// <summary>
146         /// ItemLongPressed is raised when a list item is pressed for a certain amount of time. By default it's 1 second.
147         /// </summary>
148         public event EventHandler<ListItemEventArgs> ItemLongPressed;
149
150         /// <summary>
151         /// ItemActivated is raised when a new list item is double clicked or pressed (enter|return|spacebar).
152         /// </summary>
153         public event EventHandler<ListItemEventArgs> ItemActivated;
154
155         /// <summary>
156         /// Starts the list.
157         /// Call before running <see cref="EvasObject.Show"/> on the list object.
158         /// If not called, it won't display the list properly.
159         /// </summary>
160         public void Update()
161         {
162             Interop.Elementary.elm_list_go(RealHandle);
163         }
164
165         /// <summary>
166         /// Appends a new item with a text to the end of a given list widget.
167         /// </summary>
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)
172         {
173             return Append(label, null, null);
174         }
175
176         /// <summary>
177         /// Appends a new item with a text and 2 icons to the end of a given list widget.
178         /// </summary>
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)
185         {
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);
188             AddInternal(item);
189             return item;
190         }
191
192         /// <summary>
193         /// Prepends a new item with a text to the beginning of a given list widget.
194         /// </summary>
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)
198         {
199             return Prepend(label, null, null);
200         }
201
202         /// <summary>
203         /// Prepends a new item with a text and 2 icons to the beginning of a given list widget.
204         /// </summary>
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)
210         {
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);
213             AddInternal(item);
214             return item;
215         }
216
217         /// <summary>
218         /// Removes all items from a given list widget.
219         /// To delete just one item, use <see cref="ItemObject.Delete"/>.
220         /// </summary>
221         public void Clear()
222         {
223             Interop.Elementary.elm_list_clear(RealHandle);
224             foreach (var item in _children)
225             {
226                 item.Deleted -= Item_Deleted;
227             }
228             _children.Clear();
229         }
230
231         protected override IntPtr CreateHandle(EvasObject parent)
232         {
233             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
234             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
235
236             RealHandle = Interop.Elementary.elm_list_add(handle);
237             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
238
239             return handle;
240         }
241
242         void AddInternal(ListItem item)
243         {
244             _children.Add(item);
245             item.Deleted += Item_Deleted;
246         }
247
248         void Item_Deleted(object sender, EventArgs e)
249         {
250             _children.Remove((ListItem)sender);
251         }
252     }
253 }