]> icculus.org git repositories - dana/openbox.git/blob - openbox/resist.c
let you resize aspect ratio windows in north and south directions, and make them...
[dana/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                 case OB_DIRECTION_NORTH:
238                 case OB_DIRECTION_SOUTH:
239                     dlt = l;
240                     drb = r + *w - c->frame->area.width;
241                     if (r < tl && drb >= tl &&
242                         drb < tl + resist)
243                         *w = tl - l, snapx = target;
244                     break;
245                 case OB_DIRECTION_WEST:
246                 case OB_DIRECTION_NORTHWEST:
247                 case OB_DIRECTION_SOUTHWEST:
248                     dlt = l - *w + c->frame->area.width;
249                     drb = r;
250                     if (l > tr && dlt <= tr &&
251                         dlt > tr - resist)
252                         *w = r - tr, snapx = target;
253                     break;
254                 }
255             }
256         }
257
258         if (snapy == NULL) {
259             /* vertical snapping */
260             if (l < tr && r > tl) {
261                 switch (dir) {
262                 case OB_DIRECTION_SOUTH:
263                 case OB_DIRECTION_SOUTHWEST:
264                 case OB_DIRECTION_SOUTHEAST:
265                 case OB_DIRECTION_EAST:
266                 case OB_DIRECTION_WEST:
267                     dlt = t;
268                     drb = b + *h - c->frame->area.height;
269                     if (b < tt && drb >= tt &&
270                         drb < tt + resist)
271                         *h = tt - t, snapy = target;
272                     break;
273                 case OB_DIRECTION_NORTH:
274                 case OB_DIRECTION_NORTHWEST:
275                 case OB_DIRECTION_NORTHEAST:
276                     dlt = t - *h + c->frame->area.height;
277                     drb = b;
278                     if (t > tb && dlt <= tb &&
279                         dlt > tb - resist)
280                         *h = b - tb, snapy = target;
281                     break;
282                 }
283             }
284         }
285
286         /* snapped both ways */
287         if (snapx && snapy) break;
288     }
289 }
290
291 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
292                           ObDirection dir)
293 {
294     gint l, t, r, b; /* my left, top, right and bottom sides */
295     gint dlt, drb; /* my destination left/top and right/bottom sides */
296     Rect *area, *parea;
297     gint al, at, ar, ab; /* screen boundaries */
298     gint pl, pt, pr, pb; /* physical screen boundaries */
299     gint incw, inch;
300     guint i;
301     Rect desired_area;
302
303     if (!resist) return;
304
305     l = RECT_LEFT(c->frame->area);
306     r = RECT_RIGHT(c->frame->area);
307     t = RECT_TOP(c->frame->area);
308     b = RECT_BOTTOM(c->frame->area);
309
310     incw = c->size_inc.width;
311     inch = c->size_inc.height;
312
313     RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
314
315     for (i = 0; i < screen_num_monitors; ++i) {
316         parea = screen_physical_area_monitor(i);
317
318         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
319             g_free(parea);
320             continue;
321         }
322
323         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
324                            &desired_area);
325
326         /* get the screen boundaries */
327         al = RECT_LEFT(*area);
328         at = RECT_TOP(*area);
329         ar = RECT_RIGHT(*area);
330         ab = RECT_BOTTOM(*area);
331         pl = RECT_LEFT(*parea);
332         pt = RECT_TOP(*parea);
333         pr = RECT_RIGHT(*parea);
334         pb = RECT_BOTTOM(*parea);
335
336         /* horizontal snapping */
337         switch (dir) {
338         case OB_DIRECTION_EAST:
339         case OB_DIRECTION_NORTHEAST:
340         case OB_DIRECTION_SOUTHEAST:
341         case OB_DIRECTION_NORTH:
342         case OB_DIRECTION_SOUTH:
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         }
361
362         /* vertical snapping */
363         switch (dir) {
364         case OB_DIRECTION_SOUTH:
365         case OB_DIRECTION_SOUTHWEST:
366         case OB_DIRECTION_SOUTHEAST:
367         case OB_DIRECTION_WEST:
368         case OB_DIRECTION_EAST:
369             dlt = t;
370             drb = b + *h - c->frame->area.height;
371             if (b <= ab && drb > ab && drb <= ab + resist)
372                 *h = ab - t + 1;
373             else if (b <= pb && drb > pb && drb <= pb + resist)
374                 *h = pb - t + 1;
375             break;
376         case OB_DIRECTION_NORTH:
377         case OB_DIRECTION_NORTHWEST:
378         case OB_DIRECTION_NORTHEAST:
379             dlt = t - *h + c->frame->area.height;
380             drb = b;
381             if (t >= at && dlt < at && dlt >= at - resist)
382                 *h = b - at + 1;
383             else if (t >= pt && dlt < pt && dlt >= pt - resist)
384                 *h = b - pt + 1;
385             break;
386         }
387
388         g_free(area);
389         g_free(parea);
390     }
391 }