[Gtk-sharp-list] problem marshalling struct data

Lee Mallabone gnome@fonicmonkey.net
10 Apr 2003 18:46:23 +0100

Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi all,

I'm trying to fix up Gtk.ColorSelection and am having trouble mapping
types in the PaletteFromString method.

I need to wrap a C parameter of type GdkColor** onto a C# parameter of:
out Gdk.Color[].

How should this be done? The generator does not detect the type
correctly so I tried manually. 

It didn't work so I then tried some extra logic with "out IntPtr[]".
This is in the attached ColorSelection.custom but also doesn't work.

Could someone who knows about wrapping structs take a look and tell me
what I'm doing wrong? I've attached a test program that should work but
currently throws a NullReferenceException.

I've been staring at this for hours now; I'm sure the solution is
simple, it just won't present itself to me!



Content-Disposition: attachment; filename=ColorGtkMetadata.diff
Content-Type: text/x-patch; name=ColorGtkMetadata.diff; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: 7bit

Index: sources/Gtk.metadata
RCS file: /cvs/public/gtk-sharp/sources/Gtk.metadata,v
retrieving revision 1.48
diff -u -r1.48 Gtk.metadata
--- sources/Gtk.metadata	2 Apr 2003 08:26:36 -0000	1.48
+++ sources/Gtk.metadata	10 Apr 2003 17:06:39 -0000
@@ -1606,6 +1606,20 @@
+  <class name="GtkColorSelection">
+    <method>PaletteToString</method>
+    <method>PaletteFromString</method>
+    <method>GetPreviousColor</method>
+    <method>SetPreviousColor</method>
+  </class>
+  <data>
+    <attribute target="method">
+      <name>hidden</name>
+      <value>1</value>
+    </attribute>
+  </data>
   <class name="GtkWidget">

Content-Disposition: attachment; filename=ColorSelection.custom
Content-Type: text/plain; name=ColorSelection.custom; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: 7bit

// Gtk.ColorSelection.custom - customizations and corrections for ColorSelection
// Author: Lee Mallabone <gnome@fonicmonkey.net>
// Author: Justin Malcolm

		static extern string gtk_color_selection_palette_to_string(Gdk.Color[] colors, int n_colors);

		/// <summary> PaletteToString Method </summary>
		public static string PaletteToString(Gdk.Color[] colors) {
			int n_colors = colors.Length;
			string raw_ret = gtk_color_selection_palette_to_string(colors, n_colors);
			string ret = raw_ret;
			return ret;
		static extern bool gtk_color_selection_palette_from_string(string str, out Gdk.Color[] colors, out int n_colors);

		/// <summary> PaletteFromString Method </summary>
		public static Gdk.Color[] PaletteFromString(string str) {
			IntPtr[] parsedColors;
			int n_colors;
			bool raw_ret = gtk_color_selection_palette_from_string(str, out parsedColors, out n_colors);
			// If things failed, return silently
			if (!raw_ret)
				return null;
			System.Console.WriteLine("Raw call finished, making " + n_colors + " actual colors");
			Gdk.Color[] colors = new Gdk.Color[n_colors];
			int i=0;
			foreach (IntPtr reference in parsedColors)
				colors[i] = Gdk.Color.New(reference);
			return colors;

		static extern void gtk_color_selection_set_previous_color(IntPtr raw, ref Gdk.Color color);

		static extern void gtk_color_selection_get_previous_color(IntPtr raw, out Gdk.Color color);

		// Create Gtk# property to replace two Gtk+ functions
		public Gdk.Color PreviousColor
				Gdk.Color returnColor;
				gtk_color_selection_get_previous_color(Handle, out returnColor);	
				return returnColor;
				gtk_color_selection_set_previous_color(Handle, ref value);

Content-Disposition: attachment; filename=gtk-api.xml.diff
Content-Type: text/x-patch; name=gtk-api.xml.diff; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: 7bit

Index: api/gtk-api.xml
RCS file: /cvs/public/gtk-sharp/api/gtk-api.xml,v
retrieving revision 1.30
diff -u -r1.30 gtk-api.xml
--- api/gtk-api.xml	2 Apr 2003 08:26:36 -0000	1.30
+++ api/gtk-api.xml	10 Apr 2003 17:42:31 -0000
@@ -2152,7 +2152,7 @@
       <method name="GetPreviousAlpha" cname="gtk_color_selection_get_previous_alpha">
         <return-type type="guint16"/>
-      <method name="GetPreviousColor" cname="gtk_color_selection_get_previous_color">
+      <method name="GetPreviousColor" cname="gtk_color_selection_get_previous_color" hidden="1">
         <return-type type="void"/>
           <parameter type="GdkColor*" name="color"/>
@@ -2165,7 +2165,7 @@
         <return-type type="gboolean"/>
       <constructor cname="gtk_color_selection_new"/>
-      <method name="PaletteFromString" cname="gtk_color_selection_palette_from_string" shared="true">
+      <method name="PaletteFromString" cname="gtk_color_selection_palette_from_string" shared="true" hidden="1">
         <return-type type="gboolean"/>
           <parameter type="const-gchar*" name="str"/>
@@ -2173,7 +2173,7 @@
           <parameter type="gint*" name="n_colors"/>
-      <method name="PaletteToString" cname="gtk_color_selection_palette_to_string" shared="true">
+      <method name="PaletteToString" cname="gtk_color_selection_palette_to_string" shared="true" hidden="1">
         <return-type type="gchar*"/>
           <parameter type="const-GdkColor*" name="colors"/>
@@ -2216,7 +2216,7 @@
           <parameter type="guint16" name="alpha"/>
-      <method name="SetPreviousColor" cname="gtk_color_selection_set_previous_color">
+      <method name="SetPreviousColor" cname="gtk_color_selection_set_previous_color" hidden="1">
         <return-type type="void"/>
           <parameter type="GdkColor*" name="color"/>

Content-Disposition: attachment; filename=ColorSample.cs
Content-Type: text/plain; name=ColorSample.cs; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: 7bit

using Gtk;
using Gdk;
using System;

public class ColorSample {
	public static void Main()
		// Setup simple colors
		Gdk.Color black = new Gdk.Color(0x00, 0x00, 0x00);
		Gdk.Color white = new Gdk.Color(0xFF, 0xFF, 0xFF);
		Gdk.Color[] colors = new Gdk.Color[] { black, white };
		Console.WriteLine("------ PaletteToString test ------");
		string parsedPalette = ColorSelection.PaletteToString(colors);
		Console.WriteLine("Got palette as: " + parsedPalette);

		Console.WriteLine("------ PaletteFromString test ------");
		Gdk.Color[] parsedColors = ColorSelection.PaletteFromString(parsedPalette);
		if (parsedColors == null)
			Console.WriteLine("Parsed palette array is null");
			Console.WriteLine("Parsed palette array is: ");