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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include "WindowMaker.h"
39 #include "application.h"
44 extern WPreferences wPreferences
;
46 #define X_ORIGIN WMAX(usableArea.x1,\
47 wPreferences.window_place_origin.x)
49 #define Y_ORIGIN WMAX(usableArea.y1,\
50 wPreferences.window_place_origin.y)
52 /* interactive window placement is in moveres.c */
53 extern void InteractivePlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
54 unsigned width
, unsigned height
);
56 /* Returns True if it is an icon and is in this workspace */
58 iconPosition(WCoreWindow
*wcore
, int sx1
, int sy1
, int sx2
, int sy2
,
59 int workspace
, int *retX
, int *retY
)
64 parent
= wcore
->descriptor
.parent
;
66 /* if it is an application icon */
67 if (wcore
->descriptor
.parent_type
== WCLASS_APPICON
&& !((WAppIcon
*) parent
)->docked
) {
68 *retX
= ((WAppIcon
*) parent
)->x_pos
;
69 *retY
= ((WAppIcon
*) parent
)->y_pos
;
72 } else if (wcore
->descriptor
.parent_type
== WCLASS_MINIWINDOW
&&
73 (((WIcon
*) parent
)->owner
->frame
->workspace
== workspace
74 || IS_OMNIPRESENT(((WIcon
*) parent
)->owner
)
75 || wPreferences
.sticky_icons
)
76 && ((WIcon
*) parent
)->mapped
) {
78 *retX
= ((WIcon
*) parent
)->owner
->icon_x
;
79 *retY
= ((WIcon
*) parent
)->owner
->icon_y
;
82 } else if (wcore
->descriptor
.parent_type
== WCLASS_WINDOW
83 && ((WWindow
*) parent
)->flags
.icon_moved
84 && (((WWindow
*) parent
)->frame
->workspace
== workspace
|| IS_OMNIPRESENT((WWindow
*) parent
)
85 || wPreferences
.sticky_icons
)) {
86 *retX
= ((WWindow
*) parent
)->icon_x
;
87 *retY
= ((WWindow
*) parent
)->icon_y
;
92 /* Check if it is inside the screen */
94 if (*retX
< sx1
- wPreferences
.icon_size
)
99 if (*retY
< sy1
- wPreferences
.icon_size
)
101 else if (*retY
> sy2
)
108 void PlaceIcon(WScreen
*scr
, int *x_ret
, int *y_ret
, int head
)
110 int pf
; /* primary axis */
111 int sf
; /* secondary axis */
117 int sx1
, sx2
, sy1
, sy2
; /* screen boundary */
122 int isize
= wPreferences
.icon_size
;
125 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
127 /* Find out screen boundaries. */
129 /* Allows each head to have miniwindows */
137 sw
= isize
* (sw
/ isize
);
138 sh
= isize
* (sh
/ isize
);
139 fullW
= (sx2
- sx1
) / isize
;
140 fullH
= (sy2
- sy1
) / isize
;
142 /* icon yard boundaries */
143 if (wPreferences
.icon_yard
& IY_VERT
) {
150 if (wPreferences
.icon_yard
& IY_RIGHT
) {
157 if (wPreferences
.icon_yard
& IY_TOP
) {
166 * Create a map with the occupied slots. 1 means the slot is used
167 * or at least partially used.
168 * The slot usage can be optimized by only marking fully used slots
169 * or slots that have most of it covered.
170 * Space usage is worse than the fvwm algorithm (used in the old version)
171 * but complexity is much better (faster) than it.
173 map
= wmalloc((sw
+ 2) * (sh
+ 2));
174 memset(map
, 0, (sw
+ 2) * (sh
+ 2));
176 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
178 WM_ETARETI_BAG(scr
->stacking_list
, obj
, iter
) {
183 if (iconPosition(obj
, sx1
, sy1
, sx2
, sy2
, scr
->current_workspace
, &x
, &y
)) {
184 int xdi
, ydi
; /* rounded down */
185 int xui
, yui
; /* rounded up */
189 xui
= (x
+ isize
/ 2) / isize
;
190 yui
= (y
+ isize
/ 2) / isize
;
191 map
[INDEX(xdi
, ydi
)] = 1;
192 map
[INDEX(xdi
, yui
)] = 1;
193 map
[INDEX(xui
, ydi
)] = 1;
194 map
[INDEX(xui
, yui
)] = 1;
196 obj
= obj
->stacking
->under
;
199 /* Default position */
203 /* Look for an empty slot */
204 for (si
= 0; si
< sf
; si
++) {
205 for (pi
= 0; pi
< pf
; pi
++) {
206 if (wPreferences
.icon_yard
& IY_VERT
) {
207 x
= xo
+ xs
* (si
* isize
);
208 y
= yo
+ ys
* (pi
* isize
);
210 x
= xo
+ xs
* (pi
* isize
);
211 y
= yo
+ ys
* (si
* isize
);
213 if (!map
[INDEX(x
/ isize
, y
/ isize
)]) {
227 /* Computes the intersecting length of two line sections */
228 int calcIntersectionLength(int p1
, int l1
, int p2
, int l2
)
244 else if (p2
+ l2
< p1
+ l1
)
247 isect
= p1
+ l1
- p2
;
252 /* Computes the intersecting area of two rectangles */
253 int calcIntersectionArea(int x1
, int y1
, int w1
, int h1
, int x2
, int y2
, int w2
, int h2
)
255 return calcIntersectionLength(x1
, w1
, x2
, w2
)
256 * calcIntersectionLength(y1
, h1
, y2
, h2
);
259 static int calcSumOfCoveredAreas(WWindow
*wwin
, int x
, int y
, int w
, int h
)
262 WWindow
*test_window
;
265 test_window
= wwin
->screen_ptr
->focused_window
;
266 for (; test_window
!= NULL
&& test_window
->prev
!= NULL
;)
267 test_window
= test_window
->prev
;
269 for (; test_window
!= NULL
; test_window
= test_window
->next
) {
270 if (test_window
->frame
->core
->stacking
->window_level
< WMNormalLevel
) {
274 tw
= test_window
->frame
->core
->width
;
275 th
= test_window
->frame
->core
->height
;
276 tx
= test_window
->frame_x
;
277 ty
= test_window
->frame_y
;
279 if (test_window
->flags
.mapped
|| (test_window
->flags
.shaded
&&
280 test_window
->frame
->workspace
== wwin
->screen_ptr
->current_workspace
&&
281 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
))) {
282 sum_isect
+= calcIntersectionArea(tx
, ty
, tw
, th
, x
, y
, w
, h
);
289 static void set_width_height(WWindow
*wwin
, unsigned int *width
, unsigned int *height
)
292 *height
+= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
;
294 if (HAS_TITLEBAR(wwin
))
295 *height
+= TITLEBAR_HEIGHT
;
296 if (HAS_RESIZEBAR(wwin
))
297 *height
+= RESIZEBAR_HEIGHT
;
299 if (HAS_BORDER(wwin
)) {
300 *height
+= 2 * FRAME_BORDER_WIDTH
;
301 *width
+= 2 * FRAME_BORDER_WIDTH
;
306 smartPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
, unsigned int width
,
307 unsigned int height
, WArea usableArea
)
309 int test_x
= 0, test_y
= Y_ORIGIN
;
310 int from_x
, to_x
, from_y
, to_y
;
312 int min_isect
, min_isect_x
, min_isect_y
;
315 set_width_height(wwin
, &width
, &height
);
320 min_isect_y
= test_y
;
322 while (((test_y
+ height
) < usableArea
.y2
)) {
324 while ((test_x
+ width
) < usableArea
.x2
) {
325 sum_isect
= calcSumOfCoveredAreas(wwin
, test_x
, test_y
, width
, height
);
327 if (sum_isect
< min_isect
) {
328 min_isect
= sum_isect
;
329 min_isect_x
= test_x
;
330 min_isect_y
= test_y
;
333 test_x
+= PLACETEST_HSTEP
;
335 test_y
+= PLACETEST_VSTEP
;
338 from_x
= min_isect_x
- PLACETEST_HSTEP
+ 1;
339 from_x
= WMAX(from_x
, X_ORIGIN
);
340 to_x
= min_isect_x
+ PLACETEST_HSTEP
;
341 if (to_x
+ width
> usableArea
.x2
)
342 to_x
= usableArea
.x2
- width
;
344 from_y
= min_isect_y
- PLACETEST_VSTEP
+ 1;
345 from_y
= WMAX(from_y
, Y_ORIGIN
);
346 to_y
= min_isect_y
+ PLACETEST_VSTEP
;
347 if (to_y
+ height
> usableArea
.y2
)
348 to_y
= usableArea
.y2
- height
;
350 for (test_x
= from_x
; test_x
< to_x
; test_x
++) {
351 for (test_y
= from_y
; test_y
< to_y
; test_y
++) {
352 sum_isect
= calcSumOfCoveredAreas(wwin
, test_x
, test_y
, width
, height
);
354 if (sum_isect
< min_isect
) {
355 min_isect
= sum_isect
;
356 min_isect_x
= test_x
;
357 min_isect_y
= test_y
;
362 *x_ret
= min_isect_x
;
363 *y_ret
= min_isect_y
;
367 autoPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
368 unsigned int width
, unsigned int height
, int tryCount
, WArea usableArea
)
370 WScreen
*scr
= wwin
->screen_ptr
;
371 int test_x
= 0, test_y
= Y_ORIGIN
;
372 int loc_ok
= False
, tw
, tx
, ty
, th
;
374 WWindow
*test_window
;
376 set_width_height(wwin
, &width
, &height
);
377 swidth
= usableArea
.x2
- usableArea
.x1
;
380 /* this was based on fvwm2's smart placement */
381 while (((test_y
+ height
) < (usableArea
.y2
- usableArea
.y1
)) && !loc_ok
) {
384 while (((test_x
+ width
) < swidth
) && (!loc_ok
)) {
387 test_window
= scr
->focused_window
;
389 while ((test_window
!= NULL
) && (loc_ok
== True
)) {
391 if (test_window
->frame
->core
->stacking
->window_level
392 < WMNormalLevel
&& tryCount
> 0) {
393 test_window
= test_window
->next
;
396 tw
= test_window
->frame
->core
->width
;
397 th
= test_window
->frame
->core
->height
;
398 tx
= test_window
->frame_x
;
399 ty
= test_window
->frame_y
;
401 if ((tx
< (test_x
+ width
)) && ((tx
+ tw
) > test_x
) &&
402 (ty
< (test_y
+ height
)) && ((ty
+ th
) > test_y
) &&
403 (test_window
->flags
.mapped
||
404 (test_window
->flags
.shaded
&&
405 test_window
->frame
->workspace
== scr
->current_workspace
&&
406 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
)))) {
410 test_window
= test_window
->next
;
413 test_window
= scr
->focused_window
;
415 while ((test_window
!= NULL
) && (loc_ok
== True
)) {
417 if (test_window
->frame
->core
->stacking
->window_level
418 < WMNormalLevel
&& tryCount
> 0) {
419 test_window
= test_window
->prev
;
422 tw
= test_window
->frame
->core
->width
;
423 th
= test_window
->frame
->core
->height
;
424 tx
= test_window
->frame_x
;
425 ty
= test_window
->frame_y
;
427 if ((tx
< (test_x
+ width
)) && ((tx
+ tw
) > test_x
) &&
428 (ty
< (test_y
+ height
)) && ((ty
+ th
) > test_y
) &&
429 (test_window
->flags
.mapped
||
430 (test_window
->flags
.shaded
&&
431 test_window
->frame
->workspace
== scr
->current_workspace
&&
432 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
)))) {
436 test_window
= test_window
->prev
;
438 if (loc_ok
== True
) {
443 test_x
+= PLACETEST_HSTEP
;
445 test_y
+= PLACETEST_VSTEP
;
452 cascadeWindow(WScreen
*scr
, WWindow
*wwin
, int *x_ret
, int *y_ret
,
453 unsigned int width
, unsigned int height
, int h
, WArea usableArea
)
455 set_width_height(wwin
, &width
, &height
);
457 *x_ret
= h
* scr
->cascade_index
+ X_ORIGIN
;
458 *y_ret
= h
* scr
->cascade_index
+ Y_ORIGIN
;
460 if (width
+ *x_ret
> usableArea
.x2
|| height
+ *y_ret
> usableArea
.y2
) {
461 scr
->cascade_index
= 0;
467 static void randomPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
468 unsigned int width
, unsigned int height
, WArea usableArea
)
472 set_width_height(wwin
, &width
, &height
);
474 w
= ((usableArea
.x2
- X_ORIGIN
) - width
);
475 h
= ((usableArea
.y2
- Y_ORIGIN
) - height
);
480 *x_ret
= X_ORIGIN
+ rand() % w
;
481 *y_ret
= Y_ORIGIN
+ rand() % h
;
484 void PlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
, unsigned width
, unsigned height
)
486 WScreen
*scr
= wwin
->screen_ptr
;
487 int h
= WMFontHeight(scr
->title_font
)
488 + (wPreferences
.window_title_clearance
+ TITLEBAR_EXTEND_SPACE
) * 2;
489 WArea usableArea
= wGetUsableAreaForHead(scr
, wGetHeadForPointerLocation(scr
),
492 switch (wPreferences
.window_placement
) {
494 InteractivePlaceWindow(wwin
, x_ret
, y_ret
, width
, height
);
498 smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, usableArea
);
502 if (autoPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 0, usableArea
)) {
504 } else if (autoPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 1, usableArea
)) {
507 /* there isn't a break here, because if we fail, it should fall
508 through to cascade placement, as people who want tiling want
509 automagicness aren't going to want to place their window */
512 if (wPreferences
.window_placement
== WPM_AUTO
)
513 scr
->cascade_index
++;
515 cascadeWindow(scr
, wwin
, x_ret
, y_ret
, width
, height
, h
, usableArea
);
517 if (wPreferences
.window_placement
== WPM_CASCADE
)
518 scr
->cascade_index
++;
522 randomPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, usableArea
);
527 * clip to usableArea instead of full screen
528 * this will also take dock/clip etc.. into account
529 * aswell as being xinerama friendly
531 if (*x_ret
+ width
> usableArea
.x2
)
532 *x_ret
= usableArea
.x2
- width
;
533 if (*x_ret
< usableArea
.x1
)
534 *x_ret
= usableArea
.x1
;
536 if (*y_ret
+ height
> usableArea
.y2
)
537 *y_ret
= usableArea
.y2
- height
;
538 if (*y_ret
< usableArea
.y1
)
539 *y_ret
= usableArea
.y1
;