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);
86 /// Sets or gets the style of the Item.
92 return Interop.Elementary.elm_object_item_style_get(Handle);
96 Interop.Elementary.elm_object_item_style_set(Handle, value);
100 internal IntPtr Handle
108 if (_handle == value)
111 if (_handle != IntPtr.Zero)
113 UnsetDeleteCallback();
117 s_HandleToItemTable[Handle] = this;
122 /// Deleted will be triggered when the item object is deleted
124 public event EventHandler Deleted;
127 /// Delete the item object
131 Interop.Elementary.elm_object_item_del(Handle);
132 _handle = IntPtr.Zero;
136 /// Set a content of an object item and delete old content
138 /// <param name="part">The content part name (null for the default content)</param>
139 /// <param name="content">The content of the object item</param>
140 public void SetPartContent(string part, EvasObject content)
142 SetPartContent(part, content, false);
146 /// Set a content of an object item
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 /// <param name="preserveOldContent">judge whether delete old content</param>
151 public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
153 IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
154 if (oldContent != IntPtr.Zero && !preserveOldContent)
156 Interop.Evas.evas_object_del(oldContent);
158 Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
159 _partContents[part ?? "__default__"] = content;
163 /// Set a label of an object item
165 /// <param name="part">The text part name (null for the default label)</param>
166 /// <param name="text">Text of the label</param>
167 public void SetPartText(string part, string text)
169 Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
173 /// Gets a label of an object item
175 /// <param name="part">The text part name (null for the default label)</param>
176 /// <returns></returns>
177 public string GetPartText(string part)
179 return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
183 /// Sets color of an object item
185 /// <param name="part">The text part name (null for the default label)</param>
186 /// <param name="color">the color</param>
187 public void SetPartColor(string part, Color color)
189 Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
190 color.G * color.A / 255,
191 color.B * color.A / 255,
196 /// Gets color of an object item
198 /// <param name="part">The text part name (null for the default label)</param>
199 /// <returns>the color of object item</returns>
200 public Color GetPartColor(string part)
203 Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
204 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
208 /// Deletes color of an object item
210 /// <param name="part">The text part name</param>
211 public void DeletePartColor(string part)
213 Interop.Elementary.elm_object_item_color_class_del(Handle, part);
217 /// Add a function for a signal emitted by object item edje.
219 /// <param name="emission">The signal's name.</param>
220 /// <param name="source">The signal's source.</param>
221 /// <param name="func">The function to be executed when the signal is emitted.</param>
222 public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
224 if (emission != null && source != null && func != null)
226 var signalData = new SignalData(emission, source, func);
227 if (!_signalDatas.ContainsKey(signalData))
229 var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
233 Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
239 /// Remove a signal-triggered function from a object item edje object.
241 /// <param name="emission">The signal's name.</param>
242 /// <param name="source">The signal's source.</param>
243 /// <param name="func">The function to be executed when the signal is emitted.</param>
244 public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
246 if (emission != null && source != null && func != null)
248 var signalData = new SignalData(emission, source, func);
250 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
251 _signalDatas.TryGetValue(signalData, out signalCallback);
253 if (signalCallback != null)
255 Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
256 _signalDatas.Remove(signalData);
262 /// Send a signal to the edje object of the widget item.
264 /// <param name="emission">The signal's name.</param>
265 /// <param name="source">The signal's source.</param>
266 public void EmitSignal(string emission, string source)
268 Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
272 /// Gets the handle of object item
274 /// <param name="obj">ItemObject</param>
275 public static implicit operator IntPtr(ItemObject obj)
283 /// OnInvalidate of object item
285 protected virtual void OnInvalidate() { }
287 internal static ItemObject GetItemById(int id)
290 s_IdToItemTable.TryGetValue(id, out value);
294 internal static ItemObject GetItemByHandle(IntPtr handle)
297 s_HandleToItemTable.TryGetValue(handle, out value);
301 void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
303 Deleted?.Invoke(this, EventArgs.Empty);
305 if (s_IdToItemTable.ContainsKey(Id))
307 s_IdToItemTable.Remove(Id);
309 if (s_HandleToItemTable.ContainsKey(_handle))
311 s_HandleToItemTable.Remove(_handle);
313 _partContents.Clear();
314 _handle = IntPtr.Zero;
317 void UnsetDeleteCallback()
319 Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
322 void SetDeleteCallback()
324 if (Handle != IntPtr.Zero)
325 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
328 static int GetNextId()
335 public string Emission { get; set; }
336 public string Source { get; set; }
337 public Func<string, string, bool> Func { get; set; }
339 public SignalData(string emission, string source, Func<string, string, bool> func)
347 /// Indicates whether this instance and a specified object are equal.
349 /// <param name="obj">The object to compare with the current instance.</param>
351 /// true if obj and this instance are the same type and represent the same value.
352 /// otherwise, false.
354 public override bool Equals(object obj)
356 SignalData s = obj as SignalData;
361 return (Emission == s.Emission) && (Source == s.Source) && (Func == s.Func);
364 public override int GetHashCode()
366 int hashCode = Emission.GetHashCode();
367 hashCode ^= Source.GetHashCode();
368 hashCode ^= Func.GetHashCode();
373 class ItemEvasObject : EvasObject
375 IntPtr _parent = IntPtr.Zero;
378 /// Creates and initializes a new instance of ItemEvasObject class.
380 /// <param name="parent">IntPtr</param>
381 public ItemEvasObject(IntPtr parent) : base()
388 /// Creates a widget handle.
390 /// <param name="parent">Parent EvasObject</param>
391 /// <returns>Handle IntPtr</returns>
392 protected override IntPtr CreateHandle(EvasObject parent)
394 return Interop.Elementary.elm_object_item_track(_parent);