2002-08-03 22:24:37 +00:00
//
// Gtk.Widget.custom - Gtk Widget class customizations
//
2007-04-12 17:17:09 +00:00
// Authors: Rachel Hestilow <hestilow@ximian.com>,
// Brad Taylor <brad@getcoded.net>
2002-08-03 22:24:37 +00:00
//
2007-04-12 17:17:09 +00:00
// Copyright (C) 2007 Brad Taylor
2002-08-03 22:24:37 +00:00
// Copyright (C) 2002 Rachel Hestilow
//
// This code is inserted after the automatically generated code.
//
2004-06-25 18:42:19 +00:00
//
// 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.
2009-08-30 20:06:33 +00:00
[DllImport ("gtksharpglue-3")]
2004-05-31 18:48:25 +00:00
static extern void gtksharp_gtk_widget_set_window (IntPtr widget, IntPtr window);
2002-09-24 03:21:37 +00:00
public Gdk.Window GdkWindow {
get {
2009-08-13 14:56:18 +00:00
return this.Window;
2002-09-24 03:21:37 +00:00
}
2004-05-31 18:48:25 +00:00
set {
Gdk.Window window = value as Gdk.Window;
gtksharp_gtk_widget_set_window (Handle, window.Handle);
}
2003-01-30 02:56:49 +00:00
}
2004-01-28 04:08:29 +00:00
public void AddAccelerator (string accel_signal, AccelGroup accel_group, AccelKey accel_key)
{
2004-02-10 16:04:50 +00:00
this.AddAccelerator (accel_signal, accel_group, (uint) accel_key.Key, accel_key.AccelMods, accel_key.AccelFlags);
2004-01-28 04:08:29 +00:00
}
2004-02-14 17:28:11 +00:00
2004-06-07 14:26:08 +00:00
public int FocusLineWidth {
get {
2009-04-27 17:55:30 +00:00
return (int) StyleGetProperty ("focus-line-width");
2004-06-07 14:26:08 +00:00
}
}
2004-09-23 16:50:18 +00:00
2009-04-27 17:55:30 +00:00
struct GClosure {
long fields;
IntPtr marshaler;
IntPtr data;
IntPtr notifiers;
}
2004-09-23 16:50:18 +00:00
2009-09-03 19:50:53 +00:00
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
2009-04-27 17:55:30 +00:00
delegate void ClosureMarshal (IntPtr closure, IntPtr return_val, uint n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data);
2009-09-03 19:50:53 +00:00
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-27 17:55:30 +00:00
static extern IntPtr g_closure_new_simple (int closure_size, IntPtr dummy);
2004-09-23 16:50:18 +00:00
2009-09-03 19:50:53 +00:00
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-27 17:55:30 +00:00
static extern void g_closure_set_marshal (IntPtr closure, ClosureMarshal marshaler);
2004-09-23 16:50:18 +00:00
2009-04-27 17:55:30 +00:00
static IntPtr CreateClosure (ClosureMarshal marshaler) {
IntPtr raw_closure = g_closure_new_simple (Marshal.SizeOf (typeof (GClosure)), IntPtr.Zero);
g_closure_set_marshal (raw_closure, marshaler);
return raw_closure;
}
2009-09-03 19:50:53 +00:00
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-27 17:55:30 +00:00
static extern uint g_signal_newv (IntPtr signal_name, IntPtr gtype, GLib.Signal.Flags signal_flags, IntPtr closure, IntPtr accumulator, IntPtr accu_data, IntPtr c_marshaller, IntPtr return_type, uint n_params, [MarshalAs (UnmanagedType.LPArray)] IntPtr[] param_types);
static uint RegisterSignal (string signal_name, GLib.GType gtype, GLib.Signal.Flags signal_flags, GLib.GType return_type, GLib.GType[] param_types, ClosureMarshal marshaler)
2004-09-23 16:50:18 +00:00
{
2009-04-27 17:55:30 +00:00
IntPtr[] native_param_types = new IntPtr [param_types.Length];
for (int parm_idx = 0; parm_idx < param_types.Length; parm_idx++)
native_param_types [parm_idx] = param_types [parm_idx].Val;
IntPtr native_signal_name = GLib.Marshaller.StringToPtrGStrdup (signal_name);
2007-03-09 14:22:43 +00:00
try {
2009-04-27 17:55:30 +00:00
return g_signal_newv (native_signal_name, gtype.Val, signal_flags, CreateClosure (marshaler), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, return_type.Val, (uint) param_types.Length, native_param_types);
} finally {
GLib.Marshaller.Free (native_signal_name);
}
}
static void ActivateMarshal_cb (IntPtr raw_closure, IntPtr return_val, uint n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data)
2007-04-12 17:17:09 +00:00
{
2008-05-22 18:55:22 +00:00
try {
2009-04-27 17:55:30 +00:00
GLib.Value inst_val = (GLib.Value) Marshal.PtrToStructure (param_values, typeof (GLib.Value));
Widget inst;
2008-05-22 18:55:22 +00:00
try {
2009-04-27 17:55:30 +00:00
inst = inst_val.Val as Widget;
2008-05-22 18:55:22 +00:00
} catch (GLib.MissingIntPtrCtorException) {
return;
}
2009-04-27 17:55:30 +00:00
inst.OnActivate ();
2008-05-22 18:55:22 +00:00
} catch (Exception e) {
GLib.ExceptionManager.RaiseUnhandledException (e, false);
}
2007-04-12 17:17:09 +00:00
}
2009-04-27 17:55:30 +00:00
static ClosureMarshal ActivateMarshalCallback;
2007-04-12 17:17:09 +00:00
static void ConnectActivate (GLib.GType gtype)
{
2009-04-27 17:55:30 +00:00
if (ActivateMarshalCallback == null)
ActivateMarshalCallback = new ClosureMarshal (ActivateMarshal_cb);
2009-08-13 14:56:18 +00:00
GtkWidgetClass klass = GetClassStruct (gtype, false);
klass.ActivateSignal = RegisterSignal ("activate_signal", gtype, GLib.Signal.Flags.RunLast, GLib.GType.None, new GLib.GType [0], ActivateMarshalCallback);
OverrideClassStruct (gtype, klass);
2007-04-12 17:17:09 +00:00
}
[GLib.DefaultSignalHandler (Type=typeof (Gtk.Widget), ConnectionMethod="ConnectActivate")]
protected virtual void OnActivate ()
{
}
2004-12-23 22:59:59 +00:00
private class BindingInvoker {
System.Reflection.MethodInfo mi;
object[] parms;
public BindingInvoker (System.Reflection.MethodInfo mi, object[] parms)
{
this.mi = mi;
this.parms = parms;
}
public void Invoke (Widget w)
{
mi.Invoke (w, parms);
}
}
2009-04-27 17:55:30 +00:00
/* As gtk_binding_entry_add_signall only allows passing long, double and string parameters to the specified signal, we cannot pass a pointer to the BindingInvoker directly
* to the signal. Instead, the signal takes the index of the BindingInvoker in binding_invokers.
*/
static ArrayList binding_invokers;
2004-12-23 22:59:59 +00:00
2009-04-27 17:55:30 +00:00
static void BindingMarshal_cb (IntPtr raw_closure, IntPtr return_val, uint n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data)
2004-12-23 22:59:59 +00:00
{
2007-03-09 14:22:43 +00:00
try {
2009-04-27 17:55:30 +00:00
GLib.Value[] inst_and_params = new GLib.Value [n_param_vals];
int gvalue_size = Marshal.SizeOf (typeof (GLib.Value));
for (int idx = 0; idx < n_param_vals; idx++)
inst_and_params [idx] = (GLib.Value) Marshal.PtrToStructure (new IntPtr (param_values.ToInt64 () + idx * gvalue_size), typeof (GLib.Value));
Widget w = inst_and_params [0].Val as Widget;
2009-09-24 19:54:07 +00:00
BindingInvoker invoker = binding_invokers [(int) (long) inst_and_params [1]] as BindingInvoker;
2007-03-09 14:22:43 +00:00
invoker.Invoke (w);
} catch (Exception e) {
GLib.ExceptionManager.RaiseUnhandledException (e, false);
}
2004-12-23 22:59:59 +00:00
}
2009-04-27 17:55:30 +00:00
static ClosureMarshal binding_delegate;
static ClosureMarshal BindingDelegate {
2004-12-23 22:59:59 +00:00
get {
if (binding_delegate == null)
2009-04-27 17:55:30 +00:00
binding_delegate = new ClosureMarshal (BindingMarshal_cb);
2004-12-23 22:59:59 +00:00
return binding_delegate;
}
}
2011-02-09 22:54:12 +00:00
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-27 17:55:30 +00:00
static extern IntPtr gtk_binding_set_by_class (IntPtr class_ptr);
2004-12-23 22:59:59 +00:00
2011-02-09 22:54:12 +00:00
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-27 17:55:30 +00:00
static extern void gtk_binding_entry_add_signall (IntPtr binding_set, uint keyval, Gdk.ModifierType modifiers, IntPtr signal_name, IntPtr binding_args);
[StructLayout(LayoutKind.Sequential)]
struct GtkBindingArg {
public IntPtr arg_type;
public GtkBindingArgData data;
}
[StructLayout(LayoutKind.Explicit)]
struct GtkBindingArgData {
#if WIN64LONGS
2009-06-15 19:48:08 +00:00
[FieldOffset (0)] public int long_data;
2009-04-27 17:55:30 +00:00
#else
2009-06-15 19:48:08 +00:00
[FieldOffset (0)] public IntPtr long_data;
2009-04-27 17:55:30 +00:00
#endif
[FieldOffset (0)] public double double_data;
[FieldOffset (0)] public IntPtr string_data;
}
2004-12-23 22:59:59 +00:00
static void ClassInit (GLib.GType gtype, Type t)
{
object[] attrs = t.GetCustomAttributes (typeof (BindingAttribute), true);
2009-04-27 17:55:30 +00:00
if (attrs.Length == 0) return;
2004-12-23 22:59:59 +00:00
2009-04-27 17:55:30 +00:00
string signame = t.Name.Replace (".", "_") + "_bindings";
IntPtr native_signame = GLib.Marshaller.StringToPtrGStrdup (signame);
RegisterSignal (signame, gtype, GLib.Signal.Flags.RunLast | GLib.Signal.Flags.Action, GLib.GType.None, new GLib.GType[] {GLib.GType.Long}, BindingDelegate);
2004-12-23 22:59:59 +00:00
2009-04-27 17:55:30 +00:00
if (binding_invokers == null)
binding_invokers = new ArrayList ();
2004-12-23 22:59:59 +00:00
foreach (BindingAttribute attr in attrs) {
System.Reflection.MethodInfo mi = t.GetMethod (attr.Handler, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
if (mi == null)
throw new Exception ("Instance method " + attr.Handler + " not found in " + t);
2009-04-27 17:55:30 +00:00
GtkBindingArg arg = new GtkBindingArg ();
arg.arg_type = GLib.GType.Long.Val;
2009-06-15 19:48:08 +00:00
int binding_invoker_idx = binding_invokers.Add (new BindingInvoker (mi, attr.Parms));
#if WIN64LONGS
2009-08-13 14:56:18 +00:00
arg.data.long_data = binding_invoker_idx;
2009-06-15 19:48:08 +00:00
#else
2009-08-13 14:56:18 +00:00
arg.data.long_data = new IntPtr (binding_invoker_idx);
2009-06-15 19:48:08 +00:00
#endif
2009-04-27 17:55:30 +00:00
GLib.SList binding_args = new GLib.SList (new object[] {arg}, typeof (GtkBindingArg), false, false);
2009-08-08 23:42:15 +00:00
gtk_binding_entry_add_signall (gtk_binding_set_by_class (gtype.GetClassPtr ()), (uint) attr.Key, attr.Mod, native_signame, binding_args.Handle);
2009-04-27 17:55:30 +00:00
binding_args.Dispose ();
2004-12-23 22:59:59 +00:00
}
2009-04-27 17:55:30 +00:00
GLib.Marshaller.Free (native_signame);
2004-12-23 22:59:59 +00:00
}
2009-04-21 08:11:35 +00:00
public object StyleGetProperty (string property_name)
{
GLib.Value value;
try {
value = StyleGetPropertyValue (property_name);
} catch (ArgumentException) {
return null;
2007-04-24 19:40:27 +00:00
}
2009-04-21 08:11:35 +00:00
object ret = value.Val;
value.Dispose ();
return ret;
2005-02-11 15:59:22 +00:00
}
2005-05-11 18:04:52 +00:00
2011-02-09 22:54:12 +00:00
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-21 08:11:35 +00:00
static extern IntPtr gtk_widget_class_find_style_property (IntPtr class_ptr, IntPtr property_name);
2011-02-09 22:54:12 +00:00
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2009-04-21 08:11:35 +00:00
static extern IntPtr gtk_widget_style_get_property (IntPtr inst, IntPtr property_name, ref GLib.Value value);
2007-04-17 12:16:58 +00:00
2009-04-21 08:11:35 +00:00
internal GLib.Value StyleGetPropertyValue (string property_name)
{
IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup (property_name);
try {
2009-08-08 23:42:15 +00:00
IntPtr pspec_ptr = gtk_widget_class_find_style_property (this.LookupGType ().GetClassPtr (), native_name);
2009-04-21 08:11:35 +00:00
if (pspec_ptr == IntPtr.Zero)
throw new ArgumentException (String.Format ("Cannot find style property \"{0}\"", property_name));
GLib.Value value = new GLib.Value ((new GLib.ParamSpec (pspec_ptr)).ValueType);
gtk_widget_style_get_property (Handle, native_name, ref value);
return value;
} finally {
GLib.Marshaller.Free (native_name);
}
2007-04-17 12:16:58 +00:00
}
2011-02-09 22:54:12 +00:00
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
2005-05-11 18:04:52 +00:00
static extern IntPtr gtk_widget_list_mnemonic_labels (IntPtr raw);
public Widget[] ListMnemonicLabels ()
{
IntPtr raw_ret = gtk_widget_list_mnemonic_labels (Handle);
if (raw_ret == IntPtr.Zero)
return new Widget [0];
GLib.List list = new GLib.List(raw_ret);
Widget[] result = new Widget [list.Count];
for (int i = 0; i < list.Count; i++)
result [i] = list [i] as Widget;
return result;
}
2005-08-29 19:22:23 +00:00
public void ModifyBase (Gtk.StateType state)
{
gtk_widget_modify_base (Handle, (int) state, IntPtr.Zero);
}
public void ModifyBg (Gtk.StateType state)
{
gtk_widget_modify_bg (Handle, (int) state, IntPtr.Zero);
}
public void ModifyFg (Gtk.StateType state)
{
gtk_widget_modify_fg (Handle, (int) state, IntPtr.Zero);
}
public void ModifyText (Gtk.StateType state)
{
gtk_widget_modify_text (Handle, (int) state, IntPtr.Zero);
}
2009-11-30 17:49:04 +00:00
public void Path (out string path, out string path_reversed)
{
uint len;
Path (out len, out path, out path_reversed);
}
2011-02-09 04:15:37 +00:00
// Code from Object.custom in 2.x
// Object is gone in 3.x
static Hashtable destroy_handlers;
static Hashtable DestroyHandlers {
get {
if (destroy_handlers == null)
destroy_handlers = new Hashtable ();
return destroy_handlers;
}
}
private static void OverrideDestroyed (GLib.GType gtype)
{
// Do Nothing. We don't want to hook into the native vtable.
// We will manually invoke the VM on signal invocation. The signal
// always raises before the default handler because this signal
// is RUN_CLEANUP.
}
[GLib.DefaultSignalHandler(Type=typeof(Gtk.Widget), ConnectionMethod="OverrideDestroyed")]
protected virtual void OnDestroyed ()
{
if (DestroyHandlers.Contains (Handle)) {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
handler (this, EventArgs.Empty);
DestroyHandlers.Remove (Handle);
}
}
[GLib.Signal("destroy")]
public event EventHandler Destroyed {
add {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
DestroyHandlers [Handle] = Delegate.Combine (handler, value);
}
remove {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
handler = (EventHandler) Delegate.Remove (handler, value);
if (handler != null)
DestroyHandlers [Handle] = handler;
else
DestroyHandlers.Remove (Handle);
}
}
event EventHandler InternalDestroyed {
add {
GLib.Signal sig = GLib.Signal.Lookup (this, "destroy");
sig.AddDelegate (value);
}
remove {
GLib.Signal sig = GLib.Signal.Lookup (this, "destroy");
sig.RemoveDelegate (value);
}
}
static void NativeDestroy (object o, EventArgs args)
{
Gtk.Widget widget = o as Gtk.Widget;
if (widget == null)
return;
widget.OnDestroyed ();
}
static EventHandler native_destroy_handler;
static EventHandler NativeDestroyHandler {
get {
if (native_destroy_handler == null)
native_destroy_handler = new EventHandler (NativeDestroy);
return native_destroy_handler;
}
}
protected override void CreateNativeObject (string[] names, GLib.Value[] vals)
{
base.CreateNativeObject (names, vals);
}
public override void Dispose ()
{
if (Handle == IntPtr.Zero)
return;
InternalDestroyed -= NativeDestroyHandler;
base.Dispose ();
}
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void g_object_ref_sink (IntPtr raw);
protected override IntPtr Raw {
get {
return base.Raw;
}
set {
if (value != IntPtr.Zero)
g_object_ref_sink (value);
base.Raw = value;
if (value != IntPtr.Zero)
InternalDestroyed += NativeDestroyHandler;
}
}
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void gtk_widget_destroy (IntPtr raw);
public virtual void Destroy ()
{
if (Handle == IntPtr.Zero)
return;
gtk_widget_destroy (Handle);
InternalDestroyed -= NativeDestroyHandler;
}
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool g_object_is_floating (IntPtr raw);
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void g_object_force_floating (IntPtr raw);
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void g_object_unref (IntPtr raw);
public bool IsFloating {
get {
return g_object_is_floating (Handle);
}
set {
if (value == true) {
if (!IsFloating)
g_object_force_floating (Handle);
} else {
g_object_ref_sink (Handle);
g_object_unref (Handle);
}
}
}