]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
Move all X specific code into the x11/ directory. Aside from shuffling
[~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 (!feof (infile))
216     {
217       fscanf (infile, "%1023s", instr);
218       if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
219         {
220           fscanf (infile, "%1023s", instr);
221           while (!feof (infile) && strcmp (instr, "*/") != 0)
222             fscanf (infile, "%1023s", instr);
223           fscanf(infile, "%1023s", instr);
224         }
225       if (strcmp (instr, str)==0)
226         return TRUE;
227     }
228
229   return FALSE;
230 }
231
232 static gint
233 gdk_pixmap_seek_char (FILE  *infile,
234                       gchar  c)
235 {
236   gint b, oldb;
237
238   while ((b = getc(infile)) != EOF)
239     {
240       if (c != b && b == '/')
241         {
242           b = getc (infile);
243           if (b == EOF)
244             return FALSE;
245           else if (b == '*')    /* we have a comment */
246             {
247               b = -1;
248               do
249                 {
250                   oldb = b;
251                   b = getc (infile);
252                   if (b == EOF)
253                     return FALSE;
254                 }
255               while (!(oldb == '*' && b == '/'));
256             }
257         }
258       else if (c == b)
259         return TRUE;
260     }
261   return FALSE;
262 }
263
264 static gint
265 gdk_pixmap_read_string (FILE  *infile,
266                         gchar **buffer,
267                         guint *buffer_size)
268 {
269   gint c;
270   guint cnt = 0, bufsiz, ret = FALSE;
271   gchar *buf;
272
273   buf = *buffer;
274   bufsiz = *buffer_size;
275   if (buf == NULL)
276     {
277       bufsiz = 10 * sizeof (gchar);
278       buf = g_new(gchar, bufsiz);
279     }
280
281   do
282     c = getc (infile);
283   while (c != EOF && c != '"');
284
285   if (c != '"')
286     goto out;
287
288   while ((c = getc(infile)) != EOF)
289     {
290       if (cnt == bufsiz)
291         {
292           guint new_size = bufsiz * 2;
293           if (new_size > bufsiz)
294             bufsiz = new_size;
295           else
296             goto out;
297           
298           buf = (gchar *) g_realloc (buf, bufsiz);
299           buf[bufsiz-1] = '\0';
300         }
301
302       if (c != '"')
303         buf[cnt++] = c;
304       else
305         {
306           buf[cnt] = 0;
307           ret = TRUE;
308           break;
309         }
310     }
311
312  out:
313   buf[bufsiz-1] = '\0';         /* ensure null termination for errors */
314   *buffer = buf;
315   *buffer_size = bufsiz;
316   return ret;
317 }
318
319 static gchar*
320 gdk_pixmap_skip_whitespaces (gchar *buffer)
321 {
322   gint32 index = 0;
323
324   while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
325     index++;
326
327   return &buffer[index];
328 }
329
330 static gchar*
331 gdk_pixmap_skip_string (gchar *buffer)
332 {
333   gint32 index = 0;
334
335   while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
336     index++;
337
338   return &buffer[index];
339 }
340
341 /* Xlib crashed ince at a color name lengths around 125 */
342 #define MAX_COLOR_LEN 120
343
344 static gchar*
345 gdk_pixmap_extract_color (gchar *buffer)
346 {
347   gint counter, numnames;
348   gchar *ptr = NULL, ch, temp[128];
349   gchar color[MAX_COLOR_LEN], *retcol;
350   gint space;
351
352   counter = 0;
353   while (ptr == NULL)
354     {
355       if (buffer[counter] == 'c')
356         {
357           ch = buffer[counter + 1];
358           if (ch == 0x20 || ch == 0x09)
359             ptr = &buffer[counter + 1];
360         }
361       else if (buffer[counter] == 0)
362         return NULL;
363
364       counter++;
365     }
366
367   ptr = gdk_pixmap_skip_whitespaces (ptr);
368
369   if (ptr[0] == 0)
370     return NULL;
371   else if (ptr[0] == '#')
372     {
373       counter = 1;
374       while (ptr[counter] != 0 && 
375              ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
376               (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
377               (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
378         counter++;
379
380       retcol = g_new (gchar, counter+1);
381       strncpy (retcol, ptr, counter);
382
383       retcol[counter] = 0;
384       
385       return retcol;
386     }
387
388   color[0] = 0;
389   numnames = 0;
390
391   space = MAX_COLOR_LEN - 1;
392   while (space > 0)
393     {
394       sscanf (ptr, "%127s", temp);
395
396       if (((gint)ptr[0] == 0) ||
397           (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
398           (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
399         {
400           break;
401         }
402       else
403         {
404           if (numnames > 0)
405             {
406               space -= 1;
407               strcat (color, " ");
408             }
409           strncat (color, temp, space);
410           space -= MIN (space, strlen (temp));
411           ptr = gdk_pixmap_skip_string (ptr);
412           ptr = gdk_pixmap_skip_whitespaces (ptr);
413           numnames++;
414         }
415     }
416
417   retcol = g_strdup (color);
418   return retcol;
419 }
420
421
422 enum buffer_op
423 {
424   op_header,
425   op_cmap,
426   op_body
427 };
428   
429
430 static void 
431 gdk_xpm_destroy_notify (gpointer data)
432 {
433   _GdkPixmapInfo *info = (_GdkPixmapInfo *)data;
434   GdkColor color;
435   int i;
436
437   for (i=0; i<info->ncolors; i++)
438     {
439       color.pixel = info->pixels[i];
440       gdk_colormap_free_colors (info->colormap, &color, 1);
441     }
442
443   gdk_colormap_unref (info->colormap);
444   g_free (info);
445 }
446   
447 static GdkPixmap *
448 _gdk_pixmap_create_from_xpm (GdkWindow  *window,
449                              GdkColormap *colormap,
450                              GdkBitmap **mask,
451                              GdkColor   *transparent_color,
452                              gchar *   (*get_buf) (enum buffer_op op,
453                                                    gpointer       handle),
454                              gpointer    handle)
455 {
456   GdkPixmap *pixmap = NULL;
457   GdkImage *image = NULL;
458   GdkVisual *visual;
459   GdkGC *gc = NULL;
460   GdkColor tmp_color;
461   gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes;
462   gchar *buffer, pixel_str[32];
463   gchar *name_buf;
464   _GdkPixmapColor *color = NULL, *fallbackcolor = NULL;
465   _GdkPixmapColor *colors = NULL;
466   gulong index;
467   GHashTable *color_hash = NULL;
468   _GdkPixmapInfo *color_info = NULL;
469   
470   if ((window == NULL) && (colormap == NULL))
471     g_warning ("Creating pixmap from xpm with NULL window and colormap");
472   
473   if (window == NULL)
474     window = gdk_parent_root;
475   
476   if (colormap == NULL)
477     {
478       colormap = gdk_drawable_get_colormap (window);
479       visual = gdk_drawable_get_visual (window);
480     }
481   else
482     visual = ((GdkColormapPrivate *)colormap)->visual;
483   
484   buffer = (*get_buf) (op_header, handle);
485   if (buffer == NULL)
486     return NULL;
487   
488   sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
489   if (cpp >= 32)
490     {
491       g_warning ("Pixmap has more than 31 characters per color\n");
492       return NULL;
493     }
494   
495   color_hash = g_hash_table_new (g_str_hash, g_str_equal);
496   
497   if (transparent_color == NULL)
498     {
499       gdk_color_white (colormap, &tmp_color);
500       transparent_color = &tmp_color;
501     }
502
503   /* For pseudo-color and grayscale visuals, we have to remember
504    * the colors we allocated, so we can free them later.
505    */
506   if ((visual->type == GDK_VISUAL_PSEUDO_COLOR) ||
507       (visual->type == GDK_VISUAL_GRAYSCALE))
508     {
509       color_info = g_malloc (sizeof (_GdkPixmapInfo) + 
510                              sizeof(gulong) * (num_cols - 1));
511       color_info->ncolors = num_cols;
512       color_info->colormap = colormap;
513       gdk_colormap_ref (colormap);
514     }
515
516   name_buf = g_new (gchar, num_cols * (cpp+1));
517   colors = g_new (_GdkPixmapColor, num_cols);
518
519   for (cnt = 0; cnt < num_cols; cnt++)
520     {
521       gchar *color_name;
522       
523       buffer = (*get_buf) (op_cmap, handle);
524       if (buffer == NULL)
525         goto error;
526       
527       color = &colors[cnt];
528       color->color_string = &name_buf [cnt * (cpp + 1)];
529       strncpy (color->color_string, buffer, cpp);
530       color->color_string[cpp] = 0;
531       buffer += strlen (color->color_string);
532       color->transparent = FALSE;
533       
534       color_name = gdk_pixmap_extract_color (buffer);
535       
536       if (color_name == NULL || g_strcasecmp (color_name, "None") == 0 ||
537           gdk_color_parse (color_name, &color->color) == FALSE)
538         {
539           color->color = *transparent_color;
540           color->transparent = TRUE;
541         }
542       
543       g_free (color_name);
544       
545       /* FIXME: The remaining slowness appears to happen in this
546          function. */
547       gdk_color_alloc (colormap, &color->color);
548
549       if (color_info)
550         color_info->pixels[cnt] = color->color.pixel;
551       
552       g_hash_table_insert (color_hash, color->color_string, color);
553       if (cnt == 0)
554         fallbackcolor = color;
555     }
556   
557   index = 0;
558   image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
559   
560   if (mask)
561     {
562       /* The pixmap mask is just a bits pattern.
563        * Color 0 is used for background and 1 for foreground.
564        * We don't care about the colormap, we just need 0 and 1.
565        */
566       GdkColor mask_pattern;
567       
568       *mask = gdk_pixmap_new (window, width, height, 1);
569       gc = gdk_gc_new (*mask);
570       
571       mask_pattern.pixel = 0;
572       gdk_gc_set_foreground (gc, &mask_pattern);
573       gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
574       
575       mask_pattern.pixel = 1;
576       gdk_gc_set_foreground (gc, &mask_pattern);
577     }
578   
579   wbytes = width * cpp;
580   for (ycnt = 0; ycnt < height; ycnt++)
581     {
582       buffer = (*get_buf) (op_body, handle);
583       
584       /* FIXME: this slows things down a little - it could be
585        * integrated into the strncpy below, perhaps. OTOH, strlen
586        * is fast.
587        */
588       if ((buffer == NULL) || strlen (buffer) < wbytes)
589         continue;
590       
591       for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
592         {
593           strncpy (pixel_str, &buffer[n], cpp);
594           pixel_str[cpp] = 0;
595           ns = 0;
596           
597           color = g_hash_table_lookup (color_hash, pixel_str);
598           
599           if (!color) /* screwed up XPM file */
600             color = fallbackcolor;
601           
602           gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
603           
604           if (mask && color->transparent)
605             {
606               if (cnt < xcnt)
607                 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
608               cnt = xcnt + 1;
609             }
610         }
611       
612       if (mask && (cnt < xcnt))
613         gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
614     }
615   
616  error:
617   
618   if (mask)
619     gdk_gc_unref (gc);
620   
621   if (image != NULL)
622     {
623       pixmap = gdk_pixmap_new (window, width, height, visual->depth);
624
625       if (color_info)
626         gdk_drawable_set_data (pixmap, "gdk-xpm", color_info, 
627                                gdk_xpm_destroy_notify);
628       
629       gc = gdk_gc_new (pixmap);
630       gdk_gc_set_foreground (gc, transparent_color);
631       gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
632       gdk_gc_unref (gc);
633       gdk_image_unref (image);
634     }
635   else if (color_info)
636     gdk_xpm_destroy_notify (color_info);
637   
638   if (color_hash != NULL)
639     g_hash_table_destroy (color_hash);
640
641   if (colors != NULL)
642     g_free (colors);
643
644   if (name_buf != NULL)
645     g_free (name_buf);
646
647   return pixmap;
648 }
649
650
651 struct file_handle
652 {
653   FILE *infile;
654   gchar *buffer;
655   guint buffer_size;
656 };
657
658
659 static gchar *
660 file_buffer (enum buffer_op op, gpointer handle)
661 {
662   struct file_handle *h = handle;
663
664   switch (op)
665     {
666     case op_header:
667       if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE)
668         break;
669
670       if (gdk_pixmap_seek_char (h->infile,'{') != TRUE)
671         break;
672       /* Fall through to the next gdk_pixmap_seek_char. */
673
674     case op_cmap:
675       gdk_pixmap_seek_char (h->infile, '"');
676       fseek (h->infile, -1, SEEK_CUR);
677       /* Fall through to the gdk_pixmap_read_string. */
678
679     case op_body:
680       gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size);
681       return h->buffer;
682     }
683   return 0;
684 }
685
686
687 GdkPixmap*
688 gdk_pixmap_colormap_create_from_xpm (GdkWindow   *window,
689                                      GdkColormap *colormap,
690                                      GdkBitmap  **mask,
691                                      GdkColor    *transparent_color,
692                                      const gchar *filename)
693 {
694   struct file_handle h;
695   GdkPixmap *pixmap = NULL;
696
697   memset (&h, 0, sizeof (h));
698   h.infile = fopen (filename, "rb");
699   if (h.infile != NULL)
700     {
701       pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
702                                             transparent_color,
703                                             file_buffer, &h);
704       fclose (h.infile);
705       g_free (h.buffer);
706     }
707
708   return pixmap;
709 }
710
711 GdkPixmap*
712 gdk_pixmap_create_from_xpm (GdkWindow  *window,
713                             GdkBitmap **mask,
714                             GdkColor   *transparent_color,
715                             const gchar *filename)
716 {
717   return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
718                                        transparent_color, filename);
719 }
720
721
722 struct mem_handle
723 {
724   gchar **data;
725   int offset;
726 };
727
728
729 static gchar *
730 mem_buffer (enum buffer_op op, gpointer handle)
731 {
732   struct mem_handle *h = handle;
733   switch (op)
734     {
735     case op_header:
736     case op_cmap:
737     case op_body:
738       if (h->data[h->offset])
739         return h->data[h->offset ++];
740     }
741   return 0;
742 }
743
744
745 GdkPixmap*
746 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow  *window,
747                                        GdkColormap *colormap,
748                                        GdkBitmap **mask,
749                                        GdkColor   *transparent_color,
750                                        gchar     **data)
751 {
752   struct mem_handle h;
753   GdkPixmap *pixmap = NULL;
754
755   memset (&h, 0, sizeof (h));
756   h.data = data;
757   pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
758                                         transparent_color,
759                                         mem_buffer, &h);
760   return pixmap;
761 }
762
763
764 GdkPixmap*
765 gdk_pixmap_create_from_xpm_d (GdkWindow  *window,
766                               GdkBitmap **mask,
767                               GdkColor   *transparent_color,
768                               gchar     **data)
769 {
770   return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
771                                                 transparent_color, data);
772 }
773
774 GdkPixmap*
775 gdk_pixmap_foreign_new (guint32 anid)
776 {
777   GdkPixmap *pixmap;
778   GdkDrawablePrivate *private;
779   Pixmap xpixmap;
780   Window root_return;
781   unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
782
783   /* check to make sure we were passed something at
784      least a little sane */
785   g_return_val_if_fail((anid != 0), NULL);
786   
787   /* set the pixmap to the passed in value */
788   xpixmap = anid;
789
790   /* get information about the Pixmap to fill in the structure for
791      the gdk window */
792   if (!XGetGeometry(GDK_DISPLAY(),
793                     xpixmap, &root_return,
794                     &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
795       return NULL;
796       
797   pixmap = gdk_x11_pixmap_alloc ();
798   private = (GdkDrawablePrivate *)pixmap;
799
800   GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DISPLAY ();
801   GDK_DRAWABLE_XDATA (private)->xid = xpixmap;
802
803   private->width = w_ret;
804   private->height = h_ret;
805   
806   gdk_xid_table_insert(&GDK_DRAWABLE_XID (pixmap), pixmap);
807
808   return pixmap;
809 }