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 w
= c
->frame
->area
.width
;
40 h
= c
->frame
->area
.height
;
47 cl
= RECT_LEFT(c
->frame
->area
);
48 ct
= RECT_TOP(c
->frame
->area
);
49 cr
= RECT_RIGHT(c
->frame
->area
);
50 cb
= RECT_BOTTOM(c
->frame
->area
);
52 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
54 gint tl
, tt
, tr
, tb
; /* 1 past the target's edges on each side */
56 if (!WINDOW_IS_CLIENT(it
->data
))
60 /* don't snap to self or non-visibles */
61 if (!target
->frame
->visible
|| target
== c
) continue;
63 /* don't snap to windows in layers beneath */
64 if(target
->layer
< c
->layer
&& !config_resist_layers_below
)
67 tl
= RECT_LEFT(target
->frame
->area
) - 1;
68 tt
= RECT_TOP(target
->frame
->area
) - 1;
69 tr
= RECT_RIGHT(target
->frame
->area
) + 1;
70 tb
= RECT_BOTTOM(target
->frame
->area
) + 1;
72 /* snapx and snapy ensure that the window snaps to the top-most
73 window edge available, without going all the way from
74 bottom-to-top in the stacking list
77 if (ct
< tb
&& cb
> tt
) {
78 if (cl
>= tr
&& l
< tr
&& l
>= tr
- resist
)
79 *x
= tr
, snapx
= target
;
80 else if (cr
<= tl
&& r
> tl
&&
82 *x
= tl
- w
+ 1, snapx
= target
;
84 /* try to corner snap to the window */
85 if (ct
> tt
&& t
<= tt
&&
87 *y
= tt
+ 1, snapy
= target
;
88 else if (cb
< tb
&& b
>= tb
&&
90 *y
= tb
- h
, snapy
= target
;
95 if (cl
< tr
&& cr
> tl
) {
96 if (ct
>= tb
&& t
< tb
&& t
>= tb
- resist
)
97 *y
= tb
, snapy
= target
;
98 else if (cb
<= tt
&& b
> tt
&&
100 *y
= tt
- h
+ 1, snapy
= target
;
102 /* try to corner snap to the window */
103 if (cl
> tl
&& l
<= tl
&&
105 *x
= tl
+ 1, snapx
= target
;
106 else if (cr
< tr
&& r
>= tr
&&
108 *x
= tr
- w
, snapx
= target
;
113 if (snapx
&& snapy
) break;
117 void resist_move_monitors(ObClient
*c
, gint resist
, gint
*x
, gint
*y
)
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 */
129 w
= c
->frame
->area
.width
;
130 h
= c
->frame
->area
.height
;
137 cl
= RECT_LEFT(c
->frame
->area
);
138 ct
= RECT_TOP(c
->frame
->area
);
139 cr
= RECT_RIGHT(c
->frame
->area
);
140 cb
= RECT_BOTTOM(c
->frame
->area
);
142 for (i
= 0; i
< screen_num_monitors
; ++i
) {
143 area
= screen_area_monitor(c
->desktop
, i
);
144 parea
= screen_physical_area_monitor(i
);
146 if (!RECT_INTERSECTS_RECT(*parea
, c
->frame
->area
))
149 al
= RECT_LEFT(*area
);
150 at
= RECT_TOP(*area
);
151 ar
= RECT_RIGHT(*area
);
152 ab
= RECT_BOTTOM(*area
);
153 pl
= RECT_LEFT(*parea
);
154 pt
= RECT_TOP(*parea
);
155 pr
= RECT_RIGHT(*parea
);
156 pb
= RECT_BOTTOM(*parea
);
158 if (cl
>= al
&& l
< al
&& l
>= al
- resist
)
160 else if (cr
<= ar
&& r
> ar
&& r
<= ar
+ resist
)
162 else if (cl
>= pl
&& l
< pl
&& l
>= pl
- resist
)
164 else if (cr
<= pr
&& r
> pr
&& r
<= pr
+ resist
)
167 if (ct
>= at
&& t
< at
&& t
>= at
- resist
)
169 else if (cb
<= ab
&& b
> ab
&& b
< ab
+ resist
)
171 else if (ct
>= pt
&& t
< pt
&& t
>= pt
- resist
)
173 else if (cb
<= pb
&& b
> pb
&& b
< pb
+ resist
)
178 void resist_size_windows(ObClient
*c
, gint resist
, gint
*w
, gint
*h
,
182 ObClient
*target
; /* target */
183 gint l
, t
, r
, b
; /* my left, top, right and bottom sides */
184 gint dlt
, drb
; /* my destination left/top and right/bottom sides */
185 gint tl
, tt
, tr
, tb
; /* target's left, top, right and bottom bottom sides*/
187 ObClient
*snapx
= NULL
, *snapy
= NULL
;
191 incw
= c
->size_inc
.width
;
192 inch
= c
->size_inc
.height
;
194 l
= RECT_LEFT(c
->frame
->area
);
195 r
= RECT_RIGHT(c
->frame
->area
);
196 t
= RECT_TOP(c
->frame
->area
);
197 b
= RECT_BOTTOM(c
->frame
->area
);
199 for (it
= stacking_list
; it
; it
= g_list_next(it
)) {
200 if (!WINDOW_IS_CLIENT(it
->data
))
204 /* don't snap to invisibles or ourself */
205 if (!target
->frame
->visible
|| target
== c
) continue;
207 /* don't snap to windows in layers beneath */
208 if(target
->layer
< c
->layer
&& !config_resist_layers_below
)
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
);
217 /* horizontal snapping */
218 if (t
< tb
&& b
> tt
) {
220 case OB_CORNER_TOPLEFT
:
221 case OB_CORNER_BOTTOMLEFT
:
223 drb
= r
+ *w
- c
->frame
->area
.width
;
224 if (r
< tl
&& drb
>= tl
&&
226 *w
= tl
- l
, snapx
= target
;
228 case OB_CORNER_TOPRIGHT
:
229 case OB_CORNER_BOTTOMRIGHT
:
230 dlt
= l
- *w
+ c
->frame
->area
.width
;
232 if (l
> tr
&& dlt
<= tr
&&
234 *w
= r
- tr
, snapx
= target
;
241 /* vertical snapping */
242 if (l
< tr
&& r
> tl
) {
244 case OB_CORNER_TOPLEFT
:
245 case OB_CORNER_TOPRIGHT
:
247 drb
= b
+ *h
- c
->frame
->area
.height
;
248 if (b
< tt
&& drb
>= tt
&&
250 *h
= tt
- t
, snapy
= target
;
252 case OB_CORNER_BOTTOMLEFT
:
253 case OB_CORNER_BOTTOMRIGHT
:
254 dlt
= t
- *h
+ c
->frame
->area
.height
;
256 if (t
> tb
&& dlt
<= tb
&&
258 *h
= b
- tb
, snapy
= target
;
264 /* snapped both ways */
265 if (snapx
&& snapy
) break;
269 void resist_size_monitors(ObClient
*c
, gint resist
, gint
*w
, gint
*h
,
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 */
275 gint al
, at
, ar
, ab
; /* screen boundaries */
276 gint pl
, pt
, pr
, pb
; /* physical screen boundaries */
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
);
287 incw
= c
->size_inc
.width
;
288 inch
= c
->size_inc
.height
;
290 for (i
= 0; i
< screen_num_monitors
; ++i
) {
291 area
= screen_area_monitor(c
->desktop
, i
);
292 parea
= screen_physical_area_monitor(i
);
294 if (!RECT_INTERSECTS_RECT(*parea
, c
->frame
->area
))
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
);
307 /* horizontal snapping */
309 case OB_CORNER_TOPLEFT
:
310 case OB_CORNER_BOTTOMLEFT
:
312 drb
= r
+ *w
- c
->frame
->area
.width
;
313 if (r
<= ar
&& drb
> ar
&& drb
<= ar
+ resist
)
315 else if (r
<= pr
&& drb
> pr
&& drb
<= pr
+ resist
)
318 case OB_CORNER_TOPRIGHT
:
319 case OB_CORNER_BOTTOMRIGHT
:
320 dlt
= l
- *w
+ c
->frame
->area
.width
;
322 if (l
>= al
&& dlt
< al
&& dlt
>= al
- resist
)
324 else if (l
>= pl
&& dlt
< pl
&& dlt
>= pl
- resist
)
329 /* vertical snapping */
331 case OB_CORNER_TOPLEFT
:
332 case OB_CORNER_TOPRIGHT
:
334 drb
= b
+ *h
- c
->frame
->area
.height
;
335 if (b
<= ab
&& drb
> ab
&& drb
<= ab
+ resist
)
337 else if (b
<= pb
&& drb
> pb
&& drb
<= pb
+ resist
)
340 case OB_CORNER_BOTTOMLEFT
:
341 case OB_CORNER_BOTTOMRIGHT
:
342 dlt
= t
- *h
+ c
->frame
->area
.height
;
344 if (t
>= at
&& dlt
< at
&& dlt
>= at
- resist
)
346 else if (t
>= pt
&& dlt
< pt
&& dlt
>= pt
- resist
)