2005-02-02 21:57:15 +00:00
|
|
|
// GLib.Signal.cs - signal marshaling class
|
|
|
|
//
|
|
|
|
// Authors: Mike Kestner <mkestner@novell.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2005 Novell, Inc.
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the Lesser GNU General
|
|
|
|
// Public License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
using System;
|
2008-01-22 16:54:44 +00:00
|
|
|
using System.Collections;
|
2005-02-02 21:57:15 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
2007-12-06 17:23:28 +00:00
|
|
|
[Flags]
|
|
|
|
public enum ConnectFlags {
|
|
|
|
After = 1 << 0,
|
|
|
|
Swapped = 1 << 1,
|
|
|
|
}
|
|
|
|
|
2005-02-02 21:57:15 +00:00
|
|
|
[Flags]
|
|
|
|
internal enum SignalFlags {
|
|
|
|
RunFirst = 1 << 0,
|
|
|
|
RunLast = 1 << 1,
|
|
|
|
RunCleanup = 1 << 2,
|
|
|
|
NoRecurse = 1 << 3,
|
|
|
|
Detailed = 1 << 4,
|
|
|
|
Action = 1 << 5,
|
|
|
|
NoHooks = 1 << 6
|
|
|
|
}
|
|
|
|
|
|
|
|
[StructLayout (LayoutKind.Sequential)]
|
|
|
|
internal struct InvocationHint {
|
|
|
|
public uint signal_id;
|
|
|
|
public uint detail;
|
|
|
|
public SignalFlags run_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Signal {
|
|
|
|
|
|
|
|
GCHandle gc_handle;
|
2008-01-22 16:54:44 +00:00
|
|
|
ToggleRef tref;
|
2005-02-02 21:57:15 +00:00
|
|
|
string name;
|
|
|
|
uint before_id = UInt32.MaxValue;
|
|
|
|
uint after_id = UInt32.MaxValue;
|
|
|
|
Delegate marshaler;
|
|
|
|
|
2008-01-22 16:54:44 +00:00
|
|
|
~Signal ()
|
|
|
|
{
|
|
|
|
gc_handle.Free ();
|
|
|
|
}
|
|
|
|
|
2005-02-02 21:57:15 +00:00
|
|
|
private Signal (GLib.Object obj, string signal_name, Delegate marshaler)
|
|
|
|
{
|
2008-01-22 16:54:44 +00:00
|
|
|
tref = obj.ToggleRef;
|
2005-02-02 21:57:15 +00:00
|
|
|
name = signal_name;
|
|
|
|
this.marshaler = marshaler;
|
2007-11-16 18:35:38 +00:00
|
|
|
gc_handle = GCHandle.Alloc (this, GCHandleType.Weak);
|
2008-01-22 16:54:44 +00:00
|
|
|
tref.Signals [name] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void Free ()
|
|
|
|
{
|
|
|
|
DisconnectHandler (before_id);
|
|
|
|
DisconnectHandler (after_id);
|
|
|
|
before_handler = after_handler = marshaler = null;
|
|
|
|
gc_handle.Free ();
|
|
|
|
GC.SuppressFinalize (this);
|
2005-02-02 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Signal Lookup (GLib.Object obj, string name)
|
|
|
|
{
|
|
|
|
return Lookup (obj, name, EventHandlerDelegate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Signal Lookup (GLib.Object obj, string name, Delegate marshaler)
|
|
|
|
{
|
2008-01-22 16:54:44 +00:00
|
|
|
Signal result = obj.ToggleRef.Signals [name] as Signal;
|
2007-11-16 18:35:38 +00:00
|
|
|
if (result == null)
|
|
|
|
result = new Signal (obj, name, marshaler);
|
2008-04-24 17:18:59 +00:00
|
|
|
return result;
|
2005-02-02 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Delegate before_handler;
|
|
|
|
Delegate after_handler;
|
|
|
|
|
|
|
|
public Delegate Handler {
|
|
|
|
get {
|
2008-01-22 16:54:44 +00:00
|
|
|
InvocationHint hint = (InvocationHint) Marshal.PtrToStructure (g_signal_get_invocation_hint (tref.Handle), typeof (InvocationHint));
|
2005-02-02 21:57:15 +00:00
|
|
|
if (hint.run_type == SignalFlags.RunFirst)
|
|
|
|
return before_handler;
|
|
|
|
else
|
|
|
|
return after_handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-08 21:28:08 +00:00
|
|
|
uint Connect (int flags)
|
|
|
|
{
|
|
|
|
IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup (name);
|
2008-01-22 16:54:44 +00:00
|
|
|
uint id = g_signal_connect_data (tref.Handle, native_name, marshaler, (IntPtr) gc_handle, IntPtr.Zero, flags);
|
2005-03-08 21:28:08 +00:00
|
|
|
GLib.Marshaller.Free (native_name);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2005-02-02 21:57:15 +00:00
|
|
|
public void AddDelegate (Delegate d)
|
|
|
|
{
|
|
|
|
if (d.Method.IsDefined (typeof (ConnectBeforeAttribute), false)) {
|
|
|
|
if (before_handler == null) {
|
|
|
|
before_handler = d;
|
2005-03-08 21:28:08 +00:00
|
|
|
before_id = Connect (0);
|
2005-02-02 21:57:15 +00:00
|
|
|
} else
|
|
|
|
before_handler = Delegate.Combine (before_handler, d);
|
|
|
|
} else {
|
|
|
|
if (after_handler == null) {
|
|
|
|
after_handler = d;
|
2005-03-08 21:28:08 +00:00
|
|
|
after_id = Connect (1);
|
2005-02-02 21:57:15 +00:00
|
|
|
} else
|
|
|
|
after_handler = Delegate.Combine (after_handler, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveDelegate (Delegate d)
|
|
|
|
{
|
|
|
|
if (d.Method.IsDefined (typeof (ConnectBeforeAttribute), false)) {
|
|
|
|
before_handler = Delegate.Remove (before_handler, d);
|
|
|
|
if (before_handler == null) {
|
|
|
|
DisconnectHandler (before_id);
|
|
|
|
before_id = UInt32.MaxValue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
after_handler = Delegate.Remove (after_handler, d);
|
|
|
|
if (after_handler == null) {
|
|
|
|
DisconnectHandler (after_id);
|
|
|
|
after_id = UInt32.MaxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (after_id == UInt32.MaxValue && before_id == UInt32.MaxValue)
|
2008-01-22 16:54:44 +00:00
|
|
|
tref.Signals.Remove (name);
|
2005-02-02 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisconnectHandler (uint handler_id)
|
|
|
|
{
|
2008-01-22 16:54:44 +00:00
|
|
|
if (handler_id != UInt32.MaxValue && g_signal_handler_is_connected (tref.Handle, handler_id))
|
|
|
|
g_signal_handler_disconnect (tref.Handle, handler_id);
|
2005-02-02 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2005-03-25 17:57:15 +00:00
|
|
|
[CDeclCallback]
|
2005-02-02 21:57:15 +00:00
|
|
|
delegate void voidObjectDelegate (IntPtr handle, IntPtr gch);
|
|
|
|
|
2007-11-29 14:36:26 +00:00
|
|
|
static void voidObjectCallback (IntPtr handle, IntPtr data)
|
2005-02-02 21:57:15 +00:00
|
|
|
{
|
2007-03-06 20:10:15 +00:00
|
|
|
try {
|
2007-11-29 14:36:26 +00:00
|
|
|
if (data == IntPtr.Zero)
|
2007-03-09 14:22:43 +00:00
|
|
|
return;
|
2007-11-29 14:36:26 +00:00
|
|
|
GCHandle gch = (GCHandle) data;
|
|
|
|
if (gch.Target == null)
|
|
|
|
return;
|
|
|
|
Signal sig = gch.Target as Signal;
|
2007-03-06 20:10:15 +00:00
|
|
|
if (sig == null) {
|
|
|
|
ExceptionManager.RaiseUnhandledException (new Exception ("Unknown signal class GC handle received."), false);
|
|
|
|
return;
|
|
|
|
}
|
2005-02-02 21:57:15 +00:00
|
|
|
|
2007-03-06 20:10:15 +00:00
|
|
|
EventHandler handler = (EventHandler) sig.Handler;
|
|
|
|
handler (Object.GetObject (handle), EventArgs.Empty);
|
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionManager.RaiseUnhandledException (e, false);
|
|
|
|
}
|
2005-02-02 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static voidObjectDelegate event_handler_delegate;
|
|
|
|
static voidObjectDelegate EventHandlerDelegate {
|
|
|
|
get {
|
|
|
|
if (event_handler_delegate == null)
|
|
|
|
event_handler_delegate = new voidObjectDelegate (voidObjectCallback);
|
|
|
|
return event_handler_delegate;
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 17:11:11 +00:00
|
|
|
|
|
|
|
public static object Emit (GLib.Object instance, string signal_name, string detail, params object[] args)
|
|
|
|
{
|
|
|
|
uint signal_id = GetSignalId (signal_name, instance);
|
|
|
|
uint gquark = GetGQuarkFromString (detail);
|
|
|
|
GLib.Value[] vals = new GLib.Value [args.Length + 1];
|
|
|
|
GLib.ValueArray inst_and_params = new GLib.ValueArray ((uint) args.Length + 1);
|
|
|
|
|
|
|
|
vals [0] = new GLib.Value (instance);
|
|
|
|
inst_and_params.Append (vals [0]);
|
|
|
|
for (int i = 1; i < vals.Length; i++) {
|
|
|
|
vals [i] = new GLib.Value (args [i - 1]);
|
|
|
|
inst_and_params.Append (vals [i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLib.Value ret = GLib.Value.Empty;
|
|
|
|
|
|
|
|
g_signal_emitv (inst_and_params.ArrayPtr, signal_id, gquark, ref ret);
|
|
|
|
object ret_obj = ret.Val;
|
|
|
|
|
|
|
|
foreach (GLib.Value val in vals)
|
|
|
|
val.Dispose ();
|
|
|
|
ret.Dispose ();
|
|
|
|
|
|
|
|
return ret_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static uint GetGQuarkFromString (string str) {
|
|
|
|
IntPtr native_string = GLib.Marshaller.StringToPtrGStrdup (str);
|
|
|
|
uint ret = g_quark_from_string (native_string);
|
|
|
|
GLib.Marshaller.Free (native_string);
|
|
|
|
return ret;
|
|
|
|
}
|
2005-02-02 21:57:15 +00:00
|
|
|
|
2008-04-24 17:11:11 +00:00
|
|
|
private static uint GetSignalId (string signal_name, GLib.Object obj)
|
|
|
|
{
|
|
|
|
IntPtr typeid = gtksharp_get_type_id (obj.Handle);
|
|
|
|
IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup (signal_name);
|
|
|
|
uint signal_id = g_signal_lookup (native_name, typeid);
|
|
|
|
GLib.Marshaller.Free (native_name);
|
|
|
|
return signal_id;
|
|
|
|
}
|
|
|
|
|
2005-02-02 21:57:15 +00:00
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
2005-03-08 21:28:08 +00:00
|
|
|
static extern uint g_signal_connect_data(IntPtr obj, IntPtr name, Delegate cb, IntPtr gc_handle, IntPtr dummy, int flags);
|
2005-02-02 21:57:15 +00:00
|
|
|
|
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
|
|
|
static extern IntPtr g_signal_get_invocation_hint (IntPtr instance);
|
|
|
|
|
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
|
|
|
static extern void g_signal_handler_disconnect (IntPtr instance, uint handler);
|
|
|
|
|
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
|
|
|
static extern bool g_signal_handler_is_connected (IntPtr instance, uint handler);
|
2008-04-24 17:11:11 +00:00
|
|
|
|
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
|
|
|
static extern void g_signal_emitv (IntPtr instance_and_params, uint signal_id, uint gquark_detail, ref GLib.Value return_value);
|
|
|
|
|
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
|
|
|
static extern uint g_signal_lookup (IntPtr name, IntPtr itype);
|
|
|
|
|
|
|
|
//better not to expose g_quark_from_static_string () due to memory allocation issues
|
|
|
|
[DllImport("libglib-2.0-0.dll")]
|
|
|
|
static extern uint g_quark_from_string (IntPtr str);
|
|
|
|
|
|
|
|
[DllImport("glibsharpglue-2")]
|
|
|
|
static extern IntPtr gtksharp_get_type_id (IntPtr raw);
|
2005-02-02 21:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|