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