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