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