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 "gdkprivate-x11.h"
35 #include "gdkcursor.h"
36 #include "gdkpixmap-x11.h"
38 #include <gdk/gdkpixmap.h>
39 #include <gdk-pixbuf/gdk-pixbuf.h>
43 * gdk_cursor_new_for_display:
44 * @display: the #GdkDisplay for which the cursor will be created
45 * @cursor_type: cursor to create
47 * Creates a new cursor from the set of builtin cursors.
48 * Some useful ones are:
51 * <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
54 * <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
57 * <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
60 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
63 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
66 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
69 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
72 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
75 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
78 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
81 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
84 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
87 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
90 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
93 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
96 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
99 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
103 * To make the cursor invisible, use gdk_cursor_new_from_pixmap() to create
104 * a cursor with no pixels in it.
106 * Return value: a new #GdkCursor
111 gdk_cursor_new_for_display (GdkDisplay *display,
112 GdkCursorType cursor_type)
114 GdkCursorPrivate *private;
118 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
123 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display), cursor_type);
125 private = g_new (GdkCursorPrivate, 1);
126 private->display = display;
127 private->xcursor = xcursor;
128 cursor = (GdkCursor *) private;
129 cursor->type = cursor_type;
130 cursor->ref_count = 1;
136 * gdk_cursor_new_from_pixmap:
137 * @source: the pixmap specifying the cursor.
138 * @mask: the pixmap specifying the mask, which must be the same size as
140 * @fg: the foreground color, used for the bits in the source which are 1.
141 * The color does not have to be allocated first.
142 * @bg: the background color, used for the bits in the source which are 0.
143 * The color does not have to be allocated first.
144 * @x: the horizontal offset of the 'hotspot' of the cursor.
145 * @y: the vertical offset of the 'hotspot' of the cursor.
147 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
148 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
149 * The standard cursor size is 16 by 16 pixels. You can create a bitmap
150 * from inline data as in the below example.
152 * <example><title>Creating a custom cursor</title>
154 * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
155 * utility. *<!-- -->/
156 * #define cursor1_width 16
157 * #define cursor1_height 16
158 * static unsigned char cursor1_bits[] = {
159 * 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
160 * 0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
161 * 0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
163 * static unsigned char cursor1mask_bits[] = {
164 * 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
165 * 0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
166 * 0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
170 * GdkPixmap *source, *mask;
171 * GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
172 * GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
175 * source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
176 * cursor1_width, cursor1_height);
177 * mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
178 * cursor1_width, cursor1_height);
179 * cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
180 * gdk_pixmap_unref (source);
181 * gdk_pixmap_unref (mask);
184 * gdk_window_set_cursor (widget->window, cursor);
188 * Return value: a new #GdkCursor.
191 gdk_cursor_new_from_pixmap (GdkPixmap *source,
198 GdkCursorPrivate *private;
200 Pixmap source_pixmap, mask_pixmap;
205 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
206 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
207 g_return_val_if_fail (fg != NULL, NULL);
208 g_return_val_if_fail (bg != NULL, NULL);
210 source_pixmap = GDK_PIXMAP_XID (source);
211 mask_pixmap = GDK_PIXMAP_XID (mask);
212 display = GDK_PIXMAP_DISPLAY (source);
214 xfg.pixel = fg->pixel;
217 xfg.green = fg->green;
218 xbg.pixel = bg->pixel;
221 xbg.green = bg->green;
226 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
227 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
228 private = g_new (GdkCursorPrivate, 1);
229 private->display = display;
230 private->xcursor = xcursor;
231 cursor = (GdkCursor *) private;
232 cursor->type = GDK_CURSOR_IS_PIXMAP;
233 cursor->ref_count = 1;
239 _gdk_cursor_destroy (GdkCursor *cursor)
241 GdkCursorPrivate *private;
243 g_return_if_fail (cursor != NULL);
244 g_return_if_fail (cursor->ref_count == 0);
246 private = (GdkCursorPrivate *) cursor;
247 if (!private->display->closed && private->xcursor)
248 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
254 * gdk_x11_cursor_get_xdisplay:
255 * @cursor: a #GdkCursor.
257 * Returns the display of a #GdkCursor.
259 * Return value: an Xlib <type>Display*</type>.
262 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
264 g_return_val_if_fail (cursor != NULL, NULL);
266 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
270 * gdk_x11_cursor_get_xcursor:
271 * @cursor: a #GdkCursor.
273 * Returns the X cursor belonging to a #GdkCursor.
275 * Return value: an Xlib <type>Cursor</type>.
278 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
280 g_return_val_if_fail (cursor != NULL, None);
282 return ((GdkCursorPrivate *)cursor)->xcursor;
286 * gdk_cursor_get_display:
287 * @cursor: a #GdkCursor.
289 * Returns the display on which the #GdkCursor is defined.
291 * Returns: the #GdkDisplay associated to @cursor
297 gdk_cursor_get_display (GdkCursor *cursor)
299 g_return_val_if_fail (cursor != NULL, NULL);
301 return ((GdkCursorPrivate *)cursor)->display;
308 create_cursor_image (GdkPixbuf *pixbuf,
312 guint width, height, rowstride, n_channels;
313 guchar *pixels, *src;
314 XcursorImage *xcimage;
319 width = gdk_pixbuf_get_width (pixbuf);
320 height = gdk_pixbuf_get_height (pixbuf);
322 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
323 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
324 pixels = gdk_pixbuf_get_pixels (pixbuf);
326 xcimage = XcursorImageCreate (width, height);
331 dest = xcimage->pixels;
333 for (j = 0; j < height; j++)
335 src = pixels + j * rowstride;
336 for (i = 0; i < width; i++)
343 *dest = (a << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
354 * gdk_cursor_new_from_pixbuf:
355 * @display: the #GdkDisplay for which the cursor will be created
356 * @pixbuf: the #GdkPixbuf containing the cursor image
357 * @x: the horizontal offset of the 'hotspot' of the cursor.
358 * @y: the vertical offset of the 'hotspot' of the cursor.
360 * Creates a new cursor from a pixbuf.
362 * Not all GDK backends support RGBA cursors. If they are not
363 * supported, a monochrome approximation will be displayed.
364 * The functions gdk_display_supports_cursor_alpha() and
365 * gdk_display_supports_cursor_color() can be used to determine
366 * whether RGBA cursors are supported;
367 * gdk_display_get_default_cursor_size() and
368 * gdk_display_get_maximal_cursor_size() give information about
371 * On the X backend, support for RGBA cursors requires a
372 * sufficently new version of the X Render extension.
374 * Returns: a new #GdkCursor.
379 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
384 XcursorImage *xcimage;
386 GdkCursorPrivate *private;
389 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
390 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
391 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
392 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
398 xcimage = create_cursor_image (pixbuf, x, y);
399 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
400 XcursorImageDestroy (xcimage);
403 private = g_new (GdkCursorPrivate, 1);
404 private->display = display;
405 private->xcursor = xcursor;
406 cursor = (GdkCursor *) private;
407 cursor->type = GDK_CURSOR_IS_PIXMAP;
408 cursor->ref_count = 1;
414 * gdk_display_supports_cursor_alpha:
415 * @display: a #GdkDisplay
417 * Returns %TRUE if cursors can use an 8bit alpha channel
418 * on @display. Otherwise, cursors are restricted to bilevel
419 * alpha (i.e. a mask).
421 * Returns: whether cursors can have alpha channels.
426 gdk_display_supports_cursor_alpha (GdkDisplay *display)
428 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
430 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
434 * gdk_display_supports_cursor_color:
435 * @display: a #GdkDisplay
437 * Returns %TRUE if multicolored cursors are supported
438 * on @display. Otherwise, cursors have only a forground
439 * and a background color.
441 * Returns: whether cursors can have multiple colors.
446 gdk_display_supports_cursor_color (GdkDisplay *display)
448 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
450 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
454 * gdk_display_get_default_cursor_size:
455 * @display: a #GdkDisplay
457 * Returns the default size to use for cursors on @display.
459 * Returns: the default cursor size.
464 gdk_display_get_default_cursor_size (GdkDisplay *display)
466 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
468 return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
474 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
480 GdkPixmap *pixmap, *mask;
481 guint width, height, n_channels, rowstride, i, j;
482 guint8 *data, *mask_data, *pixels;
483 GdkColor fg = { 0, 0, 0, 0 };
484 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
487 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
488 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
490 width = gdk_pixbuf_get_width (pixbuf);
491 height = gdk_pixbuf_get_height (pixbuf);
493 g_return_val_if_fail (0 <= x && x < width, NULL);
494 g_return_val_if_fail (0 <= y && y < height, NULL);
496 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
497 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
498 pixels = gdk_pixbuf_get_pixels (pixbuf);
500 data = g_new0 (guint8, (width + 7) / 8 * height);
501 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
503 for (j = 0; j < height; j++)
505 guint8 *src = pixels + j * rowstride;
506 guint8 *d = data + (width + 7) / 8 * j;
507 guint8 *md = mask_data + (width + 7) / 8 * j;
509 for (i = 0; i < width; i++)
514 if (n_channels == 3 || src[3] >= 0x80)
526 screen = gdk_display_get_default_screen (display);
527 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
528 data, width, height);
530 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
531 mask_data, width, height);
533 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
535 g_object_unref (pixmap);
536 g_object_unref (mask);
545 gdk_display_supports_cursor_alpha (GdkDisplay *display)
547 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
553 gdk_display_supports_cursor_color (GdkDisplay *display)
555 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
561 gdk_display_default_cursor_size (GdkDisplay *display)
563 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
565 /* no idea, really */
573 * gdk_display_get_maximal_cursor_size:
574 * @display: a #GdkDisplay
575 * @width: the return location for the maximal cursor width
576 * @height: the return location for the maximal cursor height
578 * Returns the maximal size to use for cursors on @display.
583 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
590 g_return_if_fail (GDK_IS_DISPLAY (display));
592 screen = gdk_display_get_default_screen (display);
593 window = gdk_screen_get_root_window (screen);
594 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
595 GDK_WINDOW_XWINDOW (window),
596 128, 128, width, height);