]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/resist.c
set the resize cursors only when the window can be resized
[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         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
127     if (!resist) return;
128
129     frame_client_gravity(c->frame, x, y, c->area.width, c->area.height);
130
131     w = c->frame->area.width;
132     h = c->frame->area.height;
133
134     l = *x;
135     t = *y;
136     r = l + w - 1;
137     b = t + h - 1;
138
139     cl = RECT_LEFT(c->frame->area);
140     ct = RECT_TOP(c->frame->area);
141     cr = RECT_RIGHT(c->frame->area);
142     cb = RECT_BOTTOM(c->frame->area);
143     
144     for (i = 0; i < screen_num_monitors; ++i) {
145         area = screen_area_monitor(c->desktop, i);
146         parea = screen_physical_area_monitor(i);
147
148         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
149             continue;
150
151         al = RECT_LEFT(*area);
152         at = RECT_TOP(*area);
153         ar = RECT_RIGHT(*area);
154         ab = RECT_BOTTOM(*area);
155         pl = RECT_LEFT(*parea);
156         pt = RECT_TOP(*parea);
157         pr = RECT_RIGHT(*parea);
158         pb = RECT_BOTTOM(*parea);
159
160         if (cl >= al && l < al && l >= al - resist)
161             *x = al;
162         else if (cr <= ar && r > ar && r <= ar + resist)
163             *x = ar - w + 1;
164         else if (cl >= pl && l < pl && l >= pl - resist)
165             *x = pl;
166         else if (cr <= pr && r > pr && r <= pr + resist)
167             *x = pr - w + 1;
168
169         if (ct >= at && t < at && t >= at - resist)
170             *y = at;
171         else if (cb <= ab && b > ab && b < ab + resist)
172             *y = ab - h + 1;
173         else if (ct >= pt && t < pt && t >= pt - resist)
174             *y = pt;
175         else if (cb <= pb && b > pb && b < pb + resist)
176             *y = pb - h + 1;
177     }
178
179     frame_frame_gravity(c->frame, x, y, c->area.width, c->area.height);
180 }
181
182 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
183                          ObCorner corn)
184 {
185     GList *it;
186     ObClient *target; /* target */
187     gint l, t, r, b; /* my left, top, right and bottom sides */
188     gint dlt, drb; /* my destination left/top and right/bottom sides */
189     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
190     gint incw, inch;
191     ObClient *snapx = NULL, *snapy = NULL;
192
193     if (!resist) return;
194
195     incw = c->size_inc.width;
196     inch = c->size_inc.height;
197
198     l = RECT_LEFT(c->frame->area);
199     r = RECT_RIGHT(c->frame->area);
200     t = RECT_TOP(c->frame->area);
201     b = RECT_BOTTOM(c->frame->area);
202
203     for (it = stacking_list; it; it = g_list_next(it)) {
204         if (!WINDOW_IS_CLIENT(it->data))
205             continue;
206         target = it->data;
207
208         /* don't snap to invisibles or ourself */
209         if (!target->frame->visible || target == c) continue; 
210
211         tl = RECT_LEFT(target->frame->area);
212         tr = RECT_RIGHT(target->frame->area);
213         tt = RECT_TOP(target->frame->area);
214         tb = RECT_BOTTOM(target->frame->area);
215
216         if (snapx == NULL) {
217             /* horizontal snapping */
218             if (t < tb && b > tt) {
219                 switch (corn) {
220                 case OB_CORNER_TOPLEFT:
221                 case OB_CORNER_BOTTOMLEFT:
222                     dlt = l;
223                     drb = r + *w - c->frame->area.width;
224                     if (r < tl && drb >= tl &&
225                         drb < tl + resist)
226                         *w = tl - l, snapx = target;
227                     break;
228                 case OB_CORNER_TOPRIGHT:
229                 case OB_CORNER_BOTTOMRIGHT:
230                     dlt = l - *w + c->frame->area.width;
231                     drb = r;
232                     if (l > tr && dlt <= tr &&
233                         dlt > tr - resist)
234                         *w = r - tr, snapx = target;
235                     break;
236                 }
237             }
238         }
239
240         if (snapy == NULL) {
241             /* vertical snapping */
242             if (l < tr && r > tl) {
243                 switch (corn) {
244                 case OB_CORNER_TOPLEFT:
245                 case OB_CORNER_TOPRIGHT:
246                     dlt = t;
247                     drb = b + *h - c->frame->area.height;
248                     if (b < tt && drb >= tt &&
249                         drb < tt + resist)
250                         *h = tt - t, snapy = target;
251                     break;
252                 case OB_CORNER_BOTTOMLEFT:
253                 case OB_CORNER_BOTTOMRIGHT:
254                     dlt = t - *h + c->frame->area.height;
255                     drb = b;
256                     if (t > tb && dlt <= tb &&
257                         dlt > tb - resist)
258                         *h = b - tb, snapy = target;
259                     break;
260                 }
261             }
262         }
263
264         /* snapped both ways */
265         if (snapx && snapy) break;
266     }
267 }
268
269 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
270                           ObCorner corn)
271 {
272     gint l, t, r, b; /* my left, top, right and bottom sides */
273     gint dlt, drb; /* my destination left/top and right/bottom sides */
274     Rect *area, *parea;
275     gint al, at, ar, ab; /* screen boundaries */ 
276     gint pl, pt, pr, pb; /* physical screen boundaries */
277     gint incw, inch;
278     guint i;
279
280     if (!resist) return;
281
282     l = RECT_LEFT(c->frame->area);
283     r = RECT_RIGHT(c->frame->area);
284     t = RECT_TOP(c->frame->area);
285     b = RECT_BOTTOM(c->frame->area);
286
287     incw = c->size_inc.width;
288     inch = c->size_inc.height;
289
290     for (i = 0; i < screen_num_monitors; ++i) {
291         area = screen_area_monitor(c->desktop, i);
292         parea = screen_physical_area_monitor(i);
293
294         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
295             continue;
296
297         /* get the screen boundaries */
298         al = RECT_LEFT(*area);
299         at = RECT_TOP(*area);
300         ar = RECT_RIGHT(*area);
301         ab = RECT_BOTTOM(*area);
302         pl = RECT_LEFT(*parea);
303         pt = RECT_TOP(*parea);
304         pr = RECT_RIGHT(*parea);
305         pb = RECT_BOTTOM(*parea);
306
307         /* horizontal snapping */
308         switch (corn) {
309         case OB_CORNER_TOPLEFT:
310         case OB_CORNER_BOTTOMLEFT:
311             dlt = l;
312             drb = r + *w - c->frame->area.width;
313             if (r <= ar && drb > ar && drb <= ar + resist)
314                 *w = ar - l + 1;
315             else if (r <= pr && drb > pr && drb <= pr + resist)
316                 *w = pr - l + 1;
317             break;
318         case OB_CORNER_TOPRIGHT:
319         case OB_CORNER_BOTTOMRIGHT:
320             dlt = l - *w + c->frame->area.width;
321             drb = r;
322             if (l >= al && dlt < al && dlt >= al - resist)
323                 *w = r - al + 1;
324             else if (l >= pl && dlt < pl && dlt >= pl - resist)
325                 *w = r - pl + 1;
326             break;
327         }
328
329         /* vertical snapping */
330         switch (corn) {
331         case OB_CORNER_TOPLEFT:
332         case OB_CORNER_TOPRIGHT:
333             dlt = t;
334             drb = b + *h - c->frame->area.height;
335             if (b <= ab && drb > ab && drb <= ab + resist)
336                 *h = ab - t + 1;
337             else if (b <= pb && drb > pb && drb <= pb + resist)
338                 *h = pb - t + 1;
339             break;
340         case OB_CORNER_BOTTOMLEFT:
341         case OB_CORNER_BOTTOMRIGHT:
342             dlt = t - *h + c->frame->area.height;
343             drb = b;
344             if (t >= at && dlt < at && dlt >= at - resist)
345                 *h = b - at + 1;
346             else if (t >= pt && dlt < pt && dlt >= pt - resist)
347                 *h = b - pt + 1;
348             break;
349         }
350     }
351 }