Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Index.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     /// An index widget gives you an index for fast access to whichever group of other UI items one might have.
24     /// Inherits Layout
25     /// </summary>
26     public class Index : Layout
27     {
28         HashSet<IndexItem> _children = new HashSet<IndexItem>();
29         SmartEvent _delayedChanged;
30
31         /// <summary>
32         /// Creates and initializes a new instance of Index class.
33         /// </summary>
34         /// <param name="parent">The parent is a given container which will be attached by Index as a child. It's <see cref="EvasObject"/> type.</param>
35         public Index(EvasObject parent) : base(parent)
36         {
37             _delayedChanged = new SmartEvent(this, this.RealHandle, "delay,changed");
38             _delayedChanged.On += _delayedChanged_On;
39         }
40
41         /// <summary>
42         /// Changed will be triggered when the selected index item is changed.
43         /// </summary>
44         public event EventHandler Changed;
45
46         /// <summary>
47         /// Sets or gets the auto hiding feature is enabled or not for a given index widget.
48         /// </summary>
49         public bool AutoHide
50         {
51             get
52             {
53                 return !Interop.Elementary.elm_index_autohide_disabled_get(RealHandle);
54             }
55             set
56             {
57                 Interop.Elementary.elm_index_autohide_disabled_set(RealHandle, !value);
58             }
59         }
60
61         /// <summary>
62         /// Sets or gets a value whether horizontal mode is enabled or not.
63         /// </summary>
64         public bool IsHorizontal
65         {
66             get
67             {
68                 return Interop.Elementary.elm_index_horizontal_get(RealHandle);
69             }
70             set
71             {
72                 Interop.Elementary.elm_index_horizontal_set(RealHandle, value);
73             }
74         }
75
76         /// <summary>
77         /// Sets or gets the value of indicator's disabled status.
78         /// </summary>
79         public bool IndicatorVisible
80         {
81             get
82             {
83                 return !Interop.Elementary.elm_index_indicator_disabled_get(RealHandle);
84             }
85             set
86             {
87                 Interop.Elementary.elm_index_indicator_disabled_set(RealHandle, !value);
88             }
89         }
90
91         /// <summary>
92         /// Sets or gets the omit feature is enabled or not for a given index widget.
93         /// </summary>
94         public bool OmitEnabled
95         {
96             get
97             {
98                 return Interop.Elementary.elm_index_omit_enabled_get(RealHandle);
99             }
100             set
101             {
102                 Interop.Elementary.elm_index_omit_enabled_set(RealHandle, value);
103             }
104         }
105
106         /// <summary>
107         /// Set a delay change time for index object.
108         /// delay time is 0.2 sec by default.
109         /// </summary>
110         public double Delay
111         {
112             get
113             {
114                 return Interop.Elementary.elm_index_delay_change_time_get(RealHandle);
115             }
116             set
117             {
118                 Interop.Elementary.elm_index_delay_change_time_set(RealHandle, value);
119             }
120         }
121
122         /// <summary>
123         /// Gets or sets the items level for a given index widget.
124         /// </summary>
125         public int Level
126         {
127             get
128             {
129                 return Interop.Elementary.elm_index_item_level_get(RealHandle);
130             }
131             set
132             {
133                 Interop.Elementary.elm_index_item_level_set(RealHandle, value);
134             }
135         }
136
137         /// <summary>
138         /// Control standard_priority group of index.
139         /// Priority group will be shown as many items as it can, and other group will be shown one character only.
140         /// </summary>
141         public int Priority
142         {
143             get
144             {
145                 return Interop.Elementary.elm_index_standard_priority_get(RealHandle);
146             }
147             set
148             {
149                 Interop.Elementary.elm_index_standard_priority_set(RealHandle, value);
150             }
151         }
152
153         /// <summary>
154         /// Gets the last selected item, for a given index widget.
155         /// </summary>
156         public IndexItem SelectedItem
157         {
158             get
159             {
160                 IntPtr handle = Interop.Elementary.elm_index_selected_item_get(RealHandle, 0);
161                 return ItemObject.GetItemByHandle(handle) as IndexItem;
162             }
163         }
164
165         /// <summary>
166         /// Append a new item on a given index widget.
167         /// </summary>
168         /// <param name="label">the label which the item should be indexed</param>
169         /// <returns>A object to the IndexItem added or null, on errors</returns>
170         public IndexItem Append(string label)
171         {
172             IndexItem item = new IndexItem(label);
173             item.Handle = Interop.Elementary.elm_index_item_append(RealHandle, label, null, (IntPtr)item.Id);
174             return item;
175         }
176
177         /// <summary>
178         /// Prepend a new item on a given index widget.
179         /// </summary>
180         /// <param name="label">the label which the item should be indexed</param>
181         /// <returns>A handle to the item added or NULL, on errors</returns>
182         public IndexItem Prepend(string label)
183         {
184             IndexItem item = new IndexItem(label);
185             item.Handle = Interop.Elementary.elm_index_item_prepend(RealHandle, label, null, (IntPtr)item.Id);
186             return item;
187         }
188
189         /// <summary>
190         /// Insert a new item into the index object before item before.
191         /// </summary>
192         /// <param name="label">the label which the item should be indexed</param>
193         /// <param name="before">The index item to insert after.</param>
194         /// <returns>A object to the IndexItem added or null, on errors</returns>
195         public IndexItem InsertBefore(string label, IndexItem before)
196         {
197             IndexItem item = new IndexItem(label);
198             item.Handle = Interop.Elementary.elm_index_item_insert_before(RealHandle, before, label, null, (IntPtr)item.Id);
199             return item;
200         }
201
202         /// <summary>
203         /// Insert a new item into the index object after item after.
204         /// </summary>
205         /// <param name="label">the label which the item should be indexed</param>
206         /// <param name="after">The index item to insert after.</param>
207         /// <returns>A object to the IndexItem added or null, on errors</returns>
208         public IndexItem InsertAfter(string label, IndexItem after)
209         {
210             IndexItem item = new IndexItem(label);
211             item.Handle = Interop.Elementary.elm_index_item_insert_after(RealHandle, after, label, null, (IntPtr)item.Id);
212             return item;
213         }
214
215         /// <summary>
216         /// Flush the changes made to the index items so they work correctly.
217         /// </summary>
218         /// <param name="level">The index level (one of 0 or 1) where changes were made</param>
219         public void Update(int level)
220         {
221             Interop.Elementary.elm_index_level_go(RealHandle, level);
222         }
223
224         /// <summary>
225         /// Removes all items from a given index widget.
226         /// </summary>
227         public void Clear()
228         {
229             Interop.Elementary.elm_index_item_clear(RealHandle);
230         }
231
232         protected override IntPtr CreateHandle(EvasObject parent)
233         {
234             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
235             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
236
237             RealHandle = Interop.Elementary.elm_index_add(handle);
238             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
239
240             return handle;
241         }
242
243         void _delayedChanged_On(object sender, EventArgs e)
244         {
245             SelectedItem?.SendSelected();
246             Changed?.Invoke(this, e);
247         }
248
249         void AddInternal(IndexItem item)
250         {
251             _children.Add(item);
252             item.Deleted += Item_Deleted;
253         }
254
255         void Item_Deleted(object sender, EventArgs e)
256         {
257             _children.Remove((IndexItem)sender);
258         }
259     }
260 }