1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include <X11/cursorfont.h>
31 #include <X11/Xcursor/Xcursor.h>
34 #include "gdkprivate-x11.h"
35 #include "gdkcursor.h"
36 #include "gdkpixmap-x11.h"
38 #include <gdk/gdkpixmap.h>
39 #include <gdk-pixbuf/gdk-pixbuf.h>
44 * gdk_cursor_new_for_display:
45 * @display: the #GdkDisplay for which the cursor will be created
46 * @cursor_type: cursor to create
48 * Creates a new cursor from the set of builtin cursors.
49 * Some useful ones are:
52 * <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
55 * <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
58 * <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
61 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
64 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
67 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
70 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
73 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
76 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
79 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
82 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
85 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
88 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
91 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
94 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
97 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
100 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
104 * To make the cursor invisible, use gdk_cursor_new_from_pixmap() to create
105 * a cursor with no pixels in it.
107 * Return value: a new #GdkCursor
112 gdk_cursor_new_for_display (GdkDisplay *display,
113 GdkCursorType cursor_type)
115 GdkCursorPrivate *private;
119 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
124 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display), cursor_type);
126 private = g_new (GdkCursorPrivate, 1);
127 private->display = display;
128 private->xcursor = xcursor;
129 cursor = (GdkCursor *) private;
130 cursor->type = cursor_type;
131 cursor->ref_count = 1;
137 * gdk_cursor_new_from_pixmap:
138 * @source: the pixmap specifying the cursor.
139 * @mask: the pixmap specifying the mask, which must be the same size as
141 * @fg: the foreground color, used for the bits in the source which are 1.
142 * The color does not have to be allocated first.
143 * @bg: the background color, used for the bits in the source which are 0.
144 * The color does not have to be allocated first.
145 * @x: the horizontal offset of the 'hotspot' of the cursor.
146 * @y: the vertical offset of the 'hotspot' of the cursor.
148 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
149 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
150 * The standard cursor size is 16 by 16 pixels. You can create a bitmap
151 * from inline data as in the below example.
153 * <example><title>Creating a custom cursor</title>
155 * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
156 * utility. *<!-- -->/
157 * #define cursor1_width 16
158 * #define cursor1_height 16
159 * static unsigned char cursor1_bits[] = {
160 * 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
161 * 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
162 * 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
164 * static unsigned char cursor1mask_bits[] = {
165 * 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
166 * 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
167 * 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
171 * GdkPixmap *source, *mask;
172 * GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
173 * GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
176 * source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
177 * cursor1_width, cursor1_height);
178 * mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
179 * cursor1_width, cursor1_height);
180 * cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
181 * gdk_pixmap_unref (source);
182 * gdk_pixmap_unref (mask);
185 * gdk_window_set_cursor (widget->window, cursor);
189 * Return value: a new #GdkCursor.
192 gdk_cursor_new_from_pixmap (GdkPixmap *source,
199 GdkCursorPrivate *private;
201 Pixmap source_pixmap, mask_pixmap;
206 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
207 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
208 g_return_val_if_fail (fg != NULL, NULL);
209 g_return_val_if_fail (bg != NULL, NULL);
211 source_pixmap = GDK_PIXMAP_XID (source);
212 mask_pixmap = GDK_PIXMAP_XID (mask);
213 display = GDK_PIXMAP_DISPLAY (source);
215 xfg.pixel = fg->pixel;
218 xfg.green = fg->green;
219 xbg.pixel = bg->pixel;
222 xbg.green = bg->green;
227 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
228 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
229 private = g_new (GdkCursorPrivate, 1);
230 private->display = display;
231 private->xcursor = xcursor;
232 cursor = (GdkCursor *) private;
233 cursor->type = GDK_CURSOR_IS_PIXMAP;
234 cursor->ref_count = 1;
240 _gdk_cursor_destroy (GdkCursor *cursor)
242 GdkCursorPrivate *private;
244 g_return_if_fail (cursor != NULL);
245 g_return_if_fail (cursor->ref_count == 0);
247 private = (GdkCursorPrivate *) cursor;
248 if (!private->display->closed && private->xcursor)
249 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
255 * gdk_x11_cursor_get_xdisplay:
256 * @cursor: a #GdkCursor.
258 * Returns the display of a #GdkCursor.
260 * Return value: an Xlib <type>Display*</type>.
263 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
265 g_return_val_if_fail (cursor != NULL, NULL);
267 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
271 * gdk_x11_cursor_get_xcursor:
272 * @cursor: a #GdkCursor.
274 * Returns the X cursor belonging to a #GdkCursor.
276 * Return value: an Xlib <type>Cursor</type>.
279 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
281 g_return_val_if_fail (cursor != NULL, None);
283 return ((GdkCursorPrivate *)cursor)->xcursor;
287 * gdk_cursor_get_display:
288 * @cursor: a #GdkCursor.
290 * Returns the display on which the #GdkCursor is defined.
292 * Returns: the #GdkDisplay associated to @cursor
298 gdk_cursor_get_display (GdkCursor *cursor)
300 g_return_val_if_fail (cursor != NULL, NULL);
302 return ((GdkCursorPrivate *)cursor)->display;
309 create_cursor_image (GdkPixbuf *pixbuf,
313 guint width, height, rowstride, n_channels;
314 guchar *pixels, *src;
315 XcursorImage *xcimage;
318 width = gdk_pixbuf_get_width (pixbuf);
319 height = gdk_pixbuf_get_height (pixbuf);
321 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
322 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
323 pixels = gdk_pixbuf_get_pixels (pixbuf);
325 xcimage = XcursorImageCreate (width, height);
330 dest = xcimage->pixels;
336 for (j = 0; j < height; j++)
338 src = pixels + j * rowstride;
339 for (i = 0; i < width; i++)
341 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
350 _gdk_x11_convert_to_format (pixels, rowstride,
351 (guchar *) dest, 4 * width,
353 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
354 GDK_MSB_FIRST : GDK_LSB_FIRST,
363 * gdk_cursor_new_from_pixbuf:
364 * @display: the #GdkDisplay for which the cursor will be created
365 * @pixbuf: the #GdkPixbuf containing the cursor image
366 * @x: the horizontal offset of the 'hotspot' of the cursor.
367 * @y: the vertical offset of the 'hotspot' of the cursor.
369 * Creates a new cursor from a pixbuf.
371 * Not all GDK backends support RGBA cursors. If they are not
372 * supported, a monochrome approximation will be displayed.
373 * The functions gdk_display_supports_cursor_alpha() and
374 * gdk_display_supports_cursor_color() can be used to determine
375 * whether RGBA cursors are supported;
376 * gdk_display_get_default_cursor_size() and
377 * gdk_display_get_maximal_cursor_size() give information about
380 * On the X backend, support for RGBA cursors requires a
381 * sufficently new version of the X Render extension.
383 * Returns: a new #GdkCursor.
388 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
393 XcursorImage *xcimage;
395 GdkCursorPrivate *private;
398 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
399 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
400 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
401 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
407 xcimage = create_cursor_image (pixbuf, x, y);
408 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
409 XcursorImageDestroy (xcimage);
412 private = g_new (GdkCursorPrivate, 1);
413 private->display = display;
414 private->xcursor = xcursor;
415 cursor = (GdkCursor *) private;
416 cursor->type = GDK_CURSOR_IS_PIXMAP;
417 cursor->ref_count = 1;
423 * gdk_display_supports_cursor_alpha:
424 * @display: a #GdkDisplay
426 * Returns %TRUE if cursors can use an 8bit alpha channel
427 * on @display. Otherwise, cursors are restricted to bilevel
428 * alpha (i.e. a mask).
430 * Returns: whether cursors can have alpha channels.
435 gdk_display_supports_cursor_alpha (GdkDisplay *display)
437 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
439 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
443 * gdk_display_supports_cursor_color:
444 * @display: a #GdkDisplay
446 * Returns %TRUE if multicolored cursors are supported
447 * on @display. Otherwise, cursors have only a forground
448 * and a background color.
450 * Returns: whether cursors can have multiple colors.
455 gdk_display_supports_cursor_color (GdkDisplay *display)
457 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
459 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
463 * gdk_display_get_default_cursor_size:
464 * @display: a #GdkDisplay
466 * Returns the default size to use for cursors on @display.
468 * Returns: the default cursor size.
473 gdk_display_get_default_cursor_size (GdkDisplay *display)
475 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
477 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
483 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
489 GdkPixmap *pixmap, *mask;
490 guint width, height, n_channels, rowstride, i, j;
491 guint8 *data, *mask_data, *pixels;
492 GdkColor fg = { 0, 0, 0, 0 };
493 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
496 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
497 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
499 width = gdk_pixbuf_get_width (pixbuf);
500 height = gdk_pixbuf_get_height (pixbuf);
502 g_return_val_if_fail (0 <= x && x < width, NULL);
503 g_return_val_if_fail (0 <= y && y < height, NULL);
505 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
506 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
507 pixels = gdk_pixbuf_get_pixels (pixbuf);
509 data = g_new0 (guint8, (width + 7) / 8 * height);
510 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
512 for (j = 0; j < height; j++)
514 guint8 *src = pixels + j * rowstride;
515 guint8 *d = data + (width + 7) / 8 * j;
516 guint8 *md = mask_data + (width + 7) / 8 * j;
518 for (i = 0; i < width; i++)
523 if (n_channels == 3 || src[3] >= 0x80)
535 screen = gdk_display_get_default_screen (display);
536 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
537 data, width, height);
539 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
540 mask_data, width, height);
542 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
544 g_object_unref (pixmap);
545 g_object_unref (mask);
554 gdk_display_supports_cursor_alpha (GdkDisplay *display)
556 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
562 gdk_display_supports_cursor_color (GdkDisplay *display)
564 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
570 gdk_display_get_default_cursor_size (GdkDisplay *display)
572 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
574 /* no idea, really */
582 * gdk_display_get_maximal_cursor_size:
583 * @display: a #GdkDisplay
584 * @width: the return location for the maximal cursor width
585 * @height: the return location for the maximal cursor height
587 * Gets the maximal size to use for cursors on @display.
592 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
599 g_return_if_fail (GDK_IS_DISPLAY (display));
601 screen = gdk_display_get_default_screen (display);
602 window = gdk_screen_get_root_window (screen);
603 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
604 GDK_WINDOW_XWINDOW (window),
605 128, 128, width, height);
608 #define __GDK_CURSOR_X11_C__
609 #include "gdkaliasdef.c"