Code update for Window Maker version 0.50.0
[wmaker-crm.git] / src / placement.c
blobee9e6433d6ae51bc7904b6c289aa4eea1e8d9616
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 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
55 extern void
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.
63 static Bool
64 iconPosition(WCoreWindow *wcore, int sx1, int sy1, int sx2, int sy2,
65 int workspace, int *retX, int *retY)
67 void *parent;
68 int ok = 0;
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;
77 ok = 1;
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;
87 ok = 1;
91 * Check if it is inside the screen.
93 if (ok) {
94 if (*retX < sx1-wPreferences.icon_size)
95 ok = 0;
96 else if (*retX > sx2)
97 ok = 0;
99 if (*retY < sy1-wPreferences.icon_size)
100 ok = 0;
101 else if (*retY > sy2)
102 ok = 0;
105 return ok;
111 void
112 PlaceIcon(WScreen *scr, int *x_ret, int *y_ret)
114 int pf; /* primary axis */
115 int sf; /* secondary axis */
116 int fullW;
117 int fullH;
118 char *map;
119 int pi, si;
120 WCoreWindow *obj;
121 int sx1, sx2, sy1, sy2; /* screen boundary */
122 int sw, sh;
123 int xo, yo;
124 int xs, ys;
125 int x, y;
126 int isize = wPreferences.icon_size;
127 int done = 0;
128 int level;
131 * Find out screen boundaries.
133 sx1 = 0;
134 sy1 = 0;
135 sx2 = scr->scr_width;
136 sy2 = scr->scr_height;
137 if (scr->dock) {
138 if (scr->dock->on_right_side)
139 sx2 -= isize + DOCK_EXTRA_SPACE;
140 else
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) {
151 pf = fullH;
152 sf = fullW;
153 } else {
154 pf = fullW;
155 sf = fullH;
157 if (wPreferences.icon_yard & IY_RIGHT) {
158 xo = sx2 - isize;
159 xs = -1;
160 } else {
161 xo = sx1;
162 xs = 1;
164 if (wPreferences.icon_yard & IY_TOP) {
165 yo = sy1;
166 ys = 1;
167 } else {
168 yo = sy2 - isize;
169 ys = -1;
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];
188 while (obj) {
189 int x, y;
191 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace,
192 &x, &y)) {
193 int xdi, ydi; /* rounded down */
194 int xui, yui; /* rounded up */
196 xdi = x/isize;
197 ydi = y/isize;
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;
209 * Default position
211 *x_ret = 0;
212 *y_ret = 0;
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);
222 } else {
223 x = xo + xs*(pi*isize);
224 y = yo + ys*(si*isize);
226 if (!map[INDEX(x/isize, y/isize)]) {
227 *x_ret = x;
228 *y_ret = y;
229 done = 1;
230 break;
233 if (done)
234 break;
237 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;
340 return loc_ok;
344 static void
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;
351 if (wwin->frame)
352 extra_height = wwin->frame->top_width + wwin->frame->bottom_width;
353 else
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);
368 void
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) {
376 case WPM_MANUAL:
377 InteractivePlaceWindow(wwin, x_ret, y_ret, width, height);
378 break;
380 case WPM_SMART:
381 if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 0)) {
382 break;
383 } else if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 1)) {
384 break;
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 */
390 case WPM_CASCADE:
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++;
398 break;
400 case WPM_RANDOM:
402 int w, h, extra_height;
403 WArea usableArea = scr->totalUsableArea;
405 if (wwin->frame)
406 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
407 else
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);
414 if (w<1) w = 1;
415 if (h<1) h = 1;
416 *x_ret = X_ORIGIN(scr) + rand()%w;
417 *y_ret = Y_ORIGIN(scr) + rand()%h;
419 break;
421 #ifdef DEBUG
422 default:
423 puts("Invalid window placement!!!");
424 *x_ret = 0;
425 *y_ret = 0;
426 #endif
429 if (*x_ret + width > scr->scr_width)
430 *x_ret = scr->scr_width - width;
431 if (*x_ret < 0)
432 *x_ret = 0;
434 if (*y_ret + height > scr->scr_height)
435 *y_ret = scr->scr_height - height;
436 if (*y_ret < 0)
437 *y_ret = 0;