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