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