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.Collections.Generic;
19 using System.ComponentModel;
25 /// IInvalidatable is a interface which can be overrided by its children class.
26 /// Inherits IDisposable
28 public interface IInvalidatable : IDisposable
30 void MakeInvalidate();
34 /// Enumeration for EvasObjectCallbackType
36 public enum EvasObjectCallbackType
39 /// Mouse In Event CallbackType.
44 /// Mouse Out Event CallbackType
49 /// Mouse Button Down Event CallbackType
54 /// Mouse Button Up Event CallbackType
59 /// Mouse Move Event CallbackType
64 /// Mouse Wheel Event CallbackType
69 /// Multi-touch Down Event CallbackType
74 /// Multi-touch Up Event CallbackType
79 /// Multi-touch Move Event CallbackType
84 /// Object Being Freed (Called after Del)
89 /// Key Press Event CallbackType
94 /// Key Release Event CallbackType
99 /// Focus In Event CallbackType
104 /// Focus Out Event CallbackType
109 /// Show Event CallbackType
114 /// Hide Event CallbackType
119 /// Move Event CallbackType
124 /// Resize Event CallbackType
129 /// Restack Event CallbackType
134 /// Object Being Deleted (called before Free)
139 /// Hold Event CallbackType, Informational purpose event to indicate something
144 /// Size hints changed Event CallbackType
149 /// Image has been preloaded
154 /// Canvas got focus as a whole
159 /// Canvas lost focus as a whole
164 /// Called just before rendering is updated on the canvas target
169 /// Called just after rendering is updated on the canvas target
174 /// Canvas object got focus
179 /// Canvas object lost focus
181 CanvasObjectFocusOut,
184 /// Image data has been unloaded (by some mechanism in Evas that throw out original image data)
189 /// Called just before rendering starts on the canvas target
194 /// Called just after rendering stops on the canvas target
199 /// Image size is changed
204 /// Devices added, removed or changed on canvas
214 /// Canvas Viewport size is changed
220 /// Event class for EvasObject
222 /// <typeparam name="TEventArgs">Kinds of EventArgs</typeparam>
223 public class EvasObjectEvent<TEventArgs> : IInvalidatable where TEventArgs : EventArgs
226 /// SmartEventInfoParser delegate of EvasObjectEvent class
228 /// <param name="data">data</param>
229 /// <param name="obj">obj</param>
230 /// <param name="info">info</param>
231 /// <returns> delegate handle</returns>
232 public delegate TEventArgs SmartEventInfoParser(IntPtr data, IntPtr obj, IntPtr info);
234 private bool _disposed = false;
235 private EvasObject _sender;
236 private IntPtr _handle;
237 private readonly EvasObjectCallbackType _type;
238 private readonly SmartEventInfoParser _parser;
239 private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
242 /// Creates and initializes a new instance of the EvasObjectEvent.
244 /// <param name="sender">EvasObject class belong to</param>
245 /// <param name="type">EvasObjectCallbackType</param>
246 /// <param name="parser">SmartEventInfoParser</param>
247 public EvasObjectEvent(EvasObject sender, EvasObjectCallbackType type, SmartEventInfoParser parser) : this(sender, sender.Handle, type, parser)
251 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
252 public EvasObjectEvent(EvasObject sender, IntPtr handle, EvasObjectCallbackType type, SmartEventInfoParser parser)
258 sender.AddToEventLifeTracker(this);
262 /// Creates and initializes a new instance of the EvasObjectEvent.
264 /// <param name="sender">EvasObject class belong with</param>
265 /// <param name="type">SmartEventInfoParser</param>
266 public EvasObjectEvent(EvasObject sender, EvasObjectCallbackType type) : this(sender, type, null)
275 private struct NativeCallback
277 public Interop.Evas.EventCallback callback;
278 public EventHandler<TEventArgs> eventHandler;
282 /// On Event Handler of EvasObjectEvent
284 public event EventHandler<TEventArgs> On
288 if (_handle == IntPtr.Zero)
292 EventHandler<TEventArgs> handler = value;
293 var cb = new Interop.Evas.EventCallback((data, evas, obj, info) =>
295 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(data, obj, info);
296 handler(_sender, ea);
298 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler });
299 int i = _nativeCallbacks.Count - 1;
300 Interop.Evas.evas_object_event_callback_add(_handle, (Interop.Evas.ObjectCallbackType)_type, _nativeCallbacks[i].callback, IntPtr.Zero);
305 if (_handle == IntPtr.Zero)
309 EventHandler<TEventArgs> handler = value;
310 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
311 foreach (var cb in callbacks)
313 Interop.Evas.evas_object_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)_type, cb.callback);
318 protected virtual void Dispose(bool disposing)
324 // Place holder to dispose managed state (managed objects).
326 if (_handle != IntPtr.Zero)
328 foreach (var cb in _nativeCallbacks)
330 Interop.Evas.evas_object_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)_type, cb.callback);
333 _nativeCallbacks.Clear();
339 /// Destroy Current Obj
341 public void Dispose()
344 GC.SuppressFinalize(this);
348 /// Make current instance invalidate
350 public void MakeInvalidate()
353 _handle = IntPtr.Zero;
358 /// Event class for EvasObject
360 public class EvasObjectEvent : IInvalidatable
362 private EvasObjectEvent<EventArgs> _evasObjectEvent;
364 private event EventHandler _handlers;
366 private bool _disposed = false;
369 /// Creates and initializes a new instance of the EvasObjectEvent.
371 /// <param name="sender">EvasObject class belong to</param>
372 /// <param name="type">EvasObjectCallbackType</param>
373 public EvasObjectEvent(EvasObject sender, EvasObjectCallbackType type) : this(sender, sender.Handle, type)
377 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
378 public EvasObjectEvent(EvasObject sender, IntPtr handle, EvasObjectCallbackType type)
380 _evasObjectEvent = new EvasObjectEvent<EventArgs>(sender, handle, type, null);
389 /// On Event Handler of EvasObjectEvent
391 public event EventHandler On
395 if (_handlers == null)
397 _evasObjectEvent.On += SendEvent;
405 if (_handlers == null)
407 _evasObjectEvent.On -= SendEvent;
412 private void SendEvent(object sender, EventArgs e)
414 _handlers?.Invoke(sender, e);
417 protected virtual void Dispose(bool disposing)
423 _evasObjectEvent.Dispose();
430 /// Destroy Current Obj
432 public void Dispose()
435 GC.SuppressFinalize(this);
439 /// Make current instance invalidate
441 public void MakeInvalidate()
443 _evasObjectEvent.MakeInvalidate();