ResizebarBack option
[wmaker-crm.git] / src / moveres.c
blob5b511467871714c465a47f0248a1db6433ebb5a0
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 #ifdef KWM_HINTS
45 #include "kwm.h"
46 #endif
48 /* How many different types of geometry/position
49 display thingies are there? */
50 #define NUM_DISPLAYS 4
52 #define LEFT 1
53 #define RIGHT 2
54 #define HORIZONTAL (LEFT|RIGHT)
55 #define UP 4
56 #define DOWN 8
57 #define VERTICAL (UP|DOWN)
59 /****** Global Variables ******/
60 extern Time LastTimestamp;
62 extern Cursor wCursor[WCUR_LAST];
64 extern WPreferences wPreferences;
66 extern Atom _XA_WM_PROTOCOLS;
70 void
71 wGetGeometryWindowSize(WScreen *scr, unsigned int *width,
72 unsigned int *height)
74 #ifdef I18N_MB
75 *width = XmbTextEscapement(scr->info_text_font->font, "-8888 x -8888", 13);
76 *height = (7 * scr->info_text_font->height) / 4 - 1;
77 #else
78 *width = XTextWidth(scr->info_text_font->font, "-8888 x -8888", 13);
79 *height = (7 * scr->info_text_font->font->ascent) / 4 - 1;
80 #endif
85 *----------------------------------------------------------------------
86 * moveGeometryDisplayCentered
88 * routine that moves the geometry/position window on scr so it is
89 * centered over the given coordinates (x,y). Also the window position
90 * is clamped so it stays on the screen at all times.
91 *----------------------------------------------------------------------
93 static void
94 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
96 x -= scr->geometry_display_width / 2;
97 y -= scr->geometry_display_height / 2;
99 if (x < 1)
100 x = 1;
101 else if (x > (scr->scr_width - scr->geometry_display_width - 3))
102 x = scr->scr_width - scr->geometry_display_width - 3;
104 if (y < 1)
105 y = 1;
106 else if (y > (scr->scr_height - scr->geometry_display_height - 3))
107 y = scr->scr_height - scr->geometry_display_height - 3;
109 XMoveWindow(dpy, scr->geometry_display, x, y);
113 static void
114 showPosition(WWindow *wwin, int x, int y)
116 WScreen *scr = wwin->screen_ptr;
117 GC gc = scr->info_text_gc;
118 char num[16];
119 int fw, fh;
121 if (wPreferences.move_display == WDIS_NEW) {
122 #if 0
123 int width = wwin->frame->core->width;
124 int height = wwin->frame->core->height;
126 GC lgc = scr->line_gc;
127 XSetForeground(dpy, lgc, scr->line_pixel);
128 sprintf(num, "%i", x);
130 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
131 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
132 y+height+2);
133 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
134 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
135 scr->scr_height);
136 #endif
137 } else {
138 XClearArea(dpy, scr->geometry_display, 1, 1,
139 scr->geometry_display_width-2, scr->geometry_display_height-2,
140 False);
141 sprintf(num, "%+i %-+i", x, y);
142 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
144 XSetForeground(dpy, gc, scr->black_pixel);
146 fh = scr->info_text_font->height;
147 wDrawString(scr->geometry_display, scr->info_text_font, gc,
148 (scr->geometry_display_width - 2 - fw) / 2,
149 (scr->geometry_display_height-fh)/2 + scr->info_text_font->y,
150 num, strlen(num));
151 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
152 scr->geometry_display_height+1,
153 scr->widget_texture, WREL_RAISED);
158 static void
159 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
161 WScreen *scr = wwin->screen_ptr;
163 wPreferences.move_display++;
164 wPreferences.move_display %= NUM_DISPLAYS;
166 if (wPreferences.move_display == WDIS_NEW) {
167 XUnmapWindow(dpy, scr->geometry_display);
168 } else {
169 if (wPreferences.move_display == WDIS_CENTER) {
170 moveGeometryDisplayCentered(scr,
171 scr->scr_width/2, scr->scr_height/2);
172 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
173 moveGeometryDisplayCentered(scr, 1, 1);
174 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
175 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
177 XMapRaised(dpy, scr->geometry_display);
178 showPosition(wwin, x, y);
183 static void
184 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
186 WScreen *scr = wwin->screen_ptr;
188 if (wPreferences.move_display == WDIS_NEW) {
189 return;
190 } else if (wPreferences.move_display == WDIS_CENTER) {
191 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
192 scr->scr_height / 2);
193 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
194 moveGeometryDisplayCentered(scr, 1, 1);
195 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
196 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
198 XMapRaised(dpy, scr->geometry_display);
199 showPosition(wwin, x, y);
202 #define unmapPositionDisplay(w) \
203 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
206 static void
207 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
209 WScreen *scr = wwin->screen_ptr;
210 Window root = scr->root_win;
211 GC gc = scr->line_gc;
212 int ty, by, my, x, y, mx, s;
213 char num[16];
214 XSegment segment[4];
215 int fw, fh;
217 ty = y1 + wwin->frame->top_width;
218 by = y2 - wwin->frame->bottom_width;
219 fw = wTextWidth(scr->info_text_font->font, "8888", 4);
220 fh = scr->info_text_font->height;
222 if (wPreferences.size_display == WDIS_NEW) {
223 XSetForeground(dpy, gc, scr->line_pixel);
225 /* vertical geometry */
226 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
227 x = x2;
228 s = -15;
229 } else {
230 x = x1;
231 s = 15;
233 my = (ty + by) / 2;
235 /* top arrow */
236 /* end bar */
237 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
238 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
240 /* arrowhead */
241 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
242 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
244 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
245 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
247 /* line */
248 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
249 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
251 XDrawSegments(dpy, root, gc, segment, 4);
253 /* bottom arrow */
254 /* end bar */
255 segment[0].y1 = by;
256 segment[0].y2 = by;
258 /* arrowhead */
259 segment[1].y1 = by - 1;
260 segment[1].y2 = by - 7;
262 segment[2].y1 = by - 1;
263 segment[2].y2 = by - 7;
265 /* line */
266 segment[3].y1 = my + fh/2 + 2;
267 segment[3].y2 = by - 1;
269 XDrawSegments(dpy, root, gc, segment, 4);
271 sprintf(num, "%i", (by - ty - wwin->normal_hints->base_height) /
272 wwin->normal_hints->height_inc);
273 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
275 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
277 /* Display the height. */
278 wDrawString(root, scr->info_text_font, gc,
279 x - s + 3 - fw/2, my - fh/2 + scr->info_text_font->y + 1,
280 num, strlen(num));
281 XSetForeground(dpy, gc, scr->line_pixel);
282 /* horizontal geometry */
283 if (y1 < 15) {
284 y = y2;
285 s = -15;
286 } else {
287 y = y1;
288 s = 15;
290 mx = x1 + (x2 - x1)/2;
291 sprintf(num, "%i", (x2 - x1 - wwin->normal_hints->base_width) /
292 wwin->normal_hints->width_inc);
293 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
295 /* left arrow */
296 /* end bar */
297 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
298 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
300 /* arrowhead */
301 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
302 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
304 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
305 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
307 /* line */
308 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
309 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
311 XDrawSegments(dpy, root, gc, segment, 4);
313 /* right arrow */
314 /* end bar */
315 segment[0].x1 = x2 + 1;
316 segment[0].x2 = x2 + 1;
318 /* arrowhead */
319 segment[1].x1 = x2 - 6;
320 segment[1].x2 = x2;
322 segment[2].x1 = x2;
323 segment[2].x2 = x2 - 6;
325 /* line */
326 segment[3].x1 = mx + fw/2 + 2;
327 segment[3].x2 = x2;
329 XDrawSegments(dpy, root, gc, segment, 4);
331 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
333 /* Display the width. */
334 wDrawString(root, scr->info_text_font, gc,
335 mx - fw/2 + 1, y - s + fh/2 + 1, num, strlen(num));
336 } else {
337 XClearArea(dpy, scr->geometry_display, 1, 1,
338 scr->geometry_display_width-2, scr->geometry_display_height-2,
339 False);
340 sprintf(num, "%i x %-i", (x2 - x1 - wwin->normal_hints->base_width)
341 / wwin->normal_hints->width_inc,
342 (by - ty - wwin->normal_hints->base_height)
343 / wwin->normal_hints->height_inc);
344 fw = wTextWidth(scr->info_text_font->font, num, strlen(num));
346 XSetForeground(dpy, scr->info_text_gc, scr->black_pixel);
348 /* Display the height. */
349 wDrawString(scr->geometry_display, scr->info_text_font,
350 scr->info_text_gc,
351 (scr->geometry_display_width-fw)/2,
352 (scr->geometry_display_height-fh)/2 +scr->info_text_font->y,
353 num, strlen(num));
354 wDrawBevel(scr->geometry_display, scr->geometry_display_width+1,
355 scr->geometry_display_height+1,
356 scr->widget_texture, WREL_RAISED);
361 static void
362 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
364 WScreen *scr = wwin->screen_ptr;
366 wPreferences.size_display++;
367 wPreferences.size_display %= NUM_DISPLAYS;
369 if (wPreferences.size_display == WDIS_NEW) {
370 XUnmapWindow(dpy, scr->geometry_display);
371 } else {
372 if (wPreferences.size_display == WDIS_CENTER) {
373 moveGeometryDisplayCentered(scr,
374 scr->scr_width / 2, scr->scr_height / 2);
375 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
376 moveGeometryDisplayCentered(scr, 1, 1);
377 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
378 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
380 XMapRaised(dpy, scr->geometry_display);
381 showGeometry(wwin, x, y, x + w, y + h, dir);
386 static void
387 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
389 WScreen *scr = wwin->screen_ptr;
391 if (wPreferences.size_display == WDIS_NEW)
392 return;
394 if (wPreferences.size_display == WDIS_CENTER) {
395 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
396 scr->scr_height / 2);
397 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
398 moveGeometryDisplayCentered(scr, 1, 1);
399 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
400 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
402 XMapRaised(dpy, scr->geometry_display);
403 showGeometry(wwin, x, y, x + w, y + h, 0);
406 #define unmapGeometryDisplay(w) \
407 XUnmapWindow(dpy, (w)->screen_ptr->geometry_display);
410 static void
411 doWindowMove(WWindow *wwin, LinkedList *list, int dx, int dy)
413 WWindow *tmpw;
414 int x, y;
415 int scr_width = wwin->screen_ptr->scr_width;
416 int scr_height = wwin->screen_ptr->scr_height;
418 if (!list) {
419 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
420 } else {
421 while (list) {
422 tmpw = list->head;
423 x = tmpw->frame_x + dx;
424 y = tmpw->frame_y + dy;
426 /* don't let windows become unreachable */
428 if (x + (int)tmpw->frame->core->width < 20)
429 x = 20 - (int)tmpw->frame->core->width;
430 else if (x + 20 > scr_width)
431 x = scr_width - 20;
433 if (y + (int)tmpw->frame->core->height < 20)
434 y = 20 - (int)tmpw->frame->core->height;
435 else if (y + 20 > scr_height)
436 y = scr_height - 20;
438 wWindowMove(tmpw, x, y);
439 list = list->tail;
445 static void
446 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
448 Window root = wwin->screen_ptr->root_win;
449 GC gc = wwin->screen_ptr->frame_gc;
450 int h = 0;
451 int bottom = 0;
453 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
454 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
456 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
457 /* Can't use wwin-frame->bottom_width because, in some cases
458 (e.g. interactive placement), frame does not point to anything. */
459 bottom = RESIZEBAR_HEIGHT - 1;
461 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
463 if (h > 0) {
464 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
466 if (bottom > 0) {
467 XDrawLine(dpy, root, gc, x + 1,
468 y + height - bottom,
469 x + width + 1,
470 y + height - bottom);
475 static void
476 drawFrames(WWindow *wwin, LinkedList *list, int dx, int dy)
478 WWindow *tmpw;
479 int scr_width = wwin->screen_ptr->scr_width;
480 int scr_height = wwin->screen_ptr->scr_height;
481 int x, y;
483 if (!list) {
485 x = wwin->frame_x + dx;
486 y = wwin->frame_y + dy;
488 drawTransparentFrame(wwin, x, y,
489 wwin->frame->core->width,
490 wwin->frame->core->height);
492 } else {
493 while (list) {
494 tmpw = list->head;
495 x = tmpw->frame_x + dx;
496 y = tmpw->frame_y + dy;
498 /* don't let windows become unreachable */
500 if (x + (int)tmpw->frame->core->width < 20)
501 x = 20 - (int)tmpw->frame->core->width;
502 else if (x + 20 > scr_width)
503 x = scr_width - 20;
505 if (y + (int)tmpw->frame->core->height < 20)
506 y = 20 - (int)tmpw->frame->core->height;
507 else if (y + 20 > scr_height)
508 y = scr_height - 20;
510 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
511 tmpw->frame->core->height);
513 list = list->tail;
520 static void
521 flushMotion()
523 XEvent ev;
525 XSync(dpy, False);
526 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
530 static void
531 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
532 int new_workspace, int rewind)
534 /* do not let window be unmapped */
535 if (opaque_move) {
536 wwin->flags.changing_workspace = 1;
537 wWindowChangeWorkspace(wwin, new_workspace);
539 /* go to new workspace */
540 wWorkspaceChange(scr, new_workspace);
542 wwin->flags.changing_workspace = 0;
544 if (rewind)
545 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
546 else
547 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
549 flushMotion();
551 if (!opaque_move) {
552 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
553 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
554 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
561 typedef struct {
562 /* arrays of WWindows sorted by the respective border position */
563 WWindow **topList; /* top border */
564 WWindow **leftList; /* left border */
565 WWindow **rightList; /* right border */
566 WWindow **bottomList; /* bottom border */
567 int count;
569 /* index of window in the above lists indicating the relative position
570 * of the window with the others */
571 int topIndex;
572 int leftIndex;
573 int rightIndex;
574 int bottomIndex;
576 int rubCount; /* for workspace switching */
578 int winWidth, winHeight; /* width/height of the window */
579 int realX, realY; /* actual position of the window */
580 int calcX, calcY; /* calculated position of window */
581 int omouseX, omouseY; /* old mouse position */
582 int mouseX, mouseY; /* last known position of the pointer */
583 } MoveData;
585 #define WTOP(w) (w)->frame_y
586 #define WLEFT(w) (w)->frame_x
587 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
588 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
590 static int
591 compareWTop(const void *a, const void *b)
593 WWindow *wwin1 = *(WWindow**)a;
594 WWindow *wwin2 = *(WWindow**)b;
596 if (WTOP(wwin1) > WTOP(wwin2))
597 return -1;
598 else if (WTOP(wwin1) < WTOP(wwin2))
599 return 1;
600 else
601 return 0;
605 static int
606 compareWLeft(const void *a, const void *b)
608 WWindow *wwin1 = *(WWindow**)a;
609 WWindow *wwin2 = *(WWindow**)b;
611 if (WLEFT(wwin1) > WLEFT(wwin2))
612 return -1;
613 else if (WLEFT(wwin1) < WLEFT(wwin2))
614 return 1;
615 else
616 return 0;
620 static int
621 compareWRight(const void *a, const void *b)
623 WWindow *wwin1 = *(WWindow**)a;
624 WWindow *wwin2 = *(WWindow**)b;
626 if (WRIGHT(wwin1) < WRIGHT(wwin2))
627 return -1;
628 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
629 return 1;
630 else
631 return 0;
636 static int
637 compareWBottom(const void *a, const void *b)
639 WWindow *wwin1 = *(WWindow**)a;
640 WWindow *wwin2 = *(WWindow**)b;
642 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
643 return -1;
644 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
645 return 1;
646 else
647 return 0;
651 static void
652 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
654 int i;
655 int newX2 = newX + data->winWidth;
656 int newY2 = newY + data->winHeight;
657 Bool ok = False;
659 if (newX < data->realX) {
660 if (data->rightIndex > 0
661 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
662 ok = True;
663 } else if (data->leftIndex <= data->count-1
664 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
665 ok = True;
667 } else if (newX > data->realX) {
668 if (data->leftIndex > 0
669 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
670 ok = True;
671 } else if (data->rightIndex <= data->count-1
672 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
673 ok = True;
677 if (!ok) {
678 if (newY < data->realY) {
679 if (data->bottomIndex > 0
680 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
681 ok = True;
682 } else if (data->topIndex <= data->count-1
683 && newY2 <= WTOP(data->topList[data->topIndex])) {
684 ok = True;
686 } else if (newY > data->realY) {
687 if (data->topIndex > 0
688 && newY2 > WTOP(data->topList[data->topIndex-1])) {
689 ok = True;
690 } else if (data->bottomIndex <= data->count-1
691 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
692 ok = True;
697 if (!ok)
698 return;
700 /* TODO: optimize this */
701 if (data->realY < WBOTTOM(data->bottomList[0])) {
702 data->bottomIndex = 0;
704 if (data->realX < WRIGHT(data->rightList[0])) {
705 data->rightIndex = 0;
707 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
708 data->leftIndex = 0;
710 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
711 data->topIndex = 0;
713 for (i = 0; i < data->count; i++) {
714 if (data->realY > WBOTTOM(data->bottomList[i])) {
715 data->bottomIndex = i + 1;
717 if (data->realX > WRIGHT(data->rightList[i])) {
718 data->rightIndex = i + 1;
720 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
721 data->leftIndex = i + 1;
723 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
724 data->topIndex = i + 1;
730 static void
731 freeMoveData(MoveData *data)
733 if (data->topList)
734 free(data->topList);
735 if (data->leftList)
736 free(data->leftList);
737 if (data->rightList)
738 free(data->rightList);
739 if (data->bottomList)
740 free(data->bottomList);
744 static void
745 updateMoveData(WWindow *wwin, MoveData *data)
747 WScreen *scr = wwin->screen_ptr;
748 WWindow *tmp;
749 int i;
751 data->count = 0;
752 tmp = scr->focused_window;
753 while (tmp) {
754 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
755 && !tmp->flags.miniaturized
756 && !tmp->flags.hidden
757 && !tmp->flags.obscured) {
758 data->topList[data->count] = tmp;
759 data->leftList[data->count] = tmp;
760 data->rightList[data->count] = tmp;
761 data->bottomList[data->count] = tmp;
762 data->count++;
764 tmp = tmp->prev;
767 if (data->count == 0) {
768 data->topIndex = 0;
769 data->leftIndex = 0;
770 data->rightIndex = 0;
771 data->bottomIndex = 0;
772 return;
776 * order from closest to the border of the screen to farthest
778 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
779 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
780 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
781 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
783 /* figure the position of the window relative to the others */
785 data->topIndex = -1;
786 data->leftIndex = -1;
787 data->rightIndex = -1;
788 data->bottomIndex = -1;
790 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
791 data->bottomIndex = 0;
793 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
794 data->rightIndex = 0;
796 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
797 data->leftIndex = 0;
799 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
800 data->topIndex = 0;
802 for (i = 0; i < data->count; i++) {
803 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
804 data->bottomIndex = i + 1;
806 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
807 data->rightIndex = i + 1;
809 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
810 data->leftIndex = i + 1;
812 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
813 data->topIndex = i + 1;
819 static void
820 initMoveData(WWindow *wwin, MoveData *data)
822 int i;
823 WWindow *tmp;
825 memset(data, 0, sizeof(MoveData));
827 for (i = 0, tmp = wwin->screen_ptr->focused_window;
828 tmp != NULL;
829 tmp = tmp->prev, i++);
831 if (i > 1) {
832 data->topList = wmalloc(sizeof(WWindow*) * i);
833 data->leftList = wmalloc(sizeof(WWindow*) * i);
834 data->rightList = wmalloc(sizeof(WWindow*) * i);
835 data->bottomList = wmalloc(sizeof(WWindow*) * i);
837 updateMoveData(wwin, data);
840 data->realX = wwin->frame_x;
841 data->realY = wwin->frame_y;
842 data->calcX = wwin->frame_x;
843 data->calcY = wwin->frame_y;
845 data->winWidth = wwin->frame->core->width + 2;
846 data->winHeight = wwin->frame->core->height + 2;
850 static Bool
851 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
853 WScreen *scr = wwin->screen_ptr;
854 Bool changed = False;
856 if (data->mouseX <= 1) {
857 if (scr->current_workspace > 0) {
859 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
860 True);
861 changed = True;
862 data->rubCount = 0;
864 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
866 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
867 True);
868 changed = True;
869 data->rubCount = 0;
871 } else if (data->mouseX >= scr->scr_width - 2) {
873 if (scr->current_workspace == scr->workspace_count - 1) {
875 if (wPreferences.ws_cycle
876 || scr->workspace_count == MAX_WORKSPACES) {
878 crossWorkspace(scr, wwin, opaqueMove, 0, False);
879 changed = True;
880 data->rubCount = 0;
882 /* if user insists on trying to go to next workspace even when
883 * it's already the last, create a new one */
884 else if (data->omouseX == data->mouseX
885 && wPreferences.ws_advance) {
887 /* detect user "rubbing" the window against the edge */
888 if (data->rubCount > 0
889 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
891 data->rubCount = -(data->rubCount + 1);
893 } else if (data->rubCount <= 0
894 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
896 data->rubCount = -data->rubCount + 1;
899 /* create a new workspace */
900 if (abs(data->rubCount) > 2) {
901 /* go to next workspace */
902 wWorkspaceNew(scr);
904 crossWorkspace(scr, wwin, opaqueMove,
905 scr->current_workspace+1, False);
906 changed = True;
907 data->rubCount = 0;
909 } else if (scr->current_workspace < scr->workspace_count) {
911 /* go to next workspace */
912 crossWorkspace(scr, wwin, opaqueMove,
913 scr->current_workspace+1, False);
914 changed = True;
915 data->rubCount = 0;
917 } else {
918 data->rubCount = 0;
921 return changed;
925 static void
926 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
927 Bool opaqueMove, int newMouseX, int newMouseY)
929 WScreen *scr = wwin->screen_ptr;
930 int dx, dy; /* how much mouse moved */
931 int winL, winR, winT, winB; /* requested new window position */
932 int newX, newY; /* actual new window position */
933 Bool hresist, vresist;
934 Bool updateIndex;
936 hresist = False;
937 vresist = False;
939 updateIndex = False;
941 /* check the direction of the movement */
942 dx = newMouseX - data->mouseX;
943 dy = newMouseY - data->mouseY;
945 data->omouseX = data->mouseX;
946 data->omouseY = data->mouseY;
947 data->mouseX = newMouseX;
948 data->mouseY = newMouseY;
950 winL = data->calcX + dx;
951 winR = data->calcX + data->winWidth + dx;
952 winT = data->calcY + dy;
953 winB = data->calcY + data->winHeight + dy;
955 newX = data->realX;
956 newY = data->realY;
958 if (doResistance) {
959 int edge;
960 int resist;
961 WWindow *rwin;
963 resist = wPreferences.edge_resistance;
964 /* horizontal movement: check horizontal edge resistances */
965 if (dx < 0) {
966 /* window is the leftmost window: check against screen edge */
967 edge = scr->totalUsableArea.x1;
969 /* check position of nearest window to the left */
970 if (data->rightIndex > 0) {
971 /* there is some window at the left: check if it will block
972 * the window */
973 rwin = data->rightList[data->rightIndex - 1];
975 if (data->realY > WBOTTOM(rwin)
976 || (data->realY + data->winHeight) < WTOP(rwin)) {
977 resist = 0;
978 } else {
979 edge = WRIGHT(rwin) + 1;
980 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
983 if (resist > 0 && winL >= edge - resist && winL <= edge) {
984 newX = edge;
985 hresist = True;
987 } else if (dx > 0) {
988 /* window is the rightmost window: check against screen edge */
989 edge = scr->totalUsableArea.x2;
991 /* check position of nearest window to the right */
992 if (data->leftIndex > 0) {
993 /* there is some window at the right: check if it will block
994 * the window */
995 rwin = data->leftList[data->leftIndex - 1];
997 if (data->realY > WBOTTOM(rwin)
998 || (data->realY + data->winHeight) < WTOP(rwin)) {
999 resist = 0;
1000 } else {
1001 edge = WLEFT(rwin);
1002 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1005 if (resist > 0 && winR <= edge + resist && winR >= edge) {
1006 newX = edge - data->winWidth;
1007 hresist = True;
1011 resist = wPreferences.edge_resistance;
1012 /* vertical movement: check vertical edge resistances */
1013 if (dy < 0) {
1014 /* window is the topmost window: check against screen edge */
1015 edge = scr->totalUsableArea.y1;
1017 /* check position of nearest window to the top */
1018 if (data->bottomIndex > 0) {
1019 /* there is some window at the top: check if it will block
1020 * the window */
1021 rwin = data->bottomList[data->bottomIndex - 1];
1023 if (data->realX > WRIGHT(rwin)
1024 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1025 resist = 0;
1026 } else {
1027 edge = WBOTTOM(rwin) + 1;
1028 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1031 if (resist > 0 && winT >= edge - resist && winT <= edge) {
1032 newY = edge;
1033 vresist = True;
1035 } else if (dy > 0) {
1036 /* window is the bottommost window: check against screen edge */
1037 edge = scr->totalUsableArea.y2;
1039 /* check position of nearest window to the bottom */
1040 if (data->topIndex > 0) {
1041 /* there is some window at the bottom: check if it will block
1042 * the window */
1043 rwin = data->topList[data->topIndex - 1];
1045 if (data->realX > WRIGHT(rwin)
1046 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1047 resist = 0;
1048 } else {
1049 edge = WTOP(rwin);
1050 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1053 if (resist > 0 && winB <= edge + resist && winB >= edge) {
1054 newY = edge - data->winHeight;
1055 vresist = True;
1060 /* update window position */
1061 data->calcX += dx;
1062 data->calcY += dy;
1064 if (((dx > 0 && data->calcX - data->realX > 0)
1065 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1066 newX = data->calcX;
1068 if (((dy > 0 && data->calcY - data->realY > 0)
1069 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1070 newY = data->calcY;
1072 if (data->realX != newX || data->realY != newY) {
1073 if (opaqueMove) {
1074 doWindowMove(wwin, scr->selected_windows,
1075 newX - wwin->frame_x,
1076 newY - wwin->frame_y);
1077 } else {
1078 /* erase frames */
1079 drawFrames(wwin, scr->selected_windows,
1080 data->realX - wwin->frame_x,
1081 data->realY - wwin->frame_y);
1084 if (!scr->selected_windows
1085 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1087 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1088 newY + data->winHeight/2);
1091 if (!opaqueMove) {
1092 /* draw frames */
1093 drawFrames(wwin, scr->selected_windows,
1094 newX - wwin->frame_x,
1095 newY - wwin->frame_y);
1098 if (!scr->selected_windows) {
1100 if (wPreferences.move_display == WDIS_NEW) {
1102 showPosition(wwin, data->realX, data->realY);
1105 showPosition(wwin, newX, newY);
1110 /* recalc relative window position */
1111 if (doResistance && (data->realX != newX || data->realY != newY)) {
1112 updateResistance(wwin, data, newX, newY);
1115 data->realX = newX;
1116 data->realY = newY;
1122 #if 0
1123 typedef struct _looper {
1124 WWindow *wwin;
1125 int x,y,w,h,ox,oy;
1126 } _looper;
1128 void
1129 _keyloop(_looper *lpr){
1130 WWindow *wwin = lpr->wwin;
1131 WScreen *scr = wwin->screen_ptr;
1132 int w = wwin->frame->core->width;
1133 int h = wwin->frame->core->height;
1134 int src_x = wwin->frame_x;
1135 int src_y = wwin->frame_y;
1137 if (!scr->selected_windows){
1138 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1140 XUngrabServer(dpy);
1141 XSync(dpy, False);
1142 wusleep(10000);
1143 XGrabServer(dpy);
1144 /* printf("called\n");*/
1145 if (!scr->selected_windows){
1146 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1148 /* reset timer */
1149 if(scr->keymove_tick)
1150 WMAddTimerHandler(15000,(WMCallback*)_keyloop, lpr);
1153 #endif
1154 #define _KS 20
1157 wKeyboardMoveResizeWindow(WWindow *wwin)
1159 WScreen *scr = wwin->screen_ptr;
1160 Window root = scr->root_win;
1161 XEvent event;
1162 int w = wwin->frame->core->width;
1163 int h = wwin->frame->core->height;
1164 int scr_width = wwin->screen_ptr->scr_width;
1165 int scr_height = wwin->screen_ptr->scr_height;
1166 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1167 int src_x = wwin->frame_x;
1168 int src_y = wwin->frame_y;
1169 int done,off_x,off_y,ww,wh;
1170 int kspeed = 1;
1171 Time lastTime = 0;
1172 KeySym keysym=NoSymbol;
1173 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1176 int timer;
1177 _looper looper;
1178 looper.wwin=wwin;
1179 scr->keymove_tick=1;
1180 WMAddTimerHandler(1000,(WMCallback*)_keyloop, &looper);
1183 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1184 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1185 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1186 ctrlmode=done=off_x=off_y=0;
1188 XSync(dpy, False);
1189 wusleep(10000);
1190 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1192 if (!wwin->flags.selected) {
1193 wUnselectWindows(scr);
1195 XGrabServer(dpy);
1196 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1197 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1198 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1200 if (wwin->flags.shaded || scr->selected_windows) {
1201 if(scr->selected_windows)
1202 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1203 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1204 if(!scr->selected_windows)
1205 mapPositionDisplay(wwin, src_x, src_y, w, h);
1206 } else {
1207 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1209 ww=w;
1210 wh=h;
1211 while(1) {
1213 looper.ox=off_x;
1214 looper.oy=off_y;
1216 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1217 | ButtonPressMask | ExposureMask, &event);
1218 if (wwin->flags.shaded || scr->selected_windows) {
1219 if(scr->selected_windows)
1220 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1221 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1222 /*** I HATE EDGE RESISTANCE - ]d ***/
1224 else {
1225 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1228 if(ctrlmode)
1229 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1231 XUngrabServer(dpy);
1232 XSync(dpy, False);
1234 switch (event.type) {
1235 case KeyPress:
1236 /* accelerate */
1237 if (event.xkey.time - lastTime > 50) {
1238 kspeed = 1;
1239 } else {
1240 if (kspeed < 20)
1241 kspeed++;
1243 lastTime = event.xkey.time;
1245 if (event.xkey.state & ControlMask && !wwin->flags.shaded){
1246 ctrlmode=1;
1247 wUnselectWindows(scr);
1249 else {
1250 ctrlmode=0;
1252 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr){
1253 if(ctrlmode)
1254 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1255 else
1256 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1258 else {
1259 keysym = XLookupKeysym(&event.xkey, 0);
1260 switch(keysym){
1261 case XK_Return:
1262 done=2;
1263 break;
1264 case XK_Escape:
1265 done=1;
1266 break;
1267 case XK_Up:
1268 case XK_KP_Up:
1269 case XK_k:
1270 if (ctrlmode){
1271 h-=kspeed;
1273 else off_y-=kspeed;
1274 break;
1275 case XK_Down:
1276 case XK_KP_Down:
1277 case XK_j:
1278 if (ctrlmode){
1279 h+=kspeed;
1281 else off_y+=kspeed;
1282 break;
1283 case XK_Left:
1284 case XK_KP_Left:
1285 case XK_h:
1286 if (ctrlmode){
1287 w-=kspeed;
1289 else off_x-=kspeed;
1290 break;
1291 case XK_Right:
1292 case XK_KP_Right:
1293 case XK_l:
1294 if (ctrlmode){
1295 w+=kspeed;
1297 else off_x+=kspeed;
1298 break;
1300 ww=w;wh=h;
1301 wh-=vert_border;
1302 wWindowConstrainSize(wwin, &ww, &wh);
1303 wh+=vert_border;
1305 if (wPreferences.ws_cycle){
1306 if (src_x + off_x + wwin->frame->core->width < 20){
1307 if(!scr->current_workspace) {
1308 wWorkspaceChange(scr, scr->workspace_count-1);
1310 else wWorkspaceChange(scr, scr->current_workspace-1);
1311 off_x += scr_width;
1313 else if (src_x + off_x + 20 > scr_width){
1314 if(scr->current_workspace == scr->workspace_count-1) {
1315 wWorkspaceChange(scr, 0);
1317 else wWorkspaceChange(scr, scr->current_workspace+1);
1318 off_x -= scr_width;
1321 else {
1322 if (src_x + off_x + wwin->frame->core->width < 20)
1323 off_x = 20 - wwin->frame->core->width - src_x;
1324 else if (src_x + off_x + 20 > scr_width)
1325 off_x = scr_width - 20 - src_x;
1328 if (src_y + off_y + wwin->frame->core->height < 20)
1329 off_y = 20 - wwin->frame->core->height - src_y;
1330 else if (src_y + off_y + 20 > scr_height)
1331 off_y = scr_height - 20 - src_y;
1334 break;
1335 case ButtonPress:
1336 case ButtonRelease:
1337 done=1;
1338 break;
1339 default:
1340 WMHandleEvent(&event);
1341 break;
1344 XGrabServer(dpy);
1345 /*xxx*/
1347 if (wwin->flags.shaded && !scr->selected_windows){
1348 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1350 else {
1351 if(ctrlmode){
1352 unmapPositionDisplay(wwin);
1353 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1355 else if(!scr->selected_windows){
1356 unmapGeometryDisplay(wwin);
1357 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1361 if (wwin->flags.shaded || scr->selected_windows) {
1362 if(scr->selected_windows)
1363 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1364 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1366 else {
1367 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1371 if(ctrlmode){
1372 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1374 else if(!scr->selected_windows)
1375 showPosition(wwin, src_x+off_x, src_y+off_y);
1376 /**/
1378 if(done){
1379 scr->keymove_tick=0;
1381 WMDeleteTimerWithClientData(&looper);
1383 if (wwin->flags.shaded || scr->selected_windows) {
1384 if(scr->selected_windows)
1385 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1386 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1388 else {
1389 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1392 if(ctrlmode){
1393 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1394 unmapGeometryDisplay(wwin);
1396 else
1397 unmapPositionDisplay(wwin);
1398 XUngrabKeyboard(dpy, CurrentTime);
1399 XUngrabPointer(dpy, CurrentTime);
1400 XUngrabServer(dpy);
1402 if(done==2) {
1403 if (wwin->flags.shaded || scr->selected_windows) {
1404 LinkedList *list;
1405 list=scr->selected_windows;
1406 if (!scr->selected_windows) {
1407 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1408 wWindowSynthConfigureNotify(wwin);
1409 } else {
1410 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1411 while (list) {
1412 wWindowSynthConfigureNotify(list->head);
1413 list = list->tail;
1416 } else {
1417 if (wwin->client.width != ww)
1418 wwin->flags.user_changed_width = 1;
1420 if (wwin->client.height != wh - vert_border)
1421 wwin->flags.user_changed_height = 1;
1423 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1424 ww, wh - vert_border);
1425 wWindowSynthConfigureNotify(wwin);
1427 wWindowChangeWorkspace(wwin, scr->current_workspace);
1428 wSetFocusTo(scr, wwin);
1430 return 1;
1437 *----------------------------------------------------------------------
1438 * wMouseMoveWindow--
1439 * Move the named window and the other selected ones (if any),
1440 * interactively. Also shows the position of the window, if only one
1441 * window is being moved.
1442 * If the window is not on the selected window list, the selected
1443 * windows are deselected.
1444 * If shift is pressed during the operation, the position display
1445 * is changed to another type.
1447 * Returns:
1448 * True if the window was moved, False otherwise.
1450 * Side effects:
1451 * The window(s) position is changed, and the client(s) are
1452 * notified about that.
1453 * The position display configuration may be changed.
1454 *----------------------------------------------------------------------
1457 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1459 WScreen *scr = wwin->screen_ptr;
1460 XEvent event;
1461 Window root = scr->root_win;
1462 KeyCode shiftl, shiftr;
1463 int started = 0;
1464 int warped = 0;
1465 /* This needs not to change while moving, else bad things can happen */
1466 int opaqueMove = wPreferences.opaque_move;
1467 MoveData moveData;
1468 #ifdef GHOST_WINDOW_MOVE
1469 RImage *rimg;
1471 rimg = InitGhostWindowMove(scr);
1472 #endif
1474 initMoveData(wwin, &moveData);
1476 moveData.mouseX = ev->xmotion.x_root;
1477 moveData.mouseY = ev->xmotion.y_root;
1479 if (!wwin->flags.selected) {
1480 /* this window is not selected, unselect others and move only wwin */
1481 wUnselectWindows(scr);
1483 #ifdef DEBUG
1484 puts("Moving window");
1485 #endif
1486 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1487 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1488 while (1) {
1489 if (warped) {
1490 int junk;
1491 Window junkw;
1493 /* XWarpPointer() doesn't seem to generate Motion events, so
1494 we've got to simulate them */
1495 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1496 &event.xmotion.y_root, &junk, &junk,
1497 (unsigned *) &junk);
1498 } else {
1499 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1500 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1501 &event);
1503 if (event.type == MotionNotify) {
1504 /* compress MotionNotify events */
1505 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1508 switch (event.type) {
1509 case KeyPress:
1510 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1511 && started && !scr->selected_windows) {
1513 if (!opaqueMove) {
1514 drawFrames(wwin, scr->selected_windows,
1515 moveData.realX - wwin->frame_x,
1516 moveData.realY - wwin->frame_y);
1519 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1520 moveData.winWidth, moveData.winHeight);
1522 if (!opaqueMove) {
1523 drawFrames(wwin, scr->selected_windows,
1524 moveData.realX - wwin->frame_x,
1525 moveData.realY - wwin->frame_y);
1528 break;
1530 case MotionNotify:
1531 if (started) {
1532 updateWindowPosition(wwin, &moveData,
1533 scr->selected_windows == NULL
1534 && wPreferences.edge_resistance > 0,
1535 opaqueMove,
1536 event.xmotion.x_root,
1537 event.xmotion.y_root);
1539 if (!warped && !wPreferences.no_autowrap) {
1540 int oldWorkspace = scr->current_workspace;
1542 if (!opaqueMove) {
1543 drawFrames(wwin, scr->selected_windows,
1544 moveData.realX - wwin->frame_x,
1545 moveData.realY - wwin->frame_y);
1547 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1548 if (scr->current_workspace != oldWorkspace
1549 && wPreferences.edge_resistance > 0
1550 && scr->selected_windows == NULL)
1551 updateMoveData(wwin, &moveData);
1552 warped = 1;
1555 if (!opaqueMove) {
1556 drawFrames(wwin, scr->selected_windows,
1557 moveData.realX - wwin->frame_x,
1558 moveData.realY - wwin->frame_y);
1560 } else {
1561 warped = 0;
1563 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1564 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1566 XChangeActivePointerGrab(dpy, ButtonMotionMask
1567 | ButtonReleaseMask | ButtonPressMask,
1568 wCursor[WCUR_MOVE], CurrentTime);
1569 started = 1;
1570 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1571 CurrentTime);
1573 if (!scr->selected_windows)
1574 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1575 moveData.winWidth, moveData.winHeight);
1577 if (started && !opaqueMove)
1578 drawFrames(wwin, scr->selected_windows, 0, 0);
1580 if (!opaqueMove)
1581 XGrabServer(dpy);
1583 break;
1585 case ButtonPress:
1586 break;
1588 case ButtonRelease:
1589 if (event.xbutton.button != ev->xbutton.button)
1590 break;
1592 if (started) {
1593 if (!opaqueMove) {
1594 drawFrames(wwin, scr->selected_windows,
1595 moveData.realX - wwin->frame_x,
1596 moveData.realY - wwin->frame_y);
1597 XSync(dpy, 0);
1598 doWindowMove(wwin, scr->selected_windows,
1599 moveData.realX - wwin->frame_x,
1600 moveData.realY - wwin->frame_y);
1602 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1603 wWindowSynthConfigureNotify(wwin);
1604 #endif
1605 XUngrabKeyboard(dpy, CurrentTime);
1606 XUngrabServer(dpy);
1607 if (!opaqueMove) {
1608 wWindowChangeWorkspace(wwin, scr->current_workspace);
1609 wSetFocusTo(scr, wwin);
1611 if (wPreferences.move_display == WDIS_NEW)
1612 showPosition(wwin, moveData.realX, moveData.realY);
1614 if (!scr->selected_windows) {
1615 /* get rid of the geometry window */
1616 unmapPositionDisplay(wwin);
1619 #ifdef DEBUG
1620 puts("End move window");
1621 #endif
1622 freeMoveData(&moveData);
1624 return started;
1626 default:
1627 if (started && !opaqueMove) {
1628 drawFrames(wwin, scr->selected_windows,
1629 moveData.realX - wwin->frame_x,
1630 moveData.realY - wwin->frame_y);
1631 XUngrabServer(dpy);
1632 WMHandleEvent(&event);
1633 XSync(dpy, False);
1634 XGrabServer(dpy);
1635 drawFrames(wwin, scr->selected_windows,
1636 moveData.realX - wwin->frame_x,
1637 moveData.realY - wwin->frame_y);
1638 } else {
1639 WMHandleEvent(&event);
1641 break;
1645 freeMoveData(&moveData);
1647 return 0;
1651 #define RESIZEBAR 1
1652 #define HCONSTRAIN 2
1654 static int
1655 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1656 int flags)
1658 int w = wwin->frame->core->width - 1;
1659 int cw = wwin->frame->resizebar_corner_width;
1660 int dir;
1662 /* if not resizing through the resizebar */
1663 if (!(flags & RESIZEBAR)) {
1664 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1665 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1666 if (abs(dx) < 2 || abs(dy) < 2) {
1667 if (abs(dy) > abs(dx))
1668 xdir = 0;
1669 else
1670 ydir = 0;
1672 return (xdir | ydir);
1675 /* window is too narrow. allow diagonal resize */
1676 if (cw * 2 >= w) {
1677 int ydir;
1679 if (flags & HCONSTRAIN)
1680 ydir = 0;
1681 else
1682 ydir = DOWN;
1683 if (x < cw)
1684 return (LEFT | ydir);
1685 else
1686 return (RIGHT | ydir);
1688 /* vertical resize */
1689 if ((x > cw) && (x < w - cw))
1690 return DOWN;
1692 if (x < cw)
1693 dir = LEFT;
1694 else
1695 dir = RIGHT;
1697 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1698 dir |= DOWN;
1700 return dir;
1704 void
1705 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1707 XEvent event;
1708 WScreen *scr = wwin->screen_ptr;
1709 Window root = scr->root_win;
1710 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1711 int fw = wwin->frame->core->width;
1712 int fh = wwin->frame->core->height;
1713 int fx = wwin->frame_x;
1714 int fy = wwin->frame_y;
1715 int is_resizebar = (wwin->frame->resizebar
1716 && ev->xany.window==wwin->frame->resizebar->window);
1717 int orig_x, orig_y;
1718 int started;
1719 int dw, dh;
1720 int rw = fw, rh = fh;
1721 int rx1, ry1, rx2, ry2;
1722 int res = 0;
1723 KeyCode shiftl, shiftr;
1724 int h = 0;
1725 int orig_fx = fx;
1726 int orig_fy = fy;
1727 int orig_fw = fw;
1728 int orig_fh = fh;
1730 if (wwin->flags.shaded) {
1731 wwarning("internal error: tryein");
1732 return;
1734 orig_x = ev->xbutton.x_root;
1735 orig_y = ev->xbutton.y_root;
1737 started = 0;
1738 #ifdef DEBUG
1739 puts("Resizing window");
1740 #endif
1742 wUnselectWindows(scr);
1743 rx1 = fx;
1744 rx2 = fx + fw - 1;
1745 ry1 = fy;
1746 ry2 = fy + fh - 1;
1747 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1748 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1749 if (!WFLAGP(wwin, no_titlebar))
1750 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
1751 else
1752 h = 0;
1753 while (1) {
1754 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1755 | ButtonPressMask | ExposureMask, &event);
1756 switch (event.type) {
1757 case KeyPress:
1758 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1759 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1760 && started) {
1761 drawTransparentFrame(wwin, fx, fy, fw, fh);
1762 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1763 drawTransparentFrame(wwin, fx, fy, fw, fh);
1765 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1766 break;
1768 case MotionNotify:
1769 if (started) {
1770 dw = 0;
1771 dh = 0;
1773 orig_fx = fx;
1774 orig_fy = fy;
1775 orig_fw = fw;
1776 orig_fh = fh;
1778 if (res & LEFT)
1779 dw = orig_x - event.xmotion.x_root;
1780 else if (res & RIGHT)
1781 dw = event.xmotion.x_root - orig_x;
1782 if (res & UP)
1783 dh = orig_y - event.xmotion.y_root;
1784 else if (res & DOWN)
1785 dh = event.xmotion.y_root - orig_y;
1787 orig_x = event.xmotion.x_root;
1788 orig_y = event.xmotion.y_root;
1790 rw += dw;
1791 rh += dh;
1792 fw = rw;
1793 fh = rh - vert_border;
1794 wWindowConstrainSize(wwin, &fw, &fh);
1795 fh += vert_border;
1796 if (res & LEFT)
1797 fx = rx2 - fw + 1;
1798 else if (res & RIGHT)
1799 fx = rx1;
1800 if (res & UP)
1801 fy = ry2 - fh + 1;
1802 else if (res & DOWN)
1803 fy = ry1;
1804 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1805 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1806 int tx, ty;
1807 Window junkw;
1808 int flags;
1810 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1811 orig_x, orig_y, &tx, &ty, &junkw);
1813 /* check if resizing through resizebar */
1814 if (is_resizebar)
1815 flags = RESIZEBAR;
1816 else
1817 flags = 0;
1819 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1820 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1821 flags |= HCONSTRAIN;
1823 res = getResizeDirection(wwin, tx, ty,
1824 orig_x - event.xmotion.x_root,
1825 orig_y - event.xmotion.y_root, flags);
1827 XChangeActivePointerGrab(dpy, ButtonMotionMask
1828 | ButtonReleaseMask | ButtonPressMask,
1829 wCursor[WCUR_RESIZE], CurrentTime);
1830 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1831 CurrentTime);
1833 XGrabServer(dpy);
1835 /* Draw the resize frame for the first time. */
1836 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1838 drawTransparentFrame(wwin, fx, fy, fw, fh);
1840 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1842 started = 1;
1844 if (started) {
1845 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1846 drawTransparentFrame(wwin, orig_fx, orig_fy,
1847 orig_fw, orig_fh);
1848 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1849 drawTransparentFrame(wwin, fx, fy, fw, fh);
1850 } else {
1851 drawTransparentFrame(wwin, orig_fx, orig_fy,
1852 orig_fw, orig_fh);
1853 drawTransparentFrame(wwin, fx, fy, fw, fh);
1855 if (fh != orig_fh || fw != orig_fw) {
1856 if (wPreferences.size_display == WDIS_NEW) {
1857 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1858 orig_fy + orig_fh, res);
1860 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1863 break;
1865 case ButtonPress:
1866 break;
1868 case ButtonRelease:
1869 if (event.xbutton.button != ev->xbutton.button)
1870 break;
1872 if (started) {
1873 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1875 drawTransparentFrame(wwin, fx, fy, fw, fh);
1877 XUngrabKeyboard(dpy, CurrentTime);
1878 unmapGeometryDisplay(wwin);
1879 XUngrabServer(dpy);
1881 if (wwin->client.width != fw)
1882 wwin->flags.user_changed_width = 1;
1884 if (wwin->client.height != fh - vert_border)
1885 wwin->flags.user_changed_height = 1;
1887 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1889 #ifdef DEBUG
1890 puts("End resize window");
1891 #endif
1892 return;
1894 default:
1895 WMHandleEvent(&event);
1900 #undef LEFT
1901 #undef RIGHT
1902 #undef HORIZONTAL
1903 #undef UP
1904 #undef DOWN
1905 #undef VERTICAL
1906 #undef HCONSTRAIN
1907 #undef RESIZEBAR
1909 void
1910 wUnselectWindows(WScreen *scr)
1912 WWindow *wwin;
1914 while (scr->selected_windows) {
1915 wwin = scr->selected_windows->head;
1916 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
1917 wIconSelect(wwin->icon);
1919 wSelectWindow(wwin, False);
1923 #ifndef LITE
1924 static void
1925 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
1927 WWindow *tmpw;
1929 /* select the windows and put them in the selected window list */
1930 tmpw = scr->focused_window;
1931 while (tmpw != NULL) {
1932 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
1933 if ((tmpw->frame->workspace == scr->current_workspace
1934 || IS_OMNIPRESENT(tmpw))
1935 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
1936 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
1937 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
1938 wSelectWindow(tmpw, True);
1941 tmpw = tmpw->prev;
1946 void
1947 wSelectWindows(WScreen *scr, XEvent *ev)
1949 XEvent event;
1950 Window root = scr->root_win;
1951 GC gc = scr->frame_gc;
1952 int xp = ev->xbutton.x_root;
1953 int yp = ev->xbutton.y_root;
1954 int w = 0, h = 0;
1955 int x = xp, y = yp;
1957 #ifdef DEBUG
1958 puts("Selecting windows");
1959 #endif
1960 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
1961 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1962 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
1963 CurrentTime) != Success) {
1964 return;
1966 XGrabServer(dpy);
1968 wUnselectWindows(scr);
1970 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
1971 while (1) {
1972 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
1973 | ButtonPressMask, &event);
1975 switch (event.type) {
1976 case MotionNotify:
1977 XDrawRectangle(dpy, root, gc, x, y, w, h);
1978 x = event.xmotion.x_root;
1979 if (x < xp) {
1980 w = xp - x;
1981 } else {
1982 w = x - xp;
1983 x = xp;
1985 y = event.xmotion.y_root;
1986 if (y < yp) {
1987 h = yp - y;
1988 } else {
1989 h = y - yp;
1990 y = yp;
1992 XDrawRectangle(dpy, root, gc, x, y, w, h);
1993 break;
1995 case ButtonPress:
1996 break;
1998 case ButtonRelease:
1999 if (event.xbutton.button != ev->xbutton.button)
2000 break;
2002 XDrawRectangle(dpy, root, gc, x, y, w, h);
2003 XUngrabServer(dpy);
2004 XUngrabPointer(dpy, CurrentTime);
2005 selectWindowsInside(scr, x, y, x + w, y + h);
2007 #ifdef KWM_HINTS
2008 wKWMSelectRootRegion(scr, x, y, w, h,
2009 event.xbutton.state & ControlMask);
2010 #endif /* KWM_HINTS */
2012 #ifdef DEBUG
2013 puts("End window selection");
2014 #endif
2015 return;
2017 default:
2018 WMHandleEvent(&event);
2019 break;
2023 #endif /* !LITE */
2025 void
2026 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2027 unsigned width, unsigned height)
2029 WScreen *scr = wwin->screen_ptr;
2030 Window root = scr->root_win;
2031 int x, y, h = 0;
2032 XEvent event;
2033 KeyCode shiftl, shiftr;
2034 Window junkw;
2035 int junk;
2037 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2038 GrabModeAsync, GrabModeAsync, None,
2039 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2040 *x_ret = 0;
2041 *y_ret = 0;
2042 return;
2044 if (!WFLAGP(wwin, no_titlebar)) {
2045 h = scr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
2046 height += h;
2048 if (!WFLAGP(wwin, no_resizebar)) {
2049 height += RESIZEBAR_HEIGHT;
2051 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2052 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2053 (unsigned *) &junk);
2054 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2056 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2058 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2059 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2060 while (1) {
2061 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2062 &event);
2063 switch (event.type) {
2064 case KeyPress:
2065 if ((event.xkey.keycode == shiftl)
2066 || (event.xkey.keycode == shiftr)) {
2067 drawTransparentFrame(wwin,
2068 x - width/2, y - h/2, width, height);
2069 cyclePositionDisplay(wwin,
2070 x - width/2, y - h/2, width, height);
2071 drawTransparentFrame(wwin,
2072 x - width/2, y - h/2, width, height);
2074 break;
2076 case MotionNotify:
2077 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2079 x = event.xmotion.x_root;
2080 y = event.xmotion.y_root;
2082 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2083 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2085 showPosition(wwin, x - width/2, y - h/2);
2087 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2089 break;
2091 case ButtonPress:
2092 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2093 XSync(dpy, 0);
2094 *x_ret = x - width/2;
2095 *y_ret = y - h/2;
2096 XUngrabPointer(dpy, CurrentTime);
2097 XUngrabKeyboard(dpy, CurrentTime);
2098 /* get rid of the geometry window */
2099 unmapPositionDisplay(wwin);
2100 return;
2102 default:
2103 WMHandleEvent(&event);
2104 break;