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