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"
25 #include "gdkprivate.h"
35 gdk_pixmap_new (GdkWindow *window,
41 GdkWindowPrivate *private;
42 GdkWindowPrivate *window_private;
45 window = (GdkWindow*) &gdk_root_parent;
48 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
50 private = g_new (GdkWindowPrivate, 1);
51 pixmap = (GdkPixmap*) private;
53 window_private = (GdkWindowPrivate*) window;
55 private->xdisplay = window_private->xdisplay;
56 private->window_type = GDK_WINDOW_PIXMAP;
57 private->xwindow = XCreatePixmap (private->xdisplay, window_private->xwindow,
58 width, height, depth);
59 private->parent = NULL;
62 private->width = width;
63 private->height = height;
64 private->resize_count = 0;
65 private->ref_count = 1;
66 private->destroyed = 0;
68 gdk_xid_table_insert (&private->xwindow, pixmap);
74 gdk_bitmap_create_from_data (GdkWindow *window,
80 GdkWindowPrivate *private;
81 GdkWindowPrivate *window_private;
83 g_return_val_if_fail (data != NULL, NULL);
86 window = (GdkWindow*) &gdk_root_parent;
88 private = g_new (GdkWindowPrivate, 1);
89 pixmap = (GdkPixmap*) private;
91 window_private = (GdkWindowPrivate*) window;
93 private->parent = NULL;
94 private->xdisplay = window_private->xdisplay;
95 private->window_type = GDK_WINDOW_PIXMAP;
98 private->width = width;
99 private->height = height;
100 private->resize_count = 0;
101 private->ref_count = 1;
102 private->destroyed = FALSE;
104 private->xwindow = XCreateBitmapFromData (private->xdisplay,
105 window_private->xwindow,
106 data, width, height);
108 gdk_xid_table_insert (&private->xwindow, pixmap);
114 gdk_pixmap_create_from_data (GdkWindow *window,
123 GdkWindowPrivate *private;
124 GdkWindowPrivate *window_private;
126 g_return_val_if_fail (data != NULL, NULL);
127 g_return_val_if_fail (fg != NULL, NULL);
128 g_return_val_if_fail (bg != NULL, NULL);
131 window = (GdkWindow*) &gdk_root_parent;
134 gdk_window_get_geometry (window, NULL, NULL, NULL, NULL, &depth);
136 private = g_new (GdkWindowPrivate, 1);
137 pixmap = (GdkPixmap*) private;
139 window_private = (GdkWindowPrivate*) window;
141 private->parent = NULL;
142 private->xdisplay = window_private->xdisplay;
143 private->window_type = GDK_WINDOW_PIXMAP;
146 private->width = width;
147 private->height = height;
148 private->resize_count = 0;
149 private->ref_count = 1;
150 private->destroyed = FALSE;
152 private->xwindow = XCreatePixmapFromBitmapData (private->xdisplay,
153 window_private->xwindow,
155 fg->pixel, bg->pixel, depth);
157 gdk_xid_table_insert (&private->xwindow, pixmap);
163 gdk_pixmap_seek_string (FILE *infile,
169 while (!feof (infile))
171 fscanf (infile, "%s", instr);
172 if (skip_comments == TRUE && strcmp (instr, "/*") == 0)
174 fscanf (infile, "%s", instr);
175 while (!feof (infile) && strcmp (instr, "*/") != 0)
176 fscanf (infile, "%s", instr);
177 fscanf(infile, "%s", instr);
179 if (strcmp (instr, str)==0)
187 gdk_pixmap_seek_char (FILE *infile,
192 while (!feof (infile))
194 fscanf(infile, "%c", &b);
195 if (c != b && b == '/')
197 fscanf (infile, "%c", &b);
201 while (!feof (infile) && !(oldb == '*' && b == '/'))
204 fscanf (infile, "%c", &b);
206 fscanf (infile, "%c", &b);
217 gdk_pixmap_read_string (FILE *infile,
224 if ((*buffer) == NULL)
226 (*buffer_size) = 10 * sizeof (gchar);
227 (*buffer) = (gchar *) malloc (*buffer_size);
231 fscanf (infile, "%c", &c);
232 while (!feof (infile) && c != '"');
237 while (!feof (infile))
239 fscanf (infile, "%c", &c);
241 if (cnt == (*buffer_size))
244 (*buffer) = (gchar *) realloc ((*buffer), *buffer_size);
248 (*buffer)[cnt++] = c;
251 (*buffer)[cnt++] = 0;
260 gdk_pixmap_skip_whitespaces (gchar *buffer)
264 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
267 return &buffer[index];
271 gdk_pixmap_skip_string (gchar *buffer)
275 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
278 return &buffer[index];
282 gdk_pixmap_extract_color (gchar *buffer)
284 gint counter, finished = FALSE, numnames;
285 gchar *ptr = NULL, ch, temp[128];
286 gchar color[128], *retcol;
291 if (buffer[counter] == 'c')
293 ch = buffer[counter + 1];
294 if (ch == 0x20 || ch == 0x09)
295 ptr = &buffer[counter + 1];
297 else if (buffer[counter] == 0)
306 ptr = gdk_pixmap_skip_whitespaces (ptr);
310 else if (ptr[0] == '#')
312 retcol = g_new(gchar, strlen (ptr) + 1);
313 strcpy (retcol, ptr);
320 while (finished == FALSE)
322 sscanf (ptr, "%s", temp);
324 if ((gint)ptr[0] == 0 || strcmp ("s", temp) == 0 || strcmp ("m", temp) == 0 ||
325 strcmp ("g", temp) == 0 || strcmp ("g4", temp) == 0)
331 strcat (color, temp);
332 ptr = gdk_pixmap_skip_string (ptr);
333 ptr = gdk_pixmap_skip_whitespaces (ptr);
338 retcol = g_new(gchar, strlen (color) + 1);
339 strcpy (retcol, color);
345 gdk_pixmap_create_from_xpm (GdkWindow *window,
347 GdkColor *transparent_color,
348 const gchar *filename)
351 GdkPixmap *pixmap = NULL;
352 GdkImage *image = NULL;
353 GdkColormap *colormap;
357 gint width, height, num_cols, cpp, cnt, n, ns, xcnt, ycnt;
358 gchar *buffer = NULL, *color_name = NULL, pixel_str[32];
359 guint buffer_size = 0;
360 _GdkPixmapColor *colors = NULL, *color = NULL;
364 window = (GdkWindow*) &gdk_root_parent;
366 infile = fopen (filename, "rb");
369 if (gdk_pixmap_seek_string (infile, "XPM", FALSE) == TRUE)
371 if (gdk_pixmap_seek_char (infile,'{') == TRUE)
373 gdk_pixmap_seek_char (infile, '"');
374 fseek (infile, -1, SEEK_CUR);
375 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
377 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
379 colors = g_new(_GdkPixmapColor, num_cols);
381 colormap = gdk_window_get_colormap (window);
382 visual = gdk_window_get_visual (window);
384 if (transparent_color == NULL)
386 gdk_color_white (colormap, &tmp_color);
387 transparent_color = &tmp_color;
390 for (cnt = 0; cnt < num_cols; cnt++)
392 gdk_pixmap_seek_char (infile, '"');
393 fseek (infile, -1, SEEK_CUR);
394 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
396 colors[cnt].color_string = g_new(gchar, cpp + 1);
397 for (n = 0; n < cpp; n++)
398 colors[cnt].color_string[n] = buffer[n];
399 colors[cnt].color_string[n] = 0;
400 colors[cnt].transparent = FALSE;
402 if (color_name != NULL)
405 color_name = gdk_pixmap_extract_color (&buffer[cpp]);
407 if (color_name != NULL)
409 if (gdk_color_parse (color_name, &colors[cnt].color) == FALSE)
411 colors[cnt].color = *transparent_color;
412 colors[cnt].transparent = TRUE;
417 colors[cnt].color = *transparent_color;
418 colors[cnt].transparent = TRUE;
421 gdk_color_alloc (colormap, &colors[cnt].color);
425 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
430 *mask = gdk_pixmap_new (window, width, height, 1);
431 gc = gdk_gc_new (*mask);
433 gdk_color_black (colormap, &tmp_color);
434 gdk_gc_set_foreground (gc, &tmp_color);
435 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
437 gdk_color_white (colormap, &tmp_color);
438 gdk_gc_set_foreground (gc, &tmp_color);
441 for (ycnt = 0; ycnt < height; ycnt++)
443 gdk_pixmap_read_string (infile, &buffer, &buffer_size);
445 for (n = 0, cnt = 0, xcnt = 0; n < (width * cpp); n += cpp, xcnt++)
447 strncpy (pixel_str, &buffer[n], cpp);
452 while (color == NULL)
454 if (strcmp (pixel_str, colors[ns].color_string) == 0)
460 gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
462 if (mask && color->transparent)
465 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
470 if (mask && (cnt < xcnt))
471 gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt);
477 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
479 gc = gdk_gc_new (pixmap);
480 gdk_gc_set_foreground (gc, transparent_color);
481 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
483 gdk_image_destroy (image);
492 for (cnt = 0; cnt < num_cols; cnt++)
493 g_free (colors[cnt].color_string);
502 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
504 GdkColor *transparent_color,
507 GdkPixmap *pixmap = NULL;
508 GdkImage *image = NULL;
509 GdkColormap *colormap;
513 gint width, height, num_cols, cpp, cnt, n, ns, xcnt, ycnt, i;
514 gchar *buffer, *color_name = NULL, pixel_str[32];
515 _GdkPixmapColor *colors = NULL, *color = NULL;
519 window = (GdkWindow*) &gdk_root_parent;
523 sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp);
525 colors = g_new(_GdkPixmapColor, num_cols);
527 colormap = gdk_window_get_colormap (window);
528 visual = gdk_window_get_visual (window);
530 if (transparent_color == NULL)
532 gdk_color_white (colormap, &tmp_color);
533 transparent_color = &tmp_color;
536 for (cnt = 0; cnt < num_cols; cnt++)
540 colors[cnt].color_string = g_new(gchar, cpp + 1);
541 for (n = 0; n < cpp; n++)
542 colors[cnt].color_string[n] = buffer[n];
543 colors[cnt].color_string[n] = 0;
544 colors[cnt].transparent = FALSE;
546 if (color_name != NULL)
549 color_name = gdk_pixmap_extract_color (&buffer[cpp]);
551 if (color_name != NULL)
553 if (gdk_color_parse (color_name, &colors[cnt].color) == FALSE)
555 colors[cnt].color = *transparent_color;
556 colors[cnt].transparent = TRUE;
561 colors[cnt].color = *transparent_color;
562 colors[cnt].transparent = TRUE;
565 gdk_color_alloc (colormap, &colors[cnt].color);
569 image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height);
574 *mask = gdk_pixmap_new (window, width, height, 1);
575 gc = gdk_gc_new (*mask);
577 gdk_color_black (colormap, &tmp_color);
578 gdk_gc_set_foreground (gc, &tmp_color);
579 gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1);
581 gdk_color_white (colormap, &tmp_color);
582 gdk_gc_set_foreground (gc, &tmp_color);
585 for (ycnt = 0; ycnt < height; ycnt++)
589 for (n = 0, cnt = 0, xcnt = 0; n < (width * cpp); n += cpp, xcnt++)
591 strncpy (pixel_str, &buffer[n], cpp);
596 while (color == NULL)
598 if (strcmp (pixel_str, colors[ns].color_string) == 0)
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);
621 pixmap = gdk_pixmap_new (window, width, height, visual->depth);
623 gc = gdk_gc_new (pixmap);
624 gdk_gc_set_foreground (gc, transparent_color);
625 gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
627 gdk_image_destroy (image);
631 for (cnt = 0; cnt < num_cols; cnt++)
632 g_free (colors[cnt].color_string);
640 gdk_pixmap_destroy (GdkPixmap *pixmap)
642 GdkWindowPrivate *private;
644 g_return_if_fail (pixmap != NULL);
646 private = (GdkPixmapPrivate*) pixmap;
647 if (private->ref_count <= 0)
649 XFreePixmap (private->xdisplay, private->xwindow);
650 gdk_xid_table_remove (private->xwindow);
655 private->ref_count -= 1;