- Fixed crashing bug in menu.c
[wmaker-crm.git] / src / moveres.c
blob0054816e1d77c9f8d4e1e453d5136eb4ac5f7b4f
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "wcore.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "actions.h"
41 #include "workspace.h"
43 #include "geomview.h"
44 #include "screen.h"
45 #include "xinerama.h"
47 #include <WINGs/WINGsP.h>
50 #ifdef KWM_HINTS
51 #include "kwm.h"
52 #endif
54 /* How many different types of geometry/position
55 display thingies are there? */
56 #define NUM_DISPLAYS 5
58 #define LEFT 1
59 #define RIGHT 2
60 #define HORIZONTAL (LEFT|RIGHT)
61 #define UP 4
62 #define DOWN 8
63 #define VERTICAL (UP|DOWN)
65 /****** Global Variables ******/
66 extern Time LastTimestamp;
68 extern Cursor wCursor[WCUR_LAST];
70 extern WPreferences wPreferences;
72 extern Atom _XA_WM_PROTOCOLS;
77 *----------------------------------------------------------------------
78 * checkMouseSamplingRate-
79 * For lowering the mouse motion sampling rate for machines where
80 * it's too high (SGIs). If it returns False then the event should be
81 * ignored.
82 *----------------------------------------------------------------------
84 static Bool
85 checkMouseSamplingRate(XEvent *ev)
87 static Time previousMotion = 0;
89 if (ev->type == MotionNotify) {
90 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
91 return False;
92 } else {
93 previousMotion = ev->xmotion.time;
96 return True;
102 *----------------------------------------------------------------------
103 * moveGeometryDisplayCentered
105 * routine that moves the geometry/position window on scr so it is
106 * centered over the given coordinates (x,y). Also the window position
107 * is clamped so it stays on the screen at all times.
108 *----------------------------------------------------------------------
110 static void
111 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
113 unsigned int w = WMWidgetWidth(scr->gview);
114 unsigned int h = WMWidgetHeight(scr->gview);
115 unsigned int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
118 x -= w / 2;
119 y -= h / 2;
122 * dead area check
124 if (scr->xine_count) {
125 WMRect rect;
126 int head, flags;
128 rect.pos.x = x;
129 rect.pos.y = y;
130 rect.size.width = w;
131 rect.size.height = h;
133 head = wGetRectPlacementInfo(scr, rect, &flags);
135 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
136 rect = wGetRectForHead(scr, head);
137 x1 = rect.pos.x;
138 y1 = rect.pos.y;
139 x2 = x1 + rect.size.width;
140 y2 = y1 + rect.size.height;
144 if (x < x1 + 1)
145 x = x1 + 1;
146 else if (x > (x2 - w))
147 x = x2 - w;
149 if (y < y1 + 1)
150 y = y1 + 1;
151 else if (y > (y2 - h))
152 y = y2 - h;
155 WMMoveWidget(scr->gview, x, y);
159 static void
160 showPosition(WWindow *wwin, int x, int y)
162 WScreen *scr = wwin->screen_ptr;
164 if (wPreferences.move_display == WDIS_NEW) {
165 #if 0
166 int width = wwin->frame->core->width;
167 int height = wwin->frame->core->height;
169 GC lgc = scr->line_gc;
170 XSetForeground(dpy, lgc, scr->line_pixel);
171 sprintf(num, "%i", x);
173 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
174 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
175 y+height+2);
176 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
177 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
178 scr->scr_height);
179 #endif
180 } else {
181 #ifdef VIRTUAL_DESKTOP
182 WSetGeometryViewShownPosition(scr->gview,
183 x + scr->workspaces[scr->current_workspace]->view_x,
184 y + scr->workspaces[scr->current_workspace]->view_y);
185 #else
186 WSetGeometryViewShownPosition(scr->gview, x, y);
187 #endif
192 static void
193 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
195 WScreen *scr = wwin->screen_ptr;
197 wPreferences.move_display++;
198 wPreferences.move_display %= NUM_DISPLAYS;
200 if (wPreferences.move_display == WDIS_NEW) {
201 wPreferences.move_display++;
202 wPreferences.move_display %= NUM_DISPLAYS;
205 if (wPreferences.move_display == WDIS_NONE) {
206 WMUnmapWidget(scr->gview);
207 } else {
208 if (wPreferences.move_display == WDIS_CENTER) {
209 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
210 moveGeometryDisplayCentered(scr,
211 rect.pos.x + rect.size.width/2,
212 rect.pos.y + rect.size.width/2);
213 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
214 moveGeometryDisplayCentered(scr, 1, 1);
215 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
216 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
218 WMMapWidget(scr->gview);
223 static void
224 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
226 WScreen *scr = wwin->screen_ptr;
228 if (wPreferences.move_display == WDIS_NEW
229 || wPreferences.move_display == WDIS_NONE) {
230 return;
231 } else if (wPreferences.move_display == WDIS_CENTER) {
232 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
233 moveGeometryDisplayCentered(scr,
234 rect.pos.x + rect.size.width/2,
235 rect.pos.y + rect.size.width/2);
236 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
237 moveGeometryDisplayCentered(scr, 1, 1);
238 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
239 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
241 WMMapWidget(scr->gview);
242 WSetGeometryViewShownPosition(scr->gview, x, y);
246 static void
247 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
249 WScreen *scr = wwin->screen_ptr;
250 Window root = scr->root_win;
251 GC gc = scr->line_gc, saveGC;
252 int ty, by, my, x, y, mx, s;
253 char num[16];
254 XSegment segment[4];
255 int fw, fh;
256 WMColor *color;
257 WMPixel pixel;
259 ty = y1 + wwin->frame->top_width;
260 by = y2 - wwin->frame->bottom_width;
262 if (wPreferences.size_display == WDIS_NEW) {
263 fw = WMWidthOfString(scr->tech_draw_font, "8888", 4);
264 fh = WMFontHeight(scr->tech_draw_font);
266 XSetForeground(dpy, gc, scr->line_pixel);
268 /* vertical geometry */
269 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
270 x = x2;
271 s = -15;
272 } else {
273 x = x1;
274 s = 15;
276 my = (ty + by) / 2;
278 /* top arrow */
279 /* end bar */
280 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
281 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
283 /* arrowhead */
284 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
285 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
287 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
288 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
290 /* line */
291 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
292 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
294 XDrawSegments(dpy, root, gc, segment, 4);
296 /* bottom arrow */
297 /* end bar */
298 segment[0].y1 = by;
299 segment[0].y2 = by;
301 /* arrowhead */
302 segment[1].y1 = by - 1;
303 segment[1].y2 = by - 7;
305 segment[2].y1 = by - 1;
306 segment[2].y2 = by - 7;
308 /* line */
309 segment[3].y1 = my + fh/2 + 2;
310 segment[3].y2 = by - 1;
312 XDrawSegments(dpy, root, gc, segment, 4);
314 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
315 wwin->normal_hints->height_inc);
316 fw = WMWidthOfString(scr->tech_draw_font, num, strlen(num));
318 /* XSetForeground(dpy, gc, WMColorPixel(scr->window_title_color[WS_UNFOCUSED])); */
320 color = WMBlackColor(scr->wmscreen);
321 saveGC = scr->wmscreen->drawStringGC;
322 pixel = color->color.pixel;
324 /* Display the height. */
326 /* // ugly hack */
327 color->color.pixel = scr->line_pixel;
328 scr->wmscreen->drawStringGC = gc;
329 WMDrawString(scr->wmscreen, root, color, scr->tech_draw_font,
330 x - s + 3 - fw/2, my - fh/2 + 1, num, strlen(num));
331 scr->wmscreen->drawStringGC = saveGC;
332 color->color.pixel = pixel;
334 XSetForeground(dpy, gc, scr->line_pixel);
335 /* horizontal geometry */
336 if (y1 < 15) {
337 y = y2;
338 s = -15;
339 } else {
340 y = y1;
341 s = 15;
343 mx = x1 + (x2 - x1)/2;
344 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
345 wwin->normal_hints->width_inc);
346 fw = WMWidthOfString(scr->tech_draw_font, num, strlen(num));
348 /* left arrow */
349 /* end bar */
350 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
351 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
353 /* arrowhead */
354 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
355 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
357 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
358 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
360 /* line */
361 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
362 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
364 XDrawSegments(dpy, root, gc, segment, 4);
366 /* right arrow */
367 /* end bar */
368 segment[0].x1 = x2 + 1;
369 segment[0].x2 = x2 + 1;
371 /* arrowhead */
372 segment[1].x1 = x2 - 6;
373 segment[1].x2 = x2;
375 segment[2].x1 = x2;
376 segment[2].x2 = x2 - 6;
378 /* line */
379 segment[3].x1 = mx + fw/2 + 2;
380 segment[3].x2 = x2;
382 XDrawSegments(dpy, root, gc, segment, 4);
384 /* XSetForeground(dpy, gc, WMColorPixel(scr->window_title_color[WS_UNFOCUSED])); */
386 /* Display the width. */
387 /* // ugly hack */
388 color->color.pixel = scr->line_pixel;
389 scr->wmscreen->drawStringGC = gc;
390 WMDrawString(scr->wmscreen, root, color, scr->tech_draw_font,
391 mx - fw/2 + 1, y - s - fh/2 + 1, num, strlen(num));
392 scr->wmscreen->drawStringGC = saveGC;
393 color->color.pixel = pixel;
394 WMReleaseColor(color);
395 } else {
396 WSetGeometryViewShownSize(scr->gview,
397 (x2 - x1 - wwin->normal_hints->base_width)
398 / wwin->normal_hints->width_inc,
399 (by - ty - wwin->normal_hints->base_height)
400 / wwin->normal_hints->height_inc);
405 static void
406 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
408 WScreen *scr = wwin->screen_ptr;
410 wPreferences.size_display++;
411 wPreferences.size_display %= NUM_DISPLAYS;
413 if (wPreferences.size_display == WDIS_NEW
414 || wPreferences.size_display == WDIS_NONE) {
415 WMUnmapWidget(scr->gview);
416 } else {
417 if (wPreferences.size_display == WDIS_CENTER) {
418 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
419 moveGeometryDisplayCentered(scr,
420 rect.pos.x + rect.size.width/2,
421 rect.pos.y + rect.size.width/2);
422 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
423 moveGeometryDisplayCentered(scr, 1, 1);
424 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
425 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
427 WMMapWidget(scr->gview);
428 showGeometry(wwin, x, y, x + w, y + h, dir);
433 static void
434 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
436 WScreen *scr = wwin->screen_ptr;
438 if (wPreferences.size_display == WDIS_NEW
439 || wPreferences.size_display == WDIS_NONE)
440 return;
442 if (wPreferences.size_display == WDIS_CENTER) {
443 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
444 moveGeometryDisplayCentered(scr,
445 rect.pos.x + rect.size.width/2,
446 rect.pos.y + rect.size.width/2);
447 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
448 moveGeometryDisplayCentered(scr, 1, 1);
449 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
450 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
452 WMMapWidget(scr->gview);
453 showGeometry(wwin, x, y, x + w, y + h, 0);
458 static void
459 doWindowMove(WWindow *wwin, WMArray *array, int dx, int dy)
461 WWindow *tmpw;
462 WScreen *scr = wwin->screen_ptr;
463 int x, y;
465 if (!array || !WMGetArrayItemCount(array)) {
466 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
467 } else {
468 WMArrayIterator iter;
470 WM_ITERATE_ARRAY(array, tmpw, iter) {
471 x = tmpw->frame_x + dx;
472 y = tmpw->frame_y + dy;
474 #if 1 /* XXX: with xinerama patch was #if 0, check this */
475 /* don't let windows become unreachable */
477 if (x + (int)tmpw->frame->core->width < 20)
478 x = 20 - (int)tmpw->frame->core->width;
479 else if (x + 20 > scr->scr_width)
480 x = scr->scr_width - 20;
482 if (y + (int)tmpw->frame->core->height < 20)
483 y = 20 - (int)tmpw->frame->core->height;
484 else if (y + 20 > scr->scr_height)
485 y = scr->scr_height - 20;
486 #else
487 wScreenBringInside(scr, &x, &y,
488 (int)tmpw->frame->core->width,
489 (int)tmpw->frame->core->height);
490 #endif
492 wWindowMove(tmpw, x, y);
498 static void
499 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
501 Window root = wwin->screen_ptr->root_win;
502 GC gc = wwin->screen_ptr->frame_gc;
503 int h = 0;
504 int bottom = 0;
506 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
507 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
509 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
510 /* Can't use wwin-frame->bottom_width because, in some cases
511 (e.g. interactive placement), frame does not point to anything. */
512 bottom = RESIZEBAR_HEIGHT - 1;
514 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
516 if (h > 0) {
517 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
519 if (bottom > 0) {
520 XDrawLine(dpy, root, gc, x + 1,
521 y + height - bottom,
522 x + width + 1,
523 y + height - bottom);
528 static void
529 drawFrames(WWindow *wwin, WMArray *array, int dx, int dy)
531 WWindow *tmpw;
532 int scr_width = wwin->screen_ptr->scr_width;
533 int scr_height = wwin->screen_ptr->scr_height;
534 int x, y;
536 if (!array) {
538 x = wwin->frame_x + dx;
539 y = wwin->frame_y + dy;
541 drawTransparentFrame(wwin, x, y,
542 wwin->frame->core->width,
543 wwin->frame->core->height);
545 } else {
546 WMArrayIterator iter;
548 WM_ITERATE_ARRAY(array, tmpw, iter) {
549 x = tmpw->frame_x + dx;
550 y = tmpw->frame_y + dy;
552 /* don't let windows become unreachable */
553 #if 1 /* XXX: was 0 in XINERAMA patch, check */
554 if (x + (int)tmpw->frame->core->width < 20)
555 x = 20 - (int)tmpw->frame->core->width;
556 else if (x + 20 > scr_width)
557 x = scr_width - 20;
559 if (y + (int)tmpw->frame->core->height < 20)
560 y = 20 - (int)tmpw->frame->core->height;
561 else if (y + 20 > scr_height)
562 y = scr_height - 20;
564 #else
565 wScreenBringInside(wwin->screen_ptr, &x, &y,
566 (int)tmpw->frame->core->width,
567 (int)tmpw->frame->core->height);
568 #endif
570 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
571 tmpw->frame->core->height);
578 static void
579 flushMotion()
581 XEvent ev;
583 XSync(dpy, False);
584 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
588 static void
589 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
590 int new_workspace, int rewind)
592 /* do not let window be unmapped */
593 if (opaque_move) {
594 wwin->flags.changing_workspace = 1;
595 wWindowChangeWorkspace(wwin, new_workspace);
597 /* go to new workspace */
598 wWorkspaceChange(scr, new_workspace);
600 wwin->flags.changing_workspace = 0;
602 if (rewind)
603 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
604 else
605 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
607 flushMotion();
609 if (!opaque_move) {
610 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
611 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
612 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
619 typedef struct {
620 /* arrays of WWindows sorted by the respective border position */
621 WWindow **topList; /* top border */
622 WWindow **leftList; /* left border */
623 WWindow **rightList; /* right border */
624 WWindow **bottomList; /* bottom border */
625 int count;
627 /* index of window in the above lists indicating the relative position
628 * of the window with the others */
629 int topIndex;
630 int leftIndex;
631 int rightIndex;
632 int bottomIndex;
634 int rubCount; /* for workspace switching */
636 int winWidth, winHeight; /* width/height of the window */
637 int realX, realY; /* actual position of the window */
638 int calcX, calcY; /* calculated position of window */
639 int omouseX, omouseY; /* old mouse position */
640 int mouseX, mouseY; /* last known position of the pointer */
641 } MoveData;
643 #define WTOP(w) (w)->frame_y
644 #define WLEFT(w) (w)->frame_x
645 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
646 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
648 static int
649 compareWTop(const void *a, const void *b)
651 WWindow *wwin1 = *(WWindow**)a;
652 WWindow *wwin2 = *(WWindow**)b;
654 if (WTOP(wwin1) > WTOP(wwin2))
655 return -1;
656 else if (WTOP(wwin1) < WTOP(wwin2))
657 return 1;
658 else
659 return 0;
663 static int
664 compareWLeft(const void *a, const void *b)
666 WWindow *wwin1 = *(WWindow**)a;
667 WWindow *wwin2 = *(WWindow**)b;
669 if (WLEFT(wwin1) > WLEFT(wwin2))
670 return -1;
671 else if (WLEFT(wwin1) < WLEFT(wwin2))
672 return 1;
673 else
674 return 0;
678 static int
679 compareWRight(const void *a, const void *b)
681 WWindow *wwin1 = *(WWindow**)a;
682 WWindow *wwin2 = *(WWindow**)b;
684 if (WRIGHT(wwin1) < WRIGHT(wwin2))
685 return -1;
686 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
687 return 1;
688 else
689 return 0;
694 static int
695 compareWBottom(const void *a, const void *b)
697 WWindow *wwin1 = *(WWindow**)a;
698 WWindow *wwin2 = *(WWindow**)b;
700 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
701 return -1;
702 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
703 return 1;
704 else
705 return 0;
709 static void
710 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
712 int i;
713 int newX2 = newX + data->winWidth;
714 int newY2 = newY + data->winHeight;
715 Bool ok = False;
717 if (newX < data->realX) {
718 if (data->rightIndex > 0
719 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
720 ok = True;
721 } else if (data->leftIndex <= data->count-1
722 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
723 ok = True;
725 } else if (newX > data->realX) {
726 if (data->leftIndex > 0
727 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
728 ok = True;
729 } else if (data->rightIndex <= data->count-1
730 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
731 ok = True;
735 if (!ok) {
736 if (newY < data->realY) {
737 if (data->bottomIndex > 0
738 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
739 ok = True;
740 } else if (data->topIndex <= data->count-1
741 && newY2 <= WTOP(data->topList[data->topIndex])) {
742 ok = True;
744 } else if (newY > data->realY) {
745 if (data->topIndex > 0
746 && newY2 > WTOP(data->topList[data->topIndex-1])) {
747 ok = True;
748 } else if (data->bottomIndex <= data->count-1
749 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
750 ok = True;
755 if (!ok)
756 return;
758 /* TODO: optimize this */
759 if (data->realY < WBOTTOM(data->bottomList[0])) {
760 data->bottomIndex = 0;
762 if (data->realX < WRIGHT(data->rightList[0])) {
763 data->rightIndex = 0;
765 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
766 data->leftIndex = 0;
768 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
769 data->topIndex = 0;
771 for (i = 0; i < data->count; i++) {
772 if (data->realY > WBOTTOM(data->bottomList[i])) {
773 data->bottomIndex = i + 1;
775 if (data->realX > WRIGHT(data->rightList[i])) {
776 data->rightIndex = i + 1;
778 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
779 data->leftIndex = i + 1;
781 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
782 data->topIndex = i + 1;
788 static void
789 freeMoveData(MoveData *data)
791 if (data->topList)
792 wfree(data->topList);
793 if (data->leftList)
794 wfree(data->leftList);
795 if (data->rightList)
796 wfree(data->rightList);
797 if (data->bottomList)
798 wfree(data->bottomList);
802 static void
803 updateMoveData(WWindow *wwin, MoveData *data)
805 WScreen *scr = wwin->screen_ptr;
806 WWindow *tmp;
807 int i;
809 data->count = 0;
810 tmp = scr->focused_window;
811 while (tmp) {
812 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
813 && !tmp->flags.miniaturized
814 && !tmp->flags.hidden
815 && !tmp->flags.obscured
816 && !WFLAGP(tmp, sunken)) {
817 data->topList[data->count] = tmp;
818 data->leftList[data->count] = tmp;
819 data->rightList[data->count] = tmp;
820 data->bottomList[data->count] = tmp;
821 data->count++;
823 tmp = tmp->prev;
826 if (data->count == 0) {
827 data->topIndex = 0;
828 data->leftIndex = 0;
829 data->rightIndex = 0;
830 data->bottomIndex = 0;
831 return;
835 * order from closest to the border of the screen to farthest
837 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
838 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
839 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
840 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
842 /* figure the position of the window relative to the others */
844 data->topIndex = -1;
845 data->leftIndex = -1;
846 data->rightIndex = -1;
847 data->bottomIndex = -1;
849 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
850 data->bottomIndex = 0;
852 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
853 data->rightIndex = 0;
855 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
856 data->leftIndex = 0;
858 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
859 data->topIndex = 0;
861 for (i = 0; i < data->count; i++) {
862 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
863 data->bottomIndex = i + 1;
865 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
866 data->rightIndex = i + 1;
868 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
869 data->leftIndex = i + 1;
871 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
872 data->topIndex = i + 1;
878 static void
879 initMoveData(WWindow *wwin, MoveData *data)
881 int i;
882 WWindow *tmp;
884 memset(data, 0, sizeof(MoveData));
886 for (i = 0, tmp = wwin->screen_ptr->focused_window;
887 tmp != NULL;
888 tmp = tmp->prev, i++);
890 if (i > 1) {
891 data->topList = wmalloc(sizeof(WWindow*) * i);
892 data->leftList = wmalloc(sizeof(WWindow*) * i);
893 data->rightList = wmalloc(sizeof(WWindow*) * i);
894 data->bottomList = wmalloc(sizeof(WWindow*) * i);
896 updateMoveData(wwin, data);
899 data->realX = wwin->frame_x;
900 data->realY = wwin->frame_y;
901 data->calcX = wwin->frame_x;
902 data->calcY = wwin->frame_y;
904 data->winWidth = wwin->frame->core->width + 2;
905 data->winHeight = wwin->frame->core->height + 2;
909 static Bool
910 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
912 WScreen *scr = wwin->screen_ptr;
913 Bool changed = False;
915 if (data->mouseX <= 1) {
916 if (scr->current_workspace > 0) {
918 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
919 True);
920 changed = True;
921 data->rubCount = 0;
923 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
925 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
926 True);
927 changed = True;
928 data->rubCount = 0;
930 } else if (data->mouseX >= scr->scr_width - 2) {
932 if (scr->current_workspace == scr->workspace_count - 1) {
934 if (wPreferences.ws_cycle
935 || scr->workspace_count == MAX_WORKSPACES) {
937 crossWorkspace(scr, wwin, opaqueMove, 0, False);
938 changed = True;
939 data->rubCount = 0;
941 /* if user insists on trying to go to next workspace even when
942 * it's already the last, create a new one */
943 else if (data->omouseX == data->mouseX
944 && wPreferences.ws_advance) {
946 /* detect user "rubbing" the window against the edge */
947 if (data->rubCount > 0
948 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
950 data->rubCount = -(data->rubCount + 1);
952 } else if (data->rubCount <= 0
953 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
955 data->rubCount = -data->rubCount + 1;
958 /* create a new workspace */
959 if (abs(data->rubCount) > 2) {
960 /* go to next workspace */
961 wWorkspaceNew(scr);
963 crossWorkspace(scr, wwin, opaqueMove,
964 scr->current_workspace+1, False);
965 changed = True;
966 data->rubCount = 0;
968 } else if (scr->current_workspace < scr->workspace_count) {
970 /* go to next workspace */
971 crossWorkspace(scr, wwin, opaqueMove,
972 scr->current_workspace+1, False);
973 changed = True;
974 data->rubCount = 0;
976 } else {
977 data->rubCount = 0;
980 return changed;
984 static void
985 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
986 Bool opaqueMove, int newMouseX, int newMouseY)
988 WScreen *scr = wwin->screen_ptr;
989 int dx, dy; /* how much mouse moved */
990 int winL, winR, winT, winB; /* requested new window position */
991 int newX, newY; /* actual new window position */
992 Bool hresist, vresist;
993 Bool updateIndex;
994 Bool attract;
996 hresist = False;
997 vresist = False;
999 updateIndex = False;
1001 /* check the direction of the movement */
1002 dx = newMouseX - data->mouseX;
1003 dy = newMouseY - data->mouseY;
1005 data->omouseX = data->mouseX;
1006 data->omouseY = data->mouseY;
1007 data->mouseX = newMouseX;
1008 data->mouseY = newMouseY;
1010 winL = data->calcX + dx;
1011 winR = data->calcX + data->winWidth + dx;
1012 winT = data->calcY + dy;
1013 winB = data->calcY + data->winHeight + dy;
1015 newX = data->realX;
1016 newY = data->realY;
1018 if (doResistance) {
1019 int l_edge, r_edge;
1020 int edge_l, edge_r;
1021 int t_edge, b_edge;
1022 int edge_t, edge_b;
1023 int resist;
1025 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1026 attract = wPreferences.attract;
1027 /* horizontal movement: check horizontal edge resistances */
1028 if (dx || dy) {
1029 int i;
1030 /* window is the leftmost window: check against screen edge */
1033 * Add inter head resistance 1/2 (if needed)
1035 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1037 l_edge = WMAX(scr->totalUsableArea.x1, rect.pos.x);
1038 edge_l = l_edge - resist;
1039 edge_r = WMIN(scr->totalUsableArea.x2, rect.pos.x + rect.size.width);
1040 r_edge = edge_r + resist;
1042 /* 1 */
1043 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
1044 WWindow *looprw;
1046 for (i = data->rightIndex - 1; i >= 0; i--) {
1047 looprw = data->rightList[i];
1048 if (!(data->realY > WBOTTOM(looprw)
1049 || (data->realY + data->winHeight) < WTOP(looprw))) {
1050 if (attract
1051 || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
1052 l_edge = WRIGHT(looprw) + 1;
1053 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1055 break;
1059 if (attract) {
1060 for (i = data->rightIndex; i < data->count; i++) {
1061 looprw = data->rightList[i];
1062 if(!(data->realY > WBOTTOM(looprw)
1063 || (data->realY + data->winHeight) < WTOP(looprw))) {
1064 r_edge = WRIGHT(looprw) + 1;
1065 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1066 break;
1072 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
1073 WWindow *looprw;
1075 for (i = data->leftIndex - 1; i >= 0; i--) {
1076 looprw = data->leftList[i];
1077 if (!(data->realY > WBOTTOM(looprw)
1078 || (data->realY + data->winHeight) < WTOP(looprw))) {
1079 if (attract
1080 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1)) && dx > 0)) {
1081 edge_r = WLEFT(looprw);
1082 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1084 break;
1088 if (attract)
1089 for (i = data->leftIndex; i < data->count; i++) {
1090 looprw = data->leftList[i];
1091 if(!(data->realY > WBOTTOM(looprw)
1092 || (data->realY + data->winHeight) < WTOP(looprw))) {
1093 edge_l = WLEFT(looprw);
1094 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1095 break;
1101 printf("%d %d\n",winL,winR);
1102 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1105 if ((winL - l_edge) < (r_edge - winL)) {
1106 if (resist > 0) {
1107 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1108 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1109 newX = l_edge;
1110 hresist = True;
1113 } else {
1114 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1115 newX = r_edge;
1116 hresist = True;
1120 if ((winR - edge_l) < (edge_r - winR)) {
1121 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1122 newX = edge_l - data->winWidth;
1123 hresist = True;
1125 } else {
1126 if (resist > 0) {
1127 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1128 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1129 newX = edge_r - data->winWidth;
1130 hresist = True;
1135 /* VeRT */
1137 * Add inter head resistance 2/2 (if needed)
1139 t_edge = WMAX(scr->totalUsableArea.y1, rect.pos.y);
1140 edge_t = t_edge - resist;
1141 edge_b = WMIN(scr->totalUsableArea.y2, rect.pos.y + rect.size.height);
1142 b_edge = edge_b + resist;
1144 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1145 WWindow *looprw;
1147 for (i = data->bottomIndex - 1; i >= 0; i--) {
1148 looprw = data->bottomList[i];
1149 if (!(data->realX > WRIGHT(looprw)
1150 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1151 if (attract
1152 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1153 t_edge = WBOTTOM(looprw) + 1;
1154 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1156 break;
1160 if (attract) {
1161 for (i = data->bottomIndex; i < data->count; i++) {
1162 looprw = data->bottomList[i];
1163 if(!(data->realX > WRIGHT(looprw)
1164 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1165 b_edge = WBOTTOM(looprw) + 1;
1166 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1167 break;
1173 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1174 WWindow *looprw;
1176 for (i = data->topIndex - 1; i >= 0; i--) {
1177 looprw = data->topList[i];
1178 if (!(data->realX > WRIGHT(looprw)
1179 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1180 if (attract
1181 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1182 edge_b = WTOP(looprw);
1183 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1185 break;
1189 if (attract)
1190 for (i = data->topIndex; i < data->count; i++) {
1191 looprw = data->topList[i];
1192 if(!(data->realX > WRIGHT(looprw)
1193 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1194 edge_t = WTOP(looprw);
1195 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1196 break;
1201 if ((winT - t_edge) < (b_edge - winT)) {
1202 if (resist > 0) {
1203 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1204 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1205 newY = t_edge;
1206 vresist = True;
1210 else {
1211 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1212 newY = b_edge;
1213 vresist = True;
1217 if ((winB - edge_t) < (edge_b - winB)) {
1218 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1219 newY = edge_t - data->winHeight;
1220 vresist = True;
1223 else {
1224 if (resist > 0) {
1225 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1226 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1227 newY = edge_b - data->winHeight;
1228 vresist = True;
1233 /* END VeRT */
1237 /* update window position */
1238 data->calcX += dx;
1239 data->calcY += dy;
1241 if (((dx > 0 && data->calcX - data->realX > 0)
1242 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1243 newX = data->calcX;
1245 if (((dy > 0 && data->calcY - data->realY > 0)
1246 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1247 newY = data->calcY;
1249 if (data->realX != newX || data->realY != newY) {
1251 if (wPreferences.move_display == WDIS_NEW
1252 && !scr->selected_windows) {
1253 showPosition(wwin, data->realX, data->realY);
1255 if (opaqueMove) {
1256 doWindowMove(wwin, scr->selected_windows,
1257 newX - wwin->frame_x,
1258 newY - wwin->frame_y);
1259 } else {
1260 /* erase frames */
1261 drawFrames(wwin, scr->selected_windows,
1262 data->realX - wwin->frame_x,
1263 data->realY - wwin->frame_y);
1266 if (!scr->selected_windows
1267 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1269 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1270 newY + data->winHeight/2);
1273 if (!opaqueMove) {
1274 /* draw frames */
1275 drawFrames(wwin, scr->selected_windows,
1276 newX - wwin->frame_x,
1277 newY - wwin->frame_y);
1280 if (!scr->selected_windows) {
1281 showPosition(wwin, newX, newY);
1286 /* recalc relative window position */
1287 if (doResistance && (data->realX != newX || data->realY != newY)) {
1288 updateResistance(wwin, data, newX, newY);
1291 data->realX = newX;
1292 data->realY = newY;
1296 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1299 wKeyboardMoveResizeWindow(WWindow *wwin)
1301 WScreen *scr = wwin->screen_ptr;
1302 Window root = scr->root_win;
1303 XEvent event;
1304 int w = wwin->frame->core->width;
1305 int h = wwin->frame->core->height;
1306 int scr_width = wwin->screen_ptr->scr_width;
1307 int scr_height = wwin->screen_ptr->scr_height;
1308 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1309 int src_x = wwin->frame_x;
1310 int src_y = wwin->frame_y;
1311 int done,off_x,off_y,ww,wh;
1312 int kspeed = _KS;
1313 Time lastTime = 0;
1314 KeySym keysym=NoSymbol;
1315 int moment=0;
1316 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1318 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1319 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1320 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1321 ctrlmode=done=off_x=off_y=0;
1323 XSync(dpy, False);
1324 wusleep(10000);
1325 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1327 if (!wwin->flags.selected) {
1328 wUnselectWindows(scr);
1330 XGrabServer(dpy);
1331 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1332 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1333 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1335 if (wwin->flags.shaded || scr->selected_windows) {
1336 if(scr->selected_windows)
1337 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1338 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1339 if(!scr->selected_windows)
1340 mapPositionDisplay(wwin, src_x, src_y, w, h);
1341 } else {
1342 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1344 ww=w;
1345 wh=h;
1346 while(1) {
1348 looper.ox=off_x;
1349 looper.oy=off_y;
1351 do {
1352 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1353 | ButtonPressMask | ExposureMask, &event);
1354 if (event.type == Expose) {
1355 WMHandleEvent(&event);
1357 } while (event.type == Expose);
1360 if (wwin->flags.shaded || scr->selected_windows) {
1361 if(scr->selected_windows)
1362 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1363 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1364 /*** I HATE EDGE RESISTANCE - ]d ***/
1366 else {
1367 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1370 if(ctrlmode)
1371 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1373 XUngrabServer(dpy);
1374 XSync(dpy, False);
1376 switch (event.type) {
1377 case KeyPress:
1378 /* accelerate */
1379 if (event.xkey.time - lastTime > 50) {
1380 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1381 } else {
1382 if (kspeed < 20) {
1383 kspeed++;
1386 if (kspeed < _KS) kspeed = _KS;
1387 lastTime = event.xkey.time;
1389 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1390 ctrlmode=1;
1391 wUnselectWindows(scr);
1393 else {
1394 ctrlmode=0;
1396 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1397 if (ctrlmode)
1398 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1399 else
1400 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1402 else {
1404 keysym = XLookupKeysym(&event.xkey, 0);
1405 switch (keysym) {
1406 case XK_Return:
1407 done=2;
1408 break;
1409 case XK_Escape:
1410 done=1;
1411 break;
1412 case XK_Up:
1413 #ifdef XK_KP_Up
1414 case XK_KP_Up:
1415 #endif
1416 case XK_k:
1417 if (ctrlmode){
1418 if (moment != UP)
1419 h = wh;
1420 h-=kspeed;
1421 moment = UP;
1422 if (h < 1) h = 1;
1424 else off_y-=kspeed;
1425 break;
1426 case XK_Down:
1427 #ifdef XK_KP_Down
1428 case XK_KP_Down:
1429 #endif
1430 case XK_j:
1431 if (ctrlmode){
1432 if (moment != DOWN)
1433 h = wh;
1434 h+=kspeed;
1435 moment = DOWN;
1437 else off_y+=kspeed;
1438 break;
1439 case XK_Left:
1440 #ifdef XK_KP_Left
1441 case XK_KP_Left:
1442 #endif
1443 case XK_h:
1444 if (ctrlmode) {
1445 if (moment != LEFT)
1446 w = ww;
1447 w-=kspeed;
1448 if (w < 1) w = 1;
1449 moment = LEFT;
1451 else off_x-=kspeed;
1452 break;
1453 case XK_Right:
1454 #ifdef XK_KP_Right
1455 case XK_KP_Right:
1456 #endif
1457 case XK_l:
1458 if (ctrlmode) {
1459 if (moment != RIGHT)
1460 w = ww;
1461 w+=kspeed;
1462 moment = RIGHT;
1464 else off_x+=kspeed;
1465 break;
1468 ww=w;wh=h;
1469 wh-=vert_border;
1470 wWindowConstrainSize(wwin, &ww, &wh);
1471 wh+=vert_border;
1473 if (wPreferences.ws_cycle){
1474 if (src_x + off_x + ww < 20){
1475 if(!scr->current_workspace) {
1476 wWorkspaceChange(scr, scr->workspace_count-1);
1478 else wWorkspaceChange(scr, scr->current_workspace-1);
1479 off_x += scr_width;
1481 else if (src_x + off_x + 20 > scr_width){
1482 if(scr->current_workspace == scr->workspace_count-1) {
1483 wWorkspaceChange(scr, 0);
1485 else wWorkspaceChange(scr, scr->current_workspace+1);
1486 off_x -= scr_width;
1489 else {
1490 if (src_x + off_x + ww < 20)
1491 off_x = 20 - ww - src_x;
1492 else if (src_x + off_x + 20 > scr_width)
1493 off_x = scr_width - 20 - src_x;
1496 if (src_y + off_y + wh < 20) {
1497 off_y = 20 - wh - src_y;
1499 else if (src_y + off_y + 20 > scr_height) {
1500 off_y = scr_height - 20 - src_y;
1503 break;
1504 case ButtonPress:
1505 case ButtonRelease:
1506 done=1;
1507 break;
1508 case Expose:
1509 WMHandleEvent(&event);
1510 while (XCheckTypedEvent(dpy, Expose, &event)) {
1511 WMHandleEvent(&event);
1513 break;
1515 default:
1516 WMHandleEvent(&event);
1517 break;
1520 XGrabServer(dpy);
1521 /*xxx*/
1523 if (wwin->flags.shaded && !scr->selected_windows){
1524 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1525 } else {
1526 if (ctrlmode) {
1527 WMUnmapWidget(scr->gview);
1528 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1529 } else if(!scr->selected_windows) {
1530 WMUnmapWidget(scr->gview);
1531 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1535 if (wwin->flags.shaded || scr->selected_windows) {
1536 if (scr->selected_windows)
1537 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1538 else
1539 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1540 } else {
1541 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1545 if (ctrlmode) {
1546 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1547 } else if(!scr->selected_windows)
1548 showPosition(wwin, src_x+off_x, src_y+off_y);
1549 /**/
1551 if (done) {
1552 scr->keymove_tick=0;
1554 WMDeleteTimerWithClientData(&looper);
1556 if (wwin->flags.shaded || scr->selected_windows) {
1557 if(scr->selected_windows)
1558 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1559 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1561 else {
1562 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1565 if (ctrlmode) {
1566 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1567 WMUnmapWidget(scr->gview);
1568 } else
1569 WMUnmapWidget(scr->gview);
1571 XUngrabKeyboard(dpy, CurrentTime);
1572 XUngrabPointer(dpy, CurrentTime);
1573 XUngrabServer(dpy);
1575 if(done==2) {
1576 if (wwin->flags.shaded || scr->selected_windows) {
1577 if (!scr->selected_windows) {
1578 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1579 wWindowSynthConfigureNotify(wwin);
1580 } else {
1581 WMArrayIterator iter;
1582 WWindow *foo;
1584 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1586 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1587 wWindowSynthConfigureNotify(foo);
1590 } else {
1591 if (wwin->client.width != ww)
1592 wwin->flags.user_changed_width = 1;
1594 if (wwin->client.height != wh - vert_border)
1595 wwin->flags.user_changed_height = 1;
1597 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1598 ww, wh - vert_border);
1599 wWindowSynthConfigureNotify(wwin);
1601 wWindowChangeWorkspace(wwin, scr->current_workspace);
1602 wSetFocusTo(scr, wwin);
1604 return 1;
1611 *----------------------------------------------------------------------
1612 * wMouseMoveWindow--
1613 * Move the named window and the other selected ones (if any),
1614 * interactively. Also shows the position of the window, if only one
1615 * window is being moved.
1616 * If the window is not on the selected window list, the selected
1617 * windows are deselected.
1618 * If shift is pressed during the operation, the position display
1619 * is changed to another type.
1621 * Returns:
1622 * True if the window was moved, False otherwise.
1624 * Side effects:
1625 * The window(s) position is changed, and the client(s) are
1626 * notified about that.
1627 * The position display configuration may be changed.
1628 *----------------------------------------------------------------------
1631 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1633 WScreen *scr = wwin->screen_ptr;
1634 XEvent event;
1635 Window root = scr->root_win;
1636 KeyCode shiftl, shiftr;
1637 Bool done = False;
1638 int started = 0;
1639 int warped = 0;
1640 /* This needs not to change while moving, else bad things can happen */
1641 int opaqueMove = wPreferences.opaque_move;
1642 MoveData moveData;
1643 #ifdef GHOST_WINDOW_MOVE
1644 RImage *rimg;
1646 rimg = InitGhostWindowMove(scr);
1647 #endif
1650 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1651 XSetWindowAttributes attr;
1653 attr.save_under = True;
1654 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1655 CWSaveUnder, &attr);
1659 initMoveData(wwin, &moveData);
1661 moveData.mouseX = ev->xmotion.x_root;
1662 moveData.mouseY = ev->xmotion.y_root;
1664 if (!wwin->flags.selected) {
1665 /* this window is not selected, unselect others and move only wwin */
1666 wUnselectWindows(scr);
1668 #ifdef DEBUG
1669 puts("Moving window");
1670 #endif
1671 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1672 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1673 while (!done) {
1674 if (warped) {
1675 int junk;
1676 Window junkw;
1678 /* XWarpPointer() doesn't seem to generate Motion events, so
1679 we've got to simulate them */
1680 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1681 &event.xmotion.y_root, &junk, &junk,
1682 (unsigned *) &junk);
1683 } else {
1684 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1685 | PointerMotionHintMask
1686 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1687 &event);
1689 if (event.type == MotionNotify) {
1690 /* compress MotionNotify events */
1691 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1692 if (!checkMouseSamplingRate(&event))
1693 continue;
1696 switch (event.type) {
1697 case KeyPress:
1698 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1699 && started && !scr->selected_windows) {
1701 if (!opaqueMove) {
1702 drawFrames(wwin, scr->selected_windows,
1703 moveData.realX - wwin->frame_x,
1704 moveData.realY - wwin->frame_y);
1707 if (wPreferences.move_display == WDIS_NEW
1708 && !scr->selected_windows) {
1709 showPosition(wwin, moveData.realX, moveData.realY);
1710 XUngrabServer(dpy);
1712 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1713 moveData.winWidth, moveData.winHeight);
1715 if (wPreferences.move_display == WDIS_NEW
1716 && !scr->selected_windows) {
1717 XGrabServer(dpy);
1718 showPosition(wwin, moveData.realX, moveData.realY);
1721 if (!opaqueMove) {
1722 drawFrames(wwin, scr->selected_windows,
1723 moveData.realX - wwin->frame_x,
1724 moveData.realY - wwin->frame_y);
1726 /*} else {
1727 WMHandleEvent(&event); this causes problems needs fixing */
1729 break;
1731 case MotionNotify:
1732 if (started) {
1733 updateWindowPosition(wwin, &moveData,
1734 scr->selected_windows == NULL
1735 && wPreferences.edge_resistance > 0,
1736 opaqueMove,
1737 event.xmotion.x_root,
1738 event.xmotion.y_root);
1740 if (!warped && !wPreferences.no_autowrap) {
1741 int oldWorkspace = scr->current_workspace;
1743 if (wPreferences.move_display == WDIS_NEW
1744 && !scr->selected_windows) {
1745 showPosition(wwin, moveData.realX, moveData.realY);
1746 XUngrabServer(dpy);
1748 if (!opaqueMove) {
1749 drawFrames(wwin, scr->selected_windows,
1750 moveData.realX - wwin->frame_x,
1751 moveData.realY - wwin->frame_y);
1753 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1754 if (scr->current_workspace != oldWorkspace
1755 && wPreferences.edge_resistance > 0
1756 && scr->selected_windows == NULL)
1757 updateMoveData(wwin, &moveData);
1758 warped = 1;
1760 if (!opaqueMove) {
1761 drawFrames(wwin, scr->selected_windows,
1762 moveData.realX - wwin->frame_x,
1763 moveData.realY - wwin->frame_y);
1765 if (wPreferences.move_display == WDIS_NEW
1766 && !scr->selected_windows) {
1767 XSync(dpy, False);
1768 showPosition(wwin, moveData.realX, moveData.realY);
1769 XGrabServer(dpy);
1771 } else {
1772 warped = 0;
1774 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1775 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1777 XChangeActivePointerGrab(dpy, ButtonMotionMask
1778 | ButtonReleaseMask | ButtonPressMask,
1779 wCursor[WCUR_MOVE], CurrentTime);
1780 started = 1;
1781 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1782 CurrentTime);
1784 if (!scr->selected_windows)
1785 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1786 moveData.winWidth, moveData.winHeight);
1788 if (started && !opaqueMove)
1789 drawFrames(wwin, scr->selected_windows, 0, 0);
1791 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1792 && !scr->selected_windows)) {
1793 XGrabServer(dpy);
1794 if (wPreferences.move_display==WDIS_NEW)
1795 showPosition(wwin, moveData.realX, moveData.realY);
1798 break;
1800 case ButtonPress:
1801 break;
1803 case ButtonRelease:
1804 if (event.xbutton.button != ev->xbutton.button)
1805 break;
1807 if (started) {
1808 XEvent e;
1809 if (!opaqueMove) {
1810 drawFrames(wwin, scr->selected_windows,
1811 moveData.realX - wwin->frame_x,
1812 moveData.realY - wwin->frame_y);
1813 XSync(dpy, 0);
1814 doWindowMove(wwin, scr->selected_windows,
1815 moveData.realX - wwin->frame_x,
1816 moveData.realY - wwin->frame_y);
1818 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1819 wWindowSynthConfigureNotify(wwin);
1820 #endif
1821 XUngrabKeyboard(dpy, CurrentTime);
1822 XUngrabServer(dpy);
1823 if (!opaqueMove) {
1824 wWindowChangeWorkspace(wwin, scr->current_workspace);
1825 wSetFocusTo(scr, wwin);
1827 if (wPreferences.move_display == WDIS_NEW)
1828 showPosition(wwin, moveData.realX, moveData.realY);
1830 /* discard all enter/leave events that happened until
1831 * the time the button was released */
1832 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1833 if (e.xcrossing.time > event.xbutton.time) {
1834 XPutBackEvent(dpy, &e);
1835 break;
1838 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1839 if (e.xcrossing.time > event.xbutton.time) {
1840 XPutBackEvent(dpy, &e);
1841 break;
1845 if (!scr->selected_windows) {
1846 /* get rid of the geometry window */
1847 WMUnmapWidget(scr->gview);
1850 #ifdef DEBUG
1851 puts("End move window");
1852 #endif
1853 done = True;
1854 break;
1856 default:
1857 if (started && !opaqueMove) {
1858 drawFrames(wwin, scr->selected_windows,
1859 moveData.realX - wwin->frame_x,
1860 moveData.realY - wwin->frame_y);
1861 XUngrabServer(dpy);
1862 WMHandleEvent(&event);
1863 XSync(dpy, False);
1864 XGrabServer(dpy);
1865 drawFrames(wwin, scr->selected_windows,
1866 moveData.realX - wwin->frame_x,
1867 moveData.realY - wwin->frame_y);
1868 } else {
1869 WMHandleEvent(&event);
1871 break;
1875 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1876 XSetWindowAttributes attr;
1879 attr.save_under = False;
1880 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1881 CWSaveUnder, &attr);
1885 freeMoveData(&moveData);
1887 return started;
1891 #define RESIZEBAR 1
1892 #define HCONSTRAIN 2
1894 static int
1895 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1896 int flags)
1898 int w = wwin->frame->core->width - 1;
1899 int cw = wwin->frame->resizebar_corner_width;
1900 int dir;
1902 /* if not resizing through the resizebar */
1903 if (!(flags & RESIZEBAR)) {
1904 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1905 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1906 if (abs(dx) < 2 || abs(dy) < 2) {
1907 if (abs(dy) > abs(dx))
1908 xdir = 0;
1909 else
1910 ydir = 0;
1912 return (xdir | ydir);
1915 /* window is too narrow. allow diagonal resize */
1916 if (cw * 2 >= w) {
1917 int ydir;
1919 if (flags & HCONSTRAIN)
1920 ydir = 0;
1921 else
1922 ydir = DOWN;
1923 if (x < cw)
1924 return (LEFT | ydir);
1925 else
1926 return (RIGHT | ydir);
1928 /* vertical resize */
1929 if ((x > cw) && (x < w - cw))
1930 return DOWN;
1932 if (x < cw)
1933 dir = LEFT;
1934 else
1935 dir = RIGHT;
1937 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1938 dir |= DOWN;
1940 return dir;
1944 void
1945 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1947 XEvent event;
1948 WScreen *scr = wwin->screen_ptr;
1949 Window root = scr->root_win;
1950 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1951 int fw = wwin->frame->core->width;
1952 int fh = wwin->frame->core->height;
1953 int fx = wwin->frame_x;
1954 int fy = wwin->frame_y;
1955 int is_resizebar = (wwin->frame->resizebar
1956 && ev->xany.window==wwin->frame->resizebar->window);
1957 int orig_x, orig_y;
1958 int started;
1959 int dw, dh;
1960 int rw = fw, rh = fh;
1961 int rx1, ry1, rx2, ry2;
1962 int res = 0;
1963 KeyCode shiftl, shiftr;
1964 int h = 0;
1965 int orig_fx = fx;
1966 int orig_fy = fy;
1967 int orig_fw = fw;
1968 int orig_fh = fh;
1970 if (wwin->flags.shaded) {
1971 wwarning("internal error: tryein");
1972 return;
1974 orig_x = ev->xbutton.x_root;
1975 orig_y = ev->xbutton.y_root;
1977 started = 0;
1978 #ifdef DEBUG
1979 puts("Resizing window");
1980 #endif
1982 wUnselectWindows(scr);
1983 rx1 = fx;
1984 rx2 = fx + fw - 1;
1985 ry1 = fy;
1986 ry2 = fy + fh - 1;
1987 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1988 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1989 if (!WFLAGP(wwin, no_titlebar))
1990 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
1991 else
1992 h = 0;
1993 while (1) {
1994 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1995 | ButtonReleaseMask | PointerMotionHintMask
1996 | ButtonPressMask | ExposureMask, &event);
1997 if (!checkMouseSamplingRate(&event))
1998 continue;
2000 switch (event.type) {
2001 case KeyPress:
2002 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2003 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
2004 && started) {
2005 drawTransparentFrame(wwin, fx, fy, fw, fh);
2006 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
2007 drawTransparentFrame(wwin, fx, fy, fw, fh);
2009 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2010 break;
2012 case MotionNotify:
2013 if (started) {
2014 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
2016 dw = 0;
2017 dh = 0;
2019 orig_fx = fx;
2020 orig_fy = fy;
2021 orig_fw = fw;
2022 orig_fh = fh;
2024 if (res & LEFT)
2025 dw = orig_x - event.xmotion.x_root;
2026 else if (res & RIGHT)
2027 dw = event.xmotion.x_root - orig_x;
2028 if (res & UP)
2029 dh = orig_y - event.xmotion.y_root;
2030 else if (res & DOWN)
2031 dh = event.xmotion.y_root - orig_y;
2033 orig_x = event.xmotion.x_root;
2034 orig_y = event.xmotion.y_root;
2036 rw += dw;
2037 rh += dh;
2038 fw = rw;
2039 fh = rh - vert_border;
2040 wWindowConstrainSize(wwin, &fw, &fh);
2041 fh += vert_border;
2042 if (res & LEFT)
2043 fx = rx2 - fw + 1;
2044 else if (res & RIGHT)
2045 fx = rx1;
2046 if (res & UP)
2047 fy = ry2 - fh + 1;
2048 else if (res & DOWN)
2049 fy = ry1;
2050 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
2051 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
2052 int tx, ty;
2053 Window junkw;
2054 int flags;
2056 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
2057 orig_x, orig_y, &tx, &ty, &junkw);
2059 /* check if resizing through resizebar */
2060 if (is_resizebar)
2061 flags = RESIZEBAR;
2062 else
2063 flags = 0;
2065 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
2066 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
2067 flags |= HCONSTRAIN;
2069 res = getResizeDirection(wwin, tx, ty,
2070 orig_x - event.xmotion.x_root,
2071 orig_y - event.xmotion.y_root, flags);
2073 if (res == (UP|LEFT))
2074 XChangeActivePointerGrab(dpy, ButtonMotionMask
2075 | ButtonReleaseMask | ButtonPressMask,
2076 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
2077 else if (res == (UP|RIGHT))
2078 XChangeActivePointerGrab(dpy, ButtonMotionMask
2079 | ButtonReleaseMask | ButtonPressMask,
2080 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
2081 else if (res == (DOWN|LEFT))
2082 XChangeActivePointerGrab(dpy, ButtonMotionMask
2083 | ButtonReleaseMask | ButtonPressMask,
2084 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2085 else if (res == (DOWN|RIGHT))
2086 XChangeActivePointerGrab(dpy, ButtonMotionMask
2087 | ButtonReleaseMask | ButtonPressMask,
2088 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2089 else if (res == DOWN || res == UP)
2090 XChangeActivePointerGrab(dpy, ButtonMotionMask
2091 | ButtonReleaseMask | ButtonPressMask,
2092 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2093 else if (res & (DOWN|UP))
2094 XChangeActivePointerGrab(dpy, ButtonMotionMask
2095 | ButtonReleaseMask | ButtonPressMask,
2096 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2097 else if (res & (LEFT|RIGHT))
2098 XChangeActivePointerGrab(dpy, ButtonMotionMask
2099 | ButtonReleaseMask | ButtonPressMask,
2100 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2102 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
2103 CurrentTime);
2105 XGrabServer(dpy);
2107 /* Draw the resize frame for the first time. */
2108 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2110 drawTransparentFrame(wwin, fx, fy, fw, fh);
2112 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2114 started = 1;
2116 if (started) {
2117 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2118 drawTransparentFrame(wwin, orig_fx, orig_fy,
2119 orig_fw, orig_fh);
2120 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2121 drawTransparentFrame(wwin, fx, fy, fw, fh);
2122 } else {
2123 drawTransparentFrame(wwin, orig_fx, orig_fy,
2124 orig_fw, orig_fh);
2125 drawTransparentFrame(wwin, fx, fy, fw, fh);
2127 if (fh != orig_fh || fw != orig_fw) {
2128 if (wPreferences.size_display == WDIS_NEW) {
2129 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2130 orig_fy + orig_fh, res);
2132 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2135 break;
2137 case ButtonPress:
2138 break;
2140 case ButtonRelease:
2141 if (event.xbutton.button != ev->xbutton.button)
2142 break;
2144 if (started) {
2145 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2147 drawTransparentFrame(wwin, fx, fy, fw, fh);
2149 XUngrabKeyboard(dpy, CurrentTime);
2150 WMUnmapWidget(scr->gview);
2151 XUngrabServer(dpy);
2153 if (wwin->client.width != fw)
2154 wwin->flags.user_changed_width = 1;
2156 if (wwin->client.height != fh - vert_border)
2157 wwin->flags.user_changed_height = 1;
2159 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2161 #ifdef DEBUG
2162 puts("End resize window");
2163 #endif
2164 return;
2166 default:
2167 WMHandleEvent(&event);
2172 #undef LEFT
2173 #undef RIGHT
2174 #undef HORIZONTAL
2175 #undef UP
2176 #undef DOWN
2177 #undef VERTICAL
2178 #undef HCONSTRAIN
2179 #undef RESIZEBAR
2181 void
2182 wUnselectWindows(WScreen *scr)
2184 WWindow *wwin;
2186 if (!scr->selected_windows)
2187 return;
2189 while (WMGetArrayItemCount(scr->selected_windows)) {
2190 wwin = WMGetFromArray(scr->selected_windows, 0);
2191 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2192 wIconSelect(wwin->icon);
2194 wSelectWindow(wwin, False);
2196 WMFreeArray(scr->selected_windows);
2197 scr->selected_windows = NULL;
2200 #ifndef LITE
2201 static void
2202 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2204 WWindow *tmpw;
2206 /* select the windows and put them in the selected window list */
2207 tmpw = scr->focused_window;
2208 while (tmpw != NULL) {
2209 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2210 if ((tmpw->frame->workspace == scr->current_workspace
2211 || IS_OMNIPRESENT(tmpw))
2212 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2213 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2214 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2215 wSelectWindow(tmpw, True);
2218 tmpw = tmpw->prev;
2223 void
2224 wSelectWindows(WScreen *scr, XEvent *ev)
2226 XEvent event;
2227 Window root = scr->root_win;
2228 GC gc = scr->frame_gc;
2229 int xp = ev->xbutton.x_root;
2230 int yp = ev->xbutton.y_root;
2231 int w = 0, h = 0;
2232 int x = xp, y = yp;
2234 #ifdef DEBUG
2235 puts("Selecting windows");
2236 #endif
2237 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2238 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2239 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2240 CurrentTime) != Success) {
2241 return;
2243 XGrabServer(dpy);
2245 wUnselectWindows(scr);
2247 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2248 while (1) {
2249 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2250 | ButtonPressMask, &event);
2252 switch (event.type) {
2253 case MotionNotify:
2254 XDrawRectangle(dpy, root, gc, x, y, w, h);
2255 x = event.xmotion.x_root;
2256 if (x < xp) {
2257 w = xp - x;
2258 } else {
2259 w = x - xp;
2260 x = xp;
2262 y = event.xmotion.y_root;
2263 if (y < yp) {
2264 h = yp - y;
2265 } else {
2266 h = y - yp;
2267 y = yp;
2269 XDrawRectangle(dpy, root, gc, x, y, w, h);
2270 break;
2272 case ButtonPress:
2273 break;
2275 case ButtonRelease:
2276 if (event.xbutton.button != ev->xbutton.button)
2277 break;
2279 XDrawRectangle(dpy, root, gc, x, y, w, h);
2280 XUngrabServer(dpy);
2281 XUngrabPointer(dpy, CurrentTime);
2282 selectWindowsInside(scr, x, y, x + w, y + h);
2284 #ifdef KWM_HINTS
2285 wKWMSelectRootRegion(scr, xp, yp, w, h,
2286 event.xbutton.state & ControlMask);
2287 #endif /* KWM_HINTS */
2289 #ifdef DEBUG
2290 puts("End window selection");
2291 #endif
2292 return;
2294 default:
2295 WMHandleEvent(&event);
2296 break;
2300 #endif /* !LITE */
2302 void
2303 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2304 unsigned width, unsigned height)
2306 WScreen *scr = wwin->screen_ptr;
2307 Window root = scr->root_win;
2308 int x, y, h = 0;
2309 XEvent event;
2310 KeyCode shiftl, shiftr;
2311 Window junkw;
2312 int junk;
2314 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2315 GrabModeAsync, GrabModeAsync, None,
2316 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2317 *x_ret = 0;
2318 *y_ret = 0;
2319 return;
2321 if (!WFLAGP(wwin, no_titlebar)) {
2322 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2323 height += h;
2325 if (!WFLAGP(wwin, no_resizebar)) {
2326 height += RESIZEBAR_HEIGHT;
2328 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2329 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2330 (unsigned *) &junk);
2331 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2333 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2335 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2336 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2337 while (1) {
2338 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2339 &event);
2341 if (!checkMouseSamplingRate(&event))
2342 continue;
2344 switch (event.type) {
2345 case KeyPress:
2346 if ((event.xkey.keycode == shiftl)
2347 || (event.xkey.keycode == shiftr)) {
2348 drawTransparentFrame(wwin,
2349 x - width/2, y - h/2, width, height);
2350 cyclePositionDisplay(wwin,
2351 x - width/2, y - h/2, width, height);
2352 drawTransparentFrame(wwin,
2353 x - width/2, y - h/2, width, height);
2355 break;
2357 case MotionNotify:
2358 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2360 x = event.xmotion.x_root;
2361 y = event.xmotion.y_root;
2363 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2364 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2366 showPosition(wwin, x - width/2, y - h/2);
2368 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2370 break;
2372 case ButtonPress:
2373 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2374 XSync(dpy, 0);
2375 *x_ret = x - width/2;
2376 *y_ret = y - h/2;
2377 XUngrabPointer(dpy, CurrentTime);
2378 XUngrabKeyboard(dpy, CurrentTime);
2379 /* get rid of the geometry window */
2380 WMUnmapWidget(scr->gview);
2381 return;
2383 default:
2384 WMHandleEvent(&event);
2385 break;