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