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 /// <since_tizen> preview </since_tizen>
26 public class ItemObject
28 private static Dictionary<int, ItemObject> s_IdToItemTable = new Dictionary<int, ItemObject>();
29 private static Dictionary<IntPtr, ItemObject> s_HandleToItemTable = new Dictionary<IntPtr, ItemObject>();
30 private static int s_globalId = 0;
32 readonly Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
33 Interop.Evas.SmartCallback _deleteCallback;
34 IntPtr _handle = IntPtr.Zero;
35 Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb> _signalDatas = new Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb>();
36 EvasObject _trackObject = null;
39 /// Creates and initializes a new instance of ItemObject class.
41 /// <param name="handle">IntPtr</param>
42 /// <since_tizen> preview </since_tizen>
43 protected ItemObject(IntPtr handle)
45 _deleteCallback = DeleteCallbackHandler;
47 s_IdToItemTable[Id] = this;
51 // C# Finalizer was called on GC thread
52 // So, We can't access to EFL object
53 // And When Finalizer was called, Field can be already released.
56 // if (Handle != IntPtr.Zero)
57 // Interop.Elementary.elm_object_item_del(Handle);
61 /// Gets the id of item object
63 /// <since_tizen> preview </since_tizen>
64 public int Id { get; private set; }
67 /// Sets or gets whether the item object is enabled
69 /// <since_tizen> preview </since_tizen>
72 get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
73 set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
77 /// Gets track object of the item.
79 /// <since_tizen> preview </since_tizen>
80 public EvasObject TrackObject
84 if (_trackObject == null)
85 _trackObject = new ItemEvasObject(Handle);
91 /// Sets or gets the style of the Item.
93 /// <since_tizen> preview </since_tizen>
94 public virtual string Style
98 return Interop.Elementary.elm_object_item_style_get(Handle);
102 Interop.Elementary.elm_object_item_style_set(Handle, value);
106 internal IntPtr Handle
114 if (_handle == value)
117 if (_handle != IntPtr.Zero)
119 UnsetDeleteCallback();
123 s_HandleToItemTable[Handle] = this;
128 /// Deleted will be triggered when the item object is deleted
130 /// <since_tizen> preview </since_tizen>
131 public event EventHandler Deleted;
134 /// Delete the item object
136 /// <since_tizen> preview </since_tizen>
139 Interop.Elementary.elm_object_item_del(Handle);
140 _handle = IntPtr.Zero;
144 /// Set a content of an object item and delete old content
146 /// <param name="part">The content part name (null for the default content)</param>
147 /// <param name="content">The content of the object item</param>
148 /// <since_tizen> preview </since_tizen>
149 public void SetPartContent(string part, EvasObject content)
151 SetPartContent(part, content, false);
155 /// Set a content of an object item
157 /// <param name="part">The content part name (null for the default content)</param>
158 /// <param name="content">The content of the object item</param>
159 /// <param name="preserveOldContent">judge whether delete old content</param>
160 /// <since_tizen> preview </since_tizen>
161 public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
163 IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
164 if (oldContent != IntPtr.Zero && !preserveOldContent)
166 Interop.Evas.evas_object_del(oldContent);
168 Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
169 _partContents[part ?? "__default__"] = content;
173 /// Set a label of an object item
175 /// <param name="part">The text part name (null for the default label)</param>
176 /// <param name="text">Text of the label</param>
177 /// <since_tizen> preview </since_tizen>
178 public void SetPartText(string part, string text)
180 Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
184 /// Gets a label of an object item
186 /// <param name="part">The text part name (null for the default label)</param>
187 /// <returns></returns>
188 /// <since_tizen> preview </since_tizen>
189 public string GetPartText(string part)
191 return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
195 /// Sets color of an object item
197 /// <param name="part">The text part name (null for the default label)</param>
198 /// <param name="color">the color</param>
199 /// <since_tizen> preview </since_tizen>
200 public void SetPartColor(string part, Color color)
202 Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
203 color.G * color.A / 255,
204 color.B * color.A / 255,
209 /// Gets color of an object item
211 /// <param name="part">The text part name (null for the default label)</param>
212 /// <returns>the color of object item</returns>
213 /// <since_tizen> preview </since_tizen>
214 public Color GetPartColor(string part)
217 Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
218 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
222 /// Deletes color of an object item
224 /// <param name="part">The text part name</param>
225 /// <since_tizen> preview </since_tizen>
226 public void DeletePartColor(string part)
228 Interop.Elementary.elm_object_item_color_class_del(Handle, part);
232 /// Add a function for a signal emitted by object item edje.
234 /// <param name="emission">The signal's name.</param>
235 /// <param name="source">The signal's source.</param>
236 /// <param name="func">The function to be executed when the signal is emitted.</param>
237 /// <since_tizen> preview </since_tizen>
238 public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
240 if (emission != null && source != null && func != null)
242 var signalData = new SignalData(emission, source, func);
243 if (!_signalDatas.ContainsKey(signalData))
245 var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
249 Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
255 /// Remove a signal-triggered function from a object item edje object.
257 /// <param name="emission">The signal's name.</param>
258 /// <param name="source">The signal's source.</param>
259 /// <param name="func">The function to be executed when the signal is emitted.</param>
260 /// <since_tizen> preview </since_tizen>
261 public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
263 if (emission != null && source != null && func != null)
265 var signalData = new SignalData(emission, source, func);
267 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
268 _signalDatas.TryGetValue(signalData, out signalCallback);
270 if (signalCallback != null)
272 Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
273 _signalDatas.Remove(signalData);
279 /// Send a signal to the edje object of the widget item.
281 /// <param name="emission">The signal's name.</param>
282 /// <param name="source">The signal's source.</param>
283 /// <since_tizen> preview </since_tizen>
284 public void EmitSignal(string emission, string source)
286 Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
290 /// Gets the handle of object item
292 /// <param name="obj">ItemObject</param>
293 /// <since_tizen> preview </since_tizen>
294 public static implicit operator IntPtr(ItemObject obj)
302 /// OnInvalidate of object item
304 /// <since_tizen> preview </since_tizen>
305 protected virtual void OnInvalidate() { }
307 internal static ItemObject GetItemById(int id)
310 s_IdToItemTable.TryGetValue(id, out value);
314 internal static ItemObject GetItemByHandle(IntPtr handle)
317 s_HandleToItemTable.TryGetValue(handle, out value);
321 void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
323 Deleted?.Invoke(this, EventArgs.Empty);
325 if (s_IdToItemTable.ContainsKey(Id))
327 s_IdToItemTable.Remove(Id);
329 if (s_HandleToItemTable.ContainsKey(_handle))
331 s_HandleToItemTable.Remove(_handle);
333 _partContents.Clear();
334 _handle = IntPtr.Zero;
337 void UnsetDeleteCallback()
339 Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
342 void SetDeleteCallback()
344 if (Handle != IntPtr.Zero)
345 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
348 static int GetNextId()
355 public string Emission { get; set; }
356 public string Source { get; set; }
357 public Func<string, string, bool> Func { get; set; }
359 public SignalData(string emission, string source, Func<string, string, bool> func)
367 /// Indicates whether this instance and a specified object are equal.
369 /// <param name="obj">The object to compare with the current instance.</param>
371 /// true if obj and this instance are the same type and represent the same value.
372 /// otherwise, false.
374 public override bool Equals(object obj)
376 SignalData s = obj as SignalData;
381 return (Emission == s.Emission) && (Source == s.Source) && (Func == s.Func);
384 public override int GetHashCode()
386 int hashCode = Emission.GetHashCode();
387 hashCode ^= Source.GetHashCode();
388 hashCode ^= Func.GetHashCode();
393 class ItemEvasObject : EvasObject
395 IntPtr _parent = IntPtr.Zero;
398 /// Creates and initializes a new instance of ItemEvasObject class.
400 /// <param name="parent">IntPtr</param>
401 public ItemEvasObject(IntPtr parent) : base()
408 /// Creates a widget handle.
410 /// <param name="parent">Parent EvasObject</param>
411 /// <returns>Handle IntPtr</returns>
412 protected override IntPtr CreateHandle(EvasObject parent)
414 return Interop.Elementary.elm_object_item_track(_parent);