]> icculus.org git repositories - dana/openbox.git/blob - openbox/resist.c
very cool struts. partial struts actually are partial struts now. possibly way broken...
[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, c->area.width, c->area.height);
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
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;
69
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
73         */
74         if (snapx == NULL) {
75             if (ct < tb && cb > tt) {
76                 if (cl >= tr && l < tr && l >= tr - resist)
77                     *x = tr, snapx = target;
78                 else if (cr <= tl && r > tl &&
79                          r <= tl + resist)
80                     *x = tl - w + 1, snapx = target;
81                 if (snapx != NULL) {
82                     /* try to corner snap to the window */
83                     if (ct > tt && t <= tt &&
84                         t > tt - resist)
85                         *y = tt + 1, snapy = target;
86                     else if (cb < tb && b >= tb &&
87                              b < tb + resist)
88                         *y = tb - h, snapy = target;
89                 }
90             }
91         }
92         if (snapy == NULL) {
93             if (cl < tr && cr > tl) {
94                 if (ct >= tb && t < tb && t >= tb - resist)
95                     *y = tb, snapy = target;
96                 else if (cb <= tt && b > tt &&
97                          b <= tt + resist)
98                     *y = tt - h + 1, snapy = target;
99                 if (snapy != NULL) {
100                     /* try to corner snap to the window */
101                     if (cl > tl && l <= tl &&
102                         l > tl - resist)
103                         *x = tl + 1, snapx = target;
104                     else if (cr < tr && r >= tr &&
105                              r < tr + resist)
106                         *x = tr - w, snapx = target;
107                 }
108             }
109         }
110
111         if (snapx && snapy) break;
112     }
113
114     frame_frame_gravity(c->frame, x, y, c->area.width, c->area.height);
115 }
116
117 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
118 {
119     Rect *area, *parea;
120     guint i;
121     gint l, t, r, b; /* requested edges */
122     gint al, at, ar, ab; /* screen area edges */
123     gint pl, pt, pr, pb; /* physical screen area edges */
124     gint cl, ct, cr, cb; /* current edges */
125     gint w, h; /* current size */
126     Rect desired_area;
127
128     if (!resist) return;
129
130     frame_client_gravity(c->frame, x, y, c->area.width, c->area.height);
131
132     w = c->frame->area.width;
133     h = c->frame->area.height;
134
135     l = *x;
136     t = *y;
137     r = l + w - 1;
138     b = t + h - 1;
139
140     cl = RECT_LEFT(c->frame->area);
141     ct = RECT_TOP(c->frame->area);
142     cr = RECT_RIGHT(c->frame->area);
143     cb = RECT_BOTTOM(c->frame->area);
144
145     RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);
146     
147     for (i = 0; i < screen_num_monitors; ++i) {
148         parea = screen_physical_area_monitor(i);
149
150         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
151             g_free(parea);
152             continue;
153         }
154
155         area = screen_area_monitor(c->desktop, i, &desired_area);
156
157         al = RECT_LEFT(*area);
158         at = RECT_TOP(*area);
159         ar = RECT_RIGHT(*area);
160         ab = RECT_BOTTOM(*area);
161         pl = RECT_LEFT(*parea);
162         pt = RECT_TOP(*parea);
163         pr = RECT_RIGHT(*parea);
164         pb = RECT_BOTTOM(*parea);
165
166         if (cl >= al && l < al && l >= al - resist)
167             *x = al;
168         else if (cr <= ar && r > ar && r <= ar + resist)
169             *x = ar - w + 1;
170         else if (cl >= pl && l < pl && l >= pl - resist)
171             *x = pl;
172         else if (cr <= pr && r > pr && r <= pr + resist)
173             *x = pr - w + 1;
174
175         if (ct >= at && t < at && t >= at - resist)
176             *y = at;
177         else if (cb <= ab && b > ab && b < ab + resist)
178             *y = ab - h + 1;
179         else if (ct >= pt && t < pt && t >= pt - resist)
180             *y = pt;
181         else if (cb <= pb && b > pb && b < pb + resist)
182             *y = pb - h + 1;
183
184         g_free(area);
185         g_free(parea);
186     }
187
188     frame_frame_gravity(c->frame, x, y, c->area.width, c->area.height);
189 }
190
191 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
192                          ObCorner corn)
193 {
194     GList *it;
195     ObClient *target; /* target */
196     gint l, t, r, b; /* my left, top, right and bottom sides */
197     gint dlt, drb; /* my destination left/top and right/bottom sides */
198     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
199     gint incw, inch;
200     ObClient *snapx = NULL, *snapy = NULL;
201
202     if (!resist) return;
203
204     incw = c->size_inc.width;
205     inch = c->size_inc.height;
206
207     l = RECT_LEFT(c->frame->area);
208     r = RECT_RIGHT(c->frame->area);
209     t = RECT_TOP(c->frame->area);
210     b = RECT_BOTTOM(c->frame->area);
211
212     for (it = stacking_list; it; it = g_list_next(it)) {
213         if (!WINDOW_IS_CLIENT(it->data))
214             continue;
215         target = it->data;
216
217         /* don't snap to invisibles or ourself */
218         if (!target->frame->visible || target == c) continue; 
219
220         tl = RECT_LEFT(target->frame->area);
221         tr = RECT_RIGHT(target->frame->area);
222         tt = RECT_TOP(target->frame->area);
223         tb = RECT_BOTTOM(target->frame->area);
224
225         if (snapx == NULL) {
226             /* horizontal snapping */
227             if (t < tb && b > tt) {
228                 switch (corn) {
229                 case OB_CORNER_TOPLEFT:
230                 case OB_CORNER_BOTTOMLEFT:
231                     dlt = l;
232                     drb = r + *w - c->frame->area.width;
233                     if (r < tl && drb >= tl &&
234                         drb < tl + resist)
235                         *w = tl - l, snapx = target;
236                     break;
237                 case OB_CORNER_TOPRIGHT:
238                 case OB_CORNER_BOTTOMRIGHT:
239                     dlt = l - *w + c->frame->area.width;
240                     drb = r;
241                     if (l > tr && dlt <= tr &&
242                         dlt > tr - resist)
243                         *w = r - tr, snapx = target;
244                     break;
245                 }
246             }
247         }
248
249         if (snapy == NULL) {
250             /* vertical snapping */
251             if (l < tr && r > tl) {
252                 switch (corn) {
253                 case OB_CORNER_TOPLEFT:
254                 case OB_CORNER_TOPRIGHT:
255                     dlt = t;
256                     drb = b + *h - c->frame->area.height;
257                     if (b < tt && drb >= tt &&
258                         drb < tt + resist)
259                         *h = tt - t, snapy = target;
260                     break;
261                 case OB_CORNER_BOTTOMLEFT:
262                 case OB_CORNER_BOTTOMRIGHT:
263                     dlt = t - *h + c->frame->area.height;
264                     drb = b;
265                     if (t > tb && dlt <= tb &&
266                         dlt > tb - resist)
267                         *h = b - tb, snapy = target;
268                     break;
269                 }
270             }
271         }
272
273         /* snapped both ways */
274         if (snapx && snapy) break;
275     }
276 }
277
278 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
279                           ObCorner corn)
280 {
281     gint l, t, r, b; /* my left, top, right and bottom sides */
282     gint dlt, drb; /* my destination left/top and right/bottom sides */
283     Rect *area, *parea;
284     gint al, at, ar, ab; /* screen boundaries */ 
285     gint pl, pt, pr, pb; /* physical screen boundaries */
286     gint incw, inch;
287     guint i;
288     Rect desired_area;
289
290     if (!resist) return;
291
292     l = RECT_LEFT(c->frame->area);
293     r = RECT_RIGHT(c->frame->area);
294     t = RECT_TOP(c->frame->area);
295     b = RECT_BOTTOM(c->frame->area);
296
297     incw = c->size_inc.width;
298     inch = c->size_inc.height;
299
300     RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
301
302     for (i = 0; i < screen_num_monitors; ++i) {
303         parea = screen_physical_area_monitor(i);
304
305         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
306             g_free(parea);
307             continue;
308         }
309
310         area = screen_area_monitor(c->desktop, i, &desired_area);
311
312         /* get the screen boundaries */
313         al = RECT_LEFT(*area);
314         at = RECT_TOP(*area);
315         ar = RECT_RIGHT(*area);
316         ab = RECT_BOTTOM(*area);
317         pl = RECT_LEFT(*parea);
318         pt = RECT_TOP(*parea);
319         pr = RECT_RIGHT(*parea);
320         pb = RECT_BOTTOM(*parea);
321
322         /* horizontal snapping */
323         switch (corn) {
324         case OB_CORNER_TOPLEFT:
325         case OB_CORNER_BOTTOMLEFT:
326             dlt = l;
327             drb = r + *w - c->frame->area.width;
328             if (r <= ar && drb > ar && drb <= ar + resist)
329                 *w = ar - l + 1;
330             else if (r <= pr && drb > pr && drb <= pr + resist)
331                 *w = pr - l + 1;
332             break;
333         case OB_CORNER_TOPRIGHT:
334         case OB_CORNER_BOTTOMRIGHT:
335             dlt = l - *w + c->frame->area.width;
336             drb = r;
337             if (l >= al && dlt < al && dlt >= al - resist)
338                 *w = r - al + 1;
339             else if (l >= pl && dlt < pl && dlt >= pl - resist)
340                 *w = r - pl + 1;
341             break;
342         }
343
344         /* vertical snapping */
345         switch (corn) {
346         case OB_CORNER_TOPLEFT:
347         case OB_CORNER_TOPRIGHT:
348             dlt = t;
349             drb = b + *h - c->frame->area.height;
350             if (b <= ab && drb > ab && drb <= ab + resist)
351                 *h = ab - t + 1;
352             else if (b <= pb && drb > pb && drb <= pb + resist)
353                 *h = pb - t + 1;
354             break;
355         case OB_CORNER_BOTTOMLEFT:
356         case OB_CORNER_BOTTOMRIGHT:
357             dlt = t - *h + c->frame->area.height;
358             drb = b;
359             if (t >= at && dlt < at && dlt >= at - resist)
360                 *h = b - at + 1;
361             else if (t >= pt && dlt < pt && dlt >= pt - resist)
362                 *h = b - pt + 1;
363             break;
364         }
365
366         g_free(area);
367         g_free(parea);
368     }
369 }