]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions/resizerelative.c
Make ResizeRelative resize in terms of pixels for windows with resize increments...
[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         // When resizing, if the resize has a non-zero value then make sure it
82         // is at least as big as the size increment so the window does actually
83         // resize.
84         x = c->area.x;
85         y = c->area.y;
86         ow = c->area.width;
87         xoff = -MAX(left, (left ? c->size_inc.width : 0));
88         nw = ow + MAX(right + left, (right + left ? c->size_inc.width : 0));
89         oh = c->area.height;
90         yoff = -MAX(top, (top ? c->size_inc.height : 0));
91         nh = oh + MAX(bottom + top, (bottom + top ? c->size_inc.height : 0));
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 }