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