1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 resist.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
27 #include "parser/parse.h"
31 static gboolean resist_move_window(Rect window,
32 Rect target, gint resist,
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;
49 cl = RECT_LEFT(window);
50 ct = RECT_TOP(window);
51 cr = RECT_RIGHT(window);
52 cb = RECT_BOTTOM(window);
54 tl = RECT_LEFT(target) - 1;
55 tt = RECT_TOP(target) - 1;
56 tr = RECT_RIGHT(target) + 1;
57 tb = RECT_BOTTOM(target) + 1;
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
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 &&
69 *x = tl - w + 1, snapx = TRUE;
71 /* try to corner snap to the window */
72 if (ct > tt && t <= tt &&
74 *y = tt + 1, snapy = TRUE;
75 else if (cb < tb && b >= tb &&
77 *y = tb - h, snapy = TRUE;
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 &&
87 *y = tt - h + 1, snapy = TRUE;
89 /* try to corner snap to the window */
90 if (cl > tl && l <= tl &&
92 *x = tl + 1, snapx = TRUE;
93 else if (cr < tr && r >= tr &&
95 *x = tr - w, snapx = TRUE;
100 return snapx && snapy;
103 void resist_move_windows(ObClient *c, gint resist, gint *x, gint *y)
110 frame_client_gravity(c->frame, x, y);
113 for (it = stacking_list; it; it = g_list_next(it)) {
116 if (!WINDOW_IS_CLIENT(it->data))
120 /* don't snap to self or non-visibles */
121 if (!target->frame->visible || target == c)
123 /* don't snap to windows set to below and skip_taskbar (desklets) */
124 if (target->below && !c->below && target->skip_taskbar)
127 if (resist_move_window(c->frame->area, target->frame->area,
131 dock_get_area(&dock_area);
132 resist_move_window(c->frame->area, dock_area, resist, x, y);
134 frame_frame_gravity(c->frame, x, y);
137 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
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 */
150 frame_client_gravity(c->frame, x, y);
152 w = c->frame->area.width;
153 h = c->frame->area.height;
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);
165 RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);
167 for (i = 0; i < screen_num_monitors; ++i) {
168 parea = screen_physical_area_monitor(i);
170 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
175 area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
178 al = RECT_LEFT(*area);
179 at = RECT_TOP(*area);
180 ar = RECT_RIGHT(*area);
181 ab = RECT_BOTTOM(*area);
182 pl = RECT_LEFT(*parea);
183 pt = RECT_TOP(*parea);
184 pr = RECT_RIGHT(*parea);
185 pb = RECT_BOTTOM(*parea);
187 if (cl >= al && l < al && l >= al - resist)
189 else if (cr <= ar && r > ar && r <= ar + resist)
191 else if (cl >= pl && l < pl && l >= pl - resist)
193 else if (cr <= pr && r > pr && r <= pr + resist)
196 if (ct >= at && t < at && t >= at - resist)
198 else if (cb <= ab && b > ab && b < ab + resist)
200 else if (ct >= pt && t < pt && t >= pt - resist)
202 else if (cb <= pb && b > pb && b < pb + resist)
209 frame_frame_gravity(c->frame, x, y);
212 static gboolean resist_size_window(Rect window, Rect target, gint resist,
213 gint *w, gint *h, ObDirection dir)
215 gint l, t, r, b; /* my left, top, right and bottom sides */
216 gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
217 gint dlt, drb; /* my destination left/top and right/bottom sides */
218 gboolean snapx = 0, snapy = 0;
221 l = RECT_LEFT(window);
222 t = RECT_TOP(window);
223 r = RECT_RIGHT(window);
224 b = RECT_BOTTOM(window);
227 orgh = window.height;
229 tl = RECT_LEFT(target);
230 tt = RECT_TOP(target);
231 tr = RECT_RIGHT(target);
232 tb = RECT_BOTTOM(target);
235 /* horizontal snapping */
236 if (t < tb && b > tt) {
238 case OB_DIRECTION_EAST:
239 case OB_DIRECTION_NORTHEAST:
240 case OB_DIRECTION_SOUTHEAST:
241 case OB_DIRECTION_NORTH:
242 case OB_DIRECTION_SOUTH:
245 if (r < tl && drb >= tl &&
247 *w = tl - l, snapx = TRUE;
249 case OB_DIRECTION_WEST:
250 case OB_DIRECTION_NORTHWEST:
251 case OB_DIRECTION_SOUTHWEST:
254 if (l > tr && dlt <= tr &&
256 *w = r - tr, snapx = TRUE;
263 /* vertical snapping */
264 if (l < tr && r > tl) {
266 case OB_DIRECTION_SOUTH:
267 case OB_DIRECTION_SOUTHWEST:
268 case OB_DIRECTION_SOUTHEAST:
269 case OB_DIRECTION_EAST:
270 case OB_DIRECTION_WEST:
273 if (b < tt && drb >= tt &&
275 *h = tt - t, snapy = TRUE;
277 case OB_DIRECTION_NORTH:
278 case OB_DIRECTION_NORTHWEST:
279 case OB_DIRECTION_NORTHEAST:
282 if (t > tb && dlt <= tb &&
284 *h = b - tb, snapy = TRUE;
290 /* snapped both ways */
291 return snapx && snapy;
294 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
298 ObClient *target; /* target */
303 for (it = stacking_list; it; it = g_list_next(it)) {
304 if (!WINDOW_IS_CLIENT(it->data))
308 /* don't snap to invisibles or ourself */
309 if (!target->frame->visible || target == c)
311 /* don't snap to windows set to below and skip_taskbar (desklets) */
312 if (target->below && !c->below && target->skip_taskbar)
315 if (resist_size_window(c->frame->area, target->frame->area,
319 dock_get_area(&dock_area);
320 resist_size_window(c->frame->area, dock_area,
324 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
327 gint l, t, r, b; /* my left, top, right and bottom sides */
328 gint dlt, drb; /* my destination left/top and right/bottom sides */
330 gint al, at, ar, ab; /* screen boundaries */
331 gint pl, pt, pr, pb; /* physical screen boundaries */
338 l = RECT_LEFT(c->frame->area);
339 r = RECT_RIGHT(c->frame->area);
340 t = RECT_TOP(c->frame->area);
341 b = RECT_BOTTOM(c->frame->area);
343 incw = c->size_inc.width;
344 inch = c->size_inc.height;
346 RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
348 for (i = 0; i < screen_num_monitors; ++i) {
349 parea = screen_physical_area_monitor(i);
351 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
356 area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
359 /* get the screen boundaries */
360 al = RECT_LEFT(*area);
361 at = RECT_TOP(*area);
362 ar = RECT_RIGHT(*area);
363 ab = RECT_BOTTOM(*area);
364 pl = RECT_LEFT(*parea);
365 pt = RECT_TOP(*parea);
366 pr = RECT_RIGHT(*parea);
367 pb = RECT_BOTTOM(*parea);
369 /* horizontal snapping */
371 case OB_DIRECTION_EAST:
372 case OB_DIRECTION_NORTHEAST:
373 case OB_DIRECTION_SOUTHEAST:
374 case OB_DIRECTION_NORTH:
375 case OB_DIRECTION_SOUTH:
377 drb = r + *w - c->frame->area.width;
378 if (r <= ar && drb > ar && drb <= ar + resist)
380 else if (r <= pr && drb > pr && drb <= pr + resist)
383 case OB_DIRECTION_WEST:
384 case OB_DIRECTION_NORTHWEST:
385 case OB_DIRECTION_SOUTHWEST:
386 dlt = l - *w + c->frame->area.width;
388 if (l >= al && dlt < al && dlt >= al - resist)
390 else if (l >= pl && dlt < pl && dlt >= pl - resist)
395 /* vertical snapping */
397 case OB_DIRECTION_SOUTH:
398 case OB_DIRECTION_SOUTHWEST:
399 case OB_DIRECTION_SOUTHEAST:
400 case OB_DIRECTION_WEST:
401 case OB_DIRECTION_EAST:
403 drb = b + *h - c->frame->area.height;
404 if (b <= ab && drb > ab && drb <= ab + resist)
406 else if (b <= pb && drb > pb && drb <= pb + resist)
409 case OB_DIRECTION_NORTH:
410 case OB_DIRECTION_NORTHWEST:
411 case OB_DIRECTION_NORTHEAST:
412 dlt = t - *h + c->frame->area.height;
414 if (t >= at && dlt < at && dlt >= at - resist)
416 else if (t >= pt && dlt < pt && dlt >= pt - resist)