added new maximize positions, top and bottom
[wmaker-crm.git] / src / moveres.c
blob8f0ecbab5f31c78659f7eaaf6b561301956f6deb
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "client.h"
35 #include "icon.h"
36 #include "dock.h"
37 #include "funcs.h"
38 #include "actions.h"
39 #include "workspace.h"
40 #include "placement.h"
42 #include "geomview.h"
43 #include "screen.h"
44 #include "xinerama.h"
46 #include <WINGs/WINGsP.h>
48 /* How many different types of geometry/position
49 display thingies are there? */
50 #define NUM_DISPLAYS 5
52 #define LEFT 1
53 #define RIGHT 2
54 #define HORIZONTAL (LEFT|RIGHT)
55 #define UP 4
56 #define DOWN 8
57 #define VERTICAL (UP|DOWN)
59 /* True if window currently has a border. This also includes borderless
60 * windows which are currently selected
62 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
64 /****** Global Variables ******/
65 extern Cursor wCursor[WCUR_LAST];
66 extern WPreferences wPreferences;
69 *----------------------------------------------------------------------
70 * checkMouseSamplingRate-
71 * For lowering the mouse motion sampling rate for machines where
72 * it's too high (SGIs). If it returns False then the event should be
73 * ignored.
74 *----------------------------------------------------------------------
76 static Bool checkMouseSamplingRate(XEvent * ev)
78 static Time previousMotion = 0;
80 if (ev->type == MotionNotify) {
81 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
82 return False;
83 } else {
84 previousMotion = ev->xmotion.time;
87 return True;
91 *----------------------------------------------------------------------
92 * moveGeometryDisplayCentered
94 * routine that moves the geometry/position window on scr so it is
95 * centered over the given coordinates (x,y). Also the window position
96 * is clamped so it stays on the screen at all times.
97 *----------------------------------------------------------------------
99 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
101 unsigned int w = WMWidgetWidth(scr->gview);
102 unsigned int h = WMWidgetHeight(scr->gview);
103 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
105 x -= w / 2;
106 y -= h / 2;
108 /* dead area check */
109 if (scr->xine_info.count) {
110 WMRect rect;
111 int head, flags;
113 rect.pos.x = x;
114 rect.pos.y = y;
115 rect.size.width = w;
116 rect.size.height = h;
118 head = wGetRectPlacementInfo(scr, rect, &flags);
120 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
121 rect = wGetRectForHead(scr, head);
122 x1 = rect.pos.x;
123 y1 = rect.pos.y;
124 x2 = x1 + rect.size.width;
125 y2 = y1 + rect.size.height;
129 if (x < x1 + 1)
130 x = x1 + 1;
131 else if (x > (x2 - w))
132 x = x2 - w;
134 if (y < y1 + 1)
135 y = y1 + 1;
136 else if (y > (y2 - h))
137 y = y2 - h;
139 WMMoveWidget(scr->gview, x, y);
142 static void showPosition(WWindow * wwin, int x, int y)
144 WScreen *scr = wwin->screen_ptr;
146 if (wPreferences.move_display == WDIS_NEW) {
147 #if 0
148 int width = wwin->frame->core->width;
149 int height = wwin->frame->core->height;
151 GC lgc = scr->line_gc;
152 XSetForeground(dpy, lgc, scr->line_pixel);
153 sprintf(num, "%i", x);
155 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
156 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
157 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
158 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
159 #endif
160 } else {
161 WSetGeometryViewShownPosition(scr->gview, x, y);
165 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
167 WScreen *scr = wwin->screen_ptr;
168 WMRect rect;
170 wPreferences.move_display++;
171 wPreferences.move_display %= NUM_DISPLAYS;
173 if (wPreferences.move_display == WDIS_NEW) {
174 wPreferences.move_display++;
175 wPreferences.move_display %= NUM_DISPLAYS;
178 if (wPreferences.move_display == WDIS_NONE) {
179 WMUnmapWidget(scr->gview);
180 } else {
181 if (wPreferences.move_display == WDIS_CENTER) {
182 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
183 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
184 rect.pos.y + rect.size.height / 2);
185 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
186 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
187 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
188 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
189 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
191 WMMapWidget(scr->gview);
195 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
197 WScreen *scr = wwin->screen_ptr;
198 WMRect rect;
200 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
201 return;
202 } else if (wPreferences.move_display == WDIS_CENTER) {
203 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
204 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
205 rect.pos.y + rect.size.height / 2);
206 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
207 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
208 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
209 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
210 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
212 WMMapWidget(scr->gview);
213 WSetGeometryViewShownPosition(scr->gview, x, y);
216 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
218 WScreen *scr = wwin->screen_ptr;
219 Window root = scr->root_win;
220 GC gc = scr->line_gc;
221 int ty, by, my, x, y, mx, s;
222 char num[16];
223 XSegment segment[4];
224 int fw, fh;
226 /* This seems necessary for some odd reason (too lazy to write x1-1 and
227 * x2-1 everywhere below in the code). But why only for x? */
228 x1--;
229 x2--;
231 if (HAS_BORDER_WITH_SELECT(wwin)) {
232 x1 += scr->frame_border_width;
233 x2 += scr->frame_border_width;
234 y1 += scr->frame_border_width;
235 y2 += scr->frame_border_width;
238 ty = y1 + wwin->frame->top_width;
239 by = y2 - wwin->frame->bottom_width;
241 if (wPreferences.size_display == WDIS_NEW) {
242 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
243 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
245 XSetForeground(dpy, gc, scr->line_pixel);
247 /* vertical geometry */
248 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
249 x = x2;
250 s = -15;
251 } else {
252 x = x1;
253 s = 15;
255 my = (ty + by) / 2;
257 /* top arrow & end bar */
258 segment[0].x1 = x - (s + 6);
259 segment[0].y1 = ty;
260 segment[0].x2 = x - (s - 10);
261 segment[0].y2 = ty;
263 /* arrowhead */
264 segment[1].x1 = x - (s - 2);
265 segment[1].y1 = ty + 1;
266 segment[1].x2 = x - (s - 5);
267 segment[1].y2 = ty + 7;
269 segment[2].x1 = x - (s - 2);
270 segment[2].y1 = ty + 1;
271 segment[2].x2 = x - (s + 1);
272 segment[2].y2 = ty + 7;
274 /* line */
275 segment[3].x1 = x - (s - 2);
276 segment[3].y1 = ty + 1;
277 segment[3].x2 = x - (s - 2);
278 segment[3].y2 = my - fh / 2 - 1;
280 XDrawSegments(dpy, root, gc, segment, 4);
282 /* bottom arrow & end bar */
283 segment[0].y1 = by;
284 segment[0].y2 = by;
286 /* arrowhead */
287 segment[1].y1 = by - 1;
288 segment[1].y2 = by - 7;
290 segment[2].y1 = by - 1;
291 segment[2].y2 = by - 7;
293 /* line */
294 segment[3].y1 = my + fh / 2 + 2;
295 segment[3].y2 = by - 1;
297 XDrawSegments(dpy, root, gc, segment, 4);
299 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
300 wwin->normal_hints->height_inc);
301 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
303 /* Display the height. */
304 XSetFont(dpy, gc, scr->tech_draw_font->fid);
305 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
306 strlen(num));
308 /* horizontal geometry */
309 if (y1 < 15) {
310 y = y2;
311 s = -15;
312 } else {
313 y = y1;
314 s = 15;
316 mx = x1 + (x2 - x1) / 2;
317 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
318 wwin->normal_hints->width_inc);
319 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
321 /* left arrow & end bar */
322 segment[0].x1 = x1;
323 segment[0].y1 = y - (s + 6);
324 segment[0].x2 = x1;
325 segment[0].y2 = y - (s - 10);
327 /* arrowhead */
328 segment[1].x1 = x1 + 7;
329 segment[1].y1 = y - (s + 1);
330 segment[1].x2 = x1 + 1;
331 segment[1].y2 = y - (s - 2);
333 segment[2].x1 = x1 + 1;
334 segment[2].y1 = y - (s - 2);
335 segment[2].x2 = x1 + 7;
336 segment[2].y2 = y - (s - 5);
338 /* line */
339 segment[3].x1 = x1 + 1;
340 segment[3].y1 = y - (s - 2);
341 segment[3].x2 = mx - fw / 2 - 2;
342 segment[3].y2 = y - (s - 2);
344 XDrawSegments(dpy, root, gc, segment, 4);
346 /* right arrow & end bar */
347 segment[0].x1 = x2 + 1;
348 segment[0].x2 = x2 + 1;
350 /* arrowhead */
351 segment[1].x1 = x2 - 6;
352 segment[1].x2 = x2;
354 segment[2].x1 = x2;
355 segment[2].x2 = x2 - 6;
357 /* line */
358 segment[3].x1 = mx + fw / 2 + 2;
359 segment[3].x2 = x2;
361 XDrawSegments(dpy, root, gc, segment, 4);
363 /* Display the width. */
364 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
365 strlen(num));
366 } else {
367 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
368 / wwin->normal_hints->width_inc,
369 (by - ty - wwin->normal_hints->base_height)
370 / wwin->normal_hints->height_inc);
374 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
376 WScreen *scr = wwin->screen_ptr;
377 WMRect rect;
379 wPreferences.size_display++;
380 wPreferences.size_display %= NUM_DISPLAYS;
382 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
383 WMUnmapWidget(scr->gview);
384 } else {
385 if (wPreferences.size_display == WDIS_CENTER) {
386 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
387 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
388 rect.pos.y + rect.size.height / 2);
389 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
390 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
391 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
392 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
393 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
395 WMMapWidget(scr->gview);
396 showGeometry(wwin, x, y, x + w, y + h, dir);
400 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
402 WScreen *scr = wwin->screen_ptr;
403 WMRect rect;
405 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
406 return;
408 if (wPreferences.size_display == WDIS_CENTER) {
409 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
410 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
411 rect.pos.y + rect.size.height / 2);
412 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
413 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
414 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
415 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
416 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
418 WMMapWidget(scr->gview);
419 showGeometry(wwin, x, y, x + w, y + h, 0);
422 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
424 WWindow *tmpw;
425 WScreen *scr = wwin->screen_ptr;
426 int x, y;
428 if (!array || !WMGetArrayItemCount(array)) {
429 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
430 } else {
431 WMArrayIterator iter;
433 WM_ITERATE_ARRAY(array, tmpw, iter) {
434 x = tmpw->frame_x + dx;
435 y = tmpw->frame_y + dy;
437 #if 1 /* XXX: with xinerama patch was #if 0, check this */
438 /* don't let windows become unreachable */
440 if (x + (int)tmpw->frame->core->width < 20)
441 x = 20 - (int)tmpw->frame->core->width;
442 else if (x + 20 > scr->scr_width)
443 x = scr->scr_width - 20;
445 if (y + (int)tmpw->frame->core->height < 20)
446 y = 20 - (int)tmpw->frame->core->height;
447 else if (y + 20 > scr->scr_height)
448 y = scr->scr_height - 20;
449 #else
450 wScreenBringInside(scr, &x, &y,
451 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
452 #endif
454 wWindowMove(tmpw, x, y);
459 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
461 Window root = wwin->screen_ptr->root_win;
462 GC gc = wwin->screen_ptr->frame_gc;
463 int h = 0;
464 int bottom = 0;
466 if (HAS_BORDER_WITH_SELECT(wwin)) {
467 x += wwin->screen_ptr->frame_border_width;
468 y += wwin->screen_ptr->frame_border_width;
471 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
472 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
473 TITLEBAR_EXTEND_SPACE) * 2;
475 if (h > wPreferences.window_title_max_height)
476 h = wPreferences.window_title_max_height;
478 if (h < wPreferences.window_title_min_height)
479 h = wPreferences.window_title_min_height;
481 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
482 /* Can't use wwin-frame->bottom_width because, in some cases
483 (e.g. interactive placement), frame does not point to anything. */
484 bottom = RESIZEBAR_HEIGHT;
486 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
488 if (h > 0) {
489 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
491 if (bottom > 0) {
492 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
496 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
498 WWindow *tmpw;
499 int scr_width = wwin->screen_ptr->scr_width;
500 int scr_height = wwin->screen_ptr->scr_height;
501 int x, y;
503 if (!array) {
505 x = wwin->frame_x + dx;
506 y = wwin->frame_y + dy;
508 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
510 } else {
511 WMArrayIterator iter;
513 WM_ITERATE_ARRAY(array, tmpw, iter) {
514 x = tmpw->frame_x + dx;
515 y = tmpw->frame_y + dy;
517 /* don't let windows become unreachable */
518 #if 1 /* XXX: was 0 in XINERAMA patch, check */
519 if (x + (int)tmpw->frame->core->width < 20)
520 x = 20 - (int)tmpw->frame->core->width;
521 else if (x + 20 > scr_width)
522 x = scr_width - 20;
524 if (y + (int)tmpw->frame->core->height < 20)
525 y = 20 - (int)tmpw->frame->core->height;
526 else if (y + 20 > scr_height)
527 y = scr_height - 20;
529 #else
530 wScreenBringInside(wwin->screen_ptr, &x, &y,
531 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
532 #endif
534 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
539 static void flushMotion(void)
541 XEvent ev;
543 XSync(dpy, False);
544 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
547 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
549 /* do not let window be unmapped */
550 if (opaque_move) {
551 wwin->flags.changing_workspace = 1;
552 wWindowChangeWorkspace(wwin, new_workspace);
554 /* go to new workspace */
555 wWorkspaceChange(scr, new_workspace);
557 wwin->flags.changing_workspace = 0;
559 if (rewind)
560 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
561 else
562 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
564 flushMotion();
566 if (!opaque_move) {
567 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
568 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
569 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
573 typedef struct {
574 /* arrays of WWindows sorted by the respective border position */
575 WWindow **topList; /* top border */
576 WWindow **leftList; /* left border */
577 WWindow **rightList; /* right border */
578 WWindow **bottomList; /* bottom border */
579 int count;
581 /* index of window in the above lists indicating the relative position
582 * of the window with the others */
583 int topIndex;
584 int leftIndex;
585 int rightIndex;
586 int bottomIndex;
588 int rubCount; /* for workspace switching */
590 int winWidth, winHeight; /* width/height of the window */
591 int realX, realY; /* actual position of the window */
592 int calcX, calcY; /* calculated position of window */
593 int omouseX, omouseY; /* old mouse position */
594 int mouseX, mouseY; /* last known position of the pointer */
595 } MoveData;
597 #define WTOP(w) (w)->frame_y
598 #define WLEFT(w) (w)->frame_x
599 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
600 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
601 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
602 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
604 static int compareWTop(const void *a, const void *b)
606 WWindow *wwin1 = *(WWindow **) a;
607 WWindow *wwin2 = *(WWindow **) b;
609 if (WTOP(wwin1) > WTOP(wwin2))
610 return -1;
611 else if (WTOP(wwin1) < WTOP(wwin2))
612 return 1;
613 else
614 return 0;
617 static int compareWLeft(const void *a, const void *b)
619 WWindow *wwin1 = *(WWindow **) a;
620 WWindow *wwin2 = *(WWindow **) b;
622 if (WLEFT(wwin1) > WLEFT(wwin2))
623 return -1;
624 else if (WLEFT(wwin1) < WLEFT(wwin2))
625 return 1;
626 else
627 return 0;
630 static int compareWRight(const void *a, const void *b)
632 WWindow *wwin1 = *(WWindow **) a;
633 WWindow *wwin2 = *(WWindow **) b;
635 if (WRIGHT(wwin1) < WRIGHT(wwin2))
636 return -1;
637 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
638 return 1;
639 else
640 return 0;
643 static int compareWBottom(const void *a, const void *b)
645 WWindow *wwin1 = *(WWindow **) a;
646 WWindow *wwin2 = *(WWindow **) b;
648 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
649 return -1;
650 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
651 return 1;
652 else
653 return 0;
656 static void updateResistance(WWindow * wwin, MoveData * data, int newX, int newY)
658 int i;
659 int newX2 = newX + data->winWidth;
660 int newY2 = newY + data->winHeight;
661 Bool ok = False;
663 if (newX < data->realX) {
664 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
665 ok = True;
666 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
667 ok = True;
669 } else if (newX > data->realX) {
670 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
671 ok = True;
672 } else if (data->rightIndex <= data->count - 1
673 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
674 ok = True;
678 if (!ok) {
679 if (newY < data->realY) {
680 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
681 ok = True;
682 } else if (data->topIndex <= data->count - 1
683 && newY2 <= WTOP(data->topList[data->topIndex])) {
684 ok = True;
686 } else if (newY > data->realY) {
687 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
688 ok = True;
689 } else if (data->bottomIndex <= data->count - 1
690 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
691 ok = True;
696 if (!ok)
697 return;
699 /* TODO: optimize this */
700 if (data->realY < WBOTTOM(data->bottomList[0])) {
701 data->bottomIndex = 0;
703 if (data->realX < WRIGHT(data->rightList[0])) {
704 data->rightIndex = 0;
706 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
707 data->leftIndex = 0;
709 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
710 data->topIndex = 0;
712 for (i = 0; i < data->count; i++) {
713 if (data->realY > WBOTTOM(data->bottomList[i])) {
714 data->bottomIndex = i + 1;
716 if (data->realX > WRIGHT(data->rightList[i])) {
717 data->rightIndex = i + 1;
719 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
720 data->leftIndex = i + 1;
722 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
723 data->topIndex = i + 1;
728 static void freeMoveData(MoveData * data)
730 if (data->topList)
731 wfree(data->topList);
732 if (data->leftList)
733 wfree(data->leftList);
734 if (data->rightList)
735 wfree(data->rightList);
736 if (data->bottomList)
737 wfree(data->bottomList);
740 static void updateMoveData(WWindow * wwin, MoveData * data)
742 WScreen *scr = wwin->screen_ptr;
743 WWindow *tmp;
744 int i;
746 data->count = 0;
747 tmp = scr->focused_window;
748 while (tmp) {
749 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
750 && !tmp->flags.miniaturized
751 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
752 data->topList[data->count] = tmp;
753 data->leftList[data->count] = tmp;
754 data->rightList[data->count] = tmp;
755 data->bottomList[data->count] = tmp;
756 data->count++;
758 tmp = tmp->prev;
761 if (data->count == 0) {
762 data->topIndex = 0;
763 data->leftIndex = 0;
764 data->rightIndex = 0;
765 data->bottomIndex = 0;
766 return;
769 /* order from closest to the border of the screen to farthest */
771 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
772 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
773 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
774 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
776 /* figure the position of the window relative to the others */
778 data->topIndex = -1;
779 data->leftIndex = -1;
780 data->rightIndex = -1;
781 data->bottomIndex = -1;
783 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
784 data->bottomIndex = 0;
786 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
787 data->rightIndex = 0;
789 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
790 data->leftIndex = 0;
792 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
793 data->topIndex = 0;
795 for (i = 0; i < data->count; i++) {
796 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
797 data->bottomIndex = i + 1;
799 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
800 data->rightIndex = i + 1;
802 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
803 data->leftIndex = i + 1;
805 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
806 data->topIndex = i + 1;
811 static void initMoveData(WWindow * wwin, MoveData * data)
813 int i;
814 WWindow *tmp;
816 memset(data, 0, sizeof(MoveData));
818 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
820 if (i > 1) {
821 data->topList = wmalloc(sizeof(WWindow *) * i);
822 data->leftList = wmalloc(sizeof(WWindow *) * i);
823 data->rightList = wmalloc(sizeof(WWindow *) * i);
824 data->bottomList = wmalloc(sizeof(WWindow *) * i);
826 updateMoveData(wwin, data);
829 data->realX = wwin->frame_x;
830 data->realY = wwin->frame_y;
831 data->calcX = wwin->frame_x;
832 data->calcY = wwin->frame_y;
834 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
835 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
838 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
840 WScreen *scr = wwin->screen_ptr;
841 Bool changed = False;
843 if (data->mouseX <= 1) {
844 if (scr->current_workspace > 0) {
846 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1, True);
847 changed = True;
848 data->rubCount = 0;
850 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
852 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1, True);
853 changed = True;
854 data->rubCount = 0;
856 } else if (data->mouseX >= scr->scr_width - 2) {
858 if (scr->current_workspace == scr->workspace_count - 1) {
860 if (wPreferences.ws_cycle || scr->workspace_count == MAX_WORKSPACES) {
862 crossWorkspace(scr, wwin, opaqueMove, 0, False);
863 changed = True;
864 data->rubCount = 0;
866 /* if user insists on trying to go to next workspace even when
867 * it's already the last, create a new one */
868 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
870 /* detect user "rubbing" the window against the edge */
871 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
873 data->rubCount = -(data->rubCount + 1);
875 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
877 data->rubCount = -data->rubCount + 1;
880 /* create a new workspace */
881 if (abs(data->rubCount) > 2) {
882 /* go to next workspace */
883 wWorkspaceNew(scr);
885 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
886 changed = True;
887 data->rubCount = 0;
889 } else if (scr->current_workspace < scr->workspace_count) {
891 /* go to next workspace */
892 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
893 changed = True;
894 data->rubCount = 0;
896 } else {
897 data->rubCount = 0;
900 return changed;
903 static void
904 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
905 Bool opaqueMove, int newMouseX, int newMouseY)
907 WScreen *scr = wwin->screen_ptr;
908 int dx, dy; /* how much mouse moved */
909 int winL, winR, winT, winB; /* requested new window position */
910 int newX, newY; /* actual new window position */
911 Bool hresist, vresist;
912 Bool attract;
914 hresist = False;
915 vresist = False;
917 /* check the direction of the movement */
918 dx = newMouseX - data->mouseX;
919 dy = newMouseY - data->mouseY;
921 data->omouseX = data->mouseX;
922 data->omouseY = data->mouseY;
923 data->mouseX = newMouseX;
924 data->mouseY = newMouseY;
926 winL = data->calcX + dx;
927 winR = data->calcX + data->winWidth + dx;
928 winT = data->calcY + dy;
929 winB = data->calcY + data->winHeight + dy;
931 newX = data->realX;
932 newY = data->realY;
934 if (doResistance) {
935 int l_edge, r_edge;
936 int edge_l, edge_r;
937 int t_edge, b_edge;
938 int edge_t, edge_b;
939 int resist;
941 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
942 attract = wPreferences.attract;
943 /* horizontal movement: check horizontal edge resistances */
944 if (dx || dy) {
945 WMRect rect;
946 int i, head;
947 /* window is the leftmost window: check against screen edge */
949 /* Add inter head resistance 1/2 (if needed) */
950 head = wGetHeadForPointerLocation(scr);
951 rect = wGetRectForHead(scr, head);
953 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
954 edge_l = l_edge - resist;
955 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
956 r_edge = edge_r + resist;
958 /* 1 */
959 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
960 WWindow *looprw;
962 for (i = data->rightIndex - 1; i >= 0; i--) {
963 looprw = data->rightList[i];
964 if (!(data->realY > WBOTTOM(looprw)
965 || (data->realY + data->winHeight) < WTOP(looprw))) {
966 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
967 l_edge = WRIGHT(looprw) + 1;
968 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
970 break;
974 if (attract) {
975 for (i = data->rightIndex; i < data->count; i++) {
976 looprw = data->rightList[i];
977 if (!(data->realY > WBOTTOM(looprw)
978 || (data->realY + data->winHeight) < WTOP(looprw))) {
979 r_edge = WRIGHT(looprw) + 1;
980 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
981 break;
987 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
988 WWindow *looprw;
990 for (i = data->leftIndex - 1; i >= 0; i--) {
991 looprw = data->leftList[i];
992 if (!(data->realY > WBOTTOM(looprw)
993 || (data->realY + data->winHeight) < WTOP(looprw))) {
994 if (attract
995 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
996 && dx > 0)) {
997 edge_r = WLEFT(looprw);
998 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1000 break;
1004 if (attract)
1005 for (i = data->leftIndex; i < data->count; i++) {
1006 looprw = data->leftList[i];
1007 if (!(data->realY > WBOTTOM(looprw)
1008 || (data->realY + data->winHeight) < WTOP(looprw))) {
1009 edge_l = WLEFT(looprw);
1010 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1011 break;
1017 printf("%d %d\n",winL,winR);
1018 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1021 if ((winL - l_edge) < (r_edge - winL)) {
1022 if (resist > 0) {
1023 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1024 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1025 newX = l_edge;
1026 hresist = True;
1029 } else {
1030 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1031 newX = r_edge;
1032 hresist = True;
1036 if ((winR - edge_l) < (edge_r - winR)) {
1037 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1038 newX = edge_l - data->winWidth;
1039 hresist = True;
1041 } else {
1042 if (resist > 0) {
1043 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1044 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1045 newX = edge_r - data->winWidth;
1046 hresist = True;
1051 /* VeRT */
1052 /* Add inter head resistance 2/2 (if needed) */
1053 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1054 edge_t = t_edge - resist;
1055 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1056 b_edge = edge_b + resist;
1058 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1059 WWindow *looprw;
1061 for (i = data->bottomIndex - 1; i >= 0; i--) {
1062 looprw = data->bottomList[i];
1063 if (!(data->realX > WRIGHT(looprw)
1064 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1065 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1066 t_edge = WBOTTOM(looprw) + 1;
1067 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1069 break;
1073 if (attract) {
1074 for (i = data->bottomIndex; i < data->count; i++) {
1075 looprw = data->bottomList[i];
1076 if (!(data->realX > WRIGHT(looprw)
1077 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1078 b_edge = WBOTTOM(looprw) + 1;
1079 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1080 break;
1086 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1087 WWindow *looprw;
1089 for (i = data->topIndex - 1; i >= 0; i--) {
1090 looprw = data->topList[i];
1091 if (!(data->realX > WRIGHT(looprw)
1092 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1093 if (attract
1094 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1095 && dy > 0)) {
1096 edge_b = WTOP(looprw);
1097 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1099 break;
1103 if (attract)
1104 for (i = data->topIndex; i < data->count; i++) {
1105 looprw = data->topList[i];
1106 if (!(data->realX > WRIGHT(looprw)
1107 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1108 edge_t = WTOP(looprw);
1109 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1110 break;
1115 if ((winT - t_edge) < (b_edge - winT)) {
1116 if (resist > 0) {
1117 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1118 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1119 newY = t_edge;
1120 vresist = True;
1123 } else {
1124 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1125 newY = b_edge;
1126 vresist = True;
1130 if ((winB - edge_t) < (edge_b - winB)) {
1131 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1132 newY = edge_t - data->winHeight;
1133 vresist = True;
1135 } else {
1136 if (resist > 0) {
1137 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1138 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1139 newY = edge_b - data->winHeight;
1140 vresist = True;
1145 /* END VeRT */
1149 /* update window position */
1150 data->calcX += dx;
1151 data->calcY += dy;
1153 if (((dx > 0 && data->calcX - data->realX > 0)
1154 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1155 newX = data->calcX;
1157 if (((dy > 0 && data->calcY - data->realY > 0)
1158 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1159 newY = data->calcY;
1161 if (data->realX != newX || data->realY != newY) {
1163 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1164 showPosition(wwin, data->realX, data->realY);
1166 if (opaqueMove) {
1167 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1168 } else {
1169 /* erase frames */
1170 drawFrames(wwin, scr->selected_windows,
1171 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1174 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1176 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1179 if (!opaqueMove) {
1180 /* draw frames */
1181 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1184 if (!scr->selected_windows) {
1185 showPosition(wwin, newX, newY);
1189 /* recalc relative window position */
1190 if (doResistance && (data->realX != newX || data->realY != newY)) {
1191 updateResistance(wwin, data, newX, newY);
1194 data->realX = newX;
1195 data->realY = newY;
1198 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1200 #define MOVABLE_BIT 0x01
1201 #define RESIZABLE_BIT 0x02
1203 int wKeyboardMoveResizeWindow(WWindow * wwin)
1205 WScreen *scr = wwin->screen_ptr;
1206 Window root = scr->root_win;
1207 XEvent event;
1208 int w = wwin->frame->core->width;
1209 int h = wwin->frame->core->height;
1210 int scr_width = wwin->screen_ptr->scr_width;
1211 int scr_height = wwin->screen_ptr->scr_height;
1212 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1213 int src_x = wwin->frame_x;
1214 int src_y = wwin->frame_y;
1215 int original_w = w;
1216 int original_h = h;
1217 int done, off_x, off_y, ww, wh;
1218 int kspeed = _KS;
1219 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1220 Time lastTime = 0;
1221 KeyCode shiftl, shiftr, ctrlmode;
1222 KeySym keysym = NoSymbol;
1223 int moment = 0;
1224 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1225 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1226 ? wGetHeadForWindow(wwin)
1227 : scr->xine_info.primary_head);
1229 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1230 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1231 ctrlmode = done = off_x = off_y = 0;
1233 if (modes == RESIZABLE_BIT) {
1234 ctrlmode = 1;
1237 XSync(dpy, False);
1238 wusleep(10000);
1239 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1241 if (!wwin->flags.selected) {
1242 wUnselectWindows(scr);
1244 XGrabServer(dpy);
1245 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1246 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1247 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1251 if (!opaqueMoveResize) {
1252 if (wwin->flags.shaded || scr->selected_windows) {
1253 if (scr->selected_windows)
1254 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1255 else
1256 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1257 } else {
1258 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1261 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1262 mapPositionDisplay(wwin, src_x, src_y, w, h);
1265 ww = w;
1266 wh = h;
1267 while (1) {
1269 looper.ox=off_x;
1270 looper.oy=off_y;
1272 do {
1273 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1274 | ButtonPressMask | ExposureMask, &event);
1275 if (event.type == Expose) {
1276 WMHandleEvent(&event);
1278 } while (event.type == Expose);
1280 if (!opaqueMoveResize) {
1281 if (wwin->flags.shaded || scr->selected_windows) {
1282 if (scr->selected_windows)
1283 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1284 else
1285 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1286 /*** I HATE EDGE RESISTANCE - ]d ***/
1287 } else {
1288 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1292 if (ctrlmode)
1293 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1296 XUngrabServer(dpy);
1297 XSync(dpy, False);
1299 switch (event.type) {
1300 case KeyPress:
1301 /* accelerate */
1302 if (event.xkey.time - lastTime > 50) {
1303 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1304 } else {
1305 if (kspeed < 20) {
1306 kspeed++;
1309 if (kspeed < _KS)
1310 kspeed = _KS;
1311 lastTime = event.xkey.time;
1312 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1313 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1314 ctrlmode = 1;
1315 wUnselectWindows(scr);
1316 } else {
1317 ctrlmode = 0;
1320 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1321 if (ctrlmode)
1322 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1323 else
1324 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1325 } else {
1327 keysym = XLookupKeysym(&event.xkey, 0);
1328 switch (keysym) {
1329 case XK_Return:
1330 done = 2;
1331 break;
1332 case XK_Escape:
1333 done = 1;
1334 break;
1335 case XK_Up:
1336 #ifdef XK_KP_Up
1337 case XK_KP_Up:
1338 #endif
1339 case XK_k:
1340 if (ctrlmode) {
1341 if (moment != UP)
1342 h = wh;
1343 h -= kspeed;
1344 moment = UP;
1345 if (h < 1)
1346 h = 1;
1347 } else
1348 off_y -= kspeed;
1349 break;
1350 case XK_Down:
1351 #ifdef XK_KP_Down
1352 case XK_KP_Down:
1353 #endif
1354 case XK_j:
1355 if (ctrlmode) {
1356 if (moment != DOWN)
1357 h = wh;
1358 h += kspeed;
1359 moment = DOWN;
1360 } else
1361 off_y += kspeed;
1362 break;
1363 case XK_Left:
1364 #ifdef XK_KP_Left
1365 case XK_KP_Left:
1366 #endif
1367 case XK_h:
1368 if (ctrlmode) {
1369 if (moment != LEFT)
1370 w = ww;
1371 w -= kspeed;
1372 if (w < 1)
1373 w = 1;
1374 moment = LEFT;
1375 } else
1376 off_x -= kspeed;
1377 break;
1378 case XK_Right:
1379 #ifdef XK_KP_Right
1380 case XK_KP_Right:
1381 #endif
1382 case XK_l:
1383 if (ctrlmode) {
1384 if (moment != RIGHT)
1385 w = ww;
1386 w += kspeed;
1387 moment = RIGHT;
1388 } else
1389 off_x += kspeed;
1390 break;
1393 ww = w;
1394 wh = h;
1395 wh -= vert_border;
1396 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1397 wh += vert_border;
1399 if (wPreferences.ws_cycle) {
1400 if (src_x + off_x + ww < 20) {
1401 if (!scr->current_workspace) {
1402 wWorkspaceChange(scr, scr->workspace_count - 1);
1403 } else
1404 wWorkspaceChange(scr, scr->current_workspace - 1);
1405 off_x += scr_width;
1406 } else if (src_x + off_x + 20 > scr_width) {
1407 if (scr->current_workspace == scr->workspace_count - 1) {
1408 wWorkspaceChange(scr, 0);
1409 } else
1410 wWorkspaceChange(scr, scr->current_workspace + 1);
1411 off_x -= scr_width;
1413 } else {
1414 if (src_x + off_x + ww < 20)
1415 off_x = 20 - ww - src_x;
1416 else if (src_x + off_x + 20 > scr_width)
1417 off_x = scr_width - 20 - src_x;
1420 if (src_y + off_y + wh < 20) {
1421 off_y = 20 - wh - src_y;
1422 } else if (src_y + off_y + 20 > scr_height) {
1423 off_y = scr_height - 20 - src_y;
1426 break;
1427 case ButtonPress:
1428 case ButtonRelease:
1429 done = 1;
1430 break;
1431 case Expose:
1432 WMHandleEvent(&event);
1433 while (XCheckTypedEvent(dpy, Expose, &event)) {
1434 WMHandleEvent(&event);
1436 break;
1438 default:
1439 WMHandleEvent(&event);
1440 break;
1443 XGrabServer(dpy);
1444 /*xxx */
1446 if (wwin->flags.shaded && !scr->selected_windows) {
1447 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1448 } else {
1449 if (ctrlmode) {
1450 WMUnmapWidget(scr->gview);
1451 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1452 } else if (!scr->selected_windows) {
1453 WMUnmapWidget(scr->gview);
1454 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1458 if (!opaqueMoveResize) {
1459 if (wwin->flags.shaded || scr->selected_windows) {
1460 if (scr->selected_windows)
1461 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1462 else
1463 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1464 } else {
1465 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1469 if (ctrlmode) {
1470 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1472 } else if (!scr->selected_windows)
1473 showPosition(wwin, src_x + off_x, src_y + off_y);
1475 if (opaqueMoveResize) {
1476 XUngrabServer(dpy);
1477 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1480 if (done) {
1481 scr->keymove_tick = 0;
1483 WMDeleteTimerWithClientData(&looper);
1485 if (!opaqueMoveResize) {/*ctrlmode=> resize */
1486 if (wwin->flags.shaded || scr->selected_windows) {
1487 if (scr->selected_windows)
1488 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1489 else
1490 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1491 } else {
1492 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1496 if (ctrlmode) {
1497 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1498 src_y + off_y + wh, 0);
1499 WMUnmapWidget(scr->gview);
1500 } else
1501 WMUnmapWidget(scr->gview);
1503 XUngrabKeyboard(dpy, CurrentTime);
1504 XUngrabPointer(dpy, CurrentTime);
1505 XUngrabServer(dpy);
1507 if (done == 2) {
1508 if (wwin->flags.shaded || scr->selected_windows) {
1509 if (!scr->selected_windows) {
1510 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1511 wWindowSynthConfigureNotify(wwin);
1512 } else {
1513 WMArrayIterator iter;
1514 WWindow *foo;
1516 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1518 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1519 wWindowSynthConfigureNotify(foo);
1522 } else {
1523 if (ww != original_w)
1524 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
1526 if (wh != original_h)
1527 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1529 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1530 wWindowSynthConfigureNotify(wwin);
1532 wWindowChangeWorkspace(wwin, scr->current_workspace);
1533 wSetFocusTo(scr, wwin);
1536 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1537 head != wGetHeadForWindow(wwin)) {
1538 wArrangeIcons(scr, True);
1541 update_saved_geometry(wwin);
1543 return 1;
1549 *----------------------------------------------------------------------
1550 * wMouseMoveWindow--
1551 * Move the named window and the other selected ones (if any),
1552 * interactively. Also shows the position of the window, if only one
1553 * window is being moved.
1554 * If the window is not on the selected window list, the selected
1555 * windows are deselected.
1556 * If shift is pressed during the operation, the position display
1557 * is changed to another type.
1559 * Returns:
1560 * True if the window was moved, False otherwise.
1562 * Side effects:
1563 * The window(s) position is changed, and the client(s) are
1564 * notified about that.
1565 * The position display configuration may be changed.
1566 *----------------------------------------------------------------------
1568 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1570 WScreen *scr = wwin->screen_ptr;
1571 XEvent event;
1572 Window root = scr->root_win;
1573 KeyCode shiftl, shiftr;
1574 Bool done = False;
1575 int started = 0;
1576 int warped = 0;
1577 /* This needs not to change while moving, else bad things can happen */
1578 int opaqueMove = wPreferences.opaque_move;
1579 MoveData moveData;
1580 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1581 ? wGetHeadForWindow(wwin)
1582 : scr->xine_info.primary_head);
1584 if (!IS_MOVABLE(wwin))
1585 return False;
1587 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1588 XSetWindowAttributes attr;
1590 attr.save_under = True;
1591 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1594 initMoveData(wwin, &moveData);
1596 moveData.mouseX = ev->xmotion.x_root;
1597 moveData.mouseY = ev->xmotion.y_root;
1599 if (!wwin->flags.selected) {
1600 /* this window is not selected, unselect others and move only wwin */
1601 wUnselectWindows(scr);
1603 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1604 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1605 while (!done) {
1606 if (warped) {
1607 int junk;
1608 Window junkw;
1610 /* XWarpPointer() doesn't seem to generate Motion events, so
1611 * we've got to simulate them */
1612 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1613 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1614 } else {
1615 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1616 | PointerMotionHintMask
1617 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1619 if (event.type == MotionNotify) {
1620 /* compress MotionNotify events */
1621 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1622 if (!checkMouseSamplingRate(&event))
1623 continue;
1626 switch (event.type) {
1627 case KeyPress:
1628 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1629 && started && !scr->selected_windows) {
1631 if (!opaqueMove) {
1632 drawFrames(wwin, scr->selected_windows,
1633 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1636 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1637 showPosition(wwin, moveData.realX, moveData.realY);
1638 XUngrabServer(dpy);
1640 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1641 moveData.winWidth, moveData.winHeight);
1643 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1644 XGrabServer(dpy);
1645 showPosition(wwin, moveData.realX, moveData.realY);
1648 if (!opaqueMove) {
1649 drawFrames(wwin, scr->selected_windows,
1650 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1652 /*} else {
1653 WMHandleEvent(&event); this causes problems needs fixing */
1655 break;
1657 case MotionNotify:
1658 if (started) {
1659 updateWindowPosition(wwin, &moveData,
1660 scr->selected_windows == NULL
1661 && wPreferences.edge_resistance > 0,
1662 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1664 if (!warped && !wPreferences.no_autowrap) {
1665 int oldWorkspace = scr->current_workspace;
1667 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1668 showPosition(wwin, moveData.realX, moveData.realY);
1669 XUngrabServer(dpy);
1671 if (!opaqueMove) {
1672 drawFrames(wwin, scr->selected_windows,
1673 moveData.realX - wwin->frame_x,
1674 moveData.realY - wwin->frame_y);
1676 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1677 if (scr->current_workspace != oldWorkspace
1678 && wPreferences.edge_resistance > 0
1679 && scr->selected_windows == NULL)
1680 updateMoveData(wwin, &moveData);
1681 warped = 1;
1683 if (!opaqueMove) {
1684 drawFrames(wwin, scr->selected_windows,
1685 moveData.realX - wwin->frame_x,
1686 moveData.realY - wwin->frame_y);
1688 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1689 XSync(dpy, False);
1690 showPosition(wwin, moveData.realX, moveData.realY);
1691 XGrabServer(dpy);
1693 } else {
1694 warped = 0;
1696 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1697 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1699 XChangeActivePointerGrab(dpy, ButtonMotionMask
1700 | ButtonReleaseMask | ButtonPressMask,
1701 wCursor[WCUR_MOVE], CurrentTime);
1702 started = 1;
1703 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1705 if (!scr->selected_windows)
1706 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1707 moveData.winWidth, moveData.winHeight);
1709 if (started && !opaqueMove)
1710 drawFrames(wwin, scr->selected_windows, 0, 0);
1712 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1713 && !scr->selected_windows)) {
1714 XGrabServer(dpy);
1715 if (wPreferences.move_display == WDIS_NEW)
1716 showPosition(wwin, moveData.realX, moveData.realY);
1719 break;
1721 case ButtonPress:
1722 break;
1724 case ButtonRelease:
1725 if (event.xbutton.button != ev->xbutton.button)
1726 break;
1728 if (started) {
1729 XEvent e;
1730 if (!opaqueMove) {
1731 drawFrames(wwin, scr->selected_windows,
1732 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1733 XSync(dpy, 0);
1734 doWindowMove(wwin, scr->selected_windows,
1735 moveData.realX - wwin->frame_x,
1736 moveData.realY - wwin->frame_y);
1738 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1739 wWindowSynthConfigureNotify(wwin);
1740 #endif
1741 XUngrabKeyboard(dpy, CurrentTime);
1742 XUngrabServer(dpy);
1743 if (!opaqueMove) {
1744 wWindowChangeWorkspace(wwin, scr->current_workspace);
1745 wSetFocusTo(scr, wwin);
1747 if (wPreferences.move_display == WDIS_NEW)
1748 showPosition(wwin, moveData.realX, moveData.realY);
1750 /* discard all enter/leave events that happened until
1751 * the time the button was released */
1752 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1753 if (e.xcrossing.time > event.xbutton.time) {
1754 XPutBackEvent(dpy, &e);
1755 break;
1758 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1759 if (e.xcrossing.time > event.xbutton.time) {
1760 XPutBackEvent(dpy, &e);
1761 break;
1765 if (!scr->selected_windows) {
1766 /* get rid of the geometry window */
1767 WMUnmapWidget(scr->gview);
1770 done = True;
1771 break;
1773 default:
1774 if (started && !opaqueMove) {
1775 drawFrames(wwin, scr->selected_windows,
1776 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1777 XUngrabServer(dpy);
1778 WMHandleEvent(&event);
1779 XSync(dpy, False);
1780 XGrabServer(dpy);
1781 drawFrames(wwin, scr->selected_windows,
1782 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1783 } else {
1784 WMHandleEvent(&event);
1786 break;
1790 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1791 XSetWindowAttributes attr;
1793 attr.save_under = False;
1794 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1798 freeMoveData(&moveData);
1800 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1801 head != wGetHeadForWindow(wwin)) {
1802 wArrangeIcons(scr, True);
1805 if (started)
1806 update_saved_geometry(wwin);
1808 return started;
1811 #define RESIZEBAR 1
1812 #define HCONSTRAIN 2
1814 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1816 int w = wwin->frame->core->width - 1;
1817 int cw = wwin->frame->resizebar_corner_width;
1818 int dir;
1820 /* if not resizing through the resizebar */
1821 if (!(flags & RESIZEBAR)) {
1822 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1823 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1824 if (abs(dx) < 2 || abs(dy) < 2) {
1825 if (abs(dy) > abs(dx))
1826 xdir = 0;
1827 else
1828 ydir = 0;
1830 return (xdir | ydir);
1833 /* window is too narrow. allow diagonal resize */
1834 if (cw * 2 >= w) {
1835 int ydir;
1837 if (flags & HCONSTRAIN)
1838 ydir = 0;
1839 else
1840 ydir = DOWN;
1841 if (x < cw)
1842 return (LEFT | ydir);
1843 else
1844 return (RIGHT | ydir);
1846 /* vertical resize */
1847 if ((x > cw) && (x < w - cw))
1848 return DOWN;
1850 if (x < cw)
1851 dir = LEFT;
1852 else
1853 dir = RIGHT;
1855 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1856 dir |= DOWN;
1858 return dir;
1861 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1863 XEvent event;
1864 WScreen *scr = wwin->screen_ptr;
1865 Window root = scr->root_win;
1866 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1867 int fw = wwin->frame->core->width;
1868 int fh = wwin->frame->core->height;
1869 int fx = wwin->frame_x;
1870 int fy = wwin->frame_y;
1871 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1872 int orig_x, orig_y;
1873 int started;
1874 int dw, dh;
1875 int rw = fw, rh = fh;
1876 int rx1, ry1, rx2, ry2;
1877 int res = 0;
1878 KeyCode shiftl, shiftr;
1879 int orig_fx = fx;
1880 int orig_fy = fy;
1881 int orig_fw = fw;
1882 int orig_fh = fh;
1883 int original_fw = fw;
1884 int original_fh = fh;
1885 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1886 ? wGetHeadForWindow(wwin)
1887 : scr->xine_info.primary_head);
1888 int opaqueResize = wPreferences.opaque_resize;
1890 if (!IS_RESIZABLE(wwin))
1891 return;
1893 if (wwin->flags.shaded) {
1894 wwarning("internal error: tryein");
1895 return;
1897 orig_x = ev->xbutton.x_root;
1898 orig_y = ev->xbutton.y_root;
1900 started = 0;
1901 wUnselectWindows(scr);
1902 rx1 = fx;
1903 rx2 = fx + fw - 1;
1904 ry1 = fy;
1905 ry2 = fy + fh - 1;
1906 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1907 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1909 while (1) {
1910 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1911 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1912 if (!checkMouseSamplingRate(&event))
1913 continue;
1915 switch (event.type) {
1916 case KeyPress:
1917 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1918 if (!opaqueResize) {
1919 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1920 && started) {
1921 drawTransparentFrame(wwin, fx, fy, fw, fh);
1922 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1923 drawTransparentFrame(wwin, fx, fy, fw, fh);
1926 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1927 break;
1929 case MotionNotify:
1930 if (started) {
1931 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1933 dw = 0;
1934 dh = 0;
1936 orig_fx = fx;
1937 orig_fy = fy;
1938 orig_fw = fw;
1939 orig_fh = fh;
1941 if (res & LEFT)
1942 dw = orig_x - event.xmotion.x_root;
1943 else if (res & RIGHT)
1944 dw = event.xmotion.x_root - orig_x;
1945 if (res & UP)
1946 dh = orig_y - event.xmotion.y_root;
1947 else if (res & DOWN)
1948 dh = event.xmotion.y_root - orig_y;
1950 orig_x = event.xmotion.x_root;
1951 orig_y = event.xmotion.y_root;
1953 rw += dw;
1954 rh += dh;
1955 fw = rw;
1956 fh = rh - vert_border;
1957 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1958 fh += vert_border;
1959 if (res & LEFT)
1960 fx = rx2 - fw + 1;
1961 else if (res & RIGHT)
1962 fx = rx1;
1963 if (res & UP)
1964 fy = ry2 - fh + 1;
1965 else if (res & DOWN)
1966 fy = ry1;
1967 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1968 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1969 int tx, ty;
1970 Window junkw;
1971 int flags;
1973 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1974 orig_x, orig_y, &tx, &ty, &junkw);
1976 /* check if resizing through resizebar */
1977 if (is_resizebar)
1978 flags = RESIZEBAR;
1979 else
1980 flags = 0;
1982 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1983 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1984 flags |= HCONSTRAIN;
1986 res = getResizeDirection(wwin, tx, ty,
1987 orig_x - event.xmotion.x_root,
1988 orig_y - event.xmotion.y_root, flags);
1990 if (res == (UP | LEFT))
1991 XChangeActivePointerGrab(dpy, ButtonMotionMask
1992 | ButtonReleaseMask | ButtonPressMask,
1993 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1994 else if (res == (UP | RIGHT))
1995 XChangeActivePointerGrab(dpy, ButtonMotionMask
1996 | ButtonReleaseMask | ButtonPressMask,
1997 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1998 else if (res == (DOWN | LEFT))
1999 XChangeActivePointerGrab(dpy, ButtonMotionMask
2000 | ButtonReleaseMask | ButtonPressMask,
2001 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2002 else if (res == (DOWN | RIGHT))
2003 XChangeActivePointerGrab(dpy, ButtonMotionMask
2004 | ButtonReleaseMask | ButtonPressMask,
2005 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2006 else if (res == DOWN || res == UP)
2007 XChangeActivePointerGrab(dpy, ButtonMotionMask
2008 | ButtonReleaseMask | ButtonPressMask,
2009 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2010 else if (res & (DOWN | UP))
2011 XChangeActivePointerGrab(dpy, ButtonMotionMask
2012 | ButtonReleaseMask | ButtonPressMask,
2013 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2014 else if (res & (LEFT | RIGHT))
2015 XChangeActivePointerGrab(dpy, ButtonMotionMask
2016 | ButtonReleaseMask | ButtonPressMask,
2017 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2019 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2021 XGrabServer(dpy);
2023 /* Draw the resize frame for the first time. */
2024 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2026 if (!opaqueResize)
2027 drawTransparentFrame(wwin, fx, fy, fw, fh);
2029 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2031 started = 1;
2033 if (started) {
2034 if (!opaqueResize)
2035 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2037 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2038 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2040 if (!opaqueResize)
2041 drawTransparentFrame(wwin, fx, fy, fw, fh);
2043 if (fh != orig_fh || fw != orig_fw) {
2044 if (wPreferences.size_display == WDIS_NEW)
2045 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2046 orig_fy + orig_fh, res);
2048 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2051 if (opaqueResize) {
2052 /* Fist clean the geometry line */
2053 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2054 /* Now, continue drawing */
2055 XUngrabServer(dpy);
2056 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2057 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2058 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2061 break;
2063 case ButtonPress:
2064 break;
2066 case ButtonRelease:
2067 if (event.xbutton.button != ev->xbutton.button)
2068 break;
2070 if (started) {
2071 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2073 if (!opaqueResize)
2074 drawTransparentFrame(wwin, fx, fy, fw, fh);
2076 XUngrabKeyboard(dpy, CurrentTime);
2077 WMUnmapWidget(scr->gview);
2078 XUngrabServer(dpy);
2080 if (fw != original_fw)
2081 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
2083 if (fh != original_fh)
2084 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2086 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2087 wWindowSynthConfigureNotify(wwin);
2089 return;
2091 default:
2092 WMHandleEvent(&event);
2096 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2097 wArrangeIcons(scr, True);
2100 #undef LEFT
2101 #undef RIGHT
2102 #undef HORIZONTAL
2103 #undef UP
2104 #undef DOWN
2105 #undef VERTICAL
2106 #undef HCONSTRAIN
2107 #undef RESIZEBAR
2109 void wUnselectWindows(WScreen * scr)
2111 WWindow *wwin;
2113 if (!scr->selected_windows)
2114 return;
2116 while (WMGetArrayItemCount(scr->selected_windows)) {
2117 wwin = WMGetFromArray(scr->selected_windows, 0);
2118 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2119 wIconSelect(wwin->icon);
2121 wSelectWindow(wwin, False);
2123 WMFreeArray(scr->selected_windows);
2124 scr->selected_windows = NULL;
2127 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2129 WWindow *tmpw;
2131 /* select the windows and put them in the selected window list */
2132 tmpw = scr->focused_window;
2133 while (tmpw != NULL) {
2134 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2135 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2136 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2137 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2138 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2139 wSelectWindow(tmpw, True);
2142 tmpw = tmpw->prev;
2146 void wSelectWindows(WScreen * scr, XEvent * ev)
2148 XEvent event;
2149 Window root = scr->root_win;
2150 GC gc = scr->frame_gc;
2151 int xp = ev->xbutton.x_root;
2152 int yp = ev->xbutton.y_root;
2153 int w = 0, h = 0;
2154 int x = xp, y = yp;
2156 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2157 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2158 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2159 return;
2161 XGrabServer(dpy);
2163 wUnselectWindows(scr);
2165 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2166 while (1) {
2167 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2169 switch (event.type) {
2170 case MotionNotify:
2171 XDrawRectangle(dpy, root, gc, x, y, w, h);
2172 x = event.xmotion.x_root;
2173 if (x < xp) {
2174 w = xp - x;
2175 } else {
2176 w = x - xp;
2177 x = xp;
2179 y = event.xmotion.y_root;
2180 if (y < yp) {
2181 h = yp - y;
2182 } else {
2183 h = y - yp;
2184 y = yp;
2186 XDrawRectangle(dpy, root, gc, x, y, w, h);
2187 break;
2189 case ButtonPress:
2190 break;
2192 case ButtonRelease:
2193 if (event.xbutton.button != ev->xbutton.button)
2194 break;
2196 XDrawRectangle(dpy, root, gc, x, y, w, h);
2197 XUngrabServer(dpy);
2198 XUngrabPointer(dpy, CurrentTime);
2199 selectWindowsInside(scr, x, y, x + w, y + h);
2200 return;
2202 default:
2203 WMHandleEvent(&event);
2204 break;
2209 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2211 WScreen *scr = wwin->screen_ptr;
2212 Window root = scr->root_win;
2213 int x, y, h = 0;
2214 XEvent event;
2215 KeyCode shiftl, shiftr;
2216 Window junkw;
2217 int junk;
2219 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2220 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2221 *x_ret = 0;
2222 *y_ret = 0;
2223 return;
2225 if (HAS_TITLEBAR(wwin)) {
2226 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2227 TITLEBAR_EXTEND_SPACE) * 2;
2229 if (h > wPreferences.window_title_max_height)
2230 h = wPreferences.window_title_max_height;
2232 if (h < wPreferences.window_title_min_height)
2233 h = wPreferences.window_title_min_height;
2235 height += h;
2237 if (HAS_RESIZEBAR(wwin)) {
2238 height += RESIZEBAR_HEIGHT;
2240 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2241 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2242 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2244 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2246 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2247 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2248 while (1) {
2249 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2251 if (!checkMouseSamplingRate(&event))
2252 continue;
2254 switch (event.type) {
2255 case KeyPress:
2256 if ((event.xkey.keycode == shiftl)
2257 || (event.xkey.keycode == shiftr)) {
2258 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2259 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2260 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2262 break;
2264 case MotionNotify:
2265 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2267 x = event.xmotion.x_root;
2268 y = event.xmotion.y_root;
2270 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2271 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2273 showPosition(wwin, x - width / 2, y - h / 2);
2275 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2277 break;
2279 case ButtonPress:
2280 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2281 XSync(dpy, 0);
2282 *x_ret = x - width / 2;
2283 *y_ret = y - h / 2;
2284 XUngrabPointer(dpy, CurrentTime);
2285 XUngrabKeyboard(dpy, CurrentTime);
2286 /* get rid of the geometry window */
2287 WMUnmapWidget(scr->gview);
2288 return;
2290 default:
2291 WMHandleEvent(&event);
2292 break;