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;
47 window = (GdkWindow*) &gdk_root_parent;
50 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
52 private = g_new (GdkWindowPrivate, 1);
53 pixmap = (GdkPixmap*) private;
55 window_private = (GdkWindowPrivate*) window;
57 private->xdisplay = window_private->xdisplay;
58 private->window_type = GDK_WINDOW_PIXMAP;
59 private->xwindow = XCreatePixmap (private->xdisplay, window_private->xwindow,
60 width, height, depth);
61 private->parent = NULL;
64 private->width = width;
65 private->height = height;
66 private->resize_count = 0;
67 private->ref_count = 1;
68 private->destroyed = 0;
70 gdk_xid_table_insert (&private->xwindow, pixmap);
76 gdk_bitmap_create_from_data (GdkWindow *window,
82 GdkWindowPrivate *private;
83 GdkWindowPrivate *window_private;
85 g_return_val_if_fail (data != NULL, NULL);
88 window = (GdkWindow*) &gdk_root_parent;
90 private = g_new (GdkWindowPrivate, 1);
91 pixmap = (GdkPixmap*) private;
93 window_private = (GdkWindowPrivate*) window;
95 private->parent = NULL;
96 private->xdisplay = window_private->xdisplay;
97 private->window_type = GDK_WINDOW_PIXMAP;
100 private->width = width;
101 private->height = height;
102 private->resize_count = 0;
103 private->ref_count = 1;
104 private->destroyed = FALSE;
106 private->xwindow = XCreateBitmapFromData (private->xdisplay,
107 window_private->xwindow,
108 data, width, height);
110 gdk_xid_table_insert (&private->xwindow, pixmap);
116 gdk_pixmap_create_from_data (GdkWindow *window,
125 GdkWindowPrivate *private;
126 GdkWindowPrivate *window_private;
128 g_return_val_if_fail (data != NULL, NULL);
129 g_return_val_if_fail (fg != NULL, NULL);
130 g_return_val_if_fail (bg != NULL, NULL);
133 window = (GdkWindow*) &gdk_root_parent;
136 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
138 private = g_new (GdkWindowPrivate, 1);
139 pixmap = (GdkPixmap*) private;
141 window_private = (GdkWindowPrivate*) window;
143 private->parent = NULL;
144 private->xdisplay = window_private->xdisplay;
145 private->window_type = GDK_WINDOW_PIXMAP;
148 private->width = width;
149 private->height = height;
150 private->resize_count = 0;
151 private->ref_count = 1;
152 private->destroyed = FALSE;
154 private->xwindow = XCreatePixmapFromBitmapData (private->xdisplay,
155 window_private->xwindow,
157 fg->pixel, bg->pixel, depth);
159 gdk_xid_table_insert (&private->xwindow, pixmap);
165 gdk_pixmap_seek_string (FILE *infile,
171 while (!feof (infile))
173 fscanf (infile, "%s", instr);
174 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
176 fscanf (infile, "%s", instr);
177 while (!feof (infile) && strcmp (instr, "*/") != 0)
178 fscanf (infile, "%s", instr);
179 fscanf(infile, "%s", instr);
181 if (strcmp (instr, str)==0)
189 gdk_pixmap_seek_char (FILE *infile,
194 while (!feof (infile))
196 fscanf(infile, "%c", &b);
197 if (c != b && b == '/')
199 fscanf (infile, "%c", &b);
203 while (!feof (infile) && !(oldb == '*' && b == '/'))
206 fscanf (infile, "%c", &b);
208 fscanf (infile, "%c", &b);
219 gdk_pixmap_read_string (FILE *infile,
226 if ((*buffer) == NULL)
228 (*buffer_size) = 10 * sizeof (gchar);
229 (*buffer) = (gchar *) malloc (*buffer_size);
233 fscanf (infile, "%c", &c);
234 while (!feof (infile) && c != '"');
239 while (!feof (infile))
241 fscanf (infile, "%c", &c);
243 if (cnt == (*buffer_size))
246 (*buffer) = (gchar *) realloc ((*buffer), *buffer_size);
250 (*buffer)[cnt++] = c;
253 (*buffer)[cnt++] = 0;
262 gdk_pixmap_skip_whitespaces (gchar *buffer)
266 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
269 return &buffer[index];
273 gdk_pixmap_skip_string (gchar *buffer)
277 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
280 return &buffer[index];
284 gdk_pixmap_extract_color (gchar *buffer)
286 gint counter, finished = FALSE, numnames;
287 gchar *ptr = NULL, ch, temp[128];
288 gchar color[128], *retcol;
293 if (buffer[counter] == 'c')
295 ch = buffer[counter + 1];
296 if (ch == 0x20 || ch == 0x09)
297 ptr = &buffer[counter + 1];
299 else if (buffer[counter] == 0)
308 ptr = gdk_pixmap_skip_whitespaces (ptr);
312 else if (ptr[0] == '#')
314 retcol = g_new(gchar, strlen (ptr) + 1);
315 strcpy (retcol, ptr);
322 while (finished == FALSE)
324 sscanf (ptr, "%s", temp);
326 if ((gint)ptr[0] == 0 || strcmp ("s", temp) == 0 || strcmp ("m", temp) == 0 ||
327 strcmp ("g", temp) == 0 || strcmp ("g4", temp) == 0)
333 strcat (color, temp);
334 ptr = gdk_pixmap_skip_string (ptr);
335 ptr = gdk_pixmap_skip_whitespaces (ptr);
340 retcol = g_new(gchar, strlen (color) + 1);
341 strcpy (retcol, color);
347 gdk_pixmap_create_from_xpm (GdkWindow *window,
349 GdkColor *transparent_color,
350 const gchar *filename)
353 GdkPixmap *pixmap = NULL;
354 GdkImage *image = NULL;
355 GdkColormap *colormap;
359 gint width, height, num_cols, cpp, cnt, n, ns, xcnt, ycnt;
360 gchar *buffer = NULL, *color_name = NULL, pixel_str[32];
361 guint buffer_size = 0;
362 _GdkPixmapColor *colors = NULL, *color = NULL;
366 window = (GdkWindow*) &gdk_root_parent;
368 infile = fopen (filename, "rb");
371 if (gdk_pixmap_seek_string (infile, "XPM", FALSE) == TRUE)
373 if (gdk_pixmap_seek_char (infile,'{') == TRUE)
375 gdk_pixmap_seek_char (infile, '"');
376 fseek (infile, -1, SEEK_CUR);
377 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
379 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
381 colors = g_new(_GdkPixmapColor, num_cols);
383 colormap = gdk_window_get_colormap (window);
384 visual = gdk_window_get_visual (window);
386 if (transparent_color == NULL)
388 gdk_color_white (colormap, &tmp_color);
389 transparent_color = &tmp_color;
392 for (cnt = 0; cnt < num_cols; cnt++)
394 gdk_pixmap_seek_char (infile, '"');
395 fseek (infile, -1, SEEK_CUR);
396 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
398 colors[cnt].color_string = g_new(gchar, cpp + 1);
399 for (n = 0; n < cpp; n++)
400 colors[cnt].color_string[n] = buffer[n];
401 colors[cnt].color_string[n] = 0;
402 colors[cnt].transparent = FALSE;
404 if (color_name != NULL)
407 color_name = gdk_pixmap_extract_color (&buffer[cpp]);
409 if (color_name != NULL)
411 if (gdk_color_parse (color_name, &colors[cnt].color) == FALSE)
413 colors[cnt].color = *transparent_color;
414 colors[cnt].transparent = TRUE;
419 colors[cnt].color = *transparent_color;
420 colors[cnt].transparent = TRUE;
423 gdk_color_alloc (colormap, &colors[cnt].color);
427 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
432 *mask = gdk_pixmap_new (window, width, height, 1);
433 gc = gdk_gc_new (*mask);
435 gdk_color_black (colormap, &tmp_color);
436 gdk_gc_set_foreground (gc, &tmp_color);
437 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
439 gdk_color_white (colormap, &tmp_color);
440 gdk_gc_set_foreground (gc, &tmp_color);
443 for (ycnt = 0; ycnt < height; ycnt++)
445 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
447 for (n = 0, cnt = 0, xcnt = 0; n < (width * cpp); n += cpp, xcnt++)
449 strncpy (pixel_str, &buffer[n], cpp);
454 while (color == NULL)
456 if (strcmp (pixel_str, colors[ns].color_string) == 0)
462 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
464 if (mask && color->transparent)
467 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
472 if (mask && (cnt < xcnt))
473 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
479 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
481 gc = gdk_gc_new (pixmap);
482 gdk_gc_set_foreground (gc, transparent_color);
483 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
485 gdk_image_destroy (image);
494 for (cnt = 0; cnt < num_cols; cnt++)
495 g_free (colors[cnt].color_string);
504 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
506 GdkColor *transparent_color,
509 GdkPixmap *pixmap = NULL;
510 GdkImage *image = NULL;
511 GdkColormap *colormap;
515 gint width, height, num_cols, cpp, cnt, n, ns, xcnt, ycnt, i;
516 gchar *buffer, *color_name = NULL, pixel_str[32];
517 _GdkPixmapColor *colors = NULL, *color = NULL;
521 window = (GdkWindow*) &gdk_root_parent;
525 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
527 colors = g_new(_GdkPixmapColor, num_cols);
529 colormap = gdk_window_get_colormap (window);
530 visual = gdk_window_get_visual (window);
532 if (transparent_color == NULL)
534 gdk_color_white (colormap, &tmp_color);
535 transparent_color = &tmp_color;
538 for (cnt = 0; cnt < num_cols; cnt++)
542 colors[cnt].color_string = g_new(gchar, cpp + 1);
543 for (n = 0; n < cpp; n++)
544 colors[cnt].color_string[n] = buffer[n];
545 colors[cnt].color_string[n] = 0;
546 colors[cnt].transparent = FALSE;
548 if (color_name != NULL)
551 color_name = gdk_pixmap_extract_color (&buffer[cpp]);
553 if (color_name != NULL)
555 if (gdk_color_parse (color_name, &colors[cnt].color) == FALSE)
557 colors[cnt].color = *transparent_color;
558 colors[cnt].transparent = TRUE;
563 colors[cnt].color = *transparent_color;
564 colors[cnt].transparent = TRUE;
567 gdk_color_alloc (colormap, &colors[cnt].color);
571 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
576 *mask = gdk_pixmap_new (window, width, height, 1);
577 gc = gdk_gc_new (*mask);
579 gdk_color_black (colormap, &tmp_color);
580 gdk_gc_set_foreground (gc, &tmp_color);
581 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
583 gdk_color_white (colormap, &tmp_color);
584 gdk_gc_set_foreground (gc, &tmp_color);
587 for (ycnt = 0; ycnt < height; ycnt++)
591 for (n = 0, cnt = 0, xcnt = 0; n < (width * cpp); n += cpp, xcnt++)
593 strncpy (pixel_str, &buffer[n], cpp);
598 while (color == NULL)
600 if (strcmp (pixel_str, colors[ns].color_string) == 0)
606 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
608 if (mask && color->transparent)
611 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
616 if (mask && (cnt < xcnt))
617 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
623 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
625 gc = gdk_gc_new (pixmap);
626 gdk_gc_set_foreground (gc, transparent_color);
627 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
629 gdk_image_destroy (image);
633 for (cnt = 0; cnt < num_cols; cnt++)
634 g_free (colors[cnt].color_string);
642 gdk_pixmap_destroy (GdkPixmap *pixmap)
644 GdkWindowPrivate *private;
646 g_return_if_fail (pixmap != NULL);
648 private = (GdkPixmapPrivate*) pixmap;
649 if (private->ref_count <= 0)
651 XFreePixmap (private->xdisplay, private->xwindow);
652 gdk_xid_table_remove (private->xwindow);
657 private->ref_count -= 1;