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