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