Cleanup includes of wcore.h, defaults.h and pixmap.h
[wmaker-crm.git] / src / moveres.c
blobd886f1fad5bd4221bc566f482bb638623764f80b
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "framewin.h"
34 #include "window.h"
35 #include "client.h"
36 #include "icon.h"
37 #include "dock.h"
38 #include "funcs.h"
39 #include "actions.h"
40 #include "workspace.h"
42 #include "geomview.h"
43 #include "screen.h"
44 #include "xinerama.h"
46 #include <WINGs/WINGsP.h>
48 /* How many different types of geometry/position
49 display thingies are there? */
50 #define NUM_DISPLAYS 5
52 #define LEFT 1
53 #define RIGHT 2
54 #define HORIZONTAL (LEFT|RIGHT)
55 #define UP 4
56 #define DOWN 8
57 #define VERTICAL (UP|DOWN)
59 /* True if window currently has a border. This also includes borderless
60 * windows which are currently selected
62 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
64 /****** Global Variables ******/
65 extern Time LastTimestamp;
67 extern Cursor wCursor[WCUR_LAST];
69 extern WPreferences wPreferences;
71 extern Atom _XA_WM_PROTOCOLS;
74 *----------------------------------------------------------------------
75 * checkMouseSamplingRate-
76 * For lowering the mouse motion sampling rate for machines where
77 * it's too high (SGIs). If it returns False then the event should be
78 * ignored.
79 *----------------------------------------------------------------------
81 static Bool checkMouseSamplingRate(XEvent * ev)
83 static Time previousMotion = 0;
85 if (ev->type == MotionNotify) {
86 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
87 return False;
88 } else {
89 previousMotion = ev->xmotion.time;
92 return True;
96 *----------------------------------------------------------------------
97 * moveGeometryDisplayCentered
99 * routine that moves the geometry/position window on scr so it is
100 * centered over the given coordinates (x,y). Also the window position
101 * is clamped so it stays on the screen at all times.
102 *----------------------------------------------------------------------
104 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
106 unsigned int w = WMWidgetWidth(scr->gview);
107 unsigned int h = WMWidgetHeight(scr->gview);
108 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
110 x -= w / 2;
111 y -= h / 2;
113 /* dead area check */
114 if (scr->xine_info.count) {
115 WMRect rect;
116 int head, flags;
118 rect.pos.x = x;
119 rect.pos.y = y;
120 rect.size.width = w;
121 rect.size.height = h;
123 head = wGetRectPlacementInfo(scr, rect, &flags);
125 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
126 rect = wGetRectForHead(scr, head);
127 x1 = rect.pos.x;
128 y1 = rect.pos.y;
129 x2 = x1 + rect.size.width;
130 y2 = y1 + rect.size.height;
134 if (x < x1 + 1)
135 x = x1 + 1;
136 else if (x > (x2 - w))
137 x = x2 - w;
139 if (y < y1 + 1)
140 y = y1 + 1;
141 else if (y > (y2 - h))
142 y = y2 - h;
144 WMMoveWidget(scr->gview, x, y);
147 static void showPosition(WWindow * wwin, int x, int y)
149 WScreen *scr = wwin->screen_ptr;
151 if (wPreferences.move_display == WDIS_NEW) {
152 #if 0
153 int width = wwin->frame->core->width;
154 int height = wwin->frame->core->height;
156 GC lgc = scr->line_gc;
157 XSetForeground(dpy, lgc, scr->line_pixel);
158 sprintf(num, "%i", x);
160 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
161 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
162 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
163 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
164 #endif
165 } else {
166 WSetGeometryViewShownPosition(scr->gview, x, y);
170 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
172 WScreen *scr = wwin->screen_ptr;
173 WMRect rect;
175 wPreferences.move_display++;
176 wPreferences.move_display %= NUM_DISPLAYS;
178 if (wPreferences.move_display == WDIS_NEW) {
179 wPreferences.move_display++;
180 wPreferences.move_display %= NUM_DISPLAYS;
183 if (wPreferences.move_display == WDIS_NONE) {
184 WMUnmapWidget(scr->gview);
185 } else {
186 if (wPreferences.move_display == WDIS_CENTER) {
187 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
188 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
189 rect.pos.y + rect.size.height / 2);
190 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
191 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
192 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
193 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
194 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
196 WMMapWidget(scr->gview);
200 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
202 WScreen *scr = wwin->screen_ptr;
203 WMRect rect;
205 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
206 return;
207 } else if (wPreferences.move_display == WDIS_CENTER) {
208 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
209 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
210 rect.pos.y + rect.size.height / 2);
211 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
212 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
213 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
214 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
215 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
217 WMMapWidget(scr->gview);
218 WSetGeometryViewShownPosition(scr->gview, x, y);
221 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
223 WScreen *scr = wwin->screen_ptr;
224 Window root = scr->root_win;
225 GC gc = scr->line_gc;
226 int ty, by, my, x, y, mx, s;
227 char num[16];
228 XSegment segment[4];
229 int fw, fh;
231 /* This seems necessary for some odd reason (too lazy to write x1-1 and
232 * x2-1 everywhere below in the code). But why only for x? */
233 x1--;
234 x2--;
236 if (HAS_BORDER_WITH_SELECT(wwin)) {
237 x1 += FRAME_BORDER_WIDTH;
238 x2 += FRAME_BORDER_WIDTH;
239 y1 += FRAME_BORDER_WIDTH;
240 y2 += FRAME_BORDER_WIDTH;
243 ty = y1 + wwin->frame->top_width;
244 by = y2 - wwin->frame->bottom_width;
246 if (wPreferences.size_display == WDIS_NEW) {
247 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
248 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
250 XSetForeground(dpy, gc, scr->line_pixel);
252 /* vertical geometry */
253 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
254 x = x2;
255 s = -15;
256 } else {
257 x = x1;
258 s = 15;
260 my = (ty + by) / 2;
262 /* top arrow & end bar */
263 segment[0].x1 = x - (s + 6);
264 segment[0].y1 = ty;
265 segment[0].x2 = x - (s - 10);
266 segment[0].y2 = ty;
268 /* arrowhead */
269 segment[1].x1 = x - (s - 2);
270 segment[1].y1 = ty + 1;
271 segment[1].x2 = x - (s - 5);
272 segment[1].y2 = ty + 7;
274 segment[2].x1 = x - (s - 2);
275 segment[2].y1 = ty + 1;
276 segment[2].x2 = x - (s + 1);
277 segment[2].y2 = ty + 7;
279 /* line */
280 segment[3].x1 = x - (s - 2);
281 segment[3].y1 = ty + 1;
282 segment[3].x2 = x - (s - 2);
283 segment[3].y2 = my - fh / 2 - 1;
285 XDrawSegments(dpy, root, gc, segment, 4);
287 /* bottom arrow & end bar */
288 segment[0].y1 = by;
289 segment[0].y2 = by;
291 /* arrowhead */
292 segment[1].y1 = by - 1;
293 segment[1].y2 = by - 7;
295 segment[2].y1 = by - 1;
296 segment[2].y2 = by - 7;
298 /* line */
299 segment[3].y1 = my + fh / 2 + 2;
300 segment[3].y2 = by - 1;
302 XDrawSegments(dpy, root, gc, segment, 4);
304 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
305 wwin->normal_hints->height_inc);
306 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
308 /* Display the height. */
309 XSetFont(dpy, gc, scr->tech_draw_font->fid);
310 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
311 strlen(num));
313 /* horizontal geometry */
314 if (y1 < 15) {
315 y = y2;
316 s = -15;
317 } else {
318 y = y1;
319 s = 15;
321 mx = x1 + (x2 - x1) / 2;
322 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
323 wwin->normal_hints->width_inc);
324 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
326 /* left arrow & end bar */
327 segment[0].x1 = x1;
328 segment[0].y1 = y - (s + 6);
329 segment[0].x2 = x1;
330 segment[0].y2 = y - (s - 10);
332 /* arrowhead */
333 segment[1].x1 = x1 + 7;
334 segment[1].y1 = y - (s + 1);
335 segment[1].x2 = x1 + 1;
336 segment[1].y2 = y - (s - 2);
338 segment[2].x1 = x1 + 1;
339 segment[2].y1 = y - (s - 2);
340 segment[2].x2 = x1 + 7;
341 segment[2].y2 = y - (s - 5);
343 /* line */
344 segment[3].x1 = x1 + 1;
345 segment[3].y1 = y - (s - 2);
346 segment[3].x2 = mx - fw / 2 - 2;
347 segment[3].y2 = y - (s - 2);
349 XDrawSegments(dpy, root, gc, segment, 4);
351 /* right arrow & end bar */
352 segment[0].x1 = x2 + 1;
353 segment[0].x2 = x2 + 1;
355 /* arrowhead */
356 segment[1].x1 = x2 - 6;
357 segment[1].x2 = x2;
359 segment[2].x1 = x2;
360 segment[2].x2 = x2 - 6;
362 /* line */
363 segment[3].x1 = mx + fw / 2 + 2;
364 segment[3].x2 = x2;
366 XDrawSegments(dpy, root, gc, segment, 4);
368 /* Display the width. */
369 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
370 strlen(num));
371 } else {
372 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
373 / wwin->normal_hints->width_inc,
374 (by - ty - wwin->normal_hints->base_height)
375 / wwin->normal_hints->height_inc);
379 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
381 WScreen *scr = wwin->screen_ptr;
382 WMRect rect;
384 wPreferences.size_display++;
385 wPreferences.size_display %= NUM_DISPLAYS;
387 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
388 WMUnmapWidget(scr->gview);
389 } else {
390 if (wPreferences.size_display == WDIS_CENTER) {
391 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
392 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
393 rect.pos.y + rect.size.height / 2);
394 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
395 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
396 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
397 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
398 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
400 WMMapWidget(scr->gview);
401 showGeometry(wwin, x, y, x + w, y + h, dir);
405 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
407 WScreen *scr = wwin->screen_ptr;
408 WMRect rect;
410 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
411 return;
413 if (wPreferences.size_display == WDIS_CENTER) {
414 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
415 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
416 rect.pos.y + rect.size.height / 2);
417 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
418 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
419 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
420 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
421 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
423 WMMapWidget(scr->gview);
424 showGeometry(wwin, x, y, x + w, y + h, 0);
427 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
429 WWindow *tmpw;
430 WScreen *scr = wwin->screen_ptr;
431 int x, y;
433 if (!array || !WMGetArrayItemCount(array)) {
434 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
435 } else {
436 WMArrayIterator iter;
438 WM_ITERATE_ARRAY(array, tmpw, iter) {
439 x = tmpw->frame_x + dx;
440 y = tmpw->frame_y + dy;
442 #if 1 /* XXX: with xinerama patch was #if 0, check this */
443 /* don't let windows become unreachable */
445 if (x + (int)tmpw->frame->core->width < 20)
446 x = 20 - (int)tmpw->frame->core->width;
447 else if (x + 20 > scr->scr_width)
448 x = scr->scr_width - 20;
450 if (y + (int)tmpw->frame->core->height < 20)
451 y = 20 - (int)tmpw->frame->core->height;
452 else if (y + 20 > scr->scr_height)
453 y = scr->scr_height - 20;
454 #else
455 wScreenBringInside(scr, &x, &y,
456 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
457 #endif
459 wWindowMove(tmpw, x, y);
464 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
466 Window root = wwin->screen_ptr->root_win;
467 GC gc = wwin->screen_ptr->frame_gc;
468 int h = 0;
469 int bottom = 0;
471 if (HAS_BORDER_WITH_SELECT(wwin)) {
472 x += FRAME_BORDER_WIDTH;
473 y += FRAME_BORDER_WIDTH;
476 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
477 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
478 TITLEBAR_EXTEND_SPACE) * 2;
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 updateIndex;
912 Bool attract;
914 hresist = False;
915 vresist = False;
917 updateIndex = False;
919 /* check the direction of the movement */
920 dx = newMouseX - data->mouseX;
921 dy = newMouseY - data->mouseY;
923 data->omouseX = data->mouseX;
924 data->omouseY = data->mouseY;
925 data->mouseX = newMouseX;
926 data->mouseY = newMouseY;
928 winL = data->calcX + dx;
929 winR = data->calcX + data->winWidth + dx;
930 winT = data->calcY + dy;
931 winB = data->calcY + data->winHeight + dy;
933 newX = data->realX;
934 newY = data->realY;
936 if (doResistance) {
937 int l_edge, r_edge;
938 int edge_l, edge_r;
939 int t_edge, b_edge;
940 int edge_t, edge_b;
941 int resist;
943 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
944 attract = wPreferences.attract;
945 /* horizontal movement: check horizontal edge resistances */
946 if (dx || dy) {
947 WMRect rect;
948 int i, head;
949 /* window is the leftmost window: check against screen edge */
951 /* Add inter head resistance 1/2 (if needed) */
952 head = wGetHeadForPointerLocation(scr);
953 rect = wGetRectForHead(scr, head);
955 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
956 edge_l = l_edge - resist;
957 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
958 r_edge = edge_r + resist;
960 /* 1 */
961 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
962 WWindow *looprw;
964 for (i = data->rightIndex - 1; i >= 0; i--) {
965 looprw = data->rightList[i];
966 if (!(data->realY > WBOTTOM(looprw)
967 || (data->realY + data->winHeight) < WTOP(looprw))) {
968 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
969 l_edge = WRIGHT(looprw) + 1;
970 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
972 break;
976 if (attract) {
977 for (i = data->rightIndex; i < data->count; i++) {
978 looprw = data->rightList[i];
979 if (!(data->realY > WBOTTOM(looprw)
980 || (data->realY + data->winHeight) < WTOP(looprw))) {
981 r_edge = WRIGHT(looprw) + 1;
982 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
983 break;
989 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
990 WWindow *looprw;
992 for (i = data->leftIndex - 1; i >= 0; i--) {
993 looprw = data->leftList[i];
994 if (!(data->realY > WBOTTOM(looprw)
995 || (data->realY + data->winHeight) < WTOP(looprw))) {
996 if (attract
997 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
998 && dx > 0)) {
999 edge_r = WLEFT(looprw);
1000 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1002 break;
1006 if (attract)
1007 for (i = data->leftIndex; i < data->count; i++) {
1008 looprw = data->leftList[i];
1009 if (!(data->realY > WBOTTOM(looprw)
1010 || (data->realY + data->winHeight) < WTOP(looprw))) {
1011 edge_l = WLEFT(looprw);
1012 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1013 break;
1019 printf("%d %d\n",winL,winR);
1020 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1023 if ((winL - l_edge) < (r_edge - winL)) {
1024 if (resist > 0) {
1025 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1026 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1027 newX = l_edge;
1028 hresist = True;
1031 } else {
1032 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1033 newX = r_edge;
1034 hresist = True;
1038 if ((winR - edge_l) < (edge_r - winR)) {
1039 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1040 newX = edge_l - data->winWidth;
1041 hresist = True;
1043 } else {
1044 if (resist > 0) {
1045 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1046 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1047 newX = edge_r - data->winWidth;
1048 hresist = True;
1053 /* VeRT */
1054 /* Add inter head resistance 2/2 (if needed) */
1055 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1056 edge_t = t_edge - resist;
1057 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1058 b_edge = edge_b + resist;
1060 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1061 WWindow *looprw;
1063 for (i = data->bottomIndex - 1; i >= 0; i--) {
1064 looprw = data->bottomList[i];
1065 if (!(data->realX > WRIGHT(looprw)
1066 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1067 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1068 t_edge = WBOTTOM(looprw) + 1;
1069 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1071 break;
1075 if (attract) {
1076 for (i = data->bottomIndex; i < data->count; i++) {
1077 looprw = data->bottomList[i];
1078 if (!(data->realX > WRIGHT(looprw)
1079 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1080 b_edge = WBOTTOM(looprw) + 1;
1081 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1082 break;
1088 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1089 WWindow *looprw;
1091 for (i = data->topIndex - 1; i >= 0; i--) {
1092 looprw = data->topList[i];
1093 if (!(data->realX > WRIGHT(looprw)
1094 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1095 if (attract
1096 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1097 && dy > 0)) {
1098 edge_b = WTOP(looprw);
1099 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1101 break;
1105 if (attract)
1106 for (i = data->topIndex; i < data->count; i++) {
1107 looprw = data->topList[i];
1108 if (!(data->realX > WRIGHT(looprw)
1109 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1110 edge_t = WTOP(looprw);
1111 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1112 break;
1117 if ((winT - t_edge) < (b_edge - winT)) {
1118 if (resist > 0) {
1119 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1120 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1121 newY = t_edge;
1122 vresist = True;
1125 } else {
1126 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1127 newY = b_edge;
1128 vresist = True;
1132 if ((winB - edge_t) < (edge_b - winB)) {
1133 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1134 newY = edge_t - data->winHeight;
1135 vresist = True;
1137 } else {
1138 if (resist > 0) {
1139 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1140 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1141 newY = edge_b - data->winHeight;
1142 vresist = True;
1147 /* END VeRT */
1151 /* update window position */
1152 data->calcX += dx;
1153 data->calcY += dy;
1155 if (((dx > 0 && data->calcX - data->realX > 0)
1156 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1157 newX = data->calcX;
1159 if (((dy > 0 && data->calcY - data->realY > 0)
1160 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1161 newY = data->calcY;
1163 if (data->realX != newX || data->realY != newY) {
1165 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1166 showPosition(wwin, data->realX, data->realY);
1168 if (opaqueMove) {
1169 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1170 } else {
1171 /* erase frames */
1172 drawFrames(wwin, scr->selected_windows,
1173 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1176 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1178 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1181 if (!opaqueMove) {
1182 /* draw frames */
1183 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1186 if (!scr->selected_windows) {
1187 showPosition(wwin, newX, newY);
1191 /* recalc relative window position */
1192 if (doResistance && (data->realX != newX || data->realY != newY)) {
1193 updateResistance(wwin, data, newX, newY);
1196 data->realX = newX;
1197 data->realY = newY;
1200 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1202 #define MOVABLE_BIT 0x01
1203 #define RESIZABLE_BIT 0x02
1205 int wKeyboardMoveResizeWindow(WWindow * wwin)
1207 WScreen *scr = wwin->screen_ptr;
1208 Window root = scr->root_win;
1209 XEvent event;
1210 int w = wwin->frame->core->width;
1211 int h = wwin->frame->core->height;
1212 int scr_width = wwin->screen_ptr->scr_width;
1213 int scr_height = wwin->screen_ptr->scr_height;
1214 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1215 int src_x = wwin->frame_x;
1216 int src_y = wwin->frame_y;
1217 int done, off_x, off_y, ww, wh;
1218 int kspeed = _KS;
1219 Time lastTime = 0;
1220 KeyCode shiftl, shiftr, ctrll, 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 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1231 ctrlmode = done = off_x = off_y = 0;
1233 if (modes == RESIZABLE_BIT) {
1234 ctrlmode = 1;
1237 XSync(dpy, False);
1238 wusleep(10000);
1239 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1241 if (!wwin->flags.selected) {
1242 wUnselectWindows(scr);
1244 XGrabServer(dpy);
1245 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1246 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1247 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1249 if (wwin->flags.shaded || scr->selected_windows) {
1250 if (scr->selected_windows)
1251 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1252 else
1253 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1254 if (!scr->selected_windows)
1255 mapPositionDisplay(wwin, src_x, src_y, w, h);
1256 } else {
1257 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1259 ww = w;
1260 wh = h;
1261 while (1) {
1263 looper.ox=off_x;
1264 looper.oy=off_y;
1266 do {
1267 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1268 | ButtonPressMask | ExposureMask, &event);
1269 if (event.type == Expose) {
1270 WMHandleEvent(&event);
1272 } while (event.type == Expose);
1274 if (wwin->flags.shaded || scr->selected_windows) {
1275 if (scr->selected_windows)
1276 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1277 else
1278 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1279 /*** I HATE EDGE RESISTANCE - ]d ***/
1280 } else {
1281 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1284 if (ctrlmode)
1285 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1288 XUngrabServer(dpy);
1289 XSync(dpy, False);
1291 switch (event.type) {
1292 case KeyPress:
1293 /* accelerate */
1294 if (event.xkey.time - lastTime > 50) {
1295 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1296 } else {
1297 if (kspeed < 20) {
1298 kspeed++;
1301 if (kspeed < _KS)
1302 kspeed = _KS;
1303 lastTime = event.xkey.time;
1304 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1305 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1306 ctrlmode = 1;
1307 wUnselectWindows(scr);
1308 } else {
1309 ctrlmode = 0;
1312 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1313 if (ctrlmode)
1314 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1315 else
1316 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1317 } else {
1319 keysym = XLookupKeysym(&event.xkey, 0);
1320 switch (keysym) {
1321 case XK_Return:
1322 done = 2;
1323 break;
1324 case XK_Escape:
1325 done = 1;
1326 break;
1327 case XK_Up:
1328 #ifdef XK_KP_Up
1329 case XK_KP_Up:
1330 #endif
1331 case XK_k:
1332 if (ctrlmode) {
1333 if (moment != UP)
1334 h = wh;
1335 h -= kspeed;
1336 moment = UP;
1337 if (h < 1)
1338 h = 1;
1339 } else
1340 off_y -= kspeed;
1341 break;
1342 case XK_Down:
1343 #ifdef XK_KP_Down
1344 case XK_KP_Down:
1345 #endif
1346 case XK_j:
1347 if (ctrlmode) {
1348 if (moment != DOWN)
1349 h = wh;
1350 h += kspeed;
1351 moment = DOWN;
1352 } else
1353 off_y += kspeed;
1354 break;
1355 case XK_Left:
1356 #ifdef XK_KP_Left
1357 case XK_KP_Left:
1358 #endif
1359 case XK_h:
1360 if (ctrlmode) {
1361 if (moment != LEFT)
1362 w = ww;
1363 w -= kspeed;
1364 if (w < 1)
1365 w = 1;
1366 moment = LEFT;
1367 } else
1368 off_x -= kspeed;
1369 break;
1370 case XK_Right:
1371 #ifdef XK_KP_Right
1372 case XK_KP_Right:
1373 #endif
1374 case XK_l:
1375 if (ctrlmode) {
1376 if (moment != RIGHT)
1377 w = ww;
1378 w += kspeed;
1379 moment = RIGHT;
1380 } else
1381 off_x += kspeed;
1382 break;
1385 ww = w;
1386 wh = h;
1387 wh -= vert_border;
1388 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1389 wh += vert_border;
1391 if (wPreferences.ws_cycle) {
1392 if (src_x + off_x + ww < 20) {
1393 if (!scr->current_workspace) {
1394 wWorkspaceChange(scr, scr->workspace_count - 1);
1395 } else
1396 wWorkspaceChange(scr, scr->current_workspace - 1);
1397 off_x += scr_width;
1398 } else if (src_x + off_x + 20 > scr_width) {
1399 if (scr->current_workspace == scr->workspace_count - 1) {
1400 wWorkspaceChange(scr, 0);
1401 } else
1402 wWorkspaceChange(scr, scr->current_workspace + 1);
1403 off_x -= scr_width;
1405 } else {
1406 if (src_x + off_x + ww < 20)
1407 off_x = 20 - ww - src_x;
1408 else if (src_x + off_x + 20 > scr_width)
1409 off_x = scr_width - 20 - src_x;
1412 if (src_y + off_y + wh < 20) {
1413 off_y = 20 - wh - src_y;
1414 } else if (src_y + off_y + 20 > scr_height) {
1415 off_y = scr_height - 20 - src_y;
1418 break;
1419 case ButtonPress:
1420 case ButtonRelease:
1421 done = 1;
1422 break;
1423 case Expose:
1424 WMHandleEvent(&event);
1425 while (XCheckTypedEvent(dpy, Expose, &event)) {
1426 WMHandleEvent(&event);
1428 break;
1430 default:
1431 WMHandleEvent(&event);
1432 break;
1435 XGrabServer(dpy);
1436 /*xxx */
1438 if (wwin->flags.shaded && !scr->selected_windows) {
1439 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1440 } else {
1441 if (ctrlmode) {
1442 WMUnmapWidget(scr->gview);
1443 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1444 } else if (!scr->selected_windows) {
1445 WMUnmapWidget(scr->gview);
1446 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1450 if (wwin->flags.shaded || scr->selected_windows) {
1451 if (scr->selected_windows)
1452 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1453 else
1454 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1455 } else {
1456 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1459 if (ctrlmode) {
1460 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1462 } else if (!scr->selected_windows)
1463 showPosition(wwin, src_x + off_x, src_y + off_y);
1465 if (done) {
1466 scr->keymove_tick = 0;
1468 WMDeleteTimerWithClientData(&looper);
1470 if (wwin->flags.shaded || scr->selected_windows) {
1471 if (scr->selected_windows)
1472 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1473 else
1474 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1475 } else {
1476 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1479 if (ctrlmode) {
1480 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1481 src_y + off_y + wh, 0);
1482 WMUnmapWidget(scr->gview);
1483 } else
1484 WMUnmapWidget(scr->gview);
1486 XUngrabKeyboard(dpy, CurrentTime);
1487 XUngrabPointer(dpy, CurrentTime);
1488 XUngrabServer(dpy);
1490 if (done == 2) {
1491 if (wwin->flags.shaded || scr->selected_windows) {
1492 if (!scr->selected_windows) {
1493 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1494 wWindowSynthConfigureNotify(wwin);
1495 } else {
1496 WMArrayIterator iter;
1497 WWindow *foo;
1499 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1501 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1502 wWindowSynthConfigureNotify(foo);
1505 } else {
1506 if (wwin->client.width != ww) {
1507 wwin->flags.user_changed_width = 1;
1508 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
1511 if (wwin->client.height != wh - vert_border) {
1512 wwin->flags.user_changed_height = 1;
1513 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1516 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1517 wWindowSynthConfigureNotify(wwin);
1519 wWindowChangeWorkspace(wwin, scr->current_workspace);
1520 wSetFocusTo(scr, wwin);
1523 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1524 head != wGetHeadForWindow(wwin)) {
1525 wArrangeIcons(scr, True);
1528 return 1;
1534 *----------------------------------------------------------------------
1535 * wMouseMoveWindow--
1536 * Move the named window and the other selected ones (if any),
1537 * interactively. Also shows the position of the window, if only one
1538 * window is being moved.
1539 * If the window is not on the selected window list, the selected
1540 * windows are deselected.
1541 * If shift is pressed during the operation, the position display
1542 * is changed to another type.
1544 * Returns:
1545 * True if the window was moved, False otherwise.
1547 * Side effects:
1548 * The window(s) position is changed, and the client(s) are
1549 * notified about that.
1550 * The position display configuration may be changed.
1551 *----------------------------------------------------------------------
1553 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1555 WScreen *scr = wwin->screen_ptr;
1556 XEvent event;
1557 Window root = scr->root_win;
1558 KeyCode shiftl, shiftr;
1559 Bool done = False;
1560 int started = 0;
1561 int warped = 0;
1562 /* This needs not to change while moving, else bad things can happen */
1563 int opaqueMove = wPreferences.opaque_move;
1564 MoveData moveData;
1565 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1566 ? wGetHeadForWindow(wwin)
1567 : scr->xine_info.primary_head);
1569 if (!IS_MOVABLE(wwin))
1570 return False;
1572 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1573 XSetWindowAttributes attr;
1575 attr.save_under = True;
1576 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1579 initMoveData(wwin, &moveData);
1581 moveData.mouseX = ev->xmotion.x_root;
1582 moveData.mouseY = ev->xmotion.y_root;
1584 if (!wwin->flags.selected) {
1585 /* this window is not selected, unselect others and move only wwin */
1586 wUnselectWindows(scr);
1588 #ifdef DEBUG
1589 puts("Moving window");
1590 #endif
1591 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1592 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1593 while (!done) {
1594 if (warped) {
1595 int junk;
1596 Window junkw;
1598 /* XWarpPointer() doesn't seem to generate Motion events, so
1599 * we've got to simulate them */
1600 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1601 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1602 } else {
1603 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1604 | PointerMotionHintMask
1605 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1607 if (event.type == MotionNotify) {
1608 /* compress MotionNotify events */
1609 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1610 if (!checkMouseSamplingRate(&event))
1611 continue;
1614 switch (event.type) {
1615 case KeyPress:
1616 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1617 && started && !scr->selected_windows) {
1619 if (!opaqueMove) {
1620 drawFrames(wwin, scr->selected_windows,
1621 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1624 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1625 showPosition(wwin, moveData.realX, moveData.realY);
1626 XUngrabServer(dpy);
1628 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1629 moveData.winWidth, moveData.winHeight);
1631 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1632 XGrabServer(dpy);
1633 showPosition(wwin, moveData.realX, moveData.realY);
1636 if (!opaqueMove) {
1637 drawFrames(wwin, scr->selected_windows,
1638 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1640 /*} else {
1641 WMHandleEvent(&event); this causes problems needs fixing */
1643 break;
1645 case MotionNotify:
1646 if (started) {
1647 updateWindowPosition(wwin, &moveData,
1648 scr->selected_windows == NULL
1649 && wPreferences.edge_resistance > 0,
1650 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1652 if (!warped && !wPreferences.no_autowrap) {
1653 int oldWorkspace = scr->current_workspace;
1655 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1656 showPosition(wwin, moveData.realX, moveData.realY);
1657 XUngrabServer(dpy);
1659 if (!opaqueMove) {
1660 drawFrames(wwin, scr->selected_windows,
1661 moveData.realX - wwin->frame_x,
1662 moveData.realY - wwin->frame_y);
1664 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1665 if (scr->current_workspace != oldWorkspace
1666 && wPreferences.edge_resistance > 0
1667 && scr->selected_windows == NULL)
1668 updateMoveData(wwin, &moveData);
1669 warped = 1;
1671 if (!opaqueMove) {
1672 drawFrames(wwin, scr->selected_windows,
1673 moveData.realX - wwin->frame_x,
1674 moveData.realY - wwin->frame_y);
1676 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1677 XSync(dpy, False);
1678 showPosition(wwin, moveData.realX, moveData.realY);
1679 XGrabServer(dpy);
1681 } else {
1682 warped = 0;
1684 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1685 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1687 XChangeActivePointerGrab(dpy, ButtonMotionMask
1688 | ButtonReleaseMask | ButtonPressMask,
1689 wCursor[WCUR_MOVE], CurrentTime);
1690 started = 1;
1691 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1693 if (!scr->selected_windows)
1694 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1695 moveData.winWidth, moveData.winHeight);
1697 if (started && !opaqueMove)
1698 drawFrames(wwin, scr->selected_windows, 0, 0);
1700 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1701 && !scr->selected_windows)) {
1702 XGrabServer(dpy);
1703 if (wPreferences.move_display == WDIS_NEW)
1704 showPosition(wwin, moveData.realX, moveData.realY);
1707 break;
1709 case ButtonPress:
1710 break;
1712 case ButtonRelease:
1713 if (event.xbutton.button != ev->xbutton.button)
1714 break;
1716 if (started) {
1717 XEvent e;
1718 if (!opaqueMove) {
1719 drawFrames(wwin, scr->selected_windows,
1720 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1721 XSync(dpy, 0);
1722 doWindowMove(wwin, scr->selected_windows,
1723 moveData.realX - wwin->frame_x,
1724 moveData.realY - wwin->frame_y);
1726 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1727 wWindowSynthConfigureNotify(wwin);
1728 #endif
1729 XUngrabKeyboard(dpy, CurrentTime);
1730 XUngrabServer(dpy);
1731 if (!opaqueMove) {
1732 wWindowChangeWorkspace(wwin, scr->current_workspace);
1733 wSetFocusTo(scr, wwin);
1735 if (wPreferences.move_display == WDIS_NEW)
1736 showPosition(wwin, moveData.realX, moveData.realY);
1738 /* discard all enter/leave events that happened until
1739 * the time the button was released */
1740 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1741 if (e.xcrossing.time > event.xbutton.time) {
1742 XPutBackEvent(dpy, &e);
1743 break;
1746 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1747 if (e.xcrossing.time > event.xbutton.time) {
1748 XPutBackEvent(dpy, &e);
1749 break;
1753 if (!scr->selected_windows) {
1754 /* get rid of the geometry window */
1755 WMUnmapWidget(scr->gview);
1758 #ifdef DEBUG
1759 puts("End move window");
1760 #endif
1761 done = True;
1762 break;
1764 default:
1765 if (started && !opaqueMove) {
1766 drawFrames(wwin, scr->selected_windows,
1767 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1768 XUngrabServer(dpy);
1769 WMHandleEvent(&event);
1770 XSync(dpy, False);
1771 XGrabServer(dpy);
1772 drawFrames(wwin, scr->selected_windows,
1773 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1774 } else {
1775 WMHandleEvent(&event);
1777 break;
1781 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1782 XSetWindowAttributes attr;
1784 attr.save_under = False;
1785 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1789 freeMoveData(&moveData);
1791 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1792 head != wGetHeadForWindow(wwin)) {
1793 wArrangeIcons(scr, True);
1795 return started;
1798 #define RESIZEBAR 1
1799 #define HCONSTRAIN 2
1801 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1803 int w = wwin->frame->core->width - 1;
1804 int cw = wwin->frame->resizebar_corner_width;
1805 int dir;
1807 /* if not resizing through the resizebar */
1808 if (!(flags & RESIZEBAR)) {
1809 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1810 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1811 if (abs(dx) < 2 || abs(dy) < 2) {
1812 if (abs(dy) > abs(dx))
1813 xdir = 0;
1814 else
1815 ydir = 0;
1817 return (xdir | ydir);
1820 /* window is too narrow. allow diagonal resize */
1821 if (cw * 2 >= w) {
1822 int ydir;
1824 if (flags & HCONSTRAIN)
1825 ydir = 0;
1826 else
1827 ydir = DOWN;
1828 if (x < cw)
1829 return (LEFT | ydir);
1830 else
1831 return (RIGHT | ydir);
1833 /* vertical resize */
1834 if ((x > cw) && (x < w - cw))
1835 return DOWN;
1837 if (x < cw)
1838 dir = LEFT;
1839 else
1840 dir = RIGHT;
1842 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1843 dir |= DOWN;
1845 return dir;
1848 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1850 XEvent event;
1851 WScreen *scr = wwin->screen_ptr;
1852 Window root = scr->root_win;
1853 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1854 int fw = wwin->frame->core->width;
1855 int fh = wwin->frame->core->height;
1856 int fx = wwin->frame_x;
1857 int fy = wwin->frame_y;
1858 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1859 int orig_x, orig_y;
1860 int started;
1861 int dw, dh;
1862 int rw = fw, rh = fh;
1863 int rx1, ry1, rx2, ry2;
1864 int res = 0;
1865 KeyCode shiftl, shiftr;
1866 int h = 0;
1867 int orig_fx = fx;
1868 int orig_fy = fy;
1869 int orig_fw = fw;
1870 int orig_fh = fh;
1871 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1872 ? wGetHeadForWindow(wwin)
1873 : scr->xine_info.primary_head);
1875 if (!IS_RESIZABLE(wwin))
1876 return;
1878 if (wwin->flags.shaded) {
1879 wwarning("internal error: tryein");
1880 return;
1882 orig_x = ev->xbutton.x_root;
1883 orig_y = ev->xbutton.y_root;
1885 started = 0;
1886 #ifdef DEBUG
1887 puts("Resizing window");
1888 #endif
1890 wUnselectWindows(scr);
1891 rx1 = fx;
1892 rx2 = fx + fw - 1;
1893 ry1 = fy;
1894 ry2 = fy + fh - 1;
1895 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1896 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1897 if (HAS_TITLEBAR(wwin))
1898 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
1899 TITLEBAR_EXTEND_SPACE) * 2;
1900 else
1901 h = 0;
1902 while (1) {
1903 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1904 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1905 if (!checkMouseSamplingRate(&event))
1906 continue;
1908 switch (event.type) {
1909 case KeyPress:
1910 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1911 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1912 && started) {
1913 drawTransparentFrame(wwin, fx, fy, fw, fh);
1914 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1915 drawTransparentFrame(wwin, fx, fy, fw, fh);
1917 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1918 break;
1920 case MotionNotify:
1921 if (started) {
1922 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1924 dw = 0;
1925 dh = 0;
1927 orig_fx = fx;
1928 orig_fy = fy;
1929 orig_fw = fw;
1930 orig_fh = fh;
1932 if (res & LEFT)
1933 dw = orig_x - event.xmotion.x_root;
1934 else if (res & RIGHT)
1935 dw = event.xmotion.x_root - orig_x;
1936 if (res & UP)
1937 dh = orig_y - event.xmotion.y_root;
1938 else if (res & DOWN)
1939 dh = event.xmotion.y_root - orig_y;
1941 orig_x = event.xmotion.x_root;
1942 orig_y = event.xmotion.y_root;
1944 rw += dw;
1945 rh += dh;
1946 fw = rw;
1947 fh = rh - vert_border;
1948 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1949 fh += vert_border;
1950 if (res & LEFT)
1951 fx = rx2 - fw + 1;
1952 else if (res & RIGHT)
1953 fx = rx1;
1954 if (res & UP)
1955 fy = ry2 - fh + 1;
1956 else if (res & DOWN)
1957 fy = ry1;
1958 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1959 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1960 int tx, ty;
1961 Window junkw;
1962 int flags;
1964 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1965 orig_x, orig_y, &tx, &ty, &junkw);
1967 /* check if resizing through resizebar */
1968 if (is_resizebar)
1969 flags = RESIZEBAR;
1970 else
1971 flags = 0;
1973 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1974 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1975 flags |= HCONSTRAIN;
1977 res = getResizeDirection(wwin, tx, ty,
1978 orig_x - event.xmotion.x_root,
1979 orig_y - event.xmotion.y_root, flags);
1981 if (res == (UP | LEFT))
1982 XChangeActivePointerGrab(dpy, ButtonMotionMask
1983 | ButtonReleaseMask | ButtonPressMask,
1984 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1985 else if (res == (UP | RIGHT))
1986 XChangeActivePointerGrab(dpy, ButtonMotionMask
1987 | ButtonReleaseMask | ButtonPressMask,
1988 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1989 else if (res == (DOWN | LEFT))
1990 XChangeActivePointerGrab(dpy, ButtonMotionMask
1991 | ButtonReleaseMask | ButtonPressMask,
1992 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
1993 else if (res == (DOWN | RIGHT))
1994 XChangeActivePointerGrab(dpy, ButtonMotionMask
1995 | ButtonReleaseMask | ButtonPressMask,
1996 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
1997 else if (res == DOWN || res == UP)
1998 XChangeActivePointerGrab(dpy, ButtonMotionMask
1999 | ButtonReleaseMask | ButtonPressMask,
2000 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2001 else if (res & (DOWN | UP))
2002 XChangeActivePointerGrab(dpy, ButtonMotionMask
2003 | ButtonReleaseMask | ButtonPressMask,
2004 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2005 else if (res & (LEFT | RIGHT))
2006 XChangeActivePointerGrab(dpy, ButtonMotionMask
2007 | ButtonReleaseMask | ButtonPressMask,
2008 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2010 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2012 XGrabServer(dpy);
2014 /* Draw the resize frame for the first time. */
2015 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2017 drawTransparentFrame(wwin, fx, fy, fw, fh);
2019 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2021 started = 1;
2023 if (started) {
2024 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2025 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2026 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2027 drawTransparentFrame(wwin, fx, fy, fw, fh);
2028 } else {
2029 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2030 drawTransparentFrame(wwin, fx, fy, fw, fh);
2032 if (fh != orig_fh || fw != orig_fw) {
2033 if (wPreferences.size_display == WDIS_NEW) {
2034 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2035 orig_fy + orig_fh, res);
2037 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2040 break;
2042 case ButtonPress:
2043 break;
2045 case ButtonRelease:
2046 if (event.xbutton.button != ev->xbutton.button)
2047 break;
2049 if (started) {
2050 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2052 drawTransparentFrame(wwin, fx, fy, fw, fh);
2054 XUngrabKeyboard(dpy, CurrentTime);
2055 WMUnmapWidget(scr->gview);
2056 XUngrabServer(dpy);
2058 if (wwin->client.width != fw) {
2059 wwin->flags.user_changed_width = 1;
2060 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
2063 if (wwin->client.height != fh - vert_border) {
2064 wwin->flags.user_changed_height = 1;
2065 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2068 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2070 #ifdef DEBUG
2071 puts("End resize window");
2072 #endif
2073 return;
2075 default:
2076 WMHandleEvent(&event);
2080 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin)) {
2081 wArrangeIcons(scr, True);
2085 #undef LEFT
2086 #undef RIGHT
2087 #undef HORIZONTAL
2088 #undef UP
2089 #undef DOWN
2090 #undef VERTICAL
2091 #undef HCONSTRAIN
2092 #undef RESIZEBAR
2094 void wUnselectWindows(WScreen * scr)
2096 WWindow *wwin;
2098 if (!scr->selected_windows)
2099 return;
2101 while (WMGetArrayItemCount(scr->selected_windows)) {
2102 wwin = WMGetFromArray(scr->selected_windows, 0);
2103 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2104 wIconSelect(wwin->icon);
2106 wSelectWindow(wwin, False);
2108 WMFreeArray(scr->selected_windows);
2109 scr->selected_windows = NULL;
2112 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2114 WWindow *tmpw;
2116 /* select the windows and put them in the selected window list */
2117 tmpw = scr->focused_window;
2118 while (tmpw != NULL) {
2119 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2120 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2121 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2122 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2123 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2124 wSelectWindow(tmpw, True);
2127 tmpw = tmpw->prev;
2131 void wSelectWindows(WScreen * scr, XEvent * ev)
2133 XEvent event;
2134 Window root = scr->root_win;
2135 GC gc = scr->frame_gc;
2136 int xp = ev->xbutton.x_root;
2137 int yp = ev->xbutton.y_root;
2138 int w = 0, h = 0;
2139 int x = xp, y = yp;
2141 #ifdef DEBUG
2142 puts("Selecting windows");
2143 #endif
2144 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2145 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2146 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2147 return;
2149 XGrabServer(dpy);
2151 wUnselectWindows(scr);
2153 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2154 while (1) {
2155 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2157 switch (event.type) {
2158 case MotionNotify:
2159 XDrawRectangle(dpy, root, gc, x, y, w, h);
2160 x = event.xmotion.x_root;
2161 if (x < xp) {
2162 w = xp - x;
2163 } else {
2164 w = x - xp;
2165 x = xp;
2167 y = event.xmotion.y_root;
2168 if (y < yp) {
2169 h = yp - y;
2170 } else {
2171 h = y - yp;
2172 y = yp;
2174 XDrawRectangle(dpy, root, gc, x, y, w, h);
2175 break;
2177 case ButtonPress:
2178 break;
2180 case ButtonRelease:
2181 if (event.xbutton.button != ev->xbutton.button)
2182 break;
2184 XDrawRectangle(dpy, root, gc, x, y, w, h);
2185 XUngrabServer(dpy);
2186 XUngrabPointer(dpy, CurrentTime);
2187 selectWindowsInside(scr, x, y, x + w, y + h);
2189 #ifdef DEBUG
2190 puts("End window selection");
2191 #endif
2192 return;
2194 default:
2195 WMHandleEvent(&event);
2196 break;
2201 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2203 WScreen *scr = wwin->screen_ptr;
2204 Window root = scr->root_win;
2205 int x, y, h = 0;
2206 XEvent event;
2207 KeyCode shiftl, shiftr;
2208 Window junkw;
2209 int junk;
2211 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2212 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2213 *x_ret = 0;
2214 *y_ret = 0;
2215 return;
2217 if (HAS_TITLEBAR(wwin)) {
2218 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2219 TITLEBAR_EXTEND_SPACE) * 2;
2220 height += h;
2222 if (HAS_RESIZEBAR(wwin)) {
2223 height += RESIZEBAR_HEIGHT;
2225 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2226 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2227 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2229 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2231 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2232 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2233 while (1) {
2234 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2236 if (!checkMouseSamplingRate(&event))
2237 continue;
2239 switch (event.type) {
2240 case KeyPress:
2241 if ((event.xkey.keycode == shiftl)
2242 || (event.xkey.keycode == shiftr)) {
2243 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2244 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2245 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2247 break;
2249 case MotionNotify:
2250 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2252 x = event.xmotion.x_root;
2253 y = event.xmotion.y_root;
2255 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2256 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2258 showPosition(wwin, x - width / 2, y - h / 2);
2260 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2262 break;
2264 case ButtonPress:
2265 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2266 XSync(dpy, 0);
2267 *x_ret = x - width / 2;
2268 *y_ret = y - h / 2;
2269 XUngrabPointer(dpy, CurrentTime);
2270 XUngrabKeyboard(dpy, CurrentTime);
2271 /* get rid of the geometry window */
2272 WMUnmapWidget(scr->gview);
2273 return;
2275 default:
2276 WMHandleEvent(&event);
2277 break;