Fixed a crashing bug in the multi vertical gradient code.
[wmaker-crm.git] / src / moveres.c
blobf1eab06082a47f8014d6204eb7bb5626a3564084
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 4
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 WSetGeometryViewShownPosition(scr->gview, x, y);
156 static void
157 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
159 WScreen *scr = wwin->screen_ptr;
161 wPreferences.move_display++;
162 wPreferences.move_display %= NUM_DISPLAYS;
164 if (wPreferences.move_display == WDIS_NEW) {
165 WMUnmapWidget(scr->gview);
166 } else {
167 if (wPreferences.move_display == WDIS_CENTER) {
168 moveGeometryDisplayCentered(scr,
169 scr->scr_width/2, scr->scr_height/2);
170 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
171 moveGeometryDisplayCentered(scr, 1, 1);
172 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
173 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
175 WMMapWidget(scr->gview);
180 static void
181 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
183 WScreen *scr = wwin->screen_ptr;
185 if (wPreferences.move_display == WDIS_NEW) {
186 return;
187 } else if (wPreferences.move_display == WDIS_CENTER) {
188 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
189 scr->scr_height / 2);
190 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
191 moveGeometryDisplayCentered(scr, 1, 1);
192 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
193 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
195 WMMapWidget(scr->gview);
196 WSetGeometryViewShownPosition(scr->gview, x, y);
200 static void
201 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
203 WScreen *scr = wwin->screen_ptr;
204 Window root = scr->root_win;
205 GC gc = scr->line_gc;
206 int ty, by, my, x, y, mx, s;
207 char num[16];
208 XSegment segment[4];
209 int fw, fh;
211 ty = y1 + wwin->frame->top_width;
212 by = y2 - wwin->frame->bottom_width;
213 fw = WMWidthOfString(scr->info_text_font, "8888", 4);
214 fh = WMFontHeight(scr->info_text_font);
216 if (wPreferences.size_display == WDIS_NEW) {
217 XSetForeground(dpy, gc, scr->line_pixel);
219 /* vertical geometry */
220 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
221 x = x2;
222 s = -15;
223 } else {
224 x = x1;
225 s = 15;
227 my = (ty + by) / 2;
229 /* top arrow */
230 /* end bar */
231 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
232 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
234 /* arrowhead */
235 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
236 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
238 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
239 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
241 /* line */
242 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
243 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
245 XDrawSegments(dpy, root, gc, segment, 4);
247 /* bottom arrow */
248 /* end bar */
249 segment[0].y1 = by;
250 segment[0].y2 = by;
252 /* arrowhead */
253 segment[1].y1 = by - 1;
254 segment[1].y2 = by - 7;
256 segment[2].y1 = by - 1;
257 segment[2].y2 = by - 7;
259 /* line */
260 segment[3].y1 = my + fh/2 + 2;
261 segment[3].y2 = by - 1;
263 XDrawSegments(dpy, root, gc, segment, 4);
265 sprintf(num, "%i", (by - ty - wwin->normal_hints->base_height) /
266 wwin->normal_hints->height_inc);
267 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
269 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
271 /* Display the height. */
272 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
273 x - s + 3 - fw/2, my - fh/2 + 1, num, strlen(num));
274 XSetForeground(dpy, gc, scr->line_pixel);
275 /* horizontal geometry */
276 if (y1 < 15) {
277 y = y2;
278 s = -15;
279 } else {
280 y = y1;
281 s = 15;
283 mx = x1 + (x2 - x1)/2;
284 sprintf(num, "%i", (x2 - x1 - wwin->normal_hints->base_width) /
285 wwin->normal_hints->width_inc);
286 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
288 /* left arrow */
289 /* end bar */
290 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
291 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
293 /* arrowhead */
294 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
295 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
297 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
298 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
300 /* line */
301 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
302 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
304 XDrawSegments(dpy, root, gc, segment, 4);
306 /* right arrow */
307 /* end bar */
308 segment[0].x1 = x2 + 1;
309 segment[0].x2 = x2 + 1;
311 /* arrowhead */
312 segment[1].x1 = x2 - 6;
313 segment[1].x2 = x2;
315 segment[2].x1 = x2;
316 segment[2].x2 = x2 - 6;
318 /* line */
319 segment[3].x1 = mx + fw/2 + 2;
320 segment[3].x2 = x2;
322 XDrawSegments(dpy, root, gc, segment, 4);
324 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
326 /* Display the width. */
327 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
328 mx - fw/2 + 1, y - s - fh/2 + 1, num, strlen(num));
329 } else {
330 WSetGeometryViewShownSize(scr->gview,
331 (x2 - x1 - wwin->normal_hints->base_width)
332 / wwin->normal_hints->width_inc,
333 (by - ty - wwin->normal_hints->base_height)
334 / wwin->normal_hints->height_inc);
339 static void
340 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
342 WScreen *scr = wwin->screen_ptr;
344 wPreferences.size_display++;
345 wPreferences.size_display %= NUM_DISPLAYS;
347 if (wPreferences.size_display == WDIS_NEW) {
348 WMUnmapWidget(scr->gview);
349 } else {
350 if (wPreferences.size_display == WDIS_CENTER) {
351 moveGeometryDisplayCentered(scr,
352 scr->scr_width / 2, scr->scr_height / 2);
353 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
354 moveGeometryDisplayCentered(scr, 1, 1);
355 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
356 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
358 WMMapWidget(scr->gview);
359 showGeometry(wwin, x, y, x + w, y + h, dir);
364 static void
365 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
367 WScreen *scr = wwin->screen_ptr;
369 if (wPreferences.size_display == WDIS_NEW)
370 return;
372 if (wPreferences.size_display == WDIS_CENTER) {
373 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
374 scr->scr_height / 2);
375 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
376 moveGeometryDisplayCentered(scr, 1, 1);
377 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
378 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
380 WMMapWidget(scr->gview);
381 showGeometry(wwin, x, y, x + w, y + h, 0);
386 static void
387 doWindowMove(WWindow *wwin, WMBag *bag, int dx, int dy)
389 WWindow *tmpw;
390 int x, y;
391 int scr_width = wwin->screen_ptr->scr_width;
392 int scr_height = wwin->screen_ptr->scr_height;
394 if (!bag || !WMGetBagItemCount(bag)) {
395 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
396 } else {
397 int i;
398 for (i = 0; i < WMGetBagItemCount(bag); i++) {
399 tmpw = WMGetFromBag(bag, i);
400 x = tmpw->frame_x + dx;
401 y = tmpw->frame_y + dy;
403 /* don't let windows become unreachable */
405 if (x + (int)tmpw->frame->core->width < 20)
406 x = 20 - (int)tmpw->frame->core->width;
407 else if (x + 20 > scr_width)
408 x = scr_width - 20;
410 if (y + (int)tmpw->frame->core->height < 20)
411 y = 20 - (int)tmpw->frame->core->height;
412 else if (y + 20 > scr_height)
413 y = scr_height - 20;
415 wWindowMove(tmpw, x, y);
421 static void
422 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
424 Window root = wwin->screen_ptr->root_win;
425 GC gc = wwin->screen_ptr->frame_gc;
426 int h = 0;
427 int bottom = 0;
429 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
430 h = WMFontHeight(wwin->screen_ptr->title_font) + TITLEBAR_EXTRA_HEIGHT;
432 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
433 /* Can't use wwin-frame->bottom_width because, in some cases
434 (e.g. interactive placement), frame does not point to anything. */
435 bottom = RESIZEBAR_HEIGHT - 1;
437 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
439 if (h > 0) {
440 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
442 if (bottom > 0) {
443 XDrawLine(dpy, root, gc, x + 1,
444 y + height - bottom,
445 x + width + 1,
446 y + height - bottom);
451 static void
452 drawFrames(WWindow *wwin, WMBag *bag, int dx, int dy)
454 WWindow *tmpw;
455 int scr_width = wwin->screen_ptr->scr_width;
456 int scr_height = wwin->screen_ptr->scr_height;
457 int x, y;
459 if (!bag) {
461 x = wwin->frame_x + dx;
462 y = wwin->frame_y + dy;
464 drawTransparentFrame(wwin, x, y,
465 wwin->frame->core->width,
466 wwin->frame->core->height);
468 } else {
469 int i;
470 for (i = 0; i < WMGetBagItemCount(bag); i++) {
471 tmpw = WMGetFromBag(bag, i);
472 x = tmpw->frame_x + dx;
473 y = tmpw->frame_y + dy;
475 /* don't let windows become unreachable */
477 if (x + (int)tmpw->frame->core->width < 20)
478 x = 20 - (int)tmpw->frame->core->width;
479 else if (x + 20 > scr_width)
480 x = scr_width - 20;
482 if (y + (int)tmpw->frame->core->height < 20)
483 y = 20 - (int)tmpw->frame->core->height;
484 else if (y + 20 > scr_height)
485 y = scr_height - 20;
487 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
488 tmpw->frame->core->height);
495 static void
496 flushMotion()
498 XEvent ev;
500 XSync(dpy, False);
501 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
505 static void
506 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
507 int new_workspace, int rewind)
509 /* do not let window be unmapped */
510 if (opaque_move) {
511 wwin->flags.changing_workspace = 1;
512 wWindowChangeWorkspace(wwin, new_workspace);
514 /* go to new workspace */
515 wWorkspaceChange(scr, new_workspace);
517 wwin->flags.changing_workspace = 0;
519 if (rewind)
520 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
521 else
522 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
524 flushMotion();
526 if (!opaque_move) {
527 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
528 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
529 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
536 typedef struct {
537 /* arrays of WWindows sorted by the respective border position */
538 WWindow **topList; /* top border */
539 WWindow **leftList; /* left border */
540 WWindow **rightList; /* right border */
541 WWindow **bottomList; /* bottom border */
542 int count;
544 /* index of window in the above lists indicating the relative position
545 * of the window with the others */
546 int topIndex;
547 int leftIndex;
548 int rightIndex;
549 int bottomIndex;
551 int rubCount; /* for workspace switching */
553 int winWidth, winHeight; /* width/height of the window */
554 int realX, realY; /* actual position of the window */
555 int calcX, calcY; /* calculated position of window */
556 int omouseX, omouseY; /* old mouse position */
557 int mouseX, mouseY; /* last known position of the pointer */
558 } MoveData;
560 #define WTOP(w) (w)->frame_y
561 #define WLEFT(w) (w)->frame_x
562 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
563 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
565 static int
566 compareWTop(const void *a, const void *b)
568 WWindow *wwin1 = *(WWindow**)a;
569 WWindow *wwin2 = *(WWindow**)b;
571 if (WTOP(wwin1) > WTOP(wwin2))
572 return -1;
573 else if (WTOP(wwin1) < WTOP(wwin2))
574 return 1;
575 else
576 return 0;
580 static int
581 compareWLeft(const void *a, const void *b)
583 WWindow *wwin1 = *(WWindow**)a;
584 WWindow *wwin2 = *(WWindow**)b;
586 if (WLEFT(wwin1) > WLEFT(wwin2))
587 return -1;
588 else if (WLEFT(wwin1) < WLEFT(wwin2))
589 return 1;
590 else
591 return 0;
595 static int
596 compareWRight(const void *a, const void *b)
598 WWindow *wwin1 = *(WWindow**)a;
599 WWindow *wwin2 = *(WWindow**)b;
601 if (WRIGHT(wwin1) < WRIGHT(wwin2))
602 return -1;
603 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
604 return 1;
605 else
606 return 0;
611 static int
612 compareWBottom(const void *a, const void *b)
614 WWindow *wwin1 = *(WWindow**)a;
615 WWindow *wwin2 = *(WWindow**)b;
617 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
618 return -1;
619 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
620 return 1;
621 else
622 return 0;
626 static void
627 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
629 int i;
630 int newX2 = newX + data->winWidth;
631 int newY2 = newY + data->winHeight;
632 Bool ok = False;
634 if (newX < data->realX) {
635 if (data->rightIndex > 0
636 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
637 ok = True;
638 } else if (data->leftIndex <= data->count-1
639 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
640 ok = True;
642 } else if (newX > data->realX) {
643 if (data->leftIndex > 0
644 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
645 ok = True;
646 } else if (data->rightIndex <= data->count-1
647 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
648 ok = True;
652 if (!ok) {
653 if (newY < data->realY) {
654 if (data->bottomIndex > 0
655 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
656 ok = True;
657 } else if (data->topIndex <= data->count-1
658 && newY2 <= WTOP(data->topList[data->topIndex])) {
659 ok = True;
661 } else if (newY > data->realY) {
662 if (data->topIndex > 0
663 && newY2 > WTOP(data->topList[data->topIndex-1])) {
664 ok = True;
665 } else if (data->bottomIndex <= data->count-1
666 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
667 ok = True;
672 if (!ok)
673 return;
675 /* TODO: optimize this */
676 if (data->realY < WBOTTOM(data->bottomList[0])) {
677 data->bottomIndex = 0;
679 if (data->realX < WRIGHT(data->rightList[0])) {
680 data->rightIndex = 0;
682 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
683 data->leftIndex = 0;
685 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
686 data->topIndex = 0;
688 for (i = 0; i < data->count; i++) {
689 if (data->realY > WBOTTOM(data->bottomList[i])) {
690 data->bottomIndex = i + 1;
692 if (data->realX > WRIGHT(data->rightList[i])) {
693 data->rightIndex = i + 1;
695 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
696 data->leftIndex = i + 1;
698 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
699 data->topIndex = i + 1;
705 static void
706 freeMoveData(MoveData *data)
708 if (data->topList)
709 free(data->topList);
710 if (data->leftList)
711 free(data->leftList);
712 if (data->rightList)
713 free(data->rightList);
714 if (data->bottomList)
715 free(data->bottomList);
719 static void
720 updateMoveData(WWindow *wwin, MoveData *data)
722 WScreen *scr = wwin->screen_ptr;
723 WWindow *tmp;
724 int i;
726 data->count = 0;
727 tmp = scr->focused_window;
728 while (tmp) {
729 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
730 && !tmp->flags.miniaturized
731 && !tmp->flags.hidden
732 && !tmp->flags.obscured
733 && !WFLAGP(tmp, sunken)) {
734 data->topList[data->count] = tmp;
735 data->leftList[data->count] = tmp;
736 data->rightList[data->count] = tmp;
737 data->bottomList[data->count] = tmp;
738 data->count++;
740 tmp = tmp->prev;
743 if (data->count == 0) {
744 data->topIndex = 0;
745 data->leftIndex = 0;
746 data->rightIndex = 0;
747 data->bottomIndex = 0;
748 return;
752 * order from closest to the border of the screen to farthest
754 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
755 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
756 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
757 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
759 /* figure the position of the window relative to the others */
761 data->topIndex = -1;
762 data->leftIndex = -1;
763 data->rightIndex = -1;
764 data->bottomIndex = -1;
766 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
767 data->bottomIndex = 0;
769 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
770 data->rightIndex = 0;
772 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
773 data->leftIndex = 0;
775 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
776 data->topIndex = 0;
778 for (i = 0; i < data->count; i++) {
779 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
780 data->bottomIndex = i + 1;
782 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
783 data->rightIndex = i + 1;
785 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
786 data->leftIndex = i + 1;
788 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
789 data->topIndex = i + 1;
795 static void
796 initMoveData(WWindow *wwin, MoveData *data)
798 int i;
799 WWindow *tmp;
801 memset(data, 0, sizeof(MoveData));
803 for (i = 0, tmp = wwin->screen_ptr->focused_window;
804 tmp != NULL;
805 tmp = tmp->prev, i++);
807 if (i > 1) {
808 data->topList = wmalloc(sizeof(WWindow*) * i);
809 data->leftList = wmalloc(sizeof(WWindow*) * i);
810 data->rightList = wmalloc(sizeof(WWindow*) * i);
811 data->bottomList = wmalloc(sizeof(WWindow*) * i);
813 updateMoveData(wwin, data);
816 data->realX = wwin->frame_x;
817 data->realY = wwin->frame_y;
818 data->calcX = wwin->frame_x;
819 data->calcY = wwin->frame_y;
821 data->winWidth = wwin->frame->core->width + 2;
822 data->winHeight = wwin->frame->core->height + 2;
826 static Bool
827 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
829 WScreen *scr = wwin->screen_ptr;
830 Bool changed = False;
832 if (data->mouseX <= 1) {
833 if (scr->current_workspace > 0) {
835 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
836 True);
837 changed = True;
838 data->rubCount = 0;
840 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
842 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
843 True);
844 changed = True;
845 data->rubCount = 0;
847 } else if (data->mouseX >= scr->scr_width - 2) {
849 if (scr->current_workspace == scr->workspace_count - 1) {
851 if (wPreferences.ws_cycle
852 || scr->workspace_count == MAX_WORKSPACES) {
854 crossWorkspace(scr, wwin, opaqueMove, 0, False);
855 changed = True;
856 data->rubCount = 0;
858 /* if user insists on trying to go to next workspace even when
859 * it's already the last, create a new one */
860 else if (data->omouseX == data->mouseX
861 && wPreferences.ws_advance) {
863 /* detect user "rubbing" the window against the edge */
864 if (data->rubCount > 0
865 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
867 data->rubCount = -(data->rubCount + 1);
869 } else if (data->rubCount <= 0
870 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
872 data->rubCount = -data->rubCount + 1;
875 /* create a new workspace */
876 if (abs(data->rubCount) > 2) {
877 /* go to next workspace */
878 wWorkspaceNew(scr);
880 crossWorkspace(scr, wwin, opaqueMove,
881 scr->current_workspace+1, False);
882 changed = True;
883 data->rubCount = 0;
885 } else if (scr->current_workspace < scr->workspace_count) {
887 /* go to next workspace */
888 crossWorkspace(scr, wwin, opaqueMove,
889 scr->current_workspace+1, False);
890 changed = True;
891 data->rubCount = 0;
893 } else {
894 data->rubCount = 0;
897 return changed;
901 static void
902 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
903 Bool opaqueMove, int newMouseX, int newMouseY)
905 WScreen *scr = wwin->screen_ptr;
906 int dx, dy; /* how much mouse moved */
907 int winL, winR, winT, winB; /* requested new window position */
908 int newX, newY; /* actual new window position */
909 Bool hresist, vresist;
910 Bool updateIndex;
911 Bool attract;
913 hresist = False;
914 vresist = False;
916 updateIndex = False;
918 /* check the direction of the movement */
919 dx = newMouseX - data->mouseX;
920 dy = newMouseY - data->mouseY;
922 data->omouseX = data->mouseX;
923 data->omouseY = data->mouseY;
924 data->mouseX = newMouseX;
925 data->mouseY = newMouseY;
927 winL = data->calcX + dx;
928 winR = data->calcX + data->winWidth + dx;
929 winT = data->calcY + dy;
930 winB = data->calcY + data->winHeight + dy;
932 newX = data->realX;
933 newY = data->realY;
935 if (doResistance) {
936 int l_edge, r_edge;
937 int edge_l, edge_r;
938 int t_edge, b_edge;
939 int edge_t, edge_b;
940 int resist;
942 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
943 attract = wPreferences.attract;
944 /* horizontal movement: check horizontal edge resistances */
945 if (dx || dy) {
946 int i;
947 /* window is the leftmost window: check against screen edge */
948 l_edge = scr->totalUsableArea.x1;
949 r_edge = scr->totalUsableArea.x2 + resist;
950 edge_l = scr->totalUsableArea.x1 - resist;
951 edge_r = scr->totalUsableArea.x2;
953 /* 1 */
954 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
955 WWindow *looprw;
957 for (i = data->rightIndex - 1; i >= 0; i--) {
958 looprw = data->rightList[i];
959 if (!(data->realY > WBOTTOM(looprw)
960 || (data->realY + data->winHeight) < WTOP(looprw))) {
961 if (attract
962 || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
963 l_edge = WRIGHT(looprw) + 1;
964 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
966 break;
970 if (attract) {
971 for (i = data->rightIndex; i < data->count; i++) {
972 looprw = data->rightList[i];
973 if(!(data->realY > WBOTTOM(looprw)
974 || (data->realY + data->winHeight) < WTOP(looprw))) {
975 r_edge = WRIGHT(looprw) + 1;
976 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
977 break;
983 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
984 WWindow *looprw;
986 for (i = data->leftIndex - 1; i >= 0; i--) {
987 looprw = data->leftList[i];
988 if (!(data->realY > WBOTTOM(looprw)
989 || (data->realY + data->winHeight) < WTOP(looprw))) {
990 if (attract
991 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1)) && dx > 0)) {
992 edge_r = WLEFT(looprw);
993 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
995 break;
999 if (attract)
1000 for (i = data->leftIndex; i < data->count; i++) {
1001 looprw = data->leftList[i];
1002 if(!(data->realY > WBOTTOM(looprw)
1003 || (data->realY + data->winHeight) < WTOP(looprw))) {
1004 edge_l = WLEFT(looprw);
1005 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1006 break;
1012 printf("%d %d\n",winL,winR);
1013 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1016 if ((winL - l_edge) < (r_edge - winL)) {
1017 if (resist > 0) {
1018 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1019 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1020 newX = l_edge;
1021 hresist = True;
1024 } else {
1025 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1026 newX = r_edge;
1027 hresist = True;
1031 if ((winR - edge_l) < (edge_r - winR)) {
1032 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1033 newX = edge_l - data->winWidth;
1034 hresist = True;
1036 } else {
1037 if (resist > 0) {
1038 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1039 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1040 newX = edge_r - data->winWidth;
1041 hresist = True;
1046 /* VeRT */
1047 t_edge = scr->totalUsableArea.y1;
1048 b_edge = scr->totalUsableArea.y2 + resist;
1049 edge_t = scr->totalUsableArea.y1 - resist;
1050 edge_b = scr->totalUsableArea.y2;
1052 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1053 WWindow *looprw;
1055 for (i = data->bottomIndex - 1; i >= 0; i--) {
1056 looprw = data->bottomList[i];
1057 if (!(data->realX > WRIGHT(looprw)
1058 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1059 if (attract
1060 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1061 t_edge = WBOTTOM(looprw) + 1;
1062 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1064 break;
1068 if (attract) {
1069 for (i = data->bottomIndex; i < data->count; i++) {
1070 looprw = data->bottomList[i];
1071 if(!(data->realX > WRIGHT(looprw)
1072 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1073 b_edge = WBOTTOM(looprw) + 1;
1074 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1075 break;
1081 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1082 WWindow *looprw;
1084 for (i = data->topIndex - 1; i >= 0; i--) {
1085 looprw = data->topList[i];
1086 if (!(data->realX > WRIGHT(looprw)
1087 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1088 if (attract
1089 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1090 edge_b = WTOP(looprw);
1091 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1093 break;
1097 if (attract)
1098 for (i = data->topIndex; i < data->count; i++) {
1099 looprw = data->topList[i];
1100 if(!(data->realX > WRIGHT(looprw)
1101 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1102 edge_t = WTOP(looprw);
1103 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1104 break;
1109 if ((winT - t_edge) < (b_edge - winT)) {
1110 if (resist > 0) {
1111 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1112 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1113 newY = t_edge;
1114 vresist = True;
1118 else {
1119 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1120 newY = b_edge;
1121 vresist = True;
1125 if ((winB - edge_t) < (edge_b - winB)) {
1126 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1127 newY = edge_t - data->winHeight;
1128 vresist = True;
1131 else {
1132 if (resist > 0) {
1133 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1134 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1135 newY = edge_b - data->winHeight;
1136 vresist = True;
1141 /* END VeRT */
1145 /* update window position */
1146 data->calcX += dx;
1147 data->calcY += dy;
1149 if (((dx > 0 && data->calcX - data->realX > 0)
1150 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1151 newX = data->calcX;
1153 if (((dy > 0 && data->calcY - data->realY > 0)
1154 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1155 newY = data->calcY;
1157 if (data->realX != newX || data->realY != newY) {
1159 if (wPreferences.move_display == WDIS_NEW
1160 && !scr->selected_windows) {
1161 showPosition(wwin, data->realX, data->realY);
1163 if (opaqueMove) {
1164 doWindowMove(wwin, scr->selected_windows,
1165 newX - wwin->frame_x,
1166 newY - wwin->frame_y);
1167 } else {
1168 /* erase frames */
1169 drawFrames(wwin, scr->selected_windows,
1170 data->realX - wwin->frame_x,
1171 data->realY - wwin->frame_y);
1174 if (!scr->selected_windows
1175 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1177 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1178 newY + data->winHeight/2);
1181 if (!opaqueMove) {
1182 /* draw frames */
1183 drawFrames(wwin, scr->selected_windows,
1184 newX - wwin->frame_x,
1185 newY - wwin->frame_y);
1188 if (!scr->selected_windows) {
1189 showPosition(wwin, newX, newY);
1194 /* recalc relative window position */
1195 if (doResistance && (data->realX != newX || data->realY != newY)) {
1196 updateResistance(wwin, data, newX, newY);
1199 data->realX = newX;
1200 data->realY = newY;
1204 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1207 wKeyboardMoveResizeWindow(WWindow *wwin)
1209 WScreen *scr = wwin->screen_ptr;
1210 Window root = scr->root_win;
1211 XEvent event;
1212 int w = wwin->frame->core->width;
1213 int h = wwin->frame->core->height;
1214 int scr_width = wwin->screen_ptr->scr_width;
1215 int scr_height = wwin->screen_ptr->scr_height;
1216 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1217 int src_x = wwin->frame_x;
1218 int src_y = wwin->frame_y;
1219 int done,off_x,off_y,ww,wh;
1220 int kspeed = _KS;
1221 Time lastTime = 0;
1222 KeySym keysym=NoSymbol;
1223 int moment=0;
1224 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1226 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1227 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1228 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1229 ctrlmode=done=off_x=off_y=0;
1231 XSync(dpy, False);
1232 wusleep(10000);
1233 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1235 if (!wwin->flags.selected) {
1236 wUnselectWindows(scr);
1238 XGrabServer(dpy);
1239 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1240 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1241 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1243 if (wwin->flags.shaded || scr->selected_windows) {
1244 if(scr->selected_windows)
1245 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1246 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1247 if(!scr->selected_windows)
1248 mapPositionDisplay(wwin, src_x, src_y, w, h);
1249 } else {
1250 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1252 ww=w;
1253 wh=h;
1254 while(1) {
1256 looper.ox=off_x;
1257 looper.oy=off_y;
1259 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1260 | ButtonPressMask | ExposureMask, &event);
1261 if (wwin->flags.shaded || scr->selected_windows) {
1262 if(scr->selected_windows)
1263 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1264 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1265 /*** I HATE EDGE RESISTANCE - ]d ***/
1267 else {
1268 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1271 if(ctrlmode)
1272 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1274 XUngrabServer(dpy);
1275 XSync(dpy, False);
1277 switch (event.type) {
1278 case KeyPress:
1279 /* accelerate */
1280 if (event.xkey.time - lastTime > 50) {
1281 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1282 } else {
1283 if (kspeed < 20) {
1284 kspeed++;
1287 if (kspeed < _KS) kspeed = _KS;
1288 lastTime = event.xkey.time;
1290 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1291 ctrlmode=1;
1292 wUnselectWindows(scr);
1294 else {
1295 ctrlmode=0;
1297 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1298 if (ctrlmode)
1299 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1300 else
1301 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1303 else {
1305 keysym = XLookupKeysym(&event.xkey, 0);
1306 switch (keysym) {
1307 case XK_Return:
1308 done=2;
1309 break;
1310 case XK_Escape:
1311 done=1;
1312 break;
1313 case XK_Up:
1314 case XK_KP_Up:
1315 case XK_k:
1316 if (ctrlmode){
1317 if (moment != UP)
1318 h = wh;
1319 h-=kspeed;
1320 moment = UP;
1321 if (h < 1) h = 1;
1323 else off_y-=kspeed;
1324 break;
1325 case XK_Down:
1326 case XK_KP_Down:
1327 case XK_j:
1328 if (ctrlmode){
1329 if (moment != DOWN)
1330 h = wh;
1331 h+=kspeed;
1332 moment = DOWN;
1334 else off_y+=kspeed;
1335 break;
1336 case XK_Left:
1337 case XK_KP_Left:
1338 case XK_h:
1339 if (ctrlmode) {
1340 if (moment != LEFT)
1341 w = ww;
1342 w-=kspeed;
1343 if (w < 1) w = 1;
1344 moment = LEFT;
1346 else off_x-=kspeed;
1347 break;
1348 case XK_Right:
1349 case XK_KP_Right:
1350 case XK_l:
1351 if (ctrlmode) {
1352 if (moment != RIGHT)
1353 w = ww;
1354 w+=kspeed;
1355 moment = RIGHT;
1357 else off_x+=kspeed;
1358 break;
1361 ww=w;wh=h;
1362 wh-=vert_border;
1363 wWindowConstrainSize(wwin, &ww, &wh);
1364 wh+=vert_border;
1366 if (wPreferences.ws_cycle){
1367 if (src_x + off_x + ww < 20){
1368 if(!scr->current_workspace) {
1369 wWorkspaceChange(scr, scr->workspace_count-1);
1371 else wWorkspaceChange(scr, scr->current_workspace-1);
1372 off_x += scr_width;
1374 else if (src_x + off_x + 20 > scr_width){
1375 if(scr->current_workspace == scr->workspace_count-1) {
1376 wWorkspaceChange(scr, 0);
1378 else wWorkspaceChange(scr, scr->current_workspace+1);
1379 off_x -= scr_width;
1382 else {
1383 if (src_x + off_x + ww < 20)
1384 off_x = 20 - ww - src_x;
1385 else if (src_x + off_x + 20 > scr_width)
1386 off_x = scr_width - 20 - src_x;
1389 if (src_y + off_y + wh < 20) {
1390 off_y = 20 - wh - src_y;
1392 else if (src_y + off_y + 20 > scr_height) {
1393 off_y = scr_height - 20 - src_y;
1396 break;
1397 case ButtonPress:
1398 case ButtonRelease:
1399 done=1;
1400 break;
1401 default:
1402 WMHandleEvent(&event);
1403 break;
1406 XGrabServer(dpy);
1407 /*xxx*/
1409 if (wwin->flags.shaded && !scr->selected_windows){
1410 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1411 } else {
1412 if (ctrlmode) {
1413 WMUnmapWidget(scr->gview);
1414 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1415 } else if(!scr->selected_windows) {
1416 WMUnmapWidget(scr->gview);
1417 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1421 if (wwin->flags.shaded || scr->selected_windows) {
1422 if (scr->selected_windows)
1423 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1424 else
1425 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1426 } else {
1427 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1431 if (ctrlmode) {
1432 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1433 } else if(!scr->selected_windows)
1434 showPosition(wwin, src_x+off_x, src_y+off_y);
1435 /**/
1437 if(done){
1438 scr->keymove_tick=0;
1440 WMDeleteTimerWithClientData(&looper);
1442 if (wwin->flags.shaded || scr->selected_windows) {
1443 if(scr->selected_windows)
1444 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1445 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1447 else {
1448 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1451 if (ctrlmode) {
1452 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1453 WMUnmapWidget(scr->gview);
1454 } else
1455 WMUnmapWidget(scr->gview);
1457 XUngrabKeyboard(dpy, CurrentTime);
1458 XUngrabPointer(dpy, CurrentTime);
1459 XUngrabServer(dpy);
1461 if(done==2) {
1462 if (wwin->flags.shaded || scr->selected_windows) {
1463 if (!scr->selected_windows) {
1464 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1465 wWindowSynthConfigureNotify(wwin);
1466 } else {
1467 int i;
1468 WMBag *bag = scr->selected_windows;
1469 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1470 for (i = 0; i < WMGetBagItemCount(bag); i++) {
1471 wWindowSynthConfigureNotify(WMGetFromBag(bag, i));
1474 } else {
1475 if (wwin->client.width != ww)
1476 wwin->flags.user_changed_width = 1;
1478 if (wwin->client.height != wh - vert_border)
1479 wwin->flags.user_changed_height = 1;
1481 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1482 ww, wh - vert_border);
1483 wWindowSynthConfigureNotify(wwin);
1485 wWindowChangeWorkspace(wwin, scr->current_workspace);
1486 wSetFocusTo(scr, wwin);
1488 return 1;
1495 *----------------------------------------------------------------------
1496 * wMouseMoveWindow--
1497 * Move the named window and the other selected ones (if any),
1498 * interactively. Also shows the position of the window, if only one
1499 * window is being moved.
1500 * If the window is not on the selected window list, the selected
1501 * windows are deselected.
1502 * If shift is pressed during the operation, the position display
1503 * is changed to another type.
1505 * Returns:
1506 * True if the window was moved, False otherwise.
1508 * Side effects:
1509 * The window(s) position is changed, and the client(s) are
1510 * notified about that.
1511 * The position display configuration may be changed.
1512 *----------------------------------------------------------------------
1515 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1517 WScreen *scr = wwin->screen_ptr;
1518 XEvent event;
1519 Window root = scr->root_win;
1520 KeyCode shiftl, shiftr;
1521 Bool done = False;
1522 int started = 0;
1523 int warped = 0;
1524 /* This needs not to change while moving, else bad things can happen */
1525 int opaqueMove = wPreferences.opaque_move;
1526 MoveData moveData;
1527 #ifdef GHOST_WINDOW_MOVE
1528 RImage *rimg;
1530 rimg = InitGhostWindowMove(scr);
1531 #endif
1534 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1535 XSetWindowAttributes attr;
1537 attr.save_under = True;
1538 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1539 CWSaveUnder, &attr);
1543 initMoveData(wwin, &moveData);
1545 moveData.mouseX = ev->xmotion.x_root;
1546 moveData.mouseY = ev->xmotion.y_root;
1548 if (!wwin->flags.selected) {
1549 /* this window is not selected, unselect others and move only wwin */
1550 wUnselectWindows(scr);
1552 #ifdef DEBUG
1553 puts("Moving window");
1554 #endif
1555 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1556 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1557 while (!done) {
1558 if (warped) {
1559 int junk;
1560 Window junkw;
1562 /* XWarpPointer() doesn't seem to generate Motion events, so
1563 we've got to simulate them */
1564 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1565 &event.xmotion.y_root, &junk, &junk,
1566 (unsigned *) &junk);
1567 } else {
1568 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1569 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1570 &event);
1572 if (event.type == MotionNotify) {
1573 /* compress MotionNotify events */
1574 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1575 if (!checkMouseSamplingRate(&event))
1576 continue;
1579 switch (event.type) {
1580 case KeyPress:
1581 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1582 && started && !scr->selected_windows) {
1584 if (!opaqueMove) {
1585 drawFrames(wwin, scr->selected_windows,
1586 moveData.realX - wwin->frame_x,
1587 moveData.realY - wwin->frame_y);
1590 if (wPreferences.move_display == WDIS_NEW
1591 && !scr->selected_windows) {
1592 showPosition(wwin, moveData.realX, moveData.realY);
1593 XUngrabServer(dpy);
1595 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1596 moveData.winWidth, moveData.winHeight);
1598 if (wPreferences.move_display == WDIS_NEW
1599 && !scr->selected_windows) {
1600 XGrabServer(dpy);
1601 showPosition(wwin, moveData.realX, moveData.realY);
1604 if (!opaqueMove) {
1605 drawFrames(wwin, scr->selected_windows,
1606 moveData.realX - wwin->frame_x,
1607 moveData.realY - wwin->frame_y);
1610 break;
1612 case MotionNotify:
1613 if (started) {
1614 updateWindowPosition(wwin, &moveData,
1615 scr->selected_windows == NULL
1616 && wPreferences.edge_resistance > 0,
1617 opaqueMove,
1618 event.xmotion.x_root,
1619 event.xmotion.y_root);
1621 if (!warped && !wPreferences.no_autowrap) {
1622 int oldWorkspace = scr->current_workspace;
1624 if (wPreferences.move_display == WDIS_NEW
1625 && !scr->selected_windows) {
1626 showPosition(wwin, moveData.realX, moveData.realY);
1627 XUngrabServer(dpy);
1629 if (!opaqueMove) {
1630 drawFrames(wwin, scr->selected_windows,
1631 moveData.realX - wwin->frame_x,
1632 moveData.realY - wwin->frame_y);
1634 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1635 if (scr->current_workspace != oldWorkspace
1636 && wPreferences.edge_resistance > 0
1637 && scr->selected_windows == NULL)
1638 updateMoveData(wwin, &moveData);
1639 warped = 1;
1641 if (!opaqueMove) {
1642 drawFrames(wwin, scr->selected_windows,
1643 moveData.realX - wwin->frame_x,
1644 moveData.realY - wwin->frame_y);
1646 if (wPreferences.move_display == WDIS_NEW
1647 && !scr->selected_windows) {
1648 XSync(dpy, False);
1649 showPosition(wwin, moveData.realX, moveData.realY);
1650 XGrabServer(dpy);
1652 } else {
1653 warped = 0;
1655 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1656 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1658 XChangeActivePointerGrab(dpy, ButtonMotionMask
1659 | ButtonReleaseMask | ButtonPressMask,
1660 wCursor[WCUR_MOVE], CurrentTime);
1661 started = 1;
1662 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1663 CurrentTime);
1665 if (!scr->selected_windows)
1666 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1667 moveData.winWidth, moveData.winHeight);
1669 if (started && !opaqueMove)
1670 drawFrames(wwin, scr->selected_windows, 0, 0);
1672 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1673 && !scr->selected_windows)) {
1674 XGrabServer(dpy);
1675 if (wPreferences.move_display==WDIS_NEW)
1676 showPosition(wwin, moveData.realX, moveData.realY);
1679 break;
1681 case ButtonPress:
1682 break;
1684 case ButtonRelease:
1685 if (event.xbutton.button != ev->xbutton.button)
1686 break;
1688 if (started) {
1689 if (!opaqueMove) {
1690 drawFrames(wwin, scr->selected_windows,
1691 moveData.realX - wwin->frame_x,
1692 moveData.realY - wwin->frame_y);
1693 XSync(dpy, 0);
1694 doWindowMove(wwin, scr->selected_windows,
1695 moveData.realX - wwin->frame_x,
1696 moveData.realY - wwin->frame_y);
1698 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1699 wWindowSynthConfigureNotify(wwin);
1700 #endif
1701 XUngrabKeyboard(dpy, CurrentTime);
1702 XUngrabServer(dpy);
1703 if (!opaqueMove) {
1704 wWindowChangeWorkspace(wwin, scr->current_workspace);
1705 wSetFocusTo(scr, wwin);
1707 if (wPreferences.move_display == WDIS_NEW)
1708 showPosition(wwin, moveData.realX, moveData.realY);
1710 if (!scr->selected_windows) {
1711 /* get rid of the geometry window */
1712 WMUnmapWidget(scr->gview);
1715 #ifdef DEBUG
1716 puts("End move window");
1717 #endif
1718 done = True;
1719 break;
1721 default:
1722 if (started && !opaqueMove) {
1723 drawFrames(wwin, scr->selected_windows,
1724 moveData.realX - wwin->frame_x,
1725 moveData.realY - wwin->frame_y);
1726 XUngrabServer(dpy);
1727 WMHandleEvent(&event);
1728 XSync(dpy, False);
1729 XGrabServer(dpy);
1730 drawFrames(wwin, scr->selected_windows,
1731 moveData.realX - wwin->frame_x,
1732 moveData.realY - wwin->frame_y);
1733 } else {
1734 WMHandleEvent(&event);
1736 break;
1740 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1741 XSetWindowAttributes attr;
1744 attr.save_under = False;
1745 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1746 CWSaveUnder, &attr);
1750 freeMoveData(&moveData);
1752 return started;
1756 #define RESIZEBAR 1
1757 #define HCONSTRAIN 2
1759 static int
1760 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1761 int flags)
1763 int w = wwin->frame->core->width - 1;
1764 int cw = wwin->frame->resizebar_corner_width;
1765 int dir;
1767 /* if not resizing through the resizebar */
1768 if (!(flags & RESIZEBAR)) {
1769 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1770 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1771 if (abs(dx) < 2 || abs(dy) < 2) {
1772 if (abs(dy) > abs(dx))
1773 xdir = 0;
1774 else
1775 ydir = 0;
1777 return (xdir | ydir);
1780 /* window is too narrow. allow diagonal resize */
1781 if (cw * 2 >= w) {
1782 int ydir;
1784 if (flags & HCONSTRAIN)
1785 ydir = 0;
1786 else
1787 ydir = DOWN;
1788 if (x < cw)
1789 return (LEFT | ydir);
1790 else
1791 return (RIGHT | ydir);
1793 /* vertical resize */
1794 if ((x > cw) && (x < w - cw))
1795 return DOWN;
1797 if (x < cw)
1798 dir = LEFT;
1799 else
1800 dir = RIGHT;
1802 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1803 dir |= DOWN;
1805 return dir;
1809 void
1810 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1812 XEvent event;
1813 WScreen *scr = wwin->screen_ptr;
1814 Window root = scr->root_win;
1815 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1816 int fw = wwin->frame->core->width;
1817 int fh = wwin->frame->core->height;
1818 int fx = wwin->frame_x;
1819 int fy = wwin->frame_y;
1820 int is_resizebar = (wwin->frame->resizebar
1821 && ev->xany.window==wwin->frame->resizebar->window);
1822 int orig_x, orig_y;
1823 int started;
1824 int dw, dh;
1825 int rw = fw, rh = fh;
1826 int rx1, ry1, rx2, ry2;
1827 int res = 0;
1828 KeyCode shiftl, shiftr;
1829 int h = 0;
1830 int orig_fx = fx;
1831 int orig_fy = fy;
1832 int orig_fw = fw;
1833 int orig_fh = fh;
1835 if (wwin->flags.shaded) {
1836 wwarning("internal error: tryein");
1837 return;
1839 orig_x = ev->xbutton.x_root;
1840 orig_y = ev->xbutton.y_root;
1842 started = 0;
1843 #ifdef DEBUG
1844 puts("Resizing window");
1845 #endif
1847 wUnselectWindows(scr);
1848 rx1 = fx;
1849 rx2 = fx + fw - 1;
1850 ry1 = fy;
1851 ry2 = fy + fh - 1;
1852 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1853 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1854 if (!WFLAGP(wwin, no_titlebar))
1855 h = WMFontHeight(wwin->screen_ptr->title_font) + TITLEBAR_EXTRA_HEIGHT;
1856 else
1857 h = 0;
1858 while (1) {
1859 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1860 | ButtonPressMask | ExposureMask, &event);
1861 if (!checkMouseSamplingRate(&event))
1862 continue;
1864 switch (event.type) {
1865 case KeyPress:
1866 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1867 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1868 && started) {
1869 drawTransparentFrame(wwin, fx, fy, fw, fh);
1870 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1871 drawTransparentFrame(wwin, fx, fy, fw, fh);
1873 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1874 break;
1876 case MotionNotify:
1877 if (started) {
1878 dw = 0;
1879 dh = 0;
1881 orig_fx = fx;
1882 orig_fy = fy;
1883 orig_fw = fw;
1884 orig_fh = fh;
1886 if (res & LEFT)
1887 dw = orig_x - event.xmotion.x_root;
1888 else if (res & RIGHT)
1889 dw = event.xmotion.x_root - orig_x;
1890 if (res & UP)
1891 dh = orig_y - event.xmotion.y_root;
1892 else if (res & DOWN)
1893 dh = event.xmotion.y_root - orig_y;
1895 orig_x = event.xmotion.x_root;
1896 orig_y = event.xmotion.y_root;
1898 rw += dw;
1899 rh += dh;
1900 fw = rw;
1901 fh = rh - vert_border;
1902 wWindowConstrainSize(wwin, &fw, &fh);
1903 fh += vert_border;
1904 if (res & LEFT)
1905 fx = rx2 - fw + 1;
1906 else if (res & RIGHT)
1907 fx = rx1;
1908 if (res & UP)
1909 fy = ry2 - fh + 1;
1910 else if (res & DOWN)
1911 fy = ry1;
1912 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1913 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1914 int tx, ty;
1915 Window junkw;
1916 int flags;
1918 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1919 orig_x, orig_y, &tx, &ty, &junkw);
1921 /* check if resizing through resizebar */
1922 if (is_resizebar)
1923 flags = RESIZEBAR;
1924 else
1925 flags = 0;
1927 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1928 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1929 flags |= HCONSTRAIN;
1931 res = getResizeDirection(wwin, tx, ty,
1932 orig_x - event.xmotion.x_root,
1933 orig_y - event.xmotion.y_root, flags);
1935 XChangeActivePointerGrab(dpy, ButtonMotionMask
1936 | ButtonReleaseMask | ButtonPressMask,
1937 wCursor[WCUR_RESIZE], CurrentTime);
1938 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1939 CurrentTime);
1941 XGrabServer(dpy);
1943 /* Draw the resize frame for the first time. */
1944 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1946 drawTransparentFrame(wwin, fx, fy, fw, fh);
1948 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1950 started = 1;
1952 if (started) {
1953 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1954 drawTransparentFrame(wwin, orig_fx, orig_fy,
1955 orig_fw, orig_fh);
1956 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1957 drawTransparentFrame(wwin, fx, fy, fw, fh);
1958 } else {
1959 drawTransparentFrame(wwin, orig_fx, orig_fy,
1960 orig_fw, orig_fh);
1961 drawTransparentFrame(wwin, fx, fy, fw, fh);
1963 if (fh != orig_fh || fw != orig_fw) {
1964 if (wPreferences.size_display == WDIS_NEW) {
1965 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1966 orig_fy + orig_fh, res);
1968 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1971 break;
1973 case ButtonPress:
1974 break;
1976 case ButtonRelease:
1977 if (event.xbutton.button != ev->xbutton.button)
1978 break;
1980 if (started) {
1981 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1983 drawTransparentFrame(wwin, fx, fy, fw, fh);
1985 XUngrabKeyboard(dpy, CurrentTime);
1986 WMUnmapWidget(scr->gview);
1987 XUngrabServer(dpy);
1989 if (wwin->client.width != fw)
1990 wwin->flags.user_changed_width = 1;
1992 if (wwin->client.height != fh - vert_border)
1993 wwin->flags.user_changed_height = 1;
1995 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1997 #ifdef DEBUG
1998 puts("End resize window");
1999 #endif
2000 return;
2002 default:
2003 WMHandleEvent(&event);
2008 #undef LEFT
2009 #undef RIGHT
2010 #undef HORIZONTAL
2011 #undef UP
2012 #undef DOWN
2013 #undef VERTICAL
2014 #undef HCONSTRAIN
2015 #undef RESIZEBAR
2017 void
2018 wUnselectWindows(WScreen *scr)
2020 WWindow *wwin;
2022 if (!scr->selected_windows)
2023 return;
2025 while (WMGetBagItemCount(scr->selected_windows)) {
2026 wwin = WMGetFromBag(scr->selected_windows, 0);
2027 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2028 wIconSelect(wwin->icon);
2030 wSelectWindow(wwin, False);
2032 WMFreeBag(scr->selected_windows);
2033 scr->selected_windows = NULL;
2036 #ifndef LITE
2037 static void
2038 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2040 WWindow *tmpw;
2042 /* select the windows and put them in the selected window list */
2043 tmpw = scr->focused_window;
2044 while (tmpw != NULL) {
2045 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2046 if ((tmpw->frame->workspace == scr->current_workspace
2047 || IS_OMNIPRESENT(tmpw))
2048 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2049 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2050 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2051 wSelectWindow(tmpw, True);
2054 tmpw = tmpw->prev;
2059 void
2060 wSelectWindows(WScreen *scr, XEvent *ev)
2062 XEvent event;
2063 Window root = scr->root_win;
2064 GC gc = scr->frame_gc;
2065 int xp = ev->xbutton.x_root;
2066 int yp = ev->xbutton.y_root;
2067 int w = 0, h = 0;
2068 int x = xp, y = yp;
2070 #ifdef DEBUG
2071 puts("Selecting windows");
2072 #endif
2073 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2074 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2075 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2076 CurrentTime) != Success) {
2077 return;
2079 XGrabServer(dpy);
2081 wUnselectWindows(scr);
2083 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2084 while (1) {
2085 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2086 | ButtonPressMask, &event);
2088 switch (event.type) {
2089 case MotionNotify:
2090 XDrawRectangle(dpy, root, gc, x, y, w, h);
2091 x = event.xmotion.x_root;
2092 if (x < xp) {
2093 w = xp - x;
2094 } else {
2095 w = x - xp;
2096 x = xp;
2098 y = event.xmotion.y_root;
2099 if (y < yp) {
2100 h = yp - y;
2101 } else {
2102 h = y - yp;
2103 y = yp;
2105 XDrawRectangle(dpy, root, gc, x, y, w, h);
2106 break;
2108 case ButtonPress:
2109 break;
2111 case ButtonRelease:
2112 if (event.xbutton.button != ev->xbutton.button)
2113 break;
2115 XDrawRectangle(dpy, root, gc, x, y, w, h);
2116 XUngrabServer(dpy);
2117 XUngrabPointer(dpy, CurrentTime);
2118 selectWindowsInside(scr, x, y, x + w, y + h);
2120 #ifdef KWM_HINTS
2121 wKWMSelectRootRegion(scr, xp, yp, w, h,
2122 event.xbutton.state & ControlMask);
2123 #endif /* KWM_HINTS */
2125 #ifdef DEBUG
2126 puts("End window selection");
2127 #endif
2128 return;
2130 default:
2131 WMHandleEvent(&event);
2132 break;
2136 #endif /* !LITE */
2138 void
2139 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2140 unsigned width, unsigned height)
2142 WScreen *scr = wwin->screen_ptr;
2143 Window root = scr->root_win;
2144 int x, y, h = 0;
2145 XEvent event;
2146 KeyCode shiftl, shiftr;
2147 Window junkw;
2148 int junk;
2150 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2151 GrabModeAsync, GrabModeAsync, None,
2152 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2153 *x_ret = 0;
2154 *y_ret = 0;
2155 return;
2157 if (!WFLAGP(wwin, no_titlebar)) {
2158 h = WMFontHeight(scr->title_font) + TITLEBAR_EXTRA_HEIGHT;
2159 height += h;
2161 if (!WFLAGP(wwin, no_resizebar)) {
2162 height += RESIZEBAR_HEIGHT;
2164 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2165 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2166 (unsigned *) &junk);
2167 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2169 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2171 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2172 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2173 while (1) {
2174 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2175 &event);
2177 if (!checkMouseSamplingRate(&event))
2178 continue;
2180 switch (event.type) {
2181 case KeyPress:
2182 if ((event.xkey.keycode == shiftl)
2183 || (event.xkey.keycode == shiftr)) {
2184 drawTransparentFrame(wwin,
2185 x - width/2, y - h/2, width, height);
2186 cyclePositionDisplay(wwin,
2187 x - width/2, y - h/2, width, height);
2188 drawTransparentFrame(wwin,
2189 x - width/2, y - h/2, width, height);
2191 break;
2193 case MotionNotify:
2194 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2196 x = event.xmotion.x_root;
2197 y = event.xmotion.y_root;
2199 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2200 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2202 showPosition(wwin, x - width/2, y - h/2);
2204 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2206 break;
2208 case ButtonPress:
2209 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2210 XSync(dpy, 0);
2211 *x_ret = x - width/2;
2212 *y_ret = y - h/2;
2213 XUngrabPointer(dpy, CurrentTime);
2214 XUngrabKeyboard(dpy, CurrentTime);
2215 /* get rid of the geometry window */
2216 WMUnmapWidget(scr->gview);
2217 return;
2219 default:
2220 WMHandleEvent(&event);
2221 break;