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