/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
namespace ElmSharp
{
///
/// It inherits System.EventArgs.
/// It contains Item which is type.
/// All events of GenGrid contain GenGridItemEventArgs as a parameter.
///
public class GenGridItemEventArgs : EventArgs
{
///
/// Gets or sets GenGrid item.The return type is .
///
public GenGridItem Item { get; set; }
internal static GenGridItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info)
{
GenGridItem item = ItemObject.GetItemByHandle(info) as GenGridItem;
return new GenGridItemEventArgs() { Item = item };
}
}
///
/// It inherits .
/// The GenGrid is a widget that aims to position objects in a grid layout while actually creating and rendering only the visible ones.
/// It has two direction in which a given GenGrid widget expands while placing its items, horizontal and vertical.
/// The GenGrid items are represented through definition field details.
///
public class GenGrid : Layout
{
HashSet _children = new HashSet();
SmartEvent _selected;
SmartEvent _unselected;
SmartEvent _activated;
SmartEvent _pressed;
SmartEvent _released;
SmartEvent _doubleClicked;
SmartEvent _realized;
SmartEvent _unrealized;
SmartEvent _longpressed;
SmartEvent _changed;
///
/// Creates and initializes a new instance of the GenGrid class.
///
/// The parent is a given container which will be attached by GenGrid as a child. It's type.
public GenGrid(EvasObject parent) : base(parent)
{
InitializeSmartEvent();
}
///
/// ItemSelected is raised when a new gengrid item is selected.
///
public event EventHandler ItemSelected;
///
/// ItemUnselected is raised when the gengrid item is Unselected.
///
public event EventHandler ItemUnselected;
///
/// ItemPressed is raised when a new gengrid item is pressed.
///
public event EventHandler ItemPressed;
///
/// ItemReleased is raised when a new gengrid item is released.
///
public event EventHandler ItemReleased;
///
/// ItemActivated is raised when a new gengrid item is double clicked or pressed (enter|return|spacebar).
///
public event EventHandler ItemActivated;
///
/// ItemDoubleClicked is raised when a new gengrid item is double clicked.
///
public event EventHandler ItemDoubleClicked;
///
/// ItemRealized is raised when a gengrid item is implementing through .
///
public event EventHandler ItemRealized;
///
/// ItemUnrealized is raised when the gengrid item is deleted.
///
public event EventHandler ItemUnrealized;
///
/// ItemLongPressed is raised when a gengrid item is pressed for a certain amount of time. By default it's 1 second.
///
public event EventHandler ItemLongPressed;
///
/// Changed is raised when an item is added, removed, resized or moved and when the gengrid is resized or gets "horizontal" property changes.
///
public event EventHandler Changed;
///
/// Gets or sets the item's grid alignment along x-axis within a given gengrid widget.
/// 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.
/// By default, value is 0.0, meaning that the gengrid has its items grid placed exactly in the left along x-axis.
///
public double ItemAlignmentX
{
get
{
double align;
Interop.Elementary.elm_gengrid_align_get(RealHandle, out align, IntPtr.Zero);
return align;
}
set
{
double aligny = ItemAlignmentY;
Interop.Elementary.elm_gengrid_align_set(RealHandle, value, aligny);
}
}
///
/// Gets or sets the item's grid alignment on y-axis within a given gengrid widget.
/// 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.
/// By default, value is 0.0, meaning that the gengrid has its items grid placed exactly in the top along y-axis.
///
public double ItemAlignmentY
{
get
{
double align;
Interop.Elementary.elm_gengrid_align_get(RealHandle, IntPtr.Zero, out align);
return align;
}
set
{
double alignx = ItemAlignmentX;
Interop.Elementary.elm_gengrid_align_set(RealHandle, alignx, value);
}
}
///
/// Gets or sets the manner in which the items grid is filled within a given gengrid widget.
/// It is filled if true, otherwise false.
///
public bool FillItems
{
get
{
return Interop.Elementary.elm_gengrid_filled_get(RealHandle);
}
set
{
Interop.Elementary.elm_gengrid_filled_set(RealHandle, value);
}
}
///
/// Gets or sets whether multi-selection is enabled or disabled for a given gengrid widget.
///
///
/// Multi-selection is the ability to have more than one item selected, on a given gengrid, simultaneously.
/// When it is enabled, a sequence of clicks on different items makes them all selected, progressively.
/// A click on an already selected item unselects it. If interacting via the keyboard, multi-selection is enabled while holding the "Shift" key.
/// By default, multi-selection is disabled.
///
public bool MultipleSelection
{
get
{
return Interop.Elementary.elm_gengrid_multi_select_get(RealHandle);
}
set
{
Interop.Elementary.elm_gengrid_multi_select_set(RealHandle, value);
}
}
///
/// Gets or sets the width for the items of a given gengrid widget.
///
///
/// A gengrid, after creation, still has no information on the size to give to each of its cells.
/// The default width and height just have one finger wide.
/// Use this property to force a custom width for your items, making them as big as you wish.
///
public int ItemWidth
{
get
{
int width;
Interop.Elementary.elm_gengrid_item_size_get(RealHandle, out width, IntPtr.Zero);
return width;
}
set
{
int height = ItemHeight;
Interop.Elementary.elm_gengrid_item_size_set(RealHandle, value, height);
}
}
///
/// Gets or sets the height for the items of a given gengrid widget.
///
///
/// A gengrid, after creation, still has no information on the size to give to each of its cells.
/// The default width and height just have one finger wide.
/// Use this property to force a custom height for your items, making them as big as you wish.
///
public int ItemHeight
{
get
{
int height;
Interop.Elementary.elm_gengrid_item_size_get(RealHandle, IntPtr.Zero, out height);
return height;
}
set
{
int width = ItemWidth;
Interop.Elementary.elm_gengrid_item_size_set(RealHandle, width, value);
}
}
///
/// Gets or sets the gengrid select mode by .
///
public GenItemSelectionMode SelectionMode
{
get
{
return (GenItemSelectionMode)Interop.Elementary.elm_gengrid_select_mode_get(RealHandle);
}
set
{
Interop.Elementary.elm_gengrid_select_mode_set(RealHandle, (int)value);
}
}
///
/// Gets or sets the direction for which a given gengrid widget expands while placing its items.
///
///
/// 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.
/// 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.
///
public bool IsHorizontal
{
get
{
return Interop.Elementary.elm_gengrid_horizontal_get(RealHandle);
}
set
{
Interop.Elementary.elm_gengrid_horizontal_set(RealHandle, value);
}
}
///
/// Gets or sets whether the gengrid items should be highlighted when an item is selected.
///
public bool IsHighlight
{
get
{
return Interop.Elementary.elm_gengrid_highlight_mode_get(RealHandle);
}
set
{
Interop.Elementary.elm_gengrid_highlight_mode_set(RealHandle, value);
}
}
///
/// Sets or gets the value of HorizontalScrollBarVisiblePolicy
///
///
/// ScrollBarVisiblePolicy.Auto means the horizontal scrollbar is made visible if it is needed, and otherwise kept hidden.
/// ScrollBarVisiblePolicy.Visible turns it on all the time, and ScrollBarVisiblePolicy.Invisible always keeps it off.
///
public ScrollBarVisiblePolicy HorizontalScrollBarVisiblePolicy
{
get
{
int policy;
Interop.Elementary.elm_scroller_policy_get(RealHandle, out policy, IntPtr.Zero);
return (ScrollBarVisiblePolicy)policy;
}
set
{
ScrollBarVisiblePolicy v = VerticalScrollBarVisiblePolicy;
Interop.Elementary.elm_scroller_policy_set(RealHandle, (int)value, (int)v);
}
}
///
/// Sets or gets the value of VerticalScrollBarVisiblePolicy
///
///
/// ScrollBarVisiblePolicy.Auto means the vertical scrollbar is made visible if it is needed, and otherwise kept hidden.
/// ScrollBarVisiblePolicy.Visible turns it on all the time, and ScrollBarVisiblePolicy.Invisible always keeps it off.
///
public ScrollBarVisiblePolicy VerticalScrollBarVisiblePolicy
{
get
{
int policy;
Interop.Elementary.elm_scroller_policy_get(RealHandle, IntPtr.Zero, out policy);
return (ScrollBarVisiblePolicy)policy;
}
set
{
ScrollBarVisiblePolicy h = HorizontalScrollBarVisiblePolicy;
Interop.Elementary.elm_scroller_policy_set(RealHandle, (int)h, (int)value);
}
}
///
/// Gets the first item in a given gengrid widget.
///
public GenGridItem FirstItem
{
get
{
IntPtr handle = Interop.Elementary.elm_gengrid_first_item_get(RealHandle);
return ItemObject.GetItemByHandle(handle) as GenGridItem;
}
}
///
/// Gets the last item in a given gengrid widget.
///
public GenGridItem LastItem
{
get
{
IntPtr handle = Interop.Elementary.elm_gengrid_last_item_get(RealHandle);
return ItemObject.GetItemByHandle(handle) as GenGridItem;
}
}
///
/// Gets the items count in a given gengrid widget.
///
public uint ItemCount
{
get
{
return Interop.Elementary.elm_gengrid_items_count(RealHandle);
}
}
///
/// Gets the selected item in a given gengrid widget.
///
public GenGridItem SelectedItem
{
get
{
IntPtr handle = Interop.Elementary.elm_gengrid_selected_item_get(RealHandle);
return ItemObject.GetItemByHandle(handle) as GenGridItem;
}
}
///
/// Gets or sets whether a given gengrid widget is or not able have items reordered.
///
public bool ReorderMode
{
get
{
return Interop.Elementary.elm_gengrid_reorder_mode_get(RealHandle);
}
set
{
Interop.Elementary.elm_gengrid_reorder_mode_set(RealHandle, value);
}
}
///
/// Appends a new item to a given gengrid widget. This adds an item to the end of the gengrid.
///
/// The itemClass defines how to display the data.
/// The item data.
/// Return a gengrid item that contains data and itemClass.
///
///
public GenGridItem Append(GenItemClass itemClass, object data)
{
GenGridItem item = new GenGridItem(data, itemClass);
IntPtr handle = Interop.Elementary.elm_gengrid_item_append(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, null, (IntPtr)item.Id);
item.Handle = handle;
AddInternal(item);
return item;
}
///
/// Prepends a new item to a given gengrid widget. This adds an item to the beginning of the gengrid.
///
/// The itemClass defines how to display the data.
/// The item data.
/// Return a gengrid item that contains data and itemClass.
///
///
public GenGridItem Prepend(GenItemClass itemClass, object data)
{
GenGridItem item = new GenGridItem(data, itemClass);
IntPtr handle = Interop.Elementary.elm_gengrid_item_prepend(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, null, (IntPtr)item.Id);
item.Handle = handle;
AddInternal(item);
return item;
}
///
/// Inserts an item before another in a gengrid widget. This inserts an item before another in the gengrid.
///
/// The itemClass defines how to display the data.
/// The item data.
/// The item before which to place this new one.
/// Return a gengrid item that contains data and itemClass./>
///
///
public GenGridItem InsertBefore(GenItemClass itemClass, object data, GenGridItem before)
{
GenGridItem item = new GenGridItem(data, itemClass);
IntPtr handle = Interop.Elementary.elm_gengrid_item_insert_before(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, before, null, (IntPtr)item.Id);
item.Handle = handle;
AddInternal(item);
return item;
}
///
/// Inserts an item before another in a gengrid widget. This inserts an item after another in the gengrid.
///
/// The itemClass defines how to display the data.
/// The item data.
/// The item after which to place this new one.
/// Return a gengrid item that contains data and itemClass.
///
///
public GenGridItem InsertAfter(GenItemClass itemClass, object data, GenGridItem after)
{
GenGridItem item = new GenGridItem(data, itemClass);
IntPtr handle = Interop.Elementary.elm_gengrid_item_insert_after(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, after, null, (IntPtr)item.Id);
item.Handle = handle;
AddInternal(item);
return item;
}
///
/// Insert an item in a gengrid widget using a user-defined sort function.
///
/// The itemClass defines how to display the data.
/// The item data.
/// User defined comparison function that defines the sort order based on gengrid item and its data.
/// Return a gengrid item that contains data and itemClass.
public GenGridItem InsertSorted(GenItemClass itemClass, object data, Comparison comparison)
{
Interop.Elementary.Eina_Compare_Cb compareCallback = (handle1, handle2) =>
{
GenGridItem item1 = ItemObject.GetItemByHandle(handle1) as GenGridItem;
GenGridItem item2 = ItemObject.GetItemByHandle(handle2) as GenGridItem;
return comparison(item1, item2);
};
GenGridItem item = new GenGridItem(data, itemClass);
IntPtr handle = Interop.Elementary.elm_gengrid_item_sorted_insert(RealHandle, itemClass.UnmanagedPtr, (IntPtr)item.Id, compareCallback, null, (IntPtr)item.Id);
item.Handle = handle;
AddInternal(item);
return item;
}
///
/// Shows a given item to the visible area of a gengrid.
///
/// The gengrid item to display.
/// The position of the item in the viewport.
/// The type of how to show the item.
///
/// If animated is true, the gengrid shows item by scrolling if it's not fully visible.
/// If animated is false, the gengrid shows item by jumping if it's not fully visible.
///
///
public void ScrollTo(GenGridItem item, ScrollToPosition position, bool animated)
{
if (animated)
{
Interop.Elementary.elm_gengrid_item_bring_in(item.Handle, (int)position);
}
else
{
Interop.Elementary.elm_gengrid_item_show(item.Handle, (int)position);
}
}
///
/// Updates the contents of all the realized items.
/// This updates all realized items by calling all the again to get the content, text, and states.
/// Use this when the original item data has changed and the changes are desired to reflect.
///
///
/// to update just one item.
///
public void UpdateRealizedItems()
{
Interop.Elementary.elm_gengrid_realized_items_update(RealHandle);
}
///
/// Removes all items from a given gengrid widget.
/// This removes(and deletes) all items in obj, making it empty.
///
///
/// to delete just one item.
///
public void Clear()
{
Interop.Elementary.elm_gengrid_clear(RealHandle);
}
///
/// Get the item that is at the x, y canvas coords.
///
/// The input x coordinate
/// The input y coordinate
/// The position relative to the item returned here.
/// -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).
///
/// The position relative to the item returned here
/// -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).
///
///
public GenGridItem GetItemByPosition(int x, int y, out int portionX, out int portionY)
{
IntPtr handle = Interop.Elementary.elm_gengrid_at_xy_item_get(RealHandle, x, y, out portionX, out portionY);
return ItemObject.GetItemByHandle(handle) as GenGridItem;
}
protected override IntPtr CreateHandle(EvasObject parent)
{
IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
RealHandle = Interop.Elementary.elm_gengrid_add(handle);
Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
return handle;
}
void InitializeSmartEvent()
{
_selected = new SmartEvent(this, this.RealHandle, "selected", GenGridItemEventArgs.CreateFromSmartEvent);
_unselected = new SmartEvent(this, this.RealHandle, "unselected", GenGridItemEventArgs.CreateFromSmartEvent);
_activated = new SmartEvent(this, this.RealHandle, "activated", GenGridItemEventArgs.CreateFromSmartEvent);
_pressed = new SmartEvent(this, this.RealHandle, "pressed", GenGridItemEventArgs.CreateFromSmartEvent);
_released = new SmartEvent(this, this.RealHandle, "released", GenGridItemEventArgs.CreateFromSmartEvent);
_doubleClicked = new SmartEvent(this, this.RealHandle, "clicked,double", GenGridItemEventArgs.CreateFromSmartEvent);
_realized = new SmartEvent(this, this.RealHandle, "realized", GenGridItemEventArgs.CreateFromSmartEvent);
_unrealized = new SmartEvent(this, this.RealHandle, "unrealized", GenGridItemEventArgs.CreateFromSmartEvent);
_longpressed = new SmartEvent(this, this.RealHandle, "longpressed", GenGridItemEventArgs.CreateFromSmartEvent);
_changed = new SmartEvent(this, this.RealHandle, "changed");
_selected.On += (s, e) => { if (e.Item != null) ItemSelected?.Invoke(this, e); };
_unselected.On += (s, e) => { if (e.Item != null) ItemUnselected?.Invoke(this, e); };
_activated.On += (s, e) => { if (e.Item != null) ItemActivated?.Invoke(this, e); };
_pressed.On += (s, e) => { if (e.Item != null) ItemPressed?.Invoke(this, e); };
_released.On += (s, e) => { if (e.Item != null) ItemReleased?.Invoke(this, e); };
_doubleClicked.On += (s, e) => { if (e.Item != null) ItemDoubleClicked?.Invoke(this, e); };
_realized.On += (s, e) => { if (e.Item != null) ItemRealized?.Invoke(this, e); };
_unrealized.On += (s, e) => { if (e.Item != null) ItemUnrealized?.Invoke(this, e); };
_longpressed.On += (s, e) => { if (e.Item != null) ItemLongPressed?.Invoke(this, e); };
_changed.On += (s, e) => { Changed?.Invoke(this, e); };
}
void AddInternal(GenGridItem item)
{
_children.Add(item);
item.Deleted += Item_Deleted;
}
void Item_Deleted(object sender, EventArgs e)
{
_children.Remove((GenGridItem)sender);
}
}
}