Merge branch 'backport' into work
[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 } 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
17 void action_growtoedge_startup(void)
18 {
19     actions_register("GrowToEdge", setup_func,
20                      g_free, run_func, NULL, NULL);
21     actions_register("ShrinkToEdge", setup_shrink_func,
22                      g_free, run_func, NULL, NULL);
23 }
24
25 static gpointer setup_func(xmlNodePtr node)
26 {
27     xmlNodePtr n;
28     Options *o;
29
30     o = g_new0(Options, 1);
31     o->dir = OB_DIRECTION_NORTH;
32     o->shrink = FALSE;
33
34     if ((n = obt_parse_find_node(node, "direction"))) {
35         gchar *s = obt_parse_node_string(n);
36         if (!g_ascii_strcasecmp(s, "north") ||
37             !g_ascii_strcasecmp(s, "up"))
38             o->dir = OB_DIRECTION_NORTH;
39         else if (!g_ascii_strcasecmp(s, "south") ||
40                  !g_ascii_strcasecmp(s, "down"))
41             o->dir = OB_DIRECTION_SOUTH;
42         else if (!g_ascii_strcasecmp(s, "west") ||
43                  !g_ascii_strcasecmp(s, "left"))
44             o->dir = OB_DIRECTION_WEST;
45         else if (!g_ascii_strcasecmp(s, "east") ||
46                  !g_ascii_strcasecmp(s, "right"))
47             o->dir = OB_DIRECTION_EAST;
48         g_free(s);
49     }
50
51     return o;
52 }
53
54 static gpointer setup_shrink_func(xmlNodePtr node)
55 {
56     Options *o;
57
58     o = setup_func(node);
59     o->shrink = TRUE;
60
61     return o;
62 }
63
64 static gboolean do_grow(ObActionsData *data, gint x, gint y, gint w, gint h)
65 {
66     gint realw, realh, lw, lh;
67
68     realw = w;
69     realh = h;
70     client_try_configure(data->client, &x, &y, &realw, &realh,
71                          &lw, &lh, TRUE);
72     /* if it's going to be resized smaller than it intended, don't
73        move the window over */
74     if (x != data->client->area.x) x += w - realw;
75     if (y != data->client->area.y) y += h - realh;
76
77     if (x != data->client->area.x || y != data->client->area.y ||
78         realw != data->client->area.width ||
79         realh != data->client->area.height)
80     {
81         actions_client_move(data, TRUE);
82         client_move_resize(data->client, x, y, realw, realh);
83         actions_client_move(data, FALSE);
84         return TRUE;
85     }
86     return FALSE;
87 }
88
89 /* Always return FALSE because its not interactive */
90 static gboolean run_func(ObActionsData *data, gpointer options)
91 {
92     Options *o = options;
93     gint x, y, w, h;
94     ObDirection opp;
95     gint half;
96
97     if (!data->client ||
98         /* don't allow vertical resize if shaded */
99         ((o->dir == OB_DIRECTION_NORTH || o->dir == OB_DIRECTION_SOUTH) &&
100          data->client->shaded))
101     {
102         return FALSE;
103     }
104
105     if (!o->shrink) {
106         /* try grow */
107         client_find_resize_directional(data->client, o->dir, TRUE,
108                                        &x, &y, &w, &h);
109         if (do_grow(data, x, y, w, h))
110             return FALSE;
111     }
112
113     /* we couldn't grow, so try shrink! */
114     opp = (o->dir == OB_DIRECTION_NORTH ? OB_DIRECTION_SOUTH :
115            (o->dir == OB_DIRECTION_SOUTH ? OB_DIRECTION_NORTH :
116             (o->dir == OB_DIRECTION_EAST ? OB_DIRECTION_WEST :
117              OB_DIRECTION_EAST)));
118     client_find_resize_directional(data->client, opp, FALSE,
119                                    &x, &y, &w, &h);
120     switch (opp) {
121     case OB_DIRECTION_NORTH:
122         half = data->client->area.y + data->client->area.height / 2;
123         if (y > half) {
124             h += y - half;
125             y = half;
126         }
127         break;
128     case OB_DIRECTION_SOUTH:
129         half = data->client->area.height / 2;
130         if (h < half)
131             h = half;
132         break;
133     case OB_DIRECTION_WEST:
134         half = data->client->area.x + data->client->area.width / 2;
135         if (x > half) {
136             w += x - half;
137             x = half;
138         }
139         break;
140     case OB_DIRECTION_EAST:
141         half = data->client->area.width / 2;
142         if (w < half)
143             w = half;
144         break;
145     default: g_assert_not_reached();
146     }
147     if (do_grow(data, x, y, w, h))
148         return FALSE;
149
150     return FALSE;
151 }