4d23be45a8fd8f16b85b7a2094fdaa6bd32b6f3b
[dana/openbox-history.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_moveto_startup()
24 {
25     actions_register("MoveTo",
26                      setup_func,
27                      free_func,
28                      run_func,
29                      NULL, NULL);
30 }
31
32 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
33 {
34     xmlNodePtr n;
35     Options *o;
36
37     o = g_new0(Options, 1);
38     o->x = G_MININT;
39     o->y = G_MININT;
40     o->w = G_MININT;
41     o->h = G_MININT;
42     o->monitor = -1;
43
44     if ((n = parse_find_node("x", node))) {
45         gchar *s = parse_string(doc, n);
46         if (!g_ascii_strcasecmp(s, "center"))
47             o->xcenter = TRUE;
48         else {
49             if (s[0] == '-')
50                 o->xopposite = TRUE;
51             if (s[0] == '-' || s[0] == '+')
52                 o->x = atoi(s+1);
53             else
54                 o->x = atoi(s);
55         }
56         g_free(s);
57     }
58
59     if ((n = parse_find_node("y", node))) {
60         gchar *s = parse_string(doc, n);
61         if (!g_ascii_strcasecmp(s, "center"))
62             o->ycenter = TRUE;
63         else {
64             if (s[0] == '-')
65                 o->yopposite = TRUE;
66             if (s[0] == '-' || s[0] == '+')
67                 o->y = atoi(s+1);
68             else
69                 o->y = atoi(s);
70         }
71         g_free(s);
72     }
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 void free_func(gpointer options)
86 {
87     Options *o = options;
88
89     g_free(o);
90 }
91
92 /* Always return FALSE because its not interactive */
93 static gboolean run_func(ObActionsData *data, gpointer options)
94 {
95     Options *o = options;
96
97     if (data->client) {
98         Rect *area, *carea;
99         ObClient *c;
100         gint mon, cmon;
101         gint x, y, lw, lh, w, h;
102
103         c = data->client;
104         mon = o->monitor;
105         cmon = client_monitor(c);
106         if (mon < 0) mon = cmon;
107         area = screen_area(c->desktop, mon, NULL);
108         carea = screen_area(c->desktop, cmon, NULL);
109
110         w = o->w;
111         if (w == G_MININT) w = c->area.width;
112
113         h = o->h;
114         if (h == G_MININT) h = c->area.height;
115
116         /* it might not be able to resize how they requested, so find out what
117            it will actually be resized to */
118         x = c->area.x;
119         y = c->area.y;
120         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
121
122         /* get the frame's size */
123         w += c->frame->size.left + c->frame->size.right;
124         h += c->frame->size.top + c->frame->size.bottom;
125
126         x = o->x;
127         if (o->xcenter) x = (area->width - w) / 2;
128         else if (x == G_MININT) x = c->frame->area.x - carea->x;
129         else if (o->xopposite) x = area->width - w;
130         x += area->x;
131
132         y = o->y;
133         if (o->ycenter) y = (area->height - h) / 2;
134         else if (y == G_MININT) y = c->frame->area.y - carea->y;
135         else if (o->yopposite) y = area->height - h;
136         y += area->y;
137
138         /* get the client's size back */
139         w -= c->frame->size.left + c->frame->size.right;
140         h -= c->frame->size.top + c->frame->size.bottom;        
141
142         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
143         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
144         /* force it on screen if its moving to another monitor */
145         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
146
147         actions_client_move(data, TRUE);
148         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
149         actions_client_move(data, FALSE);
150
151         g_free(area);
152         g_free(carea);
153     }
154
155     return FALSE;
156 }