1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include "../config.h"
22 /* Needed for SEEK_END in SunOS */
27 #include "gdkprivate.h"
37 gdk_pixmap_new (GdkWindow *window,
43 GdkWindowPrivate *private;
44 GdkWindowPrivate *window_private;
46 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
47 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
50 window = (GdkWindow*) &gdk_root_parent;
52 window_private = (GdkWindowPrivate*) window;
53 if (window_private->destroyed)
57 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
59 private = g_new (GdkWindowPrivate, 1);
60 pixmap = (GdkPixmap*) private;
62 private->xdisplay = window_private->xdisplay;
63 private->window_type = GDK_WINDOW_PIXMAP;
64 private->xwindow = XCreatePixmap (private->xdisplay, window_private->xwindow,
65 width, height, depth);
66 private->parent = NULL;
69 private->width = width;
70 private->height = height;
71 private->resize_count = 0;
72 private->ref_count = 1;
73 private->destroyed = 0;
75 gdk_xid_table_insert (&private->xwindow, pixmap);
81 gdk_bitmap_create_from_data (GdkWindow *window,
87 GdkWindowPrivate *private;
88 GdkWindowPrivate *window_private;
90 g_return_val_if_fail (data != NULL, NULL);
91 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
94 window = (GdkWindow*) &gdk_root_parent;
96 window_private = (GdkWindowPrivate*) window;
97 if (window_private->destroyed)
100 private = g_new (GdkWindowPrivate, 1);
101 pixmap = (GdkPixmap*) private;
103 private->parent = NULL;
104 private->xdisplay = window_private->xdisplay;
105 private->window_type = GDK_WINDOW_PIXMAP;
108 private->width = width;
109 private->height = height;
110 private->resize_count = 0;
111 private->ref_count = 1;
112 private->destroyed = FALSE;
114 private->xwindow = XCreateBitmapFromData (private->xdisplay,
115 window_private->xwindow,
116 data, width, height);
118 gdk_xid_table_insert (&private->xwindow, pixmap);
124 gdk_pixmap_create_from_data (GdkWindow *window,
133 GdkWindowPrivate *private;
134 GdkWindowPrivate *window_private;
136 g_return_val_if_fail (data != NULL, NULL);
137 g_return_val_if_fail (fg != NULL, NULL);
138 g_return_val_if_fail (bg != NULL, NULL);
139 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
140 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
143 window = (GdkWindow*) &gdk_root_parent;
145 window_private = (GdkWindowPrivate*) window;
146 if (window_private->destroyed)
150 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
152 private = g_new (GdkWindowPrivate, 1);
153 pixmap = (GdkPixmap*) private;
155 private->parent = NULL;
156 private->xdisplay = window_private->xdisplay;
157 private->window_type = GDK_WINDOW_PIXMAP;
160 private->width = width;
161 private->height = height;
162 private->resize_count = 0;
163 private->ref_count = 1;
164 private->destroyed = FALSE;
166 private->xwindow = XCreatePixmapFromBitmapData (private->xdisplay,
167 window_private->xwindow,
169 fg->pixel, bg->pixel, depth);
171 gdk_xid_table_insert (&private->xwindow, pixmap);
177 gdk_pixmap_seek_string (FILE *infile,
183 while (!feof (infile))
185 fscanf (infile, "%1023s", instr);
186 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
188 fscanf (infile, "%1023s", instr);
189 while (!feof (infile) && strcmp (instr, "*/") != 0)
190 fscanf (infile, "%1023s", instr);
191 fscanf(infile, "%1023s", instr);
193 if (strcmp (instr, str)==0)
201 gdk_pixmap_seek_char (FILE *infile,
206 while ((b = getc(infile)) != EOF)
208 if (c != b && b == '/')
213 else if (b == '*') /* we have a comment */
223 while (!(oldb == '*' && b == '/'));
233 gdk_pixmap_read_string (FILE *infile,
240 if ((*buffer) == NULL)
242 (*buffer_size) = 10 * sizeof (gchar);
243 (*buffer) = g_new(gchar, *buffer_size);
248 while (c != EOF && c != '"');
253 while ((c = getc(infile)) != EOF)
255 if (cnt == (*buffer_size))
258 (*buffer) = (gchar *) g_realloc ((*buffer), *buffer_size); }
261 (*buffer)[cnt++] = c;
264 (*buffer)[cnt++] = 0;
273 gdk_pixmap_skip_whitespaces (gchar *buffer)
277 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
280 return &buffer[index];
284 gdk_pixmap_skip_string (gchar *buffer)
288 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
291 return &buffer[index];
295 gdk_pixmap_extract_color (gchar *buffer)
297 gint counter, finished = FALSE, numnames;
298 gchar *ptr = NULL, ch, temp[128];
299 gchar color[128], *retcol;
304 if (buffer[counter] == 'c')
306 ch = buffer[counter + 1];
307 if (ch == 0x20 || ch == 0x09)
308 ptr = &buffer[counter + 1];
310 else if (buffer[counter] == 0)
319 ptr = gdk_pixmap_skip_whitespaces (ptr);
323 else if (ptr[0] == '#')
325 retcol = g_strdup (ptr);
332 while (finished == FALSE)
334 sscanf (ptr, "%127s", temp);
336 if ((gint)ptr[0] == 0 || strcmp ("s", temp) == 0 || strcmp ("m", temp) == 0 ||
337 strcmp ("g", temp) == 0 || strcmp ("g4", temp) == 0)
343 strcat (color, temp);
344 ptr = gdk_pixmap_skip_string (ptr);
345 ptr = gdk_pixmap_skip_whitespaces (ptr);
350 retcol = g_strdup (color);
356 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
357 GdkColormap *colormap,
359 GdkColor *transparent_color,
360 const gchar *filename)
363 GdkPixmap *pixmap = NULL;
364 GdkImage *image = NULL;
368 gint width, height, num_cols, cpp, cnt, n, ns, xcnt, ycnt;
369 gchar *buffer = NULL, pixel_str[32];
370 guint buffer_size = 0;
371 _GdkPixmapColor *colors = NULL, *color = NULL;
374 if ((window == NULL) && (colormap == NULL))
375 g_warning ("Creating pixmap from xpm with NULL window and colormap");
378 window = (GdkWindow *)&gdk_root_parent;
380 if (colormap == NULL)
382 colormap = gdk_window_get_colormap (window);
383 visual = gdk_window_get_visual (window);
386 visual = ((GdkColormapPrivate *)colormap)->visual;
388 infile = fopen (filename, "rb");
391 if (gdk_pixmap_seek_string (infile, "XPM", FALSE) == TRUE)
393 if (gdk_pixmap_seek_char (infile,'{') == TRUE)
395 gdk_pixmap_seek_char (infile, '"');
396 fseek (infile, -1, SEEK_CUR);
397 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
399 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
401 colors = g_new(_GdkPixmapColor, num_cols);
403 if (transparent_color == NULL)
405 gdk_color_white (colormap, &tmp_color);
406 transparent_color = &tmp_color;
409 for (cnt = 0; cnt < num_cols; cnt++)
413 gdk_pixmap_seek_char (infile, '"');
414 fseek (infile, -1, SEEK_CUR);
415 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
417 colors[cnt].color_string = g_new(gchar, cpp + 1);
418 for (n = 0; n < cpp; n++)
419 colors[cnt].color_string[n] = buffer[n];
420 colors[cnt].color_string[n] = 0;
421 colors[cnt].transparent = FALSE;
423 color_name = gdk_pixmap_extract_color (&buffer[cpp]);
425 if (color_name != NULL)
427 if (gdk_color_parse (color_name, &colors[cnt].color) == FALSE)
429 colors[cnt].color = *transparent_color;
430 colors[cnt].transparent = TRUE;
435 colors[cnt].color = *transparent_color;
436 colors[cnt].transparent = TRUE;
441 gdk_color_alloc (colormap, &colors[cnt].color);
445 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
450 /* The pixmap mask is just a bits pattern.
451 * Color 0 is used for background and 1 for foreground.
452 * We don't care about the colormap, we just need 0 and 1.
454 GdkColor mask_pattern;
456 *mask = gdk_pixmap_new (window, width, height, 1);
457 gc = gdk_gc_new (*mask);
459 mask_pattern.pixel = 0;
460 gdk_gc_set_foreground (gc, &mask_pattern);
461 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
463 mask_pattern.pixel = 1;
464 gdk_gc_set_foreground (gc, &mask_pattern);
467 for (ycnt = 0; ycnt < height; ycnt++)
469 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
471 for (n = 0, cnt = 0, xcnt = 0; n < (width * cpp); n += cpp, xcnt++)
473 strncpy (pixel_str, &buffer[n], cpp);
478 while ((color == NULL) && (ns < num_cols))
480 if (strcmp (pixel_str, colors[ns].color_string) == 0)
486 if (!color) /* screwed up XPM file */
489 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
491 if (mask && color->transparent)
494 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
499 if (mask && (cnt < xcnt))
500 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
506 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
508 gc = gdk_gc_new (pixmap);
509 gdk_gc_set_foreground (gc, transparent_color);
510 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
512 gdk_image_destroy (image);
521 for (cnt = 0; cnt < num_cols; cnt++)
522 g_free (colors[cnt].color_string);
531 gdk_pixmap_create_from_xpm (GdkWindow *window,
533 GdkColor *transparent_color,
534 const gchar *filename)
536 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
537 transparent_color, filename);
542 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
543 GdkColormap *colormap,
545 GdkColor *transparent_color,
548 GdkPixmap *pixmap = NULL;
549 GdkImage *image = NULL;
553 gint width, height, num_cols, cpp, cnt, n, ns, xcnt, ycnt, i;
554 gchar *buffer, pixel_str[32];
555 _GdkPixmapColor *colors = NULL, *color = NULL;
558 if ((window == NULL) && (colormap == NULL))
559 g_warning ("Creating pixmap from xpm with NULL window and colormap");
562 window = (GdkWindow *)&gdk_root_parent;
564 if (colormap == NULL)
566 colormap = gdk_window_get_colormap (window);
567 visual = gdk_window_get_visual (window);
570 visual = ((GdkColormapPrivate *)colormap)->visual;
574 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
576 colors = g_new(_GdkPixmapColor, num_cols);
578 if (transparent_color == NULL)
580 gdk_color_white (colormap, &tmp_color);
581 transparent_color = &tmp_color;
584 for (cnt = 0; cnt < num_cols; cnt++)
590 colors[cnt].color_string = g_new(gchar, cpp + 1);
591 for (n = 0; n < cpp; n++)
592 colors[cnt].color_string[n] = buffer[n];
593 colors[cnt].color_string[n] = 0;
594 colors[cnt].transparent = FALSE;
596 color_name = gdk_pixmap_extract_color (&buffer[cpp]);
598 if (color_name != NULL)
600 if (gdk_color_parse (color_name, &colors[cnt].color) == FALSE)
602 colors[cnt].color = *transparent_color;
603 colors[cnt].transparent = TRUE;
608 colors[cnt].color = *transparent_color;
609 colors[cnt].transparent = TRUE;
614 gdk_color_alloc (colormap, &colors[cnt].color);
618 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
623 /* The pixmap mask is just a bits pattern.
624 * Color 0 is used for background and 1 for foreground.
625 * We don't care about the colormap, we just need 0 and 1.
627 GdkColor mask_pattern;
629 *mask = gdk_pixmap_new (window, width, height, 1);
630 gc = gdk_gc_new (*mask);
632 mask_pattern.pixel = 0;
633 gdk_gc_set_foreground (gc, &mask_pattern);
634 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
636 mask_pattern.pixel = 1;
637 gdk_gc_set_foreground (gc, &mask_pattern);
640 for (ycnt = 0; ycnt < height; ycnt++)
644 for (n = 0, cnt = 0, xcnt = 0; n < (width * cpp); n += cpp, xcnt++)
646 strncpy (pixel_str, &buffer[n], cpp);
651 while ((color == NULL) && (ns < num_cols))
653 if (strcmp (pixel_str, colors[ns].color_string) == 0)
659 if (!color) /* screwed up XPM file */
662 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
664 if (mask && color->transparent)
667 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
672 if (mask && (cnt < xcnt))
673 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
679 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
681 gc = gdk_gc_new (pixmap);
682 gdk_gc_set_foreground (gc, transparent_color);
683 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
685 gdk_image_destroy (image);
689 for (cnt = 0; cnt < num_cols; cnt++)
690 g_free (colors[cnt].color_string);
698 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
700 GdkColor *transparent_color,
703 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
704 transparent_color, data);
708 gdk_pixmap_ref (GdkPixmap *pixmap)
710 GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap;
711 g_return_val_if_fail (pixmap != NULL, NULL);
713 private->ref_count += 1;
718 gdk_pixmap_unref (GdkPixmap *pixmap)
720 GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap;
721 g_return_if_fail(pixmap != NULL);
723 private->ref_count -= 1;
724 if (private->ref_count == 0)
726 XFreePixmap (private->xdisplay, private->xwindow);
727 gdk_xid_table_remove (private->xwindow);
733 gdk_bitmap_ref (GdkBitmap *bitmap)
735 return (GdkBitmap *)gdk_pixmap_ref ((GdkPixmap *)bitmap);
739 gdk_bitmap_unref (GdkBitmap *bitmap)
741 gdk_pixmap_unref ((GdkPixmap *)bitmap);