Update for 0.52.0. This is a test version, which brings the Appearance
[wmaker-crm.git] / src / placement.c
blobf74201ee27b8254509da4ef0b3255d39600a0110
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
85 || wcore->screen_ptr->flags.startup)) {
87 *retX = ((WIcon*)parent)->owner->icon_x;
88 *retY = ((WIcon*)parent)->owner->icon_y;
90 ok = 1;
91 } else if (wcore->descriptor.parent_type == WCLASS_WINDOW
92 && (((WWindow*)parent)->flags.icon_moved
93 || ((WWindow*)parent)->flags.hidden)
94 && (((WWindow*)parent)->frame->workspace == workspace
95 || IS_OMNIPRESENT((WWindow*)parent)
96 || wPreferences.sticky_icons)) {
97 *retX = ((WWindow*)parent)->icon_x;
98 *retY = ((WWindow*)parent)->icon_y;
100 ok = 1;
105 * Check if it is inside the screen.
107 if (ok) {
108 if (*retX < sx1-wPreferences.icon_size)
109 ok = 0;
110 else if (*retX > sx2)
111 ok = 0;
113 if (*retY < sy1-wPreferences.icon_size)
114 ok = 0;
115 else if (*retY > sy2)
116 ok = 0;
119 return ok;
125 void
126 PlaceIcon(WScreen *scr, int *x_ret, int *y_ret)
128 int pf; /* primary axis */
129 int sf; /* secondary axis */
130 int fullW;
131 int fullH;
132 char *map;
133 int pi, si;
134 WCoreWindow *obj;
135 int sx1, sx2, sy1, sy2; /* screen boundary */
136 int sw, sh;
137 int xo, yo;
138 int xs, ys;
139 int x, y;
140 int isize = wPreferences.icon_size;
141 int done = 0;
142 int level;
145 * Find out screen boundaries.
147 sx1 = 0;
148 sy1 = 0;
149 sx2 = scr->scr_width;
150 sy2 = scr->scr_height;
151 if (scr->dock) {
152 if (scr->dock->on_right_side)
153 sx2 -= isize + DOCK_EXTRA_SPACE;
154 else
155 sx1 += isize + DOCK_EXTRA_SPACE;
158 sw = isize * (scr->scr_width/isize);
159 sh = isize * (scr->scr_height/isize);
160 fullW = (sx2-sx1)/isize;
161 fullH = (sy2-sy1)/isize;
163 /* icon yard boundaries */
164 if (wPreferences.icon_yard & IY_VERT) {
165 pf = fullH;
166 sf = fullW;
167 } else {
168 pf = fullW;
169 sf = fullH;
171 if (wPreferences.icon_yard & IY_RIGHT) {
172 xo = sx2 - isize;
173 xs = -1;
174 } else {
175 xo = sx1;
176 xs = 1;
178 if (wPreferences.icon_yard & IY_TOP) {
179 yo = sy1;
180 ys = 1;
181 } else {
182 yo = sy2 - isize;
183 ys = -1;
187 * Create a map with the occupied slots. 1 means the slot is used
188 * or at least partially used.
189 * The slot usage can be optimized by only marking fully used slots
190 * or slots that have most of it covered.
191 * Space usage is worse than the fvwm algorithm (used in the old version)
192 * but complexity is much better (faster) than it.
194 map = wmalloc((sw+2) * (sh+2));
195 memset(map, 0, (sw+2) * (sh+2));
197 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
199 for (level = MAX_WINDOW_LEVELS-1; level >= WMDesktopLevel; level--) {
200 obj = scr->stacking_list[level];
202 while (obj) {
203 int x, y;
205 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace,
206 &x, &y)) {
207 int xdi, ydi; /* rounded down */
208 int xui, yui; /* rounded up */
210 xdi = x/isize;
211 ydi = y/isize;
212 xui = (x+isize/2)/isize;
213 yui = (y+isize/2)/isize;
214 map[INDEX(xdi,ydi)] = 1;
215 map[INDEX(xdi,yui)] = 1;
216 map[INDEX(xui,ydi)] = 1;
217 map[INDEX(xui,yui)] = 1;
219 obj = obj->stacking->under;
223 * Default position
225 *x_ret = 0;
226 *y_ret = 0;
229 * Look for an empty slot
231 for (si=0; si<sf; si++) {
232 for (pi=0; pi<pf; pi++) {
233 if (wPreferences.icon_yard & IY_VERT) {
234 x = xo + xs*(si*isize);
235 y = yo + ys*(pi*isize);
236 } else {
237 x = xo + xs*(pi*isize);
238 y = yo + ys*(si*isize);
240 if (!map[INDEX(x/isize, y/isize)]) {
241 *x_ret = x;
242 *y_ret = y;
243 done = 1;
244 break;
247 if (done)
248 break;
251 free(map);
255 static Bool
256 smartPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
257 unsigned int width, unsigned int height, int tryCount)
259 WScreen *scr = wwin->screen_ptr;
260 int test_x = 0, test_y = Y_ORIGIN(scr);
261 int loc_ok = False, tw,tx,ty,th;
262 int swidth, sx;
263 WWindow *test_window;
264 int extra_height;
265 WArea usableArea = scr->totalUsableArea;
267 if (wwin->frame)
268 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
269 else
270 extra_height = 24; /* random value */
272 swidth = usableArea.x2-usableArea.x1;
273 sx = X_ORIGIN(scr);
275 /* this was based on fvwm2's smart placement */
277 height += extra_height;
279 while (((test_y + height) < (scr->scr_height)) && (!loc_ok)) {
281 test_x = sx;
283 while (((test_x + width) < swidth) && (!loc_ok)) {
285 loc_ok = True;
286 test_window = scr->focused_window;
288 while ((test_window != NULL) && (loc_ok == True)) {
290 if (test_window->frame->core->stacking->window_level
291 < WMNormalLevel && tryCount > 0) {
292 test_window = test_window->next;
293 continue;
295 #if 0
296 tw = test_window->client.width;
297 if (test_window->flags.shaded)
298 th = test_window->frame->top_width;
299 else
300 th = test_window->client.height + extra_height;
301 #else
302 tw = test_window->frame->core->width;
303 th = test_window->frame->core->height;
304 #endif
305 tx = test_window->frame_x;
306 ty = test_window->frame_y;
308 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
309 (ty < (test_y + height)) && ((ty + th) > test_y) &&
310 (test_window->flags.mapped ||
311 (test_window->flags.shaded &&
312 !(test_window->flags.miniaturized ||
313 test_window->flags.hidden)))) {
315 loc_ok = False;
317 test_window = test_window->next;
320 test_window = scr->focused_window;
322 while ((test_window != NULL) && (loc_ok == True)) {
324 if (test_window->frame->core->stacking->window_level
325 < WMNormalLevel && tryCount > 0) {
326 test_window = test_window->prev;
327 continue;
329 #if 0
330 tw = test_window->client.width;
331 if (test_window->flags.shaded)
332 th = test_window->frame->top_width;
333 else
334 th = test_window->client.height + extra_height;
335 #else
336 tw = test_window->frame->core->width;
337 th = test_window->frame->core->height;
338 #endif
339 tx = test_window->frame_x;
340 ty = test_window->frame_y;
342 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
343 (ty < (test_y + height)) && ((ty + th) > test_y) &&
344 (test_window->flags.mapped ||
345 (test_window->flags.shaded &&
346 !(test_window->flags.miniaturized ||
347 test_window->flags.hidden)))) {
349 loc_ok = False;
351 test_window = test_window->prev;
353 if (loc_ok == True) {
354 *x_ret = test_x;
355 *y_ret = test_y;
356 break;
358 test_x += PLACETEST_HSTEP;
360 test_y += PLACETEST_VSTEP;
363 return loc_ok;
367 static void
368 cascadeWindow(WScreen *scr, WWindow *wwin, int *x_ret, int *y_ret,
369 unsigned int width, unsigned int height, int h)
371 unsigned int extra_height;
372 WArea usableArea = scr->totalUsableArea;
374 if (wwin->frame)
375 extra_height = wwin->frame->top_width + wwin->frame->bottom_width;
376 else
377 extra_height = 24; /* random value */
379 *x_ret = h * scr->cascade_index + X_ORIGIN(scr);
380 *y_ret = h * scr->cascade_index + Y_ORIGIN(scr);
381 height += extra_height;
383 if (width + *x_ret > usableArea.x2 || height + *y_ret > usableArea.y2) {
384 scr->cascade_index = 0;
385 *x_ret = X_ORIGIN(scr);
386 *y_ret = Y_ORIGIN(scr);
391 void
392 PlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
393 unsigned width, unsigned height)
395 WScreen *scr = wwin->screen_ptr;
396 int h = scr->title_font->height+TITLEBAR_EXTRA_HEIGHT;
398 switch (wPreferences.window_placement) {
399 case WPM_MANUAL:
400 InteractivePlaceWindow(wwin, x_ret, y_ret, width, height);
401 break;
403 case WPM_SMART:
404 if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 0)) {
405 break;
406 } else if (smartPlaceWindow(wwin, x_ret, y_ret, width, height, 1)) {
407 break;
409 /* there isn't a break here, because if we fail, it should fall
410 through to cascade placement, as people who want tiling want
411 automagicness aren't going to want to place their window */
413 case WPM_CASCADE:
414 if (wPreferences.window_placement == WPM_SMART)
415 scr->cascade_index++;
417 cascadeWindow(scr, wwin, x_ret, y_ret, width, height, h);
419 if (wPreferences.window_placement == WPM_CASCADE)
420 scr->cascade_index++;
421 break;
423 case WPM_RANDOM:
425 int w, h, extra_height;
426 WArea usableArea = scr->totalUsableArea;
428 if (wwin->frame)
429 extra_height = wwin->frame->top_width + wwin->frame->bottom_width + 2;
430 else
431 extra_height = 24; /* random value */
433 w = ((usableArea.x2-X_ORIGIN(scr)) - width);
434 h = ((usableArea.y2-Y_ORIGIN(scr)) - height - extra_height);
435 if (w<1) w = 1;
436 if (h<1) h = 1;
437 *x_ret = X_ORIGIN(scr) + rand()%w;
438 *y_ret = Y_ORIGIN(scr) + rand()%h;
440 break;
442 #ifdef DEBUG
443 default:
444 puts("Invalid window placement!!!");
445 *x_ret = 0;
446 *y_ret = 0;
447 #endif
450 if (*x_ret + width > scr->scr_width)
451 *x_ret = scr->scr_width - width;
452 if (*x_ret < 0)
453 *x_ret = 0;
455 if (*y_ret + height > scr->scr_height)
456 *y_ret = scr->scr_height - height;
457 if (*y_ret < 0)
458 *y_ret = 0;