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 the 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 the 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 the 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 the track object of the item.
79 /// <since_tizen> preview </since_tizen>
80 public EvasObject TrackObject
84 if (_trackObject == null || Interop.Elementary.elm_object_item_track_get(Handle) == 0)
86 _trackObject = new ItemEvasObject(Handle);
93 /// Sets or gets the style of the item.
95 /// <since_tizen> preview </since_tizen>
96 public virtual string Style
100 return Interop.Elementary.elm_object_item_style_get(Handle);
104 Interop.Elementary.elm_object_item_style_set(Handle, value);
108 internal IntPtr Handle
116 if (_handle == value)
119 if (_handle != IntPtr.Zero)
121 UnsetDeleteCallback();
125 s_HandleToItemTable[Handle] = this;
130 /// Deleted will be triggered when the item object is deleted.
132 /// <since_tizen> preview </since_tizen>
133 public event EventHandler Deleted;
136 /// Deletes the item object.
138 /// <since_tizen> preview </since_tizen>
141 Interop.Elementary.elm_object_item_del(Handle);
142 _handle = IntPtr.Zero;
146 /// Sets a content of an object item and deletes the old content.
148 /// <param name="part">The content part name (null for the default content).</param>
149 /// <param name="content">The content of the object item.</param>
150 /// <since_tizen> preview </since_tizen>
151 public void SetPartContent(string part, EvasObject content)
153 SetPartContent(part, content, false);
157 /// Sets a content of the object item.
159 /// <param name="part">The content part name (null for the default content)</param>
160 /// <param name="content">The content of the object item.</param>
161 /// <param name="preserveOldContent">Judge whether to delete the old content.</param>
162 /// <since_tizen> preview </since_tizen>
163 public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
165 IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
166 if (oldContent != IntPtr.Zero && !preserveOldContent)
168 Interop.Evas.evas_object_del(oldContent);
170 Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
171 _partContents[part ?? "__default__"] = content;
175 /// Sets the label of the object item.
177 /// <param name="part">The text part name (null for the default label).</param>
178 /// <param name="text">Text of the label.</param>
179 /// <since_tizen> preview </since_tizen>
180 public void SetPartText(string part, string text)
182 Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
186 /// Gets the label of the object item.
188 /// <param name="part">The text part name (null for the default label).</param>
189 /// <returns></returns>
190 /// <since_tizen> preview </since_tizen>
191 public string GetPartText(string part)
193 return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
197 /// Sets the color of the object item.
199 /// <param name="part">The text part name (null for the default label).</param>
200 /// <param name="color">The color.</param>
201 /// <since_tizen> preview </since_tizen>
202 public void SetPartColor(string part, Color color)
204 Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
205 color.G * color.A / 255,
206 color.B * color.A / 255,
211 /// Gets the color of the object item.
213 /// <param name="part">The text part name (null for the default label).</param>
214 /// <returns>The color of an object item.</returns>
215 /// <since_tizen> preview </since_tizen>
216 public Color GetPartColor(string part)
219 Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
220 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
224 /// Deletes the color of the object item.
226 /// <param name="part">The text part name.</param>
227 /// <since_tizen> preview </since_tizen>
228 public void DeletePartColor(string part)
230 Interop.Elementary.elm_object_item_color_class_del(Handle, part);
234 /// Adds a function for a signal emitted by the object item edje.
236 /// <param name="emission">The signal's name.</param>
237 /// <param name="source">The signal's source.</param>
238 /// <param name="func">The function to be executed when the signal is emitted.</param>
239 /// <since_tizen> preview </since_tizen>
240 public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
242 if (emission != null && source != null && func != null)
244 var signalData = new SignalData(emission, source, func);
245 if (!_signalDatas.ContainsKey(signalData))
247 var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
251 Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
257 /// Removes a signal-triggered function from the object item edje object.
259 /// <param name="emission">The signal's name.</param>
260 /// <param name="source">The signal's source.</param>
261 /// <param name="func">The function to be executed when the signal is emitted.</param>
262 /// <since_tizen> preview </since_tizen>
263 public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
265 if (emission != null && source != null && func != null)
267 var signalData = new SignalData(emission, source, func);
269 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
270 _signalDatas.TryGetValue(signalData, out signalCallback);
272 if (signalCallback != null)
274 Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
275 _signalDatas.Remove(signalData);
281 /// Send a signal to the edje object of the widget item.
283 /// <param name="emission">The signal's name.</param>
284 /// <param name="source">The signal's source.</param>
285 /// <since_tizen> preview </since_tizen>
286 public void EmitSignal(string emission, string source)
288 Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
292 /// Gets the handle of the object item.
294 /// <param name="obj">ItemObject</param>
295 /// <since_tizen> preview </since_tizen>
296 public static implicit operator IntPtr(ItemObject obj)
304 /// OnInvalidate of the object item.
306 /// <since_tizen> preview </since_tizen>
307 protected virtual void OnInvalidate() { }
309 internal static ItemObject GetItemById(int id)
312 s_IdToItemTable.TryGetValue(id, out value);
316 internal static ItemObject GetItemByHandle(IntPtr handle)
319 s_HandleToItemTable.TryGetValue(handle, out value);
323 void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
325 Deleted?.Invoke(this, EventArgs.Empty);
327 if (s_IdToItemTable.ContainsKey(Id))
329 s_IdToItemTable.Remove(Id);
331 if (s_HandleToItemTable.ContainsKey(_handle))
333 s_HandleToItemTable.Remove(_handle);
335 _partContents.Clear();
336 _handle = IntPtr.Zero;
339 void UnsetDeleteCallback()
341 Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
344 void SetDeleteCallback()
346 if (Handle != IntPtr.Zero)
347 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
350 static int GetNextId()
357 public string Emission { get; set; }
358 public string Source { get; set; }
359 public Func<string, string, bool> Func { get; set; }
361 public SignalData(string emission, string source, Func<string, string, bool> func)
369 /// Indicates whether this instance and a specified object are equal.
371 /// <param name="obj">The object to compare with the current instance.</param>
373 /// true if the object and this instance are of the same type and represent the same value,
376 public override bool Equals(object obj)
378 SignalData s = obj as SignalData;
383 return (Emission == s.Emission) && (Source == s.Source) && (Func == s.Func);
386 public override int GetHashCode()
388 int hashCode = Emission.GetHashCode();
389 hashCode ^= Source.GetHashCode();
390 hashCode ^= Func.GetHashCode();
395 class ItemEvasObject : EvasObject
397 IntPtr _trackHandle = IntPtr.Zero;
400 /// Creates and initializes a new instance of the ItemEvasObject class.
402 /// <param name="parent">IntPtr</param>
403 public ItemEvasObject(IntPtr parent) : base()
405 _trackHandle = Interop.Elementary.elm_object_item_track(parent);
406 if (_trackHandle != IntPtr.Zero)
413 /// Creates a widget handle.
415 /// <param name="parent">Parent EvasObject.</param>
416 /// <returns>Handle IntPtr.</returns>
417 protected override IntPtr CreateHandle(EvasObject parent)