fixed cosmetic bug in geom. dpy window for 8bpp
[wmaker-crm.git] / src / placement.c
blob638330d0b9bc5607542c7a8532055753a77f114d
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 "appicon.h"
36 #include "actions.h"
37 #include "funcs.h"
38 #include "application.h"
39 #include "appicon.h"
40 #include "dock.h"
42 #include "list.h"
44 extern WPreferences wPreferences;
47 #define X_ORIGIN(scr) WMAX((scr)->totalUsableArea.x1,\
48 wPreferences.window_place_origin.x)
50 #define Y_ORIGIN(scr) WMAX((scr)->totalUsableArea.y1,\
51 wPreferences.window_place_origin.y)
55 * interactive window placement is in moveres.c
58 extern void
59 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
60 unsigned width, unsigned height);
64 * Returns True if it is an icon and is in this workspace.
66 static Bool
67 iconPosition(WCoreWindow *wcore, int sx1, int sy1, int sx2, int sy2,
68 int workspace, int *retX, int *retY)
70 void *parent;
71 int ok = 0;
73 parent = wcore->descriptor.parent;
75 /* if it is an application icon */
76 if (wcore->descriptor.parent_type == WCLASS_APPICON
77 && !((WAppIcon*)parent)->docked) {
78 *retX = ((WAppIcon*)parent)->x_pos;
79 *retY = ((WAppIcon*)parent)->y_pos;
81 ok = 1;
82 } else if (wcore->descriptor.parent_type == WCLASS_MINIWINDOW &&
83 (((WIcon*)parent)->owner->frame->workspace == workspace
84 || IS_OMNIPRESENT(((WIcon*)parent)->owner)
85 || wPreferences.sticky_icons)
86 && ((WIcon*)parent)->mapped
87 && (!((WIcon*)parent)->owner->flags.hidden
88 || wcore->screen_ptr->flags.startup)) {
90 *retX = ((WIcon*)parent)->owner->icon_x;
91 *retY = ((WIcon*)parent)->owner->icon_y;
93 ok = 1;
94 } else if (wcore->descriptor.parent_type == WCLASS_WINDOW
95 && (((WWindow*)parent)->flags.icon_moved
96 || ((WWindow*)parent)->flags.hidden)
97 && (((WWindow*)parent)->frame->workspace == workspace
98 || IS_OMNIPRESENT((WWindow*)parent)
99 || wPreferences.sticky_icons)) {
100 *retX = ((WWindow*)parent)->icon_x;
101 *retY = ((WWindow*)parent)->icon_y;
103 ok = 1;
108 * Check if it is inside the screen.
110 if (ok) {
111 if (*retX < sx1-wPreferences.icon_size)
112 ok = 0;
113 else if (*retX > sx2)
114 ok = 0;
116 if (*retY < sy1-wPreferences.icon_size)
117 ok = 0;
118 else if (*retY > sy2)
119 ok = 0;
122 return ok;
128 void
129 PlaceIcon(WScreen *scr, int *x_ret, int *y_ret)
131 int pf; /* primary axis */
132 int sf; /* secondary axis */
133 int fullW;
134 int fullH;
135 char *map;
136 int pi, si;
137 WCoreWindow *obj;
138 int sx1, sx2, sy1, sy2; /* screen boundary */
139 int sw, sh;
140 int xo, yo;
141 int xs, ys;
142 int x, y;
143 int isize = wPreferences.icon_size;
144 int done = 0;
145 int level;
148 * Find out screen boundaries.
150 sx1 = 0;
151 sy1 = 0;
152 sx2 = scr->scr_width;
153 sy2 = scr->scr_height;
154 if (scr->dock) {
155 if (scr->dock->on_right_side)
156 sx2 -= isize + DOCK_EXTRA_SPACE;
157 else
158 sx1 += isize + DOCK_EXTRA_SPACE;
161 sw = isize * (scr->scr_width/isize);
162 sh = isize * (scr->scr_height/isize);
163 fullW = (sx2-sx1)/isize;
164 fullH = (sy2-sy1)/isize;
166 /* icon yard boundaries */
167 if (wPreferences.icon_yard & IY_VERT) {
168 pf = fullH;
169 sf = fullW;
170 } else {
171 pf = fullW;
172 sf = fullH;
174 if (wPreferences.icon_yard & IY_RIGHT) {
175 xo = sx2 - isize;
176 xs = -1;
177 } else {
178 xo = sx1;
179 xs = 1;
181 if (wPreferences.icon_yard & IY_TOP) {
182 yo = sy1;
183 ys = 1;
184 } else {
185 yo = sy2 - isize;
186 ys = -1;
190 * Create a map with the occupied slots. 1 means the slot is used
191 * or at least partially used.
192 * The slot usage can be optimized by only marking fully used slots
193 * or slots that have most of it covered.
194 * Space usage is worse than the fvwm algorithm (used in the old version)
195 * but complexity is much better (faster) than it.
197 map = wmalloc((sw+2) * (sh+2));
198 memset(map, 0, (sw+2) * (sh+2));
200 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
202 for (level = MAX_WINDOW_LEVELS-1; level >= WMDesktopLevel; level--) {
203 obj = scr->stacking_list[level];
205 while (obj) {
206 int x, y;
208 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace,
209 &x, &y)) {
210 int xdi, ydi; /* rounded down */
211 int xui, yui; /* rounded up */
213 xdi = x/isize;
214 ydi = y/isize;
215 xui = (x+isize/2)/isize;
216 yui = (y+isize/2)/isize;
217 map[INDEX(xdi,ydi)] = 1;
218 map[INDEX(xdi,yui)] = 1;
219 map[INDEX(xui,ydi)] = 1;
220 map[INDEX(xui,yui)] = 1;
222 obj = obj->stacking->under;
226 * Default position
228 *x_ret = 0;
229 *y_ret = 0;
232 * Look for an empty slot
234 for (si=0; si<sf; si++) {
235 for (pi=0; pi<pf; pi++) {
236 if (wPreferences.icon_yard & IY_VERT) {
237 x = xo + xs*(si*isize);
238 y = yo + ys*(pi*isize);
239 } else {
240 x = xo + xs*(pi*isize);
241 y = yo + ys*(si*isize);
243 if (!map[INDEX(x/isize, y/isize)]) {
244 *x_ret = x;
245 *y_ret = y;
246 done = 1;
247 break;
250 if (done)
251 break;
254 free(map);
258 static Bool
259 smartPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
260 unsigned int width, unsigned int height, int tryCount)
262 WScreen *scr = wwin->screen_ptr;
263 int test_x = 0, test_y = Y_ORIGIN(scr);
264 int loc_ok = False, tw,tx,ty,th;
265 int swidth, sx;
266 WWindow *test_window;
267 int extra_height;
268 WArea usableArea = scr->totalUsableArea;
270 if (wwin->frame)
271 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
272 else
273 extra_height = 24; /* random value */
275 swidth = usableArea.x2-usableArea.x1;
276 sx = X_ORIGIN(scr);
278 /* this was based on fvwm2's smart placement */
280 height += extra_height;
282 while (((test_y + height) < (scr->scr_height)) && (!loc_ok)) {
284 test_x = sx;
286 while (((test_x + width) < swidth) && (!loc_ok)) {
288 loc_ok = True;
289 test_window = scr->focused_window;
291 while ((test_window != NULL) && (loc_ok == True)) {
293 if (test_window->frame->core->stacking->window_level
294 < WMNormalLevel && tryCount > 0) {
295 test_window = test_window->next;
296 continue;
298 #if 0
299 tw = test_window->client.width;
300 if (test_window->flags.shaded)
301 th = test_window->frame->top_width;
302 else
303 th = test_window->client.height + extra_height;
304 #else
305 tw = test_window->frame->core->width;
306 th = test_window->frame->core->height;
307 #endif
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->next;
323 test_window = scr->focused_window;
325 while ((test_window != NULL) && (loc_ok == True)) {
327 if (test_window->frame->core->stacking->window_level
328 < WMNormalLevel && tryCount > 0) {
329 test_window = test_window->prev;
330 continue;
332 #if 0
333 tw = test_window->client.width;
334 if (test_window->flags.shaded)
335 th = test_window->frame->top_width;
336 else
337 th = test_window->client.height + extra_height;
338 #else
339 tw = test_window->frame->core->width;
340 th = test_window->frame->core->height;
341 #endif
342 tx = test_window->frame_x;
343 ty = test_window->frame_y;
345 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
346 (ty < (test_y + height)) && ((ty + th) > test_y) &&
347 (test_window->flags.mapped ||
348 (test_window->flags.shaded &&
349 !(test_window->flags.miniaturized ||
350 test_window->flags.hidden)))) {
352 loc_ok = False;
354 test_window = test_window->prev;
356 if (loc_ok == True) {
357 *x_ret = test_x;
358 *y_ret = test_y;
359 break;
361 test_x += PLACETEST_HSTEP;
363 test_y += PLACETEST_VSTEP;
366 return loc_ok;
370 static void
371 cascadeWindow(WScreen *scr, WWindow *wwin, int *x_ret, int *y_ret,
372 unsigned int width, unsigned int height, int h)
374 unsigned int extra_height;
375 WArea usableArea = scr->totalUsableArea;
377 if (wwin->frame)
378 extra_height = wwin->frame->top_width + wwin->frame->bottom_width;
379 else
380 extra_height = 24; /* random value */
382 *x_ret = h * scr->cascade_index + X_ORIGIN(scr);
383 *y_ret = h * scr->cascade_index + Y_ORIGIN(scr);
384 height += extra_height;
386 if (width + *x_ret > usableArea.x2 || height + *y_ret > usableArea.y2) {
387 scr->cascade_index = 0;
388 *x_ret = X_ORIGIN(scr);
389 *y_ret = Y_ORIGIN(scr);
394 void
395 PlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
396 unsigned width, unsigned height)
398 WScreen *scr = wwin->screen_ptr;
399 int h = WMFontHeight(scr->title_font) + TITLEBAR_EXTRA_HEIGHT;
401 switch (wPreferences.window_placement) {
402 case WPM_MANUAL:
403 InteractivePlaceWindow(wwin, x_ret, y_ret, width, height);
404 break;
406 case WPM_SMART:
407 if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 0)) {
408 break;
409 } else if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 1)) {
410 break;
412 /* there isn't a break here, because if we fail, it should fall
413 through to cascade placement, as people who want tiling want
414 automagicness aren't going to want to place their window */
416 case WPM_CASCADE:
417 if (wPreferences.window_placement == WPM_SMART)
418 scr->cascade_index++;
420 cascadeWindow(scr, wwin, x_ret, y_ret, width, height, h);
422 if (wPreferences.window_placement == WPM_CASCADE)
423 scr->cascade_index++;
424 break;
426 case WPM_RANDOM:
428 int w, h, extra_height;
429 WArea usableArea = scr->totalUsableArea;
431 if (wwin->frame)
432 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
433 else
434 extra_height = 24; /* random value */
436 w = ((usableArea.x2-X_ORIGIN(scr)) - width);
437 h = ((usableArea.y2-Y_ORIGIN(scr)) - height - extra_height);
438 if (w<1) w = 1;
439 if (h<1) h = 1;
440 *x_ret = X_ORIGIN(scr) + rand()%w;
441 *y_ret = Y_ORIGIN(scr) + rand()%h;
443 break;
445 #ifdef DEBUG
446 default:
447 puts("Invalid window placement!!!");
448 *x_ret = 0;
449 *y_ret = 0;
450 #endif
453 if (*x_ret + width > scr->scr_width)
454 *x_ret = scr->scr_width - width;
455 if (*x_ret < 0)
456 *x_ret = 0;
458 if (*y_ret + height > scr->scr_height)
459 *y_ret = scr->scr_height - height;
460 if (*y_ret < 0)
461 *y_ret = 0;