]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/resizerelative.c
Rename ObActions* to ObAction* and remove more 3.4-compat action stuff that was missed.
[dana/openbox.git] / openbox / actions / resizerelative.c
1 #include "openbox/action.h"
2 #include "openbox/action_value.h"
3 #include "openbox/client.h"
4 #include "openbox/screen.h"
5 #include "openbox/frame.h"
6 #include "openbox/config.h"
7
8 typedef struct {
9     gint left;
10     gint left_denom;
11     gint right;
12     gint right_denom;
13     gint top;
14     gint top_denom;
15     gint bottom;
16     gint bottom_denom;
17 } Options;
18
19 static gpointer setup_func(GHashTable *config);
20 static void     free_func(gpointer options);
21 static gboolean run_func(ObActionData *data, gpointer options);
22
23 void action_resizerelative_startup(void)
24 {
25     action_register("ResizeRelative", setup_func, free_func, run_func);
26 }
27
28 static gpointer setup_func(GHashTable *config)
29 {
30     ObActionValue *v;
31     Options *o;
32
33     o = g_slice_new0(Options);
34
35     v = g_hash_table_lookup(config, "left");
36     if (v && action_value_is_string(v))
37         action_value_fraction(v, &o->left, &o->left_denom);
38     v = g_hash_table_lookup(config, "right");
39     if (v && action_value_is_string(v))
40         action_value_fraction(v, &o->right, &o->right_denom);
41     v = g_hash_table_lookup(config, "top");
42     if (v && action_value_is_string(v))
43         action_value_fraction(v, &o->top, &o->top_denom);
44     v = g_hash_table_lookup(config, "bottom");
45     if (v && action_value_is_string(v))
46         action_value_fraction(v, &o->bottom, &o->bottom_denom);
47
48     return o;
49 }
50
51 static void free_func(gpointer o)
52 {
53     g_slice_free(Options, o);
54 }
55
56 /* Always return FALSE because its not interactive */
57 static gboolean run_func(ObActionData *data, gpointer options)
58 {
59     Options *o = options;
60
61     if (data->client) {
62         ObClient *c = data->client;
63         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
64         gint left = o->left, right = o->right, top = o->top, bottom = o->bottom;
65
66         if (o->left_denom)
67             left = left * c->area.width / o->left_denom;
68         if (o->right_denom)
69             right = right * c->area.width / o->right_denom;
70         if (o->top_denom)
71             top = top * c->area.height / o->top_denom;
72         if (o->bottom_denom)
73             bottom = bottom * c->area.height / o->bottom_denom;
74
75         // When resizing, if the resize has a non-zero value then make sure it
76         // is at least as big as the size increment so the window does actually
77         // resize.
78         x = c->area.x;
79         y = c->area.y;
80         ow = c->area.width;
81         xoff = -MAX(left, (left ? c->size_inc.width : 0));
82         nw = ow + MAX(right + left, (right + left ? c->size_inc.width : 0));
83         oh = c->area.height;
84         yoff = -MAX(top, (top ? c->size_inc.height : 0));
85         nh = oh + MAX(bottom + top, (bottom + top ? c->size_inc.height : 0));
86
87         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
88         xoff = xoff == 0 ? 0 :
89             (xoff < 0 ? MAX(xoff, ow-nw) : MIN(xoff, ow-nw));
90         yoff = yoff == 0 ? 0 :
91             (yoff < 0 ? MAX(yoff, oh-nh) : MIN(yoff, oh-nh));
92
93         action_client_move(data, TRUE);
94         client_move_resize(c, x + xoff, y + yoff, nw, nh);
95         action_client_move(data, FALSE);
96     }
97
98     return FALSE;
99 }