1 /* placement.c - window and icon placement on screen
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 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,
30 #include "WindowMaker.h"
37 #include "application.h"
41 extern WPreferences wPreferences
;
44 #define X_ORIGIN(scr) WMAX((scr)->totalUsableArea.x1,\
45 wPreferences.window_place_origin.x)
47 #define Y_ORIGIN(scr) WMAX((scr)->totalUsableArea.y1,\
48 wPreferences.window_place_origin.y)
52 * interactive window placement is in moveres.c
56 InteractivePlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
57 unsigned width
, unsigned height
);
61 * Returns True if it is an icon and is in this workspace.
64 iconPosition(WCoreWindow
*wcore
, int sx1
, int sy1
, int sx2
, int sy2
,
65 int workspace
, int *retX
, int *retY
)
70 parent
= wcore
->descriptor
.parent
;
72 /* if it is an application icon */
73 if (wcore
->descriptor
.parent_type
== WCLASS_APPICON
) {
74 *retX
= ((WAppIcon
*)parent
)->x_pos
;
75 *retY
= ((WAppIcon
*)parent
)->y_pos
;
78 } else if (wcore
->descriptor
.parent_type
== WCLASS_MINIWINDOW
&&
79 (((WIcon
*)parent
)->owner
->frame
->workspace
==workspace
80 || IS_OMNIPRESENT(((WIcon
*)parent
)->owner
)
81 || wPreferences
.sticky_icons
)
82 && !((WIcon
*)parent
)->owner
->flags
.hidden
) {
84 *retX
= ((WIcon
*)parent
)->owner
->icon_x
;
85 *retY
= ((WIcon
*)parent
)->owner
->icon_y
;
91 * 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
)
112 PlaceIcon(WScreen
*scr
, int *x_ret
, int *y_ret
)
114 int pf
; /* primary axis */
115 int sf
; /* secondary axis */
121 int sx1
, sx2
, sy1
, sy2
; /* screen boundary */
126 int isize
= wPreferences
.icon_size
;
131 * Find out screen boundaries.
135 sx2
= scr
->scr_width
;
136 sy2
= scr
->scr_height
;
138 if (scr
->dock
->on_right_side
)
139 sx2
-= isize
+ DOCK_EXTRA_SPACE
;
141 sx1
+= isize
+ DOCK_EXTRA_SPACE
;
144 sw
= isize
* (scr
->scr_width
/isize
);
145 sh
= isize
* (scr
->scr_height
/isize
);
146 fullW
= (sx2
-sx1
)/isize
;
147 fullH
= (sy2
-sy1
)/isize
;
149 /* icon yard boundaries */
150 if (wPreferences
.icon_yard
& IY_VERT
) {
157 if (wPreferences
.icon_yard
& IY_RIGHT
) {
164 if (wPreferences
.icon_yard
& IY_TOP
) {
173 * Create a map with the occupied slots. 1 means the slot is used
174 * or at least partially used.
175 * The slot usage can be optimized by only marking fully used slots
176 * or slots that have most of it covered.
177 * Space usage is worse than the fvwm algorithm (used in the old version)
178 * but complexity is much better (faster) than it.
180 map
= wmalloc((sw
+2) * (sh
+2));
181 memset(map
, 0, (sw
+2) * (sh
+2));
183 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
185 for (level
= WMNormalLevel
; level
>= WMDesktopLevel
; level
--) {
186 obj
= scr
->stacking_list
[level
];
191 if (iconPosition(obj
, sx1
, sy1
, sx2
, sy2
, scr
->current_workspace
,
193 int xdi
, ydi
; /* rounded down */
194 int xui
, yui
; /* rounded up */
198 xui
= (x
+isize
/2)/isize
;
199 yui
= (y
+isize
/2)/isize
;
200 map
[INDEX(xdi
,ydi
)] = 1;
201 map
[INDEX(xdi
,yui
)] = 1;
202 map
[INDEX(xui
,ydi
)] = 1;
203 map
[INDEX(xui
,yui
)] = 1;
205 obj
= obj
->stacking
->under
;
215 * Look for an empty slot
217 for (si
=0; si
<sf
; si
++) {
218 for (pi
=0; pi
<pf
; pi
++) {
219 if (wPreferences
.icon_yard
& IY_VERT
) {
220 x
= xo
+ xs
*(si
*isize
);
221 y
= yo
+ ys
*(pi
*isize
);
223 x
= xo
+ xs
*(pi
*isize
);
224 y
= yo
+ ys
*(si
*isize
);
226 if (!map
[INDEX(x
/isize
, y
/isize
)]) {
244 smartPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
245 unsigned int width
, unsigned int height
, int tryCount
)
247 WScreen
*scr
= wwin
->screen_ptr
;
248 int test_x
= 0, test_y
= Y_ORIGIN(scr
);
249 int loc_ok
= False
, tw
,tx
,ty
,th
;
251 WWindow
*test_window
;
253 WArea usableArea
= scr
->totalUsableArea
;
256 extra_height
= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
+ 2;
258 extra_height
= 24; /* random value */
260 swidth
= usableArea
.x2
-usableArea
.x1
;
263 /* this was based on fvwm2's smart placement */
265 height
+= extra_height
;
267 while (((test_y
+ height
) < (scr
->scr_height
)) && (!loc_ok
)) {
271 while (((test_x
+ width
) < swidth
) && (!loc_ok
)) {
274 test_window
= scr
->focused_window
;
276 while ((test_window
!= NULL
) && (loc_ok
== True
)) {
278 if (test_window
->frame
->core
->stacking
->window_level
279 < WMNormalLevel
&& tryCount
> 0) {
280 test_window
= test_window
->next
;
283 tw
= test_window
->client
.width
;
284 if (test_window
->flags
.shaded
)
285 th
= test_window
->frame
->top_width
;
287 th
= test_window
->client
.height
+ extra_height
;
288 tx
= test_window
->frame_x
;
289 ty
= test_window
->frame_y
;
291 if ((tx
< (test_x
+ width
)) && ((tx
+ tw
) > test_x
) &&
292 (ty
< (test_y
+ height
)) && ((ty
+ th
) > test_y
) &&
293 (test_window
->flags
.mapped
||
294 (test_window
->flags
.shaded
&&
295 !(test_window
->flags
.miniaturized
||
296 test_window
->flags
.hidden
)))) {
300 test_window
= test_window
->next
;
303 test_window
= scr
->focused_window
;
305 while ((test_window
!= NULL
) && (loc_ok
== True
)) {
307 if (test_window
->frame
->core
->stacking
->window_level
308 < WMNormalLevel
&& tryCount
> 0) {
309 test_window
= test_window
->prev
;
312 tw
= test_window
->client
.width
;
313 if (test_window
->flags
.shaded
)
314 th
= test_window
->frame
->top_width
;
316 th
= test_window
->client
.height
+ extra_height
;
317 tx
= test_window
->frame_x
;
318 ty
= test_window
->frame_y
;
320 if ((tx
< (test_x
+ width
)) && ((tx
+ tw
) > test_x
) &&
321 (ty
< (test_y
+ height
)) && ((ty
+ th
) > test_y
) &&
322 (test_window
->flags
.mapped
||
323 (test_window
->flags
.shaded
&&
324 !(test_window
->flags
.miniaturized
||
325 test_window
->flags
.hidden
)))) {
329 test_window
= test_window
->prev
;
331 if (loc_ok
== True
) {
336 test_x
+= PLACETEST_HSTEP
;
338 test_y
+= PLACETEST_VSTEP
;
345 cascadeWindow(WScreen
*scr
, WWindow
*wwin
, int *x_ret
, int *y_ret
,
346 unsigned int width
, unsigned int height
, int h
)
348 unsigned int extra_height
;
349 WArea usableArea
= scr
->totalUsableArea
;
352 extra_height
= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
;
354 extra_height
= 24; /* random value */
356 *x_ret
= h
* scr
->cascade_index
+ X_ORIGIN(scr
);
357 *y_ret
= h
* scr
->cascade_index
+ Y_ORIGIN(scr
);
358 height
+= extra_height
;
360 if (width
+ *x_ret
> usableArea
.x2
|| height
+ *y_ret
> usableArea
.y2
) {
361 scr
->cascade_index
= 0;
362 *x_ret
= X_ORIGIN(scr
);
363 *y_ret
= Y_ORIGIN(scr
);
369 PlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
370 unsigned width
, unsigned height
)
372 WScreen
*scr
= wwin
->screen_ptr
;
373 int h
= scr
->title_font
->height
+TITLEBAR_EXTRA_HEIGHT
;
375 switch (wPreferences
.window_placement
) {
377 InteractivePlaceWindow(wwin
, x_ret
, y_ret
, width
, height
);
381 if (smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 0)) {
383 } else if (smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 1)) {
386 /* there isn't a break here, because if we fail, it should fall
387 through to cascade placement, as people who want tiling want
388 automagicness aren't going to want to place their window */
391 if (wPreferences
.window_placement
== WPM_SMART
)
392 scr
->cascade_index
++;
394 cascadeWindow(scr
, wwin
, x_ret
, y_ret
, width
, height
, h
);
396 if (wPreferences
.window_placement
== WPM_CASCADE
)
397 scr
->cascade_index
++;
402 int w
, h
, extra_height
;
403 WArea usableArea
= scr
->totalUsableArea
;
406 extra_height
= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
+ 2;
408 extra_height
= 24; /* random value */
410 //w = ((usableArea.x2-usableArea.x1) - width);
411 //h = ((usableArea.y2-usableArea.y1) - height - extra_height);
412 w
= ((usableArea
.x2
-X_ORIGIN(scr
)) - width
);
413 h
= ((usableArea
.y2
-Y_ORIGIN(scr
)) - height
- extra_height
);
416 *x_ret
= X_ORIGIN(scr
) + rand()%w
;
417 *y_ret
= Y_ORIGIN(scr
) + rand()%h
;
423 puts("Invalid window placement!!!");
429 if (*x_ret
+ width
> scr
->scr_width
)
430 *x_ret
= scr
->scr_width
- width
;
434 if (*y_ret
+ height
> scr
->scr_height
)
435 *y_ret
= scr
->scr_height
- height
;