2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using System.Collections.Generic;
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.
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
37 /// The delegate for creating smart event item arguments.
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);
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>();
54 /// Creates and initializes a new instance of the SmartEvent class.
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)
66 /// Creates and initializes a new instance of the SmartEvent class.
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)
78 _eventName = eventName;
81 sender.AddToEventLifeTracker(this);
85 /// Creates and initializes a new instance of the SmartEvent class.
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)
96 /// Destroys the SmartEvent object.
103 private struct NativeCallback
105 public Interop.Evas.SmartCallback callback;
106 public EventHandler<TEventArgs> eventHandler;
110 /// Adds or removes a delegate for the event.
112 /// <since_tizen> preview </since_tizen>
113 [Obsolete("This has been deprecated in API12")]
114 public event EventHandler<TEventArgs> On
118 if (_handle == IntPtr.Zero)
122 EventHandler<TEventArgs> handler = value;
123 var cb = new Interop.Evas.SmartCallback((d, o, e) =>
125 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(d, o, e);
126 handler(_sender, ea);
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);
135 if (_handle == IntPtr.Zero)
139 EventHandler<TEventArgs> handler = value;
140 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
141 foreach (var cb in callbacks)
143 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
149 /// Destroys the current object.
151 /// <since_tizen> preview </since_tizen>
152 [Obsolete("This has been deprecated in API12")]
153 public void Dispose()
156 GC.SuppressFinalize(this);
160 /// Makes the current instance invalidate.
162 /// <since_tizen> preview </since_tizen>
163 [Obsolete("This has been deprecated in API12")]
164 public void MakeInvalidate()
167 _handle = IntPtr.Zero;
171 /// Releases all the resources currently used by this instance.
173 /// <param name="disposing">
174 /// true if the managed resources should be disposed,
177 /// <since_tizen> preview </since_tizen>
178 [Obsolete("This has been deprecated in API12")]
179 protected virtual void Dispose(bool disposing)
183 _sender.RemoveFromEventLifeTracker(this);
185 if (_handle != IntPtr.Zero)
187 foreach (var cb in _nativeCallbacks)
189 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
192 _nativeCallbacks.Clear();
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.
202 /// <since_tizen> preview </since_tizen>
203 [Obsolete("This has been deprecated in API12")]
204 public class SmartEvent : IInvalidatable
206 private SmartEvent<EventArgs> _smartEvent;
208 private event EventHandler _handlers;
211 /// Creates and initializes a new instance of the SmartEvent class.
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)
222 /// Creates and initializes a new instance of the SmartEvent class.
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)
232 _smartEvent = new SmartEvent<EventArgs>(sender, handle, eventName, null);
236 /// Destroys the SmartEvent object.
244 /// Adds or removes a delegate for the event.
246 /// <since_tizen> preview </since_tizen>
247 [Obsolete("This has been deprecated in API12")]
248 public event EventHandler On
252 if (_handlers == null)
254 _smartEvent.On += SendEvent;
262 if (_handlers == null)
264 _smartEvent.On -= SendEvent;
269 private void SendEvent(object sender, EventArgs e)
271 _handlers?.Invoke(sender, e);
275 /// Destroys the current object.
277 /// <since_tizen> preview </since_tizen>
278 [Obsolete("This has been deprecated in API12")]
279 public void Dispose()
282 GC.SuppressFinalize(this);
286 /// Makes the current instance invalidate.
288 /// <since_tizen> preview </since_tizen>
289 [Obsolete("This has been deprecated in API12")]
290 public void MakeInvalidate()
292 _smartEvent.MakeInvalidate();
296 /// Releases all the resources currently used by this instance.
298 /// <param name="disposing">
299 /// true if the managed resources should be disposed,
302 /// <since_tizen> preview </since_tizen>
303 [Obsolete("This has been deprecated in API12")]
304 protected virtual void Dispose(bool disposing)
308 // Place holder to dispose managed state (managed objects).
309 _smartEvent.Dispose();