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