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