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