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