a3cc6e2a5e0add733d9820f4e4344ae6de8e768e
[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         // C# Finalizer was called on GC thread
27         // So, We can't access to EFL object
28         // And When Finalizer was called, Field can be already released.
29         //~ItemObject()
30         //{
31         //    if (Handle != IntPtr.Zero)
32         //        Interop.Elementary.elm_object_item_del(Handle);
33         //}
34
35         public int Id { get; private set; }
36
37         public bool IsEnabled
38         {
39             get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
40             set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
41         }
42
43         internal IntPtr Handle
44         {
45             get
46             {
47                 return _handle;
48             }
49             set
50             {
51                 if (_handle == value)
52                     return;
53
54                 if (_handle != IntPtr.Zero)
55                 {
56                     UnsetDeleteCallback();
57                 }
58                 _handle = value;
59                 SetDeleteCallback();
60                 s_HandleToItemTable[Handle] = this;
61             }
62         }
63
64         public event EventHandler Deleted;
65
66         public void Delete()
67         {
68             Interop.Elementary.elm_object_item_del(Handle);
69             _handle = IntPtr.Zero;
70         }
71
72         public void SetPartContent(string part, EvasObject content)
73         {
74             SetPartContent(part, content, false);
75         }
76
77         public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
78         {
79             if (preserveOldContent)
80             {
81                 Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
82             }
83             Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
84         }
85
86         public void SetPartText(string part, string text)
87         {
88             Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
89         }
90
91         public string GetPartText(string part)
92         {
93             return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
94         }
95
96         public static implicit operator IntPtr(ItemObject obj)
97         {
98             if (obj == null)
99                 return IntPtr.Zero;
100             return obj.Handle;
101         }
102
103         protected virtual void OnInvalidate() { }
104
105         internal static ItemObject GetItemById(int id)
106         {
107             ItemObject value;
108             s_IdToItemTable.TryGetValue(id, out value);
109             return value;
110         }
111
112         internal static ItemObject GetItemByHandle(IntPtr handle)
113         {
114             ItemObject value;
115             s_HandleToItemTable.TryGetValue(handle, out value);
116             return value;
117         }
118
119         void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
120         {
121             Deleted?.Invoke(this, EventArgs.Empty);
122             OnInvalidate();
123             if (s_IdToItemTable.ContainsKey(Id))
124             {
125                 s_IdToItemTable.Remove(Id);
126             }
127             if (s_HandleToItemTable.ContainsKey(_handle))
128             {
129                 s_HandleToItemTable.Remove(_handle);
130             }
131             _handle = IntPtr.Zero;
132         }
133
134         void UnsetDeleteCallback()
135         {
136             Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
137         }
138
139         void SetDeleteCallback()
140         {
141             if (Handle != IntPtr.Zero)
142                 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
143         }
144
145         static int GetNextId()
146         {
147             return s_globalId++;
148         }
149     }
150 }