]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/moveresizeto.c
Rename ObActions* to ObAction* and remove more 3.4-compat action stuff that was missed.
[dana/openbox.git] / openbox / actions / moveresizeto.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 enum {
9     CURRENT_MONITOR = -1,
10     ALL_MONITORS = -2,
11     NEXT_MONITOR = -3,
12     PREV_MONITOR = -4
13 };
14
15 typedef struct {
16     GravityCoord x;
17     GravityCoord y;
18     gint w;
19     gint w_denom;
20     gint h;
21     gint h_denom;
22     gint monitor;
23 } Options;
24
25 static gpointer setup_func(GHashTable *config);
26 static void free_func(gpointer o);
27 static gboolean run_func(ObActionData *data, gpointer options);
28
29 void action_moveresizeto_startup(void)
30 {
31     action_register("MoveResizeTo", setup_func, free_func, run_func);
32 }
33
34 static gpointer setup_func(GHashTable *config)
35 {
36     ObActionValue *v;
37     Options *o;
38
39     o = g_slice_new0(Options);
40     o->x.pos = G_MININT;
41     o->y.pos = G_MININT;
42     o->w = G_MININT;
43     o->h = G_MININT;
44     o->monitor = CURRENT_MONITOR;
45
46     v = g_hash_table_lookup(config, "x");
47     if (v && action_value_is_string(v))
48         action_value_gravity_coord(v, &o->x);
49     v = g_hash_table_lookup(config, "y");
50     if (v && action_value_is_string(v))
51         action_value_gravity_coord(v, &o->y);
52
53     v = g_hash_table_lookup(config, "width");
54     if (v && action_value_is_string(v))
55         if (g_ascii_strcasecmp(action_value_string(v), "current") != 0)
56             action_value_fraction(v, &o->w, &o->w_denom);
57     v = g_hash_table_lookup(config, "height");
58     if (v && action_value_is_string(v))
59         if (g_ascii_strcasecmp(action_value_string(v), "current") != 0)
60             action_value_fraction(v, &o->h, &o->h_denom);
61
62     v = g_hash_table_lookup(config, "monitor");
63     if (v && action_value_is_string(v)) {
64         const gchar *s = action_value_string(v);
65         if (g_ascii_strcasecmp(s, "current") != 0) {
66             if (!g_ascii_strcasecmp(s, "all"))
67                 o->monitor = ALL_MONITORS;
68             else if(!g_ascii_strcasecmp(s, "next"))
69                 o->monitor = NEXT_MONITOR;
70             else if(!g_ascii_strcasecmp(s, "prev"))
71                 o->monitor = PREV_MONITOR;
72             else
73                 o->monitor = action_value_int(v) - 1;
74         }
75     }
76
77     return o;
78 }
79
80 static void free_func(gpointer o)
81 {
82     g_slice_free(Options, o);
83 }
84
85 /* Always return FALSE because its not interactive */
86 static gboolean run_func(ObActionData *data, gpointer options)
87 {
88     Options *o = options;
89
90     if (data->client) {
91         Rect *area, *carea;
92         ObClient *c;
93         guint mon, cmon;
94         gint x, y, lw, lh, w, h;
95
96         c = data->client;
97         mon = o->monitor;
98         cmon = client_monitor(c);
99         switch (mon) {
100         case CURRENT_MONITOR:
101             mon = cmon; break;
102         case ALL_MONITORS:
103             mon = SCREEN_AREA_ALL_MONITORS; break;
104         case NEXT_MONITOR:
105             mon = (cmon + 1 > screen_num_monitors - 1) ? 0 : (cmon + 1); break;
106         case PREV_MONITOR:
107             mon = (cmon == 0) ? (screen_num_monitors - 1) : (cmon - 1); break;
108         default:
109             g_assert_not_reached();
110         }
111
112         area = screen_area(c->desktop, mon, NULL);
113         carea = screen_area(c->desktop, cmon, NULL);
114
115         w = o->w;
116         if (w == G_MININT) w = c->area.width;
117         else if (o->w_denom) w = (w * area->width) / o->w_denom;
118
119         h = o->h;
120         if (h == G_MININT) h = c->area.height;
121         else if (o->h_denom) h = (h * area->height) / o->h_denom;
122
123         /* it might not be able to resize how they requested, so find out what
124            it will actually be resized to */
125         x = c->area.x;
126         y = c->area.y;
127         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
128
129         /* get the frame's size */
130         w += c->frame->size.left + c->frame->size.right;
131         h += c->frame->size.top + c->frame->size.bottom;
132
133         x = o->x.pos;
134         if (o->x.denom)
135             x = (x * area->width) / o->x.denom;
136         if (o->x.center) x = (area->width - w) / 2;
137         else if (x == G_MININT) x = c->frame->area.x - carea->x;
138         else if (o->x.opposite) x = area->width - w - x;
139         x += area->x;
140
141         y = o->y.pos;
142         if (o->y.denom)
143             y = (y * area->height) / o->y.denom;
144         if (o->y.center) y = (area->height - h) / 2;
145         else if (y == G_MININT) y = c->frame->area.y - carea->y;
146         else if (o->y.opposite) y = area->height - h - y;
147         y += area->y;
148
149         /* get the client's size back */
150         w -= c->frame->size.left + c->frame->size.right;
151         h -= c->frame->size.top + c->frame->size.bottom;
152
153         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
154         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
155         /* force it on screen if its moving to another monitor */
156         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
157
158         action_client_move(data, TRUE);
159         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
160         action_client_move(data, FALSE);
161
162         g_slice_free(Rect, area);
163         g_slice_free(Rect, carea);
164     }
165
166     return FALSE;
167 }