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 #include <X11/cursorfont.h>
31 #include <X11/Xcursor/Xcursor.h>
34 #include <X11/extensions/Xfixes.h>
38 #include "gdkprivate-x11.h"
39 #include "gdkcursor.h"
40 #include "gdkpixmap-x11.h"
42 #include <gdk/gdkpixmap.h>
43 #define GDK_PIXBUF_ENABLE_BACKEND
44 #include <gdk-pixbuf/gdk-pixbuf.h>
49 * gdk_cursor_new_for_display:
50 * @display: the #GdkDisplay for which the cursor will be created
51 * @cursor_type: cursor to create
53 * Creates a new cursor from the set of builtin cursors.
54 * Some useful ones are:
57 * <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
60 * <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
63 * <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
66 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
69 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
72 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
75 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
78 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
81 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
84 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
87 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
90 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
93 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
96 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
99 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
102 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
105 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
109 * To make the cursor invisible, use gdk_cursor_new_from_pixmap() to create
110 * a cursor with no pixels in it.
112 * Return value: a new #GdkCursor
117 gdk_cursor_new_for_display (GdkDisplay *display,
118 GdkCursorType cursor_type)
120 GdkCursorPrivate *private;
124 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
129 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display), cursor_type);
131 private = g_new (GdkCursorPrivate, 1);
132 private->display = display;
133 private->xcursor = xcursor;
134 private->name = NULL;
135 cursor = (GdkCursor *) private;
136 cursor->type = cursor_type;
137 cursor->ref_count = 1;
143 * gdk_cursor_new_from_pixmap:
144 * @source: the pixmap specifying the cursor.
145 * @mask: the pixmap specifying the mask, which must be the same size as
147 * @fg: the foreground color, used for the bits in the source which are 1.
148 * The color does not have to be allocated first.
149 * @bg: the background color, used for the bits in the source which are 0.
150 * The color does not have to be allocated first.
151 * @x: the horizontal offset of the 'hotspot' of the cursor.
152 * @y: the vertical offset of the 'hotspot' of the cursor.
154 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
155 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
156 * The standard cursor size is 16 by 16 pixels. You can create a bitmap
157 * from inline data as in the below example.
159 * <example><title>Creating a custom cursor</title>
161 * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
162 * utility. *<!-- -->/
163 * #define cursor1_width 16
164 * #define cursor1_height 16
165 * static unsigned char cursor1_bits[] = {
166 * 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
167 * 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
168 * 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
170 * static unsigned char cursor1mask_bits[] = {
171 * 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
172 * 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
173 * 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
177 * GdkPixmap *source, *mask;
178 * GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
179 * GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
182 * source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
183 * cursor1_width, cursor1_height);
184 * mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
185 * cursor1_width, cursor1_height);
186 * cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
187 * gdk_pixmap_unref (source);
188 * gdk_pixmap_unref (mask);
191 * gdk_window_set_cursor (widget->window, cursor);
195 * Return value: a new #GdkCursor.
198 gdk_cursor_new_from_pixmap (GdkPixmap *source,
205 GdkCursorPrivate *private;
207 Pixmap source_pixmap, mask_pixmap;
212 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
213 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
214 g_return_val_if_fail (fg != NULL, NULL);
215 g_return_val_if_fail (bg != NULL, NULL);
217 source_pixmap = GDK_PIXMAP_XID (source);
218 mask_pixmap = GDK_PIXMAP_XID (mask);
219 display = GDK_PIXMAP_DISPLAY (source);
221 xfg.pixel = fg->pixel;
224 xfg.green = fg->green;
225 xbg.pixel = bg->pixel;
228 xbg.green = bg->green;
233 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
234 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
235 private = g_new (GdkCursorPrivate, 1);
236 private->display = display;
237 private->xcursor = xcursor;
238 private->name = NULL;
239 cursor = (GdkCursor *) private;
240 cursor->type = GDK_CURSOR_IS_PIXMAP;
241 cursor->ref_count = 1;
247 _gdk_cursor_destroy (GdkCursor *cursor)
249 GdkCursorPrivate *private;
251 g_return_if_fail (cursor != NULL);
252 g_return_if_fail (cursor->ref_count == 0);
254 private = (GdkCursorPrivate *) cursor;
255 if (!private->display->closed && private->xcursor)
256 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
258 g_free (private->name);
263 * gdk_x11_cursor_get_xdisplay:
264 * @cursor: a #GdkCursor.
266 * Returns the display of a #GdkCursor.
268 * Return value: an Xlib <type>Display*</type>.
271 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
273 g_return_val_if_fail (cursor != NULL, NULL);
275 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
279 * gdk_x11_cursor_get_xcursor:
280 * @cursor: a #GdkCursor.
282 * Returns the X cursor belonging to a #GdkCursor.
284 * Return value: an Xlib <type>Cursor</type>.
287 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
289 g_return_val_if_fail (cursor != NULL, None);
291 return ((GdkCursorPrivate *)cursor)->xcursor;
295 * gdk_cursor_get_display:
296 * @cursor: a #GdkCursor.
298 * Returns the display on which the #GdkCursor is defined.
300 * Returns: the #GdkDisplay associated to @cursor
306 gdk_cursor_get_display (GdkCursor *cursor)
308 g_return_val_if_fail (cursor != NULL, NULL);
310 return ((GdkCursorPrivate *)cursor)->display;
313 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
317 load_comments (const char *file,
321 XcursorImages *images = 0;
322 XcursorComments *comments = 0;
325 f = XcursorScanTheme (theme, file);
327 f = XcursorScanTheme ("default", file);
328 if (f == XCURSOR_SCAN_CORE)
332 XcursorFileLoad (f, &comments, &images);
336 XcursorImagesDestroy (images);
344 * gdk_cursor_get_image:
345 * @cursor: a #GdkCursor
347 * Returns a #GdkPixbuf with the image used to display the cursor.
349 * Note that depending on the capabilities of the windowing system and
350 * on the cursor, GDK may not be able to obtain the image data. In this
351 * case, %NULL is returned.
353 * Returns: a #GdkPixbuf representing @cursor, or %NULL
358 gdk_cursor_get_image (GdkCursor *cursor)
361 GdkCursorPrivate *private;
362 XcursorImages *images = NULL;
364 XcursorComments *comments;
373 g_return_val_if_fail (cursor != NULL, NULL);
375 private = (GdkCursorPrivate *) cursor;
377 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
379 size = XcursorGetDefaultSize (xdisplay);
380 theme = XcursorGetTheme (xdisplay);
382 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
385 images = XcursorLibraryLoadImages (private->name, theme, size);
388 images = XcursorShapeLoadImages (cursor->type, theme, size);
393 image = images->images[0];
395 data = g_malloc (4 * image->width * image->height);
396 memcpy (data, image->pixels, 4 * image->width * image->height);
398 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
399 8, image->width, image->height,
401 (GdkPixbufDestroyNotify)g_free, NULL);
404 gdk_pixbuf_set_option (pixbuf, "name", private->name);
405 g_snprintf (buf, 32, "%d", image->xhot);
406 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
407 g_snprintf (buf, 32, "%d", image->yhot);
408 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
411 comments = load_comments (images->name, theme);
414 for (i = 0; i < comments->ncomment; i++)
416 switch (comments->comments[i].comment_type)
418 case XCURSOR_COMMENT_COPYRIGHT:
419 gdk_pixbuf_set_option (pixbuf, "copyright", comments->comments[i].comment);
421 case XCURSOR_COMMENT_LICENSE:
422 gdk_pixbuf_set_option (pixbuf, "license", comments->comments[i].comment);
425 g_snprintf (buf, 32, "comment%d", j++);
426 gdk_pixbuf_set_option (pixbuf, buf, comments->comments[i].comment);
430 XcursorCommentsDestroy (comments);
433 XcursorImagesDestroy (images);
441 gdk_cursor_get_image (GdkCursor *cursor)
443 g_return_val_if_fail (cursor != NULL, NULL);
453 create_cursor_image (GdkPixbuf *pixbuf,
457 guint width, height, rowstride, n_channels;
458 guchar *pixels, *src;
459 XcursorImage *xcimage;
462 width = gdk_pixbuf_get_width (pixbuf);
463 height = gdk_pixbuf_get_height (pixbuf);
465 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
466 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
467 pixels = gdk_pixbuf_get_pixels (pixbuf);
469 xcimage = XcursorImageCreate (width, height);
474 dest = xcimage->pixels;
480 for (j = 0; j < height; j++)
482 src = pixels + j * rowstride;
483 for (i = 0; i < width; i++)
485 *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
494 _gdk_x11_convert_to_format (pixels, rowstride,
495 (guchar *) dest, 4 * width,
497 (G_BYTE_ORDER == G_BIG_ENDIAN) ?
498 GDK_MSB_FIRST : GDK_LSB_FIRST,
507 * gdk_cursor_new_from_pixbuf:
508 * @display: the #GdkDisplay for which the cursor will be created
509 * @pixbuf: the #GdkPixbuf containing the cursor image
510 * @x: the horizontal offset of the 'hotspot' of the cursor.
511 * @y: the vertical offset of the 'hotspot' of the cursor.
513 * Creates a new cursor from a pixbuf.
515 * Not all GDK backends support RGBA cursors. If they are not
516 * supported, a monochrome approximation will be displayed.
517 * The functions gdk_display_supports_cursor_alpha() and
518 * gdk_display_supports_cursor_color() can be used to determine
519 * whether RGBA cursors are supported;
520 * gdk_display_get_default_cursor_size() and
521 * gdk_display_get_maximal_cursor_size() give information about
524 * On the X backend, support for RGBA cursors requires a
525 * sufficently new version of the X Render extension.
527 * Returns: a new #GdkCursor.
532 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
537 XcursorImage *xcimage;
539 GdkCursorPrivate *private;
542 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
543 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
544 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
545 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
551 xcimage = create_cursor_image (pixbuf, x, y);
552 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
553 XcursorImageDestroy (xcimage);
556 private = g_new (GdkCursorPrivate, 1);
557 private->display = display;
558 private->xcursor = xcursor;
559 private->name = NULL;
560 cursor = (GdkCursor *) private;
561 cursor->type = GDK_CURSOR_IS_PIXMAP;
562 cursor->ref_count = 1;
568 * gdk_cursor_new_from_name:
569 * @display: the #GdkDisplay for which the cursor will be created
570 * @name: the name of the cursor
572 * Creates a new cursor by looking up @name in the current cursor
575 * Returns: a new #GdkCursor, or %NULL if there is no cursor with
581 gdk_cursor_new_from_name (GdkDisplay *display,
586 GdkCursorPrivate *private;
589 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
595 xdisplay = GDK_DISPLAY_XDISPLAY (display);
596 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
601 private = g_new (GdkCursorPrivate, 1);
602 private->display = display;
603 private->xcursor = xcursor;
604 private->name = g_strdup (name);
605 cursor = (GdkCursor *) private;
606 cursor->type = GDK_CURSOR_IS_PIXMAP;
607 cursor->ref_count = 1;
613 * gdk_display_supports_cursor_alpha:
614 * @display: a #GdkDisplay
616 * Returns %TRUE if cursors can use an 8bit alpha channel
617 * on @display. Otherwise, cursors are restricted to bilevel
618 * alpha (i.e. a mask).
620 * Returns: whether cursors can have alpha channels.
625 gdk_display_supports_cursor_alpha (GdkDisplay *display)
627 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
629 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
633 * gdk_display_supports_cursor_color:
634 * @display: a #GdkDisplay
636 * Returns %TRUE if multicolored cursors are supported
637 * on @display. Otherwise, cursors have only a forground
638 * and a background color.
640 * Returns: whether cursors can have multiple colors.
645 gdk_display_supports_cursor_color (GdkDisplay *display)
647 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
649 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
653 * gdk_display_get_default_cursor_size:
654 * @display: a #GdkDisplay
656 * Returns the default size to use for cursors on @display.
658 * Returns: the default cursor size.
663 gdk_display_get_default_cursor_size (GdkDisplay *display)
665 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
667 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
673 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
679 GdkPixmap *pixmap, *mask;
680 guint width, height, n_channels, rowstride, i, j;
681 guint8 *data, *mask_data, *pixels;
682 GdkColor fg = { 0, 0, 0, 0 };
683 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
686 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
687 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
689 width = gdk_pixbuf_get_width (pixbuf);
690 height = gdk_pixbuf_get_height (pixbuf);
692 g_return_val_if_fail (0 <= x && x < width, NULL);
693 g_return_val_if_fail (0 <= y && y < height, NULL);
695 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
696 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
697 pixels = gdk_pixbuf_get_pixels (pixbuf);
699 data = g_new0 (guint8, (width + 7) / 8 * height);
700 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
702 for (j = 0; j < height; j++)
704 guint8 *src = pixels + j * rowstride;
705 guint8 *d = data + (width + 7) / 8 * j;
706 guint8 *md = mask_data + (width + 7) / 8 * j;
708 for (i = 0; i < width; i++)
713 if (n_channels == 3 || src[3] >= 0x80)
725 screen = gdk_display_get_default_screen (display);
726 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
727 data, width, height);
729 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
730 mask_data, width, height);
732 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
734 g_object_unref (pixmap);
735 g_object_unref (mask);
744 gdk_cursor_new_from_name (GdkDisplay *display,
747 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
753 gdk_display_supports_cursor_alpha (GdkDisplay *display)
755 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
761 gdk_display_supports_cursor_color (GdkDisplay *display)
763 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
769 gdk_display_get_default_cursor_size (GdkDisplay *display)
771 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
773 /* no idea, really */
781 * gdk_display_get_maximal_cursor_size:
782 * @display: a #GdkDisplay
783 * @width: the return location for the maximal cursor width
784 * @height: the return location for the maximal cursor height
786 * Gets the maximal size to use for cursors on @display.
791 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
798 g_return_if_fail (GDK_IS_DISPLAY (display));
800 screen = gdk_display_get_default_screen (display);
801 window = gdk_screen_get_root_window (screen);
802 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
803 GDK_WINDOW_XWINDOW (window),
804 128, 128, width, height);
807 #define __GDK_CURSOR_X11_C__
808 #include "gdkaliasdef.c"