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 "gdkdisplay-x11.h"
44 #include "gdkpixmap-x11.h"
46 #include <gdk/gdkpixmap.h>
47 #include <gdk-pixbuf/gdk-pixbuf.h>
49 static guint theme_serial = 0;
51 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
52 * libXcursor searches, cursors are added to it but only removed when
53 * their display is closed. We make the assumption that since there are
54 * a small number of display's and a small number of cursor's that this
55 * list will stay small enough not to be a problem.
57 static GSList* cursor_cache = NULL;
59 struct cursor_cache_key
66 /* Caller should check if there is already a match first.
67 * Cursor MUST be either a typed cursor or a pixmap with
71 add_to_cache (GdkCursorPrivate* cursor)
73 cursor_cache = g_slist_prepend (cursor_cache, cursor);
75 /* Take a ref so that if the caller frees it we still have it */
76 gdk_cursor_ref ((GdkCursor*) cursor);
79 /* Returns 0 on a match
82 cache_compare_func (gconstpointer listelem,
85 GdkCursorPrivate* cursor = (GdkCursorPrivate*)listelem;
86 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
88 if ((cursor->cursor.type != key->type) ||
89 (cursor->display != key->display))
90 return 1; /* No match */
92 /* Elements marked as pixmap must be named cursors
93 * (since we don't store normal pixmap cursors
95 if (key->type == GDK_CURSOR_IS_PIXMAP)
96 return strcmp (key->name, cursor->name);
101 /* Returns the cursor if there is a match, NULL if not
102 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
103 * For unnamed, typed cursors, name shall be NULL
105 static GdkCursorPrivate*
106 find_in_cache (GdkDisplay *display,
111 struct cursor_cache_key key;
113 key.display = display;
117 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
120 return (GdkCursorPrivate *) res->data;
125 /* Called by gdk_display_x11_finalize to flush any cached cursors
126 * for a dead display.
129 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
132 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
134 itemp = &cursor_cache;
137 GdkCursorPrivate* cursor = (GdkCursorPrivate*)(item->data);
138 if (cursor->display == display)
141 gdk_cursor_unref ((GdkCursor*) cursor);
142 /* Remove this item from the list */
143 *(itemp) = item->next;
145 item = g_slist_next (item);
146 g_slist_free_1 (olditem);
150 itemp = &(item->next);
151 item = g_slist_next (item);
157 get_blank_cursor (GdkDisplay *display)
161 Pixmap source_pixmap;
165 screen = gdk_display_get_default_screen (display);
166 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
167 "\0\0\0\0\0\0\0\0", 1, 1);
169 source_pixmap = GDK_PIXMAP_XID (pixmap);
172 color.red = color.blue = color.green = 0;
177 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
178 source_pixmap, source_pixmap,
179 &color, &color, 1, 1);
180 g_object_unref (pixmap);
186 * gdk_cursor_new_for_display:
187 * @display: the #GdkDisplay for which the cursor will be created
188 * @cursor_type: cursor to create
190 * Creates a new cursor from the set of builtin cursors.
191 * Some useful ones are:
194 * <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
197 * <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
200 * <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
203 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
206 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
209 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
212 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
215 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
218 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
221 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
224 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
227 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
230 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
233 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
236 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
239 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
242 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
245 * #GDK_BLANK_CURSOR (Blank cursor). Since 2.16
249 * Return value: a new #GdkCursor
254 gdk_cursor_new_for_display (GdkDisplay *display,
255 GdkCursorType cursor_type)
257 GdkCursorPrivate *private;
261 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
269 private = find_in_cache (display, cursor_type, NULL);
273 /* Cache had it, add a ref for this user */
274 gdk_cursor_ref ((GdkCursor*) private);
276 return (GdkCursor*) private;
280 if (cursor_type != GDK_BLANK_CURSOR)
281 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
284 xcursor = get_blank_cursor (display);
288 private = g_new (GdkCursorPrivate, 1);
289 private->display = display;
290 private->xcursor = xcursor;
291 private->name = NULL;
292 private->serial = theme_serial;
294 cursor = (GdkCursor *) private;
295 cursor->type = cursor_type;
296 cursor->ref_count = 1;
299 add_to_cache (private);
305 * gdk_cursor_new_from_pixmap:
306 * @source: the pixmap specifying the cursor.
307 * @mask: the pixmap specifying the mask, which must be the same size as
309 * @fg: the foreground color, used for the bits in the source which are 1.
310 * The color does not have to be allocated first.
311 * @bg: the background color, used for the bits in the source which are 0.
312 * The color does not have to be allocated first.
313 * @x: the horizontal offset of the 'hotspot' of the cursor.
314 * @y: the vertical offset of the 'hotspot' of the cursor.
316 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
317 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
318 * The standard cursor size is 16 by 16 pixels. You can create a bitmap
319 * from inline data as in the below example.
321 * <example><title>Creating a custom cursor</title>
323 * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
324 * utility. *<!-- -->/
325 * #define cursor1_width 16
326 * #define cursor1_height 16
327 * static unsigned char cursor1_bits[] = {
328 * 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
329 * 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
330 * 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
332 * static unsigned char cursor1mask_bits[] = {
333 * 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
334 * 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
335 * 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
339 * GdkPixmap *source, *mask;
340 * GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
341 * GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
344 * source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
345 * cursor1_width, cursor1_height);
346 * mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
347 * cursor1_width, cursor1_height);
348 * cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
349 * g_object_unref (source);
350 * g_object_unref (mask);
353 * gdk_window_set_cursor (widget->window, cursor);
357 * Return value: a new #GdkCursor.
360 gdk_cursor_new_from_pixmap (GdkPixmap *source,
367 GdkCursorPrivate *private;
369 Pixmap source_pixmap, mask_pixmap;
374 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
375 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
376 g_return_val_if_fail (fg != NULL, NULL);
377 g_return_val_if_fail (bg != NULL, NULL);
379 source_pixmap = GDK_PIXMAP_XID (source);
380 mask_pixmap = GDK_PIXMAP_XID (mask);
381 display = GDK_PIXMAP_DISPLAY (source);
383 xfg.pixel = fg->pixel;
386 xfg.green = fg->green;
387 xbg.pixel = bg->pixel;
390 xbg.green = bg->green;
395 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
396 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
397 private = g_new (GdkCursorPrivate, 1);
398 private->display = display;
399 private->xcursor = xcursor;
400 private->name = NULL;
401 private->serial = theme_serial;
403 cursor = (GdkCursor *) private;
404 cursor->type = GDK_CURSOR_IS_PIXMAP;
405 cursor->ref_count = 1;
411 _gdk_cursor_destroy (GdkCursor *cursor)
413 GdkCursorPrivate *private;
415 g_return_if_fail (cursor != NULL);
416 g_return_if_fail (cursor->ref_count == 0);
418 private = (GdkCursorPrivate *) cursor;
419 if (!private->display->closed && private->xcursor)
420 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
422 g_free (private->name);
427 * gdk_x11_cursor_get_xdisplay:
428 * @cursor: a #GdkCursor.
430 * Returns the display of a #GdkCursor.
432 * Return value: an Xlib <type>Display*</type>.
435 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
437 g_return_val_if_fail (cursor != NULL, NULL);
439 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
443 * gdk_x11_cursor_get_xcursor:
444 * @cursor: a #GdkCursor.
446 * Returns the X cursor belonging to a #GdkCursor.
448 * Return value: an Xlib <type>Cursor</type>.
451 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
453 g_return_val_if_fail (cursor != NULL, None);
455 return ((GdkCursorPrivate *)cursor)->xcursor;
459 * gdk_cursor_get_display:
460 * @cursor: a #GdkCursor.
462 * Returns the display on which the #GdkCursor is defined.
464 * Returns: the #GdkDisplay associated to @cursor
470 gdk_cursor_get_display (GdkCursor *cursor)
472 g_return_val_if_fail (cursor != NULL, NULL);
474 return ((GdkCursorPrivate *)cursor)->display;
477 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
480 * gdk_cursor_get_image:
481 * @cursor: a #GdkCursor
483 * Returns a #GdkPixbuf with the image used to display the cursor.
485 * Note that depending on the capabilities of the windowing system and
486 * on the cursor, GDK may not be able to obtain the image data. In this
487 * case, %NULL is returned.
489 * Returns: a #GdkPixbuf representing @cursor, or %NULL
494 gdk_cursor_get_image (GdkCursor *cursor)
497 GdkCursorPrivate *private;
498 XcursorImages *images = NULL;
502 guchar *data, *p, tmp;
506 g_return_val_if_fail (cursor != NULL, NULL);
508 private = (GdkCursorPrivate *) cursor;
510 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
512 size = XcursorGetDefaultSize (xdisplay);
513 theme = XcursorGetTheme (xdisplay);
515 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
518 images = XcursorLibraryLoadImages (private->name, theme, size);
521 images = XcursorShapeLoadImages (cursor->type, theme, size);
526 image = images->images[0];
528 data = g_malloc (4 * image->width * image->height);
529 memcpy (data, image->pixels, 4 * image->width * image->height);
531 for (p = data; p < data + (4 * image->width * image->height); p += 4)
538 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
539 8, image->width, image->height,
541 (GdkPixbufDestroyNotify)g_free, NULL);
544 gdk_pixbuf_set_option (pixbuf, "name", private->name);
545 g_snprintf (buf, 32, "%d", image->xhot);
546 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
547 g_snprintf (buf, 32, "%d", image->yhot);
548 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
550 XcursorImagesDestroy (images);
556 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
559 GdkCursorPrivate *private;
560 Cursor new_cursor = None;
561 GdkDisplayX11 *display_x11;
563 private = (GdkCursorPrivate *) cursor;
564 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
565 display_x11 = GDK_DISPLAY_X11 (private->display);
567 if (!display_x11->have_xfixes)
570 if (private->serial == theme_serial)
573 private->serial = theme_serial;
575 if (private->xcursor != None)
577 if (cursor->type == GDK_BLANK_CURSOR)
580 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
583 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
586 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
588 if (new_cursor != None)
590 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
591 private->xcursor = new_cursor;
597 update_cursor (gpointer data,
602 cursor = (GdkCursor*)(data);
607 _gdk_x11_cursor_update_theme (cursor);
611 * gdk_x11_display_set_cursor_theme:
612 * @display: a #GdkDisplay
613 * @theme: the name of the cursor theme to use, or %NULL to unset
614 * a previously set value
615 * @size: the cursor size to use, or 0 to keep the previous size
617 * Sets the cursor theme from which the images for cursor
620 * If the windowing system supports it, existing cursors created
621 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
622 * gdk_cursor_new_for_name() are updated to reflect the theme
623 * change. Custom cursors constructed with gdk_cursor_new_from_pixmap()
624 * or gdk_cursor_new_from_pixbuf() will have to be handled
625 * by the application (GTK+ applications can learn about
626 * cursor theme changes by listening for change notification
627 * for the corresponding #GtkSetting).
632 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
636 GdkDisplayX11 *display_x11;
641 g_return_if_fail (GDK_IS_DISPLAY (display));
643 display_x11 = GDK_DISPLAY_X11 (display);
644 xdisplay = GDK_DISPLAY_XDISPLAY (display);
646 old_theme = XcursorGetTheme (xdisplay);
647 old_size = XcursorGetDefaultSize (xdisplay);
649 if (old_size == size &&
650 (old_theme == theme ||
651 (old_theme && theme && strcmp (old_theme, theme) == 0)))
656 XcursorSetTheme (xdisplay, theme);
658 XcursorSetDefaultSize (xdisplay, size);
660 g_slist_foreach (cursor_cache, update_cursor, NULL);
666 gdk_cursor_get_image (GdkCursor *cursor)
668 g_return_val_if_fail (cursor != NULL, NULL);
674 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
678 g_return_if_fail (GDK_IS_DISPLAY (display));
682 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
684 g_return_if_fail (cursor != NULL);
692 create_cursor_image (GdkPixbuf *pixbuf,
696 guint width, height, rowstride, n_channels;
697 guchar *pixels, *src;
698 XcursorImage *xcimage;
701 width = gdk_pixbuf_get_width (pixbuf);
702 height = gdk_pixbuf_get_height (pixbuf);
704 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
705 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
706 pixels = gdk_pixbuf_get_pixels (pixbuf);
708 xcimage = XcursorImageCreate (width, height);
713 dest = xcimage->pixels;
719 for (j = 0; j < height; j++)
721 src = pixels + j * rowstride;
722 for (i = 0; i < width; i++)
724 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
733 _gdk_x11_convert_to_format (pixels, rowstride,
734 (guchar *) dest, 4 * width,
736 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
737 GDK_MSB_FIRST : GDK_LSB_FIRST,
746 * gdk_cursor_new_from_pixbuf:
747 * @display: the #GdkDisplay for which the cursor will be created
748 * @pixbuf: the #GdkPixbuf containing the cursor image
749 * @x: the horizontal offset of the 'hotspot' of the cursor.
750 * @y: the vertical offset of the 'hotspot' of the cursor.
752 * Creates a new cursor from a pixbuf.
754 * Not all GDK backends support RGBA cursors. If they are not
755 * supported, a monochrome approximation will be displayed.
756 * The functions gdk_display_supports_cursor_alpha() and
757 * gdk_display_supports_cursor_color() can be used to determine
758 * whether RGBA cursors are supported;
759 * gdk_display_get_default_cursor_size() and
760 * gdk_display_get_maximal_cursor_size() give information about
763 * On the X backend, support for RGBA cursors requires a
764 * sufficently new version of the X Render extension.
766 * Returns: a new #GdkCursor.
771 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
776 XcursorImage *xcimage;
778 GdkCursorPrivate *private;
781 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
782 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
783 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
784 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
790 xcimage = create_cursor_image (pixbuf, x, y);
791 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
792 XcursorImageDestroy (xcimage);
795 private = g_new (GdkCursorPrivate, 1);
796 private->display = display;
797 private->xcursor = xcursor;
798 private->name = NULL;
799 private->serial = theme_serial;
801 cursor = (GdkCursor *) private;
802 cursor->type = GDK_CURSOR_IS_PIXMAP;
803 cursor->ref_count = 1;
809 * gdk_cursor_new_from_name:
810 * @display: the #GdkDisplay for which the cursor will be created
811 * @name: the name of the cursor
813 * Creates a new cursor by looking up @name in the current cursor
816 * Returns: a new #GdkCursor, or %NULL if there is no cursor with
822 gdk_cursor_new_from_name (GdkDisplay *display,
827 GdkCursorPrivate *private;
830 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
836 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
840 /* Cache had it, add a ref for this user */
841 gdk_cursor_ref ((GdkCursor*) private);
843 return (GdkCursor*) private;
846 xdisplay = GDK_DISPLAY_XDISPLAY (display);
847 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
852 private = g_new (GdkCursorPrivate, 1);
853 private->display = display;
854 private->xcursor = xcursor;
855 private->name = g_strdup (name);
856 private->serial = theme_serial;
858 cursor = (GdkCursor *) private;
859 cursor->type = GDK_CURSOR_IS_PIXMAP;
860 cursor->ref_count = 1;
861 add_to_cache (private);
867 * gdk_display_supports_cursor_alpha:
868 * @display: a #GdkDisplay
870 * Returns %TRUE if cursors can use an 8bit alpha channel
871 * on @display. Otherwise, cursors are restricted to bilevel
872 * alpha (i.e. a mask).
874 * Returns: whether cursors can have alpha channels.
879 gdk_display_supports_cursor_alpha (GdkDisplay *display)
881 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
883 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
887 * gdk_display_supports_cursor_color:
888 * @display: a #GdkDisplay
890 * Returns %TRUE if multicolored cursors are supported
891 * on @display. Otherwise, cursors have only a forground
892 * and a background color.
894 * Returns: whether cursors can have multiple colors.
899 gdk_display_supports_cursor_color (GdkDisplay *display)
901 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
903 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
907 * gdk_display_get_default_cursor_size:
908 * @display: a #GdkDisplay
910 * Returns the default size to use for cursors on @display.
912 * Returns: the default cursor size.
917 gdk_display_get_default_cursor_size (GdkDisplay *display)
919 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
921 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
927 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
933 GdkPixmap *pixmap, *mask;
934 guint width, height, n_channels, rowstride, data_stride, i, j;
935 guint8 *data, *mask_data, *pixels;
936 GdkColor fg = { 0, 0, 0, 0 };
937 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
939 cairo_surface_t *image;
942 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
943 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
945 width = gdk_pixbuf_get_width (pixbuf);
946 height = gdk_pixbuf_get_height (pixbuf);
948 g_return_val_if_fail (0 <= x && x < width, NULL);
949 g_return_val_if_fail (0 <= y && y < height, NULL);
951 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
952 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
953 pixels = gdk_pixbuf_get_pixels (pixbuf);
955 data_stride = 4 * ((width + 31) / 32);
956 data = g_new0 (guint8, data_stride * height);
957 mask_data = g_new0 (guint8, data_stride * height);
959 for (j = 0; j < height; j++)
961 guint8 *src = pixels + j * rowstride;
962 guint8 *d = data + data_stride * j;
963 guint8 *md = mask_data + data_stride * j;
965 for (i = 0; i < width; i++)
970 if (n_channels == 3 || src[3] >= 0x80)
982 screen = gdk_display_get_default_screen (display);
984 pixmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
986 cr = gdk_cairo_create (pixmap);
987 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
988 width, height, data_stride);
989 cairo_set_source_surface (cr, image, 0, 0);
990 cairo_surface_destroy (image);
991 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
995 mask = gdk_pixmap_new (gdk_screen_get_root_window (screen),
997 cr = gdk_cairo_create (mask);
998 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
999 width, height, data_stride);
1000 cairo_set_source_surface (cr, image, 0, 0);
1001 cairo_surface_destroy (image);
1002 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1006 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
1008 g_object_unref (pixmap);
1009 g_object_unref (mask);
1018 gdk_cursor_new_from_name (GdkDisplay *display,
1021 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1027 gdk_display_supports_cursor_alpha (GdkDisplay *display)
1029 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1035 gdk_display_supports_cursor_color (GdkDisplay *display)
1037 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1043 gdk_display_get_default_cursor_size (GdkDisplay *display)
1045 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1047 /* no idea, really */
1055 * gdk_display_get_maximal_cursor_size:
1056 * @display: a #GdkDisplay
1057 * @width: (out): the return location for the maximal cursor width
1058 * @height: (out): the return location for the maximal cursor height
1060 * Gets the maximal size to use for cursors on @display.
1065 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
1072 g_return_if_fail (GDK_IS_DISPLAY (display));
1074 screen = gdk_display_get_default_screen (display);
1075 window = gdk_screen_get_root_window (screen);
1076 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
1077 GDK_WINDOW_XWINDOW (window),
1078 128, 128, width, height);