]> Dogcows Code - chaz/openbox/blob - openbox/moveresize.c
use the new sm_client_id function so all windows who should save state do. session...
[chaz/openbox] / 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,
46 RootWindow(ob_display, ob_screen),
47 0, 0, 1, 1, 0,
48 RrDepth(ob_rr_inst), InputOutput,
49 RrVisual(ob_rr_inst),
50 CWSaveUnder, &attrib);
51 stacking_add(INTERNAL_AS_WINDOW(&opaque_window));
52 stacking_raise(INTERNAL_AS_WINDOW(&opaque_window));
53
54 /* a GC to invert stuff */
55 gcv.function = GXxor;
56 gcv.line_width = ob_rr_theme->bwidth;
57 gcv.foreground = (WhitePixel(ob_display, ob_screen) ^
58 BlackPixel(ob_display, ob_screen));
59 opaque_gc = XCreateGC(ob_display, opaque_window.win,
60 GCFunction | GCForeground | GCLineWidth, &gcv);
61 }
62
63 void moveresize_shutdown()
64 {
65 popup_free(popup);
66 popup = NULL;
67 stacking_remove(&opaque_window);
68 XFreeGC(ob_display, opaque_gc);
69 XDestroyWindow(ob_display, opaque_window.win);
70 }
71
72 static void popup_coords(char *format, int a, int b)
73 {
74 char *text;
75 Rect *area;
76
77 text = g_strdup_printf(format, a, b);
78 area = screen_physical_area_monitor(0);
79 popup_position(popup, NorthWestGravity,
80 POPUP_X + area->x, POPUP_Y + area->y);
81 popup_show(popup, text, NULL);
82 g_free(text);
83 }
84
85 void moveresize_start(ObClient *c, int x, int y, guint b, guint32 cnr)
86 {
87 ObCursor cur;
88 Rect *a;
89
90 g_assert(!moveresize_in_progress);
91
92 moveresize_client = c;
93 start_cx = c->frame->area.x;
94 start_cy = c->frame->area.y;
95 start_cw = c->area.width;
96 start_ch = c->area.height;
97 start_x = x;
98 start_y = y;
99 if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
100 corner == prop_atoms.net_wm_moveresize_size_keyboard)
101 button = 0; /* mouse can't end it without being pressed first */
102 else
103 button = b;
104 corner = cnr;
105
106 if (corner == prop_atoms.net_wm_moveresize_move ||
107 corner == prop_atoms.net_wm_moveresize_move_keyboard) {
108 cur_x = start_cx;
109 cur_y = start_cy;
110 moving = TRUE;
111 } else {
112 cur_x = start_cw;
113 cur_y = start_ch;
114 moving = FALSE;
115 }
116
117 moveresize_in_progress = TRUE;
118
119 if (corner == prop_atoms.net_wm_moveresize_size_topleft)
120 cur = OB_CURSOR_NORTHWEST;
121 else if (corner == prop_atoms.net_wm_moveresize_size_top)
122 cur = OB_CURSOR_NORTH;
123 else if (corner == prop_atoms.net_wm_moveresize_size_topright)
124 cur = OB_CURSOR_NORTHEAST;
125 else if (corner == prop_atoms.net_wm_moveresize_size_right)
126 cur = OB_CURSOR_EAST;
127 else if (corner == prop_atoms.net_wm_moveresize_size_bottomright)
128 cur = OB_CURSOR_SOUTHEAST;
129 else if (corner == prop_atoms.net_wm_moveresize_size_bottom)
130 cur = OB_CURSOR_SOUTH;
131 else if (corner == prop_atoms.net_wm_moveresize_size_bottomleft)
132 cur = OB_CURSOR_SOUTHWEST;
133 else if (corner == prop_atoms.net_wm_moveresize_size_left)
134 cur = OB_CURSOR_WEST;
135 else if (corner == prop_atoms.net_wm_moveresize_size_keyboard)
136 cur = OB_CURSOR_SOUTHEAST;
137 else if (corner == prop_atoms.net_wm_moveresize_move)
138 cur = OB_CURSOR_MOVE;
139 else if (corner == prop_atoms.net_wm_moveresize_move_keyboard)
140 cur = OB_CURSOR_MOVE;
141 else
142 g_assert_not_reached();
143
144 grab_pointer(TRUE, cur);
145 grab_keyboard(TRUE);
146
147 a = screen_physical_area();
148
149 XMoveResizeWindow(ob_display, opaque_window.win,
150 a->x, a->y, a->width, a->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, OB_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, OB_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 = OB_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 = OB_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 = OB_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 = OB_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 = OB_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 = OB_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 = OB_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 = OB_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 = OB_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 == ob_keycode(OB_KEY_ESCAPE))
335 moveresize_end(TRUE);
336 else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
337 moveresize_end(FALSE);
338 else {
339 if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
340 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
341 cur_x += MAX(4, moveresize_client->size_inc.width);
342 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
343 cur_x -= MAX(4, moveresize_client->size_inc.width);
344 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
345 cur_y += MAX(4, moveresize_client->size_inc.height);
346 else if (e->xkey.keycode == ob_keycode(OB_KEY_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 == ob_keycode(OB_KEY_RIGHT))
353 cur_x += 4;
354 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
355 cur_x -= 4;
356 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
357 cur_y += 4;
358 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
359 cur_y -= 4;
360 else
361 return;
362 do_move();
363 }
364 }
365 }
366 }
This page took 0.04875 seconds and 4 git commands to generate.