1 /* placement.c - window and icon placement on screen
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "WindowMaker.h"
37 #include "application.h"
40 #include "placement.h"
43 #define X_ORIGIN WMAX(usableArea.x1,\
44 wPreferences.window_place_origin.x)
46 #define Y_ORIGIN WMAX(usableArea.y1,\
47 wPreferences.window_place_origin.y)
49 /* Returns True if it is an icon and is in this workspace */
51 iconPosition(WCoreWindow
*wcore
, int sx1
, int sy1
, int sx2
, int sy2
,
52 int workspace
, int *retX
, int *retY
)
57 parent
= wcore
->descriptor
.parent
;
59 /* if it is an application icon */
60 if (wcore
->descriptor
.parent_type
== WCLASS_APPICON
&& !((WAppIcon
*) parent
)->docked
) {
61 *retX
= ((WAppIcon
*) parent
)->x_pos
;
62 *retY
= ((WAppIcon
*) parent
)->y_pos
;
65 } else if (wcore
->descriptor
.parent_type
== WCLASS_MINIWINDOW
&&
66 (((WIcon
*) parent
)->owner
->frame
->workspace
== workspace
67 || IS_OMNIPRESENT(((WIcon
*) parent
)->owner
)
68 || wPreferences
.sticky_icons
)
69 && ((WIcon
*) parent
)->mapped
) {
71 *retX
= ((WIcon
*) parent
)->owner
->icon_x
;
72 *retY
= ((WIcon
*) parent
)->owner
->icon_y
;
75 } else if (wcore
->descriptor
.parent_type
== WCLASS_WINDOW
76 && ((WWindow
*) parent
)->flags
.icon_moved
77 && (((WWindow
*) parent
)->frame
->workspace
== workspace
|| IS_OMNIPRESENT((WWindow
*) parent
)
78 || wPreferences
.sticky_icons
)) {
79 *retX
= ((WWindow
*) parent
)->icon_x
;
80 *retY
= ((WWindow
*) parent
)->icon_y
;
85 /* Check if it is inside the screen */
87 if (*retX
< sx1
- wPreferences
.icon_size
)
92 if (*retY
< sy1
- wPreferences
.icon_size
)
101 void PlaceIcon(WScreen
*scr
, int *x_ret
, int *y_ret
, int head
)
103 int pf
; /* primary axis */
104 int sf
; /* secondary axis */
110 int sx1
, sx2
, sy1
, sy2
; /* screen boundary */
115 int isize
= wPreferences
.icon_size
;
118 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
120 /* Find out screen boundaries. */
122 /* Allows each head to have miniwindows */
130 sw
= isize
* (sw
/ isize
);
131 sh
= isize
* (sh
/ isize
);
132 fullW
= (sx2
- sx1
) / isize
;
133 fullH
= (sy2
- sy1
) / isize
;
135 /* icon yard boundaries */
136 if (wPreferences
.icon_yard
& IY_VERT
) {
143 if (wPreferences
.icon_yard
& IY_RIGHT
) {
150 if (wPreferences
.icon_yard
& IY_TOP
) {
159 * Create a map with the occupied slots. 1 means the slot is used
160 * or at least partially used.
161 * The slot usage can be optimized by only marking fully used slots
162 * or slots that have most of it covered.
163 * Space usage is worse than the fvwm algorithm (used in the old version)
164 * but complexity is much better (faster) than it.
166 map
= wmalloc((sw
+ 2) * (sh
+ 2));
168 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
170 WM_ETARETI_BAG(scr
->stacking_list
, obj
, iter
) {
175 if (iconPosition(obj
, sx1
, sy1
, sx2
, sy2
, w_global
.workspace
.current
, &x
, &y
)) {
176 int xdi
, ydi
; /* rounded down */
177 int xui
, yui
; /* rounded up */
181 xui
= (x
+ isize
/ 2) / isize
;
182 yui
= (y
+ isize
/ 2) / isize
;
183 map
[INDEX(xdi
, ydi
)] = 1;
184 map
[INDEX(xdi
, yui
)] = 1;
185 map
[INDEX(xui
, ydi
)] = 1;
186 map
[INDEX(xui
, yui
)] = 1;
188 obj
= obj
->stacking
->under
;
191 /* Default position */
195 /* Look for an empty slot */
196 for (si
= 0; si
< sf
; si
++) {
197 for (pi
= 0; pi
< pf
; pi
++) {
198 if (wPreferences
.icon_yard
& IY_VERT
) {
199 x
= xo
+ xs
* (si
* isize
);
200 y
= yo
+ ys
* (pi
* isize
);
202 x
= xo
+ xs
* (pi
* isize
);
203 y
= yo
+ ys
* (si
* isize
);
205 if (!map
[INDEX(x
/ isize
, y
/ isize
)]) {
219 /* Computes the intersecting length of two line sections */
220 int calcIntersectionLength(int p1
, int l1
, int p2
, int l2
)
236 else if (p2
+ l2
< p1
+ l1
)
239 isect
= p1
+ l1
- p2
;
244 /* Computes the intersecting area of two rectangles */
245 int calcIntersectionArea(int x1
, int y1
, int w1
, int h1
, int x2
, int y2
, int w2
, int h2
)
247 return calcIntersectionLength(x1
, w1
, x2
, w2
)
248 * calcIntersectionLength(y1
, h1
, y2
, h2
);
251 static int calcSumOfCoveredAreas(WWindow
*wwin
, int x
, int y
, int w
, int h
)
254 WWindow
*test_window
;
257 test_window
= wwin
->screen_ptr
->focused_window
;
258 for (; test_window
!= NULL
&& test_window
->prev
!= NULL
;)
259 test_window
= test_window
->prev
;
261 for (; test_window
!= NULL
; test_window
= test_window
->next
) {
262 if (test_window
->frame
->core
->stacking
->window_level
< WMNormalLevel
) {
266 tw
= test_window
->frame
->core
->width
;
267 th
= test_window
->frame
->core
->height
;
268 tx
= test_window
->frame_x
;
269 ty
= test_window
->frame_y
;
271 if (test_window
->flags
.mapped
|| (test_window
->flags
.shaded
&&
272 test_window
->frame
->workspace
== w_global
.workspace
.current
&&
273 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
))) {
274 sum_isect
+= calcIntersectionArea(tx
, ty
, tw
, th
, x
, y
, w
, h
);
281 static void set_width_height(WWindow
*wwin
, unsigned int *width
, unsigned int *height
)
284 *height
+= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
;
286 if (HAS_TITLEBAR(wwin
))
287 *height
+= TITLEBAR_HEIGHT
;
288 if (HAS_RESIZEBAR(wwin
))
289 *height
+= RESIZEBAR_HEIGHT
;
291 if (HAS_BORDER(wwin
)) {
292 *height
+= 2 * wwin
->screen_ptr
->frame_border_width
;
293 *width
+= 2 * wwin
->screen_ptr
->frame_border_width
;
298 window_overlaps(WWindow
*win
, int x
, int y
, int w
, int h
, Bool ignore_sunken
)
303 win
->frame
->core
->stacking
->window_level
< WMNormalLevel
) {
307 tw
= win
->frame
->core
->width
;
308 th
= win
->frame
->core
->height
;
312 if ((tx
< (x
+ w
)) && ((tx
+ tw
) > x
) &&
313 (ty
< (y
+ h
)) && ((ty
+ th
) > y
) &&
314 (win
->flags
.mapped
||
315 (win
->flags
.shaded
&&
316 win
->frame
->workspace
== w_global
.workspace
.current
&&
317 !(win
->flags
.miniaturized
|| win
->flags
.hidden
)))) {
325 screen_has_space(WScreen
*scr
, int x
, int y
, int w
, int h
, Bool ignore_sunken
)
327 WWindow
*focused
= scr
->focused_window
, *i
;
329 for (i
= focused
; i
; i
= i
->next
) {
330 if (window_overlaps(i
, x
, y
, w
, h
, ignore_sunken
)) {
335 for (i
= focused
; i
; i
= i
->prev
) {
336 if (window_overlaps(i
, x
, y
, w
, h
, ignore_sunken
)) {
345 smartPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
, unsigned int width
,
346 unsigned int height
, WArea usableArea
)
348 int test_x
= 0, test_y
= Y_ORIGIN
;
349 int from_x
, to_x
, from_y
, to_y
;
351 int min_isect
, min_isect_x
, min_isect_y
;
354 set_width_height(wwin
, &width
, &height
);
359 min_isect_y
= test_y
;
361 while (((test_y
+ height
) < usableArea
.y2
)) {
363 while ((test_x
+ width
) < usableArea
.x2
) {
364 sum_isect
= calcSumOfCoveredAreas(wwin
, test_x
, test_y
, width
, height
);
366 if (sum_isect
< min_isect
) {
367 min_isect
= sum_isect
;
368 min_isect_x
= test_x
;
369 min_isect_y
= test_y
;
372 test_x
+= PLACETEST_HSTEP
;
374 test_y
+= PLACETEST_VSTEP
;
377 from_x
= min_isect_x
- PLACETEST_HSTEP
+ 1;
378 from_x
= WMAX(from_x
, X_ORIGIN
);
379 to_x
= min_isect_x
+ PLACETEST_HSTEP
;
380 if (to_x
+ width
> usableArea
.x2
)
381 to_x
= usableArea
.x2
- width
;
383 from_y
= min_isect_y
- PLACETEST_VSTEP
+ 1;
384 from_y
= WMAX(from_y
, Y_ORIGIN
);
385 to_y
= min_isect_y
+ PLACETEST_VSTEP
;
386 if (to_y
+ height
> usableArea
.y2
)
387 to_y
= usableArea
.y2
- height
;
389 for (test_x
= from_x
; test_x
< to_x
; test_x
++) {
390 for (test_y
= from_y
; test_y
< to_y
; test_y
++) {
391 sum_isect
= calcSumOfCoveredAreas(wwin
, test_x
, test_y
, width
, height
);
393 if (sum_isect
< min_isect
) {
394 min_isect
= sum_isect
;
395 min_isect_x
= test_x
;
396 min_isect_y
= test_y
;
401 *x_ret
= min_isect_x
;
402 *y_ret
= min_isect_y
;
406 center_place_window(WWindow
*wwin
, int *x_ret
, int *y_ret
,
407 unsigned int width
, unsigned int height
, WArea usableArea
)
411 set_width_height(wwin
, &width
, &height
);
412 swidth
= usableArea
.x2
- usableArea
.x1
;
413 sheight
= usableArea
.y2
- usableArea
.y1
;
415 if (width
> swidth
|| height
> sheight
)
418 *x_ret
= (usableArea
.x1
+ usableArea
.x2
- width
) / 2;
419 *y_ret
= (usableArea
.y1
+ usableArea
.y2
- height
) / 2;
425 autoPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
426 unsigned int width
, unsigned int height
,
427 Bool ignore_sunken
, WArea usableArea
)
429 WScreen
*scr
= wwin
->screen_ptr
;
433 set_width_height(wwin
, &width
, &height
);
434 sw
= usableArea
.x2
- usableArea
.x1
;
435 sh
= usableArea
.y2
- usableArea
.y1
;
437 /* try placing at center first */
438 if (center_place_window(wwin
, &x
, &y
, width
, height
, usableArea
) &&
439 screen_has_space(scr
, x
, y
, width
, height
, False
)) {
445 /* this was based on fvwm2's smart placement */
446 for (y
= Y_ORIGIN
; (y
+ height
) < sh
; y
+= PLACETEST_VSTEP
) {
447 for (x
= X_ORIGIN
; (x
+ width
) < sw
; x
+= PLACETEST_HSTEP
) {
448 if (screen_has_space(scr
, x
, y
,
449 width
, height
, ignore_sunken
)) {
461 cascadeWindow(WScreen
*scr
, WWindow
*wwin
, int *x_ret
, int *y_ret
,
462 unsigned int width
, unsigned int height
, int h
, WArea usableArea
)
464 set_width_height(wwin
, &width
, &height
);
466 *x_ret
= h
* scr
->cascade_index
+ X_ORIGIN
;
467 *y_ret
= h
* scr
->cascade_index
+ Y_ORIGIN
;
469 if (width
+ *x_ret
> usableArea
.x2
|| height
+ *y_ret
> usableArea
.y2
) {
470 scr
->cascade_index
= 0;
476 static void randomPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
477 unsigned int width
, unsigned int height
, WArea usableArea
)
481 set_width_height(wwin
, &width
, &height
);
483 w
= ((usableArea
.x2
- X_ORIGIN
) - width
);
484 h
= ((usableArea
.y2
- Y_ORIGIN
) - height
);
489 *x_ret
= X_ORIGIN
+ rand() % w
;
490 *y_ret
= Y_ORIGIN
+ rand() % h
;
493 void PlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
, unsigned width
, unsigned height
)
495 WScreen
*scr
= wwin
->screen_ptr
;
496 int h
= WMFontHeight(scr
->title_font
)
497 + (wPreferences
.window_title_clearance
+ TITLEBAR_EXTEND_SPACE
) * 2;
499 if (h
> wPreferences
.window_title_max_height
)
500 h
= wPreferences
.window_title_max_height
;
502 if (h
< wPreferences
.window_title_min_height
)
503 h
= wPreferences
.window_title_min_height
;
505 WArea usableArea
= wGetUsableAreaForHead(scr
, wGetHeadForPointerLocation(scr
),
508 switch (wPreferences
.window_placement
) {
510 InteractivePlaceWindow(wwin
, x_ret
, y_ret
, width
, height
);
514 smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, usableArea
);
518 if (center_place_window(wwin
, x_ret
, y_ret
, width
, height
, usableArea
))
522 if (autoPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, False
, usableArea
)) {
524 } else if (autoPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, True
, usableArea
)) {
527 /* there isn't a break here, because if we fail, it should fall
528 through to cascade placement, as people who want tiling want
529 automagicness aren't going to want to place their window */
532 if (wPreferences
.window_placement
== WPM_AUTO
|| wPreferences
.window_placement
== WPM_CENTER
)
533 scr
->cascade_index
++;
535 cascadeWindow(scr
, wwin
, x_ret
, y_ret
, width
, height
, h
, usableArea
);
537 if (wPreferences
.window_placement
== WPM_CASCADE
)
538 scr
->cascade_index
++;
542 randomPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, usableArea
);
547 * clip to usableArea instead of full screen
548 * this will also take dock/clip etc.. into account
549 * aswell as being xinerama friendly
551 if (*x_ret
+ width
> usableArea
.x2
)
552 *x_ret
= usableArea
.x2
- width
;
553 if (*x_ret
< usableArea
.x1
)
554 *x_ret
= usableArea
.x1
;
556 if (*y_ret
+ height
> usableArea
.y2
)
557 *y_ret
= usableArea
.y2
- height
;
558 if (*y_ret
< usableArea
.y1
)
559 *y_ret
= usableArea
.y1
;