Introduce ElmSharp project
[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         internal IntPtr Handle
35         {
36             get
37             {
38                 return _handle;
39             }
40             set
41             {
42                 if (_handle == value)
43                     return;
44
45                 if (_handle != IntPtr.Zero)
46                 {
47                     UnsetDeleteCallback();
48                 }
49                 _handle = value;
50                 SetDeleteCallback();
51                 s_HandleToItemTable[Handle] = this;
52             }
53         }
54
55         internal event EventHandler Deleted;
56
57         public void Delete()
58         {
59             Interop.Elementary.elm_object_item_del(Handle);
60             _handle = IntPtr.Zero;
61         }
62
63         public void SetPartContent(string part, EvasObject content)
64         {
65             SetPartContent(part, content, false);
66         }
67
68         public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
69         {
70             if (preserveOldContent)
71             {
72                 Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
73             }
74             Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
75         }
76
77         public void SetPartText(string part, string text)
78         {
79             Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
80         }
81
82         public string GetPartText(string part)
83         {
84             return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
85         }
86
87         public static implicit operator IntPtr(ItemObject obj)
88         {
89             if (obj == null)
90                 return IntPtr.Zero;
91             return obj.Handle;
92         }
93
94         protected virtual void OnInvalidate() { }
95
96         internal static ItemObject GetItemById(int id)
97         {
98             ItemObject value;
99             s_IdToItemTable.TryGetValue(id, out value);
100             return value;
101         }
102
103         internal static ItemObject GetItemByHandle(IntPtr handle)
104         {
105             ItemObject value;
106             s_HandleToItemTable.TryGetValue(handle, out value);
107             return value;
108         }
109
110         void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
111         {
112             Deleted?.Invoke(this, EventArgs.Empty);
113             OnInvalidate();
114             _handle = IntPtr.Zero;
115         }
116
117         void UnsetDeleteCallback()
118         {
119             Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
120         }
121
122         void SetDeleteCallback()
123         {
124             if (Handle != IntPtr.Zero)
125                 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
126         }
127
128         static int GetNextId()
129         {
130             return s_globalId++;
131         }
132     }
133 }