]> Dogcows Code - chaz/openbox/blob - openbox/geom.h
Merge branch 'master' into chaz
[chaz/openbox] / openbox / geom.h
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 geom.h for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #ifndef __geom_h
21 #define __geom_h
22
23 #include <glib.h>
24
25 typedef struct _GravityCoord {
26 gint pos;
27 gint denom;
28 gboolean center;
29 gboolean opposite;
30 } GravityCoord;
31
32 typedef struct _GravityPoint {
33 GravityCoord x;
34 GravityCoord y;
35 } GravityPoint;
36
37 #define GRAVITY_COORD_SET(c, p, cen, opp) \
38 (c).pos = (p), (c).center = (cen), (c).opposite = (opp)
39
40
41 typedef struct _Point {
42 int x;
43 int y;
44 } Point;
45
46 #define POINT_SET(pt, nx, ny) (pt).x = (nx), (pt).y = (ny)
47 #define POINT_EQUAL(p1, p2) ((p1).x == (p2).x && (p1).y == (p2).y)
48
49 typedef struct _Size {
50 int width;
51 int height;
52 } Size;
53
54 #define SIZE_SET(sz, w, h) (sz).width = (w), (sz).height = (h)
55
56 typedef struct _Rect {
57 int x;
58 int y;
59 int width;
60 int height;
61 } Rect;
62
63 #define RECT_LEFT(r) ((r).x)
64 #define RECT_TOP(r) ((r).y)
65 #define RECT_RIGHT(r) ((r).x + (r).width - 1)
66 #define RECT_BOTTOM(r) ((r).y + (r).height - 1)
67
68 #define RECT_AREA(r) ((r).width * (r).height)
69
70 #define RECT_SET_POINT(r, nx, ny) \
71 (r).x = (nx), (r).y = (ny)
72 #define RECT_SET_SIZE(r, w, h) \
73 (r).width = (w), (r).height = (h)
74 #define RECT_SET(r, nx, ny, w, h) \
75 (r).x = (nx), (r).y = (ny), (r).width = (w), (r).height = (h)
76
77 #define RECT_EQUAL(r1, r2) ((r1).x == (r2).x && (r1).y == (r2).y && \
78 (r1).width == (r2).width && \
79 (r1).height == (r2).height)
80 #define RECT_EQUAL_DIMS(r, x, y, w, h) \
81 ((r).x == (x) && (r).y == (y) && (r).width == (w) && (r).height == (h))
82
83 #define RECT_TO_DIMS(r, x, y, w, h) \
84 (x) = (r).x, (y) = (r).y, (w) = (r).width, (h) = (r).height
85
86 #define RECT_CONTAINS(r, px, py) \
87 ((px) >= (r).x && (px) < (r).x + (r).width && \
88 (py) >= (r).y && (py) < (r).y + (r).height)
89 #define RECT_CONTAINS_RECT(r, o) \
90 ((o).x >= (r).x && (o).x + (o).width <= (r).x + (r).width && \
91 (o).y >= (r).y && (o).y + (o).height <= (r).y + (r).height)
92
93 /* Returns true if Rect r and o intersect */
94 #define RECT_INTERSECTS_RECT(r, o) \
95 ((o).x < (r).x + (r).width && (o).x + (o).width > (r).x && \
96 (o).y < (r).y + (r).height && (o).y + (o).height > (r).y)
97
98 /* Sets Rect r to be the intersection of Rect a and b. */
99 #define RECT_SET_INTERSECTION(r, a, b) \
100 ((r).x = MAX((a).x, (b).x), \
101 (r).y = MAX((a).y, (b).y), \
102 (r).width = MIN((a).x + (a).width - 1, \
103 (b).x + (b).width - 1) - (r).x + 1, \
104 (r).height = MIN((a).y + (a).height - 1, \
105 (b).y + (b).height - 1) - (r).y + 1)
106
107 /* Returns the shortest manhatten distance between two rects, or 0 if they
108 intersect. */
109 static inline gint rect_manhatten_distance(Rect r, Rect o)
110 {
111 if (RECT_INTERSECTS_RECT(r, o))
112 return 0;
113
114 gint min_distance = G_MAXINT;
115 if (RECT_RIGHT(o) < RECT_LEFT(r))
116 min_distance = MIN(min_distance, RECT_LEFT(r) - RECT_RIGHT(o));
117 if (RECT_LEFT(o) > RECT_RIGHT(r))
118 min_distance = MIN(min_distance, RECT_LEFT(o) - RECT_RIGHT(r));
119 if (RECT_BOTTOM(o) < RECT_TOP(r))
120 min_distance = MIN(min_distance, RECT_TOP(r) - RECT_BOTTOM(o));
121 if (RECT_TOP(o) > RECT_BOTTOM(r))
122 min_distance = MIN(min_distance, RECT_TOP(o) - RECT_BOTTOM(r));
123 return min_distance;
124 }
125
126 typedef struct _Strut {
127 int left;
128 int top;
129 int right;
130 int bottom;
131 } Strut;
132
133 typedef struct _StrutPartial {
134 int left;
135 int top;
136 int right;
137 int bottom;
138
139 int left_start, left_end;
140 int top_start, top_end;
141 int right_start, right_end;
142 int bottom_start, bottom_end;
143 } StrutPartial;
144
145 #define STRUT_SET(s, l, t, r, b) \
146 (s).left = (l), (s).top = (t), (s).right = (r), (s).bottom = (b)
147
148 #define STRUT_PARTIAL_SET(s, l, t, r, b, ls, le, ts, te, rs, re, bs, be) \
149 (s).left = (l), (s).top = (t), (s).right = (r), (s).bottom = (b), \
150 (s).left_start = (ls), (s).left_end = (le), \
151 (s).top_start = (ts), (s).top_end = (te), \
152 (s).right_start = (rs), (s).right_end = (re), \
153 (s).bottom_start = (bs), (s).bottom_end = (be)
154
155 #define STRUT_ADD(s1, s2) \
156 (s1).left = MAX((s1).left, (s2).left), \
157 (s1).right = MAX((s1).right, (s2).right), \
158 (s1).top = MAX((s1).top, (s2).top), \
159 (s1).bottom = MAX((s1).bottom, (s2).bottom)
160
161 #define STRUT_EXISTS(s1) \
162 ((s1).left || (s1).top || (s1).right || (s1).bottom)
163
164 #define STRUT_EQUAL(s1, s2) \
165 ((s1).left == (s2).left && \
166 (s1).top == (s2).top && \
167 (s1).right == (s2).right && \
168 (s1).bottom == (s2).bottom)
169
170 #define PARTIAL_STRUT_EQUAL(s1, s2) \
171 ((s1).left == (s2).left && \
172 (s1).top == (s2).top && \
173 (s1).right == (s2).right && \
174 (s1).bottom == (s2).bottom && \
175 (s1).left_start == (s2).left_start && \
176 (s1).left_end == (s2).left_end && \
177 (s1).top_start == (s2).top_start && \
178 (s1).top_end == (s2).top_end && \
179 (s1).right_start == (s2).right_start && \
180 (s1).right_end == (s2).right_end && \
181 (s1).bottom_start == (s2).bottom_start && \
182 (s1).bottom_end == (s2).bottom_end)
183
184 #define RANGES_INTERSECT(r1x, r1w, r2x, r2w) \
185 (r1w && r2w && r1x < r2x + r2w && r1x + r1w > r2x)
186
187 #endif
This page took 0.03889 seconds and 4 git commands to generate.