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>
51 * gdk_cursor_new_for_display:
52 * @display: the #GdkDisplay for which the cursor will be created
53 * @cursor_type: cursor to create
55 * Creates a new cursor from the set of builtin cursors.
56 * Some useful ones are:
59 * <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
62 * <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
65 * <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
68 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
71 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
74 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
77 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
80 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
83 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
86 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
89 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
92 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
95 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
98 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
101 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
104 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
107 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
111 * To make the cursor invisible, use gdk_cursor_new_from_pixmap() to create
112 * a cursor with no pixels in it.
114 * Return value: a new #GdkCursor
119 gdk_cursor_new_for_display (GdkDisplay *display,
120 GdkCursorType cursor_type)
122 GdkCursorPrivate *private;
126 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
131 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display), cursor_type);
133 private = g_new (GdkCursorPrivate, 1);
134 private->display = display;
135 private->xcursor = xcursor;
136 private->name = NULL;
137 cursor = (GdkCursor *) private;
138 cursor->type = cursor_type;
139 cursor->ref_count = 1;
145 * gdk_cursor_new_from_pixmap:
146 * @source: the pixmap specifying the cursor.
147 * @mask: the pixmap specifying the mask, which must be the same size as
149 * @fg: the foreground color, used for the bits in the source which are 1.
150 * The color does not have to be allocated first.
151 * @bg: the background color, used for the bits in the source which are 0.
152 * The color does not have to be allocated first.
153 * @x: the horizontal offset of the 'hotspot' of the cursor.
154 * @y: the vertical offset of the 'hotspot' of the cursor.
156 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
157 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
158 * The standard cursor size is 16 by 16 pixels. You can create a bitmap
159 * from inline data as in the below example.
161 * <example><title>Creating a custom cursor</title>
163 * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
164 * utility. *<!-- -->/
165 * #define cursor1_width 16
166 * #define cursor1_height 16
167 * static unsigned char cursor1_bits[] = {
168 * 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
169 * 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
170 * 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
172 * static unsigned char cursor1mask_bits[] = {
173 * 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
174 * 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
175 * 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
179 * GdkPixmap *source, *mask;
180 * GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
181 * GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
184 * source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
185 * cursor1_width, cursor1_height);
186 * mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
187 * cursor1_width, cursor1_height);
188 * cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
189 * gdk_pixmap_unref (source);
190 * gdk_pixmap_unref (mask);
193 * gdk_window_set_cursor (widget->window, cursor);
197 * Return value: a new #GdkCursor.
200 gdk_cursor_new_from_pixmap (GdkPixmap *source,
207 GdkCursorPrivate *private;
209 Pixmap source_pixmap, mask_pixmap;
214 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
215 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
216 g_return_val_if_fail (fg != NULL, NULL);
217 g_return_val_if_fail (bg != NULL, NULL);
219 source_pixmap = GDK_PIXMAP_XID (source);
220 mask_pixmap = GDK_PIXMAP_XID (mask);
221 display = GDK_PIXMAP_DISPLAY (source);
223 xfg.pixel = fg->pixel;
226 xfg.green = fg->green;
227 xbg.pixel = bg->pixel;
230 xbg.green = bg->green;
235 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
236 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
237 private = g_new (GdkCursorPrivate, 1);
238 private->display = display;
239 private->xcursor = xcursor;
240 private->name = NULL;
241 cursor = (GdkCursor *) private;
242 cursor->type = GDK_CURSOR_IS_PIXMAP;
243 cursor->ref_count = 1;
249 _gdk_cursor_destroy (GdkCursor *cursor)
251 GdkCursorPrivate *private;
253 g_return_if_fail (cursor != NULL);
254 g_return_if_fail (cursor->ref_count == 0);
256 private = (GdkCursorPrivate *) cursor;
257 if (!private->display->closed && private->xcursor)
258 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
260 g_free (private->name);
265 * gdk_x11_cursor_get_xdisplay:
266 * @cursor: a #GdkCursor.
268 * Returns the display of a #GdkCursor.
270 * Return value: an Xlib <type>Display*</type>.
273 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
275 g_return_val_if_fail (cursor != NULL, NULL);
277 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
281 * gdk_x11_cursor_get_xcursor:
282 * @cursor: a #GdkCursor.
284 * Returns the X cursor belonging to a #GdkCursor.
286 * Return value: an Xlib <type>Cursor</type>.
289 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
291 g_return_val_if_fail (cursor != NULL, None);
293 return ((GdkCursorPrivate *)cursor)->xcursor;
297 * gdk_cursor_get_display:
298 * @cursor: a #GdkCursor.
300 * Returns the display on which the #GdkCursor is defined.
302 * Returns: the #GdkDisplay associated to @cursor
308 gdk_cursor_get_display (GdkCursor *cursor)
310 g_return_val_if_fail (cursor != NULL, NULL);
312 return ((GdkCursorPrivate *)cursor)->display;
315 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
318 * gdk_cursor_get_image:
319 * @cursor: a #GdkCursor
321 * Returns a #GdkPixbuf with the image used to display the cursor.
323 * Note that depending on the capabilities of the windowing system and
324 * on the cursor, GDK may not be able to obtain the image data. In this
325 * case, %NULL is returned.
327 * Returns: a #GdkPixbuf representing @cursor, or %NULL
332 gdk_cursor_get_image (GdkCursor *cursor)
335 GdkCursorPrivate *private;
336 XcursorImages *images = NULL;
340 guchar *data, *p, tmp;
344 g_return_val_if_fail (cursor != NULL, NULL);
346 private = (GdkCursorPrivate *) cursor;
348 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
350 size = XcursorGetDefaultSize (xdisplay);
351 theme = XcursorGetTheme (xdisplay);
353 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
356 images = XcursorLibraryLoadImages (private->name, theme, size);
359 images = XcursorShapeLoadImages (cursor->type, theme, size);
364 image = images->images[0];
366 data = g_malloc (4 * image->width * image->height);
367 memcpy (data, image->pixels, 4 * image->width * image->height);
369 for (p = data; p < data + (4 * image->width * image->height); p += 4)
376 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
377 8, image->width, image->height,
379 (GdkPixbufDestroyNotify)g_free, NULL);
382 gdk_pixbuf_set_option (pixbuf, "name", private->name);
383 g_snprintf (buf, 32, "%d", image->xhot);
384 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
385 g_snprintf (buf, 32, "%d", image->yhot);
386 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
388 XcursorImagesDestroy (images);
394 update_cursor (gpointer key,
401 GdkCursorPrivate *private;
402 Cursor new_cursor = None;
404 if (!GDK_IS_WINDOW (value))
407 cursor = _gdk_x11_window_get_cursor (GDK_WINDOW (value));
412 private = (GdkCursorPrivate *) cursor;
413 xdisplay = (Display *)data;
415 if (private->xcursor != None)
417 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
420 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
423 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
425 if (new_cursor != None)
426 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
431 * gdk_x11_display_set_cursor_theme:
432 * @display: a #GdkDisplay
433 * @theme: the name of the cursor theme to use
434 * @size: the cursor size to use
436 * Sets the cursor theme from which the images for cursor
439 * If the windowing system supports it, existing cursors created
440 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
441 * gdk_cursor_new_for_name() are updated to reflect the theme
442 * change. Custom cursors constructed with gdk_cursor_new_from_pixmap()
443 * or gdk_cursor_new_from_pixbuf() will have to be handled
444 * by the application (GTK+ applications can learn about
445 * cursor theme changes by listening for change notification
446 * for the corresponding #GtkSetting).
451 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
455 GdkDisplayX11 *display_x11;
460 g_return_if_fail (GDK_IS_DISPLAY (display));
462 display_x11 = GDK_DISPLAY_X11 (display);
463 xdisplay = GDK_DISPLAY_XDISPLAY (display);
465 old_theme = XcursorGetTheme (xdisplay);
466 old_size = XcursorGetDefaultSize (xdisplay);
468 if (old_size == size &&
469 old_theme && strcmp (old_theme, theme) == 0)
472 XcursorSetTheme (xdisplay, theme);
473 XcursorSetDefaultSize (xdisplay, size);
475 g_hash_table_foreach (display_x11->xid_ht,
476 update_cursor, xdisplay);
482 gdk_cursor_get_image (GdkCursor *cursor)
484 g_return_val_if_fail (cursor != NULL, NULL);
490 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
494 g_return_if_fail (GDK_IS_DISPLAY (display));
504 create_cursor_image (GdkPixbuf *pixbuf,
508 guint width, height, rowstride, n_channels;
509 guchar *pixels, *src;
510 XcursorImage *xcimage;
513 width = gdk_pixbuf_get_width (pixbuf);
514 height = gdk_pixbuf_get_height (pixbuf);
516 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
517 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
518 pixels = gdk_pixbuf_get_pixels (pixbuf);
520 xcimage = XcursorImageCreate (width, height);
525 dest = xcimage->pixels;
531 for (j = 0; j < height; j++)
533 src = pixels + j * rowstride;
534 for (i = 0; i < width; i++)
536 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
545 _gdk_x11_convert_to_format (pixels, rowstride,
546 (guchar *) dest, 4 * width,
548 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
549 GDK_MSB_FIRST : GDK_LSB_FIRST,
558 * gdk_cursor_new_from_pixbuf:
559 * @display: the #GdkDisplay for which the cursor will be created
560 * @pixbuf: the #GdkPixbuf containing the cursor image
561 * @x: the horizontal offset of the 'hotspot' of the cursor.
562 * @y: the vertical offset of the 'hotspot' of the cursor.
564 * Creates a new cursor from a pixbuf.
566 * Not all GDK backends support RGBA cursors. If they are not
567 * supported, a monochrome approximation will be displayed.
568 * The functions gdk_display_supports_cursor_alpha() and
569 * gdk_display_supports_cursor_color() can be used to determine
570 * whether RGBA cursors are supported;
571 * gdk_display_get_default_cursor_size() and
572 * gdk_display_get_maximal_cursor_size() give information about
575 * On the X backend, support for RGBA cursors requires a
576 * sufficently new version of the X Render extension.
578 * Returns: a new #GdkCursor.
583 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
588 XcursorImage *xcimage;
590 GdkCursorPrivate *private;
593 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
594 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
595 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
596 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
602 xcimage = create_cursor_image (pixbuf, x, y);
603 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
604 XcursorImageDestroy (xcimage);
607 private = g_new (GdkCursorPrivate, 1);
608 private->display = display;
609 private->xcursor = xcursor;
610 private->name = NULL;
611 cursor = (GdkCursor *) private;
612 cursor->type = GDK_CURSOR_IS_PIXMAP;
613 cursor->ref_count = 1;
619 * gdk_cursor_new_from_name:
620 * @display: the #GdkDisplay for which the cursor will be created
621 * @name: the name of the cursor
623 * Creates a new cursor by looking up @name in the current cursor
626 * Returns: a new #GdkCursor, or %NULL if there is no cursor with
632 gdk_cursor_new_from_name (GdkDisplay *display,
637 GdkCursorPrivate *private;
640 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
646 xdisplay = GDK_DISPLAY_XDISPLAY (display);
647 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
652 private = g_new (GdkCursorPrivate, 1);
653 private->display = display;
654 private->xcursor = xcursor;
655 private->name = g_strdup (name);
656 cursor = (GdkCursor *) private;
657 cursor->type = GDK_CURSOR_IS_PIXMAP;
658 cursor->ref_count = 1;
664 * gdk_display_supports_cursor_alpha:
665 * @display: a #GdkDisplay
667 * Returns %TRUE if cursors can use an 8bit alpha channel
668 * on @display. Otherwise, cursors are restricted to bilevel
669 * alpha (i.e. a mask).
671 * Returns: whether cursors can have alpha channels.
676 gdk_display_supports_cursor_alpha (GdkDisplay *display)
678 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
680 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
684 * gdk_display_supports_cursor_color:
685 * @display: a #GdkDisplay
687 * Returns %TRUE if multicolored cursors are supported
688 * on @display. Otherwise, cursors have only a forground
689 * and a background color.
691 * Returns: whether cursors can have multiple colors.
696 gdk_display_supports_cursor_color (GdkDisplay *display)
698 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
700 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
704 * gdk_display_get_default_cursor_size:
705 * @display: a #GdkDisplay
707 * Returns the default size to use for cursors on @display.
709 * Returns: the default cursor size.
714 gdk_display_get_default_cursor_size (GdkDisplay *display)
716 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
718 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
724 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
730 GdkPixmap *pixmap, *mask;
731 guint width, height, n_channels, rowstride, i, j;
732 guint8 *data, *mask_data, *pixels;
733 GdkColor fg = { 0, 0, 0, 0 };
734 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
737 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
738 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
740 width = gdk_pixbuf_get_width (pixbuf);
741 height = gdk_pixbuf_get_height (pixbuf);
743 g_return_val_if_fail (0 <= x && x < width, NULL);
744 g_return_val_if_fail (0 <= y && y < height, NULL);
746 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
747 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
748 pixels = gdk_pixbuf_get_pixels (pixbuf);
750 data = g_new0 (guint8, (width + 7) / 8 * height);
751 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
753 for (j = 0; j < height; j++)
755 guint8 *src = pixels + j * rowstride;
756 guint8 *d = data + (width + 7) / 8 * j;
757 guint8 *md = mask_data + (width + 7) / 8 * j;
759 for (i = 0; i < width; i++)
764 if (n_channels == 3 || src[3] >= 0x80)
776 screen = gdk_display_get_default_screen (display);
777 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
778 data, width, height);
780 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
781 mask_data, width, height);
783 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
785 g_object_unref (pixmap);
786 g_object_unref (mask);
795 gdk_cursor_new_from_name (GdkDisplay *display,
798 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
804 gdk_display_supports_cursor_alpha (GdkDisplay *display)
806 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
812 gdk_display_supports_cursor_color (GdkDisplay *display)
814 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
820 gdk_display_get_default_cursor_size (GdkDisplay *display)
822 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
824 /* no idea, really */
832 * gdk_display_get_maximal_cursor_size:
833 * @display: a #GdkDisplay
834 * @width: the return location for the maximal cursor width
835 * @height: the return location for the maximal cursor height
837 * Gets the maximal size to use for cursors on @display.
842 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
849 g_return_if_fail (GDK_IS_DISPLAY (display));
851 screen = gdk_display_get_default_screen (display);
852 window = gdk_screen_get_root_window (screen);
853 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
854 GDK_WINDOW_XWINDOW (window),
855 128, 128, width, height);
858 #define __GDK_CURSOR_X11_C__
859 #include "gdkaliasdef.c"