Add support for using relative expressions in move and resize actions
[dana/openbox.git] / openbox / actions / resizerelative.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 left;
10     gint left_denom;
11     gint right;
12     gint right_denom;
13     gint top;
14     gint top_denom;
15     gint bottom;
16     gint bottom_denom;
17 } Options;
18
19 static gpointer setup_func(xmlNodePtr node);
20 static void     free_func(gpointer options);
21 static gboolean run_func(ObActionsData *data, gpointer options);
22
23 void action_resizerelative_startup(void)
24 {
25     actions_register("ResizeRelative", setup_func, free_func, run_func);
26 }
27
28 static void xml_node_relative(xmlNodePtr n, gint *num, gint *denom)
29 {
30     gchar *s;
31
32     s = obt_xml_node_string(n);
33     config_parse_relative_number(s, num, denom);
34     g_free(s);
35 }
36
37 static gpointer setup_func(xmlNodePtr node)
38 {
39     xmlNodePtr n;
40     Options *o;
41
42     o = g_slice_new0(Options);
43
44     if ((n = obt_xml_find_node(node, "left")))
45         xml_node_relative(n, &o->left, &o->left_denom);
46     if ((n = obt_xml_find_node(node, "right")))
47         xml_node_relative(n, &o->right, &o->right_denom);
48     if ((n = obt_xml_find_node(node, "top")) ||
49         (n = obt_xml_find_node(node, "up")))
50         xml_node_relative(n, &o->top, &o->top_denom);
51     if ((n = obt_xml_find_node(node, "bottom")) ||
52         (n = obt_xml_find_node(node, "down")))
53         xml_node_relative(n, &o->bottom, &o->bottom_denom);
54
55     return o;
56 }
57
58 static void free_func(gpointer o)
59 {
60     g_slice_free(Options, o);
61 }
62
63 /* Always return FALSE because its not interactive */
64 static gboolean run_func(ObActionsData *data, gpointer options)
65 {
66     Options *o = options;
67
68     if (data->client) {
69         ObClient *c = data->client;
70         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
71         gint left = o->left, right = o->right, top = o->top, bottom = o->bottom;
72
73         if (o->left_denom)
74             left = (left * c->area.width / c->size_inc.width) / o->left_denom;
75         if (o->right_denom)
76             right = (right * c->area.width / c->size_inc.width) / o->right_denom;
77         if (o->top_denom)
78             top = (top * c->area.height / c->size_inc.height) / o->top_denom;
79         if (o->bottom_denom)
80             bottom = (bottom * c->area.height / c->size_inc.height) / o->bottom_denom;
81
82         x = c->area.x;
83         y = c->area.y;
84         ow = c->area.width;
85         xoff = -left * c->size_inc.width;
86         nw = ow + right * c->size_inc.width
87             + left * c->size_inc.width;
88         oh = c->area.height;
89         yoff = -top * c->size_inc.height;
90         nh = oh + bottom * c->size_inc.height
91             + top * c->size_inc.height;
92
93         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
94         xoff = xoff == 0 ? 0 :
95             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
96         yoff = yoff == 0 ? 0 :
97             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
98
99         actions_client_move(data, TRUE);
100         client_move_resize(c, x + xoff, y + yoff, nw, nh);
101         actions_client_move(data, FALSE);
102     }
103
104     return FALSE;
105 }