GNOME Bugzilla – Bug 541377
gdk cursors are only black and white on win32
Last modified: 2018-02-10 03:41:36 UTC
Please describe the problem: Essentially tge cursor example from the gdk reference manual; http://library.gnome.org/devel/gdk/stable/gdk-Cursors.html #include <gtk/gtk.h> /* This data is in X bitmap format, and can be created with the 'bitmap' * utility. */ #define cursor1_width 16 #define cursor1_height 16 static unsigned char cursor1_bits[] = { 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20, 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10, 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01}; static unsigned char cursor1mask_bits[] = { 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31, 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18, 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01}; GdkCursor *cursor; static gint expose_cb( GtkWidget *pWidget, GdkEventExpose *pEvent, gpointer pData ) { gdk_window_set_cursor (pWidget->window, cursor); } int main ( int argc, char **argv ) { GtkWindow *lWindow; GdkPixmap *source, *mask; GdkColor fg = { 0, 65535, 0, 0 }; /* Red. */ GdkColor bg = { 0, 0, 0, 65535 }; /* Blue. */ gtk_init (&argc, &argv); lWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); gtk_widget_set_size_request( lWindow, 400, 400 ); source = gdk_bitmap_create_from_data (NULL, cursor1_bits, cursor1_width, cursor1_height); mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits, cursor1_width, cursor1_height); cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8); g_signal_connect( lWindow, "expose_event", G_CALLBACK( expose_cb ), NULL ); gtk_widget_show_all( lWindow ); gtk_main(); } Steps to reproduce: 1. build above on linux 2. cursor is red and blue as expected 3. build on win32 and the cursor is only black and white Actual results: Cursor is black and white Expected results: Cursor should be red and blue Does this happen every time? yes Other information: A fix would be greatly appreciated.
gdk_cursor_new_from_pixmap() in gdkcursor-win32.c contains the telltale comment: /* Such complex bit manipulation for this simple task, sigh. * The X cursor and Windows cursor concepts are quite different. * We assume here that we are always called with fg == black and * bg == white, *or* the other way around. Random colours won't work. * (Well, you will get a cursor, but not in those colours.) */ http://www.codeproject.com/KB/graphics/ColorCursorFromBmp.aspx seems to have sample code describing how to create coloured cursors. Need to investigate...
Created attachment 142904 [details] Workaround to get gdk_cursor_new_from_pixmap to support colors on Win32.
The "proper" way to do gdk_cursor_new_from_pixmap in Win32 is probably to use CreateIconIndirect as in the link Tor posted. However, I've created a different implementation using only GDK functionality (specifically, gdk_cursor_new_from_pixbuf). You'll find it in the attachment above. I'm using it to port an application using gdk_cursor_new_from_pixmap to Win32, perhaps it's useful to others as well.
We're moving to gitlab! As part of this move, we are closing bugs that haven't seen activity in more than 5 years. If this issue is still imporant to you and still relevant with GTK+ 3.22 or master, please consider creating a gitlab issue for it.