Add OPEN_PLMENU option to parse command generated proplist style menus
[wmaker-crm.git] / src / moveres.c
blob44ddefa2c8e1edd7d85b5d73c28947b962943924
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "client.h"
35 #include "icon.h"
36 #include "dock.h"
37 #include "funcs.h"
38 #include "actions.h"
39 #include "workspace.h"
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];
65 extern WPreferences wPreferences;
68 *----------------------------------------------------------------------
69 * checkMouseSamplingRate-
70 * For lowering the mouse motion sampling rate for machines where
71 * it's too high (SGIs). If it returns False then the event should be
72 * ignored.
73 *----------------------------------------------------------------------
75 static Bool checkMouseSamplingRate(XEvent * ev)
77 static Time previousMotion = 0;
79 if (ev->type == MotionNotify) {
80 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
81 return False;
82 } else {
83 previousMotion = ev->xmotion.time;
86 return True;
90 *----------------------------------------------------------------------
91 * moveGeometryDisplayCentered
93 * routine that moves the geometry/position window on scr so it is
94 * centered over the given coordinates (x,y). Also the window position
95 * is clamped so it stays on the screen at all times.
96 *----------------------------------------------------------------------
98 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
100 unsigned int w = WMWidgetWidth(scr->gview);
101 unsigned int h = WMWidgetHeight(scr->gview);
102 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
104 x -= w / 2;
105 y -= h / 2;
107 /* dead area check */
108 if (scr->xine_info.count) {
109 WMRect rect;
110 int head, flags;
112 rect.pos.x = x;
113 rect.pos.y = y;
114 rect.size.width = w;
115 rect.size.height = h;
117 head = wGetRectPlacementInfo(scr, rect, &flags);
119 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
120 rect = wGetRectForHead(scr, head);
121 x1 = rect.pos.x;
122 y1 = rect.pos.y;
123 x2 = x1 + rect.size.width;
124 y2 = y1 + rect.size.height;
128 if (x < x1 + 1)
129 x = x1 + 1;
130 else if (x > (x2 - w))
131 x = x2 - w;
133 if (y < y1 + 1)
134 y = y1 + 1;
135 else if (y > (y2 - h))
136 y = y2 - h;
138 WMMoveWidget(scr->gview, x, y);
141 static void showPosition(WWindow * wwin, int x, int y)
143 WScreen *scr = wwin->screen_ptr;
145 if (wPreferences.move_display == WDIS_NEW) {
146 #if 0
147 int width = wwin->frame->core->width;
148 int height = wwin->frame->core->height;
150 GC lgc = scr->line_gc;
151 XSetForeground(dpy, lgc, scr->line_pixel);
152 sprintf(num, "%i", x);
154 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
155 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
156 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
157 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
158 #endif
159 } else {
160 WSetGeometryViewShownPosition(scr->gview, x, y);
164 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
166 WScreen *scr = wwin->screen_ptr;
167 WMRect rect;
169 wPreferences.move_display++;
170 wPreferences.move_display %= NUM_DISPLAYS;
172 if (wPreferences.move_display == WDIS_NEW) {
173 wPreferences.move_display++;
174 wPreferences.move_display %= NUM_DISPLAYS;
177 if (wPreferences.move_display == WDIS_NONE) {
178 WMUnmapWidget(scr->gview);
179 } else {
180 if (wPreferences.move_display == WDIS_CENTER) {
181 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
182 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
183 rect.pos.y + rect.size.height / 2);
184 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
185 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
186 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
187 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
188 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
190 WMMapWidget(scr->gview);
194 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
196 WScreen *scr = wwin->screen_ptr;
197 WMRect rect;
199 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
200 return;
201 } else if (wPreferences.move_display == WDIS_CENTER) {
202 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
203 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
204 rect.pos.y + rect.size.height / 2);
205 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
206 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
207 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
208 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
209 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
211 WMMapWidget(scr->gview);
212 WSetGeometryViewShownPosition(scr->gview, x, y);
215 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
217 WScreen *scr = wwin->screen_ptr;
218 Window root = scr->root_win;
219 GC gc = scr->line_gc;
220 int ty, by, my, x, y, mx, s;
221 char num[16];
222 XSegment segment[4];
223 int fw, fh;
225 /* This seems necessary for some odd reason (too lazy to write x1-1 and
226 * x2-1 everywhere below in the code). But why only for x? */
227 x1--;
228 x2--;
230 if (HAS_BORDER_WITH_SELECT(wwin)) {
231 x1 += FRAME_BORDER_WIDTH;
232 x2 += FRAME_BORDER_WIDTH;
233 y1 += FRAME_BORDER_WIDTH;
234 y2 += FRAME_BORDER_WIDTH;
237 ty = y1 + wwin->frame->top_width;
238 by = y2 - wwin->frame->bottom_width;
240 if (wPreferences.size_display == WDIS_NEW) {
241 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
242 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
244 XSetForeground(dpy, gc, scr->line_pixel);
246 /* vertical geometry */
247 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
248 x = x2;
249 s = -15;
250 } else {
251 x = x1;
252 s = 15;
254 my = (ty + by) / 2;
256 /* top arrow & end bar */
257 segment[0].x1 = x - (s + 6);
258 segment[0].y1 = ty;
259 segment[0].x2 = x - (s - 10);
260 segment[0].y2 = ty;
262 /* arrowhead */
263 segment[1].x1 = x - (s - 2);
264 segment[1].y1 = ty + 1;
265 segment[1].x2 = x - (s - 5);
266 segment[1].y2 = ty + 7;
268 segment[2].x1 = x - (s - 2);
269 segment[2].y1 = ty + 1;
270 segment[2].x2 = x - (s + 1);
271 segment[2].y2 = ty + 7;
273 /* line */
274 segment[3].x1 = x - (s - 2);
275 segment[3].y1 = ty + 1;
276 segment[3].x2 = x - (s - 2);
277 segment[3].y2 = my - fh / 2 - 1;
279 XDrawSegments(dpy, root, gc, segment, 4);
281 /* bottom arrow & end bar */
282 segment[0].y1 = by;
283 segment[0].y2 = by;
285 /* arrowhead */
286 segment[1].y1 = by - 1;
287 segment[1].y2 = by - 7;
289 segment[2].y1 = by - 1;
290 segment[2].y2 = by - 7;
292 /* line */
293 segment[3].y1 = my + fh / 2 + 2;
294 segment[3].y2 = by - 1;
296 XDrawSegments(dpy, root, gc, segment, 4);
298 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
299 wwin->normal_hints->height_inc);
300 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
302 /* Display the height. */
303 XSetFont(dpy, gc, scr->tech_draw_font->fid);
304 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
305 strlen(num));
307 /* horizontal geometry */
308 if (y1 < 15) {
309 y = y2;
310 s = -15;
311 } else {
312 y = y1;
313 s = 15;
315 mx = x1 + (x2 - x1) / 2;
316 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
317 wwin->normal_hints->width_inc);
318 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
320 /* left arrow & end bar */
321 segment[0].x1 = x1;
322 segment[0].y1 = y - (s + 6);
323 segment[0].x2 = x1;
324 segment[0].y2 = y - (s - 10);
326 /* arrowhead */
327 segment[1].x1 = x1 + 7;
328 segment[1].y1 = y - (s + 1);
329 segment[1].x2 = x1 + 1;
330 segment[1].y2 = y - (s - 2);
332 segment[2].x1 = x1 + 1;
333 segment[2].y1 = y - (s - 2);
334 segment[2].x2 = x1 + 7;
335 segment[2].y2 = y - (s - 5);
337 /* line */
338 segment[3].x1 = x1 + 1;
339 segment[3].y1 = y - (s - 2);
340 segment[3].x2 = mx - fw / 2 - 2;
341 segment[3].y2 = y - (s - 2);
343 XDrawSegments(dpy, root, gc, segment, 4);
345 /* right arrow & end bar */
346 segment[0].x1 = x2 + 1;
347 segment[0].x2 = x2 + 1;
349 /* arrowhead */
350 segment[1].x1 = x2 - 6;
351 segment[1].x2 = x2;
353 segment[2].x1 = x2;
354 segment[2].x2 = x2 - 6;
356 /* line */
357 segment[3].x1 = mx + fw / 2 + 2;
358 segment[3].x2 = x2;
360 XDrawSegments(dpy, root, gc, segment, 4);
362 /* Display the width. */
363 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
364 strlen(num));
365 } else {
366 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
367 / wwin->normal_hints->width_inc,
368 (by - ty - wwin->normal_hints->base_height)
369 / wwin->normal_hints->height_inc);
373 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
375 WScreen *scr = wwin->screen_ptr;
376 WMRect rect;
378 wPreferences.size_display++;
379 wPreferences.size_display %= NUM_DISPLAYS;
381 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
382 WMUnmapWidget(scr->gview);
383 } else {
384 if (wPreferences.size_display == WDIS_CENTER) {
385 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
386 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
387 rect.pos.y + rect.size.height / 2);
388 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
389 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
390 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
391 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
392 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
394 WMMapWidget(scr->gview);
395 showGeometry(wwin, x, y, x + w, y + h, dir);
399 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
401 WScreen *scr = wwin->screen_ptr;
402 WMRect rect;
404 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
405 return;
407 if (wPreferences.size_display == WDIS_CENTER) {
408 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
409 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
410 rect.pos.y + rect.size.height / 2);
411 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
412 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
413 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
414 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
415 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
417 WMMapWidget(scr->gview);
418 showGeometry(wwin, x, y, x + w, y + h, 0);
421 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
423 WWindow *tmpw;
424 WScreen *scr = wwin->screen_ptr;
425 int x, y;
427 if (!array || !WMGetArrayItemCount(array)) {
428 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
429 } else {
430 WMArrayIterator iter;
432 WM_ITERATE_ARRAY(array, tmpw, iter) {
433 x = tmpw->frame_x + dx;
434 y = tmpw->frame_y + dy;
436 #if 1 /* XXX: with xinerama patch was #if 0, check this */
437 /* don't let windows become unreachable */
439 if (x + (int)tmpw->frame->core->width < 20)
440 x = 20 - (int)tmpw->frame->core->width;
441 else if (x + 20 > scr->scr_width)
442 x = scr->scr_width - 20;
444 if (y + (int)tmpw->frame->core->height < 20)
445 y = 20 - (int)tmpw->frame->core->height;
446 else if (y + 20 > scr->scr_height)
447 y = scr->scr_height - 20;
448 #else
449 wScreenBringInside(scr, &x, &y,
450 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
451 #endif
453 wWindowMove(tmpw, x, y);
458 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
460 Window root = wwin->screen_ptr->root_win;
461 GC gc = wwin->screen_ptr->frame_gc;
462 int h = 0;
463 int bottom = 0;
465 if (HAS_BORDER_WITH_SELECT(wwin)) {
466 x += FRAME_BORDER_WIDTH;
467 y += FRAME_BORDER_WIDTH;
470 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
471 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
472 TITLEBAR_EXTEND_SPACE) * 2;
474 if (h > wPreferences.window_title_max_height)
475 h = wPreferences.window_title_max_height;
477 if (h < wPreferences.window_title_min_height)
478 h = wPreferences.window_title_min_height;
480 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
481 /* Can't use wwin-frame->bottom_width because, in some cases
482 (e.g. interactive placement), frame does not point to anything. */
483 bottom = RESIZEBAR_HEIGHT;
485 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
487 if (h > 0) {
488 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
490 if (bottom > 0) {
491 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
495 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
497 WWindow *tmpw;
498 int scr_width = wwin->screen_ptr->scr_width;
499 int scr_height = wwin->screen_ptr->scr_height;
500 int x, y;
502 if (!array) {
504 x = wwin->frame_x + dx;
505 y = wwin->frame_y + dy;
507 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
509 } else {
510 WMArrayIterator iter;
512 WM_ITERATE_ARRAY(array, tmpw, iter) {
513 x = tmpw->frame_x + dx;
514 y = tmpw->frame_y + dy;
516 /* don't let windows become unreachable */
517 #if 1 /* XXX: was 0 in XINERAMA patch, check */
518 if (x + (int)tmpw->frame->core->width < 20)
519 x = 20 - (int)tmpw->frame->core->width;
520 else if (x + 20 > scr_width)
521 x = scr_width - 20;
523 if (y + (int)tmpw->frame->core->height < 20)
524 y = 20 - (int)tmpw->frame->core->height;
525 else if (y + 20 > scr_height)
526 y = scr_height - 20;
528 #else
529 wScreenBringInside(wwin->screen_ptr, &x, &y,
530 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
531 #endif
533 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
538 static void flushMotion()
540 XEvent ev;
542 XSync(dpy, False);
543 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
546 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
548 /* do not let window be unmapped */
549 if (opaque_move) {
550 wwin->flags.changing_workspace = 1;
551 wWindowChangeWorkspace(wwin, new_workspace);
553 /* go to new workspace */
554 wWorkspaceChange(scr, new_workspace);
556 wwin->flags.changing_workspace = 0;
558 if (rewind)
559 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
560 else
561 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
563 flushMotion();
565 if (!opaque_move) {
566 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
567 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
568 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
572 typedef struct {
573 /* arrays of WWindows sorted by the respective border position */
574 WWindow **topList; /* top border */
575 WWindow **leftList; /* left border */
576 WWindow **rightList; /* right border */
577 WWindow **bottomList; /* bottom border */
578 int count;
580 /* index of window in the above lists indicating the relative position
581 * of the window with the others */
582 int topIndex;
583 int leftIndex;
584 int rightIndex;
585 int bottomIndex;
587 int rubCount; /* for workspace switching */
589 int winWidth, winHeight; /* width/height of the window */
590 int realX, realY; /* actual position of the window */
591 int calcX, calcY; /* calculated position of window */
592 int omouseX, omouseY; /* old mouse position */
593 int mouseX, mouseY; /* last known position of the pointer */
594 } MoveData;
596 #define WTOP(w) (w)->frame_y
597 #define WLEFT(w) (w)->frame_x
598 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
599 (HAS_BORDER_WITH_SELECT(w) ? 2*FRAME_BORDER_WIDTH : 0))
600 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
601 (HAS_BORDER_WITH_SELECT(w) ? 2*FRAME_BORDER_WIDTH : 0))
603 static int compareWTop(const void *a, const void *b)
605 WWindow *wwin1 = *(WWindow **) a;
606 WWindow *wwin2 = *(WWindow **) b;
608 if (WTOP(wwin1) > WTOP(wwin2))
609 return -1;
610 else if (WTOP(wwin1) < WTOP(wwin2))
611 return 1;
612 else
613 return 0;
616 static int compareWLeft(const void *a, const void *b)
618 WWindow *wwin1 = *(WWindow **) a;
619 WWindow *wwin2 = *(WWindow **) b;
621 if (WLEFT(wwin1) > WLEFT(wwin2))
622 return -1;
623 else if (WLEFT(wwin1) < WLEFT(wwin2))
624 return 1;
625 else
626 return 0;
629 static int compareWRight(const void *a, const void *b)
631 WWindow *wwin1 = *(WWindow **) a;
632 WWindow *wwin2 = *(WWindow **) b;
634 if (WRIGHT(wwin1) < WRIGHT(wwin2))
635 return -1;
636 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
637 return 1;
638 else
639 return 0;
642 static int compareWBottom(const void *a, const void *b)
644 WWindow *wwin1 = *(WWindow **) a;
645 WWindow *wwin2 = *(WWindow **) b;
647 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
648 return -1;
649 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
650 return 1;
651 else
652 return 0;
655 static void updateResistance(WWindow * wwin, MoveData * data, int newX, int newY)
657 int i;
658 int newX2 = newX + data->winWidth;
659 int newY2 = newY + data->winHeight;
660 Bool ok = False;
662 if (newX < data->realX) {
663 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
664 ok = True;
665 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
666 ok = True;
668 } else if (newX > data->realX) {
669 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
670 ok = True;
671 } else if (data->rightIndex <= data->count - 1
672 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
673 ok = True;
677 if (!ok) {
678 if (newY < data->realY) {
679 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
680 ok = True;
681 } else if (data->topIndex <= data->count - 1
682 && newY2 <= WTOP(data->topList[data->topIndex])) {
683 ok = True;
685 } else if (newY > data->realY) {
686 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
687 ok = True;
688 } else if (data->bottomIndex <= data->count - 1
689 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
690 ok = True;
695 if (!ok)
696 return;
698 /* TODO: optimize this */
699 if (data->realY < WBOTTOM(data->bottomList[0])) {
700 data->bottomIndex = 0;
702 if (data->realX < WRIGHT(data->rightList[0])) {
703 data->rightIndex = 0;
705 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
706 data->leftIndex = 0;
708 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
709 data->topIndex = 0;
711 for (i = 0; i < data->count; i++) {
712 if (data->realY > WBOTTOM(data->bottomList[i])) {
713 data->bottomIndex = i + 1;
715 if (data->realX > WRIGHT(data->rightList[i])) {
716 data->rightIndex = i + 1;
718 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
719 data->leftIndex = i + 1;
721 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
722 data->topIndex = i + 1;
727 static void freeMoveData(MoveData * data)
729 if (data->topList)
730 wfree(data->topList);
731 if (data->leftList)
732 wfree(data->leftList);
733 if (data->rightList)
734 wfree(data->rightList);
735 if (data->bottomList)
736 wfree(data->bottomList);
739 static void updateMoveData(WWindow * wwin, MoveData * data)
741 WScreen *scr = wwin->screen_ptr;
742 WWindow *tmp;
743 int i;
745 data->count = 0;
746 tmp = scr->focused_window;
747 while (tmp) {
748 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
749 && !tmp->flags.miniaturized
750 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
751 data->topList[data->count] = tmp;
752 data->leftList[data->count] = tmp;
753 data->rightList[data->count] = tmp;
754 data->bottomList[data->count] = tmp;
755 data->count++;
757 tmp = tmp->prev;
760 if (data->count == 0) {
761 data->topIndex = 0;
762 data->leftIndex = 0;
763 data->rightIndex = 0;
764 data->bottomIndex = 0;
765 return;
768 /* order from closest to the border of the screen to farthest */
770 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
771 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
772 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
773 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
775 /* figure the position of the window relative to the others */
777 data->topIndex = -1;
778 data->leftIndex = -1;
779 data->rightIndex = -1;
780 data->bottomIndex = -1;
782 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
783 data->bottomIndex = 0;
785 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
786 data->rightIndex = 0;
788 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
789 data->leftIndex = 0;
791 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
792 data->topIndex = 0;
794 for (i = 0; i < data->count; i++) {
795 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
796 data->bottomIndex = i + 1;
798 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
799 data->rightIndex = i + 1;
801 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
802 data->leftIndex = i + 1;
804 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
805 data->topIndex = i + 1;
810 static void initMoveData(WWindow * wwin, MoveData * data)
812 int i;
813 WWindow *tmp;
815 memset(data, 0, sizeof(MoveData));
817 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
819 if (i > 1) {
820 data->topList = wmalloc(sizeof(WWindow *) * i);
821 data->leftList = wmalloc(sizeof(WWindow *) * i);
822 data->rightList = wmalloc(sizeof(WWindow *) * i);
823 data->bottomList = wmalloc(sizeof(WWindow *) * i);
825 updateMoveData(wwin, data);
828 data->realX = wwin->frame_x;
829 data->realY = wwin->frame_y;
830 data->calcX = wwin->frame_x;
831 data->calcY = wwin->frame_y;
833 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * FRAME_BORDER_WIDTH : 0);
834 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * FRAME_BORDER_WIDTH : 0);
837 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
839 WScreen *scr = wwin->screen_ptr;
840 Bool changed = False;
842 if (data->mouseX <= 1) {
843 if (scr->current_workspace > 0) {
845 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1, True);
846 changed = True;
847 data->rubCount = 0;
849 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
851 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1, True);
852 changed = True;
853 data->rubCount = 0;
855 } else if (data->mouseX >= scr->scr_width - 2) {
857 if (scr->current_workspace == scr->workspace_count - 1) {
859 if (wPreferences.ws_cycle || scr->workspace_count == MAX_WORKSPACES) {
861 crossWorkspace(scr, wwin, opaqueMove, 0, False);
862 changed = True;
863 data->rubCount = 0;
865 /* if user insists on trying to go to next workspace even when
866 * it's already the last, create a new one */
867 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
869 /* detect user "rubbing" the window against the edge */
870 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
872 data->rubCount = -(data->rubCount + 1);
874 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
876 data->rubCount = -data->rubCount + 1;
879 /* create a new workspace */
880 if (abs(data->rubCount) > 2) {
881 /* go to next workspace */
882 wWorkspaceNew(scr);
884 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
885 changed = True;
886 data->rubCount = 0;
888 } else if (scr->current_workspace < scr->workspace_count) {
890 /* go to next workspace */
891 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
892 changed = True;
893 data->rubCount = 0;
895 } else {
896 data->rubCount = 0;
899 return changed;
902 static void
903 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
904 Bool opaqueMove, int newMouseX, int newMouseY)
906 WScreen *scr = wwin->screen_ptr;
907 int dx, dy; /* how much mouse moved */
908 int winL, winR, winT, winB; /* requested new window position */
909 int newX, newY; /* actual new window position */
910 Bool hresist, vresist;
911 Bool attract;
913 hresist = False;
914 vresist = False;
916 /* check the direction of the movement */
917 dx = newMouseX - data->mouseX;
918 dy = newMouseY - data->mouseY;
920 data->omouseX = data->mouseX;
921 data->omouseY = data->mouseY;
922 data->mouseX = newMouseX;
923 data->mouseY = newMouseY;
925 winL = data->calcX + dx;
926 winR = data->calcX + data->winWidth + dx;
927 winT = data->calcY + dy;
928 winB = data->calcY + data->winHeight + dy;
930 newX = data->realX;
931 newY = data->realY;
933 if (doResistance) {
934 int l_edge, r_edge;
935 int edge_l, edge_r;
936 int t_edge, b_edge;
937 int edge_t, edge_b;
938 int resist;
940 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
941 attract = wPreferences.attract;
942 /* horizontal movement: check horizontal edge resistances */
943 if (dx || dy) {
944 WMRect rect;
945 int i, head;
946 /* window is the leftmost window: check against screen edge */
948 /* Add inter head resistance 1/2 (if needed) */
949 head = wGetHeadForPointerLocation(scr);
950 rect = wGetRectForHead(scr, head);
952 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
953 edge_l = l_edge - resist;
954 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
955 r_edge = edge_r + resist;
957 /* 1 */
958 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
959 WWindow *looprw;
961 for (i = data->rightIndex - 1; i >= 0; i--) {
962 looprw = data->rightList[i];
963 if (!(data->realY > WBOTTOM(looprw)
964 || (data->realY + data->winHeight) < WTOP(looprw))) {
965 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
966 l_edge = WRIGHT(looprw) + 1;
967 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
969 break;
973 if (attract) {
974 for (i = data->rightIndex; i < data->count; i++) {
975 looprw = data->rightList[i];
976 if (!(data->realY > WBOTTOM(looprw)
977 || (data->realY + data->winHeight) < WTOP(looprw))) {
978 r_edge = WRIGHT(looprw) + 1;
979 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
980 break;
986 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
987 WWindow *looprw;
989 for (i = data->leftIndex - 1; i >= 0; i--) {
990 looprw = data->leftList[i];
991 if (!(data->realY > WBOTTOM(looprw)
992 || (data->realY + data->winHeight) < WTOP(looprw))) {
993 if (attract
994 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
995 && dx > 0)) {
996 edge_r = WLEFT(looprw);
997 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
999 break;
1003 if (attract)
1004 for (i = data->leftIndex; i < data->count; i++) {
1005 looprw = data->leftList[i];
1006 if (!(data->realY > WBOTTOM(looprw)
1007 || (data->realY + data->winHeight) < WTOP(looprw))) {
1008 edge_l = WLEFT(looprw);
1009 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1010 break;
1016 printf("%d %d\n",winL,winR);
1017 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1020 if ((winL - l_edge) < (r_edge - winL)) {
1021 if (resist > 0) {
1022 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1023 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1024 newX = l_edge;
1025 hresist = True;
1028 } else {
1029 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1030 newX = r_edge;
1031 hresist = True;
1035 if ((winR - edge_l) < (edge_r - winR)) {
1036 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1037 newX = edge_l - data->winWidth;
1038 hresist = True;
1040 } else {
1041 if (resist > 0) {
1042 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1043 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1044 newX = edge_r - data->winWidth;
1045 hresist = True;
1050 /* VeRT */
1051 /* Add inter head resistance 2/2 (if needed) */
1052 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1053 edge_t = t_edge - resist;
1054 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1055 b_edge = edge_b + resist;
1057 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1058 WWindow *looprw;
1060 for (i = data->bottomIndex - 1; i >= 0; i--) {
1061 looprw = data->bottomList[i];
1062 if (!(data->realX > WRIGHT(looprw)
1063 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1064 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1065 t_edge = WBOTTOM(looprw) + 1;
1066 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1068 break;
1072 if (attract) {
1073 for (i = data->bottomIndex; i < data->count; i++) {
1074 looprw = data->bottomList[i];
1075 if (!(data->realX > WRIGHT(looprw)
1076 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1077 b_edge = WBOTTOM(looprw) + 1;
1078 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1079 break;
1085 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1086 WWindow *looprw;
1088 for (i = data->topIndex - 1; i >= 0; i--) {
1089 looprw = data->topList[i];
1090 if (!(data->realX > WRIGHT(looprw)
1091 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1092 if (attract
1093 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1094 && dy > 0)) {
1095 edge_b = WTOP(looprw);
1096 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1098 break;
1102 if (attract)
1103 for (i = data->topIndex; i < data->count; i++) {
1104 looprw = data->topList[i];
1105 if (!(data->realX > WRIGHT(looprw)
1106 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1107 edge_t = WTOP(looprw);
1108 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1109 break;
1114 if ((winT - t_edge) < (b_edge - winT)) {
1115 if (resist > 0) {
1116 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1117 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1118 newY = t_edge;
1119 vresist = True;
1122 } else {
1123 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1124 newY = b_edge;
1125 vresist = True;
1129 if ((winB - edge_t) < (edge_b - winB)) {
1130 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1131 newY = edge_t - data->winHeight;
1132 vresist = True;
1134 } else {
1135 if (resist > 0) {
1136 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1137 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1138 newY = edge_b - data->winHeight;
1139 vresist = True;
1144 /* END VeRT */
1148 /* update window position */
1149 data->calcX += dx;
1150 data->calcY += dy;
1152 if (((dx > 0 && data->calcX - data->realX > 0)
1153 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1154 newX = data->calcX;
1156 if (((dy > 0 && data->calcY - data->realY > 0)
1157 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1158 newY = data->calcY;
1160 if (data->realX != newX || data->realY != newY) {
1162 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1163 showPosition(wwin, data->realX, data->realY);
1165 if (opaqueMove) {
1166 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1167 } else {
1168 /* erase frames */
1169 drawFrames(wwin, scr->selected_windows,
1170 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1173 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1175 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1178 if (!opaqueMove) {
1179 /* draw frames */
1180 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1183 if (!scr->selected_windows) {
1184 showPosition(wwin, newX, newY);
1188 /* recalc relative window position */
1189 if (doResistance && (data->realX != newX || data->realY != newY)) {
1190 updateResistance(wwin, data, newX, newY);
1193 data->realX = newX;
1194 data->realY = newY;
1197 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1199 #define MOVABLE_BIT 0x01
1200 #define RESIZABLE_BIT 0x02
1202 int wKeyboardMoveResizeWindow(WWindow * wwin)
1204 WScreen *scr = wwin->screen_ptr;
1205 Window root = scr->root_win;
1206 XEvent event;
1207 int w = wwin->frame->core->width;
1208 int h = wwin->frame->core->height;
1209 int scr_width = wwin->screen_ptr->scr_width;
1210 int scr_height = wwin->screen_ptr->scr_height;
1211 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1212 int src_x = wwin->frame_x;
1213 int src_y = wwin->frame_y;
1214 int original_w = w;
1215 int original_h = h;
1216 int done, off_x, off_y, ww, wh;
1217 int kspeed = _KS;
1218 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1219 Time lastTime = 0;
1220 KeyCode shiftl, shiftr, ctrlmode;
1221 KeySym keysym = NoSymbol;
1222 int moment = 0;
1223 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1224 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1225 ? wGetHeadForWindow(wwin)
1226 : scr->xine_info.primary_head);
1228 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1229 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1230 ctrlmode = done = off_x = off_y = 0;
1232 if (modes == RESIZABLE_BIT) {
1233 ctrlmode = 1;
1236 XSync(dpy, False);
1237 wusleep(10000);
1238 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1240 if (!wwin->flags.selected) {
1241 wUnselectWindows(scr);
1243 XGrabServer(dpy);
1244 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1245 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1246 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1250 if (!opaqueMoveResize) {
1251 if (wwin->flags.shaded || scr->selected_windows) {
1252 if (scr->selected_windows)
1253 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1254 else
1255 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1256 } else {
1257 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1260 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1261 mapPositionDisplay(wwin, src_x, src_y, w, h);
1264 ww = w;
1265 wh = h;
1266 while (1) {
1268 looper.ox=off_x;
1269 looper.oy=off_y;
1271 do {
1272 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1273 | ButtonPressMask | ExposureMask, &event);
1274 if (event.type == Expose) {
1275 WMHandleEvent(&event);
1277 } while (event.type == Expose);
1279 if (!opaqueMoveResize) {
1280 if (wwin->flags.shaded || scr->selected_windows) {
1281 if (scr->selected_windows)
1282 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1283 else
1284 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1285 /*** I HATE EDGE RESISTANCE - ]d ***/
1286 } else {
1287 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1291 if (ctrlmode)
1292 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1295 XUngrabServer(dpy);
1296 XSync(dpy, False);
1298 switch (event.type) {
1299 case KeyPress:
1300 /* accelerate */
1301 if (event.xkey.time - lastTime > 50) {
1302 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1303 } else {
1304 if (kspeed < 20) {
1305 kspeed++;
1308 if (kspeed < _KS)
1309 kspeed = _KS;
1310 lastTime = event.xkey.time;
1311 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1312 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1313 ctrlmode = 1;
1314 wUnselectWindows(scr);
1315 } else {
1316 ctrlmode = 0;
1319 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1320 if (ctrlmode)
1321 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1322 else
1323 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1324 } else {
1326 keysym = XLookupKeysym(&event.xkey, 0);
1327 switch (keysym) {
1328 case XK_Return:
1329 done = 2;
1330 break;
1331 case XK_Escape:
1332 done = 1;
1333 break;
1334 case XK_Up:
1335 #ifdef XK_KP_Up
1336 case XK_KP_Up:
1337 #endif
1338 case XK_k:
1339 if (ctrlmode) {
1340 if (moment != UP)
1341 h = wh;
1342 h -= kspeed;
1343 moment = UP;
1344 if (h < 1)
1345 h = 1;
1346 } else
1347 off_y -= kspeed;
1348 break;
1349 case XK_Down:
1350 #ifdef XK_KP_Down
1351 case XK_KP_Down:
1352 #endif
1353 case XK_j:
1354 if (ctrlmode) {
1355 if (moment != DOWN)
1356 h = wh;
1357 h += kspeed;
1358 moment = DOWN;
1359 } else
1360 off_y += kspeed;
1361 break;
1362 case XK_Left:
1363 #ifdef XK_KP_Left
1364 case XK_KP_Left:
1365 #endif
1366 case XK_h:
1367 if (ctrlmode) {
1368 if (moment != LEFT)
1369 w = ww;
1370 w -= kspeed;
1371 if (w < 1)
1372 w = 1;
1373 moment = LEFT;
1374 } else
1375 off_x -= kspeed;
1376 break;
1377 case XK_Right:
1378 #ifdef XK_KP_Right
1379 case XK_KP_Right:
1380 #endif
1381 case XK_l:
1382 if (ctrlmode) {
1383 if (moment != RIGHT)
1384 w = ww;
1385 w += kspeed;
1386 moment = RIGHT;
1387 } else
1388 off_x += kspeed;
1389 break;
1392 ww = w;
1393 wh = h;
1394 wh -= vert_border;
1395 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1396 wh += vert_border;
1398 if (wPreferences.ws_cycle) {
1399 if (src_x + off_x + ww < 20) {
1400 if (!scr->current_workspace) {
1401 wWorkspaceChange(scr, scr->workspace_count - 1);
1402 } else
1403 wWorkspaceChange(scr, scr->current_workspace - 1);
1404 off_x += scr_width;
1405 } else if (src_x + off_x + 20 > scr_width) {
1406 if (scr->current_workspace == scr->workspace_count - 1) {
1407 wWorkspaceChange(scr, 0);
1408 } else
1409 wWorkspaceChange(scr, scr->current_workspace + 1);
1410 off_x -= scr_width;
1412 } else {
1413 if (src_x + off_x + ww < 20)
1414 off_x = 20 - ww - src_x;
1415 else if (src_x + off_x + 20 > scr_width)
1416 off_x = scr_width - 20 - src_x;
1419 if (src_y + off_y + wh < 20) {
1420 off_y = 20 - wh - src_y;
1421 } else if (src_y + off_y + 20 > scr_height) {
1422 off_y = scr_height - 20 - src_y;
1425 break;
1426 case ButtonPress:
1427 case ButtonRelease:
1428 done = 1;
1429 break;
1430 case Expose:
1431 WMHandleEvent(&event);
1432 while (XCheckTypedEvent(dpy, Expose, &event)) {
1433 WMHandleEvent(&event);
1435 break;
1437 default:
1438 WMHandleEvent(&event);
1439 break;
1442 XGrabServer(dpy);
1443 /*xxx */
1445 if (wwin->flags.shaded && !scr->selected_windows) {
1446 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1447 } else {
1448 if (ctrlmode) {
1449 WMUnmapWidget(scr->gview);
1450 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1451 } else if (!scr->selected_windows) {
1452 WMUnmapWidget(scr->gview);
1453 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1457 if (!opaqueMoveResize) {
1458 if (wwin->flags.shaded || scr->selected_windows) {
1459 if (scr->selected_windows)
1460 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1461 else
1462 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1463 } else {
1464 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1468 if (ctrlmode) {
1469 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1471 } else if (!scr->selected_windows)
1472 showPosition(wwin, src_x + off_x, src_y + off_y);
1474 if (opaqueMoveResize) {
1475 XUngrabServer(dpy);
1476 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1479 if (done) {
1480 scr->keymove_tick = 0;
1482 WMDeleteTimerWithClientData(&looper);
1484 if (!opaqueMoveResize) {/*ctrlmode=> resize */
1485 if (wwin->flags.shaded || scr->selected_windows) {
1486 if (scr->selected_windows)
1487 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1488 else
1489 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1490 } else {
1491 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1495 if (ctrlmode) {
1496 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1497 src_y + off_y + wh, 0);
1498 WMUnmapWidget(scr->gview);
1499 } else
1500 WMUnmapWidget(scr->gview);
1502 XUngrabKeyboard(dpy, CurrentTime);
1503 XUngrabPointer(dpy, CurrentTime);
1504 XUngrabServer(dpy);
1506 if (done == 2) {
1507 if (wwin->flags.shaded || scr->selected_windows) {
1508 if (!scr->selected_windows) {
1509 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1510 wWindowSynthConfigureNotify(wwin);
1511 } else {
1512 WMArrayIterator iter;
1513 WWindow *foo;
1515 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1517 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1518 wWindowSynthConfigureNotify(foo);
1521 } else {
1522 if (ww != original_w)
1523 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
1525 if (wh != original_h)
1526 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1528 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1529 wWindowSynthConfigureNotify(wwin);
1531 wWindowChangeWorkspace(wwin, scr->current_workspace);
1532 wSetFocusTo(scr, wwin);
1535 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1536 head != wGetHeadForWindow(wwin)) {
1537 wArrangeIcons(scr, True);
1540 update_saved_geometry(wwin);
1542 return 1;
1548 *----------------------------------------------------------------------
1549 * wMouseMoveWindow--
1550 * Move the named window and the other selected ones (if any),
1551 * interactively. Also shows the position of the window, if only one
1552 * window is being moved.
1553 * If the window is not on the selected window list, the selected
1554 * windows are deselected.
1555 * If shift is pressed during the operation, the position display
1556 * is changed to another type.
1558 * Returns:
1559 * True if the window was moved, False otherwise.
1561 * Side effects:
1562 * The window(s) position is changed, and the client(s) are
1563 * notified about that.
1564 * The position display configuration may be changed.
1565 *----------------------------------------------------------------------
1567 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1569 WScreen *scr = wwin->screen_ptr;
1570 XEvent event;
1571 Window root = scr->root_win;
1572 KeyCode shiftl, shiftr;
1573 Bool done = False;
1574 int started = 0;
1575 int warped = 0;
1576 /* This needs not to change while moving, else bad things can happen */
1577 int opaqueMove = wPreferences.opaque_move;
1578 MoveData moveData;
1579 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1580 ? wGetHeadForWindow(wwin)
1581 : scr->xine_info.primary_head);
1583 if (!IS_MOVABLE(wwin))
1584 return False;
1586 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1587 XSetWindowAttributes attr;
1589 attr.save_under = True;
1590 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1593 initMoveData(wwin, &moveData);
1595 moveData.mouseX = ev->xmotion.x_root;
1596 moveData.mouseY = ev->xmotion.y_root;
1598 if (!wwin->flags.selected) {
1599 /* this window is not selected, unselect others and move only wwin */
1600 wUnselectWindows(scr);
1602 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1603 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1604 while (!done) {
1605 if (warped) {
1606 int junk;
1607 Window junkw;
1609 /* XWarpPointer() doesn't seem to generate Motion events, so
1610 * we've got to simulate them */
1611 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1612 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1613 } else {
1614 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1615 | PointerMotionHintMask
1616 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1618 if (event.type == MotionNotify) {
1619 /* compress MotionNotify events */
1620 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1621 if (!checkMouseSamplingRate(&event))
1622 continue;
1625 switch (event.type) {
1626 case KeyPress:
1627 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1628 && started && !scr->selected_windows) {
1630 if (!opaqueMove) {
1631 drawFrames(wwin, scr->selected_windows,
1632 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1635 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1636 showPosition(wwin, moveData.realX, moveData.realY);
1637 XUngrabServer(dpy);
1639 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1640 moveData.winWidth, moveData.winHeight);
1642 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1643 XGrabServer(dpy);
1644 showPosition(wwin, moveData.realX, moveData.realY);
1647 if (!opaqueMove) {
1648 drawFrames(wwin, scr->selected_windows,
1649 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1651 /*} else {
1652 WMHandleEvent(&event); this causes problems needs fixing */
1654 break;
1656 case MotionNotify:
1657 if (started) {
1658 updateWindowPosition(wwin, &moveData,
1659 scr->selected_windows == NULL
1660 && wPreferences.edge_resistance > 0,
1661 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1663 if (!warped && !wPreferences.no_autowrap) {
1664 int oldWorkspace = scr->current_workspace;
1666 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1667 showPosition(wwin, moveData.realX, moveData.realY);
1668 XUngrabServer(dpy);
1670 if (!opaqueMove) {
1671 drawFrames(wwin, scr->selected_windows,
1672 moveData.realX - wwin->frame_x,
1673 moveData.realY - wwin->frame_y);
1675 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1676 if (scr->current_workspace != oldWorkspace
1677 && wPreferences.edge_resistance > 0
1678 && scr->selected_windows == NULL)
1679 updateMoveData(wwin, &moveData);
1680 warped = 1;
1682 if (!opaqueMove) {
1683 drawFrames(wwin, scr->selected_windows,
1684 moveData.realX - wwin->frame_x,
1685 moveData.realY - wwin->frame_y);
1687 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1688 XSync(dpy, False);
1689 showPosition(wwin, moveData.realX, moveData.realY);
1690 XGrabServer(dpy);
1692 } else {
1693 warped = 0;
1695 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1696 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1698 XChangeActivePointerGrab(dpy, ButtonMotionMask
1699 | ButtonReleaseMask | ButtonPressMask,
1700 wCursor[WCUR_MOVE], CurrentTime);
1701 started = 1;
1702 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1704 if (!scr->selected_windows)
1705 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1706 moveData.winWidth, moveData.winHeight);
1708 if (started && !opaqueMove)
1709 drawFrames(wwin, scr->selected_windows, 0, 0);
1711 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1712 && !scr->selected_windows)) {
1713 XGrabServer(dpy);
1714 if (wPreferences.move_display == WDIS_NEW)
1715 showPosition(wwin, moveData.realX, moveData.realY);
1718 break;
1720 case ButtonPress:
1721 break;
1723 case ButtonRelease:
1724 if (event.xbutton.button != ev->xbutton.button)
1725 break;
1727 if (started) {
1728 XEvent e;
1729 if (!opaqueMove) {
1730 drawFrames(wwin, scr->selected_windows,
1731 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1732 XSync(dpy, 0);
1733 doWindowMove(wwin, scr->selected_windows,
1734 moveData.realX - wwin->frame_x,
1735 moveData.realY - wwin->frame_y);
1737 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1738 wWindowSynthConfigureNotify(wwin);
1739 #endif
1740 XUngrabKeyboard(dpy, CurrentTime);
1741 XUngrabServer(dpy);
1742 if (!opaqueMove) {
1743 wWindowChangeWorkspace(wwin, scr->current_workspace);
1744 wSetFocusTo(scr, wwin);
1746 if (wPreferences.move_display == WDIS_NEW)
1747 showPosition(wwin, moveData.realX, moveData.realY);
1749 /* discard all enter/leave events that happened until
1750 * the time the button was released */
1751 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1752 if (e.xcrossing.time > event.xbutton.time) {
1753 XPutBackEvent(dpy, &e);
1754 break;
1757 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1758 if (e.xcrossing.time > event.xbutton.time) {
1759 XPutBackEvent(dpy, &e);
1760 break;
1764 if (!scr->selected_windows) {
1765 /* get rid of the geometry window */
1766 WMUnmapWidget(scr->gview);
1769 done = True;
1770 break;
1772 default:
1773 if (started && !opaqueMove) {
1774 drawFrames(wwin, scr->selected_windows,
1775 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1776 XUngrabServer(dpy);
1777 WMHandleEvent(&event);
1778 XSync(dpy, False);
1779 XGrabServer(dpy);
1780 drawFrames(wwin, scr->selected_windows,
1781 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1782 } else {
1783 WMHandleEvent(&event);
1785 break;
1789 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1790 XSetWindowAttributes attr;
1792 attr.save_under = False;
1793 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1797 freeMoveData(&moveData);
1799 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1800 head != wGetHeadForWindow(wwin)) {
1801 wArrangeIcons(scr, True);
1804 if (started)
1805 update_saved_geometry(wwin);
1807 return started;
1810 #define RESIZEBAR 1
1811 #define HCONSTRAIN 2
1813 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1815 int w = wwin->frame->core->width - 1;
1816 int cw = wwin->frame->resizebar_corner_width;
1817 int dir;
1819 /* if not resizing through the resizebar */
1820 if (!(flags & RESIZEBAR)) {
1821 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1822 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1823 if (abs(dx) < 2 || abs(dy) < 2) {
1824 if (abs(dy) > abs(dx))
1825 xdir = 0;
1826 else
1827 ydir = 0;
1829 return (xdir | ydir);
1832 /* window is too narrow. allow diagonal resize */
1833 if (cw * 2 >= w) {
1834 int ydir;
1836 if (flags & HCONSTRAIN)
1837 ydir = 0;
1838 else
1839 ydir = DOWN;
1840 if (x < cw)
1841 return (LEFT | ydir);
1842 else
1843 return (RIGHT | ydir);
1845 /* vertical resize */
1846 if ((x > cw) && (x < w - cw))
1847 return DOWN;
1849 if (x < cw)
1850 dir = LEFT;
1851 else
1852 dir = RIGHT;
1854 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1855 dir |= DOWN;
1857 return dir;
1860 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1862 XEvent event;
1863 WScreen *scr = wwin->screen_ptr;
1864 Window root = scr->root_win;
1865 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1866 int fw = wwin->frame->core->width;
1867 int fh = wwin->frame->core->height;
1868 int fx = wwin->frame_x;
1869 int fy = wwin->frame_y;
1870 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1871 int orig_x, orig_y;
1872 int started;
1873 int dw, dh;
1874 int rw = fw, rh = fh;
1875 int rx1, ry1, rx2, ry2;
1876 int res = 0;
1877 KeyCode shiftl, shiftr;
1878 int orig_fx = fx;
1879 int orig_fy = fy;
1880 int orig_fw = fw;
1881 int orig_fh = fh;
1882 int original_fw = fw;
1883 int original_fh = fh;
1884 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1885 ? wGetHeadForWindow(wwin)
1886 : scr->xine_info.primary_head);
1887 int opaqueResize = wPreferences.opaque_resize;
1889 if (!IS_RESIZABLE(wwin))
1890 return;
1892 if (wwin->flags.shaded) {
1893 wwarning("internal error: tryein");
1894 return;
1896 orig_x = ev->xbutton.x_root;
1897 orig_y = ev->xbutton.y_root;
1899 started = 0;
1900 wUnselectWindows(scr);
1901 rx1 = fx;
1902 rx2 = fx + fw - 1;
1903 ry1 = fy;
1904 ry2 = fy + fh - 1;
1905 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1906 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1908 while (1) {
1909 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1910 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1911 if (!checkMouseSamplingRate(&event))
1912 continue;
1914 switch (event.type) {
1915 case KeyPress:
1916 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1917 if (!opaqueResize) {
1918 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1919 && started) {
1920 drawTransparentFrame(wwin, fx, fy, fw, fh);
1921 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1922 drawTransparentFrame(wwin, fx, fy, fw, fh);
1925 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1926 break;
1928 case MotionNotify:
1929 if (started) {
1930 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1932 dw = 0;
1933 dh = 0;
1935 orig_fx = fx;
1936 orig_fy = fy;
1937 orig_fw = fw;
1938 orig_fh = fh;
1940 if (res & LEFT)
1941 dw = orig_x - event.xmotion.x_root;
1942 else if (res & RIGHT)
1943 dw = event.xmotion.x_root - orig_x;
1944 if (res & UP)
1945 dh = orig_y - event.xmotion.y_root;
1946 else if (res & DOWN)
1947 dh = event.xmotion.y_root - orig_y;
1949 orig_x = event.xmotion.x_root;
1950 orig_y = event.xmotion.y_root;
1952 rw += dw;
1953 rh += dh;
1954 fw = rw;
1955 fh = rh - vert_border;
1956 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1957 fh += vert_border;
1958 if (res & LEFT)
1959 fx = rx2 - fw + 1;
1960 else if (res & RIGHT)
1961 fx = rx1;
1962 if (res & UP)
1963 fy = ry2 - fh + 1;
1964 else if (res & DOWN)
1965 fy = ry1;
1966 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1967 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1968 int tx, ty;
1969 Window junkw;
1970 int flags;
1972 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1973 orig_x, orig_y, &tx, &ty, &junkw);
1975 /* check if resizing through resizebar */
1976 if (is_resizebar)
1977 flags = RESIZEBAR;
1978 else
1979 flags = 0;
1981 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1982 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1983 flags |= HCONSTRAIN;
1985 res = getResizeDirection(wwin, tx, ty,
1986 orig_x - event.xmotion.x_root,
1987 orig_y - event.xmotion.y_root, flags);
1989 if (res == (UP | LEFT))
1990 XChangeActivePointerGrab(dpy, ButtonMotionMask
1991 | ButtonReleaseMask | ButtonPressMask,
1992 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1993 else if (res == (UP | RIGHT))
1994 XChangeActivePointerGrab(dpy, ButtonMotionMask
1995 | ButtonReleaseMask | ButtonPressMask,
1996 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1997 else if (res == (DOWN | LEFT))
1998 XChangeActivePointerGrab(dpy, ButtonMotionMask
1999 | ButtonReleaseMask | ButtonPressMask,
2000 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2001 else if (res == (DOWN | RIGHT))
2002 XChangeActivePointerGrab(dpy, ButtonMotionMask
2003 | ButtonReleaseMask | ButtonPressMask,
2004 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2005 else if (res == DOWN || res == UP)
2006 XChangeActivePointerGrab(dpy, ButtonMotionMask
2007 | ButtonReleaseMask | ButtonPressMask,
2008 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2009 else if (res & (DOWN | UP))
2010 XChangeActivePointerGrab(dpy, ButtonMotionMask
2011 | ButtonReleaseMask | ButtonPressMask,
2012 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2013 else if (res & (LEFT | RIGHT))
2014 XChangeActivePointerGrab(dpy, ButtonMotionMask
2015 | ButtonReleaseMask | ButtonPressMask,
2016 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2018 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2020 XGrabServer(dpy);
2022 /* Draw the resize frame for the first time. */
2023 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2025 if (!opaqueResize)
2026 drawTransparentFrame(wwin, fx, fy, fw, fh);
2028 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2030 started = 1;
2032 if (started) {
2033 if (!opaqueResize)
2034 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2036 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2037 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2039 if (!opaqueResize)
2040 drawTransparentFrame(wwin, fx, fy, fw, fh);
2042 if (fh != orig_fh || fw != orig_fw) {
2043 if (wPreferences.size_display == WDIS_NEW)
2044 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2045 orig_fy + orig_fh, res);
2047 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2050 if (opaqueResize) {
2051 /* Fist clean the geometry line */
2052 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2053 /* Now, continue drawing */
2054 XUngrabServer(dpy);
2055 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2056 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2057 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2060 break;
2062 case ButtonPress:
2063 break;
2065 case ButtonRelease:
2066 if (event.xbutton.button != ev->xbutton.button)
2067 break;
2069 if (started) {
2070 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2072 if (!opaqueResize)
2073 drawTransparentFrame(wwin, fx, fy, fw, fh);
2075 XUngrabKeyboard(dpy, CurrentTime);
2076 WMUnmapWidget(scr->gview);
2077 XUngrabServer(dpy);
2079 if (fw != original_fw)
2080 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
2082 if (fh != original_fh)
2083 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2085 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2086 wWindowSynthConfigureNotify(wwin);
2088 return;
2090 default:
2091 WMHandleEvent(&event);
2095 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2096 wArrangeIcons(scr, True);
2099 #undef LEFT
2100 #undef RIGHT
2101 #undef HORIZONTAL
2102 #undef UP
2103 #undef DOWN
2104 #undef VERTICAL
2105 #undef HCONSTRAIN
2106 #undef RESIZEBAR
2108 void wUnselectWindows(WScreen * scr)
2110 WWindow *wwin;
2112 if (!scr->selected_windows)
2113 return;
2115 while (WMGetArrayItemCount(scr->selected_windows)) {
2116 wwin = WMGetFromArray(scr->selected_windows, 0);
2117 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2118 wIconSelect(wwin->icon);
2120 wSelectWindow(wwin, False);
2122 WMFreeArray(scr->selected_windows);
2123 scr->selected_windows = NULL;
2126 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2128 WWindow *tmpw;
2130 /* select the windows and put them in the selected window list */
2131 tmpw = scr->focused_window;
2132 while (tmpw != NULL) {
2133 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2134 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2135 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2136 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2137 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2138 wSelectWindow(tmpw, True);
2141 tmpw = tmpw->prev;
2145 void wSelectWindows(WScreen * scr, XEvent * ev)
2147 XEvent event;
2148 Window root = scr->root_win;
2149 GC gc = scr->frame_gc;
2150 int xp = ev->xbutton.x_root;
2151 int yp = ev->xbutton.y_root;
2152 int w = 0, h = 0;
2153 int x = xp, y = yp;
2155 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2156 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2157 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2158 return;
2160 XGrabServer(dpy);
2162 wUnselectWindows(scr);
2164 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2165 while (1) {
2166 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2168 switch (event.type) {
2169 case MotionNotify:
2170 XDrawRectangle(dpy, root, gc, x, y, w, h);
2171 x = event.xmotion.x_root;
2172 if (x < xp) {
2173 w = xp - x;
2174 } else {
2175 w = x - xp;
2176 x = xp;
2178 y = event.xmotion.y_root;
2179 if (y < yp) {
2180 h = yp - y;
2181 } else {
2182 h = y - yp;
2183 y = yp;
2185 XDrawRectangle(dpy, root, gc, x, y, w, h);
2186 break;
2188 case ButtonPress:
2189 break;
2191 case ButtonRelease:
2192 if (event.xbutton.button != ev->xbutton.button)
2193 break;
2195 XDrawRectangle(dpy, root, gc, x, y, w, h);
2196 XUngrabServer(dpy);
2197 XUngrabPointer(dpy, CurrentTime);
2198 selectWindowsInside(scr, x, y, x + w, y + h);
2199 return;
2201 default:
2202 WMHandleEvent(&event);
2203 break;
2208 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2210 WScreen *scr = wwin->screen_ptr;
2211 Window root = scr->root_win;
2212 int x, y, h = 0;
2213 XEvent event;
2214 KeyCode shiftl, shiftr;
2215 Window junkw;
2216 int junk;
2218 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2219 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2220 *x_ret = 0;
2221 *y_ret = 0;
2222 return;
2224 if (HAS_TITLEBAR(wwin)) {
2225 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2226 TITLEBAR_EXTEND_SPACE) * 2;
2228 if (h > wPreferences.window_title_max_height)
2229 h = wPreferences.window_title_max_height;
2231 if (h < wPreferences.window_title_min_height)
2232 h = wPreferences.window_title_min_height;
2234 height += h;
2236 if (HAS_RESIZEBAR(wwin)) {
2237 height += RESIZEBAR_HEIGHT;
2239 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2240 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2241 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2243 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2245 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2246 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2247 while (1) {
2248 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2250 if (!checkMouseSamplingRate(&event))
2251 continue;
2253 switch (event.type) {
2254 case KeyPress:
2255 if ((event.xkey.keycode == shiftl)
2256 || (event.xkey.keycode == shiftr)) {
2257 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2258 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2259 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2261 break;
2263 case MotionNotify:
2264 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2266 x = event.xmotion.x_root;
2267 y = event.xmotion.y_root;
2269 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2270 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2272 showPosition(wwin, x - width / 2, y - h / 2);
2274 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2276 break;
2278 case ButtonPress:
2279 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2280 XSync(dpy, 0);
2281 *x_ret = x - width / 2;
2282 *y_ret = y - h / 2;
2283 XUngrabPointer(dpy, CurrentTime);
2284 XUngrabKeyboard(dpy, CurrentTime);
2285 /* get rid of the geometry window */
2286 WMUnmapWidget(scr->gview);
2287 return;
2289 default:
2290 WMHandleEvent(&event);
2291 break;