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
31 /// Make current instance invalidate
33 void MakeInvalidate();
37 /// Enumeration for EvasObjectCallbackType
39 public enum EvasObjectCallbackType
42 /// Mouse In Event CallbackType.
47 /// Mouse Out Event CallbackType
52 /// Mouse Button Down Event CallbackType
57 /// Mouse Button Up Event CallbackType
62 /// Mouse Move Event CallbackType
67 /// Mouse Wheel Event CallbackType
72 /// Multi-touch Down Event CallbackType
77 /// Multi-touch Up Event CallbackType
82 /// Multi-touch Move Event CallbackType
87 /// Object Being Freed (Called after Del)
92 /// Key Press Event CallbackType
97 /// Key Release Event CallbackType
102 /// Focus In Event CallbackType
107 /// Focus Out Event CallbackType
112 /// Show Event CallbackType
117 /// Hide Event CallbackType
122 /// Move Event CallbackType
127 /// Resize Event CallbackType
132 /// Restack Event CallbackType
137 /// Object Being Deleted (called before Free)
142 /// Hold Event CallbackType, Informational purpose event to indicate something
147 /// Size hints changed Event CallbackType
152 /// Image has been preloaded
157 /// Canvas got focus as a whole
162 /// Canvas lost focus as a whole
167 /// Called just before rendering is updated on the canvas target
172 /// Called just after rendering is updated on the canvas target
177 /// Canvas object got focus
182 /// Canvas object lost focus
184 CanvasObjectFocusOut,
187 /// Image data has been unloaded (by some mechanism in Evas that throw out original image data)
192 /// Called just before rendering starts on the canvas target
197 /// Called just after rendering stops on the canvas target
202 /// Image size is changed
207 /// Devices added, removed or changed on canvas
217 /// Canvas Viewport size is changed
223 /// Event class for EvasObject
225 /// <typeparam name="TEventArgs">Kinds of EventArgs</typeparam>
226 public class EvasObjectEvent<TEventArgs> : IInvalidatable where TEventArgs : EventArgs
229 /// SmartEventInfoParser delegate of EvasObjectEvent class
231 /// <param name="data">data</param>
232 /// <param name="obj">obj</param>
233 /// <param name="info">info</param>
234 /// <returns> delegate handle</returns>
235 public delegate TEventArgs SmartEventInfoParser(IntPtr data, IntPtr obj, IntPtr info);
237 private bool _disposed = false;
238 private EvasObject _sender;
239 private IntPtr _handle;
240 private readonly EvasObjectCallbackType _type;
241 private readonly SmartEventInfoParser _parser;
242 private readonly List<NativeCallback> _nativeCallbacks = new List<NativeCallback>();
245 /// Creates and initializes a new instance of the EvasObjectEvent.
247 /// <param name="sender">EvasObject class belong to</param>
248 /// <param name="type">EvasObjectCallbackType</param>
249 /// <param name="parser">SmartEventInfoParser</param>
250 public EvasObjectEvent(EvasObject sender, EvasObjectCallbackType type, SmartEventInfoParser parser) : this(sender, sender.Handle, type, parser)
255 /// Creates and initializes a new instance of the EvasObjectEvent.
257 /// <param name="sender">EvasObject class belong to</param>
258 /// <param name="handle">EvasObject handle</param>
259 /// <param name="type">EvasObjectCallbackType</param>
260 /// <param name="parser">SmartEventInfoParser</param>
261 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
262 public EvasObjectEvent(EvasObject sender, IntPtr handle, EvasObjectCallbackType type, SmartEventInfoParser parser)
268 sender.AddToEventLifeTracker(this);
272 /// Creates and initializes a new instance of the EvasObjectEvent.
274 /// <param name="sender">EvasObject class belong with</param>
275 /// <param name="type">SmartEventInfoParser</param>
276 public EvasObjectEvent(EvasObject sender, EvasObjectCallbackType type) : this(sender, type, null)
281 /// Destroy the EvasObjectEvent object.
288 private struct NativeCallback
290 public Interop.Evas.EventCallback callback;
291 public EventHandler<TEventArgs> eventHandler;
295 /// On Event Handler of EvasObjectEvent
297 public event EventHandler<TEventArgs> On
301 if (_handle == IntPtr.Zero)
305 EventHandler<TEventArgs> handler = value;
306 var cb = new Interop.Evas.EventCallback((data, evas, obj, info) =>
308 TEventArgs ea = _parser == null ? (TEventArgs)EventArgs.Empty : _parser(data, obj, info);
309 handler(_sender, ea);
311 _nativeCallbacks.Add(new NativeCallback { callback = cb, eventHandler = handler });
312 int i = _nativeCallbacks.Count - 1;
313 Interop.Evas.evas_object_event_callback_add(_handle, (Interop.Evas.ObjectCallbackType)_type, _nativeCallbacks[i].callback, IntPtr.Zero);
318 if (_handle == IntPtr.Zero)
322 EventHandler<TEventArgs> handler = value;
323 var callbacks = _nativeCallbacks.Where(cb => cb.eventHandler == handler);
324 foreach (var cb in callbacks)
326 Interop.Evas.evas_object_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)_type, cb.callback);
331 protected virtual void Dispose(bool disposing)
337 // Place holder to dispose managed state (managed objects).
339 if (_handle != IntPtr.Zero)
341 foreach (var cb in _nativeCallbacks)
343 Interop.Evas.evas_object_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)_type, cb.callback);
346 _nativeCallbacks.Clear();
351 public void Dispose()
354 GC.SuppressFinalize(this);
358 /// Make current instance invalidate
360 public void MakeInvalidate()
363 _handle = IntPtr.Zero;
368 /// Event class for EvasObject
370 public class EvasObjectEvent : IInvalidatable
372 private EvasObjectEvent<EventArgs> _evasObjectEvent;
374 private event EventHandler _handlers;
376 private bool _disposed = false;
379 /// Creates and initializes a new instance of the EvasObjectEvent.
381 /// <param name="sender">EvasObject class belong to</param>
382 /// <param name="type">EvasObjectCallbackType</param>
383 public EvasObjectEvent(EvasObject sender, EvasObjectCallbackType type) : this(sender, sender.Handle, type)
388 /// Creates and initializes a new instance of the EvasObjectEvent.
390 /// <param name="sender">EvasObject class belong to</param>
391 /// <param name="handle">EvasObject handle</param>
392 /// <param name="type">EvasObjectCallbackType</param>
393 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
394 public EvasObjectEvent(EvasObject sender, IntPtr handle, EvasObjectCallbackType type)
396 _evasObjectEvent = new EvasObjectEvent<EventArgs>(sender, handle, type, null);
400 /// Destroy the EvasObjectEvent object.
408 /// On Event Handler of EvasObjectEvent
410 public event EventHandler On
414 if (_handlers == null)
416 _evasObjectEvent.On += SendEvent;
424 if (_handlers == null)
426 _evasObjectEvent.On -= SendEvent;
431 private void SendEvent(object sender, EventArgs e)
433 _handlers?.Invoke(sender, e);
436 protected virtual void Dispose(bool disposing)
442 _evasObjectEvent.Dispose();
448 public void Dispose()
451 GC.SuppressFinalize(this);
455 /// Make current instance invalidate
457 public void MakeInvalidate()
459 _evasObjectEvent.MakeInvalidate();