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 the item which is the <see cref="GenGridItem"/> type.
25 /// All events of the GenGrid contain GenGridItemEventArgs as a parameter.
27 /// <since_tizen> preview </since_tizen>
28 public class GenGridItemEventArgs : EventArgs
31 /// Gets or sets the gengrid item. The return type is <see cref="GenGridItem"/>.
33 /// <since_tizen> preview </since_tizen>
34 public GenGridItem Item { get; set; }
36 internal static GenGridItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
38 GenGridItem item = ItemObject.GetItemByHandle(info) as GenGridItem;
39 return new GenGridItemEventArgs { Item = item };
44 /// It inherits <see cref="Layout"/>.
45 /// The GenGrid is a widget that aims to position objects in a grid layout, while actually creating and rendering only the visible ones.
46 /// It has two directions in which a given GenGrid widget expands while placing its items, horizontal and vertical.
47 /// The gengrid items are represented through the <see cref="GenItemClass"/> definition field details.
49 /// <since_tizen> preview </since_tizen>
50 public class GenGrid : Layout
52 HashSet<GenGridItem> _children = new HashSet<GenGridItem>();
54 SmartEvent<GenGridItemEventArgs> _selected;
55 SmartEvent<GenGridItemEventArgs> _unselected;
56 SmartEvent<GenGridItemEventArgs> _activated;
57 SmartEvent<GenGridItemEventArgs> _pressed;
58 SmartEvent<GenGridItemEventArgs> _released;
59 SmartEvent<GenGridItemEventArgs> _doubleClicked;
60 SmartEvent<GenGridItemEventArgs> _realized;
61 SmartEvent<GenGridItemEventArgs> _unrealized;
62 SmartEvent<GenGridItemEventArgs> _longpressed;
63 SmartEvent<GenGridItemEventArgs> _focused;
64 SmartEvent<GenGridItemEventArgs> _unfocused;
68 /// Creates and initializes a new instance of the GenGrid class.
70 /// <param name="parent">The parent is a given container which will be attached by GenGrid as a child. It's the <see cref="EvasObject"/> type.</param>
71 /// <since_tizen> preview </since_tizen>
72 public GenGrid(EvasObject parent) : base(parent)
74 InitializeSmartEvent();
78 /// ItemSelected is raised when a new GenGrid item is selected.
80 /// <since_tizen> preview </since_tizen>
81 public event EventHandler<GenGridItemEventArgs> ItemSelected;
84 /// ItemUnselected is raised when the gengrid item is unselected.
86 /// <since_tizen> preview </since_tizen>
87 public event EventHandler<GenGridItemEventArgs> ItemUnselected;
90 /// ItemPressed is raised when a new gengrid item is pressed.
92 /// <since_tizen> preview </since_tizen>
93 public event EventHandler<GenGridItemEventArgs> ItemPressed;
96 /// ItemReleased is raised when a new gengrid item is released.
98 /// <since_tizen> preview </since_tizen>
99 public event EventHandler<GenGridItemEventArgs> ItemReleased;
102 /// ItemActivated is raised when a new gengrid item is double-clicked or pressed (enter|return|spacebar).
104 /// <since_tizen> preview </since_tizen>
105 public event EventHandler<GenGridItemEventArgs> ItemActivated;
108 /// ItemDoubleClicked is raised when a new gengrid item is double-clicked.
110 /// <since_tizen> preview </since_tizen>
111 public event EventHandler<GenGridItemEventArgs> ItemDoubleClicked;
114 /// ItemRealized is raised when a gengrid item is implemented through <see cref="GenItemClass"/>.
116 /// <since_tizen> preview </since_tizen>
117 public event EventHandler<GenGridItemEventArgs> ItemRealized;
120 /// ItemUnrealized is raised when the gengrid item is deleted.
122 /// <since_tizen> preview </since_tizen>
123 public event EventHandler<GenGridItemEventArgs> ItemUnrealized;
126 /// ItemLongPressed is raised when a gengrid item is pressed for a certain amount of time. By default it's 1 second.
128 /// <since_tizen> preview </since_tizen>
129 public event EventHandler<GenGridItemEventArgs> ItemLongPressed;
132 /// ItemFocussed is raised when a gengrid item has received focus.
134 /// <since_tizen> preview </since_tizen>
135 public event EventHandler<GenGridItemEventArgs> ItemFocused;
138 /// ItemUnfocussed is raised when a gengrid item has lost focus.
140 /// <since_tizen> preview </since_tizen>
141 public event EventHandler<GenGridItemEventArgs> ItemUnfocused;
144 /// Changed is raised when an item is added, removed, resized or moved and when the gengrid is resized or gets "horizontal" property changes.
146 /// <since_tizen> preview </since_tizen>
147 public event EventHandler Changed;
150 /// Gets or sets the item's grid alignment along X-axis within a given GenGrid widget.
151 /// Accepted values are in the 0.0 to 1.0 range, with the special value -1.0 used to specify "justify" or "fill" by some users.
152 /// By default, value is 0.0, meaning that the gengrid has its items grid placed exactly in the left along X-axis.
154 /// <since_tizen> preview </since_tizen>
155 public double ItemAlignmentX
160 Interop.Elementary.elm_gengrid_align_get(RealHandle, out align, IntPtr.Zero);
165 double aligny = ItemAlignmentY;
166 Interop.Elementary.elm_gengrid_align_set(RealHandle, value, aligny);
171 /// Gets or sets the item's grid alignment on Y-axis within a given GenGrid widget.
172 /// Accepted values are in the 0.0 to 1.0 range, with the special value -1.0 used to specify "justify" or "fill" by some users.
173 /// By default, value is 0.0, meaning that the gengrid has its items grid placed exactly in the top along Y-axis.
175 /// <since_tizen> preview </since_tizen>
176 public double ItemAlignmentY
181 Interop.Elementary.elm_gengrid_align_get(RealHandle, IntPtr.Zero, out align);
186 double alignx = ItemAlignmentX;
187 Interop.Elementary.elm_gengrid_align_set(RealHandle, alignx, value);
192 /// Gets or sets the manner in which the items grid is filled within a given GenGrid widget.
193 /// It is filled if true, otherwise not filled if false.
195 /// <since_tizen> preview </since_tizen>
196 public bool FillItems
200 return Interop.Elementary.elm_gengrid_filled_get(RealHandle);
204 Interop.Elementary.elm_gengrid_filled_set(RealHandle, value);
209 /// Gets or sets whether multi-selection is enabled or disabled for a given GenGrid widget.
212 /// Multi-selection is the ability to have more than one item selected, on a given gengrid, simultaneously.
213 /// When it is enabled, a sequence of clicks on different items makes them all selected, progressively.
214 /// A click on an already selected item unselects it. If interacting via the keyboard, multi-selection is enabled while holding the "Shift" key.
215 /// By default, multi-selection is disabled.
217 /// <since_tizen> preview </since_tizen>
218 public bool MultipleSelection
222 return Interop.Elementary.elm_gengrid_multi_select_get(RealHandle);
226 Interop.Elementary.elm_gengrid_multi_select_set(RealHandle, value);
231 /// Gets or sets the width for the items of a given GenGrid widget.
234 /// A gengrid, after creation, still has no information on the size to give to each of its cells.
235 /// The default width and height just are one finger wide.
236 /// Use this property to force a custom width for your items, making them as big as you wish.
238 /// <since_tizen> preview </since_tizen>
244 Interop.Elementary.elm_gengrid_item_size_get(RealHandle, out width, IntPtr.Zero);
249 int height = ItemHeight;
250 Interop.Elementary.elm_gengrid_item_size_set(RealHandle, value, height);
255 /// Gets or sets the height for the items of a given GenGrid widget.
258 /// A gengrid, after creation, still has no information on the size to give to each of its cells.
259 /// The default width and height just are one finger wide.
260 /// Use this property to force a custom height for your items, making them as big as you wish.
262 /// <since_tizen> preview </since_tizen>
263 public int ItemHeight
268 Interop.Elementary.elm_gengrid_item_size_get(RealHandle, IntPtr.Zero, out height);
273 int width = ItemWidth;
274 Interop.Elementary.elm_gengrid_item_size_set(RealHandle, width, value);
279 /// Gets or sets the gengrid select mode by <see cref="GenItemSelectionMode"/>.
281 /// <since_tizen> preview </since_tizen>
282 public GenItemSelectionMode SelectionMode
286 return (GenItemSelectionMode)Interop.Elementary.elm_gengrid_select_mode_get(RealHandle);
290 Interop.Elementary.elm_gengrid_select_mode_set(RealHandle, (int)value);
295 /// Gets or sets the direction for which a given GenGrid widget expands while placing its items.
298 /// 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.
299 /// 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.
301 /// <since_tizen> preview </since_tizen>
302 public bool IsHorizontal
306 return Interop.Elementary.elm_gengrid_horizontal_get(RealHandle);
310 Interop.Elementary.elm_gengrid_horizontal_set(RealHandle, value);
315 /// Gets or sets whether the gengrid items should be highlighted when an item is selected.
317 /// <since_tizen> preview </since_tizen>
318 public bool IsHighlight
322 return Interop.Elementary.elm_gengrid_highlight_mode_get(RealHandle);
326 Interop.Elementary.elm_gengrid_highlight_mode_set(RealHandle, value);
331 /// Sets or gets the value of HorizontalScrollBarVisiblePolicy.
334 /// ScrollBarVisiblePolicy.Auto means the horizontal scrollbar is made visible if it is needed, and otherwise kept hidden.
335 /// ScrollBarVisiblePolicy.Visible turns it on all the time, and ScrollBarVisiblePolicy.Invisible always keeps it off.
337 /// <since_tizen> preview </since_tizen>
338 public ScrollBarVisiblePolicy HorizontalScrollBarVisiblePolicy
343 Interop.Elementary.elm_scroller_policy_get(RealHandle, out policy, IntPtr.Zero);
344 return (ScrollBarVisiblePolicy)policy;
348 ScrollBarVisiblePolicy v = VerticalScrollBarVisiblePolicy;
349 Interop.Elementary.elm_scroller_policy_set(RealHandle, (int)value, (int)v);
354 /// Sets or gets the value of VerticalScrollBarVisiblePolicy.
357 /// ScrollBarVisiblePolicy.Auto means the vertical scrollbar is made visible if it is needed, and otherwise kept hidden.
358 /// ScrollBarVisiblePolicy.Visible turns it on all the time, and ScrollBarVisiblePolicy.Invisible always keeps it off.
360 /// <since_tizen> preview </since_tizen>
361 public ScrollBarVisiblePolicy VerticalScrollBarVisiblePolicy
366 Interop.Elementary.elm_scroller_policy_get(RealHandle, IntPtr.Zero, out policy);
367 return (ScrollBarVisiblePolicy)policy;
371 ScrollBarVisiblePolicy h = HorizontalScrollBarVisiblePolicy;
372 Interop.Elementary.elm_scroller_policy_set(RealHandle, (int)h, (int)value);
377 /// Gets the first item in a given GenGrid widget.
379 /// <since_tizen> preview </since_tizen>
380 public GenGridItem FirstItem
384 IntPtr handle = Interop.Elementary.elm_gengrid_first_item_get(RealHandle);
385 return ItemObject.GetItemByHandle(handle) as GenGridItem;
390 /// Gets the last item in a given GenGrid widget.
392 /// <since_tizen> preview </since_tizen>
393 public GenGridItem LastItem
397 IntPtr handle = Interop.Elementary.elm_gengrid_last_item_get(RealHandle);
398 return ItemObject.GetItemByHandle(handle) as GenGridItem;
403 /// Gets the items count in a given GenGrid widget.
405 /// <since_tizen> preview </since_tizen>
406 public uint ItemCount
410 return Interop.Elementary.elm_gengrid_items_count(RealHandle);
415 /// Gets the selected item in a given GenGrid widget.
417 /// <since_tizen> preview </since_tizen>
418 public GenGridItem SelectedItem
422 IntPtr handle = Interop.Elementary.elm_gengrid_selected_item_get(RealHandle);
423 return ItemObject.GetItemByHandle(handle) as GenGridItem;
428 /// Gets or sets whether a given GenGrid widget is able to or not able to have items reordered.
430 /// <since_tizen> preview </since_tizen>
431 public bool ReorderMode
435 return Interop.Elementary.elm_gengrid_reorder_mode_get(RealHandle);
439 Interop.Elementary.elm_gengrid_reorder_mode_set(RealHandle, value);
444 /// Appends a new item to a given GenGrid widget. This adds an item to the end of the gengrid.
446 /// <param name="itemClass">The itemClass defines how to display the data.</param>
447 /// <param name="data">The item data.</param>
448 /// <returns>Return a gengrid item that contains the data and itemClass.</returns>
449 /// <seealso cref="GenItemClass"/>
450 /// <seealso cref="GenGridItem"/>
451 /// <since_tizen> preview </since_tizen>
452 public GenGridItem Append(GenItemClass itemClass, object data)
454 GenGridItem item = new GenGridItem(data, itemClass, this);
455 item.Handle = Interop.Elementary.elm_gengrid_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, null, (IntPtr)item.Id);
461 /// Prepends a new item to a given GenGrid widget. This adds an item to the beginning of the gengrid.
463 /// <param name="itemClass">The itemClass defines how to display the data.</param>
464 /// <param name="data">The item data.</param>
465 /// <returns>Return a gengrid item that contains the data and itemClass.</returns>
466 /// <seealso cref="GenItemClass"/>
467 /// <seealso cref="GenGridItem"/>
468 /// <since_tizen> preview </since_tizen>
469 public GenGridItem Prepend(GenItemClass itemClass, object data)
471 GenGridItem item = new GenGridItem(data, itemClass, this);
472 item.Handle = Interop.Elementary.elm_gengrid_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, null, (IntPtr)item.Id);
478 /// Inserts an item before another in a GenGrid widget. This inserts an item before another in the gengrid.
480 /// <param name="itemClass">The itemClass defines how to display the data.</param>
481 /// <param name="data">The item data.</param>
482 /// <param name="before">The item before which to place this new one.</param>
483 /// <returns>Return a gengrid item that contains the data and itemClass.</returns>
484 /// <seealso cref="GenItemClass"/>
485 /// <seealso cref="GenGridItem"/>
486 /// <since_tizen> preview </since_tizen>
487 public GenGridItem InsertBefore(GenItemClass itemClass, object data, GenGridItem before)
489 GenGridItem item = new GenGridItem(data, itemClass, this);
490 item.Handle = Interop.Elementary.elm_gengrid_item_insert_before(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, before, null, (IntPtr)item.Id);
496 /// Inserts an item after another in a GenGrid widget. This inserts an item after another in the gengrid.
498 /// <param name="itemClass">The itemClass defines how to display the data.</param>
499 /// <param name="data">The item data.</param>
500 /// <param name="after">The item after which to place this new one.</param>
501 /// <returns>Return a gengrid item that contains the data and itemClass.</returns>
502 /// <seealso cref="GenItemClass"/>
503 /// <seealso cref="GenGridItem"/>
504 /// <since_tizen> preview </since_tizen>
505 public GenGridItem InsertAfter(GenItemClass itemClass, object data, GenGridItem after)
507 GenGridItem item = new GenGridItem(data, itemClass, this);
508 item.Handle = Interop.Elementary.elm_gengrid_item_insert_after(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, after, null, (IntPtr)item.Id);
514 /// Inserts an item in a GenGrid widget using a user-defined sort function.
516 /// <param name="itemClass">The itemClass defines how to display the data.</param>
517 /// <param name="data">The item data.</param>
518 /// <param name="comparison">User defined comparison function that defines the sort order based on the gengrid item and its data.</param>
519 /// <returns>Return a gengrid item that contains the data and itemClass.</returns>
520 /// <since_tizen> preview </since_tizen>
521 public GenGridItem InsertSorted(GenItemClass itemClass, object data, Comparison<object> comparison)
523 GenGridItem item = new GenGridItem(data, itemClass, this);
525 Interop.Elementary.Eina_Compare_Cb compareCallback = (handle1, handle2) =>
527 GenGridItem first = (ItemObject.GetItemByHandle(handle1) as GenGridItem) ?? item;
528 GenGridItem second = (ItemObject.GetItemByHandle(handle2) as GenGridItem) ?? item;
529 return comparison(first.Data, second.Data);
532 item.Handle = Interop.Elementary.elm_gengrid_item_sorted_insert(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, compareCallback, null, (IntPtr)item.Id);
538 /// Shows a given item to the visible area of a gengrid.
540 /// <param name="item">The gengrid item to display.</param>
541 /// <param name="position">The position of the item in the viewport.</param>
542 /// <param name="animated">The type of how to show the item.</param>
544 /// If animated is true, the gengrid shows the item by scrolling if it's not fully visible.
545 /// If animated is false, the gengrid shows the item by jumping if it's not fully visible.
547 /// <seealso cref="ScrollToPosition"/>
548 /// <since_tizen> preview </since_tizen>
549 public void ScrollTo(GenGridItem item, ScrollToPosition position, bool animated)
553 Interop.Elementary.elm_gengrid_item_bring_in(item.Handle, (int)position);
557 Interop.Elementary.elm_gengrid_item_show(item.Handle, (int)position);
562 /// Updates the contents of all the realized items.
563 /// This updates all realized items by calling all the <see cref="GenItemClass"/> again to get the content, text, and states.
564 /// Use this when the original item data has changed and the changes are desired to reflect.
567 /// <see cref="GenItem.Update()"/> to update just one item.
569 /// <since_tizen> preview </since_tizen>
570 public void UpdateRealizedItems()
572 Interop.Elementary.elm_gengrid_realized_items_update(RealHandle);
576 /// Removes all the items from a given GenGrid widget.
577 /// This removes (and deletes) all the items in the object, making it empty.
580 /// <see cref="ItemObject.Delete()"/> to delete just one item.
582 /// <since_tizen> preview </since_tizen>
585 Interop.Elementary.elm_gengrid_clear(RealHandle);
589 /// Gets the item that is at the X, Y canvas coordinates.
591 /// <param name="x">The input X coordinate.</param>
592 /// <param name="y">The input Y coordinate.</param>
593 /// <param name="portionX">The position relative to the item returned here.
594 /// -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).
596 /// <param name="portionY">The position relative to the item returned here.
597 /// -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).
599 /// <returns></returns>
600 /// <since_tizen> preview </since_tizen>
601 public GenGridItem GetItemByPosition(int x, int y, out int portionX, out int portionY)
603 IntPtr handle = Interop.Elementary.elm_gengrid_at_xy_item_get(RealHandle, x, y, out portionX, out portionY);
604 return ItemObject.GetItemByHandle(handle) as GenGridItem;
608 /// Creates a widget handle.
610 /// <param name="parent">Parent EvasObject.</param>
611 /// <returns>Handle IntPtr.</returns>
612 /// <since_tizen> preview </since_tizen>
613 protected override IntPtr CreateHandle(EvasObject parent)
615 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
616 Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
618 RealHandle = Interop.Elementary.elm_gengrid_add(handle);
619 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
624 void InitializeSmartEvent()
626 _selected = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "selected", GenGridItemEventArgs.CreateFromSmartEvent);
627 _unselected = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "unselected", GenGridItemEventArgs.CreateFromSmartEvent);
628 _activated = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "activated", GenGridItemEventArgs.CreateFromSmartEvent);
629 _pressed = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "pressed", GenGridItemEventArgs.CreateFromSmartEvent);
630 _released = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "released", GenGridItemEventArgs.CreateFromSmartEvent);
631 _doubleClicked = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "clicked,double", GenGridItemEventArgs.CreateFromSmartEvent);
632 _realized = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "realized", GenGridItemEventArgs.CreateFromSmartEvent);
633 _unrealized = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "unrealized", GenGridItemEventArgs.CreateFromSmartEvent);
634 _longpressed = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "longpressed", GenGridItemEventArgs.CreateFromSmartEvent);
635 _focused = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "item,focused", GenGridItemEventArgs.CreateFromSmartEvent);
636 _unfocused = new SmartEvent<GenGridItemEventArgs>(this, this.RealHandle, "item,unfocused", GenGridItemEventArgs.CreateFromSmartEvent);
637 _changed = new SmartEvent(this, this.RealHandle, "changed");
639 _selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
640 _unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
641 _activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
642 _pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
643 _released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
644 _doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
645 _realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
646 _unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
647 _longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
648 _focused.On += (s, e) => { if (e.Item != null) ItemFocused?.Invoke(this, e); };
649 _unfocused.On += (s, e) => { if (e.Item != null) ItemUnfocused?.Invoke(this, e); };
650 _changed.On += (s, e) => { Changed?.Invoke(this, e); };
653 void AddInternal(GenGridItem item)
656 item.Deleted += Item_Deleted;
659 void Item_Deleted(object sender, EventArgs e)
661 _children.Remove((GenGridItem)sender);