wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / src / moveres.c
blob26b8cf964c82d36a5f2b12cc13ca4556fe5b3365
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))
63 /****** Global Variables ******/
64 extern Cursor wCursor[WCUR_LAST];
67 *----------------------------------------------------------------------
68 * checkMouseSamplingRate-
69 * For lowering the mouse motion sampling rate for machines where
70 * it's too high (SGIs). If it returns False then the event should be
71 * ignored.
72 *----------------------------------------------------------------------
74 static Bool checkMouseSamplingRate(XEvent * ev)
76 static Time previousMotion = 0;
78 if (ev->type == MotionNotify) {
79 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
80 return False;
81 } else {
82 previousMotion = ev->xmotion.time;
85 return True;
89 *----------------------------------------------------------------------
90 * moveGeometryDisplayCentered
92 * routine that moves the geometry/position window on scr so it is
93 * centered over the given coordinates (x,y). Also the window position
94 * is clamped so it stays on the screen at all times.
95 *----------------------------------------------------------------------
97 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
99 unsigned int w = WMWidgetWidth(scr->gview);
100 unsigned int h = WMWidgetHeight(scr->gview);
101 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
103 x -= w / 2;
104 y -= h / 2;
106 /* dead area check */
107 if (scr->xine_info.count) {
108 WMRect rect;
109 int head, flags;
111 rect.pos.x = x;
112 rect.pos.y = y;
113 rect.size.width = w;
114 rect.size.height = h;
116 head = wGetRectPlacementInfo(scr, rect, &flags);
118 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
119 rect = wGetRectForHead(scr, head);
120 x1 = rect.pos.x;
121 y1 = rect.pos.y;
122 x2 = x1 + rect.size.width;
123 y2 = y1 + rect.size.height;
127 if (x < x1 + 1)
128 x = x1 + 1;
129 else if (x > (x2 - w))
130 x = x2 - w;
132 if (y < y1 + 1)
133 y = y1 + 1;
134 else if (y > (y2 - h))
135 y = y2 - h;
137 WMMoveWidget(scr->gview, x, y);
140 static void showPosition(WWindow * wwin, int x, int y)
142 WScreen *scr = wwin->screen_ptr;
144 if (wPreferences.move_display == WDIS_NEW) {
145 #if 0
146 int width = wwin->frame->core->width;
147 int height = wwin->frame->core->height;
149 GC lgc = scr->line_gc;
150 XSetForeground(dpy, lgc, scr->line_pixel);
151 sprintf(num, "%i", x);
153 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
154 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
155 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
156 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
157 #endif
158 } else {
159 WSetGeometryViewShownPosition(scr->gview, x, y);
163 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
165 WScreen *scr = wwin->screen_ptr;
166 WMRect rect;
168 wPreferences.move_display++;
169 wPreferences.move_display %= NUM_DISPLAYS;
171 if (wPreferences.move_display == WDIS_NEW) {
172 wPreferences.move_display++;
173 wPreferences.move_display %= NUM_DISPLAYS;
176 if (wPreferences.move_display == WDIS_NONE) {
177 WMUnmapWidget(scr->gview);
178 } else {
179 if (wPreferences.move_display == WDIS_CENTER) {
180 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
181 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
182 rect.pos.y + rect.size.height / 2);
183 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
184 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
185 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
186 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
187 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
189 WMMapWidget(scr->gview);
193 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
195 WScreen *scr = wwin->screen_ptr;
196 WMRect rect;
198 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
199 return;
200 } else if (wPreferences.move_display == WDIS_CENTER) {
201 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
202 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
203 rect.pos.y + rect.size.height / 2);
204 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
205 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
206 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
207 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
208 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
210 WMMapWidget(scr->gview);
211 WSetGeometryViewShownPosition(scr->gview, x, y);
214 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
216 WScreen *scr = wwin->screen_ptr;
217 Window root = scr->root_win;
218 GC gc = scr->line_gc;
219 int ty, by, my, x, y, mx, s;
220 char num[16];
221 XSegment segment[4];
222 int fw, fh;
224 /* This seems necessary for some odd reason (too lazy to write x1-1 and
225 * x2-1 everywhere below in the code). But why only for x? */
226 x1--;
227 x2--;
229 if (HAS_BORDER_WITH_SELECT(wwin)) {
230 x1 += scr->frame_border_width;
231 x2 += scr->frame_border_width;
232 y1 += scr->frame_border_width;
233 y2 += scr->frame_border_width;
236 ty = y1 + wwin->frame->top_width;
237 by = y2 - wwin->frame->bottom_width;
239 if (wPreferences.size_display == WDIS_NEW) {
240 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
241 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
243 XSetForeground(dpy, gc, scr->line_pixel);
245 /* vertical geometry */
246 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
247 x = x2;
248 s = -15;
249 } else {
250 x = x1;
251 s = 15;
253 my = (ty + by) / 2;
255 /* top arrow & end bar */
256 segment[0].x1 = x - (s + 6);
257 segment[0].y1 = ty;
258 segment[0].x2 = x - (s - 10);
259 segment[0].y2 = ty;
261 /* arrowhead */
262 segment[1].x1 = x - (s - 2);
263 segment[1].y1 = ty + 1;
264 segment[1].x2 = x - (s - 5);
265 segment[1].y2 = ty + 7;
267 segment[2].x1 = x - (s - 2);
268 segment[2].y1 = ty + 1;
269 segment[2].x2 = x - (s + 1);
270 segment[2].y2 = ty + 7;
272 /* line */
273 segment[3].x1 = x - (s - 2);
274 segment[3].y1 = ty + 1;
275 segment[3].x2 = x - (s - 2);
276 segment[3].y2 = my - fh / 2 - 1;
278 XDrawSegments(dpy, root, gc, segment, 4);
280 /* bottom arrow & end bar */
281 segment[0].y1 = by;
282 segment[0].y2 = by;
284 /* arrowhead */
285 segment[1].y1 = by - 1;
286 segment[1].y2 = by - 7;
288 segment[2].y1 = by - 1;
289 segment[2].y2 = by - 7;
291 /* line */
292 segment[3].y1 = my + fh / 2 + 2;
293 segment[3].y2 = by - 1;
295 XDrawSegments(dpy, root, gc, segment, 4);
297 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
298 wwin->normal_hints->height_inc);
299 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
301 /* Display the height. */
302 XSetFont(dpy, gc, scr->tech_draw_font->fid);
303 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
304 strlen(num));
306 /* horizontal geometry */
307 if (y1 < 15) {
308 y = y2;
309 s = -15;
310 } else {
311 y = y1;
312 s = 15;
314 mx = x1 + (x2 - x1) / 2;
315 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
316 wwin->normal_hints->width_inc);
317 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
319 /* left arrow & end bar */
320 segment[0].x1 = x1;
321 segment[0].y1 = y - (s + 6);
322 segment[0].x2 = x1;
323 segment[0].y2 = y - (s - 10);
325 /* arrowhead */
326 segment[1].x1 = x1 + 7;
327 segment[1].y1 = y - (s + 1);
328 segment[1].x2 = x1 + 1;
329 segment[1].y2 = y - (s - 2);
331 segment[2].x1 = x1 + 1;
332 segment[2].y1 = y - (s - 2);
333 segment[2].x2 = x1 + 7;
334 segment[2].y2 = y - (s - 5);
336 /* line */
337 segment[3].x1 = x1 + 1;
338 segment[3].y1 = y - (s - 2);
339 segment[3].x2 = mx - fw / 2 - 2;
340 segment[3].y2 = y - (s - 2);
342 XDrawSegments(dpy, root, gc, segment, 4);
344 /* right arrow & end bar */
345 segment[0].x1 = x2 + 1;
346 segment[0].x2 = x2 + 1;
348 /* arrowhead */
349 segment[1].x1 = x2 - 6;
350 segment[1].x2 = x2;
352 segment[2].x1 = x2;
353 segment[2].x2 = x2 - 6;
355 /* line */
356 segment[3].x1 = mx + fw / 2 + 2;
357 segment[3].x2 = x2;
359 XDrawSegments(dpy, root, gc, segment, 4);
361 /* Display the width. */
362 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
363 strlen(num));
364 } else {
365 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
366 / wwin->normal_hints->width_inc,
367 (by - ty - wwin->normal_hints->base_height)
368 / wwin->normal_hints->height_inc);
372 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
374 WScreen *scr = wwin->screen_ptr;
375 WMRect rect;
377 wPreferences.size_display++;
378 wPreferences.size_display %= NUM_DISPLAYS;
380 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
381 WMUnmapWidget(scr->gview);
382 } else {
383 if (wPreferences.size_display == WDIS_CENTER) {
384 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
385 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
386 rect.pos.y + rect.size.height / 2);
387 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
388 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
389 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
390 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
391 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
393 WMMapWidget(scr->gview);
394 showGeometry(wwin, x, y, x + w, y + h, dir);
398 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
400 WScreen *scr = wwin->screen_ptr;
401 WMRect rect;
403 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
404 return;
406 if (wPreferences.size_display == WDIS_CENTER) {
407 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
408 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
409 rect.pos.y + rect.size.height / 2);
410 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
411 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
412 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
413 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
414 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
416 WMMapWidget(scr->gview);
417 showGeometry(wwin, x, y, x + w, y + h, 0);
420 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
422 WWindow *tmpw;
423 WScreen *scr = wwin->screen_ptr;
424 int x, y;
426 if (!array || !WMGetArrayItemCount(array)) {
427 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
428 } else {
429 WMArrayIterator iter;
431 WM_ITERATE_ARRAY(array, tmpw, iter) {
432 x = tmpw->frame_x + dx;
433 y = tmpw->frame_y + dy;
435 #if 1 /* XXX: with xinerama patch was #if 0, check this */
436 /* don't let windows become unreachable */
438 if (x + (int)tmpw->frame->core->width < 20)
439 x = 20 - (int)tmpw->frame->core->width;
440 else if (x + 20 > scr->scr_width)
441 x = scr->scr_width - 20;
443 if (y + (int)tmpw->frame->core->height < 20)
444 y = 20 - (int)tmpw->frame->core->height;
445 else if (y + 20 > scr->scr_height)
446 y = scr->scr_height - 20;
447 #else
448 wScreenBringInside(scr, &x, &y,
449 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
450 #endif
452 wWindowMove(tmpw, x, y);
457 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
459 Window root = wwin->screen_ptr->root_win;
460 GC gc = wwin->screen_ptr->frame_gc;
461 int h = 0;
462 int bottom = 0;
464 if (HAS_BORDER_WITH_SELECT(wwin)) {
465 x += wwin->screen_ptr->frame_border_width;
466 y += wwin->screen_ptr->frame_border_width;
469 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
470 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
471 TITLEBAR_EXTEND_SPACE) * 2;
473 if (h > wPreferences.window_title_max_height)
474 h = wPreferences.window_title_max_height;
476 if (h < wPreferences.window_title_min_height)
477 h = wPreferences.window_title_min_height;
479 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
480 /* Can't use wwin-frame->bottom_width because, in some cases
481 (e.g. interactive placement), frame does not point to anything. */
482 bottom = RESIZEBAR_HEIGHT;
484 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
486 if (h > 0) {
487 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
489 if (bottom > 0) {
490 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
494 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
496 WWindow *tmpw;
497 int scr_width = wwin->screen_ptr->scr_width;
498 int scr_height = wwin->screen_ptr->scr_height;
499 int x, y;
501 if (!array) {
503 x = wwin->frame_x + dx;
504 y = wwin->frame_y + dy;
506 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
508 } else {
509 WMArrayIterator iter;
511 WM_ITERATE_ARRAY(array, tmpw, iter) {
512 x = tmpw->frame_x + dx;
513 y = tmpw->frame_y + dy;
515 /* don't let windows become unreachable */
516 #if 1 /* XXX: was 0 in XINERAMA patch, check */
517 if (x + (int)tmpw->frame->core->width < 20)
518 x = 20 - (int)tmpw->frame->core->width;
519 else if (x + 20 > scr_width)
520 x = scr_width - 20;
522 if (y + (int)tmpw->frame->core->height < 20)
523 y = 20 - (int)tmpw->frame->core->height;
524 else if (y + 20 > scr_height)
525 y = scr_height - 20;
527 #else
528 wScreenBringInside(wwin->screen_ptr, &x, &y,
529 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
530 #endif
532 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
537 static void flushMotion(void)
539 XEvent ev;
541 XSync(dpy, False);
542 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
545 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
547 /* do not let window be unmapped */
548 if (opaque_move) {
549 wwin->flags.changing_workspace = 1;
550 wWindowChangeWorkspace(wwin, new_workspace);
552 /* go to new workspace */
553 wWorkspaceChange(scr, new_workspace);
555 wwin->flags.changing_workspace = 0;
557 if (rewind)
558 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
559 else
560 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
562 flushMotion();
564 if (!opaque_move) {
565 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
566 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
567 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
571 typedef struct {
572 /* arrays of WWindows sorted by the respective border position */
573 WWindow **topList; /* top border */
574 WWindow **leftList; /* left border */
575 WWindow **rightList; /* right border */
576 WWindow **bottomList; /* bottom border */
577 int count;
579 /* index of window in the above lists indicating the relative position
580 * of the window with the others */
581 int topIndex;
582 int leftIndex;
583 int rightIndex;
584 int bottomIndex;
586 int rubCount; /* for workspace switching */
588 int winWidth, winHeight; /* width/height of the window */
589 int realX, realY; /* actual position of the window */
590 int calcX, calcY; /* calculated position of window */
591 int omouseX, omouseY; /* old mouse position */
592 int mouseX, mouseY; /* last known position of the pointer */
593 } MoveData;
595 #define WTOP(w) (w)->frame_y
596 #define WLEFT(w) (w)->frame_x
597 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
598 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
599 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
600 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
602 static int compareWTop(const void *a, const void *b)
604 WWindow *wwin1 = *(WWindow **) a;
605 WWindow *wwin2 = *(WWindow **) b;
607 if (WTOP(wwin1) > WTOP(wwin2))
608 return -1;
609 else if (WTOP(wwin1) < WTOP(wwin2))
610 return 1;
611 else
612 return 0;
615 static int compareWLeft(const void *a, const void *b)
617 WWindow *wwin1 = *(WWindow **) a;
618 WWindow *wwin2 = *(WWindow **) b;
620 if (WLEFT(wwin1) > WLEFT(wwin2))
621 return -1;
622 else if (WLEFT(wwin1) < WLEFT(wwin2))
623 return 1;
624 else
625 return 0;
628 static int compareWRight(const void *a, const void *b)
630 WWindow *wwin1 = *(WWindow **) a;
631 WWindow *wwin2 = *(WWindow **) b;
633 if (WRIGHT(wwin1) < WRIGHT(wwin2))
634 return -1;
635 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
636 return 1;
637 else
638 return 0;
641 static int compareWBottom(const void *a, const void *b)
643 WWindow *wwin1 = *(WWindow **) a;
644 WWindow *wwin2 = *(WWindow **) b;
646 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
647 return -1;
648 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
649 return 1;
650 else
651 return 0;
654 static void updateResistance(WWindow * wwin, MoveData * data, int newX, int newY)
656 int i;
657 int newX2 = newX + data->winWidth;
658 int newY2 = newY + data->winHeight;
659 Bool ok = False;
661 if (newX < data->realX) {
662 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
663 ok = True;
664 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
665 ok = True;
667 } else if (newX > data->realX) {
668 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
669 ok = True;
670 } else if (data->rightIndex <= data->count - 1
671 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
672 ok = True;
676 if (!ok) {
677 if (newY < data->realY) {
678 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
679 ok = True;
680 } else if (data->topIndex <= data->count - 1
681 && newY2 <= WTOP(data->topList[data->topIndex])) {
682 ok = True;
684 } else if (newY > data->realY) {
685 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
686 ok = True;
687 } else if (data->bottomIndex <= data->count - 1
688 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
689 ok = True;
694 if (!ok)
695 return;
697 /* TODO: optimize this */
698 if (data->realY < WBOTTOM(data->bottomList[0])) {
699 data->bottomIndex = 0;
701 if (data->realX < WRIGHT(data->rightList[0])) {
702 data->rightIndex = 0;
704 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
705 data->leftIndex = 0;
707 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
708 data->topIndex = 0;
710 for (i = 0; i < data->count; i++) {
711 if (data->realY > WBOTTOM(data->bottomList[i])) {
712 data->bottomIndex = i + 1;
714 if (data->realX > WRIGHT(data->rightList[i])) {
715 data->rightIndex = i + 1;
717 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
718 data->leftIndex = i + 1;
720 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
721 data->topIndex = i + 1;
726 static void freeMoveData(MoveData * data)
728 if (data->topList)
729 wfree(data->topList);
730 if (data->leftList)
731 wfree(data->leftList);
732 if (data->rightList)
733 wfree(data->rightList);
734 if (data->bottomList)
735 wfree(data->bottomList);
738 static void updateMoveData(WWindow * wwin, MoveData * data)
740 WScreen *scr = wwin->screen_ptr;
741 WWindow *tmp;
742 int i;
744 data->count = 0;
745 tmp = scr->focused_window;
746 while (tmp) {
747 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
748 && !tmp->flags.miniaturized
749 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
750 data->topList[data->count] = tmp;
751 data->leftList[data->count] = tmp;
752 data->rightList[data->count] = tmp;
753 data->bottomList[data->count] = tmp;
754 data->count++;
756 tmp = tmp->prev;
759 if (data->count == 0) {
760 data->topIndex = 0;
761 data->leftIndex = 0;
762 data->rightIndex = 0;
763 data->bottomIndex = 0;
764 return;
767 /* order from closest to the border of the screen to farthest */
769 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
770 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
771 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
772 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
774 /* figure the position of the window relative to the others */
776 data->topIndex = -1;
777 data->leftIndex = -1;
778 data->rightIndex = -1;
779 data->bottomIndex = -1;
781 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
782 data->bottomIndex = 0;
784 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
785 data->rightIndex = 0;
787 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
788 data->leftIndex = 0;
790 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
791 data->topIndex = 0;
793 for (i = 0; i < data->count; i++) {
794 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
795 data->bottomIndex = i + 1;
797 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
798 data->rightIndex = i + 1;
800 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
801 data->leftIndex = i + 1;
803 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
804 data->topIndex = i + 1;
809 static void initMoveData(WWindow * wwin, MoveData * data)
811 int i;
812 WWindow *tmp;
814 memset(data, 0, sizeof(MoveData));
816 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
818 if (i > 1) {
819 data->topList = wmalloc(sizeof(WWindow *) * i);
820 data->leftList = wmalloc(sizeof(WWindow *) * i);
821 data->rightList = wmalloc(sizeof(WWindow *) * i);
822 data->bottomList = wmalloc(sizeof(WWindow *) * i);
824 updateMoveData(wwin, data);
827 data->realX = wwin->frame_x;
828 data->realY = wwin->frame_y;
829 data->calcX = wwin->frame_x;
830 data->calcY = wwin->frame_y;
832 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
833 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
836 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
838 WScreen *scr = wwin->screen_ptr;
839 Bool changed = False;
841 if (data->mouseX <= 1) {
842 if (scr->current_workspace > 0) {
844 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1, True);
845 changed = True;
846 data->rubCount = 0;
848 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
850 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1, True);
851 changed = True;
852 data->rubCount = 0;
854 } else if (data->mouseX >= scr->scr_width - 2) {
856 if (scr->current_workspace == scr->workspace_count - 1) {
858 if (wPreferences.ws_cycle || scr->workspace_count == MAX_WORKSPACES) {
860 crossWorkspace(scr, wwin, opaqueMove, 0, False);
861 changed = True;
862 data->rubCount = 0;
864 /* if user insists on trying to go to next workspace even when
865 * it's already the last, create a new one */
866 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
868 /* detect user "rubbing" the window against the edge */
869 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
871 data->rubCount = -(data->rubCount + 1);
873 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
875 data->rubCount = -data->rubCount + 1;
878 /* create a new workspace */
879 if (abs(data->rubCount) > 2) {
880 /* go to next workspace */
881 wWorkspaceNew(scr);
883 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
884 changed = True;
885 data->rubCount = 0;
887 } else if (scr->current_workspace < scr->workspace_count) {
889 /* go to next workspace */
890 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
891 changed = True;
892 data->rubCount = 0;
894 } else {
895 data->rubCount = 0;
898 return changed;
901 static void
902 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
903 Bool opaqueMove, int newMouseX, int newMouseY)
905 WScreen *scr = wwin->screen_ptr;
906 int dx, dy; /* how much mouse moved */
907 int winL, winR, winT, winB; /* requested new window position */
908 int newX, newY; /* actual new window position */
909 Bool hresist, vresist;
910 Bool attract;
912 hresist = False;
913 vresist = False;
915 /* check the direction of the movement */
916 dx = newMouseX - data->mouseX;
917 dy = newMouseY - data->mouseY;
919 data->omouseX = data->mouseX;
920 data->omouseY = data->mouseY;
921 data->mouseX = newMouseX;
922 data->mouseY = newMouseY;
924 winL = data->calcX + dx;
925 winR = data->calcX + data->winWidth + dx;
926 winT = data->calcY + dy;
927 winB = data->calcY + data->winHeight + dy;
929 newX = data->realX;
930 newY = data->realY;
932 if (doResistance) {
933 int l_edge, r_edge;
934 int edge_l, edge_r;
935 int t_edge, b_edge;
936 int edge_t, edge_b;
937 int resist;
939 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
940 attract = wPreferences.attract;
941 /* horizontal movement: check horizontal edge resistances */
942 if (dx || dy) {
943 WMRect rect;
944 int i, head;
945 /* window is the leftmost window: check against screen edge */
947 /* Add inter head resistance 1/2 (if needed) */
948 head = wGetHeadForPointerLocation(scr);
949 rect = wGetRectForHead(scr, head);
951 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
952 edge_l = l_edge - resist;
953 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
954 r_edge = edge_r + resist;
956 /* 1 */
957 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
958 WWindow *looprw;
960 for (i = data->rightIndex - 1; i >= 0; i--) {
961 looprw = data->rightList[i];
962 if (!(data->realY > WBOTTOM(looprw)
963 || (data->realY + data->winHeight) < WTOP(looprw))) {
964 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
965 l_edge = WRIGHT(looprw) + 1;
966 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
968 break;
972 if (attract) {
973 for (i = data->rightIndex; i < data->count; i++) {
974 looprw = data->rightList[i];
975 if (!(data->realY > WBOTTOM(looprw)
976 || (data->realY + data->winHeight) < WTOP(looprw))) {
977 r_edge = WRIGHT(looprw) + 1;
978 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
979 break;
985 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
986 WWindow *looprw;
988 for (i = data->leftIndex - 1; i >= 0; i--) {
989 looprw = data->leftList[i];
990 if (!(data->realY > WBOTTOM(looprw)
991 || (data->realY + data->winHeight) < WTOP(looprw))) {
992 if (attract
993 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
994 && dx > 0)) {
995 edge_r = WLEFT(looprw);
996 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
998 break;
1002 if (attract)
1003 for (i = data->leftIndex; i < data->count; i++) {
1004 looprw = data->leftList[i];
1005 if (!(data->realY > WBOTTOM(looprw)
1006 || (data->realY + data->winHeight) < WTOP(looprw))) {
1007 edge_l = WLEFT(looprw);
1008 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1009 break;
1015 printf("%d %d\n",winL,winR);
1016 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1019 if ((winL - l_edge) < (r_edge - winL)) {
1020 if (resist > 0) {
1021 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1022 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1023 newX = l_edge;
1024 hresist = True;
1027 } else {
1028 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1029 newX = r_edge;
1030 hresist = True;
1034 if ((winR - edge_l) < (edge_r - winR)) {
1035 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1036 newX = edge_l - data->winWidth;
1037 hresist = True;
1039 } else {
1040 if (resist > 0) {
1041 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1042 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1043 newX = edge_r - data->winWidth;
1044 hresist = True;
1049 /* VeRT */
1050 /* Add inter head resistance 2/2 (if needed) */
1051 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1052 edge_t = t_edge - resist;
1053 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1054 b_edge = edge_b + resist;
1056 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1057 WWindow *looprw;
1059 for (i = data->bottomIndex - 1; i >= 0; i--) {
1060 looprw = data->bottomList[i];
1061 if (!(data->realX > WRIGHT(looprw)
1062 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1063 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1064 t_edge = WBOTTOM(looprw) + 1;
1065 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1067 break;
1071 if (attract) {
1072 for (i = data->bottomIndex; i < data->count; i++) {
1073 looprw = data->bottomList[i];
1074 if (!(data->realX > WRIGHT(looprw)
1075 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1076 b_edge = WBOTTOM(looprw) + 1;
1077 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1078 break;
1084 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1085 WWindow *looprw;
1087 for (i = data->topIndex - 1; i >= 0; i--) {
1088 looprw = data->topList[i];
1089 if (!(data->realX > WRIGHT(looprw)
1090 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1091 if (attract
1092 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1093 && dy > 0)) {
1094 edge_b = WTOP(looprw);
1095 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1097 break;
1101 if (attract)
1102 for (i = data->topIndex; i < data->count; i++) {
1103 looprw = data->topList[i];
1104 if (!(data->realX > WRIGHT(looprw)
1105 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1106 edge_t = WTOP(looprw);
1107 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1108 break;
1113 if ((winT - t_edge) < (b_edge - winT)) {
1114 if (resist > 0) {
1115 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1116 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1117 newY = t_edge;
1118 vresist = True;
1121 } else {
1122 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1123 newY = b_edge;
1124 vresist = True;
1128 if ((winB - edge_t) < (edge_b - winB)) {
1129 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1130 newY = edge_t - data->winHeight;
1131 vresist = True;
1133 } else {
1134 if (resist > 0) {
1135 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1136 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1137 newY = edge_b - data->winHeight;
1138 vresist = True;
1143 /* END VeRT */
1147 /* update window position */
1148 data->calcX += dx;
1149 data->calcY += dy;
1151 if (((dx > 0 && data->calcX - data->realX > 0)
1152 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1153 newX = data->calcX;
1155 if (((dy > 0 && data->calcY - data->realY > 0)
1156 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1157 newY = data->calcY;
1159 if (data->realX != newX || data->realY != newY) {
1161 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1162 showPosition(wwin, data->realX, data->realY);
1164 if (opaqueMove) {
1165 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1166 } else {
1167 /* erase frames */
1168 drawFrames(wwin, scr->selected_windows,
1169 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1172 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1174 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1177 if (!opaqueMove) {
1178 /* draw frames */
1179 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1182 if (!scr->selected_windows) {
1183 showPosition(wwin, newX, newY);
1187 /* recalc relative window position */
1188 if (doResistance && (data->realX != newX || data->realY != newY)) {
1189 updateResistance(wwin, data, newX, newY);
1192 data->realX = newX;
1193 data->realY = newY;
1196 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1198 #define MOVABLE_BIT 0x01
1199 #define RESIZABLE_BIT 0x02
1201 int wKeyboardMoveResizeWindow(WWindow * wwin)
1203 WScreen *scr = wwin->screen_ptr;
1204 Window root = scr->root_win;
1205 XEvent event;
1206 int w = wwin->frame->core->width;
1207 int h = wwin->frame->core->height;
1208 int scr_width = wwin->screen_ptr->scr_width;
1209 int scr_height = wwin->screen_ptr->scr_height;
1210 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1211 int src_x = wwin->frame_x;
1212 int src_y = wwin->frame_y;
1213 int original_w = w;
1214 int original_h = h;
1215 int done, off_x, off_y, ww, wh;
1216 int kspeed = _KS;
1217 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1218 Time lastTime = 0;
1219 KeyCode shiftl, shiftr, ctrlmode;
1220 KeySym keysym = NoSymbol;
1221 int moment = 0;
1222 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1223 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1224 ? wGetHeadForWindow(wwin)
1225 : scr->xine_info.primary_head);
1227 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1228 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1229 ctrlmode = done = off_x = off_y = 0;
1231 if (modes == RESIZABLE_BIT) {
1232 ctrlmode = 1;
1235 XSync(dpy, False);
1236 wusleep(10000);
1237 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1239 if (!wwin->flags.selected) {
1240 wUnselectWindows(scr);
1242 XGrabServer(dpy);
1243 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1244 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1245 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1249 if (!opaqueMoveResize) {
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 } else {
1256 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1259 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1260 mapPositionDisplay(wwin, src_x, src_y, w, h);
1263 ww = w;
1264 wh = h;
1265 while (1) {
1267 looper.ox=off_x;
1268 looper.oy=off_y;
1270 do {
1271 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1272 | ButtonPressMask | ExposureMask, &event);
1273 if (event.type == Expose) {
1274 WMHandleEvent(&event);
1276 } while (event.type == Expose);
1278 if (!opaqueMoveResize) {
1279 if (wwin->flags.shaded || scr->selected_windows) {
1280 if (scr->selected_windows)
1281 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1282 else
1283 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1284 /*** I HATE EDGE RESISTANCE - ]d ***/
1285 } else {
1286 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1290 if (ctrlmode)
1291 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1294 XUngrabServer(dpy);
1295 XSync(dpy, False);
1297 switch (event.type) {
1298 case KeyPress:
1299 /* accelerate */
1300 if (event.xkey.time - lastTime > 50) {
1301 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1302 } else {
1303 if (kspeed < 20) {
1304 kspeed++;
1307 if (kspeed < _KS)
1308 kspeed = _KS;
1309 lastTime = event.xkey.time;
1310 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1311 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1312 ctrlmode = 1;
1313 wUnselectWindows(scr);
1314 } else {
1315 ctrlmode = 0;
1318 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1319 if (ctrlmode)
1320 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1321 else
1322 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1323 } else {
1325 keysym = XLookupKeysym(&event.xkey, 0);
1326 switch (keysym) {
1327 case XK_Return:
1328 done = 2;
1329 break;
1330 case XK_Escape:
1331 done = 1;
1332 break;
1333 case XK_Up:
1334 #ifdef XK_KP_Up
1335 case XK_KP_Up:
1336 #endif
1337 case XK_k:
1338 if (ctrlmode) {
1339 if (moment != UP)
1340 h = wh;
1341 h -= kspeed;
1342 moment = UP;
1343 if (h < 1)
1344 h = 1;
1345 } else
1346 off_y -= kspeed;
1347 break;
1348 case XK_Down:
1349 #ifdef XK_KP_Down
1350 case XK_KP_Down:
1351 #endif
1352 case XK_j:
1353 if (ctrlmode) {
1354 if (moment != DOWN)
1355 h = wh;
1356 h += kspeed;
1357 moment = DOWN;
1358 } else
1359 off_y += kspeed;
1360 break;
1361 case XK_Left:
1362 #ifdef XK_KP_Left
1363 case XK_KP_Left:
1364 #endif
1365 case XK_h:
1366 if (ctrlmode) {
1367 if (moment != LEFT)
1368 w = ww;
1369 w -= kspeed;
1370 if (w < 1)
1371 w = 1;
1372 moment = LEFT;
1373 } else
1374 off_x -= kspeed;
1375 break;
1376 case XK_Right:
1377 #ifdef XK_KP_Right
1378 case XK_KP_Right:
1379 #endif
1380 case XK_l:
1381 if (ctrlmode) {
1382 if (moment != RIGHT)
1383 w = ww;
1384 w += kspeed;
1385 moment = RIGHT;
1386 } else
1387 off_x += kspeed;
1388 break;
1391 ww = w;
1392 wh = h;
1393 wh -= vert_border;
1394 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1395 wh += vert_border;
1397 if (wPreferences.ws_cycle) {
1398 if (src_x + off_x + ww < 20) {
1399 if (!scr->current_workspace) {
1400 wWorkspaceChange(scr, scr->workspace_count - 1);
1401 } else
1402 wWorkspaceChange(scr, scr->current_workspace - 1);
1403 off_x += scr_width;
1404 } else if (src_x + off_x + 20 > scr_width) {
1405 if (scr->current_workspace == scr->workspace_count - 1) {
1406 wWorkspaceChange(scr, 0);
1407 } else
1408 wWorkspaceChange(scr, scr->current_workspace + 1);
1409 off_x -= scr_width;
1411 } else {
1412 if (src_x + off_x + ww < 20)
1413 off_x = 20 - ww - src_x;
1414 else if (src_x + off_x + 20 > scr_width)
1415 off_x = scr_width - 20 - src_x;
1418 if (src_y + off_y + wh < 20) {
1419 off_y = 20 - wh - src_y;
1420 } else if (src_y + off_y + 20 > scr_height) {
1421 off_y = scr_height - 20 - src_y;
1424 break;
1425 case ButtonPress:
1426 case ButtonRelease:
1427 done = 1;
1428 break;
1429 case Expose:
1430 WMHandleEvent(&event);
1431 while (XCheckTypedEvent(dpy, Expose, &event)) {
1432 WMHandleEvent(&event);
1434 break;
1436 default:
1437 WMHandleEvent(&event);
1438 break;
1441 XGrabServer(dpy);
1442 /*xxx */
1444 if (wwin->flags.shaded && !scr->selected_windows) {
1445 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1446 } else {
1447 if (ctrlmode) {
1448 WMUnmapWidget(scr->gview);
1449 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1450 } else if (!scr->selected_windows) {
1451 WMUnmapWidget(scr->gview);
1452 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1456 if (!opaqueMoveResize) {
1457 if (wwin->flags.shaded || scr->selected_windows) {
1458 if (scr->selected_windows)
1459 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1460 else
1461 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1462 } else {
1463 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1467 if (ctrlmode) {
1468 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1470 } else if (!scr->selected_windows)
1471 showPosition(wwin, src_x + off_x, src_y + off_y);
1473 if (opaqueMoveResize) {
1474 XUngrabServer(dpy);
1475 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1478 if (done) {
1479 scr->keymove_tick = 0;
1481 WMDeleteTimerWithClientData(&looper);
1483 if (!opaqueMoveResize) {/*ctrlmode=> resize */
1484 if (wwin->flags.shaded || scr->selected_windows) {
1485 if (scr->selected_windows)
1486 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1487 else
1488 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1489 } else {
1490 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1494 if (ctrlmode) {
1495 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1496 src_y + off_y + wh, 0);
1497 WMUnmapWidget(scr->gview);
1498 } else
1499 WMUnmapWidget(scr->gview);
1501 XUngrabKeyboard(dpy, CurrentTime);
1502 XUngrabPointer(dpy, CurrentTime);
1503 XUngrabServer(dpy);
1505 if (done == 2) {
1506 if (wwin->flags.shaded || scr->selected_windows) {
1507 if (!scr->selected_windows) {
1508 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1509 wWindowSynthConfigureNotify(wwin);
1510 } else {
1511 WMArrayIterator iter;
1512 WWindow *foo;
1514 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1516 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1517 wWindowSynthConfigureNotify(foo);
1520 } else {
1521 if (ww != original_w)
1522 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
1524 if (wh != original_h)
1525 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1527 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1528 wWindowSynthConfigureNotify(wwin);
1530 wWindowChangeWorkspace(wwin, scr->current_workspace);
1531 wSetFocusTo(scr, wwin);
1534 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1535 head != wGetHeadForWindow(wwin)) {
1536 wArrangeIcons(scr, True);
1539 update_saved_geometry(wwin);
1541 return 1;
1547 *----------------------------------------------------------------------
1548 * wMouseMoveWindow--
1549 * Move the named window and the other selected ones (if any),
1550 * interactively. Also shows the position of the window, if only one
1551 * window is being moved.
1552 * If the window is not on the selected window list, the selected
1553 * windows are deselected.
1554 * If shift is pressed during the operation, the position display
1555 * is changed to another type.
1557 * Returns:
1558 * True if the window was moved, False otherwise.
1560 * Side effects:
1561 * The window(s) position is changed, and the client(s) are
1562 * notified about that.
1563 * The position display configuration may be changed.
1564 *----------------------------------------------------------------------
1566 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1568 WScreen *scr = wwin->screen_ptr;
1569 XEvent event;
1570 Window root = scr->root_win;
1571 KeyCode shiftl, shiftr;
1572 Bool done = False;
1573 int started = 0;
1574 int warped = 0;
1575 /* This needs not to change while moving, else bad things can happen */
1576 int opaqueMove = wPreferences.opaque_move;
1577 MoveData moveData;
1578 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1579 ? wGetHeadForWindow(wwin)
1580 : scr->xine_info.primary_head);
1582 if (!IS_MOVABLE(wwin))
1583 return False;
1585 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1586 XSetWindowAttributes attr;
1588 attr.save_under = True;
1589 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1592 initMoveData(wwin, &moveData);
1594 moveData.mouseX = ev->xmotion.x_root;
1595 moveData.mouseY = ev->xmotion.y_root;
1597 if (!wwin->flags.selected) {
1598 /* this window is not selected, unselect others and move only wwin */
1599 wUnselectWindows(scr);
1601 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1602 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1603 while (!done) {
1604 if (warped) {
1605 int junk;
1606 Window junkw;
1608 /* XWarpPointer() doesn't seem to generate Motion events, so
1609 * we've got to simulate them */
1610 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1611 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1612 } else {
1613 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1614 | PointerMotionHintMask
1615 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1617 if (event.type == MotionNotify) {
1618 /* compress MotionNotify events */
1619 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1620 if (!checkMouseSamplingRate(&event))
1621 continue;
1624 switch (event.type) {
1625 case KeyPress:
1626 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1627 && started && !scr->selected_windows) {
1629 if (!opaqueMove) {
1630 drawFrames(wwin, scr->selected_windows,
1631 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1634 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1635 showPosition(wwin, moveData.realX, moveData.realY);
1636 XUngrabServer(dpy);
1638 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1639 moveData.winWidth, moveData.winHeight);
1641 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1642 XGrabServer(dpy);
1643 showPosition(wwin, moveData.realX, moveData.realY);
1646 if (!opaqueMove) {
1647 drawFrames(wwin, scr->selected_windows,
1648 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1650 /*} else {
1651 WMHandleEvent(&event); this causes problems needs fixing */
1653 break;
1655 case MotionNotify:
1656 if (started) {
1657 updateWindowPosition(wwin, &moveData,
1658 scr->selected_windows == NULL
1659 && wPreferences.edge_resistance > 0,
1660 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1662 if (!warped && !wPreferences.no_autowrap) {
1663 int oldWorkspace = scr->current_workspace;
1665 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1666 showPosition(wwin, moveData.realX, moveData.realY);
1667 XUngrabServer(dpy);
1669 if (!opaqueMove) {
1670 drawFrames(wwin, scr->selected_windows,
1671 moveData.realX - wwin->frame_x,
1672 moveData.realY - wwin->frame_y);
1674 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1675 if (scr->current_workspace != oldWorkspace
1676 && wPreferences.edge_resistance > 0
1677 && scr->selected_windows == NULL)
1678 updateMoveData(wwin, &moveData);
1679 warped = 1;
1681 if (!opaqueMove) {
1682 drawFrames(wwin, scr->selected_windows,
1683 moveData.realX - wwin->frame_x,
1684 moveData.realY - wwin->frame_y);
1686 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1687 XSync(dpy, False);
1688 showPosition(wwin, moveData.realX, moveData.realY);
1689 XGrabServer(dpy);
1691 } else {
1692 warped = 0;
1694 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1695 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1697 XChangeActivePointerGrab(dpy, ButtonMotionMask
1698 | ButtonReleaseMask | ButtonPressMask,
1699 wCursor[WCUR_MOVE], CurrentTime);
1700 started = 1;
1701 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1703 if (!scr->selected_windows)
1704 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1705 moveData.winWidth, moveData.winHeight);
1707 if (started && !opaqueMove)
1708 drawFrames(wwin, scr->selected_windows, 0, 0);
1710 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1711 && !scr->selected_windows)) {
1712 XGrabServer(dpy);
1713 if (wPreferences.move_display == WDIS_NEW)
1714 showPosition(wwin, moveData.realX, moveData.realY);
1717 break;
1719 case ButtonPress:
1720 break;
1722 case ButtonRelease:
1723 if (event.xbutton.button != ev->xbutton.button)
1724 break;
1726 if (started) {
1727 XEvent e;
1728 if (!opaqueMove) {
1729 drawFrames(wwin, scr->selected_windows,
1730 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1731 XSync(dpy, 0);
1732 doWindowMove(wwin, scr->selected_windows,
1733 moveData.realX - wwin->frame_x,
1734 moveData.realY - wwin->frame_y);
1736 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1737 wWindowSynthConfigureNotify(wwin);
1738 #endif
1739 XUngrabKeyboard(dpy, CurrentTime);
1740 XUngrabServer(dpy);
1741 if (!opaqueMove) {
1742 wWindowChangeWorkspace(wwin, scr->current_workspace);
1743 wSetFocusTo(scr, wwin);
1745 if (wPreferences.move_display == WDIS_NEW)
1746 showPosition(wwin, moveData.realX, moveData.realY);
1748 /* discard all enter/leave events that happened until
1749 * the time the button was released */
1750 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1751 if (e.xcrossing.time > event.xbutton.time) {
1752 XPutBackEvent(dpy, &e);
1753 break;
1756 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1757 if (e.xcrossing.time > event.xbutton.time) {
1758 XPutBackEvent(dpy, &e);
1759 break;
1763 if (!scr->selected_windows) {
1764 /* get rid of the geometry window */
1765 WMUnmapWidget(scr->gview);
1768 done = True;
1769 break;
1771 default:
1772 if (started && !opaqueMove) {
1773 drawFrames(wwin, scr->selected_windows,
1774 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1775 XUngrabServer(dpy);
1776 WMHandleEvent(&event);
1777 XSync(dpy, False);
1778 XGrabServer(dpy);
1779 drawFrames(wwin, scr->selected_windows,
1780 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1781 } else {
1782 WMHandleEvent(&event);
1784 break;
1788 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1789 XSetWindowAttributes attr;
1791 attr.save_under = False;
1792 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1796 freeMoveData(&moveData);
1798 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1799 head != wGetHeadForWindow(wwin)) {
1800 wArrangeIcons(scr, True);
1803 if (started)
1804 update_saved_geometry(wwin);
1806 return started;
1809 #define RESIZEBAR 1
1810 #define HCONSTRAIN 2
1812 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1814 int w = wwin->frame->core->width - 1;
1815 int cw = wwin->frame->resizebar_corner_width;
1816 int dir;
1818 /* if not resizing through the resizebar */
1819 if (!(flags & RESIZEBAR)) {
1820 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1821 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1822 if (abs(dx) < 2 || abs(dy) < 2) {
1823 if (abs(dy) > abs(dx))
1824 xdir = 0;
1825 else
1826 ydir = 0;
1828 return (xdir | ydir);
1831 /* window is too narrow. allow diagonal resize */
1832 if (cw * 2 >= w) {
1833 int ydir;
1835 if (flags & HCONSTRAIN)
1836 ydir = 0;
1837 else
1838 ydir = DOWN;
1839 if (x < cw)
1840 return (LEFT | ydir);
1841 else
1842 return (RIGHT | ydir);
1844 /* vertical resize */
1845 if ((x > cw) && (x < w - cw))
1846 return DOWN;
1848 if (x < cw)
1849 dir = LEFT;
1850 else
1851 dir = RIGHT;
1853 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1854 dir |= DOWN;
1856 return dir;
1859 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1861 XEvent event;
1862 WScreen *scr = wwin->screen_ptr;
1863 Window root = scr->root_win;
1864 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1865 int fw = wwin->frame->core->width;
1866 int fh = wwin->frame->core->height;
1867 int fx = wwin->frame_x;
1868 int fy = wwin->frame_y;
1869 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1870 int orig_x, orig_y;
1871 int started;
1872 int dw, dh;
1873 int rw = fw, rh = fh;
1874 int rx1, ry1, rx2, ry2;
1875 int res = 0;
1876 KeyCode shiftl, shiftr;
1877 int orig_fx = fx;
1878 int orig_fy = fy;
1879 int orig_fw = fw;
1880 int orig_fh = fh;
1881 int original_fw = fw;
1882 int original_fh = fh;
1883 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1884 ? wGetHeadForWindow(wwin)
1885 : scr->xine_info.primary_head);
1886 int opaqueResize = wPreferences.opaque_resize;
1888 if (!IS_RESIZABLE(wwin))
1889 return;
1891 if (wwin->flags.shaded) {
1892 wwarning("internal error: tryein");
1893 return;
1895 orig_x = ev->xbutton.x_root;
1896 orig_y = ev->xbutton.y_root;
1898 started = 0;
1899 wUnselectWindows(scr);
1900 rx1 = fx;
1901 rx2 = fx + fw - 1;
1902 ry1 = fy;
1903 ry2 = fy + fh - 1;
1904 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1905 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1907 while (1) {
1908 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1909 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1910 if (!checkMouseSamplingRate(&event))
1911 continue;
1913 switch (event.type) {
1914 case KeyPress:
1915 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1916 if (!opaqueResize) {
1917 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1918 && started) {
1919 drawTransparentFrame(wwin, fx, fy, fw, fh);
1920 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1921 drawTransparentFrame(wwin, fx, fy, fw, fh);
1924 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1925 break;
1927 case MotionNotify:
1928 if (started) {
1929 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1931 dw = 0;
1932 dh = 0;
1934 orig_fx = fx;
1935 orig_fy = fy;
1936 orig_fw = fw;
1937 orig_fh = fh;
1939 if (res & LEFT)
1940 dw = orig_x - event.xmotion.x_root;
1941 else if (res & RIGHT)
1942 dw = event.xmotion.x_root - orig_x;
1943 if (res & UP)
1944 dh = orig_y - event.xmotion.y_root;
1945 else if (res & DOWN)
1946 dh = event.xmotion.y_root - orig_y;
1948 orig_x = event.xmotion.x_root;
1949 orig_y = event.xmotion.y_root;
1951 rw += dw;
1952 rh += dh;
1953 fw = rw;
1954 fh = rh - vert_border;
1955 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1956 fh += vert_border;
1957 if (res & LEFT)
1958 fx = rx2 - fw + 1;
1959 else if (res & RIGHT)
1960 fx = rx1;
1961 if (res & UP)
1962 fy = ry2 - fh + 1;
1963 else if (res & DOWN)
1964 fy = ry1;
1965 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1966 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1967 int tx, ty;
1968 Window junkw;
1969 int flags;
1971 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1972 orig_x, orig_y, &tx, &ty, &junkw);
1974 /* check if resizing through resizebar */
1975 if (is_resizebar)
1976 flags = RESIZEBAR;
1977 else
1978 flags = 0;
1980 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1981 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1982 flags |= HCONSTRAIN;
1984 res = getResizeDirection(wwin, tx, ty,
1985 orig_x - event.xmotion.x_root,
1986 orig_y - event.xmotion.y_root, flags);
1988 if (res == (UP | LEFT))
1989 XChangeActivePointerGrab(dpy, ButtonMotionMask
1990 | ButtonReleaseMask | ButtonPressMask,
1991 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1992 else if (res == (UP | RIGHT))
1993 XChangeActivePointerGrab(dpy, ButtonMotionMask
1994 | ButtonReleaseMask | ButtonPressMask,
1995 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1996 else if (res == (DOWN | LEFT))
1997 XChangeActivePointerGrab(dpy, ButtonMotionMask
1998 | ButtonReleaseMask | ButtonPressMask,
1999 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2000 else if (res == (DOWN | RIGHT))
2001 XChangeActivePointerGrab(dpy, ButtonMotionMask
2002 | ButtonReleaseMask | ButtonPressMask,
2003 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2004 else if (res == DOWN || res == UP)
2005 XChangeActivePointerGrab(dpy, ButtonMotionMask
2006 | ButtonReleaseMask | ButtonPressMask,
2007 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2008 else if (res & (DOWN | UP))
2009 XChangeActivePointerGrab(dpy, ButtonMotionMask
2010 | ButtonReleaseMask | ButtonPressMask,
2011 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2012 else if (res & (LEFT | RIGHT))
2013 XChangeActivePointerGrab(dpy, ButtonMotionMask
2014 | ButtonReleaseMask | ButtonPressMask,
2015 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2017 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2019 XGrabServer(dpy);
2021 /* Draw the resize frame for the first time. */
2022 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2024 if (!opaqueResize)
2025 drawTransparentFrame(wwin, fx, fy, fw, fh);
2027 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2029 started = 1;
2031 if (started) {
2032 if (!opaqueResize)
2033 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2035 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2036 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2038 if (!opaqueResize)
2039 drawTransparentFrame(wwin, fx, fy, fw, fh);
2041 if (fh != orig_fh || fw != orig_fw) {
2042 if (wPreferences.size_display == WDIS_NEW)
2043 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2044 orig_fy + orig_fh, res);
2046 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2049 if (opaqueResize) {
2050 /* Fist clean the geometry line */
2051 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2052 /* Now, continue drawing */
2053 XUngrabServer(dpy);
2054 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2055 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2056 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2059 break;
2061 case ButtonPress:
2062 break;
2064 case ButtonRelease:
2065 if (event.xbutton.button != ev->xbutton.button)
2066 break;
2068 if (started) {
2069 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2071 if (!opaqueResize)
2072 drawTransparentFrame(wwin, fx, fy, fw, fh);
2074 XUngrabKeyboard(dpy, CurrentTime);
2075 WMUnmapWidget(scr->gview);
2076 XUngrabServer(dpy);
2078 if (fw != original_fw)
2079 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
2081 if (fh != original_fh)
2082 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2084 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2085 wWindowSynthConfigureNotify(wwin);
2087 return;
2089 default:
2090 WMHandleEvent(&event);
2094 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2095 wArrangeIcons(scr, True);
2098 #undef LEFT
2099 #undef RIGHT
2100 #undef HORIZONTAL
2101 #undef UP
2102 #undef DOWN
2103 #undef VERTICAL
2104 #undef HCONSTRAIN
2105 #undef RESIZEBAR
2107 void wUnselectWindows(WScreen * scr)
2109 WWindow *wwin;
2111 if (!scr->selected_windows)
2112 return;
2114 while (WMGetArrayItemCount(scr->selected_windows)) {
2115 wwin = WMGetFromArray(scr->selected_windows, 0);
2116 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2117 wIconSelect(wwin->icon);
2119 wSelectWindow(wwin, False);
2121 WMFreeArray(scr->selected_windows);
2122 scr->selected_windows = NULL;
2125 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2127 WWindow *tmpw;
2129 /* select the windows and put them in the selected window list */
2130 tmpw = scr->focused_window;
2131 while (tmpw != NULL) {
2132 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2133 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2134 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2135 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2136 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2137 wSelectWindow(tmpw, True);
2140 tmpw = tmpw->prev;
2144 void wSelectWindows(WScreen * scr, XEvent * ev)
2146 XEvent event;
2147 Window root = scr->root_win;
2148 GC gc = scr->frame_gc;
2149 int xp = ev->xbutton.x_root;
2150 int yp = ev->xbutton.y_root;
2151 int w = 0, h = 0;
2152 int x = xp, y = yp;
2154 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2155 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2156 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2157 return;
2159 XGrabServer(dpy);
2161 wUnselectWindows(scr);
2163 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2164 while (1) {
2165 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2167 switch (event.type) {
2168 case MotionNotify:
2169 XDrawRectangle(dpy, root, gc, x, y, w, h);
2170 x = event.xmotion.x_root;
2171 if (x < xp) {
2172 w = xp - x;
2173 } else {
2174 w = x - xp;
2175 x = xp;
2177 y = event.xmotion.y_root;
2178 if (y < yp) {
2179 h = yp - y;
2180 } else {
2181 h = y - yp;
2182 y = yp;
2184 XDrawRectangle(dpy, root, gc, x, y, w, h);
2185 break;
2187 case ButtonPress:
2188 break;
2190 case ButtonRelease:
2191 if (event.xbutton.button != ev->xbutton.button)
2192 break;
2194 XDrawRectangle(dpy, root, gc, x, y, w, h);
2195 XUngrabServer(dpy);
2196 XUngrabPointer(dpy, CurrentTime);
2197 selectWindowsInside(scr, x, y, x + w, y + h);
2198 return;
2200 default:
2201 WMHandleEvent(&event);
2202 break;
2207 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2209 WScreen *scr = wwin->screen_ptr;
2210 Window root = scr->root_win;
2211 int x, y, h = 0;
2212 XEvent event;
2213 KeyCode shiftl, shiftr;
2214 Window junkw;
2215 int junk;
2217 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2218 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2219 *x_ret = 0;
2220 *y_ret = 0;
2221 return;
2223 if (HAS_TITLEBAR(wwin)) {
2224 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2225 TITLEBAR_EXTEND_SPACE) * 2;
2227 if (h > wPreferences.window_title_max_height)
2228 h = wPreferences.window_title_max_height;
2230 if (h < wPreferences.window_title_min_height)
2231 h = wPreferences.window_title_min_height;
2233 height += h;
2235 if (HAS_RESIZEBAR(wwin)) {
2236 height += RESIZEBAR_HEIGHT;
2238 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2239 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2240 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2242 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2244 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2245 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2246 while (1) {
2247 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2249 if (!checkMouseSamplingRate(&event))
2250 continue;
2252 switch (event.type) {
2253 case KeyPress:
2254 if ((event.xkey.keycode == shiftl)
2255 || (event.xkey.keycode == shiftr)) {
2256 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2257 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2258 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2260 break;
2262 case MotionNotify:
2263 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2265 x = event.xmotion.x_root;
2266 y = event.xmotion.y_root;
2268 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2269 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2271 showPosition(wwin, x - width / 2, y - h / 2);
2273 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2275 break;
2277 case ButtonPress:
2278 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2279 XSync(dpy, 0);
2280 *x_ret = x - width / 2;
2281 *y_ret = y - h / 2;
2282 XUngrabPointer(dpy, CurrentTime);
2283 XUngrabKeyboard(dpy, CurrentTime);
2284 /* get rid of the geometry window */
2285 WMUnmapWidget(scr->gview);
2286 return;
2288 default:
2289 WMHandleEvent(&event);
2290 break;