0.51.1 pre snapshot. Be careful, it may be buggy. It fixes some bugs though.
[wmaker-crm.git] / src / moveres.c
blob9b0892b619eafff373aee9e238c60f6f654d7270
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)
585 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height)
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.obscured) {
753 data->topList[data->count] = tmp;
754 data->leftList[data->count] = tmp;
755 data->rightList[data->count] = tmp;
756 data->bottomList[data->count] = tmp;
757 data->count++;
759 tmp = tmp->prev;
762 if (data->count == 0) {
763 data->topIndex = 0;
764 data->leftIndex = 0;
765 data->rightIndex = 0;
766 data->bottomIndex = 0;
767 return;
771 * order from closest to the border of the screen to farthest
773 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
774 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
775 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
776 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
778 /* figure the position of the window relative to the others */
780 data->topIndex = -1;
781 data->leftIndex = -1;
782 data->rightIndex = -1;
783 data->bottomIndex = -1;
785 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
786 data->bottomIndex = 0;
788 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
789 data->rightIndex = 0;
791 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
792 data->leftIndex = 0;
794 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
795 data->topIndex = 0;
797 for (i = 0; i < data->count; i++) {
798 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
799 data->bottomIndex = i + 1;
801 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
802 data->rightIndex = i + 1;
804 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
805 data->leftIndex = i + 1;
807 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
808 data->topIndex = i + 1;
814 static void
815 initMoveData(WWindow *wwin, MoveData *data)
817 int i;
818 WWindow *tmp;
820 memset(data, 0, sizeof(MoveData));
822 for (i = 0, tmp = wwin->screen_ptr->focused_window;
823 tmp != NULL;
824 tmp = tmp->prev, i++);
826 if (i <= 1)
827 return;
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);
836 data->realX = wwin->frame_x;
837 data->realY = wwin->frame_y;
838 data->calcX = wwin->frame_x;
839 data->calcY = wwin->frame_y;
841 data->winWidth = wwin->frame->core->width + 2;
842 data->winHeight = wwin->frame->core->height + 2;
846 static Bool
847 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
849 WScreen *scr = wwin->screen_ptr;
850 Bool changed = False;
852 if (data->mouseX <= 1) {
853 if (scr->current_workspace > 0) {
855 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
856 True);
857 changed = True;
858 data->rubCount = 0;
860 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
862 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
863 True);
864 changed = True;
865 data->rubCount = 0;
867 } else if (data->mouseX >= scr->scr_width - 2) {
869 if (scr->current_workspace == scr->workspace_count - 1) {
871 if (wPreferences.ws_cycle
872 || scr->workspace_count == MAX_WORKSPACES) {
874 crossWorkspace(scr, wwin, opaqueMove, 0, False);
875 changed = True;
876 data->rubCount = 0;
878 /* if user insists on trying to go to next workspace even when
879 * it's already the last, create a new one */
880 else if (data->omouseX == data->mouseX
881 && wPreferences.ws_advance) {
883 /* detect user "rubbing" the window against the edge */
884 if (data->rubCount > 0
885 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
887 data->rubCount = -(data->rubCount + 1);
889 } else if (data->rubCount <= 0
890 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
892 data->rubCount = -data->rubCount + 1;
895 /* create a new workspace */
896 if (abs(data->rubCount) > 2) {
897 /* go to next workspace */
898 wWorkspaceNew(scr);
900 crossWorkspace(scr, wwin, opaqueMove,
901 scr->current_workspace+1, False);
902 changed = True;
903 data->rubCount = 0;
905 } else if (scr->current_workspace < scr->workspace_count) {
907 /* go to next workspace */
908 crossWorkspace(scr, wwin, opaqueMove,
909 scr->current_workspace+1, False);
910 changed = True;
911 data->rubCount = 0;
913 } else {
914 data->rubCount = 0;
917 return changed;
921 static void
922 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
923 Bool opaqueMove, int newMouseX, int newMouseY)
925 WScreen *scr = wwin->screen_ptr;
926 int dx, dy; /* how much mouse moved */
927 int winL, winR, winT, winB; /* requested new window position */
928 int newX, newY; /* actual new window position */
929 Bool hresist, vresist;
930 Bool updateIndex;
932 hresist = False;
933 vresist = False;
935 updateIndex = False;
937 /* check the direction of the movement */
938 dx = newMouseX - data->mouseX;
939 dy = newMouseY - data->mouseY;
941 data->omouseX = data->mouseX;
942 data->omouseY = data->mouseY;
943 data->mouseX = newMouseX;
944 data->mouseY = newMouseY;
946 winL = data->calcX + dx;
947 winR = data->calcX + data->winWidth + dx;
948 winT = data->calcY + dy;
949 winB = data->calcY + data->winHeight + dy;
951 newX = data->realX;
952 newY = data->realY;
954 if (doResistance) {
955 int edge;
956 int resist;
957 WWindow *rwin;
959 resist = wPreferences.edge_resistance;
960 /* horizontal movement: check horizontal edge resistances */
961 if (dx < 0) {
962 /* window is the leftmost window: check against screen edge */
963 edge = 0;
965 /* check position of nearest window to the left */
966 if (data->rightIndex > 0) {
967 /* there is some window at the left: check if it will block
968 * the window */
969 rwin = data->rightList[data->rightIndex - 1];
971 if (data->realY > WBOTTOM(rwin)
972 || (data->realY + data->winHeight) < WTOP(rwin)) {
973 resist = 0;
974 } else {
975 edge = WRIGHT(rwin) + 1;
976 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
979 if (resist > 0 && winL >= edge - resist && winL <= edge) {
980 newX = edge;
981 hresist = True;
983 } else if (dx > 0) {
984 /* window is the rightmost window: check against screen edge */
985 edge = scr->scr_width;
987 /* check position of nearest window to the right */
988 if (data->leftIndex > 0) {
989 /* there is some window at the right: check if it will block
990 * the window */
991 rwin = data->leftList[data->leftIndex - 1];
993 if (data->realY > WBOTTOM(rwin)
994 || (data->realY + data->winHeight) < WTOP(rwin)) {
995 resist = 0;
996 } else {
997 edge = WLEFT(rwin);
998 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1001 if (resist > 0 && winR <= edge + resist && winR >= edge) {
1002 newX = edge - data->winWidth;
1003 hresist = True;
1007 resist = wPreferences.edge_resistance;
1008 /* vertical movement: check vertical edge resistances */
1009 if (dy < 0) {
1010 /* window is the topmost window: check against screen edge */
1011 edge = 0;
1013 /* check position of nearest window to the top */
1014 if (data->bottomIndex > 0) {
1015 /* there is some window at the top: check if it will block
1016 * the window */
1017 rwin = data->bottomList[data->bottomIndex - 1];
1019 if (data->realX > WRIGHT(rwin)
1020 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1021 resist = 0;
1022 } else {
1023 edge = WBOTTOM(rwin) + 1;
1024 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1027 if (resist > 0 && winT >= edge - resist && winT <= edge) {
1028 newY = edge;
1029 vresist = True;
1031 } else if (dy > 0) {
1032 /* window is the bottommost window: check against screen edge */
1033 edge = scr->scr_height;
1035 /* check position of nearest window to the bottom */
1036 if (data->topIndex > 0) {
1037 /* there is some window at the bottom: check if it will block
1038 * the window */
1039 rwin = data->topList[data->topIndex - 1];
1041 if (data->realX > WRIGHT(rwin)
1042 || (data->realX + data->winWidth) < WLEFT(rwin)) {
1043 resist = 0;
1044 } else {
1045 edge = WTOP(rwin);
1046 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1049 if (resist > 0 && winB <= edge + resist && winB >= edge) {
1050 newY = edge - data->winHeight;
1051 vresist = True;
1056 /* update window position */
1057 data->calcX += dx;
1058 data->calcY += dy;
1060 if (((dx > 0 && data->calcX - data->realX > 0)
1061 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1062 newX = data->calcX;
1064 if (((dy > 0 && data->calcY - data->realY > 0)
1065 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1066 newY = data->calcY;
1068 if (data->realX != newX || data->realY != newY) {
1069 if (opaqueMove) {
1070 doWindowMove(wwin, scr->selected_windows,
1071 newX - wwin->frame_x,
1072 newY - wwin->frame_y);
1073 } else {
1074 /* erase frames */
1075 drawFrames(wwin, scr->selected_windows,
1076 data->realX - wwin->frame_x,
1077 data->realY - wwin->frame_y);
1079 /* draw frames */
1080 drawFrames(wwin, scr->selected_windows,
1081 newX - wwin->frame_x,
1082 newY - wwin->frame_y);
1086 if (!scr->selected_windows) {
1088 if (wPreferences.move_display == WDIS_NEW) {
1090 showPosition(wwin, data->realX, data->realY);
1092 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
1094 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1095 newY + data->winHeight/2);
1097 showPosition(wwin, newX, newY);
1100 /* recalc relative window position */
1101 if (doResistance && (data->realX != newX || data->realY != newY)) {
1102 updateResistance(wwin, data, newX, newY);
1105 data->realX = newX;
1106 data->realY = newY;
1112 #if 0
1113 typedef struct _looper {
1114 WWindow *wwin;
1115 int x,y,w,h,ox,oy;
1116 } _looper;
1118 void
1119 _keyloop(_looper *lpr){
1120 WWindow *wwin = lpr->wwin;
1121 WScreen *scr = wwin->screen_ptr;
1122 int w = wwin->frame->core->width;
1123 int h = wwin->frame->core->height;
1124 int src_x = wwin->frame_x;
1125 int src_y = wwin->frame_y;
1127 if (!scr->selected_windows){
1128 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1130 XUngrabServer(dpy);
1131 XSync(dpy, False);
1132 wusleep(10000);
1133 XGrabServer(dpy);
1134 /* printf("called\n");*/
1135 if (!scr->selected_windows){
1136 drawTransparentFrame(wwin, src_x+lpr->ox, src_y+lpr->oy, w, h);
1138 /* reset timer */
1139 if(scr->keymove_tick)
1140 WMAddTimerHandler(15000,(WMCallback*)_keyloop, lpr);
1143 #endif
1144 #define _KS 20
1147 wKeyboardMoveResizeWindow(WWindow *wwin)
1149 WScreen *scr = wwin->screen_ptr;
1150 Window root = scr->root_win;
1151 XEvent event;
1152 int w = wwin->frame->core->width;
1153 int h = wwin->frame->core->height;
1154 int scr_width = wwin->screen_ptr->scr_width;
1155 int scr_height = wwin->screen_ptr->scr_height;
1156 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1157 int src_x = wwin->frame_x;
1158 int src_y = wwin->frame_y;
1159 int done,off_x,off_y,ww,wh;
1160 int kspeed = 1;
1161 Time lastTime = 0;
1162 KeySym keysym=NoSymbol;
1163 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1166 int timer;
1167 _looper looper;
1168 looper.wwin=wwin;
1169 scr->keymove_tick=1;
1170 WMAddTimerHandler(1000,(WMCallback*)_keyloop, &looper);
1173 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1174 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1175 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1176 ctrlmode=done=off_x=off_y=0;
1178 XSync(dpy, False);
1179 wusleep(10000);
1180 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1182 if (!wwin->flags.selected) {
1183 wUnselectWindows(scr);
1185 XGrabServer(dpy);
1186 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1187 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1188 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1190 if (wwin->flags.shaded || scr->selected_windows) {
1191 if(scr->selected_windows)
1192 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1193 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1194 if(!scr->selected_windows)
1195 mapPositionDisplay(wwin, src_x, src_y, w, h);
1196 } else {
1197 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1199 ww=w;
1200 wh=h;
1201 while(1) {
1203 looper.ox=off_x;
1204 looper.oy=off_y;
1206 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1207 | ButtonPressMask | ExposureMask, &event);
1208 if (wwin->flags.shaded || scr->selected_windows) {
1209 if(scr->selected_windows)
1210 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1211 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1212 /*** I HATE EDGE RESISTANCE - ]d ***/
1214 else {
1215 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1218 if(ctrlmode)
1219 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1221 XUngrabServer(dpy);
1222 XSync(dpy, False);
1224 switch (event.type) {
1225 case KeyPress:
1226 /* accelerate */
1227 if (event.xkey.time - lastTime > 50) {
1228 kspeed = 1;
1229 } else {
1230 if (kspeed < 20)
1231 kspeed++;
1233 lastTime = event.xkey.time;
1235 if (event.xkey.state & ControlMask && !wwin->flags.shaded){
1236 ctrlmode=1;
1237 wUnselectWindows(scr);
1239 else {
1240 ctrlmode=0;
1242 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr){
1243 if(ctrlmode)
1244 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1245 else
1246 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1248 else {
1249 keysym = XLookupKeysym(&event.xkey, 0);
1250 switch(keysym){
1251 case XK_Return:
1252 done=2;
1253 break;
1254 case XK_Escape:
1255 done=1;
1256 break;
1257 case XK_Up:
1258 case XK_KP_Up:
1259 case XK_k:
1260 if (ctrlmode){
1261 h-=kspeed;
1263 else off_y-=kspeed;
1264 break;
1265 case XK_Down:
1266 case XK_KP_Down:
1267 case XK_j:
1268 if (ctrlmode){
1269 h+=kspeed;
1271 else off_y+=kspeed;
1272 break;
1273 case XK_Left:
1274 case XK_KP_Left:
1275 case XK_h:
1276 if (ctrlmode){
1277 w-=kspeed;
1279 else off_x-=kspeed;
1280 break;
1281 case XK_Right:
1282 case XK_KP_Right:
1283 case XK_l:
1284 if (ctrlmode){
1285 w+=kspeed;
1287 else off_x+=kspeed;
1288 break;
1290 ww=w;wh=h;
1291 wh-=vert_border;
1292 wWindowConstrainSize(wwin, &ww, &wh);
1293 wh+=vert_border;
1295 if (wPreferences.ws_cycle){
1296 if (src_x + off_x + wwin->frame->core->width < 20){
1297 if(!scr->current_workspace) {
1298 wWorkspaceChange(scr, scr->workspace_count-1);
1300 else wWorkspaceChange(scr, scr->current_workspace-1);
1301 off_x += scr_width;
1303 else if (src_x + off_x + 20 > scr_width){
1304 if(scr->current_workspace == scr->workspace_count-1) {
1305 wWorkspaceChange(scr, 0);
1307 else wWorkspaceChange(scr, scr->current_workspace+1);
1308 off_x -= scr_width;
1311 else {
1312 if (src_x + off_x + wwin->frame->core->width < 20)
1313 off_x = 20 - wwin->frame->core->width - src_x;
1314 else if (src_x + off_x + 20 > scr_width)
1315 off_x = scr_width - 20 - src_x;
1318 if (src_y + off_y + wwin->frame->core->height < 20)
1319 off_y = 20 - wwin->frame->core->height - src_y;
1320 else if (src_y + off_y + 20 > scr_height)
1321 off_y = scr_height - 20 - src_y;
1324 break;
1325 case ButtonPress:
1326 case ButtonRelease:
1327 done=1;
1328 break;
1329 default:
1330 WMHandleEvent(&event);
1331 break;
1334 XGrabServer(dpy);
1335 /*xxx*/
1337 if (wwin->flags.shaded && !scr->selected_windows){
1338 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1340 else {
1341 if(ctrlmode){
1342 unmapPositionDisplay(wwin);
1343 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1345 else if(!scr->selected_windows){
1346 unmapGeometryDisplay(wwin);
1347 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1351 if (wwin->flags.shaded || scr->selected_windows) {
1352 if(scr->selected_windows)
1353 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1354 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1356 else {
1357 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1361 if(ctrlmode){
1362 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1364 else if(!scr->selected_windows)
1365 showPosition(wwin, src_x+off_x, src_y+off_y);
1366 /**/
1368 if(done){
1369 scr->keymove_tick=0;
1371 WMDeleteTimerWithClientData(&looper);
1373 if (wwin->flags.shaded || scr->selected_windows) {
1374 if(scr->selected_windows)
1375 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1376 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1378 else {
1379 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1382 if(ctrlmode){
1383 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1384 unmapGeometryDisplay(wwin);
1386 else
1387 unmapPositionDisplay(wwin);
1388 XUngrabKeyboard(dpy, CurrentTime);
1389 XUngrabPointer(dpy, CurrentTime);
1390 XUngrabServer(dpy);
1391 if(done==2){
1392 if (wwin->flags.shaded || scr->selected_windows) {
1393 LinkedList *list;
1394 list=scr->selected_windows;
1395 if(!scr->selected_windows){
1396 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1397 wWindowSynthConfigureNotify(wwin);
1399 else {
1400 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1401 while (list) {
1402 wWindowSynthConfigureNotify(list->head);
1403 list = list->tail;
1407 else {
1408 wWindowConfigure(wwin, src_x+off_x, src_y+off_y, ww, wh - vert_border);
1409 wWindowSynthConfigureNotify(wwin);
1411 wWindowChangeWorkspace(wwin, scr->current_workspace);
1412 wSetFocusTo(scr, wwin);
1414 return 1;
1421 *----------------------------------------------------------------------
1422 * wMouseMoveWindow--
1423 * Move the named window and the other selected ones (if any),
1424 * interactively. Also shows the position of the window, if only one
1425 * window is being moved.
1426 * If the window is not on the selected window list, the selected
1427 * windows are deselected.
1428 * If shift is pressed during the operation, the position display
1429 * is changed to another type.
1431 * Returns:
1432 * True if the window was moved, False otherwise.
1434 * Side effects:
1435 * The window(s) position is changed, and the client(s) are
1436 * notified about that.
1437 * The position display configuration may be changed.
1438 *----------------------------------------------------------------------
1441 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1443 WScreen *scr = wwin->screen_ptr;
1444 XEvent event;
1445 Window root = scr->root_win;
1446 KeyCode shiftl, shiftr;
1447 int started = 0;
1448 int warped = 0;
1449 /* This needs not to change while moving, else bad things can happen */
1450 int opaqueMove = wPreferences.opaque_move;
1451 MoveData moveData;
1453 initMoveData(wwin, &moveData);
1455 moveData.mouseX = ev->xmotion.x_root;
1456 moveData.mouseY = ev->xmotion.y_root;
1458 if (!wwin->flags.selected) {
1459 /* this window is not selected, unselect others and move only wwin */
1460 wUnselectWindows(scr);
1462 #ifdef DEBUG
1463 puts("Moving window");
1464 #endif
1465 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1466 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1467 while (1) {
1468 if (warped) {
1469 int junk;
1470 Window junkw;
1472 /* XWarpPointer() doesn't seem to generate Motion events, so
1473 we've got to simulate them */
1474 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1475 &event.xmotion.y_root, &junk, &junk,
1476 (unsigned *) &junk);
1477 } else {
1478 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1479 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1480 &event);
1482 if (event.type == MotionNotify) {
1483 /* compress MotionNotify events */
1484 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1487 switch (event.type) {
1488 case KeyPress:
1489 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1490 && started && !scr->selected_windows) {
1492 if (!opaqueMove) {
1493 drawFrames(wwin, scr->selected_windows,
1494 moveData.realX - wwin->frame_x,
1495 moveData.realY - wwin->frame_y);
1498 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1499 moveData.winWidth, moveData.winHeight);
1501 if (!opaqueMove) {
1502 drawFrames(wwin, scr->selected_windows,
1503 moveData.realX - wwin->frame_x,
1504 moveData.realY - wwin->frame_y);
1507 break;
1509 case MotionNotify:
1510 if (started) {
1511 updateWindowPosition(wwin, &moveData,
1512 scr->selected_windows == NULL
1513 && wPreferences.edge_resistance > 0,
1514 opaqueMove,
1515 event.xmotion.x_root,
1516 event.xmotion.y_root);
1518 if (!warped && !wPreferences.no_autowrap) {
1519 int oldWorkspace = scr->current_workspace;
1521 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1522 if (scr->current_workspace != oldWorkspace
1523 && wPreferences.edge_resistance > 0
1524 && scr->selected_windows == NULL)
1525 updateMoveData(wwin, &moveData);
1526 warped = 1;
1529 } else {
1530 warped = 0;
1532 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1533 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1535 XChangeActivePointerGrab(dpy, ButtonMotionMask
1536 | ButtonReleaseMask | ButtonPressMask,
1537 wCursor[WCUR_MOVE], CurrentTime);
1538 started = 1;
1539 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1540 CurrentTime);
1542 if (!scr->selected_windows)
1543 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1544 moveData.winWidth, moveData.winHeight);
1546 if (started && !opaqueMove)
1547 drawFrames(wwin, scr->selected_windows, 0, 0);
1549 if (!opaqueMove)
1550 XGrabServer(dpy);
1552 break;
1554 case ButtonPress:
1555 break;
1557 case ButtonRelease:
1558 if (event.xbutton.button != ev->xbutton.button)
1559 break;
1561 if (started) {
1562 if (!opaqueMove) {
1563 drawFrames(wwin, scr->selected_windows,
1564 moveData.realX - wwin->frame_x,
1565 moveData.realY - wwin->frame_y);
1566 XSync(dpy, 0);
1567 doWindowMove(wwin, scr->selected_windows,
1568 moveData.realX - wwin->frame_x,
1569 moveData.realY - wwin->frame_y);
1571 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1572 wWindowSynthConfigureNotify(wwin);
1573 #endif
1574 XUngrabKeyboard(dpy, CurrentTime);
1575 XUngrabServer(dpy);
1576 if (!opaqueMove) {
1577 wWindowChangeWorkspace(wwin, scr->current_workspace);
1578 wSetFocusTo(scr, wwin);
1580 if (wPreferences.move_display == WDIS_NEW)
1581 showPosition(wwin, moveData.realX, moveData.realY);
1583 if (!scr->selected_windows) {
1584 /* get rid of the geometry window */
1585 unmapPositionDisplay(wwin);
1588 #ifdef DEBUG
1589 puts("End move window");
1590 #endif
1591 freeMoveData(&moveData);
1593 return started;
1595 default:
1596 if (started && !opaqueMove) {
1597 drawFrames(wwin, scr->selected_windows,
1598 moveData.realX - wwin->frame_x,
1599 moveData.realY - wwin->frame_y);
1600 XUngrabServer(dpy);
1601 WMHandleEvent(&event);
1602 XSync(dpy, False);
1603 XGrabServer(dpy);
1604 drawFrames(wwin, scr->selected_windows,
1605 moveData.realX - wwin->frame_x,
1606 moveData.realY - wwin->frame_y);
1607 } else {
1608 WMHandleEvent(&event);
1610 break;
1614 freeMoveData(&moveData);
1616 return 0;
1620 #define RESIZEBAR 1
1621 #define HCONSTRAIN 2
1623 static int
1624 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1625 int flags)
1627 int w = wwin->frame->core->width - 1;
1628 int cw = wwin->frame->resizebar_corner_width;
1629 int dir;
1631 /* if not resizing through the resizebar */
1632 if (!(flags & RESIZEBAR)) {
1633 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1634 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1635 if (abs(dx) < 2 || abs(dy) < 2) {
1636 if (abs(dy) > abs(dx))
1637 xdir = 0;
1638 else
1639 ydir = 0;
1641 return (xdir | ydir);
1644 /* window is too narrow. allow diagonal resize */
1645 if (cw * 2 >= w) {
1646 int ydir;
1648 if (flags & HCONSTRAIN)
1649 ydir = 0;
1650 else
1651 ydir = DOWN;
1652 if (x < cw)
1653 return (LEFT | ydir);
1654 else
1655 return (RIGHT | ydir);
1657 /* vertical resize */
1658 if ((x > cw) && (x < w - cw))
1659 return DOWN;
1661 if (x < cw)
1662 dir = LEFT;
1663 else
1664 dir = RIGHT;
1666 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1667 dir |= DOWN;
1669 return dir;
1673 void
1674 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1676 XEvent event;
1677 WScreen *scr = wwin->screen_ptr;
1678 Window root = scr->root_win;
1679 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1680 int fw = wwin->frame->core->width;
1681 int fh = wwin->frame->core->height;
1682 int fx = wwin->frame_x;
1683 int fy = wwin->frame_y;
1684 int is_resizebar = (wwin->frame->resizebar
1685 && ev->xany.window==wwin->frame->resizebar->window);
1686 int orig_x, orig_y;
1687 int started;
1688 int dw, dh;
1689 int rw = fw, rh = fh;
1690 int rx1, ry1, rx2, ry2;
1691 int res = 0;
1692 KeyCode shiftl, shiftr;
1693 int h = 0;
1694 int orig_fx = fx;
1695 int orig_fy = fy;
1696 int orig_fw = fw;
1697 int orig_fh = fh;
1699 if (wwin->flags.shaded) {
1700 wwarning("internal error: tryein");
1701 return;
1703 orig_x = ev->xbutton.x_root;
1704 orig_y = ev->xbutton.y_root;
1706 started = 0;
1707 #ifdef DEBUG
1708 puts("Resizing window");
1709 #endif
1711 wUnselectWindows(scr);
1712 rx1 = fx;
1713 rx2 = fx + fw - 1;
1714 ry1 = fy;
1715 ry2 = fy + fh - 1;
1716 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1717 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1718 if (!WFLAGP(wwin, no_titlebar))
1719 h = wwin->screen_ptr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
1720 else
1721 h = 0;
1722 while (1) {
1723 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask | ButtonReleaseMask
1724 | ButtonPressMask | ExposureMask, &event);
1725 switch (event.type) {
1726 case KeyPress:
1727 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1728 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1729 && started) {
1730 drawTransparentFrame(wwin, fx, fy, fw, fh);
1731 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1732 drawTransparentFrame(wwin, fx, fy, fw, fh);
1734 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1735 break;
1737 case MotionNotify:
1738 if (started) {
1739 dw = 0;
1740 dh = 0;
1742 orig_fx = fx;
1743 orig_fy = fy;
1744 orig_fw = fw;
1745 orig_fh = fh;
1747 if (res & LEFT)
1748 dw = orig_x - event.xmotion.x_root;
1749 else if (res & RIGHT)
1750 dw = event.xmotion.x_root - orig_x;
1751 if (res & UP)
1752 dh = orig_y - event.xmotion.y_root;
1753 else if (res & DOWN)
1754 dh = event.xmotion.y_root - orig_y;
1756 orig_x = event.xmotion.x_root;
1757 orig_y = event.xmotion.y_root;
1759 rw += dw;
1760 rh += dh;
1761 fw = rw;
1762 fh = rh - vert_border;
1763 wWindowConstrainSize(wwin, &fw, &fh);
1764 fh += vert_border;
1765 if (res & LEFT)
1766 fx = rx2 - fw + 1;
1767 else if (res & RIGHT)
1768 fx = rx1;
1769 if (res & UP)
1770 fy = ry2 - fh + 1;
1771 else if (res & DOWN)
1772 fy = ry1;
1773 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1774 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1775 int tx, ty;
1776 Window junkw;
1777 int flags;
1779 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1780 orig_x, orig_y, &tx, &ty, &junkw);
1782 /* check if resizing through resizebar */
1783 if (is_resizebar)
1784 flags = RESIZEBAR;
1785 else
1786 flags = 0;
1788 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1789 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1790 flags |= HCONSTRAIN;
1792 res = getResizeDirection(wwin, tx, ty,
1793 orig_x - event.xmotion.x_root,
1794 orig_y - event.xmotion.y_root, flags);
1796 XChangeActivePointerGrab(dpy, ButtonMotionMask
1797 | ButtonReleaseMask | ButtonPressMask,
1798 wCursor[WCUR_RESIZE], CurrentTime);
1799 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1800 CurrentTime);
1802 XGrabServer(dpy);
1804 /* Draw the resize frame for the first time. */
1805 mapGeometryDisplay(wwin, fx, fy, fw, fh);
1807 drawTransparentFrame(wwin, fx, fy, fw, fh);
1809 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1811 started = 1;
1813 if (started) {
1814 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
1815 drawTransparentFrame(wwin, orig_fx, orig_fy,
1816 orig_fw, orig_fh);
1817 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
1818 drawTransparentFrame(wwin, fx, fy, fw, fh);
1819 } else {
1820 drawTransparentFrame(wwin, orig_fx, orig_fy,
1821 orig_fw, orig_fh);
1822 drawTransparentFrame(wwin, fx, fy, fw, fh);
1824 if (fh != orig_fh || fw != orig_fw) {
1825 if (wPreferences.size_display == WDIS_NEW) {
1826 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
1827 orig_fy + orig_fh, res);
1829 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1832 break;
1834 case ButtonPress:
1835 break;
1837 case ButtonRelease:
1838 if (event.xbutton.button != ev->xbutton.button)
1839 break;
1841 if (started) {
1842 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1844 drawTransparentFrame(wwin, fx, fy, fw, fh);
1846 XUngrabKeyboard(dpy, CurrentTime);
1847 unmapGeometryDisplay(wwin);
1848 XUngrabServer(dpy);
1849 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
1851 #ifdef DEBUG
1852 puts("End resize window");
1853 #endif
1854 return;
1856 default:
1857 WMHandleEvent(&event);
1862 #undef LEFT
1863 #undef RIGHT
1864 #undef HORIZONTAL
1865 #undef UP
1866 #undef DOWN
1867 #undef VERTICAL
1868 #undef HCONSTRAIN
1869 #undef RESIZEBAR
1871 void
1872 wUnselectWindows(WScreen *scr)
1874 WWindow *wwin;
1876 while (scr->selected_windows) {
1877 wwin = scr->selected_windows->head;
1878 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
1879 wIconSelect(wwin->icon);
1881 wSelectWindow(wwin, False);
1885 #ifndef LITE
1886 static void
1887 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
1889 WWindow *tmpw;
1891 /* select the windows and put them in the selected window list */
1892 tmpw = scr->focused_window;
1893 while (tmpw != NULL) {
1894 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
1895 if ((tmpw->frame->workspace == scr->current_workspace
1896 || IS_OMNIPRESENT(tmpw))
1897 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
1898 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
1899 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
1900 wSelectWindow(tmpw, True);
1903 tmpw = tmpw->prev;
1908 void
1909 wSelectWindows(WScreen *scr, XEvent *ev)
1911 XEvent event;
1912 Window root = scr->root_win;
1913 GC gc = scr->frame_gc;
1914 int xp = ev->xbutton.x_root;
1915 int yp = ev->xbutton.y_root;
1916 int w = 0, h = 0;
1917 int x = xp, y = yp;
1919 #ifdef DEBUG
1920 puts("Selecting windows");
1921 #endif
1922 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
1923 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1924 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
1925 CurrentTime) != Success) {
1926 return;
1928 XGrabServer(dpy);
1930 wUnselectWindows(scr);
1932 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
1933 while (1) {
1934 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
1935 | ButtonPressMask, &event);
1937 switch (event.type) {
1938 case MotionNotify:
1939 XDrawRectangle(dpy, root, gc, x, y, w, h);
1940 x = event.xmotion.x_root;
1941 if (x < xp) {
1942 w = xp - x;
1943 } else {
1944 w = x - xp;
1945 x = xp;
1947 y = event.xmotion.y_root;
1948 if (y < yp) {
1949 h = yp - y;
1950 } else {
1951 h = y - yp;
1952 y = yp;
1954 XDrawRectangle(dpy, root, gc, x, y, w, h);
1955 break;
1957 case ButtonPress:
1958 break;
1960 case ButtonRelease:
1961 if (event.xbutton.button != ev->xbutton.button)
1962 break;
1964 XDrawRectangle(dpy, root, gc, x, y, w, h);
1965 XUngrabServer(dpy);
1966 XUngrabPointer(dpy, CurrentTime);
1967 selectWindowsInside(scr, x, y, x + w, y + h);
1968 #ifdef DEBUG
1969 puts("End window selection");
1970 #endif
1971 return;
1973 default:
1974 WMHandleEvent(&event);
1975 break;
1979 #endif /* !LITE */
1981 void
1982 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
1983 unsigned width, unsigned height)
1985 WScreen *scr = wwin->screen_ptr;
1986 Window root = scr->root_win;
1987 int x, y, h = 0;
1988 XEvent event;
1989 KeyCode shiftl, shiftr;
1990 Window junkw;
1991 int junk;
1993 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
1994 GrabModeAsync, GrabModeAsync, None,
1995 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
1996 *x_ret = 0;
1997 *y_ret = 0;
1998 return;
2000 if (!WFLAGP(wwin, no_titlebar)) {
2001 h = scr->title_font->height + TITLEBAR_EXTRA_HEIGHT;
2002 height += h;
2004 if (!WFLAGP(wwin, no_resizebar)) {
2005 height += RESIZEBAR_HEIGHT;
2007 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2008 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2009 (unsigned *) &junk);
2010 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2012 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2014 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2015 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2016 while (1) {
2017 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2018 &event);
2019 switch (event.type) {
2020 case KeyPress:
2021 if ((event.xkey.keycode == shiftl)
2022 || (event.xkey.keycode == shiftr)) {
2023 drawTransparentFrame(wwin,
2024 x - width/2, y - h/2, width, height);
2025 cyclePositionDisplay(wwin,
2026 x - width/2, y - h/2, width, height);
2027 drawTransparentFrame(wwin,
2028 x - width/2, y - h/2, width, height);
2030 break;
2032 case MotionNotify:
2033 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2035 x = event.xmotion.x_root;
2036 y = event.xmotion.y_root;
2038 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2039 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2041 showPosition(wwin, x - width/2, y - h/2);
2043 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2045 break;
2047 case ButtonPress:
2048 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2049 XSync(dpy, 0);
2050 *x_ret = x - width/2;
2051 *y_ret = y - h/2;
2052 XUngrabPointer(dpy, CurrentTime);
2053 XUngrabKeyboard(dpy, CurrentTime);
2054 /* get rid of the geometry window */
2055 unmapPositionDisplay(wwin);
2056 return;
2058 default:
2059 WMHandleEvent(&event);
2060 break;