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