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 #include "gdkalias.h"
30 
31 /**
32  * gdk_rectangle_union:
33  * @src1: a #GdkRectangle
34  * @src2: a #GdkRectangle
35  * @dest: (out): return location for the union of @src1 and @src2
36  *
37  * Calculates the union of two rectangles.
38  * The union of rectangles @src1 and @src2 is the smallest rectangle which
39  * includes both @src1 and @src2 within it.
40  * It is allowed for @dest to be the same as either @src1 or @src2.
41  */
42 void
gdk_rectangle_union(const GdkRectangle * src1,const GdkRectangle * src2,GdkRectangle * dest)43 gdk_rectangle_union (const GdkRectangle *src1,
44 		     const GdkRectangle *src2,
45 		     GdkRectangle       *dest)
46 {
47   gint dest_x, dest_y;
48 
49   g_return_if_fail (src1 != NULL);
50   g_return_if_fail (src2 != NULL);
51   g_return_if_fail (dest != NULL);
52 
53   dest_x = MIN (src1->x, src2->x);
54   dest_y = MIN (src1->y, src2->y);
55   dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
56   dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
57   dest->x = dest_x;
58   dest->y = dest_y;
59 }
60 
61 /**
62  * gdk_rectangle_intersect:
63  * @src1: a #GdkRectangle
64  * @src2: a #GdkRectangle
65  * @dest: (out caller-allocates) (allow-none): return location for the
66  * intersection of @src1 and @src2, or %NULL
67  *
68  * Calculates the intersection of two rectangles. It is allowed for
69  * @dest to be the same as either @src1 or @src2. If the rectangles
70  * do not intersect, @dest's width and height is set to 0 and its x
71  * and y values are undefined. If you are only interested in whether
72  * the rectangles intersect, but not in the intersecting area itself,
73  * pass %NULL for @dest.
74  *
75  * Returns: %TRUE if the rectangles intersect.
76  */
77 gboolean
gdk_rectangle_intersect(const GdkRectangle * src1,const GdkRectangle * src2,GdkRectangle * dest)78 gdk_rectangle_intersect (const GdkRectangle *src1,
79 			 const GdkRectangle *src2,
80 			 GdkRectangle       *dest)
81 {
82   gint dest_x, dest_y;
83   gint dest_x2, dest_y2;
84   gint return_val;
85 
86   g_return_val_if_fail (src1 != NULL, FALSE);
87   g_return_val_if_fail (src2 != NULL, FALSE);
88 
89   return_val = FALSE;
90 
91   dest_x = MAX (src1->x, src2->x);
92   dest_y = MAX (src1->y, src2->y);
93   dest_x2 = MIN (src1->x + src1->width, src2->x + src2->width);
94   dest_y2 = MIN (src1->y + src1->height, src2->y + src2->height);
95 
96   if (dest_x2 > dest_x && dest_y2 > dest_y)
97     {
98       if (dest)
99         {
100           dest->x = dest_x;
101           dest->y = dest_y;
102           dest->width = dest_x2 - dest_x;
103           dest->height = dest_y2 - dest_y;
104         }
105       return_val = TRUE;
106     }
107   else if (dest)
108     {
109       dest->width = 0;
110       dest->height = 0;
111     }
112 
113   return return_val;
114 }
115 
116 static GdkRectangle *
gdk_rectangle_copy(const GdkRectangle * rectangle)117 gdk_rectangle_copy (const GdkRectangle *rectangle)
118 {
119   GdkRectangle *result = g_new (GdkRectangle, 1);
120   *result = *rectangle;
121 
122   return result;
123 }
124 
125 GType
gdk_rectangle_get_type(void)126 gdk_rectangle_get_type (void)
127 {
128   static GType our_type = 0;
129 
130   if (our_type == 0)
131     our_type = g_boxed_type_register_static (g_intern_static_string ("GdkRectangle"),
132 					     (GBoxedCopyFunc)gdk_rectangle_copy,
133 					     (GBoxedFreeFunc)g_free);
134   return our_type;
135 }
136 
137 
138 #define __GDK_RECTANGLE_C__
139 #include "gdkaliasdef.c"
140