Merge "Add event pass properties in popup outside area" into tizen
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / GenList.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     public enum GenListItemType
23     {
24         Normal = 0,
25         Tree = (1 << 0),
26         Group = (1 << 1),
27     }
28
29     public enum GenListMode
30     {
31         Compress = 0,
32         Scroll,
33         Limit,
34         Expand
35     }
36
37     public class GenListItemEventArgs : EventArgs
38     {
39         public GenListItem Item { get; set; }
40
41         internal static GenListItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
42         {
43             GenListItem item = ItemObject.GetItemByHandle(info) as GenListItem;
44             return new GenListItemEventArgs() { Item = item };
45         }
46     }
47
48     public enum ScrollToPosition
49     {
50         None = 0,   // Scrolls to nowhere
51         In = (1 << 0),   // Scrolls to the nearest viewport
52         Top = (1 << 1),   // Scrolls to the top of the viewport
53         Middle = (1 << 2),   // Scrolls to the middle of the viewport
54         Bottom = (1 << 3)   // Scrolls to the bottom of the viewport
55     }
56
57     public class GenList : Layout
58     {
59         HashSet<GenListItem> _children = new HashSet<GenListItem>();
60
61         SmartEvent<GenListItemEventArgs> _selected;
62         SmartEvent<GenListItemEventArgs> _unselected;
63         SmartEvent<GenListItemEventArgs> _activated;
64         SmartEvent<GenListItemEventArgs> _pressed;
65         SmartEvent<GenListItemEventArgs> _released;
66         SmartEvent<GenListItemEventArgs> _doubleClicked;
67         SmartEvent<GenListItemEventArgs> _expanded;
68         SmartEvent<GenListItemEventArgs> _realized;
69         SmartEvent<GenListItemEventArgs> _unrealized;
70         SmartEvent<GenListItemEventArgs> _longpressed;
71         SmartEvent<GenListItemEventArgs> _moved;
72         SmartEvent<GenListItemEventArgs> _movedAfter;
73         SmartEvent<GenListItemEventArgs> _movedBefore;
74         SmartEvent _scrollAnimationStarted;
75         SmartEvent _scrollAnimationStopped;
76         SmartEvent _changed;
77
78         public GenList(EvasObject parent) : base(parent)
79         {
80             InitializeSmartEvent();
81         }
82
83         public bool Homogeneous
84         {
85             get
86             {
87                 return Interop.Elementary.elm_genlist_homogeneous_get(RealHandle);
88             }
89             set
90             {
91                 Interop.Elementary.elm_genlist_homogeneous_set(RealHandle, value);
92             }
93         }
94
95         public GenListMode ListMode
96         {
97             get
98             {
99                 return (GenListMode)Interop.Elementary.elm_genlist_mode_get(RealHandle);
100             }
101             set
102             {
103                 Interop.Elementary.elm_genlist_mode_set(RealHandle, (int)value);
104             }
105         }
106
107         public GenListItem FirstItem
108         {
109             get
110             {
111                 IntPtr handle = Interop.Elementary.elm_genlist_first_item_get(RealHandle);
112                 return ItemObject.GetItemByHandle(handle) as GenListItem;
113             }
114         }
115
116         public GenListItem LastItem
117         {
118             get
119             {
120                 IntPtr handle = Interop.Elementary.elm_genlist_last_item_get(RealHandle);
121                 return ItemObject.GetItemByHandle(handle) as GenListItem;
122             }
123         }
124
125         public bool ReorderMode
126         {
127             get
128             {
129                 return Interop.Elementary.elm_genlist_reorder_mode_get(RealHandle);
130             }
131             set
132             {
133                 Interop.Elementary.elm_genlist_reorder_mode_set(RealHandle, value);
134             }
135         }
136
137         public event EventHandler<GenListItemEventArgs> ItemSelected;
138         public event EventHandler<GenListItemEventArgs> ItemUnselected;
139         public event EventHandler<GenListItemEventArgs> ItemPressed;
140         public event EventHandler<GenListItemEventArgs> ItemReleased;
141         public event EventHandler<GenListItemEventArgs> ItemActivated;
142         public event EventHandler<GenListItemEventArgs> ItemDoubleClicked;
143         public event EventHandler<GenListItemEventArgs> ItemExpanded;
144         public event EventHandler<GenListItemEventArgs> ItemRealized;
145         public event EventHandler<GenListItemEventArgs> ItemUnrealized;
146         public event EventHandler<GenListItemEventArgs> ItemLongPressed;
147         public event EventHandler<GenListItemEventArgs> ItemMoved;
148         public event EventHandler<GenListItemEventArgs> ItemMovedAfter;
149         public event EventHandler<GenListItemEventArgs> ItemMovedBefore;
150
151         public event EventHandler Changed
152         {
153             add { _changed.On += value; }
154             remove { _changed.On -= value; }
155         }
156
157         public event EventHandler ScrollAnimationStarted
158         {
159             add { _scrollAnimationStarted.On += value; }
160             remove { _scrollAnimationStarted.On -= value; }
161         }
162
163         public event EventHandler ScrollAnimationStopped
164         {
165             add { _scrollAnimationStopped.On += value; }
166             remove { _scrollAnimationStopped.On -= value; }
167         }
168
169         public GenListItem Append(GenItemClass itemClass, object data)
170         {
171             return Append(itemClass, data, GenListItemType.Normal);
172         }
173
174         public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type)
175         {
176             return Append(itemClass, data, type, null);
177         }
178
179         public GenListItem Append(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
180         {
181             GenListItem item = new GenListItem(data, itemClass);
182             IntPtr handle = Interop.Elementary.elm_genlist_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
183             item.Handle = handle;
184             AddInternal(item);
185             return item;
186         }
187
188         public GenListItem Prepend(GenItemClass itemClass, object data)
189         {
190             return Prepend(itemClass, data, GenListItemType.Normal);
191         }
192
193         public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type)
194         {
195             return Prepend(itemClass, data, type, null);
196         }
197
198         public GenListItem Prepend(GenItemClass itemClass, object data, GenListItemType type, GenListItem parent)
199         {
200             GenListItem item = new GenListItem(data, itemClass);
201             IntPtr handle = Interop.Elementary.elm_genlist_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, parent, (int)type, null, (IntPtr)item.Id);
202             item.Handle = handle;
203             AddInternal(item);
204             return item;
205         }
206
207         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before)
208         {
209             return InsertBefore(itemClass, data, before, GenListItemType.Normal);
210         }
211         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type)
212         {
213             return InsertBefore(itemClass, data, before, type, null);
214         }
215         public GenListItem InsertBefore(GenItemClass itemClass, object data, GenListItem before, GenListItemType type, GenListItem parent)
216         {
217             GenListItem item = new GenListItem(data, itemClass);
218             // insert before the `before` list item
219             IntPtr handle = Interop.Elementary.elm_genlist_item_insert_before(
220                 RealHandle, // genlist handle
221                 itemClass.UnmanagedPtr, // item class
222                 (IntPtr)item.Id, // data
223                 parent, // parent
224                 before, // before
225                 (int)type, // item type
226                 null, // select callback
227                 (IntPtr)item.Id); // callback data
228             item.Handle = handle;
229             AddInternal(item);
230             return item;
231         }
232
233         public void ScrollTo(GenListItem item, ScrollToPosition position, bool animated)
234         {
235             if (animated)
236             {
237                 Interop.Elementary.elm_genlist_item_bring_in(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
238             }
239             else
240             {
241                 Interop.Elementary.elm_genlist_item_show(item.Handle, (Interop.Elementary.Elm_Genlist_Item_Scrollto_Type)position);
242             }
243         }
244
245         public void UpdateRealizedItems()
246         {
247             Interop.Elementary.elm_genlist_realized_items_update(RealHandle);
248         }
249
250         public void Clear()
251         {
252             Interop.Elementary.elm_genlist_clear(RealHandle);
253         }
254
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_genlist_add(handle);
261             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
262
263             return handle;
264         }
265
266         void InitializeSmartEvent()
267         {
268             _selected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "selected", GenListItemEventArgs.CreateFromSmartEvent);
269             _unselected = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unselected", GenListItemEventArgs.CreateFromSmartEvent);
270             _activated = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "activated", GenListItemEventArgs.CreateFromSmartEvent);
271             _pressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "pressed", GenListItemEventArgs.CreateFromSmartEvent);
272             _released = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "released", GenListItemEventArgs.CreateFromSmartEvent);
273             _doubleClicked = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "clicked,double", GenListItemEventArgs.CreateFromSmartEvent);
274             _expanded = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "expanded", GenListItemEventArgs.CreateFromSmartEvent);
275             _realized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "realized", GenListItemEventArgs.CreateFromSmartEvent);
276             _unrealized = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "unrealized", GenListItemEventArgs.CreateFromSmartEvent);
277             _longpressed = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "longpressed", GenListItemEventArgs.CreateFromSmartEvent);
278             _moved = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved", GenListItemEventArgs.CreateFromSmartEvent);
279             _movedAfter = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,after", GenListItemEventArgs.CreateFromSmartEvent);
280             _movedBefore = new SmartEvent<GenListItemEventArgs>(this, this.RealHandle, "moved,before", GenListItemEventArgs.CreateFromSmartEvent);
281             _scrollAnimationStarted = new SmartEvent(this, this.RealHandle, "scroll,anim,start");
282             _scrollAnimationStopped = new SmartEvent(this, this.RealHandle, "scroll,anim,stop");
283             _changed = new SmartEvent(this, this.RealHandle, "changed");
284
285             _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
286             _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
287             _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
288             _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
289             _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
290             _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
291             _expanded.On += (s, e) => { if (e.Item != null) ItemExpanded?.Invoke(this, e); };
292             _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
293             _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
294             _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
295             _moved.On += (s, e) => { if (e.Item != null) ItemMoved?.Invoke(this, e); };
296             _movedAfter.On += (s, e) => { if (e.Item != null) ItemMovedAfter?.Invoke(this, e); };
297             _movedBefore.On += (s, e) => { if (e.Item != null) ItemMovedBefore?.Invoke(this, e); };
298         }
299
300         void AddInternal(GenListItem item)
301         {
302             _children.Add(item);
303             item.Deleted += Item_Deleted;
304         }
305
306         void Item_Deleted(object sender, EventArgs e)
307         {
308             _children.Remove((GenListItem)sender);
309         }
310     }
311 }