This update includes the 0.20.3pre3 code
[wmaker-crm.git] / src / placement.c
blob4a1d3b53674ef139474ba214d869b2ff02a29e13
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 wPreferences.window_place_origin.x
45 #define Y_ORIGIN wPreferences.window_place_origin.y
49 * interactive window placement is in moveres.c
52 extern void
53 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret);
57 * Returns True if it is an icon and is in this workspace.
59 static Bool
60 iconPosition(WCoreWindow *wcore, int sx1, int sy1, int sx2, int sy2,
61 int workspace, int *retX, int *retY)
63 void *parent;
64 int ok = 0;
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;
73 ok = 1;
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;
83 ok = 1;
87 * Check if it is inside the screen.
89 if (ok) {
90 if (*retX < sx1-wPreferences.icon_size)
91 ok = 0;
92 else if (*retX > sx2)
93 ok = 0;
95 if (*retY < sy1-wPreferences.icon_size)
96 ok = 0;
97 else if (*retY > sy2)
98 ok = 0;
101 return ok;
107 void
108 PlaceIcon(WScreen *scr, int *x_ret, int *y_ret)
110 int pf; /* primary axis */
111 int sf; /* secondary axis */
112 int fullW;
113 int fullH;
114 char *map;
115 int pi, si;
116 WCoreWindow *obj;
117 int sx1, sx2, sy1, sy2; /* screen boundary */
118 int sw, sh;
119 int xo, yo;
120 int xs, ys;
121 int x, y;
122 int isize = wPreferences.icon_size;
123 int done = 0;
124 int level;
127 * Find out screen boundaries.
129 sx1 = 0;
130 sy1 = 0;
131 sx2 = scr->scr_width;
132 sy2 = scr->scr_height;
133 if (scr->dock) {
134 if (scr->dock->on_right_side)
135 sx2 -= isize + DOCK_EXTRA_SPACE;
136 else
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) {
147 pf = fullH;
148 sf = fullW;
149 } else {
150 pf = fullW;
151 sf = fullH;
153 if (wPreferences.icon_yard & IY_RIGHT) {
154 xo = sx2 - isize;
155 xs = -1;
156 } else {
157 xo = sx1;
158 xs = 1;
160 if (wPreferences.icon_yard & IY_TOP) {
161 yo = sy1;
162 ys = 1;
163 } else {
164 yo = sy2 - isize;
165 ys = -1;
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];
184 while (obj) {
185 int x, y;
187 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace,
188 &x, &y)) {
189 int xdi, ydi; /* rounded down */
190 int xui, yui; /* rounded up */
192 xdi = x/isize;
193 ydi = y/isize;
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;
205 * Default position
207 *x_ret = 0;
208 *y_ret = 0;
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);
218 } else {
219 x = xo + xs*(pi*isize);
220 y = yo + ys*(si*isize);
222 if (!map[INDEX(x/isize, y/isize)]) {
223 *x_ret = x;
224 *y_ret = y;
225 done = 1;
226 break;
229 if (done)
230 break;
233 free(map);
239 static int
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;
246 int swidth, sx;
247 WWindow *test_window;
248 int extra_height;
250 if (wwin->frame)
251 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
252 else
253 extra_height = 24; /* random value */
255 swidth = scr->scr_width;
256 sx = X_ORIGIN;
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)) {
270 test_x = sx;
272 while (((test_x + width) < swidth) && (!loc_ok)) {
274 loc_ok = True;
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;
282 else
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)))) {
294 loc_ok = False;
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;
306 else
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)))) {
318 loc_ok = False;
320 test_window = test_window->prev;
322 if (loc_ok == True) {
323 *x_ret = test_x;
324 *y_ret = test_y;
325 break;
327 test_x += PLACETEST_HSTEP;
329 test_y += PLACETEST_VSTEP;
331 return loc_ok;
335 static void
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;
341 int xoffset = 0;
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;
347 } else {
348 xoffset = wPreferences.icon_size;
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;
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;
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);
379 break;
381 case WPM_SMART:
382 if (smartPlaceWindow(wwin, x_ret, y_ret, width, height))
383 break;
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 */
388 case WPM_CASCADE:
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) {
398 int x2;
400 x2 = *x_ret + width;
401 if (scr->dock->on_right_side
402 && x2 > scr->scr_width - wPreferences.icon_size -
403 DOCK_EXTRA_SPACE)
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;
410 break;
412 case WPM_RANDOM:
414 int w, h;
416 w = (scr->scr_width - width);
417 h = (scr->scr_height - height);
418 if (w<1) w = 1;
419 if (h<1) h = 1;
420 *x_ret = rand()%w;
421 *y_ret = rand()%h;
422 if (scr->dock && !scr->dock->lowered) {
423 int x2;
425 x2 = *x_ret + width;
426 if (scr->dock->on_right_side
427 && x2 > scr->scr_width - wPreferences.icon_size -
428 DOCK_EXTRA_SPACE)
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;
436 break;
438 #ifdef DEBUG
439 default:
440 puts("Invalid window placement!!!");
441 *x_ret = 0;
442 *y_ret = 0;
443 #endif
446 if (*x_ret < 0)
447 *x_ret = 0;
448 else if (*x_ret + width > scr->scr_width)
449 *x_ret = scr->scr_width - width;
451 if (*y_ret < 0)
452 *y_ret = 0;
453 else if (*y_ret + height > scr->scr_height)
454 *y_ret = scr->scr_height - height;