Merge "[Multimedia] Modified the Radio to disable sound session compatibility."
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / ItemObject.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// The ItemObject is used to manage item object
24     /// </summary>
25     public class ItemObject
26     {
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;
30
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;
36
37         /// <summary>
38         /// Creates and initializes a new instance of ItemObject class.
39         /// </summary>
40         /// <param name="handle">IntPtr</param>
41         protected ItemObject(IntPtr handle)
42         {
43             _deleteCallback = DeleteCallbackHandler;
44             Id = GetNextId();
45             s_IdToItemTable[Id] = this;
46             Handle = handle;
47         }
48
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.
52         //~ItemObject()
53         //{
54         //    if (Handle != IntPtr.Zero)
55         //        Interop.Elementary.elm_object_item_del(Handle);
56         //}
57
58         /// <summary>
59         /// Gets the id of item object
60         /// </summary>
61         public int Id { get; private set; }
62
63         /// <summary>
64         /// Sets or gets whether the item object is enabled
65         /// </summary>
66         public bool IsEnabled
67         {
68             get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
69             set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
70         }
71
72         /// <summary>
73         /// Gets track object of the item.
74         /// </summary>
75         public EvasObject TrackObject
76         {
77             get
78             {
79                 if (_trackObject == null)
80                     _trackObject = new ItemEvasObject(Handle);
81                 return _trackObject;
82             }
83         }
84
85         /// <summary>
86         /// Sets or gets the style of the Item.
87         /// </summary>
88         public string Style
89         {
90             get
91             {
92                 return Interop.Elementary.elm_object_item_style_get(Handle);
93             }
94             set
95             {
96                 Interop.Elementary.elm_object_item_style_set(Handle, value);
97             }
98         }
99
100         internal IntPtr Handle
101         {
102             get
103             {
104                 return _handle;
105             }
106             set
107             {
108                 if (_handle == value)
109                     return;
110
111                 if (_handle != IntPtr.Zero)
112                 {
113                     UnsetDeleteCallback();
114                 }
115                 _handle = value;
116                 SetDeleteCallback();
117                 s_HandleToItemTable[Handle] = this;
118             }
119         }
120
121         /// <summary>
122         /// Deleted will be triggered when the item object is deleted
123         /// </summary>
124         public event EventHandler Deleted;
125
126         /// <summary>
127         /// Delete the item object
128         /// </summary>
129         public void Delete()
130         {
131             Interop.Elementary.elm_object_item_del(Handle);
132             _handle = IntPtr.Zero;
133         }
134
135         /// <summary>
136         /// Set a content of an object item and delete old content
137         /// </summary>
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)
141         {
142             SetPartContent(part, content, false);
143         }
144
145         /// <summary>
146         /// Set a content of an object item
147         /// </summary>
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)
152         {
153             IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
154             if (oldContent != IntPtr.Zero && !preserveOldContent)
155             {
156                 Interop.Evas.evas_object_del(oldContent);
157             }
158             Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
159             _partContents[part ?? "__default__"] = content;
160         }
161
162         /// <summary>
163         /// Set a label of an object item
164         /// </summary>
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)
168         {
169             Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
170         }
171
172         /// <summary>
173         /// Gets a label of an object item
174         /// </summary>
175         /// <param name="part">The text part name (null for the default label)</param>
176         /// <returns></returns>
177         public string GetPartText(string part)
178         {
179             return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
180         }
181
182         /// <summary>
183         /// Sets color of an object item
184         /// </summary>
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)
188         {
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,
192                                                                               color.A);
193         }
194
195         /// <summary>
196         /// Gets color of an object item
197         /// </summary>
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)
201         {
202             int r, g, b, a;
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);
205         }
206
207         /// <summary>
208         /// Deletes color of an object item
209         /// </summary>
210         /// <param name="part">The text part name</param>
211         public void DeletePartColor(string part)
212         {
213             Interop.Elementary.elm_object_item_color_class_del(Handle, part);
214         }
215
216         /// <summary>
217         /// Add a function for a signal emitted by object item edje.
218         /// </summary>
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)
223         {
224             if (emission != null && source != null && func != null)
225             {
226                 var signalData = new SignalData(emission, source, func);
227                 if (!_signalDatas.ContainsKey(signalData))
228                 {
229                     var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
230                     {
231                         return func(e, s);
232                     });
233                     Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
234                 }
235             }
236         }
237
238         /// <summary>
239         /// Remove a signal-triggered function from a object item edje object.
240         /// </summary>
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)
245         {
246             if (emission != null && source != null && func != null)
247             {
248                 var signalData = new SignalData(emission, source, func);
249
250                 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
251                 _signalDatas.TryGetValue(signalData, out signalCallback);
252
253                 if (signalCallback != null)
254                 {
255                     Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
256                     _signalDatas.Remove(signalData);
257                 }
258             }
259         }
260
261         /// <summary>
262         /// Send a signal to the edje object of the widget item.
263         /// </summary>
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)
267         {
268             Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
269         }
270
271         /// <summary>
272         /// Gets the handle of object item
273         /// </summary>
274         /// <param name="obj">ItemObject</param>
275         public static implicit operator IntPtr(ItemObject obj)
276         {
277             if (obj == null)
278                 return IntPtr.Zero;
279             return obj.Handle;
280         }
281
282         /// <summary>
283         /// OnInvalidate of object item
284         /// </summary>
285         protected virtual void OnInvalidate() { }
286
287         internal static ItemObject GetItemById(int id)
288         {
289             ItemObject value;
290             s_IdToItemTable.TryGetValue(id, out value);
291             return value;
292         }
293
294         internal static ItemObject GetItemByHandle(IntPtr handle)
295         {
296             ItemObject value;
297             s_HandleToItemTable.TryGetValue(handle, out value);
298             return value;
299         }
300
301         void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
302         {
303             Deleted?.Invoke(this, EventArgs.Empty);
304             OnInvalidate();
305             if (s_IdToItemTable.ContainsKey(Id))
306             {
307                 s_IdToItemTable.Remove(Id);
308             }
309             if (s_HandleToItemTable.ContainsKey(_handle))
310             {
311                 s_HandleToItemTable.Remove(_handle);
312             }
313             _partContents.Clear();
314             _handle = IntPtr.Zero;
315         }
316
317         void UnsetDeleteCallback()
318         {
319             Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
320         }
321
322         void SetDeleteCallback()
323         {
324             if (Handle != IntPtr.Zero)
325                 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
326         }
327
328         static int GetNextId()
329         {
330             return s_globalId++;
331         }
332
333         class SignalData
334         {
335             public string Emission { get; set; }
336             public string Source { get; set; }
337             public Func<string, string, bool> Func { get; set; }
338
339             public SignalData(string emission, string source, Func<string, string, bool> func)
340             {
341                 Emission = emission;
342                 Source = source;
343                 Func = func;
344             }
345
346             /// <summary>
347             /// Indicates whether this instance and a specified object are equal.
348             /// </summary>
349             /// <param name="obj">The object to compare with the current instance.</param>
350             /// <returns>
351             /// true if obj and this instance are the same type and represent the same value.
352             /// otherwise, false.
353             /// </returns>
354             public override bool Equals(object obj)
355             {
356                 SignalData s = obj as SignalData;
357                 if (s == null)
358                 {
359                     return false;
360                 }
361                 return (Emission == s.Emission) && (Source == s.Source) && (Func == s.Func);
362             }
363
364             public override int GetHashCode()
365             {
366                 int hashCode = Emission.GetHashCode();
367                 hashCode ^= Source.GetHashCode();
368                 hashCode ^= Func.GetHashCode();
369                 return hashCode;
370             }
371         }
372
373         class ItemEvasObject : EvasObject
374         {
375             IntPtr _parent = IntPtr.Zero;
376
377             /// <summary>
378             /// Creates and initializes a new instance of ItemEvasObject class.
379             /// </summary>
380             /// <param name="parent">IntPtr</param>
381             public ItemEvasObject(IntPtr parent) : base()
382             {
383                 _parent = parent;
384                 Realize(null);
385             }
386
387             /// <summary>
388             /// Creates a widget handle.
389             /// </summary>
390             /// <param name="parent">Parent EvasObject</param>
391             /// <returns>Handle IntPtr</returns>
392             protected override IntPtr CreateHandle(EvasObject parent)
393             {
394                 return Interop.Elementary.elm_object_item_track(_parent);
395             }
396         }
397     }
398 }