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 /// The ItemObject is used to manage item object
25 public class ItemObject
27 private static Dictionary<int, ItemObject> s_IdToItemTable = new Dictionary<int, ItemObject>();
28 private static Dictionary<IntPtr, ItemObject> s_HandleToItemTable = new Dictionary<IntPtr, ItemObject>();
29 private static int s_globalId = 0;
31 readonly Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
32 Interop.Evas.SmartCallback _deleteCallback;
33 IntPtr _handle = IntPtr.Zero;
36 /// Creates and initializes a new instance of ItemObject class.
38 /// <param name="handle">IntPtr</param>
39 protected ItemObject(IntPtr handle)
41 _deleteCallback = DeleteCallbackHandler;
43 s_IdToItemTable[Id] = this;
47 // C# Finalizer was called on GC thread
48 // So, We can't access to EFL object
49 // And When Finalizer was called, Field can be already released.
52 // if (Handle != IntPtr.Zero)
53 // Interop.Elementary.elm_object_item_del(Handle);
57 /// Gets the id of item object
59 public int Id { get; private set; }
62 /// Sets or gets whether the item object is enabled
66 get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
67 set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
70 internal IntPtr Handle
81 if (_handle != IntPtr.Zero)
83 UnsetDeleteCallback();
87 s_HandleToItemTable[Handle] = this;
92 /// Deleted will be triggered when the item object is deleted
94 public event EventHandler Deleted;
97 /// Delete the item object
101 Interop.Elementary.elm_object_item_del(Handle);
102 _handle = IntPtr.Zero;
106 /// Set a content of an object item and delete old content
108 /// <param name="part">The content part name (null for the default content)</param>
109 /// <param name="content">The content of the object item</param>
110 public void SetPartContent(string part, EvasObject content)
112 SetPartContent(part, content, false);
116 /// Set a content of an object item
118 /// <param name="part">The content part name (null for the default content)</param>
119 /// <param name="content">The content of the object item</param>
120 /// <param name="preserveOldContent">judge whether delete old content</param>
121 public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
123 IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
124 if (oldContent != IntPtr.Zero && !preserveOldContent)
126 Interop.Evas.evas_object_del(oldContent);
128 Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
129 _partContents[part ?? "__default__"] = content;
133 /// Set a label of an object item
135 /// <param name="part">The text part name (null for the default label)</param>
136 /// <param name="text">Text of the label</param>
137 public void SetPartText(string part, string text)
139 Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
143 /// Gets a label of an object item
145 /// <param name="part">The text part name (null for the default label)</param>
146 /// <returns></returns>
147 public string GetPartText(string part)
149 return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
153 /// Sets color of an object item
155 /// <param name="part">The text part name (null for the default label)</param>
156 /// <param name="color">the color</param>
157 public void SetPartColor(string part, Color color)
159 Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
160 color.G * color.A / 255,
161 color.B * color.A / 255,
166 /// Gets color of an object item
168 /// <param name="part">The text part name (null for the default label)</param>
169 /// <returns>the color of object item</returns>
170 public Color GetPartColor(string part)
173 Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
174 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
178 /// Deletes color of an object item
180 /// <param name="part">The text part name</param>
181 public void DeletePartColor(string part)
183 Interop.Elementary.elm_object_item_color_class_del(Handle, part);
187 /// Gets the handle of object item
189 /// <param name="obj">ItemObject</param>
190 public static implicit operator IntPtr(ItemObject obj)
198 /// OnInvalidate of object item
200 protected virtual void OnInvalidate() { }
202 internal static ItemObject GetItemById(int id)
205 s_IdToItemTable.TryGetValue(id, out value);
209 internal static ItemObject GetItemByHandle(IntPtr handle)
212 s_HandleToItemTable.TryGetValue(handle, out value);
216 void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
218 Deleted?.Invoke(this, EventArgs.Empty);
220 if (s_IdToItemTable.ContainsKey(Id))
222 s_IdToItemTable.Remove(Id);
224 if (s_HandleToItemTable.ContainsKey(_handle))
226 s_HandleToItemTable.Remove(_handle);
228 _partContents.Clear();
229 _handle = IntPtr.Zero;
232 void UnsetDeleteCallback()
234 Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
237 void SetDeleteCallback()
239 if (Handle != IntPtr.Zero)
240 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
243 static int GetNextId()