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.
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/.
31 /* Needed for SEEK_END in SunOS */
35 #include "gdkpixmap.h"
36 #include "gdkprivate.h"
49 GdkColormap *colormap;
53 GdkDrawableClass _gdk_x11_pixmap_class;
56 gdk_x11_pixmap_destroy (GdkPixmap *pixmap)
58 XFreePixmap (GDK_DRAWABLE_XDISPLAY (pixmap), GDK_DRAWABLE_XID (pixmap));
59 gdk_xid_table_remove (GDK_DRAWABLE_XID (pixmap));
61 g_free (GDK_DRAWABLE_XDATA (pixmap));
65 gdk_x11_pixmap_alloc (void)
67 GdkDrawable *drawable;
68 GdkDrawablePrivate *private;
70 static GdkDrawableClass klass;
71 static gboolean initialized = FALSE;
77 klass = _gdk_x11_drawable_class;
78 klass.destroy = gdk_x11_pixmap_destroy;
81 drawable = gdk_drawable_alloc ();
82 private = (GdkDrawablePrivate *)drawable;
84 private->klass = &klass;
85 private->klass_data = g_new (GdkDrawableXData, 1);
86 private->window_type = GDK_DRAWABLE_PIXMAP;
92 gdk_pixmap_new (GdkWindow *window,
98 GdkDrawablePrivate *private;
100 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
101 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
102 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
105 window = gdk_parent_root;
107 if (GDK_DRAWABLE_DESTROYED (window))
111 depth = gdk_drawable_get_visual (window)->depth;
113 pixmap = gdk_x11_pixmap_alloc ();
114 private = (GdkDrawablePrivate *)pixmap;
116 GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
117 GDK_DRAWABLE_XDATA (private)->xid = XCreatePixmap (GDK_DRAWABLE_XDISPLAY (pixmap),
118 GDK_DRAWABLE_XID (window),
119 width, height, depth);
120 private->width = width;
121 private->height = height;
123 gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
129 gdk_bitmap_create_from_data (GdkWindow *window,
135 GdkDrawablePrivate *private;
137 g_return_val_if_fail (data != NULL, NULL);
138 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
139 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
142 window = gdk_parent_root;
144 if (GDK_DRAWABLE_DESTROYED (window))
147 pixmap = gdk_x11_pixmap_alloc ();
148 private = (GdkDrawablePrivate *)pixmap;
150 private->width = width;
151 private->height = height;
153 GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
154 GDK_DRAWABLE_XDATA (private)->xid = XCreateBitmapFromData (GDK_DRAWABLE_XDISPLAY (window),
155 GDK_DRAWABLE_XID (window),
156 (char *)data, width, height);
158 gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
164 gdk_pixmap_create_from_data (GdkWindow *window,
173 GdkDrawablePrivate *private;
175 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
176 g_return_val_if_fail (data != NULL, NULL);
177 g_return_val_if_fail (fg != NULL, NULL);
178 g_return_val_if_fail (bg != NULL, NULL);
179 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
180 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
183 window = gdk_parent_root;
185 if (GDK_DRAWABLE_DESTROYED (window))
189 depth = gdk_drawable_get_visual (window)->depth;
191 pixmap = gdk_x11_pixmap_alloc ();
192 private = (GdkDrawablePrivate *)pixmap;
194 private->width = width;
195 private->height = height;
197 GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
198 GDK_DRAWABLE_XDATA (private)->xid = XCreatePixmapFromBitmapData (GDK_DRAWABLE_XDISPLAY (window),
199 GDK_DRAWABLE_XID (window),
200 (char *)data, width, height,
201 fg->pixel, bg->pixel, depth);
203 gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
209 gdk_pixmap_seek_string (FILE *infile,
215 while (!feof (infile))
217 fscanf (infile, "%1023s", instr);
218 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
220 fscanf (infile, "%1023s", instr);
221 while (!feof (infile) && strcmp (instr, "*/") != 0)
222 fscanf (infile, "%1023s", instr);
223 fscanf(infile, "%1023s", instr);
225 if (strcmp (instr, str)==0)
233 gdk_pixmap_seek_char (FILE *infile,
238 while ((b = getc(infile)) != EOF)
240 if (c != b && b == '/')
245 else if (b == '*') /* we have a comment */
255 while (!(oldb == '*' && b == '/'));
265 gdk_pixmap_read_string (FILE *infile,
270 guint cnt = 0, bufsiz, ret = FALSE;
274 bufsiz = *buffer_size;
277 bufsiz = 10 * sizeof (gchar);
278 buf = g_new(gchar, bufsiz);
283 while (c != EOF && c != '"');
288 while ((c = getc(infile)) != EOF)
292 guint new_size = bufsiz * 2;
293 if (new_size > bufsiz)
298 buf = (gchar *) g_realloc (buf, bufsiz);
299 buf[bufsiz-1] = '\0';
313 buf[bufsiz-1] = '\0'; /* ensure null termination for errors */
315 *buffer_size = bufsiz;
320 gdk_pixmap_skip_whitespaces (gchar *buffer)
324 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
327 return &buffer[index];
331 gdk_pixmap_skip_string (gchar *buffer)
335 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
338 return &buffer[index];
341 /* Xlib crashed ince at a color name lengths around 125 */
342 #define MAX_COLOR_LEN 120
345 gdk_pixmap_extract_color (gchar *buffer)
347 gint counter, numnames;
348 gchar *ptr = NULL, ch, temp[128];
349 gchar color[MAX_COLOR_LEN], *retcol;
355 if (buffer[counter] == 'c')
357 ch = buffer[counter + 1];
358 if (ch == 0x20 || ch == 0x09)
359 ptr = &buffer[counter + 1];
361 else if (buffer[counter] == 0)
367 ptr = gdk_pixmap_skip_whitespaces (ptr);
371 else if (ptr[0] == '#')
374 while (ptr[counter] != 0 &&
375 ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
376 (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
377 (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
380 retcol = g_new (gchar, counter+1);
381 strncpy (retcol, ptr, counter);
391 space = MAX_COLOR_LEN - 1;
394 sscanf (ptr, "%127s", temp);
396 if (((gint)ptr[0] == 0) ||
397 (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
398 (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
409 strncat (color, temp, space);
410 space -= MIN (space, strlen (temp));
411 ptr = gdk_pixmap_skip_string (ptr);
412 ptr = gdk_pixmap_skip_whitespaces (ptr);
417 retcol = g_strdup (color);
431 gdk_xpm_destroy_notify (gpointer data)
433 _GdkPixmapInfo *info = (_GdkPixmapInfo *)data;
437 for (i=0; i<info->ncolors; i++)
439 color.pixel = info->pixels[i];
440 gdk_colormap_free_colors (info->colormap, &color, 1);
443 gdk_colormap_unref (info->colormap);
448 _gdk_pixmap_create_from_xpm (GdkWindow *window,
449 GdkColormap *colormap,
451 GdkColor *transparent_color,
452 gchar * (*get_buf) (enum buffer_op op,
456 GdkPixmap *pixmap = NULL;
457 GdkImage *image = NULL;
461 gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes;
462 gchar *buffer, pixel_str[32];
464 _GdkPixmapColor *color = NULL, *fallbackcolor = NULL;
465 _GdkPixmapColor *colors = NULL;
467 GHashTable *color_hash = NULL;
468 _GdkPixmapInfo *color_info = NULL;
470 if ((window == NULL) && (colormap == NULL))
471 g_warning ("Creating pixmap from xpm with NULL window and colormap");
474 window = gdk_parent_root;
476 if (colormap == NULL)
478 colormap = gdk_drawable_get_colormap (window);
479 visual = gdk_drawable_get_visual (window);
482 visual = ((GdkColormapPrivate *)colormap)->visual;
484 buffer = (*get_buf) (op_header, handle);
488 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
491 g_warning ("Pixmap has more than 31 characters per color\n");
495 color_hash = g_hash_table_new (g_str_hash, g_str_equal);
497 if (transparent_color == NULL)
499 gdk_color_white (colormap, &tmp_color);
500 transparent_color = &tmp_color;
503 /* For pseudo-color and grayscale visuals, we have to remember
504 * the colors we allocated, so we can free them later.
506 if ((visual->type == GDK_VISUAL_PSEUDO_COLOR) ||
507 (visual->type == GDK_VISUAL_GRAYSCALE))
509 color_info = g_malloc (sizeof (_GdkPixmapInfo) +
510 sizeof(gulong) * (num_cols - 1));
511 color_info->ncolors = num_cols;
512 color_info->colormap = colormap;
513 gdk_colormap_ref (colormap);
516 name_buf = g_new (gchar, num_cols * (cpp+1));
517 colors = g_new (_GdkPixmapColor, num_cols);
519 for (cnt = 0; cnt < num_cols; cnt++)
523 buffer = (*get_buf) (op_cmap, handle);
527 color = &colors[cnt];
528 color->color_string = &name_buf [cnt * (cpp + 1)];
529 strncpy (color->color_string, buffer, cpp);
530 color->color_string[cpp] = 0;
531 buffer += strlen (color->color_string);
532 color->transparent = FALSE;
534 color_name = gdk_pixmap_extract_color (buffer);
536 if (color_name == NULL || g_strcasecmp (color_name, "None") == 0 ||
537 gdk_color_parse (color_name, &color->color) == FALSE)
539 color->color = *transparent_color;
540 color->transparent = TRUE;
545 /* FIXME: The remaining slowness appears to happen in this
547 gdk_color_alloc (colormap, &color->color);
550 color_info->pixels[cnt] = color->color.pixel;
552 g_hash_table_insert (color_hash, color->color_string, color);
554 fallbackcolor = color;
558 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
562 /* The pixmap mask is just a bits pattern.
563 * Color 0 is used for background and 1 for foreground.
564 * We don't care about the colormap, we just need 0 and 1.
566 GdkColor mask_pattern;
568 *mask = gdk_pixmap_new (window, width, height, 1);
569 gc = gdk_gc_new (*mask);
571 mask_pattern.pixel = 0;
572 gdk_gc_set_foreground (gc, &mask_pattern);
573 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
575 mask_pattern.pixel = 1;
576 gdk_gc_set_foreground (gc, &mask_pattern);
579 wbytes = width * cpp;
580 for (ycnt = 0; ycnt < height; ycnt++)
582 buffer = (*get_buf) (op_body, handle);
584 /* FIXME: this slows things down a little - it could be
585 * integrated into the strncpy below, perhaps. OTOH, strlen
588 if ((buffer == NULL) || strlen (buffer) < wbytes)
591 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
593 strncpy (pixel_str, &buffer[n], cpp);
597 color = g_hash_table_lookup (color_hash, pixel_str);
599 if (!color) /* screwed up XPM file */
600 color = fallbackcolor;
602 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
604 if (mask && color->transparent)
607 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
612 if (mask && (cnt < xcnt))
613 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
623 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
626 gdk_drawable_set_data (pixmap, "gdk-xpm", color_info,
627 gdk_xpm_destroy_notify);
629 gc = gdk_gc_new (pixmap);
630 gdk_gc_set_foreground (gc, transparent_color);
631 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
633 gdk_image_unref (image);
636 gdk_xpm_destroy_notify (color_info);
638 if (color_hash != NULL)
639 g_hash_table_destroy (color_hash);
644 if (name_buf != NULL)
660 file_buffer (enum buffer_op op, gpointer handle)
662 struct file_handle *h = handle;
667 if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE)
670 if (gdk_pixmap_seek_char (h->infile,'{') != TRUE)
672 /* Fall through to the next gdk_pixmap_seek_char. */
675 gdk_pixmap_seek_char (h->infile, '"');
676 fseek (h->infile, -1, SEEK_CUR);
677 /* Fall through to the gdk_pixmap_read_string. */
680 gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size);
688 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
689 GdkColormap *colormap,
691 GdkColor *transparent_color,
692 const gchar *filename)
694 struct file_handle h;
695 GdkPixmap *pixmap = NULL;
697 memset (&h, 0, sizeof (h));
698 h.infile = fopen (filename, "rb");
699 if (h.infile != NULL)
701 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
712 gdk_pixmap_create_from_xpm (GdkWindow *window,
714 GdkColor *transparent_color,
715 const gchar *filename)
717 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
718 transparent_color, filename);
730 mem_buffer (enum buffer_op op, gpointer handle)
732 struct mem_handle *h = handle;
738 if (h->data[h->offset])
739 return h->data[h->offset ++];
746 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
747 GdkColormap *colormap,
749 GdkColor *transparent_color,
753 GdkPixmap *pixmap = NULL;
755 memset (&h, 0, sizeof (h));
757 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
765 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
767 GdkColor *transparent_color,
770 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
771 transparent_color, data);
775 gdk_pixmap_foreign_new (guint32 anid)
778 GdkDrawablePrivate *private;
781 unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
783 /* check to make sure we were passed something at
784 least a little sane */
785 g_return_val_if_fail((anid != 0), NULL);
787 /* set the pixmap to the passed in value */
790 /* get information about the Pixmap to fill in the structure for
792 if (!XGetGeometry(GDK_DISPLAY(),
793 xpixmap, &root_return,
794 &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
797 pixmap = gdk_x11_pixmap_alloc ();
798 private = (GdkDrawablePrivate *)pixmap;
800 GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DISPLAY ();
801 GDK_DRAWABLE_XDATA (private)->xid = xpixmap;
803 private->width = w_ret;
804 private->height = h_ret;
806 gdk_xid_table_insert(&GDK_DRAWABLE_XID (pixmap), pixmap);