Merge remote-tracking branch 'origin/master' into merge_API4
[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 the list's resizing behavior, transverse axis scrolling, and items cropping.
24     /// </summary>
25     /// <since_tizen> preview </since_tizen>
26     public enum ListMode
27     {
28         /// <summary>
29         /// The list won't set any of its size hints to inform how a possible container should resize it.
30         /// Then, if it's not created as a "resize object", it might end with zeroed dimensions.
31         /// 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.
32         /// </summary>
33         Compress = 0,
34         /// <summary>
35         /// 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.
36         /// </summary>
37         Scroll,
38         /// <summary>
39         /// Sets a minimum size hint on the genlist object, so that the containers may respect it (and resize itself to fit the child properly).
40         /// More specifically, a minimum size hint will be set for its transverse axis, so that the largest item in that direction fits well.
41         /// This is naturally bound by the list object's maximum size hints, set externally.
42         /// </summary>
43         Limit,
44         /// <summary>
45         /// Besides setting a minimum size on the transverse axis, just like on limit, the list will set a minimum size on the longitudinal axis, trying to reserve space to all its children to be visible at a time.
46         /// This is naturally bound by the list object's maximum size hints, set externally.
47         /// </summary>
48         Expand
49     }
50
51     /// <summary>
52     /// It inherits System.EventArgs.
53     /// It contains an item which is <see cref="ListItem"/> type.
54     /// All the events of a list contain ListItemEventArgs as a parameter.
55     /// </summary>
56     /// <since_tizen> preview </since_tizen>
57     public class ListItemEventArgs : EventArgs
58     {
59         /// <summary>
60         /// Gets or sets the list item. The return type is <see cref="ListItem"/>.
61         /// </summary>
62         /// <since_tizen> preview </since_tizen>
63         public ListItem Item { get; set; }
64
65         internal static ListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
66         {
67             ListItem item = ItemObject.GetItemByHandle(info) as ListItem;
68             return new ListItemEventArgs() { Item = item };
69         }
70     }
71
72     /// <summary>
73     /// It inherits <see cref="Layout"/>.
74     /// The List is a widget that aims to display a simple list item which has 2 icons, 1 text, and can be selected.
75     /// For more robust lists, <see cref="GenList"/> should probably be used.
76     /// </summary>
77     /// <seealso cref="GenList"/>
78     /// <seealso cref="GenGrid"/>
79     /// <since_tizen> preview </since_tizen>
80     public class List : Layout
81     {
82         HashSet<ListItem> _children = new HashSet<ListItem>();
83         SmartEvent<ListItemEventArgs> _selected;
84         SmartEvent<ListItemEventArgs> _unselected;
85         SmartEvent<ListItemEventArgs> _doubleClicked;
86         SmartEvent<ListItemEventArgs> _longpressed;
87         SmartEvent<ListItemEventArgs> _activated;
88
89         /// <summary>
90         /// Creates and initializes a new instance of the List class.
91         /// </summary>
92         /// <param name="parent">The parent is a given container, which will be attached by the list as a child. It's <see cref="EvasObject"/> type.</param>
93         /// <since_tizen> preview </since_tizen>
94         public List(EvasObject parent) : base(parent)
95         {
96             _selected = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "selected", ListItemEventArgs.CreateFromSmartEvent);
97             _unselected = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "unselected", ListItemEventArgs.CreateFromSmartEvent);
98             _doubleClicked = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "clicked,double", ListItemEventArgs.CreateFromSmartEvent);
99             _longpressed = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "longpressed", ListItemEventArgs.CreateFromSmartEvent);
100             _activated = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "activated", ListItemEventArgs.CreateFromSmartEvent);
101             _selected.On += (s, e) => { ItemSelected?.Invoke(this, e); };
102             _unselected.On += (s, e) => { ItemUnselected?.Invoke(this, e); };
103             _doubleClicked.On += (s, e) => { ItemDoubleClicked?.Invoke(this, e); };
104             _longpressed.On += (s, e) => { ItemLongPressed?.Invoke(this, e); };
105             _activated.On += (s, e) => { ItemActivated?.Invoke(this, e); };
106         }
107
108         /// <summary>
109         /// Gets or sets which mode to be used for the list.
110         /// </summary>
111         /// <since_tizen> preview </since_tizen>
112         public ListMode Mode
113         {
114             get
115             {
116                 return (ListMode)Interop.Elementary.elm_list_mode_get(RealHandle);
117             }
118             set
119             {
120                 Interop.Elementary.elm_list_mode_set(RealHandle, (Interop.Elementary.Elm_List_Mode)value);
121             }
122         }
123
124         /// <summary>
125         /// Gets the selected item.
126         /// </summary>
127         /// <since_tizen> preview </since_tizen>
128         public ListItem SelectedItem
129         {
130             get
131             {
132                 IntPtr item = Interop.Elementary.elm_list_selected_item_get(RealHandle);
133                 return ItemObject.GetItemByHandle(item) as ListItem;
134             }
135         }
136
137         /// <summary>
138         /// ItemSelected is raised when a new list item is selected.
139         /// </summary>
140         /// <since_tizen> preview </since_tizen>
141         public event EventHandler<ListItemEventArgs> ItemSelected;
142
143         /// <summary>
144         /// ItemUnselected is raised when a list item is Unselected.
145         /// </summary>
146         /// <since_tizen> preview </since_tizen>
147         public event EventHandler<ListItemEventArgs> ItemUnselected;
148
149         /// <summary>
150         /// ItemDoubleClicked is raised when a new list item is double-clicked.
151         /// </summary>
152         /// <since_tizen> preview </since_tizen>
153         public event EventHandler<ListItemEventArgs> ItemDoubleClicked;
154
155         /// <summary>
156         /// ItemLongPressed is raised when a list item is pressed for a certain amount of time. By default, it's 1 second.
157         /// </summary>
158         /// <since_tizen> preview </since_tizen>
159         public event EventHandler<ListItemEventArgs> ItemLongPressed;
160
161         /// <summary>
162         /// ItemActivated is raised when a new list item is double-clicked or pressed (enter|return|spacebar).
163         /// </summary>
164         /// <since_tizen> preview </since_tizen>
165         public event EventHandler<ListItemEventArgs> ItemActivated;
166
167         /// <summary>
168         /// Starts the list.
169         /// Called before running <see cref="EvasObject.Show"/> on the list object.
170         /// If not called, it won't display the list properly.
171         /// </summary>
172         /// <since_tizen> preview </since_tizen>
173         public void Update()
174         {
175             Interop.Elementary.elm_list_go(RealHandle);
176         }
177
178         /// <summary>
179         /// Appends a new item with a text to the end of a given list widget.
180         /// </summary>
181         /// <param name="label">The text for the item.</param>
182         /// <returns>Return a new added list item that contains a text.</returns>
183         /// <seealso cref="ListItem"/>
184         /// <since_tizen> preview </since_tizen>
185         public ListItem Append(string label)
186         {
187             return Append(label, null, null);
188         }
189
190         /// <summary>
191         /// Appends a new item with a text and 2 icons to the end of a given list widget.
192         /// </summary>
193         /// <param name="label">The text for the item.</param>
194         /// <param name="leftIcon">The left icon for the item.</param>
195         /// <param name="rightIcon">The right icon for the item.</param>
196         /// <returns>Return a new added list item that contains a text and 2 icons.</returns>
197         /// <seealso cref="ListItem"/>
198         /// <since_tizen> preview </since_tizen>
199         public ListItem Append(string label, EvasObject leftIcon, EvasObject rightIcon)
200         {
201             ListItem item = new ListItem(label, leftIcon, rightIcon);
202             item.Handle = Interop.Elementary.elm_list_item_append(RealHandle, label, leftIcon, rightIcon, null, (IntPtr)item.Id);
203             AddInternal(item);
204             return item;
205         }
206
207         /// <summary>
208         /// Prepends a new item with a text to the beginning of a given list widget.
209         /// </summary>
210         /// <param name="label">The text for the item.</param>
211         /// <returns>Return a new added list item that contains a text.</returns>
212         /// <since_tizen> preview </since_tizen>
213         public ListItem Prepend(string label)
214         {
215             return Prepend(label, null, null);
216         }
217
218         /// <summary>
219         /// Prepends a new item with a text and 2 icons to the beginning of a given list widget.
220         /// </summary>
221         /// <param name="label">The text for the item.</param>
222         /// <param name="leftIcon">The left icon for the item.</param>
223         /// <param name="rigthIcon">The right icon for the item.</param>
224         /// <returns>Return a new added list item that contains a text and 2 icons.</returns>
225         /// <since_tizen> preview </since_tizen>
226         public ListItem Prepend(string label, EvasObject leftIcon, EvasObject rigthIcon)
227         {
228             ListItem item = new ListItem(label, leftIcon, rigthIcon);
229             item.Handle = Interop.Elementary.elm_list_item_prepend(RealHandle, label, leftIcon, rigthIcon, null, (IntPtr)item.Id);
230             AddInternal(item);
231             return item;
232         }
233
234         /// <summary>
235         /// Removes all the items from a given list widget.
236         /// To delete just one item, use <see cref="ItemObject.Delete"/>.
237         /// </summary>
238         /// <since_tizen> preview </since_tizen>
239         public void Clear()
240         {
241             Interop.Elementary.elm_list_clear(RealHandle);
242             foreach (var item in _children)
243             {
244                 item.Deleted -= Item_Deleted;
245             }
246             _children.Clear();
247         }
248
249         /// <summary>
250         /// Creates a widget handle.
251         /// </summary>
252         /// <param name="parent">Parent EvasObject.</param>
253         /// <returns>Handle IntPtr.</returns>
254         /// <since_tizen> preview </since_tizen>
255         protected override IntPtr CreateHandle(EvasObject parent)
256         {
257             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
258             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
259
260             RealHandle = Interop.Elementary.elm_list_add(handle);
261             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
262
263             return handle;
264         }
265
266         void AddInternal(ListItem item)
267         {
268             _children.Add(item);
269             item.Deleted += Item_Deleted;
270         }
271
272         void Item_Deleted(object sender, EventArgs e)
273         {
274             _children.Remove((ListItem)sender);
275         }
276     }
277 }