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