]> Pileus Git - ~andy/gtk/blob - gdk/gdkrectangle.c
docs: Move documentation to inline comments: regions
[~andy/gtk] / gdk / gdkrectangle.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28 #include <gdk/gdk.h>
29
30
31 /**
32  * SECTION:regions
33  * @Short_description: Simple graphical data types
34  * @Title: Points and Rectangles
35  *
36  * GDK provides the #GdkPoint and #GdkRectangle data types for representing pixels
37  * and sets of pixels on the screen. Together with Cairo's #cairo_region_t data
38  * type, they make up the central types for representing graphical data.
39  *
40  * #GdkPoint is a simple structure containing an x and y coordinate of a point.
41  *
42  * #GdkRectangle is a structure holding the position and size of a rectangle.
43  * The intersection of two rectangles can be computed with
44  * gdk_rectangle_intersect(). To find the union of two rectangles use
45  * gdk_rectangle_union().
46  *
47  * #cairo_region_t is usually used for managing clipping of graphical operations.
48  */
49
50
51 /**
52  * gdk_rectangle_union:
53  * @src1: a #GdkRectangle
54  * @src2: a #GdkRectangle
55  * @dest: return location for the union of @src1 and @src2
56  *
57  * Calculates the union of two rectangles.
58  * The union of rectangles @src1 and @src2 is the smallest rectangle which
59  * includes both @src1 and @src2 within it.
60  * It is allowed for @dest to be the same as either @src1 or @src2.
61  */
62 void
63 gdk_rectangle_union (const GdkRectangle *src1,
64                      const GdkRectangle *src2,
65                      GdkRectangle       *dest)
66 {
67   gint dest_x, dest_y;
68   
69   g_return_if_fail (src1 != NULL);
70   g_return_if_fail (src2 != NULL);
71   g_return_if_fail (dest != NULL);
72
73   dest_x = MIN (src1->x, src2->x);
74   dest_y = MIN (src1->y, src2->y);
75   dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
76   dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
77   dest->x = dest_x;
78   dest->y = dest_y;
79 }
80
81 /**
82  * gdk_rectangle_intersect:
83  * @src1: a #GdkRectangle
84  * @src2: a #GdkRectangle
85  * @dest: (out caller-allocates) (allow-none): return location for the
86  * intersection of @src1 and @src2, or %NULL
87  *
88  * Calculates the intersection of two rectangles. It is allowed for
89  * @dest to be the same as either @src1 or @src2. If the rectangles 
90  * do not intersect, @dest's width and height is set to 0 and its x 
91  * and y values are undefined. If you are only interested in whether
92  * the rectangles intersect, but not in the intersecting area itself,
93  * pass %NULL for @dest.
94  *
95  * Returns: %TRUE if the rectangles intersect.
96  */
97 gboolean
98 gdk_rectangle_intersect (const GdkRectangle *src1,
99                          const GdkRectangle *src2,
100                          GdkRectangle       *dest)
101 {
102   gint dest_x, dest_y;
103   gint dest_x2, dest_y2;
104   gint return_val;
105
106   g_return_val_if_fail (src1 != NULL, FALSE);
107   g_return_val_if_fail (src2 != NULL, FALSE);
108
109   return_val = FALSE;
110
111   dest_x = MAX (src1->x, src2->x);
112   dest_y = MAX (src1->y, src2->y);
113   dest_x2 = MIN (src1->x + src1->width, src2->x + src2->width);
114   dest_y2 = MIN (src1->y + src1->height, src2->y + src2->height);
115
116   if (dest_x2 > dest_x && dest_y2 > dest_y)
117     {
118       if (dest)
119         {
120           dest->x = dest_x;
121           dest->y = dest_y;
122           dest->width = dest_x2 - dest_x;
123           dest->height = dest_y2 - dest_y;
124         }
125       return_val = TRUE;
126     }
127   else if (dest)
128     {
129       dest->width = 0;
130       dest->height = 0;
131     }
132
133   return return_val;
134 }
135
136 static GdkRectangle *
137 gdk_rectangle_copy (const GdkRectangle *rectangle)
138 {
139   GdkRectangle *result = g_new (GdkRectangle, 1);
140   *result = *rectangle;
141
142   return result;
143 }
144
145 G_DEFINE_BOXED_TYPE (GdkRectangle, gdk_rectangle,
146                      gdk_rectangle_copy,
147                      g_free)