/*
* 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
{
///
/// The ItemObject is used to manage item object
///
public class ItemObject
{
private static Dictionary s_IdToItemTable = new Dictionary();
private static Dictionary s_HandleToItemTable = new Dictionary();
private static int s_globalId = 0;
readonly Dictionary _partContents = new Dictionary();
Interop.Evas.SmartCallback _deleteCallback;
IntPtr _handle = IntPtr.Zero;
///
/// Creates and initializes a new instance of ItemObject class.
///
/// IntPtr
protected ItemObject(IntPtr handle)
{
_deleteCallback = DeleteCallbackHandler;
Id = GetNextId();
s_IdToItemTable[Id] = this;
Handle = handle;
}
// C# Finalizer was called on GC thread
// So, We can't access to EFL object
// And When Finalizer was called, Field can be already released.
//~ItemObject()
//{
// if (Handle != IntPtr.Zero)
// Interop.Elementary.elm_object_item_del(Handle);
//}
///
/// Gets the id of item object
///
public int Id { get; private set; }
///
/// Sets or gets whether the item object is enabled
///
public bool IsEnabled
{
get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
}
internal IntPtr Handle
{
get
{
return _handle;
}
set
{
if (_handle == value)
return;
if (_handle != IntPtr.Zero)
{
UnsetDeleteCallback();
}
_handle = value;
SetDeleteCallback();
s_HandleToItemTable[Handle] = this;
}
}
///
/// Deleted will be triggered when the item object is deleted
///
public event EventHandler Deleted;
///
/// Delete the item object
///
public void Delete()
{
Interop.Elementary.elm_object_item_del(Handle);
_handle = IntPtr.Zero;
}
///
/// Set a content of an object item and delete old content
///
/// The content part name (null for the default content)
/// The content of the object item
public void SetPartContent(string part, EvasObject content)
{
SetPartContent(part, content, false);
}
///
/// Set a content of an object item
///
/// The content part name (null for the default content)
/// The content of the object item
/// judge whether delete old content
public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
{
IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
if (oldContent != IntPtr.Zero && !preserveOldContent)
{
Interop.Evas.evas_object_del(oldContent);
}
Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
_partContents[part ?? "__default__"] = content;
}
///
/// Set a label of an object item
///
/// The text part name (null for the default label)
/// Text of the label
public void SetPartText(string part, string text)
{
Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
}
///
/// Gets a label of an object item
///
/// The text part name (null for the default label)
///
public string GetPartText(string part)
{
return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
}
///
/// Sets color of an object item
///
/// The text part name (null for the default label)
/// the color
public void SetPartColor(string part, Color color)
{
Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
color.G * color.A / 255,
color.B * color.A / 255,
color.A);
}
///
/// Gets color of an object item
///
/// The text part name (null for the default label)
/// the color of object item
public Color GetPartColor(string part)
{
int r, g, b, a;
Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
}
///
/// Deletes color of an object item
///
/// The text part name
public void DeletePartColor(string part)
{
Interop.Elementary.elm_object_item_color_class_del(Handle, part);
}
///
/// Gets the handle of object item
///
/// ItemObject
public static implicit operator IntPtr(ItemObject obj)
{
if (obj == null)
return IntPtr.Zero;
return obj.Handle;
}
///
/// OnInvalidate of object item
///
protected virtual void OnInvalidate() { }
internal static ItemObject GetItemById(int id)
{
ItemObject value;
s_IdToItemTable.TryGetValue(id, out value);
return value;
}
internal static ItemObject GetItemByHandle(IntPtr handle)
{
ItemObject value;
s_HandleToItemTable.TryGetValue(handle, out value);
return value;
}
void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
{
Deleted?.Invoke(this, EventArgs.Empty);
OnInvalidate();
if (s_IdToItemTable.ContainsKey(Id))
{
s_IdToItemTable.Remove(Id);
}
if (s_HandleToItemTable.ContainsKey(_handle))
{
s_HandleToItemTable.Remove(_handle);
}
_partContents.Clear();
_handle = IntPtr.Zero;
}
void UnsetDeleteCallback()
{
Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
}
void SetDeleteCallback()
{
if (Handle != IntPtr.Zero)
Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
}
static int GetNextId()
{
return s_globalId++;
}
}
}