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