63de5c5bb4ccc252e924183392e0a2da63dc52d8
[mikachu/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 "dispatch.h"
7 #include "openbox.h"
8 #include "popup.h"
9 #include "config.h"
10 #include "render/render.h"
11 #include "render/theme.h"
12
13 #include <X11/Xlib.h>
14 #include <glib.h>
15
16 gboolean moveresize_in_progress = FALSE;
17 Client *moveresize_client = NULL;
18
19 static gboolean moving = FALSE; /* TRUE - moving, FALSE - resizing */
20
21 static int start_x, start_y, start_cx, start_cy, start_cw, start_ch;
22 static int cur_x, cur_y;
23 static guint button;
24 static guint32 corner;
25 static Corner lockcorner;
26
27 static guint button_return, button_escape, button_left, button_right,
28     button_up, button_down;
29
30 static Popup *popup = NULL;
31 static InternalWindow opaque_window = { { Window_Internal }, None };
32 static GC opaque_gc = None;
33 static gboolean first_draw = FALSE;
34
35 #define POPUP_X (10)
36 #define POPUP_Y (10)
37
38 void moveresize_startup()
39 {
40     XSetWindowAttributes attrib;
41     XGCValues gcv;
42
43     button_return = XKeysymToKeycode(ob_display, XStringToKeysym("Return"));
44     button_escape = XKeysymToKeycode(ob_display, XStringToKeysym("Escape"));
45     button_left = XKeysymToKeycode(ob_display, XStringToKeysym("Left"));
46     button_right = XKeysymToKeycode(ob_display, XStringToKeysym("Right"));
47     button_up = XKeysymToKeycode(ob_display, XStringToKeysym("Up"));
48     button_down = XKeysymToKeycode(ob_display, XStringToKeysym("Down"));
49
50     popup = popup_new(FALSE);
51     popup_size_to_string(popup, "W:  0000  W:  0000");
52
53     attrib.save_under = True;
54     opaque_window.win = XCreateWindow(ob_display, ob_root, 0, 0, 1, 1, 0,
55                                       RrDepth(ob_rr_inst), InputOutput,
56                                       RrVisual(ob_rr_inst),
57                                       CWSaveUnder, &attrib);
58     stacking_add(INTERNAL_AS_WINDOW(&opaque_window));
59     stacking_raise(INTERNAL_AS_WINDOW(&opaque_window));
60
61     /* a GC to invert stuff */
62     gcv.function = GXxor;
63     gcv.line_width = ob_rr_theme->bwidth;
64     gcv.foreground = (WhitePixel(ob_display, ob_screen) ^
65                       BlackPixel(ob_display, ob_screen));
66     opaque_gc = XCreateGC(ob_display, opaque_window.win,
67                           GCFunction | GCForeground | GCLineWidth, &gcv);
68 }
69
70 void moveresize_shutdown()
71 {
72     popup_free(popup);
73     popup = NULL;
74     stacking_remove(&opaque_window);
75     XFreeGC(ob_display, opaque_gc);
76     XDestroyWindow(ob_display, opaque_window.win);
77 }
78
79 static void popup_coords(char *format, int a, int b)
80 {
81     char *text;
82     Rect *area;
83
84     text = g_strdup_printf(format, a, b);
85     area = screen_physical_area_monitor(0);
86     popup_position(popup, NorthWestGravity,
87                    POPUP_X + area->x, POPUP_Y + area->y);
88     popup_show(popup, text, NULL);
89     g_free(text);
90 }
91
92 void moveresize_start(Client *c, int x, int y, guint b, guint32 cnr)
93 {
94     Cursor cur;
95     Rect *a;
96
97     g_assert(!moveresize_in_progress);
98
99     moveresize_client = c;
100     start_cx = c->frame->area.x;
101     start_cy = c->frame->area.y;
102     start_cw = c->area.width;
103     start_ch = c->area.height;
104     start_x = x;
105     start_y = y;
106     if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
107         corner == prop_atoms.net_wm_moveresize_size_keyboard)
108         button = 0; /* mouse can't end it without being pressed first */
109     else
110         button = b;
111     corner = cnr;
112
113     if (corner == prop_atoms.net_wm_moveresize_move ||
114         corner == prop_atoms.net_wm_moveresize_move_keyboard) {
115         cur_x = start_cx;
116         cur_y = start_cy;
117         moving = TRUE;
118     } else {
119         cur_x = start_cw;
120         cur_y = start_ch;
121         moving = FALSE;
122     }
123
124     moveresize_in_progress = TRUE;
125
126     if (corner == prop_atoms.net_wm_moveresize_size_topleft)
127         cur = ob_cursors.tl;
128     else if (corner == prop_atoms.net_wm_moveresize_size_top)
129         cur = ob_cursors.t;
130     else if (corner == prop_atoms.net_wm_moveresize_size_topright)
131         cur = ob_cursors.tr;
132     else if (corner == prop_atoms.net_wm_moveresize_size_right)
133         cur = ob_cursors.r;
134     else if (corner == prop_atoms.net_wm_moveresize_size_bottomright)
135         cur = ob_cursors.br;
136     else if (corner == prop_atoms.net_wm_moveresize_size_bottom)
137         cur = ob_cursors.b;
138     else if (corner == prop_atoms.net_wm_moveresize_size_bottomleft)
139         cur = ob_cursors.bl;
140     else if (corner == prop_atoms.net_wm_moveresize_size_left)
141         cur = ob_cursors.l;
142     else if (corner == prop_atoms.net_wm_moveresize_size_keyboard)
143         cur = ob_cursors.br;
144     else if (corner == prop_atoms.net_wm_moveresize_move)
145         cur = ob_cursors.move;
146     else if (corner == prop_atoms.net_wm_moveresize_move_keyboard)
147         cur = ob_cursors.move;
148     else
149         g_assert_not_reached();
150
151     grab_pointer(TRUE, cur);
152     grab_keyboard(TRUE);
153
154     a = screen_physical_area();
155
156     XMoveResizeWindow(ob_display, opaque_window.win,
157                       a->x, a->y, a->width, a->height);
158     stacking_raise(INTERNAL_AS_WINDOW(&opaque_window));
159     if (corner == prop_atoms.net_wm_moveresize_move ||
160         corner == prop_atoms.net_wm_moveresize_move_keyboard) {
161         if (!config_opaque_move)
162             XMapWindow(ob_display, opaque_window.win);
163     } else {
164         if (!config_opaque_resize)
165             XMapWindow(ob_display, opaque_window.win);
166     }
167     first_draw = TRUE;
168 }
169
170 void moveresize_end(gboolean cancel)
171 {
172     XUnmapWindow(ob_display, opaque_window.win);
173
174     grab_keyboard(FALSE);
175     grab_pointer(FALSE, None);
176
177     popup_hide(popup);
178
179     if (moving) {
180         client_configure(moveresize_client, Corner_TopLeft,
181                          (cancel ? start_cx : cur_x),
182                          (cancel ? start_cy : cur_y),
183                          start_cw, start_ch, TRUE, TRUE);
184     } else {
185         client_configure(moveresize_client, lockcorner,
186                          moveresize_client->area.x,
187                          moveresize_client->area.y,
188                          (cancel ? start_cw : cur_x),
189                          (cancel ? start_ch : cur_y), TRUE, TRUE);
190     }
191
192     moveresize_in_progress = FALSE;
193     moveresize_client = NULL;
194 }
195
196 static void do_move()
197 {
198     int oldx, oldy, oldw, oldh;
199
200     dispatch_move(moveresize_client, &cur_x, &cur_y);
201
202     oldx = moveresize_client->frame->area.x;
203     oldy = moveresize_client->frame->area.y;
204     oldw = moveresize_client->frame->area.width;
205     oldh = moveresize_client->frame->area.height;
206     /* get where the client should be */
207     frame_frame_gravity(moveresize_client->frame, &cur_x, &cur_y);
208     client_configure(moveresize_client, Corner_TopLeft, cur_x, cur_y,
209                      start_cw, start_ch, TRUE, FALSE);
210     /* draw the new one */
211     if (moveresize_client->frame->area.x != oldx ||
212         moveresize_client->frame->area.y != oldy ||
213         moveresize_client->frame->area.width != oldw ||
214         moveresize_client->frame->area.height != oldh) {
215         if (!config_opaque_move)
216             XDrawRectangle(ob_display, opaque_window.win, opaque_gc,
217                            moveresize_client->frame->area.x,
218                            moveresize_client->frame->area.y,
219                            moveresize_client->frame->area.width - 1,
220                            moveresize_client->frame->area.height - 1);
221         /* erase the old one */
222         if (!config_opaque_move && !first_draw)
223             XDrawRectangle(ob_display, opaque_window.win, opaque_gc,
224                            oldx, oldy, oldw - 1, oldh - 1);
225         first_draw = FALSE;
226     }
227
228     /* this would be better with a fixed width font ... XXX can do it better
229        if there are 2 text boxes */
230     popup_coords("X:  %4d  Y:  %4d", moveresize_client->frame->area.x,
231                  moveresize_client->frame->area.y);
232 }
233
234 static void do_resize()
235 {
236     int oldx, oldy, oldw, oldh;
237
238     /* dispatch_resize needs the frame size */
239     cur_x += moveresize_client->frame->size.left +
240         moveresize_client->frame->size.right;
241     cur_y += moveresize_client->frame->size.top +
242         moveresize_client->frame->size.bottom;
243
244     dispatch_resize(moveresize_client, &cur_x, &cur_y, lockcorner);
245
246     cur_x -= moveresize_client->frame->size.left +
247         moveresize_client->frame->size.right;
248     cur_y -= moveresize_client->frame->size.top +
249         moveresize_client->frame->size.bottom;
250     
251     oldx = moveresize_client->frame->area.x;
252     oldy = moveresize_client->frame->area.y;
253     oldw = moveresize_client->frame->area.width;
254     oldh = moveresize_client->frame->area.height;
255     client_configure(moveresize_client, lockcorner, 
256                      moveresize_client->area.x, moveresize_client->area.y,
257                      cur_x, cur_y, TRUE, FALSE);
258     /* draw the new one */
259     if (!config_opaque_resize)
260         XDrawRectangle(ob_display, opaque_window.win, opaque_gc,
261                        moveresize_client->frame->area.x,
262                        moveresize_client->frame->area.y,
263                        moveresize_client->frame->area.width - 1,
264                        moveresize_client->frame->area.height - 1);
265     /* erase the old one */
266     if (!config_opaque_resize && !first_draw)
267         XDrawRectangle(ob_display, opaque_window.win, opaque_gc,
268                        oldx, oldy, oldw - 1, oldh - 1);
269     first_draw = FALSE;
270
271     /* this would be better with a fixed width font ... XXX can do it better
272        if there are 2 text boxes */
273     popup_coords("W:  %4d  H:  %4d", moveresize_client->logical_size.width,
274                  moveresize_client->logical_size.height);
275 }
276
277 void moveresize_event(XEvent *e)
278 {
279     g_assert(moveresize_in_progress);
280
281     if (e->type == ButtonPress) {
282         if (!button) {
283             start_x = e->xbutton.x_root;
284             start_y = e->xbutton.y_root;
285             button = e->xbutton.button; /* this will end it now */
286         }
287     } else if (e->type == ButtonRelease) {
288         if (!button || e->xbutton.button == button) {
289             moveresize_end(FALSE);
290         }
291     } else if (e->type == MotionNotify) {
292         if (moving) {
293             cur_x = start_cx + e->xmotion.x_root - start_x;
294             cur_y = start_cy + e->xmotion.y_root - start_y;
295             do_move();
296         } else {
297             if (corner == prop_atoms.net_wm_moveresize_size_topleft) {
298                 cur_x = start_cw - (e->xmotion.x_root - start_x);
299                 cur_y = start_ch - (e->xmotion.y_root - start_y);
300                 lockcorner = Corner_BottomRight;
301             } else if (corner == prop_atoms.net_wm_moveresize_size_top) {
302                 cur_x = start_cw;
303                 cur_y = start_ch - (e->xmotion.y_root - start_y);
304                 lockcorner = Corner_BottomRight;
305             } else if (corner == prop_atoms.net_wm_moveresize_size_topright) {
306                 cur_x = start_cw + (e->xmotion.x_root - start_x);
307                 cur_y = start_ch - (e->xmotion.y_root - start_y);
308                 lockcorner = Corner_BottomLeft;
309             } else if (corner == prop_atoms.net_wm_moveresize_size_right) { 
310                 cur_x = start_cw + (e->xmotion.x_root - start_x);
311                 cur_y = start_ch;
312                 lockcorner = Corner_BottomLeft;
313             } else if (corner ==
314                        prop_atoms.net_wm_moveresize_size_bottomright) {
315                 cur_x = start_cw + (e->xmotion.x_root - start_x);
316                 cur_y = start_ch + (e->xmotion.y_root - start_y);
317                 lockcorner = Corner_TopLeft;
318             } else if (corner == prop_atoms.net_wm_moveresize_size_bottom) {
319                 cur_x = start_cw;
320                 cur_y = start_ch + (e->xmotion.y_root - start_y);
321                 lockcorner = Corner_TopLeft;
322             } else if (corner ==
323                        prop_atoms.net_wm_moveresize_size_bottomleft) {
324                 cur_x = start_cw - (e->xmotion.x_root - start_x);
325                 cur_y = start_ch + (e->xmotion.y_root - start_y);
326                 lockcorner = Corner_TopRight;
327             } else if (corner == prop_atoms.net_wm_moveresize_size_left) {
328                 cur_x = start_cw - (e->xmotion.x_root - start_x);
329                 cur_y = start_ch;
330                 lockcorner = Corner_TopRight;
331             } else if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
332                 cur_x = start_cw + (e->xmotion.x_root - start_x);
333                 cur_y = start_ch + (e->xmotion.y_root - start_y);
334                 lockcorner = Corner_TopLeft;
335             } else
336                 g_assert_not_reached();
337
338             do_resize();
339         }
340     } else if (e->type == KeyPress) {
341         if (e->xkey.keycode == button_escape)
342             moveresize_end(TRUE);
343         else if (e->xkey.keycode == button_return)
344             moveresize_end(FALSE);
345         else {
346             if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
347                 if (e->xkey.keycode == button_right)
348                     cur_x += MAX(4, moveresize_client->size_inc.width);
349                 else if (e->xkey.keycode == button_left)
350                     cur_x -= MAX(4, moveresize_client->size_inc.width);
351                 else if (e->xkey.keycode == button_down)
352                     cur_y += MAX(4, moveresize_client->size_inc.height);
353                 else if (e->xkey.keycode == button_up)
354                     cur_y -= MAX(4, moveresize_client->size_inc.height);
355                 else
356                     return;
357                 do_resize();
358             } else if (corner == prop_atoms.net_wm_moveresize_move_keyboard) {
359                 if (e->xkey.keycode == button_right)
360                     cur_x += 4;
361                 else if (e->xkey.keycode == button_left)
362                     cur_x -= 4;
363                 else if (e->xkey.keycode == button_down)
364                     cur_y += 4;
365                 else if (e->xkey.keycode == button_up)
366                     cur_y -= 4;
367                 else
368                     return;
369                 do_move();
370             }
371         }
372     }
373 }