]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/moveresizeto.c
Fix a typo that prevented setting client size for the height only (???)
[dana/openbox.git] / openbox / actions / moveresizeto.c
1 #include "openbox/actions.h"
2 #include "openbox/client.h"
3 #include "openbox/screen.h"
4 #include "openbox/frame.h"
5 #include "openbox/config.h"
6
7 enum {
8     CURRENT_MONITOR = -1,
9     ALL_MONITORS = -2,
10     NEXT_MONITOR = -3,
11     PREV_MONITOR = -4
12 };
13
14 typedef struct {
15     GravityCoord x;
16     GravityCoord y;
17     gint w;
18     gint w_denom;
19     gint h;
20     gint h_denom;
21     gint monitor;
22     gboolean w_sets_client_size;
23     gboolean h_sets_client_size;
24 } Options;
25
26 static gpointer setup_func(xmlNodePtr node);
27 static void free_func(gpointer o);
28 static gboolean run_func(ObActionsData *data, gpointer options);
29 /* 3.4-compatibility */
30 static gpointer setup_center_func(xmlNodePtr node);
31
32 void action_moveresizeto_startup(void)
33 {
34     actions_register("MoveResizeTo", setup_func, free_func, run_func);
35     /* 3.4-compatibility */
36     actions_register("MoveToCenter", setup_center_func, free_func, run_func);
37 }
38
39 static gpointer setup_func(xmlNodePtr node)
40 {
41     xmlNodePtr n;
42     Options *o;
43
44     o = g_slice_new0(Options);
45     o->x.pos = G_MININT;
46     o->y.pos = G_MININT;
47     o->w = G_MININT;
48     o->h = G_MININT;
49     o->monitor = CURRENT_MONITOR;
50
51     if ((n = obt_xml_find_node(node, "x")))
52         config_parse_gravity_coord(n, &o->x);
53
54     if ((n = obt_xml_find_node(node, "y")))
55         config_parse_gravity_coord(n, &o->y);
56
57     if ((n = obt_xml_find_node(node, "width"))) {
58         gchar *s = obt_xml_node_string(n);
59         if (g_ascii_strcasecmp(s, "current") != 0)
60             config_parse_relative_number(s, &o->w, &o->w_denom);
61         g_free(s);
62
63         obt_xml_attr_bool(n, "client", &o->w_sets_client_size);
64     }
65     if ((n = obt_xml_find_node(node, "height"))) {
66         gchar *s = obt_xml_node_string(n);
67         if (g_ascii_strcasecmp(s, "current") != 0)
68             config_parse_relative_number(s, &o->h, &o->h_denom);
69         g_free(s);
70
71         obt_xml_attr_bool(n, "client", &o->h_sets_client_size);
72     }
73
74     if ((n = obt_xml_find_node(node, "monitor"))) {
75         gchar *s = obt_xml_node_string(n);
76         if (g_ascii_strcasecmp(s, "current") != 0) {
77             if (!g_ascii_strcasecmp(s, "all"))
78                 o->monitor = ALL_MONITORS;
79             else if(!g_ascii_strcasecmp(s, "next"))
80                 o->monitor = NEXT_MONITOR;
81             else if(!g_ascii_strcasecmp(s, "prev"))
82                 o->monitor = PREV_MONITOR;
83             else
84                 o->monitor = obt_xml_node_int(n) - 1;
85         }
86         g_free(s);
87     }
88
89     return o;
90 }
91
92 static void free_func(gpointer o)
93 {
94     g_slice_free(Options, o);
95 }
96
97 /* Always return FALSE because its not interactive */
98 static gboolean run_func(ObActionsData *data, gpointer options)
99 {
100     Options *o = options;
101
102     if (data->client) {
103         Rect *area, *carea;
104         ObClient *c;
105         guint mon, cmon;
106         gint x, y, lw, lh, w, h;
107
108         c = data->client;
109         mon = o->monitor;
110         cmon = client_monitor(c);
111         switch (mon) {
112         case CURRENT_MONITOR:
113             mon = cmon; break;
114         case ALL_MONITORS:
115             mon = SCREEN_AREA_ALL_MONITORS; break;
116         case NEXT_MONITOR:
117             mon = (cmon + 1 > screen_num_monitors - 1) ? 0 : (cmon + 1); break;
118         case PREV_MONITOR:
119             mon = (cmon == 0) ? (screen_num_monitors - 1) : (cmon - 1); break;
120         default:
121             g_assert_not_reached();
122         }
123
124         area = screen_area(c->desktop, mon, NULL);
125         carea = screen_area(c->desktop, cmon, NULL);
126
127         /* find a target size for the client/frame. */
128         w = o->w;
129         if (w == G_MININT) {
130             if (o->w_sets_client_size)
131                 w = c->area.width;
132             else
133                 w = c->frame->area.width;
134         }
135         else if (o->w_denom) w = (w * area->width) / o->w_denom;
136
137         h = o->h;
138         if (h == G_MININT) {
139             if (o->h_sets_client_size)
140                 h = c->area.height;
141             else
142                 h = c->frame->area.height;
143         }
144         else if (o->h_denom) h = (h * area->height) / o->h_denom;
145
146         /* get back to the client's size. */
147         if (!o->w_sets_client_size)
148             w -= c->frame->size.left + c->frame->size.right;
149         if (!o->h_sets_client_size)
150             h -= c->frame->size.top + c->frame->size.bottom;
151
152         /* it might not be able to resize how they requested, so find out what
153            it will actually be resized to */
154         x = c->area.x;
155         y = c->area.y;
156         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
157
158         /* get the frame's size */
159         w += c->frame->size.left + c->frame->size.right;
160         h += c->frame->size.top + c->frame->size.bottom;
161
162         x = o->x.pos;
163         if (o->x.denom)
164             x = (x * area->width) / o->x.denom;
165         if (o->x.center) x = (area->width - w) / 2;
166         else if (x == G_MININT) x = c->frame->area.x - carea->x;
167         else if (o->x.opposite) x = area->width - w - x;
168         x += area->x;
169
170         y = o->y.pos;
171         if (o->y.denom)
172             y = (y * area->height) / o->y.denom;
173         if (o->y.center) y = (area->height - h) / 2;
174         else if (y == G_MININT) y = c->frame->area.y - carea->y;
175         else if (o->y.opposite) y = area->height - h - y;
176         y += area->y;
177
178         /* get the client's size back */
179         w -= c->frame->size.left + c->frame->size.right;
180         h -= c->frame->size.top + c->frame->size.bottom;
181
182         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
183         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
184         /* force it on screen if its moving to another monitor */
185         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
186
187         actions_client_move(data, TRUE);
188         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
189         actions_client_move(data, FALSE);
190
191         g_slice_free(Rect, area);
192         g_slice_free(Rect, carea);
193     }
194
195     return FALSE;
196 }
197
198 /* 3.4-compatibility */
199 static gpointer setup_center_func(xmlNodePtr node)
200 {
201     Options *o;
202
203     o = g_slice_new0(Options);
204     o->x.pos = G_MININT;
205     o->y.pos = G_MININT;
206     o->w = G_MININT;
207     o->h = G_MININT;
208     o->monitor = -1;
209     o->x.center = TRUE;
210     o->y.center = TRUE;
211     return o;
212 }