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 "gdkdrawable.h"
28 #include "gdkinternals.h"
29 #include "gdkwindow.h"
30 #include "gdkscreen.h"
31 #include "gdk-pixbuf-private.h"
32 #include "gdkpixbuf.h"
34 static GdkImage* gdk_drawable_real_get_image (GdkDrawable *drawable,
39 static GdkDrawable* gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
44 gint *composite_x_offset,
45 gint *composite_y_offset);
46 static GdkRegion * gdk_drawable_real_get_visible_region (GdkDrawable *drawable);
47 static void gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
60 static void gdk_drawable_class_init (GdkDrawableClass *klass);
63 gdk_drawable_get_type (void)
65 static GType object_type = 0;
69 static const GTypeInfo object_info =
71 sizeof (GdkDrawableClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) gdk_drawable_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) NULL,
82 object_type = g_type_register_static (G_TYPE_OBJECT,
85 G_TYPE_FLAG_ABSTRACT);
92 gdk_drawable_class_init (GdkDrawableClass *klass)
94 klass->get_image = gdk_drawable_real_get_image;
95 klass->get_composite_drawable = gdk_drawable_real_get_composite_drawable;
96 /* Default implementation for clip and visible region is the same */
97 klass->get_clip_region = gdk_drawable_real_get_visible_region;
98 klass->get_visible_region = gdk_drawable_real_get_visible_region;
99 klass->draw_pixbuf = gdk_drawable_real_draw_pixbuf;
102 /* Manipulation of drawables
106 * gdk_drawable_set_data:
107 * @drawable: a #GdkDrawable
108 * @key: name to store the data under
109 * @data: arbitrary data
110 * @destroy_func: function to free @data, or %NULL
112 * This function is equivalent to g_object_set_data(),
113 * the #GObject variant should be used instead.
117 gdk_drawable_set_data (GdkDrawable *drawable,
120 GDestroyNotify destroy_func)
122 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
124 g_object_set_qdata_full (G_OBJECT (drawable),
125 g_quark_from_string (key),
131 * gdk_drawable_get_data:
132 * @drawable: a #GdkDrawable
133 * @key: name the data was stored under
135 * Equivalent to g_object_get_data(); the #GObject variant should be
138 * Return value: the data stored at @key
141 gdk_drawable_get_data (GdkDrawable *drawable,
144 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
146 return g_object_get_qdata (G_OBJECT (drawable),
147 g_quark_try_string (key));
151 * gdk_drawable_get_size:
152 * @drawable: a #GdkDrawable
153 * @width: location to store drawable's width, or %NULL
154 * @height: location to store drawable's height, or %NULL
156 * Fills *@width and *@height with the size of @drawable.
157 * @width or @height can be %NULL if you only want the other one.
159 * On the X11 platform, if @drawable is a #GdkWindow, the returned
160 * size is the size reported in the most-recently-processed configure
161 * event, rather than the current size on the X server.
165 gdk_drawable_get_size (GdkDrawable *drawable,
169 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
171 GDK_DRAWABLE_GET_CLASS (drawable)->get_size (drawable, width, height);
175 * gdk_drawable_get_visual:
176 * @drawable: a #GdkDrawable
178 * Gets the #GdkVisual describing the pixel format of @drawable.
180 * Return value: a #GdkVisual
183 gdk_drawable_get_visual (GdkDrawable *drawable)
185 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
187 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visual (drawable);
191 * gdk_drawable_get_depth:
192 * @drawable: a #GdkDrawable
194 * Obtains the bit depth of the drawable, that is, the number of bits
195 * that make up a pixel in the drawable's visual. Examples are 8 bits
196 * per pixel, 24 bits per pixel, etc.
198 * Return value: number of bits per pixel
201 gdk_drawable_get_depth (GdkDrawable *drawable)
203 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), 0);
205 return GDK_DRAWABLE_GET_CLASS (drawable)->get_depth (drawable);
208 * gdk_drawable_get_screen:
209 * @drawable: a #GdkDrawable
211 * Gets the #GdkScreen associated with a #GdkDrawable.
213 * Return value: the #GdkScreen associated with @drawable
218 gdk_drawable_get_screen(GdkDrawable *drawable)
220 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
222 return GDK_DRAWABLE_GET_CLASS (drawable)->get_screen (drawable);
226 * gdk_drawable_get_display:
227 * @drawable: a #GdkDrawable
229 * Gets the #GdkDisplay associated with a #GdkDrawable.
231 * Return value: the #GdkDisplay associated with @drawable
236 gdk_drawable_get_display (GdkDrawable *drawable)
238 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
240 return gdk_screen_get_display (gdk_drawable_get_screen (drawable));
244 * gdk_drawable_set_colormap:
245 * @drawable: a #GdkDrawable
246 * @colormap: a #GdkColormap
248 * Sets the colormap associated with @drawable. Normally this will
249 * happen automatically when the drawable is created; you only need to
250 * use this function if the drawable-creating function did not have a
251 * way to determine the colormap, and you then use drawable operations
252 * that require a colormap. The colormap for all drawables and
253 * graphics contexts you intend to use together should match. i.e.
254 * when using a #GdkGC to draw to a drawable, or copying one drawable
255 * to another, the colormaps should match.
259 gdk_drawable_set_colormap (GdkDrawable *drawable,
262 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
263 g_return_if_fail (cmap == NULL || gdk_drawable_get_depth (drawable)
264 == cmap->visual->depth);
266 GDK_DRAWABLE_GET_CLASS (drawable)->set_colormap (drawable, cmap);
270 * gdk_drawable_get_colormap:
271 * @drawable: a #GdkDrawable
273 * Gets the colormap for @drawable, if one is set; returns
276 * Return value: the colormap, or %NULL
279 gdk_drawable_get_colormap (GdkDrawable *drawable)
281 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
283 return GDK_DRAWABLE_GET_CLASS (drawable)->get_colormap (drawable);
288 * @drawable: a #GdkDrawable
290 * Deprecated equivalent of calling g_object_ref() on @drawable.
291 * (Drawables were not objects in previous versions of GDK.)
293 * Return value: the same @drawable passed in
296 gdk_drawable_ref (GdkDrawable *drawable)
298 return (GdkDrawable *) g_object_ref (drawable);
302 * gdk_drawable_unref:
303 * @drawable: a #GdkDrawable
305 * Deprecated equivalent of calling g_object_unref() on @drawable.
309 gdk_drawable_unref (GdkDrawable *drawable)
311 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
313 g_object_unref (drawable);
321 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
323 * @x: the x coordinate of the point.
324 * @y: the y coordinate of the point.
326 * Draws a point, using the foreground color and other attributes of
330 gdk_draw_point (GdkDrawable *drawable,
337 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
338 g_return_if_fail (GDK_IS_GC (gc));
343 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, &point, 1);
348 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
350 * @x1_: the x coordinate of the start point.
351 * @y1_: the y coordinate of the start point.
352 * @x2_: the x coordinate of the end point.
353 * @y2_: the y coordinate of the end point.
355 * Draws a line, using the foreground color and other attributes of
359 gdk_draw_line (GdkDrawable *drawable,
368 g_return_if_fail (drawable != NULL);
369 g_return_if_fail (gc != NULL);
370 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
371 g_return_if_fail (GDK_IS_GC (gc));
377 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, &segment, 1);
381 * gdk_draw_rectangle:
382 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
384 * @filled: %TRUE if the rectangle should be filled.
385 * @x: the x coordinate of the left edge of the rectangle.
386 * @y: the y coordinate of the top edge of the rectangle.
387 * @width: the width of the rectangle.
388 * @height: the height of the rectangle.
390 * Draws a rectangular outline or filled rectangle, using the foreground color
391 * and other attributes of the #GdkGC.
393 * A rectangle drawn filled is 1 pixel smaller in both dimensions than a
394 * rectangle outlined. Calling
395 * <literal>gdk_draw_rectangle (window, gc, TRUE, 0, 0, 20, 20)</literal>
396 * results in a filled rectangle 20 pixels wide and 20 pixels high. Calling
397 * <literal>gdk_draw_rectangle (window, gc, FALSE, 0, 0, 20, 20)</literal>
398 * results in an outlined rectangle with corners at (0, 0), (0, 20), (20, 20),
399 * and (20, 0), which makes it 21 pixels wide and 21 pixels high.
402 gdk_draw_rectangle (GdkDrawable *drawable,
410 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
411 g_return_if_fail (GDK_IS_GC (gc));
413 if (width < 0 || height < 0)
418 gdk_drawable_get_size (drawable, &real_width, &real_height);
423 height = real_height;
426 GDK_DRAWABLE_GET_CLASS (drawable)->draw_rectangle (drawable, gc, filled, x, y,
432 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
434 * @filled: %TRUE if the arc should be filled, producing a 'pie slice'.
435 * @x: the x coordinate of the left edge of the bounding rectangle.
436 * @y: the y coordinate of the top edge of the bounding rectangle.
437 * @width: the width of the bounding rectangle.
438 * @height: the height of the bounding rectangle.
439 * @angle1: the start angle of the arc, relative to the 3 o'clock position,
440 * counter-clockwise, in 1/64ths of a degree.
441 * @angle2: the end angle of the arc, relative to @angle1, in 1/64ths
444 * Draws an arc or a filled 'pie slice'. The arc is defined by the bounding
445 * rectangle of the entire ellipse, and the start and end angles of the part
446 * of the ellipse to be drawn.
449 gdk_draw_arc (GdkDrawable *drawable,
459 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
460 g_return_if_fail (GDK_IS_GC (gc));
462 if (width < 0 || height < 0)
467 gdk_drawable_get_size (drawable, &real_width, &real_height);
472 height = real_height;
475 GDK_DRAWABLE_GET_CLASS (drawable)->draw_arc (drawable, gc, filled,
476 x, y, width, height, angle1, angle2);
481 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
483 * @filled: %TRUE if the polygon should be filled. The polygon is closed
484 * automatically, connecting the last point to the first point if
486 * @points: an array of #GdkPoint structures specifying the points making
488 * @npoints: the number of points.
490 * Draws an outlined or filled polygon.
493 gdk_draw_polygon (GdkDrawable *drawable,
499 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
500 g_return_if_fail (GDK_IS_GC (gc));
502 GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
508 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
510 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
514 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
517 * @x: the x coordinate of the left edge of the text.
518 * @y: the y coordinate of the baseline of the text.
519 * @string: the string of characters to draw.
521 * Draws a string of characters in the given font or fontset.
523 * Deprecated: Use gdk_draw_layout() instead.
526 gdk_draw_string (GdkDrawable *drawable,
533 gdk_draw_text (drawable, font, gc, x, y, string, _gdk_font_strlen (font, string));
538 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
540 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
544 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
547 * @x: the x coordinate of the left edge of the text.
548 * @y: the y coordinate of the baseline of the text.
549 * @text: the characters to draw.
550 * @text_length: the number of characters of @text to draw.
552 * Draws a number of characters in the given font or fontset.
554 * Deprecated: Use gdk_draw_layout() instead.
557 gdk_draw_text (GdkDrawable *drawable,
565 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
566 g_return_if_fail (font != NULL);
567 g_return_if_fail (GDK_IS_GC (gc));
568 g_return_if_fail (text != NULL);
570 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text (drawable, font, gc, x, y, text, text_length);
575 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
578 * @x: the x coordinate of the left edge of the text.
579 * @y: the y coordinate of the baseline of the text.
580 * @text: the wide characters to draw.
581 * @text_length: the number of characters to draw.
583 * Draws a number of wide characters using the given font of fontset.
584 * If the font is a 1-byte font, the string is converted into 1-byte
585 * characters (discarding the high bytes) before output.
587 * Deprecated: Use gdk_draw_layout() instead.
590 gdk_draw_text_wc (GdkDrawable *drawable,
595 const GdkWChar *text,
598 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
599 g_return_if_fail (font != NULL);
600 g_return_if_fail (GDK_IS_GC (gc));
601 g_return_if_fail (text != NULL);
603 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text_wc (drawable, font, gc, x, y, text, text_length);
608 * @drawable: a #GdkDrawable
609 * @gc: a #GdkGC sharing the drawable's visual and colormap
610 * @src: another #GdkDrawable
611 * @xsrc: X position in @src of rectangle to draw
612 * @ysrc: Y position in @src of rectangle to draw
613 * @xdest: X position in @drawable where the rectangle should be drawn
614 * @ydest: Y position in @drawable where the rectangle should be drawn
615 * @width: width of rectangle to draw, or -1 for entire @src width
616 * @height: height of rectangle to draw, or -1 for entire @src height
618 * Copies the @width x @height region of @src at coordinates (@xsrc,
619 * @ysrc) to coordinates (@xdest, @ydest) in @drawable.
620 * @width and/or @height may be given as -1, in which case the entire
621 * @src drawable will be copied.
623 * Most fields in @gc are not used for this operation, but notably the
624 * clip mask or clip region will be honored.
626 * The source and destination drawables must have the same visual and
627 * colormap, or errors will result. (On X11, failure to match
628 * visual/colormap results in a BadMatch error from the X server.)
629 * A common cause of this problem is an attempt to draw a bitmap to
630 * a color drawable. The way to draw a bitmap is to set the
631 * bitmap as a clip mask on your #GdkGC, then use gdk_draw_rectangle()
632 * to draw a rectangle clipped to the bitmap.
635 gdk_draw_drawable (GdkDrawable *drawable,
645 GdkDrawable *composite;
646 gint composite_x_offset = 0;
647 gint composite_y_offset = 0;
649 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
650 g_return_if_fail (src != NULL);
651 g_return_if_fail (GDK_IS_GC (gc));
653 if (width < 0 || height < 0)
658 gdk_drawable_get_size (src, &real_width, &real_height);
663 height = real_height;
668 GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
672 &composite_y_offset);
675 GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc, composite,
676 xsrc - composite_x_offset,
677 ysrc - composite_y_offset,
681 g_object_unref (composite);
686 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
688 * @image: the #GdkImage to draw.
689 * @xsrc: the left edge of the source rectangle within @image.
690 * @ysrc: the top of the source rectangle within @image.
691 * @xdest: the x coordinate of the destination within @drawable.
692 * @ydest: the y coordinate of the destination within @drawable.
693 * @width: the width of the area to be copied, or -1 to make the area
694 * extend to the right edge of @image.
695 * @height: the height of the area to be copied, or -1 to make the area
696 * extend to the bottom edge of @image.
698 * Draws a #GdkImage onto a drawable.
699 * The depth of the #GdkImage must match the depth of the #GdkDrawable.
702 gdk_draw_image (GdkDrawable *drawable,
712 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
713 g_return_if_fail (image != NULL);
714 g_return_if_fail (GDK_IS_GC (gc));
717 width = image->width;
719 height = image->height;
721 GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
722 xdest, ydest, width, height);
727 * @drawable: Destination drawable.
728 * @gc: a #GdkGC, used for clipping, or %NULL
729 * @pixbuf: a #GdkPixbuf
730 * @src_x: Source X coordinate within pixbuf.
731 * @src_y: Source Y coordinates within pixbuf.
732 * @dest_x: Destination X coordinate within drawable.
733 * @dest_y: Destination Y coordinate within drawable.
734 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
735 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
736 * @dither: Dithering mode for #GdkRGB.
737 * @x_dither: X offset for dither.
738 * @y_dither: Y offset for dither.
740 * Renders a rectangular portion of a pixbuf to a drawable. The destination
741 * drawable must have a colormap. All windows have a colormap, however, pixmaps
742 * only have colormap by default if they were created with a non-%NULL window
743 * argument. Otherwise a colormap must be set on them with
744 * gdk_drawable_set_colormap().
746 * On older X servers, rendering pixbufs with an alpha channel involves round
747 * trips to the X server, and may be somewhat slow.
752 gdk_draw_pixbuf (GdkDrawable *drawable,
765 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
766 g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
767 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
770 width = gdk_pixbuf_get_width (pixbuf);
772 height = gdk_pixbuf_get_height (pixbuf);
774 GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc, pixbuf,
775 src_x, src_y, dest_x, dest_y, width, height,
776 dither, x_dither, y_dither);
781 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
783 * @points: an array of #GdkPoint structures.
784 * @npoints: the number of points to be drawn.
786 * Draws a number of points, using the foreground color and other
787 * attributes of the #GdkGC.
790 gdk_draw_points (GdkDrawable *drawable,
795 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
796 g_return_if_fail ((points != NULL) && (npoints > 0));
797 g_return_if_fail (GDK_IS_GC (gc));
798 g_return_if_fail (npoints >= 0);
803 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
808 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
810 * @segs: an array of #GdkSegment structures specifying the start and
811 * end points of the lines to be drawn.
812 * @nsegs: the number of line segments to draw, i.e. the size of the
815 * Draws a number of unconnected lines.
818 gdk_draw_segments (GdkDrawable *drawable,
823 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
828 g_return_if_fail (segs != NULL);
829 g_return_if_fail (GDK_IS_GC (gc));
830 g_return_if_fail (nsegs >= 0);
832 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
837 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
839 * @points: an array of #GdkPoint structures specifying the endpoints of the
840 * @npoints: the size of the @points array.
842 * Draws a series of lines connecting the given points.
843 * The way in which joins between lines are draw is determined by the
844 * #GdkCapStyle value in the #GdkGC. This can be set with
845 * gdk_gc_set_line_attributes().
848 gdk_draw_lines (GdkDrawable *drawable,
853 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
854 g_return_if_fail (points != NULL);
855 g_return_if_fail (GDK_IS_GC (gc));
856 g_return_if_fail (npoints >= 0);
861 GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
866 * @drawable: a #GdkDrawable
868 * @font: font to be used
869 * @x: X coordinate of baseline origin
870 * @y: Y coordinate of baseline origin
871 * @glyphs: glyphs to render
873 * This is a low-level function; 99% of text rendering should be done
874 * using gdk_draw_layout() instead.
876 * A glyph is a character in a font. This function draws a sequence of
877 * glyphs. To obtain a sequence of glyphs you have to understand a
878 * lot about internationalized text handling, which you don't want to
879 * understand; thus, use gdk_draw_layout() instead of this function,
880 * gdk_draw_layout() handles the details.
884 gdk_draw_glyphs (GdkDrawable *drawable,
889 PangoGlyphString *glyphs)
891 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
892 g_return_if_fail (GDK_IS_GC (gc));
895 GDK_DRAWABLE_GET_CLASS (drawable)->draw_glyphs (drawable, gc, font, x, y, glyphs);
900 * gdk_drawable_copy_to_image:
901 * @drawable: a #GdkDrawable
902 * @image: a #GdkDrawable, or %NULL if a new @image should be created.
903 * @src_x: x coordinate on @drawable
904 * @src_y: y coordinate on @drawable
905 * @dest_x: x coordinate within @image. Must be 0 if @image is %NULL
906 * @dest_y: y coordinate within @image. Must be 0 if @image is %NULL
907 * @width: width of region to get
908 * @height: height or region to get
910 * Copies a portion of @drawable into the client side image structure
911 * @image. If @image is %NULL, creates a new image of size @width x @height
912 * and copies into that. See gdk_drawable_get_image() for further details.
914 * Return value: @image, or a new a #GdkImage containing the contents
920 gdk_drawable_copy_to_image (GdkDrawable *drawable,
929 GdkDrawable *composite;
930 gint composite_x_offset = 0;
931 gint composite_y_offset = 0;
935 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
936 g_return_val_if_fail (src_x >= 0, NULL);
937 g_return_val_if_fail (src_y >= 0, NULL);
939 /* FIXME? Note race condition since we get the size then
940 * get the image, and the size may have changed.
943 if (width < 0 || height < 0)
944 gdk_drawable_get_size (drawable,
945 width < 0 ? &width : NULL,
946 height < 0 ? &height : NULL);
949 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
953 &composite_y_offset);
955 retval = GDK_DRAWABLE_GET_CLASS (composite)->_copy_to_image (composite,
957 src_x - composite_x_offset,
958 src_y - composite_y_offset,
962 g_object_unref (composite);
964 if (!image && retval)
966 cmap = gdk_drawable_get_colormap (drawable);
969 gdk_image_set_colormap (retval, cmap);
976 * gdk_drawable_get_image:
977 * @drawable: a #GdkDrawable
978 * @x: x coordinate on @drawable
979 * @y: y coordinate on @drawable
980 * @width: width of region to get
981 * @height: height or region to get
983 * A #GdkImage stores client-side image data (pixels). In contrast,
984 * #GdkPixmap and #GdkWindow are server-side
985 * objects. gdk_drawable_get_image() obtains the pixels from a
986 * server-side drawable as a client-side #GdkImage. The format of a
987 * #GdkImage depends on the #GdkVisual of the current display, which
988 * makes manipulating #GdkImage extremely difficult; therefore, in
989 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
990 * this lower-level function. A #GdkPixbuf contains image data in a
991 * canonicalized RGB format, rather than a display-dependent format.
992 * Of course, there's a convenience vs. speed tradeoff here, so you'll
993 * want to think about what makes sense for your application.
995 * @x, @y, @width, and @height define the region of @drawable to
996 * obtain as an image.
998 * You would usually copy image data to the client side if you intend
999 * to examine the values of individual pixels, for example to darken
1000 * an image or add a red tint. It would be prohibitively slow to
1001 * make a round-trip request to the windowing system for each pixel,
1002 * so instead you get all of them at once, modify them, then copy
1003 * them all back at once.
1005 * If the X server or other windowing system backend is on the local
1006 * machine, this function may use shared memory to avoid copying
1009 * If the source drawable is a #GdkWindow and partially offscreen
1010 * or obscured, then the obscured portions of the returned image
1011 * will contain undefined data.
1013 * Return value: a #GdkImage containing the contents of @drawable
1016 gdk_drawable_get_image (GdkDrawable *drawable,
1022 GdkDrawable *composite;
1023 gint composite_x_offset = 0;
1024 gint composite_y_offset = 0;
1028 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1029 g_return_val_if_fail (x >= 0, NULL);
1030 g_return_val_if_fail (y >= 0, NULL);
1032 /* FIXME? Note race condition since we get the size then
1033 * get the image, and the size may have changed.
1036 if (width < 0 || height < 0)
1037 gdk_drawable_get_size (drawable,
1038 width < 0 ? &width : NULL,
1039 height < 0 ? &height : NULL);
1042 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
1045 &composite_x_offset,
1046 &composite_y_offset);
1048 retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
1049 x - composite_x_offset,
1050 y - composite_y_offset,
1053 g_object_unref (composite);
1055 cmap = gdk_drawable_get_colormap (drawable);
1058 gdk_image_set_colormap (retval, cmap);
1064 gdk_drawable_real_get_image (GdkDrawable *drawable,
1070 return gdk_drawable_copy_to_image (drawable, NULL, x, y, 0, 0, width, height);
1074 gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
1079 gint *composite_x_offset,
1080 gint *composite_y_offset)
1082 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1084 *composite_x_offset = 0;
1085 *composite_y_offset = 0;
1087 return g_object_ref (drawable);
1091 * gdk_drawable_get_clip_region:
1092 * @drawable: a #GdkDrawable
1094 * Computes the region of a drawable that potentially can be written
1095 * to by drawing primitives. This region will not take into account
1096 * the clip region for the GC, and may also not take into account
1097 * other factors such as if the window is obscured by other windows,
1098 * but no area outside of this region will be affected by drawing
1101 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1102 * when you are done.
1105 gdk_drawable_get_clip_region (GdkDrawable *drawable)
1107 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1109 return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
1113 * gdk_drawable_get_visible_region:
1116 * Computes the region of a drawable that is potentially visible.
1117 * This does not necessarily take into account if the window is
1118 * obscured by other windows, but no area outside of this region
1121 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1122 * when you are done.
1125 gdk_drawable_get_visible_region (GdkDrawable *drawable)
1127 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1129 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
1133 gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
1140 gdk_drawable_get_size (drawable, &rect.width, &rect.height);
1142 return gdk_region_rectangle (&rect);
1146 composite (guchar *src_buf,
1149 gint dest_rowstride,
1153 guchar *src = src_buf;
1154 guchar *dest = dest_buf;
1158 gint twidth = width;
1167 t = a * p[0] + (255 - a) * q[0] + 0x80;
1168 q[0] = (t + (t >> 8)) >> 8;
1169 t = a * p[1] + (255 - a) * q[1] + 0x80;
1170 q[1] = (t + (t >> 8)) >> 8;
1171 t = a * p[2] + (255 - a) * q[2] + 0x80;
1172 q[2] = (t + (t >> 8)) >> 8;
1178 src += src_rowstride;
1179 dest += dest_rowstride;
1184 composite_0888 (guchar *src_buf,
1187 gint dest_rowstride,
1188 GdkByteOrder dest_byte_order,
1192 guchar *src = src_buf;
1193 guchar *dest = dest_buf;
1197 gint twidth = width;
1201 if (dest_byte_order == GDK_LSB_FIRST)
1207 t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80;
1208 q[0] = (t + (t >> 8)) >> 8;
1209 t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80;
1210 q[1] = (t + (t >> 8)) >> 8;
1211 t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80;
1212 q[2] = (t + (t >> 8)) >> 8;
1223 t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80;
1224 q[1] = (t + (t >> 8)) >> 8;
1225 t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80;
1226 q[2] = (t + (t >> 8)) >> 8;
1227 t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80;
1228 q[3] = (t + (t >> 8)) >> 8;
1234 src += src_rowstride;
1235 dest += dest_rowstride;
1240 composite_565 (guchar *src_buf,
1243 gint dest_rowstride,
1244 GdkByteOrder dest_byte_order,
1248 guchar *src = src_buf;
1249 guchar *dest = dest_buf;
1253 gint twidth = width;
1255 gushort *q = (gushort *)dest;
1261 guint tr1, tg1, tb1;
1265 /* This is fast, and corresponds to what composite() above does
1266 * if we converted to 8-bit first.
1268 tr = (tmp & 0xf800);
1269 tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1270 tg = (tmp & 0x07e0);
1271 tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1272 tb = (tmp & 0x001f);
1273 tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1275 *q = (((tr1 + (tr1 >> 8)) & 0xf800) |
1276 (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) |
1277 ((tb1 + (tb1 >> 8)) >> 11));
1279 /* This version correspond to the result we get with XRENDER -
1280 * a bit of precision is lost since we convert to 8 bit after premultiplying
1281 * instead of at the end
1283 guint tr2, tg2, tb2;
1284 guint tr3, tg3, tb3;
1286 tr = (tmp & 0xf800);
1287 tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1288 tr2 = a * p[0] + 0x80;
1289 tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8);
1291 tg = (tmp & 0x07e0);
1292 tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1293 tg2 = a * p[0] + 0x80;
1294 tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8);
1296 tb = (tmp & 0x001f);
1297 tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1298 tb2 = a * p[0] + 0x80;
1299 tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8);
1301 *q = (((tr3 & 0xf8) << 8) |
1302 ((tg3 & 0xfc) << 3) |
1310 src += src_rowstride;
1311 dest += dest_rowstride;
1316 gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
1325 GdkRgbDither dither,
1329 gboolean free_gc = FALSE;
1330 GdkPixbuf *composited = NULL;
1331 gint dwidth, dheight;
1334 GdkRectangle tmp_rect;
1336 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1337 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
1338 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
1339 g_return_if_fail (pixbuf->bits_per_sample == 8);
1341 g_return_if_fail (drawable != NULL);
1344 width = pixbuf->width;
1346 height = pixbuf->height;
1348 g_return_if_fail (width >= 0 && height >= 0);
1349 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
1350 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
1352 /* Clip to the drawable; this is required for get_from_drawable() so
1353 * can't be done implicitly
1370 gdk_drawable_get_size (drawable, &dwidth, &dheight);
1372 if ((dest_x + width) > dwidth)
1373 width = dwidth - dest_x;
1375 if ((dest_y + height) > dheight)
1376 height = dheight - dest_y;
1378 if (width <= 0 || height <= 0)
1381 /* Clip to the clip region; this avoids getting more
1382 * image data from the server than we need to.
1385 tmp_rect.x = dest_x;
1386 tmp_rect.y = dest_y;
1387 tmp_rect.width = width;
1388 tmp_rect.height = height;
1390 drect = gdk_region_rectangle (&tmp_rect);
1391 clip = gdk_drawable_get_clip_region (drawable);
1393 gdk_region_intersect (drect, clip);
1395 gdk_region_get_clipbox (drect, &tmp_rect);
1397 gdk_region_destroy (drect);
1398 gdk_region_destroy (clip);
1400 if (tmp_rect.width == 0 ||
1401 tmp_rect.height == 0)
1408 gc = gdk_gc_new (drawable);
1412 if (pixbuf->has_alpha)
1414 GdkVisual *visual = gdk_drawable_get_visual (drawable);
1415 void (*composite_func) (guchar *src_buf,
1418 gint dest_rowstride,
1419 GdkByteOrder dest_byte_order,
1421 gint height) = NULL;
1423 /* First we see if we have a visual-specific composition function that can composite
1424 * the pixbuf data directly onto the image
1428 gint bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_drawable_get_display (drawable),
1431 if (visual->byte_order == (G_BYTE_ORDER == G_BIG_ENDIAN ? GDK_MSB_FIRST : GDK_LSB_FIRST) &&
1432 visual->depth == 16 &&
1433 visual->red_mask == 0xf800 &&
1434 visual->green_mask == 0x07e0 &&
1435 visual->blue_mask == 0x001f)
1436 composite_func = composite_565;
1437 else if (visual->depth == 24 && bits_per_pixel == 32 &&
1438 visual->red_mask == 0xff0000 &&
1439 visual->green_mask == 0x00ff00 &&
1440 visual->blue_mask == 0x0000ff)
1441 composite_func = composite_0888;
1444 /* We can't use our composite func if we are required to dither
1446 if (composite_func && !(dither == GDK_RGB_DITHER_MAX && visual->depth != 24))
1449 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1451 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1452 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1456 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1458 GdkImage *image = _gdk_image_get_scratch (gdk_drawable_get_screen (drawable),
1460 gdk_drawable_get_depth (drawable), &xs0, &ys0);
1462 gdk_drawable_copy_to_image (drawable, image,
1463 dest_x + x0, dest_y + y0,
1466 (*composite_func) (pixbuf->pixels + (src_y + y0) * pixbuf->rowstride + (src_x + x0) * 4,
1468 (guchar*)image->mem + ys0 * image->bpl + xs0 * image->bpp,
1472 gdk_draw_image (drawable, gc, image,
1474 dest_x + x0, dest_y + y0,
1483 /* No special composition func, convert dest to 24 bit RGB data, composite against
1484 * that, and convert back.
1486 composited = gdk_pixbuf_get_from_drawable (NULL,
1494 composite (pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4,
1497 composited->rowstride,
1506 pixbuf = composited;
1509 if (pixbuf->n_channels == 4)
1511 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
1513 gdk_draw_rgb_32_image_dithalign (drawable, gc,
1517 buf, pixbuf->rowstride,
1518 x_dither, y_dither);
1520 else /* n_channels == 3 */
1522 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
1524 gdk_draw_rgb_image_dithalign (drawable, gc,
1528 buf, pixbuf->rowstride,
1529 x_dither, y_dither);
1534 g_object_unref (composited);
1537 g_object_unref (gc);