]> icculus.org git repositories - dana/openbox.git/blob - openbox/moveresize.c
ungrab keys before grabbnig keys at the next chain level
[dana/openbox.git] / openbox / moveresize.c
1 #include "grab.h"
2 #include "framerender.h"
3 #include "screen.h"
4 #include "prop.h"
5 #include "client.h"
6 #include "frame.h"
7 #include "openbox.h"
8 #include "resist.h"
9 #include "popup.h"
10 #include "config.h"
11 #include "render/render.h"
12 #include "render/theme.h"
13
14 #include <X11/Xlib.h>
15 #include <glib.h>
16
17 gboolean moveresize_in_progress = FALSE;
18 ObClient *moveresize_client = NULL;
19
20 static gboolean moving = FALSE; /* TRUE - moving, FALSE - resizing */
21
22 static int start_x, start_y, start_cx, start_cy, start_cw, start_ch;
23 static int cur_x, cur_y;
24 static guint button;
25 static guint32 corner;
26 static ObCorner lockcorner;
27
28 static Popup *popup = NULL;
29
30 #define POPUP_X (10)
31 #define POPUP_Y (10)
32
33 static void client_dest(ObClient *c)
34 {
35     if (moveresize_client == c)
36         moveresize_end(TRUE);    
37 }
38
39 void moveresize_startup()
40 {
41     XSetWindowAttributes attrib;
42
43     popup = popup_new(FALSE);
44     popup_size_to_string(popup, "W:  0000  W:  0000");
45
46     attrib.save_under = True;
47
48     client_add_destructor(client_dest);
49 }
50
51 void moveresize_shutdown()
52 {
53     client_remove_destructor(client_dest);
54
55     popup_free(popup);
56     popup = NULL;
57 }
58
59 static void popup_coords(char *format, int a, int b)
60 {
61     char *text;
62     Rect *area;
63
64     text = g_strdup_printf(format, a, b);
65     area = screen_physical_area_monitor(0);
66     popup_position(popup, NorthWestGravity,
67                    POPUP_X + area->x, POPUP_Y + area->y);
68     popup_show(popup, text, NULL);
69     g_free(text);
70 }
71
72 void moveresize_start(ObClient *c, int x, int y, guint b, guint32 cnr)
73 {
74     ObCursor cur;
75
76     g_assert(!moveresize_in_progress);
77
78     moveresize_client = c;
79     start_cx = c->frame->area.x;
80     start_cy = c->frame->area.y;
81     start_cw = c->area.width;
82     start_ch = c->area.height;
83     start_x = x;
84     start_y = y;
85     corner = cnr;
86     button = b;
87
88     /*
89       have to change start_cx and start_cy if going to do this..
90     if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
91         corner == prop_atoms.net_wm_moveresize_size_keyboard)
92         XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
93                      c->area.width / 2, c->area.height / 2);
94     */
95
96     if (corner == prop_atoms.net_wm_moveresize_move ||
97         corner == prop_atoms.net_wm_moveresize_move_keyboard) {
98         cur_x = start_cx;
99         cur_y = start_cy;
100         moving = TRUE;
101     } else {
102         cur_x = start_cw;
103         cur_y = start_ch;
104         moving = FALSE;
105     }
106
107     moveresize_in_progress = TRUE;
108
109     if (corner == prop_atoms.net_wm_moveresize_size_topleft)
110         cur = OB_CURSOR_NORTHWEST;
111     else if (corner == prop_atoms.net_wm_moveresize_size_top)
112         cur = OB_CURSOR_NORTH;
113     else if (corner == prop_atoms.net_wm_moveresize_size_topright)
114         cur = OB_CURSOR_NORTHEAST;
115     else if (corner == prop_atoms.net_wm_moveresize_size_right)
116         cur = OB_CURSOR_EAST;
117     else if (corner == prop_atoms.net_wm_moveresize_size_bottomright)
118         cur = OB_CURSOR_SOUTHEAST;
119     else if (corner == prop_atoms.net_wm_moveresize_size_bottom)
120         cur = OB_CURSOR_SOUTH;
121     else if (corner == prop_atoms.net_wm_moveresize_size_bottomleft)
122         cur = OB_CURSOR_SOUTHWEST;
123     else if (corner == prop_atoms.net_wm_moveresize_size_left)
124         cur = OB_CURSOR_WEST;
125     else if (corner == prop_atoms.net_wm_moveresize_size_keyboard)
126         cur = OB_CURSOR_SOUTHEAST;
127     else if (corner == prop_atoms.net_wm_moveresize_move)
128         cur = OB_CURSOR_MOVE;
129     else if (corner == prop_atoms.net_wm_moveresize_move_keyboard)
130         cur = OB_CURSOR_MOVE;
131     else
132         g_assert_not_reached();
133
134     grab_pointer(TRUE, cur);
135     grab_keyboard(TRUE);
136 }
137
138 void moveresize_end(gboolean cancel)
139 {
140     grab_keyboard(FALSE);
141     grab_pointer(FALSE, None);
142
143     popup_hide(popup);
144
145     if (moving) {
146         client_configure(moveresize_client, OB_CORNER_TOPLEFT,
147                          (cancel ? start_cx : cur_x),
148                          (cancel ? start_cy : cur_y),
149                          start_cw, start_ch, TRUE, TRUE);
150     } else {
151         client_configure(moveresize_client, lockcorner,
152                          moveresize_client->area.x,
153                          moveresize_client->area.y,
154                          (cancel ? start_cw : cur_x),
155                          (cancel ? start_ch : cur_y), TRUE, TRUE);
156     }
157
158     moveresize_in_progress = FALSE;
159     moveresize_client = NULL;
160 }
161
162 static void do_move(gboolean resist)
163 {
164     Rect *a;
165
166     if (resist)
167         resist_move(moveresize_client, &cur_x, &cur_y);
168
169     /* get where the client should be */
170     frame_frame_gravity(moveresize_client->frame, &cur_x, &cur_y);
171     client_configure(moveresize_client, OB_CORNER_TOPLEFT, cur_x, cur_y,
172                      start_cw, start_ch, TRUE, FALSE);
173
174     /* this would be better with a fixed width font ... XXX can do it better
175        if there are 2 text boxes */
176     a = screen_area(screen_desktop);
177     popup_coords("X:  %4d  Y:  %4d",
178                  moveresize_client->frame->area.x - a->x,
179                  moveresize_client->frame->area.y - a->y);
180 }
181
182 static void do_resize(gboolean resist)
183 {
184     if (resist) {
185         /* resist_size needs the frame size */
186         cur_x += moveresize_client->frame->size.left +
187             moveresize_client->frame->size.right;
188         cur_y += moveresize_client->frame->size.top +
189             moveresize_client->frame->size.bottom;
190
191         resist_size(moveresize_client, &cur_x, &cur_y, lockcorner);
192
193         cur_x -= moveresize_client->frame->size.left +
194             moveresize_client->frame->size.right;
195         cur_y -= moveresize_client->frame->size.top +
196             moveresize_client->frame->size.bottom;
197     }
198     
199     client_configure(moveresize_client, lockcorner, 
200                      moveresize_client->area.x, moveresize_client->area.y,
201                      cur_x, cur_y, TRUE, FALSE);
202
203     /* this would be better with a fixed width font ... XXX can do it better
204        if there are 2 text boxes */
205     popup_coords("W:  %4d  H:  %4d", moveresize_client->logical_size.width,
206                  moveresize_client->logical_size.height);
207 }
208
209 void moveresize_event(XEvent *e)
210 {
211     g_assert(moveresize_in_progress);
212
213     if (e->type == ButtonPress) {
214         if (!button) {
215             start_x = e->xbutton.x_root;
216             start_y = e->xbutton.y_root;
217             button = e->xbutton.button; /* this will end it now */
218         }
219     } else if (e->type == ButtonRelease) {
220         if (!button || e->xbutton.button == button) {
221             moveresize_end(FALSE);
222         }
223     } else if (e->type == MotionNotify) {
224         if (moving) {
225             cur_x = start_cx + e->xmotion.x_root - start_x;
226             cur_y = start_cy + e->xmotion.y_root - start_y;
227             do_move(TRUE);
228         } else {
229             if (corner == prop_atoms.net_wm_moveresize_size_topleft) {
230                 cur_x = start_cw - (e->xmotion.x_root - start_x);
231                 cur_y = start_ch - (e->xmotion.y_root - start_y);
232                 lockcorner = OB_CORNER_BOTTOMRIGHT;
233             } else if (corner == prop_atoms.net_wm_moveresize_size_top) {
234                 cur_x = start_cw;
235                 cur_y = start_ch - (e->xmotion.y_root - start_y);
236                 lockcorner = OB_CORNER_BOTTOMRIGHT;
237             } else if (corner == prop_atoms.net_wm_moveresize_size_topright) {
238                 cur_x = start_cw + (e->xmotion.x_root - start_x);
239                 cur_y = start_ch - (e->xmotion.y_root - start_y);
240                 lockcorner = OB_CORNER_BOTTOMLEFT;
241             } else if (corner == prop_atoms.net_wm_moveresize_size_right) { 
242                 cur_x = start_cw + (e->xmotion.x_root - start_x);
243                 cur_y = start_ch;
244                 lockcorner = OB_CORNER_BOTTOMLEFT;
245             } else if (corner ==
246                        prop_atoms.net_wm_moveresize_size_bottomright) {
247                 cur_x = start_cw + (e->xmotion.x_root - start_x);
248                 cur_y = start_ch + (e->xmotion.y_root - start_y);
249                 lockcorner = OB_CORNER_TOPLEFT;
250             } else if (corner == prop_atoms.net_wm_moveresize_size_bottom) {
251                 cur_x = start_cw;
252                 cur_y = start_ch + (e->xmotion.y_root - start_y);
253                 lockcorner = OB_CORNER_TOPLEFT;
254             } else if (corner ==
255                        prop_atoms.net_wm_moveresize_size_bottomleft) {
256                 cur_x = start_cw - (e->xmotion.x_root - start_x);
257                 cur_y = start_ch + (e->xmotion.y_root - start_y);
258                 lockcorner = OB_CORNER_TOPRIGHT;
259             } else if (corner == prop_atoms.net_wm_moveresize_size_left) {
260                 cur_x = start_cw - (e->xmotion.x_root - start_x);
261                 cur_y = start_ch;
262                 lockcorner = OB_CORNER_TOPRIGHT;
263             } else if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
264                 cur_x = start_cw + (e->xmotion.x_root - start_x);
265                 cur_y = start_ch + (e->xmotion.y_root - start_y);
266                 lockcorner = OB_CORNER_TOPLEFT;
267             } else
268                 g_assert_not_reached();
269
270             do_resize(TRUE);
271         }
272     } else if (e->type == KeyPress) {
273         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
274             moveresize_end(TRUE);
275         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
276             moveresize_end(FALSE);
277         else {
278             if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
279                 int dx = 0, dy = 0;
280
281                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
282                     dx = MAX(4, moveresize_client->size_inc.width);
283                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
284                     dx = -MAX(4, moveresize_client->size_inc.width);
285                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
286                     dy = MAX(4, moveresize_client->size_inc.height);
287                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
288                     dy = -MAX(4, moveresize_client->size_inc.height);
289                 else
290                     return;
291
292                 cur_x += dx;
293                 cur_y += dy;
294                 XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
295
296                 do_resize(FALSE);
297             } else if (corner == prop_atoms.net_wm_moveresize_move_keyboard) {
298                 int dx = 0, dy = 0;
299
300                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
301                     dx = 4;
302                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
303                     dx = -4;
304                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
305                     dy = 4;
306                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
307                     dy = -4;
308                 else
309                     return;
310
311                 cur_x += dx;
312                 cur_y += dy;
313                 XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
314
315                 do_move(FALSE);
316             }
317         }
318     }
319 }