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