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.
30 static gboolean resist_move_window(Rect window,
31 Rect target, gint resist,
34 gint l, t, r, b; /* requested edges */
35 gint cl, ct, cr, cb; /* current edges */
36 gint w, h; /* current size */
37 gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
38 gboolean snapx = 0, snapy = 0;
48 cl = RECT_LEFT(window);
49 ct = RECT_TOP(window);
50 cr = RECT_RIGHT(window);
51 cb = RECT_BOTTOM(window);
53 tl = RECT_LEFT(target) - 1;
54 tt = RECT_TOP(target) - 1;
55 tr = RECT_RIGHT(target) + 1;
56 tb = RECT_BOTTOM(target) + 1;
58 /* snapx and snapy ensure that the window snaps to the top-most
59 window edge available, without going all the way from
60 bottom-to-top in the stacking list
63 if (ct < tb && cb > tt) {
64 if (cl >= tr && l < tr && l >= tr - resist)
65 *x = tr, snapx = TRUE;
66 else if (cr <= tl && r > tl &&
68 *x = tl - w + 1, snapx = TRUE;
70 /* try to corner snap to the window */
71 if (ct > tt && t <= tt &&
73 *y = tt + 1, snapy = TRUE;
74 else if (cb < tb && b >= tb &&
76 *y = tb - h, snapy = TRUE;
81 if (cl < tr && cr > tl) {
82 if (ct >= tb && t < tb && t >= tb - resist)
83 *y = tb, snapy = TRUE;
84 else if (cb <= tt && b > tt &&
86 *y = tt - h + 1, snapy = TRUE;
88 /* try to corner snap to the window */
89 if (cl > tl && l <= tl &&
91 *x = tl + 1, snapx = TRUE;
92 else if (cr < tr && r >= tr &&
94 *x = tr - w, snapx = TRUE;
99 return snapx && snapy;
102 void resist_move_windows(ObClient *c, gint resist, gint *x, gint *y)
109 frame_client_gravity(c->frame, x, y);
111 for (it = stacking_list; it; it = g_list_next(it)) {
114 if (!WINDOW_IS_CLIENT(it->data))
118 /* don't snap to self or non-visibles */
119 if (!target->frame->visible || target == c)
122 if (resist_move_window(c->frame->area, target->frame->area,
126 dock_get_area(&dock_area);
127 resist_move_window(c->frame->area, dock_area, resist, x, y);
129 frame_frame_gravity(c->frame, x, y);
132 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
136 gint l, t, r, b; /* requested edges */
137 gint al, at, ar, ab; /* screen area edges */
138 gint pl, pt, pr, pb; /* physical screen area edges */
139 gint cl, ct, cr, cb; /* current edges */
140 gint w, h; /* current size */
145 frame_client_gravity(c->frame, x, y);
147 w = c->frame->area.width;
148 h = c->frame->area.height;
155 cl = RECT_LEFT(c->frame->area);
156 ct = RECT_TOP(c->frame->area);
157 cr = RECT_RIGHT(c->frame->area);
158 cb = RECT_BOTTOM(c->frame->area);
160 RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);
162 for (i = 0; i < screen_num_monitors; ++i) {
163 parea = screen_physical_area_monitor(i);
165 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
170 area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
173 al = RECT_LEFT(*area);
174 at = RECT_TOP(*area);
175 ar = RECT_RIGHT(*area);
176 ab = RECT_BOTTOM(*area);
177 pl = RECT_LEFT(*parea);
178 pt = RECT_TOP(*parea);
179 pr = RECT_RIGHT(*parea);
180 pb = RECT_BOTTOM(*parea);
182 if (cl >= al && l < al && l >= al - resist)
184 else if (cr <= ar && r > ar && r <= ar + resist)
186 else if (cl >= pl && l < pl && l >= pl - resist)
188 else if (cr <= pr && r > pr && r <= pr + resist)
191 if (ct >= at && t < at && t >= at - resist)
193 else if (cb <= ab && b > ab && b < ab + resist)
195 else if (ct >= pt && t < pt && t >= pt - resist)
197 else if (cb <= pb && b > pb && b < pb + resist)
204 frame_frame_gravity(c->frame, x, y);
207 static gboolean resist_size_window(Rect window, Rect target, gint resist,
208 gint *w, gint *h, ObDirection dir)
210 gint l, t, r, b; /* my left, top, right and bottom sides */
211 gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
212 gint dlt, drb; /* my destination left/top and right/bottom sides */
213 gboolean snapx = 0, snapy = 0;
216 l = RECT_LEFT(window);
217 t = RECT_TOP(window);
218 r = RECT_RIGHT(window);
219 b = RECT_BOTTOM(window);
222 orgh = window.height;
224 tl = RECT_LEFT(target);
225 tt = RECT_TOP(target);
226 tr = RECT_RIGHT(target);
227 tb = RECT_BOTTOM(target);
230 /* horizontal snapping */
231 if (t < tb && b > tt) {
233 case OB_DIRECTION_EAST:
234 case OB_DIRECTION_NORTHEAST:
235 case OB_DIRECTION_SOUTHEAST:
236 case OB_DIRECTION_NORTH:
237 case OB_DIRECTION_SOUTH:
240 if (r < tl && drb >= tl &&
242 *w = tl - l, snapx = TRUE;
244 case OB_DIRECTION_WEST:
245 case OB_DIRECTION_NORTHWEST:
246 case OB_DIRECTION_SOUTHWEST:
249 if (l > tr && dlt <= tr &&
251 *w = r - tr, snapx = TRUE;
258 /* vertical snapping */
259 if (l < tr && r > tl) {
261 case OB_DIRECTION_SOUTH:
262 case OB_DIRECTION_SOUTHWEST:
263 case OB_DIRECTION_SOUTHEAST:
264 case OB_DIRECTION_EAST:
265 case OB_DIRECTION_WEST:
268 if (b < tt && drb >= tt &&
270 *h = tt - t, snapy = TRUE;
272 case OB_DIRECTION_NORTH:
273 case OB_DIRECTION_NORTHWEST:
274 case OB_DIRECTION_NORTHEAST:
277 if (t > tb && dlt <= tb &&
279 *h = b - tb, snapy = TRUE;
285 /* snapped both ways */
286 return snapx && snapy;
289 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
293 ObClient *target; /* target */
298 for (it = stacking_list; it; it = g_list_next(it)) {
299 if (!WINDOW_IS_CLIENT(it->data))
303 /* don't snap to invisibles or ourself */
304 if (!target->frame->visible || target == c)
307 if (resist_size_window(c->frame->area, target->frame->area,
311 dock_get_area(&dock_area);
312 resist_size_window(c->frame->area, dock_area,
316 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
319 gint l, t, r, b; /* my left, top, right and bottom sides */
320 gint dlt, drb; /* my destination left/top and right/bottom sides */
322 gint al, at, ar, ab; /* screen boundaries */
323 gint pl, pt, pr, pb; /* physical screen boundaries */
330 l = RECT_LEFT(c->frame->area);
331 r = RECT_RIGHT(c->frame->area);
332 t = RECT_TOP(c->frame->area);
333 b = RECT_BOTTOM(c->frame->area);
335 incw = c->size_inc.width;
336 inch = c->size_inc.height;
338 RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
340 for (i = 0; i < screen_num_monitors; ++i) {
341 parea = screen_physical_area_monitor(i);
343 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
348 area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
351 /* get the screen boundaries */
352 al = RECT_LEFT(*area);
353 at = RECT_TOP(*area);
354 ar = RECT_RIGHT(*area);
355 ab = RECT_BOTTOM(*area);
356 pl = RECT_LEFT(*parea);
357 pt = RECT_TOP(*parea);
358 pr = RECT_RIGHT(*parea);
359 pb = RECT_BOTTOM(*parea);
361 /* horizontal snapping */
363 case OB_DIRECTION_EAST:
364 case OB_DIRECTION_NORTHEAST:
365 case OB_DIRECTION_SOUTHEAST:
366 case OB_DIRECTION_NORTH:
367 case OB_DIRECTION_SOUTH:
369 drb = r + *w - c->frame->area.width;
370 if (r <= ar && drb > ar && drb <= ar + resist)
372 else if (r <= pr && drb > pr && drb <= pr + resist)
375 case OB_DIRECTION_WEST:
376 case OB_DIRECTION_NORTHWEST:
377 case OB_DIRECTION_SOUTHWEST:
378 dlt = l - *w + c->frame->area.width;
380 if (l >= al && dlt < al && dlt >= al - resist)
382 else if (l >= pl && dlt < pl && dlt >= pl - resist)
387 /* vertical snapping */
389 case OB_DIRECTION_SOUTH:
390 case OB_DIRECTION_SOUTHWEST:
391 case OB_DIRECTION_SOUTHEAST:
392 case OB_DIRECTION_WEST:
393 case OB_DIRECTION_EAST:
395 drb = b + *h - c->frame->area.height;
396 if (b <= ab && drb > ab && drb <= ab + resist)
398 else if (b <= pb && drb > pb && drb <= pb + resist)
401 case OB_DIRECTION_NORTH:
402 case OB_DIRECTION_NORTHWEST:
403 case OB_DIRECTION_NORTHEAST:
404 dlt = t - *h + c->frame->area.height;
406 if (t >= at && dlt < at && dlt >= at - resist)
408 else if (t >= pt && dlt < pt && dlt >= pt - resist)