rearranged the crashing dialog panel code.
[wmaker-crm.git] / src / moveres.c
blob2481e1a034f208404f649c9b4b2562fa3650d5f3
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;
1123 #if 0
1124 typedef struct _looper {
1125 WWindow *wwin;
1126 int x,y,w,h,ox,oy;
1127 } _looper;
1129 void
1130 _keyloop(_looper *lpr){
1131 WWindow *wwin = lpr->wwin;
1132 WScreen *scr = wwin->screen_ptr;
1133 int w = wwin->frame->core->width;
1134 int h = wwin->frame->core->height;
1135 int src_x = wwin->frame_x;
1136 int src_y = wwin->frame_y;
1138 if (!scr->selected_windows){
1139 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1141 XUngrabServer(dpy);
1142 XSync(dpy, False);
1143 wusleep(10000);
1144 XGrabServer(dpy);
1145 /* printf("called\n");*/
1146 if (!scr->selected_windows){
1147 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1149 /* reset timer */
1150 if(scr->keymove_tick)
1151 WMAddTimerHandler(15000,(WMCallback*)_keyloop, lpr);
1154 #endif
1155 #define _KS 20
1158 wKeyboardMoveResizeWindow(WWindow *wwin)
1160 WScreen *scr = wwin->screen_ptr;
1161 Window root = scr->root_win;
1162 XEvent event;
1163 int w = wwin->frame->core->width;
1164 int h = wwin->frame->core->height;
1165 int scr_width = wwin->screen_ptr->scr_width;
1166 int scr_height = wwin->screen_ptr->scr_height;
1167 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1168 int src_x = wwin->frame_x;
1169 int src_y = wwin->frame_y;
1170 int done,off_x,off_y,ww,wh;
1171 int kspeed = 1;
1172 Time lastTime = 0;
1173 KeySym keysym=NoSymbol;
1174 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1177 int timer;
1178 _looper looper;
1179 looper.wwin=wwin;
1180 scr->keymove_tick=1;
1181 WMAddTimerHandler(1000,(WMCallback*)_keyloop, &looper);
1184 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1185 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1186 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1187 ctrlmode=done=off_x=off_y=0;
1189 XSync(dpy, False);
1190 wusleep(10000);
1191 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1193 if (!wwin->flags.selected) {
1194 wUnselectWindows(scr);
1196 XGrabServer(dpy);
1197 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1198 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1199 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1201 if (wwin->flags.shaded || scr->selected_windows) {
1202 if(scr->selected_windows)
1203 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1204 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1205 if(!scr->selected_windows)
1206 mapPositionDisplay(wwin, src_x, src_y, w, h);
1207 } else {
1208 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1210 ww=w;
1211 wh=h;
1212 while(1) {
1214 looper.ox=off_x;
1215 looper.oy=off_y;
1217 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1218 | ButtonPressMask | ExposureMask, &event);
1219 if (wwin->flags.shaded || scr->selected_windows) {
1220 if(scr->selected_windows)
1221 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1222 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1223 /*** I HATE EDGE RESISTANCE - ]d ***/
1225 else {
1226 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1229 if(ctrlmode)
1230 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1232 XUngrabServer(dpy);
1233 XSync(dpy, False);
1235 switch (event.type) {
1236 case KeyPress:
1237 /* accelerate */
1238 if (event.xkey.time - lastTime > 50) {
1239 kspeed = 1;
1240 } else {
1241 if (kspeed < 20)
1242 kspeed++;
1244 lastTime = event.xkey.time;
1246 if (event.xkey.state & ControlMask && !wwin->flags.shaded){
1247 ctrlmode=1;
1248 wUnselectWindows(scr);
1250 else {
1251 ctrlmode=0;
1253 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr){
1254 if(ctrlmode)
1255 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1256 else
1257 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1259 else {
1260 keysym = XLookupKeysym(&event.xkey, 0);
1261 switch(keysym){
1262 case XK_Return:
1263 done=2;
1264 break;
1265 case XK_Escape:
1266 done=1;
1267 break;
1268 case XK_Up:
1269 case XK_KP_Up:
1270 case XK_k:
1271 if (ctrlmode){
1272 h-=kspeed;
1274 else off_y-=kspeed;
1275 break;
1276 case XK_Down:
1277 case XK_KP_Down:
1278 case XK_j:
1279 if (ctrlmode){
1280 h+=kspeed;
1282 else off_y+=kspeed;
1283 break;
1284 case XK_Left:
1285 case XK_KP_Left:
1286 case XK_h:
1287 if (ctrlmode){
1288 w-=kspeed;
1290 else off_x-=kspeed;
1291 break;
1292 case XK_Right:
1293 case XK_KP_Right:
1294 case XK_l:
1295 if (ctrlmode){
1296 w+=kspeed;
1298 else off_x+=kspeed;
1299 break;
1301 ww=w;wh=h;
1302 wh-=vert_border;
1303 wWindowConstrainSize(wwin, &ww, &wh);
1304 wh+=vert_border;
1306 if (wPreferences.ws_cycle){
1307 if (src_x + off_x + wwin->frame->core->width < 20){
1308 if(!scr->current_workspace) {
1309 wWorkspaceChange(scr, scr->workspace_count-1);
1311 else wWorkspaceChange(scr, scr->current_workspace-1);
1312 off_x += scr_width;
1314 else if (src_x + off_x + 20 > scr_width){
1315 if(scr->current_workspace == scr->workspace_count-1) {
1316 wWorkspaceChange(scr, 0);
1318 else wWorkspaceChange(scr, scr->current_workspace+1);
1319 off_x -= scr_width;
1322 else {
1323 if (src_x + off_x + wwin->frame->core->width < 20)
1324 off_x = 20 - wwin->frame->core->width - src_x;
1325 else if (src_x + off_x + 20 > scr_width)
1326 off_x = scr_width - 20 - src_x;
1329 if (src_y + off_y + wwin->frame->core->height < 20)
1330 off_y = 20 - wwin->frame->core->height - src_y;
1331 else if (src_y + off_y + 20 > scr_height)
1332 off_y = scr_height - 20 - src_y;
1335 break;
1336 case ButtonPress:
1337 case ButtonRelease:
1338 done=1;
1339 break;
1340 default:
1341 WMHandleEvent(&event);
1342 break;
1345 XGrabServer(dpy);
1346 /*xxx*/
1348 if (wwin->flags.shaded && !scr->selected_windows){
1349 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1351 else {
1352 if(ctrlmode){
1353 unmapPositionDisplay(wwin);
1354 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1356 else if(!scr->selected_windows){
1357 unmapGeometryDisplay(wwin);
1358 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
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);
1372 if(ctrlmode){
1373 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1375 else if(!scr->selected_windows)
1376 showPosition(wwin, src_x+off_x, src_y+off_y);
1377 /**/
1379 if(done){
1380 scr->keymove_tick=0;
1382 WMDeleteTimerWithClientData(&looper);
1384 if (wwin->flags.shaded || scr->selected_windows) {
1385 if(scr->selected_windows)
1386 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1387 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1389 else {
1390 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1393 if(ctrlmode){
1394 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1395 unmapGeometryDisplay(wwin);
1397 else
1398 unmapPositionDisplay(wwin);
1399 XUngrabKeyboard(dpy, CurrentTime);
1400 XUngrabPointer(dpy, CurrentTime);
1401 XUngrabServer(dpy);
1403 if(done==2) {
1404 if (wwin->flags.shaded || scr->selected_windows) {
1405 LinkedList *list;
1406 list=scr->selected_windows;
1407 if (!scr->selected_windows) {
1408 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1409 wWindowSynthConfigureNotify(wwin);
1410 } else {
1411 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1412 while (list) {
1413 wWindowSynthConfigureNotify(list->head);
1414 list = list->tail;
1417 } else {
1418 if (wwin->client.width != ww)
1419 wwin->flags.user_changed_width = 1;
1421 if (wwin->client.height != wh - vert_border)
1422 wwin->flags.user_changed_height = 1;
1424 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1425 ww, wh - vert_border);
1426 wWindowSynthConfigureNotify(wwin);
1428 wWindowChangeWorkspace(wwin, scr->current_workspace);
1429 wSetFocusTo(scr, wwin);
1431 return 1;
1438 *----------------------------------------------------------------------
1439 * wMouseMoveWindow--
1440 * Move the named window and the other selected ones (if any),
1441 * interactively. Also shows the position of the window, if only one
1442 * window is being moved.
1443 * If the window is not on the selected window list, the selected
1444 * windows are deselected.
1445 * If shift is pressed during the operation, the position display
1446 * is changed to another type.
1448 * Returns:
1449 * True if the window was moved, False otherwise.
1451 * Side effects:
1452 * The window(s) position is changed, and the client(s) are
1453 * notified about that.
1454 * The position display configuration may be changed.
1455 *----------------------------------------------------------------------
1458 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1460 WScreen *scr = wwin->screen_ptr;
1461 XEvent event;
1462 Window root = scr->root_win;
1463 KeyCode shiftl, shiftr;
1464 int started = 0;
1465 int warped = 0;
1466 /* This needs not to change while moving, else bad things can happen */
1467 int opaqueMove = wPreferences.opaque_move;
1468 MoveData moveData;
1469 #ifdef GHOST_WINDOW_MOVE
1470 RImage *rimg;
1472 rimg = InitGhostWindowMove(scr);
1473 #endif
1475 initMoveData(wwin, &moveData);
1477 moveData.mouseX = ev->xmotion.x_root;
1478 moveData.mouseY = ev->xmotion.y_root;
1480 if (!wwin->flags.selected) {
1481 /* this window is not selected, unselect others and move only wwin */
1482 wUnselectWindows(scr);
1484 #ifdef DEBUG
1485 puts("Moving window");
1486 #endif
1487 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1488 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1489 while (1) {
1490 if (warped) {
1491 int junk;
1492 Window junkw;
1494 /* XWarpPointer() doesn't seem to generate Motion events, so
1495 we've got to simulate them */
1496 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1497 &event.xmotion.y_root, &junk, &junk,
1498 (unsigned *) &junk);
1499 } else {
1500 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1501 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1502 &event);
1504 if (event.type == MotionNotify) {
1505 /* compress MotionNotify events */
1506 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1509 switch (event.type) {
1510 case KeyPress:
1511 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1512 && started && !scr->selected_windows) {
1514 if (!opaqueMove) {
1515 drawFrames(wwin, scr->selected_windows,
1516 moveData.realX - wwin->frame_x,
1517 moveData.realY - wwin->frame_y);
1520 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1521 moveData.winWidth, moveData.winHeight);
1523 if (!opaqueMove) {
1524 drawFrames(wwin, scr->selected_windows,
1525 moveData.realX - wwin->frame_x,
1526 moveData.realY - wwin->frame_y);
1529 break;
1531 case MotionNotify:
1532 if (started) {
1533 updateWindowPosition(wwin, &moveData,
1534 scr->selected_windows == NULL
1535 && wPreferences.edge_resistance > 0,
1536 opaqueMove,
1537 event.xmotion.x_root,
1538 event.xmotion.y_root);
1540 if (!warped && !wPreferences.no_autowrap) {
1541 int oldWorkspace = scr->current_workspace;
1543 if (!opaqueMove) {
1544 drawFrames(wwin, scr->selected_windows,
1545 moveData.realX - wwin->frame_x,
1546 moveData.realY - wwin->frame_y);
1548 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1549 if (scr->current_workspace != oldWorkspace
1550 && wPreferences.edge_resistance > 0
1551 && scr->selected_windows == NULL)
1552 updateMoveData(wwin, &moveData);
1553 warped = 1;
1556 if (!opaqueMove) {
1557 drawFrames(wwin, scr->selected_windows,
1558 moveData.realX - wwin->frame_x,
1559 moveData.realY - wwin->frame_y);
1561 } else {
1562 warped = 0;
1564 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1565 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1567 XChangeActivePointerGrab(dpy, ButtonMotionMask
1568 | ButtonReleaseMask | ButtonPressMask,
1569 wCursor[WCUR_MOVE], CurrentTime);
1570 started = 1;
1571 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1572 CurrentTime);
1574 if (!scr->selected_windows)
1575 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1576 moveData.winWidth, moveData.winHeight);
1578 if (started && !opaqueMove)
1579 drawFrames(wwin, scr->selected_windows, 0, 0);
1581 if (!opaqueMove)
1582 XGrabServer(dpy);
1584 break;
1586 case ButtonPress:
1587 break;
1589 case ButtonRelease:
1590 if (event.xbutton.button != ev->xbutton.button)
1591 break;
1593 if (started) {
1594 if (!opaqueMove) {
1595 drawFrames(wwin, scr->selected_windows,
1596 moveData.realX - wwin->frame_x,
1597 moveData.realY - wwin->frame_y);
1598 XSync(dpy, 0);
1599 doWindowMove(wwin, scr->selected_windows,
1600 moveData.realX - wwin->frame_x,
1601 moveData.realY - wwin->frame_y);
1603 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1604 wWindowSynthConfigureNotify(wwin);
1605 #endif
1606 XUngrabKeyboard(dpy, CurrentTime);
1607 XUngrabServer(dpy);
1608 if (!opaqueMove) {
1609 wWindowChangeWorkspace(wwin, scr->current_workspace);
1610 wSetFocusTo(scr, wwin);
1612 if (wPreferences.move_display == WDIS_NEW)
1613 showPosition(wwin, moveData.realX, moveData.realY);
1615 if (!scr->selected_windows) {
1616 /* get rid of the geometry window */
1617 unmapPositionDisplay(wwin);
1620 #ifdef DEBUG
1621 puts("End move window");
1622 #endif
1623 freeMoveData(&moveData);
1625 return started;
1627 default:
1628 if (started && !opaqueMove) {
1629 drawFrames(wwin, scr->selected_windows,
1630 moveData.realX - wwin->frame_x,
1631 moveData.realY - wwin->frame_y);
1632 XUngrabServer(dpy);
1633 WMHandleEvent(&event);
1634 XSync(dpy, False);
1635 XGrabServer(dpy);
1636 drawFrames(wwin, scr->selected_windows,
1637 moveData.realX - wwin->frame_x,
1638 moveData.realY - wwin->frame_y);
1639 } else {
1640 WMHandleEvent(&event);
1642 break;
1646 freeMoveData(&moveData);
1648 return 0;
1652 #define RESIZEBAR 1
1653 #define HCONSTRAIN 2
1655 static int
1656 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1657 int flags)
1659 int w = wwin->frame->core->width - 1;
1660 int cw = wwin->frame->resizebar_corner_width;
1661 int dir;
1663 /* if not resizing through the resizebar */
1664 if (!(flags & RESIZEBAR)) {
1665 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1666 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1667 if (abs(dx) < 2 || abs(dy) < 2) {
1668 if (abs(dy) > abs(dx))
1669 xdir = 0;
1670 else
1671 ydir = 0;
1673 return (xdir | ydir);
1676 /* window is too narrow. allow diagonal resize */
1677 if (cw * 2 >= w) {
1678 int ydir;
1680 if (flags & HCONSTRAIN)
1681 ydir = 0;
1682 else
1683 ydir = DOWN;
1684 if (x < cw)
1685 return (LEFT | ydir);
1686 else
1687 return (RIGHT | ydir);
1689 /* vertical resize */
1690 if ((x > cw) && (x < w - cw))
1691 return DOWN;
1693 if (x < cw)
1694 dir = LEFT;
1695 else
1696 dir = RIGHT;
1698 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1699 dir |= DOWN;
1701 return dir;
1705 void
1706 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1708 XEvent event;
1709 WScreen *scr = wwin->screen_ptr;
1710 Window root = scr->root_win;
1711 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1712 int fw = wwin->frame->core->width;
1713 int fh = wwin->frame->core->height;
1714 int fx = wwin->frame_x;
1715 int fy = wwin->frame_y;
1716 int is_resizebar = (wwin->frame->resizebar
1717 && ev->xany.window==wwin->frame->resizebar->window);
1718 int orig_x, orig_y;
1719 int started;
1720 int dw, dh;
1721 int rw = fw, rh = fh;
1722 int rx1, ry1, rx2, ry2;
1723 int res = 0;
1724 KeyCode shiftl, shiftr;
1725 int h = 0;
1726 int orig_fx = fx;
1727 int orig_fy = fy;
1728 int orig_fw = fw;
1729 int orig_fh = fh;
1731 if (wwin->flags.shaded) {
1732 wwarning("internal error: tryein");
1733 return;
1735 orig_x = ev->xbutton.x_root;
1736 orig_y = ev->xbutton.y_root;
1738 started = 0;
1739 #ifdef DEBUG
1740 puts("Resizing window");
1741 #endif
1743 wUnselectWindows(scr);
1744 rx1 = fx;
1745 rx2 = fx + fw - 1;
1746 ry1 = fy;
1747 ry2 = fy + fh - 1;
1748 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1749 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1750 if (!WFLAGP(wwin, no_titlebar))
1751 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
1752 else
1753 h = 0;
1754 while (1) {
1755 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1756 | ButtonPressMask | ExposureMask, &event);
1757 switch (event.type) {
1758 case KeyPress:
1759 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1760 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1761 && started) {
1762 drawTransparentFrame(wwin, fx, fy, fw, fh);
1763 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1764 drawTransparentFrame(wwin, fx, fy, fw, fh);
1766 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1767 break;
1769 case MotionNotify:
1770 if (started) {
1771 dw = 0;
1772 dh = 0;
1774 orig_fx = fx;
1775 orig_fy = fy;
1776 orig_fw = fw;
1777 orig_fh = fh;
1779 if (res & LEFT)
1780 dw = orig_x - event.xmotion.x_root;
1781 else if (res & RIGHT)
1782 dw = event.xmotion.x_root - orig_x;
1783 if (res & UP)
1784 dh = orig_y - event.xmotion.y_root;
1785 else if (res & DOWN)
1786 dh = event.xmotion.y_root - orig_y;
1788 orig_x = event.xmotion.x_root;
1789 orig_y = event.xmotion.y_root;
1791 rw += dw;
1792 rh += dh;
1793 fw = rw;
1794 fh = rh - vert_border;
1795 wWindowConstrainSize(wwin, &fw, &fh);
1796 fh += vert_border;
1797 if (res & LEFT)
1798 fx = rx2 - fw + 1;
1799 else if (res & RIGHT)
1800 fx = rx1;
1801 if (res & UP)
1802 fy = ry2 - fh + 1;
1803 else if (res & DOWN)
1804 fy = ry1;
1805 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1806 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1807 int tx, ty;
1808 Window junkw;
1809 int flags;
1811 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1812 orig_x, orig_y, &tx, &ty, &junkw);
1814 /* check if resizing through resizebar */
1815 if (is_resizebar)
1816 flags = RESIZEBAR;
1817 else
1818 flags = 0;
1820 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1821 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1822 flags |= HCONSTRAIN;
1824 res = getResizeDirection(wwin, tx, ty,
1825 orig_x - event.xmotion.x_root,
1826 orig_y - event.xmotion.y_root, flags);
1828 XChangeActivePointerGrab(dpy, ButtonMotionMask
1829 | ButtonReleaseMask | ButtonPressMask,
1830 wCursor[WCUR_RESIZE], CurrentTime);
1831 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1832 CurrentTime);
1834 XGrabServer(dpy);
1836 /* Draw the resize frame for the first time. */
1837 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1839 drawTransparentFrame(wwin, fx, fy, fw, fh);
1841 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1843 started = 1;
1845 if (started) {
1846 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1847 drawTransparentFrame(wwin, orig_fx, orig_fy,
1848 orig_fw, orig_fh);
1849 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1850 drawTransparentFrame(wwin, fx, fy, fw, fh);
1851 } else {
1852 drawTransparentFrame(wwin, orig_fx, orig_fy,
1853 orig_fw, orig_fh);
1854 drawTransparentFrame(wwin, fx, fy, fw, fh);
1856 if (fh != orig_fh || fw != orig_fw) {
1857 if (wPreferences.size_display == WDIS_NEW) {
1858 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1859 orig_fy + orig_fh, res);
1861 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1864 break;
1866 case ButtonPress:
1867 break;
1869 case ButtonRelease:
1870 if (event.xbutton.button != ev->xbutton.button)
1871 break;
1873 if (started) {
1874 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1876 drawTransparentFrame(wwin, fx, fy, fw, fh);
1878 XUngrabKeyboard(dpy, CurrentTime);
1879 unmapGeometryDisplay(wwin);
1880 XUngrabServer(dpy);
1882 if (wwin->client.width != fw)
1883 wwin->flags.user_changed_width = 1;
1885 if (wwin->client.height != fh - vert_border)
1886 wwin->flags.user_changed_height = 1;
1888 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1890 #ifdef DEBUG
1891 puts("End resize window");
1892 #endif
1893 return;
1895 default:
1896 WMHandleEvent(&event);
1901 #undef LEFT
1902 #undef RIGHT
1903 #undef HORIZONTAL
1904 #undef UP
1905 #undef DOWN
1906 #undef VERTICAL
1907 #undef HCONSTRAIN
1908 #undef RESIZEBAR
1910 void
1911 wUnselectWindows(WScreen *scr)
1913 WWindow *wwin;
1915 while (scr->selected_windows) {
1916 wwin = scr->selected_windows->head;
1917 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
1918 wIconSelect(wwin->icon);
1920 wSelectWindow(wwin, False);
1924 #ifndef LITE
1925 static void
1926 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
1928 WWindow *tmpw;
1930 /* select the windows and put them in the selected window list */
1931 tmpw = scr->focused_window;
1932 while (tmpw != NULL) {
1933 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
1934 if ((tmpw->frame->workspace == scr->current_workspace
1935 || IS_OMNIPRESENT(tmpw))
1936 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
1937 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
1938 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
1939 wSelectWindow(tmpw, True);
1942 tmpw = tmpw->prev;
1947 void
1948 wSelectWindows(WScreen *scr, XEvent *ev)
1950 XEvent event;
1951 Window root = scr->root_win;
1952 GC gc = scr->frame_gc;
1953 int xp = ev->xbutton.x_root;
1954 int yp = ev->xbutton.y_root;
1955 int w = 0, h = 0;
1956 int x = xp, y = yp;
1958 #ifdef DEBUG
1959 puts("Selecting windows");
1960 #endif
1961 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
1962 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1963 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
1964 CurrentTime) != Success) {
1965 return;
1967 XGrabServer(dpy);
1969 wUnselectWindows(scr);
1971 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
1972 while (1) {
1973 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
1974 | ButtonPressMask, &event);
1976 switch (event.type) {
1977 case MotionNotify:
1978 XDrawRectangle(dpy, root, gc, x, y, w, h);
1979 x = event.xmotion.x_root;
1980 if (x < xp) {
1981 w = xp - x;
1982 } else {
1983 w = x - xp;
1984 x = xp;
1986 y = event.xmotion.y_root;
1987 if (y < yp) {
1988 h = yp - y;
1989 } else {
1990 h = y - yp;
1991 y = yp;
1993 XDrawRectangle(dpy, root, gc, x, y, w, h);
1994 break;
1996 case ButtonPress:
1997 break;
1999 case ButtonRelease:
2000 if (event.xbutton.button != ev->xbutton.button)
2001 break;
2003 XDrawRectangle(dpy, root, gc, x, y, w, h);
2004 XUngrabServer(dpy);
2005 XUngrabPointer(dpy, CurrentTime);
2006 selectWindowsInside(scr, x, y, x + w, y + h);
2008 #ifdef KWM_HINTS
2009 wKWMSelectRootRegion(scr, x, y, w, h,
2010 event.xbutton.state & ControlMask);
2011 #endif /* KWM_HINTS */
2013 #ifdef DEBUG
2014 puts("End window selection");
2015 #endif
2016 return;
2018 default:
2019 WMHandleEvent(&event);
2020 break;
2024 #endif /* !LITE */
2026 void
2027 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2028 unsigned width, unsigned height)
2030 WScreen *scr = wwin->screen_ptr;
2031 Window root = scr->root_win;
2032 int x, y, h = 0;
2033 XEvent event;
2034 KeyCode shiftl, shiftr;
2035 Window junkw;
2036 int junk;
2038 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2039 GrabModeAsync, GrabModeAsync, None,
2040 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2041 *x_ret = 0;
2042 *y_ret = 0;
2043 return;
2045 if (!WFLAGP(wwin, no_titlebar)) {
2046 h = scr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
2047 height += h;
2049 if (!WFLAGP(wwin, no_resizebar)) {
2050 height += RESIZEBAR_HEIGHT;
2052 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2053 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2054 (unsigned *) &junk);
2055 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2057 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2059 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2060 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2061 while (1) {
2062 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2063 &event);
2064 switch (event.type) {
2065 case KeyPress:
2066 if ((event.xkey.keycode == shiftl)
2067 || (event.xkey.keycode == shiftr)) {
2068 drawTransparentFrame(wwin,
2069 x - width/2, y - h/2, width, height);
2070 cyclePositionDisplay(wwin,
2071 x - width/2, y - h/2, width, height);
2072 drawTransparentFrame(wwin,
2073 x - width/2, y - h/2, width, height);
2075 break;
2077 case MotionNotify:
2078 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2080 x = event.xmotion.x_root;
2081 y = event.xmotion.y_root;
2083 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2084 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2086 showPosition(wwin, x - width/2, y - h/2);
2088 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2090 break;
2092 case ButtonPress:
2093 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2094 XSync(dpy, 0);
2095 *x_ret = x - width/2;
2096 *y_ret = y - h/2;
2097 XUngrabPointer(dpy, CurrentTime);
2098 XUngrabKeyboard(dpy, CurrentTime);
2099 /* get rid of the geometry window */
2100 unmapPositionDisplay(wwin);
2101 return;
2103 default:
2104 WMHandleEvent(&event);
2105 break;