- Fix that enables that the virtual desktop code be disabled on the fly
[wmaker-crm.git] / src / moveres.c
blob26e6a79f78fc07bc7b093587b9d8bda5c345c5c3
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "wcore.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "actions.h"
41 #include "workspace.h"
43 #include "geomview.h"
44 #include "screen.h"
45 #include "xinerama.h"
47 #include <WINGs/WINGsP.h>
50 /* How many different types of geometry/position
51 display thingies are there? */
52 #define NUM_DISPLAYS 5
54 #define LEFT 1
55 #define RIGHT 2
56 #define HORIZONTAL (LEFT|RIGHT)
57 #define UP 4
58 #define DOWN 8
59 #define VERTICAL (UP|DOWN)
62 /* True if window currently has a border. This also includes borderless
63 * windows which are currently selected
65 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
68 /****** Global Variables ******/
69 extern Time LastTimestamp;
71 extern Cursor wCursor[WCUR_LAST];
73 extern WPreferences wPreferences;
75 extern Atom _XA_WM_PROTOCOLS;
80 *----------------------------------------------------------------------
81 * checkMouseSamplingRate-
82 * For lowering the mouse motion sampling rate for machines where
83 * it's too high (SGIs). If it returns False then the event should be
84 * ignored.
85 *----------------------------------------------------------------------
87 static Bool
88 checkMouseSamplingRate(XEvent *ev)
90 static Time previousMotion = 0;
92 if (ev->type == MotionNotify) {
93 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
94 return False;
95 } else {
96 previousMotion = ev->xmotion.time;
99 return True;
105 *----------------------------------------------------------------------
106 * moveGeometryDisplayCentered
108 * routine that moves the geometry/position window on scr so it is
109 * centered over the given coordinates (x,y). Also the window position
110 * is clamped so it stays on the screen at all times.
111 *----------------------------------------------------------------------
113 static void
114 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
116 unsigned int w = WMWidgetWidth(scr->gview);
117 unsigned int h = WMWidgetHeight(scr->gview);
118 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
120 x -= w / 2;
121 y -= h / 2;
123 /* dead area check */
124 if (scr->xine_info.count) {
125 WMRect rect;
126 int head, flags;
128 rect.pos.x = x;
129 rect.pos.y = y;
130 rect.size.width = w;
131 rect.size.height = h;
133 head = wGetRectPlacementInfo(scr, rect, &flags);
135 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
136 rect = wGetRectForHead(scr, head);
137 x1 = rect.pos.x;
138 y1 = rect.pos.y;
139 x2 = x1 + rect.size.width;
140 y2 = y1 + rect.size.height;
144 if (x < x1 + 1)
145 x = x1 + 1;
146 else if (x > (x2 - w))
147 x = x2 - w;
149 if (y < y1 + 1)
150 y = y1 + 1;
151 else if (y > (y2 - h))
152 y = y2 - h;
155 WMMoveWidget(scr->gview, x, y);
159 static void
160 showPosition(WWindow *wwin, int x, int y)
162 WScreen *scr = wwin->screen_ptr;
164 if (wPreferences.move_display == WDIS_NEW) {
165 #if 0
166 int width = wwin->frame->core->width;
167 int height = wwin->frame->core->height;
169 GC lgc = scr->line_gc;
170 XSetForeground(dpy, lgc, scr->line_pixel);
171 sprintf(num, "%i", x);
173 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
174 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
175 y+height+2);
176 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
177 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
178 scr->scr_height);
179 #endif
180 } else {
181 #ifdef VIRTUAL_DESKTOP
182 WSetGeometryViewShownPosition(scr->gview,
183 x + scr->workspaces[scr->current_workspace]->view_x,
184 y + scr->workspaces[scr->current_workspace]->view_y);
185 #else
186 WSetGeometryViewShownPosition(scr->gview, x, y);
187 #endif
192 static void
193 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
195 WScreen *scr = wwin->screen_ptr;
196 WMRect rect;
198 wPreferences.move_display++;
199 wPreferences.move_display %= NUM_DISPLAYS;
201 if (wPreferences.move_display == WDIS_NEW) {
202 wPreferences.move_display++;
203 wPreferences.move_display %= NUM_DISPLAYS;
206 if (wPreferences.move_display == WDIS_NONE) {
207 WMUnmapWidget(scr->gview);
208 } else {
209 if (wPreferences.move_display == WDIS_CENTER) {
210 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
211 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width/2,
212 rect.pos.y + rect.size.height/2);
213 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
214 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
215 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
216 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
217 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
219 WMMapWidget(scr->gview);
224 static void
225 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
227 WScreen *scr = wwin->screen_ptr;
228 WMRect rect;
230 if (wPreferences.move_display == WDIS_NEW
231 || wPreferences.move_display == WDIS_NONE) {
232 return;
233 } else if (wPreferences.move_display == WDIS_CENTER) {
234 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
235 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width/2,
236 rect.pos.y + rect.size.height/2);
237 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
238 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
239 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
240 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
241 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
243 WMMapWidget(scr->gview);
244 WSetGeometryViewShownPosition(scr->gview, x, y);
248 static void
249 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
251 WScreen *scr = wwin->screen_ptr;
252 Window root = scr->root_win;
253 GC gc = scr->line_gc;
254 int ty, by, my, x, y, mx, s;
255 char num[16];
256 XSegment segment[4];
257 int fw, fh;
259 /* This seems necessary for some odd reason (too lazy to write x1-1 and
260 * x2-1 everywhere below in the code). But why only for x? */
261 x1--; x2--;
263 if (HAS_BORDER_WITH_SELECT(wwin)) {
264 x1 += FRAME_BORDER_WIDTH;
265 x2 += FRAME_BORDER_WIDTH;
266 y1 += FRAME_BORDER_WIDTH;
267 y2 += FRAME_BORDER_WIDTH;
270 ty = y1 + wwin->frame->top_width;
271 by = y2 - wwin->frame->bottom_width;
273 if (wPreferences.size_display == WDIS_NEW) {
274 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
275 fh = scr->tech_draw_font->ascent+scr->tech_draw_font->descent;
277 XSetForeground(dpy, gc, scr->line_pixel);
279 /* vertical geometry */
280 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
281 x = x2;
282 s = -15;
283 } else {
284 x = x1;
285 s = 15;
287 my = (ty + by) / 2;
289 /* top arrow & end bar */
290 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
291 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
293 /* arrowhead */
294 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
295 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
297 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
298 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
300 /* line */
301 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
302 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
304 XDrawSegments(dpy, root, gc, segment, 4);
306 /* bottom arrow & end bar */
307 segment[0].y1 = by;
308 segment[0].y2 = by;
310 /* arrowhead */
311 segment[1].y1 = by - 1;
312 segment[1].y2 = by - 7;
314 segment[2].y1 = by - 1;
315 segment[2].y2 = by - 7;
317 /* line */
318 segment[3].y1 = my + fh/2 + 2;
319 segment[3].y2 = by - 1;
321 XDrawSegments(dpy, root, gc, segment, 4);
323 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
324 wwin->normal_hints->height_inc);
325 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
327 /* Display the height. */
328 XSetFont(dpy, gc, scr->tech_draw_font->fid);
329 XDrawString(dpy, root, gc, x - s + 3 - fw/2, my + scr->tech_draw_font->ascent - fh/2 + 1, num, strlen(num));
331 /* horizontal geometry */
332 if (y1 < 15) {
333 y = y2;
334 s = -15;
335 } else {
336 y = y1;
337 s = 15;
339 mx = x1 + (x2 - x1)/2;
340 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
341 wwin->normal_hints->width_inc);
342 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
344 /* left arrow & end bar */
345 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
346 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
348 /* arrowhead */
349 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
350 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
352 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
353 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
355 /* line */
356 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
357 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
359 XDrawSegments(dpy, root, gc, segment, 4);
361 /* right arrow & end bar */
362 segment[0].x1 = x2 + 1;
363 segment[0].x2 = x2 + 1;
365 /* arrowhead */
366 segment[1].x1 = x2 - 6;
367 segment[1].x2 = x2;
369 segment[2].x1 = x2;
370 segment[2].x2 = x2 - 6;
372 /* line */
373 segment[3].x1 = mx + fw/2 + 2;
374 segment[3].x2 = x2;
376 XDrawSegments(dpy, root, gc, segment, 4);
378 /* Display the width. */
379 XDrawString(dpy, root, gc, mx - fw/2 + 1, y - s + scr->tech_draw_font->ascent - fh/2 + 1, num, strlen(num));
380 } else {
381 WSetGeometryViewShownSize(scr->gview,
382 (x2 - x1 - wwin->normal_hints->base_width)
383 / wwin->normal_hints->width_inc,
384 (by - ty - wwin->normal_hints->base_height)
385 / wwin->normal_hints->height_inc);
390 static void
391 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
393 WScreen *scr = wwin->screen_ptr;
394 WMRect rect;
396 wPreferences.size_display++;
397 wPreferences.size_display %= NUM_DISPLAYS;
399 if (wPreferences.size_display == WDIS_NEW
400 || wPreferences.size_display == WDIS_NONE) {
401 WMUnmapWidget(scr->gview);
402 } else {
403 if (wPreferences.size_display == WDIS_CENTER) {
404 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
405 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width/2,
406 rect.pos.y + rect.size.height/2);
407 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
408 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
409 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
410 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
411 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
413 WMMapWidget(scr->gview);
414 showGeometry(wwin, x, y, x + w, y + h, dir);
419 static void
420 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
422 WScreen *scr = wwin->screen_ptr;
423 WMRect rect;
425 if (wPreferences.size_display == WDIS_NEW
426 || wPreferences.size_display == WDIS_NONE)
427 return;
429 if (wPreferences.size_display == WDIS_CENTER) {
430 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
431 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width/2,
432 rect.pos.y + rect.size.height/2);
433 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
434 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
435 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
436 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
437 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
439 WMMapWidget(scr->gview);
440 showGeometry(wwin, x, y, x + w, y + h, 0);
445 static void
446 doWindowMove(WWindow *wwin, WMArray *array, int dx, int dy)
448 WWindow *tmpw;
449 WScreen *scr = wwin->screen_ptr;
450 int x, y;
452 if (!array || !WMGetArrayItemCount(array)) {
453 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
454 } else {
455 WMArrayIterator iter;
457 WM_ITERATE_ARRAY(array, tmpw, iter) {
458 x = tmpw->frame_x + dx;
459 y = tmpw->frame_y + dy;
461 #if 1 /* XXX: with xinerama patch was #if 0, check this */
462 /* don't let windows become unreachable */
464 if (x + (int)tmpw->frame->core->width < 20)
465 x = 20 - (int)tmpw->frame->core->width;
466 else if (x + 20 > scr->scr_width)
467 x = scr->scr_width - 20;
469 if (y + (int)tmpw->frame->core->height < 20)
470 y = 20 - (int)tmpw->frame->core->height;
471 else if (y + 20 > scr->scr_height)
472 y = scr->scr_height - 20;
473 #else
474 wScreenBringInside(scr, &x, &y,
475 (int)tmpw->frame->core->width,
476 (int)tmpw->frame->core->height);
477 #endif
479 wWindowMove(tmpw, x, y);
485 static void
486 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
488 Window root = wwin->screen_ptr->root_win;
489 GC gc = wwin->screen_ptr->frame_gc;
490 int h = 0;
491 int bottom = 0;
493 if (HAS_BORDER_WITH_SELECT(wwin)) {
494 x += FRAME_BORDER_WIDTH;
495 y += FRAME_BORDER_WIDTH;
498 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
499 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
501 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
502 /* Can't use wwin-frame->bottom_width because, in some cases
503 (e.g. interactive placement), frame does not point to anything. */
504 bottom = RESIZEBAR_HEIGHT;
506 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
508 if (h > 0) {
509 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
511 if (bottom > 0) {
512 XDrawLine(dpy, root, gc, x, y + height - bottom,
513 x + width, y + height - bottom);
518 static void
519 drawFrames(WWindow *wwin, WMArray *array, int dx, int dy)
521 WWindow *tmpw;
522 int scr_width = wwin->screen_ptr->scr_width;
523 int scr_height = wwin->screen_ptr->scr_height;
524 int x, y;
526 if (!array) {
528 x = wwin->frame_x + dx;
529 y = wwin->frame_y + dy;
531 drawTransparentFrame(wwin, x, y,
532 wwin->frame->core->width,
533 wwin->frame->core->height);
535 } else {
536 WMArrayIterator iter;
538 WM_ITERATE_ARRAY(array, tmpw, iter) {
539 x = tmpw->frame_x + dx;
540 y = tmpw->frame_y + dy;
542 /* don't let windows become unreachable */
543 #if 1 /* XXX: was 0 in XINERAMA patch, check */
544 if (x + (int)tmpw->frame->core->width < 20)
545 x = 20 - (int)tmpw->frame->core->width;
546 else if (x + 20 > scr_width)
547 x = scr_width - 20;
549 if (y + (int)tmpw->frame->core->height < 20)
550 y = 20 - (int)tmpw->frame->core->height;
551 else if (y + 20 > scr_height)
552 y = scr_height - 20;
554 #else
555 wScreenBringInside(wwin->screen_ptr, &x, &y,
556 (int)tmpw->frame->core->width,
557 (int)tmpw->frame->core->height);
558 #endif
560 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
561 tmpw->frame->core->height);
568 static void
569 flushMotion()
571 XEvent ev;
573 XSync(dpy, False);
574 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
578 static void
579 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
580 int new_workspace, int rewind)
582 /* do not let window be unmapped */
583 if (opaque_move) {
584 wwin->flags.changing_workspace = 1;
585 wWindowChangeWorkspace(wwin, new_workspace);
587 /* go to new workspace */
588 wWorkspaceChange(scr, new_workspace);
590 wwin->flags.changing_workspace = 0;
592 if (rewind)
593 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
594 else
595 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
597 flushMotion();
599 if (!opaque_move) {
600 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
601 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
602 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
609 typedef struct {
610 /* arrays of WWindows sorted by the respective border position */
611 WWindow **topList; /* top border */
612 WWindow **leftList; /* left border */
613 WWindow **rightList; /* right border */
614 WWindow **bottomList; /* bottom border */
615 int count;
617 /* index of window in the above lists indicating the relative position
618 * of the window with the others */
619 int topIndex;
620 int leftIndex;
621 int rightIndex;
622 int bottomIndex;
624 int rubCount; /* for workspace switching */
626 int winWidth, winHeight; /* width/height of the window */
627 int realX, realY; /* actual position of the window */
628 int calcX, calcY; /* calculated position of window */
629 int omouseX, omouseY; /* old mouse position */
630 int mouseX, mouseY; /* last known position of the pointer */
631 } MoveData;
633 #define WTOP(w) (w)->frame_y
634 #define WLEFT(w) (w)->frame_x
635 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
636 (HAS_BORDER_WITH_SELECT(w) ? 2*FRAME_BORDER_WIDTH : 0))
637 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
638 (HAS_BORDER_WITH_SELECT(w) ? 2*FRAME_BORDER_WIDTH : 0))
640 static int
641 compareWTop(const void *a, const void *b)
643 WWindow *wwin1 = *(WWindow**)a;
644 WWindow *wwin2 = *(WWindow**)b;
646 if (WTOP(wwin1) > WTOP(wwin2))
647 return -1;
648 else if (WTOP(wwin1) < WTOP(wwin2))
649 return 1;
650 else
651 return 0;
655 static int
656 compareWLeft(const void *a, const void *b)
658 WWindow *wwin1 = *(WWindow**)a;
659 WWindow *wwin2 = *(WWindow**)b;
661 if (WLEFT(wwin1) > WLEFT(wwin2))
662 return -1;
663 else if (WLEFT(wwin1) < WLEFT(wwin2))
664 return 1;
665 else
666 return 0;
670 static int
671 compareWRight(const void *a, const void *b)
673 WWindow *wwin1 = *(WWindow**)a;
674 WWindow *wwin2 = *(WWindow**)b;
676 if (WRIGHT(wwin1) < WRIGHT(wwin2))
677 return -1;
678 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
679 return 1;
680 else
681 return 0;
686 static int
687 compareWBottom(const void *a, const void *b)
689 WWindow *wwin1 = *(WWindow**)a;
690 WWindow *wwin2 = *(WWindow**)b;
692 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
693 return -1;
694 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
695 return 1;
696 else
697 return 0;
701 static void
702 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
704 int i;
705 int newX2 = newX + data->winWidth;
706 int newY2 = newY + data->winHeight;
707 Bool ok = False;
709 if (newX < data->realX) {
710 if (data->rightIndex > 0
711 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
712 ok = True;
713 } else if (data->leftIndex <= data->count-1
714 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
715 ok = True;
717 } else if (newX > data->realX) {
718 if (data->leftIndex > 0
719 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
720 ok = True;
721 } else if (data->rightIndex <= data->count-1
722 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
723 ok = True;
727 if (!ok) {
728 if (newY < data->realY) {
729 if (data->bottomIndex > 0
730 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
731 ok = True;
732 } else if (data->topIndex <= data->count-1
733 && newY2 <= WTOP(data->topList[data->topIndex])) {
734 ok = True;
736 } else if (newY > data->realY) {
737 if (data->topIndex > 0
738 && newY2 > WTOP(data->topList[data->topIndex-1])) {
739 ok = True;
740 } else if (data->bottomIndex <= data->count-1
741 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
742 ok = True;
747 if (!ok)
748 return;
750 /* TODO: optimize this */
751 if (data->realY < WBOTTOM(data->bottomList[0])) {
752 data->bottomIndex = 0;
754 if (data->realX < WRIGHT(data->rightList[0])) {
755 data->rightIndex = 0;
757 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
758 data->leftIndex = 0;
760 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
761 data->topIndex = 0;
763 for (i = 0; i < data->count; i++) {
764 if (data->realY > WBOTTOM(data->bottomList[i])) {
765 data->bottomIndex = i + 1;
767 if (data->realX > WRIGHT(data->rightList[i])) {
768 data->rightIndex = i + 1;
770 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
771 data->leftIndex = i + 1;
773 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
774 data->topIndex = i + 1;
780 static void
781 freeMoveData(MoveData *data)
783 if (data->topList)
784 wfree(data->topList);
785 if (data->leftList)
786 wfree(data->leftList);
787 if (data->rightList)
788 wfree(data->rightList);
789 if (data->bottomList)
790 wfree(data->bottomList);
794 static void
795 updateMoveData(WWindow *wwin, MoveData *data)
797 WScreen *scr = wwin->screen_ptr;
798 WWindow *tmp;
799 int i;
801 data->count = 0;
802 tmp = scr->focused_window;
803 while (tmp) {
804 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
805 && !tmp->flags.miniaturized
806 && !tmp->flags.hidden
807 && !tmp->flags.obscured
808 && !WFLAGP(tmp, sunken)) {
809 data->topList[data->count] = tmp;
810 data->leftList[data->count] = tmp;
811 data->rightList[data->count] = tmp;
812 data->bottomList[data->count] = tmp;
813 data->count++;
815 tmp = tmp->prev;
818 if (data->count == 0) {
819 data->topIndex = 0;
820 data->leftIndex = 0;
821 data->rightIndex = 0;
822 data->bottomIndex = 0;
823 return;
826 /* order from closest to the border of the screen to farthest */
828 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
829 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
830 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
831 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
833 /* figure the position of the window relative to the others */
835 data->topIndex = -1;
836 data->leftIndex = -1;
837 data->rightIndex = -1;
838 data->bottomIndex = -1;
840 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
841 data->bottomIndex = 0;
843 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
844 data->rightIndex = 0;
846 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
847 data->leftIndex = 0;
849 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
850 data->topIndex = 0;
852 for (i = 0; i < data->count; i++) {
853 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
854 data->bottomIndex = i + 1;
856 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
857 data->rightIndex = i + 1;
859 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
860 data->leftIndex = i + 1;
862 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
863 data->topIndex = i + 1;
869 static void
870 initMoveData(WWindow *wwin, MoveData *data)
872 int i;
873 WWindow *tmp;
875 memset(data, 0, sizeof(MoveData));
877 for (i = 0, tmp = wwin->screen_ptr->focused_window;
878 tmp != NULL;
879 tmp = tmp->prev, i++);
881 if (i > 1) {
882 data->topList = wmalloc(sizeof(WWindow*) * i);
883 data->leftList = wmalloc(sizeof(WWindow*) * i);
884 data->rightList = wmalloc(sizeof(WWindow*) * i);
885 data->bottomList = wmalloc(sizeof(WWindow*) * i);
887 updateMoveData(wwin, data);
890 data->realX = wwin->frame_x;
891 data->realY = wwin->frame_y;
892 data->calcX = wwin->frame_x;
893 data->calcY = wwin->frame_y;
895 data->winWidth = wwin->frame->core->width +
896 (HAS_BORDER_WITH_SELECT(wwin) ? 2*FRAME_BORDER_WIDTH : 0);
897 data->winHeight = wwin->frame->core->height +
898 (HAS_BORDER_WITH_SELECT(wwin) ? 2*FRAME_BORDER_WIDTH : 0);
902 static Bool
903 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
905 WScreen *scr = wwin->screen_ptr;
906 Bool changed = False;
908 if (data->mouseX <= 1) {
909 if (scr->current_workspace > 0) {
911 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
912 True);
913 changed = True;
914 data->rubCount = 0;
916 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
918 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
919 True);
920 changed = True;
921 data->rubCount = 0;
923 } else if (data->mouseX >= scr->scr_width - 2) {
925 if (scr->current_workspace == scr->workspace_count - 1) {
927 if (wPreferences.ws_cycle
928 || scr->workspace_count == MAX_WORKSPACES) {
930 crossWorkspace(scr, wwin, opaqueMove, 0, False);
931 changed = True;
932 data->rubCount = 0;
934 /* if user insists on trying to go to next workspace even when
935 * it's already the last, create a new one */
936 else if (data->omouseX == data->mouseX
937 && wPreferences.ws_advance) {
939 /* detect user "rubbing" the window against the edge */
940 if (data->rubCount > 0
941 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
943 data->rubCount = -(data->rubCount + 1);
945 } else if (data->rubCount <= 0
946 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
948 data->rubCount = -data->rubCount + 1;
951 /* create a new workspace */
952 if (abs(data->rubCount) > 2) {
953 /* go to next workspace */
954 wWorkspaceNew(scr);
956 crossWorkspace(scr, wwin, opaqueMove,
957 scr->current_workspace+1, False);
958 changed = True;
959 data->rubCount = 0;
961 } else if (scr->current_workspace < scr->workspace_count) {
963 /* go to next workspace */
964 crossWorkspace(scr, wwin, opaqueMove,
965 scr->current_workspace+1, False);
966 changed = True;
967 data->rubCount = 0;
969 } else {
970 data->rubCount = 0;
973 return changed;
977 static void
978 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
979 Bool opaqueMove, int newMouseX, int newMouseY)
981 WScreen *scr = wwin->screen_ptr;
982 int dx, dy; /* how much mouse moved */
983 int winL, winR, winT, winB; /* requested new window position */
984 int newX, newY; /* actual new window position */
985 Bool hresist, vresist;
986 Bool updateIndex;
987 Bool attract;
989 hresist = False;
990 vresist = False;
992 updateIndex = False;
994 /* check the direction of the movement */
995 dx = newMouseX - data->mouseX;
996 dy = newMouseY - data->mouseY;
998 data->omouseX = data->mouseX;
999 data->omouseY = data->mouseY;
1000 data->mouseX = newMouseX;
1001 data->mouseY = newMouseY;
1003 winL = data->calcX + dx;
1004 winR = data->calcX + data->winWidth + dx;
1005 winT = data->calcY + dy;
1006 winB = data->calcY + data->winHeight + dy;
1008 newX = data->realX;
1009 newY = data->realY;
1011 if (doResistance) {
1012 int l_edge, r_edge;
1013 int edge_l, edge_r;
1014 int t_edge, b_edge;
1015 int edge_t, edge_b;
1016 int resist;
1018 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1019 attract = wPreferences.attract;
1020 /* horizontal movement: check horizontal edge resistances */
1021 if (dx || dy) {
1022 WMRect rect;
1023 int i, head;
1024 /* window is the leftmost window: check against screen edge */
1026 /* Add inter head resistance 1/2 (if needed) */
1027 head = wGetHeadForPointerLocation(scr);
1028 rect = wGetRectForHead(scr, head);
1030 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
1031 edge_l = l_edge - resist;
1032 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
1033 r_edge = edge_r + resist;
1035 /* 1 */
1036 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
1037 WWindow *looprw;
1039 for (i = data->rightIndex - 1; i >= 0; i--) {
1040 looprw = data->rightList[i];
1041 if (!(data->realY > WBOTTOM(looprw)
1042 || (data->realY + data->winHeight) < WTOP(looprw))) {
1043 if (attract
1044 || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
1045 l_edge = WRIGHT(looprw) + 1;
1046 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1048 break;
1052 if (attract) {
1053 for (i = data->rightIndex; i < data->count; i++) {
1054 looprw = data->rightList[i];
1055 if(!(data->realY > WBOTTOM(looprw)
1056 || (data->realY + data->winHeight) < WTOP(looprw))) {
1057 r_edge = WRIGHT(looprw) + 1;
1058 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1059 break;
1065 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
1066 WWindow *looprw;
1068 for (i = data->leftIndex - 1; i >= 0; i--) {
1069 looprw = data->leftList[i];
1070 if (!(data->realY > WBOTTOM(looprw)
1071 || (data->realY + data->winHeight) < WTOP(looprw))) {
1072 if (attract
1073 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1)) && dx > 0)) {
1074 edge_r = WLEFT(looprw);
1075 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1077 break;
1081 if (attract)
1082 for (i = data->leftIndex; i < data->count; i++) {
1083 looprw = data->leftList[i];
1084 if(!(data->realY > WBOTTOM(looprw)
1085 || (data->realY + data->winHeight) < WTOP(looprw))) {
1086 edge_l = WLEFT(looprw);
1087 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1088 break;
1094 printf("%d %d\n",winL,winR);
1095 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1098 if ((winL - l_edge) < (r_edge - winL)) {
1099 if (resist > 0) {
1100 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1101 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1102 newX = l_edge;
1103 hresist = True;
1106 } else {
1107 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1108 newX = r_edge;
1109 hresist = True;
1113 if ((winR - edge_l) < (edge_r - winR)) {
1114 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1115 newX = edge_l - data->winWidth;
1116 hresist = True;
1118 } else {
1119 if (resist > 0) {
1120 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1121 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1122 newX = edge_r - data->winWidth;
1123 hresist = True;
1128 /* VeRT */
1129 /* Add inter head resistance 2/2 (if needed) */
1130 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1131 edge_t = t_edge - resist;
1132 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1133 b_edge = edge_b + resist;
1135 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1136 WWindow *looprw;
1138 for (i = data->bottomIndex - 1; i >= 0; i--) {
1139 looprw = data->bottomList[i];
1140 if (!(data->realX > WRIGHT(looprw)
1141 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1142 if (attract
1143 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1144 t_edge = WBOTTOM(looprw) + 1;
1145 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1147 break;
1151 if (attract) {
1152 for (i = data->bottomIndex; i < data->count; i++) {
1153 looprw = data->bottomList[i];
1154 if(!(data->realX > WRIGHT(looprw)
1155 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1156 b_edge = WBOTTOM(looprw) + 1;
1157 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1158 break;
1164 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1165 WWindow *looprw;
1167 for (i = data->topIndex - 1; i >= 0; i--) {
1168 looprw = data->topList[i];
1169 if (!(data->realX > WRIGHT(looprw)
1170 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1171 if (attract
1172 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1173 edge_b = WTOP(looprw);
1174 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1176 break;
1180 if (attract)
1181 for (i = data->topIndex; i < data->count; i++) {
1182 looprw = data->topList[i];
1183 if(!(data->realX > WRIGHT(looprw)
1184 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1185 edge_t = WTOP(looprw);
1186 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1187 break;
1192 if ((winT - t_edge) < (b_edge - winT)) {
1193 if (resist > 0) {
1194 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1195 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1196 newY = t_edge;
1197 vresist = True;
1201 else {
1202 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1203 newY = b_edge;
1204 vresist = True;
1208 if ((winB - edge_t) < (edge_b - winB)) {
1209 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1210 newY = edge_t - data->winHeight;
1211 vresist = True;
1214 else {
1215 if (resist > 0) {
1216 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1217 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1218 newY = edge_b - data->winHeight;
1219 vresist = True;
1224 /* END VeRT */
1228 /* update window position */
1229 data->calcX += dx;
1230 data->calcY += dy;
1232 if (((dx > 0 && data->calcX - data->realX > 0)
1233 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1234 newX = data->calcX;
1236 if (((dy > 0 && data->calcY - data->realY > 0)
1237 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1238 newY = data->calcY;
1240 if (data->realX != newX || data->realY != newY) {
1242 if (wPreferences.move_display == WDIS_NEW
1243 && !scr->selected_windows) {
1244 showPosition(wwin, data->realX, data->realY);
1246 if (opaqueMove) {
1247 doWindowMove(wwin, scr->selected_windows,
1248 newX - wwin->frame_x,
1249 newY - wwin->frame_y);
1250 } else {
1251 /* erase frames */
1252 drawFrames(wwin, scr->selected_windows,
1253 data->realX - wwin->frame_x,
1254 data->realY - wwin->frame_y);
1257 if (!scr->selected_windows
1258 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1260 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1261 newY + data->winHeight/2);
1264 if (!opaqueMove) {
1265 /* draw frames */
1266 drawFrames(wwin, scr->selected_windows,
1267 newX - wwin->frame_x,
1268 newY - wwin->frame_y);
1271 if (!scr->selected_windows) {
1272 showPosition(wwin, newX, newY);
1277 /* recalc relative window position */
1278 if (doResistance && (data->realX != newX || data->realY != newY)) {
1279 updateResistance(wwin, data, newX, newY);
1282 data->realX = newX;
1283 data->realY = newY;
1287 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1289 #define MOVABLE_BIT 0x01
1290 #define RESIZABLE_BIT 0x02
1293 wKeyboardMoveResizeWindow(WWindow *wwin)
1295 WScreen *scr = wwin->screen_ptr;
1296 Window root = scr->root_win;
1297 XEvent event;
1298 int w = wwin->frame->core->width;
1299 int h = wwin->frame->core->height;
1300 int scr_width = wwin->screen_ptr->scr_width;
1301 int scr_height = wwin->screen_ptr->scr_height;
1302 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1303 int src_x = wwin->frame_x;
1304 int src_y = wwin->frame_y;
1305 int done,off_x,off_y,ww,wh;
1306 int kspeed = _KS;
1307 Time lastTime = 0;
1308 KeyCode shiftl, shiftr, ctrll, ctrlmode;
1309 KeySym keysym=NoSymbol;
1310 int moment=0;
1311 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) |
1312 (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1313 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr)>1)
1314 ? wGetHeadForWindow(wwin)
1315 : scr->xine_info.primary_head);
1317 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1318 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1319 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1320 ctrlmode=done=off_x=off_y=0;
1322 if (modes == RESIZABLE_BIT) {
1323 ctrlmode = 1;
1326 XSync(dpy, False);
1327 wusleep(10000);
1328 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1330 if (!wwin->flags.selected) {
1331 wUnselectWindows(scr);
1333 XGrabServer(dpy);
1334 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1335 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1336 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1338 if (wwin->flags.shaded || scr->selected_windows) {
1339 if(scr->selected_windows)
1340 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1341 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1342 if(!scr->selected_windows)
1343 mapPositionDisplay(wwin, src_x, src_y, w, h);
1344 } else {
1345 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1347 ww=w;
1348 wh=h;
1349 while(1) {
1351 looper.ox=off_x;
1352 looper.oy=off_y;
1354 do {
1355 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1356 | ButtonPressMask | ExposureMask, &event);
1357 if (event.type == Expose) {
1358 WMHandleEvent(&event);
1360 } while (event.type == Expose);
1363 if (wwin->flags.shaded || scr->selected_windows) {
1364 if(scr->selected_windows)
1365 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1366 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1367 /*** I HATE EDGE RESISTANCE - ]d ***/
1369 else {
1370 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1373 if(ctrlmode)
1374 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1376 XUngrabServer(dpy);
1377 XSync(dpy, False);
1379 switch (event.type) {
1380 case KeyPress:
1381 /* accelerate */
1382 if (event.xkey.time - lastTime > 50) {
1383 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1384 } else {
1385 if (kspeed < 20) {
1386 kspeed++;
1389 if (kspeed < _KS) kspeed = _KS;
1390 lastTime = event.xkey.time;
1391 if (modes == (MOVABLE_BIT|RESIZABLE_BIT)) {
1392 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1393 ctrlmode=1;
1394 wUnselectWindows(scr);
1396 else {
1397 ctrlmode=0;
1400 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1401 if (ctrlmode)
1402 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1403 else
1404 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1406 else {
1408 keysym = XLookupKeysym(&event.xkey, 0);
1409 switch (keysym) {
1410 case XK_Return:
1411 done=2;
1412 break;
1413 case XK_Escape:
1414 done=1;
1415 break;
1416 case XK_Up:
1417 #ifdef XK_KP_Up
1418 case XK_KP_Up:
1419 #endif
1420 case XK_k:
1421 if (ctrlmode){
1422 if (moment != UP)
1423 h = wh;
1424 h-=kspeed;
1425 moment = UP;
1426 if (h < 1) h = 1;
1428 else off_y-=kspeed;
1429 break;
1430 case XK_Down:
1431 #ifdef XK_KP_Down
1432 case XK_KP_Down:
1433 #endif
1434 case XK_j:
1435 if (ctrlmode){
1436 if (moment != DOWN)
1437 h = wh;
1438 h+=kspeed;
1439 moment = DOWN;
1441 else off_y+=kspeed;
1442 break;
1443 case XK_Left:
1444 #ifdef XK_KP_Left
1445 case XK_KP_Left:
1446 #endif
1447 case XK_h:
1448 if (ctrlmode) {
1449 if (moment != LEFT)
1450 w = ww;
1451 w-=kspeed;
1452 if (w < 1) w = 1;
1453 moment = LEFT;
1455 else off_x-=kspeed;
1456 break;
1457 case XK_Right:
1458 #ifdef XK_KP_Right
1459 case XK_KP_Right:
1460 #endif
1461 case XK_l:
1462 if (ctrlmode) {
1463 if (moment != RIGHT)
1464 w = ww;
1465 w+=kspeed;
1466 moment = RIGHT;
1468 else off_x+=kspeed;
1469 break;
1472 ww=w;wh=h;
1473 wh-=vert_border;
1474 wWindowConstrainSize(wwin, &ww, &wh);
1475 wh+=vert_border;
1477 if (wPreferences.ws_cycle){
1478 if (src_x + off_x + ww < 20){
1479 if(!scr->current_workspace) {
1480 wWorkspaceChange(scr, scr->workspace_count-1);
1482 else wWorkspaceChange(scr, scr->current_workspace-1);
1483 off_x += scr_width;
1485 else if (src_x + off_x + 20 > scr_width){
1486 if(scr->current_workspace == scr->workspace_count-1) {
1487 wWorkspaceChange(scr, 0);
1489 else wWorkspaceChange(scr, scr->current_workspace+1);
1490 off_x -= scr_width;
1493 else {
1494 if (src_x + off_x + ww < 20)
1495 off_x = 20 - ww - src_x;
1496 else if (src_x + off_x + 20 > scr_width)
1497 off_x = scr_width - 20 - src_x;
1500 if (src_y + off_y + wh < 20) {
1501 off_y = 20 - wh - src_y;
1503 else if (src_y + off_y + 20 > scr_height) {
1504 off_y = scr_height - 20 - src_y;
1507 break;
1508 case ButtonPress:
1509 case ButtonRelease:
1510 done=1;
1511 break;
1512 case Expose:
1513 WMHandleEvent(&event);
1514 while (XCheckTypedEvent(dpy, Expose, &event)) {
1515 WMHandleEvent(&event);
1517 break;
1519 default:
1520 WMHandleEvent(&event);
1521 break;
1524 XGrabServer(dpy);
1525 /*xxx*/
1527 if (wwin->flags.shaded && !scr->selected_windows){
1528 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1529 } else {
1530 if (ctrlmode) {
1531 WMUnmapWidget(scr->gview);
1532 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1533 } else if(!scr->selected_windows) {
1534 WMUnmapWidget(scr->gview);
1535 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1539 if (wwin->flags.shaded || scr->selected_windows) {
1540 if (scr->selected_windows)
1541 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1542 else
1543 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1544 } else {
1545 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1549 if (ctrlmode) {
1550 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1551 } else if(!scr->selected_windows)
1552 showPosition(wwin, src_x+off_x, src_y+off_y);
1555 if (done) {
1556 scr->keymove_tick=0;
1558 WMDeleteTimerWithClientData(&looper);
1560 if (wwin->flags.shaded || scr->selected_windows) {
1561 if(scr->selected_windows)
1562 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1563 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1565 else {
1566 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1569 if (ctrlmode) {
1570 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1571 WMUnmapWidget(scr->gview);
1572 } else
1573 WMUnmapWidget(scr->gview);
1575 XUngrabKeyboard(dpy, CurrentTime);
1576 XUngrabPointer(dpy, CurrentTime);
1577 XUngrabServer(dpy);
1579 if(done==2) {
1580 if (wwin->flags.shaded || scr->selected_windows) {
1581 if (!scr->selected_windows) {
1582 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1583 wWindowSynthConfigureNotify(wwin);
1584 } else {
1585 WMArrayIterator iter;
1586 WWindow *foo;
1588 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1590 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1591 wWindowSynthConfigureNotify(foo);
1594 } else {
1595 if (wwin->client.width != ww)
1596 wwin->flags.user_changed_width = 1;
1598 if (wwin->client.height != wh - vert_border)
1599 wwin->flags.user_changed_height = 1;
1601 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1602 ww, wh - vert_border);
1603 wWindowSynthConfigureNotify(wwin);
1605 wWindowChangeWorkspace(wwin, scr->current_workspace);
1606 wSetFocusTo(scr, wwin);
1609 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr)>1 &&
1610 head != wGetHeadForWindow(wwin)) {
1611 wArrangeIcons(scr, True);
1615 #if defined(NETWM_HINTS) && defined(VIRTUAL_DESKTOP)
1616 wWorkspaceResizeViewport(scr, scr->current_workspace);
1617 #endif
1619 return 1;
1626 *----------------------------------------------------------------------
1627 * wMouseMoveWindow--
1628 * Move the named window and the other selected ones (if any),
1629 * interactively. Also shows the position of the window, if only one
1630 * window is being moved.
1631 * If the window is not on the selected window list, the selected
1632 * windows are deselected.
1633 * If shift is pressed during the operation, the position display
1634 * is changed to another type.
1636 * Returns:
1637 * True if the window was moved, False otherwise.
1639 * Side effects:
1640 * The window(s) position is changed, and the client(s) are
1641 * notified about that.
1642 * The position display configuration may be changed.
1643 *----------------------------------------------------------------------
1646 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1648 WScreen *scr = wwin->screen_ptr;
1649 XEvent event;
1650 Window root = scr->root_win;
1651 KeyCode shiftl, shiftr;
1652 Bool done = False;
1653 int started = 0;
1654 int warped = 0;
1655 /* This needs not to change while moving, else bad things can happen */
1656 int opaqueMove = wPreferences.opaque_move;
1657 MoveData moveData;
1658 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1659 ? wGetHeadForWindow(wwin)
1660 : scr->xine_info.primary_head);
1661 #ifdef GHOST_WINDOW_MOVE
1662 RImage *rimg = InitGhostWindowMove(scr);
1663 #endif
1665 if (!IS_MOVABLE(wwin))
1666 return False;
1668 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1669 XSetWindowAttributes attr;
1671 attr.save_under = True;
1672 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1673 CWSaveUnder, &attr);
1677 initMoveData(wwin, &moveData);
1679 moveData.mouseX = ev->xmotion.x_root;
1680 moveData.mouseY = ev->xmotion.y_root;
1682 if (!wwin->flags.selected) {
1683 /* this window is not selected, unselect others and move only wwin */
1684 wUnselectWindows(scr);
1686 #ifdef DEBUG
1687 puts("Moving window");
1688 #endif
1689 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1690 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1691 while (!done) {
1692 if (warped) {
1693 int junk;
1694 Window junkw;
1696 /* XWarpPointer() doesn't seem to generate Motion events, so
1697 * we've got to simulate them */
1698 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1699 &event.xmotion.y_root, &junk, &junk,
1700 (unsigned *) &junk);
1701 } else {
1702 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1703 | PointerMotionHintMask
1704 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1705 &event);
1707 if (event.type == MotionNotify) {
1708 /* compress MotionNotify events */
1709 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1710 if (!checkMouseSamplingRate(&event))
1711 continue;
1714 switch (event.type) {
1715 case KeyPress:
1716 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1717 && started && !scr->selected_windows) {
1719 if (!opaqueMove) {
1720 drawFrames(wwin, scr->selected_windows,
1721 moveData.realX - wwin->frame_x,
1722 moveData.realY - wwin->frame_y);
1725 if (wPreferences.move_display == WDIS_NEW
1726 && !scr->selected_windows) {
1727 showPosition(wwin, moveData.realX, moveData.realY);
1728 XUngrabServer(dpy);
1730 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1731 moveData.winWidth, moveData.winHeight);
1733 if (wPreferences.move_display == WDIS_NEW
1734 && !scr->selected_windows) {
1735 XGrabServer(dpy);
1736 showPosition(wwin, moveData.realX, moveData.realY);
1739 if (!opaqueMove) {
1740 drawFrames(wwin, scr->selected_windows,
1741 moveData.realX - wwin->frame_x,
1742 moveData.realY - wwin->frame_y);
1744 /*} else {
1745 WMHandleEvent(&event); this causes problems needs fixing */
1747 break;
1749 case MotionNotify:
1750 if (started) {
1751 updateWindowPosition(wwin, &moveData,
1752 scr->selected_windows == NULL
1753 && wPreferences.edge_resistance > 0,
1754 opaqueMove,
1755 event.xmotion.x_root,
1756 event.xmotion.y_root);
1758 if (!warped && !wPreferences.no_autowrap) {
1759 int oldWorkspace = scr->current_workspace;
1761 if (wPreferences.move_display == WDIS_NEW
1762 && !scr->selected_windows) {
1763 showPosition(wwin, moveData.realX, moveData.realY);
1764 XUngrabServer(dpy);
1766 if (!opaqueMove) {
1767 drawFrames(wwin, scr->selected_windows,
1768 moveData.realX - wwin->frame_x,
1769 moveData.realY - wwin->frame_y);
1771 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1772 if (scr->current_workspace != oldWorkspace
1773 && wPreferences.edge_resistance > 0
1774 && scr->selected_windows == NULL)
1775 updateMoveData(wwin, &moveData);
1776 warped = 1;
1778 if (!opaqueMove) {
1779 drawFrames(wwin, scr->selected_windows,
1780 moveData.realX - wwin->frame_x,
1781 moveData.realY - wwin->frame_y);
1783 if (wPreferences.move_display == WDIS_NEW
1784 && !scr->selected_windows) {
1785 XSync(dpy, False);
1786 showPosition(wwin, moveData.realX, moveData.realY);
1787 XGrabServer(dpy);
1789 } else {
1790 warped = 0;
1792 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1793 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1795 XChangeActivePointerGrab(dpy, ButtonMotionMask
1796 | ButtonReleaseMask | ButtonPressMask,
1797 wCursor[WCUR_MOVE], CurrentTime);
1798 started = 1;
1799 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1800 CurrentTime);
1802 if (!scr->selected_windows)
1803 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1804 moveData.winWidth, moveData.winHeight);
1806 if (started && !opaqueMove)
1807 drawFrames(wwin, scr->selected_windows, 0, 0);
1809 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1810 && !scr->selected_windows)) {
1811 XGrabServer(dpy);
1812 if (wPreferences.move_display==WDIS_NEW)
1813 showPosition(wwin, moveData.realX, moveData.realY);
1816 break;
1818 case ButtonPress:
1819 break;
1821 case ButtonRelease:
1822 if (event.xbutton.button != ev->xbutton.button)
1823 break;
1825 if (started) {
1826 XEvent e;
1827 if (!opaqueMove) {
1828 drawFrames(wwin, scr->selected_windows,
1829 moveData.realX - wwin->frame_x,
1830 moveData.realY - wwin->frame_y);
1831 XSync(dpy, 0);
1832 doWindowMove(wwin, scr->selected_windows,
1833 moveData.realX - wwin->frame_x,
1834 moveData.realY - wwin->frame_y);
1836 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1837 wWindowSynthConfigureNotify(wwin);
1838 #endif
1839 XUngrabKeyboard(dpy, CurrentTime);
1840 XUngrabServer(dpy);
1841 if (!opaqueMove) {
1842 wWindowChangeWorkspace(wwin, scr->current_workspace);
1843 wSetFocusTo(scr, wwin);
1845 if (wPreferences.move_display == WDIS_NEW)
1846 showPosition(wwin, moveData.realX, moveData.realY);
1848 /* discard all enter/leave events that happened until
1849 * the time the button was released */
1850 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1851 if (e.xcrossing.time > event.xbutton.time) {
1852 XPutBackEvent(dpy, &e);
1853 break;
1856 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1857 if (e.xcrossing.time > event.xbutton.time) {
1858 XPutBackEvent(dpy, &e);
1859 break;
1863 if (!scr->selected_windows) {
1864 /* get rid of the geometry window */
1865 WMUnmapWidget(scr->gview);
1868 #ifdef DEBUG
1869 puts("End move window");
1870 #endif
1871 done = True;
1872 break;
1874 default:
1875 if (started && !opaqueMove) {
1876 drawFrames(wwin, scr->selected_windows,
1877 moveData.realX - wwin->frame_x,
1878 moveData.realY - wwin->frame_y);
1879 XUngrabServer(dpy);
1880 WMHandleEvent(&event);
1881 XSync(dpy, False);
1882 XGrabServer(dpy);
1883 drawFrames(wwin, scr->selected_windows,
1884 moveData.realX - wwin->frame_x,
1885 moveData.realY - wwin->frame_y);
1886 } else {
1887 WMHandleEvent(&event);
1889 break;
1893 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1894 XSetWindowAttributes attr;
1896 attr.save_under = False;
1897 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1898 CWSaveUnder, &attr);
1902 freeMoveData(&moveData);
1904 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr)>1 &&
1905 head != wGetHeadForWindow(wwin)) {
1906 wArrangeIcons(scr, True);
1909 #if defined(NETWM_HINTS) && defined(VIRTUAL_DESKTOP)
1910 if (started)
1911 wWorkspaceResizeViewport(scr, scr->current_workspace);
1912 #endif
1914 return started;
1918 #define RESIZEBAR 1
1919 #define HCONSTRAIN 2
1921 static int
1922 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1923 int flags)
1925 int w = wwin->frame->core->width - 1;
1926 int cw = wwin->frame->resizebar_corner_width;
1927 int dir;
1929 /* if not resizing through the resizebar */
1930 if (!(flags & RESIZEBAR)) {
1931 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1932 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1933 if (abs(dx) < 2 || abs(dy) < 2) {
1934 if (abs(dy) > abs(dx))
1935 xdir = 0;
1936 else
1937 ydir = 0;
1939 return (xdir | ydir);
1942 /* window is too narrow. allow diagonal resize */
1943 if (cw * 2 >= w) {
1944 int ydir;
1946 if (flags & HCONSTRAIN)
1947 ydir = 0;
1948 else
1949 ydir = DOWN;
1950 if (x < cw)
1951 return (LEFT | ydir);
1952 else
1953 return (RIGHT | ydir);
1955 /* vertical resize */
1956 if ((x > cw) && (x < w - cw))
1957 return DOWN;
1959 if (x < cw)
1960 dir = LEFT;
1961 else
1962 dir = RIGHT;
1964 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1965 dir |= DOWN;
1967 return dir;
1971 void
1972 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1974 XEvent event;
1975 WScreen *scr = wwin->screen_ptr;
1976 Window root = scr->root_win;
1977 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1978 int fw = wwin->frame->core->width;
1979 int fh = wwin->frame->core->height;
1980 int fx = wwin->frame_x;
1981 int fy = wwin->frame_y;
1982 int is_resizebar = (wwin->frame->resizebar
1983 && ev->xany.window==wwin->frame->resizebar->window);
1984 int orig_x, orig_y;
1985 int started;
1986 int dw, dh;
1987 int rw = fw, rh = fh;
1988 int rx1, ry1, rx2, ry2;
1989 int res = 0;
1990 KeyCode shiftl, shiftr;
1991 int h = 0;
1992 int orig_fx = fx;
1993 int orig_fy = fy;
1994 int orig_fw = fw;
1995 int orig_fh = fh;
1996 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr)>1)
1997 ? wGetHeadForWindow(wwin)
1998 : scr->xine_info.primary_head);
2000 if (!IS_RESIZABLE(wwin))
2001 return;
2003 if (wwin->flags.shaded) {
2004 wwarning("internal error: tryein");
2005 return;
2007 orig_x = ev->xbutton.x_root;
2008 orig_y = ev->xbutton.y_root;
2010 started = 0;
2011 #ifdef DEBUG
2012 puts("Resizing window");
2013 #endif
2015 wUnselectWindows(scr);
2016 rx1 = fx;
2017 rx2 = fx + fw - 1;
2018 ry1 = fy;
2019 ry2 = fy + fh - 1;
2020 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2021 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2022 if (HAS_TITLEBAR(wwin))
2023 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2024 else
2025 h = 0;
2026 while (1) {
2027 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
2028 | ButtonReleaseMask | PointerMotionHintMask
2029 | ButtonPressMask | ExposureMask, &event);
2030 if (!checkMouseSamplingRate(&event))
2031 continue;
2033 switch (event.type) {
2034 case KeyPress:
2035 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2036 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
2037 && started) {
2038 drawTransparentFrame(wwin, fx, fy, fw, fh);
2039 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
2040 drawTransparentFrame(wwin, fx, fy, fw, fh);
2042 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2043 break;
2045 case MotionNotify:
2046 if (started) {
2047 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
2049 dw = 0;
2050 dh = 0;
2052 orig_fx = fx;
2053 orig_fy = fy;
2054 orig_fw = fw;
2055 orig_fh = fh;
2057 if (res & LEFT)
2058 dw = orig_x - event.xmotion.x_root;
2059 else if (res & RIGHT)
2060 dw = event.xmotion.x_root - orig_x;
2061 if (res & UP)
2062 dh = orig_y - event.xmotion.y_root;
2063 else if (res & DOWN)
2064 dh = event.xmotion.y_root - orig_y;
2066 orig_x = event.xmotion.x_root;
2067 orig_y = event.xmotion.y_root;
2069 rw += dw;
2070 rh += dh;
2071 fw = rw;
2072 fh = rh - vert_border;
2073 wWindowConstrainSize(wwin, &fw, &fh);
2074 fh += vert_border;
2075 if (res & LEFT)
2076 fx = rx2 - fw + 1;
2077 else if (res & RIGHT)
2078 fx = rx1;
2079 if (res & UP)
2080 fy = ry2 - fh + 1;
2081 else if (res & DOWN)
2082 fy = ry1;
2083 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
2084 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
2085 int tx, ty;
2086 Window junkw;
2087 int flags;
2089 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
2090 orig_x, orig_y, &tx, &ty, &junkw);
2092 /* check if resizing through resizebar */
2093 if (is_resizebar)
2094 flags = RESIZEBAR;
2095 else
2096 flags = 0;
2098 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
2099 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
2100 flags |= HCONSTRAIN;
2102 res = getResizeDirection(wwin, tx, ty,
2103 orig_x - event.xmotion.x_root,
2104 orig_y - event.xmotion.y_root, flags);
2106 if (res == (UP|LEFT))
2107 XChangeActivePointerGrab(dpy, ButtonMotionMask
2108 | ButtonReleaseMask | ButtonPressMask,
2109 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
2110 else if (res == (UP|RIGHT))
2111 XChangeActivePointerGrab(dpy, ButtonMotionMask
2112 | ButtonReleaseMask | ButtonPressMask,
2113 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
2114 else if (res == (DOWN|LEFT))
2115 XChangeActivePointerGrab(dpy, ButtonMotionMask
2116 | ButtonReleaseMask | ButtonPressMask,
2117 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2118 else if (res == (DOWN|RIGHT))
2119 XChangeActivePointerGrab(dpy, ButtonMotionMask
2120 | ButtonReleaseMask | ButtonPressMask,
2121 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2122 else if (res == DOWN || res == UP)
2123 XChangeActivePointerGrab(dpy, ButtonMotionMask
2124 | ButtonReleaseMask | ButtonPressMask,
2125 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2126 else if (res & (DOWN|UP))
2127 XChangeActivePointerGrab(dpy, ButtonMotionMask
2128 | ButtonReleaseMask | ButtonPressMask,
2129 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2130 else if (res & (LEFT|RIGHT))
2131 XChangeActivePointerGrab(dpy, ButtonMotionMask
2132 | ButtonReleaseMask | ButtonPressMask,
2133 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2135 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
2136 CurrentTime);
2138 XGrabServer(dpy);
2140 /* Draw the resize frame for the first time. */
2141 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2143 drawTransparentFrame(wwin, fx, fy, fw, fh);
2145 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2147 started = 1;
2149 if (started) {
2150 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2151 drawTransparentFrame(wwin, orig_fx, orig_fy,
2152 orig_fw, orig_fh);
2153 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2154 drawTransparentFrame(wwin, fx, fy, fw, fh);
2155 } else {
2156 drawTransparentFrame(wwin, orig_fx, orig_fy,
2157 orig_fw, orig_fh);
2158 drawTransparentFrame(wwin, fx, fy, fw, fh);
2160 if (fh != orig_fh || fw != orig_fw) {
2161 if (wPreferences.size_display == WDIS_NEW) {
2162 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2163 orig_fy + orig_fh, res);
2165 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2168 break;
2170 case ButtonPress:
2171 break;
2173 case ButtonRelease:
2174 if (event.xbutton.button != ev->xbutton.button)
2175 break;
2177 if (started) {
2178 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2180 drawTransparentFrame(wwin, fx, fy, fw, fh);
2182 XUngrabKeyboard(dpy, CurrentTime);
2183 WMUnmapWidget(scr->gview);
2184 XUngrabServer(dpy);
2186 if (wwin->client.width != fw)
2187 wwin->flags.user_changed_width = 1;
2189 if (wwin->client.height != fh - vert_border)
2190 wwin->flags.user_changed_height = 1;
2192 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2194 #ifdef DEBUG
2195 puts("End resize window");
2196 #endif
2197 return;
2199 default:
2200 WMHandleEvent(&event);
2204 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
2205 head != wGetHeadForWindow(wwin)) {
2206 wArrangeIcons(scr, True);
2209 #if defined(NETWM_HINTS) && defined(VIRTUAL_DESKTOP)
2210 wWorkspaceResizeViewport(scr, scr->current_workspace);
2211 #endif
2214 #undef LEFT
2215 #undef RIGHT
2216 #undef HORIZONTAL
2217 #undef UP
2218 #undef DOWN
2219 #undef VERTICAL
2220 #undef HCONSTRAIN
2221 #undef RESIZEBAR
2223 void
2224 wUnselectWindows(WScreen *scr)
2226 WWindow *wwin;
2228 if (!scr->selected_windows)
2229 return;
2231 while (WMGetArrayItemCount(scr->selected_windows)) {
2232 wwin = WMGetFromArray(scr->selected_windows, 0);
2233 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2234 wIconSelect(wwin->icon);
2236 wSelectWindow(wwin, False);
2238 WMFreeArray(scr->selected_windows);
2239 scr->selected_windows = NULL;
2242 #ifndef LITE
2243 static void
2244 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2246 WWindow *tmpw;
2248 /* select the windows and put them in the selected window list */
2249 tmpw = scr->focused_window;
2250 while (tmpw != NULL) {
2251 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2252 if ((tmpw->frame->workspace == scr->current_workspace
2253 || IS_OMNIPRESENT(tmpw))
2254 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2255 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2256 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2257 wSelectWindow(tmpw, True);
2260 tmpw = tmpw->prev;
2265 void
2266 wSelectWindows(WScreen *scr, XEvent *ev)
2268 XEvent event;
2269 Window root = scr->root_win;
2270 GC gc = scr->frame_gc;
2271 int xp = ev->xbutton.x_root;
2272 int yp = ev->xbutton.y_root;
2273 int w = 0, h = 0;
2274 int x = xp, y = yp;
2276 #ifdef DEBUG
2277 puts("Selecting windows");
2278 #endif
2279 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2280 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2281 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2282 CurrentTime) != Success) {
2283 return;
2285 XGrabServer(dpy);
2287 wUnselectWindows(scr);
2289 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2290 while (1) {
2291 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2292 | ButtonPressMask, &event);
2294 switch (event.type) {
2295 case MotionNotify:
2296 XDrawRectangle(dpy, root, gc, x, y, w, h);
2297 x = event.xmotion.x_root;
2298 if (x < xp) {
2299 w = xp - x;
2300 } else {
2301 w = x - xp;
2302 x = xp;
2304 y = event.xmotion.y_root;
2305 if (y < yp) {
2306 h = yp - y;
2307 } else {
2308 h = y - yp;
2309 y = yp;
2311 XDrawRectangle(dpy, root, gc, x, y, w, h);
2312 break;
2314 case ButtonPress:
2315 break;
2317 case ButtonRelease:
2318 if (event.xbutton.button != ev->xbutton.button)
2319 break;
2321 XDrawRectangle(dpy, root, gc, x, y, w, h);
2322 XUngrabServer(dpy);
2323 XUngrabPointer(dpy, CurrentTime);
2324 selectWindowsInside(scr, x, y, x + w, y + h);
2326 #ifdef DEBUG
2327 puts("End window selection");
2328 #endif
2329 return;
2331 default:
2332 WMHandleEvent(&event);
2333 break;
2337 #endif /* !LITE */
2339 void
2340 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2341 unsigned width, unsigned height)
2343 WScreen *scr = wwin->screen_ptr;
2344 Window root = scr->root_win;
2345 int x, y, h = 0;
2346 XEvent event;
2347 KeyCode shiftl, shiftr;
2348 Window junkw;
2349 int junk;
2351 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2352 GrabModeAsync, GrabModeAsync, None,
2353 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2354 *x_ret = 0;
2355 *y_ret = 0;
2356 return;
2358 if (HAS_TITLEBAR(wwin)) {
2359 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2360 height += h;
2362 if (HAS_RESIZEBAR(wwin)) {
2363 height += RESIZEBAR_HEIGHT;
2365 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2366 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2367 (unsigned *) &junk);
2368 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2370 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2372 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2373 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2374 while (1) {
2375 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2376 &event);
2378 if (!checkMouseSamplingRate(&event))
2379 continue;
2381 switch (event.type) {
2382 case KeyPress:
2383 if ((event.xkey.keycode == shiftl)
2384 || (event.xkey.keycode == shiftr)) {
2385 drawTransparentFrame(wwin,
2386 x - width/2, y - h/2, width, height);
2387 cyclePositionDisplay(wwin,
2388 x - width/2, y - h/2, width, height);
2389 drawTransparentFrame(wwin,
2390 x - width/2, y - h/2, width, height);
2392 break;
2394 case MotionNotify:
2395 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2397 x = event.xmotion.x_root;
2398 y = event.xmotion.y_root;
2400 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2401 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2403 showPosition(wwin, x - width/2, y - h/2);
2405 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2407 break;
2409 case ButtonPress:
2410 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2411 XSync(dpy, 0);
2412 *x_ret = x - width/2;
2413 *y_ret = y - h/2;
2414 XUngrabPointer(dpy, CurrentTime);
2415 XUngrabKeyboard(dpy, CurrentTime);
2416 /* get rid of the geometry window */
2417 WMUnmapWidget(scr->gview);
2418 return;
2420 default:
2421 WMHandleEvent(&event);
2422 break;