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 System.EventArgs.
24 /// It contains Item which is <see cref="GenGridItem"/> type.
25 /// All events of GenGrid contain GenGridItemEventArgs as a parameter.
27 public class GenGridItemEventArgs : EventArgs
30 /// Gets or sets GenGrid item.The return type is <see cref="GenGridItem"/>.
32 public GenGridItem Item { get; set; }
34 internal static GenGridItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
36 GenGridItem item = ItemObject.GetItemByHandle(info) as GenGridItem;
37 return new GenGridItemEventArgs() { Item = item };
42 /// It inherits <see cref="Layout"/>.
43 /// The GenGrid is a widget that aims to position objects in a grid layout while actually creating and rendering only the visible ones.
44 /// It has two direction in which a given GenGrid widget expands while placing its items, horizontal and vertical.
45 /// The GenGrid items are represented through <see cref="GenItemClass"/> definition field details.
47 public class GenGrid : Layout
49 HashSet<GenGridItem> _children = new HashSet<GenGridItem>();
51 SmartEvent<GenGridItemEventArgs> _selected;
52 SmartEvent<GenGridItemEventArgs> _unselected;
53 SmartEvent<GenGridItemEventArgs> _activated;
54 SmartEvent<GenGridItemEventArgs> _pressed;
55 SmartEvent<GenGridItemEventArgs> _released;
56 SmartEvent<GenGridItemEventArgs> _doubleClicked;
57 SmartEvent<GenGridItemEventArgs> _realized;
58 SmartEvent<GenGridItemEventArgs> _unrealized;
59 SmartEvent<GenGridItemEventArgs> _longpressed;
63 /// Creates and initializes a new instance of the GenGrid class.
65 /// <param name="parent">The parent is a given container which will be attached by GenGrid as a child. It's <see cref="EvasObject"/> type.</param>
66 public GenGrid(EvasObject parent) : base(parent)
68 InitializeSmartEvent();
72 /// ItemSelected is raised when a new gengrid item is selected.
74 public event EventHandler<GenGridItemEventArgs> ItemSelected;
77 /// ItemUnselected is raised when the gengrid item is Unselected.
79 public event EventHandler<GenGridItemEventArgs> ItemUnselected;
82 /// ItemPressed is raised when a new gengrid item is pressed.
84 public event EventHandler<GenGridItemEventArgs> ItemPressed;
87 /// ItemReleased is raised when a new gengrid item is released.
89 public event EventHandler<GenGridItemEventArgs> ItemReleased;
92 /// ItemActivated is raised when a new gengrid item is double clicked or pressed (enter|return|spacebar).
94 public event EventHandler<GenGridItemEventArgs> ItemActivated;
97 /// ItemDoubleClicked is raised when a new gengrid item is double clicked.
99 public event EventHandler<GenGridItemEventArgs> ItemDoubleClicked;
102 /// ItemRealized is raised when a gengrid item is implementing through <see cref="GenItemClass"/>.
104 public event EventHandler<GenGridItemEventArgs> ItemRealized;
107 /// ItemUnrealized is raised when the gengrid item is deleted.
109 public event EventHandler<GenGridItemEventArgs> ItemUnrealized;
112 /// ItemLongPressed is raised when a gengrid item is pressed for a certain amount of time. By default it's 1 second.
114 public event EventHandler<GenGridItemEventArgs> ItemLongPressed;
117 /// Changed is raised when an item is added, removed, resized or moved and when the gengrid is resized or gets "horizontal" property changes.
119 public event EventHandler Changed;
122 /// Gets or sets the item's grid alignment along x-axis within a given gengrid widget.
123 /// The range is less than or equal to 1,and greater than or equal to 0.
124 /// By default, value is 0.5, meaning that the gengrid has its items grid placed exactly in the middle along x-axis.
126 public double ItemAlignmentX
131 Interop.Elementary.elm_gengrid_align_get(RealHandle, out align, IntPtr.Zero);
136 double aligny = ItemAlignmentY;
137 Interop.Elementary.elm_gengrid_align_set(RealHandle, value, aligny);
142 /// Gets or sets the item's grid alignment on y-axis within a given gengrid widget.
143 /// The range is less than or equal to 1, and greater than or equal to 0.
144 /// By default, value is 0.5, meaning that the gengrid has its items grid placed exactly in the middle along y-axis.
146 public double ItemAlignmentY
151 Interop.Elementary.elm_gengrid_align_get(RealHandle, IntPtr.Zero, out align);
156 double alignx = ItemAlignmentX;
157 Interop.Elementary.elm_gengrid_align_set(RealHandle, alignx, value);
162 /// Gets or sets the manner in which the items grid is filled within a given gengrid widget.
163 /// It is filled if true, otherwise false.
165 public bool FillItems
169 return Interop.Elementary.elm_gengrid_filled_get(RealHandle);
173 Interop.Elementary.elm_gengrid_filled_set(RealHandle, value);
178 /// Gets or sets whether multi-selection is enabled or disabled for a given gengrid widget.
181 /// Multi-selection is the ability to have more than one item selected, on a given gengrid, simultaneously.
182 /// When it is enabled, a sequence of clicks on different items makes them all selected, progressively.
183 /// A click on an already selected item unselects it. If interacting via the keyboard, multi-selection is enabled while holding the "Shift" key.
184 /// By default, multi-selection is disabled.
186 public bool MultipleSelection
190 return Interop.Elementary.elm_gengrid_multi_select_get(RealHandle);
194 Interop.Elementary.elm_gengrid_multi_select_set(RealHandle, value);
199 /// Gets or sets the width for the items of a given gengrid widget.
202 /// A gengrid, after creation, still has no information on the size to give to each of its cells.
203 /// The default width and height just have one finger wide.
204 /// Use this property to force a custom width for your items, making them as big as you wish.
211 Interop.Elementary.elm_gengrid_item_size_get(RealHandle, out width, IntPtr.Zero);
216 int height = ItemHeight;
217 Interop.Elementary.elm_gengrid_item_size_set(RealHandle, value, height);
222 /// Gets or sets the height for the items of a given gengrid widget.
225 /// A gengrid, after creation, still has no information on the size to give to each of its cells.
226 /// The default width and height just have one finger wide.
227 /// Use this property to force a custom height for your items, making them as big as you wish.
229 public int ItemHeight
234 Interop.Elementary.elm_gengrid_item_size_get(RealHandle, IntPtr.Zero, out height);
239 int width = ItemWidth;
240 Interop.Elementary.elm_gengrid_item_size_set(RealHandle, width, value);
245 /// Gets or sets the gengrid select mode by <see cref="GenGridSelectionMode"/>.
247 public GenItemSelectionMode SelectionMode
251 return (GenItemSelectionMode)Interop.Elementary.elm_gengrid_select_mode_get(RealHandle);
255 Interop.Elementary.elm_gengrid_select_mode_set(RealHandle, (int)value);
260 /// Gets or sets the direction for which a given gengrid widget expands while placing its items.
263 /// If true, items are placed in columns from top to bottom and when the space for a column is filled, another one is started on the right, thus expanding the grid horizontally.
264 /// If false, items are placed in rows from left to right, and when the space for a row is filled, another one is started below, thus expanding the grid vertically.
266 public bool IsHorizontal
270 return Interop.Elementary.elm_gengrid_horizontal_get(RealHandle);
274 Interop.Elementary.elm_gengrid_horizontal_set(RealHandle, value);
279 /// Gets or sets whether the gengrid items should be highlighted when an item is selected.
281 public bool IsHighlight
285 return Interop.Elementary.elm_gengrid_highlight_mode_get(RealHandle);
289 Interop.Elementary.elm_gengrid_highlight_mode_set(RealHandle, value);
294 /// Sets or gets the value of HorizontalScrollBarVisiblePolicy
297 /// ScrollBarVisiblePolicy.Auto means the horizontal scrollbar is made visible if it is needed, and otherwise kept hidden.
298 /// ScrollBarVisiblePolicy.Visible turns it on all the time, and ScrollBarVisiblePolicy.Invisible always keeps it off.
300 public ScrollBarVisiblePolicy HorizontalScrollBarVisiblePolicy
305 Interop.Elementary.elm_scroller_policy_get(RealHandle, out policy, IntPtr.Zero);
306 return (ScrollBarVisiblePolicy)policy;
310 ScrollBarVisiblePolicy v = VerticalScrollBarVisiblePolicy;
311 Interop.Elementary.elm_scroller_policy_set(RealHandle, (int)value, (int)v);
316 /// Sets or gets the value of VerticalScrollBarVisiblePolicy
319 /// ScrollBarVisiblePolicy.Auto means the vertical scrollbar is made visible if it is needed, and otherwise kept hidden.
320 /// ScrollBarVisiblePolicy.Visible turns it on all the time, and ScrollBarVisiblePolicy.Invisible always keeps it off.
322 public ScrollBarVisiblePolicy VerticalScrollBarVisiblePolicy
327 Interop.Elementary.elm_scroller_policy_get(RealHandle, IntPtr.Zero, out policy);
328 return (ScrollBarVisiblePolicy)policy;
332 ScrollBarVisiblePolicy h = HorizontalScrollBarVisiblePolicy;
333 Interop.Elementary.elm_scroller_policy_set(RealHandle, (int)h, (int)value);
338 /// Gets the first item in a given gengrid widget.
340 public GenGridItem FirstItem
344 IntPtr handle = Interop.Elementary.elm_gengrid_first_item_get(RealHandle);
345 return ItemObject.GetItemByHandle(handle) as GenGridItem;
350 /// Gets the last item in a given gengrid widget.
352 public GenGridItem LastItem
356 IntPtr handle = Interop.Elementary.elm_gengrid_last_item_get(RealHandle);
357 return ItemObject.GetItemByHandle(handle) as GenGridItem;
362 /// Gets the items count in a given gengrid widget.
364 public uint ItemCount
368 return Interop.Elementary.elm_gengrid_items_count(RealHandle);
373 /// Gets the selected item in a given gengrid widget.
375 public GenGridItem SelectedItem
379 IntPtr handle = Interop.Elementary.elm_gengrid_selected_item_get(RealHandle);
380 return ItemObject.GetItemByHandle(handle) as GenGridItem;
385 /// Gets or sets whether a given gengrid widget is or not able have items reordered.
387 public bool ReorderMode
391 return Interop.Elementary.elm_gengrid_reorder_mode_get(RealHandle);
395 Interop.Elementary.elm_gengrid_reorder_mode_set(RealHandle, value);
400 /// Appends a new item to a given gengrid widget. This adds an item to the end of the gengrid.
402 /// <param name="itemClass">The itemClass defines how to display the data.</param>
403 /// <param name="data">The item data.</param>
404 /// <returns>Return a gengrid item that contains data and itemClass.</returns>
405 /// <seealso cref="GenItemClass"/>
406 /// <seealso cref="GenGridItem"/>
407 public GenGridItem Append(GenItemClass itemClass, object data)
409 GenGridItem item = new GenGridItem(data, itemClass);
410 IntPtr handle = Interop.Elementary.elm_gengrid_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, null, (IntPtr)item.Id);
411 item.Handle = handle;
417 /// Prepends a new item to a given gengrid widget. This adds an item to the beginning of the gengrid.
419 /// <param name="itemClass">The itemClass defines how to display the data.</param>
420 /// <param name="data">The item data.</param>
421 /// <returns>Return a gengrid item that contains data and itemClass.</returns>
422 /// <seealso cref="GenItemClass"/>
423 /// <seealso cref="GenGridItem"/>
424 public GenGridItem Prepend(GenItemClass itemClass, object data)
426 GenGridItem item = new GenGridItem(data, itemClass);
427 IntPtr handle = Interop.Elementary.elm_gengrid_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, null, (IntPtr)item.Id);
428 item.Handle = handle;
434 /// Inserts an item before another in a gengrid widget. This inserts an item before another in the gengrid.
436 /// <param name="itemClass">The itemClass defines how to display the data.</param>
437 /// <param name="data">The item data.</param>
438 /// <param name="before">The item before which to place this new one.</param>
439 /// <returns>Return a gengrid item that contains data and itemClass./></returns>
440 /// <seealso cref="GenItemClass"/>
441 /// <seealso cref="GenGridItem"/>
442 public GenGridItem InsertBefore(GenItemClass itemClass, object data, GenGridItem before)
444 GenGridItem item = new GenGridItem(data, itemClass);
445 IntPtr handle = Interop.Elementary.elm_gengrid_item_insert_before(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, before, null, (IntPtr)item.Id);
446 item.Handle = handle;
452 /// Inserts an item before another in a gengrid widget. This inserts an item after another in the gengrid.
454 /// <param name="itemClass">The itemClass defines how to display the data.</param>
455 /// <param name="data">The item data.</param>
456 /// <param name="after">The item after which to place this new one.</param>
457 /// <returns>Return a gengrid item that contains data and itemClass.</returns>
458 /// <seealso cref="GenItemClass"/>
459 /// <seealso cref="GenGridItem"/>
460 public GenGridItem InsertAfter(GenItemClass itemClass, object data, GenGridItem after)
462 GenGridItem item = new GenGridItem(data, itemClass);
463 IntPtr handle = Interop.Elementary.elm_gengrid_item_insert_after(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, after, null, (IntPtr)item.Id);
464 item.Handle = handle;
470 /// Insert an item in a gengrid widget using a user-defined sort function.
472 /// <param name="itemClass">The itemClass defines how to display the data.</param>
473 /// <param name="data">The item data.</param>
474 /// <param name="func">User defined comparison function that defines the sort order based on gengrid item and its data.</param>
475 /// <returns>Return a gengrid item that contains data and itemClass.</returns>
476 public GenGridItem InsertSorted(GenItemClass itemClass, object data, Comparison<GenGridItem> comparison)
478 Interop.Elementary.Eina_Compare_Cb compareCallback = (handle1, handle2) =>
480 GenGridItem item1 = ItemObject.GetItemByHandle(handle1) as GenGridItem;
481 GenGridItem item2 = ItemObject.GetItemByHandle(handle2) as GenGridItem;
482 return comparison(item1, item2);
485 GenGridItem item = new GenGridItem(data, itemClass);
487 IntPtr handle = Interop.Elementary.elm_gengrid_item_sorted_insert(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, compareCallback, null, (IntPtr)item.Id);
488 item.Handle = handle;
494 /// Shows a given item to the visible area of a gengrid.
496 /// <param name="item">The gengrid item to display.</param>
497 /// <param name="position">The position of the item in the viewport.</param>
498 /// <param name="animated">The type of how to show the item.</param>
500 /// If animated is true, the gengrid shows item by scrolling if it's not fully visible.
501 /// If animated is false, the gengrid shows item by jumping if it's not fully visible.
503 /// <seealso cref="ScrollToPosition"/>
504 public void ScrollTo(GenGridItem item, ScrollToPosition position, bool animated)
508 Interop.Elementary.elm_gengrid_item_bring_in(item.Handle, (int)position);
512 Interop.Elementary.elm_gengrid_item_show(item.Handle, (int)position);
517 /// Updates the contents of all the realized items.
518 /// This updates all realized items by calling all the <see cref="GenItemClass"/> again to get the content, text, and states.
519 /// Use this when the original item data has changed and the changes are desired to reflect.
522 /// <see cref="GenItem.Update()"/> to update just one item.
524 public void UpdateRealizedItems()
526 Interop.Elementary.elm_gengrid_realized_items_update(RealHandle);
530 /// Removes all items from a given gengrid widget.
531 /// This removes(and deletes) all items in obj, making it empty.
534 /// <see cref="ItemObject.Delete()"/> to delete just one item.
538 Interop.Elementary.elm_gengrid_clear(RealHandle);
542 /// Get the item that is at the x, y canvas coords.
544 /// <param name="x">The input x coordinate</param>
545 /// <param name="y">The input y coordinate</param>
546 /// <param name="positionX">The position relative to the item returned here.
547 /// -1, 0 or 1, depending if the coordinate is on the left portion of that item(-1), on the middle section(0) or on the right part(1).
549 /// <param name="positionY">The position relative to the item returned here
550 /// -1, 0 or 1, depending if the coordinate is on the upper portion of that item (-1), on the middle section (0) or on the lower part (1).
552 /// <returns></returns>
553 public GenGridItem GetItemByPosition(int x, int y, out int portionX, out int portionY)
555 IntPtr handle = Interop.Elementary.elm_gengrid_at_xy_item_get(RealHandle, x, y, out portionX, out portionY);
556 return ItemObject.GetItemByHandle(handle) as GenGridItem;
559 protected override IntPtr CreateHandle(EvasObject parent)
561 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
562 Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
564 RealHandle = Interop.Elementary.elm_gengrid_add(handle);
565 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
570 void InitializeSmartEvent()
572 _selected = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "selected", GenGridItemEventArgs.CreateFromSmartEvent);
573 _unselected = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "unselected", GenGridItemEventArgs.CreateFromSmartEvent);
574 _activated = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "activated", GenGridItemEventArgs.CreateFromSmartEvent);
575 _pressed = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "pressed", GenGridItemEventArgs.CreateFromSmartEvent);
576 _released = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "released", GenGridItemEventArgs.CreateFromSmartEvent);
577 _doubleClicked = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "clicked,double", GenGridItemEventArgs.CreateFromSmartEvent);
578 _realized = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "realized", GenGridItemEventArgs.CreateFromSmartEvent);
579 _unrealized = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "unrealized", GenGridItemEventArgs.CreateFromSmartEvent);
580 _longpressed = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "longpressed", GenGridItemEventArgs.CreateFromSmartEvent);
581 _changed = new SmartEvent(this, this.RealHandle, "changed");
583 _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
584 _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
585 _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
586 _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
587 _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
588 _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
589 _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
590 _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
591 _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
592 _changed.On += (s, e) => { Changed?.Invoke(this, e); };
595 void AddInternal(GenGridItem item)
598 item.Deleted += Item_Deleted;
601 void Item_Deleted(object sender, EventArgs e)
603 _children.Remove((GenGridItem)sender);