]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/resizerelative.c
Merge branch 'backport' into work
[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 <stdlib.h> /* for atoi */
6
7 typedef struct {
8     gint left;
9     gint right;
10     gint top;
11     gint bottom;
12 } Options;
13
14 static gpointer setup_func(xmlNodePtr node);
15 static gboolean run_func(ObActionsData *data, gpointer options);
16
17 void action_resizerelative_startup(void)
18 {
19     actions_register("ResizeRelative", setup_func, g_free, run_func,
20                      NULL, NULL);
21 }
22
23 static gpointer setup_func(xmlNodePtr node)
24 {
25     xmlNodePtr n;
26     Options *o;
27
28     o = g_new0(Options, 1);
29
30     if ((n = obt_parse_find_node(node, "left")))
31         o->left = obt_parse_node_int(n);
32     if ((n = obt_parse_find_node(node, "right")))
33         o->right = obt_parse_node_int(n);
34     if ((n = obt_parse_find_node(node, "top")) ||
35         (n = obt_parse_find_node(node, "up")))
36         o->top = obt_parse_node_int(n);
37     if ((n = obt_parse_find_node(node, "bottom")) ||
38         (n = obt_parse_find_node(node, "down")))
39         o->bottom = obt_parse_node_int(n);
40
41     return o;
42 }
43
44 /* Always return FALSE because its not interactive */
45 static gboolean run_func(ObActionsData *data, gpointer options)
46 {
47     Options *o = options;
48
49     if (data->client) {
50         ObClient *c = data->client;
51         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
52
53         x = c->area.x;
54         y = c->area.y;
55         ow = c->area.width;
56         xoff = -o->left * c->size_inc.width;
57         nw = ow + o->right * c->size_inc.width
58             + o->left * c->size_inc.width;
59         oh = c->area.height;
60         yoff = -o->top * c->size_inc.height;
61         nh = oh + o->bottom * c->size_inc.height
62             + o->top * c->size_inc.height;
63
64         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
65         xoff = xoff == 0 ? 0 :
66             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
67         yoff = yoff == 0 ? 0 :
68             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
69
70         actions_client_move(data, TRUE);
71         client_move_resize(c, x + xoff, y + yoff, nw, nh);
72         actions_client_move(data, FALSE);
73     }
74
75     return FALSE;
76 }