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,
217 if (fscanf (infile, "%1023s", instr) != 1)
220 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
224 if (fscanf (infile, "%1023s", instr) != 1)
227 while (strcmp (instr, "*/") != 0);
229 else if (strcmp (instr, str) == 0)
235 gdk_pixmap_seek_char (FILE *infile,
240 while ((b = getc(infile)) != EOF)
242 if (c != b && b == '/')
247 else if (b == '*') /* we have a comment */
257 while (!(oldb == '*' && b == '/'));
267 gdk_pixmap_read_string (FILE *infile,
272 guint cnt = 0, bufsiz, ret = FALSE;
276 bufsiz = *buffer_size;
279 bufsiz = 10 * sizeof (gchar);
280 buf = g_new(gchar, bufsiz);
285 while (c != EOF && c != '"');
290 while ((c = getc(infile)) != EOF)
294 guint new_size = bufsiz * 2;
295 if (new_size > bufsiz)
300 buf = (gchar *) g_realloc (buf, bufsiz);
301 buf[bufsiz-1] = '\0';
315 buf[bufsiz-1] = '\0'; /* ensure null termination for errors */
317 *buffer_size = bufsiz;
322 gdk_pixmap_skip_whitespaces (gchar *buffer)
326 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
329 return &buffer[index];
333 gdk_pixmap_skip_string (gchar *buffer)
337 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
340 return &buffer[index];
343 /* Xlib crashed ince at a color name lengths around 125 */
344 #define MAX_COLOR_LEN 120
347 gdk_pixmap_extract_color (gchar *buffer)
349 gint counter, numnames;
350 gchar *ptr = NULL, ch, temp[128];
351 gchar color[MAX_COLOR_LEN], *retcol;
357 if (buffer[counter] == 'c')
359 ch = buffer[counter + 1];
360 if (ch == 0x20 || ch == 0x09)
361 ptr = &buffer[counter + 1];
363 else if (buffer[counter] == 0)
369 ptr = gdk_pixmap_skip_whitespaces (ptr);
373 else if (ptr[0] == '#')
376 while (ptr[counter] != 0 &&
377 ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
378 (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
379 (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
382 retcol = g_new (gchar, counter+1);
383 strncpy (retcol, ptr, counter);
393 space = MAX_COLOR_LEN - 1;
396 sscanf (ptr, "%127s", temp);
398 if (((gint)ptr[0] == 0) ||
399 (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
400 (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
411 strncat (color, temp, space);
412 space -= MIN (space, strlen (temp));
413 ptr = gdk_pixmap_skip_string (ptr);
414 ptr = gdk_pixmap_skip_whitespaces (ptr);
419 retcol = g_strdup (color);
433 gdk_xpm_destroy_notify (gpointer data)
435 _GdkPixmapInfo *info = (_GdkPixmapInfo *)data;
439 for (i=0; i<info->ncolors; i++)
441 color.pixel = info->pixels[i];
442 gdk_colormap_free_colors (info->colormap, &color, 1);
445 gdk_colormap_unref (info->colormap);
450 _gdk_pixmap_create_from_xpm (GdkWindow *window,
451 GdkColormap *colormap,
453 GdkColor *transparent_color,
454 gchar * (*get_buf) (enum buffer_op op,
458 GdkPixmap *pixmap = NULL;
459 GdkImage *image = NULL;
463 gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes;
464 gchar *buffer, pixel_str[32];
466 _GdkPixmapColor *color = NULL, *fallbackcolor = NULL;
467 _GdkPixmapColor *colors = NULL;
469 GHashTable *color_hash = NULL;
470 _GdkPixmapInfo *color_info = NULL;
472 if ((window == NULL) && (colormap == NULL))
473 g_warning ("Creating pixmap from xpm with NULL window and colormap");
476 window = gdk_parent_root;
478 if (colormap == NULL)
480 colormap = gdk_drawable_get_colormap (window);
481 visual = gdk_drawable_get_visual (window);
484 visual = ((GdkColormapPrivate *)colormap)->visual;
486 buffer = (*get_buf) (op_header, handle);
490 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
493 g_warning ("Pixmap has more than 31 characters per color\n");
497 color_hash = g_hash_table_new (g_str_hash, g_str_equal);
499 if (transparent_color == NULL)
501 gdk_color_white (colormap, &tmp_color);
502 transparent_color = &tmp_color;
505 /* For pseudo-color and grayscale visuals, we have to remember
506 * the colors we allocated, so we can free them later.
508 if ((visual->type == GDK_VISUAL_PSEUDO_COLOR) ||
509 (visual->type == GDK_VISUAL_GRAYSCALE))
511 color_info = g_malloc (sizeof (_GdkPixmapInfo) +
512 sizeof(gulong) * (num_cols - 1));
513 color_info->ncolors = num_cols;
514 color_info->colormap = colormap;
515 gdk_colormap_ref (colormap);
518 name_buf = g_new (gchar, num_cols * (cpp+1));
519 colors = g_new (_GdkPixmapColor, num_cols);
521 for (cnt = 0; cnt < num_cols; cnt++)
525 buffer = (*get_buf) (op_cmap, handle);
529 color = &colors[cnt];
530 color->color_string = &name_buf [cnt * (cpp + 1)];
531 strncpy (color->color_string, buffer, cpp);
532 color->color_string[cpp] = 0;
533 buffer += strlen (color->color_string);
534 color->transparent = FALSE;
536 color_name = gdk_pixmap_extract_color (buffer);
538 if (color_name == NULL || g_strcasecmp (color_name, "None") == 0 ||
539 gdk_color_parse (color_name, &color->color) == FALSE)
541 color->color = *transparent_color;
542 color->transparent = TRUE;
547 /* FIXME: The remaining slowness appears to happen in this
549 gdk_color_alloc (colormap, &color->color);
552 color_info->pixels[cnt] = color->color.pixel;
554 g_hash_table_insert (color_hash, color->color_string, color);
556 fallbackcolor = color;
560 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
564 /* The pixmap mask is just a bits pattern.
565 * Color 0 is used for background and 1 for foreground.
566 * We don't care about the colormap, we just need 0 and 1.
568 GdkColor mask_pattern;
570 *mask = gdk_pixmap_new (window, width, height, 1);
571 gc = gdk_gc_new (*mask);
573 mask_pattern.pixel = 0;
574 gdk_gc_set_foreground (gc, &mask_pattern);
575 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
577 mask_pattern.pixel = 1;
578 gdk_gc_set_foreground (gc, &mask_pattern);
581 wbytes = width * cpp;
582 for (ycnt = 0; ycnt < height; ycnt++)
584 buffer = (*get_buf) (op_body, handle);
586 /* FIXME: this slows things down a little - it could be
587 * integrated into the strncpy below, perhaps. OTOH, strlen
590 if ((buffer == NULL) || strlen (buffer) < wbytes)
593 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
595 strncpy (pixel_str, &buffer[n], cpp);
599 color = g_hash_table_lookup (color_hash, pixel_str);
601 if (!color) /* screwed up XPM file */
602 color = fallbackcolor;
604 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
606 if (mask && color->transparent)
609 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
614 if (mask && (cnt < xcnt))
615 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
625 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
628 gdk_drawable_set_data (pixmap, "gdk-xpm", color_info,
629 gdk_xpm_destroy_notify);
631 gc = gdk_gc_new (pixmap);
632 gdk_gc_set_foreground (gc, transparent_color);
633 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
635 gdk_image_unref (image);
638 gdk_xpm_destroy_notify (color_info);
640 if (color_hash != NULL)
641 g_hash_table_destroy (color_hash);
646 if (name_buf != NULL)
662 file_buffer (enum buffer_op op, gpointer handle)
664 struct file_handle *h = handle;
669 if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE)
672 if (gdk_pixmap_seek_char (h->infile,'{') != TRUE)
674 /* Fall through to the next gdk_pixmap_seek_char. */
677 gdk_pixmap_seek_char (h->infile, '"');
678 fseek (h->infile, -1, SEEK_CUR);
679 /* Fall through to the gdk_pixmap_read_string. */
682 gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size);
690 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
691 GdkColormap *colormap,
693 GdkColor *transparent_color,
694 const gchar *filename)
696 struct file_handle h;
697 GdkPixmap *pixmap = NULL;
699 memset (&h, 0, sizeof (h));
700 h.infile = fopen (filename, "rb");
701 if (h.infile != NULL)
703 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
714 gdk_pixmap_create_from_xpm (GdkWindow *window,
716 GdkColor *transparent_color,
717 const gchar *filename)
719 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
720 transparent_color, filename);
732 mem_buffer (enum buffer_op op, gpointer handle)
734 struct mem_handle *h = handle;
740 if (h->data[h->offset])
741 return h->data[h->offset ++];
748 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
749 GdkColormap *colormap,
751 GdkColor *transparent_color,
755 GdkPixmap *pixmap = NULL;
757 memset (&h, 0, sizeof (h));
759 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
767 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
769 GdkColor *transparent_color,
772 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
773 transparent_color, data);
777 gdk_pixmap_foreign_new (guint32 anid)
780 GdkDrawablePrivate *private;
783 unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
785 /* check to make sure we were passed something at
786 least a little sane */
787 g_return_val_if_fail((anid != 0), NULL);
789 /* set the pixmap to the passed in value */
792 /* get information about the Pixmap to fill in the structure for
794 if (!XGetGeometry(GDK_DISPLAY(),
795 xpixmap, &root_return,
796 &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
799 pixmap = gdk_x11_pixmap_alloc ();
800 private = (GdkDrawablePrivate *)pixmap;
802 GDK_DRAWABLE_XDATA (private)->xdisplay = GDK_DISPLAY ();
803 GDK_DRAWABLE_XDATA (private)->xid = xpixmap;
805 private->width = w_ret;
806 private->height = h_ret;
808 gdk_xid_table_insert(&GDK_DRAWABLE_XID (pixmap), pixmap);