Remove mkpatch script
[wmaker-crm.git] / src / moveres.c
blobe2b92d1172694cf28cb6e9c68e812446e8a5596a
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "wcore.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "actions.h"
41 #include "workspace.h"
43 #include "geomview.h"
44 #include "screen.h"
45 #include "xinerama.h"
47 #include <WINGs/WINGsP.h>
49 /* How many different types of geometry/position
50 display thingies are there? */
51 #define NUM_DISPLAYS 5
53 #define LEFT 1
54 #define RIGHT 2
55 #define HORIZONTAL (LEFT|RIGHT)
56 #define UP 4
57 #define DOWN 8
58 #define VERTICAL (UP|DOWN)
60 /* True if window currently has a border. This also includes borderless
61 * windows which are currently selected
63 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
65 /****** Global Variables ******/
66 extern Time LastTimestamp;
68 extern Cursor wCursor[WCUR_LAST];
70 extern WPreferences wPreferences;
72 extern Atom _XA_WM_PROTOCOLS;
75 *----------------------------------------------------------------------
76 * checkMouseSamplingRate-
77 * For lowering the mouse motion sampling rate for machines where
78 * it's too high (SGIs). If it returns False then the event should be
79 * ignored.
80 *----------------------------------------------------------------------
82 static Bool checkMouseSamplingRate(XEvent * ev)
84 static Time previousMotion = 0;
86 if (ev->type == MotionNotify) {
87 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
88 return False;
89 } else {
90 previousMotion = ev->xmotion.time;
93 return True;
97 *----------------------------------------------------------------------
98 * moveGeometryDisplayCentered
100 * routine that moves the geometry/position window on scr so it is
101 * centered over the given coordinates (x,y). Also the window position
102 * is clamped so it stays on the screen at all times.
103 *----------------------------------------------------------------------
105 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
107 unsigned int w = WMWidgetWidth(scr->gview);
108 unsigned int h = WMWidgetHeight(scr->gview);
109 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
111 x -= w / 2;
112 y -= h / 2;
114 /* dead area check */
115 if (scr->xine_info.count) {
116 WMRect rect;
117 int head, flags;
119 rect.pos.x = x;
120 rect.pos.y = y;
121 rect.size.width = w;
122 rect.size.height = h;
124 head = wGetRectPlacementInfo(scr, rect, &flags);
126 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
127 rect = wGetRectForHead(scr, head);
128 x1 = rect.pos.x;
129 y1 = rect.pos.y;
130 x2 = x1 + rect.size.width;
131 y2 = y1 + rect.size.height;
135 if (x < x1 + 1)
136 x = x1 + 1;
137 else if (x > (x2 - w))
138 x = x2 - w;
140 if (y < y1 + 1)
141 y = y1 + 1;
142 else if (y > (y2 - h))
143 y = y2 - h;
145 WMMoveWidget(scr->gview, x, y);
148 static void showPosition(WWindow * wwin, int x, int y)
150 WScreen *scr = wwin->screen_ptr;
152 if (wPreferences.move_display == WDIS_NEW) {
153 #if 0
154 int width = wwin->frame->core->width;
155 int height = wwin->frame->core->height;
157 GC lgc = scr->line_gc;
158 XSetForeground(dpy, lgc, scr->line_pixel);
159 sprintf(num, "%i", x);
161 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
162 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
163 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
164 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
165 #endif
166 } else {
167 WSetGeometryViewShownPosition(scr->gview, x, y);
171 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
173 WScreen *scr = wwin->screen_ptr;
174 WMRect rect;
176 wPreferences.move_display++;
177 wPreferences.move_display %= NUM_DISPLAYS;
179 if (wPreferences.move_display == WDIS_NEW) {
180 wPreferences.move_display++;
181 wPreferences.move_display %= NUM_DISPLAYS;
184 if (wPreferences.move_display == WDIS_NONE) {
185 WMUnmapWidget(scr->gview);
186 } else {
187 if (wPreferences.move_display == WDIS_CENTER) {
188 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
189 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
190 rect.pos.y + rect.size.height / 2);
191 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
192 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
193 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
194 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
195 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
197 WMMapWidget(scr->gview);
201 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
203 WScreen *scr = wwin->screen_ptr;
204 WMRect rect;
206 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
207 return;
208 } else if (wPreferences.move_display == WDIS_CENTER) {
209 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
210 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
211 rect.pos.y + rect.size.height / 2);
212 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
213 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
214 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
215 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
216 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
218 WMMapWidget(scr->gview);
219 WSetGeometryViewShownPosition(scr->gview, x, y);
222 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
224 WScreen *scr = wwin->screen_ptr;
225 Window root = scr->root_win;
226 GC gc = scr->line_gc;
227 int ty, by, my, x, y, mx, s;
228 char num[16];
229 XSegment segment[4];
230 int fw, fh;
232 /* This seems necessary for some odd reason (too lazy to write x1-1 and
233 * x2-1 everywhere below in the code). But why only for x? */
234 x1--;
235 x2--;
237 if (HAS_BORDER_WITH_SELECT(wwin)) {
238 x1 += FRAME_BORDER_WIDTH;
239 x2 += FRAME_BORDER_WIDTH;
240 y1 += FRAME_BORDER_WIDTH;
241 y2 += FRAME_BORDER_WIDTH;
244 ty = y1 + wwin->frame->top_width;
245 by = y2 - wwin->frame->bottom_width;
247 if (wPreferences.size_display == WDIS_NEW) {
248 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
249 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
251 XSetForeground(dpy, gc, scr->line_pixel);
253 /* vertical geometry */
254 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
255 x = x2;
256 s = -15;
257 } else {
258 x = x1;
259 s = 15;
261 my = (ty + by) / 2;
263 /* top arrow & end bar */
264 segment[0].x1 = x - (s + 6);
265 segment[0].y1 = ty;
266 segment[0].x2 = x - (s - 10);
267 segment[0].y2 = ty;
269 /* arrowhead */
270 segment[1].x1 = x - (s - 2);
271 segment[1].y1 = ty + 1;
272 segment[1].x2 = x - (s - 5);
273 segment[1].y2 = ty + 7;
275 segment[2].x1 = x - (s - 2);
276 segment[2].y1 = ty + 1;
277 segment[2].x2 = x - (s + 1);
278 segment[2].y2 = ty + 7;
280 /* line */
281 segment[3].x1 = x - (s - 2);
282 segment[3].y1 = ty + 1;
283 segment[3].x2 = x - (s - 2);
284 segment[3].y2 = my - fh / 2 - 1;
286 XDrawSegments(dpy, root, gc, segment, 4);
288 /* bottom arrow & end bar */
289 segment[0].y1 = by;
290 segment[0].y2 = by;
292 /* arrowhead */
293 segment[1].y1 = by - 1;
294 segment[1].y2 = by - 7;
296 segment[2].y1 = by - 1;
297 segment[2].y2 = by - 7;
299 /* line */
300 segment[3].y1 = my + fh / 2 + 2;
301 segment[3].y2 = by - 1;
303 XDrawSegments(dpy, root, gc, segment, 4);
305 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
306 wwin->normal_hints->height_inc);
307 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
309 /* Display the height. */
310 XSetFont(dpy, gc, scr->tech_draw_font->fid);
311 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
312 strlen(num));
314 /* horizontal geometry */
315 if (y1 < 15) {
316 y = y2;
317 s = -15;
318 } else {
319 y = y1;
320 s = 15;
322 mx = x1 + (x2 - x1) / 2;
323 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
324 wwin->normal_hints->width_inc);
325 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
327 /* left arrow & end bar */
328 segment[0].x1 = x1;
329 segment[0].y1 = y - (s + 6);
330 segment[0].x2 = x1;
331 segment[0].y2 = y - (s - 10);
333 /* arrowhead */
334 segment[1].x1 = x1 + 7;
335 segment[1].y1 = y - (s + 1);
336 segment[1].x2 = x1 + 1;
337 segment[1].y2 = y - (s - 2);
339 segment[2].x1 = x1 + 1;
340 segment[2].y1 = y - (s - 2);
341 segment[2].x2 = x1 + 7;
342 segment[2].y2 = y - (s - 5);
344 /* line */
345 segment[3].x1 = x1 + 1;
346 segment[3].y1 = y - (s - 2);
347 segment[3].x2 = mx - fw / 2 - 2;
348 segment[3].y2 = y - (s - 2);
350 XDrawSegments(dpy, root, gc, segment, 4);
352 /* right arrow & end bar */
353 segment[0].x1 = x2 + 1;
354 segment[0].x2 = x2 + 1;
356 /* arrowhead */
357 segment[1].x1 = x2 - 6;
358 segment[1].x2 = x2;
360 segment[2].x1 = x2;
361 segment[2].x2 = x2 - 6;
363 /* line */
364 segment[3].x1 = mx + fw / 2 + 2;
365 segment[3].x2 = x2;
367 XDrawSegments(dpy, root, gc, segment, 4);
369 /* Display the width. */
370 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
371 strlen(num));
372 } else {
373 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
374 / wwin->normal_hints->width_inc,
375 (by - ty - wwin->normal_hints->base_height)
376 / wwin->normal_hints->height_inc);
380 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
382 WScreen *scr = wwin->screen_ptr;
383 WMRect rect;
385 wPreferences.size_display++;
386 wPreferences.size_display %= NUM_DISPLAYS;
388 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
389 WMUnmapWidget(scr->gview);
390 } else {
391 if (wPreferences.size_display == WDIS_CENTER) {
392 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
393 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
394 rect.pos.y + rect.size.height / 2);
395 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
396 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
397 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
398 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
399 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
401 WMMapWidget(scr->gview);
402 showGeometry(wwin, x, y, x + w, y + h, dir);
406 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
408 WScreen *scr = wwin->screen_ptr;
409 WMRect rect;
411 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
412 return;
414 if (wPreferences.size_display == WDIS_CENTER) {
415 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
416 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
417 rect.pos.y + rect.size.height / 2);
418 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
419 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
420 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
421 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
422 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
424 WMMapWidget(scr->gview);
425 showGeometry(wwin, x, y, x + w, y + h, 0);
428 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
430 WWindow *tmpw;
431 WScreen *scr = wwin->screen_ptr;
432 int x, y;
434 if (!array || !WMGetArrayItemCount(array)) {
435 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
436 } else {
437 WMArrayIterator iter;
439 WM_ITERATE_ARRAY(array, tmpw, iter) {
440 x = tmpw->frame_x + dx;
441 y = tmpw->frame_y + dy;
443 #if 1 /* XXX: with xinerama patch was #if 0, check this */
444 /* don't let windows become unreachable */
446 if (x + (int)tmpw->frame->core->width < 20)
447 x = 20 - (int)tmpw->frame->core->width;
448 else if (x + 20 > scr->scr_width)
449 x = scr->scr_width - 20;
451 if (y + (int)tmpw->frame->core->height < 20)
452 y = 20 - (int)tmpw->frame->core->height;
453 else if (y + 20 > scr->scr_height)
454 y = scr->scr_height - 20;
455 #else
456 wScreenBringInside(scr, &x, &y,
457 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
458 #endif
460 wWindowMove(tmpw, x, y);
465 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
467 Window root = wwin->screen_ptr->root_win;
468 GC gc = wwin->screen_ptr->frame_gc;
469 int h = 0;
470 int bottom = 0;
472 if (HAS_BORDER_WITH_SELECT(wwin)) {
473 x += FRAME_BORDER_WIDTH;
474 y += FRAME_BORDER_WIDTH;
477 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
478 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
479 TITLEBAR_EXTEND_SPACE) * 2;
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()
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*FRAME_BORDER_WIDTH : 0))
601 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
602 (HAS_BORDER_WITH_SELECT(w) ? 2*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 * FRAME_BORDER_WIDTH : 0);
835 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * 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 updateIndex;
913 Bool attract;
915 hresist = False;
916 vresist = False;
918 updateIndex = 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(wwin, data, newX, newY);
1197 data->realX = newX;
1198 data->realY = newY;
1201 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1203 #define MOVABLE_BIT 0x01
1204 #define RESIZABLE_BIT 0x02
1206 int wKeyboardMoveResizeWindow(WWindow * wwin)
1208 WScreen *scr = wwin->screen_ptr;
1209 Window root = scr->root_win;
1210 XEvent event;
1211 int w = wwin->frame->core->width;
1212 int h = wwin->frame->core->height;
1213 int scr_width = wwin->screen_ptr->scr_width;
1214 int scr_height = wwin->screen_ptr->scr_height;
1215 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1216 int src_x = wwin->frame_x;
1217 int src_y = wwin->frame_y;
1218 int done, off_x, off_y, ww, wh;
1219 int kspeed = _KS;
1220 Time lastTime = 0;
1221 KeyCode shiftl, shiftr, ctrll, 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 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1232 ctrlmode = done = off_x = off_y = 0;
1234 if (modes == RESIZABLE_BIT) {
1235 ctrlmode = 1;
1238 XSync(dpy, False);
1239 wusleep(10000);
1240 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1242 if (!wwin->flags.selected) {
1243 wUnselectWindows(scr);
1245 XGrabServer(dpy);
1246 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1247 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1248 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1250 if (wwin->flags.shaded || scr->selected_windows) {
1251 if (scr->selected_windows)
1252 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1253 else
1254 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1255 if (!scr->selected_windows)
1256 mapPositionDisplay(wwin, src_x, src_y, w, h);
1257 } else {
1258 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1260 ww = w;
1261 wh = h;
1262 while (1) {
1264 looper.ox=off_x;
1265 looper.oy=off_y;
1267 do {
1268 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1269 | ButtonPressMask | ExposureMask, &event);
1270 if (event.type == Expose) {
1271 WMHandleEvent(&event);
1273 } while (event.type == Expose);
1275 if (wwin->flags.shaded || scr->selected_windows) {
1276 if (scr->selected_windows)
1277 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1278 else
1279 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1280 /*** I HATE EDGE RESISTANCE - ]d ***/
1281 } else {
1282 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1285 if (ctrlmode)
1286 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1289 XUngrabServer(dpy);
1290 XSync(dpy, False);
1292 switch (event.type) {
1293 case KeyPress:
1294 /* accelerate */
1295 if (event.xkey.time - lastTime > 50) {
1296 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1297 } else {
1298 if (kspeed < 20) {
1299 kspeed++;
1302 if (kspeed < _KS)
1303 kspeed = _KS;
1304 lastTime = event.xkey.time;
1305 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1306 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1307 ctrlmode = 1;
1308 wUnselectWindows(scr);
1309 } else {
1310 ctrlmode = 0;
1313 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1314 if (ctrlmode)
1315 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1316 else
1317 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1318 } else {
1320 keysym = XLookupKeysym(&event.xkey, 0);
1321 switch (keysym) {
1322 case XK_Return:
1323 done = 2;
1324 break;
1325 case XK_Escape:
1326 done = 1;
1327 break;
1328 case XK_Up:
1329 #ifdef XK_KP_Up
1330 case XK_KP_Up:
1331 #endif
1332 case XK_k:
1333 if (ctrlmode) {
1334 if (moment != UP)
1335 h = wh;
1336 h -= kspeed;
1337 moment = UP;
1338 if (h < 1)
1339 h = 1;
1340 } else
1341 off_y -= kspeed;
1342 break;
1343 case XK_Down:
1344 #ifdef XK_KP_Down
1345 case XK_KP_Down:
1346 #endif
1347 case XK_j:
1348 if (ctrlmode) {
1349 if (moment != DOWN)
1350 h = wh;
1351 h += kspeed;
1352 moment = DOWN;
1353 } else
1354 off_y += kspeed;
1355 break;
1356 case XK_Left:
1357 #ifdef XK_KP_Left
1358 case XK_KP_Left:
1359 #endif
1360 case XK_h:
1361 if (ctrlmode) {
1362 if (moment != LEFT)
1363 w = ww;
1364 w -= kspeed;
1365 if (w < 1)
1366 w = 1;
1367 moment = LEFT;
1368 } else
1369 off_x -= kspeed;
1370 break;
1371 case XK_Right:
1372 #ifdef XK_KP_Right
1373 case XK_KP_Right:
1374 #endif
1375 case XK_l:
1376 if (ctrlmode) {
1377 if (moment != RIGHT)
1378 w = ww;
1379 w += kspeed;
1380 moment = RIGHT;
1381 } else
1382 off_x += kspeed;
1383 break;
1386 ww = w;
1387 wh = h;
1388 wh -= vert_border;
1389 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1390 wh += vert_border;
1392 if (wPreferences.ws_cycle) {
1393 if (src_x + off_x + ww < 20) {
1394 if (!scr->current_workspace) {
1395 wWorkspaceChange(scr, scr->workspace_count - 1);
1396 } else
1397 wWorkspaceChange(scr, scr->current_workspace - 1);
1398 off_x += scr_width;
1399 } else if (src_x + off_x + 20 > scr_width) {
1400 if (scr->current_workspace == scr->workspace_count - 1) {
1401 wWorkspaceChange(scr, 0);
1402 } else
1403 wWorkspaceChange(scr, scr->current_workspace + 1);
1404 off_x -= scr_width;
1406 } else {
1407 if (src_x + off_x + ww < 20)
1408 off_x = 20 - ww - src_x;
1409 else if (src_x + off_x + 20 > scr_width)
1410 off_x = scr_width - 20 - src_x;
1413 if (src_y + off_y + wh < 20) {
1414 off_y = 20 - wh - src_y;
1415 } else if (src_y + off_y + 20 > scr_height) {
1416 off_y = scr_height - 20 - src_y;
1419 break;
1420 case ButtonPress:
1421 case ButtonRelease:
1422 done = 1;
1423 break;
1424 case Expose:
1425 WMHandleEvent(&event);
1426 while (XCheckTypedEvent(dpy, Expose, &event)) {
1427 WMHandleEvent(&event);
1429 break;
1431 default:
1432 WMHandleEvent(&event);
1433 break;
1436 XGrabServer(dpy);
1437 /*xxx */
1439 if (wwin->flags.shaded && !scr->selected_windows) {
1440 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1441 } else {
1442 if (ctrlmode) {
1443 WMUnmapWidget(scr->gview);
1444 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1445 } else if (!scr->selected_windows) {
1446 WMUnmapWidget(scr->gview);
1447 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1451 if (wwin->flags.shaded || scr->selected_windows) {
1452 if (scr->selected_windows)
1453 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1454 else
1455 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1456 } else {
1457 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1460 if (ctrlmode) {
1461 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1463 } else if (!scr->selected_windows)
1464 showPosition(wwin, src_x + off_x, src_y + off_y);
1466 if (done) {
1467 scr->keymove_tick = 0;
1469 WMDeleteTimerWithClientData(&looper);
1471 if (wwin->flags.shaded || scr->selected_windows) {
1472 if (scr->selected_windows)
1473 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1474 else
1475 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1476 } else {
1477 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1480 if (ctrlmode) {
1481 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1482 src_y + off_y + wh, 0);
1483 WMUnmapWidget(scr->gview);
1484 } else
1485 WMUnmapWidget(scr->gview);
1487 XUngrabKeyboard(dpy, CurrentTime);
1488 XUngrabPointer(dpy, CurrentTime);
1489 XUngrabServer(dpy);
1491 if (done == 2) {
1492 if (wwin->flags.shaded || scr->selected_windows) {
1493 if (!scr->selected_windows) {
1494 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1495 wWindowSynthConfigureNotify(wwin);
1496 } else {
1497 WMArrayIterator iter;
1498 WWindow *foo;
1500 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1502 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1503 wWindowSynthConfigureNotify(foo);
1506 } else {
1507 if (wwin->client.width != ww) {
1508 wwin->flags.user_changed_width = 1;
1509 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
1512 if (wwin->client.height != wh - vert_border) {
1513 wwin->flags.user_changed_height = 1;
1514 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1517 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1518 wWindowSynthConfigureNotify(wwin);
1520 wWindowChangeWorkspace(wwin, scr->current_workspace);
1521 wSetFocusTo(scr, wwin);
1524 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1525 head != wGetHeadForWindow(wwin)) {
1526 wArrangeIcons(scr, True);
1529 return 1;
1535 *----------------------------------------------------------------------
1536 * wMouseMoveWindow--
1537 * Move the named window and the other selected ones (if any),
1538 * interactively. Also shows the position of the window, if only one
1539 * window is being moved.
1540 * If the window is not on the selected window list, the selected
1541 * windows are deselected.
1542 * If shift is pressed during the operation, the position display
1543 * is changed to another type.
1545 * Returns:
1546 * True if the window was moved, False otherwise.
1548 * Side effects:
1549 * The window(s) position is changed, and the client(s) are
1550 * notified about that.
1551 * The position display configuration may be changed.
1552 *----------------------------------------------------------------------
1554 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1556 WScreen *scr = wwin->screen_ptr;
1557 XEvent event;
1558 Window root = scr->root_win;
1559 KeyCode shiftl, shiftr;
1560 Bool done = False;
1561 int started = 0;
1562 int warped = 0;
1563 /* This needs not to change while moving, else bad things can happen */
1564 int opaqueMove = wPreferences.opaque_move;
1565 MoveData moveData;
1566 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1567 ? wGetHeadForWindow(wwin)
1568 : scr->xine_info.primary_head);
1570 if (!IS_MOVABLE(wwin))
1571 return False;
1573 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1574 XSetWindowAttributes attr;
1576 attr.save_under = True;
1577 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1580 initMoveData(wwin, &moveData);
1582 moveData.mouseX = ev->xmotion.x_root;
1583 moveData.mouseY = ev->xmotion.y_root;
1585 if (!wwin->flags.selected) {
1586 /* this window is not selected, unselect others and move only wwin */
1587 wUnselectWindows(scr);
1589 #ifdef DEBUG
1590 puts("Moving window");
1591 #endif
1592 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1593 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1594 while (!done) {
1595 if (warped) {
1596 int junk;
1597 Window junkw;
1599 /* XWarpPointer() doesn't seem to generate Motion events, so
1600 * we've got to simulate them */
1601 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1602 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1603 } else {
1604 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1605 | PointerMotionHintMask
1606 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1608 if (event.type == MotionNotify) {
1609 /* compress MotionNotify events */
1610 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1611 if (!checkMouseSamplingRate(&event))
1612 continue;
1615 switch (event.type) {
1616 case KeyPress:
1617 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1618 && started && !scr->selected_windows) {
1620 if (!opaqueMove) {
1621 drawFrames(wwin, scr->selected_windows,
1622 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1625 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1626 showPosition(wwin, moveData.realX, moveData.realY);
1627 XUngrabServer(dpy);
1629 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1630 moveData.winWidth, moveData.winHeight);
1632 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1633 XGrabServer(dpy);
1634 showPosition(wwin, moveData.realX, moveData.realY);
1637 if (!opaqueMove) {
1638 drawFrames(wwin, scr->selected_windows,
1639 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1641 /*} else {
1642 WMHandleEvent(&event); this causes problems needs fixing */
1644 break;
1646 case MotionNotify:
1647 if (started) {
1648 updateWindowPosition(wwin, &moveData,
1649 scr->selected_windows == NULL
1650 && wPreferences.edge_resistance > 0,
1651 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1653 if (!warped && !wPreferences.no_autowrap) {
1654 int oldWorkspace = scr->current_workspace;
1656 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1657 showPosition(wwin, moveData.realX, moveData.realY);
1658 XUngrabServer(dpy);
1660 if (!opaqueMove) {
1661 drawFrames(wwin, scr->selected_windows,
1662 moveData.realX - wwin->frame_x,
1663 moveData.realY - wwin->frame_y);
1665 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1666 if (scr->current_workspace != oldWorkspace
1667 && wPreferences.edge_resistance > 0
1668 && scr->selected_windows == NULL)
1669 updateMoveData(wwin, &moveData);
1670 warped = 1;
1672 if (!opaqueMove) {
1673 drawFrames(wwin, scr->selected_windows,
1674 moveData.realX - wwin->frame_x,
1675 moveData.realY - wwin->frame_y);
1677 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1678 XSync(dpy, False);
1679 showPosition(wwin, moveData.realX, moveData.realY);
1680 XGrabServer(dpy);
1682 } else {
1683 warped = 0;
1685 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1686 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1688 XChangeActivePointerGrab(dpy, ButtonMotionMask
1689 | ButtonReleaseMask | ButtonPressMask,
1690 wCursor[WCUR_MOVE], CurrentTime);
1691 started = 1;
1692 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1694 if (!scr->selected_windows)
1695 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1696 moveData.winWidth, moveData.winHeight);
1698 if (started && !opaqueMove)
1699 drawFrames(wwin, scr->selected_windows, 0, 0);
1701 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1702 && !scr->selected_windows)) {
1703 XGrabServer(dpy);
1704 if (wPreferences.move_display == WDIS_NEW)
1705 showPosition(wwin, moveData.realX, moveData.realY);
1708 break;
1710 case ButtonPress:
1711 break;
1713 case ButtonRelease:
1714 if (event.xbutton.button != ev->xbutton.button)
1715 break;
1717 if (started) {
1718 XEvent e;
1719 if (!opaqueMove) {
1720 drawFrames(wwin, scr->selected_windows,
1721 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1722 XSync(dpy, 0);
1723 doWindowMove(wwin, scr->selected_windows,
1724 moveData.realX - wwin->frame_x,
1725 moveData.realY - wwin->frame_y);
1727 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1728 wWindowSynthConfigureNotify(wwin);
1729 #endif
1730 XUngrabKeyboard(dpy, CurrentTime);
1731 XUngrabServer(dpy);
1732 if (!opaqueMove) {
1733 wWindowChangeWorkspace(wwin, scr->current_workspace);
1734 wSetFocusTo(scr, wwin);
1736 if (wPreferences.move_display == WDIS_NEW)
1737 showPosition(wwin, moveData.realX, moveData.realY);
1739 /* discard all enter/leave events that happened until
1740 * the time the button was released */
1741 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1742 if (e.xcrossing.time > event.xbutton.time) {
1743 XPutBackEvent(dpy, &e);
1744 break;
1747 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1748 if (e.xcrossing.time > event.xbutton.time) {
1749 XPutBackEvent(dpy, &e);
1750 break;
1754 if (!scr->selected_windows) {
1755 /* get rid of the geometry window */
1756 WMUnmapWidget(scr->gview);
1759 #ifdef DEBUG
1760 puts("End move window");
1761 #endif
1762 done = True;
1763 break;
1765 default:
1766 if (started && !opaqueMove) {
1767 drawFrames(wwin, scr->selected_windows,
1768 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1769 XUngrabServer(dpy);
1770 WMHandleEvent(&event);
1771 XSync(dpy, False);
1772 XGrabServer(dpy);
1773 drawFrames(wwin, scr->selected_windows,
1774 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1775 } else {
1776 WMHandleEvent(&event);
1778 break;
1782 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1783 XSetWindowAttributes attr;
1785 attr.save_under = False;
1786 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1790 freeMoveData(&moveData);
1792 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1793 head != wGetHeadForWindow(wwin)) {
1794 wArrangeIcons(scr, True);
1796 return started;
1799 #define RESIZEBAR 1
1800 #define HCONSTRAIN 2
1802 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1804 int w = wwin->frame->core->width - 1;
1805 int cw = wwin->frame->resizebar_corner_width;
1806 int dir;
1808 /* if not resizing through the resizebar */
1809 if (!(flags & RESIZEBAR)) {
1810 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1811 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1812 if (abs(dx) < 2 || abs(dy) < 2) {
1813 if (abs(dy) > abs(dx))
1814 xdir = 0;
1815 else
1816 ydir = 0;
1818 return (xdir | ydir);
1821 /* window is too narrow. allow diagonal resize */
1822 if (cw * 2 >= w) {
1823 int ydir;
1825 if (flags & HCONSTRAIN)
1826 ydir = 0;
1827 else
1828 ydir = DOWN;
1829 if (x < cw)
1830 return (LEFT | ydir);
1831 else
1832 return (RIGHT | ydir);
1834 /* vertical resize */
1835 if ((x > cw) && (x < w - cw))
1836 return DOWN;
1838 if (x < cw)
1839 dir = LEFT;
1840 else
1841 dir = RIGHT;
1843 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1844 dir |= DOWN;
1846 return dir;
1849 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1851 XEvent event;
1852 WScreen *scr = wwin->screen_ptr;
1853 Window root = scr->root_win;
1854 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1855 int fw = wwin->frame->core->width;
1856 int fh = wwin->frame->core->height;
1857 int fx = wwin->frame_x;
1858 int fy = wwin->frame_y;
1859 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1860 int orig_x, orig_y;
1861 int started;
1862 int dw, dh;
1863 int rw = fw, rh = fh;
1864 int rx1, ry1, rx2, ry2;
1865 int res = 0;
1866 KeyCode shiftl, shiftr;
1867 int h = 0;
1868 int orig_fx = fx;
1869 int orig_fy = fy;
1870 int orig_fw = fw;
1871 int orig_fh = fh;
1872 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1873 ? wGetHeadForWindow(wwin)
1874 : scr->xine_info.primary_head);
1876 if (!IS_RESIZABLE(wwin))
1877 return;
1879 if (wwin->flags.shaded) {
1880 wwarning("internal error: tryein");
1881 return;
1883 orig_x = ev->xbutton.x_root;
1884 orig_y = ev->xbutton.y_root;
1886 started = 0;
1887 #ifdef DEBUG
1888 puts("Resizing window");
1889 #endif
1891 wUnselectWindows(scr);
1892 rx1 = fx;
1893 rx2 = fx + fw - 1;
1894 ry1 = fy;
1895 ry2 = fy + fh - 1;
1896 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1897 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1898 if (HAS_TITLEBAR(wwin))
1899 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
1900 TITLEBAR_EXTEND_SPACE) * 2;
1901 else
1902 h = 0;
1903 while (1) {
1904 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1905 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1906 if (!checkMouseSamplingRate(&event))
1907 continue;
1909 switch (event.type) {
1910 case KeyPress:
1911 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1912 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1913 && started) {
1914 drawTransparentFrame(wwin, fx, fy, fw, fh);
1915 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1916 drawTransparentFrame(wwin, fx, fy, fw, fh);
1918 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1919 break;
1921 case MotionNotify:
1922 if (started) {
1923 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1925 dw = 0;
1926 dh = 0;
1928 orig_fx = fx;
1929 orig_fy = fy;
1930 orig_fw = fw;
1931 orig_fh = fh;
1933 if (res & LEFT)
1934 dw = orig_x - event.xmotion.x_root;
1935 else if (res & RIGHT)
1936 dw = event.xmotion.x_root - orig_x;
1937 if (res & UP)
1938 dh = orig_y - event.xmotion.y_root;
1939 else if (res & DOWN)
1940 dh = event.xmotion.y_root - orig_y;
1942 orig_x = event.xmotion.x_root;
1943 orig_y = event.xmotion.y_root;
1945 rw += dw;
1946 rh += dh;
1947 fw = rw;
1948 fh = rh - vert_border;
1949 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1950 fh += vert_border;
1951 if (res & LEFT)
1952 fx = rx2 - fw + 1;
1953 else if (res & RIGHT)
1954 fx = rx1;
1955 if (res & UP)
1956 fy = ry2 - fh + 1;
1957 else if (res & DOWN)
1958 fy = ry1;
1959 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1960 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1961 int tx, ty;
1962 Window junkw;
1963 int flags;
1965 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1966 orig_x, orig_y, &tx, &ty, &junkw);
1968 /* check if resizing through resizebar */
1969 if (is_resizebar)
1970 flags = RESIZEBAR;
1971 else
1972 flags = 0;
1974 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1975 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1976 flags |= HCONSTRAIN;
1978 res = getResizeDirection(wwin, tx, ty,
1979 orig_x - event.xmotion.x_root,
1980 orig_y - event.xmotion.y_root, flags);
1982 if (res == (UP | LEFT))
1983 XChangeActivePointerGrab(dpy, ButtonMotionMask
1984 | ButtonReleaseMask | ButtonPressMask,
1985 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1986 else if (res == (UP | RIGHT))
1987 XChangeActivePointerGrab(dpy, ButtonMotionMask
1988 | ButtonReleaseMask | ButtonPressMask,
1989 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1990 else if (res == (DOWN | LEFT))
1991 XChangeActivePointerGrab(dpy, ButtonMotionMask
1992 | ButtonReleaseMask | ButtonPressMask,
1993 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
1994 else if (res == (DOWN | RIGHT))
1995 XChangeActivePointerGrab(dpy, ButtonMotionMask
1996 | ButtonReleaseMask | ButtonPressMask,
1997 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
1998 else if (res == DOWN || res == UP)
1999 XChangeActivePointerGrab(dpy, ButtonMotionMask
2000 | ButtonReleaseMask | ButtonPressMask,
2001 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2002 else if (res & (DOWN | UP))
2003 XChangeActivePointerGrab(dpy, ButtonMotionMask
2004 | ButtonReleaseMask | ButtonPressMask,
2005 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2006 else if (res & (LEFT | RIGHT))
2007 XChangeActivePointerGrab(dpy, ButtonMotionMask
2008 | ButtonReleaseMask | ButtonPressMask,
2009 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2011 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2013 XGrabServer(dpy);
2015 /* Draw the resize frame for the first time. */
2016 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2018 drawTransparentFrame(wwin, fx, fy, fw, fh);
2020 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2022 started = 1;
2024 if (started) {
2025 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2026 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2027 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2028 drawTransparentFrame(wwin, fx, fy, fw, fh);
2029 } else {
2030 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2031 drawTransparentFrame(wwin, fx, fy, fw, fh);
2033 if (fh != orig_fh || fw != orig_fw) {
2034 if (wPreferences.size_display == WDIS_NEW) {
2035 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2036 orig_fy + orig_fh, res);
2038 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2041 break;
2043 case ButtonPress:
2044 break;
2046 case ButtonRelease:
2047 if (event.xbutton.button != ev->xbutton.button)
2048 break;
2050 if (started) {
2051 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2053 drawTransparentFrame(wwin, fx, fy, fw, fh);
2055 XUngrabKeyboard(dpy, CurrentTime);
2056 WMUnmapWidget(scr->gview);
2057 XUngrabServer(dpy);
2059 if (wwin->client.width != fw) {
2060 wwin->flags.user_changed_width = 1;
2061 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
2064 if (wwin->client.height != fh - vert_border) {
2065 wwin->flags.user_changed_height = 1;
2066 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2069 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2071 #ifdef DEBUG
2072 puts("End resize window");
2073 #endif
2074 return;
2076 default:
2077 WMHandleEvent(&event);
2081 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin)) {
2082 wArrangeIcons(scr, True);
2086 #undef LEFT
2087 #undef RIGHT
2088 #undef HORIZONTAL
2089 #undef UP
2090 #undef DOWN
2091 #undef VERTICAL
2092 #undef HCONSTRAIN
2093 #undef RESIZEBAR
2095 void wUnselectWindows(WScreen * scr)
2097 WWindow *wwin;
2099 if (!scr->selected_windows)
2100 return;
2102 while (WMGetArrayItemCount(scr->selected_windows)) {
2103 wwin = WMGetFromArray(scr->selected_windows, 0);
2104 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2105 wIconSelect(wwin->icon);
2107 wSelectWindow(wwin, False);
2109 WMFreeArray(scr->selected_windows);
2110 scr->selected_windows = NULL;
2113 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2115 WWindow *tmpw;
2117 /* select the windows and put them in the selected window list */
2118 tmpw = scr->focused_window;
2119 while (tmpw != NULL) {
2120 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2121 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2122 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2123 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2124 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2125 wSelectWindow(tmpw, True);
2128 tmpw = tmpw->prev;
2132 void wSelectWindows(WScreen * scr, XEvent * ev)
2134 XEvent event;
2135 Window root = scr->root_win;
2136 GC gc = scr->frame_gc;
2137 int xp = ev->xbutton.x_root;
2138 int yp = ev->xbutton.y_root;
2139 int w = 0, h = 0;
2140 int x = xp, y = yp;
2142 #ifdef DEBUG
2143 puts("Selecting windows");
2144 #endif
2145 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2146 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2147 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2148 return;
2150 XGrabServer(dpy);
2152 wUnselectWindows(scr);
2154 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2155 while (1) {
2156 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2158 switch (event.type) {
2159 case MotionNotify:
2160 XDrawRectangle(dpy, root, gc, x, y, w, h);
2161 x = event.xmotion.x_root;
2162 if (x < xp) {
2163 w = xp - x;
2164 } else {
2165 w = x - xp;
2166 x = xp;
2168 y = event.xmotion.y_root;
2169 if (y < yp) {
2170 h = yp - y;
2171 } else {
2172 h = y - yp;
2173 y = yp;
2175 XDrawRectangle(dpy, root, gc, x, y, w, h);
2176 break;
2178 case ButtonPress:
2179 break;
2181 case ButtonRelease:
2182 if (event.xbutton.button != ev->xbutton.button)
2183 break;
2185 XDrawRectangle(dpy, root, gc, x, y, w, h);
2186 XUngrabServer(dpy);
2187 XUngrabPointer(dpy, CurrentTime);
2188 selectWindowsInside(scr, x, y, x + w, y + h);
2190 #ifdef DEBUG
2191 puts("End window selection");
2192 #endif
2193 return;
2195 default:
2196 WMHandleEvent(&event);
2197 break;
2202 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2204 WScreen *scr = wwin->screen_ptr;
2205 Window root = scr->root_win;
2206 int x, y, h = 0;
2207 XEvent event;
2208 KeyCode shiftl, shiftr;
2209 Window junkw;
2210 int junk;
2212 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2213 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2214 *x_ret = 0;
2215 *y_ret = 0;
2216 return;
2218 if (HAS_TITLEBAR(wwin)) {
2219 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2220 TITLEBAR_EXTEND_SPACE) * 2;
2221 height += h;
2223 if (HAS_RESIZEBAR(wwin)) {
2224 height += RESIZEBAR_HEIGHT;
2226 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2227 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2228 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2230 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2232 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2233 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2234 while (1) {
2235 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2237 if (!checkMouseSamplingRate(&event))
2238 continue;
2240 switch (event.type) {
2241 case KeyPress:
2242 if ((event.xkey.keycode == shiftl)
2243 || (event.xkey.keycode == shiftr)) {
2244 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2245 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2246 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2248 break;
2250 case MotionNotify:
2251 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2253 x = event.xmotion.x_root;
2254 y = event.xmotion.y_root;
2256 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2257 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2259 showPosition(wwin, x - width / 2, y - h / 2);
2261 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2263 break;
2265 case ButtonPress:
2266 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2267 XSync(dpy, 0);
2268 *x_ret = x - width / 2;
2269 *y_ret = y - h / 2;
2270 XUngrabPointer(dpy, CurrentTime);
2271 XUngrabKeyboard(dpy, CurrentTime);
2272 /* get rid of the geometry window */
2273 WMUnmapWidget(scr->gview);
2274 return;
2276 default:
2277 WMHandleEvent(&event);
2278 break;