wmaker: Add more directions for window snapping.
[wmaker-crm.git] / src / moveres.c
blob27f6ce1d9f4cb15b6180a6f8ec989e97cf8352e0
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 "actions.h"
38 #include "workspace.h"
39 #include "placement.h"
41 #include "geomview.h"
42 #include "screen.h"
43 #include "xinerama.h"
45 #include <WINGs/WINGsP.h>
47 /* How many different types of geometry/position
48 display thingies are there? */
49 #define NUM_DISPLAYS 5
51 #define LEFT 1
52 #define RIGHT 2
53 #define HORIZONTAL (LEFT|RIGHT)
54 #define UP 4
55 #define DOWN 8
56 #define VERTICAL (UP|DOWN)
58 /* True if window currently has a border. This also includes borderless
59 * windows which are currently selected
61 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
65 *----------------------------------------------------------------------
66 * checkMouseSamplingRate-
67 * For lowering the mouse motion sampling rate for machines where
68 * it's too high (SGIs). If it returns False then the event should be
69 * ignored.
70 *----------------------------------------------------------------------
72 static Bool checkMouseSamplingRate(XEvent * ev)
74 static Time previousMotion = 0;
76 if (ev->type == MotionNotify) {
77 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
78 return False;
79 } else {
80 previousMotion = ev->xmotion.time;
83 return True;
87 *----------------------------------------------------------------------
88 * moveGeometryDisplayCentered
90 * routine that moves the geometry/position window on scr so it is
91 * centered over the given coordinates (x,y). Also the window position
92 * is clamped so it stays on the screen at all times.
93 *----------------------------------------------------------------------
95 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
97 unsigned int w = WMWidgetWidth(scr->gview);
98 unsigned int h = WMWidgetHeight(scr->gview);
99 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
101 x -= w / 2;
102 y -= h / 2;
104 /* dead area check */
105 if (scr->xine_info.count) {
106 WMRect rect;
107 int head, flags;
109 rect.pos.x = x;
110 rect.pos.y = y;
111 rect.size.width = w;
112 rect.size.height = h;
114 head = wGetRectPlacementInfo(scr, rect, &flags);
116 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
117 rect = wGetRectForHead(scr, head);
118 x1 = rect.pos.x;
119 y1 = rect.pos.y;
120 x2 = x1 + rect.size.width;
121 y2 = y1 + rect.size.height;
125 if (x < x1 + 1)
126 x = x1 + 1;
127 else if (x > (x2 - w))
128 x = x2 - w;
130 if (y < y1 + 1)
131 y = y1 + 1;
132 else if (y > (y2 - h))
133 y = y2 - h;
135 WMMoveWidget(scr->gview, x, y);
138 static void showPosition(WWindow * wwin, int x, int y)
140 WScreen *scr = wwin->screen_ptr;
142 if (wPreferences.move_display == WDIS_NEW) {
143 #if 0
144 int width = wwin->frame->core->width;
145 int height = wwin->frame->core->height;
147 GC lgc = scr->line_gc;
148 XSetForeground(dpy, lgc, scr->line_pixel);
149 sprintf(num, "%i", x);
151 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
152 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
153 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
154 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
155 #endif
156 } else {
157 WSetGeometryViewShownPosition(scr->gview, x, y);
161 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
163 WScreen *scr = wwin->screen_ptr;
164 WMRect rect;
166 wPreferences.move_display++;
167 wPreferences.move_display %= NUM_DISPLAYS;
169 if (wPreferences.move_display == WDIS_NEW) {
170 wPreferences.move_display++;
171 wPreferences.move_display %= NUM_DISPLAYS;
174 if (wPreferences.move_display == WDIS_NONE) {
175 WMUnmapWidget(scr->gview);
176 } else {
177 if (wPreferences.move_display == WDIS_CENTER) {
178 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
179 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
180 rect.pos.y + rect.size.height / 2);
181 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
182 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
183 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
184 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
185 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
187 WMMapWidget(scr->gview);
191 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
193 WScreen *scr = wwin->screen_ptr;
194 WMRect rect;
196 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
197 return;
198 } else if (wPreferences.move_display == WDIS_CENTER) {
199 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
200 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
201 rect.pos.y + rect.size.height / 2);
202 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
203 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
204 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
205 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
206 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
208 WMMapWidget(scr->gview);
209 WSetGeometryViewShownPosition(scr->gview, x, y);
212 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
214 WScreen *scr = wwin->screen_ptr;
215 Window root = scr->root_win;
216 GC gc = scr->line_gc;
217 int ty, by, my, x, y, mx, s;
218 char num[16];
219 XSegment segment[4];
220 int fw, fh;
222 /* This seems necessary for some odd reason (too lazy to write x1-1 and
223 * x2-1 everywhere below in the code). But why only for x? */
224 x1--;
225 x2--;
227 if (HAS_BORDER_WITH_SELECT(wwin)) {
228 x1 += scr->frame_border_width;
229 x2 += scr->frame_border_width;
230 y1 += scr->frame_border_width;
231 y2 += scr->frame_border_width;
234 ty = y1 + wwin->frame->top_width;
235 by = y2 - wwin->frame->bottom_width;
237 if (wPreferences.size_display == WDIS_NEW) {
238 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
239 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
241 XSetForeground(dpy, gc, scr->line_pixel);
243 /* vertical geometry */
244 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
245 x = x2;
246 s = -15;
247 } else {
248 x = x1;
249 s = 15;
251 my = (ty + by) / 2;
253 /* top arrow & end bar */
254 segment[0].x1 = x - (s + 6);
255 segment[0].y1 = ty;
256 segment[0].x2 = x - (s - 10);
257 segment[0].y2 = ty;
259 /* arrowhead */
260 segment[1].x1 = x - (s - 2);
261 segment[1].y1 = ty + 1;
262 segment[1].x2 = x - (s - 5);
263 segment[1].y2 = ty + 7;
265 segment[2].x1 = x - (s - 2);
266 segment[2].y1 = ty + 1;
267 segment[2].x2 = x - (s + 1);
268 segment[2].y2 = ty + 7;
270 /* line */
271 segment[3].x1 = x - (s - 2);
272 segment[3].y1 = ty + 1;
273 segment[3].x2 = x - (s - 2);
274 segment[3].y2 = my - fh / 2 - 1;
276 XDrawSegments(dpy, root, gc, segment, 4);
278 /* bottom arrow & end bar */
279 segment[0].y1 = by;
280 segment[0].y2 = by;
282 /* arrowhead */
283 segment[1].y1 = by - 1;
284 segment[1].y2 = by - 7;
286 segment[2].y1 = by - 1;
287 segment[2].y2 = by - 7;
289 /* line */
290 segment[3].y1 = my + fh / 2 + 2;
291 segment[3].y2 = by - 1;
293 XDrawSegments(dpy, root, gc, segment, 4);
295 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
296 wwin->normal_hints->height_inc);
297 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
299 /* Display the height. */
300 XSetFont(dpy, gc, scr->tech_draw_font->fid);
301 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
302 strlen(num));
304 /* horizontal geometry */
305 if (y1 < 15) {
306 y = y2;
307 s = -15;
308 } else {
309 y = y1;
310 s = 15;
312 mx = x1 + (x2 - x1) / 2;
313 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
314 wwin->normal_hints->width_inc);
315 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
317 /* left arrow & end bar */
318 segment[0].x1 = x1;
319 segment[0].y1 = y - (s + 6);
320 segment[0].x2 = x1;
321 segment[0].y2 = y - (s - 10);
323 /* arrowhead */
324 segment[1].x1 = x1 + 7;
325 segment[1].y1 = y - (s + 1);
326 segment[1].x2 = x1 + 1;
327 segment[1].y2 = y - (s - 2);
329 segment[2].x1 = x1 + 1;
330 segment[2].y1 = y - (s - 2);
331 segment[2].x2 = x1 + 7;
332 segment[2].y2 = y - (s - 5);
334 /* line */
335 segment[3].x1 = x1 + 1;
336 segment[3].y1 = y - (s - 2);
337 segment[3].x2 = mx - fw / 2 - 2;
338 segment[3].y2 = y - (s - 2);
340 XDrawSegments(dpy, root, gc, segment, 4);
342 /* right arrow & end bar */
343 segment[0].x1 = x2 + 1;
344 segment[0].x2 = x2 + 1;
346 /* arrowhead */
347 segment[1].x1 = x2 - 6;
348 segment[1].x2 = x2;
350 segment[2].x1 = x2;
351 segment[2].x2 = x2 - 6;
353 /* line */
354 segment[3].x1 = mx + fw / 2 + 2;
355 segment[3].x2 = x2;
357 XDrawSegments(dpy, root, gc, segment, 4);
359 /* Display the width. */
360 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
361 strlen(num));
362 } else {
363 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
364 / wwin->normal_hints->width_inc,
365 (by - ty - wwin->normal_hints->base_height)
366 / wwin->normal_hints->height_inc);
370 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
372 WScreen *scr = wwin->screen_ptr;
373 WMRect rect;
375 wPreferences.size_display++;
376 wPreferences.size_display %= NUM_DISPLAYS;
378 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
379 WMUnmapWidget(scr->gview);
380 } else {
381 if (wPreferences.size_display == WDIS_CENTER) {
382 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
383 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
384 rect.pos.y + rect.size.height / 2);
385 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
386 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
387 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
388 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
389 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
391 WMMapWidget(scr->gview);
392 showGeometry(wwin, x, y, x + w, y + h, dir);
396 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
398 WScreen *scr = wwin->screen_ptr;
399 WMRect rect;
401 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
402 return;
404 if (wPreferences.size_display == WDIS_CENTER) {
405 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
406 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
407 rect.pos.y + rect.size.height / 2);
408 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
409 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
410 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
411 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
412 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
414 WMMapWidget(scr->gview);
415 showGeometry(wwin, x, y, x + w, y + h, 0);
418 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
420 WWindow *tmpw;
421 WScreen *scr = wwin->screen_ptr;
422 int x, y;
424 if (!array || !WMGetArrayItemCount(array)) {
425 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
426 } else {
427 WMArrayIterator iter;
429 WM_ITERATE_ARRAY(array, tmpw, iter) {
430 x = tmpw->frame_x + dx;
431 y = tmpw->frame_y + dy;
433 #if 1 /* XXX: with xinerama patch was #if 0, check this */
434 /* don't let windows become unreachable */
436 if (x + (int)tmpw->frame->core->width < 20)
437 x = 20 - (int)tmpw->frame->core->width;
438 else if (x + 20 > scr->scr_width)
439 x = scr->scr_width - 20;
441 if (y + (int)tmpw->frame->core->height < 20)
442 y = 20 - (int)tmpw->frame->core->height;
443 else if (y + 20 > scr->scr_height)
444 y = scr->scr_height - 20;
445 #else
446 wScreenBringInside(scr, &x, &y,
447 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
448 #endif
450 wWindowMove(tmpw, x, y);
455 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
457 Window root = wwin->screen_ptr->root_win;
458 GC gc = wwin->screen_ptr->frame_gc;
459 int h = 0;
460 int bottom = 0;
462 if (HAS_BORDER_WITH_SELECT(wwin)) {
463 x += wwin->screen_ptr->frame_border_width;
464 y += wwin->screen_ptr->frame_border_width;
467 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
468 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
469 TITLEBAR_EXTEND_SPACE) * 2;
471 if (h > wPreferences.window_title_max_height)
472 h = wPreferences.window_title_max_height;
474 if (h < wPreferences.window_title_min_height)
475 h = wPreferences.window_title_min_height;
477 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
478 /* Can't use wwin-frame->bottom_width because, in some cases
479 (e.g. interactive placement), frame does not point to anything. */
480 bottom = RESIZEBAR_HEIGHT;
482 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
484 if (h > 0) {
485 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
487 if (bottom > 0) {
488 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
492 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
494 WWindow *tmpw;
495 int scr_width = wwin->screen_ptr->scr_width;
496 int scr_height = wwin->screen_ptr->scr_height;
497 int x, y;
499 if (!array) {
501 x = wwin->frame_x + dx;
502 y = wwin->frame_y + dy;
504 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
506 } else {
507 WMArrayIterator iter;
509 WM_ITERATE_ARRAY(array, tmpw, iter) {
510 x = tmpw->frame_x + dx;
511 y = tmpw->frame_y + dy;
513 /* don't let windows become unreachable */
514 #if 1 /* XXX: was 0 in XINERAMA patch, check */
515 if (x + (int)tmpw->frame->core->width < 20)
516 x = 20 - (int)tmpw->frame->core->width;
517 else if (x + 20 > scr_width)
518 x = scr_width - 20;
520 if (y + (int)tmpw->frame->core->height < 20)
521 y = 20 - (int)tmpw->frame->core->height;
522 else if (y + 20 > scr_height)
523 y = scr_height - 20;
525 #else
526 wScreenBringInside(wwin->screen_ptr, &x, &y,
527 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
528 #endif
530 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
535 static void flushMotion(void)
537 XEvent ev;
539 XSync(dpy, False);
540 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
543 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
545 /* do not let window be unmapped */
546 if (opaque_move) {
547 wwin->flags.changing_workspace = 1;
548 wWindowChangeWorkspace(wwin, new_workspace);
550 /* go to new workspace */
551 wWorkspaceChange(scr, new_workspace);
553 wwin->flags.changing_workspace = 0;
555 if (rewind)
556 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
557 else
558 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
560 flushMotion();
562 if (!opaque_move) {
563 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
564 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
565 GrabModeAsync, None, wPreferences.cursor[WCUR_MOVE], CurrentTime);
569 typedef struct {
570 /* arrays of WWindows sorted by the respective border position */
571 WWindow **topList; /* top border */
572 WWindow **leftList; /* left border */
573 WWindow **rightList; /* right border */
574 WWindow **bottomList; /* bottom border */
575 int count;
577 /* index of window in the above lists indicating the relative position
578 * of the window with the others */
579 int topIndex;
580 int leftIndex;
581 int rightIndex;
582 int bottomIndex;
584 int rubCount; /* for workspace switching */
586 int winWidth, winHeight; /* width/height of the window */
587 int realX, realY; /* actual position of the window */
588 int calcX, calcY; /* calculated position of window */
589 int omouseX, omouseY; /* old mouse position */
590 int mouseX, mouseY; /* last known position of the pointer */
592 enum {
593 SNAP_NONE,
594 SNAP_LEFT,
595 SNAP_RIGHT,
596 SNAP_TOP,
597 SNAP_BOTTOM,
598 SNAP_TOPLEFT,
599 SNAP_TOPRIGHT,
600 SNAP_BOTTOMLEFT,
601 SNAP_BOTTOMRIGHT
602 } snap;
603 } MoveData;
605 #define WTOP(w) (w)->frame_y
606 #define WLEFT(w) (w)->frame_x
607 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
608 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
609 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
610 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
612 static int compareWTop(const void *a, const void *b)
614 WWindow *wwin1 = *(WWindow **) a;
615 WWindow *wwin2 = *(WWindow **) b;
617 if (WTOP(wwin1) > WTOP(wwin2))
618 return -1;
619 else if (WTOP(wwin1) < WTOP(wwin2))
620 return 1;
621 else
622 return 0;
625 static int compareWLeft(const void *a, const void *b)
627 WWindow *wwin1 = *(WWindow **) a;
628 WWindow *wwin2 = *(WWindow **) b;
630 if (WLEFT(wwin1) > WLEFT(wwin2))
631 return -1;
632 else if (WLEFT(wwin1) < WLEFT(wwin2))
633 return 1;
634 else
635 return 0;
638 static int compareWRight(const void *a, const void *b)
640 WWindow *wwin1 = *(WWindow **) a;
641 WWindow *wwin2 = *(WWindow **) b;
643 if (WRIGHT(wwin1) < WRIGHT(wwin2))
644 return -1;
645 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
646 return 1;
647 else
648 return 0;
651 static int compareWBottom(const void *a, const void *b)
653 WWindow *wwin1 = *(WWindow **) a;
654 WWindow *wwin2 = *(WWindow **) b;
656 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
657 return -1;
658 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
659 return 1;
660 else
661 return 0;
664 static void updateResistance(MoveData *data, int newX, int newY)
666 int i;
667 int newX2 = newX + data->winWidth;
668 int newY2 = newY + data->winHeight;
669 Bool ok = False;
671 if (newX < data->realX) {
672 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
673 ok = True;
674 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
675 ok = True;
677 } else if (newX > data->realX) {
678 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
679 ok = True;
680 } else if (data->rightIndex <= data->count - 1
681 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
682 ok = True;
686 if (!ok) {
687 if (newY < data->realY) {
688 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
689 ok = True;
690 } else if (data->topIndex <= data->count - 1
691 && newY2 <= WTOP(data->topList[data->topIndex])) {
692 ok = True;
694 } else if (newY > data->realY) {
695 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
696 ok = True;
697 } else if (data->bottomIndex <= data->count - 1
698 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
699 ok = True;
704 if (!ok)
705 return;
707 /* TODO: optimize this */
708 if (data->realY < WBOTTOM(data->bottomList[0])) {
709 data->bottomIndex = 0;
711 if (data->realX < WRIGHT(data->rightList[0])) {
712 data->rightIndex = 0;
714 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
715 data->leftIndex = 0;
717 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
718 data->topIndex = 0;
720 for (i = 0; i < data->count; i++) {
721 if (data->realY > WBOTTOM(data->bottomList[i])) {
722 data->bottomIndex = i + 1;
724 if (data->realX > WRIGHT(data->rightList[i])) {
725 data->rightIndex = i + 1;
727 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
728 data->leftIndex = i + 1;
730 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
731 data->topIndex = i + 1;
736 static void freeMoveData(MoveData * data)
738 if (data->topList)
739 wfree(data->topList);
740 if (data->leftList)
741 wfree(data->leftList);
742 if (data->rightList)
743 wfree(data->rightList);
744 if (data->bottomList)
745 wfree(data->bottomList);
748 static void updateMoveData(WWindow * wwin, MoveData * data)
750 WScreen *scr = wwin->screen_ptr;
751 WWindow *tmp;
752 int i;
754 data->count = 0;
755 tmp = scr->focused_window;
756 while (tmp) {
757 if (tmp != wwin && w_global.workspace.current == tmp->frame->workspace
758 && !tmp->flags.miniaturized
759 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
760 data->topList[data->count] = tmp;
761 data->leftList[data->count] = tmp;
762 data->rightList[data->count] = tmp;
763 data->bottomList[data->count] = tmp;
764 data->count++;
766 tmp = tmp->prev;
769 if (data->count == 0) {
770 data->topIndex = 0;
771 data->leftIndex = 0;
772 data->rightIndex = 0;
773 data->bottomIndex = 0;
774 return;
777 /* order from closest to the border of the screen to farthest */
779 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
780 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
781 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
782 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
784 /* figure the position of the window relative to the others */
786 data->topIndex = -1;
787 data->leftIndex = -1;
788 data->rightIndex = -1;
789 data->bottomIndex = -1;
791 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
792 data->bottomIndex = 0;
794 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
795 data->rightIndex = 0;
797 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
798 data->leftIndex = 0;
800 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
801 data->topIndex = 0;
803 for (i = 0; i < data->count; i++) {
804 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
805 data->bottomIndex = i + 1;
807 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
808 data->rightIndex = i + 1;
810 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
811 data->leftIndex = i + 1;
813 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
814 data->topIndex = i + 1;
819 static void initMoveData(WWindow * wwin, MoveData * data)
821 int i;
822 WWindow *tmp;
824 memset(data, 0, sizeof(MoveData));
826 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
828 if (i > 1) {
829 data->topList = wmalloc(sizeof(WWindow *) * i);
830 data->leftList = wmalloc(sizeof(WWindow *) * i);
831 data->rightList = wmalloc(sizeof(WWindow *) * i);
832 data->bottomList = wmalloc(sizeof(WWindow *) * i);
834 updateMoveData(wwin, data);
837 data->realX = wwin->frame_x;
838 data->realY = wwin->frame_y;
839 data->calcX = wwin->frame_x;
840 data->calcY = wwin->frame_y;
842 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
843 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
845 data->snap = SNAP_NONE;
848 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
850 WScreen *scr = wwin->screen_ptr;
851 Bool changed = False;
853 if (data->mouseX <= 1) {
854 if (w_global.workspace.current > 0) {
855 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current - 1, True);
856 changed = True;
857 data->rubCount = 0;
858 } else if (w_global.workspace.current == 0 && wPreferences.ws_cycle) {
859 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.count - 1, True);
860 changed = True;
861 data->rubCount = 0;
863 } else if (data->mouseX >= scr->scr_width - 2) {
864 if (w_global.workspace.current == w_global.workspace.count - 1) {
865 if (wPreferences.ws_cycle || w_global.workspace.count == MAX_WORKSPACES) {
866 crossWorkspace(scr, wwin, opaqueMove, 0, False);
867 changed = True;
868 data->rubCount = 0;
870 /* if user insists on trying to go to next workspace even when
871 * it's already the last, create a new one */
872 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
874 /* detect user "rubbing" the window against the edge */
875 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
877 data->rubCount = -(data->rubCount + 1);
879 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
881 data->rubCount = -data->rubCount + 1;
884 /* create a new workspace */
885 if (abs(data->rubCount) > 2) {
886 /* go to next workspace */
887 wWorkspaceNew(scr);
889 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current + 1, False);
890 changed = True;
891 data->rubCount = 0;
893 } else if (w_global.workspace.current < w_global.workspace.count) {
894 /* go to next workspace */
895 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current + 1, False);
896 changed = True;
897 data->rubCount = 0;
899 } else {
900 data->rubCount = 0;
903 return changed;
906 static void
907 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
908 Bool opaqueMove, int newMouseX, int newMouseY)
910 WScreen *scr = wwin->screen_ptr;
911 int dx, dy; /* how much mouse moved */
912 int winL, winR, winT, winB; /* requested new window position */
913 int newX, newY; /* actual new window position */
914 Bool hresist, vresist;
915 Bool attract;
917 hresist = False;
918 vresist = False;
920 /* check the direction of the movement */
921 dx = newMouseX - data->mouseX;
922 dy = newMouseY - data->mouseY;
924 data->omouseX = data->mouseX;
925 data->omouseY = data->mouseY;
926 data->mouseX = newMouseX;
927 data->mouseY = newMouseY;
929 winL = data->calcX + dx;
930 winR = data->calcX + data->winWidth + dx;
931 winT = data->calcY + dy;
932 winB = data->calcY + data->winHeight + dy;
934 newX = data->realX;
935 newY = data->realY;
937 if (doResistance) {
938 int l_edge, r_edge;
939 int edge_l, edge_r;
940 int t_edge, b_edge;
941 int edge_t, edge_b;
942 int resist;
944 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
945 attract = wPreferences.attract;
946 /* horizontal movement: check horizontal edge resistances */
947 if (dx || dy) {
948 WMRect rect;
949 int i, head;
950 /* window is the leftmost window: check against screen edge */
952 /* Add inter head resistance 1/2 (if needed) */
953 head = wGetHeadForPointerLocation(scr);
954 rect = wGetRectForHead(scr, head);
956 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
957 edge_l = l_edge - resist;
958 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
959 r_edge = edge_r + resist;
961 /* 1 */
962 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
963 WWindow *looprw;
965 for (i = data->rightIndex - 1; i >= 0; i--) {
966 looprw = data->rightList[i];
967 if (!(data->realY > WBOTTOM(looprw)
968 || (data->realY + data->winHeight) < WTOP(looprw))) {
969 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
970 l_edge = WRIGHT(looprw) + 1;
971 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
973 break;
977 if (attract) {
978 for (i = data->rightIndex; i < data->count; i++) {
979 looprw = data->rightList[i];
980 if (!(data->realY > WBOTTOM(looprw)
981 || (data->realY + data->winHeight) < WTOP(looprw))) {
982 r_edge = WRIGHT(looprw) + 1;
983 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
984 break;
990 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
991 WWindow *looprw;
993 for (i = data->leftIndex - 1; i >= 0; i--) {
994 looprw = data->leftList[i];
995 if (!(data->realY > WBOTTOM(looprw)
996 || (data->realY + data->winHeight) < WTOP(looprw))) {
997 if (attract
998 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
999 && dx > 0)) {
1000 edge_r = WLEFT(looprw);
1001 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1003 break;
1007 if (attract)
1008 for (i = data->leftIndex; i < data->count; i++) {
1009 looprw = data->leftList[i];
1010 if (!(data->realY > WBOTTOM(looprw)
1011 || (data->realY + data->winHeight) < WTOP(looprw))) {
1012 edge_l = WLEFT(looprw);
1013 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1014 break;
1020 printf("%d %d\n",winL,winR);
1021 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1024 if ((winL - l_edge) < (r_edge - winL)) {
1025 if (resist > 0) {
1026 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1027 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1028 newX = l_edge;
1029 hresist = True;
1032 } else {
1033 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1034 newX = r_edge;
1035 hresist = True;
1039 if ((winR - edge_l) < (edge_r - winR)) {
1040 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1041 newX = edge_l - data->winWidth;
1042 hresist = True;
1044 } else {
1045 if (resist > 0) {
1046 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1047 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1048 newX = edge_r - data->winWidth;
1049 hresist = True;
1054 /* VeRT */
1055 /* Add inter head resistance 2/2 (if needed) */
1056 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1057 edge_t = t_edge - resist;
1058 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1059 b_edge = edge_b + resist;
1061 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1062 WWindow *looprw;
1064 for (i = data->bottomIndex - 1; i >= 0; i--) {
1065 looprw = data->bottomList[i];
1066 if (!(data->realX > WRIGHT(looprw)
1067 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1068 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1069 t_edge = WBOTTOM(looprw) + 1;
1070 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1072 break;
1076 if (attract) {
1077 for (i = data->bottomIndex; i < data->count; i++) {
1078 looprw = data->bottomList[i];
1079 if (!(data->realX > WRIGHT(looprw)
1080 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1081 b_edge = WBOTTOM(looprw) + 1;
1082 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1083 break;
1089 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1090 WWindow *looprw;
1092 for (i = data->topIndex - 1; i >= 0; i--) {
1093 looprw = data->topList[i];
1094 if (!(data->realX > WRIGHT(looprw)
1095 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1096 if (attract
1097 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1098 && dy > 0)) {
1099 edge_b = WTOP(looprw);
1100 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1102 break;
1106 if (attract)
1107 for (i = data->topIndex; i < data->count; i++) {
1108 looprw = data->topList[i];
1109 if (!(data->realX > WRIGHT(looprw)
1110 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1111 edge_t = WTOP(looprw);
1112 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1113 break;
1118 if ((winT - t_edge) < (b_edge - winT)) {
1119 if (resist > 0) {
1120 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1121 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1122 newY = t_edge;
1123 vresist = True;
1126 } else {
1127 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1128 newY = b_edge;
1129 vresist = True;
1133 if ((winB - edge_t) < (edge_b - winB)) {
1134 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1135 newY = edge_t - data->winHeight;
1136 vresist = True;
1138 } else {
1139 if (resist > 0) {
1140 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1141 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1142 newY = edge_b - data->winHeight;
1143 vresist = True;
1148 /* END VeRT */
1152 /* update window position */
1153 data->calcX += dx;
1154 data->calcY += dy;
1156 if (((dx > 0 && data->calcX - data->realX > 0)
1157 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1158 newX = data->calcX;
1160 if (((dy > 0 && data->calcY - data->realY > 0)
1161 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1162 newY = data->calcY;
1164 if (data->realX != newX || data->realY != newY) {
1166 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1167 showPosition(wwin, data->realX, data->realY);
1169 if (opaqueMove) {
1170 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1171 } else {
1172 /* erase frames */
1173 drawFrames(wwin, scr->selected_windows,
1174 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1177 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1179 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1182 if (!opaqueMove) {
1183 /* draw frames */
1184 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1187 if (!scr->selected_windows) {
1188 showPosition(wwin, newX, newY);
1192 /* recalc relative window position */
1193 if (doResistance && (data->realX != newX || data->realY != newY)) {
1194 updateResistance(data, newX, newY);
1197 data->realX = newX;
1198 data->realY = newY;
1201 static void draw_snap_frame(WWindow *wwin, int direction)
1203 WScreen *scr;
1205 scr = wwin->screen_ptr;
1207 switch (direction) {
1208 case SNAP_LEFT:
1209 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height);
1210 break;
1212 case SNAP_RIGHT:
1213 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2, scr->scr_height);
1214 break;
1216 case SNAP_TOP:
1217 drawTransparentFrame(wwin, 0, 0, scr->scr_width, scr->scr_height/2);
1218 break;
1220 case SNAP_BOTTOM:
1221 drawTransparentFrame(wwin, 0, scr->scr_height/2, scr->scr_width, scr->scr_height/2);
1222 break;
1224 case SNAP_TOPLEFT:
1225 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height/2);
1226 break;
1228 case SNAP_TOPRIGHT:
1229 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2, scr->scr_height/2);
1230 break;
1232 case SNAP_BOTTOMLEFT:
1233 drawTransparentFrame(wwin, 0, scr->scr_height/2, scr->scr_width/2, scr->scr_height/2);
1234 break;
1236 case SNAP_BOTTOMRIGHT:
1237 drawTransparentFrame(wwin, scr->scr_width/2, scr->scr_height/2,
1238 scr->scr_width/2, scr->scr_height/2);
1239 break;
1243 static int get_snap_direction(WScreen *scr, int x, int y)
1245 if (x < 1) {
1246 if (y < 1)
1247 return SNAP_TOPLEFT;
1248 if (y > scr->scr_height - 2)
1249 return SNAP_BOTTOMLEFT;
1250 return SNAP_LEFT;
1252 if (x > scr->scr_width - 2) {
1253 if (y < 1)
1254 return SNAP_TOPRIGHT;
1255 if (y > scr->scr_height - 2)
1256 return SNAP_BOTTOMRIGHT;
1257 return SNAP_RIGHT;
1259 if (y < 1)
1260 return SNAP_TOP;
1261 if (y > scr->scr_height - 2)
1262 return SNAP_BOTTOM;
1263 return SNAP_NONE;
1266 static void do_snap(WWindow *wwin, MoveData *data, Bool opaqueMove)
1268 int directions;
1269 WScreen *scr;
1271 directions = 0;
1272 scr = wwin->screen_ptr;
1274 /* erase frames */
1275 if (!opaqueMove)
1276 drawFrames(wwin, scr->selected_windows, data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1277 draw_snap_frame(wwin, data->snap);
1279 switch (data->snap) {
1280 case SNAP_NONE:
1281 return;
1282 case SNAP_LEFT:
1283 directions = MAX_VERTICAL | MAX_LEFTHALF;
1284 break;
1285 case SNAP_RIGHT:
1286 directions = MAX_VERTICAL | MAX_RIGHTHALF;
1287 break;
1288 case SNAP_TOP:
1289 directions = MAX_HORIZONTAL | MAX_TOPHALF;
1290 break;
1291 case SNAP_BOTTOM:
1292 directions = MAX_HORIZONTAL | MAX_BOTTOMHALF;
1293 break;
1294 case SNAP_TOPLEFT:
1295 directions = MAX_TOPHALF | MAX_LEFTHALF;
1296 break;
1297 case SNAP_TOPRIGHT:
1298 directions = MAX_TOPHALF | MAX_RIGHTHALF;
1299 break;
1300 case SNAP_BOTTOMLEFT:
1301 directions = MAX_BOTTOMHALF | MAX_LEFTHALF;
1302 break;
1303 case SNAP_BOTTOMRIGHT:
1304 directions = MAX_BOTTOMHALF | MAX_RIGHTHALF;
1305 break;
1308 if (directions)
1309 handleMaximize(wwin, directions);
1310 data->snap = SNAP_NONE;
1313 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1315 #define MOVABLE_BIT 0x01
1316 #define RESIZABLE_BIT 0x02
1318 int wKeyboardMoveResizeWindow(WWindow * wwin)
1320 WScreen *scr = wwin->screen_ptr;
1321 Window root = scr->root_win;
1322 XEvent event;
1323 int w = wwin->frame->core->width;
1324 int h = wwin->frame->core->height;
1325 int scr_width = wwin->screen_ptr->scr_width;
1326 int scr_height = wwin->screen_ptr->scr_height;
1327 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1328 int src_x = wwin->frame_x;
1329 int src_y = wwin->frame_y;
1330 int original_w = w;
1331 int original_h = h;
1332 int done, off_x, off_y, ww, wh;
1333 int kspeed = _KS;
1334 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1335 Time lastTime = 0;
1336 KeyCode shiftl, shiftr, ctrlmode;
1337 KeySym keysym = NoSymbol;
1338 int moment = 0;
1339 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1340 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1341 ? wGetHeadForWindow(wwin)
1342 : scr->xine_info.primary_head);
1344 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1345 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1346 ctrlmode = done = off_x = off_y = 0;
1348 if (modes == RESIZABLE_BIT) {
1349 ctrlmode = 1;
1352 XSync(dpy, False);
1353 wusleep(10000);
1354 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1356 if (!wwin->flags.selected) {
1357 wUnselectWindows(scr);
1359 XGrabServer(dpy);
1360 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1361 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1362 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime);
1366 if (!opaqueMoveResize) {
1367 if (wwin->flags.shaded || scr->selected_windows) {
1368 if (scr->selected_windows)
1369 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1370 else
1371 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1372 } else {
1373 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1376 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1377 mapPositionDisplay(wwin, src_x, src_y, w, h);
1380 ww = w;
1381 wh = h;
1382 while (1) {
1384 looper.ox=off_x;
1385 looper.oy=off_y;
1387 do {
1388 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1389 | ButtonPressMask | ExposureMask, &event);
1390 if (event.type == Expose) {
1391 WMHandleEvent(&event);
1393 } while (event.type == Expose);
1395 if (!opaqueMoveResize) {
1396 if (wwin->flags.shaded || scr->selected_windows) {
1397 if (scr->selected_windows)
1398 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1399 else
1400 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1401 /*** I HATE EDGE RESISTANCE - ]d ***/
1402 } else {
1403 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1407 if (ctrlmode)
1408 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1411 XUngrabServer(dpy);
1412 XSync(dpy, False);
1414 switch (event.type) {
1415 case KeyPress:
1416 /* accelerate */
1417 if (event.xkey.time - lastTime > 50) {
1418 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1419 } else {
1420 if (kspeed < 20) {
1421 kspeed++;
1424 if (kspeed < _KS)
1425 kspeed = _KS;
1426 lastTime = event.xkey.time;
1427 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1428 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1429 ctrlmode = 1;
1430 wUnselectWindows(scr);
1431 } else {
1432 ctrlmode = 0;
1435 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1436 if (ctrlmode)
1437 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1438 else
1439 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1440 } else {
1442 keysym = XLookupKeysym(&event.xkey, 0);
1443 switch (keysym) {
1444 case XK_Return:
1445 done = 2;
1446 break;
1447 case XK_Escape:
1448 done = 1;
1449 break;
1450 case XK_Up:
1451 #ifdef XK_KP_Up
1452 case XK_KP_Up:
1453 #endif
1454 case XK_k:
1455 if (ctrlmode) {
1456 if (moment != UP)
1457 h = wh;
1458 h -= kspeed;
1459 moment = UP;
1460 if (h < 1)
1461 h = 1;
1462 } else
1463 off_y -= kspeed;
1464 break;
1465 case XK_Down:
1466 #ifdef XK_KP_Down
1467 case XK_KP_Down:
1468 #endif
1469 case XK_j:
1470 if (ctrlmode) {
1471 if (moment != DOWN)
1472 h = wh;
1473 h += kspeed;
1474 moment = DOWN;
1475 } else
1476 off_y += kspeed;
1477 break;
1478 case XK_Left:
1479 #ifdef XK_KP_Left
1480 case XK_KP_Left:
1481 #endif
1482 case XK_h:
1483 if (ctrlmode) {
1484 if (moment != LEFT)
1485 w = ww;
1486 w -= kspeed;
1487 if (w < 1)
1488 w = 1;
1489 moment = LEFT;
1490 } else
1491 off_x -= kspeed;
1492 break;
1493 case XK_Right:
1494 #ifdef XK_KP_Right
1495 case XK_KP_Right:
1496 #endif
1497 case XK_l:
1498 if (ctrlmode) {
1499 if (moment != RIGHT)
1500 w = ww;
1501 w += kspeed;
1502 moment = RIGHT;
1503 } else
1504 off_x += kspeed;
1505 break;
1508 ww = w;
1509 wh = h;
1510 wh -= vert_border;
1511 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1512 wh += vert_border;
1514 if (wPreferences.ws_cycle) {
1515 if (src_x + off_x + ww < 20) {
1516 if (!w_global.workspace.current)
1517 wWorkspaceChange(scr, w_global.workspace.count - 1);
1518 else
1519 wWorkspaceChange(scr, w_global.workspace.current - 1);
1521 off_x += scr_width;
1522 } else if (src_x + off_x + 20 > scr_width) {
1523 if (w_global.workspace.current == w_global.workspace.count - 1)
1524 wWorkspaceChange(scr, 0);
1525 else
1526 wWorkspaceChange(scr, w_global.workspace.current + 1);
1528 off_x -= scr_width;
1530 } else {
1531 if (src_x + off_x + ww < 20)
1532 off_x = 20 - ww - src_x;
1533 else if (src_x + off_x + 20 > scr_width)
1534 off_x = scr_width - 20 - src_x;
1537 if (src_y + off_y + wh < 20) {
1538 off_y = 20 - wh - src_y;
1539 } else if (src_y + off_y + 20 > scr_height) {
1540 off_y = scr_height - 20 - src_y;
1543 break;
1544 case ButtonPress:
1545 case ButtonRelease:
1546 done = 1;
1547 break;
1548 case Expose:
1549 WMHandleEvent(&event);
1550 while (XCheckTypedEvent(dpy, Expose, &event)) {
1551 WMHandleEvent(&event);
1553 break;
1555 default:
1556 WMHandleEvent(&event);
1557 break;
1560 XGrabServer(dpy);
1561 /*xxx */
1563 if (wwin->flags.shaded && !scr->selected_windows) {
1564 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1565 } else {
1566 if (ctrlmode) {
1567 WMUnmapWidget(scr->gview);
1568 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1569 } else if (!scr->selected_windows) {
1570 WMUnmapWidget(scr->gview);
1571 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1575 if (!opaqueMoveResize) {
1576 if (wwin->flags.shaded || scr->selected_windows) {
1577 if (scr->selected_windows)
1578 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1579 else
1580 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1581 } else {
1582 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1586 if (ctrlmode) {
1587 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1589 } else if (!scr->selected_windows)
1590 showPosition(wwin, src_x + off_x, src_y + off_y);
1592 if (opaqueMoveResize) {
1593 XUngrabServer(dpy);
1594 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1597 if (done) {
1598 if (!opaqueMoveResize) { /* ctrlmode => resize */
1599 if (wwin->flags.shaded || scr->selected_windows) {
1600 if (scr->selected_windows)
1601 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1602 else
1603 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1604 } else {
1605 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1609 if (ctrlmode) {
1610 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1611 src_y + off_y + wh, 0);
1612 WMUnmapWidget(scr->gview);
1613 } else
1614 WMUnmapWidget(scr->gview);
1616 XUngrabKeyboard(dpy, CurrentTime);
1617 XUngrabPointer(dpy, CurrentTime);
1618 XUngrabServer(dpy);
1620 if (done == 2) {
1621 if (wwin->flags.shaded || scr->selected_windows) {
1622 if (!scr->selected_windows) {
1623 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1624 wWindowSynthConfigureNotify(wwin);
1625 } else {
1626 WMArrayIterator iter;
1627 WWindow *foo;
1629 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1631 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1632 wWindowSynthConfigureNotify(foo);
1635 } else {
1636 if (ww != original_w)
1637 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
1639 if (wh != original_h)
1640 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1642 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1643 wWindowSynthConfigureNotify(wwin);
1645 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1646 wSetFocusTo(scr, wwin);
1649 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1650 head != wGetHeadForWindow(wwin)) {
1651 wArrangeIcons(scr, True);
1654 update_saved_geometry(wwin);
1656 return 1;
1662 *----------------------------------------------------------------------
1663 * wMouseMoveWindow--
1664 * Move the named window and the other selected ones (if any),
1665 * interactively. Also shows the position of the window, if only one
1666 * window is being moved.
1667 * If the window is not on the selected window list, the selected
1668 * windows are deselected.
1669 * If shift is pressed during the operation, the position display
1670 * is changed to another type.
1672 * Returns:
1673 * True if the window was moved, False otherwise.
1675 * Side effects:
1676 * The window(s) position is changed, and the client(s) are
1677 * notified about that.
1678 * The position display configuration may be changed.
1679 *----------------------------------------------------------------------
1681 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1683 WScreen *scr = wwin->screen_ptr;
1684 XEvent event;
1685 Window root = scr->root_win;
1686 KeyCode shiftl, shiftr;
1687 Bool done = False;
1688 int started = 0;
1689 int warped = 0;
1690 /* This needs not to change while moving, else bad things can happen */
1691 int opaqueMove = wPreferences.opaque_move;
1692 MoveData moveData;
1693 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1694 ? wGetHeadForWindow(wwin)
1695 : scr->xine_info.primary_head);
1697 if (!IS_MOVABLE(wwin))
1698 return False;
1700 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1701 XSetWindowAttributes attr;
1703 attr.save_under = True;
1704 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1707 initMoveData(wwin, &moveData);
1709 moveData.mouseX = ev->xmotion.x_root;
1710 moveData.mouseY = ev->xmotion.y_root;
1712 if (!wwin->flags.selected) {
1713 /* this window is not selected, unselect others and move only wwin */
1714 wUnselectWindows(scr);
1716 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1717 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1718 while (!done) {
1719 if (warped) {
1720 int junk;
1721 Window junkw;
1723 /* XWarpPointer() doesn't seem to generate Motion events, so
1724 * we've got to simulate them */
1725 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1726 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1727 } else {
1728 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1729 | PointerMotionHintMask
1730 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1732 if (event.type == MotionNotify) {
1733 /* compress MotionNotify events */
1734 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1735 if (!checkMouseSamplingRate(&event))
1736 continue;
1739 switch (event.type) {
1740 case KeyPress:
1741 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1742 && started && !scr->selected_windows) {
1744 if (!opaqueMove) {
1745 drawFrames(wwin, scr->selected_windows,
1746 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1749 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1750 showPosition(wwin, moveData.realX, moveData.realY);
1751 XUngrabServer(dpy);
1753 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1754 moveData.winWidth, moveData.winHeight);
1756 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1757 XGrabServer(dpy);
1758 showPosition(wwin, moveData.realX, moveData.realY);
1761 if (!opaqueMove) {
1762 drawFrames(wwin, scr->selected_windows,
1763 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1765 /*} else {
1766 WMHandleEvent(&event); this causes problems needs fixing */
1768 break;
1770 case MotionNotify:
1771 if (IS_RESIZABLE(wwin) && wPreferences.window_snapping && wPreferences.no_autowrap) {
1772 int snap_direction;
1774 snap_direction = get_snap_direction(scr, moveData.mouseX, moveData.mouseY);
1776 if (moveData.snap != snap_direction) {
1777 /* erase old frame */
1778 if (moveData.snap)
1779 draw_snap_frame(wwin, moveData.snap);
1780 /* draw new frame */
1781 if (snap_direction)
1782 draw_snap_frame(wwin, snap_direction);
1783 moveData.snap = snap_direction;
1787 if (started) {
1788 /* erase snap frame */
1789 if (moveData.snap)
1790 draw_snap_frame(wwin, moveData.snap);
1792 updateWindowPosition(wwin, &moveData,
1793 scr->selected_windows == NULL
1794 && wPreferences.edge_resistance > 0,
1795 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1797 /* redraw snap frame */
1798 if (moveData.snap)
1799 draw_snap_frame(wwin, moveData.snap);
1801 if (!warped && !wPreferences.no_autowrap) {
1802 int oldWorkspace = w_global.workspace.current;
1804 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1805 showPosition(wwin, moveData.realX, moveData.realY);
1806 XUngrabServer(dpy);
1808 if (!opaqueMove) {
1809 drawFrames(wwin, scr->selected_windows,
1810 moveData.realX - wwin->frame_x,
1811 moveData.realY - wwin->frame_y);
1813 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1814 if (w_global.workspace.current != oldWorkspace
1815 && wPreferences.edge_resistance > 0
1816 && scr->selected_windows == NULL)
1817 updateMoveData(wwin, &moveData);
1818 warped = 1;
1820 if (!opaqueMove) {
1821 drawFrames(wwin, scr->selected_windows,
1822 moveData.realX - wwin->frame_x,
1823 moveData.realY - wwin->frame_y);
1825 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1826 XSync(dpy, False);
1827 showPosition(wwin, moveData.realX, moveData.realY);
1828 XGrabServer(dpy);
1830 } else {
1831 warped = 0;
1833 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1834 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1836 if (wwin->flags.maximized) {
1837 if (wPreferences.drag_maximized_window == DRAGMAX_RESTORE) {
1838 float titlebar_ratio;
1839 int new_x, new_y;
1841 titlebar_ratio = (moveData.mouseX - wwin->frame_x) /
1842 (float)wwin->frame->core->width;
1843 new_y = wwin->frame_y;
1844 wUnmaximizeWindow(wwin);
1845 new_x = moveData.mouseX - titlebar_ratio * wwin->frame->core->width;
1846 wWindowMove(wwin, new_x, new_y);
1847 moveData.realX = moveData.calcX = wwin->frame_x;
1848 moveData.realY = moveData.calcY = wwin->frame_y;
1850 if (wPreferences.drag_maximized_window == DRAGMAX_UNMAXIMIZE) {
1851 wwin->flags.maximized = 0;
1852 wwin->flags.old_maximized = 0;
1856 XChangeActivePointerGrab(dpy, ButtonMotionMask
1857 | ButtonReleaseMask | ButtonPressMask,
1858 wPreferences.cursor[WCUR_MOVE], CurrentTime);
1859 started = 1;
1860 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1862 if (!scr->selected_windows)
1863 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1864 moveData.winWidth, moveData.winHeight);
1866 if (started && !opaqueMove)
1867 drawFrames(wwin, scr->selected_windows, 0, 0);
1869 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1870 && !scr->selected_windows)) {
1871 XGrabServer(dpy);
1872 if (wPreferences.move_display == WDIS_NEW)
1873 showPosition(wwin, moveData.realX, moveData.realY);
1877 break;
1879 case ButtonPress:
1880 break;
1882 case ButtonRelease:
1883 if (event.xbutton.button != ev->xbutton.button)
1884 break;
1886 if (started) {
1887 XEvent e;
1889 if (moveData.snap)
1890 do_snap(wwin, &moveData, opaqueMove);
1891 else if (!opaqueMove) {
1892 drawFrames(wwin, scr->selected_windows,
1893 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1894 XSync(dpy, 0);
1895 doWindowMove(wwin, scr->selected_windows,
1896 moveData.realX - wwin->frame_x,
1897 moveData.realY - wwin->frame_y);
1899 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1900 wWindowSynthConfigureNotify(wwin);
1901 #endif
1902 XUngrabKeyboard(dpy, CurrentTime);
1903 XUngrabServer(dpy);
1904 if (!opaqueMove) {
1905 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1906 wSetFocusTo(scr, wwin);
1908 if (wPreferences.move_display == WDIS_NEW)
1909 showPosition(wwin, moveData.realX, moveData.realY);
1911 /* discard all enter/leave events that happened until
1912 * the time the button was released */
1913 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1914 if (e.xcrossing.time > event.xbutton.time) {
1915 XPutBackEvent(dpy, &e);
1916 break;
1919 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1920 if (e.xcrossing.time > event.xbutton.time) {
1921 XPutBackEvent(dpy, &e);
1922 break;
1926 if (!scr->selected_windows) {
1927 /* get rid of the geometry window */
1928 WMUnmapWidget(scr->gview);
1931 done = True;
1932 break;
1934 default:
1935 if (started && !opaqueMove) {
1936 drawFrames(wwin, scr->selected_windows,
1937 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1938 XUngrabServer(dpy);
1939 WMHandleEvent(&event);
1940 XSync(dpy, False);
1941 XGrabServer(dpy);
1942 drawFrames(wwin, scr->selected_windows,
1943 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1944 } else {
1945 WMHandleEvent(&event);
1947 break;
1951 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1952 XSetWindowAttributes attr;
1954 attr.save_under = False;
1955 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1959 freeMoveData(&moveData);
1961 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1962 head != wGetHeadForWindow(wwin)) {
1963 wArrangeIcons(scr, True);
1966 if (started)
1967 update_saved_geometry(wwin);
1969 return started;
1972 #define RESIZEBAR 1
1973 #define HCONSTRAIN 2
1975 static int getResizeDirection(WWindow * wwin, int x, int y, int dy, int flags)
1977 int w = wwin->frame->core->width - 1;
1978 int cw = wwin->frame->resizebar_corner_width;
1979 int dir;
1981 /* if not resizing through the resizebar */
1982 if (!(flags & RESIZEBAR)) {
1984 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1985 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1987 /* How much resize space is allowed */
1988 int spacew = abs(wwin->client.width / 3);
1989 int spaceh = abs(wwin->client.height / 3);
1991 /* Determine where x fits */
1992 if ((abs(x) > wwin->client.width/2 - spacew/2) &&
1993 (abs(x) < wwin->client.width/2 + spacew/2)) {
1994 /* Resize vertically */
1995 xdir = 0;
1996 } else if ((abs(y) > wwin->client.height/2 - spaceh/2) &&
1997 (abs(y) < wwin->client.height/2 + spaceh/2)) {
1998 /* Resize horizontally */
1999 ydir = 0;
2001 return (xdir | ydir);
2004 /* window is too narrow. allow diagonal resize */
2005 if (cw * 2 >= w) {
2006 int ydir;
2008 if (flags & HCONSTRAIN)
2009 ydir = 0;
2010 else
2011 ydir = DOWN;
2012 if (x < cw)
2013 return (LEFT | ydir);
2014 else
2015 return (RIGHT | ydir);
2017 /* vertical resize */
2018 if ((x > cw) && (x < w - cw))
2019 return DOWN;
2021 if (x < cw)
2022 dir = LEFT;
2023 else
2024 dir = RIGHT;
2026 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
2027 dir |= DOWN;
2029 return dir;
2032 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
2034 XEvent event;
2035 WScreen *scr = wwin->screen_ptr;
2036 Window root = scr->root_win;
2037 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
2038 int fw = wwin->frame->core->width;
2039 int fh = wwin->frame->core->height;
2040 int fx = wwin->frame_x;
2041 int fy = wwin->frame_y;
2042 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
2043 int orig_x, orig_y;
2044 int started;
2045 int dw, dh;
2046 int rw = fw, rh = fh;
2047 int rx1, ry1, rx2, ry2;
2048 int res = 0;
2049 KeyCode shiftl, shiftr;
2050 int orig_fx = fx;
2051 int orig_fy = fy;
2052 int orig_fw = fw;
2053 int orig_fh = fh;
2054 int original_fw = fw;
2055 int original_fh = fh;
2056 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
2057 ? wGetHeadForWindow(wwin)
2058 : scr->xine_info.primary_head);
2059 int opaqueResize = wPreferences.opaque_resize;
2061 if (!IS_RESIZABLE(wwin))
2062 return;
2064 if (wwin->flags.shaded) {
2065 wwarning("internal error: tryein");
2066 return;
2068 orig_x = ev->xbutton.x_root;
2069 orig_y = ev->xbutton.y_root;
2071 started = 0;
2072 wUnselectWindows(scr);
2073 rx1 = fx;
2074 rx2 = fx + fw - 1;
2075 ry1 = fy;
2076 ry2 = fy + fh - 1;
2077 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2078 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2080 while (1) {
2081 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
2082 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
2083 if (!checkMouseSamplingRate(&event))
2084 continue;
2086 switch (event.type) {
2087 case KeyPress:
2088 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2089 if (!opaqueResize) {
2090 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
2091 && started) {
2092 drawTransparentFrame(wwin, fx, fy, fw, fh);
2093 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
2094 drawTransparentFrame(wwin, fx, fy, fw, fh);
2097 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2098 break;
2100 case MotionNotify:
2101 if (started) {
2102 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
2104 dw = 0;
2105 dh = 0;
2107 orig_fx = fx;
2108 orig_fy = fy;
2109 orig_fw = fw;
2110 orig_fh = fh;
2112 if (res & LEFT)
2113 dw = orig_x - event.xmotion.x_root;
2114 else if (res & RIGHT)
2115 dw = event.xmotion.x_root - orig_x;
2116 if (res & UP)
2117 dh = orig_y - event.xmotion.y_root;
2118 else if (res & DOWN)
2119 dh = event.xmotion.y_root - orig_y;
2121 orig_x = event.xmotion.x_root;
2122 orig_y = event.xmotion.y_root;
2124 rw += dw;
2125 rh += dh;
2126 fw = rw;
2127 fh = rh - vert_border;
2128 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
2129 fh += vert_border;
2130 if (res & LEFT)
2131 fx = rx2 - fw + 1;
2132 else if (res & RIGHT)
2133 fx = rx1;
2134 if (res & UP)
2135 fy = ry2 - fh + 1;
2136 else if (res & DOWN)
2137 fy = ry1;
2138 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
2139 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
2140 int tx, ty;
2141 Window junkw;
2142 int flags;
2144 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
2145 orig_x, orig_y, &tx, &ty, &junkw);
2147 /* check if resizing through resizebar */
2148 if (is_resizebar)
2149 flags = RESIZEBAR;
2150 else
2151 flags = 0;
2153 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
2154 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
2155 flags |= HCONSTRAIN;
2157 res = getResizeDirection(wwin, tx, ty, orig_y - event.xmotion.y_root, flags);
2159 if (res == (UP | LEFT))
2160 XChangeActivePointerGrab(dpy, ButtonMotionMask
2161 | ButtonReleaseMask | ButtonPressMask,
2162 wPreferences.cursor[WCUR_TOPLEFTRESIZE], CurrentTime);
2163 else if (res == (UP | RIGHT))
2164 XChangeActivePointerGrab(dpy, ButtonMotionMask
2165 | ButtonReleaseMask | ButtonPressMask,
2166 wPreferences.cursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
2167 else if (res == (DOWN | LEFT))
2168 XChangeActivePointerGrab(dpy, ButtonMotionMask
2169 | ButtonReleaseMask | ButtonPressMask,
2170 wPreferences.cursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2171 else if (res == (DOWN | RIGHT))
2172 XChangeActivePointerGrab(dpy, ButtonMotionMask
2173 | ButtonReleaseMask | ButtonPressMask,
2174 wPreferences.cursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2175 else if (res == DOWN || res == UP)
2176 XChangeActivePointerGrab(dpy, ButtonMotionMask
2177 | ButtonReleaseMask | ButtonPressMask,
2178 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2179 else if (res & (DOWN | UP))
2180 XChangeActivePointerGrab(dpy, ButtonMotionMask
2181 | ButtonReleaseMask | ButtonPressMask,
2182 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2183 else if (res & (LEFT | RIGHT))
2184 XChangeActivePointerGrab(dpy, ButtonMotionMask
2185 | ButtonReleaseMask | ButtonPressMask,
2186 wPreferences.cursor[WCUR_HORIZONRESIZE], CurrentTime);
2188 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2190 XGrabServer(dpy);
2192 /* Draw the resize frame for the first time. */
2193 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2195 if (!opaqueResize)
2196 drawTransparentFrame(wwin, fx, fy, fw, fh);
2198 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2200 started = 1;
2202 if (started) {
2203 if (!opaqueResize)
2204 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2206 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2207 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2209 if (!opaqueResize)
2210 drawTransparentFrame(wwin, fx, fy, fw, fh);
2212 if (fh != orig_fh || fw != orig_fw) {
2213 if (wPreferences.size_display == WDIS_NEW)
2214 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2215 orig_fy + orig_fh, res);
2217 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2220 if (opaqueResize) {
2221 /* Fist clean the geometry line */
2222 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2223 /* Now, continue drawing */
2224 XUngrabServer(dpy);
2225 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2226 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2227 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2230 break;
2232 case ButtonPress:
2233 break;
2235 case ButtonRelease:
2236 if (event.xbutton.button != ev->xbutton.button)
2237 break;
2239 if (started) {
2240 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2242 if (!opaqueResize)
2243 drawTransparentFrame(wwin, fx, fy, fw, fh);
2245 XUngrabKeyboard(dpy, CurrentTime);
2246 WMUnmapWidget(scr->gview);
2247 XUngrabServer(dpy);
2249 if (fw != original_fw)
2250 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
2252 if (fh != original_fh)
2253 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2255 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2256 wWindowSynthConfigureNotify(wwin);
2258 return;
2260 default:
2261 WMHandleEvent(&event);
2265 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2266 wArrangeIcons(scr, True);
2269 #undef LEFT
2270 #undef RIGHT
2271 #undef HORIZONTAL
2272 #undef UP
2273 #undef DOWN
2274 #undef VERTICAL
2275 #undef HCONSTRAIN
2276 #undef RESIZEBAR
2278 void wUnselectWindows(WScreen * scr)
2280 WWindow *wwin;
2282 if (!scr->selected_windows)
2283 return;
2285 while (WMGetArrayItemCount(scr->selected_windows)) {
2286 wwin = WMGetFromArray(scr->selected_windows, 0);
2287 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2288 wIconSelect(wwin->icon);
2290 wSelectWindow(wwin, False);
2292 WMFreeArray(scr->selected_windows);
2293 scr->selected_windows = NULL;
2296 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2298 WWindow *tmpw;
2300 /* select the windows and put them in the selected window list */
2301 tmpw = scr->focused_window;
2302 while (tmpw != NULL) {
2303 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2304 if ((tmpw->frame->workspace == w_global.workspace.current || IS_OMNIPRESENT(tmpw))
2305 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2306 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2307 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2308 wSelectWindow(tmpw, True);
2311 tmpw = tmpw->prev;
2315 void wSelectWindows(WScreen * scr, XEvent * ev)
2317 XEvent event;
2318 Window root = scr->root_win;
2319 GC gc = scr->frame_gc;
2320 int xp = ev->xbutton.x_root;
2321 int yp = ev->xbutton.y_root;
2322 int w = 0, h = 0;
2323 int x = xp, y = yp;
2325 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2326 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2327 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2328 return;
2330 XGrabServer(dpy);
2332 wUnselectWindows(scr);
2334 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2335 while (1) {
2336 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2338 switch (event.type) {
2339 case MotionNotify:
2340 XDrawRectangle(dpy, root, gc, x, y, w, h);
2341 x = event.xmotion.x_root;
2342 if (x < xp) {
2343 w = xp - x;
2344 } else {
2345 w = x - xp;
2346 x = xp;
2348 y = event.xmotion.y_root;
2349 if (y < yp) {
2350 h = yp - y;
2351 } else {
2352 h = y - yp;
2353 y = yp;
2355 XDrawRectangle(dpy, root, gc, x, y, w, h);
2356 break;
2358 case ButtonPress:
2359 break;
2361 case ButtonRelease:
2362 if (event.xbutton.button != ev->xbutton.button)
2363 break;
2365 XDrawRectangle(dpy, root, gc, x, y, w, h);
2366 XUngrabServer(dpy);
2367 XUngrabPointer(dpy, CurrentTime);
2368 selectWindowsInside(scr, x, y, x + w, y + h);
2369 return;
2371 default:
2372 WMHandleEvent(&event);
2373 break;
2378 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2380 WScreen *scr = wwin->screen_ptr;
2381 Window root = scr->root_win;
2382 int x, y, h = 0;
2383 XEvent event;
2384 KeyCode shiftl, shiftr;
2385 Window junkw;
2386 int junk;
2388 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2389 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2390 *x_ret = 0;
2391 *y_ret = 0;
2392 return;
2394 if (HAS_TITLEBAR(wwin)) {
2395 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2396 TITLEBAR_EXTEND_SPACE) * 2;
2398 if (h > wPreferences.window_title_max_height)
2399 h = wPreferences.window_title_max_height;
2401 if (h < wPreferences.window_title_min_height)
2402 h = wPreferences.window_title_min_height;
2404 height += h;
2406 if (HAS_RESIZEBAR(wwin)) {
2407 height += RESIZEBAR_HEIGHT;
2409 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2410 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2411 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2413 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2415 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2416 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2417 while (1) {
2418 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2420 if (!checkMouseSamplingRate(&event))
2421 continue;
2423 switch (event.type) {
2424 case KeyPress:
2425 if ((event.xkey.keycode == shiftl)
2426 || (event.xkey.keycode == shiftr)) {
2427 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2428 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2429 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2431 break;
2433 case MotionNotify:
2434 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2436 x = event.xmotion.x_root;
2437 y = event.xmotion.y_root;
2439 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2440 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2442 showPosition(wwin, x - width / 2, y - h / 2);
2444 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2446 break;
2448 case ButtonPress:
2449 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2450 XSync(dpy, 0);
2451 *x_ret = x - width / 2;
2452 *y_ret = y - h / 2;
2453 XUngrabPointer(dpy, CurrentTime);
2454 XUngrabKeyboard(dpy, CurrentTime);
2455 /* get rid of the geometry window */
2456 WMUnmapWidget(scr->gview);
2457 return;
2459 default:
2460 WMHandleEvent(&event);
2461 break;