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"
43 extern WPreferences wPreferences
;
46 #define X_ORIGIN(scr) WMAX((scr)->totalUsableArea.x1,\
47 wPreferences.window_place_origin.x)
49 #define Y_ORIGIN(scr) WMAX((scr)->totalUsableArea.y1,\
50 wPreferences.window_place_origin.y)
54 * interactive window placement is in moveres.c
58 InteractivePlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
59 unsigned width
, unsigned height
);
63 * Returns True if it is an icon and is in this workspace.
66 iconPosition(WCoreWindow
*wcore
, int sx1
, int sy1
, int sx2
, int sy2
,
67 int workspace
, int *retX
, int *retY
)
72 parent
= wcore
->descriptor
.parent
;
74 /* if it is an application icon */
75 if (wcore
->descriptor
.parent_type
== WCLASS_APPICON
) {
76 *retX
= ((WAppIcon
*)parent
)->x_pos
;
77 *retY
= ((WAppIcon
*)parent
)->y_pos
;
80 } else if (wcore
->descriptor
.parent_type
== WCLASS_MINIWINDOW
&&
81 (((WIcon
*)parent
)->owner
->frame
->workspace
==workspace
82 || IS_OMNIPRESENT(((WIcon
*)parent
)->owner
)
83 || wPreferences
.sticky_icons
)
84 && !((WIcon
*)parent
)->owner
->flags
.hidden
) {
86 *retX
= ((WIcon
*)parent
)->owner
->icon_x
;
87 *retY
= ((WIcon
*)parent
)->owner
->icon_y
;
93 * Check if it is inside the screen.
96 if (*retX
< sx1
-wPreferences
.icon_size
)
101 if (*retY
< sy1
-wPreferences
.icon_size
)
103 else if (*retY
> sy2
)
114 PlaceIcon(WScreen
*scr
, int *x_ret
, int *y_ret
)
116 int pf
; /* primary axis */
117 int sf
; /* secondary axis */
123 int sx1
, sx2
, sy1
, sy2
; /* screen boundary */
128 int isize
= wPreferences
.icon_size
;
133 * Find out screen boundaries.
137 sx2
= scr
->scr_width
;
138 sy2
= scr
->scr_height
;
140 if (scr
->dock
->on_right_side
)
141 sx2
-= isize
+ DOCK_EXTRA_SPACE
;
143 sx1
+= isize
+ DOCK_EXTRA_SPACE
;
146 sw
= isize
* (scr
->scr_width
/isize
);
147 sh
= isize
* (scr
->scr_height
/isize
);
148 fullW
= (sx2
-sx1
)/isize
;
149 fullH
= (sy2
-sy1
)/isize
;
151 /* icon yard boundaries */
152 if (wPreferences
.icon_yard
& IY_VERT
) {
159 if (wPreferences
.icon_yard
& IY_RIGHT
) {
166 if (wPreferences
.icon_yard
& IY_TOP
) {
175 * Create a map with the occupied slots. 1 means the slot is used
176 * or at least partially used.
177 * The slot usage can be optimized by only marking fully used slots
178 * or slots that have most of it covered.
179 * Space usage is worse than the fvwm algorithm (used in the old version)
180 * but complexity is much better (faster) than it.
182 map
= wmalloc((sw
+2) * (sh
+2));
183 memset(map
, 0, (sw
+2) * (sh
+2));
185 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
187 for (level
= WMNormalLevel
; level
>= WMDesktopLevel
; level
--) {
188 obj
= scr
->stacking_list
[level
];
193 if (iconPosition(obj
, sx1
, sy1
, sx2
, sy2
, scr
->current_workspace
,
195 int xdi
, ydi
; /* rounded down */
196 int xui
, yui
; /* rounded up */
200 xui
= (x
+isize
/2)/isize
;
201 yui
= (y
+isize
/2)/isize
;
202 map
[INDEX(xdi
,ydi
)] = 1;
203 map
[INDEX(xdi
,yui
)] = 1;
204 map
[INDEX(xui
,ydi
)] = 1;
205 map
[INDEX(xui
,yui
)] = 1;
207 obj
= obj
->stacking
->under
;
217 * Look for an empty slot
219 for (si
=0; si
<sf
; si
++) {
220 for (pi
=0; pi
<pf
; pi
++) {
221 if (wPreferences
.icon_yard
& IY_VERT
) {
222 x
= xo
+ xs
*(si
*isize
);
223 y
= yo
+ ys
*(pi
*isize
);
225 x
= xo
+ xs
*(pi
*isize
);
226 y
= yo
+ ys
*(si
*isize
);
228 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
;
346 cascadeWindow(WScreen
*scr
, WWindow
*wwin
, int *x_ret
, int *y_ret
,
347 unsigned int width
, unsigned int height
, int h
)
349 unsigned int extra_height
;
350 WArea usableArea
= scr
->totalUsableArea
;
353 extra_height
= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
;
355 extra_height
= 24; /* random value */
357 *x_ret
= h
* scr
->cascade_index
+ X_ORIGIN(scr
);
358 *y_ret
= h
* scr
->cascade_index
+ Y_ORIGIN(scr
);
359 height
+= extra_height
;
361 if (width
+ *x_ret
> usableArea
.x2
|| height
+ *y_ret
> usableArea
.y2
) {
362 scr
->cascade_index
= 0;
363 *x_ret
= X_ORIGIN(scr
);
364 *y_ret
= Y_ORIGIN(scr
);
370 PlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
371 unsigned width
, unsigned height
)
373 WScreen
*scr
= wwin
->screen_ptr
;
374 int h
= scr
->title_font
->height
+TITLEBAR_EXTRA_HEIGHT
;
376 switch (wPreferences
.window_placement
) {
378 InteractivePlaceWindow(wwin
, x_ret
, y_ret
, width
, height
);
382 if (smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 0)) {
384 } else if (smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
, 1)) {
387 /* there isn't a break here, because if we fail, it should fall
388 through to cascade placement, as people who want tiling want
389 automagicness aren't going to want to place their window */
392 if (wPreferences
.window_placement
== WPM_SMART
)
393 scr
->cascade_index
++;
395 cascadeWindow(scr
, wwin
, x_ret
, y_ret
, width
, height
, h
);
397 if (wPreferences
.window_placement
== WPM_CASCADE
)
398 scr
->cascade_index
++;
403 int w
, h
, extra_height
;
404 WArea usableArea
= scr
->totalUsableArea
;
407 extra_height
= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
+ 2;
409 extra_height
= 24; /* random value */
411 w
= ((usableArea
.x2
-X_ORIGIN(scr
)) - width
);
412 h
= ((usableArea
.y2
-Y_ORIGIN(scr
)) - height
- extra_height
);
415 *x_ret
= X_ORIGIN(scr
) + rand()%w
;
416 *y_ret
= Y_ORIGIN(scr
) + rand()%h
;
422 puts("Invalid window placement!!!");
428 if (*x_ret
+ width
> scr
->scr_width
)
429 *x_ret
= scr
->scr_width
- width
;
433 if (*y_ret
+ height
> scr
->scr_height
)
434 *y_ret
= scr
->scr_height
- height
;