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;
34 Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb> _signalDatas = new Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb>();
35 EvasObject _trackObject = null;
38 /// Creates and initializes a new instance of ItemObject class.
40 /// <param name="handle">IntPtr</param>
41 protected ItemObject(IntPtr handle)
43 _deleteCallback = DeleteCallbackHandler;
45 s_IdToItemTable[Id] = this;
49 // C# Finalizer was called on GC thread
50 // So, We can't access to EFL object
51 // And When Finalizer was called, Field can be already released.
54 // if (Handle != IntPtr.Zero)
55 // Interop.Elementary.elm_object_item_del(Handle);
59 /// Gets the id of item object
61 public int Id { get; private set; }
64 /// Sets or gets whether the item object is enabled
68 get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
69 set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
73 /// Gets track object of the item.
75 public EvasObject TrackObject
79 if (_trackObject == null)
80 _trackObject = new ItemEvasObject(Handle);
85 internal IntPtr Handle
96 if (_handle != IntPtr.Zero)
98 UnsetDeleteCallback();
102 s_HandleToItemTable[Handle] = this;
107 /// Deleted will be triggered when the item object is deleted
109 public event EventHandler Deleted;
112 /// Delete the item object
116 Interop.Elementary.elm_object_item_del(Handle);
117 _handle = IntPtr.Zero;
121 /// Set a content of an object item and delete old content
123 /// <param name="part">The content part name (null for the default content)</param>
124 /// <param name="content">The content of the object item</param>
125 public void SetPartContent(string part, EvasObject content)
127 SetPartContent(part, content, false);
131 /// Set a content of an object item
133 /// <param name="part">The content part name (null for the default content)</param>
134 /// <param name="content">The content of the object item</param>
135 /// <param name="preserveOldContent">judge whether delete old content</param>
136 public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
138 IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
139 if (oldContent != IntPtr.Zero && !preserveOldContent)
141 Interop.Evas.evas_object_del(oldContent);
143 Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
144 _partContents[part ?? "__default__"] = content;
148 /// Set a label of an object item
150 /// <param name="part">The text part name (null for the default label)</param>
151 /// <param name="text">Text of the label</param>
152 public void SetPartText(string part, string text)
154 Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
158 /// Gets a label of an object item
160 /// <param name="part">The text part name (null for the default label)</param>
161 /// <returns></returns>
162 public string GetPartText(string part)
164 return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
168 /// Sets color of an object item
170 /// <param name="part">The text part name (null for the default label)</param>
171 /// <param name="color">the color</param>
172 public void SetPartColor(string part, Color color)
174 Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
175 color.G * color.A / 255,
176 color.B * color.A / 255,
181 /// Gets color of an object item
183 /// <param name="part">The text part name (null for the default label)</param>
184 /// <returns>the color of object item</returns>
185 public Color GetPartColor(string part)
188 Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
189 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
193 /// Deletes color of an object item
195 /// <param name="part">The text part name</param>
196 public void DeletePartColor(string part)
198 Interop.Elementary.elm_object_item_color_class_del(Handle, part);
202 /// Add a function for a signal emitted by object item edje.
204 /// <param name="emission">The signal's name.</param>
205 /// <param name="source">The signal's source.</param>
206 /// <param name="func">The function to be executed when the signal is emitted.</param>
207 public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
209 if (emission != null && source != null && func != null)
211 var signalData = new SignalData(emission, source, func);
212 if (!_signalDatas.ContainsKey(signalData))
214 var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
218 Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
224 /// Remove a signal-triggered function from a object item edje object.
226 /// <param name="emission">The signal's name.</param>
227 /// <param name="source">The signal's source.</param>
228 /// <param name="func">The function to be executed when the signal is emitted.</param>
229 public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
231 if (emission != null && source != null && func != null)
233 var signalData = new SignalData(emission, source, func);
235 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
236 _signalDatas.TryGetValue(signalData, out signalCallback);
238 if (signalCallback != null)
240 Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
241 _signalDatas.Remove(signalData);
247 /// Send a signal to the edje object of the widget item.
249 /// <param name="emission">The signal's name.</param>
250 /// <param name="source">The signal's source.</param>
251 public void EmitSignal(string emission, string source)
253 Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
257 /// Gets the handle of object item
259 /// <param name="obj">ItemObject</param>
260 public static implicit operator IntPtr(ItemObject obj)
268 /// OnInvalidate of object item
270 protected virtual void OnInvalidate() { }
272 internal static ItemObject GetItemById(int id)
275 s_IdToItemTable.TryGetValue(id, out value);
279 internal static ItemObject GetItemByHandle(IntPtr handle)
282 s_HandleToItemTable.TryGetValue(handle, out value);
286 void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
288 Deleted?.Invoke(this, EventArgs.Empty);
290 if (s_IdToItemTable.ContainsKey(Id))
292 s_IdToItemTable.Remove(Id);
294 if (s_HandleToItemTable.ContainsKey(_handle))
296 s_HandleToItemTable.Remove(_handle);
298 _partContents.Clear();
299 _handle = IntPtr.Zero;
302 void UnsetDeleteCallback()
304 Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
307 void SetDeleteCallback()
309 if (Handle != IntPtr.Zero)
310 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
313 static int GetNextId()
320 public string Emission { get; set; }
321 public string Source { get; set; }
322 public Func<string, string, bool> Func { get; set; }
324 public SignalData(string emission, string source, Func<string, string, bool> func)
331 public override bool Equals(object obj)
333 SignalData s = obj as SignalData;
338 return (Emission == s.Emission) && (Source == s.Source) && (Func == s.Func);
341 public override int GetHashCode()
343 int hashCode = Emission.GetHashCode();
344 hashCode ^= Source.GetHashCode();
345 hashCode ^= Func.GetHashCode();
350 class ItemEvasObject : EvasObject
352 IntPtr _parent = IntPtr.Zero;
354 public ItemEvasObject(IntPtr parent) : base()
360 protected override IntPtr CreateHandle(EvasObject parent)
362 return Interop.Elementary.elm_object_item_track(_parent);