fc4a615d89de90a28edbc37084270ec343093ac7
[dana/openbox.git] / openbox / actions / moverelative.c
1 #include "openbox/actions.h"
2 #include "openbox/client.h"
3 #include "openbox/screen.h"
4 #include "openbox/frame.h"
5 #include "openbox/config.h"
6 #include <stdlib.h> /* for atoi */
7
8 typedef struct {
9     gint x;
10     gint x_denom;
11     gint y;
12     gint y_denom;
13 } Options;
14
15 static gpointer setup_func(xmlNodePtr node);
16 static void free_func(gpointer o);
17 static gboolean run_func(ObActionsData *data, gpointer options);
18
19 void action_moverelative_startup(void)
20 {
21     actions_register("MoveRelative", setup_func, free_func, run_func);
22 }
23
24 static gpointer setup_func(xmlNodePtr node)
25 {
26     xmlNodePtr n;
27     Options *o;
28     gchar *s;
29
30     o = g_slice_new0(Options);
31
32     if ((n = obt_xml_find_node(node, "x"))) {
33         s = obt_xml_node_string(n);
34         config_parse_relative_number(s, &o->x, &o->x_denom);
35         g_free(s);
36     }
37     if ((n = obt_xml_find_node(node, "y"))) {
38         s = obt_xml_node_string(n);
39         config_parse_relative_number(s, &o->y, &o->y_denom);
40         g_free(s);
41     }
42
43     return o;
44 }
45
46 static void free_func(gpointer o)
47 {
48     g_slice_free(Options, o);
49 }
50
51 /* Always return FALSE because its not interactive */
52 static gboolean run_func(ObActionsData *data, gpointer options)
53 {
54     Options *o = options;
55
56     if (data->client) {
57         ObClient *c;
58         gint x, y, lw, lh, w, h;
59
60         c = data->client;
61         x = o->x;
62         y = o->y;
63         if (o->x_denom || o->y_denom) {
64             const Rect *carea;
65
66             carea = screen_area(c->desktop, client_monitor(c), NULL);
67             if (o->x_denom)
68                 x = (x * carea->width) / o->x_denom;
69             if (o->y_denom)
70                 y = (y * carea->height) / o->y_denom;
71         }
72         x = c->area.x + x;
73         y = c->area.y + y;
74         w = c->area.width;
75         h = c->area.height;
76         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
77         client_find_onscreen(c, &x, &y, w, h, FALSE);
78
79         actions_client_move(data, TRUE);
80         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
81         actions_client_move(data, FALSE);
82     }
83
84     return FALSE;
85 }