Bugs fixes and add KEY_CONTROL_WINDOW_WEIGHT to key move/resize window.
[wmaker-crm.git] / src / moveres.c
blob2f782262ae01f5e8442cfaafe63a8fc1f26143e6
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 0
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);
179 showPosition(wwin, x, y);
184 static void
185 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
187 WScreen *scr = wwin->screen_ptr;
189 if (wPreferences.move_display == WDIS_NEW) {
190 return;
191 } else if (wPreferences.move_display == WDIS_CENTER) {
192 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
193 scr->scr_height / 2);
194 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
195 moveGeometryDisplayCentered(scr, 1, 1);
196 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
197 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
199 XMapRaised(dpy, scr->geometry_display);
200 showPosition(wwin, x, y);
203 #define unmapPositionDisplay(w) \
204 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
207 static void
208 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
210 WScreen *scr = wwin->screen_ptr;
211 Window root = scr->root_win;
212 GC gc = scr->line_gc;
213 int ty, by, my, x, y, mx, s;
214 char num[16];
215 XSegment segment[4];
216 int fw, fh;
218 ty = y1 + wwin->frame->top_width;
219 by = y2 - wwin->frame->bottom_width;
220 fw = wTextWidth(scr->info_text_font->font, "8888", 4);
221 fh = scr->info_text_font->height;
223 if (wPreferences.size_display == WDIS_NEW) {
224 XSetForeground(dpy, gc, scr->line_pixel);
226 /* vertical geometry */
227 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
228 x = x2;
229 s = -15;
230 } else {
231 x = x1;
232 s = 15;
234 my = (ty + by) / 2;
236 /* top arrow */
237 /* end bar */
238 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
239 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
241 /* arrowhead */
242 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
243 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
245 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
246 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
248 /* line */
249 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
250 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
252 XDrawSegments(dpy, root, gc, segment, 4);
254 /* bottom arrow */
255 /* end bar */
256 segment[0].y1 = by;
257 segment[0].y2 = by;
259 /* arrowhead */
260 segment[1].y1 = by - 1;
261 segment[1].y2 = by - 7;
263 segment[2].y1 = by - 1;
264 segment[2].y2 = by - 7;
266 /* line */
267 segment[3].y1 = my + fh/2 + 2;
268 segment[3].y2 = by - 1;
270 XDrawSegments(dpy, root, gc, segment, 4);
272 sprintf(num, "%i", (by - ty - wwin->normal_hints->base_height) /
273 wwin->normal_hints->height_inc);
274 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
276 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
278 /* Display the height. */
279 wDrawString(root, scr->info_text_font, gc,
280 x - s + 3 - fw/2, my - fh/2 + scr->info_text_font->y + 1,
281 num, strlen(num));
282 XSetForeground(dpy, gc, scr->line_pixel);
283 /* horizontal geometry */
284 if (y1 < 15) {
285 y = y2;
286 s = -15;
287 } else {
288 y = y1;
289 s = 15;
291 mx = x1 + (x2 - x1)/2;
292 sprintf(num, "%i", (x2 - x1 - wwin->normal_hints->base_width) /
293 wwin->normal_hints->width_inc);
294 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
296 /* left arrow */
297 /* end bar */
298 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
299 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
301 /* arrowhead */
302 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
303 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
305 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
306 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
308 /* line */
309 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
310 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
312 XDrawSegments(dpy, root, gc, segment, 4);
314 /* right arrow */
315 /* end bar */
316 segment[0].x1 = x2 + 1;
317 segment[0].x2 = x2 + 1;
319 /* arrowhead */
320 segment[1].x1 = x2 - 6;
321 segment[1].x2 = x2;
323 segment[2].x1 = x2;
324 segment[2].x2 = x2 - 6;
326 /* line */
327 segment[3].x1 = mx + fw/2 + 2;
328 segment[3].x2 = x2;
330 XDrawSegments(dpy, root, gc, segment, 4);
332 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
334 /* Display the width. */
335 wDrawString(root, scr->info_text_font, gc,
336 mx - fw/2 + 1, y - s + fh/2 + 1, num, strlen(num));
337 } else {
338 XClearArea(dpy, scr->geometry_display, 1, 1,
339 scr->geometry_display_width-2, scr->geometry_display_height-2,
340 False);
341 sprintf(num, "%i x %-i", (x2 - x1 - wwin->normal_hints->base_width)
342 / wwin->normal_hints->width_inc,
343 (by - ty - wwin->normal_hints->base_height)
344 / wwin->normal_hints->height_inc);
345 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
347 XSetForeground(dpy, scr->info_text_gc, scr->black_pixel);
349 /* Display the height. */
350 wDrawString(scr->geometry_display, scr->info_text_font,
351 scr->info_text_gc,
352 (scr->geometry_display_width-fw)/2,
353 (scr->geometry_display_height-fh)/2 +scr->info_text_font->y,
354 num, strlen(num));
355 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
356 scr->geometry_display_height+1,
357 scr->widget_texture, WREL_RAISED);
362 static void
363 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
365 WScreen *scr = wwin->screen_ptr;
367 wPreferences.size_display++;
368 wPreferences.size_display %= NUM_DISPLAYS;
370 if (wPreferences.size_display == WDIS_NEW) {
371 XUnmapWindow(dpy, scr->geometry_display);
372 } else {
373 if (wPreferences.size_display == WDIS_CENTER) {
374 moveGeometryDisplayCentered(scr,
375 scr->scr_width / 2, scr->scr_height / 2);
376 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
377 moveGeometryDisplayCentered(scr, 1, 1);
378 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
379 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
381 XMapRaised(dpy, scr->geometry_display);
382 showGeometry(wwin, x, y, x + w, y + h, dir);
387 static void
388 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
390 WScreen *scr = wwin->screen_ptr;
392 if (wPreferences.size_display == WDIS_NEW)
393 return;
395 if (wPreferences.size_display == WDIS_CENTER) {
396 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
397 scr->scr_height / 2);
398 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
399 moveGeometryDisplayCentered(scr, 1, 1);
400 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
401 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
403 XMapRaised(dpy, scr->geometry_display);
404 showGeometry(wwin, x, y, x + w, y + h, 0);
407 #define unmapGeometryDisplay(w) \
408 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
411 static void
412 doWindowMove(WWindow *wwin, LinkedList *list, int dx, int dy)
414 WWindow *tmpw;
415 int x, y;
416 int scr_width = wwin->screen_ptr->scr_width;
417 int scr_height = wwin->screen_ptr->scr_height;
419 if (!list) {
420 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
421 } else {
422 while (list) {
423 tmpw = list->head;
424 x = tmpw->frame_x + dx;
425 y = tmpw->frame_y + dy;
427 /* don't let windows become unreachable */
429 if (x + (int)tmpw->frame->core->width < 20)
430 x = 20 - (int)tmpw->frame->core->width;
431 else if (x + 20 > scr_width)
432 x = scr_width - 20;
434 if (y + (int)tmpw->frame->core->height < 20)
435 y = 20 - (int)tmpw->frame->core->height;
436 else if (y + 20 > scr_height)
437 y = scr_height - 20;
439 wWindowMove(tmpw, x, y);
440 list = list->tail;
446 static void
447 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
449 Window root = wwin->screen_ptr->root_win;
450 GC gc = wwin->screen_ptr->frame_gc;
451 int h = 0;
452 int bottom = 0;
454 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
455 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
457 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
458 /* Can't use wwin-frame->bottom_width because, in some cases
459 (e.g. interactive placement), frame does not point to anything. */
460 bottom = RESIZEBAR_HEIGHT - 1;
462 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
464 if (h > 0) {
465 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
467 if (bottom > 0) {
468 XDrawLine(dpy, root, gc, x + 1,
469 y + height - bottom,
470 x + width + 1,
471 y + height - bottom);
476 static void
477 drawFrames(WWindow *wwin, LinkedList *list, int dx, int dy)
479 WWindow *tmpw;
480 int scr_width = wwin->screen_ptr->scr_width;
481 int scr_height = wwin->screen_ptr->scr_height;
482 int x, y;
484 if (!list) {
486 x = wwin->frame_x + dx;
487 y = wwin->frame_y + dy;
489 drawTransparentFrame(wwin, x, y,
490 wwin->frame->core->width,
491 wwin->frame->core->height);
493 } else {
494 while (list) {
495 tmpw = list->head;
496 x = tmpw->frame_x + dx;
497 y = tmpw->frame_y + dy;
499 /* don't let windows become unreachable */
501 if (x + (int)tmpw->frame->core->width < 20)
502 x = 20 - (int)tmpw->frame->core->width;
503 else if (x + 20 > scr_width)
504 x = scr_width - 20;
506 if (y + (int)tmpw->frame->core->height < 20)
507 y = 20 - (int)tmpw->frame->core->height;
508 else if (y + 20 > scr_height)
509 y = scr_height - 20;
511 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
512 tmpw->frame->core->height);
514 list = list->tail;
521 static void
522 flushMotion()
524 XEvent ev;
526 XSync(dpy, False);
527 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
531 static void
532 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
533 int new_workspace, int rewind)
535 /* do not let window be unmapped */
536 if (opaque_move) {
537 wwin->flags.changing_workspace = 1;
538 wWindowChangeWorkspace(wwin, new_workspace);
540 /* go to new workspace */
541 wWorkspaceChange(scr, new_workspace);
543 wwin->flags.changing_workspace = 0;
545 if (rewind)
546 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
547 else
548 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
550 flushMotion();
552 if (!opaque_move) {
553 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
554 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
555 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
562 typedef struct {
563 /* arrays of WWindows sorted by the respective border position */
564 WWindow **topList; /* top border */
565 WWindow **leftList; /* left border */
566 WWindow **rightList; /* right border */
567 WWindow **bottomList; /* bottom border */
568 int count;
570 /* index of window in the above lists indicating the relative position
571 * of the window with the others */
572 int topIndex;
573 int leftIndex;
574 int rightIndex;
575 int bottomIndex;
577 int rubCount; /* for workspace switching */
579 int winWidth, winHeight; /* width/height of the window */
580 int realX, realY; /* actual position of the window */
581 int calcX, calcY; /* calculated position of window */
582 int omouseX, omouseY; /* old mouse position */
583 int mouseX, mouseY; /* last known position of the pointer */
584 } MoveData;
586 #define WTOP(w) (w)->frame_y
587 #define WLEFT(w) (w)->frame_x
588 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
589 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
591 static int
592 compareWTop(const void *a, const void *b)
594 WWindow *wwin1 = *(WWindow**)a;
595 WWindow *wwin2 = *(WWindow**)b;
597 if (WTOP(wwin1) > WTOP(wwin2))
598 return -1;
599 else if (WTOP(wwin1) < WTOP(wwin2))
600 return 1;
601 else
602 return 0;
606 static int
607 compareWLeft(const void *a, const void *b)
609 WWindow *wwin1 = *(WWindow**)a;
610 WWindow *wwin2 = *(WWindow**)b;
612 if (WLEFT(wwin1) > WLEFT(wwin2))
613 return -1;
614 else if (WLEFT(wwin1) < WLEFT(wwin2))
615 return 1;
616 else
617 return 0;
621 static int
622 compareWRight(const void *a, const void *b)
624 WWindow *wwin1 = *(WWindow**)a;
625 WWindow *wwin2 = *(WWindow**)b;
627 if (WRIGHT(wwin1) < WRIGHT(wwin2))
628 return -1;
629 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
630 return 1;
631 else
632 return 0;
637 static int
638 compareWBottom(const void *a, const void *b)
640 WWindow *wwin1 = *(WWindow**)a;
641 WWindow *wwin2 = *(WWindow**)b;
643 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
644 return -1;
645 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
646 return 1;
647 else
648 return 0;
652 static void
653 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
655 int i;
656 int newX2 = newX + data->winWidth;
657 int newY2 = newY + data->winHeight;
658 Bool ok = False;
660 if (newX < data->realX) {
661 if (data->rightIndex > 0
662 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
663 ok = True;
664 } else if (data->leftIndex <= data->count-1
665 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
666 ok = True;
668 } else if (newX > data->realX) {
669 if (data->leftIndex > 0
670 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
671 ok = True;
672 } else if (data->rightIndex <= data->count-1
673 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
674 ok = True;
678 if (!ok) {
679 if (newY < data->realY) {
680 if (data->bottomIndex > 0
681 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
682 ok = True;
683 } else if (data->topIndex <= data->count-1
684 && newY2 <= WTOP(data->topList[data->topIndex])) {
685 ok = True;
687 } else if (newY > data->realY) {
688 if (data->topIndex > 0
689 && newY2 > WTOP(data->topList[data->topIndex-1])) {
690 ok = True;
691 } else if (data->bottomIndex <= data->count-1
692 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
693 ok = True;
698 if (!ok)
699 return;
701 /* TODO: optimize this */
702 if (data->realY < WBOTTOM(data->bottomList[0])) {
703 data->bottomIndex = 0;
705 if (data->realX < WRIGHT(data->rightList[0])) {
706 data->rightIndex = 0;
708 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
709 data->leftIndex = 0;
711 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
712 data->topIndex = 0;
714 for (i = 0; i < data->count; i++) {
715 if (data->realY > WBOTTOM(data->bottomList[i])) {
716 data->bottomIndex = i + 1;
718 if (data->realX > WRIGHT(data->rightList[i])) {
719 data->rightIndex = i + 1;
721 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
722 data->leftIndex = i + 1;
724 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
725 data->topIndex = i + 1;
731 static void
732 freeMoveData(MoveData *data)
734 if (data->topList)
735 free(data->topList);
736 if (data->leftList)
737 free(data->leftList);
738 if (data->rightList)
739 free(data->rightList);
740 if (data->bottomList)
741 free(data->bottomList);
745 static void
746 updateMoveData(WWindow *wwin, MoveData *data)
748 WScreen *scr = wwin->screen_ptr;
749 WWindow *tmp;
750 int i;
752 data->count = 0;
753 tmp = scr->focused_window;
754 while (tmp) {
755 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
756 && !tmp->flags.miniaturized
757 && !tmp->flags.hidden
758 && !tmp->flags.obscured) {
759 data->topList[data->count] = tmp;
760 data->leftList[data->count] = tmp;
761 data->rightList[data->count] = tmp;
762 data->bottomList[data->count] = tmp;
763 data->count++;
765 tmp = tmp->prev;
768 if (data->count == 0) {
769 data->topIndex = 0;
770 data->leftIndex = 0;
771 data->rightIndex = 0;
772 data->bottomIndex = 0;
773 return;
777 * order from closest to the border of the screen to farthest
779 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
780 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
781 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
782 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
784 /* figure the position of the window relative to the others */
786 data->topIndex = -1;
787 data->leftIndex = -1;
788 data->rightIndex = -1;
789 data->bottomIndex = -1;
791 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
792 data->bottomIndex = 0;
794 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
795 data->rightIndex = 0;
797 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
798 data->leftIndex = 0;
800 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
801 data->topIndex = 0;
803 for (i = 0; i < data->count; i++) {
804 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
805 data->bottomIndex = i + 1;
807 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
808 data->rightIndex = i + 1;
810 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
811 data->leftIndex = i + 1;
813 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
814 data->topIndex = i + 1;
820 static void
821 initMoveData(WWindow *wwin, MoveData *data)
823 int i;
824 WWindow *tmp;
826 memset(data, 0, sizeof(MoveData));
828 for (i = 0, tmp = wwin->screen_ptr->focused_window;
829 tmp != NULL;
830 tmp = tmp->prev, i++);
832 if (i > 1) {
833 data->topList = wmalloc(sizeof(WWindow*) * i);
834 data->leftList = wmalloc(sizeof(WWindow*) * i);
835 data->rightList = wmalloc(sizeof(WWindow*) * i);
836 data->bottomList = wmalloc(sizeof(WWindow*) * i);
838 updateMoveData(wwin, data);
841 data->realX = wwin->frame_x;
842 data->realY = wwin->frame_y;
843 data->calcX = wwin->frame_x;
844 data->calcY = wwin->frame_y;
846 data->winWidth = wwin->frame->core->width + 2;
847 data->winHeight = wwin->frame->core->height + 2;
851 static Bool
852 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
854 WScreen *scr = wwin->screen_ptr;
855 Bool changed = False;
857 if (data->mouseX <= 1) {
858 if (scr->current_workspace > 0) {
860 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
861 True);
862 changed = True;
863 data->rubCount = 0;
865 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
867 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
868 True);
869 changed = True;
870 data->rubCount = 0;
872 } else if (data->mouseX >= scr->scr_width - 2) {
874 if (scr->current_workspace == scr->workspace_count - 1) {
876 if (wPreferences.ws_cycle
877 || scr->workspace_count == MAX_WORKSPACES) {
879 crossWorkspace(scr, wwin, opaqueMove, 0, False);
880 changed = True;
881 data->rubCount = 0;
883 /* if user insists on trying to go to next workspace even when
884 * it's already the last, create a new one */
885 else if (data->omouseX == data->mouseX
886 && wPreferences.ws_advance) {
888 /* detect user "rubbing" the window against the edge */
889 if (data->rubCount > 0
890 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
892 data->rubCount = -(data->rubCount + 1);
894 } else if (data->rubCount <= 0
895 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
897 data->rubCount = -data->rubCount + 1;
900 /* create a new workspace */
901 if (abs(data->rubCount) > 2) {
902 /* go to next workspace */
903 wWorkspaceNew(scr);
905 crossWorkspace(scr, wwin, opaqueMove,
906 scr->current_workspace+1, False);
907 changed = True;
908 data->rubCount = 0;
910 } else if (scr->current_workspace < scr->workspace_count) {
912 /* go to next workspace */
913 crossWorkspace(scr, wwin, opaqueMove,
914 scr->current_workspace+1, False);
915 changed = True;
916 data->rubCount = 0;
918 } else {
919 data->rubCount = 0;
922 return changed;
926 static void
927 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
928 Bool opaqueMove, int newMouseX, int newMouseY)
930 WScreen *scr = wwin->screen_ptr;
931 int dx, dy; /* how much mouse moved */
932 int winL, winR, winT, winB; /* requested new window position */
933 int newX, newY; /* actual new window position */
934 Bool hresist, vresist;
935 Bool updateIndex;
937 hresist = False;
938 vresist = False;
940 updateIndex = False;
942 /* check the direction of the movement */
943 dx = newMouseX - data->mouseX;
944 dy = newMouseY - data->mouseY;
946 data->omouseX = data->mouseX;
947 data->omouseY = data->mouseY;
948 data->mouseX = newMouseX;
949 data->mouseY = newMouseY;
951 winL = data->calcX + dx;
952 winR = data->calcX + data->winWidth + dx;
953 winT = data->calcY + dy;
954 winB = data->calcY + data->winHeight + dy;
956 newX = data->realX;
957 newY = data->realY;
959 if (doResistance) {
960 int edge;
961 int resist;
962 WWindow *rwin;
964 resist = wPreferences.edge_resistance;
965 /* horizontal movement: check horizontal edge resistances */
966 if (dx < 0) {
967 /* window is the leftmost window: check against screen edge */
968 edge = scr->totalUsableArea.x1;
970 /* check position of nearest window to the left */
971 if (data->rightIndex > 0) {
972 /* there is some window at the left: check if it will block
973 * the window */
974 rwin = data->rightList[data->rightIndex - 1];
976 if (data->realY > WBOTTOM(rwin)
977 || (data->realY + data->winHeight) < WTOP(rwin)) {
978 resist = 0;
979 } else {
980 edge = WRIGHT(rwin) + 1;
981 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
984 if (resist > 0 && winL >= edge - resist && winL <= edge) {
985 newX = edge;
986 hresist = True;
988 } else if (dx > 0) {
989 /* window is the rightmost window: check against screen edge */
990 edge = scr->totalUsableArea.x2;
992 /* check position of nearest window to the right */
993 if (data->leftIndex > 0) {
994 /* there is some window at the right: check if it will block
995 * the window */
996 rwin = data->leftList[data->leftIndex - 1];
998 if (data->realY > WBOTTOM(rwin)
999 || (data->realY + data->winHeight) < WTOP(rwin)) {
1000 resist = 0;
1001 } else {
1002 edge = WLEFT(rwin);
1003 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1006 if (resist > 0 && winR <= edge + resist && winR >= edge) {
1007 newX = edge - data->winWidth;
1008 hresist = True;
1012 resist = wPreferences.edge_resistance;
1013 /* vertical movement: check vertical edge resistances */
1014 if (dy < 0) {
1015 /* window is the topmost window: check against screen edge */
1016 edge = scr->totalUsableArea.y1;
1018 /* check position of nearest window to the top */
1019 if (data->bottomIndex > 0) {
1020 /* there is some window at the top: check if it will block
1021 * the window */
1022 rwin = data->bottomList[data->bottomIndex - 1];
1024 if (data->realX > WRIGHT(rwin)
1025 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1026 resist = 0;
1027 } else {
1028 edge = WBOTTOM(rwin) + 1;
1029 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1032 if (resist > 0 && winT >= edge - resist && winT <= edge) {
1033 newY = edge;
1034 vresist = True;
1036 } else if (dy > 0) {
1037 /* window is the bottommost window: check against screen edge */
1038 edge = scr->totalUsableArea.y2;
1040 /* check position of nearest window to the bottom */
1041 if (data->topIndex > 0) {
1042 /* there is some window at the bottom: check if it will block
1043 * the window */
1044 rwin = data->topList[data->topIndex - 1];
1046 if (data->realX > WRIGHT(rwin)
1047 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1048 resist = 0;
1049 } else {
1050 edge = WTOP(rwin);
1051 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1054 if (resist > 0 && winB <= edge + resist && winB >= edge) {
1055 newY = edge - data->winHeight;
1056 vresist = True;
1061 /* update window position */
1062 data->calcX += dx;
1063 data->calcY += dy;
1065 if (((dx > 0 && data->calcX - data->realX > 0)
1066 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1067 newX = data->calcX;
1069 if (((dy > 0 && data->calcY - data->realY > 0)
1070 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1071 newY = data->calcY;
1073 if (data->realX != newX || data->realY != newY) {
1074 if (opaqueMove) {
1075 doWindowMove(wwin, scr->selected_windows,
1076 newX - wwin->frame_x,
1077 newY - wwin->frame_y);
1078 } else {
1079 /* erase frames */
1080 drawFrames(wwin, scr->selected_windows,
1081 data->realX - wwin->frame_x,
1082 data->realY - wwin->frame_y);
1085 if (!scr->selected_windows
1086 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1088 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1089 newY + data->winHeight/2);
1092 if (!opaqueMove) {
1093 /* draw frames */
1094 drawFrames(wwin, scr->selected_windows,
1095 newX - wwin->frame_x,
1096 newY - wwin->frame_y);
1099 if (!scr->selected_windows) {
1101 if (wPreferences.move_display == WDIS_NEW) {
1103 showPosition(wwin, data->realX, data->realY);
1106 showPosition(wwin, newX, newY);
1111 /* recalc relative window position */
1112 if (doResistance && (data->realX != newX || data->realY != newY)) {
1113 updateResistance(wwin, data, newX, newY);
1116 data->realX = newX;
1117 data->realY = newY;
1121 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1124 wKeyboardMoveResizeWindow(WWindow *wwin)
1126 WScreen *scr = wwin->screen_ptr;
1127 Window root = scr->root_win;
1128 XEvent event;
1129 int w = wwin->frame->core->width;
1130 int h = wwin->frame->core->height;
1131 int scr_width = wwin->screen_ptr->scr_width;
1132 int scr_height = wwin->screen_ptr->scr_height;
1133 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1134 int src_x = wwin->frame_x;
1135 int src_y = wwin->frame_y;
1136 int done,off_x,off_y,ww,wh;
1137 int kspeed = _KS;
1138 Time lastTime = 0;
1139 KeySym keysym=NoSymbol;
1140 int moment=0;
1141 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1143 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1144 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1145 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1146 ctrlmode=done=off_x=off_y=0;
1148 XSync(dpy, False);
1149 wusleep(10000);
1150 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1152 if (!wwin->flags.selected) {
1153 wUnselectWindows(scr);
1155 XGrabServer(dpy);
1156 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1157 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1158 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1160 if (wwin->flags.shaded || scr->selected_windows) {
1161 if(scr->selected_windows)
1162 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1163 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1164 if(!scr->selected_windows)
1165 mapPositionDisplay(wwin, src_x, src_y, w, h);
1166 } else {
1167 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1169 ww=w;
1170 wh=h;
1171 while(1) {
1173 looper.ox=off_x;
1174 looper.oy=off_y;
1176 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1177 | ButtonPressMask | ExposureMask, &event);
1178 if (wwin->flags.shaded || scr->selected_windows) {
1179 if(scr->selected_windows)
1180 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1181 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1182 /*** I HATE EDGE RESISTANCE - ]d ***/
1184 else {
1185 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1188 if(ctrlmode)
1189 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1191 XUngrabServer(dpy);
1192 XSync(dpy, False);
1194 switch (event.type) {
1195 case KeyPress:
1196 /* accelerate */
1197 if (event.xkey.time - lastTime > 50) {
1198 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1199 } else {
1200 if (kspeed < 20) {
1201 kspeed++;
1204 if (kspeed < _KS) kspeed = _KS;
1205 lastTime = event.xkey.time;
1207 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1208 ctrlmode=1;
1209 wUnselectWindows(scr);
1211 else {
1212 ctrlmode=0;
1214 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1215 if (ctrlmode)
1216 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1217 else
1218 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1220 else {
1222 keysym = XLookupKeysym(&event.xkey, 0);
1223 switch (keysym) {
1224 case XK_Return:
1225 done=2;
1226 break;
1227 case XK_Escape:
1228 done=1;
1229 break;
1230 case XK_Up:
1231 case XK_KP_Up:
1232 case XK_k:
1233 if (ctrlmode){
1234 if (moment != UP)
1235 h = wh;
1236 h-=kspeed;
1237 moment = UP;
1238 if (h < 1) h = 1;
1240 else off_y-=kspeed;
1241 break;
1242 case XK_Down:
1243 case XK_KP_Down:
1244 case XK_j:
1245 if (ctrlmode){
1246 if (moment != DOWN)
1247 h = wh;
1248 h+=kspeed;
1249 moment = DOWN;
1251 else off_y+=kspeed;
1252 break;
1253 case XK_Left:
1254 case XK_KP_Left:
1255 case XK_h:
1256 if (ctrlmode) {
1257 if (moment != LEFT)
1258 w = ww;
1259 w-=kspeed;
1260 if (w < 1) w = 1;
1261 moment = LEFT;
1263 else off_x-=kspeed;
1264 break;
1265 case XK_Right:
1266 case XK_KP_Right:
1267 case XK_l:
1268 if (ctrlmode) {
1269 if (moment != RIGHT)
1270 w = ww;
1271 w+=kspeed;
1272 moment = RIGHT;
1274 else off_x+=kspeed;
1275 break;
1278 ww=w;wh=h;
1279 wh-=vert_border;
1280 wWindowConstrainSize(wwin, &ww, &wh);
1281 wh+=vert_border;
1283 if (wPreferences.ws_cycle){
1284 if (src_x + off_x + ww < 20){
1285 if(!scr->current_workspace) {
1286 wWorkspaceChange(scr, scr->workspace_count-1);
1288 else wWorkspaceChange(scr, scr->current_workspace-1);
1289 off_x += scr_width;
1291 else if (src_x + off_x + 20 > scr_width){
1292 if(scr->current_workspace == scr->workspace_count-1) {
1293 wWorkspaceChange(scr, 0);
1295 else wWorkspaceChange(scr, scr->current_workspace+1);
1296 off_x -= scr_width;
1299 else {
1300 if (src_x + off_x + ww < 20)
1301 off_x = 20 - ww - src_x;
1302 else if (src_x + off_x + 20 > scr_width)
1303 off_x = scr_width - 20 - src_x;
1306 if (src_y + off_y + wh < 20) {
1307 off_y = 20 - wh - src_y;
1309 else if (src_y + off_y + 20 > scr_height) {
1310 off_y = scr_height - 20 - src_y;
1313 break;
1314 case ButtonPress:
1315 case ButtonRelease:
1316 done=1;
1317 break;
1318 default:
1319 WMHandleEvent(&event);
1320 break;
1323 XGrabServer(dpy);
1324 /*xxx*/
1326 if (wwin->flags.shaded && !scr->selected_windows){
1327 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1329 else {
1330 if(ctrlmode){
1331 unmapPositionDisplay(wwin);
1332 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1334 else if(!scr->selected_windows){
1335 unmapGeometryDisplay(wwin);
1336 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1340 if (wwin->flags.shaded || scr->selected_windows) {
1341 if(scr->selected_windows)
1342 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1343 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1345 else {
1346 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1350 if(ctrlmode){
1351 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1353 else if(!scr->selected_windows)
1354 showPosition(wwin, src_x+off_x, src_y+off_y);
1355 /**/
1357 if(done){
1358 scr->keymove_tick=0;
1360 WMDeleteTimerWithClientData(&looper);
1362 if (wwin->flags.shaded || scr->selected_windows) {
1363 if(scr->selected_windows)
1364 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1365 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1367 else {
1368 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1371 if(ctrlmode){
1372 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1373 unmapGeometryDisplay(wwin);
1375 else
1376 unmapPositionDisplay(wwin);
1377 XUngrabKeyboard(dpy, CurrentTime);
1378 XUngrabPointer(dpy, CurrentTime);
1379 XUngrabServer(dpy);
1381 if(done==2) {
1382 if (wwin->flags.shaded || scr->selected_windows) {
1383 LinkedList *list;
1384 list=scr->selected_windows;
1385 if (!scr->selected_windows) {
1386 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1387 wWindowSynthConfigureNotify(wwin);
1388 } else {
1389 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1390 while (list) {
1391 wWindowSynthConfigureNotify(list->head);
1392 list = list->tail;
1395 } else {
1396 if (wwin->client.width != ww)
1397 wwin->flags.user_changed_width = 1;
1399 if (wwin->client.height != wh - vert_border)
1400 wwin->flags.user_changed_height = 1;
1402 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1403 ww, wh - vert_border);
1404 wWindowSynthConfigureNotify(wwin);
1406 wWindowChangeWorkspace(wwin, scr->current_workspace);
1407 wSetFocusTo(scr, wwin);
1409 return 1;
1416 *----------------------------------------------------------------------
1417 * wMouseMoveWindow--
1418 * Move the named window and the other selected ones (if any),
1419 * interactively. Also shows the position of the window, if only one
1420 * window is being moved.
1421 * If the window is not on the selected window list, the selected
1422 * windows are deselected.
1423 * If shift is pressed during the operation, the position display
1424 * is changed to another type.
1426 * Returns:
1427 * True if the window was moved, False otherwise.
1429 * Side effects:
1430 * The window(s) position is changed, and the client(s) are
1431 * notified about that.
1432 * The position display configuration may be changed.
1433 *----------------------------------------------------------------------
1436 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1438 WScreen *scr = wwin->screen_ptr;
1439 XEvent event;
1440 Window root = scr->root_win;
1441 KeyCode shiftl, shiftr;
1442 int started = 0;
1443 int warped = 0;
1444 /* This needs not to change while moving, else bad things can happen */
1445 int opaqueMove = wPreferences.opaque_move;
1446 MoveData moveData;
1447 #ifdef GHOST_WINDOW_MOVE
1448 RImage *rimg;
1450 rimg = InitGhostWindowMove(scr);
1451 #endif
1453 initMoveData(wwin, &moveData);
1455 moveData.mouseX = ev->xmotion.x_root;
1456 moveData.mouseY = ev->xmotion.y_root;
1458 if (!wwin->flags.selected) {
1459 /* this window is not selected, unselect others and move only wwin */
1460 wUnselectWindows(scr);
1462 #ifdef DEBUG
1463 puts("Moving window");
1464 #endif
1465 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1466 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1467 while (1) {
1468 if (warped) {
1469 int junk;
1470 Window junkw;
1472 /* XWarpPointer() doesn't seem to generate Motion events, so
1473 we've got to simulate them */
1474 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1475 &event.xmotion.y_root, &junk, &junk,
1476 (unsigned *) &junk);
1477 } else {
1478 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1479 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1480 &event);
1482 if (event.type == MotionNotify) {
1483 /* compress MotionNotify events */
1484 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1487 switch (event.type) {
1488 case KeyPress:
1489 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1490 && started && !scr->selected_windows) {
1492 if (!opaqueMove) {
1493 drawFrames(wwin, scr->selected_windows,
1494 moveData.realX - wwin->frame_x,
1495 moveData.realY - wwin->frame_y);
1498 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1499 moveData.winWidth, moveData.winHeight);
1501 if (!opaqueMove) {
1502 drawFrames(wwin, scr->selected_windows,
1503 moveData.realX - wwin->frame_x,
1504 moveData.realY - wwin->frame_y);
1507 break;
1509 case MotionNotify:
1510 if (started) {
1511 updateWindowPosition(wwin, &moveData,
1512 scr->selected_windows == NULL
1513 && wPreferences.edge_resistance > 0,
1514 opaqueMove,
1515 event.xmotion.x_root,
1516 event.xmotion.y_root);
1518 if (!warped && !wPreferences.no_autowrap) {
1519 int oldWorkspace = scr->current_workspace;
1521 if (!opaqueMove) {
1522 drawFrames(wwin, scr->selected_windows,
1523 moveData.realX - wwin->frame_x,
1524 moveData.realY - wwin->frame_y);
1526 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1527 if (scr->current_workspace != oldWorkspace
1528 && wPreferences.edge_resistance > 0
1529 && scr->selected_windows == NULL)
1530 updateMoveData(wwin, &moveData);
1531 warped = 1;
1534 if (!opaqueMove) {
1535 drawFrames(wwin, scr->selected_windows,
1536 moveData.realX - wwin->frame_x,
1537 moveData.realY - wwin->frame_y);
1539 } else {
1540 warped = 0;
1542 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1543 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1545 XChangeActivePointerGrab(dpy, ButtonMotionMask
1546 | ButtonReleaseMask | ButtonPressMask,
1547 wCursor[WCUR_MOVE], CurrentTime);
1548 started = 1;
1549 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1550 CurrentTime);
1552 if (!scr->selected_windows)
1553 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1554 moveData.winWidth, moveData.winHeight);
1556 if (started && !opaqueMove)
1557 drawFrames(wwin, scr->selected_windows, 0, 0);
1559 if (!opaqueMove)
1560 XGrabServer(dpy);
1562 break;
1564 case ButtonPress:
1565 break;
1567 case ButtonRelease:
1568 if (event.xbutton.button != ev->xbutton.button)
1569 break;
1571 if (started) {
1572 if (!opaqueMove) {
1573 drawFrames(wwin, scr->selected_windows,
1574 moveData.realX - wwin->frame_x,
1575 moveData.realY - wwin->frame_y);
1576 XSync(dpy, 0);
1577 doWindowMove(wwin, scr->selected_windows,
1578 moveData.realX - wwin->frame_x,
1579 moveData.realY - wwin->frame_y);
1581 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1582 wWindowSynthConfigureNotify(wwin);
1583 #endif
1584 XUngrabKeyboard(dpy, CurrentTime);
1585 XUngrabServer(dpy);
1586 if (!opaqueMove) {
1587 wWindowChangeWorkspace(wwin, scr->current_workspace);
1588 wSetFocusTo(scr, wwin);
1590 if (wPreferences.move_display == WDIS_NEW)
1591 showPosition(wwin, moveData.realX, moveData.realY);
1593 if (!scr->selected_windows) {
1594 /* get rid of the geometry window */
1595 unmapPositionDisplay(wwin);
1598 #ifdef DEBUG
1599 puts("End move window");
1600 #endif
1601 freeMoveData(&moveData);
1603 return started;
1605 default:
1606 if (started && !opaqueMove) {
1607 drawFrames(wwin, scr->selected_windows,
1608 moveData.realX - wwin->frame_x,
1609 moveData.realY - wwin->frame_y);
1610 XUngrabServer(dpy);
1611 WMHandleEvent(&event);
1612 XSync(dpy, False);
1613 XGrabServer(dpy);
1614 drawFrames(wwin, scr->selected_windows,
1615 moveData.realX - wwin->frame_x,
1616 moveData.realY - wwin->frame_y);
1617 } else {
1618 WMHandleEvent(&event);
1620 break;
1624 freeMoveData(&moveData);
1626 return 0;
1630 #define RESIZEBAR 1
1631 #define HCONSTRAIN 2
1633 static int
1634 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1635 int flags)
1637 int w = wwin->frame->core->width - 1;
1638 int cw = wwin->frame->resizebar_corner_width;
1639 int dir;
1641 /* if not resizing through the resizebar */
1642 if (!(flags & RESIZEBAR)) {
1643 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1644 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1645 if (abs(dx) < 2 || abs(dy) < 2) {
1646 if (abs(dy) > abs(dx))
1647 xdir = 0;
1648 else
1649 ydir = 0;
1651 return (xdir | ydir);
1654 /* window is too narrow. allow diagonal resize */
1655 if (cw * 2 >= w) {
1656 int ydir;
1658 if (flags & HCONSTRAIN)
1659 ydir = 0;
1660 else
1661 ydir = DOWN;
1662 if (x < cw)
1663 return (LEFT | ydir);
1664 else
1665 return (RIGHT | ydir);
1667 /* vertical resize */
1668 if ((x > cw) && (x < w - cw))
1669 return DOWN;
1671 if (x < cw)
1672 dir = LEFT;
1673 else
1674 dir = RIGHT;
1676 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1677 dir |= DOWN;
1679 return dir;
1683 void
1684 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1686 XEvent event;
1687 WScreen *scr = wwin->screen_ptr;
1688 Window root = scr->root_win;
1689 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1690 int fw = wwin->frame->core->width;
1691 int fh = wwin->frame->core->height;
1692 int fx = wwin->frame_x;
1693 int fy = wwin->frame_y;
1694 int is_resizebar = (wwin->frame->resizebar
1695 && ev->xany.window==wwin->frame->resizebar->window);
1696 int orig_x, orig_y;
1697 int started;
1698 int dw, dh;
1699 int rw = fw, rh = fh;
1700 int rx1, ry1, rx2, ry2;
1701 int res = 0;
1702 KeyCode shiftl, shiftr;
1703 int h = 0;
1704 int orig_fx = fx;
1705 int orig_fy = fy;
1706 int orig_fw = fw;
1707 int orig_fh = fh;
1709 if (wwin->flags.shaded) {
1710 wwarning("internal error: tryein");
1711 return;
1713 orig_x = ev->xbutton.x_root;
1714 orig_y = ev->xbutton.y_root;
1716 started = 0;
1717 #ifdef DEBUG
1718 puts("Resizing window");
1719 #endif
1721 wUnselectWindows(scr);
1722 rx1 = fx;
1723 rx2 = fx + fw - 1;
1724 ry1 = fy;
1725 ry2 = fy + fh - 1;
1726 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1727 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1728 if (!WFLAGP(wwin, no_titlebar))
1729 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
1730 else
1731 h = 0;
1732 while (1) {
1733 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1734 | ButtonPressMask | ExposureMask, &event);
1735 switch (event.type) {
1736 case KeyPress:
1737 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1738 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1739 && started) {
1740 drawTransparentFrame(wwin, fx, fy, fw, fh);
1741 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1742 drawTransparentFrame(wwin, fx, fy, fw, fh);
1744 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1745 break;
1747 case MotionNotify:
1748 if (started) {
1749 dw = 0;
1750 dh = 0;
1752 orig_fx = fx;
1753 orig_fy = fy;
1754 orig_fw = fw;
1755 orig_fh = fh;
1757 if (res & LEFT)
1758 dw = orig_x - event.xmotion.x_root;
1759 else if (res & RIGHT)
1760 dw = event.xmotion.x_root - orig_x;
1761 if (res & UP)
1762 dh = orig_y - event.xmotion.y_root;
1763 else if (res & DOWN)
1764 dh = event.xmotion.y_root - orig_y;
1766 orig_x = event.xmotion.x_root;
1767 orig_y = event.xmotion.y_root;
1769 rw += dw;
1770 rh += dh;
1771 fw = rw;
1772 fh = rh - vert_border;
1773 wWindowConstrainSize(wwin, &fw, &fh);
1774 fh += vert_border;
1775 if (res & LEFT)
1776 fx = rx2 - fw + 1;
1777 else if (res & RIGHT)
1778 fx = rx1;
1779 if (res & UP)
1780 fy = ry2 - fh + 1;
1781 else if (res & DOWN)
1782 fy = ry1;
1783 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1784 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1785 int tx, ty;
1786 Window junkw;
1787 int flags;
1789 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1790 orig_x, orig_y, &tx, &ty, &junkw);
1792 /* check if resizing through resizebar */
1793 if (is_resizebar)
1794 flags = RESIZEBAR;
1795 else
1796 flags = 0;
1798 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1799 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1800 flags |= HCONSTRAIN;
1802 res = getResizeDirection(wwin, tx, ty,
1803 orig_x - event.xmotion.x_root,
1804 orig_y - event.xmotion.y_root, flags);
1806 XChangeActivePointerGrab(dpy, ButtonMotionMask
1807 | ButtonReleaseMask | ButtonPressMask,
1808 wCursor[WCUR_RESIZE], CurrentTime);
1809 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1810 CurrentTime);
1812 XGrabServer(dpy);
1814 /* Draw the resize frame for the first time. */
1815 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1817 drawTransparentFrame(wwin, fx, fy, fw, fh);
1819 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1821 started = 1;
1823 if (started) {
1824 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1825 drawTransparentFrame(wwin, orig_fx, orig_fy,
1826 orig_fw, orig_fh);
1827 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1828 drawTransparentFrame(wwin, fx, fy, fw, fh);
1829 } else {
1830 drawTransparentFrame(wwin, orig_fx, orig_fy,
1831 orig_fw, orig_fh);
1832 drawTransparentFrame(wwin, fx, fy, fw, fh);
1834 if (fh != orig_fh || fw != orig_fw) {
1835 if (wPreferences.size_display == WDIS_NEW) {
1836 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1837 orig_fy + orig_fh, res);
1839 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1842 break;
1844 case ButtonPress:
1845 break;
1847 case ButtonRelease:
1848 if (event.xbutton.button != ev->xbutton.button)
1849 break;
1851 if (started) {
1852 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1854 drawTransparentFrame(wwin, fx, fy, fw, fh);
1856 XUngrabKeyboard(dpy, CurrentTime);
1857 unmapGeometryDisplay(wwin);
1858 XUngrabServer(dpy);
1860 if (wwin->client.width != fw)
1861 wwin->flags.user_changed_width = 1;
1863 if (wwin->client.height != fh - vert_border)
1864 wwin->flags.user_changed_height = 1;
1866 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1868 #ifdef DEBUG
1869 puts("End resize window");
1870 #endif
1871 return;
1873 default:
1874 WMHandleEvent(&event);
1879 #undef LEFT
1880 #undef RIGHT
1881 #undef HORIZONTAL
1882 #undef UP
1883 #undef DOWN
1884 #undef VERTICAL
1885 #undef HCONSTRAIN
1886 #undef RESIZEBAR
1888 void
1889 wUnselectWindows(WScreen *scr)
1891 WWindow *wwin;
1893 while (scr->selected_windows) {
1894 wwin = scr->selected_windows->head;
1895 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
1896 wIconSelect(wwin->icon);
1898 wSelectWindow(wwin, False);
1902 #ifndef LITE
1903 static void
1904 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
1906 WWindow *tmpw;
1908 /* select the windows and put them in the selected window list */
1909 tmpw = scr->focused_window;
1910 while (tmpw != NULL) {
1911 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
1912 if ((tmpw->frame->workspace == scr->current_workspace
1913 || IS_OMNIPRESENT(tmpw))
1914 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
1915 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
1916 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
1917 wSelectWindow(tmpw, True);
1920 tmpw = tmpw->prev;
1925 void
1926 wSelectWindows(WScreen *scr, XEvent *ev)
1928 XEvent event;
1929 Window root = scr->root_win;
1930 GC gc = scr->frame_gc;
1931 int xp = ev->xbutton.x_root;
1932 int yp = ev->xbutton.y_root;
1933 int w = 0, h = 0;
1934 int x = xp, y = yp;
1936 #ifdef DEBUG
1937 puts("Selecting windows");
1938 #endif
1939 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
1940 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1941 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
1942 CurrentTime) != Success) {
1943 return;
1945 XGrabServer(dpy);
1947 wUnselectWindows(scr);
1949 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
1950 while (1) {
1951 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
1952 | ButtonPressMask, &event);
1954 switch (event.type) {
1955 case MotionNotify:
1956 XDrawRectangle(dpy, root, gc, x, y, w, h);
1957 x = event.xmotion.x_root;
1958 if (x < xp) {
1959 w = xp - x;
1960 } else {
1961 w = x - xp;
1962 x = xp;
1964 y = event.xmotion.y_root;
1965 if (y < yp) {
1966 h = yp - y;
1967 } else {
1968 h = y - yp;
1969 y = yp;
1971 XDrawRectangle(dpy, root, gc, x, y, w, h);
1972 break;
1974 case ButtonPress:
1975 break;
1977 case ButtonRelease:
1978 if (event.xbutton.button != ev->xbutton.button)
1979 break;
1981 XDrawRectangle(dpy, root, gc, x, y, w, h);
1982 XUngrabServer(dpy);
1983 XUngrabPointer(dpy, CurrentTime);
1984 selectWindowsInside(scr, x, y, x + w, y + h);
1986 #ifdef KWM_HINTS
1987 wKWMSelectRootRegion(scr, x, y, w, h,
1988 event.xbutton.state & ControlMask);
1989 #endif /* KWM_HINTS */
1991 #ifdef DEBUG
1992 puts("End window selection");
1993 #endif
1994 return;
1996 default:
1997 WMHandleEvent(&event);
1998 break;
2002 #endif /* !LITE */
2004 void
2005 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2006 unsigned width, unsigned height)
2008 WScreen *scr = wwin->screen_ptr;
2009 Window root = scr->root_win;
2010 int x, y, h = 0;
2011 XEvent event;
2012 KeyCode shiftl, shiftr;
2013 Window junkw;
2014 int junk;
2016 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2017 GrabModeAsync, GrabModeAsync, None,
2018 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2019 *x_ret = 0;
2020 *y_ret = 0;
2021 return;
2023 if (!WFLAGP(wwin, no_titlebar)) {
2024 h = scr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
2025 height += h;
2027 if (!WFLAGP(wwin, no_resizebar)) {
2028 height += RESIZEBAR_HEIGHT;
2030 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2031 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2032 (unsigned *) &junk);
2033 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2035 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2037 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2038 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2039 while (1) {
2040 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2041 &event);
2042 switch (event.type) {
2043 case KeyPress:
2044 if ((event.xkey.keycode == shiftl)
2045 || (event.xkey.keycode == shiftr)) {
2046 drawTransparentFrame(wwin,
2047 x - width/2, y - h/2, width, height);
2048 cyclePositionDisplay(wwin,
2049 x - width/2, y - h/2, width, height);
2050 drawTransparentFrame(wwin,
2051 x - width/2, y - h/2, width, height);
2053 break;
2055 case MotionNotify:
2056 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2058 x = event.xmotion.x_root;
2059 y = event.xmotion.y_root;
2061 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2062 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2064 showPosition(wwin, x - width/2, y - h/2);
2066 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2068 break;
2070 case ButtonPress:
2071 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2072 XSync(dpy, 0);
2073 *x_ret = x - width/2;
2074 *y_ret = y - h/2;
2075 XUngrabPointer(dpy, CurrentTime);
2076 XUngrabKeyboard(dpy, CurrentTime);
2077 /* get rid of the geometry window */
2078 unmapPositionDisplay(wwin);
2079 return;
2081 default:
2082 WMHandleEvent(&event);
2083 break;