]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions/moveresizeto.c
for MoveResizeTo make <width>current</width> and <height>current</height> and <monito...
[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 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         gchar *s = parse_string(doc, n);
71         if (g_ascii_strcasecmp(s, "current") != 0)
72             o->w = parse_int(doc, n);
73         g_free(s);
74     }
75     if ((n = parse_find_node("height", node))) {
76         gchar *s = parse_string(doc, n);
77         if (g_ascii_strcasecmp(s, "current") != 0)
78             o->h = parse_int(doc, n);
79         g_free(s);
80     }
81
82     if ((n = parse_find_node("monitor", node))) {
83         gchar *s = parse_string(doc, n);
84         if (g_ascii_strcasecmp(s, "current") != 0)
85             o->monitor = parse_int(doc, n) - 1;
86         g_free(s);
87     }
88
89     return o;
90 }
91
92 static void free_func(gpointer options)
93 {
94     Options *o = options;
95
96     g_free(o);
97 }
98
99 /* Always return FALSE because its not interactive */
100 static gboolean run_func(ObActionsData *data, gpointer options)
101 {
102     Options *o = options;
103
104     if (data->client) {
105         Rect *area, *carea;
106         ObClient *c;
107         gint mon, cmon;
108         gint x, y, lw, lh, w, h;
109
110         c = data->client;
111         mon = o->monitor;
112         cmon = client_monitor(c);
113         if (mon < 0) mon = cmon;
114         area = screen_area(c->desktop, mon, NULL);
115         carea = screen_area(c->desktop, cmon, NULL);
116
117         w = o->w;
118         if (w == G_MININT) w = c->area.width;
119
120         h = o->h;
121         if (h == G_MININT) h = c->area.height;
122
123         /* it might not be able to resize how they requested, so find out what
124            it will actually be resized to */
125         x = c->area.x;
126         y = c->area.y;
127         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
128
129         /* get the frame's size */
130         w += c->frame->size.left + c->frame->size.right;
131         h += c->frame->size.top + c->frame->size.bottom;
132
133         x = o->x;
134         if (o->xcenter) x = (area->width - w) / 2;
135         else if (x == G_MININT) x = c->frame->area.x - carea->x;
136         else if (o->xopposite) x = area->width - w;
137         x += area->x;
138
139         y = o->y;
140         if (o->ycenter) y = (area->height - h) / 2;
141         else if (y == G_MININT) y = c->frame->area.y - carea->y;
142         else if (o->yopposite) y = area->height - h;
143         y += area->y;
144
145         /* get the client's size back */
146         w -= c->frame->size.left + c->frame->size.right;
147         h -= c->frame->size.top + c->frame->size.bottom;
148
149         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
150         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
151         /* force it on screen if its moving to another monitor */
152         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
153
154         actions_client_move(data, TRUE);
155         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
156         actions_client_move(data, FALSE);
157
158         g_free(area);
159         g_free(carea);
160     }
161
162     return FALSE;
163 }