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"
38 #include "application.h"
41 #include "placement.h"
43 extern WPreferences wPreferences
;
45 #define X_ORIGIN WMAX(usableArea.x1,\
46 wPreferences.window_place_origin.x)
48 #define Y_ORIGIN WMAX(usableArea.y1,\
49 wPreferences.window_place_origin.y)
51 /* Returns True if it is an icon and is in this workspace */
53 iconPosition(WCoreWindow
*wcore
, int sx1
, int sy1
, int sx2
, int sy2
,
54 int workspace
, int *retX
, int *retY
)
59 parent
= wcore
->descriptor
.parent
;
61 /* if it is an application icon */
62 if (wcore
->descriptor
.parent_type
== WCLASS_APPICON
&& !((WAppIcon
*) parent
)->docked
) {
63 *retX
= ((WAppIcon
*) parent
)->x_pos
;
64 *retY
= ((WAppIcon
*) parent
)->y_pos
;
67 } else if (wcore
->descriptor
.parent_type
== WCLASS_MINIWINDOW
&&
68 (((WIcon
*) parent
)->owner
->frame
->workspace
== workspace
69 || IS_OMNIPRESENT(((WIcon
*) parent
)->owner
)
70 || wPreferences
.sticky_icons
)
71 && ((WIcon
*) parent
)->mapped
) {
73 *retX
= ((WIcon
*) parent
)->owner
->icon_x
;
74 *retY
= ((WIcon
*) parent
)->owner
->icon_y
;
77 } else if (wcore
->descriptor
.parent_type
== WCLASS_WINDOW
78 && ((WWindow
*) parent
)->flags
.icon_moved
79 && (((WWindow
*) parent
)->frame
->workspace
== workspace
|| IS_OMNIPRESENT((WWindow
*) parent
)
80 || wPreferences
.sticky_icons
)) {
81 *retX
= ((WWindow
*) parent
)->icon_x
;
82 *retY
= ((WWindow
*) parent
)->icon_y
;
87 /* Check if it is inside the screen */
89 if (*retX
< sx1
- wPreferences
.icon_size
)
94 if (*retY
< sy1
- wPreferences
.icon_size
)
103 void PlaceIcon(WScreen
*scr
, int *x_ret
, int *y_ret
, int head
)
105 int pf
; /* primary axis */
106 int sf
; /* secondary axis */
112 int sx1
, sx2
, sy1
, sy2
; /* screen boundary */
117 int isize
= wPreferences
.icon_size
;
120 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
122 /* Find out screen boundaries. */
124 /* Allows each head to have miniwindows */
132 sw
= isize
* (sw
/ isize
);
133 sh
= isize
* (sh
/ isize
);
134 fullW
= (sx2
- sx1
) / isize
;
135 fullH
= (sy2
- sy1
) / isize
;
137 /* icon yard boundaries */
138 if (wPreferences
.icon_yard
& IY_VERT
) {
145 if (wPreferences
.icon_yard
& IY_RIGHT
) {
152 if (wPreferences
.icon_yard
& IY_TOP
) {
161 * Create a map with the occupied slots. 1 means the slot is used
162 * or at least partially used.
163 * The slot usage can be optimized by only marking fully used slots
164 * or slots that have most of it covered.
165 * Space usage is worse than the fvwm algorithm (used in the old version)
166 * but complexity is much better (faster) than it.
168 map
= wmalloc((sw
+ 2) * (sh
+ 2));
170 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
172 WM_ETARETI_BAG(scr
->stacking_list
, obj
, iter
) {
177 if (iconPosition(obj
, sx1
, sy1
, sx2
, sy2
, scr
->current_workspace
, &x
, &y
)) {
178 int xdi
, ydi
; /* rounded down */
179 int xui
, yui
; /* rounded up */
183 xui
= (x
+ isize
/ 2) / isize
;
184 yui
= (y
+ isize
/ 2) / isize
;
185 map
[INDEX(xdi
, ydi
)] = 1;
186 map
[INDEX(xdi
, yui
)] = 1;
187 map
[INDEX(xui
, ydi
)] = 1;
188 map
[INDEX(xui
, yui
)] = 1;
190 obj
= obj
->stacking
->under
;
193 /* Default position */
197 /* Look for an empty slot */
198 for (si
= 0; si
< sf
; si
++) {
199 for (pi
= 0; pi
< pf
; pi
++) {
200 if (wPreferences
.icon_yard
& IY_VERT
) {
201 x
= xo
+ xs
* (si
* isize
);
202 y
= yo
+ ys
* (pi
* isize
);
204 x
= xo
+ xs
* (pi
* isize
);
205 y
= yo
+ ys
* (si
* isize
);
207 if (!map
[INDEX(x
/ isize
, y
/ isize
)]) {
221 /* Computes the intersecting length of two line sections */
222 int calcIntersectionLength(int p1
, int l1
, int p2
, int l2
)
238 else if (p2
+ l2
< p1
+ l1
)
241 isect
= p1
+ l1
- p2
;
246 /* Computes the intersecting area of two rectangles */
247 int calcIntersectionArea(int x1
, int y1
, int w1
, int h1
, int x2
, int y2
, int w2
, int h2
)
249 return calcIntersectionLength(x1
, w1
, x2
, w2
)
250 * calcIntersectionLength(y1
, h1
, y2
, h2
);
253 static int calcSumOfCoveredAreas(WWindow
*wwin
, int x
, int y
, int w
, int h
)
256 WWindow
*test_window
;
259 test_window
= wwin
->screen_ptr
->focused_window
;
260 for (; test_window
!= NULL
&& test_window
->prev
!= NULL
;)
261 test_window
= test_window
->prev
;
263 for (; test_window
!= NULL
; test_window
= test_window
->next
) {
264 if (test_window
->frame
->core
->stacking
->window_level
< WMNormalLevel
) {
268 tw
= test_window
->frame
->core
->width
;
269 th
= test_window
->frame
->core
->height
;
270 tx
= test_window
->frame_x
;
271 ty
= test_window
->frame_y
;
273 if (test_window
->flags
.mapped
|| (test_window
->flags
.shaded
&&
274 test_window
->frame
->workspace
== wwin
->screen_ptr
->current_workspace
&&
275 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
))) {
276 sum_isect
+= calcIntersectionArea(tx
, ty
, tw
, th
, x
, y
, w
, h
);
283 static void set_width_height(WWindow
*wwin
, unsigned int *width
, unsigned int *height
)
286 *height
+= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
;
288 if (HAS_TITLEBAR(wwin
))
289 *height
+= TITLEBAR_HEIGHT
;
290 if (HAS_RESIZEBAR(wwin
))
291 *height
+= RESIZEBAR_HEIGHT
;
293 if (HAS_BORDER(wwin
)) {
294 *height
+= 2 * wwin
->screen_ptr
->frame_border_width
;
295 *width
+= 2 * wwin
->screen_ptr
->frame_border_width
;
300 smartPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
, unsigned int width
,
301 unsigned int height
, WArea usableArea
)
303 int test_x
= 0, test_y
= Y_ORIGIN
;
304 int from_x
, to_x
, from_y
, to_y
;
306 int min_isect
, min_isect_x
, min_isect_y
;
309 set_width_height(wwin
, &width
, &height
);
314 min_isect_y
= test_y
;
316 while (((test_y
+ height
) < usableArea
.y2
)) {
318 while ((test_x
+ width
) < usableArea
.x2
) {
319 sum_isect
= calcSumOfCoveredAreas(wwin
, test_x
, test_y
, width
, height
);
321 if (sum_isect
< min_isect
) {
322 min_isect
= sum_isect
;
323 min_isect_x
= test_x
;
324 min_isect_y
= test_y
;
327 test_x
+= PLACETEST_HSTEP
;
329 test_y
+= PLACETEST_VSTEP
;
332 from_x
= min_isect_x
- PLACETEST_HSTEP
+ 1;
333 from_x
= WMAX(from_x
, X_ORIGIN
);
334 to_x
= min_isect_x
+ PLACETEST_HSTEP
;
335 if (to_x
+ width
> usableArea
.x2
)
336 to_x
= usableArea
.x2
- width
;
338 from_y
= min_isect_y
- PLACETEST_VSTEP
+ 1;
339 from_y
= WMAX(from_y
, Y_ORIGIN
);
340 to_y
= min_isect_y
+ PLACETEST_VSTEP
;
341 if (to_y
+ height
> usableArea
.y2
)
342 to_y
= usableArea
.y2
- height
;
344 for (test_x
= from_x
; test_x
< to_x
; test_x
++) {
345 for (test_y
= from_y
; test_y
< to_y
; test_y
++) {
346 sum_isect
= calcSumOfCoveredAreas(wwin
, test_x
, test_y
, width
, height
);
348 if (sum_isect
< min_isect
) {
349 min_isect
= sum_isect
;
350 min_isect_x
= test_x
;
351 min_isect_y
= test_y
;
356 *x_ret
= min_isect_x
;
357 *y_ret
= min_isect_y
;
361 center_place_window(WWindow
*wwin
, int *x_ret
, int *y_ret
,
362 unsigned int width
, unsigned int height
, WArea usableArea
)
366 set_width_height(wwin
, &width
, &height
);
367 swidth
= usableArea
.x2
- usableArea
.x1
;
368 sheight
= usableArea
.y2
- usableArea
.y1
;
370 if (width
> swidth
|| height
> sheight
)
373 *x_ret
= (usableArea
.x1
+ usableArea
.x2
- width
) / 2;
374 *y_ret
= (usableArea
.y1
+ usableArea
.y2
- height
) / 2;
380 autoPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
381 unsigned int width
, unsigned int height
, int tryCount
, WArea usableArea
)
383 WScreen
*scr
= wwin
->screen_ptr
;
384 int test_x
= 0, test_y
= Y_ORIGIN
;
385 int loc_ok
= False
, tw
, tx
, ty
, th
;
387 WWindow
*test_window
;
389 set_width_height(wwin
, &width
, &height
);
390 swidth
= usableArea
.x2
- usableArea
.x1
;
393 /* this was based on fvwm2's smart placement */
394 while (((test_y
+ height
) < (usableArea
.y2
- usableArea
.y1
)) && !loc_ok
) {
397 while (((test_x
+ width
) < swidth
) && (!loc_ok
)) {
400 test_window
= scr
->focused_window
;
402 while ((test_window
!= NULL
) && (loc_ok
== True
)) {
404 if (test_window
->frame
->core
->stacking
->window_level
405 < WMNormalLevel
&& tryCount
> 0) {
406 test_window
= test_window
->next
;
409 tw
= test_window
->frame
->core
->width
;
410 th
= test_window
->frame
->core
->height
;
411 tx
= test_window
->frame_x
;
412 ty
= test_window
->frame_y
;
414 if ((tx
< (test_x
+ width
)) && ((tx
+ tw
) > test_x
) &&
415 (ty
< (test_y
+ height
)) && ((ty
+ th
) > test_y
) &&
416 (test_window
->flags
.mapped
||
417 (test_window
->flags
.shaded
&&
418 test_window
->frame
->workspace
== scr
->current_workspace
&&
419 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
)))) {
423 test_window
= test_window
->next
;
426 test_window
= scr
->focused_window
;
428 while ((test_window
!= NULL
) && (loc_ok
== True
)) {
430 if (test_window
->frame
->core
->stacking
->window_level
431 < WMNormalLevel
&& tryCount
> 0) {
432 test_window
= test_window
->prev
;
435 tw
= test_window
->frame
->core
->width
;
436 th
= test_window
->frame
->core
->height
;
437 tx
= test_window
->frame_x
;
438 ty
= test_window
->frame_y
;
440 if ((tx
< (test_x
+ width
)) && ((tx
+ tw
) > test_x
) &&
441 (ty
< (test_y
+ height
)) && ((ty
+ th
) > test_y
) &&
442 (test_window
->flags
.mapped
||
443 (test_window
->flags
.shaded
&&
444 test_window
->frame
->workspace
== scr
->current_workspace
&&
445 !(test_window
->flags
.miniaturized
|| test_window
->flags
.hidden
)))) {
449 test_window
= test_window
->prev
;
451 if (loc_ok
== True
) {
456 test_x
+= PLACETEST_HSTEP
;
458 test_y
+= PLACETEST_VSTEP
;
465 cascadeWindow(WScreen
*scr
, WWindow
*wwin
, int *x_ret
, int *y_ret
,
466 unsigned int width
, unsigned int height
, int h
, WArea usableArea
)
468 set_width_height(wwin
, &width
, &height
);
470 *x_ret
= h
* scr
->cascade_index
+ X_ORIGIN
;
471 *y_ret
= h
* scr
->cascade_index
+ Y_ORIGIN
;
473 if (width
+ *x_ret
> usableArea
.x2
|| height
+ *y_ret
> usableArea
.y2
) {
474 scr
->cascade_index
= 0;
480 static void randomPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
481 unsigned int width
, unsigned int height
, WArea usableArea
)
485 set_width_height(wwin
, &width
, &height
);
487 w
= ((usableArea
.x2
- X_ORIGIN
) - width
);
488 h
= ((usableArea
.y2
- Y_ORIGIN
) - height
);
493 *x_ret
= X_ORIGIN
+ rand() % w
;
494 *y_ret
= Y_ORIGIN
+ rand() % h
;
497 void PlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
, unsigned width
, unsigned height
)
499 WScreen
*scr
= wwin
->screen_ptr
;
500 int h
= WMFontHeight(scr
->title_font
)
501 + (wPreferences
.window_title_clearance
+ TITLEBAR_EXTEND_SPACE
) * 2;
503 if (h
> wPreferences
.window_title_max_height
)
504 h
= wPreferences
.window_title_max_height
;
506 if (h
< wPreferences
.window_title_min_height
)
507 h
= wPreferences
.window_title_min_height
;
509 WArea usableArea
= wGetUsableAreaForHead(scr
, wGetHeadForPointerLocation(scr
),
512 switch (wPreferences
.window_placement
) {
514 InteractivePlaceWindow(wwin
, x_ret
, y_ret
, width
, height
);
518 smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, usableArea
);
522 if (center_place_window(wwin
, x_ret
, y_ret
, width
, height
, usableArea
))
526 if (autoPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 0, usableArea
)) {
528 } else if (autoPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 1, usableArea
)) {
531 /* there isn't a break here, because if we fail, it should fall
532 through to cascade placement, as people who want tiling want
533 automagicness aren't going to want to place their window */
536 if (wPreferences
.window_placement
== WPM_AUTO
|| wPreferences
.window_placement
== WPM_CENTER
)
537 scr
->cascade_index
++;
539 cascadeWindow(scr
, wwin
, x_ret
, y_ret
, width
, height
, h
, usableArea
);
541 if (wPreferences
.window_placement
== WPM_CASCADE
)
542 scr
->cascade_index
++;
546 randomPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, usableArea
);
551 * clip to usableArea instead of full screen
552 * this will also take dock/clip etc.. into account
553 * aswell as being xinerama friendly
555 if (*x_ret
+ width
> usableArea
.x2
)
556 *x_ret
= usableArea
.x2
- width
;
557 if (*x_ret
< usableArea
.x1
)
558 *x_ret
= usableArea
.x1
;
560 if (*y_ret
+ height
> usableArea
.y2
)
561 *y_ret
= usableArea
.y2
- height
;
562 if (*y_ret
< usableArea
.y1
)
563 *y_ret
= usableArea
.y1
;