changed encoding of (c) thing in about panel to utf-8
[wmaker-crm.git] / src / moveres.c
blob5dd0d8aeafebdd020b11702a5defb75d1cf96c0f
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)
66 /* True if window currently has a border. This also includes borderless
67 * windows which are currently selected
69 #define HAS_BORDER(w) ((w)->flags.selected || !WFLAGP((w), no_border))
72 /****** Global Variables ******/
73 extern Time LastTimestamp;
75 extern Cursor wCursor[WCUR_LAST];
77 extern WPreferences wPreferences;
79 extern Atom _XA_WM_PROTOCOLS;
84 *----------------------------------------------------------------------
85 * checkMouseSamplingRate-
86 * For lowering the mouse motion sampling rate for machines where
87 * it's too high (SGIs). If it returns False then the event should be
88 * ignored.
89 *----------------------------------------------------------------------
91 static Bool
92 checkMouseSamplingRate(XEvent *ev)
94 static Time previousMotion = 0;
96 if (ev->type == MotionNotify) {
97 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
98 return False;
99 } else {
100 previousMotion = ev->xmotion.time;
103 return True;
109 *----------------------------------------------------------------------
110 * moveGeometryDisplayCentered
112 * routine that moves the geometry/position window on scr so it is
113 * centered over the given coordinates (x,y). Also the window position
114 * is clamped so it stays on the screen at all times.
115 *----------------------------------------------------------------------
117 static void
118 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
120 unsigned int w = WMWidgetWidth(scr->gview);
121 unsigned int h = WMWidgetHeight(scr->gview);
122 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
124 x -= w / 2;
125 y -= h / 2;
127 /* dead area check */
128 if (scr->xine_info.count) {
129 WMRect rect;
130 int head, flags;
132 rect.pos.x = x;
133 rect.pos.y = y;
134 rect.size.width = w;
135 rect.size.height = h;
137 head = wGetRectPlacementInfo(scr, rect, &flags);
139 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
140 rect = wGetRectForHead(scr, head);
141 x1 = rect.pos.x;
142 y1 = rect.pos.y;
143 x2 = x1 + rect.size.width;
144 y2 = y1 + rect.size.height;
148 if (x < x1 + 1)
149 x = x1 + 1;
150 else if (x > (x2 - w))
151 x = x2 - w;
153 if (y < y1 + 1)
154 y = y1 + 1;
155 else if (y > (y2 - h))
156 y = y2 - h;
159 WMMoveWidget(scr->gview, x, y);
163 static void
164 showPosition(WWindow *wwin, int x, int y)
166 WScreen *scr = wwin->screen_ptr;
168 if (wPreferences.move_display == WDIS_NEW) {
169 #if 0
170 int width = wwin->frame->core->width;
171 int height = wwin->frame->core->height;
173 GC lgc = scr->line_gc;
174 XSetForeground(dpy, lgc, scr->line_pixel);
175 sprintf(num, "%i", x);
177 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
178 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
179 y+height+2);
180 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
181 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
182 scr->scr_height);
183 #endif
184 } else {
185 #ifdef VIRTUAL_DESKTOP
186 WSetGeometryViewShownPosition(scr->gview,
187 x + scr->workspaces[scr->current_workspace]->view_x,
188 y + scr->workspaces[scr->current_workspace]->view_y);
189 #else
190 WSetGeometryViewShownPosition(scr->gview, x, y);
191 #endif
196 static void
197 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
199 WScreen *scr = wwin->screen_ptr;
200 WMRect rect;
202 wPreferences.move_display++;
203 wPreferences.move_display %= NUM_DISPLAYS;
205 if (wPreferences.move_display == WDIS_NEW) {
206 wPreferences.move_display++;
207 wPreferences.move_display %= NUM_DISPLAYS;
210 if (wPreferences.move_display == WDIS_NONE) {
211 WMUnmapWidget(scr->gview);
212 } else {
213 if (wPreferences.move_display == WDIS_CENTER) {
214 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
215 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width/2,
216 rect.pos.y + rect.size.height/2);
217 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
218 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
219 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
220 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
221 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
223 WMMapWidget(scr->gview);
228 static void
229 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
231 WScreen *scr = wwin->screen_ptr;
232 WMRect rect;
234 if (wPreferences.move_display == WDIS_NEW
235 || wPreferences.move_display == WDIS_NONE) {
236 return;
237 } else if (wPreferences.move_display == WDIS_CENTER) {
238 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
239 moveGeometryDisplayCentered(scr,
240 rect.pos.x + rect.size.width/2,
241 rect.pos.y + rect.size.height/2);
242 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
243 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
244 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
245 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
246 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
248 WMMapWidget(scr->gview);
249 WSetGeometryViewShownPosition(scr->gview, x, y);
253 static void
254 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
256 WScreen *scr = wwin->screen_ptr;
257 Window root = scr->root_win;
258 GC gc = scr->line_gc;
259 int ty, by, my, x, y, mx, s;
260 char num[16];
261 XSegment segment[4];
262 int fw, fh;
264 /* This seems necessary for some odd reason (too lazy to write x1-1 and
265 * x2-1 everywhere below in the code). But why only for x? */
266 x1--; x2--;
268 if (HAS_BORDER(wwin)) {
269 x1 += FRAME_BORDER_WIDTH;
270 x2 += FRAME_BORDER_WIDTH;
271 y1 += FRAME_BORDER_WIDTH;
272 y2 += FRAME_BORDER_WIDTH;
275 ty = y1 + wwin->frame->top_width;
276 by = y2 - wwin->frame->bottom_width;
278 if (wPreferences.size_display == WDIS_NEW) {
279 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
280 fh = scr->tech_draw_font->ascent+scr->tech_draw_font->descent;
282 XSetForeground(dpy, gc, scr->line_pixel);
284 /* vertical geometry */
285 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
286 x = x2;
287 s = -15;
288 } else {
289 x = x1;
290 s = 15;
292 my = (ty + by) / 2;
294 /* top arrow & end bar */
295 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
296 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
298 /* arrowhead */
299 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
300 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
302 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
303 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
305 /* line */
306 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
307 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
309 XDrawSegments(dpy, root, gc, segment, 4);
311 /* bottom arrow & end bar */
312 segment[0].y1 = by;
313 segment[0].y2 = by;
315 /* arrowhead */
316 segment[1].y1 = by - 1;
317 segment[1].y2 = by - 7;
319 segment[2].y1 = by - 1;
320 segment[2].y2 = by - 7;
322 /* line */
323 segment[3].y1 = my + fh/2 + 2;
324 segment[3].y2 = by - 1;
326 XDrawSegments(dpy, root, gc, segment, 4);
328 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
329 wwin->normal_hints->height_inc);
330 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
332 /* Display the height. */
333 XSetFont(dpy, gc, scr->tech_draw_font->fid);
334 XDrawString(dpy, root, gc, x - s + 3 - fw/2, my + scr->tech_draw_font->ascent - fh/2 + 1, num, strlen(num));
336 /* horizontal geometry */
337 if (y1 < 15) {
338 y = y2;
339 s = -15;
340 } else {
341 y = y1;
342 s = 15;
344 mx = x1 + (x2 - x1)/2;
345 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
346 wwin->normal_hints->width_inc);
347 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
349 /* left arrow & 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 & end bar */
367 segment[0].x1 = x2 + 1;
368 segment[0].x2 = x2 + 1;
370 /* arrowhead */
371 segment[1].x1 = x2 - 6;
372 segment[1].x2 = x2;
374 segment[2].x1 = x2;
375 segment[2].x2 = x2 - 6;
377 /* line */
378 segment[3].x1 = mx + fw/2 + 2;
379 segment[3].x2 = x2;
381 XDrawSegments(dpy, root, gc, segment, 4);
383 /* Display the width. */
384 XDrawString(dpy, root, gc, mx - fw/2 + 1, y - s + scr->tech_draw_font->ascent - fh/2 + 1, num, strlen(num));
385 } else {
386 WSetGeometryViewShownSize(scr->gview,
387 (x2 - x1 - wwin->normal_hints->base_width)
388 / wwin->normal_hints->width_inc,
389 (by - ty - wwin->normal_hints->base_height)
390 / wwin->normal_hints->height_inc);
395 static void
396 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
398 WScreen *scr = wwin->screen_ptr;
399 WMRect rect;
401 wPreferences.size_display++;
402 wPreferences.size_display %= NUM_DISPLAYS;
404 if (wPreferences.size_display == WDIS_NEW
405 || wPreferences.size_display == WDIS_NONE) {
406 WMUnmapWidget(scr->gview);
407 } else {
408 if (wPreferences.size_display == WDIS_CENTER) {
409 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
410 moveGeometryDisplayCentered(scr,
411 rect.pos.x + rect.size.width/2,
412 rect.pos.y + rect.size.height/2);
413 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
414 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
415 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
416 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
417 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
419 WMMapWidget(scr->gview);
420 showGeometry(wwin, x, y, x + w, y + h, dir);
425 static void
426 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
428 WScreen *scr = wwin->screen_ptr;
429 WMRect rect;
431 if (wPreferences.size_display == WDIS_NEW
432 || wPreferences.size_display == WDIS_NONE)
433 return;
435 if (wPreferences.size_display == WDIS_CENTER) {
436 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
437 moveGeometryDisplayCentered(scr,
438 rect.pos.x + rect.size.width/2,
439 rect.pos.y + rect.size.height/2);
440 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
441 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
442 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
443 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
444 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
446 WMMapWidget(scr->gview);
447 showGeometry(wwin, x, y, x + w, y + h, 0);
452 static void
453 doWindowMove(WWindow *wwin, WMArray *array, int dx, int dy)
455 WWindow *tmpw;
456 WScreen *scr = wwin->screen_ptr;
457 int x, y;
459 if (!array || !WMGetArrayItemCount(array)) {
460 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
461 } else {
462 WMArrayIterator iter;
464 WM_ITERATE_ARRAY(array, tmpw, iter) {
465 x = tmpw->frame_x + dx;
466 y = tmpw->frame_y + dy;
468 #if 1 /* XXX: with xinerama patch was #if 0, check this */
469 /* don't let windows become unreachable */
471 if (x + (int)tmpw->frame->core->width < 20)
472 x = 20 - (int)tmpw->frame->core->width;
473 else if (x + 20 > scr->scr_width)
474 x = scr->scr_width - 20;
476 if (y + (int)tmpw->frame->core->height < 20)
477 y = 20 - (int)tmpw->frame->core->height;
478 else if (y + 20 > scr->scr_height)
479 y = scr->scr_height - 20;
480 #else
481 wScreenBringInside(scr, &x, &y,
482 (int)tmpw->frame->core->width,
483 (int)tmpw->frame->core->height);
484 #endif
486 wWindowMove(tmpw, x, y);
492 static void
493 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
495 Window root = wwin->screen_ptr->root_win;
496 GC gc = wwin->screen_ptr->frame_gc;
497 int h = 0;
498 int bottom = 0;
500 if (HAS_BORDER(wwin)) {
501 x += FRAME_BORDER_WIDTH;
502 y += FRAME_BORDER_WIDTH;
505 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
506 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
508 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
509 /* Can't use wwin-frame->bottom_width because, in some cases
510 (e.g. interactive placement), frame does not point to anything. */
511 bottom = RESIZEBAR_HEIGHT;
513 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
515 if (h > 0) {
516 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
518 if (bottom > 0) {
519 XDrawLine(dpy, root, gc, x, y + height - bottom,
520 x + width, y + height - bottom);
525 static void
526 drawFrames(WWindow *wwin, WMArray *array, int dx, int dy)
528 WWindow *tmpw;
529 int scr_width = wwin->screen_ptr->scr_width;
530 int scr_height = wwin->screen_ptr->scr_height;
531 int x, y;
533 if (!array) {
535 x = wwin->frame_x + dx;
536 y = wwin->frame_y + dy;
538 drawTransparentFrame(wwin, x, y,
539 wwin->frame->core->width,
540 wwin->frame->core->height);
542 } else {
543 WMArrayIterator iter;
545 WM_ITERATE_ARRAY(array, tmpw, iter) {
546 x = tmpw->frame_x + dx;
547 y = tmpw->frame_y + dy;
549 /* don't let windows become unreachable */
550 #if 1 /* XXX: was 0 in XINERAMA patch, check */
551 if (x + (int)tmpw->frame->core->width < 20)
552 x = 20 - (int)tmpw->frame->core->width;
553 else if (x + 20 > scr_width)
554 x = scr_width - 20;
556 if (y + (int)tmpw->frame->core->height < 20)
557 y = 20 - (int)tmpw->frame->core->height;
558 else if (y + 20 > scr_height)
559 y = scr_height - 20;
561 #else
562 wScreenBringInside(wwin->screen_ptr, &x, &y,
563 (int)tmpw->frame->core->width,
564 (int)tmpw->frame->core->height);
565 #endif
567 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
568 tmpw->frame->core->height);
575 static void
576 flushMotion()
578 XEvent ev;
580 XSync(dpy, False);
581 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
585 static void
586 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
587 int new_workspace, int rewind)
589 /* do not let window be unmapped */
590 if (opaque_move) {
591 wwin->flags.changing_workspace = 1;
592 wWindowChangeWorkspace(wwin, new_workspace);
594 /* go to new workspace */
595 wWorkspaceChange(scr, new_workspace);
597 wwin->flags.changing_workspace = 0;
599 if (rewind)
600 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
601 else
602 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
604 flushMotion();
606 if (!opaque_move) {
607 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
608 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
609 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
616 typedef struct {
617 /* arrays of WWindows sorted by the respective border position */
618 WWindow **topList; /* top border */
619 WWindow **leftList; /* left border */
620 WWindow **rightList; /* right border */
621 WWindow **bottomList; /* bottom border */
622 int count;
624 /* index of window in the above lists indicating the relative position
625 * of the window with the others */
626 int topIndex;
627 int leftIndex;
628 int rightIndex;
629 int bottomIndex;
631 int rubCount; /* for workspace switching */
633 int winWidth, winHeight; /* width/height of the window */
634 int realX, realY; /* actual position of the window */
635 int calcX, calcY; /* calculated position of window */
636 int omouseX, omouseY; /* old mouse position */
637 int mouseX, mouseY; /* last known position of the pointer */
638 } MoveData;
640 #define WTOP(w) (w)->frame_y
641 #define WLEFT(w) (w)->frame_x
642 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
643 (HAS_BORDER(w) ? 2*FRAME_BORDER_WIDTH : 0))
644 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
645 (HAS_BORDER(w) ? 2*FRAME_BORDER_WIDTH : 0))
647 static int
648 compareWTop(const void *a, const void *b)
650 WWindow *wwin1 = *(WWindow**)a;
651 WWindow *wwin2 = *(WWindow**)b;
653 if (WTOP(wwin1) > WTOP(wwin2))
654 return -1;
655 else if (WTOP(wwin1) < WTOP(wwin2))
656 return 1;
657 else
658 return 0;
662 static int
663 compareWLeft(const void *a, const void *b)
665 WWindow *wwin1 = *(WWindow**)a;
666 WWindow *wwin2 = *(WWindow**)b;
668 if (WLEFT(wwin1) > WLEFT(wwin2))
669 return -1;
670 else if (WLEFT(wwin1) < WLEFT(wwin2))
671 return 1;
672 else
673 return 0;
677 static int
678 compareWRight(const void *a, const void *b)
680 WWindow *wwin1 = *(WWindow**)a;
681 WWindow *wwin2 = *(WWindow**)b;
683 if (WRIGHT(wwin1) < WRIGHT(wwin2))
684 return -1;
685 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
686 return 1;
687 else
688 return 0;
693 static int
694 compareWBottom(const void *a, const void *b)
696 WWindow *wwin1 = *(WWindow**)a;
697 WWindow *wwin2 = *(WWindow**)b;
699 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
700 return -1;
701 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
702 return 1;
703 else
704 return 0;
708 static void
709 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
711 int i;
712 int newX2 = newX + data->winWidth;
713 int newY2 = newY + data->winHeight;
714 Bool ok = False;
716 if (newX < data->realX) {
717 if (data->rightIndex > 0
718 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
719 ok = True;
720 } else if (data->leftIndex <= data->count-1
721 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
722 ok = True;
724 } else if (newX > data->realX) {
725 if (data->leftIndex > 0
726 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
727 ok = True;
728 } else if (data->rightIndex <= data->count-1
729 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
730 ok = True;
734 if (!ok) {
735 if (newY < data->realY) {
736 if (data->bottomIndex > 0
737 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
738 ok = True;
739 } else if (data->topIndex <= data->count-1
740 && newY2 <= WTOP(data->topList[data->topIndex])) {
741 ok = True;
743 } else if (newY > data->realY) {
744 if (data->topIndex > 0
745 && newY2 > WTOP(data->topList[data->topIndex-1])) {
746 ok = True;
747 } else if (data->bottomIndex <= data->count-1
748 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
749 ok = True;
754 if (!ok)
755 return;
757 /* TODO: optimize this */
758 if (data->realY < WBOTTOM(data->bottomList[0])) {
759 data->bottomIndex = 0;
761 if (data->realX < WRIGHT(data->rightList[0])) {
762 data->rightIndex = 0;
764 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
765 data->leftIndex = 0;
767 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
768 data->topIndex = 0;
770 for (i = 0; i < data->count; i++) {
771 if (data->realY > WBOTTOM(data->bottomList[i])) {
772 data->bottomIndex = i + 1;
774 if (data->realX > WRIGHT(data->rightList[i])) {
775 data->rightIndex = i + 1;
777 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
778 data->leftIndex = i + 1;
780 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
781 data->topIndex = i + 1;
787 static void
788 freeMoveData(MoveData *data)
790 if (data->topList)
791 wfree(data->topList);
792 if (data->leftList)
793 wfree(data->leftList);
794 if (data->rightList)
795 wfree(data->rightList);
796 if (data->bottomList)
797 wfree(data->bottomList);
801 static void
802 updateMoveData(WWindow *wwin, MoveData *data)
804 WScreen *scr = wwin->screen_ptr;
805 WWindow *tmp;
806 int i;
808 data->count = 0;
809 tmp = scr->focused_window;
810 while (tmp) {
811 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
812 && !tmp->flags.miniaturized
813 && !tmp->flags.hidden
814 && !tmp->flags.obscured
815 && !WFLAGP(tmp, sunken)) {
816 data->topList[data->count] = tmp;
817 data->leftList[data->count] = tmp;
818 data->rightList[data->count] = tmp;
819 data->bottomList[data->count] = tmp;
820 data->count++;
822 tmp = tmp->prev;
825 if (data->count == 0) {
826 data->topIndex = 0;
827 data->leftIndex = 0;
828 data->rightIndex = 0;
829 data->bottomIndex = 0;
830 return;
833 /* order from closest to the border of the screen to farthest */
835 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
836 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
837 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
838 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
840 /* figure the position of the window relative to the others */
842 data->topIndex = -1;
843 data->leftIndex = -1;
844 data->rightIndex = -1;
845 data->bottomIndex = -1;
847 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
848 data->bottomIndex = 0;
850 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
851 data->rightIndex = 0;
853 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
854 data->leftIndex = 0;
856 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
857 data->topIndex = 0;
859 for (i = 0; i < data->count; i++) {
860 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
861 data->bottomIndex = i + 1;
863 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
864 data->rightIndex = i + 1;
866 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
867 data->leftIndex = i + 1;
869 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
870 data->topIndex = i + 1;
876 static void
877 initMoveData(WWindow *wwin, MoveData *data)
879 int i;
880 WWindow *tmp;
882 memset(data, 0, sizeof(MoveData));
884 for (i = 0, tmp = wwin->screen_ptr->focused_window;
885 tmp != NULL;
886 tmp = tmp->prev, i++);
888 if (i > 1) {
889 data->topList = wmalloc(sizeof(WWindow*) * i);
890 data->leftList = wmalloc(sizeof(WWindow*) * i);
891 data->rightList = wmalloc(sizeof(WWindow*) * i);
892 data->bottomList = wmalloc(sizeof(WWindow*) * i);
894 updateMoveData(wwin, data);
897 data->realX = wwin->frame_x;
898 data->realY = wwin->frame_y;
899 data->calcX = wwin->frame_x;
900 data->calcY = wwin->frame_y;
902 data->winWidth = wwin->frame->core->width +
903 (HAS_BORDER(wwin) ? 2*FRAME_BORDER_WIDTH : 0);
904 data->winHeight = wwin->frame->core->height +
905 (HAS_BORDER(wwin) ? 2*FRAME_BORDER_WIDTH : 0);
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 WMRect rect;
1030 int i, head;
1031 /* window is the leftmost window: check against screen edge */
1033 /* Add inter head resistance 1/2 (if needed) */
1034 head = wGetHeadForPointerLocation(scr);
1035 rect = wGetRectForHead(scr, head);
1037 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
1038 edge_l = l_edge - resist;
1039 edge_r = WMIN(scr->totalUsableArea[head].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 */
1136 /* Add inter head resistance 2/2 (if needed) */
1137 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1138 edge_t = t_edge - resist;
1139 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1140 b_edge = edge_b + resist;
1142 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1143 WWindow *looprw;
1145 for (i = data->bottomIndex - 1; i >= 0; i--) {
1146 looprw = data->bottomList[i];
1147 if (!(data->realX > WRIGHT(looprw)
1148 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1149 if (attract
1150 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1151 t_edge = WBOTTOM(looprw) + 1;
1152 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1154 break;
1158 if (attract) {
1159 for (i = data->bottomIndex; i < data->count; i++) {
1160 looprw = data->bottomList[i];
1161 if(!(data->realX > WRIGHT(looprw)
1162 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1163 b_edge = WBOTTOM(looprw) + 1;
1164 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1165 break;
1171 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1172 WWindow *looprw;
1174 for (i = data->topIndex - 1; i >= 0; i--) {
1175 looprw = data->topList[i];
1176 if (!(data->realX > WRIGHT(looprw)
1177 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1178 if (attract
1179 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1180 edge_b = WTOP(looprw);
1181 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1183 break;
1187 if (attract)
1188 for (i = data->topIndex; i < data->count; i++) {
1189 looprw = data->topList[i];
1190 if(!(data->realX > WRIGHT(looprw)
1191 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1192 edge_t = WTOP(looprw);
1193 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1194 break;
1199 if ((winT - t_edge) < (b_edge - winT)) {
1200 if (resist > 0) {
1201 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1202 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1203 newY = t_edge;
1204 vresist = True;
1208 else {
1209 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1210 newY = b_edge;
1211 vresist = True;
1215 if ((winB - edge_t) < (edge_b - winB)) {
1216 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1217 newY = edge_t - data->winHeight;
1218 vresist = True;
1221 else {
1222 if (resist > 0) {
1223 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1224 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1225 newY = edge_b - data->winHeight;
1226 vresist = True;
1231 /* END VeRT */
1235 /* update window position */
1236 data->calcX += dx;
1237 data->calcY += dy;
1239 if (((dx > 0 && data->calcX - data->realX > 0)
1240 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1241 newX = data->calcX;
1243 if (((dy > 0 && data->calcY - data->realY > 0)
1244 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1245 newY = data->calcY;
1247 if (data->realX != newX || data->realY != newY) {
1249 if (wPreferences.move_display == WDIS_NEW
1250 && !scr->selected_windows) {
1251 showPosition(wwin, data->realX, data->realY);
1253 if (opaqueMove) {
1254 doWindowMove(wwin, scr->selected_windows,
1255 newX - wwin->frame_x,
1256 newY - wwin->frame_y);
1257 } else {
1258 /* erase frames */
1259 drawFrames(wwin, scr->selected_windows,
1260 data->realX - wwin->frame_x,
1261 data->realY - wwin->frame_y);
1264 if (!scr->selected_windows
1265 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1267 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1268 newY + data->winHeight/2);
1271 if (!opaqueMove) {
1272 /* draw frames */
1273 drawFrames(wwin, scr->selected_windows,
1274 newX - wwin->frame_x,
1275 newY - wwin->frame_y);
1278 if (!scr->selected_windows) {
1279 showPosition(wwin, newX, newY);
1284 /* recalc relative window position */
1285 if (doResistance && (data->realX != newX || data->realY != newY)) {
1286 updateResistance(wwin, data, newX, newY);
1289 data->realX = newX;
1290 data->realY = newY;
1294 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1297 wKeyboardMoveResizeWindow(WWindow *wwin)
1299 WScreen *scr = wwin->screen_ptr;
1300 Window root = scr->root_win;
1301 XEvent event;
1302 int w = wwin->frame->core->width;
1303 int h = wwin->frame->core->height;
1304 int scr_width = wwin->screen_ptr->scr_width;
1305 int scr_height = wwin->screen_ptr->scr_height;
1306 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1307 int src_x = wwin->frame_x;
1308 int src_y = wwin->frame_y;
1309 int done,off_x,off_y,ww,wh;
1310 int kspeed = _KS;
1311 Time lastTime = 0;
1312 KeySym keysym=NoSymbol;
1313 int moment=0;
1314 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1316 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1317 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1318 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1319 ctrlmode=done=off_x=off_y=0;
1321 XSync(dpy, False);
1322 wusleep(10000);
1323 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1325 if (!wwin->flags.selected) {
1326 wUnselectWindows(scr);
1328 XGrabServer(dpy);
1329 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1330 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1331 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1333 if (wwin->flags.shaded || scr->selected_windows) {
1334 if(scr->selected_windows)
1335 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1336 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1337 if(!scr->selected_windows)
1338 mapPositionDisplay(wwin, src_x, src_y, w, h);
1339 } else {
1340 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1342 ww=w;
1343 wh=h;
1344 while(1) {
1346 looper.ox=off_x;
1347 looper.oy=off_y;
1349 do {
1350 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1351 | ButtonPressMask | ExposureMask, &event);
1352 if (event.type == Expose) {
1353 WMHandleEvent(&event);
1355 } while (event.type == Expose);
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);
1362 /*** I HATE EDGE RESISTANCE - ]d ***/
1364 else {
1365 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 XUngrabServer(dpy);
1372 XSync(dpy, False);
1374 switch (event.type) {
1375 case KeyPress:
1376 /* accelerate */
1377 if (event.xkey.time - lastTime > 50) {
1378 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1379 } else {
1380 if (kspeed < 20) {
1381 kspeed++;
1384 if (kspeed < _KS) kspeed = _KS;
1385 lastTime = event.xkey.time;
1387 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1388 ctrlmode=1;
1389 wUnselectWindows(scr);
1391 else {
1392 ctrlmode=0;
1394 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1395 if (ctrlmode)
1396 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1397 else
1398 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1400 else {
1402 keysym = XLookupKeysym(&event.xkey, 0);
1403 switch (keysym) {
1404 case XK_Return:
1405 done=2;
1406 break;
1407 case XK_Escape:
1408 done=1;
1409 break;
1410 case XK_Up:
1411 #ifdef XK_KP_Up
1412 case XK_KP_Up:
1413 #endif
1414 case XK_k:
1415 if (ctrlmode){
1416 if (moment != UP)
1417 h = wh;
1418 h-=kspeed;
1419 moment = UP;
1420 if (h < 1) h = 1;
1422 else off_y-=kspeed;
1423 break;
1424 case XK_Down:
1425 #ifdef XK_KP_Down
1426 case XK_KP_Down:
1427 #endif
1428 case XK_j:
1429 if (ctrlmode){
1430 if (moment != DOWN)
1431 h = wh;
1432 h+=kspeed;
1433 moment = DOWN;
1435 else off_y+=kspeed;
1436 break;
1437 case XK_Left:
1438 #ifdef XK_KP_Left
1439 case XK_KP_Left:
1440 #endif
1441 case XK_h:
1442 if (ctrlmode) {
1443 if (moment != LEFT)
1444 w = ww;
1445 w-=kspeed;
1446 if (w < 1) w = 1;
1447 moment = LEFT;
1449 else off_x-=kspeed;
1450 break;
1451 case XK_Right:
1452 #ifdef XK_KP_Right
1453 case XK_KP_Right:
1454 #endif
1455 case XK_l:
1456 if (ctrlmode) {
1457 if (moment != RIGHT)
1458 w = ww;
1459 w+=kspeed;
1460 moment = RIGHT;
1462 else off_x+=kspeed;
1463 break;
1466 ww=w;wh=h;
1467 wh-=vert_border;
1468 wWindowConstrainSize(wwin, &ww, &wh);
1469 wh+=vert_border;
1471 if (wPreferences.ws_cycle){
1472 if (src_x + off_x + ww < 20){
1473 if(!scr->current_workspace) {
1474 wWorkspaceChange(scr, scr->workspace_count-1);
1476 else wWorkspaceChange(scr, scr->current_workspace-1);
1477 off_x += scr_width;
1479 else if (src_x + off_x + 20 > scr_width){
1480 if(scr->current_workspace == scr->workspace_count-1) {
1481 wWorkspaceChange(scr, 0);
1483 else wWorkspaceChange(scr, scr->current_workspace+1);
1484 off_x -= scr_width;
1487 else {
1488 if (src_x + off_x + ww < 20)
1489 off_x = 20 - ww - src_x;
1490 else if (src_x + off_x + 20 > scr_width)
1491 off_x = scr_width - 20 - src_x;
1494 if (src_y + off_y + wh < 20) {
1495 off_y = 20 - wh - src_y;
1497 else if (src_y + off_y + 20 > scr_height) {
1498 off_y = scr_height - 20 - src_y;
1501 break;
1502 case ButtonPress:
1503 case ButtonRelease:
1504 done=1;
1505 break;
1506 case Expose:
1507 WMHandleEvent(&event);
1508 while (XCheckTypedEvent(dpy, Expose, &event)) {
1509 WMHandleEvent(&event);
1511 break;
1513 default:
1514 WMHandleEvent(&event);
1515 break;
1518 XGrabServer(dpy);
1519 /*xxx*/
1521 if (wwin->flags.shaded && !scr->selected_windows){
1522 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1523 } else {
1524 if (ctrlmode) {
1525 WMUnmapWidget(scr->gview);
1526 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1527 } else if(!scr->selected_windows) {
1528 WMUnmapWidget(scr->gview);
1529 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1533 if (wwin->flags.shaded || scr->selected_windows) {
1534 if (scr->selected_windows)
1535 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1536 else
1537 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1538 } else {
1539 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1543 if (ctrlmode) {
1544 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1545 } else if(!scr->selected_windows)
1546 showPosition(wwin, src_x+off_x, src_y+off_y);
1547 /**/
1549 if (done) {
1550 scr->keymove_tick=0;
1552 WMDeleteTimerWithClientData(&looper);
1554 if (wwin->flags.shaded || scr->selected_windows) {
1555 if(scr->selected_windows)
1556 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1557 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1559 else {
1560 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1563 if (ctrlmode) {
1564 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1565 WMUnmapWidget(scr->gview);
1566 } else
1567 WMUnmapWidget(scr->gview);
1569 XUngrabKeyboard(dpy, CurrentTime);
1570 XUngrabPointer(dpy, CurrentTime);
1571 XUngrabServer(dpy);
1573 if(done==2) {
1574 if (wwin->flags.shaded || scr->selected_windows) {
1575 if (!scr->selected_windows) {
1576 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1577 wWindowSynthConfigureNotify(wwin);
1578 } else {
1579 WMArrayIterator iter;
1580 WWindow *foo;
1582 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1584 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1585 wWindowSynthConfigureNotify(foo);
1588 } else {
1589 if (wwin->client.width != ww)
1590 wwin->flags.user_changed_width = 1;
1592 if (wwin->client.height != wh - vert_border)
1593 wwin->flags.user_changed_height = 1;
1595 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1596 ww, wh - vert_border);
1597 wWindowSynthConfigureNotify(wwin);
1599 wWindowChangeWorkspace(wwin, scr->current_workspace);
1600 wSetFocusTo(scr, wwin);
1602 return 1;
1609 *----------------------------------------------------------------------
1610 * wMouseMoveWindow--
1611 * Move the named window and the other selected ones (if any),
1612 * interactively. Also shows the position of the window, if only one
1613 * window is being moved.
1614 * If the window is not on the selected window list, the selected
1615 * windows are deselected.
1616 * If shift is pressed during the operation, the position display
1617 * is changed to another type.
1619 * Returns:
1620 * True if the window was moved, False otherwise.
1622 * Side effects:
1623 * The window(s) position is changed, and the client(s) are
1624 * notified about that.
1625 * The position display configuration may be changed.
1626 *----------------------------------------------------------------------
1629 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1631 WScreen *scr = wwin->screen_ptr;
1632 XEvent event;
1633 Window root = scr->root_win;
1634 KeyCode shiftl, shiftr;
1635 Bool done = False;
1636 int started = 0;
1637 int warped = 0;
1638 /* This needs not to change while moving, else bad things can happen */
1639 int opaqueMove = wPreferences.opaque_move;
1640 MoveData moveData;
1641 #ifdef GHOST_WINDOW_MOVE
1642 RImage *rimg;
1644 rimg = InitGhostWindowMove(scr);
1645 #endif
1648 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1649 XSetWindowAttributes attr;
1651 attr.save_under = True;
1652 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1653 CWSaveUnder, &attr);
1657 initMoveData(wwin, &moveData);
1659 moveData.mouseX = ev->xmotion.x_root;
1660 moveData.mouseY = ev->xmotion.y_root;
1662 if (!wwin->flags.selected) {
1663 /* this window is not selected, unselect others and move only wwin */
1664 wUnselectWindows(scr);
1666 #ifdef DEBUG
1667 puts("Moving window");
1668 #endif
1669 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1670 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1671 while (!done) {
1672 if (warped) {
1673 int junk;
1674 Window junkw;
1676 /* XWarpPointer() doesn't seem to generate Motion events, so
1677 * we've got to simulate them */
1678 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1679 &event.xmotion.y_root, &junk, &junk,
1680 (unsigned *) &junk);
1681 } else {
1682 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1683 | PointerMotionHintMask
1684 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1685 &event);
1687 if (event.type == MotionNotify) {
1688 /* compress MotionNotify events */
1689 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1690 if (!checkMouseSamplingRate(&event))
1691 continue;
1694 switch (event.type) {
1695 case KeyPress:
1696 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1697 && started && !scr->selected_windows) {
1699 if (!opaqueMove) {
1700 drawFrames(wwin, scr->selected_windows,
1701 moveData.realX - wwin->frame_x,
1702 moveData.realY - wwin->frame_y);
1705 if (wPreferences.move_display == WDIS_NEW
1706 && !scr->selected_windows) {
1707 showPosition(wwin, moveData.realX, moveData.realY);
1708 XUngrabServer(dpy);
1710 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1711 moveData.winWidth, moveData.winHeight);
1713 if (wPreferences.move_display == WDIS_NEW
1714 && !scr->selected_windows) {
1715 XGrabServer(dpy);
1716 showPosition(wwin, moveData.realX, moveData.realY);
1719 if (!opaqueMove) {
1720 drawFrames(wwin, scr->selected_windows,
1721 moveData.realX - wwin->frame_x,
1722 moveData.realY - wwin->frame_y);
1724 /*} else {
1725 WMHandleEvent(&event); this causes problems needs fixing */
1727 break;
1729 case MotionNotify:
1730 if (started) {
1731 updateWindowPosition(wwin, &moveData,
1732 scr->selected_windows == NULL
1733 && wPreferences.edge_resistance > 0,
1734 opaqueMove,
1735 event.xmotion.x_root,
1736 event.xmotion.y_root);
1738 if (!warped && !wPreferences.no_autowrap) {
1739 int oldWorkspace = scr->current_workspace;
1741 if (wPreferences.move_display == WDIS_NEW
1742 && !scr->selected_windows) {
1743 showPosition(wwin, moveData.realX, moveData.realY);
1744 XUngrabServer(dpy);
1746 if (!opaqueMove) {
1747 drawFrames(wwin, scr->selected_windows,
1748 moveData.realX - wwin->frame_x,
1749 moveData.realY - wwin->frame_y);
1751 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1752 if (scr->current_workspace != oldWorkspace
1753 && wPreferences.edge_resistance > 0
1754 && scr->selected_windows == NULL)
1755 updateMoveData(wwin, &moveData);
1756 warped = 1;
1758 if (!opaqueMove) {
1759 drawFrames(wwin, scr->selected_windows,
1760 moveData.realX - wwin->frame_x,
1761 moveData.realY - wwin->frame_y);
1763 if (wPreferences.move_display == WDIS_NEW
1764 && !scr->selected_windows) {
1765 XSync(dpy, False);
1766 showPosition(wwin, moveData.realX, moveData.realY);
1767 XGrabServer(dpy);
1769 } else {
1770 warped = 0;
1772 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1773 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1775 XChangeActivePointerGrab(dpy, ButtonMotionMask
1776 | ButtonReleaseMask | ButtonPressMask,
1777 wCursor[WCUR_MOVE], CurrentTime);
1778 started = 1;
1779 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1780 CurrentTime);
1782 if (!scr->selected_windows)
1783 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1784 moveData.winWidth, moveData.winHeight);
1786 if (started && !opaqueMove)
1787 drawFrames(wwin, scr->selected_windows, 0, 0);
1789 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1790 && !scr->selected_windows)) {
1791 XGrabServer(dpy);
1792 if (wPreferences.move_display==WDIS_NEW)
1793 showPosition(wwin, moveData.realX, moveData.realY);
1796 break;
1798 case ButtonPress:
1799 break;
1801 case ButtonRelease:
1802 if (event.xbutton.button != ev->xbutton.button)
1803 break;
1805 if (started) {
1806 XEvent e;
1807 if (!opaqueMove) {
1808 drawFrames(wwin, scr->selected_windows,
1809 moveData.realX - wwin->frame_x,
1810 moveData.realY - wwin->frame_y);
1811 XSync(dpy, 0);
1812 doWindowMove(wwin, scr->selected_windows,
1813 moveData.realX - wwin->frame_x,
1814 moveData.realY - wwin->frame_y);
1816 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1817 wWindowSynthConfigureNotify(wwin);
1818 #endif
1819 XUngrabKeyboard(dpy, CurrentTime);
1820 XUngrabServer(dpy);
1821 if (!opaqueMove) {
1822 wWindowChangeWorkspace(wwin, scr->current_workspace);
1823 wSetFocusTo(scr, wwin);
1825 if (wPreferences.move_display == WDIS_NEW)
1826 showPosition(wwin, moveData.realX, moveData.realY);
1828 /* discard all enter/leave events that happened until
1829 * the time the button was released */
1830 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1831 if (e.xcrossing.time > event.xbutton.time) {
1832 XPutBackEvent(dpy, &e);
1833 break;
1836 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1837 if (e.xcrossing.time > event.xbutton.time) {
1838 XPutBackEvent(dpy, &e);
1839 break;
1843 if (!scr->selected_windows) {
1844 /* get rid of the geometry window */
1845 WMUnmapWidget(scr->gview);
1848 #ifdef DEBUG
1849 puts("End move window");
1850 #endif
1851 done = True;
1852 break;
1854 default:
1855 if (started && !opaqueMove) {
1856 drawFrames(wwin, scr->selected_windows,
1857 moveData.realX - wwin->frame_x,
1858 moveData.realY - wwin->frame_y);
1859 XUngrabServer(dpy);
1860 WMHandleEvent(&event);
1861 XSync(dpy, False);
1862 XGrabServer(dpy);
1863 drawFrames(wwin, scr->selected_windows,
1864 moveData.realX - wwin->frame_x,
1865 moveData.realY - wwin->frame_y);
1866 } else {
1867 WMHandleEvent(&event);
1869 break;
1873 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1874 XSetWindowAttributes attr;
1877 attr.save_under = False;
1878 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1879 CWSaveUnder, &attr);
1883 freeMoveData(&moveData);
1885 return started;
1889 #define RESIZEBAR 1
1890 #define HCONSTRAIN 2
1892 static int
1893 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1894 int flags)
1896 int w = wwin->frame->core->width - 1;
1897 int cw = wwin->frame->resizebar_corner_width;
1898 int dir;
1900 /* if not resizing through the resizebar */
1901 if (!(flags & RESIZEBAR)) {
1902 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1903 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1904 if (abs(dx) < 2 || abs(dy) < 2) {
1905 if (abs(dy) > abs(dx))
1906 xdir = 0;
1907 else
1908 ydir = 0;
1910 return (xdir | ydir);
1913 /* window is too narrow. allow diagonal resize */
1914 if (cw * 2 >= w) {
1915 int ydir;
1917 if (flags & HCONSTRAIN)
1918 ydir = 0;
1919 else
1920 ydir = DOWN;
1921 if (x < cw)
1922 return (LEFT | ydir);
1923 else
1924 return (RIGHT | ydir);
1926 /* vertical resize */
1927 if ((x > cw) && (x < w - cw))
1928 return DOWN;
1930 if (x < cw)
1931 dir = LEFT;
1932 else
1933 dir = RIGHT;
1935 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1936 dir |= DOWN;
1938 return dir;
1942 void
1943 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1945 XEvent event;
1946 WScreen *scr = wwin->screen_ptr;
1947 Window root = scr->root_win;
1948 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1949 int fw = wwin->frame->core->width;
1950 int fh = wwin->frame->core->height;
1951 int fx = wwin->frame_x;
1952 int fy = wwin->frame_y;
1953 int is_resizebar = (wwin->frame->resizebar
1954 && ev->xany.window==wwin->frame->resizebar->window);
1955 int orig_x, orig_y;
1956 int started;
1957 int dw, dh;
1958 int rw = fw, rh = fh;
1959 int rx1, ry1, rx2, ry2;
1960 int res = 0;
1961 KeyCode shiftl, shiftr;
1962 int h = 0;
1963 int orig_fx = fx;
1964 int orig_fy = fy;
1965 int orig_fw = fw;
1966 int orig_fh = fh;
1968 if (wwin->flags.shaded) {
1969 wwarning("internal error: tryein");
1970 return;
1972 orig_x = ev->xbutton.x_root;
1973 orig_y = ev->xbutton.y_root;
1975 started = 0;
1976 #ifdef DEBUG
1977 puts("Resizing window");
1978 #endif
1980 wUnselectWindows(scr);
1981 rx1 = fx;
1982 rx2 = fx + fw - 1;
1983 ry1 = fy;
1984 ry2 = fy + fh - 1;
1985 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1986 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1987 if (!WFLAGP(wwin, no_titlebar))
1988 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
1989 else
1990 h = 0;
1991 while (1) {
1992 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1993 | ButtonReleaseMask | PointerMotionHintMask
1994 | ButtonPressMask | ExposureMask, &event);
1995 if (!checkMouseSamplingRate(&event))
1996 continue;
1998 switch (event.type) {
1999 case KeyPress:
2000 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2001 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
2002 && started) {
2003 drawTransparentFrame(wwin, fx, fy, fw, fh);
2004 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
2005 drawTransparentFrame(wwin, fx, fy, fw, fh);
2007 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2008 break;
2010 case MotionNotify:
2011 if (started) {
2012 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
2014 dw = 0;
2015 dh = 0;
2017 orig_fx = fx;
2018 orig_fy = fy;
2019 orig_fw = fw;
2020 orig_fh = fh;
2022 if (res & LEFT)
2023 dw = orig_x - event.xmotion.x_root;
2024 else if (res & RIGHT)
2025 dw = event.xmotion.x_root - orig_x;
2026 if (res & UP)
2027 dh = orig_y - event.xmotion.y_root;
2028 else if (res & DOWN)
2029 dh = event.xmotion.y_root - orig_y;
2031 orig_x = event.xmotion.x_root;
2032 orig_y = event.xmotion.y_root;
2034 rw += dw;
2035 rh += dh;
2036 fw = rw;
2037 fh = rh - vert_border;
2038 wWindowConstrainSize(wwin, &fw, &fh);
2039 fh += vert_border;
2040 if (res & LEFT)
2041 fx = rx2 - fw + 1;
2042 else if (res & RIGHT)
2043 fx = rx1;
2044 if (res & UP)
2045 fy = ry2 - fh + 1;
2046 else if (res & DOWN)
2047 fy = ry1;
2048 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
2049 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
2050 int tx, ty;
2051 Window junkw;
2052 int flags;
2054 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
2055 orig_x, orig_y, &tx, &ty, &junkw);
2057 /* check if resizing through resizebar */
2058 if (is_resizebar)
2059 flags = RESIZEBAR;
2060 else
2061 flags = 0;
2063 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
2064 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
2065 flags |= HCONSTRAIN;
2067 res = getResizeDirection(wwin, tx, ty,
2068 orig_x - event.xmotion.x_root,
2069 orig_y - event.xmotion.y_root, flags);
2071 if (res == (UP|LEFT))
2072 XChangeActivePointerGrab(dpy, ButtonMotionMask
2073 | ButtonReleaseMask | ButtonPressMask,
2074 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
2075 else if (res == (UP|RIGHT))
2076 XChangeActivePointerGrab(dpy, ButtonMotionMask
2077 | ButtonReleaseMask | ButtonPressMask,
2078 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
2079 else if (res == (DOWN|LEFT))
2080 XChangeActivePointerGrab(dpy, ButtonMotionMask
2081 | ButtonReleaseMask | ButtonPressMask,
2082 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2083 else if (res == (DOWN|RIGHT))
2084 XChangeActivePointerGrab(dpy, ButtonMotionMask
2085 | ButtonReleaseMask | ButtonPressMask,
2086 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2087 else if (res == DOWN || res == UP)
2088 XChangeActivePointerGrab(dpy, ButtonMotionMask
2089 | ButtonReleaseMask | ButtonPressMask,
2090 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2091 else if (res & (DOWN|UP))
2092 XChangeActivePointerGrab(dpy, ButtonMotionMask
2093 | ButtonReleaseMask | ButtonPressMask,
2094 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2095 else if (res & (LEFT|RIGHT))
2096 XChangeActivePointerGrab(dpy, ButtonMotionMask
2097 | ButtonReleaseMask | ButtonPressMask,
2098 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2100 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
2101 CurrentTime);
2103 XGrabServer(dpy);
2105 /* Draw the resize frame for the first time. */
2106 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2108 drawTransparentFrame(wwin, fx, fy, fw, fh);
2110 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2112 started = 1;
2114 if (started) {
2115 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2116 drawTransparentFrame(wwin, orig_fx, orig_fy,
2117 orig_fw, orig_fh);
2118 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2119 drawTransparentFrame(wwin, fx, fy, fw, fh);
2120 } else {
2121 drawTransparentFrame(wwin, orig_fx, orig_fy,
2122 orig_fw, orig_fh);
2123 drawTransparentFrame(wwin, fx, fy, fw, fh);
2125 if (fh != orig_fh || fw != orig_fw) {
2126 if (wPreferences.size_display == WDIS_NEW) {
2127 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2128 orig_fy + orig_fh, res);
2130 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2133 break;
2135 case ButtonPress:
2136 break;
2138 case ButtonRelease:
2139 if (event.xbutton.button != ev->xbutton.button)
2140 break;
2142 if (started) {
2143 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2145 drawTransparentFrame(wwin, fx, fy, fw, fh);
2147 XUngrabKeyboard(dpy, CurrentTime);
2148 WMUnmapWidget(scr->gview);
2149 XUngrabServer(dpy);
2151 if (wwin->client.width != fw)
2152 wwin->flags.user_changed_width = 1;
2154 if (wwin->client.height != fh - vert_border)
2155 wwin->flags.user_changed_height = 1;
2157 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2159 #ifdef DEBUG
2160 puts("End resize window");
2161 #endif
2162 return;
2164 default:
2165 WMHandleEvent(&event);
2170 #undef LEFT
2171 #undef RIGHT
2172 #undef HORIZONTAL
2173 #undef UP
2174 #undef DOWN
2175 #undef VERTICAL
2176 #undef HCONSTRAIN
2177 #undef RESIZEBAR
2179 void
2180 wUnselectWindows(WScreen *scr)
2182 WWindow *wwin;
2184 if (!scr->selected_windows)
2185 return;
2187 while (WMGetArrayItemCount(scr->selected_windows)) {
2188 wwin = WMGetFromArray(scr->selected_windows, 0);
2189 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2190 wIconSelect(wwin->icon);
2192 wSelectWindow(wwin, False);
2194 WMFreeArray(scr->selected_windows);
2195 scr->selected_windows = NULL;
2198 #ifndef LITE
2199 static void
2200 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2202 WWindow *tmpw;
2204 /* select the windows and put them in the selected window list */
2205 tmpw = scr->focused_window;
2206 while (tmpw != NULL) {
2207 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2208 if ((tmpw->frame->workspace == scr->current_workspace
2209 || IS_OMNIPRESENT(tmpw))
2210 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2211 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2212 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2213 wSelectWindow(tmpw, True);
2216 tmpw = tmpw->prev;
2221 void
2222 wSelectWindows(WScreen *scr, XEvent *ev)
2224 XEvent event;
2225 Window root = scr->root_win;
2226 GC gc = scr->frame_gc;
2227 int xp = ev->xbutton.x_root;
2228 int yp = ev->xbutton.y_root;
2229 int w = 0, h = 0;
2230 int x = xp, y = yp;
2232 #ifdef DEBUG
2233 puts("Selecting windows");
2234 #endif
2235 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2236 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2237 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2238 CurrentTime) != Success) {
2239 return;
2241 XGrabServer(dpy);
2243 wUnselectWindows(scr);
2245 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2246 while (1) {
2247 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2248 | ButtonPressMask, &event);
2250 switch (event.type) {
2251 case MotionNotify:
2252 XDrawRectangle(dpy, root, gc, x, y, w, h);
2253 x = event.xmotion.x_root;
2254 if (x < xp) {
2255 w = xp - x;
2256 } else {
2257 w = x - xp;
2258 x = xp;
2260 y = event.xmotion.y_root;
2261 if (y < yp) {
2262 h = yp - y;
2263 } else {
2264 h = y - yp;
2265 y = yp;
2267 XDrawRectangle(dpy, root, gc, x, y, w, h);
2268 break;
2270 case ButtonPress:
2271 break;
2273 case ButtonRelease:
2274 if (event.xbutton.button != ev->xbutton.button)
2275 break;
2277 XDrawRectangle(dpy, root, gc, x, y, w, h);
2278 XUngrabServer(dpy);
2279 XUngrabPointer(dpy, CurrentTime);
2280 selectWindowsInside(scr, x, y, x + w, y + h);
2282 #ifdef KWM_HINTS
2283 wKWMSelectRootRegion(scr, xp, yp, w, h,
2284 event.xbutton.state & ControlMask);
2285 #endif /* KWM_HINTS */
2287 #ifdef DEBUG
2288 puts("End window selection");
2289 #endif
2290 return;
2292 default:
2293 WMHandleEvent(&event);
2294 break;
2298 #endif /* !LITE */
2300 void
2301 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2302 unsigned width, unsigned height)
2304 WScreen *scr = wwin->screen_ptr;
2305 Window root = scr->root_win;
2306 int x, y, h = 0;
2307 XEvent event;
2308 KeyCode shiftl, shiftr;
2309 Window junkw;
2310 int junk;
2312 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2313 GrabModeAsync, GrabModeAsync, None,
2314 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2315 *x_ret = 0;
2316 *y_ret = 0;
2317 return;
2319 if (!WFLAGP(wwin, no_titlebar)) {
2320 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2321 height += h;
2323 if (!WFLAGP(wwin, no_resizebar)) {
2324 height += RESIZEBAR_HEIGHT;
2326 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2327 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2328 (unsigned *) &junk);
2329 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2331 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2333 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2334 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2335 while (1) {
2336 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2337 &event);
2339 if (!checkMouseSamplingRate(&event))
2340 continue;
2342 switch (event.type) {
2343 case KeyPress:
2344 if ((event.xkey.keycode == shiftl)
2345 || (event.xkey.keycode == shiftr)) {
2346 drawTransparentFrame(wwin,
2347 x - width/2, y - h/2, width, height);
2348 cyclePositionDisplay(wwin,
2349 x - width/2, y - h/2, width, height);
2350 drawTransparentFrame(wwin,
2351 x - width/2, y - h/2, width, height);
2353 break;
2355 case MotionNotify:
2356 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2358 x = event.xmotion.x_root;
2359 y = event.xmotion.y_root;
2361 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2362 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2364 showPosition(wwin, x - width/2, y - h/2);
2366 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2368 break;
2370 case ButtonPress:
2371 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2372 XSync(dpy, 0);
2373 *x_ret = x - width/2;
2374 *y_ret = y - h/2;
2375 XUngrabPointer(dpy, CurrentTime);
2376 XUngrabKeyboard(dpy, CurrentTime);
2377 /* get rid of the geometry window */
2378 WMUnmapWidget(scr->gview);
2379 return;
2381 default:
2382 WMHandleEvent(&event);
2383 break;