[Gtk-sharp-list] How to change the cursor to a xpm file

Martin Willemoes Hansen mwh@sysrq.dk
Tue, 11 Nov 2003 12:51:16 +0100


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

Hi!

Im trying to find out how I can change the cursor to a given xpm file?

I have attached the xpm file I want to use.

Below is the code I have comed up with so far:

// Getting the foreground and background colors of the xpm file.
Gdk.Color bgcolor = new Gdk.Color (0,0,0);
Gdk.Color fgcolor = new Gdk.Color (0,0,0);
Gdk.Color.Parse ("grey100", ref bgcolor);
Gdk.Color.Parse ("black", ref fgcolor);

// Creation of a pixmap object, to be used as a cursor.
// This pixmap is eaten by the cursor code, but just displays garbage.

Pixmap pm = new Pixmap (new Drawable (IntPtr.Zero), 32, 32, 1);

// From the code, it seems like 1 ie depth of 1 is important, if it is
// changed X fails to change the cursor

// The code below loads a XPM file
Pixmap pm2 = Pixmap.CreateFromXpm (main.GdkWindow, 
				   new Bitmap (IntPtr.Zero), 
				   bgcolor, 
				   "/home/user/zoom.xpm");

// If I assign it to a Gtk.Image it is displayed.
image2.Pixmap = pm2;

// Here I try to change the color depth of the pixbuf from 16 to 1
// First of all the visual can not be created, I get this error
// Unhandled Exception: System.InvalidCastException: Cannot cast from 
// source type to destination typein <0x0006f> 
// Gdk.Visual:GetBestWithBoth (int,Gdk.VisualType)

Visual visual = Visual.GetBestWithBoth(1, VisualType.Grayscale);
pm2.Colormap = new Colormap (visual, true);

// This is the final code which tries to set the cursor. 

main.GdkWindow.Cursor = new Cursor (pm2, pm2, 
	                            fgcolor, bgcolor,
				    0, 0);

When used with the loaded xpm file I get this error:
The program '<unknown>' received an X Window System error.
Which I guess is cause it can only take a pixbuf with depth 1, like the
bogus one pm I created first.

Hope someone can help me out by pointing me in the right direction.
-- 
Martin Willemoes Hansen

--------------------------------------------------------
E-Mail	mwh@sysrq.dk	Website	mwh.sysrq.dk
IRC     MWH, freenode.net
--------------------------------------------------------               


--=-VSjDXpB8pz85oKEY4bRG
Content-Disposition: attachment; filename=zoom.xpm
Content-Type: image/x-xpixmap; name=zoom.xpm
Content-Transfer-Encoding: 7bit

/* XPM */
static char *zoom[] = {
/* columns rows colors chars-per-pixel */
"32 32 2 1",
"  c black",
". c gray100",
/* pixels */
"................................",
"................................",
"......     .....................",
"..... ..... ....................",
".... ....... ...................",
"... ......... ..................",
".. ........... .................",
".. ........... .................",
".. ........... .................",
".. ........... .................",
".. ........... .................",
"... ......... ..................",
".... .......  ..................",
"..... .....    .................",
"......     ..   ................",
"..............   ...............",
"...............   ..............",
"................   .............",
".................   ............",
"..................   ...........",
"...................   ..........",
".................... ...........",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................"
};

--=-VSjDXpB8pz85oKEY4bRG--