add the resize action
[dana/openbox-history.git] / openbox / actions / resize.c
1 #include "openbox/actions.h"
2 #include "openbox/prop.h"
3 #include "openbox/moveresize.h"
4 #include "openbox/client.h"
5 #include "openbox/frame.h"
6
7 typedef struct {
8     guint32 corner;
9 } Options;
10
11 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
12 static void     free_func(gpointer options);
13 static gboolean run_func(ObActionsData *data, gpointer options);
14
15 static guint32 pick_corner(gint x, gint y, gint cx, gint cy, gint cw, gint ch,
16                            gboolean shaded);
17
18 void action_resize_startup()
19 {
20     actions_register("Resize",
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("edge", node))) {
35         gchar *s = parse_string(doc, n);
36         if (!g_ascii_strcasecmp(s, "top"))
37             o->corner = prop_atoms.net_wm_moveresize_size_top;
38         else if (!g_ascii_strcasecmp(s, "bottom"))
39             o->corner = prop_atoms.net_wm_moveresize_size_bottom;
40         else if (!g_ascii_strcasecmp(s, "left"))
41             o->corner = prop_atoms.net_wm_moveresize_size_left;
42         else if (!g_ascii_strcasecmp(s, "right"))
43             o->corner = prop_atoms.net_wm_moveresize_size_right;
44         else if (!g_ascii_strcasecmp(s, "topleft"))
45             o->corner = prop_atoms.net_wm_moveresize_size_topleft;
46         else if (!g_ascii_strcasecmp(s, "topright"))
47             o->corner = prop_atoms.net_wm_moveresize_size_topright;
48         else if (!g_ascii_strcasecmp(s, "bottomleft"))
49             o->corner = prop_atoms.net_wm_moveresize_size_bottomleft;
50         else if (!g_ascii_strcasecmp(s, "bottomright"))
51             o->corner = prop_atoms.net_wm_moveresize_size_bottomright;
52         g_free(s);
53     }
54     return o;
55 }
56
57 static void free_func(gpointer options)
58 {
59     Options *o = options;
60
61     g_free(o);
62 }
63
64 /* Always return FALSE because its not interactive */
65 static gboolean run_func(ObActionsData *data, gpointer options)
66 {
67     Options *o = options;
68
69     if (data->client) {
70         ObClient *c = data->client;
71         guint32 corner;
72
73         if (!data->button)
74             corner = prop_atoms.net_wm_moveresize_size_keyboard;
75         else if (o->corner)
76             corner = o->corner; /* it was specified in the binding */
77         else
78             corner = pick_corner(data->x, data->y,
79                                  c->frame->area.x, c->frame->area.y,
80                                  /* use the client size because the frame
81                                     can be differently sized (shaded
82                                     windows) and we want this based on the
83                                     clients size */
84                                  c->area.width + c->frame->size.left +
85                                  c->frame->size.right,
86                                  c->area.height + c->frame->size.top +
87                                  c->frame->size.bottom, c->shaded);
88
89         moveresize_start(c, data->x, data->y, data->button, corner);
90     }
91
92     return FALSE;
93 }
94
95 static guint32 pick_corner(gint x, gint y, gint cx, gint cy, gint cw, gint ch,
96                            gboolean shaded)
97 {
98     /* let's make x and y client relative instead of screen relative */
99     x = x - cx;
100     y = ch - (y - cy); /* y is inverted, 0 is at the bottom of the window */
101
102 #define X x*ch/cw
103 #define A -4*X + 7*ch/3
104 #define B  4*X -15*ch/9
105 #define C -X/4 + 2*ch/3
106 #define D  X/4 + 5*ch/12
107 #define E  X/4 +   ch/3
108 #define F -X/4 + 7*ch/12
109 #define G  4*X - 4*ch/3
110 #define H -4*X + 8*ch/3
111 #define a (y > 5*ch/9)
112 #define b (x < 4*cw/9)
113 #define c (x > 5*cw/9)
114 #define d (y < 4*ch/9)
115
116     /*
117       Each of these defines (except X which is just there for fun), represents
118       the equation of a line. The lines they represent are shown in the diagram
119       below. Checking y against these lines, we are able to choose a region
120       of the window as shown.
121
122       +---------------------A-------|-------|-------B---------------------+
123       |                     |A                     B|                     |
124       |                     |A      |       |      B|                     |
125       |                     | A                   B |                     |
126       |                     | A     |       |     B |                     |
127       |                     |  A                 B  |                     |
128       |                     |  A    |       |    B  |                     |
129       |        northwest    |   A     north     B   |   northeast         |
130       |                     |   A   |       |   B   |                     |
131       |                     |    A             B    |                     |
132       C---------------------+----A--+-------+--B----+---------------------D
133       |CCCCCCC              |     A           B     |              DDDDDDD|
134       |       CCCCCCCC      |     A |       | B     |      DDDDDDDD       |
135       |               CCCCCCC      A         B      DDDDDDD               |
136       - - - - - - - - - - - +CCCCCCC+aaaaaaa+DDDDDDD+ - - - - - - - - - - - -
137       |                     |       b       c       |                     | sh
138       |             west    |       b  move c       |   east              | ad
139       |                     |       b       c       |                     | ed
140       - - - - - - - - - - - +EEEEEEE+ddddddd+FFFFFFF+- - - - - - - - - - -  -
141       |               EEEEEEE      G         H      FFFFFFF               |
142       |       EEEEEEEE      |     G |       | H     |      FFFFFFFF       |
143       |EEEEEEE              |     G           H     |              FFFFFFF|
144       E---------------------+----G--+-------+--H----+---------------------F
145       |                     |    G             H    |                     |
146       |                     |   G   |       |   H   |                     |
147       |        southwest    |   G     south     H   |   southeast         |
148       |                     |  G    |       |    H  |                     |
149       |                     |  G                 H  |                     |
150       |                     | G     |       |     H |                     |
151       |                     | G                   H |                     |
152       |                     |G      |       |      H|                     |
153       |                     |G                     H|                     |
154       +---------------------G-------|-------|-------H---------------------+
155     */
156
157     if (shaded) {
158         /* for shaded windows, you can only resize west/east and move */
159         if (b)
160             return prop_atoms.net_wm_moveresize_size_left;
161         if (c)
162             return prop_atoms.net_wm_moveresize_size_right;
163         return prop_atoms.net_wm_moveresize_move;
164     }
165
166     if (y < A && y >= C)
167         return prop_atoms.net_wm_moveresize_size_topleft;
168     else if (y >= A && y >= B && a)
169         return prop_atoms.net_wm_moveresize_size_top;
170     else if (y < B && y >= D)
171         return prop_atoms.net_wm_moveresize_size_topright;
172     else if (y < C && y >= E && b)
173         return prop_atoms.net_wm_moveresize_size_left;
174     else if (y < D && y >= F && c)
175         return prop_atoms.net_wm_moveresize_size_right;
176     else if (y < E && y >= G)
177         return prop_atoms.net_wm_moveresize_size_bottomleft;
178     else if (y < G && y < H && d)
179         return prop_atoms.net_wm_moveresize_size_bottom;
180     else if (y >= H && y < F)
181         return prop_atoms.net_wm_moveresize_size_bottomright;
182     else
183         return prop_atoms.net_wm_moveresize_move;
184
185 #undef X
186 #undef A
187 #undef B
188 #undef C
189 #undef D
190 #undef E
191 #undef F
192 #undef G
193 #undef H
194 #undef a
195 #undef b
196 #undef c
197 #undef d
198 }