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