1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 resist.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
25 #include "parser/parse.h"
29 void resist_move_windows(ObClient
*c
, gint resist
, gint
*x
, gint
*y
)
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
;
39 frame_client_gravity(c
->frame
, x
, y
, c
->area
.width
, c
->area
.height
);
41 w
= c
->frame
->area
.width
;
42 h
= c
->frame
->area
.height
;
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
);
54 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
56 gint tl
, tt
, tr
, tb
; /* 1 past the target's edges on each side */
58 if (!WINDOW_IS_CLIENT(it
->data
))
62 /* don't snap to self or non-visibles */
63 if (!target
->frame
->visible
|| target
== c
) continue;
65 /* don't snap to windows in layers beneath */
66 if(target
->layer
< c
->layer
&& !config_resist_layers_below
)
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;
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
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
&&
84 *x
= tl
- w
+ 1, snapx
= target
;
86 /* try to corner snap to the window */
87 if (ct
> tt
&& t
<= tt
&&
89 *y
= tt
+ 1, snapy
= target
;
90 else if (cb
< tb
&& b
>= tb
&&
92 *y
= tb
- h
, snapy
= target
;
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
&&
102 *y
= tt
- h
+ 1, snapy
= target
;
104 /* try to corner snap to the window */
105 if (cl
> tl
&& l
<= tl
&&
107 *x
= tl
+ 1, snapx
= target
;
108 else if (cr
< tr
&& r
>= tr
&&
110 *x
= tr
- w
, snapx
= target
;
115 if (snapx
&& snapy
) break;
118 frame_frame_gravity(c
->frame
, x
, y
, c
->area
.width
, c
->area
.height
);
121 void resist_move_monitors(ObClient
*c
, gint resist
, gint
*x
, gint
*y
)
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 */
133 frame_client_gravity(c
->frame
, x
, y
, c
->area
.width
, c
->area
.height
);
135 w
= c
->frame
->area
.width
;
136 h
= c
->frame
->area
.height
;
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
);
148 for (i
= 0; i
< screen_num_monitors
; ++i
) {
149 area
= screen_area_monitor(c
->desktop
, i
);
150 parea
= screen_physical_area_monitor(i
);
152 if (!RECT_INTERSECTS_RECT(*parea
, c
->frame
->area
))
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
);
164 if (cl
>= al
&& l
< al
&& l
>= al
- resist
)
166 else if (cr
<= ar
&& r
> ar
&& r
<= ar
+ resist
)
168 else if (cl
>= pl
&& l
< pl
&& l
>= pl
- resist
)
170 else if (cr
<= pr
&& r
> pr
&& r
<= pr
+ resist
)
173 if (ct
>= at
&& t
< at
&& t
>= at
- resist
)
175 else if (cb
<= ab
&& b
> ab
&& b
< ab
+ resist
)
177 else if (ct
>= pt
&& t
< pt
&& t
>= pt
- resist
)
179 else if (cb
<= pb
&& b
> pb
&& b
< pb
+ resist
)
183 frame_frame_gravity(c
->frame
, x
, y
, c
->area
.width
, c
->area
.height
);
186 void resist_size_windows(ObClient
*c
, gint resist
, gint
*w
, gint
*h
,
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*/
195 ObClient
*snapx
= NULL
, *snapy
= NULL
;
199 incw
= c
->size_inc
.width
;
200 inch
= c
->size_inc
.height
;
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
);
207 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
208 if (!WINDOW_IS_CLIENT(it
->data
))
212 /* don't snap to invisibles or ourself */
213 if (!target
->frame
->visible
|| target
== c
) continue;
215 /* don't snap to windows in layers beneath */
216 if(target
->layer
< c
->layer
&& !config_resist_layers_below
)
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
);
225 /* horizontal snapping */
226 if (t
< tb
&& b
> tt
) {
228 case OB_CORNER_TOPLEFT
:
229 case OB_CORNER_BOTTOMLEFT
:
231 drb
= r
+ *w
- c
->frame
->area
.width
;
232 if (r
< tl
&& drb
>= tl
&&
234 *w
= tl
- l
, snapx
= target
;
236 case OB_CORNER_TOPRIGHT
:
237 case OB_CORNER_BOTTOMRIGHT
:
238 dlt
= l
- *w
+ c
->frame
->area
.width
;
240 if (l
> tr
&& dlt
<= tr
&&
242 *w
= r
- tr
, snapx
= target
;
249 /* vertical snapping */
250 if (l
< tr
&& r
> tl
) {
252 case OB_CORNER_TOPLEFT
:
253 case OB_CORNER_TOPRIGHT
:
255 drb
= b
+ *h
- c
->frame
->area
.height
;
256 if (b
< tt
&& drb
>= tt
&&
258 *h
= tt
- t
, snapy
= target
;
260 case OB_CORNER_BOTTOMLEFT
:
261 case OB_CORNER_BOTTOMRIGHT
:
262 dlt
= t
- *h
+ c
->frame
->area
.height
;
264 if (t
> tb
&& dlt
<= tb
&&
266 *h
= b
- tb
, snapy
= target
;
272 /* snapped both ways */
273 if (snapx
&& snapy
) break;
277 void resist_size_monitors(ObClient
*c
, gint resist
, gint
*w
, gint
*h
,
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 */
283 gint al
, at
, ar
, ab
; /* screen boundaries */
284 gint pl
, pt
, pr
, pb
; /* physical screen boundaries */
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
);
295 incw
= c
->size_inc
.width
;
296 inch
= c
->size_inc
.height
;
298 for (i
= 0; i
< screen_num_monitors
; ++i
) {
299 area
= screen_area_monitor(c
->desktop
, i
);
300 parea
= screen_physical_area_monitor(i
);
302 if (!RECT_INTERSECTS_RECT(*parea
, c
->frame
->area
))
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
);
315 /* horizontal snapping */
317 case OB_CORNER_TOPLEFT
:
318 case OB_CORNER_BOTTOMLEFT
:
320 drb
= r
+ *w
- c
->frame
->area
.width
;
321 if (r
<= ar
&& drb
> ar
&& drb
<= ar
+ resist
)
323 else if (r
<= pr
&& drb
> pr
&& drb
<= pr
+ resist
)
326 case OB_CORNER_TOPRIGHT
:
327 case OB_CORNER_BOTTOMRIGHT
:
328 dlt
= l
- *w
+ c
->frame
->area
.width
;
330 if (l
>= al
&& dlt
< al
&& dlt
>= al
- resist
)
332 else if (l
>= pl
&& dlt
< pl
&& dlt
>= pl
- resist
)
337 /* vertical snapping */
339 case OB_CORNER_TOPLEFT
:
340 case OB_CORNER_TOPRIGHT
:
342 drb
= b
+ *h
- c
->frame
->area
.height
;
343 if (b
<= ab
&& drb
> ab
&& drb
<= ab
+ resist
)
345 else if (b
<= pb
&& drb
> pb
&& drb
<= pb
+ resist
)
348 case OB_CORNER_BOTTOMLEFT
:
349 case OB_CORNER_BOTTOMRIGHT
:
350 dlt
= t
- *h
+ c
->frame
->area
.height
;
352 if (t
>= at
&& dlt
< at
&& dlt
>= at
- resist
)
354 else if (t
>= pt
&& dlt
< pt
&& dlt
>= pt
- resist
)