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,
436 if (*xid & XID_FONT_BIT)
439 if (!GDK_IS_WINDOW (value))
442 cursor = _gdk_x11_window_get_cursor (GDK_WINDOW (value));
447 _gdk_x11_cursor_update_theme (cursor);
451 * gdk_x11_display_set_cursor_theme:
452 * @display: a #GdkDisplay
453 * @theme: the name of the cursor theme to use, or %NULL to unset
454 * a previously set value
455 * @size: the cursor size to use, or 0 to keep the previous size
457 * Sets the cursor theme from which the images for cursor
460 * If the windowing system supports it, existing cursors created
461 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
462 * gdk_cursor_new_for_name() are updated to reflect the theme
463 * change. Custom cursors constructed with gdk_cursor_new_from_pixmap()
464 * or gdk_cursor_new_from_pixbuf() will have to be handled
465 * by the application (GTK+ applications can learn about
466 * cursor theme changes by listening for change notification
467 * for the corresponding #GtkSetting).
472 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
476 GdkDisplayX11 *display_x11;
481 g_return_if_fail (GDK_IS_DISPLAY (display));
483 display_x11 = GDK_DISPLAY_X11 (display);
484 xdisplay = GDK_DISPLAY_XDISPLAY (display);
486 old_theme = XcursorGetTheme (xdisplay);
487 old_size = XcursorGetDefaultSize (xdisplay);
489 if (old_size == size &&
490 (old_theme == theme ||
491 (old_theme && theme && strcmp (old_theme, theme) == 0)))
496 XcursorSetTheme (xdisplay, theme);
498 XcursorSetDefaultSize (xdisplay, size);
500 g_hash_table_foreach (display_x11->xid_ht, update_cursor, NULL);
506 gdk_cursor_get_image (GdkCursor *cursor)
508 g_return_val_if_fail (cursor != NULL, NULL);
514 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
518 g_return_if_fail (GDK_IS_DISPLAY (display));
522 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
524 g_return_if_fail (cursor != NULL);
532 create_cursor_image (GdkPixbuf *pixbuf,
536 guint width, height, rowstride, n_channels;
537 guchar *pixels, *src;
538 XcursorImage *xcimage;
541 width = gdk_pixbuf_get_width (pixbuf);
542 height = gdk_pixbuf_get_height (pixbuf);
544 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
545 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
546 pixels = gdk_pixbuf_get_pixels (pixbuf);
548 xcimage = XcursorImageCreate (width, height);
553 dest = xcimage->pixels;
559 for (j = 0; j < height; j++)
561 src = pixels + j * rowstride;
562 for (i = 0; i < width; i++)
564 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
573 _gdk_x11_convert_to_format (pixels, rowstride,
574 (guchar *) dest, 4 * width,
576 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
577 GDK_MSB_FIRST : GDK_LSB_FIRST,
586 * gdk_cursor_new_from_pixbuf:
587 * @display: the #GdkDisplay for which the cursor will be created
588 * @pixbuf: the #GdkPixbuf containing the cursor image
589 * @x: the horizontal offset of the 'hotspot' of the cursor.
590 * @y: the vertical offset of the 'hotspot' of the cursor.
592 * Creates a new cursor from a pixbuf.
594 * Not all GDK backends support RGBA cursors. If they are not
595 * supported, a monochrome approximation will be displayed.
596 * The functions gdk_display_supports_cursor_alpha() and
597 * gdk_display_supports_cursor_color() can be used to determine
598 * whether RGBA cursors are supported;
599 * gdk_display_get_default_cursor_size() and
600 * gdk_display_get_maximal_cursor_size() give information about
603 * On the X backend, support for RGBA cursors requires a
604 * sufficently new version of the X Render extension.
606 * Returns: a new #GdkCursor.
611 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
616 XcursorImage *xcimage;
618 GdkCursorPrivate *private;
621 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
622 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
623 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
624 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
630 xcimage = create_cursor_image (pixbuf, x, y);
631 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
632 XcursorImageDestroy (xcimage);
635 private = g_new (GdkCursorPrivate, 1);
636 private->display = display;
637 private->xcursor = xcursor;
638 private->name = NULL;
639 private->serial = theme_serial;
641 cursor = (GdkCursor *) private;
642 cursor->type = GDK_CURSOR_IS_PIXMAP;
643 cursor->ref_count = 1;
649 * gdk_cursor_new_from_name:
650 * @display: the #GdkDisplay for which the cursor will be created
651 * @name: the name of the cursor
653 * Creates a new cursor by looking up @name in the current cursor
656 * Returns: a new #GdkCursor, or %NULL if there is no cursor with
662 gdk_cursor_new_from_name (GdkDisplay *display,
667 GdkCursorPrivate *private;
670 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
676 xdisplay = GDK_DISPLAY_XDISPLAY (display);
677 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
682 private = g_new (GdkCursorPrivate, 1);
683 private->display = display;
684 private->xcursor = xcursor;
685 private->name = g_strdup (name);
686 private->serial = theme_serial;
688 cursor = (GdkCursor *) private;
689 cursor->type = GDK_CURSOR_IS_PIXMAP;
690 cursor->ref_count = 1;
696 * gdk_display_supports_cursor_alpha:
697 * @display: a #GdkDisplay
699 * Returns %TRUE if cursors can use an 8bit alpha channel
700 * on @display. Otherwise, cursors are restricted to bilevel
701 * alpha (i.e. a mask).
703 * Returns: whether cursors can have alpha channels.
708 gdk_display_supports_cursor_alpha (GdkDisplay *display)
710 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
712 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
716 * gdk_display_supports_cursor_color:
717 * @display: a #GdkDisplay
719 * Returns %TRUE if multicolored cursors are supported
720 * on @display. Otherwise, cursors have only a forground
721 * and a background color.
723 * Returns: whether cursors can have multiple colors.
728 gdk_display_supports_cursor_color (GdkDisplay *display)
730 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
732 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
736 * gdk_display_get_default_cursor_size:
737 * @display: a #GdkDisplay
739 * Returns the default size to use for cursors on @display.
741 * Returns: the default cursor size.
746 gdk_display_get_default_cursor_size (GdkDisplay *display)
748 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
750 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
756 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
762 GdkPixmap *pixmap, *mask;
763 guint width, height, n_channels, rowstride, i, j;
764 guint8 *data, *mask_data, *pixels;
765 GdkColor fg = { 0, 0, 0, 0 };
766 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
769 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
770 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
772 width = gdk_pixbuf_get_width (pixbuf);
773 height = gdk_pixbuf_get_height (pixbuf);
775 g_return_val_if_fail (0 <= x && x < width, NULL);
776 g_return_val_if_fail (0 <= y && y < height, NULL);
778 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
779 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
780 pixels = gdk_pixbuf_get_pixels (pixbuf);
782 data = g_new0 (guint8, (width + 7) / 8 * height);
783 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
785 for (j = 0; j < height; j++)
787 guint8 *src = pixels + j * rowstride;
788 guint8 *d = data + (width + 7) / 8 * j;
789 guint8 *md = mask_data + (width + 7) / 8 * j;
791 for (i = 0; i < width; i++)
796 if (n_channels == 3 || src[3] >= 0x80)
808 screen = gdk_display_get_default_screen (display);
809 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
810 data, width, height);
812 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
813 mask_data, width, height);
815 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
817 g_object_unref (pixmap);
818 g_object_unref (mask);
827 gdk_cursor_new_from_name (GdkDisplay *display,
830 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
836 gdk_display_supports_cursor_alpha (GdkDisplay *display)
838 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
844 gdk_display_supports_cursor_color (GdkDisplay *display)
846 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
852 gdk_display_get_default_cursor_size (GdkDisplay *display)
854 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
856 /* no idea, really */
864 * gdk_display_get_maximal_cursor_size:
865 * @display: a #GdkDisplay
866 * @width: the return location for the maximal cursor width
867 * @height: the return location for the maximal cursor height
869 * Gets the maximal size to use for cursors on @display.
874 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
881 g_return_if_fail (GDK_IS_DISPLAY (display));
883 screen = gdk_display_get_default_screen (display);
884 window = gdk_screen_get_root_window (screen);
885 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
886 GDK_WINDOW_XWINDOW (window),
887 128, 128, width, height);
890 #define __GDK_CURSOR_X11_C__
891 #include "gdkaliasdef.c"