Fixed problem with kcalc not having a miniaturize button
[wmaker-crm.git] / src / moveres.c
blob9ee3423b4e5315432713765ec2ffc5da2c06ef1d
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998, 1999 Alfredo K. Kojima
5 *
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "wcore.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "actions.h"
41 #include "workspace.h"
43 #include "geomview.h"
46 #ifdef KWM_HINTS
47 #include "kwm.h"
48 #endif
50 /* How many different types of geometry/position
51 display thingies are there? */
52 #define NUM_DISPLAYS 5
54 #define LEFT 1
55 #define RIGHT 2
56 #define HORIZONTAL (LEFT|RIGHT)
57 #define UP 4
58 #define DOWN 8
59 #define VERTICAL (UP|DOWN)
61 /****** Global Variables ******/
62 extern Time LastTimestamp;
64 extern Cursor wCursor[WCUR_LAST];
66 extern WPreferences wPreferences;
68 extern Atom _XA_WM_PROTOCOLS;
73 *----------------------------------------------------------------------
74 * checkMouseSamplingRate-
75 * For lowering the mouse motion sampling rate for machines where
76 * it's too high (SGIs). If it returns False then the event should be
77 * ignored.
78 *----------------------------------------------------------------------
80 static Bool
81 checkMouseSamplingRate(XEvent *ev)
83 static Time previousMotion = 0;
85 if (ev->type == MotionNotify) {
86 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
87 return False;
88 } else {
89 previousMotion = ev->xmotion.time;
92 return True;
98 *----------------------------------------------------------------------
99 * moveGeometryDisplayCentered
101 * routine that moves the geometry/position window on scr so it is
102 * centered over the given coordinates (x,y). Also the window position
103 * is clamped so it stays on the screen at all times.
104 *----------------------------------------------------------------------
106 static void
107 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
109 unsigned int w = WMWidgetWidth(scr->gview);
110 unsigned int h = WMWidgetHeight(scr->gview);
112 x -= w / 2;
113 y -= h / 2;
115 if (x < 1)
116 x = 1;
117 else if (x > (scr->scr_width - w - 3))
118 x = scr->scr_width - w - 3;
120 if (y < 1)
121 y = 1;
122 else if (y > (scr->scr_height - h - 3))
123 y = scr->scr_height - h - 3;
125 WMMoveWidget(scr->gview, x, y);
129 static void
130 showPosition(WWindow *wwin, int x, int y)
132 WScreen *scr = wwin->screen_ptr;
134 if (wPreferences.move_display == WDIS_NEW) {
135 #if 0
136 int width = wwin->frame->core->width;
137 int height = wwin->frame->core->height;
139 GC lgc = scr->line_gc;
140 XSetForeground(dpy, lgc, scr->line_pixel);
141 sprintf(num, "%i", x);
143 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
144 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
145 y+height+2);
146 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
147 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
148 scr->scr_height);
149 #endif
150 } else {
151 #ifdef VIRTUAL_DESKTOP
152 WSetGeometryViewShownPosition(scr->gview,
153 x + scr->workspaces[scr->current_workspace]->view_x,
154 y + scr->workspaces[scr->current_workspace]->view_y);
155 #else
156 WSetGeometryViewShownPosition(scr->gview, x, y);
157 #endif
162 static void
163 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
165 WScreen *scr = wwin->screen_ptr;
167 wPreferences.move_display++;
168 wPreferences.move_display %= NUM_DISPLAYS;
170 if (wPreferences.move_display == WDIS_NEW
171 || wPreferences.move_display == WDIS_NONE) {
172 WMUnmapWidget(scr->gview);
173 } else {
174 if (wPreferences.move_display == WDIS_CENTER) {
175 moveGeometryDisplayCentered(scr,
176 scr->scr_width/2, scr->scr_height/2);
177 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
178 moveGeometryDisplayCentered(scr, 1, 1);
179 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
180 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
182 WMMapWidget(scr->gview);
187 static void
188 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
190 WScreen *scr = wwin->screen_ptr;
192 if (wPreferences.move_display == WDIS_NEW
193 || wPreferences.move_display == WDIS_NONE) {
194 return;
195 } else if (wPreferences.move_display == WDIS_CENTER) {
196 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
197 scr->scr_height / 2);
198 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
199 moveGeometryDisplayCentered(scr, 1, 1);
200 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
201 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
203 WMMapWidget(scr->gview);
204 WSetGeometryViewShownPosition(scr->gview, x, y);
208 static void
209 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
211 WScreen *scr = wwin->screen_ptr;
212 Window root = scr->root_win;
213 GC gc = scr->line_gc;
214 int ty, by, my, x, y, mx, s;
215 char num[16];
216 XSegment segment[4];
217 int fw, fh;
219 ty = y1 + wwin->frame->top_width;
220 by = y2 - wwin->frame->bottom_width;
221 fw = WMWidthOfString(scr->info_text_font, "8888", 4);
222 fh = WMFontHeight(scr->info_text_font);
224 if (wPreferences.size_display == WDIS_NEW) {
225 XSetForeground(dpy, gc, scr->line_pixel);
227 /* vertical geometry */
228 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
229 x = x2;
230 s = -15;
231 } else {
232 x = x1;
233 s = 15;
235 my = (ty + by) / 2;
237 /* top arrow */
238 /* end bar */
239 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
240 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
242 /* arrowhead */
243 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
244 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
246 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
247 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
249 /* line */
250 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
251 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
253 XDrawSegments(dpy, root, gc, segment, 4);
255 /* bottom arrow */
256 /* end bar */
257 segment[0].y1 = by;
258 segment[0].y2 = by;
260 /* arrowhead */
261 segment[1].y1 = by - 1;
262 segment[1].y2 = by - 7;
264 segment[2].y1 = by - 1;
265 segment[2].y2 = by - 7;
267 /* line */
268 segment[3].y1 = my + fh/2 + 2;
269 segment[3].y2 = by - 1;
271 XDrawSegments(dpy, root, gc, segment, 4);
273 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
274 wwin->normal_hints->height_inc);
275 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
277 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
279 /* Display the height. */
280 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
281 x - s + 3 - fw/2, my - fh/2 + 1, num, strlen(num));
282 XSetForeground(dpy, gc, scr->line_pixel);
283 /* horizontal geometry */
284 if (y1 < 15) {
285 y = y2;
286 s = -15;
287 } else {
288 y = y1;
289 s = 15;
291 mx = x1 + (x2 - x1)/2;
292 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
293 wwin->normal_hints->width_inc);
294 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
296 /* left arrow */
297 /* end bar */
298 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
299 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
301 /* arrowhead */
302 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
303 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
305 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
306 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
308 /* line */
309 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
310 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
312 XDrawSegments(dpy, root, gc, segment, 4);
314 /* right arrow */
315 /* end bar */
316 segment[0].x1 = x2 + 1;
317 segment[0].x2 = x2 + 1;
319 /* arrowhead */
320 segment[1].x1 = x2 - 6;
321 segment[1].x2 = x2;
323 segment[2].x1 = x2;
324 segment[2].x2 = x2 - 6;
326 /* line */
327 segment[3].x1 = mx + fw/2 + 2;
328 segment[3].x2 = x2;
330 XDrawSegments(dpy, root, gc, segment, 4);
332 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
334 /* Display the width. */
335 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
336 mx - fw/2 + 1, y - s - fh/2 + 1, num, strlen(num));
337 } else {
338 WSetGeometryViewShownSize(scr->gview,
339 (x2 - x1 - wwin->normal_hints->base_width)
340 / wwin->normal_hints->width_inc,
341 (by - ty - wwin->normal_hints->base_height)
342 / wwin->normal_hints->height_inc);
347 static void
348 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
350 WScreen *scr = wwin->screen_ptr;
352 wPreferences.size_display++;
353 wPreferences.size_display %= NUM_DISPLAYS;
355 if (wPreferences.size_display == WDIS_NEW
356 || wPreferences.size_display == WDIS_NONE) {
357 WMUnmapWidget(scr->gview);
358 } else {
359 if (wPreferences.size_display == WDIS_CENTER) {
360 moveGeometryDisplayCentered(scr,
361 scr->scr_width / 2, scr->scr_height / 2);
362 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
363 moveGeometryDisplayCentered(scr, 1, 1);
364 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
365 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
367 WMMapWidget(scr->gview);
368 showGeometry(wwin, x, y, x + w, y + h, dir);
373 static void
374 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
376 WScreen *scr = wwin->screen_ptr;
378 if (wPreferences.size_display == WDIS_NEW
379 || wPreferences.size_display == WDIS_NONE)
380 return;
382 if (wPreferences.size_display == WDIS_CENTER) {
383 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
384 scr->scr_height / 2);
385 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
386 moveGeometryDisplayCentered(scr, 1, 1);
387 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
388 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
390 WMMapWidget(scr->gview);
391 showGeometry(wwin, x, y, x + w, y + h, 0);
396 static void
397 doWindowMove(WWindow *wwin, WMArray *array, int dx, int dy)
399 WWindow *tmpw;
400 int x, y;
401 int scr_width = wwin->screen_ptr->scr_width;
402 int scr_height = wwin->screen_ptr->scr_height;
404 if (!array || !WMGetArrayItemCount(array)) {
405 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
406 } else {
407 WMArrayIterator iter;
409 WM_ITERATE_ARRAY(array, tmpw, iter) {
410 x = tmpw->frame_x + dx;
411 y = tmpw->frame_y + dy;
413 /* don't let windows become unreachable */
415 if (x + (int)tmpw->frame->core->width < 20)
416 x = 20 - (int)tmpw->frame->core->width;
417 else if (x + 20 > scr_width)
418 x = scr_width - 20;
420 if (y + (int)tmpw->frame->core->height < 20)
421 y = 20 - (int)tmpw->frame->core->height;
422 else if (y + 20 > scr_height)
423 y = scr_height - 20;
425 wWindowMove(tmpw, x, y);
431 static void
432 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
434 Window root = wwin->screen_ptr->root_win;
435 GC gc = wwin->screen_ptr->frame_gc;
436 int h = 0;
437 int bottom = 0;
439 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
440 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
442 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
443 /* Can't use wwin-frame->bottom_width because, in some cases
444 (e.g. interactive placement), frame does not point to anything. */
445 bottom = RESIZEBAR_HEIGHT - 1;
447 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
449 if (h > 0) {
450 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
452 if (bottom > 0) {
453 XDrawLine(dpy, root, gc, x + 1,
454 y + height - bottom,
455 x + width + 1,
456 y + height - bottom);
461 static void
462 drawFrames(WWindow *wwin, WMArray *array, int dx, int dy)
464 WWindow *tmpw;
465 int scr_width = wwin->screen_ptr->scr_width;
466 int scr_height = wwin->screen_ptr->scr_height;
467 int x, y;
469 if (!array) {
471 x = wwin->frame_x + dx;
472 y = wwin->frame_y + dy;
474 drawTransparentFrame(wwin, x, y,
475 wwin->frame->core->width,
476 wwin->frame->core->height);
478 } else {
479 WMArrayIterator iter;
481 WM_ITERATE_ARRAY(array, tmpw, iter) {
482 x = tmpw->frame_x + dx;
483 y = tmpw->frame_y + dy;
485 /* don't let windows become unreachable */
487 if (x + (int)tmpw->frame->core->width < 20)
488 x = 20 - (int)tmpw->frame->core->width;
489 else if (x + 20 > scr_width)
490 x = scr_width - 20;
492 if (y + (int)tmpw->frame->core->height < 20)
493 y = 20 - (int)tmpw->frame->core->height;
494 else if (y + 20 > scr_height)
495 y = scr_height - 20;
497 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
498 tmpw->frame->core->height);
505 static void
506 flushMotion()
508 XEvent ev;
510 XSync(dpy, False);
511 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
515 static void
516 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
517 int new_workspace, int rewind)
519 /* do not let window be unmapped */
520 if (opaque_move) {
521 wwin->flags.changing_workspace = 1;
522 wWindowChangeWorkspace(wwin, new_workspace);
524 /* go to new workspace */
525 wWorkspaceChange(scr, new_workspace);
527 wwin->flags.changing_workspace = 0;
529 if (rewind)
530 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
531 else
532 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
534 flushMotion();
536 if (!opaque_move) {
537 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
538 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
539 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
546 typedef struct {
547 /* arrays of WWindows sorted by the respective border position */
548 WWindow **topList; /* top border */
549 WWindow **leftList; /* left border */
550 WWindow **rightList; /* right border */
551 WWindow **bottomList; /* bottom border */
552 int count;
554 /* index of window in the above lists indicating the relative position
555 * of the window with the others */
556 int topIndex;
557 int leftIndex;
558 int rightIndex;
559 int bottomIndex;
561 int rubCount; /* for workspace switching */
563 int winWidth, winHeight; /* width/height of the window */
564 int realX, realY; /* actual position of the window */
565 int calcX, calcY; /* calculated position of window */
566 int omouseX, omouseY; /* old mouse position */
567 int mouseX, mouseY; /* last known position of the pointer */
568 } MoveData;
570 #define WTOP(w) (w)->frame_y
571 #define WLEFT(w) (w)->frame_x
572 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
573 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
575 static int
576 compareWTop(const void *a, const void *b)
578 WWindow *wwin1 = *(WWindow**)a;
579 WWindow *wwin2 = *(WWindow**)b;
581 if (WTOP(wwin1) > WTOP(wwin2))
582 return -1;
583 else if (WTOP(wwin1) < WTOP(wwin2))
584 return 1;
585 else
586 return 0;
590 static int
591 compareWLeft(const void *a, const void *b)
593 WWindow *wwin1 = *(WWindow**)a;
594 WWindow *wwin2 = *(WWindow**)b;
596 if (WLEFT(wwin1) > WLEFT(wwin2))
597 return -1;
598 else if (WLEFT(wwin1) < WLEFT(wwin2))
599 return 1;
600 else
601 return 0;
605 static int
606 compareWRight(const void *a, const void *b)
608 WWindow *wwin1 = *(WWindow**)a;
609 WWindow *wwin2 = *(WWindow**)b;
611 if (WRIGHT(wwin1) < WRIGHT(wwin2))
612 return -1;
613 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
614 return 1;
615 else
616 return 0;
621 static int
622 compareWBottom(const void *a, const void *b)
624 WWindow *wwin1 = *(WWindow**)a;
625 WWindow *wwin2 = *(WWindow**)b;
627 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
628 return -1;
629 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
630 return 1;
631 else
632 return 0;
636 static void
637 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
639 int i;
640 int newX2 = newX + data->winWidth;
641 int newY2 = newY + data->winHeight;
642 Bool ok = False;
644 if (newX < data->realX) {
645 if (data->rightIndex > 0
646 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
647 ok = True;
648 } else if (data->leftIndex <= data->count-1
649 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
650 ok = True;
652 } else if (newX > data->realX) {
653 if (data->leftIndex > 0
654 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
655 ok = True;
656 } else if (data->rightIndex <= data->count-1
657 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
658 ok = True;
662 if (!ok) {
663 if (newY < data->realY) {
664 if (data->bottomIndex > 0
665 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
666 ok = True;
667 } else if (data->topIndex <= data->count-1
668 && newY2 <= WTOP(data->topList[data->topIndex])) {
669 ok = True;
671 } else if (newY > data->realY) {
672 if (data->topIndex > 0
673 && newY2 > WTOP(data->topList[data->topIndex-1])) {
674 ok = True;
675 } else if (data->bottomIndex <= data->count-1
676 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
677 ok = True;
682 if (!ok)
683 return;
685 /* TODO: optimize this */
686 if (data->realY < WBOTTOM(data->bottomList[0])) {
687 data->bottomIndex = 0;
689 if (data->realX < WRIGHT(data->rightList[0])) {
690 data->rightIndex = 0;
692 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
693 data->leftIndex = 0;
695 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
696 data->topIndex = 0;
698 for (i = 0; i < data->count; i++) {
699 if (data->realY > WBOTTOM(data->bottomList[i])) {
700 data->bottomIndex = i + 1;
702 if (data->realX > WRIGHT(data->rightList[i])) {
703 data->rightIndex = i + 1;
705 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
706 data->leftIndex = i + 1;
708 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
709 data->topIndex = i + 1;
715 static void
716 freeMoveData(MoveData *data)
718 if (data->topList)
719 wfree(data->topList);
720 if (data->leftList)
721 wfree(data->leftList);
722 if (data->rightList)
723 wfree(data->rightList);
724 if (data->bottomList)
725 wfree(data->bottomList);
729 static void
730 updateMoveData(WWindow *wwin, MoveData *data)
732 WScreen *scr = wwin->screen_ptr;
733 WWindow *tmp;
734 int i;
736 data->count = 0;
737 tmp = scr->focused_window;
738 while (tmp) {
739 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
740 && !tmp->flags.miniaturized
741 && !tmp->flags.hidden
742 && !tmp->flags.obscured
743 && !WFLAGP(tmp, sunken)) {
744 data->topList[data->count] = tmp;
745 data->leftList[data->count] = tmp;
746 data->rightList[data->count] = tmp;
747 data->bottomList[data->count] = tmp;
748 data->count++;
750 tmp = tmp->prev;
753 if (data->count == 0) {
754 data->topIndex = 0;
755 data->leftIndex = 0;
756 data->rightIndex = 0;
757 data->bottomIndex = 0;
758 return;
762 * order from closest to the border of the screen to farthest
764 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
765 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
766 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
767 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
769 /* figure the position of the window relative to the others */
771 data->topIndex = -1;
772 data->leftIndex = -1;
773 data->rightIndex = -1;
774 data->bottomIndex = -1;
776 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
777 data->bottomIndex = 0;
779 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
780 data->rightIndex = 0;
782 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
783 data->leftIndex = 0;
785 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
786 data->topIndex = 0;
788 for (i = 0; i < data->count; i++) {
789 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
790 data->bottomIndex = i + 1;
792 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
793 data->rightIndex = i + 1;
795 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
796 data->leftIndex = i + 1;
798 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
799 data->topIndex = i + 1;
805 static void
806 initMoveData(WWindow *wwin, MoveData *data)
808 int i;
809 WWindow *tmp;
811 memset(data, 0, sizeof(MoveData));
813 for (i = 0, tmp = wwin->screen_ptr->focused_window;
814 tmp != NULL;
815 tmp = tmp->prev, i++);
817 if (i > 1) {
818 data->topList = wmalloc(sizeof(WWindow*) * i);
819 data->leftList = wmalloc(sizeof(WWindow*) * i);
820 data->rightList = wmalloc(sizeof(WWindow*) * i);
821 data->bottomList = wmalloc(sizeof(WWindow*) * i);
823 updateMoveData(wwin, data);
826 data->realX = wwin->frame_x;
827 data->realY = wwin->frame_y;
828 data->calcX = wwin->frame_x;
829 data->calcY = wwin->frame_y;
831 data->winWidth = wwin->frame->core->width + 2;
832 data->winHeight = wwin->frame->core->height + 2;
836 static Bool
837 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
839 WScreen *scr = wwin->screen_ptr;
840 Bool changed = False;
842 if (data->mouseX <= 1) {
843 if (scr->current_workspace > 0) {
845 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
846 True);
847 changed = True;
848 data->rubCount = 0;
850 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
852 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
853 True);
854 changed = True;
855 data->rubCount = 0;
857 } else if (data->mouseX >= scr->scr_width - 2) {
859 if (scr->current_workspace == scr->workspace_count - 1) {
861 if (wPreferences.ws_cycle
862 || scr->workspace_count == MAX_WORKSPACES) {
864 crossWorkspace(scr, wwin, opaqueMove, 0, False);
865 changed = True;
866 data->rubCount = 0;
868 /* if user insists on trying to go to next workspace even when
869 * it's already the last, create a new one */
870 else if (data->omouseX == data->mouseX
871 && wPreferences.ws_advance) {
873 /* detect user "rubbing" the window against the edge */
874 if (data->rubCount > 0
875 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
877 data->rubCount = -(data->rubCount + 1);
879 } else if (data->rubCount <= 0
880 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
882 data->rubCount = -data->rubCount + 1;
885 /* create a new workspace */
886 if (abs(data->rubCount) > 2) {
887 /* go to next workspace */
888 wWorkspaceNew(scr);
890 crossWorkspace(scr, wwin, opaqueMove,
891 scr->current_workspace+1, False);
892 changed = True;
893 data->rubCount = 0;
895 } else if (scr->current_workspace < scr->workspace_count) {
897 /* go to next workspace */
898 crossWorkspace(scr, wwin, opaqueMove,
899 scr->current_workspace+1, False);
900 changed = True;
901 data->rubCount = 0;
903 } else {
904 data->rubCount = 0;
907 return changed;
911 static void
912 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
913 Bool opaqueMove, int newMouseX, int newMouseY)
915 WScreen *scr = wwin->screen_ptr;
916 int dx, dy; /* how much mouse moved */
917 int winL, winR, winT, winB; /* requested new window position */
918 int newX, newY; /* actual new window position */
919 Bool hresist, vresist;
920 Bool updateIndex;
921 Bool attract;
923 hresist = False;
924 vresist = False;
926 updateIndex = False;
928 /* check the direction of the movement */
929 dx = newMouseX - data->mouseX;
930 dy = newMouseY - data->mouseY;
932 data->omouseX = data->mouseX;
933 data->omouseY = data->mouseY;
934 data->mouseX = newMouseX;
935 data->mouseY = newMouseY;
937 winL = data->calcX + dx;
938 winR = data->calcX + data->winWidth + dx;
939 winT = data->calcY + dy;
940 winB = data->calcY + data->winHeight + dy;
942 newX = data->realX;
943 newY = data->realY;
945 if (doResistance) {
946 int l_edge, r_edge;
947 int edge_l, edge_r;
948 int t_edge, b_edge;
949 int edge_t, edge_b;
950 int resist;
952 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
953 attract = wPreferences.attract;
954 /* horizontal movement: check horizontal edge resistances */
955 if (dx || dy) {
956 int i;
957 /* window is the leftmost window: check against screen edge */
958 l_edge = scr->totalUsableArea.x1;
959 r_edge = scr->totalUsableArea.x2 + resist;
960 edge_l = scr->totalUsableArea.x1 - resist;
961 edge_r = scr->totalUsableArea.x2;
963 /* 1 */
964 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
965 WWindow *looprw;
967 for (i = data->rightIndex - 1; i >= 0; i--) {
968 looprw = data->rightList[i];
969 if (!(data->realY > WBOTTOM(looprw)
970 || (data->realY + data->winHeight) < WTOP(looprw))) {
971 if (attract
972 || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
973 l_edge = WRIGHT(looprw) + 1;
974 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
976 break;
980 if (attract) {
981 for (i = data->rightIndex; i < data->count; i++) {
982 looprw = data->rightList[i];
983 if(!(data->realY > WBOTTOM(looprw)
984 || (data->realY + data->winHeight) < WTOP(looprw))) {
985 r_edge = WRIGHT(looprw) + 1;
986 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
987 break;
993 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
994 WWindow *looprw;
996 for (i = data->leftIndex - 1; i >= 0; i--) {
997 looprw = data->leftList[i];
998 if (!(data->realY > WBOTTOM(looprw)
999 || (data->realY + data->winHeight) < WTOP(looprw))) {
1000 if (attract
1001 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1)) && dx > 0)) {
1002 edge_r = WLEFT(looprw);
1003 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1005 break;
1009 if (attract)
1010 for (i = data->leftIndex; i < data->count; i++) {
1011 looprw = data->leftList[i];
1012 if(!(data->realY > WBOTTOM(looprw)
1013 || (data->realY + data->winHeight) < WTOP(looprw))) {
1014 edge_l = WLEFT(looprw);
1015 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1016 break;
1022 printf("%d %d\n",winL,winR);
1023 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1026 if ((winL - l_edge) < (r_edge - winL)) {
1027 if (resist > 0) {
1028 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1029 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1030 newX = l_edge;
1031 hresist = True;
1034 } else {
1035 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1036 newX = r_edge;
1037 hresist = True;
1041 if ((winR - edge_l) < (edge_r - winR)) {
1042 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1043 newX = edge_l - data->winWidth;
1044 hresist = True;
1046 } else {
1047 if (resist > 0) {
1048 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1049 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1050 newX = edge_r - data->winWidth;
1051 hresist = True;
1056 /* VeRT */
1057 t_edge = scr->totalUsableArea.y1;
1058 b_edge = scr->totalUsableArea.y2 + resist;
1059 edge_t = scr->totalUsableArea.y1 - resist;
1060 edge_b = scr->totalUsableArea.y2;
1062 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1063 WWindow *looprw;
1065 for (i = data->bottomIndex - 1; i >= 0; i--) {
1066 looprw = data->bottomList[i];
1067 if (!(data->realX > WRIGHT(looprw)
1068 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1069 if (attract
1070 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1071 t_edge = WBOTTOM(looprw) + 1;
1072 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1074 break;
1078 if (attract) {
1079 for (i = data->bottomIndex; i < data->count; i++) {
1080 looprw = data->bottomList[i];
1081 if(!(data->realX > WRIGHT(looprw)
1082 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1083 b_edge = WBOTTOM(looprw) + 1;
1084 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1085 break;
1091 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1092 WWindow *looprw;
1094 for (i = data->topIndex - 1; i >= 0; i--) {
1095 looprw = data->topList[i];
1096 if (!(data->realX > WRIGHT(looprw)
1097 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1098 if (attract
1099 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1100 edge_b = WTOP(looprw);
1101 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1103 break;
1107 if (attract)
1108 for (i = data->topIndex; i < data->count; i++) {
1109 looprw = data->topList[i];
1110 if(!(data->realX > WRIGHT(looprw)
1111 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1112 edge_t = WTOP(looprw);
1113 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1114 break;
1119 if ((winT - t_edge) < (b_edge - winT)) {
1120 if (resist > 0) {
1121 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1122 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1123 newY = t_edge;
1124 vresist = True;
1128 else {
1129 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1130 newY = b_edge;
1131 vresist = True;
1135 if ((winB - edge_t) < (edge_b - winB)) {
1136 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1137 newY = edge_t - data->winHeight;
1138 vresist = True;
1141 else {
1142 if (resist > 0) {
1143 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1144 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1145 newY = edge_b - data->winHeight;
1146 vresist = True;
1151 /* END VeRT */
1155 /* update window position */
1156 data->calcX += dx;
1157 data->calcY += dy;
1159 if (((dx > 0 && data->calcX - data->realX > 0)
1160 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1161 newX = data->calcX;
1163 if (((dy > 0 && data->calcY - data->realY > 0)
1164 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1165 newY = data->calcY;
1167 if (data->realX != newX || data->realY != newY) {
1169 if (wPreferences.move_display == WDIS_NEW
1170 && !scr->selected_windows) {
1171 showPosition(wwin, data->realX, data->realY);
1173 if (opaqueMove) {
1174 doWindowMove(wwin, scr->selected_windows,
1175 newX - wwin->frame_x,
1176 newY - wwin->frame_y);
1177 } else {
1178 /* erase frames */
1179 drawFrames(wwin, scr->selected_windows,
1180 data->realX - wwin->frame_x,
1181 data->realY - wwin->frame_y);
1184 if (!scr->selected_windows
1185 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1187 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1188 newY + data->winHeight/2);
1191 if (!opaqueMove) {
1192 /* draw frames */
1193 drawFrames(wwin, scr->selected_windows,
1194 newX - wwin->frame_x,
1195 newY - wwin->frame_y);
1198 if (!scr->selected_windows) {
1199 showPosition(wwin, newX, newY);
1204 /* recalc relative window position */
1205 if (doResistance && (data->realX != newX || data->realY != newY)) {
1206 updateResistance(wwin, data, newX, newY);
1209 data->realX = newX;
1210 data->realY = newY;
1214 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1217 wKeyboardMoveResizeWindow(WWindow *wwin)
1219 WScreen *scr = wwin->screen_ptr;
1220 Window root = scr->root_win;
1221 XEvent event;
1222 int w = wwin->frame->core->width;
1223 int h = wwin->frame->core->height;
1224 int scr_width = wwin->screen_ptr->scr_width;
1225 int scr_height = wwin->screen_ptr->scr_height;
1226 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1227 int src_x = wwin->frame_x;
1228 int src_y = wwin->frame_y;
1229 int done,off_x,off_y,ww,wh;
1230 int kspeed = _KS;
1231 Time lastTime = 0;
1232 KeySym keysym=NoSymbol;
1233 int moment=0;
1234 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1236 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1237 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1238 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1239 ctrlmode=done=off_x=off_y=0;
1241 XSync(dpy, False);
1242 wusleep(10000);
1243 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1245 if (!wwin->flags.selected) {
1246 wUnselectWindows(scr);
1248 XGrabServer(dpy);
1249 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1250 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1251 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1253 if (wwin->flags.shaded || scr->selected_windows) {
1254 if(scr->selected_windows)
1255 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1256 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1257 if(!scr->selected_windows)
1258 mapPositionDisplay(wwin, src_x, src_y, w, h);
1259 } else {
1260 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1262 ww=w;
1263 wh=h;
1264 while(1) {
1266 looper.ox=off_x;
1267 looper.oy=off_y;
1269 do {
1270 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1271 | ButtonPressMask | ExposureMask, &event);
1272 if (event.type == Expose) {
1273 WMHandleEvent(&event);
1275 } while (event.type == Expose);
1278 if (wwin->flags.shaded || scr->selected_windows) {
1279 if(scr->selected_windows)
1280 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1281 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1282 /*** I HATE EDGE RESISTANCE - ]d ***/
1284 else {
1285 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1288 if(ctrlmode)
1289 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1291 XUngrabServer(dpy);
1292 XSync(dpy, False);
1294 switch (event.type) {
1295 case KeyPress:
1296 /* accelerate */
1297 if (event.xkey.time - lastTime > 50) {
1298 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1299 } else {
1300 if (kspeed < 20) {
1301 kspeed++;
1304 if (kspeed < _KS) kspeed = _KS;
1305 lastTime = event.xkey.time;
1307 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1308 ctrlmode=1;
1309 wUnselectWindows(scr);
1311 else {
1312 ctrlmode=0;
1314 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1315 if (ctrlmode)
1316 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1317 else
1318 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1320 else {
1322 keysym = XLookupKeysym(&event.xkey, 0);
1323 switch (keysym) {
1324 case XK_Return:
1325 done=2;
1326 break;
1327 case XK_Escape:
1328 done=1;
1329 break;
1330 case XK_Up:
1331 #ifdef XK_KP_Up
1332 case XK_KP_Up:
1333 #endif
1334 case XK_k:
1335 if (ctrlmode){
1336 if (moment != UP)
1337 h = wh;
1338 h-=kspeed;
1339 moment = UP;
1340 if (h < 1) h = 1;
1342 else off_y-=kspeed;
1343 break;
1344 case XK_Down:
1345 #ifdef XK_KP_Down
1346 case XK_KP_Down:
1347 #endif
1348 case XK_j:
1349 if (ctrlmode){
1350 if (moment != DOWN)
1351 h = wh;
1352 h+=kspeed;
1353 moment = DOWN;
1355 else off_y+=kspeed;
1356 break;
1357 case XK_Left:
1358 #ifdef XK_KP_Left
1359 case XK_KP_Left:
1360 #endif
1361 case XK_h:
1362 if (ctrlmode) {
1363 if (moment != LEFT)
1364 w = ww;
1365 w-=kspeed;
1366 if (w < 1) w = 1;
1367 moment = LEFT;
1369 else 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;
1382 else off_x+=kspeed;
1383 break;
1386 ww=w;wh=h;
1387 wh-=vert_border;
1388 wWindowConstrainSize(wwin, &ww, &wh);
1389 wh+=vert_border;
1391 if (wPreferences.ws_cycle){
1392 if (src_x + off_x + ww < 20){
1393 if(!scr->current_workspace) {
1394 wWorkspaceChange(scr, scr->workspace_count-1);
1396 else wWorkspaceChange(scr, scr->current_workspace-1);
1397 off_x += scr_width;
1399 else if (src_x + off_x + 20 > scr_width){
1400 if(scr->current_workspace == scr->workspace_count-1) {
1401 wWorkspaceChange(scr, 0);
1403 else wWorkspaceChange(scr, scr->current_workspace+1);
1404 off_x -= scr_width;
1407 else {
1408 if (src_x + off_x + ww < 20)
1409 off_x = 20 - ww - src_x;
1410 else if (src_x + off_x + 20 > scr_width)
1411 off_x = scr_width - 20 - src_x;
1414 if (src_y + off_y + wh < 20) {
1415 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 (wwin->flags.shaded || scr->selected_windows) {
1454 if (scr->selected_windows)
1455 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1456 else
1457 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1458 } else {
1459 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1463 if (ctrlmode) {
1464 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1465 } else if(!scr->selected_windows)
1466 showPosition(wwin, src_x+off_x, src_y+off_y);
1467 /**/
1469 if (done) {
1470 scr->keymove_tick=0;
1472 WMDeleteTimerWithClientData(&looper);
1474 if (wwin->flags.shaded || scr->selected_windows) {
1475 if(scr->selected_windows)
1476 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1477 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1479 else {
1480 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1483 if (ctrlmode) {
1484 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1485 WMUnmapWidget(scr->gview);
1486 } else
1487 WMUnmapWidget(scr->gview);
1489 XUngrabKeyboard(dpy, CurrentTime);
1490 XUngrabPointer(dpy, CurrentTime);
1491 XUngrabServer(dpy);
1493 if(done==2) {
1494 if (wwin->flags.shaded || scr->selected_windows) {
1495 if (!scr->selected_windows) {
1496 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1497 wWindowSynthConfigureNotify(wwin);
1498 } else {
1499 WMArrayIterator iter;
1500 WWindow *foo;
1502 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1504 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1505 wWindowSynthConfigureNotify(foo);
1508 } else {
1509 if (wwin->client.width != ww)
1510 wwin->flags.user_changed_width = 1;
1512 if (wwin->client.height != wh - vert_border)
1513 wwin->flags.user_changed_height = 1;
1515 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1516 ww, wh - vert_border);
1517 wWindowSynthConfigureNotify(wwin);
1519 wWindowChangeWorkspace(wwin, scr->current_workspace);
1520 wSetFocusTo(scr, wwin);
1522 return 1;
1529 *----------------------------------------------------------------------
1530 * wMouseMoveWindow--
1531 * Move the named window and the other selected ones (if any),
1532 * interactively. Also shows the position of the window, if only one
1533 * window is being moved.
1534 * If the window is not on the selected window list, the selected
1535 * windows are deselected.
1536 * If shift is pressed during the operation, the position display
1537 * is changed to another type.
1539 * Returns:
1540 * True if the window was moved, False otherwise.
1542 * Side effects:
1543 * The window(s) position is changed, and the client(s) are
1544 * notified about that.
1545 * The position display configuration may be changed.
1546 *----------------------------------------------------------------------
1549 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1551 WScreen *scr = wwin->screen_ptr;
1552 XEvent event;
1553 Window root = scr->root_win;
1554 KeyCode shiftl, shiftr;
1555 Bool done = False;
1556 int started = 0;
1557 int warped = 0;
1558 /* This needs not to change while moving, else bad things can happen */
1559 int opaqueMove = wPreferences.opaque_move;
1560 MoveData moveData;
1561 #ifdef GHOST_WINDOW_MOVE
1562 RImage *rimg;
1564 rimg = InitGhostWindowMove(scr);
1565 #endif
1568 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1569 XSetWindowAttributes attr;
1571 attr.save_under = True;
1572 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1573 CWSaveUnder, &attr);
1577 initMoveData(wwin, &moveData);
1579 moveData.mouseX = ev->xmotion.x_root;
1580 moveData.mouseY = ev->xmotion.y_root;
1582 if (!wwin->flags.selected) {
1583 /* this window is not selected, unselect others and move only wwin */
1584 wUnselectWindows(scr);
1586 #ifdef DEBUG
1587 puts("Moving window");
1588 #endif
1589 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1590 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1591 while (!done) {
1592 if (warped) {
1593 int junk;
1594 Window junkw;
1596 /* XWarpPointer() doesn't seem to generate Motion events, so
1597 we've got to simulate them */
1598 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1599 &event.xmotion.y_root, &junk, &junk,
1600 (unsigned *) &junk);
1601 } else {
1602 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1603 | PointerMotionHintMask
1604 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1605 &event);
1607 if (event.type == MotionNotify) {
1608 /* compress MotionNotify events */
1609 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1610 if (!checkMouseSamplingRate(&event))
1611 continue;
1614 switch (event.type) {
1615 case KeyPress:
1616 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1617 && started && !scr->selected_windows) {
1619 if (!opaqueMove) {
1620 drawFrames(wwin, scr->selected_windows,
1621 moveData.realX - wwin->frame_x,
1622 moveData.realY - wwin->frame_y);
1625 if (wPreferences.move_display == WDIS_NEW
1626 && !scr->selected_windows) {
1627 showPosition(wwin, moveData.realX, moveData.realY);
1628 XUngrabServer(dpy);
1630 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1631 moveData.winWidth, moveData.winHeight);
1633 if (wPreferences.move_display == WDIS_NEW
1634 && !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,
1642 moveData.realY - wwin->frame_y);
1644 /*} else {
1645 WMHandleEvent(&event); this causes problems needs fixing */
1647 break;
1649 case MotionNotify:
1650 if (started) {
1651 updateWindowPosition(wwin, &moveData,
1652 scr->selected_windows == NULL
1653 && wPreferences.edge_resistance > 0,
1654 opaqueMove,
1655 event.xmotion.x_root,
1656 event.xmotion.y_root);
1658 if (!warped && !wPreferences.no_autowrap) {
1659 int oldWorkspace = scr->current_workspace;
1661 if (wPreferences.move_display == WDIS_NEW
1662 && !scr->selected_windows) {
1663 showPosition(wwin, moveData.realX, moveData.realY);
1664 XUngrabServer(dpy);
1666 if (!opaqueMove) {
1667 drawFrames(wwin, scr->selected_windows,
1668 moveData.realX - wwin->frame_x,
1669 moveData.realY - wwin->frame_y);
1671 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1672 if (scr->current_workspace != oldWorkspace
1673 && wPreferences.edge_resistance > 0
1674 && scr->selected_windows == NULL)
1675 updateMoveData(wwin, &moveData);
1676 warped = 1;
1678 if (!opaqueMove) {
1679 drawFrames(wwin, scr->selected_windows,
1680 moveData.realX - wwin->frame_x,
1681 moveData.realY - wwin->frame_y);
1683 if (wPreferences.move_display == WDIS_NEW
1684 && !scr->selected_windows) {
1685 XSync(dpy, False);
1686 showPosition(wwin, moveData.realX, moveData.realY);
1687 XGrabServer(dpy);
1689 } else {
1690 warped = 0;
1692 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1693 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1695 XChangeActivePointerGrab(dpy, ButtonMotionMask
1696 | ButtonReleaseMask | ButtonPressMask,
1697 wCursor[WCUR_MOVE], CurrentTime);
1698 started = 1;
1699 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1700 CurrentTime);
1702 if (!scr->selected_windows)
1703 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1704 moveData.winWidth, moveData.winHeight);
1706 if (started && !opaqueMove)
1707 drawFrames(wwin, scr->selected_windows, 0, 0);
1709 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1710 && !scr->selected_windows)) {
1711 XGrabServer(dpy);
1712 if (wPreferences.move_display==WDIS_NEW)
1713 showPosition(wwin, moveData.realX, moveData.realY);
1716 break;
1718 case ButtonPress:
1719 break;
1721 case ButtonRelease:
1722 if (event.xbutton.button != ev->xbutton.button)
1723 break;
1725 if (started) {
1726 XEvent e;
1727 if (!opaqueMove) {
1728 drawFrames(wwin, scr->selected_windows,
1729 moveData.realX - wwin->frame_x,
1730 moveData.realY - wwin->frame_y);
1731 XSync(dpy, 0);
1732 doWindowMove(wwin, scr->selected_windows,
1733 moveData.realX - wwin->frame_x,
1734 moveData.realY - wwin->frame_y);
1736 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1737 wWindowSynthConfigureNotify(wwin);
1738 #endif
1739 XUngrabKeyboard(dpy, CurrentTime);
1740 XUngrabServer(dpy);
1741 if (!opaqueMove) {
1742 wWindowChangeWorkspace(wwin, scr->current_workspace);
1743 wSetFocusTo(scr, wwin);
1745 if (wPreferences.move_display == WDIS_NEW)
1746 showPosition(wwin, moveData.realX, moveData.realY);
1748 /* discard all enter/leave events that happened until
1749 * the time the button was released */
1750 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1751 if (e.xcrossing.time > event.xbutton.time) {
1752 XPutBackEvent(dpy, &e);
1753 break;
1756 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1757 if (e.xcrossing.time > event.xbutton.time) {
1758 XPutBackEvent(dpy, &e);
1759 break;
1763 if (!scr->selected_windows) {
1764 /* get rid of the geometry window */
1765 WMUnmapWidget(scr->gview);
1768 #ifdef DEBUG
1769 puts("End move window");
1770 #endif
1771 done = True;
1772 break;
1774 default:
1775 if (started && !opaqueMove) {
1776 drawFrames(wwin, scr->selected_windows,
1777 moveData.realX - wwin->frame_x,
1778 moveData.realY - wwin->frame_y);
1779 XUngrabServer(dpy);
1780 WMHandleEvent(&event);
1781 XSync(dpy, False);
1782 XGrabServer(dpy);
1783 drawFrames(wwin, scr->selected_windows,
1784 moveData.realX - wwin->frame_x,
1785 moveData.realY - wwin->frame_y);
1786 } else {
1787 WMHandleEvent(&event);
1789 break;
1793 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1794 XSetWindowAttributes attr;
1797 attr.save_under = False;
1798 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1799 CWSaveUnder, &attr);
1803 freeMoveData(&moveData);
1805 return started;
1809 #define RESIZEBAR 1
1810 #define HCONSTRAIN 2
1812 static int
1813 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1814 int flags)
1816 int w = wwin->frame->core->width - 1;
1817 int cw = wwin->frame->resizebar_corner_width;
1818 int dir;
1820 /* if not resizing through the resizebar */
1821 if (!(flags & RESIZEBAR)) {
1822 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1823 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1824 if (abs(dx) < 2 || abs(dy) < 2) {
1825 if (abs(dy) > abs(dx))
1826 xdir = 0;
1827 else
1828 ydir = 0;
1830 return (xdir | ydir);
1833 /* window is too narrow. allow diagonal resize */
1834 if (cw * 2 >= w) {
1835 int ydir;
1837 if (flags & HCONSTRAIN)
1838 ydir = 0;
1839 else
1840 ydir = DOWN;
1841 if (x < cw)
1842 return (LEFT | ydir);
1843 else
1844 return (RIGHT | ydir);
1846 /* vertical resize */
1847 if ((x > cw) && (x < w - cw))
1848 return DOWN;
1850 if (x < cw)
1851 dir = LEFT;
1852 else
1853 dir = RIGHT;
1855 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1856 dir |= DOWN;
1858 return dir;
1862 void
1863 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1865 XEvent event;
1866 WScreen *scr = wwin->screen_ptr;
1867 Window root = scr->root_win;
1868 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1869 int fw = wwin->frame->core->width;
1870 int fh = wwin->frame->core->height;
1871 int fx = wwin->frame_x;
1872 int fy = wwin->frame_y;
1873 int is_resizebar = (wwin->frame->resizebar
1874 && ev->xany.window==wwin->frame->resizebar->window);
1875 int orig_x, orig_y;
1876 int started;
1877 int dw, dh;
1878 int rw = fw, rh = fh;
1879 int rx1, ry1, rx2, ry2;
1880 int res = 0;
1881 KeyCode shiftl, shiftr;
1882 int h = 0;
1883 int orig_fx = fx;
1884 int orig_fy = fy;
1885 int orig_fw = fw;
1886 int orig_fh = fh;
1888 if (wwin->flags.shaded) {
1889 wwarning("internal error: tryein");
1890 return;
1892 orig_x = ev->xbutton.x_root;
1893 orig_y = ev->xbutton.y_root;
1895 started = 0;
1896 #ifdef DEBUG
1897 puts("Resizing window");
1898 #endif
1900 wUnselectWindows(scr);
1901 rx1 = fx;
1902 rx2 = fx + fw - 1;
1903 ry1 = fy;
1904 ry2 = fy + fh - 1;
1905 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1906 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1907 if (!WFLAGP(wwin, no_titlebar))
1908 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
1909 else
1910 h = 0;
1911 while (1) {
1912 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1913 | ButtonReleaseMask | PointerMotionHintMask
1914 | ButtonPressMask | ExposureMask, &event);
1915 if (!checkMouseSamplingRate(&event))
1916 continue;
1918 switch (event.type) {
1919 case KeyPress:
1920 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1921 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1922 && started) {
1923 drawTransparentFrame(wwin, fx, fy, fw, fh);
1924 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1925 drawTransparentFrame(wwin, fx, fy, fw, fh);
1927 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1928 break;
1930 case MotionNotify:
1931 if (started) {
1932 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1934 dw = 0;
1935 dh = 0;
1937 orig_fx = fx;
1938 orig_fy = fy;
1939 orig_fw = fw;
1940 orig_fh = fh;
1942 if (res & LEFT)
1943 dw = orig_x - event.xmotion.x_root;
1944 else if (res & RIGHT)
1945 dw = event.xmotion.x_root - orig_x;
1946 if (res & UP)
1947 dh = orig_y - event.xmotion.y_root;
1948 else if (res & DOWN)
1949 dh = event.xmotion.y_root - orig_y;
1951 orig_x = event.xmotion.x_root;
1952 orig_y = event.xmotion.y_root;
1954 rw += dw;
1955 rh += dh;
1956 fw = rw;
1957 fh = rh - vert_border;
1958 wWindowConstrainSize(wwin, &fw, &fh);
1959 fh += vert_border;
1960 if (res & LEFT)
1961 fx = rx2 - fw + 1;
1962 else if (res & RIGHT)
1963 fx = rx1;
1964 if (res & UP)
1965 fy = ry2 - fh + 1;
1966 else if (res & DOWN)
1967 fy = ry1;
1968 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1969 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1970 int tx, ty;
1971 Window junkw;
1972 int flags;
1974 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1975 orig_x, orig_y, &tx, &ty, &junkw);
1977 /* check if resizing through resizebar */
1978 if (is_resizebar)
1979 flags = RESIZEBAR;
1980 else
1981 flags = 0;
1983 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1984 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1985 flags |= HCONSTRAIN;
1987 res = getResizeDirection(wwin, tx, ty,
1988 orig_x - event.xmotion.x_root,
1989 orig_y - event.xmotion.y_root, flags);
1991 if (res == (UP|LEFT))
1992 XChangeActivePointerGrab(dpy, ButtonMotionMask
1993 | ButtonReleaseMask | ButtonPressMask,
1994 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1995 else if (res == (UP|RIGHT))
1996 XChangeActivePointerGrab(dpy, ButtonMotionMask
1997 | ButtonReleaseMask | ButtonPressMask,
1998 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1999 else if (res == (DOWN|LEFT))
2000 XChangeActivePointerGrab(dpy, ButtonMotionMask
2001 | ButtonReleaseMask | ButtonPressMask,
2002 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2003 else if (res == (DOWN|RIGHT))
2004 XChangeActivePointerGrab(dpy, ButtonMotionMask
2005 | ButtonReleaseMask | ButtonPressMask,
2006 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2007 else if (res == DOWN || res == UP)
2008 XChangeActivePointerGrab(dpy, ButtonMotionMask
2009 | ButtonReleaseMask | ButtonPressMask,
2010 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2011 else if (res & (DOWN|UP))
2012 XChangeActivePointerGrab(dpy, ButtonMotionMask
2013 | ButtonReleaseMask | ButtonPressMask,
2014 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2015 else if (res & (LEFT|RIGHT))
2016 XChangeActivePointerGrab(dpy, ButtonMotionMask
2017 | ButtonReleaseMask | ButtonPressMask,
2018 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2020 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
2021 CurrentTime);
2023 XGrabServer(dpy);
2025 /* Draw the resize frame for the first time. */
2026 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2028 drawTransparentFrame(wwin, fx, fy, fw, fh);
2030 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2032 started = 1;
2034 if (started) {
2035 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2036 drawTransparentFrame(wwin, orig_fx, orig_fy,
2037 orig_fw, orig_fh);
2038 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2039 drawTransparentFrame(wwin, fx, fy, fw, fh);
2040 } else {
2041 drawTransparentFrame(wwin, orig_fx, orig_fy,
2042 orig_fw, orig_fh);
2043 drawTransparentFrame(wwin, fx, fy, fw, fh);
2045 if (fh != orig_fh || fw != orig_fw) {
2046 if (wPreferences.size_display == WDIS_NEW) {
2047 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2048 orig_fy + orig_fh, res);
2050 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2053 break;
2055 case ButtonPress:
2056 break;
2058 case ButtonRelease:
2059 if (event.xbutton.button != ev->xbutton.button)
2060 break;
2062 if (started) {
2063 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2065 drawTransparentFrame(wwin, fx, fy, fw, fh);
2067 XUngrabKeyboard(dpy, CurrentTime);
2068 WMUnmapWidget(scr->gview);
2069 XUngrabServer(dpy);
2071 if (wwin->client.width != fw)
2072 wwin->flags.user_changed_width = 1;
2074 if (wwin->client.height != fh - vert_border)
2075 wwin->flags.user_changed_height = 1;
2077 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2079 #ifdef DEBUG
2080 puts("End resize window");
2081 #endif
2082 return;
2084 default:
2085 WMHandleEvent(&event);
2090 #undef LEFT
2091 #undef RIGHT
2092 #undef HORIZONTAL
2093 #undef UP
2094 #undef DOWN
2095 #undef VERTICAL
2096 #undef HCONSTRAIN
2097 #undef RESIZEBAR
2099 void
2100 wUnselectWindows(WScreen *scr)
2102 WWindow *wwin;
2104 if (!scr->selected_windows)
2105 return;
2107 while (WMGetArrayItemCount(scr->selected_windows)) {
2108 wwin = WMGetFromArray(scr->selected_windows, 0);
2109 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2110 wIconSelect(wwin->icon);
2112 wSelectWindow(wwin, False);
2114 WMFreeArray(scr->selected_windows);
2115 scr->selected_windows = NULL;
2118 #ifndef LITE
2119 static void
2120 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2122 WWindow *tmpw;
2124 /* select the windows and put them in the selected window list */
2125 tmpw = scr->focused_window;
2126 while (tmpw != NULL) {
2127 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2128 if ((tmpw->frame->workspace == scr->current_workspace
2129 || IS_OMNIPRESENT(tmpw))
2130 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2131 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2132 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2133 wSelectWindow(tmpw, True);
2136 tmpw = tmpw->prev;
2141 void
2142 wSelectWindows(WScreen *scr, XEvent *ev)
2144 XEvent event;
2145 Window root = scr->root_win;
2146 GC gc = scr->frame_gc;
2147 int xp = ev->xbutton.x_root;
2148 int yp = ev->xbutton.y_root;
2149 int w = 0, h = 0;
2150 int x = xp, y = yp;
2152 #ifdef DEBUG
2153 puts("Selecting windows");
2154 #endif
2155 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2156 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2157 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2158 CurrentTime) != Success) {
2159 return;
2161 XGrabServer(dpy);
2163 wUnselectWindows(scr);
2165 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2166 while (1) {
2167 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2168 | ButtonPressMask, &event);
2170 switch (event.type) {
2171 case MotionNotify:
2172 XDrawRectangle(dpy, root, gc, x, y, w, h);
2173 x = event.xmotion.x_root;
2174 if (x < xp) {
2175 w = xp - x;
2176 } else {
2177 w = x - xp;
2178 x = xp;
2180 y = event.xmotion.y_root;
2181 if (y < yp) {
2182 h = yp - y;
2183 } else {
2184 h = y - yp;
2185 y = yp;
2187 XDrawRectangle(dpy, root, gc, x, y, w, h);
2188 break;
2190 case ButtonPress:
2191 break;
2193 case ButtonRelease:
2194 if (event.xbutton.button != ev->xbutton.button)
2195 break;
2197 XDrawRectangle(dpy, root, gc, x, y, w, h);
2198 XUngrabServer(dpy);
2199 XUngrabPointer(dpy, CurrentTime);
2200 selectWindowsInside(scr, x, y, x + w, y + h);
2202 #ifdef KWM_HINTS
2203 wKWMSelectRootRegion(scr, xp, yp, w, h,
2204 event.xbutton.state & ControlMask);
2205 #endif /* KWM_HINTS */
2207 #ifdef DEBUG
2208 puts("End window selection");
2209 #endif
2210 return;
2212 default:
2213 WMHandleEvent(&event);
2214 break;
2218 #endif /* !LITE */
2220 void
2221 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2222 unsigned width, unsigned height)
2224 WScreen *scr = wwin->screen_ptr;
2225 Window root = scr->root_win;
2226 int x, y, h = 0;
2227 XEvent event;
2228 KeyCode shiftl, shiftr;
2229 Window junkw;
2230 int junk;
2232 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2233 GrabModeAsync, GrabModeAsync, None,
2234 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2235 *x_ret = 0;
2236 *y_ret = 0;
2237 return;
2239 if (!WFLAGP(wwin, no_titlebar)) {
2240 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2241 height += h;
2243 if (!WFLAGP(wwin, no_resizebar)) {
2244 height += RESIZEBAR_HEIGHT;
2246 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2247 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2248 (unsigned *) &junk);
2249 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2251 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2253 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2254 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2255 while (1) {
2256 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2257 &event);
2259 if (!checkMouseSamplingRate(&event))
2260 continue;
2262 switch (event.type) {
2263 case KeyPress:
2264 if ((event.xkey.keycode == shiftl)
2265 || (event.xkey.keycode == shiftr)) {
2266 drawTransparentFrame(wwin,
2267 x - width/2, y - h/2, width, height);
2268 cyclePositionDisplay(wwin,
2269 x - width/2, y - h/2, width, height);
2270 drawTransparentFrame(wwin,
2271 x - width/2, y - h/2, width, height);
2273 break;
2275 case MotionNotify:
2276 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2278 x = event.xmotion.x_root;
2279 y = event.xmotion.y_root;
2281 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2282 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2284 showPosition(wwin, x - width/2, y - h/2);
2286 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2288 break;
2290 case ButtonPress:
2291 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2292 XSync(dpy, 0);
2293 *x_ret = x - width/2;
2294 *y_ret = y - h/2;
2295 XUngrabPointer(dpy, CurrentTime);
2296 XUngrabKeyboard(dpy, CurrentTime);
2297 /* get rid of the geometry window */
2298 WMUnmapWidget(scr->gview);
2299 return;
2301 default:
2302 WMHandleEvent(&event);
2303 break;