fixed bug that caused slow mapping/raising/lowering of large windows
[wmaker-crm.git] / src / moveres.c
blobbab53487e02603be70333f3c28209226b38116fe
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 "list.h"
45 #ifdef KWM_HINTS
46 #include "kwm.h"
47 #endif
49 /* How many different types of geometry/position
50 display thingies are there? */
51 #define NUM_DISPLAYS 4
53 #define LEFT 1
54 #define RIGHT 2
55 #define HORIZONTAL (LEFT|RIGHT)
56 #define UP 4
57 #define DOWN 8
58 #define VERTICAL (UP|DOWN)
60 /****** Global Variables ******/
61 extern Time LastTimestamp;
63 extern Cursor wCursor[WCUR_LAST];
65 extern WPreferences wPreferences;
67 extern Atom _XA_WM_PROTOCOLS;
71 void
72 wGetGeometryWindowSize(WScreen *scr, unsigned int *width,
73 unsigned int *height)
75 #ifdef I18N_MB
76 *width = XmbTextEscapement(scr->info_text_font->font, "-8888 x -8888", 13);
77 *height = (7 * scr->info_text_font->height) / 4 - 1;
78 #else
79 *width = XTextWidth(scr->info_text_font->font, "-8888 x -8888", 13);
80 *height = (7 * scr->info_text_font->font->ascent) / 4 - 1;
81 #endif
86 *----------------------------------------------------------------------
87 * moveGeometryDisplayCentered
89 * routine that moves the geometry/position window on scr so it is
90 * centered over the given coordinates (x,y). Also the window position
91 * is clamped so it stays on the screen at all times.
92 *----------------------------------------------------------------------
94 static void
95 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
97 x -= scr->geometry_display_width / 2;
98 y -= scr->geometry_display_height / 2;
100 if (x < 1)
101 x = 1;
102 else if (x > (scr->scr_width - scr->geometry_display_width - 3))
103 x = scr->scr_width - scr->geometry_display_width - 3;
105 if (y < 1)
106 y = 1;
107 else if (y > (scr->scr_height - scr->geometry_display_height - 3))
108 y = scr->scr_height - scr->geometry_display_height - 3;
110 XMoveWindow(dpy, scr->geometry_display, x, y);
114 static void
115 showPosition(WWindow *wwin, int x, int y)
117 WScreen *scr = wwin->screen_ptr;
118 GC gc = scr->info_text_gc;
119 char num[16];
120 int fw, fh;
122 if (wPreferences.move_display == WDIS_NEW) {
123 #if 1
124 int width = wwin->frame->core->width;
125 int height = wwin->frame->core->height;
127 GC lgc = scr->line_gc;
128 XSetForeground(dpy, lgc, scr->line_pixel);
129 sprintf(num, "%i", x);
131 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
132 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
133 y+height+2);
134 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
135 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
136 scr->scr_height);
137 #endif
138 } else {
139 XClearArea(dpy, scr->geometry_display, 1, 1,
140 scr->geometry_display_width-2, scr->geometry_display_height-2,
141 False);
142 sprintf(num, "%+i %-+i", x, y);
143 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
145 XSetForeground(dpy, gc, scr->black_pixel);
147 fh = scr->info_text_font->height;
148 wDrawString(scr->geometry_display, scr->info_text_font, gc,
149 (scr->geometry_display_width - 2 - fw) / 2,
150 (scr->geometry_display_height-fh)/2 + scr->info_text_font->y,
151 num, strlen(num));
152 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
153 scr->geometry_display_height+1,
154 scr->widget_texture, WREL_RAISED);
159 static void
160 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
162 WScreen *scr = wwin->screen_ptr;
164 wPreferences.move_display++;
165 wPreferences.move_display %= NUM_DISPLAYS;
167 if (wPreferences.move_display == WDIS_NEW) {
168 XUnmapWindow(dpy, scr->geometry_display);
169 } else {
170 if (wPreferences.move_display == WDIS_CENTER) {
171 moveGeometryDisplayCentered(scr,
172 scr->scr_width/2, scr->scr_height/2);
173 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
174 moveGeometryDisplayCentered(scr, 1, 1);
175 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
176 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
178 XMapRaised(dpy, scr->geometry_display);
183 static void
184 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
186 WScreen *scr = wwin->screen_ptr;
188 if (wPreferences.move_display == WDIS_NEW) {
189 return;
190 } else if (wPreferences.move_display == WDIS_CENTER) {
191 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
192 scr->scr_height / 2);
193 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
194 moveGeometryDisplayCentered(scr, 1, 1);
195 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
196 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
198 XMapRaised(dpy, scr->geometry_display);
199 showPosition(wwin, x, y);
202 #define unmapPositionDisplay(w) \
203 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
206 static void
207 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
209 WScreen *scr = wwin->screen_ptr;
210 Window root = scr->root_win;
211 GC gc = scr->line_gc;
212 int ty, by, my, x, y, mx, s;
213 char num[16];
214 XSegment segment[4];
215 int fw, fh;
217 ty = y1 + wwin->frame->top_width;
218 by = y2 - wwin->frame->bottom_width;
219 fw = wTextWidth(scr->info_text_font->font, "8888", 4);
220 fh = scr->info_text_font->height;
222 if (wPreferences.size_display == WDIS_NEW) {
223 XSetForeground(dpy, gc, scr->line_pixel);
225 /* vertical geometry */
226 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
227 x = x2;
228 s = -15;
229 } else {
230 x = x1;
231 s = 15;
233 my = (ty + by) / 2;
235 /* top arrow */
236 /* end bar */
237 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
238 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
240 /* arrowhead */
241 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
242 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
244 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
245 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
247 /* line */
248 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
249 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
251 XDrawSegments(dpy, root, gc, segment, 4);
253 /* bottom arrow */
254 /* end bar */
255 segment[0].y1 = by;
256 segment[0].y2 = by;
258 /* arrowhead */
259 segment[1].y1 = by - 1;
260 segment[1].y2 = by - 7;
262 segment[2].y1 = by - 1;
263 segment[2].y2 = by - 7;
265 /* line */
266 segment[3].y1 = my + fh/2 + 2;
267 segment[3].y2 = by - 1;
269 XDrawSegments(dpy, root, gc, segment, 4);
271 sprintf(num, "%i", (by - ty - wwin->normal_hints->base_height) /
272 wwin->normal_hints->height_inc);
273 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
275 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
277 /* Display the height. */
278 wDrawString(root, scr->info_text_font, gc,
279 x - s + 3 - fw/2, my - fh/2 + scr->info_text_font->y + 1,
280 num, strlen(num));
281 XSetForeground(dpy, gc, scr->line_pixel);
282 /* horizontal geometry */
283 if (y1 < 15) {
284 y = y2;
285 s = -15;
286 } else {
287 y = y1;
288 s = 15;
290 mx = x1 + (x2 - x1)/2;
291 sprintf(num, "%i", (x2 - x1 - wwin->normal_hints->base_width) /
292 wwin->normal_hints->width_inc);
293 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
295 /* left arrow */
296 /* end bar */
297 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
298 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
300 /* arrowhead */
301 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
302 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
304 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
305 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
307 /* line */
308 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
309 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
311 XDrawSegments(dpy, root, gc, segment, 4);
313 /* right arrow */
314 /* end bar */
315 segment[0].x1 = x2 + 1;
316 segment[0].x2 = x2 + 1;
318 /* arrowhead */
319 segment[1].x1 = x2 - 6;
320 segment[1].x2 = x2;
322 segment[2].x1 = x2;
323 segment[2].x2 = x2 - 6;
325 /* line */
326 segment[3].x1 = mx + fw/2 + 2;
327 segment[3].x2 = x2;
329 XDrawSegments(dpy, root, gc, segment, 4);
331 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
333 /* Display the width. */
334 wDrawString(root, scr->info_text_font, gc,
335 mx - fw/2 + 1, y - s + fh/2 + 1, num, strlen(num));
336 } else {
337 XClearArea(dpy, scr->geometry_display, 1, 1,
338 scr->geometry_display_width-2, scr->geometry_display_height-2,
339 False);
340 sprintf(num, "%i x %-i", (x2 - x1 - wwin->normal_hints->base_width)
341 / wwin->normal_hints->width_inc,
342 (by - ty - wwin->normal_hints->base_height)
343 / wwin->normal_hints->height_inc);
344 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
346 XSetForeground(dpy, scr->info_text_gc, scr->black_pixel);
348 /* Display the height. */
349 wDrawString(scr->geometry_display, scr->info_text_font,
350 scr->info_text_gc,
351 (scr->geometry_display_width-fw)/2,
352 (scr->geometry_display_height-fh)/2 +scr->info_text_font->y,
353 num, strlen(num));
354 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
355 scr->geometry_display_height+1,
356 scr->widget_texture, WREL_RAISED);
361 static void
362 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
364 WScreen *scr = wwin->screen_ptr;
366 wPreferences.size_display++;
367 wPreferences.size_display %= NUM_DISPLAYS;
369 if (wPreferences.size_display == WDIS_NEW) {
370 XUnmapWindow(dpy, scr->geometry_display);
371 } else {
372 if (wPreferences.size_display == WDIS_CENTER) {
373 moveGeometryDisplayCentered(scr,
374 scr->scr_width / 2, 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 XMapRaised(dpy, scr->geometry_display);
381 showGeometry(wwin, x, y, x + w, y + h, dir);
386 static void
387 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
389 WScreen *scr = wwin->screen_ptr;
391 if (wPreferences.size_display == WDIS_NEW)
392 return;
394 if (wPreferences.size_display == WDIS_CENTER) {
395 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
396 scr->scr_height / 2);
397 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
398 moveGeometryDisplayCentered(scr, 1, 1);
399 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
400 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
402 XMapRaised(dpy, scr->geometry_display);
403 showGeometry(wwin, x, y, x + w, y + h, 0);
406 #define unmapGeometryDisplay(w) \
407 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
410 static void
411 doWindowMove(WWindow *wwin, LinkedList *list, int dx, int dy)
413 WWindow *tmpw;
414 int x, y;
415 int scr_width = wwin->screen_ptr->scr_width;
416 int scr_height = wwin->screen_ptr->scr_height;
418 if (!list) {
419 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
420 } else {
421 while (list) {
422 tmpw = list->head;
423 x = tmpw->frame_x + dx;
424 y = tmpw->frame_y + dy;
426 /* don't let windows become unreachable */
428 if (x + (int)tmpw->frame->core->width < 20)
429 x = 20 - (int)tmpw->frame->core->width;
430 else if (x + 20 > scr_width)
431 x = scr_width - 20;
433 if (y + (int)tmpw->frame->core->height < 20)
434 y = 20 - (int)tmpw->frame->core->height;
435 else if (y + 20 > scr_height)
436 y = scr_height - 20;
438 wWindowMove(tmpw, x, y);
439 list = list->tail;
445 static void
446 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
448 Window root = wwin->screen_ptr->root_win;
449 GC gc = wwin->screen_ptr->frame_gc;
450 int h = 0;
451 int bottom = 0;
453 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
454 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
456 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
457 /* Can't use wwin-frame->bottom_width because, in some cases
458 (e.g. interactive placement), frame does not point to anything. */
459 bottom = RESIZEBAR_HEIGHT - 1;
461 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
463 if (h > 0) {
464 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
466 if (bottom > 0) {
467 XDrawLine(dpy, root, gc, x + 1,
468 y + height - bottom,
469 x + width + 1,
470 y + height - bottom);
475 static void
476 drawFrames(WWindow *wwin, LinkedList *list, int dx, int dy)
478 WWindow *tmpw;
479 int scr_width = wwin->screen_ptr->scr_width;
480 int scr_height = wwin->screen_ptr->scr_height;
481 int x, y;
483 if (!list) {
485 x = wwin->frame_x + dx;
486 y = wwin->frame_y + dy;
488 drawTransparentFrame(wwin, x, y,
489 wwin->frame->core->width,
490 wwin->frame->core->height);
492 } else {
493 while (list) {
494 tmpw = list->head;
495 x = tmpw->frame_x + dx;
496 y = tmpw->frame_y + dy;
498 /* don't let windows become unreachable */
500 if (x + (int)tmpw->frame->core->width < 20)
501 x = 20 - (int)tmpw->frame->core->width;
502 else if (x + 20 > scr_width)
503 x = scr_width - 20;
505 if (y + (int)tmpw->frame->core->height < 20)
506 y = 20 - (int)tmpw->frame->core->height;
507 else if (y + 20 > scr_height)
508 y = scr_height - 20;
510 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
511 tmpw->frame->core->height);
513 list = list->tail;
520 static void
521 flushMotion()
523 XEvent ev;
525 XSync(dpy, False);
526 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
530 static void
531 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
532 int new_workspace, int rewind)
534 /* do not let window be unmapped */
535 if (opaque_move) {
536 wwin->flags.changing_workspace = 1;
537 wWindowChangeWorkspace(wwin, new_workspace);
539 /* go to new workspace */
540 wWorkspaceChange(scr, new_workspace);
542 wwin->flags.changing_workspace = 0;
544 if (rewind)
545 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
546 else
547 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
549 flushMotion();
551 if (!opaque_move) {
552 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
553 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
554 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
561 typedef struct {
562 /* arrays of WWindows sorted by the respective border position */
563 WWindow **topList; /* top border */
564 WWindow **leftList; /* left border */
565 WWindow **rightList; /* right border */
566 WWindow **bottomList; /* bottom border */
567 int count;
569 /* index of window in the above lists indicating the relative position
570 * of the window with the others */
571 int topIndex;
572 int leftIndex;
573 int rightIndex;
574 int bottomIndex;
576 int rubCount; /* for workspace switching */
578 int winWidth, winHeight; /* width/height of the window */
579 int realX, realY; /* actual position of the window */
580 int calcX, calcY; /* calculated position of window */
581 int omouseX, omouseY; /* old mouse position */
582 int mouseX, mouseY; /* last known position of the pointer */
583 } MoveData;
585 #define WTOP(w) (w)->frame_y
586 #define WLEFT(w) (w)->frame_x
587 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
588 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
590 static int
591 compareWTop(const void *a, const void *b)
593 WWindow *wwin1 = *(WWindow**)a;
594 WWindow *wwin2 = *(WWindow**)b;
596 if (WTOP(wwin1) > WTOP(wwin2))
597 return -1;
598 else if (WTOP(wwin1) < WTOP(wwin2))
599 return 1;
600 else
601 return 0;
605 static int
606 compareWLeft(const void *a, const void *b)
608 WWindow *wwin1 = *(WWindow**)a;
609 WWindow *wwin2 = *(WWindow**)b;
611 if (WLEFT(wwin1) > WLEFT(wwin2))
612 return -1;
613 else if (WLEFT(wwin1) < WLEFT(wwin2))
614 return 1;
615 else
616 return 0;
620 static int
621 compareWRight(const void *a, const void *b)
623 WWindow *wwin1 = *(WWindow**)a;
624 WWindow *wwin2 = *(WWindow**)b;
626 if (WRIGHT(wwin1) < WRIGHT(wwin2))
627 return -1;
628 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
629 return 1;
630 else
631 return 0;
636 static int
637 compareWBottom(const void *a, const void *b)
639 WWindow *wwin1 = *(WWindow**)a;
640 WWindow *wwin2 = *(WWindow**)b;
642 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
643 return -1;
644 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
645 return 1;
646 else
647 return 0;
651 static void
652 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
654 int i;
655 int newX2 = newX + data->winWidth;
656 int newY2 = newY + data->winHeight;
657 Bool ok = False;
659 if (newX < data->realX) {
660 if (data->rightIndex > 0
661 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
662 ok = True;
663 } else if (data->leftIndex <= data->count-1
664 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
665 ok = True;
667 } else if (newX > data->realX) {
668 if (data->leftIndex > 0
669 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
670 ok = True;
671 } else if (data->rightIndex <= data->count-1
672 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
673 ok = True;
677 if (!ok) {
678 if (newY < data->realY) {
679 if (data->bottomIndex > 0
680 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
681 ok = True;
682 } else if (data->topIndex <= data->count-1
683 && newY2 <= WTOP(data->topList[data->topIndex])) {
684 ok = True;
686 } else if (newY > data->realY) {
687 if (data->topIndex > 0
688 && newY2 > WTOP(data->topList[data->topIndex-1])) {
689 ok = True;
690 } else if (data->bottomIndex <= data->count-1
691 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
692 ok = True;
697 if (!ok)
698 return;
700 /* TODO: optimize this */
701 if (data->realY < WBOTTOM(data->bottomList[0])) {
702 data->bottomIndex = 0;
704 if (data->realX < WRIGHT(data->rightList[0])) {
705 data->rightIndex = 0;
707 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
708 data->leftIndex = 0;
710 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
711 data->topIndex = 0;
713 for (i = 0; i < data->count; i++) {
714 if (data->realY > WBOTTOM(data->bottomList[i])) {
715 data->bottomIndex = i + 1;
717 if (data->realX > WRIGHT(data->rightList[i])) {
718 data->rightIndex = i + 1;
720 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
721 data->leftIndex = i + 1;
723 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
724 data->topIndex = i + 1;
730 static void
731 freeMoveData(MoveData *data)
733 if (data->topList)
734 free(data->topList);
735 if (data->leftList)
736 free(data->leftList);
737 if (data->rightList)
738 free(data->rightList);
739 if (data->bottomList)
740 free(data->bottomList);
744 static void
745 updateMoveData(WWindow *wwin, MoveData *data)
747 WScreen *scr = wwin->screen_ptr;
748 WWindow *tmp;
749 int i;
751 data->count = 0;
752 tmp = scr->focused_window;
753 while (tmp) {
754 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
755 && !tmp->flags.miniaturized
756 && !tmp->flags.hidden
757 && !tmp->flags.obscured) {
758 data->topList[data->count] = tmp;
759 data->leftList[data->count] = tmp;
760 data->rightList[data->count] = tmp;
761 data->bottomList[data->count] = tmp;
762 data->count++;
764 tmp = tmp->prev;
767 if (data->count == 0) {
768 data->topIndex = 0;
769 data->leftIndex = 0;
770 data->rightIndex = 0;
771 data->bottomIndex = 0;
772 return;
776 * order from closest to the border of the screen to farthest
778 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
779 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
780 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
781 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
783 /* figure the position of the window relative to the others */
785 data->topIndex = -1;
786 data->leftIndex = -1;
787 data->rightIndex = -1;
788 data->bottomIndex = -1;
790 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
791 data->bottomIndex = 0;
793 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
794 data->rightIndex = 0;
796 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
797 data->leftIndex = 0;
799 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
800 data->topIndex = 0;
802 for (i = 0; i < data->count; i++) {
803 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
804 data->bottomIndex = i + 1;
806 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
807 data->rightIndex = i + 1;
809 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
810 data->leftIndex = i + 1;
812 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
813 data->topIndex = i + 1;
819 static void
820 initMoveData(WWindow *wwin, MoveData *data)
822 int i;
823 WWindow *tmp;
825 memset(data, 0, sizeof(MoveData));
827 for (i = 0, tmp = wwin->screen_ptr->focused_window;
828 tmp != NULL;
829 tmp = tmp->prev, i++);
831 if (i > 1) {
832 data->topList = wmalloc(sizeof(WWindow*) * i);
833 data->leftList = wmalloc(sizeof(WWindow*) * i);
834 data->rightList = wmalloc(sizeof(WWindow*) * i);
835 data->bottomList = wmalloc(sizeof(WWindow*) * i);
837 updateMoveData(wwin, data);
840 data->realX = wwin->frame_x;
841 data->realY = wwin->frame_y;
842 data->calcX = wwin->frame_x;
843 data->calcY = wwin->frame_y;
845 data->winWidth = wwin->frame->core->width + 2;
846 data->winHeight = wwin->frame->core->height + 2;
850 static Bool
851 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
853 WScreen *scr = wwin->screen_ptr;
854 Bool changed = False;
856 if (data->mouseX <= 1) {
857 if (scr->current_workspace > 0) {
859 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
860 True);
861 changed = True;
862 data->rubCount = 0;
864 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
866 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
867 True);
868 changed = True;
869 data->rubCount = 0;
871 } else if (data->mouseX >= scr->scr_width - 2) {
873 if (scr->current_workspace == scr->workspace_count - 1) {
875 if (wPreferences.ws_cycle
876 || scr->workspace_count == MAX_WORKSPACES) {
878 crossWorkspace(scr, wwin, opaqueMove, 0, False);
879 changed = True;
880 data->rubCount = 0;
882 /* if user insists on trying to go to next workspace even when
883 * it's already the last, create a new one */
884 else if (data->omouseX == data->mouseX
885 && wPreferences.ws_advance) {
887 /* detect user "rubbing" the window against the edge */
888 if (data->rubCount > 0
889 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
891 data->rubCount = -(data->rubCount + 1);
893 } else if (data->rubCount <= 0
894 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
896 data->rubCount = -data->rubCount + 1;
899 /* create a new workspace */
900 if (abs(data->rubCount) > 2) {
901 /* go to next workspace */
902 wWorkspaceNew(scr);
904 crossWorkspace(scr, wwin, opaqueMove,
905 scr->current_workspace+1, False);
906 changed = True;
907 data->rubCount = 0;
909 } else if (scr->current_workspace < scr->workspace_count) {
911 /* go to next workspace */
912 crossWorkspace(scr, wwin, opaqueMove,
913 scr->current_workspace+1, False);
914 changed = True;
915 data->rubCount = 0;
917 } else {
918 data->rubCount = 0;
921 return changed;
925 static void
926 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
927 Bool opaqueMove, int newMouseX, int newMouseY)
929 WScreen *scr = wwin->screen_ptr;
930 int dx, dy; /* how much mouse moved */
931 int winL, winR, winT, winB; /* requested new window position */
932 int newX, newY; /* actual new window position */
933 Bool hresist, vresist;
934 Bool updateIndex;
936 hresist = False;
937 vresist = False;
939 updateIndex = False;
941 /* check the direction of the movement */
942 dx = newMouseX - data->mouseX;
943 dy = newMouseY - data->mouseY;
945 data->omouseX = data->mouseX;
946 data->omouseY = data->mouseY;
947 data->mouseX = newMouseX;
948 data->mouseY = newMouseY;
950 winL = data->calcX + dx;
951 winR = data->calcX + data->winWidth + dx;
952 winT = data->calcY + dy;
953 winB = data->calcY + data->winHeight + dy;
955 newX = data->realX;
956 newY = data->realY;
958 if (doResistance) {
959 int edge;
960 int resist;
961 WWindow *rwin;
963 resist = wPreferences.edge_resistance;
964 /* horizontal movement: check horizontal edge resistances */
965 if (dx < 0) {
966 /* window is the leftmost window: check against screen edge */
967 edge = scr->totalUsableArea.x1;
969 /* check position of nearest window to the left */
970 if (data->rightIndex > 0) {
971 /* there is some window at the left: check if it will block
972 * the window */
973 rwin = data->rightList[data->rightIndex - 1];
975 if (data->realY > WBOTTOM(rwin)
976 || (data->realY + data->winHeight) < WTOP(rwin)) {
977 resist = 0;
978 } else {
979 edge = WRIGHT(rwin) + 1;
980 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
983 if (resist > 0 && winL >= edge - resist && winL <= edge) {
984 newX = edge;
985 hresist = True;
987 } else if (dx > 0) {
988 /* window is the rightmost window: check against screen edge */
989 edge = scr->totalUsableArea.x2;
991 /* check position of nearest window to the right */
992 if (data->leftIndex > 0) {
993 /* there is some window at the right: check if it will block
994 * the window */
995 rwin = data->leftList[data->leftIndex - 1];
997 if (data->realY > WBOTTOM(rwin)
998 || (data->realY + data->winHeight) < WTOP(rwin)) {
999 resist = 0;
1000 } else {
1001 edge = WLEFT(rwin);
1002 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1005 if (resist > 0 && winR <= edge + resist && winR >= edge) {
1006 newX = edge - data->winWidth;
1007 hresist = True;
1011 resist = wPreferences.edge_resistance;
1012 /* vertical movement: check vertical edge resistances */
1013 if (dy < 0) {
1014 /* window is the topmost window: check against screen edge */
1015 edge = scr->totalUsableArea.y1;
1017 /* check position of nearest window to the top */
1018 if (data->bottomIndex > 0) {
1019 /* there is some window at the top: check if it will block
1020 * the window */
1021 rwin = data->bottomList[data->bottomIndex - 1];
1023 if (data->realX > WRIGHT(rwin)
1024 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1025 resist = 0;
1026 } else {
1027 edge = WBOTTOM(rwin) + 1;
1028 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1031 if (resist > 0 && winT >= edge - resist && winT <= edge) {
1032 newY = edge;
1033 vresist = True;
1035 } else if (dy > 0) {
1036 /* window is the bottommost window: check against screen edge */
1037 edge = scr->totalUsableArea.y2;
1039 /* check position of nearest window to the bottom */
1040 if (data->topIndex > 0) {
1041 /* there is some window at the bottom: check if it will block
1042 * the window */
1043 rwin = data->topList[data->topIndex - 1];
1045 if (data->realX > WRIGHT(rwin)
1046 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1047 resist = 0;
1048 } else {
1049 edge = WTOP(rwin);
1050 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1053 if (resist > 0 && winB <= edge + resist && winB >= edge) {
1054 newY = edge - data->winHeight;
1055 vresist = True;
1060 /* update window position */
1061 data->calcX += dx;
1062 data->calcY += dy;
1064 if (((dx > 0 && data->calcX - data->realX > 0)
1065 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1066 newX = data->calcX;
1068 if (((dy > 0 && data->calcY - data->realY > 0)
1069 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1070 newY = data->calcY;
1072 if (data->realX != newX || data->realY != newY) {
1074 if (wPreferences.move_display == WDIS_NEW
1075 && !scr->selected_windows) {
1076 showPosition(wwin, data->realX, data->realY);
1078 if (opaqueMove) {
1079 doWindowMove(wwin, scr->selected_windows,
1080 newX - wwin->frame_x,
1081 newY - wwin->frame_y);
1082 } else {
1083 /* erase frames */
1084 drawFrames(wwin, scr->selected_windows,
1085 data->realX - wwin->frame_x,
1086 data->realY - wwin->frame_y);
1089 if (!scr->selected_windows
1090 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1092 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1093 newY + data->winHeight/2);
1096 if (!opaqueMove) {
1097 /* draw frames */
1098 drawFrames(wwin, scr->selected_windows,
1099 newX - wwin->frame_x,
1100 newY - wwin->frame_y);
1103 if (!scr->selected_windows) {
1104 showPosition(wwin, newX, newY);
1109 /* recalc relative window position */
1110 if (doResistance && (data->realX != newX || data->realY != newY)) {
1111 updateResistance(wwin, data, newX, newY);
1114 data->realX = newX;
1115 data->realY = newY;
1119 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1122 wKeyboardMoveResizeWindow(WWindow *wwin)
1124 WScreen *scr = wwin->screen_ptr;
1125 Window root = scr->root_win;
1126 XEvent event;
1127 int w = wwin->frame->core->width;
1128 int h = wwin->frame->core->height;
1129 int scr_width = wwin->screen_ptr->scr_width;
1130 int scr_height = wwin->screen_ptr->scr_height;
1131 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1132 int src_x = wwin->frame_x;
1133 int src_y = wwin->frame_y;
1134 int done,off_x,off_y,ww,wh;
1135 int kspeed = _KS;
1136 Time lastTime = 0;
1137 KeySym keysym=NoSymbol;
1138 int moment=0;
1139 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1141 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1142 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1143 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1144 ctrlmode=done=off_x=off_y=0;
1146 XSync(dpy, False);
1147 wusleep(10000);
1148 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1150 if (!wwin->flags.selected) {
1151 wUnselectWindows(scr);
1153 XGrabServer(dpy);
1154 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1155 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1156 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1158 if (wwin->flags.shaded || scr->selected_windows) {
1159 if(scr->selected_windows)
1160 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1161 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1162 if(!scr->selected_windows)
1163 mapPositionDisplay(wwin, src_x, src_y, w, h);
1164 } else {
1165 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1167 ww=w;
1168 wh=h;
1169 while(1) {
1171 looper.ox=off_x;
1172 looper.oy=off_y;
1174 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1175 | ButtonPressMask | ExposureMask, &event);
1176 if (wwin->flags.shaded || scr->selected_windows) {
1177 if(scr->selected_windows)
1178 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1179 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1180 /*** I HATE EDGE RESISTANCE - ]d ***/
1182 else {
1183 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1186 if(ctrlmode)
1187 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1189 XUngrabServer(dpy);
1190 XSync(dpy, False);
1192 switch (event.type) {
1193 case KeyPress:
1194 /* accelerate */
1195 if (event.xkey.time - lastTime > 50) {
1196 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1197 } else {
1198 if (kspeed < 20) {
1199 kspeed++;
1202 if (kspeed < _KS) kspeed = _KS;
1203 lastTime = event.xkey.time;
1205 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1206 ctrlmode=1;
1207 wUnselectWindows(scr);
1209 else {
1210 ctrlmode=0;
1212 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1213 if (ctrlmode)
1214 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1215 else
1216 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1218 else {
1220 keysym = XLookupKeysym(&event.xkey, 0);
1221 switch (keysym) {
1222 case XK_Return:
1223 done=2;
1224 break;
1225 case XK_Escape:
1226 done=1;
1227 break;
1228 case XK_Up:
1229 case XK_KP_Up:
1230 case XK_k:
1231 if (ctrlmode){
1232 if (moment != UP)
1233 h = wh;
1234 h-=kspeed;
1235 moment = UP;
1236 if (h < 1) h = 1;
1238 else off_y-=kspeed;
1239 break;
1240 case XK_Down:
1241 case XK_KP_Down:
1242 case XK_j:
1243 if (ctrlmode){
1244 if (moment != DOWN)
1245 h = wh;
1246 h+=kspeed;
1247 moment = DOWN;
1249 else off_y+=kspeed;
1250 break;
1251 case XK_Left:
1252 case XK_KP_Left:
1253 case XK_h:
1254 if (ctrlmode) {
1255 if (moment != LEFT)
1256 w = ww;
1257 w-=kspeed;
1258 if (w < 1) w = 1;
1259 moment = LEFT;
1261 else off_x-=kspeed;
1262 break;
1263 case XK_Right:
1264 case XK_KP_Right:
1265 case XK_l:
1266 if (ctrlmode) {
1267 if (moment != RIGHT)
1268 w = ww;
1269 w+=kspeed;
1270 moment = RIGHT;
1272 else off_x+=kspeed;
1273 break;
1276 ww=w;wh=h;
1277 wh-=vert_border;
1278 wWindowConstrainSize(wwin, &ww, &wh);
1279 wh+=vert_border;
1281 if (wPreferences.ws_cycle){
1282 if (src_x + off_x + ww < 20){
1283 if(!scr->current_workspace) {
1284 wWorkspaceChange(scr, scr->workspace_count-1);
1286 else wWorkspaceChange(scr, scr->current_workspace-1);
1287 off_x += scr_width;
1289 else if (src_x + off_x + 20 > scr_width){
1290 if(scr->current_workspace == scr->workspace_count-1) {
1291 wWorkspaceChange(scr, 0);
1293 else wWorkspaceChange(scr, scr->current_workspace+1);
1294 off_x -= scr_width;
1297 else {
1298 if (src_x + off_x + ww < 20)
1299 off_x = 20 - ww - src_x;
1300 else if (src_x + off_x + 20 > scr_width)
1301 off_x = scr_width - 20 - src_x;
1304 if (src_y + off_y + wh < 20) {
1305 off_y = 20 - wh - src_y;
1307 else if (src_y + off_y + 20 > scr_height) {
1308 off_y = scr_height - 20 - src_y;
1311 break;
1312 case ButtonPress:
1313 case ButtonRelease:
1314 done=1;
1315 break;
1316 default:
1317 WMHandleEvent(&event);
1318 break;
1321 XGrabServer(dpy);
1322 /*xxx*/
1324 if (wwin->flags.shaded && !scr->selected_windows){
1325 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1327 else {
1328 if(ctrlmode){
1329 unmapPositionDisplay(wwin);
1330 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1332 else if(!scr->selected_windows){
1333 unmapGeometryDisplay(wwin);
1334 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1338 if (wwin->flags.shaded || scr->selected_windows) {
1339 if(scr->selected_windows)
1340 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1341 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1343 else {
1344 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1348 if(ctrlmode){
1349 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1351 else if(!scr->selected_windows)
1352 showPosition(wwin, src_x+off_x, src_y+off_y);
1353 /**/
1355 if(done){
1356 scr->keymove_tick=0;
1358 WMDeleteTimerWithClientData(&looper);
1360 if (wwin->flags.shaded || scr->selected_windows) {
1361 if(scr->selected_windows)
1362 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1363 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1365 else {
1366 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1369 if(ctrlmode){
1370 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1371 unmapGeometryDisplay(wwin);
1373 else
1374 unmapPositionDisplay(wwin);
1375 XUngrabKeyboard(dpy, CurrentTime);
1376 XUngrabPointer(dpy, CurrentTime);
1377 XUngrabServer(dpy);
1379 if(done==2) {
1380 if (wwin->flags.shaded || scr->selected_windows) {
1381 LinkedList *list;
1382 list=scr->selected_windows;
1383 if (!scr->selected_windows) {
1384 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1385 wWindowSynthConfigureNotify(wwin);
1386 } else {
1387 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1388 while (list) {
1389 wWindowSynthConfigureNotify(list->head);
1390 list = list->tail;
1393 } else {
1394 if (wwin->client.width != ww)
1395 wwin->flags.user_changed_width = 1;
1397 if (wwin->client.height != wh - vert_border)
1398 wwin->flags.user_changed_height = 1;
1400 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1401 ww, wh - vert_border);
1402 wWindowSynthConfigureNotify(wwin);
1404 wWindowChangeWorkspace(wwin, scr->current_workspace);
1405 wSetFocusTo(scr, wwin);
1407 return 1;
1414 *----------------------------------------------------------------------
1415 * wMouseMoveWindow--
1416 * Move the named window and the other selected ones (if any),
1417 * interactively. Also shows the position of the window, if only one
1418 * window is being moved.
1419 * If the window is not on the selected window list, the selected
1420 * windows are deselected.
1421 * If shift is pressed during the operation, the position display
1422 * is changed to another type.
1424 * Returns:
1425 * True if the window was moved, False otherwise.
1427 * Side effects:
1428 * The window(s) position is changed, and the client(s) are
1429 * notified about that.
1430 * The position display configuration may be changed.
1431 *----------------------------------------------------------------------
1434 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1436 WScreen *scr = wwin->screen_ptr;
1437 XEvent event;
1438 Window root = scr->root_win;
1439 KeyCode shiftl, shiftr;
1440 Bool done = False;
1441 int started = 0;
1442 int warped = 0;
1443 /* This needs not to change while moving, else bad things can happen */
1444 int opaqueMove = wPreferences.opaque_move;
1445 MoveData moveData;
1446 #ifdef GHOST_WINDOW_MOVE
1447 RImage *rimg;
1449 rimg = InitGhostWindowMove(scr);
1450 #endif
1452 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1453 XSetWindowAttributes attr;
1455 attr.save_under = True;
1456 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1457 CWSaveUnder, &attr);
1460 initMoveData(wwin, &moveData);
1462 moveData.mouseX = ev->xmotion.x_root;
1463 moveData.mouseY = ev->xmotion.y_root;
1465 if (!wwin->flags.selected) {
1466 /* this window is not selected, unselect others and move only wwin */
1467 wUnselectWindows(scr);
1469 #ifdef DEBUG
1470 puts("Moving window");
1471 #endif
1472 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1473 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1474 while (!done) {
1475 if (warped) {
1476 int junk;
1477 Window junkw;
1479 /* XWarpPointer() doesn't seem to generate Motion events, so
1480 we've got to simulate them */
1481 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1482 &event.xmotion.y_root, &junk, &junk,
1483 (unsigned *) &junk);
1484 } else {
1485 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1486 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1487 &event);
1489 if (event.type == MotionNotify) {
1490 /* compress MotionNotify events */
1491 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1494 switch (event.type) {
1495 case KeyPress:
1496 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1497 && started && !scr->selected_windows) {
1499 if (!opaqueMove) {
1500 drawFrames(wwin, scr->selected_windows,
1501 moveData.realX - wwin->frame_x,
1502 moveData.realY - wwin->frame_y);
1505 if (wPreferences.move_display == WDIS_NEW
1506 && !scr->selected_windows) {
1507 showPosition(wwin, moveData.realX, moveData.realY);
1508 XUngrabServer(dpy);
1510 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1511 moveData.winWidth, moveData.winHeight);
1513 if (wPreferences.move_display == WDIS_NEW
1514 && !scr->selected_windows) {
1515 XGrabServer(dpy);
1516 showPosition(wwin, moveData.realX, moveData.realY);
1519 if (!opaqueMove) {
1520 drawFrames(wwin, scr->selected_windows,
1521 moveData.realX - wwin->frame_x,
1522 moveData.realY - wwin->frame_y);
1525 break;
1527 case MotionNotify:
1528 if (started) {
1529 updateWindowPosition(wwin, &moveData,
1530 scr->selected_windows == NULL
1531 && wPreferences.edge_resistance > 0,
1532 opaqueMove,
1533 event.xmotion.x_root,
1534 event.xmotion.y_root);
1536 if (!warped && !wPreferences.no_autowrap) {
1537 int oldWorkspace = scr->current_workspace;
1539 if (wPreferences.move_display == WDIS_NEW
1540 && !scr->selected_windows) {
1541 showPosition(wwin, moveData.realX, moveData.realY);
1542 XUngrabServer(dpy);
1544 if (!opaqueMove) {
1545 drawFrames(wwin, scr->selected_windows,
1546 moveData.realX - wwin->frame_x,
1547 moveData.realY - wwin->frame_y);
1549 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1550 if (scr->current_workspace != oldWorkspace
1551 && wPreferences.edge_resistance > 0
1552 && scr->selected_windows == NULL)
1553 updateMoveData(wwin, &moveData);
1554 warped = 1;
1556 if (!opaqueMove) {
1557 drawFrames(wwin, scr->selected_windows,
1558 moveData.realX - wwin->frame_x,
1559 moveData.realY - wwin->frame_y);
1561 if (wPreferences.move_display == WDIS_NEW
1562 && !scr->selected_windows) {
1563 XSync(dpy, False);
1564 showPosition(wwin, moveData.realX, moveData.realY);
1565 XGrabServer(dpy);
1567 } else {
1568 warped = 0;
1570 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1571 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1573 XChangeActivePointerGrab(dpy, ButtonMotionMask
1574 | ButtonReleaseMask | ButtonPressMask,
1575 wCursor[WCUR_MOVE], CurrentTime);
1576 started = 1;
1577 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1578 CurrentTime);
1580 if (!scr->selected_windows)
1581 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1582 moveData.winWidth, moveData.winHeight);
1584 if (started && !opaqueMove)
1585 drawFrames(wwin, scr->selected_windows, 0, 0);
1587 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1588 && !scr->selected_windows)) {
1589 XGrabServer(dpy);
1590 if (wPreferences.move_display==WDIS_NEW)
1591 showPosition(wwin, moveData.realX, moveData.realY);
1594 break;
1596 case ButtonPress:
1597 break;
1599 case ButtonRelease:
1600 if (event.xbutton.button != ev->xbutton.button)
1601 break;
1603 if (started) {
1604 if (!opaqueMove) {
1605 drawFrames(wwin, scr->selected_windows,
1606 moveData.realX - wwin->frame_x,
1607 moveData.realY - wwin->frame_y);
1608 XSync(dpy, 0);
1609 doWindowMove(wwin, scr->selected_windows,
1610 moveData.realX - wwin->frame_x,
1611 moveData.realY - wwin->frame_y);
1613 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1614 wWindowSynthConfigureNotify(wwin);
1615 #endif
1616 XUngrabKeyboard(dpy, CurrentTime);
1617 XUngrabServer(dpy);
1618 if (!opaqueMove) {
1619 wWindowChangeWorkspace(wwin, scr->current_workspace);
1620 wSetFocusTo(scr, wwin);
1622 if (wPreferences.move_display == WDIS_NEW)
1623 showPosition(wwin, moveData.realX, moveData.realY);
1625 if (!scr->selected_windows) {
1626 /* get rid of the geometry window */
1627 unmapPositionDisplay(wwin);
1630 #ifdef DEBUG
1631 puts("End move window");
1632 #endif
1633 done = True;
1634 break;
1636 default:
1637 if (started && !opaqueMove) {
1638 drawFrames(wwin, scr->selected_windows,
1639 moveData.realX - wwin->frame_x,
1640 moveData.realY - wwin->frame_y);
1641 XUngrabServer(dpy);
1642 WMHandleEvent(&event);
1643 XSync(dpy, False);
1644 XGrabServer(dpy);
1645 drawFrames(wwin, scr->selected_windows,
1646 moveData.realX - wwin->frame_x,
1647 moveData.realY - wwin->frame_y);
1648 } else {
1649 WMHandleEvent(&event);
1651 break;
1655 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1656 XSetWindowAttributes attr;
1658 attr.save_under = False;
1659 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1660 CWSaveUnder, &attr);
1663 freeMoveData(&moveData);
1665 return started;
1669 #define RESIZEBAR 1
1670 #define HCONSTRAIN 2
1672 static int
1673 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1674 int flags)
1676 int w = wwin->frame->core->width - 1;
1677 int cw = wwin->frame->resizebar_corner_width;
1678 int dir;
1680 /* if not resizing through the resizebar */
1681 if (!(flags & RESIZEBAR)) {
1682 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1683 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1684 if (abs(dx) < 2 || abs(dy) < 2) {
1685 if (abs(dy) > abs(dx))
1686 xdir = 0;
1687 else
1688 ydir = 0;
1690 return (xdir | ydir);
1693 /* window is too narrow. allow diagonal resize */
1694 if (cw * 2 >= w) {
1695 int ydir;
1697 if (flags & HCONSTRAIN)
1698 ydir = 0;
1699 else
1700 ydir = DOWN;
1701 if (x < cw)
1702 return (LEFT | ydir);
1703 else
1704 return (RIGHT | ydir);
1706 /* vertical resize */
1707 if ((x > cw) && (x < w - cw))
1708 return DOWN;
1710 if (x < cw)
1711 dir = LEFT;
1712 else
1713 dir = RIGHT;
1715 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1716 dir |= DOWN;
1718 return dir;
1722 void
1723 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1725 XEvent event;
1726 WScreen *scr = wwin->screen_ptr;
1727 Window root = scr->root_win;
1728 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1729 int fw = wwin->frame->core->width;
1730 int fh = wwin->frame->core->height;
1731 int fx = wwin->frame_x;
1732 int fy = wwin->frame_y;
1733 int is_resizebar = (wwin->frame->resizebar
1734 && ev->xany.window==wwin->frame->resizebar->window);
1735 int orig_x, orig_y;
1736 int started;
1737 int dw, dh;
1738 int rw = fw, rh = fh;
1739 int rx1, ry1, rx2, ry2;
1740 int res = 0;
1741 KeyCode shiftl, shiftr;
1742 int h = 0;
1743 int orig_fx = fx;
1744 int orig_fy = fy;
1745 int orig_fw = fw;
1746 int orig_fh = fh;
1748 if (wwin->flags.shaded) {
1749 wwarning("internal error: tryein");
1750 return;
1752 orig_x = ev->xbutton.x_root;
1753 orig_y = ev->xbutton.y_root;
1755 started = 0;
1756 #ifdef DEBUG
1757 puts("Resizing window");
1758 #endif
1760 wUnselectWindows(scr);
1761 rx1 = fx;
1762 rx2 = fx + fw - 1;
1763 ry1 = fy;
1764 ry2 = fy + fh - 1;
1765 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1766 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1767 if (!WFLAGP(wwin, no_titlebar))
1768 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
1769 else
1770 h = 0;
1771 while (1) {
1772 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1773 | ButtonPressMask | ExposureMask, &event);
1774 switch (event.type) {
1775 case KeyPress:
1776 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1777 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1778 && started) {
1779 drawTransparentFrame(wwin, fx, fy, fw, fh);
1780 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1781 drawTransparentFrame(wwin, fx, fy, fw, fh);
1783 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1784 break;
1786 case MotionNotify:
1787 if (started) {
1788 dw = 0;
1789 dh = 0;
1791 orig_fx = fx;
1792 orig_fy = fy;
1793 orig_fw = fw;
1794 orig_fh = fh;
1796 if (res & LEFT)
1797 dw = orig_x - event.xmotion.x_root;
1798 else if (res & RIGHT)
1799 dw = event.xmotion.x_root - orig_x;
1800 if (res & UP)
1801 dh = orig_y - event.xmotion.y_root;
1802 else if (res & DOWN)
1803 dh = event.xmotion.y_root - orig_y;
1805 orig_x = event.xmotion.x_root;
1806 orig_y = event.xmotion.y_root;
1808 rw += dw;
1809 rh += dh;
1810 fw = rw;
1811 fh = rh - vert_border;
1812 wWindowConstrainSize(wwin, &fw, &fh);
1813 fh += vert_border;
1814 if (res & LEFT)
1815 fx = rx2 - fw + 1;
1816 else if (res & RIGHT)
1817 fx = rx1;
1818 if (res & UP)
1819 fy = ry2 - fh + 1;
1820 else if (res & DOWN)
1821 fy = ry1;
1822 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1823 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1824 int tx, ty;
1825 Window junkw;
1826 int flags;
1828 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1829 orig_x, orig_y, &tx, &ty, &junkw);
1831 /* check if resizing through resizebar */
1832 if (is_resizebar)
1833 flags = RESIZEBAR;
1834 else
1835 flags = 0;
1837 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1838 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1839 flags |= HCONSTRAIN;
1841 res = getResizeDirection(wwin, tx, ty,
1842 orig_x - event.xmotion.x_root,
1843 orig_y - event.xmotion.y_root, flags);
1845 XChangeActivePointerGrab(dpy, ButtonMotionMask
1846 | ButtonReleaseMask | ButtonPressMask,
1847 wCursor[WCUR_RESIZE], CurrentTime);
1848 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1849 CurrentTime);
1851 XGrabServer(dpy);
1853 /* Draw the resize frame for the first time. */
1854 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1856 drawTransparentFrame(wwin, fx, fy, fw, fh);
1858 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1860 started = 1;
1862 if (started) {
1863 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1864 drawTransparentFrame(wwin, orig_fx, orig_fy,
1865 orig_fw, orig_fh);
1866 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1867 drawTransparentFrame(wwin, fx, fy, fw, fh);
1868 } else {
1869 drawTransparentFrame(wwin, orig_fx, orig_fy,
1870 orig_fw, orig_fh);
1871 drawTransparentFrame(wwin, fx, fy, fw, fh);
1873 if (fh != orig_fh || fw != orig_fw) {
1874 if (wPreferences.size_display == WDIS_NEW) {
1875 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1876 orig_fy + orig_fh, res);
1878 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1881 break;
1883 case ButtonPress:
1884 break;
1886 case ButtonRelease:
1887 if (event.xbutton.button != ev->xbutton.button)
1888 break;
1890 if (started) {
1891 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1893 drawTransparentFrame(wwin, fx, fy, fw, fh);
1895 XUngrabKeyboard(dpy, CurrentTime);
1896 unmapGeometryDisplay(wwin);
1897 XUngrabServer(dpy);
1899 if (wwin->client.width != fw)
1900 wwin->flags.user_changed_width = 1;
1902 if (wwin->client.height != fh - vert_border)
1903 wwin->flags.user_changed_height = 1;
1905 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1907 #ifdef DEBUG
1908 puts("End resize window");
1909 #endif
1910 return;
1912 default:
1913 WMHandleEvent(&event);
1918 #undef LEFT
1919 #undef RIGHT
1920 #undef HORIZONTAL
1921 #undef UP
1922 #undef DOWN
1923 #undef VERTICAL
1924 #undef HCONSTRAIN
1925 #undef RESIZEBAR
1927 void
1928 wUnselectWindows(WScreen *scr)
1930 WWindow *wwin;
1932 while (scr->selected_windows) {
1933 wwin = scr->selected_windows->head;
1934 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
1935 wIconSelect(wwin->icon);
1937 wSelectWindow(wwin, False);
1941 #ifndef LITE
1942 static void
1943 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
1945 WWindow *tmpw;
1947 /* select the windows and put them in the selected window list */
1948 tmpw = scr->focused_window;
1949 while (tmpw != NULL) {
1950 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
1951 if ((tmpw->frame->workspace == scr->current_workspace
1952 || IS_OMNIPRESENT(tmpw))
1953 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
1954 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
1955 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
1956 wSelectWindow(tmpw, True);
1959 tmpw = tmpw->prev;
1964 void
1965 wSelectWindows(WScreen *scr, XEvent *ev)
1967 XEvent event;
1968 Window root = scr->root_win;
1969 GC gc = scr->frame_gc;
1970 int xp = ev->xbutton.x_root;
1971 int yp = ev->xbutton.y_root;
1972 int w = 0, h = 0;
1973 int x = xp, y = yp;
1975 #ifdef DEBUG
1976 puts("Selecting windows");
1977 #endif
1978 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
1979 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1980 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
1981 CurrentTime) != Success) {
1982 return;
1984 XGrabServer(dpy);
1986 wUnselectWindows(scr);
1988 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
1989 while (1) {
1990 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
1991 | ButtonPressMask, &event);
1993 switch (event.type) {
1994 case MotionNotify:
1995 XDrawRectangle(dpy, root, gc, x, y, w, h);
1996 x = event.xmotion.x_root;
1997 if (x < xp) {
1998 w = xp - x;
1999 } else {
2000 w = x - xp;
2001 x = xp;
2003 y = event.xmotion.y_root;
2004 if (y < yp) {
2005 h = yp - y;
2006 } else {
2007 h = y - yp;
2008 y = yp;
2010 XDrawRectangle(dpy, root, gc, x, y, w, h);
2011 break;
2013 case ButtonPress:
2014 break;
2016 case ButtonRelease:
2017 if (event.xbutton.button != ev->xbutton.button)
2018 break;
2020 XDrawRectangle(dpy, root, gc, x, y, w, h);
2021 XUngrabServer(dpy);
2022 XUngrabPointer(dpy, CurrentTime);
2023 selectWindowsInside(scr, x, y, x + w, y + h);
2025 #ifdef KWM_HINTS
2026 wKWMSelectRootRegion(scr, x, y, w, h,
2027 event.xbutton.state & ControlMask);
2028 #endif /* KWM_HINTS */
2030 #ifdef DEBUG
2031 puts("End window selection");
2032 #endif
2033 return;
2035 default:
2036 WMHandleEvent(&event);
2037 break;
2041 #endif /* !LITE */
2043 void
2044 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2045 unsigned width, unsigned height)
2047 WScreen *scr = wwin->screen_ptr;
2048 Window root = scr->root_win;
2049 int x, y, h = 0;
2050 XEvent event;
2051 KeyCode shiftl, shiftr;
2052 Window junkw;
2053 int junk;
2055 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2056 GrabModeAsync, GrabModeAsync, None,
2057 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2058 *x_ret = 0;
2059 *y_ret = 0;
2060 return;
2062 if (!WFLAGP(wwin, no_titlebar)) {
2063 h = scr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
2064 height += h;
2066 if (!WFLAGP(wwin, no_resizebar)) {
2067 height += RESIZEBAR_HEIGHT;
2069 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2070 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2071 (unsigned *) &junk);
2072 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2074 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2076 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2077 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2078 while (1) {
2079 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2080 &event);
2081 switch (event.type) {
2082 case KeyPress:
2083 if ((event.xkey.keycode == shiftl)
2084 || (event.xkey.keycode == shiftr)) {
2085 drawTransparentFrame(wwin,
2086 x - width/2, y - h/2, width, height);
2087 cyclePositionDisplay(wwin,
2088 x - width/2, y - h/2, width, height);
2089 drawTransparentFrame(wwin,
2090 x - width/2, y - h/2, width, height);
2092 break;
2094 case MotionNotify:
2095 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2097 x = event.xmotion.x_root;
2098 y = event.xmotion.y_root;
2100 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2101 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2103 showPosition(wwin, x - width/2, y - h/2);
2105 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2107 break;
2109 case ButtonPress:
2110 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2111 XSync(dpy, 0);
2112 *x_ret = x - width/2;
2113 *y_ret = y - h/2;
2114 XUngrabPointer(dpy, CurrentTime);
2115 XUngrabKeyboard(dpy, CurrentTime);
2116 /* get rid of the geometry window */
2117 unmapPositionDisplay(wwin);
2118 return;
2120 default:
2121 WMHandleEvent(&event);
2122 break;