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