1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gdkdrawable.h"
29 #include "gdkinternals.h"
30 #include "gdkwindow.h"
31 #include "gdkscreen.h"
32 #include "gdk-pixbuf-private.h"
33 #include "gdkpixbuf.h"
35 static GdkImage* gdk_drawable_real_get_image (GdkDrawable *drawable,
40 static GdkDrawable* gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
45 gint *composite_x_offset,
46 gint *composite_y_offset);
47 static GdkRegion * gdk_drawable_real_get_visible_region (GdkDrawable *drawable);
48 static void gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
61 static void gdk_drawable_class_init (GdkDrawableClass *klass);
64 gdk_drawable_get_type (void)
66 static GType object_type = 0;
70 static const GTypeInfo object_info =
72 sizeof (GdkDrawableClass),
74 (GBaseFinalizeFunc) NULL,
75 (GClassInitFunc) gdk_drawable_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
80 (GInstanceInitFunc) NULL,
83 object_type = g_type_register_static (G_TYPE_OBJECT,
86 G_TYPE_FLAG_ABSTRACT);
93 gdk_drawable_class_init (GdkDrawableClass *klass)
95 klass->get_image = gdk_drawable_real_get_image;
96 klass->get_composite_drawable = gdk_drawable_real_get_composite_drawable;
97 /* Default implementation for clip and visible region is the same */
98 klass->get_clip_region = gdk_drawable_real_get_visible_region;
99 klass->get_visible_region = gdk_drawable_real_get_visible_region;
100 klass->draw_pixbuf = gdk_drawable_real_draw_pixbuf;
103 /* Manipulation of drawables
107 * gdk_drawable_set_data:
108 * @drawable: a #GdkDrawable
109 * @key: name to store the data under
110 * @data: arbitrary data
111 * @destroy_func: function to free @data, or %NULL
113 * This function is equivalent to g_object_set_data(),
114 * the #GObject variant should be used instead.
118 gdk_drawable_set_data (GdkDrawable *drawable,
121 GDestroyNotify destroy_func)
123 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
125 g_object_set_qdata_full (G_OBJECT (drawable),
126 g_quark_from_string (key),
132 * gdk_drawable_get_data:
133 * @drawable: a #GdkDrawable
134 * @key: name the data was stored under
136 * Equivalent to g_object_get_data(); the #GObject variant should be
139 * Return value: the data stored at @key
142 gdk_drawable_get_data (GdkDrawable *drawable,
145 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
147 return g_object_get_qdata (G_OBJECT (drawable),
148 g_quark_try_string (key));
152 * gdk_drawable_get_size:
153 * @drawable: a #GdkDrawable
154 * @width: location to store drawable's width, or %NULL
155 * @height: location to store drawable's height, or %NULL
157 * Fills *@width and *@height with the size of @drawable.
158 * @width or @height can be %NULL if you only want the other one.
160 * On the X11 platform, if @drawable is a #GdkWindow, the returned
161 * size is the size reported in the most-recently-processed configure
162 * event, rather than the current size on the X server.
166 gdk_drawable_get_size (GdkDrawable *drawable,
170 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
172 GDK_DRAWABLE_GET_CLASS (drawable)->get_size (drawable, width, height);
176 * gdk_drawable_get_visual:
177 * @drawable: a #GdkDrawable
179 * Gets the #GdkVisual describing the pixel format of @drawable.
181 * Return value: a #GdkVisual
184 gdk_drawable_get_visual (GdkDrawable *drawable)
186 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
188 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visual (drawable);
192 * gdk_drawable_get_depth:
193 * @drawable: a #GdkDrawable
195 * Obtains the bit depth of the drawable, that is, the number of bits
196 * that make up a pixel in the drawable's visual. Examples are 8 bits
197 * per pixel, 24 bits per pixel, etc.
199 * Return value: number of bits per pixel
202 gdk_drawable_get_depth (GdkDrawable *drawable)
204 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), 0);
206 return GDK_DRAWABLE_GET_CLASS (drawable)->get_depth (drawable);
209 * gdk_drawable_get_screen:
210 * @drawable: a #GdkDrawable
212 * Gets the #GdkScreen associated with a #GdkDrawable.
214 * Return value: the #GdkScreen associated with @drawable
219 gdk_drawable_get_screen(GdkDrawable *drawable)
221 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
223 return GDK_DRAWABLE_GET_CLASS (drawable)->get_screen (drawable);
227 * gdk_drawable_get_display:
228 * @drawable: a #GdkDrawable
230 * Gets the #GdkDisplay associated with a #GdkDrawable.
232 * Return value: the #GdkDisplay associated with @drawable
237 gdk_drawable_get_display (GdkDrawable *drawable)
239 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
241 return gdk_screen_get_display (gdk_drawable_get_screen (drawable));
245 * gdk_drawable_set_colormap:
246 * @drawable: a #GdkDrawable
247 * @colormap: a #GdkColormap
249 * Sets the colormap associated with @drawable. Normally this will
250 * happen automatically when the drawable is created; you only need to
251 * use this function if the drawable-creating function did not have a
252 * way to determine the colormap, and you then use drawable operations
253 * that require a colormap. The colormap for all drawables and
254 * graphics contexts you intend to use together should match. i.e.
255 * when using a #GdkGC to draw to a drawable, or copying one drawable
256 * to another, the colormaps should match.
260 gdk_drawable_set_colormap (GdkDrawable *drawable,
263 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
264 g_return_if_fail (cmap == NULL || gdk_drawable_get_depth (drawable)
265 == cmap->visual->depth);
267 GDK_DRAWABLE_GET_CLASS (drawable)->set_colormap (drawable, cmap);
271 * gdk_drawable_get_colormap:
272 * @drawable: a #GdkDrawable
274 * Gets the colormap for @drawable, if one is set; returns
277 * Return value: the colormap, or %NULL
280 gdk_drawable_get_colormap (GdkDrawable *drawable)
282 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
284 return GDK_DRAWABLE_GET_CLASS (drawable)->get_colormap (drawable);
289 * @drawable: a #GdkDrawable
291 * Deprecated equivalent of calling g_object_ref() on @drawable.
292 * (Drawables were not objects in previous versions of GDK.)
294 * Return value: the same @drawable passed in
297 gdk_drawable_ref (GdkDrawable *drawable)
299 return (GdkDrawable *) g_object_ref (drawable);
303 * gdk_drawable_unref:
304 * @drawable: a #GdkDrawable
306 * Deprecated equivalent of calling g_object_unref() on @drawable.
310 gdk_drawable_unref (GdkDrawable *drawable)
312 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
314 g_object_unref (drawable);
322 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
324 * @x: the x coordinate of the point.
325 * @y: the y coordinate of the point.
327 * Draws a point, using the foreground color and other attributes of
331 gdk_draw_point (GdkDrawable *drawable,
338 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
339 g_return_if_fail (GDK_IS_GC (gc));
344 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, &point, 1);
349 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
351 * @x1_: the x coordinate of the start point.
352 * @y1_: the y coordinate of the start point.
353 * @x2_: the x coordinate of the end point.
354 * @y2_: the y coordinate of the end point.
356 * Draws a line, using the foreground color and other attributes of
360 gdk_draw_line (GdkDrawable *drawable,
369 g_return_if_fail (drawable != NULL);
370 g_return_if_fail (gc != NULL);
371 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
372 g_return_if_fail (GDK_IS_GC (gc));
378 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, &segment, 1);
382 * gdk_draw_rectangle:
383 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
385 * @filled: %TRUE if the rectangle should be filled.
386 * @x: the x coordinate of the left edge of the rectangle.
387 * @y: the y coordinate of the top edge of the rectangle.
388 * @width: the width of the rectangle.
389 * @height: the height of the rectangle.
391 * Draws a rectangular outline or filled rectangle, using the foreground color
392 * and other attributes of the #GdkGC.
394 * A rectangle drawn filled is 1 pixel smaller in both dimensions than a
395 * rectangle outlined. Calling
396 * <literal>gdk_draw_rectangle (window, gc, TRUE, 0, 0, 20, 20)</literal>
397 * results in a filled rectangle 20 pixels wide and 20 pixels high. Calling
398 * <literal>gdk_draw_rectangle (window, gc, FALSE, 0, 0, 20, 20)</literal>
399 * results in an outlined rectangle with corners at (0, 0), (0, 20), (20, 20),
400 * and (20, 0), which makes it 21 pixels wide and 21 pixels high.
403 gdk_draw_rectangle (GdkDrawable *drawable,
411 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
412 g_return_if_fail (GDK_IS_GC (gc));
414 if (width < 0 || height < 0)
419 gdk_drawable_get_size (drawable, &real_width, &real_height);
424 height = real_height;
427 GDK_DRAWABLE_GET_CLASS (drawable)->draw_rectangle (drawable, gc, filled, x, y,
433 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
435 * @filled: %TRUE if the arc should be filled, producing a 'pie slice'.
436 * @x: the x coordinate of the left edge of the bounding rectangle.
437 * @y: the y coordinate of the top edge of the bounding rectangle.
438 * @width: the width of the bounding rectangle.
439 * @height: the height of the bounding rectangle.
440 * @angle1: the start angle of the arc, relative to the 3 o'clock position,
441 * counter-clockwise, in 1/64ths of a degree.
442 * @angle2: the end angle of the arc, relative to @angle1, in 1/64ths
445 * Draws an arc or a filled 'pie slice'. The arc is defined by the bounding
446 * rectangle of the entire ellipse, and the start and end angles of the part
447 * of the ellipse to be drawn.
450 gdk_draw_arc (GdkDrawable *drawable,
460 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
461 g_return_if_fail (GDK_IS_GC (gc));
463 if (width < 0 || height < 0)
468 gdk_drawable_get_size (drawable, &real_width, &real_height);
473 height = real_height;
476 GDK_DRAWABLE_GET_CLASS (drawable)->draw_arc (drawable, gc, filled,
477 x, y, width, height, angle1, angle2);
482 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
484 * @filled: %TRUE if the polygon should be filled. The polygon is closed
485 * automatically, connecting the last point to the first point if
487 * @points: an array of #GdkPoint structures specifying the points making
489 * @npoints: the number of points.
491 * Draws an outlined or filled polygon.
494 gdk_draw_polygon (GdkDrawable *drawable,
500 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
501 g_return_if_fail (GDK_IS_GC (gc));
503 GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
509 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
511 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
515 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
518 * @x: the x coordinate of the left edge of the text.
519 * @y: the y coordinate of the baseline of the text.
520 * @string: the string of characters to draw.
522 * Draws a string of characters in the given font or fontset.
524 * Deprecated: Use gdk_draw_layout() instead.
527 gdk_draw_string (GdkDrawable *drawable,
534 gdk_draw_text (drawable, font, gc, x, y, string, _gdk_font_strlen (font, string));
539 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
541 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
545 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
548 * @x: the x coordinate of the left edge of the text.
549 * @y: the y coordinate of the baseline of the text.
550 * @text: the characters to draw.
551 * @text_length: the number of characters of @text to draw.
553 * Draws a number of characters in the given font or fontset.
555 * Deprecated: Use gdk_draw_layout() instead.
558 gdk_draw_text (GdkDrawable *drawable,
566 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
567 g_return_if_fail (font != NULL);
568 g_return_if_fail (GDK_IS_GC (gc));
569 g_return_if_fail (text != NULL);
571 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text (drawable, font, gc, x, y, text, text_length);
576 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
579 * @x: the x coordinate of the left edge of the text.
580 * @y: the y coordinate of the baseline of the text.
581 * @text: the wide characters to draw.
582 * @text_length: the number of characters to draw.
584 * Draws a number of wide characters using the given font of fontset.
585 * If the font is a 1-byte font, the string is converted into 1-byte
586 * characters (discarding the high bytes) before output.
588 * Deprecated: Use gdk_draw_layout() instead.
591 gdk_draw_text_wc (GdkDrawable *drawable,
596 const GdkWChar *text,
599 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
600 g_return_if_fail (font != NULL);
601 g_return_if_fail (GDK_IS_GC (gc));
602 g_return_if_fail (text != NULL);
604 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text_wc (drawable, font, gc, x, y, text, text_length);
609 * @drawable: a #GdkDrawable
610 * @gc: a #GdkGC sharing the drawable's visual and colormap
611 * @src: the source #GdkDrawable, which may be the same as @drawable
612 * @xsrc: X position in @src of rectangle to draw
613 * @ysrc: Y position in @src of rectangle to draw
614 * @xdest: X position in @drawable where the rectangle should be drawn
615 * @ydest: Y position in @drawable where the rectangle should be drawn
616 * @width: width of rectangle to draw, or -1 for entire @src width
617 * @height: height of rectangle to draw, or -1 for entire @src height
619 * Copies the @width x @height region of @src at coordinates (@xsrc,
620 * @ysrc) to coordinates (@xdest, @ydest) in @drawable.
621 * @width and/or @height may be given as -1, in which case the entire
622 * @src drawable will be copied.
624 * Most fields in @gc are not used for this operation, but notably the
625 * clip mask or clip region will be honored.
627 * The source and destination drawables must have the same visual and
628 * colormap, or errors will result. (On X11, failure to match
629 * visual/colormap results in a BadMatch error from the X server.)
630 * A common cause of this problem is an attempt to draw a bitmap to
631 * a color drawable. The way to draw a bitmap is to set the
632 * bitmap as a clip mask on your #GdkGC, then use gdk_draw_rectangle()
633 * to draw a rectangle clipped to the bitmap.
636 gdk_draw_drawable (GdkDrawable *drawable,
646 GdkDrawable *composite;
647 gint composite_x_offset = 0;
648 gint composite_y_offset = 0;
650 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
651 g_return_if_fail (src != NULL);
652 g_return_if_fail (GDK_IS_GC (gc));
654 if (width < 0 || height < 0)
659 gdk_drawable_get_size (src, &real_width, &real_height);
664 height = real_height;
669 GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
673 &composite_y_offset);
676 GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc, composite,
677 xsrc - composite_x_offset,
678 ysrc - composite_y_offset,
682 g_object_unref (composite);
687 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
689 * @image: the #GdkImage to draw.
690 * @xsrc: the left edge of the source rectangle within @image.
691 * @ysrc: the top of the source rectangle within @image.
692 * @xdest: the x coordinate of the destination within @drawable.
693 * @ydest: the y coordinate of the destination within @drawable.
694 * @width: the width of the area to be copied, or -1 to make the area
695 * extend to the right edge of @image.
696 * @height: the height of the area to be copied, or -1 to make the area
697 * extend to the bottom edge of @image.
699 * Draws a #GdkImage onto a drawable.
700 * The depth of the #GdkImage must match the depth of the #GdkDrawable.
703 gdk_draw_image (GdkDrawable *drawable,
713 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
714 g_return_if_fail (image != NULL);
715 g_return_if_fail (GDK_IS_GC (gc));
718 width = image->width;
720 height = image->height;
722 GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
723 xdest, ydest, width, height);
728 * @drawable: Destination drawable.
729 * @gc: a #GdkGC, used for clipping, or %NULL
730 * @pixbuf: a #GdkPixbuf
731 * @src_x: Source X coordinate within pixbuf.
732 * @src_y: Source Y coordinates within pixbuf.
733 * @dest_x: Destination X coordinate within drawable.
734 * @dest_y: Destination Y coordinate within drawable.
735 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
736 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
737 * @dither: Dithering mode for #GdkRGB.
738 * @x_dither: X offset for dither.
739 * @y_dither: Y offset for dither.
741 * Renders a rectangular portion of a pixbuf to a drawable. The destination
742 * drawable must have a colormap. All windows have a colormap, however, pixmaps
743 * only have colormap by default if they were created with a non-%NULL window
744 * argument. Otherwise a colormap must be set on them with
745 * gdk_drawable_set_colormap().
747 * On older X servers, rendering pixbufs with an alpha channel involves round
748 * trips to the X server, and may be somewhat slow.
750 * The clip mask of @gc is ignored, but clip rectangles and clip regions work
756 gdk_draw_pixbuf (GdkDrawable *drawable,
769 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
770 g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
771 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
774 width = gdk_pixbuf_get_width (pixbuf);
776 height = gdk_pixbuf_get_height (pixbuf);
778 GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc, pixbuf,
779 src_x, src_y, dest_x, dest_y, width, height,
780 dither, x_dither, y_dither);
785 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
787 * @points: an array of #GdkPoint structures.
788 * @npoints: the number of points to be drawn.
790 * Draws a number of points, using the foreground color and other
791 * attributes of the #GdkGC.
794 gdk_draw_points (GdkDrawable *drawable,
799 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
800 g_return_if_fail ((points != NULL) && (npoints > 0));
801 g_return_if_fail (GDK_IS_GC (gc));
802 g_return_if_fail (npoints >= 0);
807 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
812 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
814 * @segs: an array of #GdkSegment structures specifying the start and
815 * end points of the lines to be drawn.
816 * @nsegs: the number of line segments to draw, i.e. the size of the
819 * Draws a number of unconnected lines.
822 gdk_draw_segments (GdkDrawable *drawable,
827 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
832 g_return_if_fail (segs != NULL);
833 g_return_if_fail (GDK_IS_GC (gc));
834 g_return_if_fail (nsegs >= 0);
836 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
841 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
843 * @points: an array of #GdkPoint structures specifying the endpoints of the
844 * @npoints: the size of the @points array.
846 * Draws a series of lines connecting the given points.
847 * The way in which joins between lines are draw is determined by the
848 * #GdkCapStyle value in the #GdkGC. This can be set with
849 * gdk_gc_set_line_attributes().
852 gdk_draw_lines (GdkDrawable *drawable,
857 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
858 g_return_if_fail (points != NULL);
859 g_return_if_fail (GDK_IS_GC (gc));
860 g_return_if_fail (npoints >= 0);
865 GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
870 * @drawable: a #GdkDrawable
872 * @font: font to be used
873 * @x: X coordinate of baseline origin
874 * @y: Y coordinate of baseline origin
875 * @glyphs: glyphs to render
877 * This is a low-level function; 99% of text rendering should be done
878 * using gdk_draw_layout() instead.
880 * A glyph is a character in a font. This function draws a sequence of
881 * glyphs. To obtain a sequence of glyphs you have to understand a
882 * lot about internationalized text handling, which you don't want to
883 * understand; thus, use gdk_draw_layout() instead of this function,
884 * gdk_draw_layout() handles the details.
888 gdk_draw_glyphs (GdkDrawable *drawable,
893 PangoGlyphString *glyphs)
895 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
896 g_return_if_fail (GDK_IS_GC (gc));
899 GDK_DRAWABLE_GET_CLASS (drawable)->draw_glyphs (drawable, gc, font, x, y, glyphs);
904 * gdk_drawable_copy_to_image:
905 * @drawable: a #GdkDrawable
906 * @image: a #GdkDrawable, or %NULL if a new @image should be created.
907 * @src_x: x coordinate on @drawable
908 * @src_y: y coordinate on @drawable
909 * @dest_x: x coordinate within @image. Must be 0 if @image is %NULL
910 * @dest_y: y coordinate within @image. Must be 0 if @image is %NULL
911 * @width: width of region to get
912 * @height: height or region to get
914 * Copies a portion of @drawable into the client side image structure
915 * @image. If @image is %NULL, creates a new image of size @width x @height
916 * and copies into that. See gdk_drawable_get_image() for further details.
918 * Return value: @image, or a new a #GdkImage containing the contents
924 gdk_drawable_copy_to_image (GdkDrawable *drawable,
933 GdkDrawable *composite;
934 gint composite_x_offset = 0;
935 gint composite_y_offset = 0;
939 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
940 g_return_val_if_fail (src_x >= 0, NULL);
941 g_return_val_if_fail (src_y >= 0, NULL);
943 /* FIXME? Note race condition since we get the size then
944 * get the image, and the size may have changed.
947 if (width < 0 || height < 0)
948 gdk_drawable_get_size (drawable,
949 width < 0 ? &width : NULL,
950 height < 0 ? &height : NULL);
953 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
957 &composite_y_offset);
959 retval = GDK_DRAWABLE_GET_CLASS (composite)->_copy_to_image (composite,
961 src_x - composite_x_offset,
962 src_y - composite_y_offset,
966 g_object_unref (composite);
968 if (!image && retval)
970 cmap = gdk_drawable_get_colormap (drawable);
973 gdk_image_set_colormap (retval, cmap);
980 * gdk_drawable_get_image:
981 * @drawable: a #GdkDrawable
982 * @x: x coordinate on @drawable
983 * @y: y coordinate on @drawable
984 * @width: width of region to get
985 * @height: height or region to get
987 * A #GdkImage stores client-side image data (pixels). In contrast,
988 * #GdkPixmap and #GdkWindow are server-side
989 * objects. gdk_drawable_get_image() obtains the pixels from a
990 * server-side drawable as a client-side #GdkImage. The format of a
991 * #GdkImage depends on the #GdkVisual of the current display, which
992 * makes manipulating #GdkImage extremely difficult; therefore, in
993 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
994 * this lower-level function. A #GdkPixbuf contains image data in a
995 * canonicalized RGB format, rather than a display-dependent format.
996 * Of course, there's a convenience vs. speed tradeoff here, so you'll
997 * want to think about what makes sense for your application.
999 * @x, @y, @width, and @height define the region of @drawable to
1000 * obtain as an image.
1002 * You would usually copy image data to the client side if you intend
1003 * to examine the values of individual pixels, for example to darken
1004 * an image or add a red tint. It would be prohibitively slow to
1005 * make a round-trip request to the windowing system for each pixel,
1006 * so instead you get all of them at once, modify them, then copy
1007 * them all back at once.
1009 * If the X server or other windowing system backend is on the local
1010 * machine, this function may use shared memory to avoid copying
1013 * If the source drawable is a #GdkWindow and partially offscreen
1014 * or obscured, then the obscured portions of the returned image
1015 * will contain undefined data.
1017 * Return value: a #GdkImage containing the contents of @drawable
1020 gdk_drawable_get_image (GdkDrawable *drawable,
1026 GdkDrawable *composite;
1027 gint composite_x_offset = 0;
1028 gint composite_y_offset = 0;
1032 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1033 g_return_val_if_fail (x >= 0, NULL);
1034 g_return_val_if_fail (y >= 0, NULL);
1036 /* FIXME? Note race condition since we get the size then
1037 * get the image, and the size may have changed.
1040 if (width < 0 || height < 0)
1041 gdk_drawable_get_size (drawable,
1042 width < 0 ? &width : NULL,
1043 height < 0 ? &height : NULL);
1046 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
1049 &composite_x_offset,
1050 &composite_y_offset);
1052 retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
1053 x - composite_x_offset,
1054 y - composite_y_offset,
1057 g_object_unref (composite);
1059 cmap = gdk_drawable_get_colormap (drawable);
1062 gdk_image_set_colormap (retval, cmap);
1068 gdk_drawable_real_get_image (GdkDrawable *drawable,
1074 return gdk_drawable_copy_to_image (drawable, NULL, x, y, 0, 0, width, height);
1078 gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
1083 gint *composite_x_offset,
1084 gint *composite_y_offset)
1086 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1088 *composite_x_offset = 0;
1089 *composite_y_offset = 0;
1091 return g_object_ref (drawable);
1095 * gdk_drawable_get_clip_region:
1096 * @drawable: a #GdkDrawable
1098 * Computes the region of a drawable that potentially can be written
1099 * to by drawing primitives. This region will not take into account
1100 * the clip region for the GC, and may also not take into account
1101 * other factors such as if the window is obscured by other windows,
1102 * but no area outside of this region will be affected by drawing
1105 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1106 * when you are done.
1109 gdk_drawable_get_clip_region (GdkDrawable *drawable)
1111 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1113 return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
1117 * gdk_drawable_get_visible_region:
1120 * Computes the region of a drawable that is potentially visible.
1121 * This does not necessarily take into account if the window is
1122 * obscured by other windows, but no area outside of this region
1125 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
1126 * when you are done.
1129 gdk_drawable_get_visible_region (GdkDrawable *drawable)
1131 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1133 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
1137 gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
1144 gdk_drawable_get_size (drawable, &rect.width, &rect.height);
1146 return gdk_region_rectangle (&rect);
1150 composite (guchar *src_buf,
1153 gint dest_rowstride,
1157 guchar *src = src_buf;
1158 guchar *dest = dest_buf;
1162 gint twidth = width;
1171 t = a * p[0] + (255 - a) * q[0] + 0x80;
1172 q[0] = (t + (t >> 8)) >> 8;
1173 t = a * p[1] + (255 - a) * q[1] + 0x80;
1174 q[1] = (t + (t >> 8)) >> 8;
1175 t = a * p[2] + (255 - a) * q[2] + 0x80;
1176 q[2] = (t + (t >> 8)) >> 8;
1182 src += src_rowstride;
1183 dest += dest_rowstride;
1188 composite_0888 (guchar *src_buf,
1191 gint dest_rowstride,
1192 GdkByteOrder dest_byte_order,
1196 guchar *src = src_buf;
1197 guchar *dest = dest_buf;
1201 gint twidth = width;
1205 if (dest_byte_order == GDK_LSB_FIRST)
1211 t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80;
1212 q[0] = (t + (t >> 8)) >> 8;
1213 t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80;
1214 q[1] = (t + (t >> 8)) >> 8;
1215 t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80;
1216 q[2] = (t + (t >> 8)) >> 8;
1227 t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80;
1228 q[1] = (t + (t >> 8)) >> 8;
1229 t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80;
1230 q[2] = (t + (t >> 8)) >> 8;
1231 t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80;
1232 q[3] = (t + (t >> 8)) >> 8;
1238 src += src_rowstride;
1239 dest += dest_rowstride;
1244 composite_565 (guchar *src_buf,
1247 gint dest_rowstride,
1248 GdkByteOrder dest_byte_order,
1252 guchar *src = src_buf;
1253 guchar *dest = dest_buf;
1257 gint twidth = width;
1259 gushort *q = (gushort *)dest;
1265 guint tr1, tg1, tb1;
1269 /* This is fast, and corresponds to what composite() above does
1270 * if we converted to 8-bit first.
1272 tr = (tmp & 0xf800);
1273 tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1274 tg = (tmp & 0x07e0);
1275 tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1276 tb = (tmp & 0x001f);
1277 tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1279 *q = (((tr1 + (tr1 >> 8)) & 0xf800) |
1280 (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) |
1281 ((tb1 + (tb1 >> 8)) >> 11));
1283 /* This version correspond to the result we get with XRENDER -
1284 * a bit of precision is lost since we convert to 8 bit after premultiplying
1285 * instead of at the end
1287 guint tr2, tg2, tb2;
1288 guint tr3, tg3, tb3;
1290 tr = (tmp & 0xf800);
1291 tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1292 tr2 = a * p[0] + 0x80;
1293 tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8);
1295 tg = (tmp & 0x07e0);
1296 tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1297 tg2 = a * p[0] + 0x80;
1298 tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8);
1300 tb = (tmp & 0x001f);
1301 tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1302 tb2 = a * p[0] + 0x80;
1303 tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8);
1305 *q = (((tr3 & 0xf8) << 8) |
1306 ((tg3 & 0xfc) << 3) |
1314 src += src_rowstride;
1315 dest += dest_rowstride;
1320 gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
1329 GdkRgbDither dither,
1333 GdkPixbuf *composited = NULL;
1334 gint dwidth, dheight;
1337 GdkRectangle tmp_rect;
1339 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1340 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
1341 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
1342 g_return_if_fail (pixbuf->bits_per_sample == 8);
1344 g_return_if_fail (drawable != NULL);
1347 width = pixbuf->width;
1349 height = pixbuf->height;
1351 g_return_if_fail (width >= 0 && height >= 0);
1352 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
1353 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
1355 /* Clip to the drawable; this is required for get_from_drawable() so
1356 * can't be done implicitly
1373 gdk_drawable_get_size (drawable, &dwidth, &dheight);
1375 if ((dest_x + width) > dwidth)
1376 width = dwidth - dest_x;
1378 if ((dest_y + height) > dheight)
1379 height = dheight - dest_y;
1381 if (width <= 0 || height <= 0)
1384 /* Clip to the clip region; this avoids getting more
1385 * image data from the server than we need to.
1388 tmp_rect.x = dest_x;
1389 tmp_rect.y = dest_y;
1390 tmp_rect.width = width;
1391 tmp_rect.height = height;
1393 drect = gdk_region_rectangle (&tmp_rect);
1394 clip = gdk_drawable_get_clip_region (drawable);
1396 gdk_region_intersect (drect, clip);
1398 gdk_region_get_clipbox (drect, &tmp_rect);
1400 gdk_region_destroy (drect);
1401 gdk_region_destroy (clip);
1403 if (tmp_rect.width == 0 ||
1404 tmp_rect.height == 0)
1409 gc = _gdk_drawable_get_scratch_gc (drawable, FALSE);
1411 if (pixbuf->has_alpha)
1413 GdkVisual *visual = gdk_drawable_get_visual (drawable);
1414 void (*composite_func) (guchar *src_buf,
1417 gint dest_rowstride,
1418 GdkByteOrder dest_byte_order,
1420 gint height) = NULL;
1422 /* First we see if we have a visual-specific composition function that can composite
1423 * the pixbuf data directly onto the image
1427 gint bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_drawable_get_display (drawable),
1430 if (visual->byte_order == (G_BYTE_ORDER == G_BIG_ENDIAN ? GDK_MSB_FIRST : GDK_LSB_FIRST) &&
1431 visual->depth == 16 &&
1432 visual->red_mask == 0xf800 &&
1433 visual->green_mask == 0x07e0 &&
1434 visual->blue_mask == 0x001f)
1435 composite_func = composite_565;
1436 else if (visual->depth == 24 && bits_per_pixel == 32 &&
1437 visual->red_mask == 0xff0000 &&
1438 visual->green_mask == 0x00ff00 &&
1439 visual->blue_mask == 0x0000ff)
1440 composite_func = composite_0888;
1443 /* We can't use our composite func if we are required to dither
1445 if (composite_func && !(dither == GDK_RGB_DITHER_MAX && visual->depth != 24))
1448 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1450 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1451 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1455 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1457 GdkImage *image = _gdk_image_get_scratch (gdk_drawable_get_screen (drawable),
1459 gdk_drawable_get_depth (drawable), &xs0, &ys0);
1461 gdk_drawable_copy_to_image (drawable, image,
1462 dest_x + x0, dest_y + y0,
1465 (*composite_func) (pixbuf->pixels + (src_y + y0) * pixbuf->rowstride + (src_x + x0) * 4,
1467 (guchar*)image->mem + ys0 * image->bpl + xs0 * image->bpp,
1471 gdk_draw_image (drawable, gc, image,
1473 dest_x + x0, dest_y + y0,
1482 /* No special composition func, convert dest to 24 bit RGB data, composite against
1483 * that, and convert back.
1485 composited = gdk_pixbuf_get_from_drawable (NULL,
1493 composite (pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4,
1496 composited->rowstride,
1505 pixbuf = composited;
1508 if (pixbuf->n_channels == 4)
1510 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
1512 gdk_draw_rgb_32_image_dithalign (drawable, gc,
1516 buf, pixbuf->rowstride,
1517 x_dither, y_dither);
1519 else /* n_channels == 3 */
1521 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
1523 gdk_draw_rgb_image_dithalign (drawable, gc,
1527 buf, pixbuf->rowstride,
1528 x_dither, y_dither);
1533 g_object_unref (composited);
1537 * _gdk_drawable_get_scratch_gc:
1538 * @drawable: A #GdkDrawable
1539 * @graphics_exposures: Whether the returned #GdkGC should generate graphics exposures
1541 * Returns a #GdkGC suitable for drawing on @drawable. The #GdkGC has
1542 * the standard values for @drawable, except for the graphics_exposures
1543 * field which is determined by the @graphics_exposures parameter.
1545 * The foreground color of the returned #GdkGC is undefined. The #GdkGC
1546 * must not be altered in any way, except to change its foreground color.
1548 * Return value: A #GdkGC suitable for drawing on @drawable
1553 _gdk_drawable_get_scratch_gc (GdkDrawable *drawable,
1554 gboolean graphics_exposures)
1559 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
1561 screen = gdk_drawable_get_screen (drawable);
1563 g_return_val_if_fail (!screen->closed, NULL);
1565 depth = gdk_drawable_get_depth (drawable) - 1;
1567 if (graphics_exposures)
1569 if (!screen->exposure_gcs[depth])
1572 GdkGCValuesMask mask;
1574 values.graphics_exposures = TRUE;
1575 mask = GDK_GC_EXPOSURES;
1577 screen->exposure_gcs[depth] =
1578 gdk_gc_new_with_values (drawable, &values, mask);
1581 return screen->exposure_gcs[depth];
1585 if (!screen->normal_gcs[depth])
1587 screen->normal_gcs[depth] =
1588 gdk_gc_new (drawable);
1591 return screen->normal_gcs[depth];