Fix compiler warnings.
[fvwm.git] / libs / fvwmrect.c
blob05a39912364db2ecab3a6fabb24f883d9a88d3d6
1 /* -*-c-*- */
2 /* Copyright (C) 2001 Dominik Vogt */
3 /* This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ** fvwmrect.c:
20 ** This file supplies routines for fvwm internal rectangle handling.
23 /* ---------------------------- included header files ---------------------- */
25 #include "config.h"
26 #include <X11/Xlib.h>
27 #include "fvwmrect.h"
29 /* ---------------------------- local definitions -------------------------- */
31 /* ---------------------------- local macros ------------------------------- */
33 /* ---------------------------- imports ------------------------------------ */
35 /* ---------------------------- exported variables (globals) --------------- */
37 /* ---------------------------- local types -------------------------------- */
39 /* ---------------------------- forward declarations ----------------------- */
41 /* ---------------------------- local variables ---------------------------- */
43 /* ---------------------------- local functions ---------------------------- */
45 static int fvwmrect_do_intervals_intersect(
46 int x1, int width1, int x2, int width2)
48 return !(x1 + width1 <= x2 || x2 + width2 <= x1);
51 /* ---------------------------- interface functions ------------------------ */
53 /* Returns 1 if the given rectangles intersect and 0 otherwise */
54 int fvwmrect_do_rectangles_intersect(rectangle *r, rectangle *s)
56 if (r->x + r->width <= s->x)
58 return 0;
60 if (s->x + s->width <= r->x)
62 return 0;
64 if (r->y + r->height <= s->y)
66 return 0;
68 if (s->y + s->height <= r->y)
70 return 0;
73 return 1;
76 /* Subtracts the values in s2_ from the ones in s1_g and stores the result in
77 * diff_g. */
78 void fvwmrect_subtract_rectangles(
79 rectangle *rdiff, rectangle *r1, rectangle *r2)
81 rdiff->x = r1->x - r2->x;
82 rdiff->y = r1->y - r2->y;
83 rdiff->width = r1->width - r2->width;
84 rdiff->height = r1->height - r2->height;
86 return;
89 /* Returns 1 is the rectangles are identical and 0 if not */
90 int fvwmrect_rectangles_equal(
91 rectangle *r1, rectangle *r2)
93 if (r1 == r2)
95 return 1;
97 if (r1 == NULL || r2 == NULL)
99 return 0;
101 if (r1->x != r2->x || r1->y != r2->y || r1->width != r2->width ||
102 r1->height != r2->height)
104 return 1;
107 return 0;
110 int fvwmrect_move_into_rectangle(rectangle *move_rec, rectangle *target_rec)
112 int has_changed = 0;
114 if (!fvwmrect_do_intervals_intersect(
115 move_rec->x, move_rec->width, target_rec->x,
116 target_rec->width))
118 move_rec->x = move_rec->x % (int)target_rec->width;
119 if (move_rec->x < 0)
121 move_rec->x += target_rec->width;
123 move_rec->x += target_rec->x;
124 has_changed = 1;
126 if (!fvwmrect_do_intervals_intersect(
127 move_rec->y, move_rec->height, target_rec->y,
128 target_rec->height))
130 move_rec->y = move_rec->y % (int)target_rec->height;
131 if (move_rec->y < 0)
133 move_rec->y += target_rec->height;
135 move_rec->y += target_rec->y;
136 has_changed = 1;
139 return has_changed;
142 int fvwmrect_intersect_xrectangles(
143 XRectangle *r1, XRectangle *r2)
145 int x1 = max(r1->x, r2->x);
146 int y1 = max(r1->y, r2->y);
147 int x2 = min(r1->x + r1->width, r2->x + r2->width);
148 int y2 = min(r1->y + r1->height, r2->y + r2->height);
150 r1->x = x1;
151 r1->y = y1;
152 r1->width = x2 - x1;
153 r1->height = y2 - y1;
155 if (x2 > x1 && y2 > y1)
157 return 1;
159 else
161 return 0;