Make Deleted event for GenListItem object public
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / ItemObject.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace ElmSharp
8 {
9     public class ItemObject
10     {
11         private static Dictionary<int, ItemObject> s_IdToItemTable = new Dictionary<int, ItemObject>();
12         private static Dictionary<IntPtr, ItemObject> s_HandleToItemTable = new Dictionary<IntPtr, ItemObject>();
13         private static int s_globalId = 0;
14
15         Interop.Evas.SmartCallback _deleteCallback;
16         IntPtr _handle = IntPtr.Zero;
17
18         protected ItemObject(IntPtr handle)
19         {
20             _deleteCallback = DeleteCallbackHandler;
21             Id = GetNextId();
22             s_IdToItemTable[Id] = this;
23             Handle = handle;
24         }
25
26         ~ItemObject()
27         {
28             if (Handle != IntPtr.Zero)
29                 Interop.Elementary.elm_object_item_del(Handle);
30         }
31
32         public int Id { get; private set; }
33
34         public bool IsEnabled
35         {
36             get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
37             set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
38         }
39
40         internal IntPtr Handle
41         {
42             get
43             {
44                 return _handle;
45             }
46             set
47             {
48                 if (_handle == value)
49                     return;
50
51                 if (_handle != IntPtr.Zero)
52                 {
53                     UnsetDeleteCallback();
54                 }
55                 _handle = value;
56                 SetDeleteCallback();
57                 s_HandleToItemTable[Handle] = this;
58             }
59         }
60
61         public event EventHandler Deleted;
62
63         public void Delete()
64         {
65             Interop.Elementary.elm_object_item_del(Handle);
66             _handle = IntPtr.Zero;
67         }
68
69         public void SetPartContent(string part, EvasObject content)
70         {
71             SetPartContent(part, content, false);
72         }
73
74         public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
75         {
76             if (preserveOldContent)
77             {
78                 Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
79             }
80             Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
81         }
82
83         public void SetPartText(string part, string text)
84         {
85             Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
86         }
87
88         public string GetPartText(string part)
89         {
90             return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
91         }
92
93         public static implicit operator IntPtr(ItemObject obj)
94         {
95             if (obj == null)
96                 return IntPtr.Zero;
97             return obj.Handle;
98         }
99
100         protected virtual void OnInvalidate() { }
101
102         internal static ItemObject GetItemById(int id)
103         {
104             ItemObject value;
105             s_IdToItemTable.TryGetValue(id, out value);
106             return value;
107         }
108
109         internal static ItemObject GetItemByHandle(IntPtr handle)
110         {
111             ItemObject value;
112             s_HandleToItemTable.TryGetValue(handle, out value);
113             return value;
114         }
115
116         void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
117         {
118             Deleted?.Invoke(this, EventArgs.Empty);
119             OnInvalidate();
120             _handle = IntPtr.Zero;
121         }
122
123         void UnsetDeleteCallback()
124         {
125             Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
126         }
127
128         void SetDeleteCallback()
129         {
130             if (Handle != IntPtr.Zero)
131                 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
132         }
133
134         static int GetNextId()
135         {
136             return s_globalId++;
137         }
138     }
139 }