]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/moveresizeto.c
Merge branch 'master' into 3.4-working
[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 gpointer setup_center_func(ObParseInst *i, xmlDocPtr doc,
21                                   xmlNodePtr node);
22 static void     free_func(gpointer options);
23 static gboolean run_func(ObActionsData *data, gpointer options);
24
25 void action_moveresizeto_startup()
26 {
27     actions_register("MoveResizeTo",
28                      setup_func,
29                      free_func,
30                      run_func,
31                      NULL, NULL);
32     actions_register("MoveToCenter",
33                      setup_center_func,
34                      free_func,
35                      run_func,
36                      NULL, NULL);
37 }
38
39 static void parse_coord(xmlDocPtr doc, xmlNodePtr n, gint *pos,
40                         gboolean *opposite, gboolean *center)
41 {
42     gchar *s = parse_string(doc, n);
43     if (!g_ascii_strcasecmp(s, "center"))
44         *center = TRUE;
45     else {
46         if (s[0] == '-')
47             *opposite = TRUE;
48         if (s[0] == '-' || s[0] == '+')
49             *pos = atoi(s+1);
50         else
51             *pos = atoi(s);
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 = -1;
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         o->w = parse_int(doc, n) - 1;
76     if ((n = parse_find_node("height", node)))
77         o->h = parse_int(doc, n) - 1;
78
79     if ((n = parse_find_node("monitor", node)))
80         o->monitor = parse_int(doc, n) - 1;
81
82     return o;
83 }
84
85 static gpointer setup_center_func(ObParseInst *i, xmlDocPtr doc,
86                                   xmlNodePtr node)
87 {
88     xmlNodePtr n;
89     Options *o;
90
91     o = g_new0(Options, 1);
92     o->x = G_MININT;
93     o->y = G_MININT;
94     o->w = G_MININT;
95     o->h = G_MININT;
96     o->monitor = -1;
97     o->xcenter = TRUE;
98     o->ycenter = TRUE;
99     return o;
100 }
101
102 static void free_func(gpointer options)
103 {
104     Options *o = options;
105
106     g_free(o);
107 }
108
109 /* Always return FALSE because its not interactive */
110 static gboolean run_func(ObActionsData *data, gpointer options)
111 {
112     Options *o = options;
113
114     if (data->client) {
115         Rect *area, *carea;
116         ObClient *c;
117         gint mon, cmon;
118         gint x, y, lw, lh, w, h;
119
120         c = data->client;
121         mon = o->monitor;
122         cmon = client_monitor(c);
123         if (mon < 0) mon = cmon;
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;
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;
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 }