]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions/growtoedge.c
Remove incorrect assertion for MoveResizeTo
[mikachu/openbox.git] / openbox / actions / growtoedge.c
1 #include "openbox/actions.h"
2 #include "openbox/misc.h"
3 #include "openbox/client.h"
4 #include "openbox/frame.h"
5 #include "openbox/screen.h"
6 #include <glib.h>
7
8 typedef struct {
9     ObDirection dir;
10     gboolean shrink;
11     gboolean fill;
12 } Options;
13
14 static gpointer setup_grow_func(xmlNodePtr node);
15 static gpointer setup_fill_func(xmlNodePtr node);
16 static gpointer setup_shrink_func(xmlNodePtr node);
17 static void free_func(gpointer o);
18 static gboolean run_func(ObActionsData *data, gpointer options);
19 /* 3.4-compatibility */
20 static gpointer setup_north_func(xmlNodePtr node);
21 static gpointer setup_south_func(xmlNodePtr node);
22 static gpointer setup_east_func(xmlNodePtr node);
23 static gpointer setup_west_func(xmlNodePtr node);
24
25 void action_growtoedge_startup(void)
26 {
27     actions_register("GrowToEdge", setup_grow_func,
28                      free_func, run_func);
29     actions_register("GrowToFill", setup_fill_func,
30                      free_func, run_func);
31     actions_register("ShrinkToEdge", setup_shrink_func,
32                      free_func, run_func);
33     /* 3.4-compatibility */
34     actions_register("GrowToEdgeNorth", setup_north_func, free_func, run_func);
35     actions_register("GrowToEdgeSouth", setup_south_func, free_func, run_func);
36     actions_register("GrowToEdgeEast", setup_east_func, free_func, run_func);
37     actions_register("GrowToEdgeWest", setup_west_func, free_func, run_func);
38 }
39
40 static gpointer setup_func(xmlNodePtr node)
41 {
42     xmlNodePtr n;
43     Options *o;
44
45     o = g_slice_new0(Options);
46     o->dir = OB_DIRECTION_NORTH;
47
48     if ((n = obt_xml_find_node(node, "direction"))) {
49         gchar *s = obt_xml_node_string(n);
50         if (!g_ascii_strcasecmp(s, "north") ||
51             !g_ascii_strcasecmp(s, "up"))
52             o->dir = OB_DIRECTION_NORTH;
53         else if (!g_ascii_strcasecmp(s, "south") ||
54                  !g_ascii_strcasecmp(s, "down"))
55             o->dir = OB_DIRECTION_SOUTH;
56         else if (!g_ascii_strcasecmp(s, "west") ||
57                  !g_ascii_strcasecmp(s, "left"))
58             o->dir = OB_DIRECTION_WEST;
59         else if (!g_ascii_strcasecmp(s, "east") ||
60                  !g_ascii_strcasecmp(s, "right"))
61             o->dir = OB_DIRECTION_EAST;
62         g_free(s);
63     }
64
65     return o;
66 }
67
68 static gpointer setup_grow_func(xmlNodePtr node)
69 {
70     Options *o;
71
72     o = setup_func(node);
73     o->shrink = FALSE;
74     o->fill = FALSE;
75
76     return o;
77 }
78
79 static gpointer setup_fill_func(xmlNodePtr node)
80 {
81     Options *o;
82
83     o = setup_func(node);
84     o->shrink = FALSE;
85     o->fill = TRUE;
86
87     return o;
88 }
89
90 static gpointer setup_shrink_func(xmlNodePtr node)
91 {
92     Options *o;
93
94     o = setup_func(node);
95     o->shrink = TRUE;
96     o->fill = FALSE;
97
98     return o;
99 }
100
101 static gboolean do_grow(ObActionsData *data, gint x, gint y, gint w, gint h)
102 {
103     gint realw, realh, lw, lh;
104
105     realw = w;
106     realh = h;
107     client_try_configure(data->client, &x, &y, &realw, &realh,
108                          &lw, &lh, TRUE);
109     /* if it's going to be resized smaller than it intended, don't
110        move the window over */
111     if (x != data->client->area.x) x += w - realw;
112     if (y != data->client->area.y) y += h - realh;
113
114     if (x != data->client->area.x || y != data->client->area.y ||
115         realw != data->client->area.width ||
116         realh != data->client->area.height)
117     {
118         actions_client_move(data, TRUE);
119         client_move_resize(data->client, x, y, realw, realh);
120         actions_client_move(data, FALSE);
121         return TRUE;
122     }
123     return FALSE;
124 }
125
126 static gboolean do_grow_all_edges(ObActionsData* data,
127                                   ObClientDirectionalResizeType resize_type)
128 {
129     gint x, y, w, h;
130     gint temp_x, temp_y, temp_w, temp_h;
131
132     client_find_resize_directional(data->client,
133                                    OB_DIRECTION_NORTH,
134                                    resize_type,
135                                    &temp_x, &temp_y, &temp_w, &temp_h);
136     y = temp_y;
137     h = temp_h;
138
139     client_find_resize_directional(data->client,
140                                    OB_DIRECTION_SOUTH,
141                                    resize_type,
142                                    &temp_x, &temp_y, &temp_w, &temp_h);
143     h += temp_h - data->client->area.height;
144
145
146     client_find_resize_directional(data->client,
147                                    OB_DIRECTION_WEST,
148                                    resize_type,
149                                    &temp_x, &temp_y, &temp_w, &temp_h);
150     x = temp_x;
151     w = temp_w;
152
153     client_find_resize_directional(data->client,
154                                    OB_DIRECTION_EAST,
155                                    resize_type,
156                                    &temp_x, &temp_y, &temp_w, &temp_h);
157     w += temp_w - data->client->area.width;
158
159     /* When filling, we allow the window to move to an arbitrary x/y
160        position, since we'll be growing the other edge as well. */
161     int lw, lh;
162     client_try_configure(data->client, &x, &y, &w, &h, &lw, &lh, TRUE);
163
164     if (x == data->client->area.x &&
165         y == data->client->area.y &&
166         w == data->client->area.width &&
167         h == data->client->area.height)
168     {
169         return FALSE;
170     }
171
172     actions_client_move(data, TRUE);
173     client_move_resize(data->client, x, y, w, h);
174     actions_client_move(data, FALSE);
175     return TRUE;
176 }
177
178 static void free_func(gpointer o)
179 {
180     g_slice_free(Options, o);
181 }
182
183 /* Always return FALSE because its not interactive */
184 static gboolean run_func(ObActionsData *data, gpointer options)
185 {
186     Options *o = options;
187
188     if (!data->client ||
189         actions_client_locked(data))
190         return FALSE;
191
192     gboolean doing_vertical_resize =
193         o->dir == OB_DIRECTION_NORTH ||
194         o->dir == OB_DIRECTION_SOUTH ||
195         o->fill;
196     if (data->client->shaded && doing_vertical_resize)
197             return FALSE;
198
199     if (o->fill) {
200         if (o->shrink) {
201             /* We don't have any implementation of shrinking for the FillToGrow
202                action. */
203             return FALSE;
204         }
205
206         if (do_grow_all_edges(data, CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE))
207             return FALSE;
208
209         /* If all the edges are blocked, then allow them to jump past their
210            current block points. */
211         do_grow_all_edges(data, CLIENT_RESIZE_GROW);
212         return FALSE;
213     }
214
215     if (!o->shrink) {
216         gint x, y, w, h;
217
218         /* Try grow. */
219         client_find_resize_directional(data->client,
220                                        o->dir,
221                                        CLIENT_RESIZE_GROW,
222                                        &x, &y, &w, &h);
223
224         if (do_grow(data, x, y, w, h))
225             return FALSE;
226     }
227
228     /* We couldn't grow, so try shrink! */
229     ObDirection opposite =
230         (o->dir == OB_DIRECTION_NORTH ? OB_DIRECTION_SOUTH :
231          (o->dir == OB_DIRECTION_SOUTH ? OB_DIRECTION_NORTH :
232           (o->dir == OB_DIRECTION_EAST ? OB_DIRECTION_WEST :
233            OB_DIRECTION_EAST)));
234
235     gint x, y, w, h;
236     gint half;
237
238     client_find_resize_directional(data->client,
239                                    opposite,
240                                    CLIENT_RESIZE_SHRINK,
241                                    &x, &y, &w, &h);
242
243     switch (opposite) {
244     case OB_DIRECTION_NORTH:
245         half = data->client->area.y + data->client->area.height / 2;
246         if (y > half) {
247             h += y - half;
248             y = half;
249         }
250         break;
251     case OB_DIRECTION_SOUTH:
252         half = data->client->area.height / 2;
253         if (h < half)
254             h = half;
255         break;
256     case OB_DIRECTION_WEST:
257         half = data->client->area.x + data->client->area.width / 2;
258         if (x > half) {
259             w += x - half;
260             x = half;
261         }
262         break;
263     case OB_DIRECTION_EAST:
264         half = data->client->area.width / 2;
265         if (w < half)
266             w = half;
267         break;
268     default: g_assert_not_reached();
269     }
270     if (do_grow(data, x, y, w, h))
271         return FALSE;
272
273     return FALSE;
274 }
275
276 /* 3.4-compatibility */
277 static gpointer setup_north_func(xmlNodePtr node)
278 {
279     Options *o = g_slice_new0(Options);
280     o->shrink = FALSE;
281     o->dir = OB_DIRECTION_NORTH;
282     return o;
283 }
284
285 static gpointer setup_south_func(xmlNodePtr node)
286 {
287     Options *o = g_slice_new0(Options);
288     o->shrink = FALSE;
289     o->dir = OB_DIRECTION_SOUTH;
290     return o;
291 }
292
293 static gpointer setup_east_func(xmlNodePtr node)
294 {
295     Options *o = g_slice_new0(Options);
296     o->shrink = FALSE;
297     o->dir = OB_DIRECTION_EAST;
298     return o;
299 }
300
301 static gpointer setup_west_func(xmlNodePtr node)
302 {
303     Options *o = g_slice_new0(Options);
304     o->shrink = FALSE;
305     o->dir = OB_DIRECTION_WEST;
306     return o;
307 }