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 public class SmartEvent<TEventArgs> : IInvalidatable where TEventArgs : EventArgs
35 /// The delegate for creating smart event item args.
37 /// <param name="data">The item data.</param>
38 /// <param name="obj">The sender obj.</param>
39 /// <param name="info">The item sender obj.</param>
40 /// <returns>Return smart event item args.</returns>
41 public delegate TEventArgs SmartEventInfoParser(IntPtr data, IntPtr obj, IntPtr info);
43 private EvasObject _sender;
44 private readonly string _eventName;
45 private IntPtr _handle;
46 private readonly SmartEventInfoParser _parser;
47 private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
50 /// Creates and initializes a new instance of the SmartEvent class.
52 /// <param name="sender">The source of the event.</param>
53 /// <param name="eventName">The event name.</param>
54 /// <param name="parser">The event parameter.</param>
55 public SmartEvent(EvasObject sender, string eventName, SmartEventInfoParser parser) : this(sender, sender.Handle, eventName, parser)
60 /// reates and initializes a new instance of the SmartEvent class.
62 /// <param name="sender">The source of the event.</param>
63 /// <param name="handle">Teh event handler.</param>
64 /// <param name="eventName">The event name.</param>
65 /// <param name="parser">The event parser.</param>
66 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
67 public SmartEvent(EvasObject sender, IntPtr handle, string eventName, SmartEventInfoParser parser)
70 _eventName = eventName;
73 sender.AddToEventLifeTracker(this);
77 /// Creates and initializes a new instance of the SmartEvent class.
79 /// <param name="sender">The source of the event.</param>
80 /// <param name="eventName">The event name.</param>
81 public SmartEvent(EvasObject sender, string eventName) : this(sender, eventName, null)
86 /// Destroy the SmartEvent object.
93 private struct NativeCallback
95 public Interop.Evas.SmartCallback callback;
96 public EventHandler<TEventArgs> eventHandler;
100 /// Adds or removes delegate for event.
102 public event EventHandler<TEventArgs> On
106 if (_handle == IntPtr.Zero)
110 EventHandler<TEventArgs> handler = value;
111 var cb = new Interop.Evas.SmartCallback((d, o, e) =>
113 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(d, o, e);
114 handler(_sender, ea);
116 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler });
117 int i = _nativeCallbacks.Count - 1;
118 Interop.Evas.evas_object_smart_callback_add(_handle, _eventName, _nativeCallbacks[i].callback, IntPtr.Zero);
123 if (_handle == IntPtr.Zero)
127 EventHandler<TEventArgs> handler = value;
128 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
129 foreach (var cb in callbacks)
131 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
137 /// Destroy current object
139 public void Dispose()
142 GC.SuppressFinalize(this);
146 /// Make current instance invalidate.
148 public void MakeInvalidate()
151 _handle = IntPtr.Zero;
155 /// Releases all resources currently used by this instance.
157 /// <param name="disposing">
158 /// true if managed resources should be disposed
159 /// otherwise, false.
161 protected virtual void Dispose(bool disposing)
165 // Place holder to dispose managed state (managed objects).
167 if (_handle != IntPtr.Zero)
169 foreach (var cb in _nativeCallbacks)
171 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
174 _nativeCallbacks.Clear();
179 /// It inherits <see cref="IInvalidatable"/>.
180 /// EvasObject can elect SmartEvent occurring inside of them to be reported back to their users via delegates.
181 /// This way, you can extend EvasObject's own <see cref="EvasObjectEvent"/>.
182 /// They are defined by an event string, which identifies them uniquely.
184 public class SmartEvent : IInvalidatable
186 private SmartEvent<EventArgs> _smartEvent;
188 private event EventHandler _handlers;
191 /// Creates and initializes a new instance of the SmartEvent class.
193 /// <param name="sender">The source of the event.</param>
194 /// <param name="eventName">The event name.</param>
195 public SmartEvent(EvasObject sender, string eventName) : this(sender, sender.RealHandle, eventName)
200 /// Creates and initializes a new instance of the SmartEvent class.
202 /// <param name="sender">The source of the event.</param>
203 /// <param name="handle">The event handler.</param>
204 /// <param name="eventName">The event name.</param>
205 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
206 public SmartEvent(EvasObject sender, IntPtr handle, string eventName)
208 _smartEvent = new SmartEvent<EventArgs>(sender, handle, eventName, null);
212 /// Destroy the SmartEvent object.
220 /// Adds or removes delegate for event.
222 public event EventHandler On
226 if (_handlers == null)
228 _smartEvent.On += SendEvent;
236 if (_handlers == null)
238 _smartEvent.On -= SendEvent;
243 private void SendEvent(object sender, EventArgs e)
245 _handlers?.Invoke(sender, e);
249 /// Destroy current object
251 public void Dispose()
254 GC.SuppressFinalize(this);
258 /// Make current instance invalidate.
260 public void MakeInvalidate()
262 _smartEvent.MakeInvalidate();
266 /// Releases all resources currently used by this instance.
268 /// <param name="disposing">
269 /// true if managed resources should be disposed
270 /// otherwise, false.
272 protected virtual void Dispose(bool disposing)
276 // Place holder to dispose managed state (managed objects).
277 _smartEvent.Dispose();