wmaker: Add window snapping feature.
[wmaker-crm.git] / src / moveres.c
blob6899a8663fa24592984a24e336e9f07cf01ff94b
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "client.h"
35 #include "icon.h"
36 #include "dock.h"
37 #include "actions.h"
38 #include "workspace.h"
39 #include "placement.h"
41 #include "geomview.h"
42 #include "screen.h"
43 #include "xinerama.h"
45 #include <WINGs/WINGsP.h>
47 /* How many different types of geometry/position
48 display thingies are there? */
49 #define NUM_DISPLAYS 5
51 #define LEFT 1
52 #define RIGHT 2
53 #define HORIZONTAL (LEFT|RIGHT)
54 #define UP 4
55 #define DOWN 8
56 #define VERTICAL (UP|DOWN)
58 /* True if window currently has a border. This also includes borderless
59 * windows which are currently selected
61 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
65 *----------------------------------------------------------------------
66 * checkMouseSamplingRate-
67 * For lowering the mouse motion sampling rate for machines where
68 * it's too high (SGIs). If it returns False then the event should be
69 * ignored.
70 *----------------------------------------------------------------------
72 static Bool checkMouseSamplingRate(XEvent * ev)
74 static Time previousMotion = 0;
76 if (ev->type == MotionNotify) {
77 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
78 return False;
79 } else {
80 previousMotion = ev->xmotion.time;
83 return True;
87 *----------------------------------------------------------------------
88 * moveGeometryDisplayCentered
90 * routine that moves the geometry/position window on scr so it is
91 * centered over the given coordinates (x,y). Also the window position
92 * is clamped so it stays on the screen at all times.
93 *----------------------------------------------------------------------
95 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
97 unsigned int w = WMWidgetWidth(scr->gview);
98 unsigned int h = WMWidgetHeight(scr->gview);
99 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
101 x -= w / 2;
102 y -= h / 2;
104 /* dead area check */
105 if (scr->xine_info.count) {
106 WMRect rect;
107 int head, flags;
109 rect.pos.x = x;
110 rect.pos.y = y;
111 rect.size.width = w;
112 rect.size.height = h;
114 head = wGetRectPlacementInfo(scr, rect, &flags);
116 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
117 rect = wGetRectForHead(scr, head);
118 x1 = rect.pos.x;
119 y1 = rect.pos.y;
120 x2 = x1 + rect.size.width;
121 y2 = y1 + rect.size.height;
125 if (x < x1 + 1)
126 x = x1 + 1;
127 else if (x > (x2 - w))
128 x = x2 - w;
130 if (y < y1 + 1)
131 y = y1 + 1;
132 else if (y > (y2 - h))
133 y = y2 - h;
135 WMMoveWidget(scr->gview, x, y);
138 static void showPosition(WWindow * wwin, int x, int y)
140 WScreen *scr = wwin->screen_ptr;
142 if (wPreferences.move_display == WDIS_NEW) {
143 #if 0
144 int width = wwin->frame->core->width;
145 int height = wwin->frame->core->height;
147 GC lgc = scr->line_gc;
148 XSetForeground(dpy, lgc, scr->line_pixel);
149 sprintf(num, "%i", x);
151 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
152 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
153 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
154 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
155 #endif
156 } else {
157 WSetGeometryViewShownPosition(scr->gview, x, y);
161 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
163 WScreen *scr = wwin->screen_ptr;
164 WMRect rect;
166 wPreferences.move_display++;
167 wPreferences.move_display %= NUM_DISPLAYS;
169 if (wPreferences.move_display == WDIS_NEW) {
170 wPreferences.move_display++;
171 wPreferences.move_display %= NUM_DISPLAYS;
174 if (wPreferences.move_display == WDIS_NONE) {
175 WMUnmapWidget(scr->gview);
176 } else {
177 if (wPreferences.move_display == WDIS_CENTER) {
178 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
179 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
180 rect.pos.y + rect.size.height / 2);
181 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
182 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
183 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
184 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
185 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
187 WMMapWidget(scr->gview);
191 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
193 WScreen *scr = wwin->screen_ptr;
194 WMRect rect;
196 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
197 return;
198 } else if (wPreferences.move_display == WDIS_CENTER) {
199 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
200 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
201 rect.pos.y + rect.size.height / 2);
202 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
203 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
204 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
205 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
206 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
208 WMMapWidget(scr->gview);
209 WSetGeometryViewShownPosition(scr->gview, x, y);
212 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
214 WScreen *scr = wwin->screen_ptr;
215 Window root = scr->root_win;
216 GC gc = scr->line_gc;
217 int ty, by, my, x, y, mx, s;
218 char num[16];
219 XSegment segment[4];
220 int fw, fh;
222 /* This seems necessary for some odd reason (too lazy to write x1-1 and
223 * x2-1 everywhere below in the code). But why only for x? */
224 x1--;
225 x2--;
227 if (HAS_BORDER_WITH_SELECT(wwin)) {
228 x1 += scr->frame_border_width;
229 x2 += scr->frame_border_width;
230 y1 += scr->frame_border_width;
231 y2 += scr->frame_border_width;
234 ty = y1 + wwin->frame->top_width;
235 by = y2 - wwin->frame->bottom_width;
237 if (wPreferences.size_display == WDIS_NEW) {
238 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
239 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
241 XSetForeground(dpy, gc, scr->line_pixel);
243 /* vertical geometry */
244 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
245 x = x2;
246 s = -15;
247 } else {
248 x = x1;
249 s = 15;
251 my = (ty + by) / 2;
253 /* top arrow & end bar */
254 segment[0].x1 = x - (s + 6);
255 segment[0].y1 = ty;
256 segment[0].x2 = x - (s - 10);
257 segment[0].y2 = ty;
259 /* arrowhead */
260 segment[1].x1 = x - (s - 2);
261 segment[1].y1 = ty + 1;
262 segment[1].x2 = x - (s - 5);
263 segment[1].y2 = ty + 7;
265 segment[2].x1 = x - (s - 2);
266 segment[2].y1 = ty + 1;
267 segment[2].x2 = x - (s + 1);
268 segment[2].y2 = ty + 7;
270 /* line */
271 segment[3].x1 = x - (s - 2);
272 segment[3].y1 = ty + 1;
273 segment[3].x2 = x - (s - 2);
274 segment[3].y2 = my - fh / 2 - 1;
276 XDrawSegments(dpy, root, gc, segment, 4);
278 /* bottom arrow & end bar */
279 segment[0].y1 = by;
280 segment[0].y2 = by;
282 /* arrowhead */
283 segment[1].y1 = by - 1;
284 segment[1].y2 = by - 7;
286 segment[2].y1 = by - 1;
287 segment[2].y2 = by - 7;
289 /* line */
290 segment[3].y1 = my + fh / 2 + 2;
291 segment[3].y2 = by - 1;
293 XDrawSegments(dpy, root, gc, segment, 4);
295 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
296 wwin->normal_hints->height_inc);
297 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
299 /* Display the height. */
300 XSetFont(dpy, gc, scr->tech_draw_font->fid);
301 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
302 strlen(num));
304 /* horizontal geometry */
305 if (y1 < 15) {
306 y = y2;
307 s = -15;
308 } else {
309 y = y1;
310 s = 15;
312 mx = x1 + (x2 - x1) / 2;
313 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
314 wwin->normal_hints->width_inc);
315 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
317 /* left arrow & end bar */
318 segment[0].x1 = x1;
319 segment[0].y1 = y - (s + 6);
320 segment[0].x2 = x1;
321 segment[0].y2 = y - (s - 10);
323 /* arrowhead */
324 segment[1].x1 = x1 + 7;
325 segment[1].y1 = y - (s + 1);
326 segment[1].x2 = x1 + 1;
327 segment[1].y2 = y - (s - 2);
329 segment[2].x1 = x1 + 1;
330 segment[2].y1 = y - (s - 2);
331 segment[2].x2 = x1 + 7;
332 segment[2].y2 = y - (s - 5);
334 /* line */
335 segment[3].x1 = x1 + 1;
336 segment[3].y1 = y - (s - 2);
337 segment[3].x2 = mx - fw / 2 - 2;
338 segment[3].y2 = y - (s - 2);
340 XDrawSegments(dpy, root, gc, segment, 4);
342 /* right arrow & end bar */
343 segment[0].x1 = x2 + 1;
344 segment[0].x2 = x2 + 1;
346 /* arrowhead */
347 segment[1].x1 = x2 - 6;
348 segment[1].x2 = x2;
350 segment[2].x1 = x2;
351 segment[2].x2 = x2 - 6;
353 /* line */
354 segment[3].x1 = mx + fw / 2 + 2;
355 segment[3].x2 = x2;
357 XDrawSegments(dpy, root, gc, segment, 4);
359 /* Display the width. */
360 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
361 strlen(num));
362 } else {
363 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
364 / wwin->normal_hints->width_inc,
365 (by - ty - wwin->normal_hints->base_height)
366 / wwin->normal_hints->height_inc);
370 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
372 WScreen *scr = wwin->screen_ptr;
373 WMRect rect;
375 wPreferences.size_display++;
376 wPreferences.size_display %= NUM_DISPLAYS;
378 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
379 WMUnmapWidget(scr->gview);
380 } else {
381 if (wPreferences.size_display == WDIS_CENTER) {
382 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
383 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
384 rect.pos.y + rect.size.height / 2);
385 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
386 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
387 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
388 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
389 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
391 WMMapWidget(scr->gview);
392 showGeometry(wwin, x, y, x + w, y + h, dir);
396 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
398 WScreen *scr = wwin->screen_ptr;
399 WMRect rect;
401 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
402 return;
404 if (wPreferences.size_display == WDIS_CENTER) {
405 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
406 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
407 rect.pos.y + rect.size.height / 2);
408 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
409 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
410 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
411 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
412 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
414 WMMapWidget(scr->gview);
415 showGeometry(wwin, x, y, x + w, y + h, 0);
418 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
420 WWindow *tmpw;
421 WScreen *scr = wwin->screen_ptr;
422 int x, y;
424 if (!array || !WMGetArrayItemCount(array)) {
425 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
426 } else {
427 WMArrayIterator iter;
429 WM_ITERATE_ARRAY(array, tmpw, iter) {
430 x = tmpw->frame_x + dx;
431 y = tmpw->frame_y + dy;
433 #if 1 /* XXX: with xinerama patch was #if 0, check this */
434 /* don't let windows become unreachable */
436 if (x + (int)tmpw->frame->core->width < 20)
437 x = 20 - (int)tmpw->frame->core->width;
438 else if (x + 20 > scr->scr_width)
439 x = scr->scr_width - 20;
441 if (y + (int)tmpw->frame->core->height < 20)
442 y = 20 - (int)tmpw->frame->core->height;
443 else if (y + 20 > scr->scr_height)
444 y = scr->scr_height - 20;
445 #else
446 wScreenBringInside(scr, &x, &y,
447 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
448 #endif
450 wWindowMove(tmpw, x, y);
455 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
457 Window root = wwin->screen_ptr->root_win;
458 GC gc = wwin->screen_ptr->frame_gc;
459 int h = 0;
460 int bottom = 0;
462 if (HAS_BORDER_WITH_SELECT(wwin)) {
463 x += wwin->screen_ptr->frame_border_width;
464 y += wwin->screen_ptr->frame_border_width;
467 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
468 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
469 TITLEBAR_EXTEND_SPACE) * 2;
471 if (h > wPreferences.window_title_max_height)
472 h = wPreferences.window_title_max_height;
474 if (h < wPreferences.window_title_min_height)
475 h = wPreferences.window_title_min_height;
477 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
478 /* Can't use wwin-frame->bottom_width because, in some cases
479 (e.g. interactive placement), frame does not point to anything. */
480 bottom = RESIZEBAR_HEIGHT;
482 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
484 if (h > 0) {
485 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
487 if (bottom > 0) {
488 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
492 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
494 WWindow *tmpw;
495 int scr_width = wwin->screen_ptr->scr_width;
496 int scr_height = wwin->screen_ptr->scr_height;
497 int x, y;
499 if (!array) {
501 x = wwin->frame_x + dx;
502 y = wwin->frame_y + dy;
504 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
506 } else {
507 WMArrayIterator iter;
509 WM_ITERATE_ARRAY(array, tmpw, iter) {
510 x = tmpw->frame_x + dx;
511 y = tmpw->frame_y + dy;
513 /* don't let windows become unreachable */
514 #if 1 /* XXX: was 0 in XINERAMA patch, check */
515 if (x + (int)tmpw->frame->core->width < 20)
516 x = 20 - (int)tmpw->frame->core->width;
517 else if (x + 20 > scr_width)
518 x = scr_width - 20;
520 if (y + (int)tmpw->frame->core->height < 20)
521 y = 20 - (int)tmpw->frame->core->height;
522 else if (y + 20 > scr_height)
523 y = scr_height - 20;
525 #else
526 wScreenBringInside(wwin->screen_ptr, &x, &y,
527 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
528 #endif
530 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
535 static void flushMotion(void)
537 XEvent ev;
539 XSync(dpy, False);
540 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
543 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
545 /* do not let window be unmapped */
546 if (opaque_move) {
547 wwin->flags.changing_workspace = 1;
548 wWindowChangeWorkspace(wwin, new_workspace);
550 /* go to new workspace */
551 wWorkspaceChange(scr, new_workspace);
553 wwin->flags.changing_workspace = 0;
555 if (rewind)
556 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
557 else
558 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
560 flushMotion();
562 if (!opaque_move) {
563 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
564 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
565 GrabModeAsync, None, wPreferences.cursor[WCUR_MOVE], CurrentTime);
569 typedef struct {
570 /* arrays of WWindows sorted by the respective border position */
571 WWindow **topList; /* top border */
572 WWindow **leftList; /* left border */
573 WWindow **rightList; /* right border */
574 WWindow **bottomList; /* bottom border */
575 int count;
577 /* index of window in the above lists indicating the relative position
578 * of the window with the others */
579 int topIndex;
580 int leftIndex;
581 int rightIndex;
582 int bottomIndex;
584 int rubCount; /* for workspace switching */
586 int winWidth, winHeight; /* width/height of the window */
587 int realX, realY; /* actual position of the window */
588 int calcX, calcY; /* calculated position of window */
589 int omouseX, omouseY; /* old mouse position */
590 int mouseX, mouseY; /* last known position of the pointer */
592 enum {SNAP_NONE, SNAP_LEFT, SNAP_RIGHT} snap;
593 } MoveData;
595 #define WTOP(w) (w)->frame_y
596 #define WLEFT(w) (w)->frame_x
597 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
598 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
599 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
600 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
602 static int compareWTop(const void *a, const void *b)
604 WWindow *wwin1 = *(WWindow **) a;
605 WWindow *wwin2 = *(WWindow **) b;
607 if (WTOP(wwin1) > WTOP(wwin2))
608 return -1;
609 else if (WTOP(wwin1) < WTOP(wwin2))
610 return 1;
611 else
612 return 0;
615 static int compareWLeft(const void *a, const void *b)
617 WWindow *wwin1 = *(WWindow **) a;
618 WWindow *wwin2 = *(WWindow **) b;
620 if (WLEFT(wwin1) > WLEFT(wwin2))
621 return -1;
622 else if (WLEFT(wwin1) < WLEFT(wwin2))
623 return 1;
624 else
625 return 0;
628 static int compareWRight(const void *a, const void *b)
630 WWindow *wwin1 = *(WWindow **) a;
631 WWindow *wwin2 = *(WWindow **) b;
633 if (WRIGHT(wwin1) < WRIGHT(wwin2))
634 return -1;
635 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
636 return 1;
637 else
638 return 0;
641 static int compareWBottom(const void *a, const void *b)
643 WWindow *wwin1 = *(WWindow **) a;
644 WWindow *wwin2 = *(WWindow **) b;
646 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
647 return -1;
648 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
649 return 1;
650 else
651 return 0;
654 static void updateResistance(MoveData *data, int newX, int newY)
656 int i;
657 int newX2 = newX + data->winWidth;
658 int newY2 = newY + data->winHeight;
659 Bool ok = False;
661 if (newX < data->realX) {
662 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
663 ok = True;
664 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
665 ok = True;
667 } else if (newX > data->realX) {
668 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
669 ok = True;
670 } else if (data->rightIndex <= data->count - 1
671 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
672 ok = True;
676 if (!ok) {
677 if (newY < data->realY) {
678 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
679 ok = True;
680 } else if (data->topIndex <= data->count - 1
681 && newY2 <= WTOP(data->topList[data->topIndex])) {
682 ok = True;
684 } else if (newY > data->realY) {
685 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
686 ok = True;
687 } else if (data->bottomIndex <= data->count - 1
688 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
689 ok = True;
694 if (!ok)
695 return;
697 /* TODO: optimize this */
698 if (data->realY < WBOTTOM(data->bottomList[0])) {
699 data->bottomIndex = 0;
701 if (data->realX < WRIGHT(data->rightList[0])) {
702 data->rightIndex = 0;
704 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
705 data->leftIndex = 0;
707 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
708 data->topIndex = 0;
710 for (i = 0; i < data->count; i++) {
711 if (data->realY > WBOTTOM(data->bottomList[i])) {
712 data->bottomIndex = i + 1;
714 if (data->realX > WRIGHT(data->rightList[i])) {
715 data->rightIndex = i + 1;
717 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
718 data->leftIndex = i + 1;
720 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
721 data->topIndex = i + 1;
726 static void freeMoveData(MoveData * data)
728 if (data->topList)
729 wfree(data->topList);
730 if (data->leftList)
731 wfree(data->leftList);
732 if (data->rightList)
733 wfree(data->rightList);
734 if (data->bottomList)
735 wfree(data->bottomList);
738 static void updateMoveData(WWindow * wwin, MoveData * data)
740 WScreen *scr = wwin->screen_ptr;
741 WWindow *tmp;
742 int i;
744 data->count = 0;
745 tmp = scr->focused_window;
746 while (tmp) {
747 if (tmp != wwin && w_global.workspace.current == tmp->frame->workspace
748 && !tmp->flags.miniaturized
749 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
750 data->topList[data->count] = tmp;
751 data->leftList[data->count] = tmp;
752 data->rightList[data->count] = tmp;
753 data->bottomList[data->count] = tmp;
754 data->count++;
756 tmp = tmp->prev;
759 if (data->count == 0) {
760 data->topIndex = 0;
761 data->leftIndex = 0;
762 data->rightIndex = 0;
763 data->bottomIndex = 0;
764 return;
767 /* order from closest to the border of the screen to farthest */
769 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
770 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
771 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
772 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
774 /* figure the position of the window relative to the others */
776 data->topIndex = -1;
777 data->leftIndex = -1;
778 data->rightIndex = -1;
779 data->bottomIndex = -1;
781 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
782 data->bottomIndex = 0;
784 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
785 data->rightIndex = 0;
787 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
788 data->leftIndex = 0;
790 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
791 data->topIndex = 0;
793 for (i = 0; i < data->count; i++) {
794 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
795 data->bottomIndex = i + 1;
797 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
798 data->rightIndex = i + 1;
800 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
801 data->leftIndex = i + 1;
803 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
804 data->topIndex = i + 1;
809 static void initMoveData(WWindow * wwin, MoveData * data)
811 int i;
812 WWindow *tmp;
814 memset(data, 0, sizeof(MoveData));
816 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
818 if (i > 1) {
819 data->topList = wmalloc(sizeof(WWindow *) * i);
820 data->leftList = wmalloc(sizeof(WWindow *) * i);
821 data->rightList = wmalloc(sizeof(WWindow *) * i);
822 data->bottomList = wmalloc(sizeof(WWindow *) * i);
824 updateMoveData(wwin, data);
827 data->realX = wwin->frame_x;
828 data->realY = wwin->frame_y;
829 data->calcX = wwin->frame_x;
830 data->calcY = wwin->frame_y;
832 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
833 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
835 data->snap = SNAP_NONE;
838 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
840 WScreen *scr = wwin->screen_ptr;
841 Bool changed = False;
843 if (data->mouseX <= 1) {
844 if (w_global.workspace.current > 0) {
845 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current - 1, True);
846 changed = True;
847 data->rubCount = 0;
848 } else if (w_global.workspace.current == 0 && wPreferences.ws_cycle) {
849 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.count - 1, True);
850 changed = True;
851 data->rubCount = 0;
853 } else if (data->mouseX >= scr->scr_width - 2) {
854 if (w_global.workspace.current == w_global.workspace.count - 1) {
855 if (wPreferences.ws_cycle || w_global.workspace.count == MAX_WORKSPACES) {
856 crossWorkspace(scr, wwin, opaqueMove, 0, False);
857 changed = True;
858 data->rubCount = 0;
860 /* if user insists on trying to go to next workspace even when
861 * it's already the last, create a new one */
862 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
864 /* detect user "rubbing" the window against the edge */
865 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
867 data->rubCount = -(data->rubCount + 1);
869 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
871 data->rubCount = -data->rubCount + 1;
874 /* create a new workspace */
875 if (abs(data->rubCount) > 2) {
876 /* go to next workspace */
877 wWorkspaceNew(scr);
879 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current + 1, False);
880 changed = True;
881 data->rubCount = 0;
883 } else if (w_global.workspace.current < w_global.workspace.count) {
884 /* go to next workspace */
885 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current + 1, False);
886 changed = True;
887 data->rubCount = 0;
889 } else {
890 data->rubCount = 0;
893 return changed;
896 static void
897 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
898 Bool opaqueMove, int newMouseX, int newMouseY)
900 WScreen *scr = wwin->screen_ptr;
901 int dx, dy; /* how much mouse moved */
902 int winL, winR, winT, winB; /* requested new window position */
903 int newX, newY; /* actual new window position */
904 Bool hresist, vresist;
905 Bool attract;
907 hresist = False;
908 vresist = False;
910 /* check the direction of the movement */
911 dx = newMouseX - data->mouseX;
912 dy = newMouseY - data->mouseY;
914 data->omouseX = data->mouseX;
915 data->omouseY = data->mouseY;
916 data->mouseX = newMouseX;
917 data->mouseY = newMouseY;
919 winL = data->calcX + dx;
920 winR = data->calcX + data->winWidth + dx;
921 winT = data->calcY + dy;
922 winB = data->calcY + data->winHeight + dy;
924 newX = data->realX;
925 newY = data->realY;
927 if (doResistance) {
928 int l_edge, r_edge;
929 int edge_l, edge_r;
930 int t_edge, b_edge;
931 int edge_t, edge_b;
932 int resist;
934 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
935 attract = wPreferences.attract;
936 /* horizontal movement: check horizontal edge resistances */
937 if (dx || dy) {
938 WMRect rect;
939 int i, head;
940 /* window is the leftmost window: check against screen edge */
942 /* Add inter head resistance 1/2 (if needed) */
943 head = wGetHeadForPointerLocation(scr);
944 rect = wGetRectForHead(scr, head);
946 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
947 edge_l = l_edge - resist;
948 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
949 r_edge = edge_r + resist;
951 /* 1 */
952 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
953 WWindow *looprw;
955 for (i = data->rightIndex - 1; i >= 0; i--) {
956 looprw = data->rightList[i];
957 if (!(data->realY > WBOTTOM(looprw)
958 || (data->realY + data->winHeight) < WTOP(looprw))) {
959 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
960 l_edge = WRIGHT(looprw) + 1;
961 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
963 break;
967 if (attract) {
968 for (i = data->rightIndex; i < data->count; i++) {
969 looprw = data->rightList[i];
970 if (!(data->realY > WBOTTOM(looprw)
971 || (data->realY + data->winHeight) < WTOP(looprw))) {
972 r_edge = WRIGHT(looprw) + 1;
973 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
974 break;
980 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
981 WWindow *looprw;
983 for (i = data->leftIndex - 1; i >= 0; i--) {
984 looprw = data->leftList[i];
985 if (!(data->realY > WBOTTOM(looprw)
986 || (data->realY + data->winHeight) < WTOP(looprw))) {
987 if (attract
988 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
989 && dx > 0)) {
990 edge_r = WLEFT(looprw);
991 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
993 break;
997 if (attract)
998 for (i = data->leftIndex; i < data->count; i++) {
999 looprw = data->leftList[i];
1000 if (!(data->realY > WBOTTOM(looprw)
1001 || (data->realY + data->winHeight) < WTOP(looprw))) {
1002 edge_l = WLEFT(looprw);
1003 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1004 break;
1010 printf("%d %d\n",winL,winR);
1011 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1014 if ((winL - l_edge) < (r_edge - winL)) {
1015 if (resist > 0) {
1016 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1017 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1018 newX = l_edge;
1019 hresist = True;
1022 } else {
1023 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1024 newX = r_edge;
1025 hresist = True;
1029 if ((winR - edge_l) < (edge_r - winR)) {
1030 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1031 newX = edge_l - data->winWidth;
1032 hresist = True;
1034 } else {
1035 if (resist > 0) {
1036 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1037 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1038 newX = edge_r - data->winWidth;
1039 hresist = True;
1044 /* VeRT */
1045 /* Add inter head resistance 2/2 (if needed) */
1046 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1047 edge_t = t_edge - resist;
1048 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1049 b_edge = edge_b + resist;
1051 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1052 WWindow *looprw;
1054 for (i = data->bottomIndex - 1; i >= 0; i--) {
1055 looprw = data->bottomList[i];
1056 if (!(data->realX > WRIGHT(looprw)
1057 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1058 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1059 t_edge = WBOTTOM(looprw) + 1;
1060 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1062 break;
1066 if (attract) {
1067 for (i = data->bottomIndex; i < data->count; i++) {
1068 looprw = data->bottomList[i];
1069 if (!(data->realX > WRIGHT(looprw)
1070 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1071 b_edge = WBOTTOM(looprw) + 1;
1072 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1073 break;
1079 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1080 WWindow *looprw;
1082 for (i = data->topIndex - 1; i >= 0; i--) {
1083 looprw = data->topList[i];
1084 if (!(data->realX > WRIGHT(looprw)
1085 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1086 if (attract
1087 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1088 && dy > 0)) {
1089 edge_b = WTOP(looprw);
1090 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1092 break;
1096 if (attract)
1097 for (i = data->topIndex; i < data->count; i++) {
1098 looprw = data->topList[i];
1099 if (!(data->realX > WRIGHT(looprw)
1100 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1101 edge_t = WTOP(looprw);
1102 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1103 break;
1108 if ((winT - t_edge) < (b_edge - winT)) {
1109 if (resist > 0) {
1110 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1111 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1112 newY = t_edge;
1113 vresist = True;
1116 } else {
1117 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1118 newY = b_edge;
1119 vresist = True;
1123 if ((winB - edge_t) < (edge_b - winB)) {
1124 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1125 newY = edge_t - data->winHeight;
1126 vresist = True;
1128 } else {
1129 if (resist > 0) {
1130 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1131 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1132 newY = edge_b - data->winHeight;
1133 vresist = True;
1138 /* END VeRT */
1142 /* update window position */
1143 data->calcX += dx;
1144 data->calcY += dy;
1146 if (((dx > 0 && data->calcX - data->realX > 0)
1147 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1148 newX = data->calcX;
1150 if (((dy > 0 && data->calcY - data->realY > 0)
1151 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1152 newY = data->calcY;
1154 if (data->realX != newX || data->realY != newY) {
1156 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1157 showPosition(wwin, data->realX, data->realY);
1159 if (opaqueMove) {
1160 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1161 } else {
1162 /* erase frames */
1163 drawFrames(wwin, scr->selected_windows,
1164 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1167 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1169 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1172 if (!opaqueMove) {
1173 /* draw frames */
1174 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1177 if (!scr->selected_windows) {
1178 showPosition(wwin, newX, newY);
1182 /* recalc relative window position */
1183 if (doResistance && (data->realX != newX || data->realY != newY)) {
1184 updateResistance(data, newX, newY);
1187 data->realX = newX;
1188 data->realY = newY;
1191 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1193 #define MOVABLE_BIT 0x01
1194 #define RESIZABLE_BIT 0x02
1196 int wKeyboardMoveResizeWindow(WWindow * wwin)
1198 WScreen *scr = wwin->screen_ptr;
1199 Window root = scr->root_win;
1200 XEvent event;
1201 int w = wwin->frame->core->width;
1202 int h = wwin->frame->core->height;
1203 int scr_width = wwin->screen_ptr->scr_width;
1204 int scr_height = wwin->screen_ptr->scr_height;
1205 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1206 int src_x = wwin->frame_x;
1207 int src_y = wwin->frame_y;
1208 int original_w = w;
1209 int original_h = h;
1210 int done, off_x, off_y, ww, wh;
1211 int kspeed = _KS;
1212 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1213 Time lastTime = 0;
1214 KeyCode shiftl, shiftr, ctrlmode;
1215 KeySym keysym = NoSymbol;
1216 int moment = 0;
1217 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1218 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1219 ? wGetHeadForWindow(wwin)
1220 : scr->xine_info.primary_head);
1222 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1223 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1224 ctrlmode = done = off_x = off_y = 0;
1226 if (modes == RESIZABLE_BIT) {
1227 ctrlmode = 1;
1230 XSync(dpy, False);
1231 wusleep(10000);
1232 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1234 if (!wwin->flags.selected) {
1235 wUnselectWindows(scr);
1237 XGrabServer(dpy);
1238 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1239 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1240 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime);
1244 if (!opaqueMoveResize) {
1245 if (wwin->flags.shaded || scr->selected_windows) {
1246 if (scr->selected_windows)
1247 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1248 else
1249 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1250 } else {
1251 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1254 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1255 mapPositionDisplay(wwin, src_x, src_y, w, h);
1258 ww = w;
1259 wh = h;
1260 while (1) {
1262 looper.ox=off_x;
1263 looper.oy=off_y;
1265 do {
1266 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1267 | ButtonPressMask | ExposureMask, &event);
1268 if (event.type == Expose) {
1269 WMHandleEvent(&event);
1271 } while (event.type == Expose);
1273 if (!opaqueMoveResize) {
1274 if (wwin->flags.shaded || scr->selected_windows) {
1275 if (scr->selected_windows)
1276 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1277 else
1278 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1279 /*** I HATE EDGE RESISTANCE - ]d ***/
1280 } else {
1281 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1285 if (ctrlmode)
1286 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1289 XUngrabServer(dpy);
1290 XSync(dpy, False);
1292 switch (event.type) {
1293 case KeyPress:
1294 /* accelerate */
1295 if (event.xkey.time - lastTime > 50) {
1296 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1297 } else {
1298 if (kspeed < 20) {
1299 kspeed++;
1302 if (kspeed < _KS)
1303 kspeed = _KS;
1304 lastTime = event.xkey.time;
1305 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1306 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1307 ctrlmode = 1;
1308 wUnselectWindows(scr);
1309 } else {
1310 ctrlmode = 0;
1313 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1314 if (ctrlmode)
1315 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1316 else
1317 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1318 } else {
1320 keysym = XLookupKeysym(&event.xkey, 0);
1321 switch (keysym) {
1322 case XK_Return:
1323 done = 2;
1324 break;
1325 case XK_Escape:
1326 done = 1;
1327 break;
1328 case XK_Up:
1329 #ifdef XK_KP_Up
1330 case XK_KP_Up:
1331 #endif
1332 case XK_k:
1333 if (ctrlmode) {
1334 if (moment != UP)
1335 h = wh;
1336 h -= kspeed;
1337 moment = UP;
1338 if (h < 1)
1339 h = 1;
1340 } else
1341 off_y -= kspeed;
1342 break;
1343 case XK_Down:
1344 #ifdef XK_KP_Down
1345 case XK_KP_Down:
1346 #endif
1347 case XK_j:
1348 if (ctrlmode) {
1349 if (moment != DOWN)
1350 h = wh;
1351 h += kspeed;
1352 moment = DOWN;
1353 } else
1354 off_y += kspeed;
1355 break;
1356 case XK_Left:
1357 #ifdef XK_KP_Left
1358 case XK_KP_Left:
1359 #endif
1360 case XK_h:
1361 if (ctrlmode) {
1362 if (moment != LEFT)
1363 w = ww;
1364 w -= kspeed;
1365 if (w < 1)
1366 w = 1;
1367 moment = LEFT;
1368 } else
1369 off_x -= kspeed;
1370 break;
1371 case XK_Right:
1372 #ifdef XK_KP_Right
1373 case XK_KP_Right:
1374 #endif
1375 case XK_l:
1376 if (ctrlmode) {
1377 if (moment != RIGHT)
1378 w = ww;
1379 w += kspeed;
1380 moment = RIGHT;
1381 } else
1382 off_x += kspeed;
1383 break;
1386 ww = w;
1387 wh = h;
1388 wh -= vert_border;
1389 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1390 wh += vert_border;
1392 if (wPreferences.ws_cycle) {
1393 if (src_x + off_x + ww < 20) {
1394 if (!w_global.workspace.current)
1395 wWorkspaceChange(scr, w_global.workspace.count - 1);
1396 else
1397 wWorkspaceChange(scr, w_global.workspace.current - 1);
1399 off_x += scr_width;
1400 } else if (src_x + off_x + 20 > scr_width) {
1401 if (w_global.workspace.current == w_global.workspace.count - 1)
1402 wWorkspaceChange(scr, 0);
1403 else
1404 wWorkspaceChange(scr, w_global.workspace.current + 1);
1406 off_x -= scr_width;
1408 } else {
1409 if (src_x + off_x + ww < 20)
1410 off_x = 20 - ww - src_x;
1411 else if (src_x + off_x + 20 > scr_width)
1412 off_x = scr_width - 20 - src_x;
1415 if (src_y + off_y + wh < 20) {
1416 off_y = 20 - wh - src_y;
1417 } else if (src_y + off_y + 20 > scr_height) {
1418 off_y = scr_height - 20 - src_y;
1421 break;
1422 case ButtonPress:
1423 case ButtonRelease:
1424 done = 1;
1425 break;
1426 case Expose:
1427 WMHandleEvent(&event);
1428 while (XCheckTypedEvent(dpy, Expose, &event)) {
1429 WMHandleEvent(&event);
1431 break;
1433 default:
1434 WMHandleEvent(&event);
1435 break;
1438 XGrabServer(dpy);
1439 /*xxx */
1441 if (wwin->flags.shaded && !scr->selected_windows) {
1442 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1443 } else {
1444 if (ctrlmode) {
1445 WMUnmapWidget(scr->gview);
1446 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1447 } else if (!scr->selected_windows) {
1448 WMUnmapWidget(scr->gview);
1449 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1453 if (!opaqueMoveResize) {
1454 if (wwin->flags.shaded || scr->selected_windows) {
1455 if (scr->selected_windows)
1456 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1457 else
1458 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1459 } else {
1460 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1464 if (ctrlmode) {
1465 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1467 } else if (!scr->selected_windows)
1468 showPosition(wwin, src_x + off_x, src_y + off_y);
1470 if (opaqueMoveResize) {
1471 XUngrabServer(dpy);
1472 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1475 if (done) {
1476 if (!opaqueMoveResize) { /* ctrlmode => resize */
1477 if (wwin->flags.shaded || scr->selected_windows) {
1478 if (scr->selected_windows)
1479 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1480 else
1481 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1482 } else {
1483 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1487 if (ctrlmode) {
1488 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1489 src_y + off_y + wh, 0);
1490 WMUnmapWidget(scr->gview);
1491 } else
1492 WMUnmapWidget(scr->gview);
1494 XUngrabKeyboard(dpy, CurrentTime);
1495 XUngrabPointer(dpy, CurrentTime);
1496 XUngrabServer(dpy);
1498 if (done == 2) {
1499 if (wwin->flags.shaded || scr->selected_windows) {
1500 if (!scr->selected_windows) {
1501 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1502 wWindowSynthConfigureNotify(wwin);
1503 } else {
1504 WMArrayIterator iter;
1505 WWindow *foo;
1507 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1509 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1510 wWindowSynthConfigureNotify(foo);
1513 } else {
1514 if (ww != original_w)
1515 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
1517 if (wh != original_h)
1518 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1520 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1521 wWindowSynthConfigureNotify(wwin);
1523 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1524 wSetFocusTo(scr, wwin);
1527 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1528 head != wGetHeadForWindow(wwin)) {
1529 wArrangeIcons(scr, True);
1532 update_saved_geometry(wwin);
1534 return 1;
1540 *----------------------------------------------------------------------
1541 * wMouseMoveWindow--
1542 * Move the named window and the other selected ones (if any),
1543 * interactively. Also shows the position of the window, if only one
1544 * window is being moved.
1545 * If the window is not on the selected window list, the selected
1546 * windows are deselected.
1547 * If shift is pressed during the operation, the position display
1548 * is changed to another type.
1550 * Returns:
1551 * True if the window was moved, False otherwise.
1553 * Side effects:
1554 * The window(s) position is changed, and the client(s) are
1555 * notified about that.
1556 * The position display configuration may be changed.
1557 *----------------------------------------------------------------------
1559 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1561 WScreen *scr = wwin->screen_ptr;
1562 XEvent event;
1563 Window root = scr->root_win;
1564 KeyCode shiftl, shiftr;
1565 Bool done = False;
1566 int started = 0;
1567 int warped = 0;
1568 /* This needs not to change while moving, else bad things can happen */
1569 int opaqueMove = wPreferences.opaque_move;
1570 MoveData moveData;
1571 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1572 ? wGetHeadForWindow(wwin)
1573 : scr->xine_info.primary_head);
1575 if (!IS_MOVABLE(wwin))
1576 return False;
1578 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1579 XSetWindowAttributes attr;
1581 attr.save_under = True;
1582 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1585 initMoveData(wwin, &moveData);
1587 moveData.mouseX = ev->xmotion.x_root;
1588 moveData.mouseY = ev->xmotion.y_root;
1590 if (!wwin->flags.selected) {
1591 /* this window is not selected, unselect others and move only wwin */
1592 wUnselectWindows(scr);
1594 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1595 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1596 while (!done) {
1597 if (warped) {
1598 int junk;
1599 Window junkw;
1601 /* XWarpPointer() doesn't seem to generate Motion events, so
1602 * we've got to simulate them */
1603 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1604 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1605 } else {
1606 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1607 | PointerMotionHintMask
1608 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1610 if (event.type == MotionNotify) {
1611 /* compress MotionNotify events */
1612 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1613 if (!checkMouseSamplingRate(&event))
1614 continue;
1617 switch (event.type) {
1618 case KeyPress:
1619 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1620 && started && !scr->selected_windows) {
1622 if (!opaqueMove) {
1623 drawFrames(wwin, scr->selected_windows,
1624 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1627 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1628 showPosition(wwin, moveData.realX, moveData.realY);
1629 XUngrabServer(dpy);
1631 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1632 moveData.winWidth, moveData.winHeight);
1634 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1635 XGrabServer(dpy);
1636 showPosition(wwin, moveData.realX, moveData.realY);
1639 if (!opaqueMove) {
1640 drawFrames(wwin, scr->selected_windows,
1641 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1643 /*} else {
1644 WMHandleEvent(&event); this causes problems needs fixing */
1646 break;
1648 case MotionNotify:
1649 if (IS_RESIZABLE(wwin) && wPreferences.window_snapping && wPreferences.no_autowrap) {
1650 if (moveData.snap == SNAP_LEFT && moveData.mouseX > 1) {
1651 moveData.snap = SNAP_NONE;
1652 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height);
1654 if (moveData.snap == SNAP_RIGHT && moveData.mouseX < scr->scr_width - 2) {
1655 moveData.snap = SNAP_NONE;
1656 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2,
1657 scr->scr_height);
1659 if (moveData.snap == SNAP_NONE) {
1660 if (moveData.mouseX <= 1) {
1661 moveData.snap = SNAP_LEFT;
1662 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2,
1663 scr->scr_height);
1665 if (moveData.mouseX >= scr->scr_width - 2) {
1666 moveData.snap = SNAP_RIGHT;
1667 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2,
1668 scr->scr_height);
1673 if (started) {
1674 if (moveData.snap == SNAP_LEFT)
1675 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height);
1676 if (moveData.snap == SNAP_RIGHT)
1677 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2,
1678 scr->scr_height);
1680 updateWindowPosition(wwin, &moveData,
1681 scr->selected_windows == NULL
1682 && wPreferences.edge_resistance > 0,
1683 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1685 if (moveData.snap == SNAP_LEFT)
1686 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height);
1687 if (moveData.snap == SNAP_RIGHT)
1688 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2,
1689 scr->scr_height);
1691 if (!warped && !wPreferences.no_autowrap) {
1692 int oldWorkspace = w_global.workspace.current;
1694 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1695 showPosition(wwin, moveData.realX, moveData.realY);
1696 XUngrabServer(dpy);
1698 if (!opaqueMove) {
1699 drawFrames(wwin, scr->selected_windows,
1700 moveData.realX - wwin->frame_x,
1701 moveData.realY - wwin->frame_y);
1703 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1704 if (w_global.workspace.current != oldWorkspace
1705 && wPreferences.edge_resistance > 0
1706 && scr->selected_windows == NULL)
1707 updateMoveData(wwin, &moveData);
1708 warped = 1;
1710 if (!opaqueMove) {
1711 drawFrames(wwin, scr->selected_windows,
1712 moveData.realX - wwin->frame_x,
1713 moveData.realY - wwin->frame_y);
1715 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1716 XSync(dpy, False);
1717 showPosition(wwin, moveData.realX, moveData.realY);
1718 XGrabServer(dpy);
1720 } else {
1721 warped = 0;
1723 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1724 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1726 XChangeActivePointerGrab(dpy, ButtonMotionMask
1727 | ButtonReleaseMask | ButtonPressMask,
1728 wPreferences.cursor[WCUR_MOVE], CurrentTime);
1729 started = 1;
1730 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1732 if (!scr->selected_windows)
1733 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1734 moveData.winWidth, moveData.winHeight);
1736 if (started && !opaqueMove)
1737 drawFrames(wwin, scr->selected_windows, 0, 0);
1739 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1740 && !scr->selected_windows)) {
1741 XGrabServer(dpy);
1742 if (wPreferences.move_display == WDIS_NEW)
1743 showPosition(wwin, moveData.realX, moveData.realY);
1747 break;
1749 case ButtonPress:
1750 break;
1752 case ButtonRelease:
1753 if (event.xbutton.button != ev->xbutton.button)
1754 break;
1756 if (moveData.snap != SNAP_NONE) {
1757 if (moveData.snap == SNAP_LEFT) {
1758 /* erase frames */
1759 if (!opaqueMove)
1760 drawFrames(wwin, scr->selected_windows,
1761 moveData.realX - wwin->frame_x,
1762 moveData.realY - wwin->frame_y);
1763 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height);
1764 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF);
1766 if (moveData.snap == SNAP_RIGHT) {
1767 /* erase frames */
1768 if (!opaqueMove)
1769 drawFrames(wwin, scr->selected_windows,
1770 moveData.realX - wwin->frame_x,
1771 moveData.realY - wwin->frame_y);
1772 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2,
1773 scr->scr_height);
1774 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF);
1776 moveData.snap = SNAP_NONE;
1777 } else if (started) {
1778 XEvent e;
1779 if (!opaqueMove) {
1780 drawFrames(wwin, scr->selected_windows,
1781 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1782 XSync(dpy, 0);
1783 doWindowMove(wwin, scr->selected_windows,
1784 moveData.realX - wwin->frame_x,
1785 moveData.realY - wwin->frame_y);
1787 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1788 wWindowSynthConfigureNotify(wwin);
1789 #endif
1790 XUngrabKeyboard(dpy, CurrentTime);
1791 XUngrabServer(dpy);
1792 if (!opaqueMove) {
1793 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1794 wSetFocusTo(scr, wwin);
1796 if (wPreferences.move_display == WDIS_NEW)
1797 showPosition(wwin, moveData.realX, moveData.realY);
1799 /* discard all enter/leave events that happened until
1800 * the time the button was released */
1801 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1802 if (e.xcrossing.time > event.xbutton.time) {
1803 XPutBackEvent(dpy, &e);
1804 break;
1807 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1808 if (e.xcrossing.time > event.xbutton.time) {
1809 XPutBackEvent(dpy, &e);
1810 break;
1814 if (!scr->selected_windows) {
1815 /* get rid of the geometry window */
1816 WMUnmapWidget(scr->gview);
1819 done = True;
1820 break;
1822 default:
1823 if (started && !opaqueMove) {
1824 drawFrames(wwin, scr->selected_windows,
1825 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1826 XUngrabServer(dpy);
1827 WMHandleEvent(&event);
1828 XSync(dpy, False);
1829 XGrabServer(dpy);
1830 drawFrames(wwin, scr->selected_windows,
1831 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1832 } else {
1833 WMHandleEvent(&event);
1835 break;
1839 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1840 XSetWindowAttributes attr;
1842 attr.save_under = False;
1843 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1847 freeMoveData(&moveData);
1849 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1850 head != wGetHeadForWindow(wwin)) {
1851 wArrangeIcons(scr, True);
1854 if (started)
1855 update_saved_geometry(wwin);
1857 return started;
1860 #define RESIZEBAR 1
1861 #define HCONSTRAIN 2
1863 static int getResizeDirection(WWindow * wwin, int x, int y, int dy, int flags)
1865 int w = wwin->frame->core->width - 1;
1866 int cw = wwin->frame->resizebar_corner_width;
1867 int dir;
1869 /* if not resizing through the resizebar */
1870 if (!(flags & RESIZEBAR)) {
1872 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1873 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1875 /* How much resize space is allowed */
1876 int spacew = abs(wwin->client.width / 3);
1877 int spaceh = abs(wwin->client.height / 3);
1879 /* Determine where x fits */
1880 if ((abs(x) > wwin->client.width/2 - spacew/2) &&
1881 (abs(x) < wwin->client.width/2 + spacew/2)) {
1882 /* Resize vertically */
1883 xdir = 0;
1884 } else if ((abs(y) > wwin->client.height/2 - spaceh/2) &&
1885 (abs(y) < wwin->client.height/2 + spaceh/2)) {
1886 /* Resize horizontally */
1887 ydir = 0;
1889 return (xdir | ydir);
1892 /* window is too narrow. allow diagonal resize */
1893 if (cw * 2 >= w) {
1894 int ydir;
1896 if (flags & HCONSTRAIN)
1897 ydir = 0;
1898 else
1899 ydir = DOWN;
1900 if (x < cw)
1901 return (LEFT | ydir);
1902 else
1903 return (RIGHT | ydir);
1905 /* vertical resize */
1906 if ((x > cw) && (x < w - cw))
1907 return DOWN;
1909 if (x < cw)
1910 dir = LEFT;
1911 else
1912 dir = RIGHT;
1914 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1915 dir |= DOWN;
1917 return dir;
1920 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1922 XEvent event;
1923 WScreen *scr = wwin->screen_ptr;
1924 Window root = scr->root_win;
1925 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1926 int fw = wwin->frame->core->width;
1927 int fh = wwin->frame->core->height;
1928 int fx = wwin->frame_x;
1929 int fy = wwin->frame_y;
1930 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1931 int orig_x, orig_y;
1932 int started;
1933 int dw, dh;
1934 int rw = fw, rh = fh;
1935 int rx1, ry1, rx2, ry2;
1936 int res = 0;
1937 KeyCode shiftl, shiftr;
1938 int orig_fx = fx;
1939 int orig_fy = fy;
1940 int orig_fw = fw;
1941 int orig_fh = fh;
1942 int original_fw = fw;
1943 int original_fh = fh;
1944 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1945 ? wGetHeadForWindow(wwin)
1946 : scr->xine_info.primary_head);
1947 int opaqueResize = wPreferences.opaque_resize;
1949 if (!IS_RESIZABLE(wwin))
1950 return;
1952 if (wwin->flags.shaded) {
1953 wwarning("internal error: tryein");
1954 return;
1956 orig_x = ev->xbutton.x_root;
1957 orig_y = ev->xbutton.y_root;
1959 started = 0;
1960 wUnselectWindows(scr);
1961 rx1 = fx;
1962 rx2 = fx + fw - 1;
1963 ry1 = fy;
1964 ry2 = fy + fh - 1;
1965 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1966 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1968 while (1) {
1969 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1970 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1971 if (!checkMouseSamplingRate(&event))
1972 continue;
1974 switch (event.type) {
1975 case KeyPress:
1976 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1977 if (!opaqueResize) {
1978 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1979 && started) {
1980 drawTransparentFrame(wwin, fx, fy, fw, fh);
1981 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1982 drawTransparentFrame(wwin, fx, fy, fw, fh);
1985 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1986 break;
1988 case MotionNotify:
1989 if (started) {
1990 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1992 dw = 0;
1993 dh = 0;
1995 orig_fx = fx;
1996 orig_fy = fy;
1997 orig_fw = fw;
1998 orig_fh = fh;
2000 if (res & LEFT)
2001 dw = orig_x - event.xmotion.x_root;
2002 else if (res & RIGHT)
2003 dw = event.xmotion.x_root - orig_x;
2004 if (res & UP)
2005 dh = orig_y - event.xmotion.y_root;
2006 else if (res & DOWN)
2007 dh = event.xmotion.y_root - orig_y;
2009 orig_x = event.xmotion.x_root;
2010 orig_y = event.xmotion.y_root;
2012 rw += dw;
2013 rh += dh;
2014 fw = rw;
2015 fh = rh - vert_border;
2016 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
2017 fh += vert_border;
2018 if (res & LEFT)
2019 fx = rx2 - fw + 1;
2020 else if (res & RIGHT)
2021 fx = rx1;
2022 if (res & UP)
2023 fy = ry2 - fh + 1;
2024 else if (res & DOWN)
2025 fy = ry1;
2026 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
2027 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
2028 int tx, ty;
2029 Window junkw;
2030 int flags;
2032 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
2033 orig_x, orig_y, &tx, &ty, &junkw);
2035 /* check if resizing through resizebar */
2036 if (is_resizebar)
2037 flags = RESIZEBAR;
2038 else
2039 flags = 0;
2041 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
2042 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
2043 flags |= HCONSTRAIN;
2045 res = getResizeDirection(wwin, tx, ty, orig_y - event.xmotion.y_root, flags);
2047 if (res == (UP | LEFT))
2048 XChangeActivePointerGrab(dpy, ButtonMotionMask
2049 | ButtonReleaseMask | ButtonPressMask,
2050 wPreferences.cursor[WCUR_TOPLEFTRESIZE], CurrentTime);
2051 else if (res == (UP | RIGHT))
2052 XChangeActivePointerGrab(dpy, ButtonMotionMask
2053 | ButtonReleaseMask | ButtonPressMask,
2054 wPreferences.cursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
2055 else if (res == (DOWN | LEFT))
2056 XChangeActivePointerGrab(dpy, ButtonMotionMask
2057 | ButtonReleaseMask | ButtonPressMask,
2058 wPreferences.cursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2059 else if (res == (DOWN | RIGHT))
2060 XChangeActivePointerGrab(dpy, ButtonMotionMask
2061 | ButtonReleaseMask | ButtonPressMask,
2062 wPreferences.cursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2063 else if (res == DOWN || res == UP)
2064 XChangeActivePointerGrab(dpy, ButtonMotionMask
2065 | ButtonReleaseMask | ButtonPressMask,
2066 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2067 else if (res & (DOWN | UP))
2068 XChangeActivePointerGrab(dpy, ButtonMotionMask
2069 | ButtonReleaseMask | ButtonPressMask,
2070 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2071 else if (res & (LEFT | RIGHT))
2072 XChangeActivePointerGrab(dpy, ButtonMotionMask
2073 | ButtonReleaseMask | ButtonPressMask,
2074 wPreferences.cursor[WCUR_HORIZONRESIZE], CurrentTime);
2076 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2078 XGrabServer(dpy);
2080 /* Draw the resize frame for the first time. */
2081 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2083 if (!opaqueResize)
2084 drawTransparentFrame(wwin, fx, fy, fw, fh);
2086 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2088 started = 1;
2090 if (started) {
2091 if (!opaqueResize)
2092 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2094 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2095 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2097 if (!opaqueResize)
2098 drawTransparentFrame(wwin, fx, fy, fw, fh);
2100 if (fh != orig_fh || fw != orig_fw) {
2101 if (wPreferences.size_display == WDIS_NEW)
2102 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2103 orig_fy + orig_fh, res);
2105 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2108 if (opaqueResize) {
2109 /* Fist clean the geometry line */
2110 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2111 /* Now, continue drawing */
2112 XUngrabServer(dpy);
2113 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2114 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2115 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2118 break;
2120 case ButtonPress:
2121 break;
2123 case ButtonRelease:
2124 if (event.xbutton.button != ev->xbutton.button)
2125 break;
2127 if (started) {
2128 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2130 if (!opaqueResize)
2131 drawTransparentFrame(wwin, fx, fy, fw, fh);
2133 XUngrabKeyboard(dpy, CurrentTime);
2134 WMUnmapWidget(scr->gview);
2135 XUngrabServer(dpy);
2137 if (fw != original_fw)
2138 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
2140 if (fh != original_fh)
2141 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2143 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2144 wWindowSynthConfigureNotify(wwin);
2146 return;
2148 default:
2149 WMHandleEvent(&event);
2153 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2154 wArrangeIcons(scr, True);
2157 #undef LEFT
2158 #undef RIGHT
2159 #undef HORIZONTAL
2160 #undef UP
2161 #undef DOWN
2162 #undef VERTICAL
2163 #undef HCONSTRAIN
2164 #undef RESIZEBAR
2166 void wUnselectWindows(WScreen * scr)
2168 WWindow *wwin;
2170 if (!scr->selected_windows)
2171 return;
2173 while (WMGetArrayItemCount(scr->selected_windows)) {
2174 wwin = WMGetFromArray(scr->selected_windows, 0);
2175 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2176 wIconSelect(wwin->icon);
2178 wSelectWindow(wwin, False);
2180 WMFreeArray(scr->selected_windows);
2181 scr->selected_windows = NULL;
2184 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2186 WWindow *tmpw;
2188 /* select the windows and put them in the selected window list */
2189 tmpw = scr->focused_window;
2190 while (tmpw != NULL) {
2191 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2192 if ((tmpw->frame->workspace == w_global.workspace.current || IS_OMNIPRESENT(tmpw))
2193 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2194 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2195 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2196 wSelectWindow(tmpw, True);
2199 tmpw = tmpw->prev;
2203 void wSelectWindows(WScreen * scr, XEvent * ev)
2205 XEvent event;
2206 Window root = scr->root_win;
2207 GC gc = scr->frame_gc;
2208 int xp = ev->xbutton.x_root;
2209 int yp = ev->xbutton.y_root;
2210 int w = 0, h = 0;
2211 int x = xp, y = yp;
2213 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2214 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2215 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2216 return;
2218 XGrabServer(dpy);
2220 wUnselectWindows(scr);
2222 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2223 while (1) {
2224 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2226 switch (event.type) {
2227 case MotionNotify:
2228 XDrawRectangle(dpy, root, gc, x, y, w, h);
2229 x = event.xmotion.x_root;
2230 if (x < xp) {
2231 w = xp - x;
2232 } else {
2233 w = x - xp;
2234 x = xp;
2236 y = event.xmotion.y_root;
2237 if (y < yp) {
2238 h = yp - y;
2239 } else {
2240 h = y - yp;
2241 y = yp;
2243 XDrawRectangle(dpy, root, gc, x, y, w, h);
2244 break;
2246 case ButtonPress:
2247 break;
2249 case ButtonRelease:
2250 if (event.xbutton.button != ev->xbutton.button)
2251 break;
2253 XDrawRectangle(dpy, root, gc, x, y, w, h);
2254 XUngrabServer(dpy);
2255 XUngrabPointer(dpy, CurrentTime);
2256 selectWindowsInside(scr, x, y, x + w, y + h);
2257 return;
2259 default:
2260 WMHandleEvent(&event);
2261 break;
2266 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2268 WScreen *scr = wwin->screen_ptr;
2269 Window root = scr->root_win;
2270 int x, y, h = 0;
2271 XEvent event;
2272 KeyCode shiftl, shiftr;
2273 Window junkw;
2274 int junk;
2276 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2277 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2278 *x_ret = 0;
2279 *y_ret = 0;
2280 return;
2282 if (HAS_TITLEBAR(wwin)) {
2283 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2284 TITLEBAR_EXTEND_SPACE) * 2;
2286 if (h > wPreferences.window_title_max_height)
2287 h = wPreferences.window_title_max_height;
2289 if (h < wPreferences.window_title_min_height)
2290 h = wPreferences.window_title_min_height;
2292 height += h;
2294 if (HAS_RESIZEBAR(wwin)) {
2295 height += RESIZEBAR_HEIGHT;
2297 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2298 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2299 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2301 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2303 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2304 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2305 while (1) {
2306 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2308 if (!checkMouseSamplingRate(&event))
2309 continue;
2311 switch (event.type) {
2312 case KeyPress:
2313 if ((event.xkey.keycode == shiftl)
2314 || (event.xkey.keycode == shiftr)) {
2315 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2316 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2317 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2319 break;
2321 case MotionNotify:
2322 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2324 x = event.xmotion.x_root;
2325 y = event.xmotion.y_root;
2327 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2328 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2330 showPosition(wwin, x - width / 2, y - h / 2);
2332 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2334 break;
2336 case ButtonPress:
2337 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2338 XSync(dpy, 0);
2339 *x_ret = x - width / 2;
2340 *y_ret = y - h / 2;
2341 XUngrabPointer(dpy, CurrentTime);
2342 XUngrabKeyboard(dpy, CurrentTime);
2343 /* get rid of the geometry window */
2344 WMUnmapWidget(scr->gview);
2345 return;
2347 default:
2348 WMHandleEvent(&event);
2349 break;