ColorSelection bug fixes (was Re: [Gtk-sharp-list] problem marshalling struct data)

Lee Mallabone
12 Apr 2003 14:08:40 +0100

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

On Sat, 2003-04-12 at 03:24, Mike Kestner wrote:

> Only thing I can think of is to return an IntPtr, and in a .custom,
> increment the pointer while iterating on n_color using Gdk.Color.New to
> marshal the individual Colors. May need to go into unsafe mode to
> increment the IntPtr, but I'm not sure off the top of my head.

I was trying to do that originally, but in a way that didn't actually
manipulate the IntPtr, so it didn't work.

It works now (though PaletteFromString is unsafe).

I've attached patches that fix up bugs in ColorSelection. They make
PaletteFromString() and PaletteToString have good method signatures as
well as making PreviousColor a single property. This resolves bugs
#27835 & #38672.

Okay to commit?

I've also attached ColorSample.cs which is actually a visual test that
things work. I could probably turn this into an NUnit test without too
much trouble. Does Gtk# have a framework/suite setup for NUnit tests? If
not, is this worth putting on cvs anywhere, (it crashes without my
patches, works correctly with them).



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 <>
// 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 IntPtr colors, out int n_colors);

		public unsafe 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];
			for (int i=0; i < n_colors; i++)
				colors[i] = Gdk.Color.New(parsedColors);
				parsedColors = (IntPtr) ((int)parsedColors + sizeof(Gdk.Color));
			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=colorsel.diff
Content-Type: text/x-patch; name=colorsel.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	12 Apr 2003 12:16:11 -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">
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	12 Apr 2003 12:16:15 -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: ");
			foreach (Gdk.Color col in parsedColors)

		colors[1] = parsedColors[1];	
		Console.WriteLine("------ PaletteToString test ------");
		parsedPalette = ColorSelection.PaletteToString(colors);
		Console.WriteLine("Got palette as: " + parsedPalette);