Radio and Check buttons now work
[wmaker-crm.git] / src / stacking.c
blob8199c8d796ec25d106efd35eb276b5b9e85eca78
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 scr->window_count = c;
103 CommitStacking(scr);
107 *----------------------------------------------------------------------
108 * CommitStacking--
109 * Reorders the actual window stacking, so that it has the stacking
110 * order in the internal window stacking lists. It does the opposite
111 * of RemakeStackList().
113 * Side effects:
114 * Windows may be restacked.
115 *----------------------------------------------------------------------
117 void CommitStacking(WScreen * scr)
119 WCoreWindow *tmp;
120 int nwindows, i;
121 Window *windows;
122 WMBagIterator iter;
124 nwindows = scr->window_count;
125 windows = wmalloc(sizeof(Window) * nwindows);
127 i = 0;
128 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
129 while (tmp) {
130 windows[i++] = tmp->window;
131 tmp = tmp->stacking->under;
134 XRestackWindows(dpy, windows, i);
135 wfree(windows);
136 WMPostNotificationName(WMNResetStacking, scr, NULL);
140 *----------------------------------------------------------------------
141 * moveFrameToUnder--
142 * Reestacks windows so that "frame" is under "under".
144 * Returns:
145 * None
147 * Side effects:
148 * Changes the stacking order of frame.
149 *----------------------------------------------------------------------
151 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
153 Window wins[2];
155 wins[0] = under->window;
156 wins[1] = frame->window;
157 XRestackWindows(dpy, wins, 2);
161 *----------------------------------------------------------------------
162 * wRaiseFrame--
163 * Raises a frame taking the window level into account.
165 * Returns:
166 * None
168 * Side effects:
169 * Window stacking order and stacking list are changed.
171 *----------------------------------------------------------------------
173 void wRaiseFrame(WCoreWindow * frame)
175 WCoreWindow *wlist = frame;
176 int level = frame->stacking->window_level;
177 WScreen *scr = frame->screen_ptr;
179 /* already on top */
180 if (frame->stacking->above == NULL) {
181 return;
184 /* insert it on top of other windows on the same level */
185 if (frame->stacking->under)
186 frame->stacking->under->stacking->above = frame->stacking->above;
187 if (frame->stacking->above)
188 frame->stacking->above->stacking->under = frame->stacking->under;
190 frame->stacking->above = NULL;
191 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
192 if (frame->stacking->under) {
193 frame->stacking->under->stacking->above = frame;
195 WMSetInBag(scr->stacking_list, level, frame);
197 /* raise transients under us from bottom to top
198 * so that the order is kept */
199 again:
200 wlist = frame->stacking->under;
201 while (wlist && wlist->stacking->under)
202 wlist = wlist->stacking->under;
203 while (wlist && wlist != frame) {
204 if (wlist->stacking->child_of == frame) {
205 wRaiseFrame(wlist);
206 goto again;
208 wlist = wlist->stacking->above;
211 /* try to optimize things a little */
212 if (frame->stacking->above == NULL) {
213 WMBagIterator iter;
214 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
215 int i, last = above->stacking->window_level;
217 /* find the 1st level above us which has windows in it */
218 for (i = level + 1, above = NULL; i <= last; i++) {
219 above = WMGetFromBag(scr->stacking_list, i);
220 if (above != NULL)
221 break;
224 if (above != frame && above != NULL) {
225 while (above->stacking->under)
226 above = above->stacking->under;
227 moveFrameToUnder(above, frame);
228 } else {
229 /* no window above us */
230 above = NULL;
231 XRaiseWindow(dpy, frame->window);
233 } else {
234 moveFrameToUnder(frame->stacking->above, frame);
237 notifyStackChange(frame, "raise");
240 void wRaiseLowerFrame(WCoreWindow * frame)
242 if (!frame->stacking->above
243 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
245 wLowerFrame(frame);
246 } else {
247 WCoreWindow *scan = frame->stacking->above;
248 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
250 while (scan) {
252 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
253 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
255 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
256 && scan_wwin->flags.mapped) {
257 break;
260 scan = scan->stacking->above;
263 if (scan) {
264 wRaiseFrame(frame);
265 } else {
266 wLowerFrame(frame);
271 void wLowerFrame(WCoreWindow * frame)
273 WScreen *scr = frame->screen_ptr;
274 WCoreWindow *wlist = frame;
275 int level = frame->stacking->window_level;
277 /* already in bottom */
278 if (wlist->stacking->under == NULL) {
279 return;
281 /* cant lower transient below below its owner */
282 if (wlist->stacking->under == wlist->stacking->child_of) {
283 return;
285 /* remove from the list */
286 if (WMGetFromBag(scr->stacking_list, level) == frame) {
287 /* it was the top window */
288 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
289 frame->stacking->under->stacking->above = NULL;
290 } else {
291 if (frame->stacking->under)
292 frame->stacking->under->stacking->above = frame->stacking->above;
293 if (frame->stacking->above)
294 frame->stacking->above->stacking->under = frame->stacking->under;
296 wlist = WMGetFromBag(scr->stacking_list, level);
298 /* look for place to put this window */
299 if (wlist) {
300 WCoreWindow *owner = frame->stacking->child_of;
302 if (owner != wlist) {
303 while (wlist->stacking->under) {
304 /* if this is a transient, it should not be placed under
305 * it's owner */
306 if (owner == wlist->stacking->under)
307 break;
308 wlist = wlist->stacking->under;
312 /* insert under the place found */
313 frame->stacking->above = wlist;
314 if (wlist) {
315 frame->stacking->under = wlist->stacking->under;
316 if (wlist->stacking->under)
317 wlist->stacking->under->stacking->above = frame;
318 wlist->stacking->under = frame;
319 } else {
320 frame->stacking->under = NULL;
323 if (frame->stacking->above == NULL) {
324 WMBagIterator iter;
325 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
326 int i, last = above->stacking->window_level;
328 /* find the 1st level above us which has windows in it */
329 for (i = level + 1, above = NULL; i <= last; i++) {
330 above = WMGetFromBag(scr->stacking_list, i);
331 if (above != NULL)
332 break;
335 if (above != frame && above != NULL) {
336 while (above->stacking->under)
337 above = above->stacking->under;
338 moveFrameToUnder(above, frame);
339 } else {
340 /* no window below us */
341 XLowerWindow(dpy, frame->window);
343 } else {
344 moveFrameToUnder(frame->stacking->above, frame);
347 notifyStackChange(frame, "lower");
351 *----------------------------------------------------------------------
352 * AddToStackList--
353 * Inserts the frame in the top of the stacking list. The
354 * stacking precedence is obeyed.
356 * Returns:
357 * None
359 * Side effects:
360 * The frame is added to it's screen's window list.
361 *----------------------------------------------------------------------
363 void AddToStackList(WCoreWindow * frame)
365 WCoreWindow *curtop, *wlist;
366 int index = frame->stacking->window_level;
367 WScreen *scr = frame->screen_ptr;
368 WCoreWindow *trans = NULL;
370 frame->screen_ptr->window_count++;
371 XSaveContext(dpy, frame->window, wStackContext, (XPointer) frame);
372 curtop = WMGetFromBag(scr->stacking_list, index);
374 /* first window in this level */
375 if (curtop == NULL) {
376 WMSetInBag(scr->stacking_list, index, frame);
377 frame->stacking->above = NULL;
378 frame->stacking->under = NULL;
379 CommitStacking(scr);
380 return;
383 /* check if this is a transient owner */
384 wlist = curtop;
385 while (wlist) {
386 if (wlist->stacking->child_of == frame)
387 trans = wlist;
388 wlist = wlist->stacking->under;
390 /* trans will hold the transient in the lowest position
391 * in stacking list */
393 frame->stacking->above = trans;
394 if (trans != NULL) {
395 /* window is owner of a transient.. put it below
396 * the lowest transient */
397 frame->stacking->under = trans->stacking->under;
398 if (trans->stacking->under) {
399 trans->stacking->under->stacking->above = frame;
401 trans->stacking->under = frame;
402 } else {
403 /* window is not owner of transients.. just put it in the
404 * top of other windows */
405 frame->stacking->under = curtop;
406 curtop->stacking->above = frame;
407 WMSetInBag(scr->stacking_list, index, frame);
409 CommitStacking(scr);
413 *----------------------------------------------------------------------
414 * MoveInStackListAbove--
415 * Moves the frame above "next".
417 * Returns:
418 * None
420 * Side effects:
421 * Stacking order may be changed.
422 * Window level for frame may be changed.
423 *----------------------------------------------------------------------
425 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
427 WCoreWindow *tmpw;
428 WScreen *scr = frame->screen_ptr;
429 int index;
431 if (!next || frame->stacking->under == next)
432 return;
434 if (frame->stacking->window_level != next->stacking->window_level)
435 ChangeStackingLevel(frame, next->stacking->window_level);
437 index = frame->stacking->window_level;
439 tmpw = WMGetFromBag(scr->stacking_list, index);
440 if (tmpw == frame)
441 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
442 if (frame->stacking->under)
443 frame->stacking->under->stacking->above = frame->stacking->above;
444 if (frame->stacking->above)
445 frame->stacking->above->stacking->under = frame->stacking->under;
446 if (next->stacking->above)
447 next->stacking->above->stacking->under = frame;
448 frame->stacking->under = next;
449 frame->stacking->above = next->stacking->above;
450 next->stacking->above = frame;
451 if (tmpw == next)
452 WMSetInBag(scr->stacking_list, index, frame);
454 /* try to optimize things a little */
455 if (frame->stacking->above == NULL) {
456 WCoreWindow *above = NULL;
457 WMBagIterator iter;
459 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
460 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
462 /* can't optimize */
463 while (above->stacking->under)
464 above = above->stacking->under;
465 break;
467 if (above == NULL) {
468 XRaiseWindow(dpy, frame->window);
469 } else {
470 moveFrameToUnder(above, frame);
472 } else {
473 moveFrameToUnder(frame->stacking->above, frame);
476 WMPostNotificationName(WMNResetStacking, scr, NULL);
480 *----------------------------------------------------------------------
481 * MoveInStackListUnder--
482 * Moves the frame to under "prev".
484 * Returns:
485 * None
487 * Side effects:
488 * Stacking order may be changed.
489 * Window level for frame may be changed.
490 *----------------------------------------------------------------------
492 void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
494 WCoreWindow *tmpw;
495 int index;
496 WScreen *scr = frame->screen_ptr;
498 if (!prev || frame->stacking->above == prev)
499 return;
501 if (frame->stacking->window_level != prev->stacking->window_level)
502 ChangeStackingLevel(frame, prev->stacking->window_level);
504 index = frame->stacking->window_level;
506 tmpw = WMGetFromBag(scr->stacking_list, index);
507 if (tmpw == frame)
508 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
509 if (frame->stacking->under)
510 frame->stacking->under->stacking->above = frame->stacking->above;
511 if (frame->stacking->above)
512 frame->stacking->above->stacking->under = frame->stacking->under;
513 if (prev->stacking->under)
514 prev->stacking->under->stacking->above = frame;
515 frame->stacking->above = prev;
516 frame->stacking->under = prev->stacking->under;
517 prev->stacking->under = frame;
518 moveFrameToUnder(prev, frame);
520 WMPostNotificationName(WMNResetStacking, scr, NULL);
523 void RemoveFromStackList(WCoreWindow * frame)
525 int index = frame->stacking->window_level;
527 if (XDeleteContext(dpy, frame->window, wStackContext) == XCNOENT) {
528 wwarning("RemoveFromStackingList(): window not in list ");
529 return;
531 /* remove from the window stack list */
532 if (frame->stacking->under)
533 frame->stacking->under->stacking->above = frame->stacking->above;
534 if (frame->stacking->above)
535 frame->stacking->above->stacking->under = frame->stacking->under;
536 else /* this was the first window on the list */
537 WMSetInBag(frame->screen_ptr->stacking_list, index, frame->stacking->under);
539 frame->screen_ptr->window_count--;
541 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
544 void ChangeStackingLevel(WCoreWindow * frame, int new_level)
546 int old_level;
548 if (frame->stacking->window_level == new_level)
549 return;
550 old_level = frame->stacking->window_level;
552 RemoveFromStackList(frame);
553 frame->stacking->window_level = new_level;
554 AddToStackList(frame);
555 if (old_level > new_level) {
556 wRaiseFrame(frame);
557 } else {
558 wLowerFrame(frame);