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