Update to Window Maker 0.50.2
[wmaker-crm.git] / src / placement.c
bloba90074589fb398fd44cf010afe50f81dd7f66e7f
1 /* placement.c - window and icon placement on screen
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
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,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "WindowMaker.h"
31 #include "wcore.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "icon.h"
35 #include "actions.h"
36 #include "funcs.h"
37 #include "application.h"
38 #include "appicon.h"
39 #include "dock.h"
41 #include "list.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
57 extern void
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.
65 static Bool
66 iconPosition(WCoreWindow *wcore, int sx1, int sy1, int sx2, int sy2,
67 int workspace, int *retX, int *retY)
69 void *parent;
70 int ok = 0;
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;
79 ok = 1;
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;
89 ok = 1;
93 * Check if it is inside the screen.
95 if (ok) {
96 if (*retX < sx1-wPreferences.icon_size)
97 ok = 0;
98 else if (*retX > sx2)
99 ok = 0;
101 if (*retY < sy1-wPreferences.icon_size)
102 ok = 0;
103 else if (*retY > sy2)
104 ok = 0;
107 return ok;
113 void
114 PlaceIcon(WScreen *scr, int *x_ret, int *y_ret)
116 int pf; /* primary axis */
117 int sf; /* secondary axis */
118 int fullW;
119 int fullH;
120 char *map;
121 int pi, si;
122 WCoreWindow *obj;
123 int sx1, sx2, sy1, sy2; /* screen boundary */
124 int sw, sh;
125 int xo, yo;
126 int xs, ys;
127 int x, y;
128 int isize = wPreferences.icon_size;
129 int done = 0;
130 int level;
133 * Find out screen boundaries.
135 sx1 = 0;
136 sy1 = 0;
137 sx2 = scr->scr_width;
138 sy2 = scr->scr_height;
139 if (scr->dock) {
140 if (scr->dock->on_right_side)
141 sx2 -= isize + DOCK_EXTRA_SPACE;
142 else
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) {
153 pf = fullH;
154 sf = fullW;
155 } else {
156 pf = fullW;
157 sf = fullH;
159 if (wPreferences.icon_yard & IY_RIGHT) {
160 xo = sx2 - isize;
161 xs = -1;
162 } else {
163 xo = sx1;
164 xs = 1;
166 if (wPreferences.icon_yard & IY_TOP) {
167 yo = sy1;
168 ys = 1;
169 } else {
170 yo = sy2 - isize;
171 ys = -1;
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];
190 while (obj) {
191 int x, y;
193 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace,
194 &x, &y)) {
195 int xdi, ydi; /* rounded down */
196 int xui, yui; /* rounded up */
198 xdi = x/isize;
199 ydi = y/isize;
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;
211 * Default position
213 *x_ret = 0;
214 *y_ret = 0;
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);
224 } else {
225 x = xo + xs*(pi*isize);
226 y = yo + ys*(si*isize);
228 if (!map[INDEX(x/isize, y/isize)]) {
229 *x_ret = x;
230 *y_ret = y;
231 done = 1;
232 break;
235 if (done)
236 break;
239 free(map);
243 static Bool
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;
250 int swidth, sx;
251 WWindow *test_window;
252 int extra_height;
253 WArea usableArea = scr->totalUsableArea;
255 if (wwin->frame)
256 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
257 else
258 extra_height = 24; /* random value */
260 swidth = usableArea.x2-usableArea.x1;
261 sx = X_ORIGIN(scr);
263 /* this was based on fvwm2's smart placement */
265 height += extra_height;
267 while (((test_y + height) < (scr->scr_height)) && (!loc_ok)) {
269 test_x = sx;
271 while (((test_x + width) < swidth) && (!loc_ok)) {
273 loc_ok = True;
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;
281 continue;
283 tw = test_window->client.width;
284 if (test_window->flags.shaded)
285 th = test_window->frame->top_width;
286 else
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)))) {
298 loc_ok = False;
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;
310 continue;
312 tw = test_window->client.width;
313 if (test_window->flags.shaded)
314 th = test_window->frame->top_width;
315 else
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)))) {
327 loc_ok = False;
329 test_window = test_window->prev;
331 if (loc_ok == True) {
332 *x_ret = test_x;
333 *y_ret = test_y;
334 break;
336 test_x += PLACETEST_HSTEP;
338 test_y += PLACETEST_VSTEP;
341 return loc_ok;
345 static void
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;
352 if (wwin->frame)
353 extra_height = wwin->frame->top_width + wwin->frame->bottom_width;
354 else
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);
369 void
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) {
377 case WPM_MANUAL:
378 InteractivePlaceWindow(wwin, x_ret, y_ret, width, height);
379 break;
381 case WPM_SMART:
382 if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 0)) {
383 break;
384 } else if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 1)) {
385 break;
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 */
391 case WPM_CASCADE:
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++;
399 break;
401 case WPM_RANDOM:
403 int w, h, extra_height;
404 WArea usableArea = scr->totalUsableArea;
406 if (wwin->frame)
407 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
408 else
409 extra_height = 24; /* random value */
411 w = ((usableArea.x2-X_ORIGIN(scr)) - width);
412 h = ((usableArea.y2-Y_ORIGIN(scr)) - height - extra_height);
413 if (w<1) w = 1;
414 if (h<1) h = 1;
415 *x_ret = X_ORIGIN(scr) + rand()%w;
416 *y_ret = Y_ORIGIN(scr) + rand()%h;
418 break;
420 #ifdef DEBUG
421 default:
422 puts("Invalid window placement!!!");
423 *x_ret = 0;
424 *y_ret = 0;
425 #endif
428 if (*x_ret + width > scr->scr_width)
429 *x_ret = scr->scr_width - width;
430 if (*x_ret < 0)
431 *x_ret = 0;
433 if (*y_ret + height > scr->scr_height)
434 *y_ret = scr->scr_height - height;
435 if (*y_ret < 0)
436 *y_ret = 0;