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"
34 static GdkGC *gdk_pixmap_create_gc (GdkDrawable *drawable,
36 GdkGCValuesMask mask);
37 static void gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
44 static void gdk_pixmap_draw_arc (GdkDrawable *drawable,
53 static void gdk_pixmap_draw_polygon (GdkDrawable *drawable,
58 static void gdk_pixmap_draw_text (GdkDrawable *drawable,
65 static void gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
72 static void gdk_pixmap_draw_drawable (GdkDrawable *drawable,
81 static void gdk_pixmap_draw_points (GdkDrawable *drawable,
85 static void gdk_pixmap_draw_segments (GdkDrawable *drawable,
89 static void gdk_pixmap_draw_lines (GdkDrawable *drawable,
94 static void gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
99 PangoGlyphString *glyphs);
100 static void gdk_pixmap_draw_glyphs_transformed (GdkDrawable *drawable,
106 PangoGlyphString *glyphs);
108 static void gdk_pixmap_draw_image (GdkDrawable *drawable,
117 static void gdk_pixmap_draw_pixbuf (GdkDrawable *drawable,
129 static void gdk_pixmap_draw_trapezoids (GdkDrawable *drawable,
131 GdkTrapezoid *trapezoids,
134 static void gdk_pixmap_real_get_size (GdkDrawable *drawable,
138 static GdkImage* gdk_pixmap_copy_to_image (GdkDrawable *drawable,
147 static cairo_surface_t *gdk_pixmap_ref_cairo_surface (GdkDrawable *drawable);
149 static GdkVisual* gdk_pixmap_real_get_visual (GdkDrawable *drawable);
150 static gint gdk_pixmap_real_get_depth (GdkDrawable *drawable);
151 static void gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
153 static GdkColormap* gdk_pixmap_real_get_colormap (GdkDrawable *drawable);
154 static GdkScreen* gdk_pixmap_real_get_screen (GdkDrawable *drawable);
156 static void gdk_pixmap_init (GdkPixmapObject *pixmap);
157 static void gdk_pixmap_class_init (GdkPixmapObjectClass *klass);
158 static void gdk_pixmap_finalize (GObject *object);
160 static gpointer parent_class = NULL;
163 gdk_pixmap_get_type (void)
165 static GType object_type = 0;
168 object_type = g_type_register_static_simple (GDK_TYPE_DRAWABLE,
170 sizeof (GdkPixmapObjectClass),
171 (GClassInitFunc) gdk_pixmap_class_init,
172 sizeof (GdkPixmapObject),
173 (GInstanceInitFunc) gdk_pixmap_init,
180 gdk_pixmap_init (GdkPixmapObject *pixmap)
182 /* 0-initialization is good for all other fields. */
183 pixmap->impl = g_object_new (_gdk_pixmap_impl_get_type (), NULL);
187 gdk_pixmap_class_init (GdkPixmapObjectClass *klass)
189 GObjectClass *object_class = G_OBJECT_CLASS (klass);
190 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
192 parent_class = g_type_class_peek_parent (klass);
194 object_class->finalize = gdk_pixmap_finalize;
196 drawable_class->create_gc = gdk_pixmap_create_gc;
197 drawable_class->draw_rectangle = gdk_pixmap_draw_rectangle;
198 drawable_class->draw_arc = gdk_pixmap_draw_arc;
199 drawable_class->draw_polygon = gdk_pixmap_draw_polygon;
200 drawable_class->draw_text = gdk_pixmap_draw_text;
201 drawable_class->draw_text_wc = gdk_pixmap_draw_text_wc;
202 drawable_class->draw_drawable = gdk_pixmap_draw_drawable;
203 drawable_class->draw_points = gdk_pixmap_draw_points;
204 drawable_class->draw_segments = gdk_pixmap_draw_segments;
205 drawable_class->draw_lines = gdk_pixmap_draw_lines;
206 drawable_class->draw_glyphs = gdk_pixmap_draw_glyphs;
207 drawable_class->draw_glyphs_transformed = gdk_pixmap_draw_glyphs_transformed;
208 drawable_class->draw_image = gdk_pixmap_draw_image;
209 drawable_class->draw_pixbuf = gdk_pixmap_draw_pixbuf;
210 drawable_class->draw_trapezoids = gdk_pixmap_draw_trapezoids;
211 drawable_class->get_depth = gdk_pixmap_real_get_depth;
212 drawable_class->get_screen = gdk_pixmap_real_get_screen;
213 drawable_class->get_size = gdk_pixmap_real_get_size;
214 drawable_class->set_colormap = gdk_pixmap_real_set_colormap;
215 drawable_class->get_colormap = gdk_pixmap_real_get_colormap;
216 drawable_class->get_visual = gdk_pixmap_real_get_visual;
217 drawable_class->_copy_to_image = gdk_pixmap_copy_to_image;
218 drawable_class->ref_cairo_surface = gdk_pixmap_ref_cairo_surface;
222 gdk_pixmap_finalize (GObject *object)
224 GdkPixmapObject *obj = (GdkPixmapObject *) object;
226 g_object_unref (obj->impl);
229 G_OBJECT_CLASS (parent_class)->finalize (object);
233 gdk_pixmap_new (GdkDrawable *drawable,
238 GdkDrawable *source_drawable;
240 source_drawable = _gdk_drawable_get_source_drawable (drawable);
241 return _gdk_pixmap_new (source_drawable, width, height, depth);
245 gdk_bitmap_create_from_data (GdkDrawable *drawable,
250 GdkDrawable *source_drawable;
252 source_drawable = _gdk_drawable_get_source_drawable (drawable);
253 return _gdk_bitmap_create_from_data (source_drawable, data, width, height);
257 gdk_pixmap_create_from_data (GdkDrawable *drawable,
265 GdkDrawable *source_drawable;
267 source_drawable = _gdk_drawable_get_source_drawable (drawable);
268 return _gdk_pixmap_create_from_data (source_drawable,
275 gdk_pixmap_create_gc (GdkDrawable *drawable,
277 GdkGCValuesMask mask)
279 return gdk_gc_new_with_values (((GdkPixmapObject *) drawable)->impl,
284 gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
292 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
294 _gdk_gc_remove_drawable_clip (gc);
295 gdk_draw_rectangle (private->impl, gc, filled,
296 x, y, width, height);
300 gdk_pixmap_draw_arc (GdkDrawable *drawable,
310 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
312 _gdk_gc_remove_drawable_clip (gc);
313 gdk_draw_arc (private->impl, gc, filled,
315 width, height, angle1, angle2);
319 gdk_pixmap_draw_polygon (GdkDrawable *drawable,
325 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
327 _gdk_gc_remove_drawable_clip (gc);
328 gdk_draw_polygon (private->impl, gc, filled, points, npoints);
332 gdk_pixmap_draw_text (GdkDrawable *drawable,
340 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
342 _gdk_gc_remove_drawable_clip (gc);
343 gdk_draw_text (private->impl, font, gc,
344 x, y, text, text_length);
348 gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
353 const GdkWChar *text,
356 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
358 _gdk_gc_remove_drawable_clip (gc);
359 gdk_draw_text_wc (private->impl, font, gc,
360 x, y, text, text_length);
364 gdk_pixmap_draw_drawable (GdkDrawable *drawable,
374 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
376 _gdk_gc_remove_drawable_clip (gc);
377 gdk_draw_drawable (private->impl, gc, src, xsrc, ysrc,
383 gdk_pixmap_draw_points (GdkDrawable *drawable,
388 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
390 _gdk_gc_remove_drawable_clip (gc);
391 gdk_draw_points (private->impl, gc, points, npoints);
395 gdk_pixmap_draw_segments (GdkDrawable *drawable,
400 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
402 _gdk_gc_remove_drawable_clip (gc);
403 gdk_draw_segments (private->impl, gc, segs, nsegs);
407 gdk_pixmap_draw_lines (GdkDrawable *drawable,
412 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
414 _gdk_gc_remove_drawable_clip (gc);
415 gdk_draw_lines (private->impl, gc, points, npoints);
419 gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
424 PangoGlyphString *glyphs)
426 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
428 _gdk_gc_remove_drawable_clip (gc);
429 gdk_draw_glyphs (private->impl, gc, font, x, y, glyphs);
433 gdk_pixmap_draw_glyphs_transformed (GdkDrawable *drawable,
439 PangoGlyphString *glyphs)
441 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
443 _gdk_gc_remove_drawable_clip (gc);
444 gdk_draw_glyphs_transformed (private->impl, gc, matrix, font, x, y, glyphs);
448 gdk_pixmap_draw_image (GdkDrawable *drawable,
458 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
460 _gdk_gc_remove_drawable_clip (gc);
461 gdk_draw_image (private->impl, gc, image, xsrc, ysrc, xdest, ydest,
466 gdk_pixmap_draw_pixbuf (GdkDrawable *drawable,
479 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
482 _gdk_gc_remove_drawable_clip (gc);
483 gdk_draw_pixbuf (private->impl, gc, pixbuf,
484 src_x, src_y, dest_x, dest_y, width, height,
485 dither, x_dither, y_dither);
489 gdk_pixmap_draw_trapezoids (GdkDrawable *drawable,
491 GdkTrapezoid *trapezoids,
494 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
496 _gdk_gc_remove_drawable_clip (gc);
497 gdk_draw_trapezoids (private->impl, gc, trapezoids, n_trapezoids);
501 gdk_pixmap_real_get_size (GdkDrawable *drawable,
505 g_return_if_fail (GDK_IS_PIXMAP (drawable));
507 gdk_drawable_get_size (GDK_DRAWABLE (((GdkPixmapObject*)drawable)->impl),
512 gdk_pixmap_real_get_visual (GdkDrawable *drawable)
514 GdkColormap *colormap;
516 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
518 colormap = gdk_drawable_get_colormap (drawable);
519 return colormap ? gdk_colormap_get_visual (colormap) : NULL;
523 gdk_pixmap_real_get_depth (GdkDrawable *drawable)
527 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), 0);
529 depth = GDK_PIXMAP_OBJECT (drawable)->depth;
535 gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
538 g_return_if_fail (GDK_IS_PIXMAP (drawable));
540 gdk_drawable_set_colormap (((GdkPixmapObject*)drawable)->impl, cmap);
544 gdk_pixmap_real_get_colormap (GdkDrawable *drawable)
546 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
548 return gdk_drawable_get_colormap (((GdkPixmapObject*)drawable)->impl);
552 gdk_pixmap_copy_to_image (GdkDrawable *drawable,
561 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
563 return gdk_drawable_copy_to_image (((GdkPixmapObject*)drawable)->impl,
565 src_x, src_y, dest_x, dest_y,
569 static cairo_surface_t *
570 gdk_pixmap_ref_cairo_surface (GdkDrawable *drawable)
572 return _gdk_drawable_ref_cairo_surface (((GdkPixmapObject*)drawable)->impl);
576 make_solid_mask (GdkScreen *screen, gint width, gint height)
580 GdkGCValues gc_values;
582 bitmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
585 gc_values.foreground.pixel = 1;
586 gc = gdk_gc_new_with_values (bitmap, &gc_values, GDK_GC_FOREGROUND);
588 gdk_draw_rectangle (bitmap, gc, TRUE, 0, 0, width, height);
595 #define PACKED_COLOR(c) ((((c)->red & 0xff00) << 8) | \
596 ((c)->green & 0xff00) | \
600 gdk_pixmap_colormap_new_from_pixbuf (GdkColormap *colormap,
602 const GdkColor *transparent_color,
606 GdkPixbuf *render_pixbuf;
608 GdkScreen *screen = gdk_colormap_get_screen (colormap);
610 pixmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
611 gdk_pixbuf_get_width (pixbuf),
612 gdk_pixbuf_get_height (pixbuf),
613 gdk_colormap_get_visual (colormap)->depth);
614 gdk_drawable_set_colormap (pixmap, colormap);
616 if (transparent_color)
618 guint32 packed_color = PACKED_COLOR (transparent_color);
619 render_pixbuf = gdk_pixbuf_composite_color_simple (pixbuf,
620 gdk_pixbuf_get_width (pixbuf),
621 gdk_pixbuf_get_height (pixbuf),
623 255, 16, packed_color, packed_color);
626 render_pixbuf = pixbuf;
628 tmp_gc = _gdk_drawable_get_scratch_gc (pixmap, FALSE);
629 gdk_draw_pixbuf (pixmap, tmp_gc, render_pixbuf, 0, 0, 0, 0,
630 gdk_pixbuf_get_width (render_pixbuf),
631 gdk_pixbuf_get_height (render_pixbuf),
632 GDK_RGB_DITHER_NORMAL, 0, 0);
634 if (render_pixbuf != pixbuf)
635 g_object_unref (render_pixbuf);
638 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf, colormap, NULL, mask, 128);
641 *mask = make_solid_mask (screen,
642 gdk_pixbuf_get_width (pixbuf),
643 gdk_pixbuf_get_height (pixbuf));
649 gdk_pixmap_colormap_create_from_xpm (GdkDrawable *drawable,
650 GdkColormap *colormap,
652 const GdkColor *transparent_color,
653 const gchar *filename)
658 g_return_val_if_fail (drawable != NULL || colormap != NULL, NULL);
659 g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
660 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
662 if (colormap == NULL)
663 colormap = gdk_drawable_get_colormap (drawable);
665 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
669 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
671 g_object_unref (pixbuf);
677 gdk_pixmap_create_from_xpm (GdkDrawable *drawable,
679 const GdkColor *transparent_color,
680 const gchar *filename)
682 return gdk_pixmap_colormap_create_from_xpm (drawable, NULL, mask,
683 transparent_color, filename);
687 gdk_pixmap_colormap_create_from_xpm_d (GdkDrawable *drawable,
688 GdkColormap *colormap,
690 const GdkColor *transparent_color,
696 g_return_val_if_fail (drawable != NULL || colormap != NULL, NULL);
697 g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
698 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
700 if (colormap == NULL)
701 colormap = gdk_drawable_get_colormap (drawable);
703 pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)data);
707 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
709 g_object_unref (pixbuf);
715 gdk_pixmap_create_from_xpm_d (GdkDrawable *drawable,
717 const GdkColor *transparent_color,
720 return gdk_pixmap_colormap_create_from_xpm_d (drawable, NULL, mask,
721 transparent_color, data);
725 gdk_pixmap_real_get_screen (GdkDrawable *drawable)
727 return gdk_drawable_get_screen (GDK_PIXMAP_OBJECT (drawable)->impl);
730 #define __GDK_PIXMAP_C__
731 #include "gdkaliasdef.c"