Re: [gtk-list] Need help with GdkCursors



"Daniel J. Kressin" <kressd11@sol.acs.uwosh.edu> wrote:
>I've got a drawing area (part of a more complex layout) created and shown
>and I'd like to have the mouse cursor change to a custom cursor when it's
>over the drawing area.  My custom cursor image is 32x32, 3 colors (black,
>white, transparent) and stored in xpm format.  Can someone please give me
>a quick rundown on how to make my custom-cursor-in-my-drawing-area dream a
>reality?

Hiya, here's how I do it:

- make two xbm's for the cursor: one a mask, the other the pixels.

#define magin_src_width 16
#define magin_src_height 16
static unsigned char magin_src_bits[] = {
	0x00, 0x00, 0xf0, 0x00, 0xfc, 0x03, 0x9c, 0x03, 0x9e, 0x07, 0x06, 0x06,
	0x06, 0x06, 0x9e, 0x07, 0x9c, 0x03, 0xfc, 0x03, 0xf0, 0x08, 0x00, 0x14,
	0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00, 0x00};

#define mag_msk_width 16
#define mag_msk_height 16
static unsigned char mag_msk_bits[] = {
	0xf0, 0x00, 0xfc, 0x03, 0xfe, 0x07, 0xfe, 0x07, 0xff, 0x0f, 0xff, 0x0f,
	0xff, 0x0f, 0xff, 0x0f, 0xfe, 0x07, 0xfe, 0x0f, 0xfc, 0x1f, 0xf0, 0x3e,
	0x00, 0x7c, 0x00, 0xf8, 0x00, 0xf0, 0x00, 0xe0};

A '1' in the mask means that pixel is not transparent ... in which case X
displays black or white depending on the state of the source xbm.

- use these to make a GdkCursor

GdkCursor *
make_data_cursor( GdkWindow *window,
        guchar *src_bits, guchar *msk_bits, int w, int h, int x, int y )
{
        GdkPixmap *src;
        GdkPixmap *msk;
        GdkCursor *cursor;
        GdkColor fg = { 0, 255 << 8, 255 << 8, 255 << 8 };
        GdkColor bg = { 0, 0, 0, 0 };

        src = gdk_bitmap_create_from_data( window,
                (const char *) src_bits, w, h );
        msk = gdk_bitmap_create_from_data( window,
                (const char *) msk_bits, w, h );
        cursor = gdk_cursor_new_from_pixmap( src, msk, &fg, &bg, x, y );
        gdk_pixmap_unref( src );
        gdk_pixmap_unref( msk );

        return( cursor );
}

- attach the cursor to your window

gdk_window_set_cursor( GTK_WIDGET( drawing_area )->window, cursor );

Hope this helps,

John
--
John Cupitt, john.cupitt@ng-london.org.uk, +44 (0)171 930 2108
VASARI Lab, The National Gallery, Trafalgar Square, London, WC2N 5DN



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]