Remove dead code ifdef'ed by GRADIENT_CLIP_ARROW
[wmaker-crm.git] / src / placement.c
blob4e1976500352807a4aade62a696f09779a076f56
1 /* placement.c - window and icon placement on screen
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <limits.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 "dock.h"
40 #include "xinerama.h"
42 extern WPreferences wPreferences;
44 #define X_ORIGIN WMAX(usableArea.x1,\
45 wPreferences.window_place_origin.x)
47 #define Y_ORIGIN WMAX(usableArea.y1,\
48 wPreferences.window_place_origin.y)
50 /* interactive window placement is in moveres.c */
51 extern void InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
52 unsigned width, unsigned height);
54 /* Returns True if it is an icon and is in this workspace */
55 static Bool
56 iconPosition(WCoreWindow *wcore, int sx1, int sy1, int sx2, int sy2,
57 int workspace, int *retX, int *retY)
59 void *parent;
60 int ok = 0;
62 parent = wcore->descriptor.parent;
64 /* if it is an application icon */
65 if (wcore->descriptor.parent_type == WCLASS_APPICON && !((WAppIcon *) parent)->docked) {
66 *retX = ((WAppIcon *) parent)->x_pos;
67 *retY = ((WAppIcon *) parent)->y_pos;
69 ok = 1;
70 } else if (wcore->descriptor.parent_type == WCLASS_MINIWINDOW &&
71 (((WIcon *) parent)->owner->frame->workspace == workspace
72 || IS_OMNIPRESENT(((WIcon *) parent)->owner)
73 || wPreferences.sticky_icons)
74 && ((WIcon *) parent)->mapped) {
76 *retX = ((WIcon *) parent)->owner->icon_x;
77 *retY = ((WIcon *) parent)->owner->icon_y;
79 ok = 1;
80 } else if (wcore->descriptor.parent_type == WCLASS_WINDOW
81 && ((WWindow *) parent)->flags.icon_moved
82 && (((WWindow *) parent)->frame->workspace == workspace || IS_OMNIPRESENT((WWindow *) parent)
83 || wPreferences.sticky_icons)) {
84 *retX = ((WWindow *) parent)->icon_x;
85 *retY = ((WWindow *) parent)->icon_y;
87 ok = 1;
90 /* Check if it is inside the screen */
91 if (ok) {
92 if (*retX < sx1 - wPreferences.icon_size)
93 ok = 0;
94 else if (*retX > sx2)
95 ok = 0;
97 if (*retY < sy1 - wPreferences.icon_size)
98 ok = 0;
99 else if (*retY > sy2)
100 ok = 0;
103 return ok;
106 void PlaceIcon(WScreen *scr, int *x_ret, int *y_ret, int head)
108 int pf; /* primary axis */
109 int sf; /* secondary axis */
110 int fullW;
111 int fullH;
112 char *map;
113 int pi, si;
114 WCoreWindow *obj;
115 int sx1, sx2, sy1, sy2; /* screen boundary */
116 int sw, sh;
117 int xo, yo;
118 int xs, ys;
119 int x, y;
120 int isize = wPreferences.icon_size;
121 int done = 0;
122 WMBagIterator iter;
123 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
125 /* Find out screen boundaries. */
127 /* Allows each head to have miniwindows */
128 sx1 = area.x1;
129 sy1 = area.y1;
130 sx2 = area.x2;
131 sy2 = area.y2;
132 sw = sx2 - sx1;
133 sh = sy2 - sy1;
135 sw = isize * (sw / isize);
136 sh = isize * (sh / isize);
137 fullW = (sx2 - sx1) / isize;
138 fullH = (sy2 - sy1) / isize;
140 /* icon yard boundaries */
141 if (wPreferences.icon_yard & IY_VERT) {
142 pf = fullH;
143 sf = fullW;
144 } else {
145 pf = fullW;
146 sf = fullH;
148 if (wPreferences.icon_yard & IY_RIGHT) {
149 xo = sx2 - isize;
150 xs = -1;
151 } else {
152 xo = sx1;
153 xs = 1;
155 if (wPreferences.icon_yard & IY_TOP) {
156 yo = sy1;
157 ys = 1;
158 } else {
159 yo = sy2 - isize;
160 ys = -1;
164 * Create a map with the occupied slots. 1 means the slot is used
165 * or at least partially used.
166 * The slot usage can be optimized by only marking fully used slots
167 * or slots that have most of it covered.
168 * Space usage is worse than the fvwm algorithm (used in the old version)
169 * but complexity is much better (faster) than it.
171 map = wmalloc((sw + 2) * (sh + 2));
172 memset(map, 0, (sw + 2) * (sh + 2));
174 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
176 WM_ETARETI_BAG(scr->stacking_list, obj, iter) {
178 while (obj) {
179 int x, y;
181 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace, &x, &y)) {
182 int xdi, ydi; /* rounded down */
183 int xui, yui; /* rounded up */
185 xdi = x / isize;
186 ydi = y / isize;
187 xui = (x + isize / 2) / isize;
188 yui = (y + isize / 2) / isize;
189 map[INDEX(xdi, ydi)] = 1;
190 map[INDEX(xdi, yui)] = 1;
191 map[INDEX(xui, ydi)] = 1;
192 map[INDEX(xui, yui)] = 1;
194 obj = obj->stacking->under;
197 /* Default position */
198 *x_ret = 0;
199 *y_ret = 0;
201 /* Look for an empty slot */
202 for (si = 0; si < sf; si++) {
203 for (pi = 0; pi < pf; pi++) {
204 if (wPreferences.icon_yard & IY_VERT) {
205 x = xo + xs * (si * isize);
206 y = yo + ys * (pi * isize);
207 } else {
208 x = xo + xs * (pi * isize);
209 y = yo + ys * (si * isize);
211 if (!map[INDEX(x / isize, y / isize)]) {
212 *x_ret = x;
213 *y_ret = y;
214 done = 1;
215 break;
218 if (done)
219 break;
222 wfree(map);
225 /* Computes the intersecting length of two line sections */
226 int calcIntersectionLength(int p1, int l1, int p2, int l2)
228 int isect;
229 int tmp;
231 if (p1 > p2) {
232 tmp = p1;
233 p1 = p2;
234 p2 = tmp;
235 tmp = l1;
236 l1 = l2;
237 l2 = tmp;
240 if (p1 + l1 < p2)
241 isect = 0;
242 else if (p2 + l2 < p1 + l1)
243 isect = l2;
244 else
245 isect = p1 + l1 - p2;
247 return isect;
250 /* Computes the intersecting area of two rectangles */
251 int calcIntersectionArea(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
253 return calcIntersectionLength(x1, w1, x2, w2)
254 * calcIntersectionLength(y1, h1, y2, h2);
257 static int calcSumOfCoveredAreas(WWindow *wwin, int x, int y, int w, int h)
259 int sum_isect = 0;
260 WWindow *test_window;
261 int tw, tx, ty, th;
263 test_window = wwin->screen_ptr->focused_window;
264 for (; test_window != NULL && test_window->prev != NULL;)
265 test_window = test_window->prev;
267 for (; test_window != NULL; test_window = test_window->next) {
268 if (test_window->frame->core->stacking->window_level < WMNormalLevel) {
269 continue;
272 tw = test_window->frame->core->width;
273 th = test_window->frame->core->height;
274 tx = test_window->frame_x;
275 ty = test_window->frame_y;
277 if (test_window->flags.mapped || (test_window->flags.shaded &&
278 test_window->frame->workspace == wwin->screen_ptr->current_workspace &&
279 !(test_window->flags.miniaturized || test_window->flags.hidden))) {
280 sum_isect += calcIntersectionArea(tx, ty, tw, th, x, y, w, h);
284 return sum_isect;
287 static void set_width_height(WWindow *wwin, unsigned int *width, unsigned int *height)
289 if (wwin->frame) {
290 *height += wwin->frame->top_width + wwin->frame->bottom_width;
291 } else {
292 if (HAS_TITLEBAR(wwin))
293 *height += TITLEBAR_HEIGHT;
294 if (HAS_RESIZEBAR(wwin))
295 *height += RESIZEBAR_HEIGHT;
297 if (HAS_BORDER(wwin)) {
298 *height += 2 * FRAME_BORDER_WIDTH;
299 *width += 2 * FRAME_BORDER_WIDTH;
303 static void
304 smartPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret, unsigned int width,
305 unsigned int height, WArea usableArea)
307 int test_x = 0, test_y = Y_ORIGIN;
308 int from_x, to_x, from_y, to_y;
309 int sx;
310 int min_isect, min_isect_x, min_isect_y;
311 int sum_isect;
313 set_width_height(wwin, &width, &height);
315 sx = X_ORIGIN;
316 min_isect = INT_MAX;
317 min_isect_x = sx;
318 min_isect_y = test_y;
320 while (((test_y + height) < usableArea.y2)) {
321 test_x = sx;
322 while ((test_x + width) < usableArea.x2) {
323 sum_isect = calcSumOfCoveredAreas(wwin, test_x, test_y, width, height);
325 if (sum_isect < min_isect) {
326 min_isect = sum_isect;
327 min_isect_x = test_x;
328 min_isect_y = test_y;
331 test_x += PLACETEST_HSTEP;
333 test_y += PLACETEST_VSTEP;
336 from_x = min_isect_x - PLACETEST_HSTEP + 1;
337 from_x = WMAX(from_x, X_ORIGIN);
338 to_x = min_isect_x + PLACETEST_HSTEP;
339 if (to_x + width > usableArea.x2)
340 to_x = usableArea.x2 - width;
342 from_y = min_isect_y - PLACETEST_VSTEP + 1;
343 from_y = WMAX(from_y, Y_ORIGIN);
344 to_y = min_isect_y + PLACETEST_VSTEP;
345 if (to_y + height > usableArea.y2)
346 to_y = usableArea.y2 - height;
348 for (test_x = from_x; test_x < to_x; test_x++) {
349 for (test_y = from_y; test_y < to_y; test_y++) {
350 sum_isect = calcSumOfCoveredAreas(wwin, test_x, test_y, width, height);
352 if (sum_isect < min_isect) {
353 min_isect = sum_isect;
354 min_isect_x = test_x;
355 min_isect_y = test_y;
360 *x_ret = min_isect_x;
361 *y_ret = min_isect_y;
364 static Bool
365 autoPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
366 unsigned int width, unsigned int height, int tryCount, WArea usableArea)
368 WScreen *scr = wwin->screen_ptr;
369 int test_x = 0, test_y = Y_ORIGIN;
370 int loc_ok = False, tw, tx, ty, th;
371 int swidth, sx;
372 WWindow *test_window;
374 set_width_height(wwin, &width, &height);
375 swidth = usableArea.x2 - usableArea.x1;
376 sx = X_ORIGIN;
378 /* this was based on fvwm2's smart placement */
379 while (((test_y + height) < (usableArea.y2 - usableArea.y1)) && !loc_ok) {
380 test_x = sx;
382 while (((test_x + width) < swidth) && (!loc_ok)) {
384 loc_ok = True;
385 test_window = scr->focused_window;
387 while ((test_window != NULL) && (loc_ok == True)) {
389 if (test_window->frame->core->stacking->window_level
390 < WMNormalLevel && tryCount > 0) {
391 test_window = test_window->next;
392 continue;
394 tw = test_window->frame->core->width;
395 th = test_window->frame->core->height;
396 tx = test_window->frame_x;
397 ty = test_window->frame_y;
399 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
400 (ty < (test_y + height)) && ((ty + th) > test_y) &&
401 (test_window->flags.mapped ||
402 (test_window->flags.shaded &&
403 test_window->frame->workspace == scr->current_workspace &&
404 !(test_window->flags.miniaturized || test_window->flags.hidden)))) {
406 loc_ok = False;
408 test_window = test_window->next;
411 test_window = scr->focused_window;
413 while ((test_window != NULL) && (loc_ok == True)) {
415 if (test_window->frame->core->stacking->window_level
416 < WMNormalLevel && tryCount > 0) {
417 test_window = test_window->prev;
418 continue;
420 tw = test_window->frame->core->width;
421 th = test_window->frame->core->height;
422 tx = test_window->frame_x;
423 ty = test_window->frame_y;
425 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
426 (ty < (test_y + height)) && ((ty + th) > test_y) &&
427 (test_window->flags.mapped ||
428 (test_window->flags.shaded &&
429 test_window->frame->workspace == scr->current_workspace &&
430 !(test_window->flags.miniaturized || test_window->flags.hidden)))) {
432 loc_ok = False;
434 test_window = test_window->prev;
436 if (loc_ok == True) {
437 *x_ret = test_x;
438 *y_ret = test_y;
439 break;
441 test_x += PLACETEST_HSTEP;
443 test_y += PLACETEST_VSTEP;
446 return loc_ok;
449 static void
450 cascadeWindow(WScreen *scr, WWindow *wwin, int *x_ret, int *y_ret,
451 unsigned int width, unsigned int height, int h, WArea usableArea)
453 set_width_height(wwin, &width, &height);
455 *x_ret = h * scr->cascade_index + X_ORIGIN;
456 *y_ret = h * scr->cascade_index + Y_ORIGIN;
458 if (width + *x_ret > usableArea.x2 || height + *y_ret > usableArea.y2) {
459 scr->cascade_index = 0;
460 *x_ret = X_ORIGIN;
461 *y_ret = Y_ORIGIN;
465 static void randomPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
466 unsigned int width, unsigned int height, WArea usableArea)
468 int w, h;
470 set_width_height(wwin, &width, &height);
472 w = ((usableArea.x2 - X_ORIGIN) - width);
473 h = ((usableArea.y2 - Y_ORIGIN) - height);
474 if (w < 1)
475 w = 1;
476 if (h < 1)
477 h = 1;
478 *x_ret = X_ORIGIN + rand() % w;
479 *y_ret = Y_ORIGIN + rand() % h;
482 void PlaceWindow(WWindow *wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
484 WScreen *scr = wwin->screen_ptr;
485 int h = WMFontHeight(scr->title_font)
486 + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
487 WArea usableArea = wGetUsableAreaForHead(scr, wGetHeadForPointerLocation(scr),
488 NULL, True);
490 switch (wPreferences.window_placement) {
491 case WPM_MANUAL:
492 InteractivePlaceWindow(wwin, x_ret, y_ret, width, height);
493 break;
495 case WPM_SMART:
496 smartPlaceWindow(wwin, x_ret, y_ret, width, height, usableArea);
497 break;
499 case WPM_AUTO:
500 if (autoPlaceWindow(wwin, x_ret, y_ret, width, height, 0, usableArea)) {
501 break;
502 } else if (autoPlaceWindow(wwin, x_ret, y_ret, width, height, 1, usableArea)) {
503 break;
505 /* there isn't a break here, because if we fail, it should fall
506 through to cascade placement, as people who want tiling want
507 automagicness aren't going to want to place their window */
509 case WPM_CASCADE:
510 if (wPreferences.window_placement == WPM_AUTO)
511 scr->cascade_index++;
513 cascadeWindow(scr, wwin, x_ret, y_ret, width, height, h, usableArea);
515 if (wPreferences.window_placement == WPM_CASCADE)
516 scr->cascade_index++;
517 break;
519 case WPM_RANDOM:
520 randomPlaceWindow(wwin, x_ret, y_ret, width, height, usableArea);
521 break;
525 * clip to usableArea instead of full screen
526 * this will also take dock/clip etc.. into account
527 * aswell as being xinerama friendly
529 if (*x_ret + width > usableArea.x2)
530 *x_ret = usableArea.x2 - width;
531 if (*x_ret < usableArea.x1)
532 *x_ret = usableArea.x1;
534 if (*y_ret + height > usableArea.y2)
535 *y_ret = usableArea.y2 - height;
536 if (*y_ret < usableArea.y1)
537 *y_ret = usableArea.y1;