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 != NULL; it = it->next) {
52 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
54 if (!WINDOW_IS_CLIENT(it->data))
57 /* don't snap to self or non-visibles */
58 if (!target->frame->visible || target == c) continue;
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;
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
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;
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;
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;
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;
106 if (snapx && snapy) break;
110 void resist_move_monitors(ObClient *c, gint *x, gint *y)
114 gint l, t, r, b; /* requested edges */
115 gint al, at, ar, ab; /* screen area edges */
116 gint cl, ct, cr, cb; /* current edges */
117 gint w, h; /* current size */
119 w = c->frame->area.width;
120 h = c->frame->area.height;
127 cl = RECT_LEFT(c->frame->area);
128 ct = RECT_TOP(c->frame->area);
129 cr = RECT_RIGHT(c->frame->area);
130 cb = RECT_BOTTOM(c->frame->area);
132 if (config_resist_edge) {
133 for (i = 0; i < screen_num_monitors; ++i) {
134 area = screen_area_monitor(c->desktop, i);
136 if (!RECT_INTERSECTS_RECT(*area, c->frame->area))
139 al = RECT_LEFT(*area);
140 at = RECT_TOP(*area);
141 ar = RECT_RIGHT(*area);
142 ab = RECT_BOTTOM(*area);
144 if (cl >= al && l < al && l >= al - config_resist_edge)
146 else if (cr <= ar && r > ar && r <= ar + config_resist_edge)
148 if (ct >= at && t < at && t >= at - config_resist_edge)
150 else if (cb <= ab && b > ab && b < ab + config_resist_edge)
156 void resist_size_windows(ObClient *c, gint *w, gint *h, ObCorner corn)
159 ObClient *target; /* target */
160 gint l, t, r, b; /* my left, top, right and bottom sides */
161 gint dlt, drb; /* my destination left/top and right/bottom sides */
162 gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
164 ObClient *snapx = NULL, *snapy = NULL;
166 incw = c->size_inc.width;
167 inch = c->size_inc.height;
169 l = RECT_LEFT(c->frame->area);
170 r = RECT_RIGHT(c->frame->area);
171 t = RECT_TOP(c->frame->area);
172 b = RECT_BOTTOM(c->frame->area);
174 if (config_resist_win) {
175 for (it = stacking_list; it != NULL; it = it->next) {
176 if (!WINDOW_IS_CLIENT(it->data))
180 /* don't snap to invisibles or ourself */
181 if (!target->frame->visible || target == c) continue;
183 tl = RECT_LEFT(target->frame->area);
184 tr = RECT_RIGHT(target->frame->area);
185 tt = RECT_TOP(target->frame->area);
186 tb = RECT_BOTTOM(target->frame->area);
189 /* horizontal snapping */
190 if (t < tb && b > tt) {
192 case OB_CORNER_TOPLEFT:
193 case OB_CORNER_BOTTOMLEFT:
195 drb = r + *w - c->frame->area.width;
196 if (r < tl && drb >= tl &&
197 drb < tl + config_resist_win)
198 *w = tl - l, snapx = target;
200 case OB_CORNER_TOPRIGHT:
201 case OB_CORNER_BOTTOMRIGHT:
202 dlt = l - *w + c->frame->area.width;
204 if (l > tr && dlt <= tr &&
205 dlt > tr - config_resist_win)
206 *w = r - tr, snapx = target;
213 /* vertical snapping */
214 if (l < tr && r > tl) {
216 case OB_CORNER_TOPLEFT:
217 case OB_CORNER_TOPRIGHT:
219 drb = b + *h - c->frame->area.height;
220 if (b < tt && drb >= tt &&
221 drb < tt + config_resist_win)
222 *h = tt - t, snapy = target;
224 case OB_CORNER_BOTTOMLEFT:
225 case OB_CORNER_BOTTOMRIGHT:
226 dlt = t - *h + c->frame->area.height;
228 if (t > tb && dlt <= tb &&
229 dlt > tb - config_resist_win)
230 *h = b - tb, snapy = target;
236 /* snapped both ways */
237 if (snapx && snapy) break;
242 void resist_size_monitors(ObClient *c, gint *w, gint *h, ObCorner corn)
244 gint l, t, r, b; /* my left, top, right and bottom sides */
245 gint dlt, drb; /* my destination left/top and right/bottom sides */
247 gint al, at, ar, ab; /* screen boundaries */
250 l = RECT_LEFT(c->frame->area);
251 r = RECT_RIGHT(c->frame->area);
252 t = RECT_TOP(c->frame->area);
253 b = RECT_BOTTOM(c->frame->area);
255 incw = c->size_inc.width;
256 inch = c->size_inc.height;
258 /* get the screen boundaries */
259 area = screen_area(c->desktop);
260 al = RECT_LEFT(*area);
261 at = RECT_TOP(*area);
262 ar = RECT_RIGHT(*area);
263 ab = RECT_BOTTOM(*area);
265 if (config_resist_edge) {
266 /* horizontal snapping */
268 case OB_CORNER_TOPLEFT:
269 case OB_CORNER_BOTTOMLEFT:
271 drb = r + *w - c->frame->area.width;
272 if (r <= ar && drb > ar && drb <= ar + config_resist_edge)
275 case OB_CORNER_TOPRIGHT:
276 case OB_CORNER_BOTTOMRIGHT:
277 dlt = l - *w + c->frame->area.width;
279 if (l >= al && dlt < al && dlt >= al - config_resist_edge)
284 /* vertical snapping */
286 case OB_CORNER_TOPLEFT:
287 case OB_CORNER_TOPRIGHT:
289 drb = b + *h - c->frame->area.height;
290 if (b <= ab && drb > ab && drb <= ab + config_resist_edge)
293 case OB_CORNER_BOTTOMLEFT:
294 case OB_CORNER_BOTTOMRIGHT:
295 dlt = t - *h + c->frame->area.height;
297 if (t >= at && dlt < at && dlt >= at - config_resist_edge)