support for transients of groups
[dana/openbox-history.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 void stacking_raise(Client *client)
40 {
41     Window wins[2];  /* only ever restack 2 windows. */
42     GList *it;
43     Client *m;
44
45     g_assert(stacking_list != NULL); /* this would be bad */
46
47     m = client_find_modal_child(client);
48     /* if we have a modal child, raise it instead, we'll go along tho later */
49     if (m) stacking_raise(m);
50   
51     /* remove the client before looking so we can't run into ourselves */
52     stacking_list = g_list_remove(stacking_list, client);
53   
54     /* the stacking list is from highest to lowest */
55     it = stacking_list;
56     while (it != NULL) {
57         Client *c = it->data;
58         if (client->layer >= c->layer && m != c)
59             break;
60         it = it->next;
61     }
62
63     /*
64       if our new position is the top, we want to stack under the focus_backup.
65       otherwise, we want to stack under the previous window in the stack.
66     */
67     if (it == stacking_list)
68         wins[0] = focus_backup;
69     else if (it != NULL)
70         wins[0] = ((Client*)it->prev->data)->frame->window;
71     else
72         wins[0] = ((Client*)g_list_last(stacking_list)->data)->frame->window;
73     wins[1] = client->frame->window;
74
75     stacking_list = g_list_insert_before(stacking_list, it, client);
76
77     XRestackWindows(ob_display, wins, 2);
78
79     stacking_set_list();
80 }
81
82 void stacking_lower(Client *client)
83 {
84     Window wins[2];  /* only ever restack 2 windows. */
85     GList *it;
86
87     g_assert(stacking_list != NULL); /* this would be bad */
88
89     if (client->modal && client->transient_for) {
90         if (client->transient_for == TRAN_GROUP) {
91             /* don't let a modal of the group lower below any other windows
92                in the group */
93             for (it = stacking_list; it; it = it->next) {
94                 GSList *sit;
95                 Client *c = it->data;
96
97                 if (it->data == client) continue;
98
99                 for (sit = c->group->members; sit; sit = sit->next)
100                     if (sit->data == it->data) break;
101                 if (sit) break; /* got it */
102             }
103             if (it == NULL)
104                 goto lower_no_parent;
105         } else {
106             /* don't let a modal window lower below its transient_for */
107             it = g_list_find(stacking_list, client->transient_for);
108         }
109         g_assert(it != NULL);
110
111         wins[0] = (it == stacking_list ? focus_backup :
112                    ((Client*)it->prev->data)->frame->window);
113         wins[1] = client->frame->window;
114         if (wins[0] == wins[1]) return; /* already right above the window */
115
116         stacking_list = g_list_remove(stacking_list, client);
117         stacking_list = g_list_insert_before(stacking_list, it, client);
118     } else {
119     lower_no_parent:
120
121         it = g_list_last(stacking_list);
122
123         while (it != stacking_list) {
124             Client *c = it->data;
125             if (client->layer <= c->layer)
126                 break;
127             it = it->prev;
128         }
129         if (it->data == client) return; /* already the bottom, return */
130
131         wins[0] = ((Client*)it->data)->frame->window;
132         wins[1] = client->frame->window;
133
134         stacking_list = g_list_remove(stacking_list, client);
135         stacking_list = g_list_insert_before(stacking_list,
136                                              it->next, client);
137     }
138
139     XRestackWindows(ob_display, wins, 2);
140     stacking_set_list();
141 }
142