4c8eb08d204b0413ad0954c06c174d36e1f10d2a
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Toolbar.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.ComponentModel;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// Enumeration for the selection mode of the toolbar.
24     /// </summary>
25     /// <since_tizen> preview </since_tizen>
26     public enum ToolbarSelectionMode
27     {
28         /// <summary>
29         /// Default select mode.
30         /// </summary>
31         Default = 0,
32
33         /// <summary>
34         /// Always select mode.
35         /// </summary>
36         Always,
37
38         /// <summary>
39         /// No select mode.
40         /// </summary>
41         None,
42
43         /// <summary>
44         /// No select mode with no finger size rule.
45         /// </summary>
46         DisplayOnly,
47     }
48
49     /// <summary>
50     /// Enumeration for setting the toolbar items display behavior, it can be scrollable, can show a menu with exceeding items, or simply hide them.
51     /// </summary>
52     /// <since_tizen> preview </since_tizen>
53     public enum ToolbarShrinkMode
54     {
55         /// <summary>
56         /// Sets the minimum toolbar size to fit all the items.
57         /// </summary>
58         None = 0,
59
60         /// <summary>
61         /// Hides exceeding items.
62         /// </summary>
63         Hide,
64
65         /// <summary>
66         /// Allows accessing exceeding items through a scroller.
67         /// </summary>
68         Scroll,
69
70         /// <summary>
71         /// Inserts a button to popup a menu with exceeding items.
72         /// </summary>
73         Menu,
74
75         /// <summary>
76         /// Expands all items according to the size of the toolbar.
77         /// </summary>
78         Expand
79     }
80
81     /// <summary>
82     /// Enumeration for the icon lookup order of the toolbar.
83     /// </summary>
84     /// <since_tizen> preview </since_tizen>
85     public enum ToolbarIconLookupOrder
86     {
87         /// <summary>
88         /// Icon lookup order: freedesktop, theme.
89         /// </summary>
90         FreedesktopTheme,
91
92         /// <summary>
93         /// Icon lookup order: theme, freedesktop.
94         /// </summary>
95         ThemeFreedesktop,
96
97         /// <summary>
98         /// Icon lookup order: freedesktop.
99         /// </summary>
100         Freedesktop,
101
102         /// <summary>
103         /// Icon lookup order: theme.
104         /// </summary>
105         Theme,
106     }
107
108     /// <summary>
109     /// Event arguments for events of <see cref="ToolbarItem"/>.
110     /// </summary>
111     /// <remarks>
112     /// Inherits EventArgs.
113     /// </remarks>
114     /// <since_tizen> preview </since_tizen>
115     public class ToolbarItemEventArgs : EventArgs
116     {
117         /// <summary>
118         /// Gets the ToolbarItem.
119         /// </summary>
120         /// <since_tizen> preview </since_tizen>
121         public ToolbarItem Item { get; private set; }
122
123         internal static ToolbarItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
124         {
125             ToolbarItem item = ItemObject.GetItemByHandle(info) as ToolbarItem;
126             return new ToolbarItemEventArgs { Item = item };
127         }
128     }
129
130     /// <summary>
131     /// The Toolbar is a widget that displays a list of items inside a box.
132     /// </summary>
133     /// <since_tizen> preview </since_tizen>
134     public class Toolbar : Widget
135     {
136         SmartEvent<ToolbarItemEventArgs> _clicked;
137         SmartEvent<ToolbarItemEventArgs> _selected;
138         SmartEvent<ToolbarItemEventArgs> _longpressed;
139
140         /// <summary>
141         /// Creates and initializes a new instance of the Toolbar class.
142         /// </summary>
143         /// <param name="parent">
144         /// A EvasObject to which the new Table instance will be attached.
145         /// </param>
146         /// <since_tizen> preview </since_tizen>
147         public Toolbar(EvasObject parent) : base(parent)
148         {
149             _selected = new SmartEvent<ToolbarItemEventArgs>(this, this.RealHandle, "selected", ToolbarItemEventArgs.CreateFromSmartEvent);
150             _selected.On += (s, e) =>
151             {
152                 if (e.Item != null)
153                 {
154                     Selected?.Invoke(this, e);
155                     e.Item.SendSelected();
156                 }
157             };
158             _longpressed = new SmartEvent<ToolbarItemEventArgs>(this, this.RealHandle, "longpressed", ToolbarItemEventArgs.CreateFromSmartEvent);
159             _longpressed.On += (s, e) =>
160             {
161                 e.Item?.SendLongPressed();
162             };
163             _clicked = new SmartEvent<ToolbarItemEventArgs>(this, this.RealHandle, "clicked", ToolbarItemEventArgs.CreateFromSmartEvent);
164             _clicked.On += (s, e) =>
165             {
166                 e.Item?.SendClicked();
167             };
168         }
169
170         /// <summary>
171         /// Selected will be triggered when toolbar has been selected.
172         /// </summary>
173         /// <since_tizen> preview </since_tizen>
174         public event EventHandler<ToolbarItemEventArgs> Selected;
175
176         /// <summary>
177         /// Sets or gets whether the layout of this toolbar is homogeneous.
178         /// </summary>
179         /// <remarks>True for homogeneous, False for no homogeneous.</remarks>
180         /// <since_tizen> preview </since_tizen>
181         public bool Homogeneous
182         {
183             get
184             {
185                 return Interop.Elementary.elm_toolbar_homogeneous_get(RealHandle);
186             }
187             set
188             {
189                 Interop.Elementary.elm_toolbar_homogeneous_set(RealHandle, value);
190             }
191         }
192
193         /// <summary>
194         /// Sets or gets the slection mode of a given Toolbar widget.
195         /// </summary>
196         /// <since_tizen> preview </since_tizen>
197         public ToolbarSelectionMode SelectionMode
198         {
199             get
200             {
201                 return (ToolbarSelectionMode)Interop.Elementary.elm_toolbar_select_mode_get(RealHandle);
202             }
203             set
204             {
205                 Interop.Elementary.elm_toolbar_select_mode_set(RealHandle, (int)value);
206             }
207         }
208
209         /// <summary>
210         /// Sets or gets the shrink mode of a given Toolbar widget.
211         /// </summary>
212         /// <since_tizen> preview </since_tizen>
213         public ToolbarShrinkMode ShrinkMode
214         {
215             get
216             {
217                 return (ToolbarShrinkMode)Interop.Elementary.elm_toolbar_shrink_mode_get(RealHandle);
218             }
219             set
220             {
221                 Interop.Elementary.elm_toolbar_shrink_mode_set(RealHandle, (int)value);
222             }
223         }
224
225         /// <summary>
226         /// Sets or gets the toolbar's current orientation.
227         /// </summary>
228         /// <since_tizen> preview </since_tizen>
229         [EditorBrowsable(EditorBrowsableState.Never)]
230         public bool IsHorizontal
231         {
232             get
233             {
234                 return Interop.Elementary.elm_toolbar_horizontal_get(RealHandle);
235             }
236             set
237             {
238                 Interop.Elementary.elm_toolbar_horizontal_set(RealHandle, value);
239             }
240         }
241
242         /// <summary>
243         /// Sets or gets the icon lookup order, for toolbar items' icons.
244         /// The default lookup order is ToolbarIocnLookupOrder.ThemeFreedesktop.
245         /// Icons added before calling this function will not be affected.
246         /// </summary>
247         /// <since_tizen> preview </since_tizen>
248         public ToolbarIconLookupOrder IconLookupOrder
249         {
250             get
251             {
252                 return (ToolbarIconLookupOrder)Interop.Elementary.elm_toolbar_icon_order_lookup_get(RealHandle);
253             }
254             set
255             {
256                 Interop.Elementary.elm_toolbar_icon_order_lookup_set(RealHandle, (int)value);
257             }
258         }
259
260         /// <summary>
261         /// Sets or gets the icon size of a given toolbar widget.
262         /// Default value is 32 pixels, to be used by toolbar items.
263         /// </summary>
264         /// <since_tizen> preview </since_tizen>
265         public int IconSize
266         {
267             get
268             {
269                 return Interop.Elementary.elm_toolbar_icon_size_get(RealHandle);
270             }
271             set
272             {
273                 Interop.Elementary.elm_toolbar_icon_size_set(RealHandle, value);
274             }
275         }
276
277         /// <summary>
278         /// Gets the number of items in a Toolbar widget.
279         /// </summary>
280         /// <since_tizen> preview </since_tizen>
281         public int ItemsCount
282         {
283             get
284             {
285                 return Interop.Elementary.elm_toolbar_items_count(RealHandle);
286             }
287         }
288
289         /// <summary>
290         /// Sets or gets the alignment of the items.
291         /// </summary>
292         /// <remarks>The toolbar items alignment, a float between 0.0 and 1.0.</remarks>
293         /// <since_tizen> preview </since_tizen>
294         public double ItemAlignment
295         {
296             get
297             {
298                 return Interop.Elementary.elm_toolbar_align_get(RealHandle);
299             }
300             set
301             {
302                 Interop.Elementary.elm_toolbar_align_set(RealHandle, value);
303             }
304         }
305
306         /// <summary>
307         /// Sets or gets the item's transverse expansion of a given Toolbar widget.
308         /// </summary>
309         /// <remarks>
310         /// The transverse expansion of the item, true for on and false for off.
311         /// By default it's false.
312         /// </remarks>
313         /// <since_tizen> preview </since_tizen>
314         public bool TransverseExpansion
315         {
316             get
317             {
318                 return Interop.Elementary.elm_toolbar_transverse_expanded_get(RealHandle);
319             }
320             set
321             {
322                 Interop.Elementary.elm_toolbar_transverse_expanded_set(RealHandle, value);
323             }
324         }
325
326         /// <summary>
327         /// Appends the ToolbarItem, which just contains label to the toolbar.
328         /// </summary>
329         /// <param name="label">The label of the item.</param>
330         /// <returns>The new ToolbarItem which is appended to the toolbar.</returns>
331         /// <seealso cref="Append(string, string)"/>
332         /// <seealso cref="Prepend(string)"/>
333         /// <since_tizen> preview </since_tizen>
334         public ToolbarItem Append(string label)
335         {
336             return Append(label, null);
337         }
338
339         /// <summary>
340         /// Appends the ToolbarItem, which contains label and icon to the toolbar.
341         /// </summary>
342         /// <param name="label">The label of the item.</param>
343         /// <param name="icon">A string with the icon name or the absolute path of an image file.</param>
344         /// <returns>The new ToolbarItem which is appended to the toolbar.</returns>
345         /// <seealso cref="Append(string)"/>
346         /// <seealso cref="Prepend(string)"/>
347         /// <seealso cref="Prepend(string, string)"/>
348         /// <since_tizen> preview </since_tizen>
349         public ToolbarItem Append(string label, string icon)
350         {
351             ToolbarItem item = new ToolbarItem(label, icon);
352             item.Handle = Interop.Elementary.elm_toolbar_item_append(RealHandle, icon, label, null, (IntPtr)item.Id);
353             return item;
354         }
355
356         /// <summary>
357         /// Prepends the ToolbarItem, which just contains label to the toolbar.
358         /// </summary>
359         /// <param name="label">The label of the item.</param>
360         /// <returns>The new ToolbarItem which is prepended to the toolbar.</returns>
361         /// <seealso cref="Append(string)"/>
362         /// <seealso cref="Append(string, string)"/>
363         /// <seealso cref="Prepend(string, string)"/>
364         /// <since_tizen> preview </since_tizen>
365         public ToolbarItem Prepend(string label)
366         {
367             return Prepend(label, null);
368         }
369
370         /// <summary>
371         /// Prepends the ToolbarItem, which contains label and icon to the toolbar.
372         /// </summary>
373         /// <param name="label">The label of the item.</param>
374         /// <param name="icon">A string with the icon name or the absolute path of an image file.</param>
375         /// <returns>The new <see cref="ToolbarItem"/> which is prepended to the toolbar.</returns>
376         /// <seealso cref="Append(string)"/>
377         /// <seealso cref="Append(string, string)"/>
378         /// <seealso cref="Prepend(string)"/>
379         /// <since_tizen> preview </since_tizen>
380         public ToolbarItem Prepend(string label, string icon)
381         {
382             ToolbarItem item = new ToolbarItem(label, icon);
383             item.Handle = Interop.Elementary.elm_toolbar_item_prepend(RealHandle, icon, label, null, (IntPtr)item.Id);
384             return item;
385         }
386
387         /// <summary>
388         /// Inserts a new item which just contains label into the toolbar object before item <paramref name="before"/>.
389         /// </summary>
390         /// <param name="before">The toolbar item to insert before.</param>
391         /// <param name="label">The label of the item.</param>
392         /// <returns>The new <see cref="ToolbarItem"/> which is inserted into the toolbar.</returns>
393         /// <seealso cref="InsertBefore(ToolbarItem, string, string)"/>
394         /// <since_tizen> preview </since_tizen>
395         public ToolbarItem InsertBefore(ToolbarItem before, string label)
396         {
397             return InsertBefore(before, label, string.Empty);
398         }
399
400         /// <summary>
401         /// Inserts a new item which contains label and icon into the toolbar object before item <paramref name="before"/>.
402         /// </summary>
403         /// <param name="before">The toolbar item to insert before.</param>
404         /// <param name="label">The label of the item.</param>
405         /// <param name="icon">A string with the icon name or the absolute path of an image file.</param>
406         /// <returns>The new <see cref="ToolbarItem"/> which is inserted into the toolbar.</returns>
407         /// <seealso cref="InsertBefore(ToolbarItem, string)"/>
408         /// <since_tizen> preview </since_tizen>
409         public ToolbarItem InsertBefore(ToolbarItem before, string label, string icon)
410         {
411             ToolbarItem item = new ToolbarItem(label, icon);
412             item.Handle = Interop.Elementary.elm_toolbar_item_insert_before(RealHandle, before, icon, label, null, (IntPtr)item.Id);
413             return item;
414         }
415
416         /// <summary>
417         /// Inserts a new item which contains label and icon into the toolbar object after item <paramref name="after"/>.
418         /// </summary>
419         /// <param name="after">The toolbar item to insert after.</param>
420         /// <param name="label">The label of the item.</param>
421         /// <param name="icon">A string with the icon name or the absolute path of an image file.</param>
422         /// <returns>The new <see cref="ToolbarItem"/> which is inserted into the toolbar.</returns>
423         /// <since_tizen> preview </since_tizen>
424         public ToolbarItem InsertAfter(ToolbarItem after, string label, string icon)
425         {
426             ToolbarItem item = new ToolbarItem(label, icon);
427             item.Handle = Interop.Elementary.elm_toolbar_item_insert_after(RealHandle, after, icon, label, null, (IntPtr)item.Id);
428             return item;
429         }
430
431         /// <summary>
432         /// Finds the item with that label in the toolbar.
433         /// </summary>
434         /// <param name="label">The label of the item.</param>
435         /// <returns>The <see cref="ToolbarItem"/> into the toolbar.</returns>
436         /// <since_tizen> preview </since_tizen>
437         public ToolbarItem FindItemByLabel(string label)
438         {
439             IntPtr handle = Interop.Elementary.elm_toolbar_item_find_by_label(RealHandle, label);
440             return ItemObject.GetItemByHandle(handle) as ToolbarItem;
441         }
442
443         /// <summary>
444         /// Gets the selected ToolbarItemItem of the toolbar.
445         /// </summary>
446         /// <since_tizen> preview </since_tizen>
447         public ToolbarItem SelectedItem
448         {
449             get
450             {
451                 IntPtr handle = Interop.Elementary.elm_toolbar_selected_item_get(RealHandle);
452                 return ItemObject.GetItemByHandle(handle) as ToolbarItem;
453             }
454         }
455
456         /// <summary>
457         /// Gets the first ToolbarItemItem of the toolbar.
458         /// </summary>
459         /// <since_tizen> preview </since_tizen>
460         public ToolbarItem FirstItem
461         {
462             get
463             {
464                 IntPtr handle = Interop.Elementary.elm_toolbar_first_item_get(RealHandle);
465                 return ItemObject.GetItemByHandle(handle) as ToolbarItem;
466             }
467         }
468
469         /// <summary>
470         /// Gets the last ToolbarItemItem of the toolbar.
471         /// </summary>
472         /// <since_tizen> preview </since_tizen>
473         public ToolbarItem LastItem
474         {
475             get
476             {
477                 IntPtr handle = Interop.Elementary.elm_toolbar_last_item_get(RealHandle);
478                 return ItemObject.GetItemByHandle(handle) as ToolbarItem;
479             }
480         }
481
482         /// <summary>
483         /// Creates a widget handle.
484         /// </summary>
485         /// <param name="parent">Parent EvasObject.</param>
486         /// <returns>Handle IntPtr.</returns>
487         /// <since_tizen> preview </since_tizen>
488         protected override IntPtr CreateHandle(EvasObject parent)
489         {
490             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
491             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
492
493             RealHandle = Interop.Elementary.elm_toolbar_add(handle);
494             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
495
496             return handle;
497         }
498     }
499 }