]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
45807cb6136ceb6051577fc62afb7c92448634fd
[~andy/gtk] / gdk / x11 / gdkpixmap-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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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-1999.  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 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 /* Needed for SEEK_END in SunOS */
32 #include <unistd.h>
33 #include <X11/Xlib.h>
34
35 #include "gdkpixmap.h"
36 #include "gdkprivate.h"
37 #include "gdkx.h"
38
39 typedef struct
40 {
41   gchar *color_string;
42   GdkColor color;
43   gint transparent;
44 } _GdkPixmapColor;
45
46 typedef struct
47 {
48   guint ncolors;
49   GdkColormap *colormap;
50   gulong pixels[1];
51 } _GdkPixmapInfo;
52
53 GdkDrawableClass _gdk_x11_pixmap_class;
54
55 static void
56 gdk_x11_pixmap_destroy (GdkPixmap *pixmap)
57 {
58   XFreePixmap (GDK_DRAWABLE_XDISPLAY (pixmap), GDK_DRAWABLE_XID (pixmap));
59   gdk_xid_table_remove (GDK_DRAWABLE_XID (pixmap));
60
61   g_free (GDK_DRAWABLE_XDATA (pixmap));
62 }
63
64 static GdkDrawable *
65 gdk_x11_pixmap_alloc (void)
66 {
67   GdkDrawable *drawable;
68   GdkDrawablePrivate *private;
69   
70   static GdkDrawableClass klass;
71   static gboolean initialized = FALSE;
72
73   if (!initialized)
74     {
75       initialized = TRUE;
76       
77       klass = _gdk_x11_drawable_class;
78       klass.destroy = gdk_x11_pixmap_destroy;
79     }
80
81   drawable = gdk_drawable_alloc ();
82   private = (GdkDrawablePrivate *)drawable;
83
84   private->klass = &klass;
85   private->klass_data = g_new (GdkDrawableXData, 1);
86   private->window_type = GDK_DRAWABLE_PIXMAP;
87
88   return drawable;
89 }
90
91 GdkPixmap*
92 gdk_pixmap_new (GdkWindow *window,
93                 gint       width,
94                 gint       height,
95                 gint       depth)
96 {
97   GdkPixmap *pixmap;
98   GdkDrawablePrivate *private;
99
100   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
101   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
102   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
103   
104   if (!window)
105     window = gdk_parent_root;
106
107   if (GDK_DRAWABLE_DESTROYED (window))
108     return NULL;
109
110   if (depth == -1)
111     depth = gdk_drawable_get_visual (window)->depth;
112
113   pixmap = gdk_x11_pixmap_alloc ();
114   private = (GdkDrawablePrivate *)pixmap;
115
116   GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
117   GDK_DRAWABLE_XDATA (private)->xid = XCreatePixmap (GDK_DRAWABLE_XDISPLAY (pixmap),
118                                                      GDK_DRAWABLE_XID (window),
119                                                      width, height, depth);
120   private->width = width;
121   private->height = height;
122
123   gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
124
125   return pixmap;
126 }
127
128 GdkPixmap *
129 gdk_bitmap_create_from_data (GdkWindow   *window,
130                              const gchar *data,
131                              gint         width,
132                              gint         height)
133 {
134   GdkPixmap *pixmap;
135   GdkDrawablePrivate *private;
136
137   g_return_val_if_fail (data != NULL, NULL);
138   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
139   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
140
141   if (!window)
142     window = gdk_parent_root;
143
144   if (GDK_DRAWABLE_DESTROYED (window))
145     return NULL;
146
147   pixmap = gdk_x11_pixmap_alloc ();
148   private = (GdkDrawablePrivate *)pixmap;
149
150   private->width = width;
151   private->height = height;
152
153   GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
154   GDK_DRAWABLE_XDATA (private)->xid = XCreateBitmapFromData (GDK_DRAWABLE_XDISPLAY (window),
155                                                              GDK_DRAWABLE_XID (window),
156                                                              (char *)data, width, height);
157
158   gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
159
160   return pixmap;
161 }
162
163 GdkPixmap*
164 gdk_pixmap_create_from_data (GdkWindow   *window,
165                              const gchar *data,
166                              gint         width,
167                              gint         height,
168                              gint         depth,
169                              GdkColor    *fg,
170                              GdkColor    *bg)
171 {
172   GdkPixmap *pixmap;
173   GdkDrawablePrivate *private;
174
175   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
176   g_return_val_if_fail (data != NULL, NULL);
177   g_return_val_if_fail (fg != NULL, NULL);
178   g_return_val_if_fail (bg != NULL, NULL);
179   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
180   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
181
182   if (!window)
183     window = gdk_parent_root;
184
185   if (GDK_DRAWABLE_DESTROYED (window))
186     return NULL;
187
188   if (depth == -1)
189     depth = gdk_drawable_get_visual (window)->depth;
190
191   pixmap = gdk_x11_pixmap_alloc ();
192   private = (GdkDrawablePrivate *)pixmap;
193
194   private->width = width;
195   private->height = height;
196
197   GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
198   GDK_DRAWABLE_XDATA (private)->xid = XCreatePixmapFromBitmapData (GDK_DRAWABLE_XDISPLAY (window),
199                                                                    GDK_DRAWABLE_XID (window),
200                                                                    (char *)data, width, height,
201                                                                    fg->pixel, bg->pixel, depth);
202
203   gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
204
205   return pixmap;
206 }
207
208 static gint
209 gdk_pixmap_seek_string (FILE  *infile,
210                         const gchar *str,
211                         gint   skip_comments)
212 {
213   char instr[1024];
214
215   while (1)
216     {
217       if (fscanf (infile, "%1023s", instr) != 1)
218         return FALSE;
219           
220       if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
221         {
222           do
223             {
224               if (fscanf (infile, "%1023s", instr) != 1)
225                 return FALSE;
226             }
227           while (strcmp (instr, "*/") != 0);
228         }
229       else if (strcmp (instr, str) == 0)
230         return TRUE;
231     }
232 }
233
234 static gint
235 gdk_pixmap_seek_char (FILE  *infile,
236                       gchar  c)
237 {
238   gint b, oldb;
239
240   while ((b = getc(infile)) != EOF)
241     {
242       if (c != b && b == '/')
243         {
244           b = getc (infile);
245           if (b == EOF)
246             return FALSE;
247           else if (b == '*')    /* we have a comment */
248             {
249               b = -1;
250               do
251                 {
252                   oldb = b;
253                   b = getc (infile);
254                   if (b == EOF)
255                     return FALSE;
256                 }
257               while (!(oldb == '*' && b == '/'));
258             }
259         }
260       else if (c == b)
261         return TRUE;
262     }
263   return FALSE;
264 }
265
266 static gint
267 gdk_pixmap_read_string (FILE  *infile,
268                         gchar **buffer,
269                         guint *buffer_size)
270 {
271   gint c;
272   guint cnt = 0, bufsiz, ret = FALSE;
273   gchar *buf;
274
275   buf = *buffer;
276   bufsiz = *buffer_size;
277   if (buf == NULL)
278     {
279       bufsiz = 10 * sizeof (gchar);
280       buf = g_new(gchar, bufsiz);
281     }
282
283   do
284     c = getc (infile);
285   while (c != EOF && c != '"');
286
287   if (c != '"')
288     goto out;
289
290   while ((c = getc(infile)) != EOF)
291     {
292       if (cnt == bufsiz)
293         {
294           guint new_size = bufsiz * 2;
295           if (new_size > bufsiz)
296             bufsiz = new_size;
297           else
298             goto out;
299           
300           buf = (gchar *) g_realloc (buf, bufsiz);
301           buf[bufsiz-1] = '\0';
302         }
303
304       if (c != '"')
305         buf[cnt++] = c;
306       else
307         {
308           buf[cnt] = 0;
309           ret = TRUE;
310           break;
311         }
312     }
313
314  out:
315   buf[bufsiz-1] = '\0';         /* ensure null termination for errors */
316   *buffer = buf;
317   *buffer_size = bufsiz;
318   return ret;
319 }
320
321 static gchar*
322 gdk_pixmap_skip_whitespaces (gchar *buffer)
323 {
324   gint32 index = 0;
325
326   while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
327     index++;
328
329   return &buffer[index];
330 }
331
332 static gchar*
333 gdk_pixmap_skip_string (gchar *buffer)
334 {
335   gint32 index = 0;
336
337   while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
338     index++;
339
340   return &buffer[index];
341 }
342
343 /* Xlib crashed ince at a color name lengths around 125 */
344 #define MAX_COLOR_LEN 120
345
346 static gchar*
347 gdk_pixmap_extract_color (gchar *buffer)
348 {
349   gint counter, numnames;
350   gchar *ptr = NULL, ch, temp[128];
351   gchar color[MAX_COLOR_LEN], *retcol;
352   gint space;
353
354   counter = 0;
355   while (ptr == NULL)
356     {
357       if (buffer[counter] == 'c')
358         {
359           ch = buffer[counter + 1];
360           if (ch == 0x20 || ch == 0x09)
361             ptr = &buffer[counter + 1];
362         }
363       else if (buffer[counter] == 0)
364         return NULL;
365
366       counter++;
367     }
368
369   ptr = gdk_pixmap_skip_whitespaces (ptr);
370
371   if (ptr[0] == 0)
372     return NULL;
373   else if (ptr[0] == '#')
374     {
375       counter = 1;
376       while (ptr[counter] != 0 && 
377              ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
378               (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
379               (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
380         counter++;
381
382       retcol = g_new (gchar, counter+1);
383       strncpy (retcol, ptr, counter);
384
385       retcol[counter] = 0;
386       
387       return retcol;
388     }
389
390   color[0] = 0;
391   numnames = 0;
392
393   space = MAX_COLOR_LEN - 1;
394   while (space > 0)
395     {
396       sscanf (ptr, "%127s", temp);
397
398       if (((gint)ptr[0] == 0) ||
399           (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
400           (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
401         {
402           break;
403         }
404       else
405         {
406           if (numnames > 0)
407             {
408               space -= 1;
409               strcat (color, " ");
410             }
411           strncat (color, temp, space);
412           space -= MIN (space, strlen (temp));
413           ptr = gdk_pixmap_skip_string (ptr);
414           ptr = gdk_pixmap_skip_whitespaces (ptr);
415           numnames++;
416         }
417     }
418
419   retcol = g_strdup (color);
420   return retcol;
421 }
422
423
424 enum buffer_op
425 {
426   op_header,
427   op_cmap,
428   op_body
429 };
430   
431
432 static void 
433 gdk_xpm_destroy_notify (gpointer data)
434 {
435   _GdkPixmapInfo *info = (_GdkPixmapInfo *)data;
436   GdkColor color;
437   int i;
438
439   for (i=0; i<info->ncolors; i++)
440     {
441       color.pixel = info->pixels[i];
442       gdk_colormap_free_colors (info->colormap, &color, 1);
443     }
444
445   gdk_colormap_unref (info->colormap);
446   g_free (info);
447 }
448   
449 static GdkPixmap *
450 _gdk_pixmap_create_from_xpm (GdkWindow  *window,
451                              GdkColormap *colormap,
452                              GdkBitmap **mask,
453                              GdkColor   *transparent_color,
454                              gchar *   (*get_buf) (enum buffer_op op,
455                                                    gpointer       handle),
456                              gpointer    handle)
457 {
458   GdkPixmap *pixmap = NULL;
459   GdkImage *image = NULL;
460   GdkVisual *visual;
461   GdkGC *gc = NULL;
462   GdkColor tmp_color;
463   gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes;
464   gchar *buffer, pixel_str[32];
465   gchar *name_buf;
466   _GdkPixmapColor *color = NULL, *fallbackcolor = NULL;
467   _GdkPixmapColor *colors = NULL;
468   gulong index;
469   GHashTable *color_hash = NULL;
470   _GdkPixmapInfo *color_info = NULL;
471   
472   if ((window == NULL) && (colormap == NULL))
473     g_warning ("Creating pixmap from xpm with NULL window and colormap");
474   
475   if (window == NULL)
476     window = gdk_parent_root;
477   
478   if (colormap == NULL)
479     {
480       colormap = gdk_drawable_get_colormap (window);
481       visual = gdk_drawable_get_visual (window);
482     }
483   else
484     visual = ((GdkColormapPrivate *)colormap)->visual;
485   
486   buffer = (*get_buf) (op_header, handle);
487   if (buffer == NULL)
488     return NULL;
489   
490   sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
491   if (cpp >= 32)
492     {
493       g_warning ("Pixmap has more than 31 characters per color\n");
494       return NULL;
495     }
496   
497   color_hash = g_hash_table_new (g_str_hash, g_str_equal);
498   
499   if (transparent_color == NULL)
500     {
501       gdk_color_white (colormap, &tmp_color);
502       transparent_color = &tmp_color;
503     }
504
505   /* For pseudo-color and grayscale visuals, we have to remember
506    * the colors we allocated, so we can free them later.
507    */
508   if ((visual->type == GDK_VISUAL_PSEUDO_COLOR) ||
509       (visual->type == GDK_VISUAL_GRAYSCALE))
510     {
511       color_info = g_malloc (sizeof (_GdkPixmapInfo) + 
512                              sizeof(gulong) * (num_cols - 1));
513       color_info->ncolors = num_cols;
514       color_info->colormap = colormap;
515       gdk_colormap_ref (colormap);
516     }
517
518   name_buf = g_new (gchar, num_cols * (cpp+1));
519   colors = g_new (_GdkPixmapColor, num_cols);
520
521   for (cnt = 0; cnt < num_cols; cnt++)
522     {
523       gchar *color_name;
524       
525       buffer = (*get_buf) (op_cmap, handle);
526       if (buffer == NULL)
527         goto error;
528       
529       color = &colors[cnt];
530       color->color_string = &name_buf [cnt * (cpp + 1)];
531       strncpy (color->color_string, buffer, cpp);
532       color->color_string[cpp] = 0;
533       buffer += strlen (color->color_string);
534       color->transparent = FALSE;
535       
536       color_name = gdk_pixmap_extract_color (buffer);
537       
538       if (color_name == NULL || g_strcasecmp (color_name, "None") == 0 ||
539           gdk_color_parse (color_name, &color->color) == FALSE)
540         {
541           color->color = *transparent_color;
542           color->transparent = TRUE;
543         }
544       
545       g_free (color_name);
546       
547       /* FIXME: The remaining slowness appears to happen in this
548          function. */
549       gdk_color_alloc (colormap, &color->color);
550
551       if (color_info)
552         color_info->pixels[cnt] = color->color.pixel;
553       
554       g_hash_table_insert (color_hash, color->color_string, color);
555       if (cnt == 0)
556         fallbackcolor = color;
557     }
558   
559   index = 0;
560   image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
561   
562   if (mask)
563     {
564       /* The pixmap mask is just a bits pattern.
565        * Color 0 is used for background and 1 for foreground.
566        * We don't care about the colormap, we just need 0 and 1.
567        */
568       GdkColor mask_pattern;
569       
570       *mask = gdk_pixmap_new (window, width, height, 1);
571       gc = gdk_gc_new (*mask);
572       
573       mask_pattern.pixel = 0;
574       gdk_gc_set_foreground (gc, &mask_pattern);
575       gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
576       
577       mask_pattern.pixel = 1;
578       gdk_gc_set_foreground (gc, &mask_pattern);
579     }
580   
581   wbytes = width * cpp;
582   for (ycnt = 0; ycnt < height; ycnt++)
583     {
584       buffer = (*get_buf) (op_body, handle);
585       
586       /* FIXME: this slows things down a little - it could be
587        * integrated into the strncpy below, perhaps. OTOH, strlen
588        * is fast.
589        */
590       if ((buffer == NULL) || strlen (buffer) < wbytes)
591         continue;
592       
593       for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
594         {
595           strncpy (pixel_str, &buffer[n], cpp);
596           pixel_str[cpp] = 0;
597           ns = 0;
598           
599           color = g_hash_table_lookup (color_hash, pixel_str);
600           
601           if (!color) /* screwed up XPM file */
602             color = fallbackcolor;
603           
604           gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
605           
606           if (mask && color->transparent)
607             {
608               if (cnt < xcnt)
609                 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
610               cnt = xcnt + 1;
611             }
612         }
613       
614       if (mask && (cnt < xcnt))
615         gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
616     }
617   
618  error:
619   
620   if (mask)
621     gdk_gc_unref (gc);
622   
623   if (image != NULL)
624     {
625       pixmap = gdk_pixmap_new (window, width, height, visual->depth);
626
627       if (color_info)
628         gdk_drawable_set_data (pixmap, "gdk-xpm", color_info, 
629                                gdk_xpm_destroy_notify);
630       
631       gc = gdk_gc_new (pixmap);
632       gdk_gc_set_foreground (gc, transparent_color);
633       gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
634       gdk_gc_unref (gc);
635       gdk_image_unref (image);
636     }
637   else if (color_info)
638     gdk_xpm_destroy_notify (color_info);
639   
640   if (color_hash != NULL)
641     g_hash_table_destroy (color_hash);
642
643   if (colors != NULL)
644     g_free (colors);
645
646   if (name_buf != NULL)
647     g_free (name_buf);
648
649   return pixmap;
650 }
651
652
653 struct file_handle
654 {
655   FILE *infile;
656   gchar *buffer;
657   guint buffer_size;
658 };
659
660
661 static gchar *
662 file_buffer (enum buffer_op op, gpointer handle)
663 {
664   struct file_handle *h = handle;
665
666   switch (op)
667     {
668     case op_header:
669       if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE)
670         break;
671
672       if (gdk_pixmap_seek_char (h->infile,'{') != TRUE)
673         break;
674       /* Fall through to the next gdk_pixmap_seek_char. */
675
676     case op_cmap:
677       gdk_pixmap_seek_char (h->infile, '"');
678       fseek (h->infile, -1, SEEK_CUR);
679       /* Fall through to the gdk_pixmap_read_string. */
680
681     case op_body:
682       gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size);
683       return h->buffer;
684     }
685   return 0;
686 }
687
688
689 GdkPixmap*
690 gdk_pixmap_colormap_create_from_xpm (GdkWindow   *window,
691                                      GdkColormap *colormap,
692                                      GdkBitmap  **mask,
693                                      GdkColor    *transparent_color,
694                                      const gchar *filename)
695 {
696   struct file_handle h;
697   GdkPixmap *pixmap = NULL;
698
699   memset (&h, 0, sizeof (h));
700   h.infile = fopen (filename, "rb");
701   if (h.infile != NULL)
702     {
703       pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
704                                             transparent_color,
705                                             file_buffer, &h);
706       fclose (h.infile);
707       g_free (h.buffer);
708     }
709
710   return pixmap;
711 }
712
713 GdkPixmap*
714 gdk_pixmap_create_from_xpm (GdkWindow  *window,
715                             GdkBitmap **mask,
716                             GdkColor   *transparent_color,
717                             const gchar *filename)
718 {
719   return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
720                                        transparent_color, filename);
721 }
722
723
724 struct mem_handle
725 {
726   gchar **data;
727   int offset;
728 };
729
730
731 static gchar *
732 mem_buffer (enum buffer_op op, gpointer handle)
733 {
734   struct mem_handle *h = handle;
735   switch (op)
736     {
737     case op_header:
738     case op_cmap:
739     case op_body:
740       if (h->data[h->offset])
741         return h->data[h->offset ++];
742     }
743   return 0;
744 }
745
746
747 GdkPixmap*
748 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow  *window,
749                                        GdkColormap *colormap,
750                                        GdkBitmap **mask,
751                                        GdkColor   *transparent_color,
752                                        gchar     **data)
753 {
754   struct mem_handle h;
755   GdkPixmap *pixmap = NULL;
756
757   memset (&h, 0, sizeof (h));
758   h.data = data;
759   pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
760                                         transparent_color,
761                                         mem_buffer, &h);
762   return pixmap;
763 }
764
765
766 GdkPixmap*
767 gdk_pixmap_create_from_xpm_d (GdkWindow  *window,
768                               GdkBitmap **mask,
769                               GdkColor   *transparent_color,
770                               gchar     **data)
771 {
772   return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
773                                                 transparent_color, data);
774 }
775
776 GdkPixmap*
777 gdk_pixmap_foreign_new (guint32 anid)
778 {
779   GdkPixmap *pixmap;
780   GdkDrawablePrivate *private;
781   Pixmap xpixmap;
782   Window root_return;
783   unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
784
785   /* check to make sure we were passed something at
786      least a little sane */
787   g_return_val_if_fail((anid != 0), NULL);
788   
789   /* set the pixmap to the passed in value */
790   xpixmap = anid;
791
792   /* get information about the Pixmap to fill in the structure for
793      the gdk window */
794   if (!XGetGeometry(GDK_DISPLAY(),
795                     xpixmap, &root_return,
796                     &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
797       return NULL;
798       
799   pixmap = gdk_x11_pixmap_alloc ();
800   private = (GdkDrawablePrivate *)pixmap;
801
802   GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DISPLAY ();
803   GDK_DRAWABLE_XDATA (private)->xid = xpixmap;
804
805   private->width = w_ret;
806   private->height = h_ret;
807   
808   gdk_xid_table_insert(&GDK_DRAWABLE_XID (pixmap), pixmap);
809
810   return pixmap;
811 }