63ebd937c9b9a0e1179e8fb8bb6dc6951f7d2e3a
[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
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// Enumeration for the selection mode of Toolbar.
23     /// </summary>
24     public enum ToolbarSelectionMode
25     {
26         /// <summary>
27         /// Default select mode.
28         /// </summary>
29         Default = 0,
30
31         /// <summary>
32         /// Always select mode.
33         /// </summary>
34         Always,
35
36         /// <summary>
37         /// No select mode.
38         /// </summary>
39         None,
40
41         /// <summary>
42         /// No select mode with no finger size rule.
43         /// </summary>
44         DisplayOnly,
45     }
46
47     /// <summary>
48     /// Enumeration that sets the toolbar items display behavior, it can be scrollable, can show a menu with exceeding items, or simply hide them.
49     /// </summary>
50     public enum ToolbarShrinkMode
51     {
52         /// <summary>
53         /// Sets minimum toolbar size to fit all the items.
54         /// </summary>
55         None = 0,
56
57         /// <summary>
58         /// Hides exceeding items.
59         /// </summary>
60         Hide,
61
62         /// <summary>
63         /// Allows accessing exceeding items through a scroller.
64         /// </summary>
65         Scroll,
66
67         /// <summary>
68         /// Inserts a button to pop up a menu with exceeding items.
69         /// </summary>
70         Menu,
71
72         /// <summary>
73         /// Expands all items according to the size of the toolbar.
74         /// </summary>
75         Expand
76     }
77
78     /// <summary>
79     /// Event arguments for events of <see cref="ToolbarItem"/>.
80     /// </summary>
81     /// <remarks>
82     /// Inherits EventArgs.
83     /// </remarks>
84     public class ToolbarItemEventArgs : EventArgs
85     {
86         /// <summary>
87         /// Gets the ToolbarItem.
88         /// </summary>
89         public ToolbarItem Item { get; private set; }
90
91         internal static ToolbarItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
92         {
93             ToolbarItem item = ItemObject.GetItemByHandle(info) as ToolbarItem;
94             return new ToolbarItemEventArgs() { Item = item };
95         }
96     }
97
98     /// <summary>
99     /// The Toolbar is a widget that displays a list of items inside a box.
100     /// </summary>
101     public class Toolbar : Widget
102     {
103         SmartEvent<ToolbarItemEventArgs> _clicked;
104         SmartEvent<ToolbarItemEventArgs> _selected;
105         SmartEvent<ToolbarItemEventArgs> _longpressed;
106
107         /// <summary>
108         /// Creates and initializes a new instance of the Toolbar class.
109         /// </summary>
110         /// <param name="parent">
111         /// A EvasObject to which the new Table instance will be attached.
112         /// </param>
113         public Toolbar(EvasObject parent) : base(parent)
114         {
115             _selected = new SmartEvent<ToolbarItemEventArgs>(this, this.RealHandle, "selected", ToolbarItemEventArgs.CreateFromSmartEvent);
116             _selected.On += (s, e) =>
117             {
118                 if (e.Item != null)
119                 {
120                     Selected?.Invoke(this, e);
121                     e.Item.SendSelected();
122                 }
123             };
124             _longpressed = new SmartEvent<ToolbarItemEventArgs>(this, this.RealHandle, "longpressed", ToolbarItemEventArgs.CreateFromSmartEvent);
125             _longpressed.On += (s, e) =>
126             {
127                 e.Item?.SendLongPressed();
128             };
129             _clicked = new SmartEvent<ToolbarItemEventArgs>(this, this.RealHandle, "clicked", ToolbarItemEventArgs.CreateFromSmartEvent);
130             _clicked.On += (s, e) =>
131             {
132                 e.Item?.SendClicked();
133             };
134         }
135
136         /// <summary>
137         /// Selected will be triggered when toolbar have been selected.
138         /// </summary>
139         public event EventHandler<ToolbarItemEventArgs> Selected;
140
141         /// <summary>
142         /// Sets or gets whether the layout of this toolbar is homogeneous.
143         /// </summary>
144         /// <remarks>True for homogeneous, False for no homogeneous</remarks>
145         public bool Homogeneous
146         {
147             get
148             {
149                 return Interop.Elementary.elm_toolbar_homogeneous_get(RealHandle);
150             }
151             set
152             {
153                 Interop.Elementary.elm_toolbar_homogeneous_set(RealHandle, value);
154             }
155         }
156
157         /// <summary>
158         /// Sets or gets the slection mode of a given Toolbar widget.
159         /// </summary>
160         public ToolbarSelectionMode SelectionMode
161         {
162             get
163             {
164                 return (ToolbarSelectionMode)Interop.Elementary.elm_toolbar_select_mode_get(RealHandle);
165             }
166             set
167             {
168                 Interop.Elementary.elm_toolbar_select_mode_set(RealHandle, (int)value);
169             }
170         }
171
172         /// <summary>
173         /// Sets or gets the shrink mode of a given Toolbar widget.
174         /// </summary>
175         public ToolbarShrinkMode ShrinkMode
176         {
177             get
178             {
179                 return (ToolbarShrinkMode)Interop.Elementary.elm_toolbar_shrink_mode_get(RealHandle);
180             }
181             set
182             {
183                 Interop.Elementary.elm_toolbar_shrink_mode_set(RealHandle, (int)value);
184             }
185         }
186
187         /// <summary>
188         /// Sets or gets the alignment of the items.
189         /// </summary>
190         /// <remarks>The toolbar items alignment, a float between 0.0 and 1.0</remarks>
191         public double ItemAlignment
192         {
193             get
194             {
195                 return Interop.Elementary.elm_toolbar_align_get(RealHandle);
196             }
197             set
198             {
199                 Interop.Elementary.elm_toolbar_align_set(RealHandle, value);
200             }
201         }
202
203         /// <summary>
204         /// Sets or gets the item's transverse expansion of a given toolbar widget.
205         /// </summary>
206         /// <remarks>
207         /// The transverse expansion of the item, true for on and false for off.
208         /// By default it's false.
209         /// </remarks>
210         public bool TransverseExpansion
211         {
212             get
213             {
214                 return Interop.Elementary.elm_toolbar_transverse_expanded_get(RealHandle);
215             }
216             set
217             {
218                 Interop.Elementary.elm_toolbar_transverse_expanded_set(RealHandle, value);
219             }
220         }
221
222         /// <summary>
223         /// Appends ToolbarItem which just contains label to the toolbar.
224         /// </summary>
225         /// <param name="label">The label of the item</param>
226         /// <returns>The new ToolbarItem which appended to the toolbar</returns>
227         /// <seealso cref="Append(string, string)"/>
228         /// <seealso cref="Prepend(string)"/>
229         public ToolbarItem Append(string label)
230         {
231             return Append(label, null);
232         }
233
234         /// <summary>
235         /// Appends ToolbarItem which contains label and icon to the toolbar.
236         /// </summary>
237         /// <param name="label">The label of the item</param>
238         /// <param name="icon">A string with the icon name or the absolute path of an image file</param>
239         /// <returns>The new ToolbarItem which appended to the toolbar</returns>
240         /// <seealso cref="Append(string)"/>
241         /// <seealso cref="Prepend(string)"/>
242         /// <seealso cref="Prepend(string, string)"/>
243         public ToolbarItem Append(string label, string icon)
244         {
245             ToolbarItem item = new ToolbarItem(label, icon);
246             item.Handle = Interop.Elementary.elm_toolbar_item_append(RealHandle, icon, label, null, (IntPtr)item.Id);
247             return item;
248         }
249
250         /// <summary>
251         /// Prepends ToolbarItem which just contains label to the toolbar.
252         /// </summary>
253         /// <param name="label">The label of the item</param>
254         /// <returns>The new ToolbarItem which prepended to the toolbar</returns>
255         /// <seealso cref="Append(string)"/>
256         /// <seealso cref="Append(string, string)"/>
257         /// <seealso cref="Prepend(string, string)"/>
258         public ToolbarItem Prepend(string label)
259         {
260             return Prepend(label, null);
261         }
262
263         /// <summary>
264         /// Prepends ToolbarItem which contains label and icon to the toolbar.
265         /// </summary>
266         /// <param name="label">The label of the item</param>
267         /// <param name="icon">A string with the icon name or the absolute path of an image file</param>
268         /// <returns>The new <see cref="ToolbarItem"/> which prepended to the toolbar</returns>
269         /// <seealso cref="Append(string)"/>
270         /// <seealso cref="Append(string, string)"/>
271         /// <seealso cref="Prepend(string)"/>
272         public ToolbarItem Prepend(string label, string icon)
273         {
274             ToolbarItem item = new ToolbarItem(label, icon);
275             item.Handle = Interop.Elementary.elm_toolbar_item_prepend(RealHandle, icon, label, null, (IntPtr)item.Id);
276             return item;
277         }
278
279         /// <summary>
280         /// Inserts a new item which just contains label into the toolbar object before item <paramref name="before"/>.
281         /// </summary>
282         /// <param name="before">The toolbar item to insert before</param>
283         /// <param name="label">The label of the item</param>
284         /// <returns>The new <see cref="ToolbarItem"/> which insert into the toolbar</returns>
285         /// <seealso cref="InsertBefore(ToolbarItem, string, string)"/>
286         public ToolbarItem InsertBefore(ToolbarItem before, string label)
287         {
288             return InsertBefore(before, label);
289         }
290
291         /// <summary>
292         /// Inserts a new item which contains label and icon into the toolbar object before item <paramref name="before"/>.
293         /// </summary>
294         /// <param name="before">The toolbar item to insert before</param>
295         /// <param name="label">The label of the item</param>
296         /// <param name="icon">A string with the icon name or the absolute path of an image file</param>
297         /// <returns>The new <see cref="ToolbarItem"/> which insert into the toolbar</returns>
298         /// <seealso cref="InsertBefore(ToolbarItem, string)"/>
299         public ToolbarItem InsertBefore(ToolbarItem before, string label, string icon)
300         {
301             ToolbarItem item = new ToolbarItem(label, icon);
302             item.Handle = Interop.Elementary.elm_toolbar_item_insert_before(RealHandle, before, icon, label, null, (IntPtr)item.Id);
303             return item;
304         }
305
306         /// <summary>
307         /// Gets the selected ToolbarItemItem of the toolbar.
308         /// </summary>
309         public ToolbarItem SelectedItem
310         {
311             get
312             {
313                 IntPtr handle = Interop.Elementary.elm_toolbar_selected_item_get(RealHandle);
314                 return ItemObject.GetItemByHandle(handle) as ToolbarItem;
315             }
316         }
317
318         protected override IntPtr CreateHandle(EvasObject parent)
319         {
320             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
321             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
322
323             RealHandle = Interop.Elementary.elm_toolbar_add(handle);
324             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
325
326             return handle;
327         }
328     }
329 }