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,
400 GdkCursorPrivate *private;
401 Cursor new_cursor = None;
403 if (!GDK_IS_WINDOW (value))
406 cursor = _gdk_x11_window_get_cursor (GDK_WINDOW (value));
411 private = (GdkCursorPrivate *) cursor;
412 xdisplay = (Display *)data;
414 if (private->xcursor != None)
416 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
419 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
422 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
424 if (new_cursor != None)
425 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
430 * gdk_x11_display_set_cursor_theme:
431 * @display: a #GdkDisplay
432 * @theme: the name of the cursor theme to use
433 * @size: the cursor size to use
435 * Sets the cursor theme from which the images for cursor
438 * If the windowing system supports it, existing cursors created
439 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
440 * gdk_cursor_new_for_name() are updated to reflect the theme
441 * change. Custom cursors constructed with gdk_cursor_new_from_pixmap()
442 * or gdk_cursor_new_from_pixbuf() will have to be handled
443 * by the application (GTK+ applications can learn about
444 * cursor theme changes by listening for change notification
445 * for the corresponding #GtkSetting).
450 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
454 GdkDisplayX11 *display_x11;
459 g_return_if_fail (GDK_IS_DISPLAY (display));
461 display_x11 = GDK_DISPLAY_X11 (display);
462 xdisplay = GDK_DISPLAY_XDISPLAY (display);
464 old_theme = XcursorGetTheme (xdisplay);
465 old_size = XcursorGetDefaultSize (xdisplay);
467 if (old_size == size &&
468 old_theme && strcmp (old_theme, theme) == 0)
471 XcursorSetTheme (xdisplay, theme);
472 XcursorSetDefaultSize (xdisplay, size);
474 g_hash_table_foreach (display_x11->xid_ht,
475 update_cursor, xdisplay);
481 gdk_cursor_get_image (GdkCursor *cursor)
483 g_return_val_if_fail (cursor != NULL, NULL);
489 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
493 g_return_if_fail (GDK_IS_DISPLAY (display));
503 create_cursor_image (GdkPixbuf *pixbuf,
507 guint width, height, rowstride, n_channels;
508 guchar *pixels, *src;
509 XcursorImage *xcimage;
512 width = gdk_pixbuf_get_width (pixbuf);
513 height = gdk_pixbuf_get_height (pixbuf);
515 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
516 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
517 pixels = gdk_pixbuf_get_pixels (pixbuf);
519 xcimage = XcursorImageCreate (width, height);
524 dest = xcimage->pixels;
530 for (j = 0; j < height; j++)
532 src = pixels + j * rowstride;
533 for (i = 0; i < width; i++)
535 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
544 _gdk_x11_convert_to_format (pixels, rowstride,
545 (guchar *) dest, 4 * width,
547 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
548 GDK_MSB_FIRST : GDK_LSB_FIRST,
557 * gdk_cursor_new_from_pixbuf:
558 * @display: the #GdkDisplay for which the cursor will be created
559 * @pixbuf: the #GdkPixbuf containing the cursor image
560 * @x: the horizontal offset of the 'hotspot' of the cursor.
561 * @y: the vertical offset of the 'hotspot' of the cursor.
563 * Creates a new cursor from a pixbuf.
565 * Not all GDK backends support RGBA cursors. If they are not
566 * supported, a monochrome approximation will be displayed.
567 * The functions gdk_display_supports_cursor_alpha() and
568 * gdk_display_supports_cursor_color() can be used to determine
569 * whether RGBA cursors are supported;
570 * gdk_display_get_default_cursor_size() and
571 * gdk_display_get_maximal_cursor_size() give information about
574 * On the X backend, support for RGBA cursors requires a
575 * sufficently new version of the X Render extension.
577 * Returns: a new #GdkCursor.
582 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
587 XcursorImage *xcimage;
589 GdkCursorPrivate *private;
592 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
593 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
594 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
595 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
601 xcimage = create_cursor_image (pixbuf, x, y);
602 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
603 XcursorImageDestroy (xcimage);
606 private = g_new (GdkCursorPrivate, 1);
607 private->display = display;
608 private->xcursor = xcursor;
609 private->name = NULL;
610 cursor = (GdkCursor *) private;
611 cursor->type = GDK_CURSOR_IS_PIXMAP;
612 cursor->ref_count = 1;
618 * gdk_cursor_new_from_name:
619 * @display: the #GdkDisplay for which the cursor will be created
620 * @name: the name of the cursor
622 * Creates a new cursor by looking up @name in the current cursor
625 * Returns: a new #GdkCursor, or %NULL if there is no cursor with
631 gdk_cursor_new_from_name (GdkDisplay *display,
636 GdkCursorPrivate *private;
639 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
645 xdisplay = GDK_DISPLAY_XDISPLAY (display);
646 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
651 private = g_new (GdkCursorPrivate, 1);
652 private->display = display;
653 private->xcursor = xcursor;
654 private->name = g_strdup (name);
655 cursor = (GdkCursor *) private;
656 cursor->type = GDK_CURSOR_IS_PIXMAP;
657 cursor->ref_count = 1;
663 * gdk_display_supports_cursor_alpha:
664 * @display: a #GdkDisplay
666 * Returns %TRUE if cursors can use an 8bit alpha channel
667 * on @display. Otherwise, cursors are restricted to bilevel
668 * alpha (i.e. a mask).
670 * Returns: whether cursors can have alpha channels.
675 gdk_display_supports_cursor_alpha (GdkDisplay *display)
677 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
679 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
683 * gdk_display_supports_cursor_color:
684 * @display: a #GdkDisplay
686 * Returns %TRUE if multicolored cursors are supported
687 * on @display. Otherwise, cursors have only a forground
688 * and a background color.
690 * Returns: whether cursors can have multiple colors.
695 gdk_display_supports_cursor_color (GdkDisplay *display)
697 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
699 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
703 * gdk_display_get_default_cursor_size:
704 * @display: a #GdkDisplay
706 * Returns the default size to use for cursors on @display.
708 * Returns: the default cursor size.
713 gdk_display_get_default_cursor_size (GdkDisplay *display)
715 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
717 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
723 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
729 GdkPixmap *pixmap, *mask;
730 guint width, height, n_channels, rowstride, i, j;
731 guint8 *data, *mask_data, *pixels;
732 GdkColor fg = { 0, 0, 0, 0 };
733 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
736 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
737 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
739 width = gdk_pixbuf_get_width (pixbuf);
740 height = gdk_pixbuf_get_height (pixbuf);
742 g_return_val_if_fail (0 <= x && x < width, NULL);
743 g_return_val_if_fail (0 <= y && y < height, NULL);
745 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
746 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
747 pixels = gdk_pixbuf_get_pixels (pixbuf);
749 data = g_new0 (guint8, (width + 7) / 8 * height);
750 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
752 for (j = 0; j < height; j++)
754 guint8 *src = pixels + j * rowstride;
755 guint8 *d = data + (width + 7) / 8 * j;
756 guint8 *md = mask_data + (width + 7) / 8 * j;
758 for (i = 0; i < width; i++)
763 if (n_channels == 3 || src[3] >= 0x80)
775 screen = gdk_display_get_default_screen (display);
776 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
777 data, width, height);
779 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
780 mask_data, width, height);
782 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
784 g_object_unref (pixmap);
785 g_object_unref (mask);
794 gdk_cursor_new_from_name (GdkDisplay *display,
797 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
803 gdk_display_supports_cursor_alpha (GdkDisplay *display)
805 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
811 gdk_display_supports_cursor_color (GdkDisplay *display)
813 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
819 gdk_display_get_default_cursor_size (GdkDisplay *display)
821 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
823 /* no idea, really */
831 * gdk_display_get_maximal_cursor_size:
832 * @display: a #GdkDisplay
833 * @width: the return location for the maximal cursor width
834 * @height: the return location for the maximal cursor height
836 * Gets the maximal size to use for cursors on @display.
841 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
848 g_return_if_fail (GDK_IS_DISPLAY (display));
850 screen = gdk_display_get_default_screen (display);
851 window = gdk_screen_get_root_window (screen);
852 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
853 GDK_WINDOW_XWINDOW (window),
854 128, 128, width, height);
857 #define __GDK_CURSOR_X11_C__
858 #include "gdkaliasdef.c"