]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/moveto.c
you can use -'s for opposite sides of the screen in moveto
[dana/openbox.git] / openbox / actions / moveto.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 monitor;
15 } Options;
16
17 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
18 static void     free_func(gpointer options);
19 static gboolean run_func(ObActionsData *data, gpointer options);
20
21 void action_moveto_startup()
22 {
23     actions_register("MoveTo",
24                      setup_func,
25                      free_func,
26                      run_func,
27                      NULL, NULL);
28 }
29
30 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
31 {
32     xmlNodePtr n;
33     Options *o;
34
35     o = g_new0(Options, 1);
36     o->x = G_MININT;
37     o->y = G_MININT;
38     o->monitor = -1;
39
40     if ((n = parse_find_node("x", node))) {
41         gchar *s = parse_string(doc, n);
42         if (!g_ascii_strcasecmp(s, "center"))
43             o->xcenter = TRUE;
44         else {
45             if (s[0] == '-')
46                 o->xopposite = TRUE;
47             if (s[0] == '-' || s[0] == '+')
48                 o->x = atoi(s+1);
49             else
50                 o->x = atoi(s);
51         }
52         g_free(s);
53     }
54
55     if ((n = parse_find_node("y", node))) {
56         gchar *s = parse_string(doc, n);
57         if (!g_ascii_strcasecmp(s, "center"))
58             o->ycenter = TRUE;
59         else {
60             if (s[0] == '-')
61                 o->yopposite = TRUE;
62             if (s[0] == '-' || s[0] == '+')
63                 o->y = atoi(s+1);
64             else
65                 o->y = atoi(s);
66         }
67         g_free(s);
68     }
69
70     if ((n = parse_find_node("monitor", node)))
71         o->monitor = parse_int(doc, n) - 1;
72
73     return o;
74 }
75
76 static void free_func(gpointer options)
77 {
78     Options *o = options;
79
80     g_free(o);
81 }
82
83 /* Always return FALSE because its not interactive */
84 static gboolean run_func(ObActionsData *data, gpointer options)
85 {
86     Options *o = options;
87
88     if (data->client) {
89         Rect *area, *carea;
90         ObClient *c;
91         gint mon, cmon;
92         gint x, y, lw, lh, w, h;
93
94         c = data->client;
95         mon = o->monitor;
96         cmon = client_monitor(c);
97         if (mon < 0) mon = cmon;
98         area = screen_area(c->desktop, mon, NULL);
99         carea = screen_area(c->desktop, cmon, NULL);
100
101         x = o->x;
102         if (o->xcenter) x = (area->width - c->frame->area.width) / 2;
103         else if (x == G_MININT) x = c->frame->area.x - carea->x;
104         else if (o->xopposite) x = area->width - c->frame->area.width;
105         x += area->x;
106
107         y = o->y;
108         if (o->ycenter) y = (area->height - c->frame->area.height) / 2;
109         else if (y == G_MININT) y = c->frame->area.y - carea->y;
110         else if (o->yopposite) y = area->height - c->frame->area.height;
111         y += area->y;
112
113         w = c->area.width;
114         h = c->area.height;
115
116         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
117         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
118         /* force it on screen if its moving to another monitor */
119         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
120
121         actions_client_move(data, TRUE);
122         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
123         actions_client_move(data, FALSE);
124
125         g_free(area);
126         g_free(carea);
127     }
128
129     return FALSE;
130 }