2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
23 /// It inherits <see cref="Layout"/>.
24 /// The MultiButtonEntry is a widget letting an user enter text and each chunk of text managed as a set of buttons.
25 /// Each text button is inserted by pressing the "return" key. If there is no space in the current row, a new button is added to the next row.
26 /// When a text button is pressed, it will become focused. Backspace removes the focus. When the multi-button entry loses focus, items longer than one line are shrunk to one line.
27 /// The typical use case of multi-button entry is composing emails/messages to a group of addresses, each of which is an item that can be clicked for further actions.
29 public class MultiButtonEntry : Layout
31 HashSet<MultiButtonEntryItem> _children = new HashSet<MultiButtonEntryItem>();
32 List<Func<string, bool>> _filters = new List<Func<string, bool>>();
35 Interop.Elementary.MultiButtonEntryItemFilterCallback _filtercallback;
39 SmartEvent _contracted;
40 SmartEvent _expandedStateChanged;
41 SmartEvent<MultiButtonEntryItemEventArgs> _itemSelected;
42 SmartEvent<MultiButtonEntryItemEventArgs> _itemClicked;
43 SmartEvent<MultiButtonEntryItemEventArgs> _itemLongPressed;
44 SmartEvent<MultiButtonEntryItemEventArgs> _itemAdded;
47 /// Creates and initializes a new instance of the MultiButtonEntry class.
49 /// <param name="parent">The parent is a given container which will be attached by MultiButtonEntry as a child. It's <see cref="EvasObject"/> type.</param>
50 public MultiButtonEntry(EvasObject parent) : base(parent)
52 _clicked = new SmartEvent(this, "clicked");
53 _expanded = new SmartEvent(this, "expanded");
54 _contracted = new SmartEvent(this, "contracted");
55 _expandedStateChanged = new SmartEvent(this, "expand,state,changed");
57 _itemSelected = new SmartEvent<MultiButtonEntryItemEventArgs>(this, "item,selected", MultiButtonEntryItemEventArgs.CreateFromSmartEvent);
58 _itemClicked = new SmartEvent<MultiButtonEntryItemEventArgs>(this, "item,clicked", MultiButtonEntryItemEventArgs.CreateFromSmartEvent);
59 _itemLongPressed = new SmartEvent<MultiButtonEntryItemEventArgs>(this, "item,longpressed", MultiButtonEntryItemEventArgs.CreateFromSmartEvent);
60 _itemAdded = new SmartEvent<MultiButtonEntryItemEventArgs>(this, "item,added", MultiButtonEntryItemEventArgs.CreateAndAddFromSmartEvent);
62 _filtercallback = new Interop.Elementary.MultiButtonEntryItemFilterCallback(FilterCallbackHandler);
64 _clicked.On += (sender, e) => Clicked?.Invoke(this, EventArgs.Empty);
65 _expanded.On += (sender, e) => Expanded?.Invoke(this, EventArgs.Empty);
66 _contracted.On += (sender, e) => Contracted?.Invoke(this, EventArgs.Empty);
67 _expandedStateChanged.On += (sender, e) => ExpandedStateChanged?.Invoke(this, EventArgs.Empty);
69 _itemSelected.On += (sender, e) => { ItemSelected?.Invoke(this, e); };
70 _itemClicked.On += (sender, e) => { ItemClicked?.Invoke(this, e); };
71 _itemLongPressed.On += (sender, e) => { ItemLongPressed?.Invoke(this, e); };
72 _itemAdded.On += OnItemAdded;
76 /// Clicked is raised when a MultiButtonEntry is clicked.
78 public event EventHandler Clicked;
81 /// Expanded is raised when a MultiButtonEntry is expanded.
83 public event EventHandler Expanded;
86 /// Contracted is raised when a MultiButtonEntry is contracted.
88 public event EventHandler Contracted;
91 /// ExpandedStateChanged is raised when shrink mode state of MultiButtonEntry is changed.
93 public event EventHandler ExpandedStateChanged;
96 /// ItemSelected is raised when an item is selected by api, user interaction, and etc.
97 /// This is also raised when a user press back space while cursor is on the first field of entry.
99 public event EventHandler<MultiButtonEntryItemEventArgs> ItemSelected;
102 /// ItemClicked is raised when an item is clicked by user interaction.
104 public event EventHandler<MultiButtonEntryItemEventArgs> ItemClicked;
107 /// ItemLongPressed is raised when MultiButtonEntry item is pressed for a long time.
109 public event EventHandler<MultiButtonEntryItemEventArgs> ItemLongPressed;
112 /// ItemAdded is raised when a new MultiButtonEntry item is added.
114 public event EventHandler<MultiButtonEntryItemEventArgs> ItemAdded;
117 /// ItemDeleted is raised when a MultiButtonEntry item is deleted.
119 public event EventHandler<MultiButtonEntryItemEventArgs> ItemDeleted;
122 /// Gets the selected item in the multibuttonentry.
124 public MultiButtonEntryItem SelectedItem
128 IntPtr handle = Interop.Elementary.elm_multibuttonentry_selected_item_get(RealHandle);
129 return ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
134 /// Gets or sets whether the multibuttonentry is editable or not.
136 public bool IsEditable
140 return Interop.Elementary.elm_multibuttonentry_editable_get(RealHandle);
144 Interop.Elementary.elm_multibuttonentry_editable_set(RealHandle, value);
149 /// Gets or sets the multibuttonentry to expanded state.
150 /// If true, expanded state.
151 /// If false, single line state.
153 public bool IsExpanded
157 return Interop.Elementary.elm_multibuttonentry_expanded_get(RealHandle);
161 Interop.Elementary.elm_multibuttonentry_expanded_set(RealHandle, value);
166 /// Gets the first item in the multibuttonentry.
168 public MultiButtonEntryItem FirstItem
172 IntPtr handle = Interop.Elementary.elm_multibuttonentry_first_item_get(RealHandle);
173 return ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
178 /// Gets the last item in the multibuttonentry.
180 public MultiButtonEntryItem LastItem
184 IntPtr handle = Interop.Elementary.elm_multibuttonentry_last_item_get(RealHandle);
185 return ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
190 /// Gets the entry object int the multibuttonentry.
198 _entry = new EntryInner(this);
205 protected override IntPtr CreateHandle(EvasObject parent)
207 return Interop.Elementary.elm_multibuttonentry_add(parent.Handle);
211 /// Append a new item to the multibuttonentry.
213 /// <param name="label">The label of new item.</param>
214 /// <returns>A MultiButtonEntryItem to the item added.</returns>
215 public MultiButtonEntryItem Append(string label)
217 var handle = Interop.Elementary.elm_multibuttonentry_item_append(RealHandle, label, null, IntPtr.Zero);
218 MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
223 /// Prepend a new item to the multibuttonentry.
225 /// <param name="label">The label of new item.</param>
226 /// <returns>A MultiButtonEntryItem to the item added.</returns>
227 public MultiButtonEntryItem Prepend(string label)
229 var handle = Interop.Elementary.elm_multibuttonentry_item_prepend(RealHandle, label, null, IntPtr.Zero);
230 MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
235 /// Add a new item to the multibuttonentry before the indicated object reference.
237 /// <param name="before">The item before which to add it.</param>
238 /// <param name="label">The label of new item.</param>
239 /// <returns>A MultiButtonEntryItem to the item added.</returns>
240 public MultiButtonEntryItem InsertBefore(MultiButtonEntryItem before, string label)
242 var handle = Interop.Elementary.elm_multibuttonentry_item_insert_before(RealHandle, before.Handle, label, null, IntPtr.Zero);
243 MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
248 /// Add a new item to the multibuttonentry after the indicated object.
250 /// <param name="after">The item after which to add it.</param>
251 /// <param name="label">The label of new item.</param>
252 /// <returns>A MultiButtonEntryItem to the item added.</returns>
253 public MultiButtonEntryItem InsertAfter(MultiButtonEntryItem after, string label)
255 var handle = Interop.Elementary.elm_multibuttonentry_item_insert_after(RealHandle, after.Handle, label, null, IntPtr.Zero);
256 MultiButtonEntryItem item = ItemObject.GetItemByHandle(handle) as MultiButtonEntryItem;
261 /// Remove all items in the multibuttonentry.
265 Interop.Elementary.elm_multibuttonentry_clear(RealHandle);
270 /// Append an item filter function for text inserted in the Multibuttonentry.
272 /// <param name="func">The function to use as item filter.</param>
273 public void AppendFilter(Func<string, bool> func)
276 if (_filters.Count == 1)
278 Interop.Elementary.elm_multibuttonentry_item_filter_append(RealHandle, _filtercallback, IntPtr.Zero);
283 /// Prepend a filter function for text inserted in the Multibuttonentry.
285 /// <param name="func">The function to use as text filter.</param>
286 public void PrependFilter(Func<string, bool> func)
288 _filters.Insert(0, func);
289 if (_filters.Count == 1)
291 Interop.Elementary.elm_multibuttonentry_item_filter_prepend(RealHandle, _filtercallback, IntPtr.Zero);
296 /// Remove a filter from the list.
298 /// <param name="func">The filter function to remove.</param>
299 public void RemoveFilter(Func<string, bool> func)
301 _filters.Remove(func);
302 if (_filters.Count == 0)
304 Interop.Elementary.elm_multibuttonentry_item_filter_remove(RealHandle, _filtercallback, IntPtr.Zero);
308 void Item_Deleted(object sender, EventArgs e)
310 var removed = sender as MultiButtonEntryItem;
311 _children.Remove(removed);
313 // "item,deleted" event will be called after removing the item from ItemObject has been done.
314 // ItemObject will no longer have the item instance that is deleted after this.
315 // So, ItemDelete event with the removed item should be triggered here.
316 ItemDeleted?.Invoke(this, new MultiButtonEntryItemEventArgs() { Item = removed });
319 void OnItemAdded(object sender, MultiButtonEntryItemEventArgs e)
321 _children.Add(e.Item);
322 e.Item.Deleted += Item_Deleted;
323 ItemAdded?.Invoke(this, e);
326 bool FilterCallbackHandler(IntPtr obj, string label, IntPtr itemData, IntPtr data)
328 foreach (var func in _filters)
336 internal class EntryInner : Entry
338 internal EntryInner(EvasObject parent) : base(parent)
342 protected override IntPtr CreateHandle(EvasObject parent)
344 return Interop.Elementary.elm_multibuttonentry_entry_get(parent.Handle);
350 /// It inherits System.EventArgs.
351 /// The MultiButtonEntryItemEventArgs is a argument for all events of MultiButtonEntry.
352 /// It contains Item which is <see cref="MultiButtonEntryItem"/> type.
354 public class MultiButtonEntryItemEventArgs : EventArgs
357 /// Gets or sets MultiButtonEntryItem item. The return type is <see cref="MultiButtonEntryItem"/>.
359 public MultiButtonEntryItem Item { get; set; }
361 internal static MultiButtonEntryItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
363 MultiButtonEntryItem item = ItemObject.GetItemByHandle(info) as MultiButtonEntryItem;
364 return new MultiButtonEntryItemEventArgs() { Item = item };
367 internal static MultiButtonEntryItemEventArgs CreateAndAddFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
369 // Item can be added throught calling Append method and user input.
370 // And since "item.added" event will be called before xx_append() method returns,
371 // ItemObject does NOT have an item that contains handle matched to "info" at this time.
372 // So, item should be created and added internally here.
373 MultiButtonEntryItem item = new MultiButtonEntryItem(info);
374 return new MultiButtonEntryItemEventArgs() { Item = item };