]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcursor-x11.c
Handle theme == NULL.
[~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, or %NULL to unset
450  *         a previously set value
451  * @size: the cursor size to use
452  *
453  * Sets the cursor theme from which the images for cursor
454  * should be taken. 
455  * 
456  * If the windowing system supports it, existing cursors created 
457  * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
458  * gdk_cursor_new_for_name() are updated to reflect the theme 
459  * change. Custom cursors constructed with gdk_cursor_new_from_pixmap() 
460  * or gdk_cursor_new_from_pixbuf() will have to be handled
461  * by the application (GTK+ applications can learn about 
462  * cursor theme changes by listening for change notification
463  * for the corresponding #GtkSetting).
464  *
465  * Since: 2.8
466  */
467 void
468 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
469                                   const gchar *theme,
470                                   const gint   size)
471 {
472   GdkDisplayX11 *display_x11;
473   Display *xdisplay;
474   gchar *old_theme;
475   gint old_size;
476
477   g_return_if_fail (GDK_IS_DISPLAY (display));
478
479   display_x11 = GDK_DISPLAY_X11 (display);
480   xdisplay = GDK_DISPLAY_XDISPLAY (display);
481
482   old_theme = XcursorGetTheme (xdisplay);
483   old_size = XcursorGetDefaultSize (xdisplay);
484
485   if (old_size == size &&
486       (old_theme == theme ||
487        (old_theme && theme && strcmp (old_theme, theme) == 0)))
488     return;
489
490   theme_serial++;
491
492   XcursorSetTheme (xdisplay, theme);
493   XcursorSetDefaultSize (xdisplay, size);
494     
495   g_hash_table_foreach (display_x11->xid_ht, update_cursor, NULL);
496 }
497
498 #else
499
500 GdkPixbuf*  
501 gdk_cursor_get_image (GdkCursor *cursor)
502 {
503   g_return_val_if_fail (cursor != NULL, NULL);
504   
505   return NULL;
506 }
507
508 void
509 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
510                                   const gchar *theme,
511                                   const gint   size)
512 {
513   g_return_if_fail (GDK_IS_DISPLAY (display));
514 }
515
516 void
517 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
518 {
519   g_return_if_fail (cursor != NULL);
520 }
521
522 #endif
523
524 #ifdef HAVE_XCURSOR
525
526 static XcursorImage*
527 create_cursor_image (GdkPixbuf *pixbuf,
528                      gint       x,
529                      gint       y)
530 {
531   guint width, height, rowstride, n_channels;
532   guchar *pixels, *src;
533   XcursorImage *xcimage;
534   XcursorPixel *dest;
535
536   width = gdk_pixbuf_get_width (pixbuf);
537   height = gdk_pixbuf_get_height (pixbuf);
538
539   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
540   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
541   pixels = gdk_pixbuf_get_pixels (pixbuf);
542
543   xcimage = XcursorImageCreate (width, height);
544
545   xcimage->xhot = x;
546   xcimage->yhot = y;
547
548   dest = xcimage->pixels;
549
550   if (n_channels == 3)
551     {
552       gint i, j;
553
554       for (j = 0; j < height; j++)
555         {
556           src = pixels + j * rowstride;
557           for (i = 0; i < width; i++)
558             {
559               *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
560             }
561
562           src += n_channels;
563           dest++;
564         }
565     }
566   else
567     {
568       _gdk_x11_convert_to_format (pixels, rowstride,
569                                   (guchar *) dest, 4 * width,
570                                   GDK_X11_FORMAT_ARGB,
571                                   (G_BYTE_ORDER == G_BIG_ENDIAN) ?
572                                   GDK_MSB_FIRST : GDK_LSB_FIRST,
573                                   width, height);
574     }
575
576   return xcimage;
577 }
578
579
580 /**
581  * gdk_cursor_new_from_pixbuf:
582  * @display: the #GdkDisplay for which the cursor will be created
583  * @pixbuf: the #GdkPixbuf containing the cursor image
584  * @x: the horizontal offset of the 'hotspot' of the cursor. 
585  * @y: the vertical offset of the 'hotspot' of the cursor.
586  *
587  * Creates a new cursor from a pixbuf. 
588  *
589  * Not all GDK backends support RGBA cursors. If they are not 
590  * supported, a monochrome approximation will be displayed. 
591  * The functions gdk_display_supports_cursor_alpha() and 
592  * gdk_display_supports_cursor_color() can be used to determine
593  * whether RGBA cursors are supported; 
594  * gdk_display_get_default_cursor_size() and 
595  * gdk_display_get_maximal_cursor_size() give information about 
596  * cursor sizes.
597  *
598  * On the X backend, support for RGBA cursors requires a
599  * sufficently new version of the X Render extension. 
600  *
601  * Returns: a new #GdkCursor.
602  * 
603  * Since: 2.4
604  */
605 GdkCursor *
606 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
607                             GdkPixbuf  *pixbuf,
608                             gint        x,
609                             gint        y)
610 {
611   XcursorImage *xcimage;
612   Cursor xcursor;
613   GdkCursorPrivate *private;
614   GdkCursor *cursor;
615
616   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
617   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
618   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
619   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
620
621   if (display->closed)
622     xcursor = None;
623   else 
624     {
625       xcimage = create_cursor_image (pixbuf, x, y);
626       xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
627       XcursorImageDestroy (xcimage);
628     }
629
630   private = g_new (GdkCursorPrivate, 1);
631   private->display = display;
632   private->xcursor = xcursor;
633   private->name = NULL;
634   private->serial = theme_serial;
635
636   cursor = (GdkCursor *) private;
637   cursor->type = GDK_CURSOR_IS_PIXMAP;
638   cursor->ref_count = 1;
639   
640   return cursor;
641 }
642
643 /**
644  * gdk_cursor_new_from_name:
645  * @display: the #GdkDisplay for which the cursor will be created
646  * @name: the name of the cursor
647  *
648  * Creates a new cursor by looking up @name in the current cursor
649  * theme. 
650  * 
651  * Returns: a new #GdkCursor, or %NULL if there is no cursor with 
652  *   the given name 
653  *
654  * Since: 2.8
655  */
656 GdkCursor*  
657 gdk_cursor_new_from_name (GdkDisplay  *display,
658                           const gchar *name)
659 {
660   Cursor xcursor;
661   Display *xdisplay;
662   GdkCursorPrivate *private;
663   GdkCursor *cursor;
664
665   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
666
667   if (display->closed)
668     xcursor = None;
669   else 
670     {
671       xdisplay = GDK_DISPLAY_XDISPLAY (display);
672       xcursor = XcursorLibraryLoadCursor (xdisplay, name);
673       if (xcursor == None)
674         return NULL;
675     }
676
677   private = g_new (GdkCursorPrivate, 1);
678   private->display = display;
679   private->xcursor = xcursor;
680   private->name = g_strdup (name);
681   private->serial = theme_serial;
682
683   cursor = (GdkCursor *) private;
684   cursor->type = GDK_CURSOR_IS_PIXMAP;
685   cursor->ref_count = 1;
686   
687   return cursor;
688 }
689
690 /**
691  * gdk_display_supports_cursor_alpha:
692  * @display: a #GdkDisplay
693  *
694  * Returns %TRUE if cursors can use an 8bit alpha channel 
695  * on @display. Otherwise, cursors are restricted to bilevel 
696  * alpha (i.e. a mask).
697  *
698  * Returns: whether cursors can have alpha channels.
699  *
700  * Since: 2.4
701  */
702 gboolean 
703 gdk_display_supports_cursor_alpha (GdkDisplay *display)
704 {
705   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
706
707   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
708 }
709
710 /**
711  * gdk_display_supports_cursor_color:
712  * @display: a #GdkDisplay
713  *
714  * Returns %TRUE if multicolored cursors are supported
715  * on @display. Otherwise, cursors have only a forground
716  * and a background color.
717  *
718  * Returns: whether cursors can have multiple colors.
719  *
720  * Since: 2.4
721  */
722 gboolean 
723 gdk_display_supports_cursor_color (GdkDisplay *display)
724 {
725   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
726
727   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
728 }
729
730 /**
731  * gdk_display_get_default_cursor_size:
732  * @display: a #GdkDisplay
733  *
734  * Returns the default size to use for cursors on @display.
735  *
736  * Returns: the default cursor size.
737  *
738  * Since: 2.4
739  */
740 guint     
741 gdk_display_get_default_cursor_size (GdkDisplay *display)
742 {
743   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
744
745   return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
746 }
747
748 #else
749
750 GdkCursor *
751 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
752                             GdkPixbuf  *pixbuf,
753                             gint        x,
754                             gint        y)
755 {
756   GdkCursor *cursor;
757   GdkPixmap *pixmap, *mask;
758   guint width, height, n_channels, rowstride, i, j;
759   guint8 *data, *mask_data, *pixels;
760   GdkColor fg = { 0, 0, 0, 0 };
761   GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
762   GdkScreen *screen;
763
764   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
765   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
766
767   width = gdk_pixbuf_get_width (pixbuf);
768   height = gdk_pixbuf_get_height (pixbuf);
769
770   g_return_val_if_fail (0 <= x && x < width, NULL);
771   g_return_val_if_fail (0 <= y && y < height, NULL);
772
773   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
774   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
775   pixels = gdk_pixbuf_get_pixels (pixbuf);
776
777   data = g_new0 (guint8, (width + 7) / 8 * height);
778   mask_data = g_new0 (guint8, (width + 7) / 8 * height);
779
780   for (j = 0; j < height; j++)
781     {
782       guint8 *src = pixels + j * rowstride;
783       guint8 *d = data + (width + 7) / 8 * j;
784       guint8 *md = mask_data + (width + 7) / 8 * j;
785         
786       for (i = 0; i < width; i++)
787         {
788           if (src[1] < 0x80)
789             *d |= 1 << (i % 8);
790           
791           if (n_channels == 3 || src[3] >= 0x80)
792             *md |= 1 << (i % 8);
793           
794           src += n_channels;
795           if (i % 8 == 7)
796             {
797               d++; 
798               md++;
799             }
800         }
801     }
802       
803   screen = gdk_display_get_default_screen (display);
804   pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
805                                         data, width, height);
806  
807   mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
808                                       mask_data, width, height);
809    
810   cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
811    
812   g_object_unref (pixmap);
813   g_object_unref (mask);
814
815   g_free (data);
816   g_free (mask_data);
817   
818   return cursor;
819 }
820
821 GdkCursor*  
822 gdk_cursor_new_from_name (GdkDisplay  *display,
823                           const gchar *name)
824 {
825   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
826
827   return NULL;
828 }
829
830 gboolean 
831 gdk_display_supports_cursor_alpha (GdkDisplay    *display)
832 {
833   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
834
835   return FALSE;
836 }
837
838 gboolean 
839 gdk_display_supports_cursor_color (GdkDisplay    *display)
840 {
841   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
842
843   return FALSE;
844 }
845
846 guint     
847 gdk_display_get_default_cursor_size (GdkDisplay    *display)
848 {
849   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
850   
851   /* no idea, really */
852   return 20; 
853 }
854
855 #endif
856
857
858 /**
859  * gdk_display_get_maximal_cursor_size:
860  * @display: a #GdkDisplay
861  * @width: the return location for the maximal cursor width
862  * @height: the return location for the maximal cursor height
863  *
864  * Gets the maximal size to use for cursors on @display.
865  *
866  * Since: 2.4
867  */
868 void     
869 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
870                                      guint       *width,
871                                      guint       *height)
872 {
873   GdkScreen *screen;
874   GdkWindow *window;
875
876   g_return_if_fail (GDK_IS_DISPLAY (display));
877   
878   screen = gdk_display_get_default_screen (display);
879   window = gdk_screen_get_root_window (screen);
880   XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display), 
881                     GDK_WINDOW_XWINDOW (window), 
882                     128, 128, width, height);
883 }
884
885 #define __GDK_CURSOR_X11_C__
886 #include "gdkaliasdef.c"