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 SmartEvent occurring inside of 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 public class SmartEvent<TEventArgs> : IInvalidatable where TEventArgs : EventArgs
36 /// The delegate for creating smart event item args.
38 /// <param name="data">The item data.</param>
39 /// <param name="obj">The sender obj.</param>
40 /// <param name="info">The item sender obj.</param>
41 /// <returns>Return smart event item args.</returns>
42 /// <since_tizen> preview </since_tizen>
43 public delegate TEventArgs SmartEventInfoParser(IntPtr data, IntPtr obj, IntPtr info);
45 private EvasObject _sender;
46 private readonly string _eventName;
47 private IntPtr _handle;
48 private readonly SmartEventInfoParser _parser;
49 private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
52 /// Creates and initializes a new instance of the SmartEvent class.
54 /// <param name="sender">The source of the event.</param>
55 /// <param name="eventName">The event name.</param>
56 /// <param name="parser">The event parameter.</param>
57 /// <since_tizen> preview </since_tizen>
58 public SmartEvent(EvasObject sender, string eventName, SmartEventInfoParser parser) : this(sender, sender.Handle, eventName, parser)
63 /// reates and initializes a new instance of the SmartEvent class.
65 /// <param name="sender">The source of the event.</param>
66 /// <param name="handle">Teh event handler.</param>
67 /// <param name="eventName">The event name.</param>
68 /// <param name="parser">The event parser.</param>
69 /// <since_tizen> preview </since_tizen>
70 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
71 public SmartEvent(EvasObject sender, IntPtr handle, string eventName, SmartEventInfoParser parser)
74 _eventName = eventName;
77 sender.AddToEventLifeTracker(this);
81 /// Creates and initializes a new instance of the SmartEvent class.
83 /// <param name="sender">The source of the event.</param>
84 /// <param name="eventName">The event name.</param>
85 /// <since_tizen> preview </since_tizen>
86 public SmartEvent(EvasObject sender, string eventName) : this(sender, eventName, null)
91 /// Destroy the SmartEvent object.
98 private struct NativeCallback
100 public Interop.Evas.SmartCallback callback;
101 public EventHandler<TEventArgs> eventHandler;
105 /// Adds or removes delegate for event.
107 /// <since_tizen> preview </since_tizen>
108 public event EventHandler<TEventArgs> On
112 if (_handle == IntPtr.Zero)
116 EventHandler<TEventArgs> handler = value;
117 var cb = new Interop.Evas.SmartCallback((d, o, e) =>
119 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(d, o, e);
120 handler(_sender, ea);
122 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler });
123 int i = _nativeCallbacks.Count - 1;
124 Interop.Evas.evas_object_smart_callback_add(_handle, _eventName, _nativeCallbacks[i].callback, IntPtr.Zero);
129 if (_handle == IntPtr.Zero)
133 EventHandler<TEventArgs> handler = value;
134 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
135 foreach (var cb in callbacks)
137 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
143 /// Destroy current object
145 /// <since_tizen> preview </since_tizen>
146 public void Dispose()
149 GC.SuppressFinalize(this);
153 /// Make current instance invalidate.
155 /// <since_tizen> preview </since_tizen>
156 public void MakeInvalidate()
159 _handle = IntPtr.Zero;
163 /// Releases all resources currently used by this instance.
165 /// <param name="disposing">
166 /// true if managed resources should be disposed
167 /// otherwise, false.
169 /// <since_tizen> preview </since_tizen>
170 protected virtual void Dispose(bool disposing)
174 // Place holder to dispose managed state (managed objects).
176 if (_handle != IntPtr.Zero)
178 foreach (var cb in _nativeCallbacks)
180 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
183 _nativeCallbacks.Clear();
188 /// It inherits <see cref="IInvalidatable"/>.
189 /// EvasObject can elect SmartEvent occurring inside of them to be reported back to their users via delegates.
190 /// This way, you can extend EvasObject's own <see cref="EvasObjectEvent"/>.
191 /// They are defined by an event string, which identifies them uniquely.
193 /// <since_tizen> preview </since_tizen>
194 public class SmartEvent : IInvalidatable
196 private SmartEvent<EventArgs> _smartEvent;
198 private event EventHandler _handlers;
201 /// Creates and initializes a new instance of the SmartEvent class.
203 /// <param name="sender">The source of the event.</param>
204 /// <param name="eventName">The event name.</param>
205 /// <since_tizen> preview </since_tizen>
206 public SmartEvent(EvasObject sender, string eventName) : this(sender, sender.RealHandle, eventName)
211 /// Creates and initializes a new instance of the SmartEvent class.
213 /// <param name="sender">The source of the event.</param>
214 /// <param name="handle">The event handler.</param>
215 /// <param name="eventName">The event name.</param>
216 /// <since_tizen> preview </since_tizen>
217 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
218 public SmartEvent(EvasObject sender, IntPtr handle, string eventName)
220 _smartEvent = new SmartEvent<EventArgs>(sender, handle, eventName, null);
224 /// Destroy the SmartEvent object.
232 /// Adds or removes delegate for event.
234 /// <since_tizen> preview </since_tizen>
235 public event EventHandler On
239 if (_handlers == null)
241 _smartEvent.On += SendEvent;
249 if (_handlers == null)
251 _smartEvent.On -= SendEvent;
256 private void SendEvent(object sender, EventArgs e)
258 _handlers?.Invoke(sender, e);
262 /// Destroy current object
264 /// <since_tizen> preview </since_tizen>
265 public void Dispose()
268 GC.SuppressFinalize(this);
272 /// Make current instance invalidate.
274 /// <since_tizen> preview </since_tizen>
275 public void MakeInvalidate()
277 _smartEvent.MakeInvalidate();
281 /// Releases all resources currently used by this instance.
283 /// <param name="disposing">
284 /// true if managed resources should be disposed
285 /// otherwise, false.
287 /// <since_tizen> preview </since_tizen>
288 protected virtual void Dispose(bool disposing)
292 // Place holder to dispose managed state (managed objects).
293 _smartEvent.Dispose();