3a213806619fb9be16ec0202a885cf72beab9282
[dana/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 void free_func(gpointer o)
127 {
128     g_slice_free(Options, o);
129 }
130
131 /* Always return FALSE because its not interactive */
132 static gboolean run_func(ObActionsData *data, gpointer options)
133 {
134     Options *o = options;
135     gint x, y, w, h;
136
137     gint half;
138
139     if (!data->client)
140         return FALSE;
141     if (data->client->shaded) {
142         gboolean doing_verical_resize =
143             o->dir == OB_DIRECTION_NORTH ||
144             o->dir == OB_DIRECTION_SOUTH ||
145             o->fill;
146         if (doing_verical_resize)
147             return FALSE;
148     }
149
150     if (o->fill) {
151         if (o->shrink) {
152             /* We don't have any implementation of shrinking for the FillToGrow
153                action. */
154             return FALSE;
155         }
156
157         ObClientDirectionalResizeType grow = CLIENT_RESIZE_GROW_IF_NOT_ON_EDGE;
158
159         gint temp_x;
160         gint temp_y;
161         gint temp_w;
162         gint temp_h;
163
164         client_find_resize_directional(data->client,
165                                        OB_DIRECTION_NORTH,
166                                        grow,
167                                        &temp_x, &temp_y, &temp_w, &temp_h);
168         y = temp_y;
169         h = temp_h;
170
171         client_find_resize_directional(data->client,
172                                        OB_DIRECTION_SOUTH,
173                                        grow,
174                                        &temp_x, &temp_y, &temp_w, &temp_h);
175         h += temp_h - data->client->area.height;
176
177
178         client_find_resize_directional(data->client,
179                                        OB_DIRECTION_WEST,
180                                        grow,
181                                        &temp_x, &temp_y, &temp_w, &temp_h);
182         x = temp_x;
183         w = temp_w;
184
185         client_find_resize_directional(data->client,
186                                        OB_DIRECTION_EAST,
187                                        grow,
188                                        &temp_x, &temp_y, &temp_w, &temp_h);
189         w += temp_w - data->client->area.width;
190
191         /* When filling, we allow the window to move to an arbitrary x/y
192            position, since we'll be growing the other edge as well. */
193         if (x != data->client->area.x || y != data->client->area.y ||
194             w != data->client->area.width || h != data->client->area.height)
195         {
196             actions_client_move(data, TRUE);
197             client_move_resize(data->client, x, y, w, h);
198             actions_client_move(data, FALSE);
199         }
200         return FALSE;
201     }
202
203     if (!o->shrink) {
204         /* Try grow. */
205         client_find_resize_directional(data->client,
206                                        o->dir,
207                                        CLIENT_RESIZE_GROW,
208                                        &x, &y, &w, &h);
209
210         if (do_grow(data, x, y, w, h))
211             return FALSE;
212     }
213
214     /* We couldn't grow, so try shrink! */
215     ObDirection opposite =
216         (o->dir == OB_DIRECTION_NORTH ? OB_DIRECTION_SOUTH :
217          (o->dir == OB_DIRECTION_SOUTH ? OB_DIRECTION_NORTH :
218           (o->dir == OB_DIRECTION_EAST ? OB_DIRECTION_WEST :
219            OB_DIRECTION_EAST)));
220     client_find_resize_directional(data->client,
221                                    opposite,
222                                    CLIENT_RESIZE_SHRINK,
223                                    &x, &y, &w, &h);
224     switch (opposite) {
225     case OB_DIRECTION_NORTH:
226         half = data->client->area.y + data->client->area.height / 2;
227         if (y > half) {
228             h += y - half;
229             y = half;
230         }
231         break;
232     case OB_DIRECTION_SOUTH:
233         half = data->client->area.height / 2;
234         if (h < half)
235             h = half;
236         break;
237     case OB_DIRECTION_WEST:
238         half = data->client->area.x + data->client->area.width / 2;
239         if (x > half) {
240             w += x - half;
241             x = half;
242         }
243         break;
244     case OB_DIRECTION_EAST:
245         half = data->client->area.width / 2;
246         if (w < half)
247             w = half;
248         break;
249     default: g_assert_not_reached();
250     }
251     if (do_grow(data, x, y, w, h))
252         return FALSE;
253
254     return FALSE;
255 }
256
257 /* 3.4-compatibility */
258 static gpointer setup_north_func(xmlNodePtr node)
259 {
260     Options *o = g_slice_new0(Options);
261     o->shrink = FALSE;
262     o->dir = OB_DIRECTION_NORTH;
263     return o;
264 }
265
266 static gpointer setup_south_func(xmlNodePtr node)
267 {
268     Options *o = g_slice_new0(Options);
269     o->shrink = FALSE;
270     o->dir = OB_DIRECTION_SOUTH;
271     return o;
272 }
273
274 static gpointer setup_east_func(xmlNodePtr node)
275 {
276     Options *o = g_slice_new0(Options);
277     o->shrink = FALSE;
278     o->dir = OB_DIRECTION_EAST;
279     return o;
280 }
281
282 static gpointer setup_west_func(xmlNodePtr node)
283 {
284     Options *o = g_slice_new0(Options);
285     o->shrink = FALSE;
286     o->dir = OB_DIRECTION_WEST;
287     return o;
288 }