1 | /* GDK - The GIMP Drawing Kit |
---|
2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
---|
3 | * |
---|
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. |
---|
8 | * |
---|
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. |
---|
13 | * |
---|
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. |
---|
18 | */ |
---|
19 | |
---|
20 | /* |
---|
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/. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "config.h" |
---|
28 | #include <stdlib.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <string.h> |
---|
31 | /* Needed for SEEK_END in SunOS */ |
---|
32 | #include <unistd.h> |
---|
33 | #include <X11/Xlib.h> |
---|
34 | |
---|
35 | #include "gdk.h" |
---|
36 | #include "gdkprivate.h" |
---|
37 | |
---|
38 | typedef struct |
---|
39 | { |
---|
40 | gchar *color_string; |
---|
41 | GdkColor color; |
---|
42 | gint transparent; |
---|
43 | } _GdkPixmapColor; |
---|
44 | |
---|
45 | typedef struct |
---|
46 | { |
---|
47 | guint ncolors; |
---|
48 | GdkColormap *colormap; |
---|
49 | gulong pixels[1]; |
---|
50 | } _GdkPixmapInfo; |
---|
51 | |
---|
52 | GdkPixmap* |
---|
53 | gdk_pixmap_new (GdkWindow *window, |
---|
54 | gint width, |
---|
55 | gint height, |
---|
56 | gint depth) |
---|
57 | { |
---|
58 | GdkPixmap *pixmap; |
---|
59 | GdkWindowPrivate *private; |
---|
60 | GdkWindowPrivate *window_private; |
---|
61 | |
---|
62 | g_return_val_if_fail ((window != NULL) || (depth != -1), NULL); |
---|
63 | g_return_val_if_fail ((width != 0) && (height != 0), NULL); |
---|
64 | |
---|
65 | if (!window) |
---|
66 | window = (GdkWindow*) &gdk_root_parent; |
---|
67 | |
---|
68 | window_private = (GdkWindowPrivate*) window; |
---|
69 | if (window_private->destroyed) |
---|
70 | return NULL; |
---|
71 | |
---|
72 | if (depth == -1) |
---|
73 | depth = gdk_window_get_visual (window)->depth; |
---|
74 | |
---|
75 | private = g_new0 (GdkWindowPrivate, 1); |
---|
76 | pixmap = (GdkPixmap*) private; |
---|
77 | |
---|
78 | private->xdisplay = window_private->xdisplay; |
---|
79 | private->window_type = GDK_WINDOW_PIXMAP; |
---|
80 | private->xwindow = XCreatePixmap (private->xdisplay, window_private->xwindow, |
---|
81 | width, height, depth); |
---|
82 | private->colormap = NULL; |
---|
83 | private->parent = NULL; |
---|
84 | private->x = 0; |
---|
85 | private->y = 0; |
---|
86 | private->width = width; |
---|
87 | private->height = height; |
---|
88 | private->resize_count = 0; |
---|
89 | private->ref_count = 1; |
---|
90 | private->destroyed = 0; |
---|
91 | |
---|
92 | gdk_xid_table_insert (&private->xwindow, pixmap); |
---|
93 | |
---|
94 | return pixmap; |
---|
95 | } |
---|
96 | |
---|
97 | GdkPixmap * |
---|
98 | gdk_bitmap_create_from_data (GdkWindow *window, |
---|
99 | const gchar *data, |
---|
100 | gint width, |
---|
101 | gint height) |
---|
102 | { |
---|
103 | GdkPixmap *pixmap; |
---|
104 | GdkWindowPrivate *private; |
---|
105 | GdkWindowPrivate *window_private; |
---|
106 | |
---|
107 | g_return_val_if_fail (data != NULL, NULL); |
---|
108 | g_return_val_if_fail ((width != 0) && (height != 0), NULL); |
---|
109 | |
---|
110 | if (!window) |
---|
111 | window = (GdkWindow*) &gdk_root_parent; |
---|
112 | |
---|
113 | window_private = (GdkWindowPrivate*) window; |
---|
114 | if (window_private->destroyed) |
---|
115 | return NULL; |
---|
116 | |
---|
117 | private = g_new0 (GdkWindowPrivate, 1); |
---|
118 | pixmap = (GdkPixmap*) private; |
---|
119 | |
---|
120 | private->parent = NULL; |
---|
121 | private->xdisplay = window_private->xdisplay; |
---|
122 | private->window_type = GDK_WINDOW_PIXMAP; |
---|
123 | private->x = 0; |
---|
124 | private->y = 0; |
---|
125 | private->width = width; |
---|
126 | private->height = height; |
---|
127 | private->resize_count = 0; |
---|
128 | private->ref_count = 1; |
---|
129 | private->destroyed = FALSE; |
---|
130 | |
---|
131 | private->xwindow = XCreateBitmapFromData (private->xdisplay, |
---|
132 | window_private->xwindow, |
---|
133 | (char *)data, width, height); |
---|
134 | |
---|
135 | gdk_xid_table_insert (&private->xwindow, pixmap); |
---|
136 | |
---|
137 | return pixmap; |
---|
138 | } |
---|
139 | |
---|
140 | GdkPixmap* |
---|
141 | gdk_pixmap_create_from_data (GdkWindow *window, |
---|
142 | const gchar *data, |
---|
143 | gint width, |
---|
144 | gint height, |
---|
145 | gint depth, |
---|
146 | GdkColor *fg, |
---|
147 | GdkColor *bg) |
---|
148 | { |
---|
149 | GdkPixmap *pixmap; |
---|
150 | GdkWindowPrivate *private; |
---|
151 | GdkWindowPrivate *window_private; |
---|
152 | |
---|
153 | g_return_val_if_fail (data != NULL, NULL); |
---|
154 | g_return_val_if_fail (fg != NULL, NULL); |
---|
155 | g_return_val_if_fail (bg != NULL, NULL); |
---|
156 | g_return_val_if_fail ((window != NULL) || (depth != -1), NULL); |
---|
157 | g_return_val_if_fail ((width != 0) && (height != 0), NULL); |
---|
158 | |
---|
159 | if (!window) |
---|
160 | window = (GdkWindow*) &gdk_root_parent; |
---|
161 | |
---|
162 | window_private = (GdkWindowPrivate*) window; |
---|
163 | if (window_private->destroyed) |
---|
164 | return NULL; |
---|
165 | |
---|
166 | if (depth == -1) |
---|
167 | depth = gdk_window_get_visual (window)->depth; |
---|
168 | |
---|
169 | private = g_new0 (GdkWindowPrivate, 1); |
---|
170 | pixmap = (GdkPixmap*) private; |
---|
171 | |
---|
172 | private->parent = NULL; |
---|
173 | private->xdisplay = window_private->xdisplay; |
---|
174 | private->window_type = GDK_WINDOW_PIXMAP; |
---|
175 | private->x = 0; |
---|
176 | private->y = 0; |
---|
177 | private->width = width; |
---|
178 | private->height = height; |
---|
179 | private->resize_count = 0; |
---|
180 | private->ref_count = 1; |
---|
181 | private->destroyed = FALSE; |
---|
182 | |
---|
183 | private->xwindow = XCreatePixmapFromBitmapData (private->xdisplay, |
---|
184 | window_private->xwindow, |
---|
185 | (char *)data, width, height, |
---|
186 | fg->pixel, bg->pixel, depth); |
---|
187 | |
---|
188 | gdk_xid_table_insert (&private->xwindow, pixmap); |
---|
189 | |
---|
190 | return pixmap; |
---|
191 | } |
---|
192 | |
---|
193 | static gint |
---|
194 | gdk_pixmap_seek_string (FILE *infile, |
---|
195 | const gchar *str, |
---|
196 | gint skip_comments) |
---|
197 | { |
---|
198 | char instr[1024]; |
---|
199 | |
---|
200 | while (1) |
---|
201 | { |
---|
202 | if (fscanf (infile, "%1023s", instr) != 1) |
---|
203 | return FALSE; |
---|
204 | |
---|
205 | if (skip_comments == TRUE && strcmp (instr, "/*") == 0) |
---|
206 | { |
---|
207 | do |
---|
208 | { |
---|
209 | if (fscanf (infile, "%1023s", instr) != 1) |
---|
210 | return FALSE; |
---|
211 | } |
---|
212 | while (strcmp (instr, "*/") != 0); |
---|
213 | } |
---|
214 | else if (strcmp (instr, str) == 0) |
---|
215 | return TRUE; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | static gint |
---|
220 | gdk_pixmap_seek_char (FILE *infile, |
---|
221 | gchar c) |
---|
222 | { |
---|
223 | gint b, oldb; |
---|
224 | |
---|
225 | while ((b = getc(infile)) != EOF) |
---|
226 | { |
---|
227 | if (c != b && b == '/') |
---|
228 | { |
---|
229 | b = getc (infile); |
---|
230 | if (b == EOF) |
---|
231 | return FALSE; |
---|
232 | else if (b == '*') /* we have a comment */ |
---|
233 | { |
---|
234 | b = -1; |
---|
235 | do |
---|
236 | { |
---|
237 | oldb = b; |
---|
238 | b = getc (infile); |
---|
239 | if (b == EOF) |
---|
240 | return FALSE; |
---|
241 | } |
---|
242 | while (!(oldb == '*' && b == '/')); |
---|
243 | } |
---|
244 | } |
---|
245 | else if (c == b) |
---|
246 | return TRUE; |
---|
247 | } |
---|
248 | return FALSE; |
---|
249 | } |
---|
250 | |
---|
251 | static gint |
---|
252 | gdk_pixmap_read_string (FILE *infile, |
---|
253 | gchar **buffer, |
---|
254 | guint *buffer_size) |
---|
255 | { |
---|
256 | gint c; |
---|
257 | guint cnt = 0, bufsiz, ret = FALSE; |
---|
258 | gchar *buf; |
---|
259 | |
---|
260 | buf = *buffer; |
---|
261 | bufsiz = *buffer_size; |
---|
262 | if (buf == NULL) |
---|
263 | { |
---|
264 | bufsiz = 10 * sizeof (gchar); |
---|
265 | buf = g_new(gchar, bufsiz); |
---|
266 | } |
---|
267 | |
---|
268 | do |
---|
269 | c = getc (infile); |
---|
270 | while (c != EOF && c != '"'); |
---|
271 | |
---|
272 | if (c != '"') |
---|
273 | goto out; |
---|
274 | |
---|
275 | while ((c = getc(infile)) != EOF) |
---|
276 | { |
---|
277 | if (cnt == bufsiz) |
---|
278 | { |
---|
279 | guint new_size = bufsiz * 2; |
---|
280 | if (new_size > bufsiz) |
---|
281 | bufsiz = new_size; |
---|
282 | else |
---|
283 | goto out; |
---|
284 | |
---|
285 | buf = (gchar *) g_realloc (buf, bufsiz); |
---|
286 | buf[bufsiz-1] = '\0'; |
---|
287 | } |
---|
288 | |
---|
289 | if (c != '"') |
---|
290 | buf[cnt++] = c; |
---|
291 | else |
---|
292 | { |
---|
293 | buf[cnt] = 0; |
---|
294 | ret = TRUE; |
---|
295 | break; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | out: |
---|
300 | buf[bufsiz-1] = '\0'; /* ensure null termination for errors */ |
---|
301 | *buffer = buf; |
---|
302 | *buffer_size = bufsiz; |
---|
303 | return ret; |
---|
304 | } |
---|
305 | |
---|
306 | static gchar* |
---|
307 | gdk_pixmap_skip_whitespaces (gchar *buffer) |
---|
308 | { |
---|
309 | gint32 index = 0; |
---|
310 | |
---|
311 | while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09)) |
---|
312 | index++; |
---|
313 | |
---|
314 | return &buffer[index]; |
---|
315 | } |
---|
316 | |
---|
317 | static gchar* |
---|
318 | gdk_pixmap_skip_string (gchar *buffer) |
---|
319 | { |
---|
320 | gint32 index = 0; |
---|
321 | |
---|
322 | while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09) |
---|
323 | index++; |
---|
324 | |
---|
325 | return &buffer[index]; |
---|
326 | } |
---|
327 | |
---|
328 | /* Xlib crashed ince at a color name lengths around 125 */ |
---|
329 | #define MAX_COLOR_LEN 120 |
---|
330 | |
---|
331 | static gchar* |
---|
332 | gdk_pixmap_extract_color (gchar *buffer) |
---|
333 | { |
---|
334 | gint counter, numnames; |
---|
335 | gchar *ptr = NULL, ch, temp[128]; |
---|
336 | gchar color[MAX_COLOR_LEN], *retcol; |
---|
337 | gint space; |
---|
338 | |
---|
339 | counter = 0; |
---|
340 | while (ptr == NULL) |
---|
341 | { |
---|
342 | if (buffer[counter] == 'c') |
---|
343 | { |
---|
344 | ch = buffer[counter + 1]; |
---|
345 | if (ch == 0x20 || ch == 0x09) |
---|
346 | ptr = &buffer[counter + 1]; |
---|
347 | } |
---|
348 | else if (buffer[counter] == 0) |
---|
349 | return NULL; |
---|
350 | |
---|
351 | counter++; |
---|
352 | } |
---|
353 | |
---|
354 | ptr = gdk_pixmap_skip_whitespaces (ptr); |
---|
355 | |
---|
356 | if (ptr[0] == 0) |
---|
357 | return NULL; |
---|
358 | else if (ptr[0] == '#') |
---|
359 | { |
---|
360 | counter = 1; |
---|
361 | while (ptr[counter] != 0 && |
---|
362 | ((ptr[counter] >= '0' && ptr[counter] <= '9') || |
---|
363 | (ptr[counter] >= 'a' && ptr[counter] <= 'f') || |
---|
364 | (ptr[counter] >= 'A' && ptr[counter] <= 'F'))) |
---|
365 | counter++; |
---|
366 | |
---|
367 | retcol = g_new (gchar, counter+1); |
---|
368 | strncpy (retcol, ptr, counter); |
---|
369 | |
---|
370 | retcol[counter] = 0; |
---|
371 | |
---|
372 | return retcol; |
---|
373 | } |
---|
374 | |
---|
375 | color[0] = 0; |
---|
376 | numnames = 0; |
---|
377 | |
---|
378 | space = MAX_COLOR_LEN - 1; |
---|
379 | while (space > 0) |
---|
380 | { |
---|
381 | sscanf (ptr, "%127s", temp); |
---|
382 | |
---|
383 | if (((gint)ptr[0] == 0) || |
---|
384 | (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) || |
---|
385 | (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0)) |
---|
386 | { |
---|
387 | break; |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
391 | if (numnames > 0) |
---|
392 | { |
---|
393 | space -= 1; |
---|
394 | strcat (color, " "); |
---|
395 | } |
---|
396 | strncat (color, temp, space); |
---|
397 | space -= MIN (space, strlen (temp)); |
---|
398 | ptr = gdk_pixmap_skip_string (ptr); |
---|
399 | ptr = gdk_pixmap_skip_whitespaces (ptr); |
---|
400 | numnames++; |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | retcol = g_strdup (color); |
---|
405 | return retcol; |
---|
406 | } |
---|
407 | |
---|
408 | |
---|
409 | enum buffer_op |
---|
410 | { |
---|
411 | op_header, |
---|
412 | op_cmap, |
---|
413 | op_body |
---|
414 | }; |
---|
415 | |
---|
416 | |
---|
417 | static void |
---|
418 | gdk_xpm_destroy_notify (gpointer data) |
---|
419 | { |
---|
420 | _GdkPixmapInfo *info = (_GdkPixmapInfo *)data; |
---|
421 | GdkColor color; |
---|
422 | int i; |
---|
423 | |
---|
424 | for (i=0; i<info->ncolors; i++) |
---|
425 | { |
---|
426 | color.pixel = info->pixels[i]; |
---|
427 | gdk_colormap_free_colors (info->colormap, &color, 1); |
---|
428 | } |
---|
429 | |
---|
430 | gdk_colormap_unref (info->colormap); |
---|
431 | g_free (info); |
---|
432 | } |
---|
433 | |
---|
434 | static GdkPixmap * |
---|
435 | _gdk_pixmap_create_from_xpm (GdkWindow *window, |
---|
436 | GdkColormap *colormap, |
---|
437 | GdkBitmap **mask, |
---|
438 | GdkColor *transparent_color, |
---|
439 | gchar * (*get_buf) (enum buffer_op op, |
---|
440 | gpointer handle), |
---|
441 | gpointer handle) |
---|
442 | { |
---|
443 | GdkPixmap *pixmap = NULL; |
---|
444 | GdkImage *image = NULL; |
---|
445 | GdkVisual *visual; |
---|
446 | GdkGC *gc = NULL; |
---|
447 | GdkColor tmp_color; |
---|
448 | gint width, height, num_cols, cpp, n, ns, cnt, xcnt, ycnt, wbytes; |
---|
449 | gchar *buffer, pixel_str[32]; |
---|
450 | gchar *name_buf; |
---|
451 | _GdkPixmapColor *color = NULL, *fallbackcolor = NULL; |
---|
452 | _GdkPixmapColor *colors = NULL; |
---|
453 | gulong index; |
---|
454 | GHashTable *color_hash = NULL; |
---|
455 | _GdkPixmapInfo *color_info = NULL; |
---|
456 | |
---|
457 | if ((window == NULL) && (colormap == NULL)) |
---|
458 | g_warning ("Creating pixmap from xpm with NULL window and colormap"); |
---|
459 | |
---|
460 | if (window == NULL) |
---|
461 | window = (GdkWindow *)&gdk_root_parent; |
---|
462 | |
---|
463 | if (colormap == NULL) |
---|
464 | { |
---|
465 | colormap = gdk_window_get_colormap (window); |
---|
466 | visual = gdk_window_get_visual (window); |
---|
467 | } |
---|
468 | else |
---|
469 | visual = ((GdkColormapPrivate *)colormap)->visual; |
---|
470 | |
---|
471 | buffer = (*get_buf) (op_header, handle); |
---|
472 | if (buffer == NULL) |
---|
473 | return NULL; |
---|
474 | |
---|
475 | sscanf (buffer,"%d %d %d %d", &width, &height, &num_cols, &cpp); |
---|
476 | if (cpp >= 32) |
---|
477 | { |
---|
478 | g_warning ("Pixmap has more than 31 characters per color\n"); |
---|
479 | return NULL; |
---|
480 | } |
---|
481 | |
---|
482 | color_hash = g_hash_table_new (g_str_hash, g_str_equal); |
---|
483 | |
---|
484 | if (transparent_color == NULL) |
---|
485 | { |
---|
486 | gdk_color_white (colormap, &tmp_color); |
---|
487 | transparent_color = &tmp_color; |
---|
488 | } |
---|
489 | |
---|
490 | /* For pseudo-color and grayscale visuals, we have to remember |
---|
491 | * the colors we allocated, so we can free them later. |
---|
492 | */ |
---|
493 | if ((visual->type == GDK_VISUAL_PSEUDO_COLOR) || |
---|
494 | (visual->type == GDK_VISUAL_GRAYSCALE)) |
---|
495 | { |
---|
496 | color_info = g_malloc (sizeof (_GdkPixmapInfo) + |
---|
497 | sizeof(gulong) * (num_cols - 1)); |
---|
498 | color_info->ncolors = num_cols; |
---|
499 | color_info->colormap = colormap; |
---|
500 | gdk_colormap_ref (colormap); |
---|
501 | } |
---|
502 | |
---|
503 | name_buf = g_new (gchar, num_cols * (cpp+1)); |
---|
504 | colors = g_new (_GdkPixmapColor, num_cols); |
---|
505 | |
---|
506 | for (cnt = 0; cnt < num_cols; cnt++) |
---|
507 | { |
---|
508 | gchar *color_name; |
---|
509 | |
---|
510 | buffer = (*get_buf) (op_cmap, handle); |
---|
511 | if (buffer == NULL) |
---|
512 | goto error; |
---|
513 | |
---|
514 | color = &colors[cnt]; |
---|
515 | color->color_string = &name_buf [cnt * (cpp + 1)]; |
---|
516 | strncpy (color->color_string, buffer, cpp); |
---|
517 | color->color_string[cpp] = 0; |
---|
518 | buffer += strlen (color->color_string); |
---|
519 | color->transparent = FALSE; |
---|
520 | |
---|
521 | color_name = gdk_pixmap_extract_color (buffer); |
---|
522 | |
---|
523 | if (color_name == NULL || g_strcasecmp (color_name, "None") == 0 || |
---|
524 | gdk_color_parse (color_name, &color->color) == FALSE) |
---|
525 | { |
---|
526 | color->color = *transparent_color; |
---|
527 | color->transparent = TRUE; |
---|
528 | } |
---|
529 | |
---|
530 | g_free (color_name); |
---|
531 | |
---|
532 | /* FIXME: The remaining slowness appears to happen in this |
---|
533 | function. */ |
---|
534 | gdk_color_alloc (colormap, &color->color); |
---|
535 | |
---|
536 | if (color_info) |
---|
537 | color_info->pixels[cnt] = color->color.pixel; |
---|
538 | |
---|
539 | g_hash_table_insert (color_hash, color->color_string, color); |
---|
540 | if (cnt == 0) |
---|
541 | fallbackcolor = color; |
---|
542 | } |
---|
543 | |
---|
544 | index = 0; |
---|
545 | image = gdk_image_new (GDK_IMAGE_FASTEST, visual, width, height); |
---|
546 | |
---|
547 | if (mask) |
---|
548 | { |
---|
549 | /* The pixmap mask is just a bits pattern. |
---|
550 | * Color 0 is used for background and 1 for foreground. |
---|
551 | * We don't care about the colormap, we just need 0 and 1. |
---|
552 | */ |
---|
553 | GdkColor mask_pattern; |
---|
554 | |
---|
555 | *mask = gdk_pixmap_new (window, width, height, 1); |
---|
556 | gc = gdk_gc_new (*mask); |
---|
557 | |
---|
558 | mask_pattern.pixel = 0; |
---|
559 | gdk_gc_set_foreground (gc, &mask_pattern); |
---|
560 | gdk_draw_rectangle (*mask, gc, TRUE, 0, 0, -1, -1); |
---|
561 | |
---|
562 | mask_pattern.pixel = 1; |
---|
563 | gdk_gc_set_foreground (gc, &mask_pattern); |
---|
564 | } |
---|
565 | |
---|
566 | wbytes = width * cpp; |
---|
567 | for (ycnt = 0; ycnt < height; ycnt++) |
---|
568 | { |
---|
569 | buffer = (*get_buf) (op_body, handle); |
---|
570 | |
---|
571 | /* FIXME: this slows things down a little - it could be |
---|
572 | * integrated into the strncpy below, perhaps. OTOH, strlen |
---|
573 | * is fast. |
---|
574 | */ |
---|
575 | if ((buffer == NULL) || strlen (buffer) < wbytes) |
---|
576 | continue; |
---|
577 | |
---|
578 | for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) |
---|
579 | { |
---|
580 | strncpy (pixel_str, &buffer[n], cpp); |
---|
581 | pixel_str[cpp] = 0; |
---|
582 | ns = 0; |
---|
583 | |
---|
584 | color = g_hash_table_lookup (color_hash, pixel_str); |
---|
585 | |
---|
586 | if (!color) /* screwed up XPM file */ |
---|
587 | color = fallbackcolor; |
---|
588 | |
---|
589 | gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel); |
---|
590 | |
---|
591 | if (mask && color->transparent) |
---|
592 | { |
---|
593 | if (cnt < xcnt) |
---|
594 | gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt); |
---|
595 | cnt = xcnt + 1; |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | if (mask && (cnt < xcnt)) |
---|
600 | gdk_draw_line (*mask, gc, cnt, ycnt, xcnt - 1, ycnt); |
---|
601 | } |
---|
602 | |
---|
603 | error: |
---|
604 | |
---|
605 | if (mask) |
---|
606 | gdk_gc_destroy (gc); |
---|
607 | |
---|
608 | if (image != NULL) |
---|
609 | { |
---|
610 | pixmap = gdk_pixmap_new (window, width, height, visual->depth); |
---|
611 | |
---|
612 | if (color_info) |
---|
613 | gdk_drawable_set_data (pixmap, "gdk-xpm", color_info, |
---|
614 | gdk_xpm_destroy_notify); |
---|
615 | |
---|
616 | gc = gdk_gc_new (pixmap); |
---|
617 | gdk_gc_set_foreground (gc, transparent_color); |
---|
618 | gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height); |
---|
619 | gdk_gc_destroy (gc); |
---|
620 | gdk_image_destroy (image); |
---|
621 | } |
---|
622 | else if (color_info) |
---|
623 | gdk_xpm_destroy_notify (color_info); |
---|
624 | |
---|
625 | if (color_hash != NULL) |
---|
626 | g_hash_table_destroy (color_hash); |
---|
627 | |
---|
628 | if (colors != NULL) |
---|
629 | g_free (colors); |
---|
630 | |
---|
631 | if (name_buf != NULL) |
---|
632 | g_free (name_buf); |
---|
633 | |
---|
634 | return pixmap; |
---|
635 | } |
---|
636 | |
---|
637 | |
---|
638 | struct file_handle |
---|
639 | { |
---|
640 | FILE *infile; |
---|
641 | gchar *buffer; |
---|
642 | guint buffer_size; |
---|
643 | }; |
---|
644 | |
---|
645 | |
---|
646 | static gchar * |
---|
647 | file_buffer (enum buffer_op op, gpointer handle) |
---|
648 | { |
---|
649 | struct file_handle *h = handle; |
---|
650 | |
---|
651 | switch (op) |
---|
652 | { |
---|
653 | case op_header: |
---|
654 | if (gdk_pixmap_seek_string (h->infile, "XPM", FALSE) != TRUE) |
---|
655 | break; |
---|
656 | |
---|
657 | if (gdk_pixmap_seek_char (h->infile,'{') != TRUE) |
---|
658 | break; |
---|
659 | /* Fall through to the next gdk_pixmap_seek_char. */ |
---|
660 | |
---|
661 | case op_cmap: |
---|
662 | gdk_pixmap_seek_char (h->infile, '"'); |
---|
663 | fseek (h->infile, -1, SEEK_CUR); |
---|
664 | /* Fall through to the gdk_pixmap_read_string. */ |
---|
665 | |
---|
666 | case op_body: |
---|
667 | gdk_pixmap_read_string (h->infile, &h->buffer, &h->buffer_size); |
---|
668 | return h->buffer; |
---|
669 | } |
---|
670 | return 0; |
---|
671 | } |
---|
672 | |
---|
673 | |
---|
674 | GdkPixmap* |
---|
675 | gdk_pixmap_colormap_create_from_xpm (GdkWindow *window, |
---|
676 | GdkColormap *colormap, |
---|
677 | GdkBitmap **mask, |
---|
678 | GdkColor *transparent_color, |
---|
679 | const gchar *filename) |
---|
680 | { |
---|
681 | struct file_handle h; |
---|
682 | GdkPixmap *pixmap = NULL; |
---|
683 | |
---|
684 | memset (&h, 0, sizeof (h)); |
---|
685 | h.infile = fopen (filename, "rb"); |
---|
686 | if (h.infile != NULL) |
---|
687 | { |
---|
688 | pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask, |
---|
689 | transparent_color, |
---|
690 | file_buffer, &h); |
---|
691 | fclose (h.infile); |
---|
692 | g_free (h.buffer); |
---|
693 | } |
---|
694 | |
---|
695 | return pixmap; |
---|
696 | } |
---|
697 | |
---|
698 | GdkPixmap* |
---|
699 | gdk_pixmap_create_from_xpm (GdkWindow *window, |
---|
700 | GdkBitmap **mask, |
---|
701 | GdkColor *transparent_color, |
---|
702 | const gchar *filename) |
---|
703 | { |
---|
704 | return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask, |
---|
705 | transparent_color, filename); |
---|
706 | } |
---|
707 | |
---|
708 | |
---|
709 | struct mem_handle |
---|
710 | { |
---|
711 | gchar **data; |
---|
712 | int offset; |
---|
713 | }; |
---|
714 | |
---|
715 | |
---|
716 | static gchar * |
---|
717 | mem_buffer (enum buffer_op op, gpointer handle) |
---|
718 | { |
---|
719 | struct mem_handle *h = handle; |
---|
720 | switch (op) |
---|
721 | { |
---|
722 | case op_header: |
---|
723 | case op_cmap: |
---|
724 | case op_body: |
---|
725 | if (h->data[h->offset]) |
---|
726 | return h->data[h->offset ++]; |
---|
727 | } |
---|
728 | return 0; |
---|
729 | } |
---|
730 | |
---|
731 | |
---|
732 | GdkPixmap* |
---|
733 | gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window, |
---|
734 | GdkColormap *colormap, |
---|
735 | GdkBitmap **mask, |
---|
736 | GdkColor *transparent_color, |
---|
737 | gchar **data) |
---|
738 | { |
---|
739 | struct mem_handle h; |
---|
740 | GdkPixmap *pixmap = NULL; |
---|
741 | |
---|
742 | memset (&h, 0, sizeof (h)); |
---|
743 | h.data = data; |
---|
744 | pixmap = _gdk_pixmap_create_from_xpm (window, colormap, mask, |
---|
745 | transparent_color, |
---|
746 | mem_buffer, &h); |
---|
747 | return pixmap; |
---|
748 | } |
---|
749 | |
---|
750 | |
---|
751 | GdkPixmap* |
---|
752 | gdk_pixmap_create_from_xpm_d (GdkWindow *window, |
---|
753 | GdkBitmap **mask, |
---|
754 | GdkColor *transparent_color, |
---|
755 | gchar **data) |
---|
756 | { |
---|
757 | return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask, |
---|
758 | transparent_color, data); |
---|
759 | } |
---|
760 | |
---|
761 | GdkPixmap* |
---|
762 | gdk_pixmap_foreign_new (guint32 anid) |
---|
763 | { |
---|
764 | GdkPixmap *pixmap; |
---|
765 | GdkWindowPrivate *window_private; |
---|
766 | GdkWindowPrivate *private; |
---|
767 | Pixmap xpixmap; |
---|
768 | Window root_return; |
---|
769 | unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret; |
---|
770 | |
---|
771 | /* check to make sure we were passed something at |
---|
772 | least a little sane */ |
---|
773 | g_return_val_if_fail((anid != 0), NULL); |
---|
774 | |
---|
775 | /* set the pixmap to the passed in value */ |
---|
776 | xpixmap = anid; |
---|
777 | /* get the root window */ |
---|
778 | window_private = &gdk_root_parent; |
---|
779 | |
---|
780 | /* get information about the Pixmap to fill in the structure for |
---|
781 | the gdk window */ |
---|
782 | if (!XGetGeometry(window_private->xdisplay, xpixmap, &root_return, |
---|
783 | &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret)) |
---|
784 | return NULL; |
---|
785 | |
---|
786 | /* allocate a new gdk pixmap */ |
---|
787 | private = g_new(GdkWindowPrivate, 1); |
---|
788 | pixmap = (GdkPixmap *)private; |
---|
789 | |
---|
790 | private->xdisplay = window_private->xdisplay; |
---|
791 | private->window_type = GDK_WINDOW_PIXMAP; |
---|
792 | private->xwindow = xpixmap; |
---|
793 | private->colormap = NULL; |
---|
794 | private->parent = NULL; |
---|
795 | private->x = 0; |
---|
796 | private->y = 0; |
---|
797 | private->width = w_ret; |
---|
798 | private->height = h_ret; |
---|
799 | private->resize_count = 0; |
---|
800 | private->ref_count = 1; |
---|
801 | private->destroyed = 0; |
---|
802 | |
---|
803 | gdk_xid_table_insert(&private->xwindow, pixmap); |
---|
804 | |
---|
805 | return pixmap; |
---|
806 | } |
---|
807 | |
---|
808 | GdkPixmap* |
---|
809 | gdk_pixmap_ref (GdkPixmap *pixmap) |
---|
810 | { |
---|
811 | GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap; |
---|
812 | g_return_val_if_fail (pixmap != NULL, NULL); |
---|
813 | |
---|
814 | private->ref_count += 1; |
---|
815 | return pixmap; |
---|
816 | } |
---|
817 | |
---|
818 | void |
---|
819 | gdk_pixmap_unref (GdkPixmap *pixmap) |
---|
820 | { |
---|
821 | GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap; |
---|
822 | g_return_if_fail (pixmap != NULL); |
---|
823 | g_return_if_fail (private->ref_count > 0); |
---|
824 | |
---|
825 | private->ref_count -= 1; |
---|
826 | if (private->ref_count == 0) |
---|
827 | { |
---|
828 | XFreePixmap (private->xdisplay, private->xwindow); |
---|
829 | gdk_xid_table_remove (private->xwindow); |
---|
830 | g_dataset_destroy (private); |
---|
831 | g_free (private); |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | GdkBitmap * |
---|
836 | gdk_bitmap_ref (GdkBitmap *bitmap) |
---|
837 | { |
---|
838 | return (GdkBitmap *)gdk_pixmap_ref ((GdkPixmap *)bitmap); |
---|
839 | } |
---|
840 | |
---|
841 | void |
---|
842 | gdk_bitmap_unref (GdkBitmap *bitmap) |
---|
843 | { |
---|
844 | gdk_pixmap_unref ((GdkPixmap *)bitmap); |
---|
845 | } |
---|