]> icculus.org git repositories - dana/openbox.git/blob - openbox/stacking.c
raise grouped util windows with main windows
[dana/openbox.git] / openbox / stacking.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    stacking.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "openbox.h"
20 #include "prop.h"
21 #include "screen.h"
22 #include "focus.h"
23 #include "client.h"
24 #include "group.h"
25 #include "frame.h"
26 #include "window.h"
27
28 GList  *stacking_list = NULL;
29
30 void stacking_set_list()
31 {
32     Window *windows = NULL;
33     GList *it;
34     guint i = 0;
35
36     /* on shutdown, don't update the properties, so that we can read it back
37        in on startup and re-stack the windows as they were before we shut down
38     */
39     if (ob_state() == OB_STATE_EXITING) return;
40
41     /* create an array of the window ids (from bottom to top,
42        reverse order!) */
43     if (stacking_list) {
44         windows = g_new(Window, g_list_length(stacking_list));
45         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
46             if (WINDOW_IS_CLIENT(it->data))
47                 windows[i++] = WINDOW_AS_CLIENT(it->data)->window;
48         }
49     }
50
51     PROP_SETA32(RootWindow(ob_display, ob_screen),
52                 net_client_list_stacking, window, (guint32*)windows, i);
53
54     g_free(windows);
55 }
56
57 static void do_restack(GList *wins, GList *before)
58 {
59     GList *it, *next;
60     Window *win;
61     int i;
62
63 #ifdef DEBUG
64     /* pls only restack stuff in the same layer at a time */
65     for (it = wins; it; it = next) {
66         next = g_list_next(it);
67         if (!next) break;
68         g_assert (window_layer(it->data) == window_layer(next->data));
69     }
70     if (before)
71         g_assert(window_layer(it->data) >= window_layer(before->data));
72 #endif
73
74     win = g_new(Window, g_list_length(wins) + 1);
75
76     if (before == stacking_list)
77         win[0] = screen_support_win;
78     else if (!before)
79         win[0] = window_top(g_list_last(stacking_list)->data);
80     else
81         win[0] = window_top(g_list_previous(before)->data);
82
83     for (i = 1, it = wins; it; ++i, it = g_list_next(it)) {
84         win[i] = window_top(it->data);
85         g_assert(win[i] != None); /* better not call stacking shit before
86                                      setting your top level window value */
87         stacking_list = g_list_insert_before(stacking_list, before, it->data);
88     }
89
90 #ifdef DEBUG
91     /* some debug checking of the stacking list's order */
92     for (it = stacking_list; ; it = next) {
93         next = g_list_next(it);
94         if (!next) break;
95         g_assert(window_layer(it->data) >= window_layer(next->data));
96     }
97 #endif
98
99     XRestackWindows(ob_display, win, i);
100     g_free(win);
101
102     stacking_set_list();
103 }
104
105 static void do_raise(GList *wins)
106 {
107     GList *it;
108     GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
109     int i;
110
111     for (it = wins; it; it = g_list_next(it)) {
112         ObStackingLayer l;
113
114         l = window_layer(it->data);
115         layer[l] = g_list_append(layer[l], it->data);
116     }
117
118     it = stacking_list;
119     for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
120         if (layer[i]) {
121             for (; it; it = g_list_next(it)) {
122                 /* look for the top of the layer */
123                 if (window_layer(it->data) <= (ObStackingLayer) i)
124                     break;
125             }
126             do_restack(layer[i], it);
127             g_list_free(layer[i]);
128         }
129     }
130 }
131
132 static void do_lower(GList *wins)
133 {
134     GList *it;
135     GList *layer[OB_NUM_STACKING_LAYERS] = {NULL};
136     int i;
137
138     for (it = wins; it; it = g_list_next(it)) {
139         ObStackingLayer l;
140
141         l = window_layer(it->data);
142         layer[l] = g_list_append(layer[l], it->data);
143     }
144
145     it = stacking_list;
146     for (i = OB_NUM_STACKING_LAYERS - 1; i >= 0; --i) {
147         if (layer[i]) {
148             for (; it; it = g_list_next(it)) {
149                 /* look for the top of the next layer down */
150                 if (window_layer(it->data) < (ObStackingLayer) i)
151                     break;
152             }
153             do_restack(layer[i], it);
154             g_list_free(layer[i]);
155         }
156     }
157 }
158
159 static GList *pick_windows(ObClient *top, ObClient *selected, gboolean raise)
160 {
161     GList *ret = NULL;
162     GList *it, *next, *prev;
163     GSList *sit;
164     int i, n;
165     GList *modals = NULL;
166     GList *trans = NULL;
167     GList *modal_sel = NULL; /* the selected guys if modal */
168     GList *trans_sel = NULL; /* the selected guys if not */
169
170     /* remove first so we can't run into ourself */
171     if ((it = g_list_find(stacking_list, top)))
172         stacking_list = g_list_delete_link(stacking_list, it);
173     else
174         return NULL;
175
176     i = 0;
177     n = g_slist_length(top->transients);
178     for (it = stacking_list; i < n && it; it = next) {
179         prev = g_list_previous(it);
180         next = g_list_next(it);
181
182         if ((sit = g_slist_find(top->transients, it->data))) {
183             ObClient *c = sit->data;
184             gboolean sel_child;
185
186             ++i;
187
188             if (c == selected)
189                 sel_child = TRUE;
190             else
191                 sel_child = client_search_transient(c, selected) != NULL;
192
193             if (!c->modal) {
194                 if (!sel_child) {
195                     trans = g_list_concat(trans,
196                                           pick_windows(c, selected, raise));
197                 } else {
198                     trans_sel = g_list_concat(trans_sel,
199                                                  pick_windows(c, selected,
200                                                               raise));
201                 }
202             } else {
203                 if (!sel_child) {
204                     modals = g_list_concat(modals,
205                                            pick_windows(c, selected, raise));
206                 } else {
207                     modal_sel = g_list_concat(modal_sel,
208                                                  pick_windows(c, selected,
209                                                               raise));
210                 }
211             }
212             /* if we dont have a prev then start back at the beginning,
213                otherwise skip back to the prev's next */
214             next = prev ? g_list_next(prev) : stacking_list;
215         }
216     }
217
218     ret = g_list_concat((raise ? modal_sel : modals),
219                         (raise ? modals : modal_sel));
220
221     ret = g_list_concat(ret, (raise ? trans_sel : trans));
222     ret = g_list_concat(ret, (raise ? trans : trans_sel));
223
224
225     /* add itself */
226     ret = g_list_append(ret, top);
227
228     return ret;
229 }
230
231 static GList *pick_group_windows(ObClient *top, ObClient *selected,
232                                  gboolean raise)
233 {
234     GList *ret = NULL;
235     GList *it, *next, *prev;
236     GSList *sit;
237     int i, n;
238
239     /* add group members in their stacking order */
240     if (top->group) {
241         i = 0;
242         n = g_slist_length(top->group->members) - 1;
243         for (it = stacking_list; i < n && it; it = next) {
244             prev = g_list_previous(it);
245             next = g_list_next(it);
246
247             if ((sit = g_slist_find(top->group->members, it->data))) {
248                 ObClientType t;
249
250                 ++i;
251
252                 t = ((ObClient*)it->data)->type;
253
254                 if (t == OB_CLIENT_TYPE_TOOLBAR ||
255                     t == OB_CLIENT_TYPE_MENU ||
256                     t == OB_CLIENT_TYPE_UTILITY)
257                 {
258                     ret = g_list_concat(ret,
259                                         pick_windows(sit->data,
260                                                      selected, raise)); 
261                     /* if we dont have a prev then start back at the beginning,
262                        otherwise skip back to the prev's next */
263                     next = prev ? g_list_next(prev) : stacking_list;
264                 }
265             }
266         }
267     }
268     return ret;
269 }
270
271 void stacking_raise(ObWindow *window)
272 {
273     GList *wins;
274
275     if (WINDOW_IS_CLIENT(window)) {
276         ObClient *c;
277         ObClient *selected;
278         selected = WINDOW_AS_CLIENT(window);
279         c = client_search_top_transient(selected);
280         wins = pick_windows(c, selected, TRUE);
281         wins = g_list_concat(wins, pick_group_windows(c, selected, TRUE));
282     } else {
283         wins = g_list_append(NULL, window);
284         stacking_list = g_list_remove(stacking_list, window);
285     }
286     do_raise(wins);
287     g_list_free(wins);
288 }
289
290 void stacking_lower(ObWindow *window)
291 {
292     GList *wins;
293
294     if (WINDOW_IS_CLIENT(window)) {
295         ObClient *c;
296         ObClient *selected;
297         selected = WINDOW_AS_CLIENT(window);
298         c = client_search_top_transient(selected);
299         wins = pick_windows(c, selected, FALSE);
300         wins = g_list_concat(pick_group_windows(c, selected, FALSE), wins);
301     } else {
302         wins = g_list_append(NULL, window);
303         stacking_list = g_list_remove(stacking_list, window);
304     }
305     do_lower(wins);
306     g_list_free(wins);
307 }
308
309 void stacking_below(ObWindow *window, ObWindow *below)
310 {
311     GList *wins, *before;
312
313     if (window_layer(window) != window_layer(below))
314         return;
315
316     wins = g_list_append(NULL, window);
317     stacking_list = g_list_remove(stacking_list, window);
318     before = g_list_next(g_list_find(stacking_list, below));
319     do_restack(wins, before);
320     g_list_free(wins);
321 }
322
323 void stacking_add(ObWindow *win)
324 {
325     ObStackingLayer l;
326     GList *wins;
327
328     g_assert(screen_support_win != None); /* make sure I dont break this in the
329                                        future */
330
331     l = window_layer(win);
332     wins = g_list_append(NULL, win); /* list of 1 element */
333
334     stacking_list = g_list_append(stacking_list, win);
335     stacking_raise(win);
336 }
337
338 void stacking_add_nonintrusive(ObWindow *win)
339 {
340     ObClient *client;
341     ObClient *parent = NULL;
342     GList *it_before = NULL;
343
344     if (!WINDOW_IS_CLIENT(win)) {
345         stacking_add(win); /* no special rules for others */
346         return;
347     }
348
349     client = WINDOW_AS_CLIENT(win);
350
351     /* insert above its highest parent */
352     if (client->transient_for) {
353         if (client->transient_for != OB_TRAN_GROUP) {
354             parent = client->transient_for;
355         } else {
356             GSList *sit;
357             GList *it;
358
359             if (client->group)
360                 for (it = stacking_list; !parent && it; it = it->next) {
361                     if ((sit = g_slist_find(client->group->members, it->data)))
362                 for (sit = client->group->members; !parent && sit;
363                      sit = sit->next) {
364                     ObClient *c = sit->data;
365                     /* checking transient_for prevents infinate loops! */
366                     if (sit->data == it->data && !c->transient_for)
367                         parent = it->data;
368                 }
369             }
370         }
371     }
372
373     if (!(it_before = g_list_find(stacking_list, parent))) {
374         /* no parent to put above, try find the focused client to go
375            under */
376         if (focus_client && focus_client->layer == client->layer) {
377             if ((it_before = g_list_find(stacking_list, focus_client)))
378                 it_before = it_before->next;
379         }
380     }
381     if (!it_before) {
382         /* out of ideas, just add it normally... */
383         stacking_add(win);
384     } else {
385         GList *wins = g_list_append(NULL, win);
386         do_restack(wins, it_before);
387         g_list_free(wins);
388     }
389 }