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 #define GDK_PIXBUF_ENABLE_BACKEND
32 #include <X11/cursorfont.h>
34 #include <X11/Xcursor/Xcursor.h>
37 #include <X11/extensions/Xfixes.h>
41 #include "gdkprivate-x11.h"
42 #include "gdkcursor.h"
43 #include "gdkpixmap-x11.h"
45 #include <gdk/gdkpixmap.h>
46 #include <gdk-pixbuf/gdk-pixbuf.h>
49 static guint theme_serial = 0;
52 * gdk_cursor_new_for_display:
53 * @display: the #GdkDisplay for which the cursor will be created
54 * @cursor_type: cursor to create
56 * Creates a new cursor from the set of builtin cursors.
57 * Some useful ones are:
60 * <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
63 * <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
66 * <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
69 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
72 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
75 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
78 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
81 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
84 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
87 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
90 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
93 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
96 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
99 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
102 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
105 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
108 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
112 * To make the cursor invisible, use gdk_cursor_new_from_pixmap() to create
113 * a cursor with no pixels in it.
115 * Return value: a new #GdkCursor
120 gdk_cursor_new_for_display (GdkDisplay *display,
121 GdkCursorType cursor_type)
123 GdkCursorPrivate *private;
127 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
132 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display), cursor_type);
134 private = g_new (GdkCursorPrivate, 1);
135 private->display = display;
136 private->xcursor = xcursor;
137 private->name = NULL;
138 private->serial = theme_serial;
140 cursor = (GdkCursor *) private;
141 cursor->type = cursor_type;
142 cursor->ref_count = 1;
148 * gdk_cursor_new_from_pixmap:
149 * @source: the pixmap specifying the cursor.
150 * @mask: the pixmap specifying the mask, which must be the same size as
152 * @fg: the foreground color, used for the bits in the source which are 1.
153 * The color does not have to be allocated first.
154 * @bg: the background color, used for the bits in the source which are 0.
155 * The color does not have to be allocated first.
156 * @x: the horizontal offset of the 'hotspot' of the cursor.
157 * @y: the vertical offset of the 'hotspot' of the cursor.
159 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
160 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
161 * The standard cursor size is 16 by 16 pixels. You can create a bitmap
162 * from inline data as in the below example.
164 * <example><title>Creating a custom cursor</title>
166 * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
167 * utility. *<!-- -->/
168 * #define cursor1_width 16
169 * #define cursor1_height 16
170 * static unsigned char cursor1_bits[] = {
171 * 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
172 * 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
173 * 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
175 * static unsigned char cursor1mask_bits[] = {
176 * 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
177 * 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
178 * 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
182 * GdkPixmap *source, *mask;
183 * GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
184 * GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
187 * source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
188 * cursor1_width, cursor1_height);
189 * mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
190 * cursor1_width, cursor1_height);
191 * cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
192 * gdk_pixmap_unref (source);
193 * gdk_pixmap_unref (mask);
196 * gdk_window_set_cursor (widget->window, cursor);
200 * Return value: a new #GdkCursor.
203 gdk_cursor_new_from_pixmap (GdkPixmap *source,
210 GdkCursorPrivate *private;
212 Pixmap source_pixmap, mask_pixmap;
217 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
218 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
219 g_return_val_if_fail (fg != NULL, NULL);
220 g_return_val_if_fail (bg != NULL, NULL);
222 source_pixmap = GDK_PIXMAP_XID (source);
223 mask_pixmap = GDK_PIXMAP_XID (mask);
224 display = GDK_PIXMAP_DISPLAY (source);
226 xfg.pixel = fg->pixel;
229 xfg.green = fg->green;
230 xbg.pixel = bg->pixel;
233 xbg.green = bg->green;
238 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
239 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
240 private = g_new (GdkCursorPrivate, 1);
241 private->display = display;
242 private->xcursor = xcursor;
243 private->name = NULL;
244 private->serial = theme_serial;
246 cursor = (GdkCursor *) private;
247 cursor->type = GDK_CURSOR_IS_PIXMAP;
248 cursor->ref_count = 1;
254 _gdk_cursor_destroy (GdkCursor *cursor)
256 GdkCursorPrivate *private;
258 g_return_if_fail (cursor != NULL);
259 g_return_if_fail (cursor->ref_count == 0);
261 private = (GdkCursorPrivate *) cursor;
262 if (!private->display->closed && private->xcursor)
263 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
265 g_free (private->name);
270 * gdk_x11_cursor_get_xdisplay:
271 * @cursor: a #GdkCursor.
273 * Returns the display of a #GdkCursor.
275 * Return value: an Xlib <type>Display*</type>.
278 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
280 g_return_val_if_fail (cursor != NULL, NULL);
282 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
286 * gdk_x11_cursor_get_xcursor:
287 * @cursor: a #GdkCursor.
289 * Returns the X cursor belonging to a #GdkCursor.
291 * Return value: an Xlib <type>Cursor</type>.
294 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
296 g_return_val_if_fail (cursor != NULL, None);
298 return ((GdkCursorPrivate *)cursor)->xcursor;
302 * gdk_cursor_get_display:
303 * @cursor: a #GdkCursor.
305 * Returns the display on which the #GdkCursor is defined.
307 * Returns: the #GdkDisplay associated to @cursor
313 gdk_cursor_get_display (GdkCursor *cursor)
315 g_return_val_if_fail (cursor != NULL, NULL);
317 return ((GdkCursorPrivate *)cursor)->display;
320 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
323 * gdk_cursor_get_image:
324 * @cursor: a #GdkCursor
326 * Returns a #GdkPixbuf with the image used to display the cursor.
328 * Note that depending on the capabilities of the windowing system and
329 * on the cursor, GDK may not be able to obtain the image data. In this
330 * case, %NULL is returned.
332 * Returns: a #GdkPixbuf representing @cursor, or %NULL
337 gdk_cursor_get_image (GdkCursor *cursor)
340 GdkCursorPrivate *private;
341 XcursorImages *images = NULL;
345 guchar *data, *p, tmp;
349 g_return_val_if_fail (cursor != NULL, NULL);
351 private = (GdkCursorPrivate *) cursor;
353 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
355 size = XcursorGetDefaultSize (xdisplay);
356 theme = XcursorGetTheme (xdisplay);
358 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
361 images = XcursorLibraryLoadImages (private->name, theme, size);
364 images = XcursorShapeLoadImages (cursor->type, theme, size);
369 image = images->images[0];
371 data = g_malloc (4 * image->width * image->height);
372 memcpy (data, image->pixels, 4 * image->width * image->height);
374 for (p = data; p < data + (4 * image->width * image->height); p += 4)
381 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
382 8, image->width, image->height,
384 (GdkPixbufDestroyNotify)g_free, NULL);
387 gdk_pixbuf_set_option (pixbuf, "name", private->name);
388 g_snprintf (buf, 32, "%d", image->xhot);
389 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
390 g_snprintf (buf, 32, "%d", image->yhot);
391 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
393 XcursorImagesDestroy (images);
399 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
402 GdkCursorPrivate *private;
403 Cursor new_cursor = None;
405 private = (GdkCursorPrivate *) cursor;
406 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
408 if (private->serial == theme_serial)
411 private->serial = theme_serial;
413 if (private->xcursor != None)
415 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
418 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
421 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
423 if (new_cursor != None)
424 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
429 update_cursor (gpointer key,
435 if (!GDK_IS_WINDOW (value))
438 cursor = _gdk_x11_window_get_cursor (GDK_WINDOW (value));
443 _gdk_x11_cursor_update_theme (cursor);
447 * gdk_x11_display_set_cursor_theme:
448 * @display: a #GdkDisplay
449 * @theme: the name of the cursor theme to use
450 * @size: the cursor size to use
452 * Sets the cursor theme from which the images for cursor
455 * If the windowing system supports it, existing cursors created
456 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
457 * gdk_cursor_new_for_name() are updated to reflect the theme
458 * change. Custom cursors constructed with gdk_cursor_new_from_pixmap()
459 * or gdk_cursor_new_from_pixbuf() will have to be handled
460 * by the application (GTK+ applications can learn about
461 * cursor theme changes by listening for change notification
462 * for the corresponding #GtkSetting).
467 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
471 GdkDisplayX11 *display_x11;
476 g_return_if_fail (GDK_IS_DISPLAY (display));
478 display_x11 = GDK_DISPLAY_X11 (display);
479 xdisplay = GDK_DISPLAY_XDISPLAY (display);
481 old_theme = XcursorGetTheme (xdisplay);
482 old_size = XcursorGetDefaultSize (xdisplay);
484 if (old_size == size &&
485 old_theme && strcmp (old_theme, theme) == 0)
490 XcursorSetTheme (xdisplay, theme);
491 XcursorSetDefaultSize (xdisplay, size);
493 g_hash_table_foreach (display_x11->xid_ht, update_cursor, NULL);
499 gdk_cursor_get_image (GdkCursor *cursor)
501 g_return_val_if_fail (cursor != NULL, NULL);
507 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
511 g_return_if_fail (GDK_IS_DISPLAY (display));
515 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
517 g_return_if_fail (cursor != NULL);
525 create_cursor_image (GdkPixbuf *pixbuf,
529 guint width, height, rowstride, n_channels;
530 guchar *pixels, *src;
531 XcursorImage *xcimage;
534 width = gdk_pixbuf_get_width (pixbuf);
535 height = gdk_pixbuf_get_height (pixbuf);
537 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
538 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
539 pixels = gdk_pixbuf_get_pixels (pixbuf);
541 xcimage = XcursorImageCreate (width, height);
546 dest = xcimage->pixels;
552 for (j = 0; j < height; j++)
554 src = pixels + j * rowstride;
555 for (i = 0; i < width; i++)
557 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
566 _gdk_x11_convert_to_format (pixels, rowstride,
567 (guchar *) dest, 4 * width,
569 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
570 GDK_MSB_FIRST : GDK_LSB_FIRST,
579 * gdk_cursor_new_from_pixbuf:
580 * @display: the #GdkDisplay for which the cursor will be created
581 * @pixbuf: the #GdkPixbuf containing the cursor image
582 * @x: the horizontal offset of the 'hotspot' of the cursor.
583 * @y: the vertical offset of the 'hotspot' of the cursor.
585 * Creates a new cursor from a pixbuf.
587 * Not all GDK backends support RGBA cursors. If they are not
588 * supported, a monochrome approximation will be displayed.
589 * The functions gdk_display_supports_cursor_alpha() and
590 * gdk_display_supports_cursor_color() can be used to determine
591 * whether RGBA cursors are supported;
592 * gdk_display_get_default_cursor_size() and
593 * gdk_display_get_maximal_cursor_size() give information about
596 * On the X backend, support for RGBA cursors requires a
597 * sufficently new version of the X Render extension.
599 * Returns: a new #GdkCursor.
604 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
609 XcursorImage *xcimage;
611 GdkCursorPrivate *private;
614 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
615 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
616 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
617 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
623 xcimage = create_cursor_image (pixbuf, x, y);
624 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
625 XcursorImageDestroy (xcimage);
628 private = g_new (GdkCursorPrivate, 1);
629 private->display = display;
630 private->xcursor = xcursor;
631 private->name = NULL;
632 private->serial = theme_serial;
634 cursor = (GdkCursor *) private;
635 cursor->type = GDK_CURSOR_IS_PIXMAP;
636 cursor->ref_count = 1;
642 * gdk_cursor_new_from_name:
643 * @display: the #GdkDisplay for which the cursor will be created
644 * @name: the name of the cursor
646 * Creates a new cursor by looking up @name in the current cursor
649 * Returns: a new #GdkCursor, or %NULL if there is no cursor with
655 gdk_cursor_new_from_name (GdkDisplay *display,
660 GdkCursorPrivate *private;
663 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
669 xdisplay = GDK_DISPLAY_XDISPLAY (display);
670 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
675 private = g_new (GdkCursorPrivate, 1);
676 private->display = display;
677 private->xcursor = xcursor;
678 private->name = g_strdup (name);
679 private->serial = theme_serial;
681 cursor = (GdkCursor *) private;
682 cursor->type = GDK_CURSOR_IS_PIXMAP;
683 cursor->ref_count = 1;
689 * gdk_display_supports_cursor_alpha:
690 * @display: a #GdkDisplay
692 * Returns %TRUE if cursors can use an 8bit alpha channel
693 * on @display. Otherwise, cursors are restricted to bilevel
694 * alpha (i.e. a mask).
696 * Returns: whether cursors can have alpha channels.
701 gdk_display_supports_cursor_alpha (GdkDisplay *display)
703 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
705 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
709 * gdk_display_supports_cursor_color:
710 * @display: a #GdkDisplay
712 * Returns %TRUE if multicolored cursors are supported
713 * on @display. Otherwise, cursors have only a forground
714 * and a background color.
716 * Returns: whether cursors can have multiple colors.
721 gdk_display_supports_cursor_color (GdkDisplay *display)
723 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
725 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
729 * gdk_display_get_default_cursor_size:
730 * @display: a #GdkDisplay
732 * Returns the default size to use for cursors on @display.
734 * Returns: the default cursor size.
739 gdk_display_get_default_cursor_size (GdkDisplay *display)
741 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
743 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
749 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
755 GdkPixmap *pixmap, *mask;
756 guint width, height, n_channels, rowstride, i, j;
757 guint8 *data, *mask_data, *pixels;
758 GdkColor fg = { 0, 0, 0, 0 };
759 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
762 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
763 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
765 width = gdk_pixbuf_get_width (pixbuf);
766 height = gdk_pixbuf_get_height (pixbuf);
768 g_return_val_if_fail (0 <= x && x < width, NULL);
769 g_return_val_if_fail (0 <= y && y < height, NULL);
771 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
772 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
773 pixels = gdk_pixbuf_get_pixels (pixbuf);
775 data = g_new0 (guint8, (width + 7) / 8 * height);
776 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
778 for (j = 0; j < height; j++)
780 guint8 *src = pixels + j * rowstride;
781 guint8 *d = data + (width + 7) / 8 * j;
782 guint8 *md = mask_data + (width + 7) / 8 * j;
784 for (i = 0; i < width; i++)
789 if (n_channels == 3 || src[3] >= 0x80)
801 screen = gdk_display_get_default_screen (display);
802 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
803 data, width, height);
805 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
806 mask_data, width, height);
808 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
810 g_object_unref (pixmap);
811 g_object_unref (mask);
820 gdk_cursor_new_from_name (GdkDisplay *display,
823 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
829 gdk_display_supports_cursor_alpha (GdkDisplay *display)
831 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
837 gdk_display_supports_cursor_color (GdkDisplay *display)
839 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
845 gdk_display_get_default_cursor_size (GdkDisplay *display)
847 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
849 /* no idea, really */
857 * gdk_display_get_maximal_cursor_size:
858 * @display: a #GdkDisplay
859 * @width: the return location for the maximal cursor width
860 * @height: the return location for the maximal cursor height
862 * Gets the maximal size to use for cursors on @display.
867 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
874 g_return_if_fail (GDK_IS_DISPLAY (display));
876 screen = gdk_display_get_default_screen (display);
877 window = gdk_screen_get_root_window (screen);
878 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
879 GDK_WINDOW_XWINDOW (window),
880 128, 128, width, height);
883 #define __GDK_CURSOR_X11_C__
884 #include "gdkaliasdef.c"