42c03675c7ca722f995c99b840eb79662e9db75b
[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) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "client.h"
20 #include "frame.h"
21 #include "stacking.h"
22 #include "screen.h"
23 #include "config.h"
24 #include "parser/parse.h"
25
26 #include <glib.h>
27
28 void resist_move_windows(ObClient *c, gint *x, gint *y)
29 {
30     GList *it;
31     gint l, t, r, b; /* requested edges */
32     gint cl, ct, cr, cb; /* current edges */
33     gint w, h; /* current size */
34     ObClient *snapx = NULL, *snapy = NULL;
35
36     w = c->frame->area.width;
37     h = c->frame->area.height;
38
39     l = *x;
40     t = *y;
41     r = l + w - 1;
42     b = t + h - 1;
43
44     cl = RECT_LEFT(c->frame->area);
45     ct = RECT_TOP(c->frame->area);
46     cr = RECT_RIGHT(c->frame->area);
47     cb = RECT_BOTTOM(c->frame->area);
48     
49     if (config_resist_win)
50         for (it = stacking_list; it; it = g_list_next(it)) {
51             ObClient *target;
52             gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
53
54             if (!WINDOW_IS_CLIENT(it->data))
55                 continue;
56             target = it->data;
57
58             /* don't snap to self or non-visibles */
59             if (!target->frame->visible || target == c) continue; 
60
61             /* don't snap to windows in layers beneath */
62             if(target->layer < c->layer && !config_resist_layers_below)
63                 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 - config_resist_win)
77                         *x = tr, snapx = target;
78                     else if (cr <= tl && r > tl &&
79                              r <= tl + config_resist_win)
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 - config_resist_win)
85                             *y = tt + 1, snapy = target;
86                         else if (cb < tb && b >= tb &&
87                                  b < tb + config_resist_win)
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 - config_resist_win)
95                         *y = tb, snapy = target;
96                     else if (cb <= tt && b > tt &&
97                              b <= tt + config_resist_win)
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 - config_resist_win)
103                             *x = tl + 1, snapx = target;
104                         else if (cr < tr && r >= tr &&
105                                  r < tr + config_resist_win)
106                             *x = tr - w, snapx = target;
107                     }
108                 }
109             }
110
111             if (snapx && snapy) break;
112         }
113 }
114
115 void resist_move_monitors(ObClient *c, gint *x, gint *y)
116 {
117     Rect *area, *parea;
118     guint i;
119     gint l, t, r, b; /* requested edges */
120     gint al, at, ar, ab; /* screen area edges */
121     gint pl, pt, pr, pb; /* physical screen area edges */
122     gint cl, ct, cr, cb; /* current edges */
123     gint w, h; /* current size */
124
125     w = c->frame->area.width;
126     h = c->frame->area.height;
127
128     l = *x;
129     t = *y;
130     r = l + w - 1;
131     b = t + h - 1;
132
133     cl = RECT_LEFT(c->frame->area);
134     ct = RECT_TOP(c->frame->area);
135     cr = RECT_RIGHT(c->frame->area);
136     cb = RECT_BOTTOM(c->frame->area);
137     
138     if (config_resist_edge) {
139         for (i = 0; i < screen_num_monitors; ++i) {
140             area = screen_area_monitor(c->desktop, i);
141             parea = screen_physical_area_monitor(i);
142
143             if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
144                 continue;
145
146             al = RECT_LEFT(*area);
147             at = RECT_TOP(*area);
148             ar = RECT_RIGHT(*area);
149             ab = RECT_BOTTOM(*area);
150             pl = RECT_LEFT(*parea);
151             pt = RECT_TOP(*parea);
152             pr = RECT_RIGHT(*parea);
153             pb = RECT_BOTTOM(*parea);
154
155             if (cl >= al && l < al && l >= al - config_resist_edge)
156                 *x = al;
157             else if (cr <= ar && r > ar && r <= ar + config_resist_edge)
158                 *x = ar - w + 1;
159             else if (cl >= pl && l < pl && l >= pl - config_resist_edge)
160                 *x = pl;
161             else if (cr <= pr && r > pr && r <= pr + config_resist_edge)
162                 *x = pr - w + 1;
163
164             if (ct >= at && t < at && t >= at - config_resist_edge)
165                 *y = at;
166             else if (cb <= ab && b > ab && b < ab + config_resist_edge)
167                 *y = ab - h + 1;
168             else if (ct >= pt && t < pt && t >= pt - config_resist_edge)
169                 *y = pt;
170             else if (cb <= pb && b > pb && b < pb + config_resist_edge)
171                 *y = pb - h + 1;
172         }
173     }
174 }
175
176 void resist_size_windows(ObClient *c, gint *w, gint *h, ObCorner corn)
177 {
178     GList *it;
179     ObClient *target; /* target */
180     gint l, t, r, b; /* my left, top, right and bottom sides */
181     gint dlt, drb; /* my destination left/top and right/bottom sides */
182     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
183     gint incw, inch;
184     ObClient *snapx = NULL, *snapy = NULL;
185
186     incw = c->size_inc.width;
187     inch = c->size_inc.height;
188
189     l = RECT_LEFT(c->frame->area);
190     r = RECT_RIGHT(c->frame->area);
191     t = RECT_TOP(c->frame->area);
192     b = RECT_BOTTOM(c->frame->area);
193
194     if (config_resist_win) {
195         for (it = stacking_list; it; it = g_list_next(it)) {
196             if (!WINDOW_IS_CLIENT(it->data))
197                 continue;
198             target = it->data;
199
200             /* don't snap to invisibles or ourself */
201             if (!target->frame->visible || target == c) continue;
202
203             /* don't snap to windows in layers beneath */
204             if(target->layer < c->layer && !config_resist_layers_below)
205                 continue;
206
207             tl = RECT_LEFT(target->frame->area);
208             tr = RECT_RIGHT(target->frame->area);
209             tt = RECT_TOP(target->frame->area);
210             tb = RECT_BOTTOM(target->frame->area);
211
212             if (snapx == NULL) {
213                 /* horizontal snapping */
214                 if (t < tb && b > tt) {
215                     switch (corn) {
216                     case OB_CORNER_TOPLEFT:
217                     case OB_CORNER_BOTTOMLEFT:
218                         dlt = l;
219                         drb = r + *w - c->frame->area.width;
220                         if (r < tl && drb >= tl &&
221                             drb < tl + config_resist_win)
222                             *w = tl - l, snapx = target;
223                         break;
224                     case OB_CORNER_TOPRIGHT:
225                     case OB_CORNER_BOTTOMRIGHT:
226                         dlt = l - *w + c->frame->area.width;
227                         drb = r;
228                         if (l > tr && dlt <= tr &&
229                             dlt > tr - config_resist_win)
230                             *w = r - tr, snapx = target;
231                         break;
232                     }
233                 }
234             }
235
236             if (snapy == NULL) {
237                 /* vertical snapping */
238                 if (l < tr && r > tl) {
239                     switch (corn) {
240                     case OB_CORNER_TOPLEFT:
241                     case OB_CORNER_TOPRIGHT:
242                         dlt = t;
243                         drb = b + *h - c->frame->area.height;
244                         if (b < tt && drb >= tt &&
245                             drb < tt + config_resist_win)
246                             *h = tt - t, snapy = target;
247                         break;
248                     case OB_CORNER_BOTTOMLEFT:
249                     case OB_CORNER_BOTTOMRIGHT:
250                         dlt = t - *h + c->frame->area.height;
251                         drb = b;
252                         if (t > tb && dlt <= tb &&
253                             dlt > tb - config_resist_win)
254                             *h = b - tb, snapy = target;
255                         break;
256                     }
257                 }
258             }
259
260             /* snapped both ways */
261             if (snapx && snapy) break;
262         }
263     }
264 }
265
266 void resist_size_monitors(ObClient *c, gint *w, gint *h, ObCorner corn)
267 {
268     gint l, t, r, b; /* my left, top, right and bottom sides */
269     gint dlt, drb; /* my destination left/top and right/bottom sides */
270     Rect *area, *parea;
271     gint al, at, ar, ab; /* screen boundaries */ 
272     gint pl, pt, pr, pb; /* physical screen boundaries */
273     gint incw, inch;
274     guint i;
275
276     l = RECT_LEFT(c->frame->area);
277     r = RECT_RIGHT(c->frame->area);
278     t = RECT_TOP(c->frame->area);
279     b = RECT_BOTTOM(c->frame->area);
280
281     incw = c->size_inc.width;
282     inch = c->size_inc.height;
283
284     for (i = 0; i < screen_num_monitors; ++i) {
285         area = screen_area_monitor(c->desktop, i);
286         parea = screen_physical_area_monitor(i);
287
288         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
289             continue;
290
291         /* get the screen boundaries */
292         al = RECT_LEFT(*area);
293         at = RECT_TOP(*area);
294         ar = RECT_RIGHT(*area);
295         ab = RECT_BOTTOM(*area);
296         pl = RECT_LEFT(*parea);
297         pt = RECT_TOP(*parea);
298         pr = RECT_RIGHT(*parea);
299         pb = RECT_BOTTOM(*parea);
300
301         if (config_resist_edge) {
302             /* horizontal snapping */
303             switch (corn) {
304             case OB_CORNER_TOPLEFT:
305             case OB_CORNER_BOTTOMLEFT:
306                 dlt = l;
307                 drb = r + *w - c->frame->area.width;
308                 if (r <= ar && drb > ar && drb <= ar + config_resist_edge)
309                     *w = ar - l + 1;
310                 else if (r <= pr && drb > pr && drb <= pr + config_resist_edge)
311                     *w = pr - l + 1;
312                 break;
313             case OB_CORNER_TOPRIGHT:
314             case OB_CORNER_BOTTOMRIGHT:
315                 dlt = l - *w + c->frame->area.width;
316                 drb = r;
317                 if (l >= al && dlt < al && dlt >= al - config_resist_edge)
318                     *w = r - al + 1;
319                 else if (l >= pl && dlt < pl && dlt >= pl - config_resist_edge)
320                     *w = r - pl + 1;
321                 break;
322             }
323
324             /* vertical snapping */
325             switch (corn) {
326             case OB_CORNER_TOPLEFT:
327             case OB_CORNER_TOPRIGHT:
328                 dlt = t;
329                 drb = b + *h - c->frame->area.height;
330                 if (b <= ab && drb > ab && drb <= ab + config_resist_edge)
331                     *h = ab - t + 1;
332                 else if (b <= pb && drb > pb && drb <= pb + config_resist_edge)
333                     *h = pb - t + 1;
334                 break;
335             case OB_CORNER_BOTTOMLEFT:
336             case OB_CORNER_BOTTOMRIGHT:
337                 dlt = t - *h + c->frame->area.height;
338                 drb = b;
339                 if (t >= at && dlt < at && dlt >= at - config_resist_edge)
340                     *h = b - at + 1;
341                 else if (t >= pt && dlt < pt && dlt >= pt - config_resist_edge)
342                     *h = b - pt + 1;
343                 break;
344             }
345         }
346     }
347 }