d6016bf152b0f5f2ca546aa79169b717a2e6f4cc
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / SmartEvent.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.ComponentModel;
19 using System.Collections.Generic;
20 using System.Linq;
21
22 namespace ElmSharp
23 {
24     /// <summary>
25     /// It inherits <see cref="IInvalidatable"/>.
26     /// The event with TEventArgs for <see cref="EvasObject"/>.
27     /// EvasObject can elect the SmartEvent occurring inside them, to be reported back to their users via delegates.
28     /// This way, you can extend EvasObject's own <see cref="EvasObjectEvent"/>.
29     /// They are defined by an event string, which identifies them uniquely.
30     /// </summary>
31     /// <typeparam name="TEventArgs">The parameter for the event.</typeparam>
32     /// <since_tizen> preview </since_tizen>
33     [Obsolete("This has been deprecated in API12")]
34     public class SmartEvent<TEventArgs> : IInvalidatable where TEventArgs : EventArgs
35     {
36         /// <summary>
37         /// The delegate for creating smart event item arguments.
38         /// </summary>
39         /// <param name="data">The item data.</param>
40         /// <param name="obj">The sender object.</param>
41         /// <param name="info">The item sender object.</param>
42         /// <returns>Return smart event item args.</returns>
43         /// <since_tizen> preview </since_tizen>
44         [Obsolete("This has been deprecated in API12")]
45         public delegate TEventArgs SmartEventInfoParser(IntPtr data, IntPtr obj, IntPtr info);
46
47         private EvasObject _sender;
48         private readonly string _eventName;
49         private IntPtr _handle;
50         private readonly SmartEventInfoParser _parser;
51         private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
52
53         /// <summary>
54         /// Creates and initializes a new instance of the SmartEvent class.
55         /// </summary>
56         /// <param name="sender">The source of the event.</param>
57         /// <param name="eventName">The event name.</param>
58         /// <param name="parser">The event parameter.</param>
59         /// <since_tizen> preview </since_tizen>
60         [Obsolete("This has been deprecated in API12")]
61         public SmartEvent(EvasObject sender, string eventName, SmartEventInfoParser parser) : this(sender, sender.Handle, eventName, parser)
62         {
63         }
64
65         /// <summary>
66         /// Creates and initializes a new instance of the SmartEvent class.
67         /// </summary>
68         /// <param name="sender">The source of the event.</param>
69         /// <param name="handle">Teh event handler.</param>
70         /// <param name="eventName">The event name.</param>
71         /// <param name="parser">The event parser.</param>
72         /// <since_tizen> preview </since_tizen>
73         [Obsolete("This has been deprecated in API12")]
74         [EditorBrowsableAttribute(EditorBrowsableState.Never)]
75         public SmartEvent(EvasObject sender, IntPtr handle, string eventName, SmartEventInfoParser parser)
76         {
77             _sender = sender;
78             _eventName = eventName;
79             _handle = handle;
80             _parser = parser;
81             sender.AddToEventLifeTracker(this);
82         }
83
84         /// <summary>
85         /// Creates and initializes a new instance of the SmartEvent class.
86         /// </summary>
87         /// <param name="sender">The source of the event.</param>
88         /// <param name="eventName">The event name.</param>
89         /// <since_tizen> preview </since_tizen>
90         [Obsolete("This has been deprecated in API12")]
91         public SmartEvent(EvasObject sender, string eventName) : this(sender, eventName, null)
92         {
93         }
94
95         /// <summary>
96         /// Destroys the SmartEvent object.
97         /// </summary>
98         ~SmartEvent()
99         {
100             Dispose(false);
101         }
102
103         private struct NativeCallback
104         {
105             public Interop.Evas.SmartCallback callback;
106             public EventHandler<TEventArgs> eventHandler;
107         }
108
109         /// <summary>
110         /// Adds or removes a delegate for the event.
111         /// </summary>
112         /// <since_tizen> preview </since_tizen>
113         [Obsolete("This has been deprecated in API12")]
114         public event EventHandler<TEventArgs> On
115         {
116             add
117             {
118                 if (_handle == IntPtr.Zero)
119                 {
120                     return;
121                 }
122                 EventHandler<TEventArgs> handler = value;
123                 var cb = new Interop.Evas.SmartCallback((d, o, e) =>
124                 {
125                     TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(d, o, e);
126                     handler(_sender, ea);
127                 });
128                 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler });
129                 int i = _nativeCallbacks.Count - 1;
130                 Interop.Evas.evas_object_smart_callback_add(_handle, _eventName, _nativeCallbacks[i].callback, IntPtr.Zero);
131             }
132
133             remove
134             {
135                 if (_handle == IntPtr.Zero)
136                 {
137                     return;
138                 }
139                 EventHandler<TEventArgs> handler = value;
140                 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
141                 foreach (var cb in callbacks)
142                 {
143                     Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
144                 }
145             }
146         }
147
148         /// <summary>
149         /// Destroys the current object.
150         /// </summary>
151         /// <since_tizen> preview </since_tizen>
152         [Obsolete("This has been deprecated in API12")]
153         public void Dispose()
154         {
155             Dispose(true);
156             GC.SuppressFinalize(this);
157         }
158
159         /// <summary>
160         /// Makes the current instance invalidate.
161         /// </summary>
162         /// <since_tizen> preview </since_tizen>
163         [Obsolete("This has been deprecated in API12")]
164         public void MakeInvalidate()
165         {
166             _sender = null;
167             _handle = IntPtr.Zero;
168         }
169
170         /// <summary>
171         /// Releases all the resources currently used by this instance.
172         /// </summary>
173         /// <param name="disposing">
174         /// true if the managed resources should be disposed,
175         /// otherwise false.
176         /// </param>
177         /// <since_tizen> preview </since_tizen>
178         [Obsolete("This has been deprecated in API12")]
179         protected virtual void Dispose(bool disposing)
180         {
181             if (disposing)
182             {
183                 _sender.RemoveFromEventLifeTracker(this);
184             }
185             if (_handle != IntPtr.Zero)
186             {
187                 foreach (var cb in _nativeCallbacks)
188                 {
189                     Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
190                 }
191             }
192             _nativeCallbacks.Clear();
193         }
194     }
195
196     /// <summary>
197     /// It inherits <see cref="IInvalidatable"/>.
198     /// EvasObject can elect the SmartEvent occurring inside them, to be reported back to their users via delegates.
199     /// This way, you can extend EvasObject's own <see cref="EvasObjectEvent"/>.
200     /// They are defined by an event string, which identifies them uniquely.
201     /// </summary>
202     /// <since_tizen> preview </since_tizen>
203     [Obsolete("This has been deprecated in API12")]
204     public class SmartEvent : IInvalidatable
205     {
206         private SmartEvent<EventArgs> _smartEvent;
207
208         private event EventHandler _handlers;
209
210         /// <summary>
211         /// Creates and initializes a new instance of the SmartEvent class.
212         /// </summary>
213         /// <param name="sender">The source of the event.</param>
214         /// <param name="eventName">The event name.</param>
215         /// <since_tizen> preview </since_tizen>
216         [Obsolete("This has been deprecated in API12")]
217         public SmartEvent(EvasObject sender, string eventName) : this(sender, sender.RealHandle, eventName)
218         {
219         }
220
221         /// <summary>
222         /// Creates and initializes a new instance of the SmartEvent class.
223         /// </summary>
224         /// <param name="sender">The source of the event.</param>
225         /// <param name="handle">The event handler.</param>
226         /// <param name="eventName">The event name.</param>
227         /// <since_tizen> preview </since_tizen>
228         [Obsolete("This has been deprecated in API12")]
229         [EditorBrowsableAttribute(EditorBrowsableState.Never)]
230         public SmartEvent(EvasObject sender, IntPtr handle, string eventName)
231         {
232             _smartEvent = new SmartEvent<EventArgs>(sender, handle, eventName, null);
233         }
234
235         /// <summary>
236         /// Destroys the SmartEvent object.
237         /// </summary>
238         ~SmartEvent()
239         {
240             Dispose(false);
241         }
242
243         /// <summary>
244         /// Adds or removes a delegate for the event.
245         /// </summary>
246         /// <since_tizen> preview </since_tizen>
247         [Obsolete("This has been deprecated in API12")]
248         public event EventHandler On
249         {
250             add
251             {
252                 if (_handlers == null)
253                 {
254                     _smartEvent.On += SendEvent;
255                 }
256                 _handlers += value;
257             }
258
259             remove
260             {
261                 _handlers -= value;
262                 if (_handlers == null)
263                 {
264                     _smartEvent.On -= SendEvent;
265                 }
266             }
267         }
268
269         private void SendEvent(object sender, EventArgs e)
270         {
271             _handlers?.Invoke(sender, e);
272         }
273
274         /// <summary>
275         /// Destroys the current object.
276         /// </summary>
277         /// <since_tizen> preview </since_tizen>
278         [Obsolete("This has been deprecated in API12")]
279         public void Dispose()
280         {
281             Dispose(true);
282             GC.SuppressFinalize(this);
283         }
284
285         /// <summary>
286         /// Makes the current instance invalidate.
287         /// </summary>
288         /// <since_tizen> preview </since_tizen>
289         [Obsolete("This has been deprecated in API12")]
290         public void MakeInvalidate()
291         {
292             _smartEvent.MakeInvalidate();
293         }
294
295         /// <summary>
296         /// Releases all the resources currently used by this instance.
297         /// </summary>
298         /// <param name="disposing">
299         /// true if the managed resources should be disposed,
300         /// otherwise false.
301         /// </param>
302         /// <since_tizen> preview </since_tizen>
303         [Obsolete("This has been deprecated in API12")]
304         protected virtual void Dispose(bool disposing)
305         {
306             if (disposing)
307             {
308                 // Place holder to dispose managed state (managed objects).
309                 _smartEvent.Dispose();
310             }
311         }
312     }
313 }