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 wPreferences.window_place_origin.x
45 #define Y_ORIGIN wPreferences.window_place_origin.y
49 * interactive window placement is in moveres.c
53 InteractivePlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
);
57 * Returns True if it is an icon and is in this workspace.
60 iconPosition(WCoreWindow
*wcore
, int sx1
, int sy1
, int sx2
, int sy2
,
61 int workspace
, int *retX
, int *retY
)
66 parent
= wcore
->descriptor
.parent
;
68 /* if it is an application icon */
69 if (wcore
->descriptor
.parent_type
== WCLASS_APPICON
) {
70 *retX
= ((WAppIcon
*)parent
)->x_pos
;
71 *retY
= ((WAppIcon
*)parent
)->y_pos
;
74 } else if (wcore
->descriptor
.parent_type
== WCLASS_MINIWINDOW
&&
75 (((WIcon
*)parent
)->owner
->frame
->workspace
==workspace
76 || ((WIcon
*)parent
)->owner
->window_flags
.omnipresent
77 || wPreferences
.sticky_icons
)
78 && !((WIcon
*)parent
)->owner
->flags
.hidden
) {
80 *retX
= ((WIcon
*)parent
)->owner
->icon_x
;
81 *retY
= ((WIcon
*)parent
)->owner
->icon_y
;
87 * Check if it is inside the screen.
90 if (*retX
< sx1
-wPreferences
.icon_size
)
95 if (*retY
< sy1
-wPreferences
.icon_size
)
108 PlaceIcon(WScreen
*scr
, int *x_ret
, int *y_ret
)
110 int pf
; /* primary axis */
111 int sf
; /* secondary axis */
117 int sx1
, sx2
, sy1
, sy2
; /* screen boundary */
122 int isize
= wPreferences
.icon_size
;
127 * Find out screen boundaries.
131 sx2
= scr
->scr_width
;
132 sy2
= scr
->scr_height
;
134 if (scr
->dock
->on_right_side
)
135 sx2
-= isize
+ DOCK_EXTRA_SPACE
;
137 sx1
+= isize
+ DOCK_EXTRA_SPACE
;
140 sw
= isize
* (scr
->scr_width
/isize
);
141 sh
= isize
* (scr
->scr_height
/isize
);
142 fullW
= (sx2
-sx1
)/isize
;
143 fullH
= (sy2
-sy1
)/isize
;
145 /* icon yard boundaries */
146 if (wPreferences
.icon_yard
& IY_VERT
) {
153 if (wPreferences
.icon_yard
& IY_RIGHT
) {
160 if (wPreferences
.icon_yard
& IY_TOP
) {
169 * Create a map with the occupied slots. 1 means the slot is used
170 * or at least partially used.
171 * The slot usage can be optimized by only marking fully used slots
172 * or slots that have most of it covered.
173 * Space usage is worse than the fvwm algorithm (used in the old version)
174 * but complexity is much better (faster) than it.
176 map
= wmalloc((sw
+2) * (sh
+2));
177 memset(map
, 0, (sw
+2) * (sh
+2));
179 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
181 for (level
= WMNormalLevel
; level
>= WMDesktopLevel
; level
--) {
182 obj
= scr
->stacking_list
[level
];
187 if (iconPosition(obj
, sx1
, sy1
, sx2
, sy2
, scr
->current_workspace
,
189 int xdi
, ydi
; /* rounded down */
190 int xui
, yui
; /* rounded up */
194 xui
= (x
+isize
/2)/isize
;
195 yui
= (y
+isize
/2)/isize
;
196 map
[INDEX(xdi
,ydi
)] = 1;
197 map
[INDEX(xdi
,yui
)] = 1;
198 map
[INDEX(xui
,ydi
)] = 1;
199 map
[INDEX(xui
,yui
)] = 1;
201 obj
= obj
->stacking
->under
;
211 * Look for an empty slot
213 for (si
=0; si
<sf
; si
++) {
214 for (pi
=0; pi
<pf
; pi
++) {
215 if (wPreferences
.icon_yard
& IY_VERT
) {
216 x
= xo
+ xs
*(si
*isize
);
217 y
= yo
+ ys
*(pi
*isize
);
219 x
= xo
+ xs
*(pi
*isize
);
220 y
= yo
+ ys
*(si
*isize
);
222 if (!map
[INDEX(x
/isize
, y
/isize
)]) {
240 smartPlaceWindow(WWindow
*wwin
, int *x_ret
, int *y_ret
,
241 unsigned int width
, unsigned int height
)
243 WScreen
*scr
= wwin
->screen_ptr
;
244 int test_x
= 0, test_y
= Y_ORIGIN
;
245 int loc_ok
= False
, tw
,tx
,ty
,th
;
247 WWindow
*test_window
;
251 extra_height
= wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
+ 2;
253 extra_height
= 24; /* random value */
255 swidth
= scr
->scr_width
;
257 if (scr
->dock
&& !scr
->dock
->lowered
) {
258 if (scr
->dock
->on_right_side
)
259 swidth
-= wPreferences
.icon_size
+ DOCK_EXTRA_SPACE
;
260 else if (X_ORIGIN
< wPreferences
.icon_size
+ DOCK_EXTRA_SPACE
)
261 sx
+= wPreferences
.icon_size
+ DOCK_EXTRA_SPACE
- X_ORIGIN
;
264 /* this was based on fvwm2's smart placement */
266 height
+= extra_height
;
268 while (((test_y
+ height
) < (scr
->scr_height
)) && (!loc_ok
)) {
272 while (((test_x
+ width
) < swidth
) && (!loc_ok
)) {
275 test_window
= scr
->focused_window
;
277 while ((test_window
!= (WWindow
*) 0) && (loc_ok
== True
)) {
279 tw
= test_window
->client
.width
;
280 if (test_window
->flags
.shaded
)
281 th
= test_window
->frame
->top_width
;
283 th
= test_window
->client
.height
+extra_height
;
284 tx
= test_window
->frame_x
;
285 ty
= test_window
->frame_y
;
287 if((tx
<(test_x
+width
))&&((tx
+ tw
) > test_x
)&&
288 (ty
<(test_y
+height
))&&((ty
+ th
)>test_y
)&&
289 (test_window
->flags
.mapped
||
290 (test_window
->flags
.shaded
&&
291 !(test_window
->flags
.miniaturized
||
292 test_window
->flags
.hidden
)))) {
296 test_window
= test_window
->next
;
299 test_window
= scr
->focused_window
;
301 while ((test_window
!= (WWindow
*) 0) && (loc_ok
== True
)) {
303 tw
= test_window
->client
.width
;
304 if (test_window
->flags
.shaded
)
305 th
= test_window
->frame
->top_width
;
307 th
= test_window
->client
.height
+extra_height
;
308 tx
= test_window
->frame_x
;
309 ty
= test_window
->frame_y
;
311 if((tx
<(test_x
+width
))&&((tx
+ tw
) > test_x
)&&
312 (ty
<(test_y
+height
))&&((ty
+ th
)>test_y
)&&
313 (test_window
->flags
.mapped
||
314 (test_window
->flags
.shaded
&&
315 !(test_window
->flags
.miniaturized
||
316 test_window
->flags
.hidden
)))) {
320 test_window
= test_window
->prev
;
322 if (loc_ok
== True
) {
327 test_x
+= PLACETEST_HSTEP
;
329 test_y
+= PLACETEST_VSTEP
;
336 cascadeWindow(WScreen
*scr
, WWindow
*wwin
, int *x_ret
, int *y_ret
,
337 unsigned int width
, unsigned int height
, int h
)
339 unsigned int extra_height
;
340 unsigned int scr_width
;
343 scr_width
= scr
->scr_width
;
344 if (scr
->dock
&& !scr
->dock
->lowered
) {
345 if (scr
->dock
->on_right_side
) {
346 scr_width
-= wPreferences
.icon_size
;
348 xoffset
= wPreferences
.icon_size
;
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
;
358 *y_ret
= h
* scr
->cascade_index
+ Y_ORIGIN
;
359 height
+= extra_height
;
361 if (width
+ *x_ret
> scr_width
|| height
+ *y_ret
> scr
->scr_height
) {
362 scr
->cascade_index
= 0;
363 *x_ret
= h
*scr
->cascade_index
+ X_ORIGIN
;
364 *y_ret
= h
*scr
->cascade_index
+ Y_ORIGIN
;
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
);
382 if (smartPlaceWindow(wwin
, x_ret
, y_ret
, width
, height
))
384 /* there isn't a break here, because if we fail, it should fall
385 through to cascade placement, as people who want tiling want
386 automagicness aren't going to want to place their window */
389 if (wPreferences
.window_placement
== WPM_SMART
)
390 scr
->cascade_index
++;
392 cascadeWindow(scr
, wwin
, x_ret
, y_ret
, width
, height
, h
);
394 if (wPreferences
.window_placement
== WPM_CASCADE
)
395 scr
->cascade_index
++;
397 if (scr
->dock
&& !scr
->dock
->lowered
) {
401 if (scr
->dock
->on_right_side
402 && x2
> scr
->scr_width
- wPreferences
.icon_size
-
404 *x_ret
= scr
->scr_width
- width
405 - wPreferences
.icon_size
- DOCK_EXTRA_SPACE
;
406 else if (!scr
->dock
->on_right_side
&&
407 X_ORIGIN
< wPreferences
.icon_size
+ DOCK_EXTRA_SPACE
)
408 *x_ret
+= wPreferences
.icon_size
+ DOCK_EXTRA_SPACE
- X_ORIGIN
;
416 w
= (scr
->scr_width
- width
);
417 h
= (scr
->scr_height
- height
);
422 if (scr
->dock
&& !scr
->dock
->lowered
) {
426 if (scr
->dock
->on_right_side
427 && x2
> scr
->scr_width
- wPreferences
.icon_size
-
429 *x_ret
= scr
->scr_width
- width
430 - wPreferences
.icon_size
- DOCK_EXTRA_SPACE
;
431 else if (!scr
->dock
->on_right_side
432 && *x_ret
< wPreferences
.icon_size
)
433 *x_ret
= wPreferences
.icon_size
+ DOCK_EXTRA_SPACE
;
440 puts("Invalid window placement!!!");
448 else if (*x_ret
+ width
> scr
->scr_width
)
449 *x_ret
= scr
->scr_width
- width
;
453 else if (*y_ret
+ height
> scr
->scr_height
)
454 *y_ret
= scr
->scr_height
- height
;