74a3bd745a08e1301da9a201307d395a60160d2e
[mikachu/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
7 typedef struct {
8     gint left;
9     gint left_denom;
10     gint right;
11     gint right_denom;
12     gint top;
13     gint top_denom;
14     gint bottom;
15     gint bottom_denom;
16 } Options;
17
18 static gpointer setup_func(xmlNodePtr node);
19 static void     free_func(gpointer options);
20 static gboolean run_func(ObActionsData *data, gpointer options);
21
22 void action_resizerelative_startup(void)
23 {
24     actions_register("ResizeRelative", setup_func, free_func, run_func);
25 }
26
27 static void xml_node_relative(xmlNodePtr n, gint *num, gint *denom)
28 {
29     gchar *s;
30
31     s = obt_xml_node_string(n);
32     config_parse_relative_number(s, num, denom);
33     g_free(s);
34 }
35
36 static gpointer setup_func(xmlNodePtr node)
37 {
38     xmlNodePtr n;
39     Options *o;
40
41     o = g_slice_new0(Options);
42
43     if ((n = obt_xml_find_node(node, "left")))
44         xml_node_relative(n, &o->left, &o->left_denom);
45     if ((n = obt_xml_find_node(node, "right")))
46         xml_node_relative(n, &o->right, &o->right_denom);
47     if ((n = obt_xml_find_node(node, "top")) ||
48         (n = obt_xml_find_node(node, "up")))
49         xml_node_relative(n, &o->top, &o->top_denom);
50     if ((n = obt_xml_find_node(node, "bottom")) ||
51         (n = obt_xml_find_node(node, "down")))
52         xml_node_relative(n, &o->bottom, &o->bottom_denom);
53
54     return o;
55 }
56
57 static void free_func(gpointer o)
58 {
59     g_slice_free(Options, o);
60 }
61
62 /* Always return FALSE because its not interactive */
63 static gboolean run_func(ObActionsData *data, gpointer options)
64 {
65     Options *o = options;
66
67     if (data->client) {
68         ObClient *c = data->client;
69         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
70         gint left = o->left, right = o->right, top = o->top, bottom = o->bottom;
71
72         if (o->left_denom)
73             left = left * c->area.width / o->left_denom;
74         if (o->right_denom)
75             right = right * c->area.width / o->right_denom;
76         if (o->top_denom)
77             top = top * c->area.height / o->top_denom;
78         if (o->bottom_denom)
79             bottom = bottom * c->area.height / o->bottom_denom;
80
81         if (left && ABS(left) < c->size_inc.width)
82             left = left < 0 ? -c->size_inc.width : c->size_inc.width;
83         if (right && ABS(right) < c->size_inc.width)
84             right = right < 0 ? -c->size_inc.width : c->size_inc.width;
85         if (top && ABS(top) < c->size_inc.height)
86             top = top < 0 ? -c->size_inc.height : c->size_inc.height;
87         if (bottom && ABS(bottom) < c->size_inc.height)
88             bottom = bottom < 0 ? -c->size_inc.height : c->size_inc.height;
89
90         /* When resizing, if the resize has a non-zero value then make sure it
91            is at least as big as the size increment so the window does actually
92            resize. */
93         x = c->area.x;
94         y = c->area.y;
95         ow = c->area.width;
96         xoff = -left;
97         nw = ow + right + left;
98         oh = c->area.height;
99         yoff = -top;
100         nh = oh + bottom + top;
101
102         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
103         xoff = xoff == 0 ? 0 :
104             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
105         yoff = yoff == 0 ? 0 :
106             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
107
108         actions_client_move(data, TRUE);
109         client_move_resize(c, x + xoff, y + yoff, nw, nh);
110         actions_client_move(data, FALSE);
111     }
112
113     return FALSE;
114 }