]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/resizerelative.c
Remove some unneeded stdlib.h includes
[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
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 / c->size_inc.width) / o->left_denom;
74         if (o->right_denom)
75             right = (right * c->area.width / c->size_inc.width) / o->right_denom;
76         if (o->top_denom)
77             top = (top * c->area.height / c->size_inc.height) / o->top_denom;
78         if (o->bottom_denom)
79             bottom = (bottom * c->area.height / c->size_inc.height) / o->bottom_denom;
80
81         x = c->area.x;
82         y = c->area.y;
83         ow = c->area.width;
84         xoff = -left * c->size_inc.width;
85         nw = ow + right * c->size_inc.width
86             + left * c->size_inc.width;
87         oh = c->area.height;
88         yoff = -top * c->size_inc.height;
89         nh = oh + bottom * c->size_inc.height
90             + top * c->size_inc.height;
91
92         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
93         xoff = xoff == 0 ? 0 :
94             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
95         yoff = yoff == 0 ? 0 :
96             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
97
98         actions_client_move(data, TRUE);
99         client_move_resize(c, x + xoff, y + yoff, nw, nh);
100         actions_client_move(data, FALSE);
101     }
102
103     return FALSE;
104 }