Update Serbian translation from master branch
[wmaker-crm.git] / src / stacking.c
blob240c7b1e0bd34ff1b826513dc7fc8568f4d7b32b
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 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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
30 #include "WindowMaker.h"
31 #include "screen.h"
32 #include "window.h"
33 #include "actions.h"
34 #include "properties.h"
35 #include "stacking.h"
36 #include "workspace.h"
39 static void notifyStackChange(WCoreWindow * frame, char *detail)
41 WWindow *wwin = wWindowFor(frame->window);
43 WMPostNotificationName(WMNChangedStacking, wwin, detail);
47 *----------------------------------------------------------------------
48 * RemakeStackList--
49 * Remakes the stacking_list for the screen, getting the real
50 * stacking order from the server and reordering windows that are not
51 * in the correct stacking.
53 * Side effects:
54 * The stacking order list and the actual window stacking
55 * may be changed (corrected)
57 *----------------------------------------------------------------------
59 void RemakeStackList(WScreen * scr)
61 Window *windows;
62 unsigned int nwindows;
63 Window junkr, junkp;
64 WCoreWindow *frame;
65 WCoreWindow *tmp;
66 int level;
67 int i, c;
69 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
70 wwarning(_("could not get window list!!"));
71 return;
72 } else {
73 WMEmptyBag(scr->stacking_list);
75 /* verify list integrity */
76 c = 0;
77 for (i = 0; i < nwindows; i++) {
78 if (XFindContext(dpy, windows[i], w_global.context.stack, (XPointer *) & frame)
79 == XCNOENT) {
80 continue;
82 if (!frame)
83 continue;
84 c++;
85 level = frame->stacking->window_level;
86 tmp = WMGetFromBag(scr->stacking_list, level);
87 if (tmp)
88 tmp->stacking->above = frame;
89 frame->stacking->under = tmp;
90 frame->stacking->above = NULL;
91 WMSetInBag(scr->stacking_list, level, frame);
93 XFree(windows);
94 scr->window_count = c;
97 CommitStacking(scr);
101 *----------------------------------------------------------------------
102 * CommitStacking--
103 * Reorders the actual window stacking, so that it has the stacking
104 * order in the internal window stacking lists. It does the opposite
105 * of RemakeStackList().
107 * Side effects:
108 * Windows may be restacked.
109 *----------------------------------------------------------------------
111 void CommitStacking(WScreen * scr)
113 WCoreWindow *tmp;
114 int nwindows, i;
115 Window *windows;
116 WMBagIterator iter;
118 nwindows = scr->window_count;
119 windows = wmalloc(sizeof(Window) * nwindows);
121 i = 0;
122 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
123 while (tmp) {
124 windows[i++] = tmp->window;
125 tmp = tmp->stacking->under;
128 XRestackWindows(dpy, windows, i);
129 wfree(windows);
130 WMPostNotificationName(WMNResetStacking, scr, NULL);
134 *----------------------------------------------------------------------
135 * moveFrameToUnder--
136 * Reestacks windows so that "frame" is under "under".
138 * Returns:
139 * None
141 * Side effects:
142 * Changes the stacking order of frame.
143 *----------------------------------------------------------------------
145 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
147 Window wins[2];
149 wins[0] = under->window;
150 wins[1] = frame->window;
151 XRestackWindows(dpy, wins, 2);
155 *----------------------------------------------------------------------
156 * CommitStackingForWindow--
157 * Reorders the stacking for the specified window, so that it has the
158 * stacking order in the internal window stacking lists.
160 * Side effects:
161 * Windows may be restacked.
162 *----------------------------------------------------------------------
164 void CommitStackingForWindow(WCoreWindow * frame)
166 int level = frame->stacking->window_level;
167 WScreen *scr = frame->screen_ptr;
169 if (frame->stacking->above == NULL) {
170 WMBagIterator iter;
171 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
172 int i, last = above->stacking->window_level;
174 /* find the 1st level above us which has windows in it */
175 for (i = level + 1, above = NULL; i <= last; i++) {
176 above = WMGetFromBag(scr->stacking_list, i);
177 if (above != NULL)
178 break;
181 if (above != frame && above != NULL) {
182 while (above->stacking->under)
183 above = above->stacking->under;
184 moveFrameToUnder(above, frame);
185 } else {
186 /* no window above us */
187 XRaiseWindow(dpy, frame->window);
189 } else {
190 moveFrameToUnder(frame->stacking->above, frame);
195 *----------------------------------------------------------------------
196 * wRaiseFrame--
197 * Raises a frame taking the window level into account.
199 * Returns:
200 * None
202 * Side effects:
203 * Window stacking order and stacking list are changed.
205 *----------------------------------------------------------------------
207 void wRaiseFrame(WCoreWindow * frame)
209 WCoreWindow *wlist = frame;
210 int level = frame->stacking->window_level;
211 WScreen *scr = frame->screen_ptr;
213 /* already on top */
214 if (frame->stacking->above == NULL) {
215 return;
218 /* insert it on top of other windows on the same level */
219 if (frame->stacking->under)
220 frame->stacking->under->stacking->above = frame->stacking->above;
221 if (frame->stacking->above)
222 frame->stacking->above->stacking->under = frame->stacking->under;
224 frame->stacking->above = NULL;
225 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
226 if (frame->stacking->under) {
227 frame->stacking->under->stacking->above = frame;
229 WMSetInBag(scr->stacking_list, level, frame);
231 /* raise transients under us from bottom to top
232 * so that the order is kept */
233 again:
234 wlist = frame->stacking->under;
235 while (wlist && wlist->stacking->under)
236 wlist = wlist->stacking->under;
237 while (wlist && wlist != frame) {
238 if (wlist->stacking->child_of == frame) {
239 wRaiseFrame(wlist);
240 goto again;
242 wlist = wlist->stacking->above;
245 /* try to optimize things a little */
246 if (frame->stacking->above == NULL) {
247 WMBagIterator iter;
248 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
249 int i, last = above->stacking->window_level;
251 /* find the 1st level above us which has windows in it */
252 for (i = level + 1, above = NULL; i <= last; i++) {
253 above = WMGetFromBag(scr->stacking_list, i);
254 if (above != NULL)
255 break;
258 if (above != frame && above != NULL) {
259 while (above->stacking->under)
260 above = above->stacking->under;
261 moveFrameToUnder(above, frame);
262 } else {
263 /* no window above us */
264 above = NULL;
265 XRaiseWindow(dpy, frame->window);
267 } else {
268 moveFrameToUnder(frame->stacking->above, frame);
271 notifyStackChange(frame, "raise");
274 void wRaiseLowerFrame(WCoreWindow * frame)
276 if (!frame->stacking->above
277 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
279 wLowerFrame(frame);
280 } else {
281 WCoreWindow *scan = frame->stacking->above;
282 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
284 while (scan) {
286 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
287 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
289 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
290 && scan_wwin->flags.mapped) {
291 break;
294 scan = scan->stacking->above;
297 if (scan) {
298 wRaiseFrame(frame);
299 } else {
300 wLowerFrame(frame);
305 void wLowerFrame(WCoreWindow * frame)
307 WScreen *scr = frame->screen_ptr;
308 WCoreWindow *wlist = frame;
309 int level = frame->stacking->window_level;
311 /* already in bottom */
312 if (wlist->stacking->under == NULL) {
313 return;
315 /* can't lower transient below below its owner */
316 if (wlist->stacking->under == wlist->stacking->child_of) {
317 return;
319 /* remove from the list */
320 if (WMGetFromBag(scr->stacking_list, level) == frame) {
321 /* it was the top window */
322 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
323 frame->stacking->under->stacking->above = NULL;
324 } else {
325 if (frame->stacking->under)
326 frame->stacking->under->stacking->above = frame->stacking->above;
327 if (frame->stacking->above)
328 frame->stacking->above->stacking->under = frame->stacking->under;
330 wlist = WMGetFromBag(scr->stacking_list, level);
332 /* look for place to put this window */
333 if (wlist) {
334 WCoreWindow *owner = frame->stacking->child_of;
336 if (owner != wlist) {
337 while (wlist->stacking->under) {
338 /* if this is a transient, it should not be placed under
339 * its owner */
340 if (owner == wlist->stacking->under)
341 break;
342 wlist = wlist->stacking->under;
346 /* insert under the place found */
347 frame->stacking->above = wlist;
348 if (wlist) {
349 frame->stacking->under = wlist->stacking->under;
350 if (wlist->stacking->under)
351 wlist->stacking->under->stacking->above = frame;
352 wlist->stacking->under = frame;
353 } else {
354 frame->stacking->under = NULL;
357 if (frame->stacking->above == NULL) {
358 WMBagIterator iter;
359 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
360 int i, last = above->stacking->window_level;
362 /* find the 1st level above us which has windows in it */
363 for (i = level + 1, above = NULL; i <= last; i++) {
364 above = WMGetFromBag(scr->stacking_list, i);
365 if (above != NULL)
366 break;
369 if (above != frame && above != NULL) {
370 while (above->stacking->under)
371 above = above->stacking->under;
372 moveFrameToUnder(above, frame);
373 } else {
374 /* no window below us */
375 XLowerWindow(dpy, frame->window);
377 } else {
378 moveFrameToUnder(frame->stacking->above, frame);
381 notifyStackChange(frame, "lower");
385 *----------------------------------------------------------------------
386 * AddToStackList--
387 * Inserts the frame in the top of the stacking list. The
388 * stacking precedence is obeyed.
390 * Returns:
391 * None
393 * Side effects:
394 * The frame is added to its screen's window list.
395 *----------------------------------------------------------------------
397 void AddToStackList(WCoreWindow * frame)
399 WCoreWindow *curtop, *wlist;
400 int index = frame->stacking->window_level;
401 WScreen *scr = frame->screen_ptr;
402 WCoreWindow *trans = NULL;
404 frame->screen_ptr->window_count++;
405 XSaveContext(dpy, frame->window, w_global.context.stack, (XPointer) frame);
406 curtop = WMGetFromBag(scr->stacking_list, index);
408 /* first window in this level */
409 if (curtop == NULL) {
410 WMSetInBag(scr->stacking_list, index, frame);
411 frame->stacking->above = NULL;
412 frame->stacking->under = NULL;
413 CommitStacking(scr);
414 return;
417 /* check if this is a transient owner */
418 wlist = curtop;
419 while (wlist) {
420 if (wlist->stacking->child_of == frame)
421 trans = wlist;
422 wlist = wlist->stacking->under;
424 /* trans will hold the transient in the lowest position
425 * in stacking list */
427 frame->stacking->above = trans;
428 if (trans != NULL) {
429 /* window is owner of a transient.. put it below
430 * the lowest transient */
431 frame->stacking->under = trans->stacking->under;
432 if (trans->stacking->under) {
433 trans->stacking->under->stacking->above = frame;
435 trans->stacking->under = frame;
436 } else {
437 /* window is not owner of transients.. just put it in the
438 * top of other windows */
439 frame->stacking->under = curtop;
440 curtop->stacking->above = frame;
441 WMSetInBag(scr->stacking_list, index, frame);
443 CommitStacking(scr);
447 *----------------------------------------------------------------------
448 * MoveInStackListAbove--
449 * Moves the frame above "next".
451 * Returns:
452 * None
454 * Side effects:
455 * Stacking order may be changed.
456 * Window level for frame may be changed.
457 *----------------------------------------------------------------------
459 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
461 WCoreWindow *tmpw;
462 WScreen *scr = frame->screen_ptr;
463 int index;
465 if (!next || frame->stacking->under == next)
466 return;
468 if (frame->stacking->window_level != next->stacking->window_level)
469 ChangeStackingLevel(frame, next->stacking->window_level);
471 index = frame->stacking->window_level;
473 tmpw = WMGetFromBag(scr->stacking_list, index);
474 if (tmpw == frame)
475 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
476 if (frame->stacking->under)
477 frame->stacking->under->stacking->above = frame->stacking->above;
478 if (frame->stacking->above)
479 frame->stacking->above->stacking->under = frame->stacking->under;
480 if (next->stacking->above)
481 next->stacking->above->stacking->under = frame;
482 frame->stacking->under = next;
483 frame->stacking->above = next->stacking->above;
484 next->stacking->above = frame;
485 if (tmpw == next)
486 WMSetInBag(scr->stacking_list, index, frame);
488 /* try to optimize things a little */
489 if (frame->stacking->above == NULL) {
490 WCoreWindow *above = NULL;
491 WMBagIterator iter;
493 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
494 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
496 /* can't optimize */
497 while (above->stacking->under)
498 above = above->stacking->under;
499 break;
501 if (above == NULL) {
502 XRaiseWindow(dpy, frame->window);
503 } else {
504 moveFrameToUnder(above, frame);
506 } else {
507 moveFrameToUnder(frame->stacking->above, frame);
510 WMPostNotificationName(WMNResetStacking, scr, NULL);
514 *----------------------------------------------------------------------
515 * MoveInStackListUnder--
516 * Moves the frame to under "prev".
518 * Returns:
519 * None
521 * Side effects:
522 * Stacking order may be changed.
523 * Window level for frame may be changed.
524 *----------------------------------------------------------------------
526 void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
528 WCoreWindow *tmpw;
529 int index;
530 WScreen *scr = frame->screen_ptr;
532 if (!prev || frame->stacking->above == prev)
533 return;
535 if (frame->stacking->window_level != prev->stacking->window_level)
536 ChangeStackingLevel(frame, prev->stacking->window_level);
538 index = frame->stacking->window_level;
540 tmpw = WMGetFromBag(scr->stacking_list, index);
541 if (tmpw == frame)
542 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
543 if (frame->stacking->under)
544 frame->stacking->under->stacking->above = frame->stacking->above;
545 if (frame->stacking->above)
546 frame->stacking->above->stacking->under = frame->stacking->under;
547 if (prev->stacking->under)
548 prev->stacking->under->stacking->above = frame;
549 frame->stacking->above = prev;
550 frame->stacking->under = prev->stacking->under;
551 prev->stacking->under = frame;
552 moveFrameToUnder(prev, frame);
554 WMPostNotificationName(WMNResetStacking, scr, NULL);
557 void RemoveFromStackList(WCoreWindow * frame)
559 int index = frame->stacking->window_level;
561 if (XDeleteContext(dpy, frame->window, w_global.context.stack) == XCNOENT) {
562 wwarning("RemoveFromStackingList(): window not in list ");
563 return;
565 /* remove from the window stack list */
566 if (frame->stacking->under)
567 frame->stacking->under->stacking->above = frame->stacking->above;
568 if (frame->stacking->above)
569 frame->stacking->above->stacking->under = frame->stacking->under;
570 else /* this was the first window on the list */
571 WMSetInBag(frame->screen_ptr->stacking_list, index, frame->stacking->under);
573 frame->screen_ptr->window_count--;
575 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
578 void ChangeStackingLevel(WCoreWindow * frame, int new_level)
580 int old_level;
582 if (frame->stacking->window_level == new_level)
583 return;
584 old_level = frame->stacking->window_level;
586 RemoveFromStackList(frame);
587 frame->stacking->window_level = new_level;
588 AddToStackList(frame);
589 if (old_level > new_level) {
590 wRaiseFrame(frame);
591 } else {
592 wLowerFrame(frame);