File: fvwmrect.c

package info (click to toggle)
fvwm 1%3A2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (162 lines) | stat: -rw-r--r-- 3,916 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*-c-*- */
/* Copyright (C) 2001  Dominik Vogt */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/*
** fvwmrect.c:
** This file supplies routines for fvwm internal rectangle handling.
*/

/* ---------------------------- included header files ---------------------- */

#include "config.h"
#include <X11/Xlib.h>
#include "fvwmrect.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

/* ---------------------------- local functions ---------------------------- */

static int fvwmrect_do_intervals_intersect(
	int x1, int width1, int x2, int width2)
{
	return !(x1 + width1 <= x2 || x2 + width2 <= x1);
}

/* ---------------------------- interface functions ------------------------ */

/* Returns 1 if the given rectangles intersect and 0 otherwise */
int fvwmrect_do_rectangles_intersect(rectangle *r, rectangle *s)
{
	if (r->x + r->width <= s->x)
	{
		return 0;
	}
	if (s->x + s->width <= r->x)
	{
		return 0;
	}
	if (r->y + r->height <= s->y)
	{
		return 0;
	}
	if (s->y + s->height <= r->y)
	{
		return 0;
	}

	return 1;
}

/* Subtracts the values in s2_ from the ones in s1_g and stores the result in
 * diff_g. */
void fvwmrect_subtract_rectangles(
	rectangle *rdiff, rectangle *r1, rectangle *r2)
{
	rdiff->x = r1->x - r2->x;
	rdiff->y = r1->y - r2->y;
	rdiff->width = r1->width - r2->width;
	rdiff->height = r1->height - r2->height;

	return;
}

/* Returns 1 is the rectangles are identical and 0 if not */
int fvwmrect_rectangles_equal(
	rectangle *r1, rectangle *r2)
{
	if (r1 == r2)
	{
		return 1;
	}
	if (r1 == NULL || r2 == NULL)
	{
		return 0;
	}
	if (r1->x != r2->x || r1->y != r2->y || r1->width != r2->width ||
	    r1->height != r2->height)
	{
		return 1;
	}

	return 0;
}

int fvwmrect_move_into_rectangle(rectangle *move_rec, rectangle *target_rec)
{
	int has_changed = 0;

	if (!fvwmrect_do_intervals_intersect(
		    move_rec->x, move_rec->width, target_rec->x,
		    target_rec->width))
	{
		move_rec->x = move_rec->x % (int)target_rec->width;
		if (move_rec->x < 0)
		{
			move_rec->x += target_rec->width;
		}
		move_rec->x += target_rec->x;
		has_changed = 1;
	}
	if (!fvwmrect_do_intervals_intersect(
		    move_rec->y, move_rec->height, target_rec->y,
		    target_rec->height))
	{
		move_rec->y = move_rec->y % (int)target_rec->height;
		if (move_rec->y < 0)
		{
			move_rec->y += target_rec->height;
		}
		move_rec->y += target_rec->y;
		has_changed = 1;
	}

	return has_changed;
}

int fvwmrect_intersect_xrectangles(
	XRectangle *r1, XRectangle *r2)
{
	int x1 = max(r1->x, r2->x);
	int y1 = max(r1->y, r2->y);
	int x2 = min(r1->x + r1->width, r2->x + r2->width);
	int y2 = min(r1->y + r1->height, r2->y + r2->height);

	r1->x = x1;
	r1->y = y1;
	r1->width = x2 - x1;
	r1->height = y2 - y1;

	if (x2 > x1 && y2 > y1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}