rename the obt_parse library to obt_xml (since it is very xml specific)
[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 }
21
22 static gpointer setup_func(xmlNodePtr node)
23 {
24     xmlNodePtr n;
25     Options *o;
26
27     o = g_new0(Options, 1);
28
29     if ((n = obt_xml_find_node(node, "left")))
30         o->left = obt_xml_node_int(n);
31     if ((n = obt_xml_find_node(node, "right")))
32         o->right = obt_xml_node_int(n);
33     if ((n = obt_xml_find_node(node, "top")) ||
34         (n = obt_xml_find_node(node, "up")))
35         o->top = obt_xml_node_int(n);
36     if ((n = obt_xml_find_node(node, "bottom")) ||
37         (n = obt_xml_find_node(node, "down")))
38         o->bottom = obt_xml_node_int(n);
39
40     return o;
41 }
42
43 /* Always return FALSE because its not interactive */
44 static gboolean run_func(ObActionsData *data, gpointer options)
45 {
46     Options *o = options;
47
48     if (data->client) {
49         ObClient *c = data->client;
50         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
51
52         x = c->area.x;
53         y = c->area.y;
54         ow = c->area.width;
55         xoff = -o->left * c->size_inc.width;
56         nw = ow + o->right * c->size_inc.width
57             + o->left * c->size_inc.width;
58         oh = c->area.height;
59         yoff = -o->top * c->size_inc.height;
60         nh = oh + o->bottom * c->size_inc.height
61             + o->top * c->size_inc.height;
62
63         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
64         xoff = xoff == 0 ? 0 :
65             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
66         yoff = yoff == 0 ? 0 :
67             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
68
69         actions_client_move(data, TRUE);
70         client_move_resize(c, x + xoff, y + yoff, nw, nh);
71         actions_client_move(data, FALSE);
72     }
73
74     return FALSE;
75 }