08d96ad61d7251e984f86105cf1564ffc6d14d13
[dana/openbox.git] / openbox / moveresize.c
1 #include "grab.h"
2 #include "framerender.h"
3 #include "prop.h"
4 #include "client.h"
5 #include "dispatch.h"
6 #include "openbox.h"
7
8 #include <X11/Xlib.h>
9 #include <glib.h>
10
11 gboolean moveresize_in_progress = FALSE;
12 Client *moveresize_client = NULL;
13
14 static gboolean moving = FALSE; /* TRUE - moving, FALSE - resizing */
15
16 static Window coords = None;
17 static int start_x, start_y, start_cx, start_cy, start_cw, start_ch;
18 static int cur_x, cur_y;
19 static guint button;
20 static guint32 corner;
21 static Corner lockcorner;
22
23 static guint button_return, button_escape, button_left, button_right,
24     button_up, button_down;
25
26 #define POPUP_X (10)
27 #define POPUP_Y (10)
28
29 void moveresize_startup()
30 {
31     button_return = XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
32     button_escape = XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
33     button_left = XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
34     button_right = XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
35     button_up = XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
36     button_down = XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
37 }
38
39 static void popup_coords(char *format, int a, int b)
40 {
41     XSetWindowAttributes attrib;
42     Size s;
43     char *text;
44
45     if (coords == None) {
46         attrib.override_redirect = TRUE;
47         coords = XCreateWindow(ob_display, ob_root,
48                                0, 0, 1, 1, 0, render_depth, InputOutput,
49                                render_visual, CWOverrideRedirect, &attrib);
50         g_assert(coords != None);
51
52         XMapWindow(ob_display, coords);
53     }
54
55     text = g_strdup_printf(format, a, b);
56     framerender_size_popup_label(text, &s);
57     XMoveResizeWindow(ob_display, coords,
58                       POPUP_X, POPUP_Y, s.width, s.height);
59     framerender_popup_label(coords, &s, text);
60     g_free(text);
61 }
62
63 void moveresize_start(Client *c, int x, int y, guint b, guint32 cnr)
64 {
65     Cursor cur;
66
67     g_assert(!moveresize_in_progress);
68
69     moveresize_client = c;
70     start_cx = c->frame->area.x;
71     start_cy = c->frame->area.y;
72     start_cw = c->area.width;
73     start_ch = c->area.height;
74     start_x = x;
75     start_y = y;
76     if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
77         corner == prop_atoms.net_wm_moveresize_size_keyboard)
78         button = 0; /* mouse can't end it without being pressed first */
79     else
80         button = b;
81     corner = cnr;
82
83     if (corner == prop_atoms.net_wm_moveresize_move ||
84         corner == prop_atoms.net_wm_moveresize_move_keyboard) {
85         cur_x = start_cx;
86         cur_y = start_cy;
87         moving = TRUE;
88     } else {
89         cur_x = start_cw;
90         cur_y = start_ch;
91         moving = FALSE;
92     }
93
94     moveresize_in_progress = TRUE;
95
96     if (corner == prop_atoms.net_wm_moveresize_size_topleft)
97         cur = ob_cursors.tl;
98     else if (corner == prop_atoms.net_wm_moveresize_size_top)
99         cur = ob_cursors.t;
100     else if (corner == prop_atoms.net_wm_moveresize_size_topright)
101         cur = ob_cursors.tr;
102     else if (corner == prop_atoms.net_wm_moveresize_size_right)
103         cur = ob_cursors.r;
104     else if (corner == prop_atoms.net_wm_moveresize_size_bottomright)
105         cur = ob_cursors.br;
106     else if (corner == prop_atoms.net_wm_moveresize_size_bottom)
107         cur = ob_cursors.b;
108     else if (corner == prop_atoms.net_wm_moveresize_size_bottomleft)
109         cur = ob_cursors.bl;
110     else if (corner == prop_atoms.net_wm_moveresize_size_left)
111         cur = ob_cursors.l;
112     else if (corner == prop_atoms.net_wm_moveresize_size_keyboard)
113         cur = ob_cursors.br;
114     else if (corner == prop_atoms.net_wm_moveresize_move)
115         cur = ob_cursors.move;
116     else if (corner == prop_atoms.net_wm_moveresize_move_keyboard)
117         cur = ob_cursors.move;
118     else
119         g_assert_not_reached();
120
121     grab_pointer(TRUE, cur);
122     grab_keyboard(TRUE);
123 }
124
125 void moveresize_end(gboolean cancel)
126 {
127     grab_keyboard(FALSE);
128     grab_pointer(FALSE, None);
129
130     XDestroyWindow(ob_display, coords);
131     coords = None;
132
133     if (moving) {
134         client_configure(moveresize_client, Corner_TopLeft,
135                          (cancel ? start_cx : cur_x),
136                          (cancel ? start_cy : cur_y),
137                          start_cw, start_ch, TRUE, TRUE);
138     } else {
139         client_configure(moveresize_client, lockcorner,
140                          moveresize_client->area.x,
141                          moveresize_client->area.y,
142                          (cancel ? start_cw : cur_x),
143                          (cancel ? start_ch : cur_y), TRUE, TRUE);
144     }
145
146     moveresize_in_progress = FALSE;
147     moveresize_client = NULL;
148 }
149
150 static void do_move()
151 {
152     dispatch_move(moveresize_client, &cur_x, &cur_y);
153
154     /* get where the client should be */
155     frame_frame_gravity(moveresize_client->frame, &cur_x, &cur_y);
156     client_configure(moveresize_client, Corner_TopLeft, cur_x, cur_y,
157                      start_cw, start_ch, TRUE, FALSE);
158
159     popup_coords("X:  %d  Y:  %d", moveresize_client->frame->area.x,
160                  moveresize_client->frame->area.y);
161 }
162
163 static void do_resize()
164 {
165     /* dispatch_resize needs the frame size */
166     cur_x += moveresize_client->frame->size.left +
167         moveresize_client->frame->size.right;
168     cur_y += moveresize_client->frame->size.top +
169         moveresize_client->frame->size.bottom;
170
171     dispatch_resize(moveresize_client, &cur_x, &cur_y, lockcorner);
172
173     cur_x -= moveresize_client->frame->size.left +
174         moveresize_client->frame->size.right;
175     cur_y -= moveresize_client->frame->size.top +
176         moveresize_client->frame->size.bottom;
177     
178     client_configure(moveresize_client, lockcorner, moveresize_client->area.x,
179                      moveresize_client->area.y, cur_x, cur_y, TRUE, FALSE);
180
181     popup_coords("W:  %d  H:  %d", moveresize_client->logical_size.width,
182                  moveresize_client->logical_size.height);
183 }
184
185 void moveresize_event(XEvent *e)
186 {
187     g_assert(moveresize_in_progress);
188
189     if (e->type == ButtonPress) {
190         if (!button) {
191             start_x = e->xbutton.x_root;
192             start_y = e->xbutton.y_root;
193             button = e->xbutton.button; /* this will end it now */
194         }
195     } else if (e->type == ButtonRelease) {
196         if (!button || e->xbutton.button == button) {
197             moveresize_end(FALSE);
198         }
199     } else if (e->type == MotionNotify) {
200         if (moving) {
201             cur_x = start_cx + e->xmotion.x_root - start_x;
202             cur_y = start_cy + e->xmotion.y_root - start_y;
203             do_move();
204         } else {
205             if (corner == prop_atoms.net_wm_moveresize_size_topleft) {
206                 cur_x = start_cw - (e->xmotion.x_root - start_x);
207                 cur_y = start_ch - (e->xmotion.y_root - start_y);
208                 lockcorner = Corner_BottomRight;
209             } else if (corner == prop_atoms.net_wm_moveresize_size_top) {
210                 cur_x = start_cw;
211                 cur_y = start_ch - (e->xmotion.y_root - start_y);
212                 lockcorner = Corner_BottomRight;
213             } else if (corner == prop_atoms.net_wm_moveresize_size_topright) {
214                 cur_x = start_cw + (e->xmotion.x_root - start_x);
215                 cur_y = start_ch - (e->xmotion.y_root - start_y);
216                 lockcorner = Corner_BottomLeft;
217             } else if (corner == prop_atoms.net_wm_moveresize_size_right) { 
218                 cur_x = start_cw + (e->xmotion.x_root - start_x);
219                 cur_y = start_ch;
220                 lockcorner = Corner_BottomLeft;
221             } else if (corner ==
222                        prop_atoms.net_wm_moveresize_size_bottomright) {
223                 cur_x = start_cw + (e->xmotion.x_root - start_x);
224                 cur_y = start_ch + (e->xmotion.y_root - start_y);
225                 lockcorner = Corner_TopLeft;
226             } else if (corner == prop_atoms.net_wm_moveresize_size_bottom) {
227                 cur_x = start_cw;
228                 cur_y = start_ch + (e->xmotion.y_root - start_y);
229                 lockcorner = Corner_TopLeft;
230             } else if (corner ==
231                        prop_atoms.net_wm_moveresize_size_bottomleft) {
232                 cur_x = start_cw - (e->xmotion.x_root - start_x);
233                 cur_y = start_ch + (e->xmotion.y_root - start_y);
234                 lockcorner = Corner_TopRight;
235             } else if (corner == prop_atoms.net_wm_moveresize_size_left) {
236                 cur_x = start_cw - (e->xmotion.x_root - start_x);
237                 cur_y = start_ch;
238                 lockcorner = Corner_TopRight;
239             } else if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
240                 cur_x = start_cw + (e->xmotion.x_root - start_x);
241                 cur_y = start_ch + (e->xmotion.y_root - start_y);
242                 lockcorner = Corner_TopLeft;
243             } else
244                 g_assert_not_reached();
245
246             do_resize();
247         }
248     } else if (e->type == KeyPress) {
249         if (e->xkey.keycode == button_escape)
250             moveresize_end(TRUE);
251         else if (e->xkey.keycode == button_return)
252             moveresize_end(FALSE);
253         else {
254             if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
255                 if (e->xkey.keycode == button_right)
256                     cur_x += MAX(4, moveresize_client->size_inc.width);
257                 else if (e->xkey.keycode == button_left)
258                     cur_x -= MAX(4, moveresize_client->size_inc.width);
259                 else if (e->xkey.keycode == button_down)
260                     cur_y += MAX(4, moveresize_client->size_inc.height);
261                 else if (e->xkey.keycode == button_up)
262                     cur_y -= MAX(4, moveresize_client->size_inc.height);
263                 else
264                     return;
265                 do_resize();
266             } else if (corner == prop_atoms.net_wm_moveresize_move_keyboard) {
267                 if (e->xkey.keycode == button_right)
268                     cur_x += 4;
269                 else if (e->xkey.keycode == button_left)
270                     cur_x -= 4;
271                 else if (e->xkey.keycode == button_down)
272                     cur_y += 4;
273                 else if (e->xkey.keycode == button_up)
274                     cur_y -= 4;
275                 else
276                     return;
277                 do_move();
278             }
279         }
280     }
281 }