Merge branch 'backport' into 3.4-working
[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_north_func(ObParseInst *i,
13                                  xmlDocPtr doc, xmlNodePtr node);
14 static gpointer setup_south_func(ObParseInst *i,
15                                  xmlDocPtr doc, xmlNodePtr node);
16 static gpointer setup_east_func(ObParseInst *i,
17                                 xmlDocPtr doc, xmlNodePtr node);
18 static gpointer setup_west_func(ObParseInst *i,
19                                 xmlDocPtr doc, xmlNodePtr node);
20 static gboolean run_func(ObActionsData *data, gpointer options);
21
22 void action_growtoedge_startup(void)
23 {
24     actions_register("GrowToEdgeNorth", setup_north_func, g_free, run_func,
25                      NULL, NULL);
26     actions_register("GrowToEdgeSouth", setup_south_func, g_free, run_func,
27                      NULL, NULL);
28     actions_register("GrowToEdgeEast", setup_east_func, g_free, run_func,
29                      NULL, NULL);
30     actions_register("GrowToEdgeWest", setup_west_func, g_free, run_func,
31                      NULL, NULL);
32 }
33
34 static gpointer setup_north_func(ObParseInst *i,
35                                  xmlDocPtr doc, xmlNodePtr node)
36 {
37     Options *o = g_new0(Options, 1);
38     o->dir = OB_DIRECTION_NORTH;
39     return o;
40 }
41
42 static gpointer setup_south_func(ObParseInst *i,
43                                  xmlDocPtr doc, xmlNodePtr node)
44 {
45     Options *o = g_new0(Options, 1);
46     o->dir = OB_DIRECTION_SOUTH;
47     return o;
48 }
49
50 static gpointer setup_east_func(ObParseInst *i,
51                                 xmlDocPtr doc, xmlNodePtr node)
52 {
53     Options *o = g_new0(Options, 1);
54     o->dir = OB_DIRECTION_EAST;
55     return o;
56 }
57
58 static gpointer setup_west_func(ObParseInst *i,
59                                 xmlDocPtr doc, xmlNodePtr node)
60 {
61     Options *o = g_new0(Options, 1);
62     o->dir = OB_DIRECTION_WEST;
63     return o;
64 }
65
66 static gboolean do_grow(ObActionsData *data, gint x, gint y, gint w, gint h)
67 {
68     gint realw, realh, lw, lh;
69
70     realw = w;
71     realh = h;
72     client_try_configure(data->client, &x, &y, &realw, &realh,
73                          &lw, &lh, TRUE);
74     /* if it's going to be resized smaller than it intended, don't
75        move the window over */
76     if (x != data->client->area.x) x += w - realw;
77     if (y != data->client->area.y) y += h - realh;
78
79     if (x != data->client->area.x || y != data->client->area.y ||
80         realw != data->client->area.width ||
81         realh != data->client->area.height)
82     {
83         actions_client_move(data, TRUE);
84         client_move_resize(data->client, x, y, realw, realh);
85         actions_client_move(data, FALSE);
86         return TRUE;
87     }
88     return FALSE;
89 }
90
91 /* Always return FALSE because its not interactive */
92 static gboolean run_func(ObActionsData *data, gpointer options)
93 {
94     Options *o = options;
95     gint x, y, w, h;
96     ObDirection opp;
97     gint half;
98
99     if (!data->client ||
100         /* don't allow vertical resize if shaded */
101         ((o->dir == OB_DIRECTION_NORTH || o->dir == OB_DIRECTION_SOUTH) &&
102          data->client->shaded))
103     {
104         return FALSE;
105     }
106
107     /* try grow */
108     client_find_resize_directional(data->client, o->dir, TRUE,
109                                    &x, &y, &w, &h);
110     if (do_grow(data, x, y, w, h))
111         return FALSE;
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 }