]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcursor-x11.c
remove GDK_CURSOR and GDK_IS_CURSOR, instance checks don't work on boxed
[~andy/gtk] / gdk / x11 / gdkcursor-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <config.h>
28
29 #define GDK_PIXBUF_ENABLE_BACKEND
30
31 #include <X11/Xlib.h>
32 #include <X11/cursorfont.h>
33 #ifdef HAVE_XCURSOR
34 #include <X11/Xcursor/Xcursor.h>
35 #endif
36 #ifdef HAVE_XFIXES
37 #include <X11/extensions/Xfixes.h>
38 #endif
39 #include <string.h>
40
41 #include "gdkprivate-x11.h"
42 #include "gdkcursor.h"
43 #include "gdkpixmap-x11.h"
44 #include "gdkx.h"
45 #include <gdk/gdkpixmap.h>
46 #include <gdk-pixbuf/gdk-pixbuf.h>
47 #include "gdkalias.h"
48
49 static guint theme_serial = 0;
50
51 /**
52  * gdk_cursor_new_for_display:
53  * @display: the #GdkDisplay for which the cursor will be created
54  * @cursor_type: cursor to create
55  * 
56  * Creates a new cursor from the set of builtin cursors.
57  * Some useful ones are:
58  * <itemizedlist>
59  * <listitem><para>
60  *  <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
61  * </para></listitem>
62  * <listitem><para>
63  *  <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
64  * </para></listitem>
65  * <listitem><para>
66  *  <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
67  * </para></listitem>
68  * <listitem><para>
69  * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
70  * </para></listitem>
71  * <listitem><para>
72  * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
73  * </para></listitem>
74  * <listitem><para>
75  * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
76  * </para></listitem>
77  * <listitem><para>
78  * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
79  * </para></listitem>
80  * <listitem><para>
81  * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
82  * </para></listitem>
83  * <listitem><para>
84  * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
85  * </para></listitem>
86  * <listitem><para>
87  * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
88  * </para></listitem>
89  * <listitem><para>
90  * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
91  * </para></listitem>
92  * <listitem><para>
93  * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
94  * </para></listitem>
95  * <listitem><para>
96  * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
97  * </para></listitem>
98  * <listitem><para>
99  * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
100  * </para></listitem>
101  * <listitem><para>
102  * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
103  * </para></listitem>
104  * <listitem><para>
105  * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
106  * </para></listitem>
107  * <listitem><para>
108  * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
109  * </para></listitem>
110  * </itemizedlist>
111  *
112  * To make the cursor invisible, use gdk_cursor_new_from_pixmap() to create
113  * a cursor with no pixels in it.
114  * 
115  * Return value: a new #GdkCursor
116  *
117  * Since: 2.2
118  **/
119 GdkCursor*
120 gdk_cursor_new_for_display (GdkDisplay    *display,
121                             GdkCursorType  cursor_type)
122 {
123   GdkCursorPrivate *private;
124   GdkCursor *cursor;
125   Cursor xcursor;
126
127   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
128
129   if (display->closed)
130     xcursor = None;
131   else
132     xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display), cursor_type);
133   
134   private = g_new (GdkCursorPrivate, 1);
135   private->display = display;
136   private->xcursor = xcursor;
137   private->name = NULL;
138   private->serial = theme_serial;
139
140   cursor = (GdkCursor *) private;
141   cursor->type = cursor_type;
142   cursor->ref_count = 1;
143   
144   return cursor;
145 }
146
147 /**
148  * gdk_cursor_new_from_pixmap:
149  * @source: the pixmap specifying the cursor.
150  * @mask: the pixmap specifying the mask, which must be the same size as 
151  *    @source.
152  * @fg: the foreground color, used for the bits in the source which are 1.
153  *    The color does not have to be allocated first. 
154  * @bg: the background color, used for the bits in the source which are 0.
155  *    The color does not have to be allocated first.
156  * @x: the horizontal offset of the 'hotspot' of the cursor. 
157  * @y: the vertical offset of the 'hotspot' of the cursor.
158  * 
159  * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
160  * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
161  * The standard cursor size is 16 by 16 pixels. You can create a bitmap 
162  * from inline data as in the below example.
163  * 
164  * <example><title>Creating a custom cursor</title>
165  * <programlisting>
166  * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
167  *    utility. *<!-- -->/
168  * &num;define cursor1_width 16
169  * &num;define cursor1_height 16
170  * static unsigned char cursor1_bits[] = {
171  *   0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
172  *   0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
173  *   0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
174  *  
175  * static unsigned char cursor1mask_bits[] = {
176  *   0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
177  *   0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
178  *   0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
179  *  
180  *  
181  *  GdkCursor *cursor;
182  *  GdkPixmap *source, *mask;
183  *  GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
184  *  GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
185  *  
186  *  
187  *  source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
188  *                                        cursor1_width, cursor1_height);
189  *  mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
190  *                                      cursor1_width, cursor1_height);
191  *  cursor = gdk_cursor_new_from_pixmap (source, mask, &amp;fg, &amp;bg, 8, 8);
192  *  gdk_pixmap_unref (source);
193  *  gdk_pixmap_unref (mask);
194  *  
195  *  
196  *  gdk_window_set_cursor (widget->window, cursor);
197  * </programlisting>
198  * </example>
199  *
200  * Return value: a new #GdkCursor.
201  **/
202 GdkCursor*
203 gdk_cursor_new_from_pixmap (GdkPixmap      *source,
204                             GdkPixmap      *mask,
205                             const GdkColor *fg,
206                             const GdkColor *bg,
207                             gint            x,
208                             gint            y)
209 {
210   GdkCursorPrivate *private;
211   GdkCursor *cursor;
212   Pixmap source_pixmap, mask_pixmap;
213   Cursor xcursor;
214   XColor xfg, xbg;
215   GdkDisplay *display;
216
217   g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
218   g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
219   g_return_val_if_fail (fg != NULL, NULL);
220   g_return_val_if_fail (bg != NULL, NULL);
221
222   source_pixmap = GDK_PIXMAP_XID (source);
223   mask_pixmap   = GDK_PIXMAP_XID (mask);
224   display = GDK_PIXMAP_DISPLAY (source);
225
226   xfg.pixel = fg->pixel;
227   xfg.red = fg->red;
228   xfg.blue = fg->blue;
229   xfg.green = fg->green;
230   xbg.pixel = bg->pixel;
231   xbg.red = bg->red;
232   xbg.blue = bg->blue;
233   xbg.green = bg->green;
234   
235   if (display->closed)
236     xcursor = None;
237   else
238     xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
239                                    source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
240   private = g_new (GdkCursorPrivate, 1);
241   private->display = display;
242   private->xcursor = xcursor;
243   private->name = NULL;
244   private->serial = theme_serial;
245
246   cursor = (GdkCursor *) private;
247   cursor->type = GDK_CURSOR_IS_PIXMAP;
248   cursor->ref_count = 1;
249   
250   return cursor;
251 }
252
253 void
254 _gdk_cursor_destroy (GdkCursor *cursor)
255 {
256   GdkCursorPrivate *private;
257
258   g_return_if_fail (cursor != NULL);
259   g_return_if_fail (cursor->ref_count == 0);
260
261   private = (GdkCursorPrivate *) cursor;
262   if (!private->display->closed && private->xcursor)
263     XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
264
265   g_free (private->name);
266   g_free (private);
267 }
268
269 /**
270  * gdk_x11_cursor_get_xdisplay:
271  * @cursor: a #GdkCursor.
272  * 
273  * Returns the display of a #GdkCursor.
274  * 
275  * Return value: an Xlib <type>Display*</type>.
276  **/
277 Display *
278 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
279 {
280   g_return_val_if_fail (cursor != NULL, NULL);
281
282   return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
283 }
284
285 /**
286  * gdk_x11_cursor_get_xcursor:
287  * @cursor: a #GdkCursor.
288  * 
289  * Returns the X cursor belonging to a #GdkCursor.
290  * 
291  * Return value: an Xlib <type>Cursor</type>.
292  **/
293 Cursor
294 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
295 {
296   g_return_val_if_fail (cursor != NULL, None);
297
298   return ((GdkCursorPrivate *)cursor)->xcursor;
299 }
300
301 /** 
302  * gdk_cursor_get_display:
303  * @cursor: a #GdkCursor.
304  *
305  * Returns the display on which the #GdkCursor is defined.
306  *
307  * Returns: the #GdkDisplay associated to @cursor
308  *
309  * Since: 2.2
310  */
311
312 GdkDisplay *
313 gdk_cursor_get_display (GdkCursor *cursor)
314 {
315   g_return_val_if_fail (cursor != NULL, NULL);
316
317   return ((GdkCursorPrivate *)cursor)->display;
318 }
319
320 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
321
322 /**
323  * gdk_cursor_get_image:
324  * @cursor: a #GdkCursor
325  *
326  * Returns a #GdkPixbuf with the image used to display the cursor.
327  *
328  * Note that depending on the capabilities of the windowing system and 
329  * on the cursor, GDK may not be able to obtain the image data. In this 
330  * case, %NULL is returned.
331  *
332  * Returns: a #GdkPixbuf representing @cursor, or %NULL
333  *
334  * Since: 2.8
335  */
336 GdkPixbuf*  
337 gdk_cursor_get_image (GdkCursor *cursor)
338 {
339   Display *xdisplay;
340   GdkCursorPrivate *private;
341   XcursorImages *images = NULL;
342   XcursorImage *image;
343   gint size;
344   gchar buf[32];
345   guchar *data, *p, tmp;
346   GdkPixbuf *pixbuf;
347   gchar *theme;
348   
349   g_return_val_if_fail (cursor != NULL, NULL);
350
351   private = (GdkCursorPrivate *) cursor;
352     
353   xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
354
355   size = XcursorGetDefaultSize (xdisplay);
356   theme = XcursorGetTheme (xdisplay);
357
358   if (cursor->type == GDK_CURSOR_IS_PIXMAP)
359     {
360       if (private->name)
361         images = XcursorLibraryLoadImages (private->name, theme, size);
362     }
363   else 
364     images = XcursorShapeLoadImages (cursor->type, theme, size);
365
366   if (!images)
367     return NULL;
368   
369   image = images->images[0];
370
371   data = g_malloc (4 * image->width * image->height);
372   memcpy (data, image->pixels, 4 * image->width * image->height);
373
374   for (p = data; p < data + (4 * image->width * image->height); p += 4)
375     {
376       tmp = p[0];
377       p[0] = p[2];
378       p[2] = tmp;
379     }
380
381   pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
382                                      8, image->width, image->height,
383                                      4 * image->width, 
384                                      (GdkPixbufDestroyNotify)g_free, NULL);
385
386   if (private->name)
387     gdk_pixbuf_set_option (pixbuf, "name", private->name);
388   g_snprintf (buf, 32, "%d", image->xhot);
389   gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
390   g_snprintf (buf, 32, "%d", image->yhot);
391   gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
392
393   XcursorImagesDestroy (images);
394
395   return pixbuf;
396 }
397
398 void
399 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
400 {
401   Display *xdisplay;
402   GdkCursorPrivate *private;
403   Cursor new_cursor = None;
404
405   private = (GdkCursorPrivate *) cursor;
406   xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
407           
408   if (private->serial == theme_serial)
409     return;
410
411   private->serial = theme_serial;
412
413   if (private->xcursor != None)
414     {
415       if (cursor->type == GDK_CURSOR_IS_PIXMAP)
416         {
417           if (private->name)
418             new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
419         }
420       else 
421         new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
422       
423       if (new_cursor != None)
424         XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
425     }
426 }
427
428 static void
429 update_cursor (gpointer key,
430                gpointer value,
431                gpointer data)
432 {
433   GdkCursor *cursor;
434
435   if (!GDK_IS_WINDOW (value))
436     return;
437
438   cursor = _gdk_x11_window_get_cursor (GDK_WINDOW (value));
439
440   if (!cursor)
441     return;
442   
443   _gdk_x11_cursor_update_theme (cursor);
444 }
445
446 /**
447  * gdk_x11_display_set_cursor_theme:
448  * @display: a #GdkDisplay
449  * @theme: the name of the cursor theme to use
450  * @size: the cursor size to use
451  *
452  * Sets the cursor theme from which the images for cursor
453  * should be taken. 
454  * 
455  * If the windowing system supports it, existing cursors created 
456  * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
457  * gdk_cursor_new_for_name() are updated to reflect the theme 
458  * change. Custom cursors constructed with gdk_cursor_new_from_pixmap() 
459  * or gdk_cursor_new_from_pixbuf() will have to be handled
460  * by the application (GTK+ applications can learn about 
461  * cursor theme changes by listening for change notification
462  * for the corresponding #GtkSetting).
463  *
464  * Since: 2.8
465  */
466 void
467 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
468                                   const gchar *theme,
469                                   const gint   size)
470 {
471   GdkDisplayX11 *display_x11;
472   Display *xdisplay;
473   gchar *old_theme;
474   gint old_size;
475
476   g_return_if_fail (GDK_IS_DISPLAY (display));
477
478   display_x11 = GDK_DISPLAY_X11 (display);
479   xdisplay = GDK_DISPLAY_XDISPLAY (display);
480
481   old_theme = XcursorGetTheme (xdisplay);
482   old_size = XcursorGetDefaultSize (xdisplay);
483
484   if (old_size == size && 
485       old_theme && strcmp (old_theme, theme) == 0)
486     return;
487
488   theme_serial++;
489
490   XcursorSetTheme (xdisplay, theme);
491   XcursorSetDefaultSize (xdisplay, size);
492     
493   g_hash_table_foreach (display_x11->xid_ht, update_cursor, NULL);
494 }
495
496 #else
497
498 GdkPixbuf*  
499 gdk_cursor_get_image (GdkCursor *cursor)
500 {
501   g_return_val_if_fail (cursor != NULL, NULL);
502   
503   return NULL;
504 }
505
506 void
507 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
508                                   const gchar *theme,
509                                   const gint   size)
510 {
511   g_return_if_fail (GDK_IS_DISPLAY (display));
512 }
513
514 void
515 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
516 {
517   g_return_if_fail (cursor != NULL);
518 }
519
520 #endif
521
522 #ifdef HAVE_XCURSOR
523
524 static XcursorImage*
525 create_cursor_image (GdkPixbuf *pixbuf,
526                      gint       x,
527                      gint       y)
528 {
529   guint width, height, rowstride, n_channels;
530   guchar *pixels, *src;
531   XcursorImage *xcimage;
532   XcursorPixel *dest;
533
534   width = gdk_pixbuf_get_width (pixbuf);
535   height = gdk_pixbuf_get_height (pixbuf);
536
537   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
538   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
539   pixels = gdk_pixbuf_get_pixels (pixbuf);
540
541   xcimage = XcursorImageCreate (width, height);
542
543   xcimage->xhot = x;
544   xcimage->yhot = y;
545
546   dest = xcimage->pixels;
547
548   if (n_channels == 3)
549     {
550       gint i, j;
551
552       for (j = 0; j < height; j++)
553         {
554           src = pixels + j * rowstride;
555           for (i = 0; i < width; i++)
556             {
557               *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
558             }
559
560           src += n_channels;
561           dest++;
562         }
563     }
564   else
565     {
566       _gdk_x11_convert_to_format (pixels, rowstride,
567                                   (guchar *) dest, 4 * width,
568                                   GDK_X11_FORMAT_ARGB,
569                                   (G_BYTE_ORDER == G_BIG_ENDIAN) ?
570                                   GDK_MSB_FIRST : GDK_LSB_FIRST,
571                                   width, height);
572     }
573
574   return xcimage;
575 }
576
577
578 /**
579  * gdk_cursor_new_from_pixbuf:
580  * @display: the #GdkDisplay for which the cursor will be created
581  * @pixbuf: the #GdkPixbuf containing the cursor image
582  * @x: the horizontal offset of the 'hotspot' of the cursor. 
583  * @y: the vertical offset of the 'hotspot' of the cursor.
584  *
585  * Creates a new cursor from a pixbuf. 
586  *
587  * Not all GDK backends support RGBA cursors. If they are not 
588  * supported, a monochrome approximation will be displayed. 
589  * The functions gdk_display_supports_cursor_alpha() and 
590  * gdk_display_supports_cursor_color() can be used to determine
591  * whether RGBA cursors are supported; 
592  * gdk_display_get_default_cursor_size() and 
593  * gdk_display_get_maximal_cursor_size() give information about 
594  * cursor sizes.
595  *
596  * On the X backend, support for RGBA cursors requires a
597  * sufficently new version of the X Render extension. 
598  *
599  * Returns: a new #GdkCursor.
600  * 
601  * Since: 2.4
602  */
603 GdkCursor *
604 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
605                             GdkPixbuf  *pixbuf,
606                             gint        x,
607                             gint        y)
608 {
609   XcursorImage *xcimage;
610   Cursor xcursor;
611   GdkCursorPrivate *private;
612   GdkCursor *cursor;
613
614   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
615   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
616   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
617   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
618
619   if (display->closed)
620     xcursor = None;
621   else 
622     {
623       xcimage = create_cursor_image (pixbuf, x, y);
624       xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
625       XcursorImageDestroy (xcimage);
626     }
627
628   private = g_new (GdkCursorPrivate, 1);
629   private->display = display;
630   private->xcursor = xcursor;
631   private->name = NULL;
632   private->serial = theme_serial;
633
634   cursor = (GdkCursor *) private;
635   cursor->type = GDK_CURSOR_IS_PIXMAP;
636   cursor->ref_count = 1;
637   
638   return cursor;
639 }
640
641 /**
642  * gdk_cursor_new_from_name:
643  * @display: the #GdkDisplay for which the cursor will be created
644  * @name: the name of the cursor
645  *
646  * Creates a new cursor by looking up @name in the current cursor
647  * theme. 
648  * 
649  * Returns: a new #GdkCursor, or %NULL if there is no cursor with 
650  *   the given name 
651  *
652  * Since: 2.8
653  */
654 GdkCursor*  
655 gdk_cursor_new_from_name (GdkDisplay  *display,
656                           const gchar *name)
657 {
658   Cursor xcursor;
659   Display *xdisplay;
660   GdkCursorPrivate *private;
661   GdkCursor *cursor;
662
663   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
664
665   if (display->closed)
666     xcursor = None;
667   else 
668     {
669       xdisplay = GDK_DISPLAY_XDISPLAY (display);
670       xcursor = XcursorLibraryLoadCursor (xdisplay, name);
671       if (xcursor == None)
672         return NULL;
673     }
674
675   private = g_new (GdkCursorPrivate, 1);
676   private->display = display;
677   private->xcursor = xcursor;
678   private->name = g_strdup (name);
679   private->serial = theme_serial;
680
681   cursor = (GdkCursor *) private;
682   cursor->type = GDK_CURSOR_IS_PIXMAP;
683   cursor->ref_count = 1;
684   
685   return cursor;
686 }
687
688 /**
689  * gdk_display_supports_cursor_alpha:
690  * @display: a #GdkDisplay
691  *
692  * Returns %TRUE if cursors can use an 8bit alpha channel 
693  * on @display. Otherwise, cursors are restricted to bilevel 
694  * alpha (i.e. a mask).
695  *
696  * Returns: whether cursors can have alpha channels.
697  *
698  * Since: 2.4
699  */
700 gboolean 
701 gdk_display_supports_cursor_alpha (GdkDisplay *display)
702 {
703   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
704
705   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
706 }
707
708 /**
709  * gdk_display_supports_cursor_color:
710  * @display: a #GdkDisplay
711  *
712  * Returns %TRUE if multicolored cursors are supported
713  * on @display. Otherwise, cursors have only a forground
714  * and a background color.
715  *
716  * Returns: whether cursors can have multiple colors.
717  *
718  * Since: 2.4
719  */
720 gboolean 
721 gdk_display_supports_cursor_color (GdkDisplay *display)
722 {
723   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
724
725   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
726 }
727
728 /**
729  * gdk_display_get_default_cursor_size:
730  * @display: a #GdkDisplay
731  *
732  * Returns the default size to use for cursors on @display.
733  *
734  * Returns: the default cursor size.
735  *
736  * Since: 2.4
737  */
738 guint     
739 gdk_display_get_default_cursor_size (GdkDisplay *display)
740 {
741   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
742
743   return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
744 }
745
746 #else
747
748 GdkCursor *
749 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
750                             GdkPixbuf  *pixbuf,
751                             gint        x,
752                             gint        y)
753 {
754   GdkCursor *cursor;
755   GdkPixmap *pixmap, *mask;
756   guint width, height, n_channels, rowstride, i, j;
757   guint8 *data, *mask_data, *pixels;
758   GdkColor fg = { 0, 0, 0, 0 };
759   GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
760   GdkScreen *screen;
761
762   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
763   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
764
765   width = gdk_pixbuf_get_width (pixbuf);
766   height = gdk_pixbuf_get_height (pixbuf);
767
768   g_return_val_if_fail (0 <= x && x < width, NULL);
769   g_return_val_if_fail (0 <= y && y < height, NULL);
770
771   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
772   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
773   pixels = gdk_pixbuf_get_pixels (pixbuf);
774
775   data = g_new0 (guint8, (width + 7) / 8 * height);
776   mask_data = g_new0 (guint8, (width + 7) / 8 * height);
777
778   for (j = 0; j < height; j++)
779     {
780       guint8 *src = pixels + j * rowstride;
781       guint8 *d = data + (width + 7) / 8 * j;
782       guint8 *md = mask_data + (width + 7) / 8 * j;
783         
784       for (i = 0; i < width; i++)
785         {
786           if (src[1] < 0x80)
787             *d |= 1 << (i % 8);
788           
789           if (n_channels == 3 || src[3] >= 0x80)
790             *md |= 1 << (i % 8);
791           
792           src += n_channels;
793           if (i % 8 == 7)
794             {
795               d++; 
796               md++;
797             }
798         }
799     }
800       
801   screen = gdk_display_get_default_screen (display);
802   pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
803                                         data, width, height);
804  
805   mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
806                                       mask_data, width, height);
807    
808   cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
809    
810   g_object_unref (pixmap);
811   g_object_unref (mask);
812
813   g_free (data);
814   g_free (mask_data);
815   
816   return cursor;
817 }
818
819 GdkCursor*  
820 gdk_cursor_new_from_name (GdkDisplay  *display,
821                           const gchar *name)
822 {
823   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
824
825   return NULL;
826 }
827
828 gboolean 
829 gdk_display_supports_cursor_alpha (GdkDisplay    *display)
830 {
831   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
832
833   return FALSE;
834 }
835
836 gboolean 
837 gdk_display_supports_cursor_color (GdkDisplay    *display)
838 {
839   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
840
841   return FALSE;
842 }
843
844 guint     
845 gdk_display_get_default_cursor_size (GdkDisplay    *display)
846 {
847   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
848   
849   /* no idea, really */
850   return 20; 
851 }
852
853 #endif
854
855
856 /**
857  * gdk_display_get_maximal_cursor_size:
858  * @display: a #GdkDisplay
859  * @width: the return location for the maximal cursor width
860  * @height: the return location for the maximal cursor height
861  *
862  * Gets the maximal size to use for cursors on @display.
863  *
864  * Since: 2.4
865  */
866 void     
867 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
868                                      guint       *width,
869                                      guint       *height)
870 {
871   GdkScreen *screen;
872   GdkWindow *window;
873
874   g_return_if_fail (GDK_IS_DISPLAY (display));
875   
876   screen = gdk_display_get_default_screen (display);
877   window = gdk_screen_get_root_window (screen);
878   XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display), 
879                     GDK_WINDOW_XWINDOW (window), 
880                     128, 128, width, height);
881 }
882
883 #define __GDK_CURSOR_X11_C__
884 #include "gdkaliasdef.c"