]> Dogcows Code - chaz/openbox/blob - openbox/resist.c
resist screen physical edges as well as strut edges
[chaz/openbox] / openbox / resist.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 resist.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "client.h"
20 #include "frame.h"
21 #include "stacking.h"
22 #include "screen.h"
23 #include "config.h"
24 #include "parser/parse.h"
25
26 #include <glib.h>
27
28 void resist_move_windows(ObClient *c, gint *x, gint *y)
29 {
30 GList *it;
31 gint l, t, r, b; /* requested edges */
32 gint cl, ct, cr, cb; /* current edges */
33 gint w, h; /* current size */
34 ObClient *snapx = NULL, *snapy = NULL;
35
36 w = c->frame->area.width;
37 h = c->frame->area.height;
38
39 l = *x;
40 t = *y;
41 r = l + w - 1;
42 b = t + h - 1;
43
44 cl = RECT_LEFT(c->frame->area);
45 ct = RECT_TOP(c->frame->area);
46 cr = RECT_RIGHT(c->frame->area);
47 cb = RECT_BOTTOM(c->frame->area);
48
49 if (config_resist_win)
50 for (it = stacking_list; it != NULL; it = it->next) {
51 ObClient *target;
52 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
53
54 if (!WINDOW_IS_CLIENT(it->data))
55 continue;
56 target = it->data;
57 /* don't snap to self or non-visibles */
58 if (!target->frame->visible || target == c) continue;
59
60 tl = RECT_LEFT(target->frame->area) - 1;
61 tt = RECT_TOP(target->frame->area) - 1;
62 tr = RECT_RIGHT(target->frame->area) + 1;
63 tb = RECT_BOTTOM(target->frame->area) + 1;
64
65 /* snapx and snapy ensure that the window snaps to the top-most
66 window edge available, without going all the way from
67 bottom-to-top in the stacking list
68 */
69 if (snapx == NULL) {
70 if (ct < tb && cb > tt) {
71 if (cl >= tr && l < tr && l >= tr - config_resist_win)
72 *x = tr, snapx = target;
73 else if (cr <= tl && r > tl &&
74 r <= tl + config_resist_win)
75 *x = tl - w + 1, snapx = target;
76 if (snapx != NULL) {
77 /* try to corner snap to the window */
78 if (ct > tt && t <= tt &&
79 t > tt - config_resist_win)
80 *y = tt + 1, snapy = target;
81 else if (cb < tb && b >= tb &&
82 b < tb + config_resist_win)
83 *y = tb - h, snapy = target;
84 }
85 }
86 }
87 if (snapy == NULL) {
88 if (cl < tr && cr > tl) {
89 if (ct >= tb && t < tb && t >= tb - config_resist_win)
90 *y = tb, snapy = target;
91 else if (cb <= tt && b > tt &&
92 b <= tt + config_resist_win)
93 *y = tt - h + 1, snapy = target;
94 if (snapy != NULL) {
95 /* try to corner snap to the window */
96 if (cl > tl && l <= tl &&
97 l > tl - config_resist_win)
98 *x = tl + 1, snapx = target;
99 else if (cr < tr && r >= tr &&
100 r < tr + config_resist_win)
101 *x = tr - w, snapx = target;
102 }
103 }
104 }
105
106 if (snapx && snapy) break;
107 }
108 }
109
110 void resist_move_monitors(ObClient *c, gint *x, gint *y)
111 {
112 Rect *area, *parea;
113 guint i;
114 gint l, t, r, b; /* requested edges */
115 gint al, at, ar, ab; /* screen area edges */
116 gint pl, pt, pr, pb; /* physical screen area edges */
117 gint cl, ct, cr, cb; /* current edges */
118 gint w, h; /* current size */
119
120 w = c->frame->area.width;
121 h = c->frame->area.height;
122
123 l = *x;
124 t = *y;
125 r = l + w - 1;
126 b = t + h - 1;
127
128 cl = RECT_LEFT(c->frame->area);
129 ct = RECT_TOP(c->frame->area);
130 cr = RECT_RIGHT(c->frame->area);
131 cb = RECT_BOTTOM(c->frame->area);
132
133 if (config_resist_edge) {
134 for (i = 0; i < screen_num_monitors; ++i) {
135 area = screen_area_monitor(c->desktop, i);
136 parea = screen_physical_area_monitor(i);
137
138 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
139 continue;
140
141 al = RECT_LEFT(*area);
142 at = RECT_TOP(*area);
143 ar = RECT_RIGHT(*area);
144 ab = RECT_BOTTOM(*area);
145 pl = RECT_LEFT(*parea);
146 pt = RECT_TOP(*parea);
147 pr = RECT_RIGHT(*parea);
148 pb = RECT_BOTTOM(*parea);
149
150 if (cl >= al && l < al && l >= al - config_resist_edge)
151 *x = al;
152 else if (cr <= ar && r > ar && r <= ar + config_resist_edge)
153 *x = ar - w + 1;
154 else if (cl >= pl && l < pl && l >= pl - config_resist_edge)
155 *x = pl;
156 else if (cr <= pr && r > pr && r <= pr + config_resist_edge)
157 *x = pr - w + 1;
158
159 if (ct >= at && t < at && t >= at - config_resist_edge)
160 *y = at;
161 else if (cb <= ab && b > ab && b < ab + config_resist_edge)
162 *y = ab - h + 1;
163 else if (ct >= pt && t < pt && t >= pt - config_resist_edge)
164 *y = pt;
165 else if (cb <= pb && b > pb && b < pb + config_resist_edge)
166 *y = pb - h + 1;
167 }
168 }
169 }
170
171 void resist_size_windows(ObClient *c, gint *w, gint *h, ObCorner corn)
172 {
173 GList *it;
174 ObClient *target; /* target */
175 gint l, t, r, b; /* my left, top, right and bottom sides */
176 gint dlt, drb; /* my destination left/top and right/bottom sides */
177 gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
178 gint incw, inch;
179 ObClient *snapx = NULL, *snapy = NULL;
180
181 incw = c->size_inc.width;
182 inch = c->size_inc.height;
183
184 l = RECT_LEFT(c->frame->area);
185 r = RECT_RIGHT(c->frame->area);
186 t = RECT_TOP(c->frame->area);
187 b = RECT_BOTTOM(c->frame->area);
188
189 if (config_resist_win) {
190 for (it = stacking_list; it != NULL; it = it->next) {
191 if (!WINDOW_IS_CLIENT(it->data))
192 continue;
193 target = it->data;
194
195 /* don't snap to invisibles or ourself */
196 if (!target->frame->visible || target == c) continue;
197
198 tl = RECT_LEFT(target->frame->area);
199 tr = RECT_RIGHT(target->frame->area);
200 tt = RECT_TOP(target->frame->area);
201 tb = RECT_BOTTOM(target->frame->area);
202
203 if (snapx == NULL) {
204 /* horizontal snapping */
205 if (t < tb && b > tt) {
206 switch (corn) {
207 case OB_CORNER_TOPLEFT:
208 case OB_CORNER_BOTTOMLEFT:
209 dlt = l;
210 drb = r + *w - c->frame->area.width;
211 if (r < tl && drb >= tl &&
212 drb < tl + config_resist_win)
213 *w = tl - l, snapx = target;
214 break;
215 case OB_CORNER_TOPRIGHT:
216 case OB_CORNER_BOTTOMRIGHT:
217 dlt = l - *w + c->frame->area.width;
218 drb = r;
219 if (l > tr && dlt <= tr &&
220 dlt > tr - config_resist_win)
221 *w = r - tr, snapx = target;
222 break;
223 }
224 }
225 }
226
227 if (snapy == NULL) {
228 /* vertical snapping */
229 if (l < tr && r > tl) {
230 switch (corn) {
231 case OB_CORNER_TOPLEFT:
232 case OB_CORNER_TOPRIGHT:
233 dlt = t;
234 drb = b + *h - c->frame->area.height;
235 if (b < tt && drb >= tt &&
236 drb < tt + config_resist_win)
237 *h = tt - t, snapy = target;
238 break;
239 case OB_CORNER_BOTTOMLEFT:
240 case OB_CORNER_BOTTOMRIGHT:
241 dlt = t - *h + c->frame->area.height;
242 drb = b;
243 if (t > tb && dlt <= tb &&
244 dlt > tb - config_resist_win)
245 *h = b - tb, snapy = target;
246 break;
247 }
248 }
249 }
250
251 /* snapped both ways */
252 if (snapx && snapy) break;
253 }
254 }
255 }
256
257 void resist_size_monitors(ObClient *c, gint *w, gint *h, ObCorner corn)
258 {
259 gint l, t, r, b; /* my left, top, right and bottom sides */
260 gint dlt, drb; /* my destination left/top and right/bottom sides */
261 Rect *area, *parea;
262 gint al, at, ar, ab; /* screen boundaries */
263 gint pl, pt, pr, pb; /* physical screen boundaries */
264 gint incw, inch;
265 guint i;
266
267 l = RECT_LEFT(c->frame->area);
268 r = RECT_RIGHT(c->frame->area);
269 t = RECT_TOP(c->frame->area);
270 b = RECT_BOTTOM(c->frame->area);
271
272 incw = c->size_inc.width;
273 inch = c->size_inc.height;
274
275 for (i = 0; i < screen_num_monitors; ++i) {
276 area = screen_area_monitor(c->desktop, i);
277 parea = screen_physical_area_monitor(i);
278
279 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
280 continue;
281
282 /* get the screen boundaries */
283 al = RECT_LEFT(*area);
284 at = RECT_TOP(*area);
285 ar = RECT_RIGHT(*area);
286 ab = RECT_BOTTOM(*area);
287 pl = RECT_LEFT(*parea);
288 pt = RECT_TOP(*parea);
289 pr = RECT_RIGHT(*parea);
290 pb = RECT_BOTTOM(*parea);
291
292 if (config_resist_edge) {
293 /* horizontal snapping */
294 switch (corn) {
295 case OB_CORNER_TOPLEFT:
296 case OB_CORNER_BOTTOMLEFT:
297 dlt = l;
298 drb = r + *w - c->frame->area.width;
299 if (r <= ar && drb > ar && drb <= ar + config_resist_edge)
300 *w = ar - l + 1;
301 else if (r <= pr && drb > pr && drb <= pr + config_resist_edge)
302 *w = pr - l + 1;
303 break;
304 case OB_CORNER_TOPRIGHT:
305 case OB_CORNER_BOTTOMRIGHT:
306 dlt = l - *w + c->frame->area.width;
307 drb = r;
308 if (l >= al && dlt < al && dlt >= al - config_resist_edge)
309 *w = r - al + 1;
310 else if (l >= pl && dlt < pl && dlt >= pl - config_resist_edge)
311 *w = r - pl + 1;
312 break;
313 }
314
315 /* vertical snapping */
316 switch (corn) {
317 case OB_CORNER_TOPLEFT:
318 case OB_CORNER_TOPRIGHT:
319 dlt = t;
320 drb = b + *h - c->frame->area.height;
321 if (b <= ab && drb > ab && drb <= ab + config_resist_edge)
322 *h = ab - t + 1;
323 else if (b <= pb && drb > pb && drb <= pb + config_resist_edge)
324 *h = pb - t + 1;
325 break;
326 case OB_CORNER_BOTTOMLEFT:
327 case OB_CORNER_BOTTOMRIGHT:
328 dlt = t - *h + c->frame->area.height;
329 drb = b;
330 if (t >= at && dlt < at && dlt >= at - config_resist_edge)
331 *h = b - at + 1;
332 else if (t >= pt && dlt < pt && dlt >= pt - config_resist_edge)
333 *h = b - pt + 1;
334 break;
335 }
336 }
337 }
338 }
This page took 0.051184 seconds and 5 git commands to generate.