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