[ACR-564] deprecate unused API
[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 the item object.
24     /// </summary>
25     /// <since_tizen> preview </since_tizen>
26     [Obsolete("This has been deprecated in API12")]
27     public class ItemObject
28     {
29         private static Dictionary<int, ItemObject> s_IdToItemTable = new Dictionary<int, ItemObject>();
30         private static Dictionary<IntPtr, ItemObject> s_HandleToItemTable = new Dictionary<IntPtr, ItemObject>();
31         private static int s_globalId = 0;
32
33         readonly Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
34         Interop.Evas.SmartCallback _deleteCallback;
35         IntPtr _handle = IntPtr.Zero;
36         Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb> _signalDatas = new Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb>();
37         EvasObject _trackObject = null;
38
39         /// <summary>
40         /// Gets the parent object for ItemObject.
41         /// </summary>
42         /// <since_tizen> preview </since_tizen>
43         [Obsolete("This has been deprecated in API12")]
44         public EvasObject Parent { get; internal set; }
45
46         /// <summary>
47         /// Creates and initializes a new instance of the ItemObject class.
48         /// </summary>
49         /// <param name="handle">IntPtr</param>
50         /// <since_tizen> preview </since_tizen>
51         [Obsolete("This has been deprecated in API12")]
52         protected ItemObject(IntPtr handle)
53         {
54             _deleteCallback = DeleteCallbackHandler;
55             Id = GetNextId();
56             s_IdToItemTable[Id] = this;
57             Parent = null;
58             Handle = handle;
59         }
60
61         /// <summary>
62         /// Creates and initializes a new instance of the ItemObject class with parent object.
63         /// </summary>
64         /// <param name="handle">IntPtr</param>
65         /// <param name="parent">Parent EvasObject</param>
66         /// <since_tizen> preview </since_tizen>
67         [Obsolete("This has been deprecated in API12")]
68         protected ItemObject(IntPtr handle, EvasObject parent)
69         {
70             _deleteCallback = DeleteCallbackHandler;
71             Id = GetNextId();
72             s_IdToItemTable[Id] = this;
73             Parent = parent;
74             Handle = handle;
75         }
76
77         // C# Finalizer was called on GC thread
78         // So, We can't access to EFL object
79         // And When Finalizer was called, Field can be already released.
80         //~ItemObject()
81         //{
82         //    if (Handle != IntPtr.Zero)
83         //        Interop.Elementary.elm_object_item_del(Handle);
84         //}
85
86         /// <summary>
87         /// Gets the ID of the item object.
88         /// </summary>
89         /// <since_tizen> preview </since_tizen>
90         [Obsolete("This has been deprecated in API12")]
91         public int Id { get; private set; }
92
93         /// <summary>
94         /// Sets or gets whether the item object is enabled.
95         /// </summary>
96         /// <since_tizen> preview </since_tizen>
97         [Obsolete("This has been deprecated in API12")]
98         public bool IsEnabled
99         {
100             get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
101             set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
102         }
103
104         /// <summary>
105         /// Gets the track object of the item.
106         /// </summary>
107         /// <since_tizen> preview </since_tizen>
108         [Obsolete("This has been deprecated in API12")]
109         public EvasObject TrackObject
110         {
111             get
112             {
113                 if (_trackObject == null || Interop.Elementary.elm_object_item_track_get(Handle) == 0)
114                 {
115                     _trackObject = new ItemEvasObject(Handle);
116                 }
117                 return _trackObject;
118             }
119         }
120
121         /// <summary>
122         /// Sets or gets the style of the item.
123         /// </summary>
124         /// <since_tizen> preview </since_tizen>
125         [Obsolete("This has been deprecated in API12")]
126         public virtual string Style
127         {
128             get
129             {
130                 return Interop.Elementary.elm_object_item_style_get(Handle);
131             }
132             set
133             {
134                 Interop.Elementary.elm_object_item_style_set(Handle, value);
135             }
136         }
137
138         internal IntPtr Handle
139         {
140             get
141             {
142                 return _handle;
143             }
144             set
145             {
146                 if (_handle == value)
147                     return;
148
149                 if (_handle != IntPtr.Zero)
150                 {
151                     UnsetDeleteCallback();
152                 }
153                 _handle = value;
154                 SetDeleteCallback();
155                 s_HandleToItemTable[Handle] = this;
156                 if (_handle != IntPtr.Zero)
157                 {
158                     Elementary.SendItemObjectRealized(this);
159                 }
160             }
161         }
162
163         /// <summary>
164         /// Deleted will be triggered when the item object is deleted.
165         /// </summary>
166         /// <since_tizen> preview </since_tizen>
167         [Obsolete("This has been deprecated in API12")]
168         public event EventHandler Deleted;
169
170         /// <summary>
171         /// Deletes the item object.
172         /// </summary>
173         /// <since_tizen> preview </since_tizen>
174         [Obsolete("This has been deprecated in API12")]
175         public void Delete()
176         {
177             Interop.Elementary.elm_object_item_del(Handle);
178             _handle = IntPtr.Zero;
179         }
180
181         /// <summary>
182         /// Sets a content of an object item and deletes the old content.
183         /// </summary>
184         /// <param name="part">The content part name (null for the default content).</param>
185         /// <param name="content">The content of the object item.</param>
186         /// <since_tizen> preview </since_tizen>
187         [Obsolete("This has been deprecated in API12")]
188         public void SetPartContent(string part, EvasObject content)
189         {
190             SetPartContent(part, content, false);
191         }
192
193         /// <summary>.
194         /// Sets a content of the object item.
195         /// </summary>
196         /// <param name="part">The content part name (null for the default content)</param>
197         /// <param name="content">The content of the object item.</param>
198         /// <param name="preserveOldContent">Judge whether to delete the old content.</param>
199         /// <since_tizen> preview </since_tizen>
200         [Obsolete("This has been deprecated in API12")]
201         public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
202         {
203             IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
204             if (oldContent != IntPtr.Zero && !preserveOldContent)
205             {
206                 Interop.Evas.evas_object_del(oldContent);
207             }
208             Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
209             _partContents[part ?? "__default__"] = content;
210         }
211
212         /// <summary>
213         /// Sets the label of the object item.
214         /// </summary>
215         /// <param name="part">The text part name (null for the default label).</param>
216         /// <param name="text">Text of the label.</param>
217         /// <since_tizen> preview </since_tizen>
218         [Obsolete("This has been deprecated in API12")]
219         public void SetPartText(string part, string text)
220         {
221             Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
222         }
223
224         /// <summary>
225         /// Gets the label of the object item.
226         /// </summary>
227         /// <param name="part">The text part name (null for the default label).</param>
228         /// <returns></returns>
229         /// <since_tizen> preview </since_tizen>
230         [Obsolete("This has been deprecated in API12")]
231         public string GetPartText(string part)
232         {
233             return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
234         }
235
236         /// <summary>
237         /// Sets the color of the object item.
238         /// </summary>
239         /// <param name="part">The text part name (null for the default label).</param>
240         /// <param name="color">The color.</param>
241         /// <since_tizen> preview </since_tizen>
242         [Obsolete("This has been deprecated in API12")]
243         public void SetPartColor(string part, Color color)
244         {
245             Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
246                                                                               color.G * color.A / 255,
247                                                                               color.B * color.A / 255,
248                                                                               color.A);
249         }
250
251         /// <summary>
252         /// Gets the color of the object item.
253         /// </summary>
254         /// <param name="part">The text part name (null for the default label).</param>
255         /// <returns>The color of an object item.</returns>
256         /// <since_tizen> preview </since_tizen>
257         [Obsolete("This has been deprecated in API12")]
258         public Color GetPartColor(string part)
259         {
260             int r, g, b, a;
261             Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
262             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
263         }
264
265         /// <summary>
266         /// Deletes the color of the object item.
267         /// </summary>
268         /// <param name="part">The text part name.</param>
269         /// <since_tizen> preview </since_tizen>
270         [Obsolete("This has been deprecated in API12")]
271         public void DeletePartColor(string part)
272         {
273             Interop.Elementary.elm_object_item_color_class_del(Handle, part);
274         }
275
276         /// <summary>
277         /// Adds a function for a signal emitted by the object item edje.
278         /// </summary>
279         /// <param name="emission">The signal's name.</param>
280         /// <param name="source">The signal's source.</param>
281         /// <param name="func">The function to be executed when the signal is emitted.</param>
282         /// <since_tizen> preview </since_tizen>
283         [Obsolete("This has been deprecated in API12")]
284         public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
285         {
286             if (emission != null && source != null && func != null)
287             {
288                 var signalData = new SignalData(emission, source, func);
289                 if (!_signalDatas.ContainsKey(signalData))
290                 {
291                     var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
292                     {
293                         return func(e, s);
294                     });
295                     Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
296                 }
297             }
298         }
299
300         /// <summary>
301         /// Removes a signal-triggered function from the object item edje object.
302         /// </summary>
303         /// <param name="emission">The signal's name.</param>
304         /// <param name="source">The signal's source.</param>
305         /// <param name="func">The function to be executed when the signal is emitted.</param>
306         /// <since_tizen> preview </since_tizen>
307         [Obsolete("This has been deprecated in API12")]
308         public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
309         {
310             if (emission != null && source != null && func != null)
311             {
312                 var signalData = new SignalData(emission, source, func);
313
314                 Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
315                 _signalDatas.TryGetValue(signalData, out signalCallback);
316
317                 if (signalCallback != null)
318                 {
319                     Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
320                     _signalDatas.Remove(signalData);
321                 }
322             }
323         }
324
325         /// <summary>
326         /// Send a signal to the edje object of the widget item.
327         /// </summary>
328         /// <param name="emission">The signal's name.</param>
329         /// <param name="source">The signal's source.</param>
330         /// <since_tizen> preview </since_tizen>
331         [Obsolete("This has been deprecated in API12")]
332         public void EmitSignal(string emission, string source)
333         {
334             Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
335         }
336
337         /// <summary>
338         /// Gets the handle of the object item.
339         /// </summary>
340         /// <param name="obj">ItemObject</param>
341         /// <since_tizen> preview </since_tizen>
342         [Obsolete("This has been deprecated in API12")]
343         public static implicit operator IntPtr(ItemObject obj)
344         {
345             if (obj == null)
346                 return IntPtr.Zero;
347             return obj.Handle;
348         }
349
350         /// <summary>
351         /// OnInvalidate of the object item.
352         /// </summary>
353         /// <since_tizen> preview </since_tizen>
354         [Obsolete("This has been deprecated in API12")]
355         protected virtual void OnInvalidate() { }
356
357         internal static ItemObject GetItemById(int id)
358         {
359             ItemObject value;
360             s_IdToItemTable.TryGetValue(id, out value);
361             return value;
362         }
363
364         internal static ItemObject GetItemByHandle(IntPtr handle)
365         {
366             ItemObject value;
367             s_HandleToItemTable.TryGetValue(handle, out value);
368             return value;
369         }
370
371         void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
372         {
373             Deleted?.Invoke(this, EventArgs.Empty);
374             Parent = null;
375             OnInvalidate();
376             if (s_IdToItemTable.ContainsKey(Id))
377             {
378                 s_IdToItemTable.Remove(Id);
379             }
380             if (s_HandleToItemTable.ContainsKey(_handle))
381             {
382                 s_HandleToItemTable.Remove(_handle);
383             }
384             _partContents.Clear();
385             _handle = IntPtr.Zero;
386         }
387
388         void UnsetDeleteCallback()
389         {
390             Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
391         }
392
393         void SetDeleteCallback()
394         {
395             if (Handle != IntPtr.Zero)
396                 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
397         }
398
399         static int GetNextId()
400         {
401             return s_globalId++;
402         }
403
404         class SignalData : IEquatable<SignalData>
405         {
406             public string Emission { get; set; }
407             public string Source { get; set; }
408             public Func<string, string, bool> Func { get; set; }
409
410             public SignalData(string emission, string source, Func<string, string, bool> func)
411             {
412                 Emission = emission;
413                 Source = source;
414                 Func = func;
415             }
416
417             public override bool Equals(object other)
418             {
419                 return Equals(other as SignalData);
420             }
421
422             public bool Equals(SignalData other)
423             {
424                 if (other == null)
425                 {
426                     return false;
427                 }
428                 return (Emission == other.Emission) && (Source == other.Source) && (Func == other.Func);
429             }
430
431             public override int GetHashCode()
432             {
433                 int hashCode = Emission.GetHashCode();
434                 hashCode ^= Source.GetHashCode();
435                 hashCode ^= Func.GetHashCode();
436                 return hashCode;
437             }
438         }
439
440         class ItemEvasObject : EvasObject
441         {
442             IntPtr _trackHandle = IntPtr.Zero;
443
444             public ItemEvasObject(IntPtr parent)
445             {
446                 _trackHandle = Interop.Elementary.elm_object_item_track(parent);
447                 if (_trackHandle != IntPtr.Zero)
448                 {
449                     Realize(null);
450                 }
451             }
452
453             protected override IntPtr CreateHandle(EvasObject parent)
454             {
455                 return _trackHandle;
456             }
457         }
458     }
459 }