new/changelog fix
[wmaker-crm.git] / src / moveres.c
blob01a132689fc553db27e81f2b9b7f91f0a9edada8
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>
31 #include "WindowMaker.h"
32 #include "wcore.h"
33 #include "framewin.h"
34 #include "window.h"
35 #include "client.h"
36 #include "icon.h"
37 #include "dock.h"
38 #include "funcs.h"
39 #include "actions.h"
40 #include "workspace.h"
42 #include "list.h"
44 /* How many different types of geometry/position
45 display thingies are there? */
46 #define NUM_DISPLAYS 4
48 #define LEFT 1
49 #define RIGHT 2
50 #define HORIZONTAL (LEFT|RIGHT)
51 #define UP 4
52 #define DOWN 8
53 #define VERTICAL (UP|DOWN)
55 /****** Global Variables ******/
56 extern Time LastTimestamp;
58 extern Cursor wCursor[WCUR_LAST];
60 extern WPreferences wPreferences;
62 extern Atom _XA_WM_PROTOCOLS;
66 void
67 wGetGeometryWindowSize(WScreen *scr, unsigned int *width,
68 unsigned int *height)
70 #ifdef I18N_MB
71 *width = XmbTextEscapement(scr->info_text_font->font, "-8888 x -8888", 13);
72 *height = (7 * scr->info_text_font->height) / 4 - 1;
73 #else
74 *width = XTextWidth(scr->info_text_font->font, "-8888 x -8888", 13);
75 *height = (7 * scr->info_text_font->font->ascent) / 4 - 1;
76 #endif
81 *----------------------------------------------------------------------
82 * moveGeometryDisplayCentered
84 * routine that moves the geometry/position window on scr so it is
85 * centered over the given coordinates (x,y). Also the window position
86 * is clamped so it stays on the screen at all times.
87 *----------------------------------------------------------------------
89 static void
90 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
92 x -= scr->geometry_display_width / 2;
93 y -= scr->geometry_display_height / 2;
95 if (x < 1)
96 x = 1;
97 else if (x > (scr->scr_width - scr->geometry_display_width - 3))
98 x = scr->scr_width - scr->geometry_display_width - 3;
100 if (y < 1)
101 y = 1;
102 else if (y > (scr->scr_height - scr->geometry_display_height - 3))
103 y = scr->scr_height - scr->geometry_display_height - 3;
105 XMoveWindow(dpy, scr->geometry_display, x, y);
109 static void
110 showPosition(WWindow *wwin, int x, int y)
112 WScreen *scr = wwin->screen_ptr;
113 GC gc = scr->info_text_gc;
114 char num[16];
115 int fw, fh;
117 if (wPreferences.move_display == WDIS_NEW) {
118 #if 0
119 int width = wwin->frame->core->width;
120 int height = wwin->frame->core->height;
122 GC lgc = scr->line_gc;
123 XSetForeground(dpy, lgc, scr->line_pixel);
124 sprintf(num, "%i", x);
126 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
127 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
128 y+height+2);
129 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
130 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
131 scr->scr_height);
132 #endif
133 } else {
134 XClearArea(dpy, scr->geometry_display, 1, 1,
135 scr->geometry_display_width-2, scr->geometry_display_height-2,
136 False);
137 sprintf(num, "%+i %-+i", x, y);
138 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
140 XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]);
142 fh = scr->info_text_font->height;
143 wDrawString(scr->geometry_display, scr->info_text_font, gc,
144 (scr->geometry_display_width - 2 - fw) / 2,
145 (scr->geometry_display_height-fh)/2 + scr->info_text_font->y,
146 num, strlen(num));
147 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
148 scr->geometry_display_height+1, scr->resizebar_texture[0],
149 WREL_RAISED);
154 static void
155 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
157 WScreen *scr = wwin->screen_ptr;
159 wPreferences.move_display++;
160 wPreferences.move_display %= NUM_DISPLAYS;
162 if (wPreferences.move_display == WDIS_NEW) {
163 XUnmapWindow(dpy, scr->geometry_display);
164 } else {
165 if (wPreferences.move_display == WDIS_CENTER) {
166 moveGeometryDisplayCentered(scr,
167 scr->scr_width/2, scr->scr_height/2);
168 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
169 moveGeometryDisplayCentered(scr, 1, 1);
170 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
171 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
173 XMapRaised(dpy, scr->geometry_display);
174 showPosition(wwin, x, y);
179 static void
180 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
182 WScreen *scr = wwin->screen_ptr;
184 if (wPreferences.move_display == WDIS_NEW) {
185 return;
186 } else if (wPreferences.move_display == WDIS_CENTER) {
187 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
188 scr->scr_height / 2);
189 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
190 moveGeometryDisplayCentered(scr, 1, 1);
191 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
192 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
194 XMapRaised(dpy, scr->geometry_display);
195 showPosition(wwin, x, y);
198 #define unmapPositionDisplay(w) \
199 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
202 static void
203 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
205 WScreen *scr = wwin->screen_ptr;
206 Window root = scr->root_win;
207 GC gc = scr->line_gc;
208 int ty, by, my, x, y, mx, s;
209 char num[16];
210 XSegment segment[4];
211 int fw, fh;
213 ty = y1 + wwin->frame->top_width;
214 by = y2 - wwin->frame->bottom_width;
215 fw = wTextWidth(scr->info_text_font->font, "8888", 4);
216 fh = scr->info_text_font->height;
218 if (wPreferences.size_display == WDIS_NEW) {
219 XSetForeground(dpy, gc, scr->line_pixel);
221 /* vertical geometry */
222 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
223 x = x2;
224 s = -15;
225 } else {
226 x = x1;
227 s = 15;
229 my = (ty + by) / 2;
231 /* top arrow */
232 /* end bar */
233 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
234 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
236 /* arrowhead */
237 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
238 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
240 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
241 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
243 /* line */
244 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
245 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
247 XDrawSegments(dpy, root, gc, segment, 4);
249 /* bottom arrow */
250 /* end bar */
251 segment[0].y1 = by;
252 segment[0].y2 = by;
254 /* arrowhead */
255 segment[1].y1 = by - 1;
256 segment[1].y2 = by - 7;
258 segment[2].y1 = by - 1;
259 segment[2].y2 = by - 7;
261 /* line */
262 segment[3].y1 = my + fh/2 + 2;
263 segment[3].y2 = by - 1;
265 XDrawSegments(dpy, root, gc, segment, 4);
267 sprintf(num, "%i", (by - ty - wwin->normal_hints->base_height) /
268 wwin->normal_hints->height_inc);
269 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
271 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
273 /* Display the height. */
274 wDrawString(root, scr->info_text_font, gc,
275 x - s + 3 - fw/2, my - fh/2 + scr->info_text_font->y + 1,
276 num, strlen(num));
277 XSetForeground(dpy, gc, scr->line_pixel);
278 /* horizontal geometry */
279 if (y1 < 15) {
280 y = y2;
281 s = -15;
282 } else {
283 y = y1;
284 s = 15;
286 mx = x1 + (x2 - x1)/2;
287 sprintf(num, "%i", (x2 - x1 - wwin->normal_hints->base_width) /
288 wwin->normal_hints->width_inc);
289 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
291 /* left arrow */
292 /* end bar */
293 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
294 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
296 /* arrowhead */
297 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
298 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
300 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
301 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
303 /* line */
304 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
305 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
307 XDrawSegments(dpy, root, gc, segment, 4);
309 /* right arrow */
310 /* end bar */
311 segment[0].x1 = x2 + 1;
312 segment[0].x2 = x2 + 1;
314 /* arrowhead */
315 segment[1].x1 = x2 - 6;
316 segment[1].x2 = x2;
318 segment[2].x1 = x2;
319 segment[2].x2 = x2 - 6;
321 /* line */
322 segment[3].x1 = mx + fw/2 + 2;
323 segment[3].x2 = x2;
325 XDrawSegments(dpy, root, gc, segment, 4);
327 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
329 /* Display the width. */
330 wDrawString(root, scr->info_text_font, gc,
331 mx - fw/2 + 1, y - s + fh/2 + 1, num, strlen(num));
332 } else {
333 XClearArea(dpy, scr->geometry_display, 1, 1,
334 scr->geometry_display_width-2, scr->geometry_display_height-2,
335 False);
336 sprintf(num, "%i x %-i", (x2 - x1 - wwin->normal_hints->base_width)
337 / wwin->normal_hints->width_inc,
338 (by - ty - wwin->normal_hints->base_height)
339 / wwin->normal_hints->height_inc);
340 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
342 XSetForeground(dpy, scr->info_text_gc,
343 scr->window_title_pixel[WS_UNFOCUSED]);
345 /* Display the height. */
346 wDrawString(scr->geometry_display, scr->info_text_font,
347 scr->info_text_gc,
348 (scr->geometry_display_width-fw)/2,
349 (scr->geometry_display_height-fh)/2 +scr->info_text_font->y,
350 num, strlen(num));
351 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
352 scr->geometry_display_height+1, scr->resizebar_texture[0],
353 WREL_RAISED);
358 static void
359 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
361 WScreen *scr = wwin->screen_ptr;
363 wPreferences.size_display++;
364 wPreferences.size_display %= NUM_DISPLAYS;
366 if (wPreferences.size_display == WDIS_NEW) {
367 XUnmapWindow(dpy, scr->geometry_display);
368 } else {
369 if (wPreferences.size_display == WDIS_CENTER) {
370 moveGeometryDisplayCentered(scr,
371 scr->scr_width / 2, scr->scr_height / 2);
372 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
373 moveGeometryDisplayCentered(scr, 1, 1);
374 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
375 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
377 XMapRaised(dpy, scr->geometry_display);
378 showGeometry(wwin, x, y, x + w, y + h, dir);
383 static void
384 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
386 WScreen *scr = wwin->screen_ptr;
388 if (wPreferences.size_display == WDIS_NEW)
389 return;
391 if (wPreferences.size_display == WDIS_CENTER) {
392 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
393 scr->scr_height / 2);
394 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
395 moveGeometryDisplayCentered(scr, 1, 1);
396 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
397 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
399 XMapRaised(dpy, scr->geometry_display);
400 showGeometry(wwin, x, y, x + w, y + h, 0);
403 #define unmapGeometryDisplay(w) \
404 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
407 static void
408 doWindowMove(WWindow *wwin, LinkedList *list, int dx, int dy)
410 WWindow *tmpw;
411 int x, y;
412 int scr_width = wwin->screen_ptr->scr_width;
413 int scr_height = wwin->screen_ptr->scr_height;
415 if (!list) {
416 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
417 } else {
418 while (list) {
419 tmpw = list->head;
420 x = tmpw->frame_x + dx;
421 y = tmpw->frame_y + dy;
423 /* don't let windows become unreachable */
425 if (x + (int)tmpw->frame->core->width < 20)
426 x = 20 - (int)tmpw->frame->core->width;
427 else if (x + 20 > scr_width)
428 x = scr_width - 20;
430 if (y + (int)tmpw->frame->core->height < 20)
431 y = 20 - (int)tmpw->frame->core->height;
432 else if (y + 20 > scr_height)
433 y = scr_height - 20;
435 wWindowMove(tmpw, x, y);
436 list = list->tail;
442 static void
443 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
445 Window root = wwin->screen_ptr->root_win;
446 GC gc = wwin->screen_ptr->frame_gc;
447 int h = 0;
448 int bottom = 0;
450 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
451 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
453 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
454 /* Can't use wwin-frame->bottom_width because, in some cases
455 (e.g. interactive placement), frame does not point to anything. */
456 bottom = RESIZEBAR_HEIGHT - 1;
458 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
460 if (h > 0) {
461 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
463 if (bottom > 0) {
464 XDrawLine(dpy, root, gc, x + 1,
465 y + height - bottom,
466 x + width + 1,
467 y + height - bottom);
472 static void
473 drawFrames(WWindow *wwin, LinkedList *list, int dx, int dy)
475 WWindow *tmpw;
476 int scr_width = wwin->screen_ptr->scr_width;
477 int scr_height = wwin->screen_ptr->scr_height;
478 int x, y;
480 if (!list) {
482 x = wwin->frame_x + dx;
483 y = wwin->frame_y + dy;
485 drawTransparentFrame(wwin, x, y,
486 wwin->frame->core->width,
487 wwin->frame->core->height);
489 } else {
490 while (list) {
491 tmpw = list->head;
492 x = tmpw->frame_x + dx;
493 y = tmpw->frame_y + dy;
495 /* don't let windows become unreachable */
497 if (x + (int)tmpw->frame->core->width < 20)
498 x = 20 - (int)tmpw->frame->core->width;
499 else if (x + 20 > scr_width)
500 x = scr_width - 20;
502 if (y + (int)tmpw->frame->core->height < 20)
503 y = 20 - (int)tmpw->frame->core->height;
504 else if (y + 20 > scr_height)
505 y = scr_height - 20;
507 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
508 tmpw->frame->core->height);
510 list = list->tail;
517 static void
518 flushMotion()
520 XEvent ev;
522 XSync(dpy, False);
523 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
527 static void
528 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
529 int new_workspace, int rewind)
531 /* do not let window be unmapped */
532 if (opaque_move) {
533 wwin->flags.changing_workspace = 1;
534 wWindowChangeWorkspace(wwin, new_workspace);
536 /* go to new workspace */
537 wWorkspaceChange(scr, new_workspace);
539 wwin->flags.changing_workspace = 0;
541 if (rewind)
542 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
543 else
544 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
546 flushMotion();
548 if (!opaque_move) {
549 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
550 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
551 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
558 typedef struct {
559 /* arrays of WWindows sorted by the respective border position */
560 WWindow **topList; /* top border */
561 WWindow **leftList; /* left border */
562 WWindow **rightList; /* right border */
563 WWindow **bottomList; /* bottom border */
564 int count;
566 /* index of window in the above lists indicating the relative position
567 * of the window with the others */
568 int topIndex;
569 int leftIndex;
570 int rightIndex;
571 int bottomIndex;
573 int rubCount; /* for workspace switching */
575 int winWidth, winHeight; /* width/height of the window */
576 int realX, realY; /* actual position of the window */
577 int calcX, calcY; /* calculated position of window */
578 int omouseX, omouseY; /* old mouse position */
579 int mouseX, mouseY; /* last known position of the pointer */
580 } MoveData;
582 #define WTOP(w) (w)->frame_y
583 #define WLEFT(w) (w)->frame_x
584 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
585 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
587 static int
588 compareWTop(const void *a, const void *b)
590 WWindow *wwin1 = *(WWindow**)a;
591 WWindow *wwin2 = *(WWindow**)b;
593 if (WTOP(wwin1) > WTOP(wwin2))
594 return -1;
595 else if (WTOP(wwin1) < WTOP(wwin2))
596 return 1;
597 else
598 return 0;
602 static int
603 compareWLeft(const void *a, const void *b)
605 WWindow *wwin1 = *(WWindow**)a;
606 WWindow *wwin2 = *(WWindow**)b;
608 if (WLEFT(wwin1) > WLEFT(wwin2))
609 return -1;
610 else if (WLEFT(wwin1) < WLEFT(wwin2))
611 return 1;
612 else
613 return 0;
617 static int
618 compareWRight(const void *a, const void *b)
620 WWindow *wwin1 = *(WWindow**)a;
621 WWindow *wwin2 = *(WWindow**)b;
623 if (WRIGHT(wwin1) < WRIGHT(wwin2))
624 return -1;
625 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
626 return 1;
627 else
628 return 0;
633 static int
634 compareWBottom(const void *a, const void *b)
636 WWindow *wwin1 = *(WWindow**)a;
637 WWindow *wwin2 = *(WWindow**)b;
639 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
640 return -1;
641 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
642 return 1;
643 else
644 return 0;
648 static void
649 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
651 int i;
652 int newX2 = newX + data->winWidth;
653 int newY2 = newY + data->winHeight;
654 Bool ok = False;
656 if (newX < data->realX) {
657 if (data->rightIndex > 0
658 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
659 ok = True;
660 } else if (data->leftIndex <= data->count-1
661 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
662 ok = True;
664 } else if (newX > data->realX) {
665 if (data->leftIndex > 0
666 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
667 ok = True;
668 } else if (data->rightIndex <= data->count-1
669 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
670 ok = True;
674 if (!ok) {
675 if (newY < data->realY) {
676 if (data->bottomIndex > 0
677 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
678 ok = True;
679 } else if (data->topIndex <= data->count-1
680 && newY2 <= WTOP(data->topList[data->topIndex])) {
681 ok = True;
683 } else if (newY > data->realY) {
684 if (data->topIndex > 0
685 && newY2 > WTOP(data->topList[data->topIndex-1])) {
686 ok = True;
687 } else if (data->bottomIndex <= data->count-1
688 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
689 ok = True;
694 if (!ok)
695 return;
697 /* TODO: optimize this */
698 if (data->realY < WBOTTOM(data->bottomList[0])) {
699 data->bottomIndex = 0;
701 if (data->realX < WRIGHT(data->rightList[0])) {
702 data->rightIndex = 0;
704 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
705 data->leftIndex = 0;
707 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
708 data->topIndex = 0;
710 for (i = 0; i < data->count; i++) {
711 if (data->realY > WBOTTOM(data->bottomList[i])) {
712 data->bottomIndex = i + 1;
714 if (data->realX > WRIGHT(data->rightList[i])) {
715 data->rightIndex = i + 1;
717 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
718 data->leftIndex = i + 1;
720 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
721 data->topIndex = i + 1;
727 static void
728 freeMoveData(MoveData *data)
730 if (data->topList)
731 free(data->topList);
732 if (data->leftList)
733 free(data->leftList);
734 if (data->rightList)
735 free(data->rightList);
736 if (data->bottomList)
737 free(data->bottomList);
741 static void
742 updateMoveData(WWindow *wwin, MoveData *data)
744 WScreen *scr = wwin->screen_ptr;
745 WWindow *tmp;
746 int i;
748 data->count = 0;
749 tmp = scr->focused_window;
750 while (tmp) {
751 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
752 && !tmp->flags.miniaturized
753 && !tmp->flags.hidden
754 && !tmp->flags.obscured) {
755 data->topList[data->count] = tmp;
756 data->leftList[data->count] = tmp;
757 data->rightList[data->count] = tmp;
758 data->bottomList[data->count] = tmp;
759 data->count++;
761 tmp = tmp->prev;
764 if (data->count == 0) {
765 data->topIndex = 0;
766 data->leftIndex = 0;
767 data->rightIndex = 0;
768 data->bottomIndex = 0;
769 return;
773 * order from closest to the border of the screen to farthest
775 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
776 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
777 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
778 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
780 /* figure the position of the window relative to the others */
782 data->topIndex = -1;
783 data->leftIndex = -1;
784 data->rightIndex = -1;
785 data->bottomIndex = -1;
787 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
788 data->bottomIndex = 0;
790 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
791 data->rightIndex = 0;
793 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
794 data->leftIndex = 0;
796 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
797 data->topIndex = 0;
799 for (i = 0; i < data->count; i++) {
800 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
801 data->bottomIndex = i + 1;
803 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
804 data->rightIndex = i + 1;
806 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
807 data->leftIndex = i + 1;
809 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
810 data->topIndex = i + 1;
816 static void
817 initMoveData(WWindow *wwin, MoveData *data)
819 int i;
820 WWindow *tmp;
822 memset(data, 0, sizeof(MoveData));
824 for (i = 0, tmp = wwin->screen_ptr->focused_window;
825 tmp != NULL;
826 tmp = tmp->prev, i++);
828 if (i > 1) {
829 data->topList = wmalloc(sizeof(WWindow*) * i);
830 data->leftList = wmalloc(sizeof(WWindow*) * i);
831 data->rightList = wmalloc(sizeof(WWindow*) * i);
832 data->bottomList = wmalloc(sizeof(WWindow*) * i);
834 updateMoveData(wwin, data);
837 data->realX = wwin->frame_x;
838 data->realY = wwin->frame_y;
839 data->calcX = wwin->frame_x;
840 data->calcY = wwin->frame_y;
842 data->winWidth = wwin->frame->core->width + 2;
843 data->winHeight = wwin->frame->core->height + 2;
847 static Bool
848 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
850 WScreen *scr = wwin->screen_ptr;
851 Bool changed = False;
853 if (data->mouseX <= 1) {
854 if (scr->current_workspace > 0) {
856 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
857 True);
858 changed = True;
859 data->rubCount = 0;
861 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
863 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
864 True);
865 changed = True;
866 data->rubCount = 0;
868 } else if (data->mouseX >= scr->scr_width - 2) {
870 if (scr->current_workspace == scr->workspace_count - 1) {
872 if (wPreferences.ws_cycle
873 || scr->workspace_count == MAX_WORKSPACES) {
875 crossWorkspace(scr, wwin, opaqueMove, 0, False);
876 changed = True;
877 data->rubCount = 0;
879 /* if user insists on trying to go to next workspace even when
880 * it's already the last, create a new one */
881 else if (data->omouseX == data->mouseX
882 && wPreferences.ws_advance) {
884 /* detect user "rubbing" the window against the edge */
885 if (data->rubCount > 0
886 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
888 data->rubCount = -(data->rubCount + 1);
890 } else if (data->rubCount <= 0
891 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
893 data->rubCount = -data->rubCount + 1;
896 /* create a new workspace */
897 if (abs(data->rubCount) > 2) {
898 /* go to next workspace */
899 wWorkspaceNew(scr);
901 crossWorkspace(scr, wwin, opaqueMove,
902 scr->current_workspace+1, False);
903 changed = True;
904 data->rubCount = 0;
906 } else if (scr->current_workspace < scr->workspace_count) {
908 /* go to next workspace */
909 crossWorkspace(scr, wwin, opaqueMove,
910 scr->current_workspace+1, False);
911 changed = True;
912 data->rubCount = 0;
914 } else {
915 data->rubCount = 0;
918 return changed;
922 static void
923 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
924 Bool opaqueMove, int newMouseX, int newMouseY)
926 WScreen *scr = wwin->screen_ptr;
927 int dx, dy; /* how much mouse moved */
928 int winL, winR, winT, winB; /* requested new window position */
929 int newX, newY; /* actual new window position */
930 Bool hresist, vresist;
931 Bool updateIndex;
933 hresist = False;
934 vresist = False;
936 updateIndex = False;
938 /* check the direction of the movement */
939 dx = newMouseX - data->mouseX;
940 dy = newMouseY - data->mouseY;
942 data->omouseX = data->mouseX;
943 data->omouseY = data->mouseY;
944 data->mouseX = newMouseX;
945 data->mouseY = newMouseY;
947 winL = data->calcX + dx;
948 winR = data->calcX + data->winWidth + dx;
949 winT = data->calcY + dy;
950 winB = data->calcY + data->winHeight + dy;
952 newX = data->realX;
953 newY = data->realY;
955 if (doResistance) {
956 int edge;
957 int resist;
958 WWindow *rwin;
960 resist = wPreferences.edge_resistance;
961 /* horizontal movement: check horizontal edge resistances */
962 if (dx < 0) {
963 /* window is the leftmost window: check against screen edge */
964 edge = scr->totalUsableArea.x1;
966 /* check position of nearest window to the left */
967 if (data->rightIndex > 0) {
968 /* there is some window at the left: check if it will block
969 * the window */
970 rwin = data->rightList[data->rightIndex - 1];
972 if (data->realY > WBOTTOM(rwin)
973 || (data->realY + data->winHeight) < WTOP(rwin)) {
974 resist = 0;
975 } else {
976 edge = WRIGHT(rwin) + 1;
977 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
980 if (resist > 0 && winL >= edge - resist && winL <= edge) {
981 newX = edge;
982 hresist = True;
984 } else if (dx > 0) {
985 /* window is the rightmost window: check against screen edge */
986 edge = scr->totalUsableArea.x2;
988 /* check position of nearest window to the right */
989 if (data->leftIndex > 0) {
990 /* there is some window at the right: check if it will block
991 * the window */
992 rwin = data->leftList[data->leftIndex - 1];
994 if (data->realY > WBOTTOM(rwin)
995 || (data->realY + data->winHeight) < WTOP(rwin)) {
996 resist = 0;
997 } else {
998 edge = WLEFT(rwin);
999 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1002 if (resist > 0 && winR <= edge + resist && winR >= edge) {
1003 newX = edge - data->winWidth;
1004 hresist = True;
1008 resist = wPreferences.edge_resistance;
1009 /* vertical movement: check vertical edge resistances */
1010 if (dy < 0) {
1011 /* window is the topmost window: check against screen edge */
1012 edge = scr->totalUsableArea.y1;
1014 /* check position of nearest window to the top */
1015 if (data->bottomIndex > 0) {
1016 /* there is some window at the top: check if it will block
1017 * the window */
1018 rwin = data->bottomList[data->bottomIndex - 1];
1020 if (data->realX > WRIGHT(rwin)
1021 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1022 resist = 0;
1023 } else {
1024 edge = WBOTTOM(rwin) + 1;
1025 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1028 if (resist > 0 && winT >= edge - resist && winT <= edge) {
1029 newY = edge;
1030 vresist = True;
1032 } else if (dy > 0) {
1033 /* window is the bottommost window: check against screen edge */
1034 edge = scr->totalUsableArea.y2;
1036 /* check position of nearest window to the bottom */
1037 if (data->topIndex > 0) {
1038 /* there is some window at the bottom: check if it will block
1039 * the window */
1040 rwin = data->topList[data->topIndex - 1];
1042 if (data->realX > WRIGHT(rwin)
1043 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1044 resist = 0;
1045 } else {
1046 edge = WTOP(rwin);
1047 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1050 if (resist > 0 && winB <= edge + resist && winB >= edge) {
1051 newY = edge - data->winHeight;
1052 vresist = True;
1057 /* update window position */
1058 data->calcX += dx;
1059 data->calcY += dy;
1061 if (((dx > 0 && data->calcX - data->realX > 0)
1062 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1063 newX = data->calcX;
1065 if (((dy > 0 && data->calcY - data->realY > 0)
1066 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1067 newY = data->calcY;
1069 if (data->realX != newX || data->realY != newY) {
1070 if (opaqueMove) {
1071 doWindowMove(wwin, scr->selected_windows,
1072 newX - wwin->frame_x,
1073 newY - wwin->frame_y);
1074 } else {
1075 /* erase frames */
1076 drawFrames(wwin, scr->selected_windows,
1077 data->realX - wwin->frame_x,
1078 data->realY - wwin->frame_y);
1081 if (!scr->selected_windows
1082 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1084 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1085 newY + data->winHeight/2);
1088 if (!opaqueMove) {
1089 /* draw frames */
1090 drawFrames(wwin, scr->selected_windows,
1091 newX - wwin->frame_x,
1092 newY - wwin->frame_y);
1095 if (!scr->selected_windows) {
1097 if (wPreferences.move_display == WDIS_NEW) {
1099 showPosition(wwin, data->realX, data->realY);
1102 showPosition(wwin, newX, newY);
1107 /* recalc relative window position */
1108 if (doResistance && (data->realX != newX || data->realY != newY)) {
1109 updateResistance(wwin, data, newX, newY);
1112 data->realX = newX;
1113 data->realY = newY;
1119 #if 0
1120 typedef struct _looper {
1121 WWindow *wwin;
1122 int x,y,w,h,ox,oy;
1123 } _looper;
1125 void
1126 _keyloop(_looper *lpr){
1127 WWindow *wwin = lpr->wwin;
1128 WScreen *scr = wwin->screen_ptr;
1129 int w = wwin->frame->core->width;
1130 int h = wwin->frame->core->height;
1131 int src_x = wwin->frame_x;
1132 int src_y = wwin->frame_y;
1134 if (!scr->selected_windows){
1135 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1137 XUngrabServer(dpy);
1138 XSync(dpy, False);
1139 wusleep(10000);
1140 XGrabServer(dpy);
1141 /* printf("called\n");*/
1142 if (!scr->selected_windows){
1143 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1145 /* reset timer */
1146 if(scr->keymove_tick)
1147 WMAddTimerHandler(15000,(WMCallback*)_keyloop, lpr);
1150 #endif
1151 #define _KS 20
1154 wKeyboardMoveResizeWindow(WWindow *wwin)
1156 WScreen *scr = wwin->screen_ptr;
1157 Window root = scr->root_win;
1158 XEvent event;
1159 int w = wwin->frame->core->width;
1160 int h = wwin->frame->core->height;
1161 int scr_width = wwin->screen_ptr->scr_width;
1162 int scr_height = wwin->screen_ptr->scr_height;
1163 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1164 int src_x = wwin->frame_x;
1165 int src_y = wwin->frame_y;
1166 int done,off_x,off_y,ww,wh;
1167 int kspeed = 1;
1168 Time lastTime = 0;
1169 KeySym keysym=NoSymbol;
1170 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1173 int timer;
1174 _looper looper;
1175 looper.wwin=wwin;
1176 scr->keymove_tick=1;
1177 WMAddTimerHandler(1000,(WMCallback*)_keyloop, &looper);
1180 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1181 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1182 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1183 ctrlmode=done=off_x=off_y=0;
1185 XSync(dpy, False);
1186 wusleep(10000);
1187 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1189 if (!wwin->flags.selected) {
1190 wUnselectWindows(scr);
1192 XGrabServer(dpy);
1193 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1194 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1195 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1197 if (wwin->flags.shaded || scr->selected_windows) {
1198 if(scr->selected_windows)
1199 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1200 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1201 if(!scr->selected_windows)
1202 mapPositionDisplay(wwin, src_x, src_y, w, h);
1203 } else {
1204 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1206 ww=w;
1207 wh=h;
1208 while(1) {
1210 looper.ox=off_x;
1211 looper.oy=off_y;
1213 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1214 | ButtonPressMask | ExposureMask, &event);
1215 if (wwin->flags.shaded || scr->selected_windows) {
1216 if(scr->selected_windows)
1217 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1218 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1219 /*** I HATE EDGE RESISTANCE - ]d ***/
1221 else {
1222 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1225 if(ctrlmode)
1226 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1228 XUngrabServer(dpy);
1229 XSync(dpy, False);
1231 switch (event.type) {
1232 case KeyPress:
1233 /* accelerate */
1234 if (event.xkey.time - lastTime > 50) {
1235 kspeed = 1;
1236 } else {
1237 if (kspeed < 20)
1238 kspeed++;
1240 lastTime = event.xkey.time;
1242 if (event.xkey.state & ControlMask && !wwin->flags.shaded){
1243 ctrlmode=1;
1244 wUnselectWindows(scr);
1246 else {
1247 ctrlmode=0;
1249 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr){
1250 if(ctrlmode)
1251 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1252 else
1253 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1255 else {
1256 keysym = XLookupKeysym(&event.xkey, 0);
1257 switch(keysym){
1258 case XK_Return:
1259 done=2;
1260 break;
1261 case XK_Escape:
1262 done=1;
1263 break;
1264 case XK_Up:
1265 case XK_KP_Up:
1266 case XK_k:
1267 if (ctrlmode){
1268 h-=kspeed;
1270 else off_y-=kspeed;
1271 break;
1272 case XK_Down:
1273 case XK_KP_Down:
1274 case XK_j:
1275 if (ctrlmode){
1276 h+=kspeed;
1278 else off_y+=kspeed;
1279 break;
1280 case XK_Left:
1281 case XK_KP_Left:
1282 case XK_h:
1283 if (ctrlmode){
1284 w-=kspeed;
1286 else off_x-=kspeed;
1287 break;
1288 case XK_Right:
1289 case XK_KP_Right:
1290 case XK_l:
1291 if (ctrlmode){
1292 w+=kspeed;
1294 else off_x+=kspeed;
1295 break;
1297 ww=w;wh=h;
1298 wh-=vert_border;
1299 wWindowConstrainSize(wwin, &ww, &wh);
1300 wh+=vert_border;
1302 if (wPreferences.ws_cycle){
1303 if (src_x + off_x + wwin->frame->core->width < 20){
1304 if(!scr->current_workspace) {
1305 wWorkspaceChange(scr, scr->workspace_count-1);
1307 else wWorkspaceChange(scr, scr->current_workspace-1);
1308 off_x += scr_width;
1310 else if (src_x + off_x + 20 > scr_width){
1311 if(scr->current_workspace == scr->workspace_count-1) {
1312 wWorkspaceChange(scr, 0);
1314 else wWorkspaceChange(scr, scr->current_workspace+1);
1315 off_x -= scr_width;
1318 else {
1319 if (src_x + off_x + wwin->frame->core->width < 20)
1320 off_x = 20 - wwin->frame->core->width - src_x;
1321 else if (src_x + off_x + 20 > scr_width)
1322 off_x = scr_width - 20 - src_x;
1325 if (src_y + off_y + wwin->frame->core->height < 20)
1326 off_y = 20 - wwin->frame->core->height - src_y;
1327 else if (src_y + off_y + 20 > scr_height)
1328 off_y = scr_height - 20 - src_y;
1331 break;
1332 case ButtonPress:
1333 case ButtonRelease:
1334 done=1;
1335 break;
1336 default:
1337 WMHandleEvent(&event);
1338 break;
1341 XGrabServer(dpy);
1342 /*xxx*/
1344 if (wwin->flags.shaded && !scr->selected_windows){
1345 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1347 else {
1348 if(ctrlmode){
1349 unmapPositionDisplay(wwin);
1350 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1352 else if(!scr->selected_windows){
1353 unmapGeometryDisplay(wwin);
1354 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1358 if (wwin->flags.shaded || scr->selected_windows) {
1359 if(scr->selected_windows)
1360 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1361 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1363 else {
1364 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1368 if(ctrlmode){
1369 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1371 else if(!scr->selected_windows)
1372 showPosition(wwin, src_x+off_x, src_y+off_y);
1373 /**/
1375 if(done){
1376 scr->keymove_tick=0;
1378 WMDeleteTimerWithClientData(&looper);
1380 if (wwin->flags.shaded || scr->selected_windows) {
1381 if(scr->selected_windows)
1382 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1383 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1385 else {
1386 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1389 if(ctrlmode){
1390 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1391 unmapGeometryDisplay(wwin);
1393 else
1394 unmapPositionDisplay(wwin);
1395 XUngrabKeyboard(dpy, CurrentTime);
1396 XUngrabPointer(dpy, CurrentTime);
1397 XUngrabServer(dpy);
1398 if(done==2){
1399 if (wwin->flags.shaded || scr->selected_windows) {
1400 LinkedList *list;
1401 list=scr->selected_windows;
1402 if(!scr->selected_windows){
1403 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1404 wWindowSynthConfigureNotify(wwin);
1406 else {
1407 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1408 while (list) {
1409 wWindowSynthConfigureNotify(list->head);
1410 list = list->tail;
1414 else {
1415 wWindowConfigure(wwin, src_x+off_x, src_y+off_y, ww, wh - vert_border);
1416 wWindowSynthConfigureNotify(wwin);
1418 wWindowChangeWorkspace(wwin, scr->current_workspace);
1419 wSetFocusTo(scr, wwin);
1421 return 1;
1428 *----------------------------------------------------------------------
1429 * wMouseMoveWindow--
1430 * Move the named window and the other selected ones (if any),
1431 * interactively. Also shows the position of the window, if only one
1432 * window is being moved.
1433 * If the window is not on the selected window list, the selected
1434 * windows are deselected.
1435 * If shift is pressed during the operation, the position display
1436 * is changed to another type.
1438 * Returns:
1439 * True if the window was moved, False otherwise.
1441 * Side effects:
1442 * The window(s) position is changed, and the client(s) are
1443 * notified about that.
1444 * The position display configuration may be changed.
1445 *----------------------------------------------------------------------
1448 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1450 WScreen *scr = wwin->screen_ptr;
1451 XEvent event;
1452 Window root = scr->root_win;
1453 KeyCode shiftl, shiftr;
1454 int started = 0;
1455 int warped = 0;
1456 /* This needs not to change while moving, else bad things can happen */
1457 int opaqueMove = wPreferences.opaque_move;
1458 MoveData moveData;
1459 #ifdef GHOST_WINDOW_MOVE
1460 RImage *rimg;
1462 rimg = InitGhostWindowMove(scr);
1463 #endif
1465 initMoveData(wwin, &moveData);
1467 moveData.mouseX = ev->xmotion.x_root;
1468 moveData.mouseY = ev->xmotion.y_root;
1470 if (!wwin->flags.selected) {
1471 /* this window is not selected, unselect others and move only wwin */
1472 wUnselectWindows(scr);
1474 #ifdef DEBUG
1475 puts("Moving window");
1476 #endif
1477 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1478 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1479 while (1) {
1480 if (warped) {
1481 int junk;
1482 Window junkw;
1484 /* XWarpPointer() doesn't seem to generate Motion events, so
1485 we've got to simulate them */
1486 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1487 &event.xmotion.y_root, &junk, &junk,
1488 (unsigned *) &junk);
1489 } else {
1490 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1491 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1492 &event);
1494 if (event.type == MotionNotify) {
1495 /* compress MotionNotify events */
1496 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1499 switch (event.type) {
1500 case KeyPress:
1501 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1502 && started && !scr->selected_windows) {
1504 if (!opaqueMove) {
1505 drawFrames(wwin, scr->selected_windows,
1506 moveData.realX - wwin->frame_x,
1507 moveData.realY - wwin->frame_y);
1510 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1511 moveData.winWidth, moveData.winHeight);
1513 if (!opaqueMove) {
1514 drawFrames(wwin, scr->selected_windows,
1515 moveData.realX - wwin->frame_x,
1516 moveData.realY - wwin->frame_y);
1519 break;
1521 case MotionNotify:
1522 if (started) {
1523 updateWindowPosition(wwin, &moveData,
1524 scr->selected_windows == NULL
1525 && wPreferences.edge_resistance > 0,
1526 opaqueMove,
1527 event.xmotion.x_root,
1528 event.xmotion.y_root);
1530 if (!warped && !wPreferences.no_autowrap) {
1531 int oldWorkspace = scr->current_workspace;
1533 drawFrames(wwin, scr->selected_windows,
1534 moveData.realX - wwin->frame_x,
1535 moveData.realY - wwin->frame_y);
1537 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1538 if (scr->current_workspace != oldWorkspace
1539 && wPreferences.edge_resistance > 0
1540 && scr->selected_windows == NULL)
1541 updateMoveData(wwin, &moveData);
1542 warped = 1;
1545 drawFrames(wwin, scr->selected_windows,
1546 moveData.realX - wwin->frame_x,
1547 moveData.realY - wwin->frame_y);
1548 } else {
1549 warped = 0;
1551 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1552 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1554 XChangeActivePointerGrab(dpy, ButtonMotionMask
1555 | ButtonReleaseMask | ButtonPressMask,
1556 wCursor[WCUR_MOVE], CurrentTime);
1557 started = 1;
1558 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1559 CurrentTime);
1561 if (!scr->selected_windows)
1562 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1563 moveData.winWidth, moveData.winHeight);
1565 if (started && !opaqueMove)
1566 drawFrames(wwin, scr->selected_windows, 0, 0);
1568 if (!opaqueMove)
1569 XGrabServer(dpy);
1571 break;
1573 case ButtonPress:
1574 break;
1576 case ButtonRelease:
1577 if (event.xbutton.button != ev->xbutton.button)
1578 break;
1580 if (started) {
1581 if (!opaqueMove) {
1582 drawFrames(wwin, scr->selected_windows,
1583 moveData.realX - wwin->frame_x,
1584 moveData.realY - wwin->frame_y);
1585 XSync(dpy, 0);
1586 doWindowMove(wwin, scr->selected_windows,
1587 moveData.realX - wwin->frame_x,
1588 moveData.realY - wwin->frame_y);
1590 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1591 wWindowSynthConfigureNotify(wwin);
1592 #endif
1593 XUngrabKeyboard(dpy, CurrentTime);
1594 XUngrabServer(dpy);
1595 if (!opaqueMove) {
1596 wWindowChangeWorkspace(wwin, scr->current_workspace);
1597 wSetFocusTo(scr, wwin);
1599 if (wPreferences.move_display == WDIS_NEW)
1600 showPosition(wwin, moveData.realX, moveData.realY);
1602 if (!scr->selected_windows) {
1603 /* get rid of the geometry window */
1604 unmapPositionDisplay(wwin);
1607 #ifdef DEBUG
1608 puts("End move window");
1609 #endif
1610 freeMoveData(&moveData);
1612 return started;
1614 default:
1615 if (started && !opaqueMove) {
1616 drawFrames(wwin, scr->selected_windows,
1617 moveData.realX - wwin->frame_x,
1618 moveData.realY - wwin->frame_y);
1619 XUngrabServer(dpy);
1620 WMHandleEvent(&event);
1621 XSync(dpy, False);
1622 XGrabServer(dpy);
1623 drawFrames(wwin, scr->selected_windows,
1624 moveData.realX - wwin->frame_x,
1625 moveData.realY - wwin->frame_y);
1626 } else {
1627 WMHandleEvent(&event);
1629 break;
1633 freeMoveData(&moveData);
1635 return 0;
1639 #define RESIZEBAR 1
1640 #define HCONSTRAIN 2
1642 static int
1643 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1644 int flags)
1646 int w = wwin->frame->core->width - 1;
1647 int cw = wwin->frame->resizebar_corner_width;
1648 int dir;
1650 /* if not resizing through the resizebar */
1651 if (!(flags & RESIZEBAR)) {
1652 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1653 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1654 if (abs(dx) < 2 || abs(dy) < 2) {
1655 if (abs(dy) > abs(dx))
1656 xdir = 0;
1657 else
1658 ydir = 0;
1660 return (xdir | ydir);
1663 /* window is too narrow. allow diagonal resize */
1664 if (cw * 2 >= w) {
1665 int ydir;
1667 if (flags & HCONSTRAIN)
1668 ydir = 0;
1669 else
1670 ydir = DOWN;
1671 if (x < cw)
1672 return (LEFT | ydir);
1673 else
1674 return (RIGHT | ydir);
1676 /* vertical resize */
1677 if ((x > cw) && (x < w - cw))
1678 return DOWN;
1680 if (x < cw)
1681 dir = LEFT;
1682 else
1683 dir = RIGHT;
1685 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1686 dir |= DOWN;
1688 return dir;
1692 void
1693 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1695 XEvent event;
1696 WScreen *scr = wwin->screen_ptr;
1697 Window root = scr->root_win;
1698 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1699 int fw = wwin->frame->core->width;
1700 int fh = wwin->frame->core->height;
1701 int fx = wwin->frame_x;
1702 int fy = wwin->frame_y;
1703 int is_resizebar = (wwin->frame->resizebar
1704 && ev->xany.window==wwin->frame->resizebar->window);
1705 int orig_x, orig_y;
1706 int started;
1707 int dw, dh;
1708 int rw = fw, rh = fh;
1709 int rx1, ry1, rx2, ry2;
1710 int res = 0;
1711 KeyCode shiftl, shiftr;
1712 int h = 0;
1713 int orig_fx = fx;
1714 int orig_fy = fy;
1715 int orig_fw = fw;
1716 int orig_fh = fh;
1718 if (wwin->flags.shaded) {
1719 wwarning("internal error: tryein");
1720 return;
1722 orig_x = ev->xbutton.x_root;
1723 orig_y = ev->xbutton.y_root;
1725 started = 0;
1726 #ifdef DEBUG
1727 puts("Resizing window");
1728 #endif
1730 wUnselectWindows(scr);
1731 rx1 = fx;
1732 rx2 = fx + fw - 1;
1733 ry1 = fy;
1734 ry2 = fy + fh - 1;
1735 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1736 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1737 if (!WFLAGP(wwin, no_titlebar))
1738 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
1739 else
1740 h = 0;
1741 while (1) {
1742 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1743 | ButtonPressMask | ExposureMask, &event);
1744 switch (event.type) {
1745 case KeyPress:
1746 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1747 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1748 && started) {
1749 drawTransparentFrame(wwin, fx, fy, fw, fh);
1750 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1751 drawTransparentFrame(wwin, fx, fy, fw, fh);
1753 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1754 break;
1756 case MotionNotify:
1757 if (started) {
1758 dw = 0;
1759 dh = 0;
1761 orig_fx = fx;
1762 orig_fy = fy;
1763 orig_fw = fw;
1764 orig_fh = fh;
1766 if (res & LEFT)
1767 dw = orig_x - event.xmotion.x_root;
1768 else if (res & RIGHT)
1769 dw = event.xmotion.x_root - orig_x;
1770 if (res & UP)
1771 dh = orig_y - event.xmotion.y_root;
1772 else if (res & DOWN)
1773 dh = event.xmotion.y_root - orig_y;
1775 orig_x = event.xmotion.x_root;
1776 orig_y = event.xmotion.y_root;
1778 rw += dw;
1779 rh += dh;
1780 fw = rw;
1781 fh = rh - vert_border;
1782 wWindowConstrainSize(wwin, &fw, &fh);
1783 fh += vert_border;
1784 if (res & LEFT)
1785 fx = rx2 - fw + 1;
1786 else if (res & RIGHT)
1787 fx = rx1;
1788 if (res & UP)
1789 fy = ry2 - fh + 1;
1790 else if (res & DOWN)
1791 fy = ry1;
1792 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1793 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1794 int tx, ty;
1795 Window junkw;
1796 int flags;
1798 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1799 orig_x, orig_y, &tx, &ty, &junkw);
1801 /* check if resizing through resizebar */
1802 if (is_resizebar)
1803 flags = RESIZEBAR;
1804 else
1805 flags = 0;
1807 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1808 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1809 flags |= HCONSTRAIN;
1811 res = getResizeDirection(wwin, tx, ty,
1812 orig_x - event.xmotion.x_root,
1813 orig_y - event.xmotion.y_root, flags);
1815 XChangeActivePointerGrab(dpy, ButtonMotionMask
1816 | ButtonReleaseMask | ButtonPressMask,
1817 wCursor[WCUR_RESIZE], CurrentTime);
1818 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1819 CurrentTime);
1821 XGrabServer(dpy);
1823 /* Draw the resize frame for the first time. */
1824 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1826 drawTransparentFrame(wwin, fx, fy, fw, fh);
1828 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1830 started = 1;
1832 if (started) {
1833 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1834 drawTransparentFrame(wwin, orig_fx, orig_fy,
1835 orig_fw, orig_fh);
1836 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1837 drawTransparentFrame(wwin, fx, fy, fw, fh);
1838 } else {
1839 drawTransparentFrame(wwin, orig_fx, orig_fy,
1840 orig_fw, orig_fh);
1841 drawTransparentFrame(wwin, fx, fy, fw, fh);
1843 if (fh != orig_fh || fw != orig_fw) {
1844 if (wPreferences.size_display == WDIS_NEW) {
1845 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1846 orig_fy + orig_fh, res);
1848 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1851 break;
1853 case ButtonPress:
1854 break;
1856 case ButtonRelease:
1857 if (event.xbutton.button != ev->xbutton.button)
1858 break;
1860 if (started) {
1861 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1863 drawTransparentFrame(wwin, fx, fy, fw, fh);
1865 XUngrabKeyboard(dpy, CurrentTime);
1866 unmapGeometryDisplay(wwin);
1867 XUngrabServer(dpy);
1868 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1870 #ifdef DEBUG
1871 puts("End resize window");
1872 #endif
1873 return;
1875 default:
1876 WMHandleEvent(&event);
1881 #undef LEFT
1882 #undef RIGHT
1883 #undef HORIZONTAL
1884 #undef UP
1885 #undef DOWN
1886 #undef VERTICAL
1887 #undef HCONSTRAIN
1888 #undef RESIZEBAR
1890 void
1891 wUnselectWindows(WScreen *scr)
1893 WWindow *wwin;
1895 while (scr->selected_windows) {
1896 wwin = scr->selected_windows->head;
1897 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
1898 wIconSelect(wwin->icon);
1900 wSelectWindow(wwin, False);
1904 #ifndef LITE
1905 static void
1906 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
1908 WWindow *tmpw;
1910 /* select the windows and put them in the selected window list */
1911 tmpw = scr->focused_window;
1912 while (tmpw != NULL) {
1913 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
1914 if ((tmpw->frame->workspace == scr->current_workspace
1915 || IS_OMNIPRESENT(tmpw))
1916 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
1917 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
1918 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
1919 wSelectWindow(tmpw, True);
1922 tmpw = tmpw->prev;
1927 void
1928 wSelectWindows(WScreen *scr, XEvent *ev)
1930 XEvent event;
1931 Window root = scr->root_win;
1932 GC gc = scr->frame_gc;
1933 int xp = ev->xbutton.x_root;
1934 int yp = ev->xbutton.y_root;
1935 int w = 0, h = 0;
1936 int x = xp, y = yp;
1938 #ifdef DEBUG
1939 puts("Selecting windows");
1940 #endif
1941 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
1942 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1943 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
1944 CurrentTime) != Success) {
1945 return;
1947 XGrabServer(dpy);
1949 wUnselectWindows(scr);
1951 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
1952 while (1) {
1953 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
1954 | ButtonPressMask, &event);
1956 switch (event.type) {
1957 case MotionNotify:
1958 XDrawRectangle(dpy, root, gc, x, y, w, h);
1959 x = event.xmotion.x_root;
1960 if (x < xp) {
1961 w = xp - x;
1962 } else {
1963 w = x - xp;
1964 x = xp;
1966 y = event.xmotion.y_root;
1967 if (y < yp) {
1968 h = yp - y;
1969 } else {
1970 h = y - yp;
1971 y = yp;
1973 XDrawRectangle(dpy, root, gc, x, y, w, h);
1974 break;
1976 case ButtonPress:
1977 break;
1979 case ButtonRelease:
1980 if (event.xbutton.button != ev->xbutton.button)
1981 break;
1983 XDrawRectangle(dpy, root, gc, x, y, w, h);
1984 XUngrabServer(dpy);
1985 XUngrabPointer(dpy, CurrentTime);
1986 selectWindowsInside(scr, x, y, x + w, y + h);
1987 #ifdef DEBUG
1988 puts("End window selection");
1989 #endif
1990 return;
1992 default:
1993 WMHandleEvent(&event);
1994 break;
1998 #endif /* !LITE */
2000 void
2001 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2002 unsigned width, unsigned height)
2004 WScreen *scr = wwin->screen_ptr;
2005 Window root = scr->root_win;
2006 int x, y, h = 0;
2007 XEvent event;
2008 KeyCode shiftl, shiftr;
2009 Window junkw;
2010 int junk;
2012 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2013 GrabModeAsync, GrabModeAsync, None,
2014 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2015 *x_ret = 0;
2016 *y_ret = 0;
2017 return;
2019 if (!WFLAGP(wwin, no_titlebar)) {
2020 h = scr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
2021 height += h;
2023 if (!WFLAGP(wwin, no_resizebar)) {
2024 height += RESIZEBAR_HEIGHT;
2026 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2027 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2028 (unsigned *) &junk);
2029 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2031 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2033 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2034 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2035 while (1) {
2036 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2037 &event);
2038 switch (event.type) {
2039 case KeyPress:
2040 if ((event.xkey.keycode == shiftl)
2041 || (event.xkey.keycode == shiftr)) {
2042 drawTransparentFrame(wwin,
2043 x - width/2, y - h/2, width, height);
2044 cyclePositionDisplay(wwin,
2045 x - width/2, y - h/2, width, height);
2046 drawTransparentFrame(wwin,
2047 x - width/2, y - h/2, width, height);
2049 break;
2051 case MotionNotify:
2052 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2054 x = event.xmotion.x_root;
2055 y = event.xmotion.y_root;
2057 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2058 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2060 showPosition(wwin, x - width/2, y - h/2);
2062 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2064 break;
2066 case ButtonPress:
2067 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2068 XSync(dpy, 0);
2069 *x_ret = x - width/2;
2070 *y_ret = y - h/2;
2071 XUngrabPointer(dpy, CurrentTime);
2072 XUngrabKeyboard(dpy, CurrentTime);
2073 /* get rid of the geometry window */
2074 unmapPositionDisplay(wwin);
2075 return;
2077 default:
2078 WMHandleEvent(&event);
2079 break;