summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/List.cs
blob: 9a14f939b8f0c8bbce7f2fa5eb88de23780669f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * 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
{
    /// <summary>
    /// Enumeration for setting list's resizing behavior, transverse axis scrolling and items cropping.
    /// </summary>
    public enum ListMode
    {
        /// <summary>
        /// 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.
        /// </summary>
        Compress = 0,
        /// <summary>
        /// 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.
        /// </summary>
        Scroll,
        /// <summary>
        /// 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.
        /// </summary>
        Limit,
        /// <summary>
        /// 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.
        /// </summary>
        Expand
    }

    /// <summary>
    /// It inherits System.EventArgs.
    /// It contains Item which is <see cref="ListItem"/> type.
    /// All events of List contain ListItemEventArgs as a parameter.
    /// </summary>
    public class ListItemEventArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets List item. The return type is <see cref="ListItem"/>.
        /// </summary>
        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 };
        }
    }

    /// <summary>
    /// It inherits <see cref="Layout"/>.
    /// 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, <see cref="GenList"/> should probably be used.
    /// </summary>
    /// <seealso cref="GenList"/>
    /// <seealso cref="GenGrid"/>
    public class List : Layout
    {
        HashSet<ListItem> _children = new HashSet<ListItem>();
        SmartEvent<ListItemEventArgs> _selected;
        SmartEvent<ListItemEventArgs> _unselected;
        SmartEvent<ListItemEventArgs> _doubleClicked;
        SmartEvent<ListItemEventArgs> _longpressed;
        SmartEvent<ListItemEventArgs> _activated;

        /// <summary>
        /// Creates and initializes a new instance of the List class.
        /// </summary>
        /// <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>
        public List(EvasObject parent) : base(parent)
        {
            _selected = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "selected", ListItemEventArgs.CreateFromSmartEvent);
            _unselected = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "unselected", ListItemEventArgs.CreateFromSmartEvent);
            _doubleClicked = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "clicked,double", ListItemEventArgs.CreateFromSmartEvent);
            _longpressed = new SmartEvent<ListItemEventArgs>(this, this.RealHandle, "longpressed", ListItemEventArgs.CreateFromSmartEvent);
            _activated = new SmartEvent<ListItemEventArgs>(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); };
        }

        /// <summary>
        /// Gets or sets which mode to use for the list.
        /// </summary>
        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);
            }
        }

        /// <summary>
        /// Gets the selected item.
        /// </summary>
        public ListItem SelectedItem
        {
            get
            {
                IntPtr item = Interop.Elementary.elm_list_selected_item_get(RealHandle);
                return ItemObject.GetItemByHandle(item) as ListItem;
            }
        }

        /// <summary>
        /// ItemSelected is raised when a new list item is selected.
        /// </summary>
        public event EventHandler<ListItemEventArgs> ItemSelected;

        /// <summary>
        /// ItemUnselected is raised when the list item is Unselected.
        /// </summary>
        public event EventHandler<ListItemEventArgs> ItemUnselected;

        /// <summary>
        /// ItemDoubleClicked is raised when a new list item is double clicked.
        /// </summary>
        public event EventHandler<ListItemEventArgs> ItemDoubleClicked;

        /// <summary>
        /// ItemLongPressed is raised when a list item is pressed for a certain amount of time. By default it's 1 second.
        /// </summary>
        public event EventHandler<ListItemEventArgs> ItemLongPressed;

        /// <summary>
        /// ItemActivated is raised when a new list item is double clicked or pressed (enter|return|spacebar).
        /// </summary>
        public event EventHandler<ListItemEventArgs> ItemActivated;

        /// <summary>
        /// Starts the list.
        /// Call before running <see cref="EvasObject.Show"/> on the list object.
        /// If not called, it won't display the list properly.
        /// </summary>
        public void Update()
        {
            Interop.Elementary.elm_list_go(RealHandle);
        }

        /// <summary>
        /// Appends a new item with a text to the end of a given list widget.
        /// </summary>
        /// <param name="label">The text for the item.</param>
        /// <returns>Return a new added list item that contains a text.</returns>
        /// <seealso cref="ListItem"/>
        public ListItem Append(string label)
        {
            return Append(label, null, null);
        }

        /// <summary>
        /// Appends a new item with a text and 2 icons to the end of a given list widget.
        /// </summary>
        /// <param name="label">The text for the item.</param>
        /// <param name="leftIcon">The left icon for the item.</param>
        /// <param name="rightIcon">The right icon for the item.</param>
        /// <returns>Return a new added list item that contains a text and 2 icons.</returns>
        /// <seealso cref="ListItem"/>
        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;
        }

        /// <summary>
        /// Prepends a new item with a text to the beginning of a given list widget.
        /// </summary>
        /// <param name="label">The text for the item.</param>
        /// <returns>Return a new added list item that contains a text.</returns>
        public ListItem Prepend(string label)
        {
            return Prepend(label, null, null);
        }

        /// <summary>
        /// Prepends a new item with a text and 2 icons to the beginning of a given list widget.
        /// </summary>
        /// <param name="label">The text for the item.</param>
        /// <param name="leftIcon">The left icon for the item.</param>
        /// <param name="rigthIcon">The right icon for the item.</param>
        /// <returns>Return a new added list item that contains a text and 2 icons.</returns>
        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;
        }

        /// <summary>
        /// Removes all items from a given list widget.
        /// To delete just one item, use <see cref="ItemObject.Delete"/>.
        /// </summary>
        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);
        }
    }
}