668a063fec2ab0fd2989c9aad7a60f676f1be94e
[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 <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(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
15 static void     free_func(gpointer options);
16 static gboolean run_func(ObActionsData *data, gpointer options);
17
18 void action_resizerelative_startup()
19 {
20     actions_register("ResizeRelative",
21                      setup_func,
22                      free_func,
23                      run_func,
24                      NULL, NULL);
25 }
26
27 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
28 {
29     xmlNodePtr n;
30     Options *o;
31
32     o = g_new0(Options, 1);
33
34     if ((n = parse_find_node("left", node)))
35         o->left = parse_int(doc, n);
36     if ((n = parse_find_node("right", node)))
37         o->right = parse_int(doc, n);
38     if ((n = parse_find_node("top", node)) ||
39         (n = parse_find_node("up", node)))
40         o->top = parse_int(doc, n);
41     if ((n = parse_find_node("bottom", node)) ||
42         (n = parse_find_node("down", node)))
43         o->bottom = parse_int(doc, n);
44
45     return o;
46 }
47
48 static void free_func(gpointer options)
49 {
50     Options *o = options;
51
52     g_free(o);
53 }
54
55 /* Always return FALSE because its not interactive */
56 static gboolean run_func(ObActionsData *data, gpointer options)
57 {
58     Options *o = options;
59
60     if (data->client) {
61         ObClient *c = data->client;
62         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
63
64         x = c->area.x;
65         y = c->area.y;
66         ow = c->area.width;
67         xoff = -o->left * c->size_inc.width;
68         nw = ow + o->right * c->size_inc.width
69             + o->left * c->size_inc.width;
70         oh = c->area.height;
71         yoff = -o->top * c->size_inc.height;
72         nh = oh + o->bottom * c->size_inc.height
73             + o->top * c->size_inc.height;
74
75         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
76         xoff = xoff == 0 ? 0 :
77             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
78         yoff = yoff == 0 ? 0 :
79             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
80
81         actions_client_move(data, TRUE);
82         client_move_resize(c, x + xoff, y + yoff, nw, nh);
83         actions_client_move(data, FALSE);
84     }
85
86     return FALSE;
87 }