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