Enhance GenList and GenGrid Widgets
[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 item object
24     /// </summary>
25     public class ItemObject
26     {
27         private static Dictionary<int, ItemObject> s_IdToItemTable = new Dictionary<int, ItemObject>();
28         private static Dictionary<IntPtr, ItemObject> s_HandleToItemTable = new Dictionary<IntPtr, ItemObject>();
29         private static int s_globalId = 0;
30
31         readonly Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
32         Interop.Evas.SmartCallback _deleteCallback;
33         IntPtr _handle = IntPtr.Zero;
34
35         /// <summary>
36         /// Creates and initializes a new instance of ItemObject class.
37         /// </summary>
38         /// <param name="handle">IntPtr</param>
39         protected ItemObject(IntPtr handle)
40         {
41             _deleteCallback = DeleteCallbackHandler;
42             Id = GetNextId();
43             s_IdToItemTable[Id] = this;
44             Handle = handle;
45         }
46
47         // C# Finalizer was called on GC thread
48         // So, We can't access to EFL object
49         // And When Finalizer was called, Field can be already released.
50         //~ItemObject()
51         //{
52         //    if (Handle != IntPtr.Zero)
53         //        Interop.Elementary.elm_object_item_del(Handle);
54         //}
55
56         /// <summary>
57         /// Gets the id of item object
58         /// </summary>
59         public int Id { get; private set; }
60
61         /// <summary>
62         /// Sets or gets whether the item object is enabled
63         /// </summary>
64         public bool IsEnabled
65         {
66             get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
67             set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
68         }
69
70         internal IntPtr Handle
71         {
72             get
73             {
74                 return _handle;
75             }
76             set
77             {
78                 if (_handle == value)
79                     return;
80
81                 if (_handle != IntPtr.Zero)
82                 {
83                     UnsetDeleteCallback();
84                 }
85                 _handle = value;
86                 SetDeleteCallback();
87                 s_HandleToItemTable[Handle] = this;
88             }
89         }
90
91         /// <summary>
92         /// Deleted will be triggered when the item object is deleted
93         /// </summary>
94         public event EventHandler Deleted;
95
96         /// <summary>
97         /// Delete the item object
98         /// </summary>
99         public void Delete()
100         {
101             Interop.Elementary.elm_object_item_del(Handle);
102             _handle = IntPtr.Zero;
103         }
104
105         /// <summary>
106         /// Set a content of an object item and delete old content
107         /// </summary>
108         /// <param name="part">The content part name (null for the default content)</param>
109         /// <param name="content">The content of the object item</param>
110         public void SetPartContent(string part, EvasObject content)
111         {
112             SetPartContent(part, content, false);
113         }
114
115         /// <summary>
116         /// Set a content of an object item
117         /// </summary>
118         /// <param name="part">The content part name (null for the default content)</param>
119         /// <param name="content">The content of the object item</param>
120         /// <param name="preserveOldContent">judge whether delete old content</param>
121         public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
122         {
123             IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
124             if (oldContent != IntPtr.Zero && !preserveOldContent)
125             {
126                 Interop.Evas.evas_object_del(oldContent);
127             }
128             Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
129             _partContents[part ?? "__default__"] = content;
130         }
131
132         /// <summary>
133         /// Set a label of an object item
134         /// </summary>
135         /// <param name="part">The text part name (null for the default label)</param>
136         /// <param name="text">Text of the label</param>
137         public void SetPartText(string part, string text)
138         {
139             Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
140         }
141
142         /// <summary>
143         /// Gets a label of an object item
144         /// </summary>
145         /// <param name="part">The text part name (null for the default label)</param>
146         /// <returns></returns>
147         public string GetPartText(string part)
148         {
149             return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
150         }
151
152         /// <summary>
153         /// Sets color of an object item
154         /// </summary>
155         /// <param name="part">The text part name (null for the default label)</param>
156         /// <param name="color">the color</param>
157         public void SetPartColor(string part, Color color)
158         {
159             Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
160                                                                               color.G * color.A / 255,
161                                                                               color.B * color.A / 255,
162                                                                               color.A);
163         }
164
165         /// <summary>
166         /// Gets color of an object item
167         /// </summary>
168         /// <param name="part">The text part name (null for the default label)</param>
169         /// <returns>the color of object item</returns>
170         public Color GetPartColor(string part)
171         {
172             int r, g, b, a;
173             Interop.Elementary.elm_object_item_color_class_color_get(Handle, part, out r, out g, out b, out a);
174             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
175         }
176
177         /// <summary>
178         /// Deletes color of an object item
179         /// </summary>
180         /// <param name="part">The text part name</param>
181         public void DeletePartColor(string part)
182         {
183             Interop.Elementary.elm_object_item_color_class_del(Handle, part);
184         }
185
186         /// <summary>
187         /// Gets the handle of object item
188         /// </summary>
189         /// <param name="obj">ItemObject</param>
190         public static implicit operator IntPtr(ItemObject obj)
191         {
192             if (obj == null)
193                 return IntPtr.Zero;
194             return obj.Handle;
195         }
196
197         /// <summary>
198         /// OnInvalidate of object item
199         /// </summary>
200         protected virtual void OnInvalidate() { }
201
202         internal static ItemObject GetItemById(int id)
203         {
204             ItemObject value;
205             s_IdToItemTable.TryGetValue(id, out value);
206             return value;
207         }
208
209         internal static ItemObject GetItemByHandle(IntPtr handle)
210         {
211             ItemObject value = null;
212             s_HandleToItemTable.TryGetValue(handle, out value);
213             if (value == null)
214             {
215                 int id = (int)Interop.Elementary.elm_object_item_data_get(handle);
216                 return GetItemById(id);
217             }
218             return value;
219         }
220
221         void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
222         {
223             Deleted?.Invoke(this, EventArgs.Empty);
224             OnInvalidate();
225             if (s_IdToItemTable.ContainsKey(Id))
226             {
227                 s_IdToItemTable.Remove(Id);
228             }
229             if (s_HandleToItemTable.ContainsKey(_handle))
230             {
231                 s_HandleToItemTable.Remove(_handle);
232             }
233             _partContents.Clear();
234             _handle = IntPtr.Zero;
235         }
236
237         void UnsetDeleteCallback()
238         {
239             Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
240         }
241
242         void SetDeleteCallback()
243         {
244             if (Handle != IntPtr.Zero)
245                 Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
246         }
247
248         static int GetNextId()
249         {
250             return s_globalId++;
251         }
252     }
253 }