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/.
28 #include "gdkpixmap.h"
29 #include "gdkinternals.h"
30 #include "gdkpixbuf.h"
31 #include "gdkscreen.h"
33 static GdkGC *gdk_pixmap_create_gc (GdkDrawable *drawable,
35 GdkGCValuesMask mask);
36 static void gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
43 static void gdk_pixmap_draw_arc (GdkDrawable *drawable,
52 static void gdk_pixmap_draw_polygon (GdkDrawable *drawable,
57 static void gdk_pixmap_draw_text (GdkDrawable *drawable,
64 static void gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
71 static void gdk_pixmap_draw_drawable (GdkDrawable *drawable,
80 static void gdk_pixmap_draw_points (GdkDrawable *drawable,
84 static void gdk_pixmap_draw_segments (GdkDrawable *drawable,
88 static void gdk_pixmap_draw_lines (GdkDrawable *drawable,
92 static void gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
97 PangoGlyphString *glyphs);
98 static void gdk_pixmap_draw_image (GdkDrawable *drawable,
107 static void gdk_pixmap_draw_pixbuf (GdkDrawable *drawable,
121 static void gdk_pixmap_real_get_size (GdkDrawable *drawable,
125 static GdkImage* gdk_pixmap_copy_to_image (GdkDrawable *drawable,
134 static GdkVisual* gdk_pixmap_real_get_visual (GdkDrawable *drawable);
135 static gint gdk_pixmap_real_get_depth (GdkDrawable *drawable);
136 static void gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
138 static GdkColormap* gdk_pixmap_real_get_colormap (GdkDrawable *drawable);
139 static GdkScreen* gdk_pixmap_real_get_screen (GdkDrawable *drawable);
141 static void gdk_pixmap_init (GdkPixmapObject *pixmap);
142 static void gdk_pixmap_class_init (GdkPixmapObjectClass *klass);
143 static void gdk_pixmap_finalize (GObject *object);
145 static gpointer parent_class = NULL;
148 gdk_pixmap_get_type (void)
150 static GType object_type = 0;
154 static const GTypeInfo object_info =
156 sizeof (GdkPixmapObjectClass),
157 (GBaseInitFunc) NULL,
158 (GBaseFinalizeFunc) NULL,
159 (GClassInitFunc) gdk_pixmap_class_init,
160 NULL, /* class_finalize */
161 NULL, /* class_data */
162 sizeof (GdkPixmapObject),
164 (GInstanceInitFunc) gdk_pixmap_init,
167 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
176 gdk_pixmap_init (GdkPixmapObject *pixmap)
178 /* 0-initialization is good for all other fields. */
179 pixmap->impl = g_object_new (_gdk_pixmap_impl_get_type (), NULL);
183 gdk_pixmap_class_init (GdkPixmapObjectClass *klass)
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
186 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
188 parent_class = g_type_class_peek_parent (klass);
190 object_class->finalize = gdk_pixmap_finalize;
192 drawable_class->create_gc = gdk_pixmap_create_gc;
193 drawable_class->draw_rectangle = gdk_pixmap_draw_rectangle;
194 drawable_class->draw_arc = gdk_pixmap_draw_arc;
195 drawable_class->draw_polygon = gdk_pixmap_draw_polygon;
196 drawable_class->draw_text = gdk_pixmap_draw_text;
197 drawable_class->draw_text_wc = gdk_pixmap_draw_text_wc;
198 drawable_class->draw_drawable = gdk_pixmap_draw_drawable;
199 drawable_class->draw_points = gdk_pixmap_draw_points;
200 drawable_class->draw_segments = gdk_pixmap_draw_segments;
201 drawable_class->draw_lines = gdk_pixmap_draw_lines;
202 drawable_class->draw_glyphs = gdk_pixmap_draw_glyphs;
203 drawable_class->draw_image = gdk_pixmap_draw_image;
204 drawable_class->draw_pixbuf = gdk_pixmap_draw_pixbuf;
205 drawable_class->get_depth = gdk_pixmap_real_get_depth;
206 drawable_class->get_screen = gdk_pixmap_real_get_screen;
207 drawable_class->get_size = gdk_pixmap_real_get_size;
208 drawable_class->set_colormap = gdk_pixmap_real_set_colormap;
209 drawable_class->get_colormap = gdk_pixmap_real_get_colormap;
210 drawable_class->get_visual = gdk_pixmap_real_get_visual;
211 drawable_class->_copy_to_image = gdk_pixmap_copy_to_image;
215 gdk_pixmap_finalize (GObject *object)
217 GdkPixmapObject *obj = (GdkPixmapObject *) object;
219 g_object_unref (obj->impl);
222 G_OBJECT_CLASS (parent_class)->finalize (object);
226 gdk_pixmap_create_gc (GdkDrawable *drawable,
228 GdkGCValuesMask mask)
230 return gdk_gc_new_with_values (((GdkPixmapObject *) drawable)->impl,
235 gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
243 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
245 gdk_draw_rectangle (private->impl, gc, filled,
246 x, y, width, height);
250 gdk_pixmap_draw_arc (GdkDrawable *drawable,
260 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
262 gdk_draw_arc (private->impl, gc, filled,
264 width, height, angle1, angle2);
268 gdk_pixmap_draw_polygon (GdkDrawable *drawable,
274 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
276 gdk_draw_polygon (private->impl, gc, filled, points, npoints);
280 gdk_pixmap_draw_text (GdkDrawable *drawable,
288 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
290 gdk_draw_text (private->impl, font, gc,
291 x, y, text, text_length);
295 gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
300 const GdkWChar *text,
303 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
305 gdk_draw_text_wc (private->impl, font, gc,
306 x, y, text, text_length);
310 gdk_pixmap_draw_drawable (GdkDrawable *drawable,
320 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
322 gdk_draw_drawable (private->impl, gc, src, xsrc, ysrc,
328 gdk_pixmap_draw_points (GdkDrawable *drawable,
333 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
335 gdk_draw_points (private->impl, gc, points, npoints);
339 gdk_pixmap_draw_segments (GdkDrawable *drawable,
344 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
346 gdk_draw_segments (private->impl, gc, segs, nsegs);
350 gdk_pixmap_draw_lines (GdkDrawable *drawable,
355 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
357 gdk_draw_lines (private->impl, gc, points, npoints);
361 gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
366 PangoGlyphString *glyphs)
368 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
370 gdk_draw_glyphs (private->impl, gc, font, x, y, glyphs);
374 gdk_pixmap_draw_image (GdkDrawable *drawable,
384 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
386 gdk_draw_image (private->impl, gc, image, xsrc, ysrc, xdest, ydest,
391 gdk_pixmap_draw_pixbuf (GdkDrawable *drawable,
404 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
406 gdk_draw_pixbuf (private->impl, gc, pixbuf,
407 src_x, src_y, dest_x, dest_y, width, height,
408 dither, x_dither, y_dither);
412 gdk_pixmap_real_get_size (GdkDrawable *drawable,
416 g_return_if_fail (GDK_IS_PIXMAP (drawable));
418 gdk_drawable_get_size (GDK_DRAWABLE (((GdkPixmapObject*)drawable)->impl),
423 gdk_pixmap_real_get_visual (GdkDrawable *drawable)
425 GdkColormap *colormap;
427 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
429 colormap = gdk_drawable_get_colormap (drawable);
430 return colormap ? gdk_colormap_get_visual (colormap) : NULL;
434 gdk_pixmap_real_get_depth (GdkDrawable *drawable)
438 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), 0);
440 depth = GDK_PIXMAP_OBJECT (drawable)->depth;
446 gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
449 g_return_if_fail (GDK_IS_PIXMAP (drawable));
451 gdk_drawable_set_colormap (((GdkPixmapObject*)drawable)->impl, cmap);
455 gdk_pixmap_real_get_colormap (GdkDrawable *drawable)
457 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
459 return gdk_drawable_get_colormap (((GdkPixmapObject*)drawable)->impl);
463 gdk_pixmap_copy_to_image (GdkDrawable *drawable,
472 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
474 return gdk_drawable_copy_to_image (((GdkPixmapObject*)drawable)->impl,
476 src_x, src_y, dest_x, dest_y,
481 make_solid_mask (GdkScreen *screen, gint width, gint height)
485 GdkGCValues gc_values;
487 bitmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
490 gc_values.foreground.pixel = 1;
491 gc = gdk_gc_new_with_values (bitmap, &gc_values, GDK_GC_FOREGROUND);
493 gdk_draw_rectangle (bitmap, gc, TRUE, 0, 0, width, height);
500 #define PACKED_COLOR(c) ((((c)->red & 0xff00) << 8) | \
501 ((c)->green & 0xff00) | \
505 gdk_pixmap_colormap_new_from_pixbuf (GdkColormap *colormap,
507 const GdkColor *transparent_color,
511 GdkPixbuf *render_pixbuf;
513 GdkScreen *screen = gdk_colormap_get_screen (colormap);
515 pixmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
516 gdk_pixbuf_get_width (pixbuf),
517 gdk_pixbuf_get_height (pixbuf),
518 gdk_colormap_get_visual (colormap)->depth);
519 gdk_drawable_set_colormap (pixmap, colormap);
521 if (transparent_color)
523 guint32 packed_color = PACKED_COLOR (transparent_color);
524 render_pixbuf = gdk_pixbuf_composite_color_simple (pixbuf,
525 gdk_pixbuf_get_width (pixbuf),
526 gdk_pixbuf_get_height (pixbuf),
528 255, 16, packed_color, packed_color);
531 render_pixbuf = pixbuf;
533 tmp_gc = _gdk_drawable_get_scratch_gc (pixmap, FALSE);
534 gdk_draw_pixbuf (pixmap, tmp_gc, render_pixbuf, 0, 0, 0, 0,
535 gdk_pixbuf_get_width (render_pixbuf),
536 gdk_pixbuf_get_height (render_pixbuf),
537 GDK_RGB_DITHER_NORMAL, 0, 0);
539 if (render_pixbuf != pixbuf)
540 g_object_unref (render_pixbuf);
543 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf, colormap, NULL, mask, 128);
546 *mask = make_solid_mask (screen,
547 gdk_pixbuf_get_width (pixbuf),
548 gdk_pixbuf_get_height (pixbuf));
554 gdk_pixmap_colormap_create_from_xpm (GdkDrawable *drawable,
555 GdkColormap *colormap,
557 const GdkColor *transparent_color,
558 const gchar *filename)
563 g_return_val_if_fail (drawable != NULL || colormap != NULL, NULL);
564 g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
565 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
567 if (colormap == NULL)
568 colormap = gdk_drawable_get_colormap (drawable);
570 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
574 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
576 g_object_unref (pixbuf);
582 gdk_pixmap_create_from_xpm (GdkDrawable *drawable,
584 const GdkColor *transparent_color,
585 const gchar *filename)
587 return gdk_pixmap_colormap_create_from_xpm (drawable, NULL, mask,
588 transparent_color, filename);
592 gdk_pixmap_colormap_create_from_xpm_d (GdkDrawable *drawable,
593 GdkColormap *colormap,
595 const GdkColor *transparent_color,
601 g_return_val_if_fail (drawable != NULL || colormap != NULL, NULL);
602 g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
603 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
605 if (colormap == NULL)
606 colormap = gdk_drawable_get_colormap (drawable);
608 pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)data);
612 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
614 g_object_unref (pixbuf);
620 gdk_pixmap_create_from_xpm_d (GdkDrawable *drawable,
622 const GdkColor *transparent_color,
625 return gdk_pixmap_colormap_create_from_xpm_d (drawable, NULL, mask,
626 transparent_color, data);
630 gdk_pixmap_real_get_screen (GdkDrawable *drawable)
632 return gdk_drawable_get_screen (GDK_PIXMAP_OBJECT (drawable)->impl);