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)
59 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
60 public SmartEvent(EvasObject sender, IntPtr handle, string eventName, SmartEventInfoParser parser)
63 _eventName = eventName;
66 sender.AddToEventLifeTracker(this);
70 /// Creates and initializes a new instance of the SmartEvent class.
72 /// <param name="sender">The source of the event.</param>
73 /// <param name="eventName">The event name.</param>
74 public SmartEvent(EvasObject sender, string eventName) : this(sender, eventName, null)
83 private struct NativeCallback
85 public Interop.Evas.SmartCallback callback;
86 public EventHandler<TEventArgs> eventHandler;
90 /// Adds or removes delegate for event.
92 public event EventHandler<TEventArgs> On
96 if (_handle == IntPtr.Zero)
100 EventHandler<TEventArgs> handler = value;
101 var cb = new Interop.Evas.SmartCallback((d, o, e) =>
103 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(d, o, e);
104 handler(_sender, ea);
106 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler });
107 int i = _nativeCallbacks.Count - 1;
108 Interop.Evas.evas_object_smart_callback_add(_handle, _eventName, _nativeCallbacks[i].callback, IntPtr.Zero);
113 if (_handle == IntPtr.Zero)
117 EventHandler<TEventArgs> handler = value;
118 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
119 foreach (var cb in callbacks)
121 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
126 public void Dispose()
129 GC.SuppressFinalize(this);
133 /// Make current instance invalidate.
135 public void MakeInvalidate()
138 _handle = IntPtr.Zero;
141 protected virtual void Dispose(bool disposing)
145 // Place holder to dispose managed state (managed objects).
147 if (_handle != IntPtr.Zero)
149 foreach (var cb in _nativeCallbacks)
151 Interop.Evas.evas_object_smart_callback_del(_handle, _eventName, cb.callback);
154 _nativeCallbacks.Clear();
159 /// It inherits <see cref="IInvalidatable"/>.
160 /// EvasObject can elect SmartEvent occurring inside of them to be reported back to their users via delegates.
161 /// This way, you can extend EvasObject's own <see cref="EvasObjectEvent"/>.
162 /// They are defined by an event string, which identifies them uniquely.
164 public class SmartEvent : IInvalidatable
166 private SmartEvent<EventArgs> _smartEvent;
167 private event EventHandler _handlers;
170 /// Creates and initializes a new instance of the SmartEvent class.
172 /// <param name="sender">The source of the event.</param>
173 /// <param name="eventName">The event name.</param>
174 public SmartEvent(EvasObject sender, string eventName) : this(sender, sender.RealHandle, eventName)
178 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
179 public SmartEvent(EvasObject sender, IntPtr handle, string eventName)
181 _smartEvent = new SmartEvent<EventArgs>(sender, handle, eventName, null);
190 /// Adds or removes delegate for event.
192 public event EventHandler On
196 if (_handlers == null)
198 _smartEvent.On += SendEvent;
206 if (_handlers == null)
208 _smartEvent.On -= SendEvent;
213 private void SendEvent(object sender, EventArgs e)
215 _handlers?.Invoke(sender, e);
218 public void Dispose()
221 GC.SuppressFinalize(this);
225 /// Make current instance invalidate.
227 public void MakeInvalidate()
229 _smartEvent.MakeInvalidate();
232 protected virtual void Dispose(bool disposing)
236 // Place holder to dispose managed state (managed objects).
237 _smartEvent.Dispose();