]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixmap.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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-2000.  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 "gdkpixmap.h"
28 #include "gdkinternals.h"
29 #include "gdkpixbuf.h"
30
31 static GdkGC *gdk_pixmap_create_gc      (GdkDrawable     *drawable,
32                                          GdkGCValues     *values,
33                                          GdkGCValuesMask  mask);
34 static void   gdk_pixmap_draw_rectangle (GdkDrawable     *drawable,
35                                          GdkGC           *gc,
36                                          gint             filled,
37                                          gint             x,
38                                          gint             y,
39                                          gint             width,
40                                          gint             height);
41 static void   gdk_pixmap_draw_arc       (GdkDrawable     *drawable,
42                                          GdkGC           *gc,
43                                          gint             filled,
44                                          gint             x,
45                                          gint             y,
46                                          gint             width,
47                                          gint             height,
48                                          gint             angle1,
49                                          gint             angle2);
50 static void   gdk_pixmap_draw_polygon   (GdkDrawable     *drawable,
51                                          GdkGC           *gc,
52                                          gint             filled,
53                                          GdkPoint        *points,
54                                          gint             npoints);
55 static void   gdk_pixmap_draw_text      (GdkDrawable     *drawable,
56                                          GdkFont         *font,
57                                          GdkGC           *gc,
58                                          gint             x,
59                                          gint             y,
60                                          const gchar     *text,
61                                          gint             text_length);
62 static void   gdk_pixmap_draw_text_wc   (GdkDrawable     *drawable,
63                                          GdkFont         *font,
64                                          GdkGC           *gc,
65                                          gint             x,
66                                          gint             y,
67                                          const GdkWChar  *text,
68                                          gint             text_length);
69 static void   gdk_pixmap_draw_drawable  (GdkDrawable     *drawable,
70                                          GdkGC           *gc,
71                                          GdkPixmap       *src,
72                                          gint             xsrc,
73                                          gint             ysrc,
74                                          gint             xdest,
75                                          gint             ydest,
76                                          gint             width,
77                                          gint             height);
78 static void   gdk_pixmap_draw_points    (GdkDrawable     *drawable,
79                                          GdkGC           *gc,
80                                          GdkPoint        *points,
81                                          gint             npoints);
82 static void   gdk_pixmap_draw_segments  (GdkDrawable     *drawable,
83                                          GdkGC           *gc,
84                                          GdkSegment      *segs,
85                                          gint             nsegs);
86 static void   gdk_pixmap_draw_lines     (GdkDrawable     *drawable,
87                                          GdkGC           *gc,
88                                          GdkPoint        *points,
89                                          gint             npoints);
90 static void   gdk_pixmap_draw_glyphs    (GdkDrawable      *drawable,
91                                          GdkGC            *gc,
92                                          PangoFont        *font,
93                                          gint              x,
94                                          gint              y,
95                                          PangoGlyphString *glyphs);
96 static void   gdk_pixmap_draw_image     (GdkDrawable     *drawable,
97                                          GdkGC           *gc,
98                                          GdkImage        *image,
99                                          gint             xsrc,
100                                          gint             ysrc,
101                                          gint             xdest,
102                                          gint             ydest,
103                                          gint             width,
104                                          gint             height);
105
106 static void   gdk_pixmap_real_get_size  (GdkDrawable     *drawable,
107                                          gint            *width,
108                                          gint            *height);
109
110 static GdkVisual*   gdk_pixmap_real_get_visual   (GdkDrawable *drawable);
111 static gint         gdk_pixmap_real_get_depth    (GdkDrawable *drawable);
112 static void         gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
113                                                   GdkColormap *cmap);
114 static GdkColormap* gdk_pixmap_real_get_colormap (GdkDrawable *drawable);
115
116 static void gdk_pixmap_init       (GdkPixmapObject      *pixmap);
117 static void gdk_pixmap_class_init (GdkPixmapObjectClass *klass);
118 static void gdk_pixmap_finalize   (GObject              *object);
119
120 static gpointer parent_class = NULL;
121
122 GType
123 gdk_pixmap_get_type (void)
124 {
125   static GType object_type = 0;
126
127   if (!object_type)
128     {
129       static const GTypeInfo object_info =
130       {
131         sizeof (GdkPixmapObjectClass),
132         (GBaseInitFunc) NULL,
133         (GBaseFinalizeFunc) NULL,
134         (GClassInitFunc) gdk_pixmap_class_init,
135         NULL,           /* class_finalize */
136         NULL,           /* class_data */
137         sizeof (GdkPixmapObject),
138         0,              /* n_preallocs */
139         (GInstanceInitFunc) gdk_pixmap_init,
140       };
141       
142       object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
143                                             "GdkPixmap",
144                                             &object_info);
145     }
146   
147   return object_type;
148 }
149
150 static void
151 gdk_pixmap_init (GdkPixmapObject *pixmap)
152 {
153   /* 0-initialization is good for all other fields. */
154   pixmap->impl = g_object_new (_gdk_pixmap_impl_get_type (), NULL);
155 }
156
157 static void
158 gdk_pixmap_class_init (GdkPixmapObjectClass *klass)
159 {
160   GObjectClass *object_class = G_OBJECT_CLASS (klass);
161   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
162   
163   parent_class = g_type_class_peek_parent (klass);
164
165   object_class->finalize = gdk_pixmap_finalize;
166
167   drawable_class->create_gc = gdk_pixmap_create_gc;
168   drawable_class->draw_rectangle = gdk_pixmap_draw_rectangle;
169   drawable_class->draw_arc = gdk_pixmap_draw_arc;
170   drawable_class->draw_polygon = gdk_pixmap_draw_polygon;
171   drawable_class->draw_text = gdk_pixmap_draw_text;
172   drawable_class->draw_text_wc = gdk_pixmap_draw_text_wc;
173   drawable_class->draw_drawable = gdk_pixmap_draw_drawable;
174   drawable_class->draw_points = gdk_pixmap_draw_points;
175   drawable_class->draw_segments = gdk_pixmap_draw_segments;
176   drawable_class->draw_lines = gdk_pixmap_draw_lines;
177   drawable_class->draw_glyphs = gdk_pixmap_draw_glyphs;
178   drawable_class->draw_image = gdk_pixmap_draw_image;
179   drawable_class->get_depth = gdk_pixmap_real_get_depth;
180   drawable_class->get_size = gdk_pixmap_real_get_size;
181   drawable_class->set_colormap = gdk_pixmap_real_set_colormap;
182   drawable_class->get_colormap = gdk_pixmap_real_get_colormap;
183   drawable_class->get_visual = gdk_pixmap_real_get_visual;
184 }
185
186 static void
187 gdk_pixmap_finalize (GObject *object)
188 {
189   GdkPixmapObject *obj = (GdkPixmapObject *) object;
190
191   g_object_unref (G_OBJECT (obj->impl));
192   obj->impl = NULL;
193   
194   G_OBJECT_CLASS (parent_class)->finalize (object);
195 }
196
197 static GdkGC *
198 gdk_pixmap_create_gc (GdkDrawable     *drawable,
199                       GdkGCValues     *values,
200                       GdkGCValuesMask  mask)
201 {
202   return gdk_gc_new_with_values (((GdkPixmapObject *) drawable)->impl,
203                                  values, mask);
204 }
205
206 static void
207 gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
208                            GdkGC       *gc,
209                            gint         filled,
210                            gint         x,
211                            gint         y,
212                            gint         width,
213                            gint         height)
214 {
215   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
216
217   gdk_draw_rectangle (private->impl, gc, filled,
218                       x, y, width, height);
219 }
220
221 static void
222 gdk_pixmap_draw_arc (GdkDrawable *drawable,
223                      GdkGC       *gc,
224                      gint         filled,
225                      gint         x,
226                      gint         y,
227                      gint         width,
228                      gint         height,
229                      gint         angle1,
230                      gint         angle2)
231 {
232   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
233
234   gdk_draw_arc (private->impl, gc, filled,
235                 x, y,
236                 width, height, angle1, angle2);
237 }
238
239 static void
240 gdk_pixmap_draw_polygon (GdkDrawable *drawable,
241                          GdkGC       *gc,
242                          gint         filled,
243                          GdkPoint    *points,
244                          gint         npoints)
245 {
246   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
247
248   gdk_draw_polygon (private->impl, gc, filled, points, npoints);
249 }
250
251 static void
252 gdk_pixmap_draw_text (GdkDrawable *drawable,
253                       GdkFont     *font,
254                       GdkGC       *gc,
255                       gint         x,
256                       gint         y,
257                       const gchar *text,
258                       gint         text_length)
259 {
260   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
261
262   gdk_draw_text (private->impl, font, gc,
263                  x, y, text, text_length);
264 }
265
266 static void
267 gdk_pixmap_draw_text_wc (GdkDrawable    *drawable,
268                          GdkFont        *font,
269                          GdkGC          *gc,
270                          gint            x,
271                          gint            y,
272                          const GdkWChar *text,
273                          gint            text_length)
274 {
275   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
276
277   gdk_draw_text_wc (private->impl, font, gc,
278                     x, y, text, text_length);
279 }
280
281 static void
282 gdk_pixmap_draw_drawable (GdkDrawable *drawable,
283                           GdkGC       *gc,
284                           GdkPixmap   *src,
285                           gint         xsrc,
286                           gint         ysrc,
287                           gint         xdest,
288                           gint         ydest,
289                           gint         width,
290                           gint         height)
291 {
292   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
293
294   gdk_draw_drawable (private->impl, gc, src, xsrc, ysrc,
295                      xdest, ydest,
296                      width, height);
297 }
298
299 static void
300 gdk_pixmap_draw_points (GdkDrawable *drawable,
301                         GdkGC       *gc,
302                         GdkPoint    *points,
303                         gint         npoints)
304 {
305   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
306
307   gdk_draw_points (private->impl, gc, points, npoints);
308 }
309
310 static void
311 gdk_pixmap_draw_segments (GdkDrawable *drawable,
312                           GdkGC       *gc,
313                           GdkSegment  *segs,
314                           gint         nsegs)
315 {
316   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
317
318   gdk_draw_segments (private->impl, gc, segs, nsegs);
319 }
320
321 static void
322 gdk_pixmap_draw_lines (GdkDrawable *drawable,
323                        GdkGC       *gc,
324                        GdkPoint    *points,
325                        gint         npoints)
326 {
327   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
328
329   gdk_draw_lines (private->impl, gc, points, npoints);
330 }
331
332 static void
333 gdk_pixmap_draw_glyphs (GdkDrawable      *drawable,
334                         GdkGC            *gc,
335                         PangoFont        *font,
336                         gint              x,
337                         gint              y,
338                         PangoGlyphString *glyphs)
339 {
340   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
341
342   gdk_draw_glyphs (private->impl, gc, font, x, y, glyphs);
343 }
344
345 static void
346 gdk_pixmap_draw_image (GdkDrawable     *drawable,
347                        GdkGC           *gc,
348                        GdkImage        *image,
349                        gint             xsrc,
350                        gint             ysrc,
351                        gint             xdest,
352                        gint             ydest,
353                        gint             width,
354                        gint             height)
355 {
356   GdkPixmapObject *private = (GdkPixmapObject *)drawable;
357
358   gdk_draw_image (private->impl, gc, image, xsrc, ysrc, xdest, ydest,
359                   width, height);
360 }
361
362 static void
363 gdk_pixmap_real_get_size (GdkDrawable *drawable,
364                           gint *width,
365                           gint *height)
366 {
367   g_return_if_fail (GDK_IS_PIXMAP (drawable));
368
369   gdk_drawable_get_size (GDK_DRAWABLE (((GdkPixmapObject*)drawable)->impl),
370                          width, height);
371 }
372
373 static GdkVisual*
374 gdk_pixmap_real_get_visual (GdkDrawable *drawable)
375 {
376   GdkColormap *colormap;
377
378   g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
379
380   colormap = gdk_drawable_get_colormap (drawable);
381   return colormap ? gdk_colormap_get_visual (colormap) : NULL;
382 }
383
384 static gint
385 gdk_pixmap_real_get_depth (GdkDrawable *drawable)
386 {
387   gint depth;
388   
389   g_return_val_if_fail (GDK_IS_PIXMAP (drawable), 0);
390
391   depth = GDK_PIXMAP_OBJECT (drawable)->depth;
392
393   return depth;
394 }
395
396 static void
397 gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
398                               GdkColormap *cmap)
399 {
400   g_return_if_fail (GDK_IS_PIXMAP (drawable));  
401   
402   gdk_drawable_set_colormap (((GdkPixmapObject*)drawable)->impl, cmap);
403 }
404
405 static GdkColormap*
406 gdk_pixmap_real_get_colormap (GdkDrawable *drawable)
407 {
408   g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
409   
410   return gdk_drawable_get_colormap (((GdkPixmapObject*)drawable)->impl);
411 }
412
413 #define PACKED_COLOR(c) ((((c)->red & 0xff) << 8) | ((c)->green & 0xff) | ((c)->blue >> 8))
414
415 static GdkPixmap *
416 gdk_pixmap_colormap_new_from_pixbuf (GdkColormap *colormap,
417                                      GdkBitmap  **mask,
418                                      GdkColor    *transparent_color,
419                                      GdkPixbuf   *pixbuf)
420 {
421   GdkPixmap *pixmap;
422   GdkPixbuf *render_pixbuf;
423   GdkGC *tmp_gc;
424   
425   pixmap = gdk_pixmap_new (NULL,
426                            gdk_pixbuf_get_width (pixbuf),
427                            gdk_pixbuf_get_height (pixbuf),
428                            gdk_colormap_get_visual (colormap)->depth);
429   gdk_drawable_set_colormap (pixmap, colormap);
430   
431   if (transparent_color)
432     {
433       guint32 packed_color = PACKED_COLOR (transparent_color);
434       render_pixbuf = gdk_pixbuf_composite_color_simple (pixbuf,
435                                                          gdk_pixbuf_get_width (pixbuf),
436                                                          gdk_pixbuf_get_height (pixbuf),
437                                                          GDK_INTERP_NEAREST,
438                                                          255, 16, packed_color, packed_color);
439     }
440   else
441     render_pixbuf = pixbuf;
442
443   tmp_gc = gdk_gc_new (pixmap);
444   gdk_pixbuf_render_to_drawable (render_pixbuf, pixmap, tmp_gc, 0, 0, 0, 0,
445                                  gdk_pixbuf_get_width (render_pixbuf),
446                                  gdk_pixbuf_get_height (render_pixbuf),
447                                  GDK_RGB_DITHER_NORMAL, 0, 0);
448   gdk_gc_unref (tmp_gc);
449
450   if (render_pixbuf != pixbuf)
451     gdk_pixbuf_unref (render_pixbuf);
452
453   if (mask)
454     gdk_pixbuf_render_pixmap_and_mask (pixbuf, NULL, mask, 128);
455
456   return pixmap;
457 }
458
459 GdkPixmap*
460 gdk_pixmap_colormap_create_from_xpm (GdkWindow   *window,
461                                      GdkColormap *colormap,
462                                      GdkBitmap  **mask,
463                                      GdkColor    *transparent_color,
464                                      const gchar *filename)
465 {
466   GdkPixbuf *pixbuf;
467   GdkPixmap *pixmap;
468
469   g_return_val_if_fail (window != NULL || colormap != NULL, NULL);
470   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
471   g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
472
473   if (colormap == NULL)
474     colormap = gdk_drawable_get_colormap (window);
475   
476   pixbuf = gdk_pixbuf_new_from_file (filename);
477   if (!pixbuf)
478     return NULL;
479
480   pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
481   gdk_pixbuf_unref (pixbuf);
482
483   return pixmap;
484 }
485
486 GdkPixmap*
487 gdk_pixmap_create_from_xpm (GdkWindow  *window,
488                             GdkBitmap **mask,
489                             GdkColor   *transparent_color,
490                             const gchar *filename)
491 {
492   return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
493                                               transparent_color, filename);
494 }
495
496 GdkPixmap*
497 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow  *window,
498                                        GdkColormap *colormap,
499                                        GdkBitmap **mask,
500                                        GdkColor   *transparent_color,
501                                        gchar     **data)
502 {
503   GdkPixbuf *pixbuf;
504   GdkPixmap *pixmap;
505
506   g_return_val_if_fail (window != NULL || colormap != NULL, NULL);
507   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
508   g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
509
510   if (colormap == NULL)
511     colormap = gdk_drawable_get_colormap (window);
512   
513   pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)data);
514   if (!pixbuf)
515     return NULL;
516
517   pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
518   gdk_pixbuf_unref (pixbuf);
519
520   return pixmap;
521 }
522
523 GdkPixmap*
524 gdk_pixmap_create_from_xpm_d (GdkWindow  *window,
525                               GdkBitmap **mask,
526                               GdkColor   *transparent_color,
527                               gchar     **data)
528 {
529   return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
530                                                 transparent_color, data);
531 }
532