1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gdkpixmap.h"
28 #include "gdkinternals.h"
29 #include "gdkpixbuf.h"
31 static GdkGC *gdk_pixmap_create_gc (GdkDrawable *drawable,
33 GdkGCValuesMask mask);
34 static void gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
41 static void gdk_pixmap_draw_arc (GdkDrawable *drawable,
50 static void gdk_pixmap_draw_polygon (GdkDrawable *drawable,
55 static void gdk_pixmap_draw_text (GdkDrawable *drawable,
62 static void gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
69 static void gdk_pixmap_draw_drawable (GdkDrawable *drawable,
78 static void gdk_pixmap_draw_points (GdkDrawable *drawable,
82 static void gdk_pixmap_draw_segments (GdkDrawable *drawable,
86 static void gdk_pixmap_draw_lines (GdkDrawable *drawable,
90 static void gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
95 PangoGlyphString *glyphs);
96 static void gdk_pixmap_draw_image (GdkDrawable *drawable,
105 static void gdk_pixmap_draw_pixbuf (GdkDrawable *drawable,
119 static void gdk_pixmap_real_get_size (GdkDrawable *drawable,
123 static GdkImage* gdk_pixmap_copy_to_image (GdkDrawable *drawable,
132 static GdkVisual* gdk_pixmap_real_get_visual (GdkDrawable *drawable);
133 static gint gdk_pixmap_real_get_depth (GdkDrawable *drawable);
134 static void gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
136 static GdkColormap* gdk_pixmap_real_get_colormap (GdkDrawable *drawable);
138 static void gdk_pixmap_init (GdkPixmapObject *pixmap);
139 static void gdk_pixmap_class_init (GdkPixmapObjectClass *klass);
140 static void gdk_pixmap_finalize (GObject *object);
142 static gpointer parent_class = NULL;
145 gdk_pixmap_get_type (void)
147 static GType object_type = 0;
151 static const GTypeInfo object_info =
153 sizeof (GdkPixmapObjectClass),
154 (GBaseInitFunc) NULL,
155 (GBaseFinalizeFunc) NULL,
156 (GClassInitFunc) gdk_pixmap_class_init,
157 NULL, /* class_finalize */
158 NULL, /* class_data */
159 sizeof (GdkPixmapObject),
161 (GInstanceInitFunc) gdk_pixmap_init,
164 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
173 gdk_pixmap_init (GdkPixmapObject *pixmap)
175 /* 0-initialization is good for all other fields. */
176 pixmap->impl = g_object_new (_gdk_pixmap_impl_get_type (), NULL);
180 gdk_pixmap_class_init (GdkPixmapObjectClass *klass)
182 GObjectClass *object_class = G_OBJECT_CLASS (klass);
183 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
185 parent_class = g_type_class_peek_parent (klass);
187 object_class->finalize = gdk_pixmap_finalize;
189 drawable_class->create_gc = gdk_pixmap_create_gc;
190 drawable_class->draw_rectangle = gdk_pixmap_draw_rectangle;
191 drawable_class->draw_arc = gdk_pixmap_draw_arc;
192 drawable_class->draw_polygon = gdk_pixmap_draw_polygon;
193 drawable_class->draw_text = gdk_pixmap_draw_text;
194 drawable_class->draw_text_wc = gdk_pixmap_draw_text_wc;
195 drawable_class->draw_drawable = gdk_pixmap_draw_drawable;
196 drawable_class->draw_points = gdk_pixmap_draw_points;
197 drawable_class->draw_segments = gdk_pixmap_draw_segments;
198 drawable_class->draw_lines = gdk_pixmap_draw_lines;
199 drawable_class->draw_glyphs = gdk_pixmap_draw_glyphs;
200 drawable_class->draw_image = gdk_pixmap_draw_image;
201 drawable_class->_draw_pixbuf = gdk_pixmap_draw_pixbuf;
202 drawable_class->get_depth = gdk_pixmap_real_get_depth;
203 drawable_class->get_size = gdk_pixmap_real_get_size;
204 drawable_class->set_colormap = gdk_pixmap_real_set_colormap;
205 drawable_class->get_colormap = gdk_pixmap_real_get_colormap;
206 drawable_class->get_visual = gdk_pixmap_real_get_visual;
207 drawable_class->_copy_to_image = gdk_pixmap_copy_to_image;
211 gdk_pixmap_finalize (GObject *object)
213 GdkPixmapObject *obj = (GdkPixmapObject *) object;
215 g_object_unref (G_OBJECT (obj->impl));
218 G_OBJECT_CLASS (parent_class)->finalize (object);
222 gdk_pixmap_create_gc (GdkDrawable *drawable,
224 GdkGCValuesMask mask)
226 return gdk_gc_new_with_values (((GdkPixmapObject *) drawable)->impl,
231 gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
239 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
241 gdk_draw_rectangle (private->impl, gc, filled,
242 x, y, width, height);
246 gdk_pixmap_draw_arc (GdkDrawable *drawable,
256 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
258 gdk_draw_arc (private->impl, gc, filled,
260 width, height, angle1, angle2);
264 gdk_pixmap_draw_polygon (GdkDrawable *drawable,
270 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
272 gdk_draw_polygon (private->impl, gc, filled, points, npoints);
276 gdk_pixmap_draw_text (GdkDrawable *drawable,
284 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
286 gdk_draw_text (private->impl, font, gc,
287 x, y, text, text_length);
291 gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
296 const GdkWChar *text,
299 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
301 gdk_draw_text_wc (private->impl, font, gc,
302 x, y, text, text_length);
306 gdk_pixmap_draw_drawable (GdkDrawable *drawable,
316 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
318 gdk_draw_drawable (private->impl, gc, src, xsrc, ysrc,
324 gdk_pixmap_draw_points (GdkDrawable *drawable,
329 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
331 gdk_draw_points (private->impl, gc, points, npoints);
335 gdk_pixmap_draw_segments (GdkDrawable *drawable,
340 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
342 gdk_draw_segments (private->impl, gc, segs, nsegs);
346 gdk_pixmap_draw_lines (GdkDrawable *drawable,
351 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
353 gdk_draw_lines (private->impl, gc, points, npoints);
357 gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
362 PangoGlyphString *glyphs)
364 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
366 gdk_draw_glyphs (private->impl, gc, font, x, y, glyphs);
370 gdk_pixmap_draw_image (GdkDrawable *drawable,
380 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
382 gdk_draw_image (private->impl, gc, image, xsrc, ysrc, xdest, ydest,
387 gdk_pixmap_draw_pixbuf (GdkDrawable *drawable,
400 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
402 _gdk_draw_pixbuf (private->impl, gc, pixbuf,
403 src_x, src_y, dest_x, dest_y, width, height,
404 dither, x_dither, y_dither);
408 gdk_pixmap_real_get_size (GdkDrawable *drawable,
412 g_return_if_fail (GDK_IS_PIXMAP (drawable));
414 gdk_drawable_get_size (GDK_DRAWABLE (((GdkPixmapObject*)drawable)->impl),
419 gdk_pixmap_real_get_visual (GdkDrawable *drawable)
421 GdkColormap *colormap;
423 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
425 colormap = gdk_drawable_get_colormap (drawable);
426 return colormap ? gdk_colormap_get_visual (colormap) : NULL;
430 gdk_pixmap_real_get_depth (GdkDrawable *drawable)
434 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), 0);
436 depth = GDK_PIXMAP_OBJECT (drawable)->depth;
442 gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
445 g_return_if_fail (GDK_IS_PIXMAP (drawable));
447 gdk_drawable_set_colormap (((GdkPixmapObject*)drawable)->impl, cmap);
451 gdk_pixmap_real_get_colormap (GdkDrawable *drawable)
453 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
455 return gdk_drawable_get_colormap (((GdkPixmapObject*)drawable)->impl);
459 gdk_pixmap_copy_to_image (GdkDrawable *drawable,
468 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
470 return _gdk_drawable_copy_to_image (((GdkPixmapObject*)drawable)->impl,
472 src_x, src_y, dest_x, dest_y,
477 make_solid_mask (gint width, gint height)
481 GdkGCValues gc_values;
483 bitmap = gdk_pixmap_new (NULL, width, height, 1);
485 gc_values.foreground.pixel = 1;
486 gc = gdk_gc_new_with_values (bitmap, &gc_values, GDK_GC_FOREGROUND);
488 gdk_draw_rectangle (bitmap, gc, TRUE, 0, 0, width, height);
495 #define PACKED_COLOR(c) ((((c)->red & 0xff) << 8) | ((c)->green & 0xff) | ((c)->blue >> 8))
498 gdk_pixmap_colormap_new_from_pixbuf (GdkColormap *colormap,
500 GdkColor *transparent_color,
504 GdkPixbuf *render_pixbuf;
507 pixmap = gdk_pixmap_new (NULL,
508 gdk_pixbuf_get_width (pixbuf),
509 gdk_pixbuf_get_height (pixbuf),
510 gdk_colormap_get_visual (colormap)->depth);
511 gdk_drawable_set_colormap (pixmap, colormap);
513 if (transparent_color)
515 guint32 packed_color = PACKED_COLOR (transparent_color);
516 render_pixbuf = gdk_pixbuf_composite_color_simple (pixbuf,
517 gdk_pixbuf_get_width (pixbuf),
518 gdk_pixbuf_get_height (pixbuf),
520 255, 16, packed_color, packed_color);
523 render_pixbuf = pixbuf;
525 tmp_gc = gdk_gc_new (pixmap);
526 gdk_pixbuf_render_to_drawable (render_pixbuf, pixmap, tmp_gc, 0, 0, 0, 0,
527 gdk_pixbuf_get_width (render_pixbuf),
528 gdk_pixbuf_get_height (render_pixbuf),
529 GDK_RGB_DITHER_NORMAL, 0, 0);
530 gdk_gc_unref (tmp_gc);
532 if (render_pixbuf != pixbuf)
533 gdk_pixbuf_unref (render_pixbuf);
536 gdk_pixbuf_render_pixmap_and_mask (pixbuf, NULL, mask, 128);
539 *mask = make_solid_mask (gdk_pixbuf_get_width (pixbuf),
540 gdk_pixbuf_get_height (pixbuf));
546 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
547 GdkColormap *colormap,
549 GdkColor *transparent_color,
550 const gchar *filename)
555 g_return_val_if_fail (window != NULL || colormap != NULL, NULL);
556 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
557 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
559 if (colormap == NULL)
560 colormap = gdk_drawable_get_colormap (window);
562 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
566 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
568 gdk_pixbuf_unref (pixbuf);
574 gdk_pixmap_create_from_xpm (GdkWindow *window,
576 GdkColor *transparent_color,
577 const gchar *filename)
579 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
580 transparent_color, filename);
584 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
585 GdkColormap *colormap,
587 GdkColor *transparent_color,
593 g_return_val_if_fail (window != NULL || colormap != NULL, NULL);
594 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
595 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
597 if (colormap == NULL)
598 colormap = gdk_drawable_get_colormap (window);
600 pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)data);
604 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
606 gdk_pixbuf_unref (pixbuf);
612 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
614 GdkColor *transparent_color,
617 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
618 transparent_color, data);