]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/moveresizeto.c
Implement plugin engine to draw frame (Follow Master patch)
[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/engine_interface.h"
5 #include "openbox/openbox.h"
6
7 #include <stdlib.h> /* for atoi */
8
9 enum {
10     CURRENT_MONITOR = -1,
11     ALL_MONITORS = -2
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 gboolean run_func(ObActionsData *data, gpointer options);
28
29 void action_moveresizeto_startup(void)
30 {
31     actions_register("MoveResizeTo", setup_func, g_free, run_func, NULL, NULL);
32 }
33
34 static void parse_coord(xmlNodePtr n, gint *pos,
35                         gboolean *opposite, gboolean *center)
36 {
37     gchar *s = obt_parse_node_string(n);
38     if (g_ascii_strcasecmp(s, "current") != 0) {
39         if (!g_ascii_strcasecmp(s, "center"))
40             *center = TRUE;
41         else {
42             if (s[0] == '-')
43                 *opposite = TRUE;
44             if (s[0] == '-' || s[0] == '+')
45                 *pos = atoi(s+1);
46             else
47                 *pos = atoi(s);
48         }
49     }
50     g_free(s);
51 }
52
53 static gpointer setup_func(xmlNodePtr node)
54 {
55     xmlNodePtr n;
56     Options *o;
57
58     o = g_new0(Options, 1);
59     o->x = G_MININT;
60     o->y = G_MININT;
61     o->w = G_MININT;
62     o->h = G_MININT;
63     o->monitor = CURRENT_MONITOR;
64
65     if ((n = obt_parse_find_node(node, "x")))
66         parse_coord(n, &o->x, &o->xopposite, &o->xcenter);
67
68     if ((n = obt_parse_find_node(node, "y")))
69         parse_coord(n, &o->y, &o->yopposite, &o->ycenter);
70
71     if ((n = obt_parse_find_node(node, "width"))) {
72         gchar *s = obt_parse_node_string(n);
73         if (g_ascii_strcasecmp(s, "current") != 0)
74             o->w = obt_parse_node_int(n);
75         g_free(s);
76     }
77     if ((n = obt_parse_find_node(node, "height"))) {
78         gchar *s = obt_parse_node_string(n);
79         if (g_ascii_strcasecmp(s, "current") != 0)
80             o->h = obt_parse_node_int(n);
81         g_free(s);
82     }
83
84     if ((n = obt_parse_find_node(node, "monitor"))) {
85         gchar *s = obt_parse_node_string(n);
86         if (g_ascii_strcasecmp(s, "current") != 0) {
87             if (!g_ascii_strcasecmp(s, "all"))
88                 o->monitor = ALL_MONITORS;
89             else
90                 o->monitor = obt_parse_node_int(n) - 1;
91         }
92         g_free(s);
93     }
94
95     return o;
96 }
97
98 /* Always return FALSE because its not interactive */
99 static gboolean run_func(ObActionsData *data, gpointer options)
100 {
101     Options *o = options;
102
103     if (data->client) {
104         Rect *area, *carea;
105         ObClient *c;
106         gint mon, cmon;
107         gint x, y, lw, lh, w, h;
108
109         c = data->client;
110         mon = o->monitor;
111         cmon = client_monitor(c);
112         if (mon == CURRENT_MONITOR) mon = cmon;
113         else if (mon == ALL_MONITORS) mon = SCREEN_AREA_ALL_MONITORS;
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         Strut c_size = render_plugin->frame_get_size(c->frame);
130         Rect c_area = render_plugin->frame_get_window_area(c->frame);
131         /* get the frame's size */
132         w += c_size.left + c_size.right;
133         h += c_size.top + c_size.bottom;
134
135         x = o->x;
136         if (o->xcenter) x = (area->width - w) / 2;
137         else if (x == G_MININT) x = c_area.x - carea->x;
138         else if (o->xopposite) x = area->width - w;
139         x += area->x;
140
141         y = o->y;
142         if (o->ycenter) y = (area->height - h) / 2;
143         else if (y == G_MININT) y = c_area.y - carea->y;
144         else if (o->yopposite) y = area->height - h;
145         y += area->y;
146
147         /* get the client's size back */
148         w -= c_size.left + c_size.right;
149         h -= c_size.top + c_size.bottom;
150
151         frame_frame_gravity(c, &x, &y); /* get the client coords */
152         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
153         /* force it on screen if its moving to another monitor */
154         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
155
156         actions_client_move(data, TRUE);
157         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
158         actions_client_move(data, FALSE);
159
160         g_free(area);
161         g_free(carea);
162     }
163
164     return FALSE;
165 }