diff --git a/ChangeLog b/ChangeLog index a1ee6dd6a..7a413aa7d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2001-10-02 Mike Kestner + + * glib/Value.cs : Tried adding CallingConvention.Cdecl to all the + DllImports, but still couldn't get reliable Propery setting without + periodic NullReference exceptions. When all else fails, drop back + and punt. + * glib/Object.cs : Rewrote Set|GetProperty methods. Now they use + g_object_get|set and don't rely on GValues. The int, bool, and string + prop types are now working reliably. + * gtk/Window.cs : Update all Properties to use new GLib.Object + signatures. + * sample/HelloWorld.cs : added some more property usage for testing + purposes. + 2001-09-29 Mike Kestner * glib/Value.cs (int ctor): New constructor for int-based values. diff --git a/glib/Object.cs b/glib/Object.cs index d5deb2c15..70543eefe 100644 --- a/glib/Object.cs +++ b/glib/Object.cs @@ -126,17 +126,61 @@ namespace GLib { /// /// /// - /// Accesses a raw Object Property. + /// Accesses a string Property. /// - [DllImport("gobject-1.3.dll")] - static extern void g_object_get_property (IntPtr obj, - String name, - IntPtr val); + [DllImport("gobject-1.3.dll", CharSet=CharSet.Ansi, + CallingConvention=CallingConvention.Cdecl)] + static extern void g_object_get (IntPtr obj, IntPtr name, + out IntPtr val, IntPtr term); - public void GetProperty (String name, Value val) + public void GetProperty (String name, out String val) { - g_object_get_property (RawObject, name, val.MarshalAs); + IntPtr propval; + g_object_get (RawObject, + Marshal.StringToHGlobalAnsi (name), + out propval, new IntPtr (0)); + val = Marshal.PtrToStringAnsi (propval); + } + + /// + /// GetProperty Method + /// + /// + /// + /// Accesses a boolean Property. + /// + + [DllImport("gobject-1.3.dll", CharSet=CharSet.Ansi, + CallingConvention=CallingConvention.Cdecl)] + static extern void g_object_get (IntPtr obj, IntPtr name, + out bool val, IntPtr term); + + public void GetProperty (String name, out bool val) + { + g_object_get (RawObject, + Marshal.StringToHGlobalAnsi (name), + out val, new IntPtr (0)); + } + + /// + /// GetProperty Method + /// + /// + /// + /// Accesses an integer Property. + /// + + [DllImport("gobject-1.3.dll", CharSet=CharSet.Ansi, + CallingConvention=CallingConvention.Cdecl)] + static extern void g_object_get (IntPtr obj, IntPtr name, + out int val, IntPtr term); + + public void GetProperty (String name, out int val) + { + g_object_get (RawObject, + Marshal.StringToHGlobalAnsi (name), + out val, new IntPtr (0)); } /// @@ -144,17 +188,60 @@ namespace GLib { /// /// /// - /// Changes the value of a raw Object Property. + /// Changes the value of a string Property. /// - [DllImport("gobject-1.3.dll")] - static extern void g_object_set_property (IntPtr obj, - String name, - IntPtr val); + [DllImport("gobject-1.3.dll", CharSet=CharSet.Ansi, + CallingConvention=CallingConvention.Cdecl)] + static extern void g_object_set (IntPtr obj, IntPtr name, + IntPtr val, IntPtr term); - public void SetProperty (String name, Value val) + public void SetProperty (String name, String val) { - g_object_set_property (RawObject, name, val.MarshalAs); + g_object_set (RawObject, + Marshal.StringToHGlobalAnsi (name), + Marshal.StringToHGlobalAnsi (val), + new IntPtr (0)); + } + + /// + /// SetProperty Method + /// + /// + /// + /// Changes the value of an integer Property. + /// + + [DllImport("gobject-1.3.dll", CharSet=CharSet.Ansi, + CallingConvention=CallingConvention.Cdecl)] + static extern void g_object_set (IntPtr obj, IntPtr name, + int val, IntPtr term); + + public void SetProperty (String name, int val) + { + g_object_set (RawObject, + Marshal.StringToHGlobalAnsi (name), + val, new IntPtr (0)); + } + + /// + /// SetProperty Method + /// + /// + /// + /// Changes the value of a boolean Property. + /// + + [DllImport("gobject-1.3.dll", CharSet=CharSet.Ansi, + CallingConvention=CallingConvention.Cdecl)] + static extern void g_object_set (IntPtr obj, IntPtr name, + bool val, IntPtr term); + + public void SetProperty (String name, bool val) + { + g_object_set (RawObject, + Marshal.StringToHGlobalAnsi (name), + val, new IntPtr (0)); } /* @@ -218,7 +305,6 @@ gpointer g_object_steal_qdata (GObject *object, void g_object_watch_closure (GObject *object, GClosure *closure); void g_object_run_dispose (GObject *object); -gpointer g_value_get_object (const GValue *value); */ } diff --git a/glib/Value.cs b/glib/Value.cs index b2e6d3a80..c77459e8c 100755 --- a/glib/Value.cs +++ b/glib/Value.cs @@ -44,7 +44,8 @@ namespace GLib { /// value to it. /// - [DllImport("glib-1.3.dll")] + [DllImport("glib-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern IntPtr g_malloc0 (long n_bytes); public Value () @@ -73,7 +74,8 @@ namespace GLib { /// Constructs a Value from a specified boolean. /// - [DllImport("gobject-1.3.dll")] + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_boolean (IntPtr val, bool data); public Value (bool val) : this () @@ -90,7 +92,8 @@ namespace GLib { /// Constructs a Value from a specified integer. /// - [DllImport("gobject-1.3.dll")] + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_int (IntPtr val, int data); public Value (int val) : this () @@ -107,13 +110,15 @@ namespace GLib { /// Constructs a Value from a specified string. /// - [DllImport("gobject-1.3.dll")] + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_string (IntPtr val, - String data); + IntPtr data); public Value (String val) : this () { g_value_init (_val, TypeFundamentals.TypeString); - g_value_set_string (_val, val); + g_value_set_string (_val, + Marshal.StringToHGlobalAnsi (val)); } /// @@ -124,7 +129,8 @@ namespace GLib { /// Prepares a raw value to hold a specified type. /// - [DllImport("gobject-1.3.dll")] + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern void g_value_init (IntPtr val, TypeFundamentals type); @@ -143,7 +149,8 @@ namespace GLib { /// boolean value. /// - [DllImport("gobject-1.3.dll")] + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern bool g_value_get_boolean (IntPtr val); public static explicit operator bool (Value val) @@ -163,7 +170,8 @@ namespace GLib { /// integer value. /// - [DllImport("gobject-1.3.dll")] + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] static extern int g_value_get_int (IntPtr val); public static explicit operator int (Value val) @@ -183,14 +191,16 @@ namespace GLib { /// string value. /// - [DllImport("gobject-1.3.dll")] - static extern String g_value_get_string (IntPtr val); + [DllImport("gobject-1.3.dll", + CallingConvention=CallingConvention.Cdecl)] + static extern IntPtr g_value_get_string (IntPtr val); public static explicit operator String (Value val) { // FIXME: Insert an appropriate exception here if // _val.type indicates an error. - return g_value_get_string (val._val); + return Marshal.PtrToStringAnsi ( + g_value_get_string (val._val)); } /// diff --git a/gtk/Window.cs b/gtk/Window.cs index f55363c5f..1c0ca6f09 100755 --- a/gtk/Window.cs +++ b/gtk/Window.cs @@ -72,14 +72,12 @@ namespace Gtk { public bool AllowGrow { get { - Value val = new Value ( - TypeFundamentals.TypeBoolean); - GetProperty ("allow-grow", val); - return ((bool) val); + bool val; + GetProperty ("allow-grow", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("allow-grow", val); + SetProperty ("allow-grow", value); } } @@ -94,14 +92,12 @@ namespace Gtk { public bool AllowShrink { get { - Value val = new Value ( - TypeFundamentals.TypeBoolean); - GetProperty ("allow-shrink", val); - return ((bool) val); + bool val; + GetProperty ("allow-shrink", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("allow-shrink", val); + SetProperty ("allow-shrink", value); } } @@ -115,18 +111,15 @@ namespace Gtk { public int DefaultHeight { get { - Value val = new Value ( - TypeFundamentals.TypeInt); - GetProperty ("default-height", val); - return ((int) val); + int val; + GetProperty ("default-height", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("default-height", val); + SetProperty ("default-height", value); } } -/* /// /// DefaultSize Property /// @@ -137,14 +130,14 @@ namespace Gtk { public Size DefaultSize { get { - GValue val = GetProp ("default-size"); - return (val != 0); + return new Size (DefaultWidth, DefaultHeight); } set { - SetProp ("default-size", new GValue (value)); + DefaultWidth = value.Width; + DefaultHeight = value.Height; } } -*/ + /// /// DefaultWidth Property /// @@ -155,14 +148,12 @@ namespace Gtk { public int DefaultWidth { get { - Value val = new Value ( - TypeFundamentals.TypeInt); - GetProperty ("default-width", val); - return ((int) val); + int val; + GetProperty ("default-width", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("default-width", val); + SetProperty ("default-width", value); } } @@ -178,14 +169,12 @@ namespace Gtk { public bool DestroyWithParent { get { - Value val = new Value ( - TypeFundamentals.TypeBoolean); - GetProperty ("destroy-with-parent", val); - return ((bool) val); + bool val; + GetProperty ("destroy-with-parent", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("destroy-with-parent", val); + SetProperty ("destroy-with-parent", value); } } @@ -202,14 +191,12 @@ namespace Gtk { public bool Modal { get { - Value val = new Value ( - TypeFundamentals.TypeBoolean); - GetProperty ("modal", val); - return ((bool) val); + bool val; + GetProperty ("modal", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("modal", val); + SetProperty ("modal", value); } } @@ -244,14 +231,12 @@ namespace Gtk { public bool Resizable { get { - Value val = new Value ( - TypeFundamentals.TypeBoolean); - GetProperty ("resizable", val); - return ((bool) val); + bool val; + GetProperty ("resizable", out val); + return (val); } set { - Value val = new Value (value); - SetProperty ("resizable", val); + SetProperty ("resizable", value); } } @@ -263,18 +248,14 @@ namespace Gtk { /// The Title displayed in the Window's Title Bar. /// - [DllImport("gobject-1.3.dll")] - static extern void g_object_set (IntPtr obj, String name, - IntPtr val, IntPtr term); - public String Title { + get { + String val; + GetProperty ("title", out val); + return val; + } set { - g_object_set (RawObject, "title", - Marshal.StringToHGlobalAnsi (value), new IntPtr (0)); -/* FIXME: When the String value setting problem is solved. - Value val = new Value (value); - SetProperty ("title", val); -*/ + SetProperty ("title", value); } } } diff --git a/sample/HelloWorld.cs b/sample/HelloWorld.cs index 47890c8a1..77f6c7db9 100755 --- a/sample/HelloWorld.cs +++ b/sample/HelloWorld.cs @@ -8,6 +8,7 @@ namespace GtkSamples { using Gtk; using System; + using System.Drawing; public class HelloWorld { @@ -15,6 +16,10 @@ namespace GtkSamples { { Application.Init (ref args); Window win = new Window ("Gtk# Hello World"); + win.DefaultSize = new Size (400, 400); + System.Console.WriteLine (win.Title); + System.Console.WriteLine (win.DefaultSize); + System.Console.WriteLine (win.AllowShrink); win.DeleteEvent += new EventHandler (Window_Delete); win.Show (); Application.Run ();