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