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.
524 gdk_draw_string (GdkDrawable *drawable,
531 gdk_draw_text (drawable, font, gc, x, y, string, _gdk_font_strlen (font, string));
536 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
538 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
542 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
545 * @x: the x coordinate of the left edge of the text.
546 * @y: the y coordinate of the baseline of the text.
547 * @text: the characters to draw.
548 * @text_length: the number of characters of @text to draw.
550 * Draws a number of characters in the given font or fontset.
553 gdk_draw_text (GdkDrawable *drawable,
561 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
562 g_return_if_fail (font != NULL);
563 g_return_if_fail (GDK_IS_GC (gc));
564 g_return_if_fail (text != NULL);
566 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text (drawable, font, gc, x, y, text, text_length);
571 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
574 * @x: the x coordinate of the left edge of the text.
575 * @y: the y coordinate of the baseline of the text.
576 * @text: the wide characters to draw.
577 * @text_length: the number of characters to draw.
579 * Draws a number of wide characters using the given font of fontset.
580 * If the font is a 1-byte font, the string is converted into 1-byte
581 * characters (discarding the high bytes) before output.
584 gdk_draw_text_wc (GdkDrawable *drawable,
589 const GdkWChar *text,
592 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
593 g_return_if_fail (font != NULL);
594 g_return_if_fail (GDK_IS_GC (gc));
595 g_return_if_fail (text != NULL);
597 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text_wc (drawable, font, gc, x, y, text, text_length);
602 * @drawable: a #GdkDrawable
603 * @gc: a #GdkGC sharing the drawable's visual and colormap
604 * @src: another #GdkDrawable
605 * @xsrc: X position in @src of rectangle to draw
606 * @ysrc: Y position in @src of rectangle to draw
607 * @xdest: X position in @drawable where the rectangle should be drawn
608 * @ydest: Y position in @drawable where the rectangle should be drawn
609 * @width: width of rectangle to draw, or -1 for entire @src width
610 * @height: height of rectangle to draw, or -1 for entire @src height
612 * Copies the @width x @height region of @src at coordinates (@xsrc,
613 * @ysrc) to coordinates (@xdest, @ydest) in @drawable.
614 * @width and/or @height may be given as -1, in which case the entire
615 * @src drawable will be copied.
617 * Most fields in @gc are not used for this operation, but notably the
618 * clip mask or clip region will be honored.
620 * The source and destination drawables must have the same visual and
621 * colormap, or errors will result. (On X11, failure to match
622 * visual/colormap results in a BadMatch error from the X server.)
623 * A common cause of this problem is an attempt to draw a bitmap to
624 * a color drawable. The way to draw a bitmap is to set the
625 * bitmap as a clip mask on your #GdkGC, then use gdk_draw_rectangle()
626 * to draw a rectangle clipped to the bitmap.
629 gdk_draw_drawable (GdkDrawable *drawable,
639 GdkDrawable *composite;
640 gint composite_x_offset = 0;
641 gint composite_y_offset = 0;
643 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
644 g_return_if_fail (src != NULL);
645 g_return_if_fail (GDK_IS_GC (gc));
647 if (width < 0 || height < 0)
652 gdk_drawable_get_size (src, &real_width, &real_height);
657 height = real_height;
662 GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
666 &composite_y_offset);
669 GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc, composite,
670 xsrc - composite_x_offset,
671 ysrc - composite_y_offset,
675 g_object_unref (composite);
680 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
682 * @image: the #GdkImage to draw.
683 * @xsrc: the left edge of the source rectangle within @image.
684 * @ysrc: the top of the source rectangle within @image.
685 * @xdest: the x coordinate of the destination within @drawable.
686 * @ydest: the y coordinate of the destination within @drawable.
687 * @width: the width of the area to be copied, or -1 to make the area
688 * extend to the right edge of @image.
689 * @height: the height of the area to be copied, or -1 to make the area
690 * extend to the bottom edge of @image.
692 * Draws a #GdkImage onto a drawable.
693 * The depth of the #GdkImage must match the depth of the #GdkDrawable.
696 gdk_draw_image (GdkDrawable *drawable,
706 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
707 g_return_if_fail (image != NULL);
708 g_return_if_fail (GDK_IS_GC (gc));
711 width = image->width;
713 height = image->height;
715 GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
716 xdest, ydest, width, height);
721 * @drawable: Destination drawable.
722 * @gc: a #GdkGC, used for clipping, or %NULL
723 * @pixbuf: a #GdkPixbuf
724 * @src_x: Source X coordinate within pixbuf.
725 * @src_y: Source Y coordinates within pixbuf.
726 * @dest_x: Destination X coordinate within drawable.
727 * @dest_y: Destination Y coordinate within drawable.
728 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
729 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
730 * @dither: Dithering mode for #GdkRGB.
731 * @x_dither: X offset for dither.
732 * @y_dither: Y offset for dither.
734 * Renders a rectangular portion of a pixbuf to a drawable. The destination
735 * drawable must have a colormap. All windows have a colormap, however, pixmaps
736 * only have colormap by default if they were created with a non-%NULL window
737 * argument. Otherwise a colormap must be set on them with
738 * gdk_drawable_set_colormap().
740 * On older X servers, rendering pixbufs with an alpha channel involves round
741 * trips to the X server, and may be somewhat slow.
746 gdk_draw_pixbuf (GdkDrawable *drawable,
759 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
760 g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
761 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
764 width = gdk_pixbuf_get_width (pixbuf);
766 height = gdk_pixbuf_get_height (pixbuf);
768 GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc, pixbuf,
769 src_x, src_y, dest_x, dest_y, width, height,
770 dither, x_dither, y_dither);
775 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
777 * @points: an array of #GdkPoint structures.
778 * @npoints: the number of points to be drawn.
780 * Draws a number of points, using the foreground color and other
781 * attributes of the #GdkGC.
784 gdk_draw_points (GdkDrawable *drawable,
789 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
790 g_return_if_fail ((points != NULL) && (npoints > 0));
791 g_return_if_fail (GDK_IS_GC (gc));
792 g_return_if_fail (npoints >= 0);
797 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
802 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
804 * @segs: an array of #GdkSegment structures specifying the start and
805 * end points of the lines to be drawn.
806 * @nsegs: the number of line segments to draw, i.e. the size of the
809 * Draws a number of unconnected lines.
812 gdk_draw_segments (GdkDrawable *drawable,
817 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
822 g_return_if_fail (segs != NULL);
823 g_return_if_fail (GDK_IS_GC (gc));
824 g_return_if_fail (nsegs >= 0);
826 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
831 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
833 * @points: an array of #GdkPoint structures specifying the endpoints of the
834 * @npoints: the size of the @points array.
836 * Draws a series of lines connecting the given points.
837 * The way in which joins between lines are draw is determined by the
838 * #GdkCapStyle value in the #GdkGC. This can be set with
839 * gdk_gc_set_line_attributes().
842 gdk_draw_lines (GdkDrawable *drawable,
847 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
848 g_return_if_fail (points != NULL);
849 g_return_if_fail (GDK_IS_GC (gc));
850 g_return_if_fail (npoints >= 0);
855 GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
860 * @drawable: a #GdkDrawable
862 * @font: font to be used
863 * @x: X coordinate of baseline origin
864 * @y: Y coordinate of baseline origin
865 * @glyphs: glyphs to render
867 * This is a low-level function; 99% of text rendering should be done
868 * using gdk_draw_layout() instead.
870 * A glyph is a character in a font. This function draws a sequence of
871 * glyphs. To obtain a sequence of glyphs you have to understand a
872 * lot about internationalized text handling, which you don't want to
873 * understand; thus, use gdk_draw_layout() instead of this function,
874 * gdk_draw_layout() handles the details.
878 gdk_draw_glyphs (GdkDrawable *drawable,
883 PangoGlyphString *glyphs)
885 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
886 g_return_if_fail (GDK_IS_GC (gc));
889 GDK_DRAWABLE_GET_CLASS (drawable)->draw_glyphs (drawable, gc, font, x, y, glyphs);
894 * _gdk_drawable_copy_to_image:
895 * @drawable: a #GdkDrawable
896 * @image: a #GdkDrawable, or %NULL if a new @image should be created.
897 * @src_x: x coordinate on @drawable
898 * @src_y: y coordinate on @drawable
899 * @dest_x: x coordinate within @image. Must be 0 if @image is %NULL
900 * @dest_y: y coordinate within @image. Must be 0 if @image is %NULL
901 * @width: width of region to get
902 * @height: height or region to get
904 * Copies a portion of @drawable into the client side image structure
905 * @image. If @image is %NULL, creates a new image of size @width x @height
906 * and copies into that. See gdk_drawable_get_image() for further details.
908 * Return value: @image, or a new a #GdkImage containing the contents
912 _gdk_drawable_copy_to_image (GdkDrawable *drawable,
921 GdkDrawable *composite;
922 gint composite_x_offset = 0;
923 gint composite_y_offset = 0;
927 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
928 g_return_val_if_fail (src_x >= 0, NULL);
929 g_return_val_if_fail (src_y >= 0, NULL);
931 /* FIXME? Note race condition since we get the size then
932 * get the image, and the size may have changed.
935 if (width < 0 || height < 0)
936 gdk_drawable_get_size (drawable,
937 width < 0 ? &width : NULL,
938 height < 0 ? &height : NULL);
941 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
945 &composite_y_offset);
947 retval = GDK_DRAWABLE_GET_CLASS (composite)->_copy_to_image (composite,
949 src_x - composite_x_offset,
950 src_y - composite_y_offset,
954 g_object_unref (composite);
956 if (!image && retval)
958 cmap = gdk_drawable_get_colormap (drawable);
961 gdk_image_set_colormap (retval, cmap);
968 * gdk_drawable_get_image:
969 * @drawable: a #GdkDrawable
970 * @x: x coordinate on @drawable
971 * @y: y coordinate on @drawable
972 * @width: width of region to get
973 * @height: height or region to get
975 * A #GdkImage stores client-side image data (pixels). In contrast,
976 * #GdkPixmap and #GdkWindow are server-side
977 * objects. gdk_drawable_get_image() obtains the pixels from a
978 * server-side drawable as a client-side #GdkImage. The format of a
979 * #GdkImage depends on the #GdkVisual of the current display, which
980 * makes manipulating #GdkImage extremely difficult; therefore, in
981 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
982 * this lower-level function. A #GdkPixbuf contains image data in a
983 * canonicalized RGB format, rather than a display-dependent format.
984 * Of course, there's a convenience vs. speed tradeoff here, so you'll
985 * want to think about what makes sense for your application.
987 * @x, @y, @width, and @height define the region of @drawable to
988 * obtain as an image.
990 * You would usually copy image data to the client side if you intend
991 * to examine the values of individual pixels, for example to darken
992 * an image or add a red tint. It would be prohibitively slow to
993 * make a round-trip request to the windowing system for each pixel,
994 * so instead you get all of them at once, modify them, then copy
995 * them all back at once.
997 * If the X server or other windowing system backend is on the local
998 * machine, this function may use shared memory to avoid copying
1001 * If the source drawable is a #GdkWindow and partially offscreen
1002 * or obscured, then the obscured portions of the returned image
1003 * will contain undefined data.
1005 * Return value: a #GdkImage containing the contents of @drawable
1008 gdk_drawable_get_image (GdkDrawable *drawable,
1014 GdkDrawable *composite;
1015 gint composite_x_offset = 0;
1016 gint composite_y_offset = 0;
1020 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1021 g_return_val_if_fail (x >= 0, NULL);
1022 g_return_val_if_fail (y >= 0, NULL);
1024 /* FIXME? Note race condition since we get the size then
1025 * get the image, and the size may have changed.
1028 if (width < 0 || height < 0)
1029 gdk_drawable_get_size (drawable,
1030 width < 0 ? &width : NULL,
1031 height < 0 ? &height : NULL);
1034 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
1037 &composite_x_offset,
1038 &composite_y_offset);
1040 retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
1041 x - composite_x_offset,
1042 y - composite_y_offset,
1045 g_object_unref (composite);
1047 cmap = gdk_drawable_get_colormap (drawable);
1050 gdk_image_set_colormap (retval, cmap);
1056 gdk_drawable_real_get_image (GdkDrawable *drawable,
1062 return _gdk_drawable_copy_to_image (drawable, NULL, x, y, 0, 0, width, height);
1066 gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
1071 gint *composite_x_offset,
1072 gint *composite_y_offset)
1074 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1076 *composite_x_offset = 0;
1077 *composite_y_offset = 0;
1079 return g_object_ref (drawable);
1083 * gdk_drawable_get_clip_region:
1084 * @drawable: a #GdkDrawable
1086 * Computes the region of a drawable that potentially can be written
1087 * to by drawing primitives. This region will not take into account
1088 * the clip region for the GC, and may also not take into account
1089 * other factors such as if the window is obscured by other windows,
1090 * but no area outside of this region will be affected by drawing
1093 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1094 * when you are done.
1097 gdk_drawable_get_clip_region (GdkDrawable *drawable)
1099 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1101 return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
1105 * gdk_drawable_get_visible_region:
1108 * Computes the region of a drawable that is potentially visible.
1109 * This does not necessarily take into account if the window is
1110 * obscured by other windows, but no area outside of this region
1113 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1114 * when you are done.
1117 gdk_drawable_get_visible_region (GdkDrawable *drawable)
1119 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1121 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
1125 gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
1132 gdk_drawable_get_size (drawable, &rect.width, &rect.height);
1134 return gdk_region_rectangle (&rect);
1138 composite (guchar *src_buf,
1141 gint dest_rowstride,
1145 guchar *src = src_buf;
1146 guchar *dest = dest_buf;
1150 gint twidth = width;
1159 t = a * p[0] + (255 - a) * q[0] + 0x80;
1160 q[0] = (t + (t >> 8)) >> 8;
1161 t = a * p[1] + (255 - a) * q[1] + 0x80;
1162 q[1] = (t + (t >> 8)) >> 8;
1163 t = a * p[2] + (255 - a) * q[2] + 0x80;
1164 q[2] = (t + (t >> 8)) >> 8;
1170 src += src_rowstride;
1171 dest += dest_rowstride;
1176 composite_0888 (guchar *src_buf,
1179 gint dest_rowstride,
1180 GdkByteOrder dest_byte_order,
1184 guchar *src = src_buf;
1185 guchar *dest = dest_buf;
1189 gint twidth = width;
1193 if (dest_byte_order == GDK_LSB_FIRST)
1199 t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80;
1200 q[0] = (t + (t >> 8)) >> 8;
1201 t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80;
1202 q[1] = (t + (t >> 8)) >> 8;
1203 t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80;
1204 q[2] = (t + (t >> 8)) >> 8;
1215 t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80;
1216 q[1] = (t + (t >> 8)) >> 8;
1217 t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80;
1218 q[2] = (t + (t >> 8)) >> 8;
1219 t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80;
1220 q[3] = (t + (t >> 8)) >> 8;
1226 src += src_rowstride;
1227 dest += dest_rowstride;
1232 composite_565 (guchar *src_buf,
1235 gint dest_rowstride,
1236 GdkByteOrder dest_byte_order,
1240 guchar *src = src_buf;
1241 guchar *dest = dest_buf;
1245 gint twidth = width;
1247 gushort *q = (gushort *)dest;
1253 guint tr1, tg1, tb1;
1257 /* This is fast, and corresponds to what composite() above does
1258 * if we converted to 8-bit first.
1260 tr = (tmp & 0xf800);
1261 tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1262 tg = (tmp & 0x07e0);
1263 tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1264 tb = (tmp & 0x001f);
1265 tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1267 *q = (((tr1 + (tr1 >> 8)) & 0xf800) |
1268 (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) |
1269 ((tb1 + (tb1 >> 8)) >> 11));
1271 /* This version correspond to the result we get with XRENDER -
1272 * a bit of precision is lost since we convert to 8 bit after premultiplying
1273 * instead of at the end
1275 guint tr2, tg2, tb2;
1276 guint tr3, tg3, tb3;
1278 tr = (tmp & 0xf800);
1279 tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1280 tr2 = a * p[0] + 0x80;
1281 tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8);
1283 tg = (tmp & 0x07e0);
1284 tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1285 tg2 = a * p[0] + 0x80;
1286 tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8);
1288 tb = (tmp & 0x001f);
1289 tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1290 tb2 = a * p[0] + 0x80;
1291 tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8);
1293 *q = (((tr3 & 0xf8) << 8) |
1294 ((tg3 & 0xfc) << 3) |
1302 src += src_rowstride;
1303 dest += dest_rowstride;
1308 gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
1317 GdkRgbDither dither,
1321 gboolean free_gc = FALSE;
1322 GdkPixbuf *composited = NULL;
1323 gint dwidth, dheight;
1326 GdkRectangle tmp_rect;
1328 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1329 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
1330 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
1331 g_return_if_fail (pixbuf->bits_per_sample == 8);
1333 g_return_if_fail (drawable != NULL);
1336 width = pixbuf->width;
1338 height = pixbuf->height;
1340 g_return_if_fail (width >= 0 && height >= 0);
1341 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
1342 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
1344 /* Clip to the drawable; this is required for get_from_drawable() so
1345 * can't be done implicitly
1362 gdk_drawable_get_size (drawable, &dwidth, &dheight);
1364 if ((dest_x + width) > dwidth)
1365 width = dwidth - dest_x;
1367 if ((dest_y + height) > dheight)
1368 height = dheight - dest_y;
1370 if (width <= 0 || height <= 0)
1373 /* Clip to the clip region; this avoids getting more
1374 * image data from the server than we need to.
1377 tmp_rect.x = dest_x;
1378 tmp_rect.y = dest_y;
1379 tmp_rect.width = width;
1380 tmp_rect.height = height;
1382 drect = gdk_region_rectangle (&tmp_rect);
1383 clip = gdk_drawable_get_clip_region (drawable);
1385 gdk_region_intersect (drect, clip);
1387 gdk_region_get_clipbox (drect, &tmp_rect);
1389 gdk_region_destroy (drect);
1390 gdk_region_destroy (clip);
1392 if (tmp_rect.width == 0 ||
1393 tmp_rect.height == 0)
1400 gc = gdk_gc_new (drawable);
1404 if (pixbuf->has_alpha)
1406 GdkVisual *visual = gdk_drawable_get_visual (drawable);
1407 void (*composite_func) (guchar *src_buf,
1410 gint dest_rowstride,
1411 GdkByteOrder dest_byte_order,
1413 gint height) = NULL;
1415 /* First we see if we have a visual-specific composition function that can composite
1416 * the pixbuf data directly onto the image
1420 gint bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_drawable_get_display (drawable),
1423 if (visual->byte_order == (G_BYTE_ORDER == G_BIG_ENDIAN ? GDK_MSB_FIRST : GDK_LSB_FIRST) &&
1424 visual->depth == 16 &&
1425 visual->red_mask == 0xf800 &&
1426 visual->green_mask == 0x07e0 &&
1427 visual->blue_mask == 0x001f)
1428 composite_func = composite_565;
1429 else if (visual->depth == 24 && bits_per_pixel == 32 &&
1430 visual->red_mask == 0xff0000 &&
1431 visual->green_mask == 0x00ff00 &&
1432 visual->blue_mask == 0x0000ff)
1433 composite_func = composite_0888;
1436 /* We can't use our composite func if we are required to dither
1438 if (composite_func && !(dither == GDK_RGB_DITHER_MAX && visual->depth != 24))
1441 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1443 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1444 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1448 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1450 GdkImage *image = _gdk_image_get_scratch (gdk_drawable_get_screen (drawable),
1452 gdk_drawable_get_depth (drawable), &xs0, &ys0);
1454 _gdk_drawable_copy_to_image (drawable, image,
1455 dest_x + x0, dest_y + y0,
1458 (*composite_func) (pixbuf->pixels + (src_y + y0) * pixbuf->rowstride + (src_x + x0) * 4,
1460 (guchar*)image->mem + ys0 * image->bpl + xs0 * image->bpp,
1464 gdk_draw_image (drawable, gc, image,
1466 dest_x + x0, dest_y + y0,
1475 /* No special composition func, convert dest to 24 bit RGB data, composite against
1476 * that, and convert back.
1478 composited = gdk_pixbuf_get_from_drawable (NULL,
1486 composite (pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4,
1489 composited->rowstride,
1498 pixbuf = composited;
1501 if (pixbuf->n_channels == 4)
1503 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
1505 gdk_draw_rgb_32_image_dithalign (drawable, gc,
1509 buf, pixbuf->rowstride,
1510 x_dither, y_dither);
1512 else /* n_channels == 3 */
1514 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
1516 gdk_draw_rgb_image_dithalign (drawable, gc,
1520 buf, pixbuf->rowstride,
1521 x_dither, y_dither);
1526 g_object_unref (composited);
1529 g_object_unref (gc);