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