Change to the linux kernel coding style
[wmaker-crm.git] / src / stacking.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
9af1c6c4 dan1998-10-21 14:43:47 +00002 * Window Maker window manager
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
6830b057 dan2004-10-12 21:28:27 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * 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.
11 *
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.
16 *
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
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
23#include "wconfig.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <X11/Xlib.h>
29#include <X11/Xutil.h>
30
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"
9aca0d5f dan2004-10-12 01:34:32 +000038#include "workspace.h"
5ef342d9 kojima2001-09-06 21:42:28 +000039
9d2e6ef9 scottc1998-09-29 22:36:29 +000040/*** Global Variables ***/
41extern XContext wStackContext;
42
43extern WPreferences wPreferences;
44
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020045static void notifyStackChange(WCoreWindow * frame, char *detail)
5ef342d9 kojima2001-09-06 21:42:28 +000046{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020047 WWindow *wwin = wWindowFor(frame->window);
5ef342d9 kojima2001-09-06 21:42:28 +000048
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049 WMPostNotificationName(WMNChangedStacking, wwin, detail);
5ef342d9 kojima2001-09-06 21:42:28 +000050}
51
9d2e6ef9 scottc1998-09-29 22:36:29 +000052/*
6830b057 dan2004-10-12 21:28:27 +000053 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +000054 * 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.
6830b057 dan2004-10-12 21:28:27 +000058 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000059 * Side effects:
6830b057 dan2004-10-12 21:28:27 +000060 * The stacking order list and the actual window stacking
9d2e6ef9 scottc1998-09-29 22:36:29 +000061 * may be changed (corrected)
6830b057 dan2004-10-12 21:28:27 +000062 *
63 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +000064 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020065void RemakeStackList(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +000066{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020067 Window *windows;
68 unsigned int nwindows;
69 Window junkr, junkp;
70 WCoreWindow *frame;
71 WCoreWindow *tmp;
72 int level;
73 int i, c;
74
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);
80
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;
87 }
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);
98 }
99 XFree(windows);
9af1c6c4 dan1998-10-21 14:43:47 +0000100#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200101 if (c != scr->window_count) {
102 puts("Found different number of windows than in window lists!!!");
103 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000104#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105 scr->window_count = c;
106 }
6830b057 dan2004-10-12 21:28:27 +0000107
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200108 CommitStacking(scr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000109}
110
111/*
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().
6830b057 dan2004-10-12 21:28:27 +0000117 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000118 * Side effects:
119 * Windows may be restacked.
6830b057 dan2004-10-12 21:28:27 +0000120 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000121 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200122void CommitStacking(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000123{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200124 WCoreWindow *tmp;
125 int nwindows, i;
126 Window *windows;
127 WMBagIterator iter;
6830b057 dan2004-10-12 21:28:27 +0000128
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200129 nwindows = scr->window_count;
130 windows = wmalloc(sizeof(Window) * nwindows);
6830b057 dan2004-10-12 21:28:27 +0000131
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200132 i = 0;
133 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
134 while (tmp) {
9af1c6c4 dan1998-10-21 14:43:47 +0000135#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200136 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;
141 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000142#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200143 windows[i++] = tmp->window;
144 tmp = tmp->stacking->under;
145 }
146 }
147 XRestackWindows(dpy, windows, i);
148 wfree(windows);
5ef342d9 kojima2001-09-06 21:42:28 +0000149
9aca0d5f dan2004-10-12 01:34:32 +0000150#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200151 wWorkspaceRaiseEdge(scr);
9aca0d5f dan2004-10-12 01:34:32 +0000152#endif
6830b057 dan2004-10-12 21:28:27 +0000153
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200154 WMPostNotificationName(WMNResetStacking, scr, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000155}
156
157/*
158 *----------------------------------------------------------------------
159 * moveFrameToUnder--
160 * Reestacks windows so that "frame" is under "under".
6830b057 dan2004-10-12 21:28:27 +0000161 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000162 * Returns:
163 * None
6830b057 dan2004-10-12 21:28:27 +0000164 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000165 * Side effects:
166 * Changes the stacking order of frame.
167 *----------------------------------------------------------------------
168 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200169static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000170{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200171 Window wins[2];
9d2e6ef9 scottc1998-09-29 22:36:29 +0000172
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200173 wins[0] = under->window;
174 wins[1] = frame->window;
175 XRestackWindows(dpy, wins, 2);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000176}
177
178/*
179 *----------------------------------------------------------------------
180 * wRaiseFrame--
6830b057 dan2004-10-12 21:28:27 +0000181 * Raises a frame taking the window level into account.
182 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000183 * Returns:
184 * None
6830b057 dan2004-10-12 21:28:27 +0000185 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000186 * Side effects:
f65c5498 kojima2000-03-30 02:52:07 +0000187 * Window stacking order and stacking list are changed.
6830b057 dan2004-10-12 21:28:27 +0000188 *
189 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000190 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200191void wRaiseFrame(WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000192{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200193 WCoreWindow *wlist = frame;
194 int level = frame->stacking->window_level;
195 WScreen *scr = frame->screen_ptr;
196
197 /* already on top */
198 if (frame->stacking->above == NULL) {
199 return;
200 }
201
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;
207
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;
212 }
213 WMSetInBag(scr->stacking_list, level, frame);
214
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;
225 }
226 wlist = wlist->stacking->above;
227 }
228
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;
234
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;
240 }
241
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);
250 }
251 } else {
252 moveFrameToUnder(frame->stacking->above, frame);
253 }
254
255 notifyStackChange(frame, "raise");
5ef342d9 kojima2001-09-06 21:42:28 +0000256
8eb0c79c id1999-12-18 11:52:49 +0000257#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200258 wWorkspaceRaiseEdge(scr);
8eb0c79c id1999-12-18 11:52:49 +0000259#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000260}
261
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200262void wRaiseLowerFrame(WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000263{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200264 if (!frame->stacking->above
265 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
266
267 wLowerFrame(frame);
268 } else {
269 WCoreWindow *scan = frame->stacking->above;
270 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
271
272 while (scan) {
273
274 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
275 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
276
277 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
278 && scan_wwin->flags.mapped) {
279 break;
280 }
281 }
282 scan = scan->stacking->above;
283 }
284
285 if (scan) {
286 wRaiseFrame(frame);
287 } else {
288 wLowerFrame(frame);
289 }
290 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000291}
292
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200293void wLowerFrame(WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000294{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200295 WScreen *scr = frame->screen_ptr;
296 WCoreWindow *wlist = frame;
297 int level = frame->stacking->window_level;
298
299 /* already in bottom */
300 if (wlist->stacking->under == NULL) {
301 return;
302 }
303 /* cant lower transient below below its owner */
304 if (wlist->stacking->under == wlist->stacking->child_of) {
305 return;
306 }
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;
317 }
318 wlist = WMGetFromBag(scr->stacking_list, level);
319
320 /* look for place to put this window */
321 if (wlist) {
322 WCoreWindow *owner = frame->stacking->child_of;
323
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;
331 }
332 }
333 }
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;
343 }
344
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;
349
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;
355 }
356
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);
364 }
365 } else {
366 moveFrameToUnder(frame->stacking->above, frame);
367 }
368
369 notifyStackChange(frame, "lower");
6830b057 dan2004-10-12 21:28:27 +0000370
9aca0d5f dan2004-10-12 01:34:32 +0000371#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200372 wWorkspaceRaiseEdge(scr);
9aca0d5f dan2004-10-12 01:34:32 +0000373#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000374}
375
9d2e6ef9 scottc1998-09-29 22:36:29 +0000376/*
377 *----------------------------------------------------------------------
378 * AddToStackList--
379 * Inserts the frame in the top of the stacking list. The
380 * stacking precedence is obeyed.
6830b057 dan2004-10-12 21:28:27 +0000381 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000382 * Returns:
383 * None
6830b057 dan2004-10-12 21:28:27 +0000384 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000385 * Side effects:
386 * The frame is added to it's screen's window list.
6830b057 dan2004-10-12 21:28:27 +0000387 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000388 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200389void AddToStackList(WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000390{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200391 WCoreWindow *curtop, *wlist;
392 int index = frame->stacking->window_level;
393 WScreen *scr = frame->screen_ptr;
394 WCoreWindow *trans = NULL;
395
396 frame->screen_ptr->window_count++;
397 XSaveContext(dpy, frame->window, wStackContext, (XPointer) frame);
398 curtop = WMGetFromBag(scr->stacking_list, index);
399
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;
407 }
408
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;
415 }
416 /* trans will hold the transient in the lowest position
417 * in stacking list */
418
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;
426 }
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);
434 }
435 CommitStacking(scr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000436}
437
9d2e6ef9 scottc1998-09-29 22:36:29 +0000438/*
439 *----------------------------------------------------------------------
440 * MoveInStackListAbove--
441 * Moves the frame above "next".
6830b057 dan2004-10-12 21:28:27 +0000442 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000443 * Returns:
444 * None
6830b057 dan2004-10-12 21:28:27 +0000445 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000446 * Side effects:
447 * Stacking order may be changed.
448 * Window level for frame may be changed.
6830b057 dan2004-10-12 21:28:27 +0000449 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000450 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200451void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000452{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200453 WCoreWindow *tmpw;
454 WScreen *scr = frame->screen_ptr;
455 int index;
456
457 if (!next || frame->stacking->under == next)
458 return;
459
460 if (frame->stacking->window_level != next->stacking->window_level)
461 ChangeStackingLevel(frame, next->stacking->window_level);
462
463 index = frame->stacking->window_level;
464
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);
479
480 /* try to optimize things a little */
481 if (frame->stacking->above == NULL) {
482 WCoreWindow *above = NULL;
483 WMBagIterator iter;
484
485 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
486 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
487
488 /* can't optimize */
489 while (above->stacking->under)
490 above = above->stacking->under;
491 break;
492 }
493 if (above == NULL) {
494 XRaiseWindow(dpy, frame->window);
495 } else {
496 moveFrameToUnder(above, frame);
497 }
498 } else {
499 moveFrameToUnder(frame->stacking->above, frame);
500 }
501
502 WMPostNotificationName(WMNResetStacking, scr, NULL);
6830b057 dan2004-10-12 21:28:27 +0000503
9aca0d5f dan2004-10-12 01:34:32 +0000504#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200505 wWorkspaceRaiseEdge(scr);
9aca0d5f dan2004-10-12 01:34:32 +0000506#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000507}
508
9d2e6ef9 scottc1998-09-29 22:36:29 +0000509/*
510 *----------------------------------------------------------------------
511 * MoveInStackListUnder--
0261c326 dan1999-01-06 15:22:33 +0000512 * Moves the frame to under "prev".
6830b057 dan2004-10-12 21:28:27 +0000513 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000514 * Returns:
515 * None
6830b057 dan2004-10-12 21:28:27 +0000516 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000517 * Side effects:
518 * Stacking order may be changed.
519 * Window level for frame may be changed.
6830b057 dan2004-10-12 21:28:27 +0000520 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000521 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200522void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000523{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200524 WCoreWindow *tmpw;
525 int index;
526 WScreen *scr = frame->screen_ptr;
527
528 if (!prev || frame->stacking->above == prev)
529 return;
530
531 if (frame->stacking->window_level != prev->stacking->window_level)
532 ChangeStackingLevel(frame, prev->stacking->window_level);
533
534 index = frame->stacking->window_level;
535
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);
549
550 WMPostNotificationName(WMNResetStacking, scr, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000551}
552
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200553void RemoveFromStackList(WCoreWindow * frame)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000554{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200555 int index = frame->stacking->window_level;
556
557 if (XDeleteContext(dpy, frame->window, wStackContext) == XCNOENT) {
558 wwarning("RemoveFromStackingList(): window not in list ");
559 return;
560 }
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);
568
569 frame->screen_ptr->window_count--;
570
571 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000572}
573
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200574void ChangeStackingLevel(WCoreWindow * frame, int new_level)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000575{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200576 int old_level;
577
578 if (frame->stacking->window_level == new_level)
579 return;
580 old_level = frame->stacking->window_level;
581
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);
589 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000590}