Merge branch 'backport' into work
[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 } Options;
12
13 static gpointer setup_func(xmlNodePtr node);
14 static gpointer setup_shrink_func(xmlNodePtr node);
15 static gboolean run_func(ObActionsData *data, gpointer options);
16 /* 3.4-compatibility */
17 static gpointer setup_north_func(xmlNodePtr node);
18 static gpointer setup_south_func(xmlNodePtr node);
19 static gpointer setup_east_func(xmlNodePtr node);
20 static gpointer setup_west_func(xmlNodePtr node);
21
22 void action_growtoedge_startup(void)
23 {
24     actions_register("GrowToEdge", setup_func,
25                      g_free, run_func, NULL, NULL);
26     actions_register("ShrinkToEdge", setup_shrink_func,
27                      g_free, run_func, NULL, NULL);
28     /* 3.4-compatibility */
29     actions_register("GrowToEdgeNorth", setup_north_func, g_free, run_func,
30                      NULL, NULL);
31     actions_register("GrowToEdgeSouth", setup_south_func, g_free, run_func,
32                      NULL, NULL);
33     actions_register("GrowToEdgeEast", setup_east_func, g_free, run_func,
34                      NULL, NULL);
35     actions_register("GrowToEdgeWest", setup_west_func, g_free, run_func,
36                      NULL, NULL);
37 }
38
39 static gpointer setup_func(xmlNodePtr node)
40 {
41     xmlNodePtr n;
42     Options *o;
43
44     o = g_new0(Options, 1);
45     o->dir = OB_DIRECTION_NORTH;
46     o->shrink = FALSE;
47
48     if ((n = obt_parse_find_node(node, "direction"))) {
49         gchar *s = obt_parse_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_shrink_func(xmlNodePtr node)
69 {
70     Options *o;
71
72     o = setup_func(node);
73     o->shrink = TRUE;
74
75     return o;
76 }
77
78 static gboolean do_grow(ObActionsData *data, gint x, gint y, gint w, gint h)
79 {
80     gint realw, realh, lw, lh;
81
82     realw = w;
83     realh = h;
84     client_try_configure(data->client, &x, &y, &realw, &realh,
85                          &lw, &lh, TRUE);
86     /* if it's going to be resized smaller than it intended, don't
87        move the window over */
88     if (x != data->client->area.x) x += w - realw;
89     if (y != data->client->area.y) y += h - realh;
90
91     if (x != data->client->area.x || y != data->client->area.y ||
92         realw != data->client->area.width ||
93         realh != data->client->area.height)
94     {
95         actions_client_move(data, TRUE);
96         client_move_resize(data->client, x, y, realw, realh);
97         actions_client_move(data, FALSE);
98         return TRUE;
99     }
100     return FALSE;
101 }
102
103 /* Always return FALSE because its not interactive */
104 static gboolean run_func(ObActionsData *data, gpointer options)
105 {
106     Options *o = options;
107     gint x, y, w, h;
108     ObDirection opp;
109     gint half;
110
111     if (!data->client ||
112         /* don't allow vertical resize if shaded */
113         ((o->dir == OB_DIRECTION_NORTH || o->dir == OB_DIRECTION_SOUTH) &&
114          data->client->shaded))
115     {
116         return FALSE;
117     }
118
119     if (!o->shrink) {
120         /* try grow */
121         client_find_resize_directional(data->client, o->dir, TRUE,
122                                        &x, &y, &w, &h);
123         if (do_grow(data, x, y, w, h))
124             return FALSE;
125     }
126
127     /* we couldn't grow, so try shrink! */
128     opp = (o->dir == OB_DIRECTION_NORTH ? OB_DIRECTION_SOUTH :
129            (o->dir == OB_DIRECTION_SOUTH ? OB_DIRECTION_NORTH :
130             (o->dir == OB_DIRECTION_EAST ? OB_DIRECTION_WEST :
131              OB_DIRECTION_EAST)));
132     client_find_resize_directional(data->client, opp, FALSE,
133                                    &x, &y, &w, &h);
134     switch (opp) {
135     case OB_DIRECTION_NORTH:
136         half = data->client->area.y + data->client->area.height / 2;
137         if (y > half) {
138             h += y - half;
139             y = half;
140         }
141         break;
142     case OB_DIRECTION_SOUTH:
143         half = data->client->area.height / 2;
144         if (h < half)
145             h = half;
146         break;
147     case OB_DIRECTION_WEST:
148         half = data->client->area.x + data->client->area.width / 2;
149         if (x > half) {
150             w += x - half;
151             x = half;
152         }
153         break;
154     case OB_DIRECTION_EAST:
155         half = data->client->area.width / 2;
156         if (w < half)
157             w = half;
158         break;
159     default: g_assert_not_reached();
160     }
161     if (do_grow(data, x, y, w, h))
162         return FALSE;
163
164     return FALSE;
165 }
166
167 /* 3.4-compatibility */
168 static gpointer setup_north_func(xmlNodePtr node)
169 {
170     Options *o = g_new0(Options, 1);
171     o->shrink = FALSE;
172     o->dir = OB_DIRECTION_NORTH;
173     return o;
174 }
175
176 static gpointer setup_south_func(xmlNodePtr node)
177 {
178     Options *o = g_new0(Options, 1);
179     o->shrink = FALSE;
180     o->dir = OB_DIRECTION_SOUTH;
181     return o;
182 }
183
184 static gpointer setup_east_func(xmlNodePtr node)
185 {
186     Options *o = g_new0(Options, 1);
187     o->shrink = FALSE;
188     o->dir = OB_DIRECTION_EAST;
189     return o;
190 }
191
192 static gpointer setup_west_func(xmlNodePtr node)
193 {
194     Options *o = g_new0(Options, 1);
195     o->shrink = FALSE;
196     o->dir = OB_DIRECTION_WEST;
197     return o;
198 }