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/.
29 #include <pango/pangocairo.h>
30 #include "gdkdrawable.h"
31 #include "gdkinternals.h"
32 #include "gdkwindow.h"
33 #include "gdkscreen.h"
34 #include "gdk-pixbuf-private.h"
35 #include "gdkpixbuf.h"
38 static GdkImage* gdk_drawable_real_get_image (GdkDrawable *drawable,
43 static GdkDrawable* gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
48 gint *composite_x_offset,
49 gint *composite_y_offset);
50 static GdkRegion * gdk_drawable_real_get_visible_region (GdkDrawable *drawable);
51 static void gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
64 static void gdk_drawable_class_init (GdkDrawableClass *klass);
67 gdk_drawable_get_type (void)
69 static GType object_type = 0;
73 static const GTypeInfo object_info =
75 sizeof (GdkDrawableClass),
77 (GBaseFinalizeFunc) NULL,
78 (GClassInitFunc) gdk_drawable_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
83 (GInstanceInitFunc) NULL,
86 object_type = g_type_register_static (G_TYPE_OBJECT,
89 G_TYPE_FLAG_ABSTRACT);
96 gdk_drawable_class_init (GdkDrawableClass *klass)
98 klass->get_image = gdk_drawable_real_get_image;
99 klass->get_composite_drawable = gdk_drawable_real_get_composite_drawable;
100 /* Default implementation for clip and visible region is the same */
101 klass->get_clip_region = gdk_drawable_real_get_visible_region;
102 klass->get_visible_region = gdk_drawable_real_get_visible_region;
103 klass->draw_pixbuf = gdk_drawable_real_draw_pixbuf;
106 /* Manipulation of drawables
110 * gdk_drawable_set_data:
111 * @drawable: a #GdkDrawable
112 * @key: name to store the data under
113 * @data: arbitrary data
114 * @destroy_func: function to free @data, or %NULL
116 * This function is equivalent to g_object_set_data(),
117 * the #GObject variant should be used instead.
121 gdk_drawable_set_data (GdkDrawable *drawable,
124 GDestroyNotify destroy_func)
126 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
128 g_object_set_qdata_full (G_OBJECT (drawable),
129 g_quark_from_string (key),
135 * gdk_drawable_get_data:
136 * @drawable: a #GdkDrawable
137 * @key: name the data was stored under
139 * Equivalent to g_object_get_data(); the #GObject variant should be
142 * Return value: the data stored at @key
145 gdk_drawable_get_data (GdkDrawable *drawable,
148 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
150 return g_object_get_qdata (G_OBJECT (drawable),
151 g_quark_try_string (key));
155 * gdk_drawable_get_size:
156 * @drawable: a #GdkDrawable
157 * @width: location to store drawable's width, or %NULL
158 * @height: location to store drawable's height, or %NULL
160 * Fills *@width and *@height with the size of @drawable.
161 * @width or @height can be %NULL if you only want the other one.
163 * On the X11 platform, if @drawable is a #GdkWindow, the returned
164 * size is the size reported in the most-recently-processed configure
165 * event, rather than the current size on the X server.
169 gdk_drawable_get_size (GdkDrawable *drawable,
173 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
175 GDK_DRAWABLE_GET_CLASS (drawable)->get_size (drawable, width, height);
179 * gdk_drawable_get_visual:
180 * @drawable: a #GdkDrawable
182 * Gets the #GdkVisual describing the pixel format of @drawable.
184 * Return value: a #GdkVisual
187 gdk_drawable_get_visual (GdkDrawable *drawable)
189 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
191 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visual (drawable);
195 * gdk_drawable_get_depth:
196 * @drawable: a #GdkDrawable
198 * Obtains the bit depth of the drawable, that is, the number of bits
199 * that make up a pixel in the drawable's visual. Examples are 8 bits
200 * per pixel, 24 bits per pixel, etc.
202 * Return value: number of bits per pixel
205 gdk_drawable_get_depth (GdkDrawable *drawable)
207 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), 0);
209 return GDK_DRAWABLE_GET_CLASS (drawable)->get_depth (drawable);
212 * gdk_drawable_get_screen:
213 * @drawable: a #GdkDrawable
215 * Gets the #GdkScreen associated with a #GdkDrawable.
217 * Return value: the #GdkScreen associated with @drawable
222 gdk_drawable_get_screen(GdkDrawable *drawable)
224 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
226 return GDK_DRAWABLE_GET_CLASS (drawable)->get_screen (drawable);
230 * gdk_drawable_get_display:
231 * @drawable: a #GdkDrawable
233 * Gets the #GdkDisplay associated with a #GdkDrawable.
235 * Return value: the #GdkDisplay associated with @drawable
240 gdk_drawable_get_display (GdkDrawable *drawable)
242 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
244 return gdk_screen_get_display (gdk_drawable_get_screen (drawable));
248 * gdk_drawable_set_colormap:
249 * @drawable: a #GdkDrawable
250 * @colormap: a #GdkColormap
252 * Sets the colormap associated with @drawable. Normally this will
253 * happen automatically when the drawable is created; you only need to
254 * use this function if the drawable-creating function did not have a
255 * way to determine the colormap, and you then use drawable operations
256 * that require a colormap. The colormap for all drawables and
257 * graphics contexts you intend to use together should match. i.e.
258 * when using a #GdkGC to draw to a drawable, or copying one drawable
259 * to another, the colormaps should match.
263 gdk_drawable_set_colormap (GdkDrawable *drawable,
266 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
267 g_return_if_fail (cmap == NULL || gdk_drawable_get_depth (drawable)
268 == cmap->visual->depth);
270 GDK_DRAWABLE_GET_CLASS (drawable)->set_colormap (drawable, cmap);
274 * gdk_drawable_get_colormap:
275 * @drawable: a #GdkDrawable
277 * Gets the colormap for @drawable, if one is set; returns
280 * Return value: the colormap, or %NULL
283 gdk_drawable_get_colormap (GdkDrawable *drawable)
285 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
287 return GDK_DRAWABLE_GET_CLASS (drawable)->get_colormap (drawable);
292 * @drawable: a #GdkDrawable
294 * Deprecated equivalent of calling g_object_ref() on @drawable.
295 * (Drawables were not objects in previous versions of GDK.)
297 * Return value: the same @drawable passed in
300 gdk_drawable_ref (GdkDrawable *drawable)
302 return (GdkDrawable *) g_object_ref (drawable);
306 * gdk_drawable_unref:
307 * @drawable: a #GdkDrawable
309 * Deprecated equivalent of calling g_object_unref() on @drawable.
313 gdk_drawable_unref (GdkDrawable *drawable)
315 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
317 g_object_unref (drawable);
325 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
327 * @x: the x coordinate of the point.
328 * @y: the y coordinate of the point.
330 * Draws a point, using the foreground color and other attributes of
334 gdk_draw_point (GdkDrawable *drawable,
341 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
342 g_return_if_fail (GDK_IS_GC (gc));
347 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, &point, 1);
352 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
354 * @x1_: the x coordinate of the start point.
355 * @y1_: the y coordinate of the start point.
356 * @x2_: the x coordinate of the end point.
357 * @y2_: the y coordinate of the end point.
359 * Draws a line, using the foreground color and other attributes of
363 gdk_draw_line (GdkDrawable *drawable,
372 g_return_if_fail (drawable != NULL);
373 g_return_if_fail (gc != NULL);
374 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
375 g_return_if_fail (GDK_IS_GC (gc));
381 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, &segment, 1);
385 * gdk_draw_rectangle:
386 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
388 * @filled: %TRUE if the rectangle should be filled.
389 * @x: the x coordinate of the left edge of the rectangle.
390 * @y: the y coordinate of the top edge of the rectangle.
391 * @width: the width of the rectangle.
392 * @height: the height of the rectangle.
394 * Draws a rectangular outline or filled rectangle, using the foreground color
395 * and other attributes of the #GdkGC.
397 * A rectangle drawn filled is 1 pixel smaller in both dimensions than a
398 * rectangle outlined. Calling
399 * <literal>gdk_draw_rectangle (window, gc, TRUE, 0, 0, 20, 20)</literal>
400 * results in a filled rectangle 20 pixels wide and 20 pixels high. Calling
401 * <literal>gdk_draw_rectangle (window, gc, FALSE, 0, 0, 20, 20)</literal>
402 * results in an outlined rectangle with corners at (0, 0), (0, 20), (20, 20),
403 * and (20, 0), which makes it 21 pixels wide and 21 pixels high.
406 gdk_draw_rectangle (GdkDrawable *drawable,
414 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
415 g_return_if_fail (GDK_IS_GC (gc));
417 if (width < 0 || height < 0)
422 gdk_drawable_get_size (drawable, &real_width, &real_height);
427 height = real_height;
430 GDK_DRAWABLE_GET_CLASS (drawable)->draw_rectangle (drawable, gc, filled, x, y,
436 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
438 * @filled: %TRUE if the arc should be filled, producing a 'pie slice'.
439 * @x: the x coordinate of the left edge of the bounding rectangle.
440 * @y: the y coordinate of the top edge of the bounding rectangle.
441 * @width: the width of the bounding rectangle.
442 * @height: the height of the bounding rectangle.
443 * @angle1: the start angle of the arc, relative to the 3 o'clock position,
444 * counter-clockwise, in 1/64ths of a degree.
445 * @angle2: the end angle of the arc, relative to @angle1, in 1/64ths
448 * Draws an arc or a filled 'pie slice'. The arc is defined by the bounding
449 * rectangle of the entire ellipse, and the start and end angles of the part
450 * of the ellipse to be drawn.
453 gdk_draw_arc (GdkDrawable *drawable,
463 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
464 g_return_if_fail (GDK_IS_GC (gc));
466 if (width < 0 || height < 0)
471 gdk_drawable_get_size (drawable, &real_width, &real_height);
476 height = real_height;
479 GDK_DRAWABLE_GET_CLASS (drawable)->draw_arc (drawable, gc, filled,
480 x, y, width, height, angle1, angle2);
485 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
487 * @filled: %TRUE if the polygon should be filled. The polygon is closed
488 * automatically, connecting the last point to the first point if
490 * @points: an array of #GdkPoint structures specifying the points making
492 * @npoints: the number of points.
494 * Draws an outlined or filled polygon.
497 gdk_draw_polygon (GdkDrawable *drawable,
503 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
504 g_return_if_fail (GDK_IS_GC (gc));
506 GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
512 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
514 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
518 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
521 * @x: the x coordinate of the left edge of the text.
522 * @y: the y coordinate of the baseline of the text.
523 * @string: the string of characters to draw.
525 * Draws a string of characters in the given font or fontset.
527 * Deprecated: Use gdk_draw_layout() instead.
530 gdk_draw_string (GdkDrawable *drawable,
537 gdk_draw_text (drawable, font, gc, x, y, string, _gdk_font_strlen (font, string));
542 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
544 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
548 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
551 * @x: the x coordinate of the left edge of the text.
552 * @y: the y coordinate of the baseline of the text.
553 * @text: the characters to draw.
554 * @text_length: the number of characters of @text to draw.
556 * Draws a number of characters in the given font or fontset.
558 * Deprecated: Use gdk_draw_layout() instead.
561 gdk_draw_text (GdkDrawable *drawable,
569 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
570 g_return_if_fail (font != NULL);
571 g_return_if_fail (GDK_IS_GC (gc));
572 g_return_if_fail (text != NULL);
574 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text (drawable, font, gc, x, y, text, text_length);
579 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
582 * @x: the x coordinate of the left edge of the text.
583 * @y: the y coordinate of the baseline of the text.
584 * @text: the wide characters to draw.
585 * @text_length: the number of characters to draw.
587 * Draws a number of wide characters using the given font of fontset.
588 * If the font is a 1-byte font, the string is converted into 1-byte
589 * characters (discarding the high bytes) before output.
591 * Deprecated: Use gdk_draw_layout() instead.
594 gdk_draw_text_wc (GdkDrawable *drawable,
599 const GdkWChar *text,
602 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
603 g_return_if_fail (font != NULL);
604 g_return_if_fail (GDK_IS_GC (gc));
605 g_return_if_fail (text != NULL);
607 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text_wc (drawable, font, gc, x, y, text, text_length);
612 * @drawable: a #GdkDrawable
613 * @gc: a #GdkGC sharing the drawable's visual and colormap
614 * @src: the source #GdkDrawable, which may be the same as @drawable
615 * @xsrc: X position in @src of rectangle to draw
616 * @ysrc: Y position in @src of rectangle to draw
617 * @xdest: X position in @drawable where the rectangle should be drawn
618 * @ydest: Y position in @drawable where the rectangle should be drawn
619 * @width: width of rectangle to draw, or -1 for entire @src width
620 * @height: height of rectangle to draw, or -1 for entire @src height
622 * Copies the @width x @height region of @src at coordinates (@xsrc,
623 * @ysrc) to coordinates (@xdest, @ydest) in @drawable.
624 * @width and/or @height may be given as -1, in which case the entire
625 * @src drawable will be copied.
627 * Most fields in @gc are not used for this operation, but notably the
628 * clip mask or clip region will be honored.
630 * The source and destination drawables must have the same visual and
631 * colormap, or errors will result. (On X11, failure to match
632 * visual/colormap results in a BadMatch error from the X server.)
633 * A common cause of this problem is an attempt to draw a bitmap to
634 * a color drawable. The way to draw a bitmap is to set the
635 * bitmap as a clip mask on your #GdkGC, then use gdk_draw_rectangle()
636 * to draw a rectangle clipped to the bitmap.
639 gdk_draw_drawable (GdkDrawable *drawable,
649 GdkDrawable *composite;
650 gint composite_x_offset = 0;
651 gint composite_y_offset = 0;
653 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
654 g_return_if_fail (src != NULL);
655 g_return_if_fail (GDK_IS_GC (gc));
657 if (width < 0 || height < 0)
662 gdk_drawable_get_size (src, &real_width, &real_height);
667 height = real_height;
672 GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
676 &composite_y_offset);
679 GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc, composite,
680 xsrc - composite_x_offset,
681 ysrc - composite_y_offset,
685 g_object_unref (composite);
690 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
692 * @image: the #GdkImage to draw.
693 * @xsrc: the left edge of the source rectangle within @image.
694 * @ysrc: the top of the source rectangle within @image.
695 * @xdest: the x coordinate of the destination within @drawable.
696 * @ydest: the y coordinate of the destination within @drawable.
697 * @width: the width of the area to be copied, or -1 to make the area
698 * extend to the right edge of @image.
699 * @height: the height of the area to be copied, or -1 to make the area
700 * extend to the bottom edge of @image.
702 * Draws a #GdkImage onto a drawable.
703 * The depth of the #GdkImage must match the depth of the #GdkDrawable.
706 gdk_draw_image (GdkDrawable *drawable,
716 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
717 g_return_if_fail (image != NULL);
718 g_return_if_fail (GDK_IS_GC (gc));
721 width = image->width;
723 height = image->height;
725 GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
726 xdest, ydest, width, height);
731 * @drawable: Destination drawable.
732 * @gc: a #GdkGC, used for clipping, or %NULL
733 * @pixbuf: a #GdkPixbuf
734 * @src_x: Source X coordinate within pixbuf.
735 * @src_y: Source Y coordinates within pixbuf.
736 * @dest_x: Destination X coordinate within drawable.
737 * @dest_y: Destination Y coordinate within drawable.
738 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
739 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
740 * @dither: Dithering mode for #GdkRGB.
741 * @x_dither: X offset for dither.
742 * @y_dither: Y offset for dither.
744 * Renders a rectangular portion of a pixbuf to a drawable. The destination
745 * drawable must have a colormap. All windows have a colormap, however, pixmaps
746 * only have colormap by default if they were created with a non-%NULL window
747 * argument. Otherwise a colormap must be set on them with
748 * gdk_drawable_set_colormap().
750 * On older X servers, rendering pixbufs with an alpha channel involves round
751 * trips to the X server, and may be somewhat slow.
753 * The clip mask of @gc is ignored, but clip rectangles and clip regions work
759 gdk_draw_pixbuf (GdkDrawable *drawable,
772 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
773 g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
774 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
777 width = gdk_pixbuf_get_width (pixbuf);
779 height = gdk_pixbuf_get_height (pixbuf);
781 GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc, pixbuf,
782 src_x, src_y, dest_x, dest_y, width, height,
783 dither, x_dither, y_dither);
788 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
790 * @points: an array of #GdkPoint structures.
791 * @npoints: the number of points to be drawn.
793 * Draws a number of points, using the foreground color and other
794 * attributes of the #GdkGC.
797 gdk_draw_points (GdkDrawable *drawable,
802 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
803 g_return_if_fail ((points != NULL) && (npoints > 0));
804 g_return_if_fail (GDK_IS_GC (gc));
805 g_return_if_fail (npoints >= 0);
810 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
815 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
817 * @segs: an array of #GdkSegment structures specifying the start and
818 * end points of the lines to be drawn.
819 * @nsegs: the number of line segments to draw, i.e. the size of the
822 * Draws a number of unconnected lines.
825 gdk_draw_segments (GdkDrawable *drawable,
830 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
835 g_return_if_fail (segs != NULL);
836 g_return_if_fail (GDK_IS_GC (gc));
837 g_return_if_fail (nsegs >= 0);
839 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
844 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
846 * @points: an array of #GdkPoint structures specifying the endpoints of the
847 * @npoints: the size of the @points array.
849 * Draws a series of lines connecting the given points.
850 * The way in which joins between lines are draw is determined by the
851 * #GdkCapStyle value in the #GdkGC. This can be set with
852 * gdk_gc_set_line_attributes().
855 gdk_draw_lines (GdkDrawable *drawable,
860 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
861 g_return_if_fail (points != NULL);
862 g_return_if_fail (GDK_IS_GC (gc));
863 g_return_if_fail (npoints >= 0);
868 GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
872 real_draw_glyphs (GdkDrawable *drawable,
878 PangoGlyphString *glyphs)
882 cr = gdk_drawable_create_cairo_context (drawable);
883 _gdk_gc_update_context (gc, cr, NULL, NULL);
887 cairo_matrix_t *cairo_matrix;
889 cairo_matrix = cairo_matrix_create ();
890 cairo_matrix_set_affine (cairo_matrix,
891 matrix->xx, matrix->yx,
892 matrix->xy, matrix->yy,
893 matrix->x0, matrix->y0);
895 cairo_set_matrix (cr, cairo_matrix);
896 cairo_matrix_destroy (cairo_matrix);
899 cairo_move_to (cr, x, y);
900 pango_cairo_show_glyph_string (cr, font, glyphs);
907 * @drawable: a #GdkDrawable
909 * @font: font to be used
910 * @x: X coordinate of baseline origin
911 * @y: Y coordinate of baseline origin
912 * @glyphs: the glyph string to draw
914 * This is a low-level function; 99% of text rendering should be done
915 * using gdk_draw_layout() instead.
917 * A glyph is a single image in a font. This function draws a sequence of
918 * glyphs. To obtain a sequence of glyphs you have to understand a
919 * lot about internationalized text handling, which you don't want to
920 * understand; thus, use gdk_draw_layout() instead of this function,
921 * gdk_draw_layout() handles the details.
925 gdk_draw_glyphs (GdkDrawable *drawable,
930 PangoGlyphString *glyphs)
932 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
933 g_return_if_fail (GDK_IS_GC (gc));
935 real_draw_glyphs (drawable, gc, NULL, font,
940 * gdk_draw_glyphs_transformed:
941 * @drawable: a #GdkDrawable
943 * @matrix: a #PangoMatrix, or %NULL to use an identity transformation
944 * @font: the font in which to draw the string
945 * @x: the x position of the start of the string (in Pango
946 * units in user space coordinates)
947 * @y: the y position of the baseline (in Pango units
948 * in user space coordinates)
949 * @glyphs: the glyph string to draw
951 * Renders a #PangoGlyphString onto a drawable, possibly
952 * transforming the layed-out coordinates through a transformation
953 * matrix. Note that the transformation matrix for @font is not
954 * changed, so to produce correct rendering results, the @font
955 * must have been loaded using a #PangoContext with an identical
956 * transformation matrix to that passed in to this function.
958 * See also gdk_draw_glyphs(), gdk_draw_layout().
963 gdk_draw_glyphs_transformed (GdkDrawable *drawable,
969 PangoGlyphString *glyphs)
971 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
972 g_return_if_fail (GDK_IS_GC (gc));
974 real_draw_glyphs (drawable, gc, matrix, font,
975 x / PANGO_SCALE, y / PANGO_SCALE, glyphs);
979 * gdk_draw_trapezoids:
980 * @drawable: a #GdkDrawable
982 * @trapezoids: an array of #GdkTrapezoid structures
983 * @n_trapezoids: the number of trapezoids to draw
985 * Draws a set of anti-aliased trapezoids. The trapezoids are
986 * combined using saturation addition, then drawn over the background
987 * as a set. This is low level functionality used internally to implement
988 * rotated underlines and backgrouds when rendering a PangoLayout and is
989 * likely not useful for applications.
994 gdk_draw_trapezoids (GdkDrawable *drawable,
996 GdkTrapezoid *trapezoids,
1002 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
1003 g_return_if_fail (GDK_IS_GC (gc));
1004 g_return_if_fail (n_trapezoids == 0 || trapezoids != NULL);
1006 cr = gdk_drawable_create_cairo_context (drawable);
1007 _gdk_gc_update_context (gc, cr, NULL, NULL);
1009 for (i = 0; i < n_trapezoids; i++)
1011 cairo_move_to (cr, trapezoids[i].x11, trapezoids[i].y1);
1012 cairo_line_to (cr, trapezoids[i].x21, trapezoids[i].y1);
1013 cairo_line_to (cr, trapezoids[i].x22, trapezoids[i].y2);
1014 cairo_line_to (cr, trapezoids[i].x21, trapezoids[i].y2);
1015 cairo_close_path (cr);
1024 * gdk_drawable_copy_to_image:
1025 * @drawable: a #GdkDrawable
1026 * @image: a #GdkDrawable, or %NULL if a new @image should be created.
1027 * @src_x: x coordinate on @drawable
1028 * @src_y: y coordinate on @drawable
1029 * @dest_x: x coordinate within @image. Must be 0 if @image is %NULL
1030 * @dest_y: y coordinate within @image. Must be 0 if @image is %NULL
1031 * @width: width of region to get
1032 * @height: height or region to get
1034 * Copies a portion of @drawable into the client side image structure
1035 * @image. If @image is %NULL, creates a new image of size @width x @height
1036 * and copies into that. See gdk_drawable_get_image() for further details.
1038 * Return value: @image, or a new a #GdkImage containing the contents
1044 gdk_drawable_copy_to_image (GdkDrawable *drawable,
1053 GdkDrawable *composite;
1054 gint composite_x_offset = 0;
1055 gint composite_y_offset = 0;
1059 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1060 g_return_val_if_fail (src_x >= 0, NULL);
1061 g_return_val_if_fail (src_y >= 0, NULL);
1063 /* FIXME? Note race condition since we get the size then
1064 * get the image, and the size may have changed.
1067 if (width < 0 || height < 0)
1068 gdk_drawable_get_size (drawable,
1069 width < 0 ? &width : NULL,
1070 height < 0 ? &height : NULL);
1073 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
1076 &composite_x_offset,
1077 &composite_y_offset);
1079 retval = GDK_DRAWABLE_GET_CLASS (composite)->_copy_to_image (composite,
1081 src_x - composite_x_offset,
1082 src_y - composite_y_offset,
1086 g_object_unref (composite);
1088 if (!image && retval)
1090 cmap = gdk_drawable_get_colormap (drawable);
1093 gdk_image_set_colormap (retval, cmap);
1100 * gdk_drawable_get_image:
1101 * @drawable: a #GdkDrawable
1102 * @x: x coordinate on @drawable
1103 * @y: y coordinate on @drawable
1104 * @width: width of region to get
1105 * @height: height or region to get
1107 * A #GdkImage stores client-side image data (pixels). In contrast,
1108 * #GdkPixmap and #GdkWindow are server-side
1109 * objects. gdk_drawable_get_image() obtains the pixels from a
1110 * server-side drawable as a client-side #GdkImage. The format of a
1111 * #GdkImage depends on the #GdkVisual of the current display, which
1112 * makes manipulating #GdkImage extremely difficult; therefore, in
1113 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
1114 * this lower-level function. A #GdkPixbuf contains image data in a
1115 * canonicalized RGB format, rather than a display-dependent format.
1116 * Of course, there's a convenience vs. speed tradeoff here, so you'll
1117 * want to think about what makes sense for your application.
1119 * @x, @y, @width, and @height define the region of @drawable to
1120 * obtain as an image.
1122 * You would usually copy image data to the client side if you intend
1123 * to examine the values of individual pixels, for example to darken
1124 * an image or add a red tint. It would be prohibitively slow to
1125 * make a round-trip request to the windowing system for each pixel,
1126 * so instead you get all of them at once, modify them, then copy
1127 * them all back at once.
1129 * If the X server or other windowing system backend is on the local
1130 * machine, this function may use shared memory to avoid copying
1133 * If the source drawable is a #GdkWindow and partially offscreen
1134 * or obscured, then the obscured portions of the returned image
1135 * will contain undefined data.
1137 * Return value: a #GdkImage containing the contents of @drawable
1140 gdk_drawable_get_image (GdkDrawable *drawable,
1146 GdkDrawable *composite;
1147 gint composite_x_offset = 0;
1148 gint composite_y_offset = 0;
1152 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1153 g_return_val_if_fail (x >= 0, NULL);
1154 g_return_val_if_fail (y >= 0, NULL);
1156 /* FIXME? Note race condition since we get the size then
1157 * get the image, and the size may have changed.
1160 if (width < 0 || height < 0)
1161 gdk_drawable_get_size (drawable,
1162 width < 0 ? &width : NULL,
1163 height < 0 ? &height : NULL);
1166 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
1169 &composite_x_offset,
1170 &composite_y_offset);
1172 retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
1173 x - composite_x_offset,
1174 y - composite_y_offset,
1177 g_object_unref (composite);
1179 cmap = gdk_drawable_get_colormap (drawable);
1182 gdk_image_set_colormap (retval, cmap);
1188 gdk_drawable_real_get_image (GdkDrawable *drawable,
1194 return gdk_drawable_copy_to_image (drawable, NULL, x, y, 0, 0, width, height);
1198 gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
1203 gint *composite_x_offset,
1204 gint *composite_y_offset)
1206 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1208 *composite_x_offset = 0;
1209 *composite_y_offset = 0;
1211 return g_object_ref (drawable);
1215 * gdk_drawable_get_clip_region:
1216 * @drawable: a #GdkDrawable
1218 * Computes the region of a drawable that potentially can be written
1219 * to by drawing primitives. This region will not take into account
1220 * the clip region for the GC, and may also not take into account
1221 * other factors such as if the window is obscured by other windows,
1222 * but no area outside of this region will be affected by drawing
1225 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1226 * when you are done.
1229 gdk_drawable_get_clip_region (GdkDrawable *drawable)
1231 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1233 return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
1237 * gdk_drawable_get_visible_region:
1240 * Computes the region of a drawable that is potentially visible.
1241 * This does not necessarily take into account if the window is
1242 * obscured by other windows, but no area outside of this region
1245 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1246 * when you are done.
1249 gdk_drawable_get_visible_region (GdkDrawable *drawable)
1251 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1253 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
1257 gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
1264 gdk_drawable_get_size (drawable, &rect.width, &rect.height);
1266 return gdk_region_rectangle (&rect);
1270 * _gdk_drawable_ref_cairo_surface:
1271 * @drawable: a #GdkDrawable
1273 * Obtains a #cairo_surface_t for the given drawable. If a
1274 * #cairo_surface_t for the drawable already exists, it will be
1275 * referenced, otherwise a new surface will be created.
1277 * Return value: a newly referenced #cairo_surface_t that points
1278 * to @drawable. Unref with cairo_surface_destroy()
1281 _gdk_drawable_ref_cairo_surface (GdkDrawable *drawable)
1283 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1285 return GDK_DRAWABLE_GET_CLASS (drawable)->ref_cairo_surface (drawable);
1289 * gdk_drawable_create_cairo_context:
1290 * @drawable: a #GdkDrawable
1292 * Creates a Cairo context for drawing to @drawable.
1294 * Return value: A newly created Cairo context. Free with
1295 * cairo_destroy() when you are done drawing.
1300 gdk_drawable_create_cairo_context (GdkDrawable *drawable)
1302 cairo_surface_t *surface;
1305 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1307 cr = cairo_create ();
1309 surface = _gdk_drawable_ref_cairo_surface (drawable);
1311 cairo_set_target_surface (cr, surface);
1317 composite (guchar *src_buf,
1320 gint dest_rowstride,
1324 guchar *src = src_buf;
1325 guchar *dest = dest_buf;
1329 gint twidth = width;
1338 t = a * p[0] + (255 - a) * q[0] + 0x80;
1339 q[0] = (t + (t >> 8)) >> 8;
1340 t = a * p[1] + (255 - a) * q[1] + 0x80;
1341 q[1] = (t + (t >> 8)) >> 8;
1342 t = a * p[2] + (255 - a) * q[2] + 0x80;
1343 q[2] = (t + (t >> 8)) >> 8;
1349 src += src_rowstride;
1350 dest += dest_rowstride;
1355 composite_0888 (guchar *src_buf,
1358 gint dest_rowstride,
1359 GdkByteOrder dest_byte_order,
1363 guchar *src = src_buf;
1364 guchar *dest = dest_buf;
1368 gint twidth = width;
1372 if (dest_byte_order == GDK_LSB_FIRST)
1378 t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80;
1379 q[0] = (t + (t >> 8)) >> 8;
1380 t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80;
1381 q[1] = (t + (t >> 8)) >> 8;
1382 t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80;
1383 q[2] = (t + (t >> 8)) >> 8;
1394 t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80;
1395 q[1] = (t + (t >> 8)) >> 8;
1396 t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80;
1397 q[2] = (t + (t >> 8)) >> 8;
1398 t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80;
1399 q[3] = (t + (t >> 8)) >> 8;
1405 src += src_rowstride;
1406 dest += dest_rowstride;
1411 composite_565 (guchar *src_buf,
1414 gint dest_rowstride,
1415 GdkByteOrder dest_byte_order,
1419 guchar *src = src_buf;
1420 guchar *dest = dest_buf;
1424 gint twidth = width;
1426 gushort *q = (gushort *)dest;
1432 guint tr1, tg1, tb1;
1436 /* This is fast, and corresponds to what composite() above does
1437 * if we converted to 8-bit first.
1439 tr = (tmp & 0xf800);
1440 tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1441 tg = (tmp & 0x07e0);
1442 tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1443 tb = (tmp & 0x001f);
1444 tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1446 *q = (((tr1 + (tr1 >> 8)) & 0xf800) |
1447 (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) |
1448 ((tb1 + (tb1 >> 8)) >> 11));
1450 /* This version correspond to the result we get with XRENDER -
1451 * a bit of precision is lost since we convert to 8 bit after premultiplying
1452 * instead of at the end
1454 guint tr2, tg2, tb2;
1455 guint tr3, tg3, tb3;
1457 tr = (tmp & 0xf800);
1458 tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1459 tr2 = a * p[0] + 0x80;
1460 tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8);
1462 tg = (tmp & 0x07e0);
1463 tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1464 tg2 = a * p[0] + 0x80;
1465 tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8);
1467 tb = (tmp & 0x001f);
1468 tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1469 tb2 = a * p[0] + 0x80;
1470 tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8);
1472 *q = (((tr3 & 0xf8) << 8) |
1473 ((tg3 & 0xfc) << 3) |
1481 src += src_rowstride;
1482 dest += dest_rowstride;
1487 gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
1496 GdkRgbDither dither,
1500 GdkPixbuf *composited = NULL;
1501 gint dwidth, dheight;
1504 GdkRectangle tmp_rect;
1506 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1507 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
1508 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
1509 g_return_if_fail (pixbuf->bits_per_sample == 8);
1511 g_return_if_fail (drawable != NULL);
1514 width = pixbuf->width;
1516 height = pixbuf->height;
1518 g_return_if_fail (width >= 0 && height >= 0);
1519 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
1520 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
1522 /* Clip to the drawable; this is required for get_from_drawable() so
1523 * can't be done implicitly
1540 gdk_drawable_get_size (drawable, &dwidth, &dheight);
1542 if ((dest_x + width) > dwidth)
1543 width = dwidth - dest_x;
1545 if ((dest_y + height) > dheight)
1546 height = dheight - dest_y;
1548 if (width <= 0 || height <= 0)
1551 /* Clip to the clip region; this avoids getting more
1552 * image data from the server than we need to.
1555 tmp_rect.x = dest_x;
1556 tmp_rect.y = dest_y;
1557 tmp_rect.width = width;
1558 tmp_rect.height = height;
1560 drect = gdk_region_rectangle (&tmp_rect);
1561 clip = gdk_drawable_get_clip_region (drawable);
1563 gdk_region_intersect (drect, clip);
1565 gdk_region_get_clipbox (drect, &tmp_rect);
1567 gdk_region_destroy (drect);
1568 gdk_region_destroy (clip);
1570 if (tmp_rect.width == 0 ||
1571 tmp_rect.height == 0)
1576 gc = _gdk_drawable_get_scratch_gc (drawable, FALSE);
1578 if (pixbuf->has_alpha)
1580 GdkVisual *visual = gdk_drawable_get_visual (drawable);
1581 void (*composite_func) (guchar *src_buf,
1584 gint dest_rowstride,
1585 GdkByteOrder dest_byte_order,
1587 gint height) = NULL;
1589 /* First we see if we have a visual-specific composition function that can composite
1590 * the pixbuf data directly onto the image
1594 gint bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_drawable_get_display (drawable),
1597 if (visual->byte_order == (G_BYTE_ORDER == G_BIG_ENDIAN ? GDK_MSB_FIRST : GDK_LSB_FIRST) &&
1598 visual->depth == 16 &&
1599 visual->red_mask == 0xf800 &&
1600 visual->green_mask == 0x07e0 &&
1601 visual->blue_mask == 0x001f)
1602 composite_func = composite_565;
1603 else if (visual->depth == 24 && bits_per_pixel == 32 &&
1604 visual->red_mask == 0xff0000 &&
1605 visual->green_mask == 0x00ff00 &&
1606 visual->blue_mask == 0x0000ff)
1607 composite_func = composite_0888;
1610 /* We can't use our composite func if we are required to dither
1612 if (composite_func && !(dither == GDK_RGB_DITHER_MAX && visual->depth != 24))
1615 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1617 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1618 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1622 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1624 GdkImage *image = _gdk_image_get_scratch (gdk_drawable_get_screen (drawable),
1626 gdk_drawable_get_depth (drawable), &xs0, &ys0);
1628 gdk_drawable_copy_to_image (drawable, image,
1629 dest_x + x0, dest_y + y0,
1632 (*composite_func) (pixbuf->pixels + (src_y + y0) * pixbuf->rowstride + (src_x + x0) * 4,
1634 (guchar*)image->mem + ys0 * image->bpl + xs0 * image->bpp,
1638 gdk_draw_image (drawable, gc, image,
1640 dest_x + x0, dest_y + y0,
1649 /* No special composition func, convert dest to 24 bit RGB data, composite against
1650 * that, and convert back.
1652 composited = gdk_pixbuf_get_from_drawable (NULL,
1660 composite (pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4,
1663 composited->rowstride,
1672 pixbuf = composited;
1675 if (pixbuf->n_channels == 4)
1677 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
1679 gdk_draw_rgb_32_image_dithalign (drawable, gc,
1683 buf, pixbuf->rowstride,
1684 x_dither, y_dither);
1686 else /* n_channels == 3 */
1688 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
1690 gdk_draw_rgb_image_dithalign (drawable, gc,
1694 buf, pixbuf->rowstride,
1695 x_dither, y_dither);
1700 g_object_unref (composited);
1703 /************************************************************************/
1706 * _gdk_drawable_get_scratch_gc:
1707 * @drawable: A #GdkDrawable
1708 * @graphics_exposures: Whether the returned #GdkGC should generate graphics exposures
1710 * Returns a #GdkGC suitable for drawing on @drawable. The #GdkGC has
1711 * the standard values for @drawable, except for the graphics_exposures
1712 * field which is determined by the @graphics_exposures parameter.
1714 * The foreground color of the returned #GdkGC is undefined. The #GdkGC
1715 * must not be altered in any way, except to change its foreground color.
1717 * Return value: A #GdkGC suitable for drawing on @drawable
1722 _gdk_drawable_get_scratch_gc (GdkDrawable *drawable,
1723 gboolean graphics_exposures)
1728 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1730 screen = gdk_drawable_get_screen (drawable);
1732 g_return_val_if_fail (!screen->closed, NULL);
1734 depth = gdk_drawable_get_depth (drawable) - 1;
1736 if (graphics_exposures)
1738 if (!screen->exposure_gcs[depth])
1741 GdkGCValuesMask mask;
1743 values.graphics_exposures = TRUE;
1744 mask = GDK_GC_EXPOSURES;
1746 screen->exposure_gcs[depth] =
1747 gdk_gc_new_with_values (drawable, &values, mask);
1750 return screen->exposure_gcs[depth];
1754 if (!screen->normal_gcs[depth])
1756 screen->normal_gcs[depth] =
1757 gdk_gc_new (drawable);
1760 return screen->normal_gcs[depth];
1764 #define __GDK_DRAW_C__
1765 #include "gdkaliasdef.c"