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 /// Gets the parent object for ItemObject.
41 /// <since_tizen> preview </since_tizen>
42 public EvasObject Parent { get; internal set; }
45 /// Creates and initializes a new instance of the ItemObject class.
47 /// <param name="handle">IntPtr</param>
48 /// <since_tizen> preview </since_tizen>
49 protected ItemObject(IntPtr handle)
51 _deleteCallback = DeleteCallbackHandler;
53 s_IdToItemTable[Id] = this;
59 /// Creates and initializes a new instance of the ItemObject class with parent object.
61 /// <param name="handle">IntPtr</param>
62 /// <param name="parent">Parent EvasObject</param>
63 /// <since_tizen> preview </since_tizen>
64 protected ItemObject(IntPtr handle, EvasObject parent)
66 _deleteCallback = DeleteCallbackHandler;
68 s_IdToItemTable[Id] = this;
73 // C# Finalizer was called on GC thread
74 // So, We can't access to EFL object
75 // And When Finalizer was called, Field can be already released.
78 // if (Handle != IntPtr.Zero)
79 // Interop.Elementary.elm_object_item_del(Handle);
83 /// Gets the ID of the item object.
85 /// <since_tizen> preview </since_tizen>
86 public int Id { get; private set; }
89 /// Sets or gets whether the item object is enabled.
91 /// <since_tizen> preview </since_tizen>
94 get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
95 set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
99 /// Gets the track object of the item.
101 /// <since_tizen> preview </since_tizen>
102 public EvasObject TrackObject
106 if (_trackObject == null || Interop.Elementary.elm_object_item_track_get(Handle) == 0)
108 _trackObject = new ItemEvasObject(Handle);
115 /// Sets or gets the style of the item.
117 /// <since_tizen> preview </since_tizen>
118 public virtual string Style
122 return Interop.Elementary.elm_object_item_style_get(Handle);
126 Interop.Elementary.elm_object_item_style_set(Handle, value);
130 internal IntPtr Handle
138 if (_handle == value)
141 if (_handle != IntPtr.Zero)
143 UnsetDeleteCallback();
147 s_HandleToItemTable[Handle] = this;
148 if (_handle != IntPtr.Zero)
150 Elementary.SendItemObjectRealized(this);
156 /// Deleted will be triggered when the item object is deleted.
158 /// <since_tizen> preview </since_tizen>
159 public event EventHandler Deleted;
162 /// Deletes the item object.
164 /// <since_tizen> preview </since_tizen>
167 Interop.Elementary.elm_object_item_del(Handle);
168 _handle = IntPtr.Zero;
172 /// Sets a content of an object item and deletes the old content.
174 /// <param name="part">The content part name (null for the default content).</param>
175 /// <param name="content">The content of the object item.</param>
176 /// <since_tizen> preview </since_tizen>
177 public void SetPartContent(string part, EvasObject content)
179 SetPartContent(part, content, false);
183 /// Sets a content of the object item.
185 /// <param name="part">The content part name (null for the default content)</param>
186 /// <param name="content">The content of the object item.</param>
187 /// <param name="preserveOldContent">Judge whether to delete the old content.</param>
188 /// <since_tizen> preview </since_tizen>
189 public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
191 IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
192 if (oldContent != IntPtr.Zero && !preserveOldContent)
194 Interop.Evas.evas_object_del(oldContent);
196 Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
197 _partContents[part ?? "__default__"] = content;
201 /// Sets the label of the object item.
203 /// <param name="part">The text part name (null for the default label).</param>
204 /// <param name="text">Text of the label.</param>
205 /// <since_tizen> preview </since_tizen>
206 public void SetPartText(string part, string text)
208 Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
212 /// Gets the label of the object item.
214 /// <param name="part">The text part name (null for the default label).</param>
215 /// <returns></returns>
216 /// <since_tizen> preview </since_tizen>
217 public string GetPartText(string part)
219 return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
223 /// Sets the color of the object item.
225 /// <param name="part">The text part name (null for the default label).</param>
226 /// <param name="color">The color.</param>
227 /// <since_tizen> preview </since_tizen>
228 public void SetPartColor(string part, Color color)
230 Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
231 color.G * color.A / 255,
232 color.B * color.A / 255,
237 /// Gets the color of the object item.
239 /// <param name="part">The text part name (null for the default label).</param>
240 /// <returns>The color of an object item.</returns>
241 /// <since_tizen> preview </since_tizen>
242 public Color GetPartColor(string part)
245 Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
246 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
250 /// Deletes the color of the object item.
252 /// <param name="part">The text part name.</param>
253 /// <since_tizen> preview </since_tizen>
254 public void DeletePartColor(string part)
256 Interop.Elementary.elm_object_item_color_class_del(Handle, part);
260 /// Adds a function for a signal emitted by the object item edje.
262 /// <param name="emission">The signal's name.</param>
263 /// <param name="source">The signal's source.</param>
264 /// <param name="func">The function to be executed when the signal is emitted.</param>
265 /// <since_tizen> preview </since_tizen>
266 public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
268 if (emission != null && source != null && func != null)
270 var signalData = new SignalData(emission, source, func);
271 if (!_signalDatas.ContainsKey(signalData))
273 var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
277 Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
283 /// Removes a signal-triggered function from the object item edje object.
285 /// <param name="emission">The signal's name.</param>
286 /// <param name="source">The signal's source.</param>
287 /// <param name="func">The function to be executed when the signal is emitted.</param>
288 /// <since_tizen> preview </since_tizen>
289 public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
291 if (emission != null && source != null && func != null)
293 var signalData = new SignalData(emission, source, func);
295 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
296 _signalDatas.TryGetValue(signalData, out signalCallback);
298 if (signalCallback != null)
300 Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
301 _signalDatas.Remove(signalData);
307 /// Send a signal to the edje object of the widget item.
309 /// <param name="emission">The signal's name.</param>
310 /// <param name="source">The signal's source.</param>
311 /// <since_tizen> preview </since_tizen>
312 public void EmitSignal(string emission, string source)
314 Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
318 /// Gets the handle of the object item.
320 /// <param name="obj">ItemObject</param>
321 /// <since_tizen> preview </since_tizen>
322 public static implicit operator IntPtr(ItemObject obj)
330 /// OnInvalidate of the object item.
332 /// <since_tizen> preview </since_tizen>
333 protected virtual void OnInvalidate() { }
335 internal static ItemObject GetItemById(int id)
338 s_IdToItemTable.TryGetValue(id, out value);
342 internal static ItemObject GetItemByHandle(IntPtr handle)
345 s_HandleToItemTable.TryGetValue(handle, out value);
349 void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
351 Deleted?.Invoke(this, EventArgs.Empty);
354 if (s_IdToItemTable.ContainsKey(Id))
356 s_IdToItemTable.Remove(Id);
358 if (s_HandleToItemTable.ContainsKey(_handle))
360 s_HandleToItemTable.Remove(_handle);
362 _partContents.Clear();
363 _handle = IntPtr.Zero;
366 void UnsetDeleteCallback()
368 Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
371 void SetDeleteCallback()
373 if (Handle != IntPtr.Zero)
374 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
377 static int GetNextId()
382 class SignalData : IEquatable<SignalData>
384 public string Emission { get; set; }
385 public string Source { get; set; }
386 public Func<string, string, bool> Func { get; set; }
388 public SignalData(string emission, string source, Func<string, string, bool> func)
395 public override bool Equals(object other)
397 return Equals(other as SignalData);
400 public bool Equals(SignalData other) {
405 return (Emission == other.Emission) && (Source == other.Source) && (Func == other.Func);
408 public override int GetHashCode()
410 int hashCode = Emission.GetHashCode();
411 hashCode ^= Source.GetHashCode();
412 hashCode ^= Func.GetHashCode();
417 class ItemEvasObject : EvasObject
419 IntPtr _trackHandle = IntPtr.Zero;
421 public ItemEvasObject(IntPtr parent)
423 _trackHandle = Interop.Elementary.elm_object_item_track(parent);
424 if (_trackHandle != IntPtr.Zero)
430 protected override IntPtr CreateHandle(EvasObject parent)