1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 resist.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
24 #include "parser/parse.h"
28 void resist_move_windows(ObClient *c, gint *x, gint *y)
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;
36 w = c->frame->area.width;
37 h = c->frame->area.height;
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);
49 if (config_resist_win)
50 for (it = stacking_list; it; it = g_list_next(it)) {
52 gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
54 if (!WINDOW_IS_CLIENT(it->data))
58 /* don't snap to self or non-visibles */
59 if (!target->frame->visible || target == c) continue;
61 /* don't snap to windows in layers beneath */
62 if(target->layer < c->layer && !config_resist_layers_below)
65 tl = RECT_LEFT(target->frame->area) - 1;
66 tt = RECT_TOP(target->frame->area) - 1;
67 tr = RECT_RIGHT(target->frame->area) + 1;
68 tb = RECT_BOTTOM(target->frame->area) + 1;
70 /* snapx and snapy ensure that the window snaps to the top-most
71 window edge available, without going all the way from
72 bottom-to-top in the stacking list
75 if (ct < tb && cb > tt) {
76 if (cl >= tr && l < tr && l >= tr - config_resist_win)
77 *x = tr, snapx = target;
78 else if (cr <= tl && r > tl &&
79 r <= tl + config_resist_win)
80 *x = tl - w + 1, snapx = target;
82 /* try to corner snap to the window */
83 if (ct > tt && t <= tt &&
84 t > tt - config_resist_win)
85 *y = tt + 1, snapy = target;
86 else if (cb < tb && b >= tb &&
87 b < tb + config_resist_win)
88 *y = tb - h, snapy = target;
93 if (cl < tr && cr > tl) {
94 if (ct >= tb && t < tb && t >= tb - config_resist_win)
95 *y = tb, snapy = target;
96 else if (cb <= tt && b > tt &&
97 b <= tt + config_resist_win)
98 *y = tt - h + 1, snapy = target;
100 /* try to corner snap to the window */
101 if (cl > tl && l <= tl &&
102 l > tl - config_resist_win)
103 *x = tl + 1, snapx = target;
104 else if (cr < tr && r >= tr &&
105 r < tr + config_resist_win)
106 *x = tr - w, snapx = target;
111 if (snapx && snapy) break;
115 void resist_move_monitors(ObClient *c, gint *x, gint *y)
119 gint l, t, r, b; /* requested edges */
120 gint al, at, ar, ab; /* screen area edges */
121 gint pl, pt, pr, pb; /* physical screen area edges */
122 gint cl, ct, cr, cb; /* current edges */
123 gint w, h; /* current size */
125 w = c->frame->area.width;
126 h = c->frame->area.height;
133 cl = RECT_LEFT(c->frame->area);
134 ct = RECT_TOP(c->frame->area);
135 cr = RECT_RIGHT(c->frame->area);
136 cb = RECT_BOTTOM(c->frame->area);
138 if (config_resist_edge) {
139 for (i = 0; i < screen_num_monitors; ++i) {
140 area = screen_area_monitor(c->desktop, i);
141 parea = screen_physical_area_monitor(i);
143 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
146 al = RECT_LEFT(*area);
147 at = RECT_TOP(*area);
148 ar = RECT_RIGHT(*area);
149 ab = RECT_BOTTOM(*area);
150 pl = RECT_LEFT(*parea);
151 pt = RECT_TOP(*parea);
152 pr = RECT_RIGHT(*parea);
153 pb = RECT_BOTTOM(*parea);
155 if (cl >= al && l < al && l >= al - config_resist_edge)
157 else if (cr <= ar && r > ar && r <= ar + config_resist_edge)
159 else if (cl >= pl && l < pl && l >= pl - config_resist_edge)
161 else if (cr <= pr && r > pr && r <= pr + config_resist_edge)
164 if (ct >= at && t < at && t >= at - config_resist_edge)
166 else if (cb <= ab && b > ab && b < ab + config_resist_edge)
168 else if (ct >= pt && t < pt && t >= pt - config_resist_edge)
170 else if (cb <= pb && b > pb && b < pb + config_resist_edge)
176 void resist_size_windows(ObClient *c, gint *w, gint *h, ObCorner corn)
179 ObClient *target; /* target */
180 gint l, t, r, b; /* my left, top, right and bottom sides */
181 gint dlt, drb; /* my destination left/top and right/bottom sides */
182 gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
184 ObClient *snapx = NULL, *snapy = NULL;
186 incw = c->size_inc.width;
187 inch = c->size_inc.height;
189 l = RECT_LEFT(c->frame->area);
190 r = RECT_RIGHT(c->frame->area);
191 t = RECT_TOP(c->frame->area);
192 b = RECT_BOTTOM(c->frame->area);
194 if (config_resist_win) {
195 for (it = stacking_list; it; it = g_list_next(it)) {
196 if (!WINDOW_IS_CLIENT(it->data))
200 /* don't snap to invisibles or ourself */
201 if (!target->frame->visible || target == c) continue;
203 /* don't snap to windows in layers beneath */
204 if(target->layer < c->layer && !config_resist_layers_below)
207 tl = RECT_LEFT(target->frame->area);
208 tr = RECT_RIGHT(target->frame->area);
209 tt = RECT_TOP(target->frame->area);
210 tb = RECT_BOTTOM(target->frame->area);
213 /* horizontal snapping */
214 if (t < tb && b > tt) {
216 case OB_CORNER_TOPLEFT:
217 case OB_CORNER_BOTTOMLEFT:
219 drb = r + *w - c->frame->area.width;
220 if (r < tl && drb >= tl &&
221 drb < tl + config_resist_win)
222 *w = tl - l, snapx = target;
224 case OB_CORNER_TOPRIGHT:
225 case OB_CORNER_BOTTOMRIGHT:
226 dlt = l - *w + c->frame->area.width;
228 if (l > tr && dlt <= tr &&
229 dlt > tr - config_resist_win)
230 *w = r - tr, snapx = target;
237 /* vertical snapping */
238 if (l < tr && r > tl) {
240 case OB_CORNER_TOPLEFT:
241 case OB_CORNER_TOPRIGHT:
243 drb = b + *h - c->frame->area.height;
244 if (b < tt && drb >= tt &&
245 drb < tt + config_resist_win)
246 *h = tt - t, snapy = target;
248 case OB_CORNER_BOTTOMLEFT:
249 case OB_CORNER_BOTTOMRIGHT:
250 dlt = t - *h + c->frame->area.height;
252 if (t > tb && dlt <= tb &&
253 dlt > tb - config_resist_win)
254 *h = b - tb, snapy = target;
260 /* snapped both ways */
261 if (snapx && snapy) break;
266 void resist_size_monitors(ObClient *c, gint *w, gint *h, ObCorner corn)
268 gint l, t, r, b; /* my left, top, right and bottom sides */
269 gint dlt, drb; /* my destination left/top and right/bottom sides */
271 gint al, at, ar, ab; /* screen boundaries */
272 gint pl, pt, pr, pb; /* physical screen boundaries */
276 l = RECT_LEFT(c->frame->area);
277 r = RECT_RIGHT(c->frame->area);
278 t = RECT_TOP(c->frame->area);
279 b = RECT_BOTTOM(c->frame->area);
281 incw = c->size_inc.width;
282 inch = c->size_inc.height;
284 for (i = 0; i < screen_num_monitors; ++i) {
285 area = screen_area_monitor(c->desktop, i);
286 parea = screen_physical_area_monitor(i);
288 if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
291 /* get the screen boundaries */
292 al = RECT_LEFT(*area);
293 at = RECT_TOP(*area);
294 ar = RECT_RIGHT(*area);
295 ab = RECT_BOTTOM(*area);
296 pl = RECT_LEFT(*parea);
297 pt = RECT_TOP(*parea);
298 pr = RECT_RIGHT(*parea);
299 pb = RECT_BOTTOM(*parea);
301 if (config_resist_edge) {
302 /* horizontal snapping */
304 case OB_CORNER_TOPLEFT:
305 case OB_CORNER_BOTTOMLEFT:
307 drb = r + *w - c->frame->area.width;
308 if (r <= ar && drb > ar && drb <= ar + config_resist_edge)
310 else if (r <= pr && drb > pr && drb <= pr + config_resist_edge)
313 case OB_CORNER_TOPRIGHT:
314 case OB_CORNER_BOTTOMRIGHT:
315 dlt = l - *w + c->frame->area.width;
317 if (l >= al && dlt < al && dlt >= al - config_resist_edge)
319 else if (l >= pl && dlt < pl && dlt >= pl - config_resist_edge)
324 /* vertical snapping */
326 case OB_CORNER_TOPLEFT:
327 case OB_CORNER_TOPRIGHT:
329 drb = b + *h - c->frame->area.height;
330 if (b <= ab && drb > ab && drb <= ab + config_resist_edge)
332 else if (b <= pb && drb > pb && drb <= pb + config_resist_edge)
335 case OB_CORNER_BOTTOMLEFT:
336 case OB_CORNER_BOTTOMRIGHT:
337 dlt = t - *h + c->frame->area.height;
339 if (t >= at && dlt < at && dlt >= at - config_resist_edge)
341 else if (t >= pt && dlt < pt && dlt >= pt - config_resist_edge)