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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #include "../config.h"
23 /* Needed for SEEK_END in SunOS */
28 #include "gdkprivate.h"
38 gdk_pixmap_new (GdkWindow *window,
44 GdkWindowPrivate *private;
45 GdkWindowPrivate *window_private;
47 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
48 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
51 window = (GdkWindow*) &gdk_root_parent;
53 window_private = (GdkWindowPrivate*) window;
54 if (window_private->destroyed)
58 depth = gdk_window_get_visual (window)->depth;
60 private = g_new (GdkWindowPrivate, 1);
61 pixmap = (GdkPixmap*) private;
63 private->xdisplay = window_private->xdisplay;
64 private->window_type = GDK_WINDOW_PIXMAP;
65 private->xwindow = XCreatePixmap (private->xdisplay, window_private->xwindow,
66 width, height, depth);
67 private->colormap = NULL;
68 private->parent = NULL;
71 private->width = width;
72 private->height = height;
73 private->resize_count = 0;
74 private->ref_count = 1;
75 private->destroyed = 0;
77 gdk_xid_table_insert (&private->xwindow, pixmap);
83 gdk_bitmap_create_from_data (GdkWindow *window,
89 GdkWindowPrivate *private;
90 GdkWindowPrivate *window_private;
92 g_return_val_if_fail (data != NULL, NULL);
93 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
96 window = (GdkWindow*) &gdk_root_parent;
98 window_private = (GdkWindowPrivate*) window;
99 if (window_private->destroyed)
102 private = g_new (GdkWindowPrivate, 1);
103 pixmap = (GdkPixmap*) private;
105 private->parent = NULL;
106 private->xdisplay = window_private->xdisplay;
107 private->window_type = GDK_WINDOW_PIXMAP;
110 private->width = width;
111 private->height = height;
112 private->resize_count = 0;
113 private->ref_count = 1;
114 private->destroyed = FALSE;
116 private->xwindow = XCreateBitmapFromData (private->xdisplay,
117 window_private->xwindow,
118 data, width, height);
120 gdk_xid_table_insert (&private->xwindow, pixmap);
126 gdk_pixmap_create_from_data (GdkWindow *window,
135 GdkWindowPrivate *private;
136 GdkWindowPrivate *window_private;
138 g_return_val_if_fail (data != NULL, NULL);
139 g_return_val_if_fail (fg != NULL, NULL);
140 g_return_val_if_fail (bg != NULL, NULL);
141 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
142 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
145 window = (GdkWindow*) &gdk_root_parent;
147 window_private = (GdkWindowPrivate*) window;
148 if (window_private->destroyed)
152 depth = gdk_window_get_visual (window)->depth;
154 private = g_new (GdkWindowPrivate, 1);
155 pixmap = (GdkPixmap*) private;
157 private->parent = NULL;
158 private->xdisplay = window_private->xdisplay;
159 private->window_type = GDK_WINDOW_PIXMAP;
162 private->width = width;
163 private->height = height;
164 private->resize_count = 0;
165 private->ref_count = 1;
166 private->destroyed = FALSE;
168 private->xwindow = XCreatePixmapFromBitmapData (private->xdisplay,
169 window_private->xwindow,
171 fg->pixel, bg->pixel, depth);
173 gdk_xid_table_insert (&private->xwindow, pixmap);
179 gdk_pixmap_seek_string (FILE *infile,
185 while (!feof (infile))
187 fscanf (infile, "%1023s", instr);
188 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
190 fscanf (infile, "%1023s", instr);
191 while (!feof (infile) && strcmp (instr, "*/") != 0)
192 fscanf (infile, "%1023s", instr);
193 fscanf(infile, "%1023s", instr);
195 if (strcmp (instr, str)==0)
203 gdk_pixmap_seek_char (FILE *infile,
208 while ((b = getc(infile)) != EOF)
210 if (c != b && b == '/')
215 else if (b == '*') /* we have a comment */
225 while (!(oldb == '*' && b == '/'));
235 gdk_pixmap_read_string (FILE *infile,
240 guint cnt = 0, bufsiz, ret = FALSE;
244 bufsiz = *buffer_size;
247 bufsiz = 10 * sizeof (gchar);
248 buf = g_new(gchar, bufsiz);
253 while (c != EOF && c != '"');
258 while ((c = getc(infile)) != EOF)
262 guint new_size = bufsiz * 2;
263 if (new_size > bufsiz)
268 buf = (gchar *) g_realloc (buf, bufsiz);
269 buf[bufsiz-1] = '\0';
283 buf[bufsiz-1] = '\0'; /* ensure null termination for errors */
285 *buffer_size = bufsiz;
290 gdk_pixmap_skip_whitespaces (gchar *buffer)
294 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
297 return &buffer[index];
301 gdk_pixmap_skip_string (gchar *buffer)
305 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
308 return &buffer[index];
311 /* Xlib crashed ince at a color name lengths around 125 */
312 #define MAX_COLOR_LEN 120
315 gdk_pixmap_extract_color (gchar *buffer)
317 gint counter, numnames;
318 gchar *ptr = NULL, ch, temp[128];
319 gchar color[MAX_COLOR_LEN], *retcol;
325 if (buffer[counter] == 'c')
327 ch = buffer[counter + 1];
328 if (ch == 0x20 || ch == 0x09)
329 ptr = &buffer[counter + 1];
331 else if (buffer[counter] == 0)
337 ptr = gdk_pixmap_skip_whitespaces (ptr);
341 else if (ptr[0] == '#')
343 retcol = g_strdup (ptr);
350 space = MAX_COLOR_LEN - 1;
353 sscanf (ptr, "%127s", temp);
355 if (((gint)ptr[0] == 0) ||
356 (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
357 (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
368 strncat (color, temp, space);
369 space -= MIN (space, strlen (temp));
370 ptr = gdk_pixmap_skip_string (ptr);
371 ptr = gdk_pixmap_skip_whitespaces (ptr);
376 retcol = g_strdup (color);
381 free_color (gpointer key, gpointer value, gpointer user_data)
397 _gdk_pixmap_create_from_xpm (GdkWindow *window,
398 GdkColormap *colormap,
400 GdkColor *transparent_color,
401 gchar * (*get_buf) (enum buffer_op op,
405 GdkPixmap *pixmap = NULL;
406 GdkImage *image = NULL;
410 gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes;
411 gchar *buffer, pixel_str[32];
412 _GdkPixmapColor *color = NULL, *fallbackcolor = NULL;
414 GHashTable *colors = NULL;
416 if ((window == NULL) && (colormap == NULL))
417 g_warning ("Creating pixmap from xpm with NULL window and colormap");
420 window = (GdkWindow *)&gdk_root_parent;
422 if (colormap == NULL)
424 colormap = gdk_window_get_colormap (window);
425 visual = gdk_window_get_visual (window);
428 visual = ((GdkColormapPrivate *)colormap)->visual;
430 buffer = (*get_buf) (op_header, handle);
434 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
437 g_warning ("Pixmap has more than 31 characters per color\n");
441 colors = g_hash_table_new (g_str_hash, g_str_equal);
443 if (transparent_color == NULL)
445 gdk_color_white (colormap, &tmp_color);
446 transparent_color = &tmp_color;
449 for (cnt = 0; cnt < num_cols; cnt++)
453 buffer = (*get_buf) (op_cmap, handle);
457 color = g_new (_GdkPixmapColor, 1);
458 color->color_string = g_new(gchar, cpp + 1);
459 strncpy (color->color_string, buffer, cpp);
460 color->color_string[cpp] = 0;
461 buffer += strlen (color->color_string);
462 color->transparent = FALSE;
464 color_name = gdk_pixmap_extract_color (buffer);
466 if (color_name == NULL ||
467 gdk_color_parse (color_name, &color->color) == FALSE)
469 color->color = *transparent_color;
470 color->transparent = TRUE;
475 /* FIXME: The remaining slowness appears to happen in this
477 gdk_color_alloc (colormap, &color->color);
478 g_hash_table_insert (colors, color->color_string, color);
480 fallbackcolor = color;
484 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
489 /* The pixmap mask is just a bits pattern.
490 * Color 0 is used for background and 1 for foreground.
491 * We don't care about the colormap, we just need 0 and 1.
493 GdkColor mask_pattern;
495 *mask = gdk_pixmap_new (window, width, height, 1);
496 gc = gdk_gc_new (*mask);
498 mask_pattern.pixel = 0;
499 gdk_gc_set_foreground (gc, &mask_pattern);
500 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
502 mask_pattern.pixel = 1;
503 gdk_gc_set_foreground (gc, &mask_pattern);
506 wbytes = width * cpp;
507 for (ycnt = 0; ycnt < height; ycnt++)
509 buffer = (*get_buf) (op_body, handle);
511 /* FIXME: this slows things down a little - it could be
512 * integrated into the strncpy below, perhaps. OTOH, strlen
515 if ((buffer == NULL) || strlen (buffer) < wbytes)
518 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
520 strncpy (pixel_str, &buffer[n], cpp);
524 color = g_hash_table_lookup (colors, pixel_str);
526 if (!color) /* screwed up XPM file */
527 color = fallbackcolor;
529 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
531 if (mask && color->transparent)
534 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
539 if (mask && (cnt < xcnt))
540 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
550 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
552 gc = gdk_gc_new (pixmap);
553 gdk_gc_set_foreground (gc, transparent_color);
554 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
556 gdk_image_destroy (image);
561 g_hash_table_foreach (colors, free_color, 0);
562 g_hash_table_destroy (colors);
578 file_buffer (enum buffer_op op, gpointer handle)
580 struct file_handle *h = handle;
585 if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE)
588 if (gdk_pixmap_seek_char (h->infile,'{') != TRUE)
590 /* Fall through to the next gdk_pixmap_seek_char. */
593 gdk_pixmap_seek_char (h->infile, '"');
594 fseek (h->infile, -1, SEEK_CUR);
595 /* Fall through to the gdk_pixmap_read_string. */
598 gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size);
606 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
607 GdkColormap *colormap,
609 GdkColor *transparent_color,
610 const gchar *filename)
612 struct file_handle h;
613 GdkPixmap *pixmap = NULL;
615 memset (&h, 0, sizeof (h));
616 h.infile = fopen (filename, "rb");
617 if (h.infile != NULL)
619 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
630 gdk_pixmap_create_from_xpm (GdkWindow *window,
632 GdkColor *transparent_color,
633 const gchar *filename)
635 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
636 transparent_color, filename);
648 mem_buffer (enum buffer_op op, gpointer handle)
650 struct mem_handle *h = handle;
656 if (h->data[h->offset])
657 return h->data[h->offset ++];
664 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
665 GdkColormap *colormap,
667 GdkColor *transparent_color,
671 GdkPixmap *pixmap = NULL;
673 memset (&h, 0, sizeof (h));
675 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
683 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
685 GdkColor *transparent_color,
688 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
689 transparent_color, data);
693 gdk_pixmap_ref (GdkPixmap *pixmap)
695 GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap;
696 g_return_val_if_fail (pixmap != NULL, NULL);
698 private->ref_count += 1;
703 gdk_pixmap_unref (GdkPixmap *pixmap)
705 GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap;
706 g_return_if_fail(pixmap != NULL);
708 private->ref_count -= 1;
709 if (private->ref_count == 0)
711 XFreePixmap (private->xdisplay, private->xwindow);
712 gdk_xid_table_remove (private->xwindow);
718 gdk_bitmap_ref (GdkBitmap *bitmap)
720 return (GdkBitmap *)gdk_pixmap_ref ((GdkPixmap *)bitmap);
724 gdk_bitmap_unref (GdkBitmap *bitmap)
726 gdk_pixmap_unref ((GdkPixmap *)bitmap);