]> icculus.org git repositories - dana/openbox.git/blob - openbox/stacking.c
keep all transient windows above their parents
[dana/openbox.git] / openbox / stacking.c
1 #include "openbox.h"
2 #include "prop.h"
3 #include "focus.h"
4 #include "client.h"
5 #include "group.h"
6 #include "frame.h"
7 #include <glib.h>
8
9 GList  *stacking_list = NULL;
10
11 void stacking_set_list()
12 {
13     Window *windows, *win_it;
14     GList *it;
15     guint size = g_list_length(stacking_list);
16
17     /* on shutdown, don't update the properties, so that we can read it back
18        in on startup and re-stack the windows as they were before we shut down
19     */
20     if (ob_state == State_Exiting) return;
21
22     /* create an array of the window ids (from bottom to top,
23        reverse order!) */
24     if (size > 0) {
25         windows = g_new(Window, size);
26         win_it = windows;
27         for (it = g_list_last(stacking_list); it != NULL;
28              it = it->prev, ++win_it)
29             *win_it = ((Client*)it->data)->window;
30     } else
31         windows = NULL;
32
33     PROP_SET32A(ob_root, net_client_list_stacking, window, windows, size);
34
35     if (windows)
36         g_free(windows);
37 }
38
39 static GList *find_lowest_transient(Client *c)
40 {
41     GList *it;
42     GSList *sit;
43
44     for (it = g_list_last(stacking_list); it; it = it->prev)
45         for (sit = c->transients; sit; sit = sit->next)
46             if (it->data == sit->data) /* found a transient */
47                 if (((Client*)it->data)->layer == c->layer) /* same layer? */
48                     return it;
49     return NULL;
50 }
51
52 void stacking_raise(Client *client)
53 {
54     Window wins[2];  /* only ever restack 2 windows. */
55     GList *it;
56     GSList *sit;
57
58     g_assert(stacking_list != NULL); /* this would be bad */
59
60     /* if we have a transients raise them */
61     for (sit = client->transients; sit; sit = sit->next)
62         stacking_raise(sit->data);
63
64     /* remove the client before looking so we can't run into ourselves */
65     stacking_list = g_list_remove(stacking_list, client);
66   
67     /* find 'it' where it is the positiion in the stacking order where
68        'client' will be inserted *after* */
69
70     it = find_lowest_transient(client);
71     if (it) {
72         it = it->next;
73     } else {
74         /* the stacking list is from highest to lowest */
75         for (it = stacking_list; it; it = it->next) {
76             if (client->layer >= ((Client*)it->data)->layer)
77                 break;
78         }
79     }
80
81     /*
82       if our new position is the top, we want to stack under the focus_backup.
83       otherwise, we want to stack under the previous window in the stack.
84     */
85     if (it == stacking_list)
86         wins[0] = focus_backup;
87     else if (it != NULL)
88         wins[0] = ((Client*)it->prev->data)->frame->window;
89     else
90         wins[0] = ((Client*)g_list_last(stacking_list)->data)->frame->window;
91     wins[1] = client->frame->window;
92
93     stacking_list = g_list_insert_before(stacking_list, it, client);
94
95     XRestackWindows(ob_display, wins, 2);
96
97     stacking_set_list();
98 }
99
100 static void lower_recursive(Client *client, Client *above)
101 {
102     Window wins[2];  /* only ever restack 2 windows. */
103     GList *it;
104     GSList *sit;
105
106     it = g_list_last(stacking_list);
107
108     /* find 'it' where 'it' is the position in the stacking_list where the
109        'client' will be placed *after* */
110
111     while (it != stacking_list) {
112         Client *c = it->data;
113         if (client->layer <= c->layer && c != above)
114             break;
115         it = it->prev;
116     }
117
118     if (it->data != client) { /* not already the bottom */
119         wins[0] = ((Client*)it->data)->frame->window;
120         wins[1] = client->frame->window;
121
122         stacking_list = g_list_remove(stacking_list, client);
123         stacking_list = g_list_insert_before(stacking_list,
124                                          it->next, client);
125         XRestackWindows(ob_display, wins, 2);
126     }
127
128     for (sit = client->transients; sit; sit = sit->next)
129         lower_recursive(sit->data, client);
130
131     if (!above) /* only need to do this once */
132         stacking_set_list();
133 }
134
135 void stacking_lower(Client *client)
136 {
137     g_assert(stacking_list != NULL); /* this would be bad */
138
139     /* move up the transient chain as far as possible first */
140     while (client->transient_for) {
141         if (client->transient_for != TRAN_GROUP) {
142             client = client->transient_for;
143         } else {
144             GSList *it;
145
146             /* the check for TRAN_GROUP is to prevent an infinate loop with
147                2 transients of the same group at the head of the group's
148                members list */
149             for (it = client->group->members; it; it = it->next) {
150                 Client *c = it->data;
151
152                 if (c != client && c->transient_for != TRAN_GROUP) {
153                     client = it->data;
154                     break;
155                 }
156             }
157             if (it == NULL) break;
158         }
159     }
160
161     lower_recursive(client, NULL);
162 }
163