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"
31 static GdkDrawable* gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
36 gint *composite_x_offset,
37 gint *composite_y_offset);
38 static GdkRegion * gdk_drawable_real_get_visible_region (GdkDrawable *drawable);
40 static void gdk_drawable_class_init (GdkDrawableClass *klass);
43 gdk_drawable_get_type (void)
45 static GType object_type = 0;
49 static const GTypeInfo object_info =
51 sizeof (GdkDrawableClass),
53 (GBaseFinalizeFunc) NULL,
54 (GClassInitFunc) gdk_drawable_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
59 (GInstanceInitFunc) NULL,
62 object_type = g_type_register_static (G_TYPE_OBJECT,
71 gdk_drawable_class_init (GdkDrawableClass *klass)
73 klass->get_composite_drawable = gdk_drawable_real_get_composite_drawable;
74 /* Default implementation for clip and visible region is the same */
75 klass->get_clip_region = gdk_drawable_real_get_visible_region;
76 klass->get_visible_region = gdk_drawable_real_get_visible_region;
79 /* Manipulation of drawables
83 * gdk_drawable_set_data:
84 * @drawable: a #GdkDrawable
85 * @key: name to store the data under
86 * @data: arbitrary data
87 * @destroy_func: function to free @data, or %NULL
89 * This function is equivalent to g_object_set_data(),
90 * the #GObject variant should be used instead.
94 gdk_drawable_set_data (GdkDrawable *drawable,
97 GDestroyNotify destroy_func)
99 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
101 g_object_set_qdata_full (G_OBJECT (drawable),
102 g_quark_from_string (key),
108 * gdk_drawable_get_data:
109 * @drawable: a #GdkDrawable
110 * @key: name the data was stored under
112 * Equivalent to g_object_get_data(); the #GObject variant should be
115 * Return value: the data stored at @key
118 gdk_drawable_get_data (GdkDrawable *drawable,
121 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
123 return g_object_get_qdata (G_OBJECT (drawable),
124 g_quark_try_string (key));
128 * gdk_drawable_get_size:
129 * @drawable: a #GdkDrawable
130 * @width: location to store drawable's width, or %NULL
131 * @height: location to store drawable's height, or %NULL
133 * Fills *@width and *@height with the size of @drawable.
134 * @width or @height can be %NULL if you only want the other one.
138 gdk_drawable_get_size (GdkDrawable *drawable,
142 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
144 GDK_DRAWABLE_GET_CLASS (drawable)->get_size (drawable, width, height);
148 * gdk_drawable_get_visual:
149 * @drawable: a #GdkDrawable
151 * Gets the #GdkVisual describing the pixel format of @drawable.
153 * Return value: a #GdkVisual
156 gdk_drawable_get_visual (GdkDrawable *drawable)
158 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
160 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visual (drawable);
164 * gdk_drawable_get_depth:
165 * @drawable: a #GdkDrawable
167 * Obtains the bit depth of the drawable, that is, the number of bits
168 * that make up a pixel in the drawable's visual. Examples are 8 bits
169 * per pixel, 24 bits per pixel, etc.
171 * Return value: number of bits per pixel
174 gdk_drawable_get_depth (GdkDrawable *drawable)
176 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), 0);
178 return GDK_DRAWABLE_GET_CLASS (drawable)->get_depth (drawable);
182 * gdk_drawable_set_colormap:
183 * @drawable: a #GdkDrawable
184 * @colormap: a #GdkColormap
186 * Sets the colormap associated with @drawable. Normally this will
187 * happen automatically when the drawable is created; you only need to
188 * use this function if the drawable-creating function did not have a
189 * way to determine the colormap, and you then use drawable operations
190 * that require a colormap. The colormap for all drawables and
191 * graphics contexts you intend to use together should match. i.e.
192 * when using a #GdkGC to draw to a drawable, or copying one drawable
193 * to another, the colormaps should match.
197 gdk_drawable_set_colormap (GdkDrawable *drawable,
200 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
202 GDK_DRAWABLE_GET_CLASS (drawable)->set_colormap (drawable, cmap);
206 * gdk_drawable_get_colormap:
207 * @drawable: a #GdkDrawable
209 * Gets the colormap for @drawable, if one is set; returns
212 * Return value: the colormap, or %NULL
215 gdk_drawable_get_colormap (GdkDrawable *drawable)
217 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
219 return GDK_DRAWABLE_GET_CLASS (drawable)->get_colormap (drawable);
224 * @drawable: a #GdkDrawable
226 * Deprecated equivalent of calling g_object_ref() on @drawable.
227 * (Drawables were not objects in previous versions of GDK.)
229 * Return value: the same @drawable passed in
232 gdk_drawable_ref (GdkDrawable *drawable)
234 return (GdkDrawable *) g_object_ref (G_OBJECT (drawable));
238 * gdk_drawable_unref:
239 * @drawable: a #GdkDrawable
241 * Deprecated equivalent of calling g_object_unref() on @drawable.
245 gdk_drawable_unref (GdkDrawable *drawable)
247 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
249 g_object_unref (G_OBJECT (drawable));
255 gdk_draw_point (GdkDrawable *drawable,
262 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
263 g_return_if_fail (GDK_IS_GC (gc));
268 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, &point, 1);
272 gdk_draw_line (GdkDrawable *drawable,
281 g_return_if_fail (drawable != NULL);
282 g_return_if_fail (gc != NULL);
283 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
284 g_return_if_fail (GDK_IS_GC (gc));
290 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, &segment, 1);
294 gdk_draw_rectangle (GdkDrawable *drawable,
302 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
303 g_return_if_fail (GDK_IS_GC (gc));
305 if (width < 0 || height < 0)
310 gdk_drawable_get_size (drawable, &real_width, &real_height);
315 height = real_height;
318 GDK_DRAWABLE_GET_CLASS (drawable)->draw_rectangle (drawable, gc, filled, x, y,
323 gdk_draw_arc (GdkDrawable *drawable,
333 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
334 g_return_if_fail (GDK_IS_GC (gc));
336 if (width < 0 || height < 0)
341 gdk_drawable_get_size (drawable, &real_width, &real_height);
346 height = real_height;
349 GDK_DRAWABLE_GET_CLASS (drawable)->draw_arc (drawable, gc, filled,
350 x, y, width, height, angle1, angle2);
354 gdk_draw_polygon (GdkDrawable *drawable,
360 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
361 g_return_if_fail (GDK_IS_GC (gc));
363 GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
369 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
371 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
374 gdk_draw_string (GdkDrawable *drawable,
381 gdk_draw_text (drawable, font, gc, x, y, string, _gdk_font_strlen (font, string));
386 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
388 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
391 gdk_draw_text (GdkDrawable *drawable,
399 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
400 g_return_if_fail (font != NULL);
401 g_return_if_fail (GDK_IS_GC (gc));
402 g_return_if_fail (text != NULL);
404 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text (drawable, font, gc, x, y, text, text_length);
408 gdk_draw_text_wc (GdkDrawable *drawable,
413 const GdkWChar *text,
416 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
417 g_return_if_fail (font != NULL);
418 g_return_if_fail (GDK_IS_GC (gc));
419 g_return_if_fail (text != NULL);
421 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text_wc (drawable, font, gc, x, y, text, text_length);
425 gdk_draw_drawable (GdkDrawable *drawable,
435 GdkDrawable *composite;
436 gint composite_x_offset = 0;
437 gint composite_y_offset = 0;
439 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
440 g_return_if_fail (src != NULL);
441 g_return_if_fail (GDK_IS_GC (gc));
443 if (width < 0 || height < 0)
448 gdk_drawable_get_size (src, &real_width, &real_height);
453 height = real_height;
458 GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
462 &composite_y_offset);
465 GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc, composite,
466 xsrc - composite_x_offset,
467 ysrc - composite_y_offset,
471 g_object_unref (G_OBJECT (composite));
475 gdk_draw_image (GdkDrawable *drawable,
485 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
486 g_return_if_fail (image != NULL);
487 g_return_if_fail (GDK_IS_GC (gc));
490 width = image->width;
492 height = image->height;
494 GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
495 xdest, ydest, width, height);
499 gdk_draw_points (GdkDrawable *drawable,
504 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
505 g_return_if_fail ((points != NULL) && (npoints > 0));
506 g_return_if_fail (GDK_IS_GC (gc));
507 g_return_if_fail (npoints >= 0);
512 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
516 gdk_draw_segments (GdkDrawable *drawable,
521 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
526 g_return_if_fail (segs != NULL);
527 g_return_if_fail (GDK_IS_GC (gc));
528 g_return_if_fail (nsegs >= 0);
530 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
534 gdk_draw_lines (GdkDrawable *drawable,
540 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
541 g_return_if_fail (points != NULL);
542 g_return_if_fail (GDK_IS_GC (gc));
543 g_return_if_fail (npoints >= 0);
548 GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
552 gdk_draw_glyphs (GdkDrawable *drawable,
557 PangoGlyphString *glyphs)
559 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
560 g_return_if_fail (GDK_IS_GC (gc));
563 GDK_DRAWABLE_GET_CLASS (drawable)->draw_glyphs (drawable, gc, font, x, y, glyphs);
568 * gdk_drawable_get_image:
569 * @drawable: a #GdkDrawable
570 * @x: x coordinate on @drawable
571 * @y: y coordinate on @drawable
572 * @width: width of region to get
573 * @height: height or region to get
575 * A #GdkImage stores client-side image data (pixels). In contrast,
576 * #GdkPixmap and #GdkWindow are server-side
577 * objects. gdk_drawable_get_image() obtains the pixels from a
578 * server-side drawable as a client-side #GdkImage. The format of a
579 * #GdkImage depends on the #GdkVisual of the current display, which
580 * makes manipulating #GdkImage extremely difficult; therefore, in
581 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
582 * this lower-level function. A #GdkPixbuf contains image data in a
583 * canonicalized RGB format, rather than a display-dependent format.
584 * Of course, there's a convenience vs. speed tradeoff here, so you'll
585 * want to think about what makes sense for your application.
587 * @x, @y, @width, and @height define the region of @drawable to
588 * obtain as an image.
590 * You would usually copy image data to the client side if you intend
591 * to examine the values of individual pixels, for example to darken
592 * an image or add a red tint. It would be prohibitively slow to
593 * make a round-trip request to the windowing system for each pixel,
594 * so instead you get all of them at once, modify them, then copy
595 * them all back at once.
597 * If the X server or other windowing system backend is on the local
598 * machine, this function may use shared memory to avoid copying
601 * Return value: a #GdkImage containing the contents of @drawable
604 gdk_drawable_get_image (GdkDrawable *drawable,
610 GdkDrawable *composite;
611 gint composite_x_offset = 0;
612 gint composite_y_offset = 0;
615 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
616 g_return_val_if_fail (x >= 0, NULL);
617 g_return_val_if_fail (y >= 0, NULL);
619 /* FIXME? Note race condition since we get the size then
620 * get the image, and the size may have changed.
623 if (width < 0 || height < 0)
624 gdk_drawable_get_size (drawable,
625 width < 0 ? &width : NULL,
626 height < 0 ? &height : NULL);
629 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
633 &composite_y_offset);
635 retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
636 x - composite_x_offset,
637 y - composite_y_offset,
640 g_object_unref (G_OBJECT (composite));
646 gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
651 gint *composite_x_offset,
652 gint *composite_y_offset)
654 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
656 *composite_x_offset = 0;
657 *composite_y_offset = 0;
659 return GDK_DRAWABLE (g_object_ref (G_OBJECT (drawable)));
663 * gdk_drawable_get_clip_region:
664 * @drawable: a #GdkDrawable
666 * Computes the region of a drawable that potentially can be written
667 * to by drawing primitives. This region will not take into account
668 * the clip region for the GC, and may also not take into account
669 * other factors such as if the window is obscured by other windows,
670 * but no area outside of this region will be affected by drawing
673 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
677 gdk_drawable_get_clip_region (GdkDrawable *drawable)
679 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
681 return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
685 * gdk_drawable_get_visible_region:
688 * Computes the region of a drawable that is potentially visible.
689 * This does not necessarily take into account if the window is
690 * obscured by other windows, but no area outside of this region
693 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
697 gdk_drawable_get_visible_region (GdkDrawable *drawable)
699 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
701 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
705 gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
712 gdk_drawable_get_size (drawable, &rect.width, &rect.height);
714 return gdk_region_rectangle (&rect);