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 <gdk/gdkpixmap.h>
36 #include "gdkpixmap-x11.h"
37 #include "gdkprivate-x11.h"
49 GdkColormap *colormap;
53 static void gdk_pixmap_impl_x11_get_size (GdkDrawable *drawable,
57 static void gdk_pixmap_impl_x11_init (GdkPixmapImplX11 *pixmap);
58 static void gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass);
59 static void gdk_pixmap_impl_x11_finalize (GObject *object);
61 static gpointer parent_class = NULL;
64 gdk_pixmap_impl_x11_get_type (void)
66 static GType object_type = 0;
70 static const GTypeInfo object_info =
72 sizeof (GdkPixmapImplX11Class),
74 (GBaseFinalizeFunc) NULL,
75 (GClassInitFunc) gdk_pixmap_impl_x11_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof (GdkPixmapImplX11),
80 (GInstanceInitFunc) gdk_pixmap_impl_x11_init,
83 object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_X11,
93 _gdk_pixmap_impl_get_type (void)
95 return gdk_pixmap_impl_x11_get_type ();
99 gdk_pixmap_impl_x11_init (GdkPixmapImplX11 *impl)
106 gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass)
108 GObjectClass *object_class = G_OBJECT_CLASS (klass);
109 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
111 parent_class = g_type_class_peek_parent (klass);
113 object_class->finalize = gdk_pixmap_impl_x11_finalize;
115 drawable_class->get_size = gdk_pixmap_impl_x11_get_size;
119 gdk_pixmap_impl_x11_finalize (GObject *object)
121 GdkPixmapImplX11 *impl = GDK_PIXMAP_IMPL_X11 (object);
122 GdkPixmap *wrapper = GDK_PIXMAP (GDK_DRAWABLE_IMPL_X11 (impl)->wrapper);
124 XFreePixmap (GDK_PIXMAP_XDISPLAY (wrapper), GDK_PIXMAP_XID (wrapper));
125 gdk_xid_table_remove (GDK_PIXMAP_XID (wrapper));
127 G_OBJECT_CLASS (parent_class)->finalize (object);
131 gdk_pixmap_impl_x11_get_size (GdkDrawable *drawable,
136 *width = GDK_PIXMAP_IMPL_X11 (drawable)->width;
138 *height = GDK_PIXMAP_IMPL_X11 (drawable)->height;
142 gdk_pixmap_new (GdkWindow *window,
148 GdkDrawableImplX11 *draw_impl;
149 GdkPixmapImplX11 *pix_impl;
151 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
152 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
153 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
156 window = gdk_parent_root;
158 if (GDK_WINDOW_DESTROYED (window))
162 depth = gdk_drawable_get_depth (GDK_DRAWABLE (window));
164 pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
165 draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
166 pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
167 draw_impl->wrapper = GDK_DRAWABLE (pixmap);
169 draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
170 draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
171 GDK_WINDOW_XID (window),
172 width, height, depth);
174 pix_impl->width = width;
175 pix_impl->height = height;
176 GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
178 gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
184 gdk_bitmap_create_from_data (GdkWindow *window,
190 GdkDrawableImplX11 *draw_impl;
191 GdkPixmapImplX11 *pix_impl;
193 g_return_val_if_fail (data != NULL, NULL);
194 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
195 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
198 window = gdk_parent_root;
200 if (GDK_WINDOW_DESTROYED (window))
203 pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
204 draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
205 pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
206 draw_impl->wrapper = GDK_DRAWABLE (pixmap);
208 pix_impl->width = width;
209 pix_impl->height = height;
210 GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
212 draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
213 draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (window),
214 GDK_WINDOW_XID (window),
215 (char *)data, width, height);
217 gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
223 gdk_pixmap_create_from_data (GdkWindow *window,
232 GdkDrawableImplX11 *draw_impl;
233 GdkPixmapImplX11 *pix_impl;
235 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
236 g_return_val_if_fail (data != NULL, NULL);
237 g_return_val_if_fail (fg != NULL, NULL);
238 g_return_val_if_fail (bg != NULL, NULL);
239 g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
240 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
243 window = gdk_parent_root;
245 if (GDK_WINDOW_DESTROYED (window))
249 depth = gdk_drawable_get_visual (window)->depth;
251 pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
252 draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
253 pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
254 draw_impl->wrapper = GDK_DRAWABLE (pixmap);
256 pix_impl->width = width;
257 pix_impl->height = height;
258 GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
260 draw_impl->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
261 draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (window),
262 GDK_WINDOW_XID (window),
263 (char *)data, width, height,
264 fg->pixel, bg->pixel, depth);
266 gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
272 gdk_pixmap_seek_string (FILE *infile,
280 if (fscanf (infile, "%1023s", instr) != 1)
283 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
287 if (fscanf (infile, "%1023s", instr) != 1)
290 while (strcmp (instr, "*/") != 0);
292 else if (strcmp (instr, str) == 0)
298 gdk_pixmap_seek_char (FILE *infile,
303 while ((b = getc(infile)) != EOF)
305 if (c != b && b == '/')
310 else if (b == '*') /* we have a comment */
320 while (!(oldb == '*' && b == '/'));
330 gdk_pixmap_read_string (FILE *infile,
335 guint cnt = 0, bufsiz, ret = FALSE;
339 bufsiz = *buffer_size;
342 bufsiz = 10 * sizeof (gchar);
343 buf = g_new(gchar, bufsiz);
348 while (c != EOF && c != '"');
353 while ((c = getc(infile)) != EOF)
357 guint new_size = bufsiz * 2;
358 if (new_size > bufsiz)
363 buf = (gchar *) g_realloc (buf, bufsiz);
364 buf[bufsiz-1] = '\0';
378 buf[bufsiz-1] = '\0'; /* ensure null termination for errors */
380 *buffer_size = bufsiz;
385 gdk_pixmap_skip_whitespaces (gchar *buffer)
389 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
392 return &buffer[index];
396 gdk_pixmap_skip_string (gchar *buffer)
400 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
403 return &buffer[index];
406 /* Xlib crashed ince at a color name lengths around 125 */
407 #define MAX_COLOR_LEN 120
410 gdk_pixmap_extract_color (gchar *buffer)
412 gint counter, numnames;
413 gchar *ptr = NULL, ch, temp[128];
414 gchar color[MAX_COLOR_LEN], *retcol;
420 if (buffer[counter] == 'c')
422 ch = buffer[counter + 1];
423 if (ch == 0x20 || ch == 0x09)
424 ptr = &buffer[counter + 1];
426 else if (buffer[counter] == 0)
432 ptr = gdk_pixmap_skip_whitespaces (ptr);
436 else if (ptr[0] == '#')
439 while (ptr[counter] != 0 &&
440 ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
441 (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
442 (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
445 retcol = g_new (gchar, counter+1);
446 strncpy (retcol, ptr, counter);
456 space = MAX_COLOR_LEN - 1;
459 sscanf (ptr, "%127s", temp);
461 if (((gint)ptr[0] == 0) ||
462 (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
463 (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
474 strncat (color, temp, space);
475 space -= MIN (space, strlen (temp));
476 ptr = gdk_pixmap_skip_string (ptr);
477 ptr = gdk_pixmap_skip_whitespaces (ptr);
482 retcol = g_strdup (color);
496 gdk_xpm_destroy_notify (gpointer data)
498 _GdkPixmapInfo *info = (_GdkPixmapInfo *)data;
502 for (i=0; i<info->ncolors; i++)
504 color.pixel = info->pixels[i];
505 gdk_colormap_free_colors (info->colormap, &color, 1);
508 gdk_colormap_unref (info->colormap);
513 _gdk_pixmap_create_from_xpm (GdkWindow *window,
514 GdkColormap *colormap,
516 GdkColor *transparent_color,
517 gchar * (*get_buf) (enum buffer_op op,
521 GdkPixmap *pixmap = NULL;
522 GdkImage *image = NULL;
526 gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes;
527 gchar *buffer, pixel_str[32];
529 _GdkPixmapColor *color = NULL, *fallbackcolor = NULL;
530 _GdkPixmapColor *colors = NULL;
532 GHashTable *color_hash = NULL;
533 _GdkPixmapInfo *color_info = NULL;
535 if ((window == NULL) && (colormap == NULL))
536 g_warning ("Creating pixmap from xpm with NULL window and colormap");
539 window = gdk_parent_root;
541 if (colormap == NULL)
543 colormap = gdk_drawable_get_colormap (window);
544 visual = gdk_drawable_get_visual (window);
547 visual = colormap->visual;
549 buffer = (*get_buf) (op_header, handle);
553 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
556 g_warning ("Pixmap has more than 31 characters per color\n");
560 color_hash = g_hash_table_new (g_str_hash, g_str_equal);
562 if (transparent_color == NULL)
564 gdk_color_white (colormap, &tmp_color);
565 transparent_color = &tmp_color;
568 /* For pseudo-color and grayscale visuals, we have to remember
569 * the colors we allocated, so we can free them later.
571 if ((visual->type == GDK_VISUAL_PSEUDO_COLOR) ||
572 (visual->type == GDK_VISUAL_GRAYSCALE))
574 color_info = g_malloc (sizeof (_GdkPixmapInfo) +
575 sizeof(gulong) * (num_cols - 1));
576 color_info->ncolors = num_cols;
577 color_info->colormap = colormap;
578 gdk_colormap_ref (colormap);
581 name_buf = g_new (gchar, num_cols * (cpp+1));
582 colors = g_new (_GdkPixmapColor, num_cols);
584 for (cnt = 0; cnt < num_cols; cnt++)
588 buffer = (*get_buf) (op_cmap, handle);
592 color = &colors[cnt];
593 color->color_string = &name_buf [cnt * (cpp + 1)];
594 strncpy (color->color_string, buffer, cpp);
595 color->color_string[cpp] = 0;
596 buffer += strlen (color->color_string);
597 color->transparent = FALSE;
599 color_name = gdk_pixmap_extract_color (buffer);
601 if (color_name == NULL || g_strcasecmp (color_name, "None") == 0 ||
602 gdk_color_parse (color_name, &color->color) == FALSE)
604 color->color = *transparent_color;
605 color->transparent = TRUE;
610 /* FIXME: The remaining slowness appears to happen in this
612 gdk_color_alloc (colormap, &color->color);
615 color_info->pixels[cnt] = color->color.pixel;
617 g_hash_table_insert (color_hash, color->color_string, color);
619 fallbackcolor = color;
623 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
627 /* The pixmap mask is just a bits pattern.
628 * Color 0 is used for background and 1 for foreground.
629 * We don't care about the colormap, we just need 0 and 1.
631 GdkColor mask_pattern;
633 *mask = gdk_pixmap_new (window, width, height, 1);
634 gc = gdk_gc_new (*mask);
636 mask_pattern.pixel = 0;
637 gdk_gc_set_foreground (gc, &mask_pattern);
638 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
640 mask_pattern.pixel = 1;
641 gdk_gc_set_foreground (gc, &mask_pattern);
644 wbytes = width * cpp;
645 for (ycnt = 0; ycnt < height; ycnt++)
647 buffer = (*get_buf) (op_body, handle);
649 /* FIXME: this slows things down a little - it could be
650 * integrated into the strncpy below, perhaps. OTOH, strlen
653 if ((buffer == NULL) || strlen (buffer) < wbytes)
656 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
658 strncpy (pixel_str, &buffer[n], cpp);
662 color = g_hash_table_lookup (color_hash, pixel_str);
664 if (!color) /* screwed up XPM file */
665 color = fallbackcolor;
667 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
669 if (mask && color->transparent)
672 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
677 if (mask && (cnt < xcnt))
678 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
688 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
691 gdk_drawable_set_data (pixmap, "gdk-xpm", color_info,
692 gdk_xpm_destroy_notify);
694 gc = gdk_gc_new (pixmap);
695 gdk_gc_set_foreground (gc, transparent_color);
696 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
698 gdk_image_unref (image);
701 gdk_xpm_destroy_notify (color_info);
703 if (color_hash != NULL)
704 g_hash_table_destroy (color_hash);
709 if (name_buf != NULL)
725 file_buffer (enum buffer_op op, gpointer handle)
727 struct file_handle *h = handle;
732 if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE)
735 if (gdk_pixmap_seek_char (h->infile,'{') != TRUE)
737 /* Fall through to the next gdk_pixmap_seek_char. */
740 gdk_pixmap_seek_char (h->infile, '"');
741 fseek (h->infile, -1, SEEK_CUR);
742 /* Fall through to the gdk_pixmap_read_string. */
745 gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size);
753 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
754 GdkColormap *colormap,
756 GdkColor *transparent_color,
757 const gchar *filename)
759 struct file_handle h;
760 GdkPixmap *pixmap = NULL;
762 memset (&h, 0, sizeof (h));
763 h.infile = fopen (filename, "rb");
764 if (h.infile != NULL)
766 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
777 gdk_pixmap_create_from_xpm (GdkWindow *window,
779 GdkColor *transparent_color,
780 const gchar *filename)
782 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
783 transparent_color, filename);
795 mem_buffer (enum buffer_op op, gpointer handle)
797 struct mem_handle *h = handle;
803 if (h->data[h->offset])
804 return h->data[h->offset ++];
811 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
812 GdkColormap *colormap,
814 GdkColor *transparent_color,
818 GdkPixmap *pixmap = NULL;
820 memset (&h, 0, sizeof (h));
822 pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask,
830 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
832 GdkColor *transparent_color,
835 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
836 transparent_color, data);
840 gdk_pixmap_foreign_new (GdkNativeWindow anid)
843 GdkDrawableImplX11 *draw_impl;
844 GdkPixmapImplX11 *pix_impl;
847 unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
849 /* check to make sure we were passed something at
850 least a little sane */
851 g_return_val_if_fail((anid != 0), NULL);
853 /* set the pixmap to the passed in value */
856 /* get information about the Pixmap to fill in the structure for
858 if (!XGetGeometry(GDK_DISPLAY(),
859 xpixmap, &root_return,
860 &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
863 pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
864 draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
865 pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
866 draw_impl->wrapper = GDK_DRAWABLE (pixmap);
869 draw_impl->xdisplay = GDK_DISPLAY ();
870 draw_impl->xid = xpixmap;
872 pix_impl->width = w_ret;
873 pix_impl->height = h_ret;
874 GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
876 gdk_xid_table_insert(&GDK_PIXMAP_XID (pixmap), pixmap);