Merge "[Camera] Fix XML documentation warnings"
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / GenItemClass.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 using System.Runtime.InteropServices;
20
21 namespace ElmSharp
22 {
23     /// <summary>
24     /// It represents the GenGrid or GenList item class definition field details.
25     /// It has some display styles, such as "default", "full" and "group_index".
26     /// </summary>
27     public class GenItemClass : IDisposable
28     {
29         static Dictionary<IntPtr, EvasObject> s_HandleToEvasObject = new Dictionary<IntPtr, EvasObject>();
30
31         /// <summary>
32         /// The delegate to define <see cref="GetTextHandler"/>.
33         /// </summary>
34         /// <param name="data">The item data.</param>
35         /// <param name="part">The part where the data should be shown.</param>
36         /// <returns>Return string that should be shown.</returns>
37         public delegate string GetTextDelegate(object data, string part);
38
39         /// <summary>
40         /// The delegate to define <see cref="GetContentHandler"/>.
41         /// </summary>
42         /// <param name="data">The item data.</param>
43         /// <param name="part">The part where the data should be shown.</param>
44         /// <returns>Return content that should be shown.</returns>
45         public delegate EvasObject GetContentDelegate(object data, string part);
46
47         /// <summary>
48         /// The delegate to define <see cref="DeleteHandler"/>.
49         /// </summary>
50         /// <param name="data">The item data.</param>
51         public delegate void DeleteDelegate(object data);
52
53         /// <summary>
54         /// The delegate to define <see cref="ReusableContentHandler"/>.
55         /// </summary>
56         /// <param name="data">The item data.</param>
57         /// <param name="part">The part where the data should be shown.</param>
58         /// <param name="old">The content has been added in gengrid.</param>
59         /// <returns>Return content that should be shown.</returns>
60         public delegate EvasObject GetReusableContentDelegate(object data, string part, EvasObject old);
61
62         ItemClass _itemClass;
63         IntPtr _unmanagedPtr = IntPtr.Zero;
64         string _style;
65
66         /// <summary>
67         /// Creates and initializes a new instance of the GenItemClass.
68         /// </summary>
69         /// <param name="style">The item display style.</param>
70         public GenItemClass(string style)
71         {
72             _style = style;
73             IntPtr unmanaged = CreateItemClass();
74
75             _itemClass = Marshal.PtrToStructure<ItemClass>(unmanaged);
76             _itemClass.itemStyle = style;
77             _itemClass.textCallback = GetTextCallback;
78             _itemClass.contentCallback = GetContentCallback;
79             _itemClass.stateCallback = null;
80             _itemClass.delCallback = DelCallback;
81             _itemClass.reusableContentCallback = GetReusableContentCallback;
82
83             ReleaseItemClass(unmanaged);
84         }
85
86         /// <summary>
87         /// Destroy the GenItemClass object.
88         /// </summary>
89         ~GenItemClass()
90         {
91             Dispose(false);
92         }
93
94         /// <summary>
95         /// Gets the item style.
96         /// </summary>
97         public string ItemStyle { get { return _style; } }
98
99         /// <summary>
100         /// Gets or sets the callback that defines how to display item text.
101         /// If get, return <see cref="GetTextDelegate"/>.
102         /// </summary>
103         public GetTextDelegate GetTextHandler { get; set; }
104
105         /// <summary>
106         /// Gets or sets the callback that defines how to display item content.
107         /// If get, return <see cref="GetContentDelegate"/>.
108         /// </summary>
109         public GetContentDelegate GetContentHandler { get; set; }
110
111         /// <summary>
112         /// Gets or sets the callback that defines how to delete item text and content.
113         /// If get, return <see cref="DeleteDelegate"/>.
114         /// </summary>
115         public DeleteDelegate DeleteHandler { get; set; }
116
117         /// <summary>
118         /// Gets or sets the callback that defines how to reuse item content.
119         /// If get, return <see cref="GetReusableContentDelegate"/>.
120         /// </summary>
121         public GetReusableContentDelegate ReusableContentHandler { get; set; }
122
123         internal IntPtr UnmanagedPtr
124         {
125             get
126             {
127                 if (_unmanagedPtr == IntPtr.Zero)
128                 {
129                     _unmanagedPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_itemClass));
130                     Marshal.StructureToPtr(_itemClass, _unmanagedPtr, false);
131                 }
132                 return _unmanagedPtr;
133             }
134         }
135
136         /// <summary>
137         /// Releases all resources currently used by this instance.
138         /// </summary>
139         /// <param name="disposing">
140         /// true if managed resources should be disposed
141         /// otherwise, false.
142         /// </param>
143         protected virtual void Dispose(bool disposing)
144         {
145             if (_unmanagedPtr != IntPtr.Zero)
146             {
147                 Marshal.FreeHGlobal(_unmanagedPtr);
148                 _unmanagedPtr = IntPtr.Zero;
149             }
150         }
151
152         /// <summary>
153         /// Destroy current object
154         /// </summary>
155         public void Dispose()
156         {
157             Dispose(true);
158             GC.SuppressFinalize(this);
159         }
160
161         internal void SendItemDeleted(object data)
162         {
163             // data is user inserted value with GenItem
164             DeleteHandler?.Invoke(data);
165         }
166
167         /// <summary>
168         /// Create a new genlist item class in a given genlist widget.
169         /// </summary>
170         /// <returns>The new item class object.</returns>
171         protected virtual IntPtr CreateItemClass()
172         {
173             return Interop.Elementary.elm_genlist_item_class_new();
174         }
175
176         /// <summary>
177         /// Remove an item class in a given genlist widget.
178         /// </summary>
179         /// <param name="unmanagedPtr">The object to be removed.</param>
180         protected virtual void ReleaseItemClass(IntPtr unmanagedPtr)
181         {
182             Interop.Elementary.elm_genlist_item_class_free(unmanagedPtr);
183         }
184
185         string GetTextCallback(IntPtr data, IntPtr obj, IntPtr part)
186         {
187             GenItem item = ItemObject.GetItemById((int)data) as GenItem;
188             return GetTextHandler?.Invoke(item?.Data, Marshal.PtrToStringAnsi(part));
189         }
190
191         IntPtr GetContentCallback(IntPtr data, IntPtr obj, IntPtr part)
192         {
193             GenItem item = ItemObject.GetItemById((int)data) as GenItem;
194             EvasObject evasObject = GetContentHandler?.Invoke(item?.Data, Marshal.PtrToStringAnsi(part));
195             if (evasObject != null)
196             {
197                 s_HandleToEvasObject[evasObject.Handle] = evasObject;
198                 evasObject.Deleted += EvasObjectDeleted;
199             }
200             return evasObject;
201         }
202
203         void EvasObjectDeleted(object sender, EventArgs e)
204         {
205             IntPtr handle = (sender as EvasObject).Handle;
206             s_HandleToEvasObject.Remove(handle);
207         }
208
209         IntPtr GetReusableContentCallback(IntPtr data, IntPtr obj, IntPtr part, IntPtr old)
210         {
211             IntPtr reusedHandle = IntPtr.Zero;
212             GenItem item = ItemObject.GetItemById((int)data) as GenItem;
213             if (s_HandleToEvasObject.ContainsKey(old))
214             {
215                 reusedHandle = ReusableContentHandler?.Invoke(item?.Data, Marshal.PtrToStringAnsi(part), s_HandleToEvasObject[old]);
216             }
217             return reusedHandle;
218         }
219
220         void DelCallback(IntPtr data, IntPtr obj)
221         {
222             // We can't use this callback
223             // because, when item was deleted
224             // First, ItemObject deleted callback was called
225             // and We need to clean up ItemObject related objects
226             // This callback was called after ItemObject deleted callback was completed.
227             // So, We can't get resource reletated with ItemObject
228         }
229     }
230
231     /// <summary>
232     /// It represents the GenGrid item class definition field details.
233     /// </summary>
234     public class GenGridItemClass : GenItemClass
235     {
236         /// <summary>
237         /// Create the GenGridItemClass instance.
238         /// </summary>
239         /// <param name="style">The item display style.</param>
240         public GenGridItemClass(string style) : base(style)
241         {
242         }
243
244         /// <summary>
245         /// Add a new gengrid item class in a given gengrid widget.
246         /// </summary>
247         /// <returns>The new instance.</returns>
248         protected override IntPtr CreateItemClass()
249         {
250             return Interop.Elementary.elm_gengrid_item_class_new();
251         }
252
253         /// <summary>
254         /// Remove an item class in a given gengrid widget.
255         /// </summary>
256         /// <param name="unmanagedPtr">The object to be removed.</param>
257         protected override void ReleaseItemClass(IntPtr unmanagedPtr)
258         {
259             Interop.Elementary.elm_gengrid_item_class_free(unmanagedPtr);
260         }
261     }
262
263     [StructLayout(LayoutKind.Sequential)]
264     internal class ItemClass
265     {
266         public delegate string GetTextCallback(IntPtr data, IntPtr obj, IntPtr part);
267
268         public delegate IntPtr GetContentCallback(IntPtr data, IntPtr obj, IntPtr part);
269
270         public delegate int GetStateCallback(IntPtr data, IntPtr obj, IntPtr part);
271
272         public delegate void DelCallback(IntPtr data, IntPtr obj);
273
274         public delegate int FilterCallback(IntPtr data, IntPtr obj, IntPtr key);
275
276         public delegate IntPtr GetReusableContentCallback(IntPtr data, IntPtr obj, IntPtr part, IntPtr old);
277
278         public int version;
279         public uint refCount;
280         public int deleteMe;
281         public string itemStyle;
282         public readonly string decorateItemStyle;
283         public readonly string decorateAllItemStyle;
284         public GetTextCallback textCallback;
285         public GetContentCallback contentCallback;
286         public GetStateCallback stateCallback;
287         public DelCallback delCallback;
288         public FilterCallback filterCallback;
289         public GetReusableContentCallback reusableContentCallback;
290     }
291 }