]> Dogcows Code - chaz/openbox/blob - openbox/resist.c
Make the focus cycle indicator follow target fallback in the popup
[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) 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 #include "client.h"
21 #include "frame.h"
22 #include "stacking.h"
23 #include "screen.h"
24 #include "dock.h"
25 #include "config.h"
26 #include "resist.h"
27 #include "parser/parse.h"
28
29 #include <glib.h>
30
31 static gboolean resist_move_window(Rect window,
32 Rect target, gint resist,
33 gint *x, gint *y)
34 {
35 gint l, t, r, b; /* requested edges */
36 gint cl, ct, cr, cb; /* current edges */
37 gint w, h; /* current size */
38 gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
39 gboolean snapx = 0, snapy = 0;
40
41 w = window.width;
42 h = window.height;
43
44 l = *x;
45 t = *y;
46 r = l + w - 1;
47 b = t + h - 1;
48
49 cl = RECT_LEFT(window);
50 ct = RECT_TOP(window);
51 cr = RECT_RIGHT(window);
52 cb = RECT_BOTTOM(window);
53
54 tl = RECT_LEFT(target) - 1;
55 tt = RECT_TOP(target) - 1;
56 tr = RECT_RIGHT(target) + 1;
57 tb = RECT_BOTTOM(target) + 1;
58
59 /* snapx and snapy ensure that the window snaps to the top-most
60 window edge available, without going all the way from
61 bottom-to-top in the stacking list
62 */
63 if (!snapx) {
64 if (ct < tb && cb > tt) {
65 if (cl >= tr && l < tr && l >= tr - resist)
66 *x = tr, snapx = TRUE;
67 else if (cr <= tl && r > tl &&
68 r <= tl + resist)
69 *x = tl - w + 1, snapx = TRUE;
70 if (snapx) {
71 /* try to corner snap to the window */
72 if (ct > tt && t <= tt &&
73 t > tt - resist)
74 *y = tt + 1, snapy = TRUE;
75 else if (cb < tb && b >= tb &&
76 b < tb + resist)
77 *y = tb - h, snapy = TRUE;
78 }
79 }
80 }
81 if (!snapy) {
82 if (cl < tr && cr > tl) {
83 if (ct >= tb && t < tb && t >= tb - resist)
84 *y = tb, snapy = TRUE;
85 else if (cb <= tt && b > tt &&
86 b <= tt + resist)
87 *y = tt - h + 1, snapy = TRUE;
88 if (snapy) {
89 /* try to corner snap to the window */
90 if (cl > tl && l <= tl &&
91 l > tl - resist)
92 *x = tl + 1, snapx = TRUE;
93 else if (cr < tr && r >= tr &&
94 r < tr + resist)
95 *x = tr - w, snapx = TRUE;
96 }
97 }
98 }
99
100 return snapx && snapy;
101 }
102
103 void resist_move_windows(ObClient *c, gint resist, gint *x, gint *y)
104 {
105 GList *it;
106 Rect dock_area;
107
108 if (!resist) return;
109
110 frame_client_gravity(c->frame, x, y);
111
112
113 for (it = stacking_list; it; it = g_list_next(it)) {
114 ObClient *target;
115
116 if (!WINDOW_IS_CLIENT(it->data))
117 continue;
118 target = it->data;
119
120 /* don't snap to self or non-visibles */
121 if (!target->frame->visible || target == c)
122 continue;
123 /* don't snap to windows set to below and skip_taskbar (desklets) */
124 if (target->below && !c->below && target->skip_taskbar)
125 continue;
126
127 if (resist_move_window(c->frame->area, target->frame->area,
128 resist, x, y))
129 break;
130 }
131 dock_get_area(&dock_area);
132 resist_move_window(c->frame->area, dock_area, resist, x, y);
133
134 frame_frame_gravity(c->frame, x, y);
135 }
136
137 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
138 {
139 Rect *area, *parea;
140 guint i;
141 gint l, t, r, b; /* requested edges */
142 gint al, at, ar, ab; /* screen area edges */
143 gint pl, pt, pr, pb; /* physical screen area edges */
144 gint cl, ct, cr, cb; /* current edges */
145 gint w, h; /* current size */
146 Rect desired_area;
147
148 if (!resist) return;
149
150 frame_client_gravity(c->frame, x, y);
151
152 w = c->frame->area.width;
153 h = c->frame->area.height;
154
155 l = *x;
156 t = *y;
157 r = l + w - 1;
158 b = t + h - 1;
159
160 cl = RECT_LEFT(c->frame->area);
161 ct = RECT_TOP(c->frame->area);
162 cr = RECT_RIGHT(c->frame->area);
163 cb = RECT_BOTTOM(c->frame->area);
164
165 RECT_SET(desired_area, c->frame->area.x, c->frame->area.y,
166 c->frame->area.width, c->frame->area.height);
167
168 for (i = 0; i < screen_num_monitors; ++i) {
169 parea = screen_physical_area_monitor(i);
170
171 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
172 g_free(parea);
173 continue;
174 }
175
176 area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
177 &desired_area);
178
179 al = RECT_LEFT(*area);
180 at = RECT_TOP(*area);
181 ar = RECT_RIGHT(*area);
182 ab = RECT_BOTTOM(*area);
183 pl = RECT_LEFT(*parea);
184 pt = RECT_TOP(*parea);
185 pr = RECT_RIGHT(*parea);
186 pb = RECT_BOTTOM(*parea);
187
188 if (cl >= al && l < al && l >= al - resist)
189 *x = al;
190 else if (cr <= ar && r > ar && r <= ar + resist)
191 *x = ar - w + 1;
192 else if (cl >= pl && l < pl && l >= pl - resist)
193 *x = pl;
194 else if (cr <= pr && r > pr && r <= pr + resist)
195 *x = pr - w + 1;
196
197 if (ct >= at && t < at && t >= at - resist)
198 *y = at;
199 else if (cb <= ab && b > ab && b < ab + resist)
200 *y = ab - h + 1;
201 else if (ct >= pt && t < pt && t >= pt - resist)
202 *y = pt;
203 else if (cb <= pb && b > pb && b < pb + resist)
204 *y = pb - h + 1;
205
206 g_free(area);
207 g_free(parea);
208 }
209
210 frame_frame_gravity(c->frame, x, y);
211 }
212
213 static gboolean resist_size_window(Rect window, Rect target, gint resist,
214 gint *w, gint *h, ObDirection dir)
215 {
216 gint l, t, r, b; /* my left, top, right and bottom sides */
217 gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
218 gint dlt, drb; /* my destination left/top and right/bottom sides */
219 gboolean snapx = 0, snapy = 0;
220 gint orgw, orgh;
221
222 l = RECT_LEFT(window);
223 t = RECT_TOP(window);
224 r = RECT_RIGHT(window);
225 b = RECT_BOTTOM(window);
226
227 orgw = window.width;
228 orgh = window.height;
229
230 tl = RECT_LEFT(target);
231 tt = RECT_TOP(target);
232 tr = RECT_RIGHT(target);
233 tb = RECT_BOTTOM(target);
234
235 if (!snapx) {
236 /* horizontal snapping */
237 if (t < tb && b > tt) {
238 switch (dir) {
239 case OB_DIRECTION_EAST:
240 case OB_DIRECTION_NORTHEAST:
241 case OB_DIRECTION_SOUTHEAST:
242 case OB_DIRECTION_NORTH:
243 case OB_DIRECTION_SOUTH:
244 dlt = l;
245 drb = r + *w - orgw;
246 if (r < tl && drb >= tl &&
247 drb < tl + resist)
248 *w = tl - l, snapx = TRUE;
249 break;
250 case OB_DIRECTION_WEST:
251 case OB_DIRECTION_NORTHWEST:
252 case OB_DIRECTION_SOUTHWEST:
253 dlt = l - *w + orgw;
254 drb = r;
255 if (l > tr && dlt <= tr &&
256 dlt > tr - resist)
257 *w = r - tr, snapx = TRUE;
258 break;
259 }
260 }
261 }
262
263 if (!snapy) {
264 /* vertical snapping */
265 if (l < tr && r > tl) {
266 switch (dir) {
267 case OB_DIRECTION_SOUTH:
268 case OB_DIRECTION_SOUTHWEST:
269 case OB_DIRECTION_SOUTHEAST:
270 case OB_DIRECTION_EAST:
271 case OB_DIRECTION_WEST:
272 dlt = t;
273 drb = b + *h - orgh;
274 if (b < tt && drb >= tt &&
275 drb < tt + resist)
276 *h = tt - t, snapy = TRUE;
277 break;
278 case OB_DIRECTION_NORTH:
279 case OB_DIRECTION_NORTHWEST:
280 case OB_DIRECTION_NORTHEAST:
281 dlt = t - *h + orgh;
282 drb = b;
283 if (t > tb && dlt <= tb &&
284 dlt > tb - resist)
285 *h = b - tb, snapy = TRUE;
286 break;
287 }
288 }
289 }
290
291 /* snapped both ways */
292 return snapx && snapy;
293 }
294
295 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
296 ObDirection dir)
297 {
298 GList *it;
299 ObClient *target; /* target */
300 Rect dock_area;
301
302 if (!resist) return;
303
304 for (it = stacking_list; it; it = g_list_next(it)) {
305 if (!WINDOW_IS_CLIENT(it->data))
306 continue;
307 target = it->data;
308
309 /* don't snap to invisibles or ourself */
310 if (!target->frame->visible || target == c)
311 continue;
312 /* don't snap to windows set to below and skip_taskbar (desklets) */
313 if (target->below && !c->below && target->skip_taskbar)
314 continue;
315
316 if (resist_size_window(c->frame->area, target->frame->area,
317 resist, w, h, dir))
318 break;
319 }
320 dock_get_area(&dock_area);
321 resist_size_window(c->frame->area, dock_area,
322 resist, w, h, dir);
323 }
324
325 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
326 ObDirection dir)
327 {
328 gint l, t, r, b; /* my left, top, right and bottom sides */
329 gint dlt, drb; /* my destination left/top and right/bottom sides */
330 Rect *area, *parea;
331 gint al, at, ar, ab; /* screen boundaries */
332 gint pl, pt, pr, pb; /* physical screen boundaries */
333 gint incw, inch;
334 guint i;
335 Rect desired_area;
336
337 if (!resist) return;
338
339 l = RECT_LEFT(c->frame->area);
340 r = RECT_RIGHT(c->frame->area);
341 t = RECT_TOP(c->frame->area);
342 b = RECT_BOTTOM(c->frame->area);
343
344 incw = c->size_inc.width;
345 inch = c->size_inc.height;
346
347 RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
348
349 for (i = 0; i < screen_num_monitors; ++i) {
350 parea = screen_physical_area_monitor(i);
351
352 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
353 g_free(parea);
354 continue;
355 }
356
357 area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
358 &desired_area);
359
360 /* get the screen boundaries */
361 al = RECT_LEFT(*area);
362 at = RECT_TOP(*area);
363 ar = RECT_RIGHT(*area);
364 ab = RECT_BOTTOM(*area);
365 pl = RECT_LEFT(*parea);
366 pt = RECT_TOP(*parea);
367 pr = RECT_RIGHT(*parea);
368 pb = RECT_BOTTOM(*parea);
369
370 /* horizontal snapping */
371 switch (dir) {
372 case OB_DIRECTION_EAST:
373 case OB_DIRECTION_NORTHEAST:
374 case OB_DIRECTION_SOUTHEAST:
375 case OB_DIRECTION_NORTH:
376 case OB_DIRECTION_SOUTH:
377 dlt = l;
378 drb = r + *w - c->frame->area.width;
379 if (r <= ar && drb > ar && drb <= ar + resist)
380 *w = ar - l + 1;
381 else if (r <= pr && drb > pr && drb <= pr + resist)
382 *w = pr - l + 1;
383 break;
384 case OB_DIRECTION_WEST:
385 case OB_DIRECTION_NORTHWEST:
386 case OB_DIRECTION_SOUTHWEST:
387 dlt = l - *w + c->frame->area.width;
388 drb = r;
389 if (l >= al && dlt < al && dlt >= al - resist)
390 *w = r - al + 1;
391 else if (l >= pl && dlt < pl && dlt >= pl - resist)
392 *w = r - pl + 1;
393 break;
394 }
395
396 /* vertical snapping */
397 switch (dir) {
398 case OB_DIRECTION_SOUTH:
399 case OB_DIRECTION_SOUTHWEST:
400 case OB_DIRECTION_SOUTHEAST:
401 case OB_DIRECTION_WEST:
402 case OB_DIRECTION_EAST:
403 dlt = t;
404 drb = b + *h - c->frame->area.height;
405 if (b <= ab && drb > ab && drb <= ab + resist)
406 *h = ab - t + 1;
407 else if (b <= pb && drb > pb && drb <= pb + resist)
408 *h = pb - t + 1;
409 break;
410 case OB_DIRECTION_NORTH:
411 case OB_DIRECTION_NORTHWEST:
412 case OB_DIRECTION_NORTHEAST:
413 dlt = t - *h + c->frame->area.height;
414 drb = b;
415 if (t >= at && dlt < at && dlt >= at - resist)
416 *h = b - at + 1;
417 else if (t >= pt && dlt < pt && dlt >= pt - resist)
418 *h = b - pt + 1;
419 break;
420 }
421
422 g_free(area);
423 g_free(parea);
424 }
425 }
This page took 0.053063 seconds and 4 git commands to generate.