Add history to some dialog boxes
[wmaker-crm.git] / src / stacking.c
blobbc1cf223119a14119d20f81fb70608260e6a4a07
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
31 #include "WindowMaker.h"
32 #include "screen.h"
33 #include "window.h"
34 #include "funcs.h"
35 #include "actions.h"
36 #include "properties.h"
37 #include "stacking.h"
38 #include "workspace.h"
40 /*** Global Variables ***/
41 extern XContext wStackContext;
43 extern WPreferences wPreferences;
45 static void notifyStackChange(WCoreWindow * frame, char *detail)
47 WWindow *wwin = wWindowFor(frame->window);
49 WMPostNotificationName(WMNChangedStacking, wwin, detail);
53 *----------------------------------------------------------------------
54 * RemakeStackList--
55 * Remakes the stacking_list for the screen, getting the real
56 * stacking order from the server and reordering windows that are not
57 * in the correct stacking.
59 * Side effects:
60 * The stacking order list and the actual window stacking
61 * may be changed (corrected)
63 *----------------------------------------------------------------------
65 void RemakeStackList(WScreen * scr)
67 Window *windows;
68 unsigned int nwindows;
69 Window junkr, junkp;
70 WCoreWindow *frame;
71 WCoreWindow *tmp;
72 int level;
73 int i, c;
75 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
76 wwarning(_("could not get window list!!"));
77 return;
78 } else {
79 WMEmptyBag(scr->stacking_list);
81 /* verify list integrity */
82 c = 0;
83 for (i = 0; i < nwindows; i++) {
84 if (XFindContext(dpy, windows[i], wStackContext, (XPointer *) & frame)
85 == XCNOENT) {
86 continue;
88 if (!frame)
89 continue;
90 c++;
91 level = frame->stacking->window_level;
92 tmp = WMGetFromBag(scr->stacking_list, level);
93 if (tmp)
94 tmp->stacking->above = frame;
95 frame->stacking->under = tmp;
96 frame->stacking->above = NULL;
97 WMSetInBag(scr->stacking_list, level, frame);
99 XFree(windows);
100 #ifdef DEBUG
101 if (c != scr->window_count) {
102 puts("Found different number of windows than in window lists!!!");
104 #endif
105 scr->window_count = c;
108 CommitStacking(scr);
112 *----------------------------------------------------------------------
113 * CommitStacking--
114 * Reorders the actual window stacking, so that it has the stacking
115 * order in the internal window stacking lists. It does the opposite
116 * of RemakeStackList().
118 * Side effects:
119 * Windows may be restacked.
120 *----------------------------------------------------------------------
122 void CommitStacking(WScreen * scr)
124 WCoreWindow *tmp;
125 int nwindows, i;
126 Window *windows;
127 WMBagIterator iter;
129 nwindows = scr->window_count;
130 windows = wmalloc(sizeof(Window) * nwindows);
132 i = 0;
133 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
134 while (tmp) {
135 #ifdef DEBUG
136 if (i >= nwindows) {
137 puts("Internal inconsistency! window_count is incorrect!!!");
138 printf("window_count says %i windows\n", nwindows);
139 wfree(windows);
140 return;
142 #endif
143 windows[i++] = tmp->window;
144 tmp = tmp->stacking->under;
147 XRestackWindows(dpy, windows, i);
148 wfree(windows);
150 #ifdef VIRTUAL_DESKTOP
151 wWorkspaceRaiseEdge(scr);
152 #endif
154 WMPostNotificationName(WMNResetStacking, scr, NULL);
158 *----------------------------------------------------------------------
159 * moveFrameToUnder--
160 * Reestacks windows so that "frame" is under "under".
162 * Returns:
163 * None
165 * Side effects:
166 * Changes the stacking order of frame.
167 *----------------------------------------------------------------------
169 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
171 Window wins[2];
173 wins[0] = under->window;
174 wins[1] = frame->window;
175 XRestackWindows(dpy, wins, 2);
179 *----------------------------------------------------------------------
180 * wRaiseFrame--
181 * Raises a frame taking the window level into account.
183 * Returns:
184 * None
186 * Side effects:
187 * Window stacking order and stacking list are changed.
189 *----------------------------------------------------------------------
191 void wRaiseFrame(WCoreWindow * frame)
193 WCoreWindow *wlist = frame;
194 int level = frame->stacking->window_level;
195 WScreen *scr = frame->screen_ptr;
197 /* already on top */
198 if (frame->stacking->above == NULL) {
199 return;
202 /* insert it on top of other windows on the same level */
203 if (frame->stacking->under)
204 frame->stacking->under->stacking->above = frame->stacking->above;
205 if (frame->stacking->above)
206 frame->stacking->above->stacking->under = frame->stacking->under;
208 frame->stacking->above = NULL;
209 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
210 if (frame->stacking->under) {
211 frame->stacking->under->stacking->above = frame;
213 WMSetInBag(scr->stacking_list, level, frame);
215 /* raise transients under us from bottom to top
216 * so that the order is kept */
217 again:
218 wlist = frame->stacking->under;
219 while (wlist && wlist->stacking->under)
220 wlist = wlist->stacking->under;
221 while (wlist && wlist != frame) {
222 if (wlist->stacking->child_of == frame) {
223 wRaiseFrame(wlist);
224 goto again;
226 wlist = wlist->stacking->above;
229 /* try to optimize things a little */
230 if (frame->stacking->above == NULL) {
231 WMBagIterator iter;
232 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
233 int i, last = above->stacking->window_level;
235 /* find the 1st level above us which has windows in it */
236 for (i = level + 1, above = NULL; i <= last; i++) {
237 above = WMGetFromBag(scr->stacking_list, i);
238 if (above != NULL)
239 break;
242 if (above != frame && above != NULL) {
243 while (above->stacking->under)
244 above = above->stacking->under;
245 moveFrameToUnder(above, frame);
246 } else {
247 /* no window above us */
248 above = NULL;
249 XRaiseWindow(dpy, frame->window);
251 } else {
252 moveFrameToUnder(frame->stacking->above, frame);
255 notifyStackChange(frame, "raise");
257 #ifdef VIRTUAL_DESKTOP
258 wWorkspaceRaiseEdge(scr);
259 #endif
262 void wRaiseLowerFrame(WCoreWindow * frame)
264 if (!frame->stacking->above
265 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
267 wLowerFrame(frame);
268 } else {
269 WCoreWindow *scan = frame->stacking->above;
270 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
272 while (scan) {
274 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
275 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
277 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
278 && scan_wwin->flags.mapped) {
279 break;
282 scan = scan->stacking->above;
285 if (scan) {
286 wRaiseFrame(frame);
287 } else {
288 wLowerFrame(frame);
293 void wLowerFrame(WCoreWindow * frame)
295 WScreen *scr = frame->screen_ptr;
296 WCoreWindow *wlist = frame;
297 int level = frame->stacking->window_level;
299 /* already in bottom */
300 if (wlist->stacking->under == NULL) {
301 return;
303 /* cant lower transient below below its owner */
304 if (wlist->stacking->under == wlist->stacking->child_of) {
305 return;
307 /* remove from the list */
308 if (WMGetFromBag(scr->stacking_list, level) == frame) {
309 /* it was the top window */
310 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
311 frame->stacking->under->stacking->above = NULL;
312 } else {
313 if (frame->stacking->under)
314 frame->stacking->under->stacking->above = frame->stacking->above;
315 if (frame->stacking->above)
316 frame->stacking->above->stacking->under = frame->stacking->under;
318 wlist = WMGetFromBag(scr->stacking_list, level);
320 /* look for place to put this window */
321 if (wlist) {
322 WCoreWindow *owner = frame->stacking->child_of;
324 if (owner != wlist) {
325 while (wlist->stacking->under) {
326 /* if this is a transient, it should not be placed under
327 * it's owner */
328 if (owner == wlist->stacking->under)
329 break;
330 wlist = wlist->stacking->under;
334 /* insert under the place found */
335 frame->stacking->above = wlist;
336 if (wlist) {
337 frame->stacking->under = wlist->stacking->under;
338 if (wlist->stacking->under)
339 wlist->stacking->under->stacking->above = frame;
340 wlist->stacking->under = frame;
341 } else {
342 frame->stacking->under = NULL;
345 if (frame->stacking->above == NULL) {
346 WMBagIterator iter;
347 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
348 int i, last = above->stacking->window_level;
350 /* find the 1st level above us which has windows in it */
351 for (i = level + 1, above = NULL; i <= last; i++) {
352 above = WMGetFromBag(scr->stacking_list, i);
353 if (above != NULL)
354 break;
357 if (above != frame && above != NULL) {
358 while (above->stacking->under)
359 above = above->stacking->under;
360 moveFrameToUnder(above, frame);
361 } else {
362 /* no window below us */
363 XLowerWindow(dpy, frame->window);
365 } else {
366 moveFrameToUnder(frame->stacking->above, frame);
369 notifyStackChange(frame, "lower");
371 #ifdef VIRTUAL_DESKTOP
372 wWorkspaceRaiseEdge(scr);
373 #endif
377 *----------------------------------------------------------------------
378 * AddToStackList--
379 * Inserts the frame in the top of the stacking list. The
380 * stacking precedence is obeyed.
382 * Returns:
383 * None
385 * Side effects:
386 * The frame is added to it's screen's window list.
387 *----------------------------------------------------------------------
389 void AddToStackList(WCoreWindow * frame)
391 WCoreWindow *curtop, *wlist;
392 int index = frame->stacking->window_level;
393 WScreen *scr = frame->screen_ptr;
394 WCoreWindow *trans = NULL;
396 frame->screen_ptr->window_count++;
397 XSaveContext(dpy, frame->window, wStackContext, (XPointer) frame);
398 curtop = WMGetFromBag(scr->stacking_list, index);
400 /* first window in this level */
401 if (curtop == NULL) {
402 WMSetInBag(scr->stacking_list, index, frame);
403 frame->stacking->above = NULL;
404 frame->stacking->under = NULL;
405 CommitStacking(scr);
406 return;
409 /* check if this is a transient owner */
410 wlist = curtop;
411 while (wlist) {
412 if (wlist->stacking->child_of == frame)
413 trans = wlist;
414 wlist = wlist->stacking->under;
416 /* trans will hold the transient in the lowest position
417 * in stacking list */
419 frame->stacking->above = trans;
420 if (trans != NULL) {
421 /* window is owner of a transient.. put it below
422 * the lowest transient */
423 frame->stacking->under = trans->stacking->under;
424 if (trans->stacking->under) {
425 trans->stacking->under->stacking->above = frame;
427 trans->stacking->under = frame;
428 } else {
429 /* window is not owner of transients.. just put it in the
430 * top of other windows */
431 frame->stacking->under = curtop;
432 curtop->stacking->above = frame;
433 WMSetInBag(scr->stacking_list, index, frame);
435 CommitStacking(scr);
439 *----------------------------------------------------------------------
440 * MoveInStackListAbove--
441 * Moves the frame above "next".
443 * Returns:
444 * None
446 * Side effects:
447 * Stacking order may be changed.
448 * Window level for frame may be changed.
449 *----------------------------------------------------------------------
451 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
453 WCoreWindow *tmpw;
454 WScreen *scr = frame->screen_ptr;
455 int index;
457 if (!next || frame->stacking->under == next)
458 return;
460 if (frame->stacking->window_level != next->stacking->window_level)
461 ChangeStackingLevel(frame, next->stacking->window_level);
463 index = frame->stacking->window_level;
465 tmpw = WMGetFromBag(scr->stacking_list, index);
466 if (tmpw == frame)
467 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
468 if (frame->stacking->under)
469 frame->stacking->under->stacking->above = frame->stacking->above;
470 if (frame->stacking->above)
471 frame->stacking->above->stacking->under = frame->stacking->under;
472 if (next->stacking->above)
473 next->stacking->above->stacking->under = frame;
474 frame->stacking->under = next;
475 frame->stacking->above = next->stacking->above;
476 next->stacking->above = frame;
477 if (tmpw == next)
478 WMSetInBag(scr->stacking_list, index, frame);
480 /* try to optimize things a little */
481 if (frame->stacking->above == NULL) {
482 WCoreWindow *above = NULL;
483 WMBagIterator iter;
485 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
486 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
488 /* can't optimize */
489 while (above->stacking->under)
490 above = above->stacking->under;
491 break;
493 if (above == NULL) {
494 XRaiseWindow(dpy, frame->window);
495 } else {
496 moveFrameToUnder(above, frame);
498 } else {
499 moveFrameToUnder(frame->stacking->above, frame);
502 WMPostNotificationName(WMNResetStacking, scr, NULL);
504 #ifdef VIRTUAL_DESKTOP
505 wWorkspaceRaiseEdge(scr);
506 #endif
510 *----------------------------------------------------------------------
511 * MoveInStackListUnder--
512 * Moves the frame to under "prev".
514 * Returns:
515 * None
517 * Side effects:
518 * Stacking order may be changed.
519 * Window level for frame may be changed.
520 *----------------------------------------------------------------------
522 void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
524 WCoreWindow *tmpw;
525 int index;
526 WScreen *scr = frame->screen_ptr;
528 if (!prev || frame->stacking->above == prev)
529 return;
531 if (frame->stacking->window_level != prev->stacking->window_level)
532 ChangeStackingLevel(frame, prev->stacking->window_level);
534 index = frame->stacking->window_level;
536 tmpw = WMGetFromBag(scr->stacking_list, index);
537 if (tmpw == frame)
538 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
539 if (frame->stacking->under)
540 frame->stacking->under->stacking->above = frame->stacking->above;
541 if (frame->stacking->above)
542 frame->stacking->above->stacking->under = frame->stacking->under;
543 if (prev->stacking->under)
544 prev->stacking->under->stacking->above = frame;
545 frame->stacking->above = prev;
546 frame->stacking->under = prev->stacking->under;
547 prev->stacking->under = frame;
548 moveFrameToUnder(prev, frame);
550 WMPostNotificationName(WMNResetStacking, scr, NULL);
553 void RemoveFromStackList(WCoreWindow * frame)
555 int index = frame->stacking->window_level;
557 if (XDeleteContext(dpy, frame->window, wStackContext) == XCNOENT) {
558 wwarning("RemoveFromStackingList(): window not in list ");
559 return;
561 /* remove from the window stack list */
562 if (frame->stacking->under)
563 frame->stacking->under->stacking->above = frame->stacking->above;
564 if (frame->stacking->above)
565 frame->stacking->above->stacking->under = frame->stacking->under;
566 else /* this was the first window on the list */
567 WMSetInBag(frame->screen_ptr->stacking_list, index, frame->stacking->under);
569 frame->screen_ptr->window_count--;
571 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
574 void ChangeStackingLevel(WCoreWindow * frame, int new_level)
576 int old_level;
578 if (frame->stacking->window_level == new_level)
579 return;
580 old_level = frame->stacking->window_level;
582 RemoveFromStackList(frame);
583 frame->stacking->window_level = new_level;
584 AddToStackList(frame);
585 if (old_level > new_level) {
586 wRaiseFrame(frame);
587 } else {
588 wLowerFrame(frame);