Don't save app settings proplist when nothing changed
[wmaker-crm.git] / src / moveres.c
blobc0bf039916e7e8ce478777d0b995cd03ac85e750
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "client.h"
35 #include "icon.h"
36 #include "dock.h"
37 #include "funcs.h"
38 #include "actions.h"
39 #include "workspace.h"
41 #include "geomview.h"
42 #include "screen.h"
43 #include "xinerama.h"
45 #include <WINGs/WINGsP.h>
47 /* How many different types of geometry/position
48 display thingies are there? */
49 #define NUM_DISPLAYS 5
51 #define LEFT 1
52 #define RIGHT 2
53 #define HORIZONTAL (LEFT|RIGHT)
54 #define UP 4
55 #define DOWN 8
56 #define VERTICAL (UP|DOWN)
58 /* True if window currently has a border. This also includes borderless
59 * windows which are currently selected
61 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
63 /****** Global Variables ******/
64 extern Cursor wCursor[WCUR_LAST];
65 extern WPreferences wPreferences;
68 *----------------------------------------------------------------------
69 * checkMouseSamplingRate-
70 * For lowering the mouse motion sampling rate for machines where
71 * it's too high (SGIs). If it returns False then the event should be
72 * ignored.
73 *----------------------------------------------------------------------
75 static Bool checkMouseSamplingRate(XEvent * ev)
77 static Time previousMotion = 0;
79 if (ev->type == MotionNotify) {
80 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
81 return False;
82 } else {
83 previousMotion = ev->xmotion.time;
86 return True;
90 *----------------------------------------------------------------------
91 * moveGeometryDisplayCentered
93 * routine that moves the geometry/position window on scr so it is
94 * centered over the given coordinates (x,y). Also the window position
95 * is clamped so it stays on the screen at all times.
96 *----------------------------------------------------------------------
98 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
100 unsigned int w = WMWidgetWidth(scr->gview);
101 unsigned int h = WMWidgetHeight(scr->gview);
102 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
104 x -= w / 2;
105 y -= h / 2;
107 /* dead area check */
108 if (scr->xine_info.count) {
109 WMRect rect;
110 int head, flags;
112 rect.pos.x = x;
113 rect.pos.y = y;
114 rect.size.width = w;
115 rect.size.height = h;
117 head = wGetRectPlacementInfo(scr, rect, &flags);
119 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
120 rect = wGetRectForHead(scr, head);
121 x1 = rect.pos.x;
122 y1 = rect.pos.y;
123 x2 = x1 + rect.size.width;
124 y2 = y1 + rect.size.height;
128 if (x < x1 + 1)
129 x = x1 + 1;
130 else if (x > (x2 - w))
131 x = x2 - w;
133 if (y < y1 + 1)
134 y = y1 + 1;
135 else if (y > (y2 - h))
136 y = y2 - h;
138 WMMoveWidget(scr->gview, x, y);
141 static void showPosition(WWindow * wwin, int x, int y)
143 WScreen *scr = wwin->screen_ptr;
145 if (wPreferences.move_display == WDIS_NEW) {
146 #if 0
147 int width = wwin->frame->core->width;
148 int height = wwin->frame->core->height;
150 GC lgc = scr->line_gc;
151 XSetForeground(dpy, lgc, scr->line_pixel);
152 sprintf(num, "%i", x);
154 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
155 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
156 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
157 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
158 #endif
159 } else {
160 WSetGeometryViewShownPosition(scr->gview, x, y);
164 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
166 WScreen *scr = wwin->screen_ptr;
167 WMRect rect;
169 wPreferences.move_display++;
170 wPreferences.move_display %= NUM_DISPLAYS;
172 if (wPreferences.move_display == WDIS_NEW) {
173 wPreferences.move_display++;
174 wPreferences.move_display %= NUM_DISPLAYS;
177 if (wPreferences.move_display == WDIS_NONE) {
178 WMUnmapWidget(scr->gview);
179 } else {
180 if (wPreferences.move_display == WDIS_CENTER) {
181 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
182 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
183 rect.pos.y + rect.size.height / 2);
184 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
185 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
186 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
187 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
188 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
190 WMMapWidget(scr->gview);
194 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
196 WScreen *scr = wwin->screen_ptr;
197 WMRect rect;
199 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
200 return;
201 } else if (wPreferences.move_display == WDIS_CENTER) {
202 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
203 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
204 rect.pos.y + rect.size.height / 2);
205 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
206 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
207 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
208 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
209 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
211 WMMapWidget(scr->gview);
212 WSetGeometryViewShownPosition(scr->gview, x, y);
215 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
217 WScreen *scr = wwin->screen_ptr;
218 Window root = scr->root_win;
219 GC gc = scr->line_gc;
220 int ty, by, my, x, y, mx, s;
221 char num[16];
222 XSegment segment[4];
223 int fw, fh;
225 /* This seems necessary for some odd reason (too lazy to write x1-1 and
226 * x2-1 everywhere below in the code). But why only for x? */
227 x1--;
228 x2--;
230 if (HAS_BORDER_WITH_SELECT(wwin)) {
231 x1 += FRAME_BORDER_WIDTH;
232 x2 += FRAME_BORDER_WIDTH;
233 y1 += FRAME_BORDER_WIDTH;
234 y2 += FRAME_BORDER_WIDTH;
237 ty = y1 + wwin->frame->top_width;
238 by = y2 - wwin->frame->bottom_width;
240 if (wPreferences.size_display == WDIS_NEW) {
241 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
242 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
244 XSetForeground(dpy, gc, scr->line_pixel);
246 /* vertical geometry */
247 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
248 x = x2;
249 s = -15;
250 } else {
251 x = x1;
252 s = 15;
254 my = (ty + by) / 2;
256 /* top arrow & end bar */
257 segment[0].x1 = x - (s + 6);
258 segment[0].y1 = ty;
259 segment[0].x2 = x - (s - 10);
260 segment[0].y2 = ty;
262 /* arrowhead */
263 segment[1].x1 = x - (s - 2);
264 segment[1].y1 = ty + 1;
265 segment[1].x2 = x - (s - 5);
266 segment[1].y2 = ty + 7;
268 segment[2].x1 = x - (s - 2);
269 segment[2].y1 = ty + 1;
270 segment[2].x2 = x - (s + 1);
271 segment[2].y2 = ty + 7;
273 /* line */
274 segment[3].x1 = x - (s - 2);
275 segment[3].y1 = ty + 1;
276 segment[3].x2 = x - (s - 2);
277 segment[3].y2 = my - fh / 2 - 1;
279 XDrawSegments(dpy, root, gc, segment, 4);
281 /* bottom arrow & end bar */
282 segment[0].y1 = by;
283 segment[0].y2 = by;
285 /* arrowhead */
286 segment[1].y1 = by - 1;
287 segment[1].y2 = by - 7;
289 segment[2].y1 = by - 1;
290 segment[2].y2 = by - 7;
292 /* line */
293 segment[3].y1 = my + fh / 2 + 2;
294 segment[3].y2 = by - 1;
296 XDrawSegments(dpy, root, gc, segment, 4);
298 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
299 wwin->normal_hints->height_inc);
300 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
302 /* Display the height. */
303 XSetFont(dpy, gc, scr->tech_draw_font->fid);
304 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
305 strlen(num));
307 /* horizontal geometry */
308 if (y1 < 15) {
309 y = y2;
310 s = -15;
311 } else {
312 y = y1;
313 s = 15;
315 mx = x1 + (x2 - x1) / 2;
316 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
317 wwin->normal_hints->width_inc);
318 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
320 /* left arrow & end bar */
321 segment[0].x1 = x1;
322 segment[0].y1 = y - (s + 6);
323 segment[0].x2 = x1;
324 segment[0].y2 = y - (s - 10);
326 /* arrowhead */
327 segment[1].x1 = x1 + 7;
328 segment[1].y1 = y - (s + 1);
329 segment[1].x2 = x1 + 1;
330 segment[1].y2 = y - (s - 2);
332 segment[2].x1 = x1 + 1;
333 segment[2].y1 = y - (s - 2);
334 segment[2].x2 = x1 + 7;
335 segment[2].y2 = y - (s - 5);
337 /* line */
338 segment[3].x1 = x1 + 1;
339 segment[3].y1 = y - (s - 2);
340 segment[3].x2 = mx - fw / 2 - 2;
341 segment[3].y2 = y - (s - 2);
343 XDrawSegments(dpy, root, gc, segment, 4);
345 /* right arrow & end bar */
346 segment[0].x1 = x2 + 1;
347 segment[0].x2 = x2 + 1;
349 /* arrowhead */
350 segment[1].x1 = x2 - 6;
351 segment[1].x2 = x2;
353 segment[2].x1 = x2;
354 segment[2].x2 = x2 - 6;
356 /* line */
357 segment[3].x1 = mx + fw / 2 + 2;
358 segment[3].x2 = x2;
360 XDrawSegments(dpy, root, gc, segment, 4);
362 /* Display the width. */
363 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
364 strlen(num));
365 } else {
366 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
367 / wwin->normal_hints->width_inc,
368 (by - ty - wwin->normal_hints->base_height)
369 / wwin->normal_hints->height_inc);
373 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
375 WScreen *scr = wwin->screen_ptr;
376 WMRect rect;
378 wPreferences.size_display++;
379 wPreferences.size_display %= NUM_DISPLAYS;
381 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
382 WMUnmapWidget(scr->gview);
383 } else {
384 if (wPreferences.size_display == WDIS_CENTER) {
385 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
386 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
387 rect.pos.y + rect.size.height / 2);
388 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
389 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
390 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
391 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
392 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
394 WMMapWidget(scr->gview);
395 showGeometry(wwin, x, y, x + w, y + h, dir);
399 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
401 WScreen *scr = wwin->screen_ptr;
402 WMRect rect;
404 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
405 return;
407 if (wPreferences.size_display == WDIS_CENTER) {
408 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
409 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
410 rect.pos.y + rect.size.height / 2);
411 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
412 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
413 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
414 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
415 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
417 WMMapWidget(scr->gview);
418 showGeometry(wwin, x, y, x + w, y + h, 0);
421 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
423 WWindow *tmpw;
424 WScreen *scr = wwin->screen_ptr;
425 int x, y;
427 if (!array || !WMGetArrayItemCount(array)) {
428 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
429 } else {
430 WMArrayIterator iter;
432 WM_ITERATE_ARRAY(array, tmpw, iter) {
433 x = tmpw->frame_x + dx;
434 y = tmpw->frame_y + dy;
436 #if 1 /* XXX: with xinerama patch was #if 0, check this */
437 /* don't let windows become unreachable */
439 if (x + (int)tmpw->frame->core->width < 20)
440 x = 20 - (int)tmpw->frame->core->width;
441 else if (x + 20 > scr->scr_width)
442 x = scr->scr_width - 20;
444 if (y + (int)tmpw->frame->core->height < 20)
445 y = 20 - (int)tmpw->frame->core->height;
446 else if (y + 20 > scr->scr_height)
447 y = scr->scr_height - 20;
448 #else
449 wScreenBringInside(scr, &x, &y,
450 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
451 #endif
453 wWindowMove(tmpw, x, y);
458 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
460 Window root = wwin->screen_ptr->root_win;
461 GC gc = wwin->screen_ptr->frame_gc;
462 int h = 0;
463 int bottom = 0;
465 if (HAS_BORDER_WITH_SELECT(wwin)) {
466 x += FRAME_BORDER_WIDTH;
467 y += FRAME_BORDER_WIDTH;
470 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
471 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
472 TITLEBAR_EXTEND_SPACE) * 2;
474 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
475 /* Can't use wwin-frame->bottom_width because, in some cases
476 (e.g. interactive placement), frame does not point to anything. */
477 bottom = RESIZEBAR_HEIGHT;
479 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
481 if (h > 0) {
482 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
484 if (bottom > 0) {
485 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
489 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
491 WWindow *tmpw;
492 int scr_width = wwin->screen_ptr->scr_width;
493 int scr_height = wwin->screen_ptr->scr_height;
494 int x, y;
496 if (!array) {
498 x = wwin->frame_x + dx;
499 y = wwin->frame_y + dy;
501 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
503 } else {
504 WMArrayIterator iter;
506 WM_ITERATE_ARRAY(array, tmpw, iter) {
507 x = tmpw->frame_x + dx;
508 y = tmpw->frame_y + dy;
510 /* don't let windows become unreachable */
511 #if 1 /* XXX: was 0 in XINERAMA patch, check */
512 if (x + (int)tmpw->frame->core->width < 20)
513 x = 20 - (int)tmpw->frame->core->width;
514 else if (x + 20 > scr_width)
515 x = scr_width - 20;
517 if (y + (int)tmpw->frame->core->height < 20)
518 y = 20 - (int)tmpw->frame->core->height;
519 else if (y + 20 > scr_height)
520 y = scr_height - 20;
522 #else
523 wScreenBringInside(wwin->screen_ptr, &x, &y,
524 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
525 #endif
527 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
532 static void flushMotion()
534 XEvent ev;
536 XSync(dpy, False);
537 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
540 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
542 /* do not let window be unmapped */
543 if (opaque_move) {
544 wwin->flags.changing_workspace = 1;
545 wWindowChangeWorkspace(wwin, new_workspace);
547 /* go to new workspace */
548 wWorkspaceChange(scr, new_workspace);
550 wwin->flags.changing_workspace = 0;
552 if (rewind)
553 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
554 else
555 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
557 flushMotion();
559 if (!opaque_move) {
560 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
561 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
562 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
566 typedef struct {
567 /* arrays of WWindows sorted by the respective border position */
568 WWindow **topList; /* top border */
569 WWindow **leftList; /* left border */
570 WWindow **rightList; /* right border */
571 WWindow **bottomList; /* bottom border */
572 int count;
574 /* index of window in the above lists indicating the relative position
575 * of the window with the others */
576 int topIndex;
577 int leftIndex;
578 int rightIndex;
579 int bottomIndex;
581 int rubCount; /* for workspace switching */
583 int winWidth, winHeight; /* width/height of the window */
584 int realX, realY; /* actual position of the window */
585 int calcX, calcY; /* calculated position of window */
586 int omouseX, omouseY; /* old mouse position */
587 int mouseX, mouseY; /* last known position of the pointer */
588 } MoveData;
590 #define WTOP(w) (w)->frame_y
591 #define WLEFT(w) (w)->frame_x
592 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
593 (HAS_BORDER_WITH_SELECT(w) ? 2*FRAME_BORDER_WIDTH : 0))
594 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
595 (HAS_BORDER_WITH_SELECT(w) ? 2*FRAME_BORDER_WIDTH : 0))
597 static int compareWTop(const void *a, const void *b)
599 WWindow *wwin1 = *(WWindow **) a;
600 WWindow *wwin2 = *(WWindow **) b;
602 if (WTOP(wwin1) > WTOP(wwin2))
603 return -1;
604 else if (WTOP(wwin1) < WTOP(wwin2))
605 return 1;
606 else
607 return 0;
610 static int compareWLeft(const void *a, const void *b)
612 WWindow *wwin1 = *(WWindow **) a;
613 WWindow *wwin2 = *(WWindow **) b;
615 if (WLEFT(wwin1) > WLEFT(wwin2))
616 return -1;
617 else if (WLEFT(wwin1) < WLEFT(wwin2))
618 return 1;
619 else
620 return 0;
623 static int compareWRight(const void *a, const void *b)
625 WWindow *wwin1 = *(WWindow **) a;
626 WWindow *wwin2 = *(WWindow **) b;
628 if (WRIGHT(wwin1) < WRIGHT(wwin2))
629 return -1;
630 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
631 return 1;
632 else
633 return 0;
636 static int compareWBottom(const void *a, const void *b)
638 WWindow *wwin1 = *(WWindow **) a;
639 WWindow *wwin2 = *(WWindow **) b;
641 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
642 return -1;
643 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
644 return 1;
645 else
646 return 0;
649 static void updateResistance(WWindow * wwin, MoveData * data, int newX, int newY)
651 int i;
652 int newX2 = newX + data->winWidth;
653 int newY2 = newY + data->winHeight;
654 Bool ok = False;
656 if (newX < data->realX) {
657 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
658 ok = True;
659 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
660 ok = True;
662 } else if (newX > data->realX) {
663 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
664 ok = True;
665 } else if (data->rightIndex <= data->count - 1
666 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
667 ok = True;
671 if (!ok) {
672 if (newY < data->realY) {
673 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
674 ok = True;
675 } else if (data->topIndex <= data->count - 1
676 && newY2 <= WTOP(data->topList[data->topIndex])) {
677 ok = True;
679 } else if (newY > data->realY) {
680 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
681 ok = True;
682 } else if (data->bottomIndex <= data->count - 1
683 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
684 ok = True;
689 if (!ok)
690 return;
692 /* TODO: optimize this */
693 if (data->realY < WBOTTOM(data->bottomList[0])) {
694 data->bottomIndex = 0;
696 if (data->realX < WRIGHT(data->rightList[0])) {
697 data->rightIndex = 0;
699 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
700 data->leftIndex = 0;
702 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
703 data->topIndex = 0;
705 for (i = 0; i < data->count; i++) {
706 if (data->realY > WBOTTOM(data->bottomList[i])) {
707 data->bottomIndex = i + 1;
709 if (data->realX > WRIGHT(data->rightList[i])) {
710 data->rightIndex = i + 1;
712 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
713 data->leftIndex = i + 1;
715 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
716 data->topIndex = i + 1;
721 static void freeMoveData(MoveData * data)
723 if (data->topList)
724 wfree(data->topList);
725 if (data->leftList)
726 wfree(data->leftList);
727 if (data->rightList)
728 wfree(data->rightList);
729 if (data->bottomList)
730 wfree(data->bottomList);
733 static void updateMoveData(WWindow * wwin, MoveData * data)
735 WScreen *scr = wwin->screen_ptr;
736 WWindow *tmp;
737 int i;
739 data->count = 0;
740 tmp = scr->focused_window;
741 while (tmp) {
742 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
743 && !tmp->flags.miniaturized
744 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
745 data->topList[data->count] = tmp;
746 data->leftList[data->count] = tmp;
747 data->rightList[data->count] = tmp;
748 data->bottomList[data->count] = tmp;
749 data->count++;
751 tmp = tmp->prev;
754 if (data->count == 0) {
755 data->topIndex = 0;
756 data->leftIndex = 0;
757 data->rightIndex = 0;
758 data->bottomIndex = 0;
759 return;
762 /* order from closest to the border of the screen to farthest */
764 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
765 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
766 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
767 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
769 /* figure the position of the window relative to the others */
771 data->topIndex = -1;
772 data->leftIndex = -1;
773 data->rightIndex = -1;
774 data->bottomIndex = -1;
776 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
777 data->bottomIndex = 0;
779 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
780 data->rightIndex = 0;
782 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
783 data->leftIndex = 0;
785 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
786 data->topIndex = 0;
788 for (i = 0; i < data->count; i++) {
789 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
790 data->bottomIndex = i + 1;
792 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
793 data->rightIndex = i + 1;
795 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
796 data->leftIndex = i + 1;
798 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
799 data->topIndex = i + 1;
804 static void initMoveData(WWindow * wwin, MoveData * data)
806 int i;
807 WWindow *tmp;
809 memset(data, 0, sizeof(MoveData));
811 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
813 if (i > 1) {
814 data->topList = wmalloc(sizeof(WWindow *) * i);
815 data->leftList = wmalloc(sizeof(WWindow *) * i);
816 data->rightList = wmalloc(sizeof(WWindow *) * i);
817 data->bottomList = wmalloc(sizeof(WWindow *) * i);
819 updateMoveData(wwin, data);
822 data->realX = wwin->frame_x;
823 data->realY = wwin->frame_y;
824 data->calcX = wwin->frame_x;
825 data->calcY = wwin->frame_y;
827 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * FRAME_BORDER_WIDTH : 0);
828 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * FRAME_BORDER_WIDTH : 0);
831 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
833 WScreen *scr = wwin->screen_ptr;
834 Bool changed = False;
836 if (data->mouseX <= 1) {
837 if (scr->current_workspace > 0) {
839 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1, True);
840 changed = True;
841 data->rubCount = 0;
843 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
845 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1, True);
846 changed = True;
847 data->rubCount = 0;
849 } else if (data->mouseX >= scr->scr_width - 2) {
851 if (scr->current_workspace == scr->workspace_count - 1) {
853 if (wPreferences.ws_cycle || scr->workspace_count == MAX_WORKSPACES) {
855 crossWorkspace(scr, wwin, opaqueMove, 0, False);
856 changed = True;
857 data->rubCount = 0;
859 /* if user insists on trying to go to next workspace even when
860 * it's already the last, create a new one */
861 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
863 /* detect user "rubbing" the window against the edge */
864 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
866 data->rubCount = -(data->rubCount + 1);
868 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
870 data->rubCount = -data->rubCount + 1;
873 /* create a new workspace */
874 if (abs(data->rubCount) > 2) {
875 /* go to next workspace */
876 wWorkspaceNew(scr);
878 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
879 changed = True;
880 data->rubCount = 0;
882 } else if (scr->current_workspace < scr->workspace_count) {
884 /* go to next workspace */
885 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
886 changed = True;
887 data->rubCount = 0;
889 } else {
890 data->rubCount = 0;
893 return changed;
896 static void
897 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
898 Bool opaqueMove, int newMouseX, int newMouseY)
900 WScreen *scr = wwin->screen_ptr;
901 int dx, dy; /* how much mouse moved */
902 int winL, winR, winT, winB; /* requested new window position */
903 int newX, newY; /* actual new window position */
904 Bool hresist, vresist;
905 Bool updateIndex;
906 Bool attract;
908 hresist = False;
909 vresist = False;
911 updateIndex = False;
913 /* check the direction of the movement */
914 dx = newMouseX - data->mouseX;
915 dy = newMouseY - data->mouseY;
917 data->omouseX = data->mouseX;
918 data->omouseY = data->mouseY;
919 data->mouseX = newMouseX;
920 data->mouseY = newMouseY;
922 winL = data->calcX + dx;
923 winR = data->calcX + data->winWidth + dx;
924 winT = data->calcY + dy;
925 winB = data->calcY + data->winHeight + dy;
927 newX = data->realX;
928 newY = data->realY;
930 if (doResistance) {
931 int l_edge, r_edge;
932 int edge_l, edge_r;
933 int t_edge, b_edge;
934 int edge_t, edge_b;
935 int resist;
937 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
938 attract = wPreferences.attract;
939 /* horizontal movement: check horizontal edge resistances */
940 if (dx || dy) {
941 WMRect rect;
942 int i, head;
943 /* window is the leftmost window: check against screen edge */
945 /* Add inter head resistance 1/2 (if needed) */
946 head = wGetHeadForPointerLocation(scr);
947 rect = wGetRectForHead(scr, head);
949 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
950 edge_l = l_edge - resist;
951 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
952 r_edge = edge_r + resist;
954 /* 1 */
955 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
956 WWindow *looprw;
958 for (i = data->rightIndex - 1; i >= 0; i--) {
959 looprw = data->rightList[i];
960 if (!(data->realY > WBOTTOM(looprw)
961 || (data->realY + data->winHeight) < WTOP(looprw))) {
962 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
963 l_edge = WRIGHT(looprw) + 1;
964 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
966 break;
970 if (attract) {
971 for (i = data->rightIndex; i < data->count; i++) {
972 looprw = data->rightList[i];
973 if (!(data->realY > WBOTTOM(looprw)
974 || (data->realY + data->winHeight) < WTOP(looprw))) {
975 r_edge = WRIGHT(looprw) + 1;
976 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
977 break;
983 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
984 WWindow *looprw;
986 for (i = data->leftIndex - 1; i >= 0; i--) {
987 looprw = data->leftList[i];
988 if (!(data->realY > WBOTTOM(looprw)
989 || (data->realY + data->winHeight) < WTOP(looprw))) {
990 if (attract
991 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
992 && dx > 0)) {
993 edge_r = WLEFT(looprw);
994 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
996 break;
1000 if (attract)
1001 for (i = data->leftIndex; i < data->count; i++) {
1002 looprw = data->leftList[i];
1003 if (!(data->realY > WBOTTOM(looprw)
1004 || (data->realY + data->winHeight) < WTOP(looprw))) {
1005 edge_l = WLEFT(looprw);
1006 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1007 break;
1013 printf("%d %d\n",winL,winR);
1014 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1017 if ((winL - l_edge) < (r_edge - winL)) {
1018 if (resist > 0) {
1019 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1020 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1021 newX = l_edge;
1022 hresist = True;
1025 } else {
1026 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1027 newX = r_edge;
1028 hresist = True;
1032 if ((winR - edge_l) < (edge_r - winR)) {
1033 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1034 newX = edge_l - data->winWidth;
1035 hresist = True;
1037 } else {
1038 if (resist > 0) {
1039 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1040 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1041 newX = edge_r - data->winWidth;
1042 hresist = True;
1047 /* VeRT */
1048 /* Add inter head resistance 2/2 (if needed) */
1049 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1050 edge_t = t_edge - resist;
1051 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1052 b_edge = edge_b + resist;
1054 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1055 WWindow *looprw;
1057 for (i = data->bottomIndex - 1; i >= 0; i--) {
1058 looprw = data->bottomList[i];
1059 if (!(data->realX > WRIGHT(looprw)
1060 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1061 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1062 t_edge = WBOTTOM(looprw) + 1;
1063 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1065 break;
1069 if (attract) {
1070 for (i = data->bottomIndex; i < data->count; i++) {
1071 looprw = data->bottomList[i];
1072 if (!(data->realX > WRIGHT(looprw)
1073 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1074 b_edge = WBOTTOM(looprw) + 1;
1075 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1076 break;
1082 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1083 WWindow *looprw;
1085 for (i = data->topIndex - 1; i >= 0; i--) {
1086 looprw = data->topList[i];
1087 if (!(data->realX > WRIGHT(looprw)
1088 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1089 if (attract
1090 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1091 && dy > 0)) {
1092 edge_b = WTOP(looprw);
1093 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1095 break;
1099 if (attract)
1100 for (i = data->topIndex; i < data->count; i++) {
1101 looprw = data->topList[i];
1102 if (!(data->realX > WRIGHT(looprw)
1103 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1104 edge_t = WTOP(looprw);
1105 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1106 break;
1111 if ((winT - t_edge) < (b_edge - winT)) {
1112 if (resist > 0) {
1113 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1114 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1115 newY = t_edge;
1116 vresist = True;
1119 } else {
1120 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1121 newY = b_edge;
1122 vresist = True;
1126 if ((winB - edge_t) < (edge_b - winB)) {
1127 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1128 newY = edge_t - data->winHeight;
1129 vresist = True;
1131 } else {
1132 if (resist > 0) {
1133 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1134 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1135 newY = edge_b - data->winHeight;
1136 vresist = True;
1141 /* END VeRT */
1145 /* update window position */
1146 data->calcX += dx;
1147 data->calcY += dy;
1149 if (((dx > 0 && data->calcX - data->realX > 0)
1150 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1151 newX = data->calcX;
1153 if (((dy > 0 && data->calcY - data->realY > 0)
1154 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1155 newY = data->calcY;
1157 if (data->realX != newX || data->realY != newY) {
1159 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1160 showPosition(wwin, data->realX, data->realY);
1162 if (opaqueMove) {
1163 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1164 } else {
1165 /* erase frames */
1166 drawFrames(wwin, scr->selected_windows,
1167 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1170 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1172 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1175 if (!opaqueMove) {
1176 /* draw frames */
1177 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1180 if (!scr->selected_windows) {
1181 showPosition(wwin, newX, newY);
1185 /* recalc relative window position */
1186 if (doResistance && (data->realX != newX || data->realY != newY)) {
1187 updateResistance(wwin, data, newX, newY);
1190 data->realX = newX;
1191 data->realY = newY;
1194 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1196 #define MOVABLE_BIT 0x01
1197 #define RESIZABLE_BIT 0x02
1199 int wKeyboardMoveResizeWindow(WWindow * wwin)
1201 WScreen *scr = wwin->screen_ptr;
1202 Window root = scr->root_win;
1203 XEvent event;
1204 int w = wwin->frame->core->width;
1205 int h = wwin->frame->core->height;
1206 int scr_width = wwin->screen_ptr->scr_width;
1207 int scr_height = wwin->screen_ptr->scr_height;
1208 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1209 int src_x = wwin->frame_x;
1210 int src_y = wwin->frame_y;
1211 int done, off_x, off_y, ww, wh;
1212 int kspeed = _KS;
1213 Time lastTime = 0;
1214 KeyCode shiftl, shiftr, ctrll, ctrlmode;
1215 KeySym keysym = NoSymbol;
1216 int moment = 0;
1217 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1218 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1219 ? wGetHeadForWindow(wwin)
1220 : scr->xine_info.primary_head);
1222 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1223 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1224 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1225 ctrlmode = done = off_x = off_y = 0;
1227 if (modes == RESIZABLE_BIT) {
1228 ctrlmode = 1;
1231 XSync(dpy, False);
1232 wusleep(10000);
1233 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1235 if (!wwin->flags.selected) {
1236 wUnselectWindows(scr);
1238 XGrabServer(dpy);
1239 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1240 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1241 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1243 if (wwin->flags.shaded || scr->selected_windows) {
1244 if (scr->selected_windows)
1245 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1246 else
1247 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1248 if (!scr->selected_windows)
1249 mapPositionDisplay(wwin, src_x, src_y, w, h);
1250 } else {
1251 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1253 ww = w;
1254 wh = h;
1255 while (1) {
1257 looper.ox=off_x;
1258 looper.oy=off_y;
1260 do {
1261 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1262 | ButtonPressMask | ExposureMask, &event);
1263 if (event.type == Expose) {
1264 WMHandleEvent(&event);
1266 } while (event.type == Expose);
1268 if (wwin->flags.shaded || scr->selected_windows) {
1269 if (scr->selected_windows)
1270 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1271 else
1272 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1273 /*** I HATE EDGE RESISTANCE - ]d ***/
1274 } else {
1275 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1278 if (ctrlmode)
1279 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1282 XUngrabServer(dpy);
1283 XSync(dpy, False);
1285 switch (event.type) {
1286 case KeyPress:
1287 /* accelerate */
1288 if (event.xkey.time - lastTime > 50) {
1289 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1290 } else {
1291 if (kspeed < 20) {
1292 kspeed++;
1295 if (kspeed < _KS)
1296 kspeed = _KS;
1297 lastTime = event.xkey.time;
1298 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1299 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1300 ctrlmode = 1;
1301 wUnselectWindows(scr);
1302 } else {
1303 ctrlmode = 0;
1306 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1307 if (ctrlmode)
1308 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1309 else
1310 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1311 } else {
1313 keysym = XLookupKeysym(&event.xkey, 0);
1314 switch (keysym) {
1315 case XK_Return:
1316 done = 2;
1317 break;
1318 case XK_Escape:
1319 done = 1;
1320 break;
1321 case XK_Up:
1322 #ifdef XK_KP_Up
1323 case XK_KP_Up:
1324 #endif
1325 case XK_k:
1326 if (ctrlmode) {
1327 if (moment != UP)
1328 h = wh;
1329 h -= kspeed;
1330 moment = UP;
1331 if (h < 1)
1332 h = 1;
1333 } else
1334 off_y -= kspeed;
1335 break;
1336 case XK_Down:
1337 #ifdef XK_KP_Down
1338 case XK_KP_Down:
1339 #endif
1340 case XK_j:
1341 if (ctrlmode) {
1342 if (moment != DOWN)
1343 h = wh;
1344 h += kspeed;
1345 moment = DOWN;
1346 } else
1347 off_y += kspeed;
1348 break;
1349 case XK_Left:
1350 #ifdef XK_KP_Left
1351 case XK_KP_Left:
1352 #endif
1353 case XK_h:
1354 if (ctrlmode) {
1355 if (moment != LEFT)
1356 w = ww;
1357 w -= kspeed;
1358 if (w < 1)
1359 w = 1;
1360 moment = LEFT;
1361 } else
1362 off_x -= kspeed;
1363 break;
1364 case XK_Right:
1365 #ifdef XK_KP_Right
1366 case XK_KP_Right:
1367 #endif
1368 case XK_l:
1369 if (ctrlmode) {
1370 if (moment != RIGHT)
1371 w = ww;
1372 w += kspeed;
1373 moment = RIGHT;
1374 } else
1375 off_x += kspeed;
1376 break;
1379 ww = w;
1380 wh = h;
1381 wh -= vert_border;
1382 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1383 wh += vert_border;
1385 if (wPreferences.ws_cycle) {
1386 if (src_x + off_x + ww < 20) {
1387 if (!scr->current_workspace) {
1388 wWorkspaceChange(scr, scr->workspace_count - 1);
1389 } else
1390 wWorkspaceChange(scr, scr->current_workspace - 1);
1391 off_x += scr_width;
1392 } else if (src_x + off_x + 20 > scr_width) {
1393 if (scr->current_workspace == scr->workspace_count - 1) {
1394 wWorkspaceChange(scr, 0);
1395 } else
1396 wWorkspaceChange(scr, scr->current_workspace + 1);
1397 off_x -= scr_width;
1399 } else {
1400 if (src_x + off_x + ww < 20)
1401 off_x = 20 - ww - src_x;
1402 else if (src_x + off_x + 20 > scr_width)
1403 off_x = scr_width - 20 - src_x;
1406 if (src_y + off_y + wh < 20) {
1407 off_y = 20 - wh - src_y;
1408 } else if (src_y + off_y + 20 > scr_height) {
1409 off_y = scr_height - 20 - src_y;
1412 break;
1413 case ButtonPress:
1414 case ButtonRelease:
1415 done = 1;
1416 break;
1417 case Expose:
1418 WMHandleEvent(&event);
1419 while (XCheckTypedEvent(dpy, Expose, &event)) {
1420 WMHandleEvent(&event);
1422 break;
1424 default:
1425 WMHandleEvent(&event);
1426 break;
1429 XGrabServer(dpy);
1430 /*xxx */
1432 if (wwin->flags.shaded && !scr->selected_windows) {
1433 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1434 } else {
1435 if (ctrlmode) {
1436 WMUnmapWidget(scr->gview);
1437 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1438 } else if (!scr->selected_windows) {
1439 WMUnmapWidget(scr->gview);
1440 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1444 if (wwin->flags.shaded || scr->selected_windows) {
1445 if (scr->selected_windows)
1446 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1447 else
1448 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1449 } else {
1450 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1453 if (ctrlmode) {
1454 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1456 } else if (!scr->selected_windows)
1457 showPosition(wwin, src_x + off_x, src_y + off_y);
1459 if (done) {
1460 scr->keymove_tick = 0;
1462 WMDeleteTimerWithClientData(&looper);
1464 if (wwin->flags.shaded || scr->selected_windows) {
1465 if (scr->selected_windows)
1466 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1467 else
1468 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1469 } else {
1470 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1473 if (ctrlmode) {
1474 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1475 src_y + off_y + wh, 0);
1476 WMUnmapWidget(scr->gview);
1477 } else
1478 WMUnmapWidget(scr->gview);
1480 XUngrabKeyboard(dpy, CurrentTime);
1481 XUngrabPointer(dpy, CurrentTime);
1482 XUngrabServer(dpy);
1484 if (done == 2) {
1485 if (wwin->flags.shaded || scr->selected_windows) {
1486 if (!scr->selected_windows) {
1487 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1488 wWindowSynthConfigureNotify(wwin);
1489 } else {
1490 WMArrayIterator iter;
1491 WWindow *foo;
1493 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1495 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1496 wWindowSynthConfigureNotify(foo);
1499 } else {
1500 if (wwin->client.width != ww) {
1501 wwin->flags.user_changed_width = 1;
1502 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
1505 if (wwin->client.height != wh - vert_border) {
1506 wwin->flags.user_changed_height = 1;
1507 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1510 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1511 wWindowSynthConfigureNotify(wwin);
1513 wWindowChangeWorkspace(wwin, scr->current_workspace);
1514 wSetFocusTo(scr, wwin);
1517 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1518 head != wGetHeadForWindow(wwin)) {
1519 wArrangeIcons(scr, True);
1522 return 1;
1528 *----------------------------------------------------------------------
1529 * wMouseMoveWindow--
1530 * Move the named window and the other selected ones (if any),
1531 * interactively. Also shows the position of the window, if only one
1532 * window is being moved.
1533 * If the window is not on the selected window list, the selected
1534 * windows are deselected.
1535 * If shift is pressed during the operation, the position display
1536 * is changed to another type.
1538 * Returns:
1539 * True if the window was moved, False otherwise.
1541 * Side effects:
1542 * The window(s) position is changed, and the client(s) are
1543 * notified about that.
1544 * The position display configuration may be changed.
1545 *----------------------------------------------------------------------
1547 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1549 WScreen *scr = wwin->screen_ptr;
1550 XEvent event;
1551 Window root = scr->root_win;
1552 KeyCode shiftl, shiftr;
1553 Bool done = False;
1554 int started = 0;
1555 int warped = 0;
1556 /* This needs not to change while moving, else bad things can happen */
1557 int opaqueMove = wPreferences.opaque_move;
1558 MoveData moveData;
1559 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1560 ? wGetHeadForWindow(wwin)
1561 : scr->xine_info.primary_head);
1563 if (!IS_MOVABLE(wwin))
1564 return False;
1566 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1567 XSetWindowAttributes attr;
1569 attr.save_under = True;
1570 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1573 initMoveData(wwin, &moveData);
1575 moveData.mouseX = ev->xmotion.x_root;
1576 moveData.mouseY = ev->xmotion.y_root;
1578 if (!wwin->flags.selected) {
1579 /* this window is not selected, unselect others and move only wwin */
1580 wUnselectWindows(scr);
1582 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1583 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1584 while (!done) {
1585 if (warped) {
1586 int junk;
1587 Window junkw;
1589 /* XWarpPointer() doesn't seem to generate Motion events, so
1590 * we've got to simulate them */
1591 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1592 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1593 } else {
1594 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1595 | PointerMotionHintMask
1596 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1598 if (event.type == MotionNotify) {
1599 /* compress MotionNotify events */
1600 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1601 if (!checkMouseSamplingRate(&event))
1602 continue;
1605 switch (event.type) {
1606 case KeyPress:
1607 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1608 && started && !scr->selected_windows) {
1610 if (!opaqueMove) {
1611 drawFrames(wwin, scr->selected_windows,
1612 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1615 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1616 showPosition(wwin, moveData.realX, moveData.realY);
1617 XUngrabServer(dpy);
1619 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1620 moveData.winWidth, moveData.winHeight);
1622 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1623 XGrabServer(dpy);
1624 showPosition(wwin, moveData.realX, moveData.realY);
1627 if (!opaqueMove) {
1628 drawFrames(wwin, scr->selected_windows,
1629 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1631 /*} else {
1632 WMHandleEvent(&event); this causes problems needs fixing */
1634 break;
1636 case MotionNotify:
1637 if (started) {
1638 updateWindowPosition(wwin, &moveData,
1639 scr->selected_windows == NULL
1640 && wPreferences.edge_resistance > 0,
1641 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1643 if (!warped && !wPreferences.no_autowrap) {
1644 int oldWorkspace = scr->current_workspace;
1646 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1647 showPosition(wwin, moveData.realX, moveData.realY);
1648 XUngrabServer(dpy);
1650 if (!opaqueMove) {
1651 drawFrames(wwin, scr->selected_windows,
1652 moveData.realX - wwin->frame_x,
1653 moveData.realY - wwin->frame_y);
1655 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1656 if (scr->current_workspace != oldWorkspace
1657 && wPreferences.edge_resistance > 0
1658 && scr->selected_windows == NULL)
1659 updateMoveData(wwin, &moveData);
1660 warped = 1;
1662 if (!opaqueMove) {
1663 drawFrames(wwin, scr->selected_windows,
1664 moveData.realX - wwin->frame_x,
1665 moveData.realY - wwin->frame_y);
1667 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1668 XSync(dpy, False);
1669 showPosition(wwin, moveData.realX, moveData.realY);
1670 XGrabServer(dpy);
1672 } else {
1673 warped = 0;
1675 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1676 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1678 XChangeActivePointerGrab(dpy, ButtonMotionMask
1679 | ButtonReleaseMask | ButtonPressMask,
1680 wCursor[WCUR_MOVE], CurrentTime);
1681 started = 1;
1682 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1684 if (!scr->selected_windows)
1685 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1686 moveData.winWidth, moveData.winHeight);
1688 if (started && !opaqueMove)
1689 drawFrames(wwin, scr->selected_windows, 0, 0);
1691 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1692 && !scr->selected_windows)) {
1693 XGrabServer(dpy);
1694 if (wPreferences.move_display == WDIS_NEW)
1695 showPosition(wwin, moveData.realX, moveData.realY);
1698 break;
1700 case ButtonPress:
1701 break;
1703 case ButtonRelease:
1704 if (event.xbutton.button != ev->xbutton.button)
1705 break;
1707 if (started) {
1708 XEvent e;
1709 if (!opaqueMove) {
1710 drawFrames(wwin, scr->selected_windows,
1711 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1712 XSync(dpy, 0);
1713 doWindowMove(wwin, scr->selected_windows,
1714 moveData.realX - wwin->frame_x,
1715 moveData.realY - wwin->frame_y);
1717 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1718 wWindowSynthConfigureNotify(wwin);
1719 #endif
1720 XUngrabKeyboard(dpy, CurrentTime);
1721 XUngrabServer(dpy);
1722 if (!opaqueMove) {
1723 wWindowChangeWorkspace(wwin, scr->current_workspace);
1724 wSetFocusTo(scr, wwin);
1726 if (wPreferences.move_display == WDIS_NEW)
1727 showPosition(wwin, moveData.realX, moveData.realY);
1729 /* discard all enter/leave events that happened until
1730 * the time the button was released */
1731 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1732 if (e.xcrossing.time > event.xbutton.time) {
1733 XPutBackEvent(dpy, &e);
1734 break;
1737 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1738 if (e.xcrossing.time > event.xbutton.time) {
1739 XPutBackEvent(dpy, &e);
1740 break;
1744 if (!scr->selected_windows) {
1745 /* get rid of the geometry window */
1746 WMUnmapWidget(scr->gview);
1749 done = True;
1750 break;
1752 default:
1753 if (started && !opaqueMove) {
1754 drawFrames(wwin, scr->selected_windows,
1755 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1756 XUngrabServer(dpy);
1757 WMHandleEvent(&event);
1758 XSync(dpy, False);
1759 XGrabServer(dpy);
1760 drawFrames(wwin, scr->selected_windows,
1761 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1762 } else {
1763 WMHandleEvent(&event);
1765 break;
1769 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1770 XSetWindowAttributes attr;
1772 attr.save_under = False;
1773 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1777 freeMoveData(&moveData);
1779 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1780 head != wGetHeadForWindow(wwin)) {
1781 wArrangeIcons(scr, True);
1783 return started;
1786 #define RESIZEBAR 1
1787 #define HCONSTRAIN 2
1789 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1791 int w = wwin->frame->core->width - 1;
1792 int cw = wwin->frame->resizebar_corner_width;
1793 int dir;
1795 /* if not resizing through the resizebar */
1796 if (!(flags & RESIZEBAR)) {
1797 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1798 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1799 if (abs(dx) < 2 || abs(dy) < 2) {
1800 if (abs(dy) > abs(dx))
1801 xdir = 0;
1802 else
1803 ydir = 0;
1805 return (xdir | ydir);
1808 /* window is too narrow. allow diagonal resize */
1809 if (cw * 2 >= w) {
1810 int ydir;
1812 if (flags & HCONSTRAIN)
1813 ydir = 0;
1814 else
1815 ydir = DOWN;
1816 if (x < cw)
1817 return (LEFT | ydir);
1818 else
1819 return (RIGHT | ydir);
1821 /* vertical resize */
1822 if ((x > cw) && (x < w - cw))
1823 return DOWN;
1825 if (x < cw)
1826 dir = LEFT;
1827 else
1828 dir = RIGHT;
1830 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1831 dir |= DOWN;
1833 return dir;
1836 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1838 XEvent event;
1839 WScreen *scr = wwin->screen_ptr;
1840 Window root = scr->root_win;
1841 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1842 int fw = wwin->frame->core->width;
1843 int fh = wwin->frame->core->height;
1844 int fx = wwin->frame_x;
1845 int fy = wwin->frame_y;
1846 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1847 int orig_x, orig_y;
1848 int started;
1849 int dw, dh;
1850 int rw = fw, rh = fh;
1851 int rx1, ry1, rx2, ry2;
1852 int res = 0;
1853 KeyCode shiftl, shiftr;
1854 int h = 0;
1855 int orig_fx = fx;
1856 int orig_fy = fy;
1857 int orig_fw = fw;
1858 int orig_fh = fh;
1859 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1860 ? wGetHeadForWindow(wwin)
1861 : scr->xine_info.primary_head);
1863 if (!IS_RESIZABLE(wwin))
1864 return;
1866 if (wwin->flags.shaded) {
1867 wwarning("internal error: tryein");
1868 return;
1870 orig_x = ev->xbutton.x_root;
1871 orig_y = ev->xbutton.y_root;
1873 started = 0;
1874 wUnselectWindows(scr);
1875 rx1 = fx;
1876 rx2 = fx + fw - 1;
1877 ry1 = fy;
1878 ry2 = fy + fh - 1;
1879 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1880 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1881 if (HAS_TITLEBAR(wwin))
1882 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
1883 TITLEBAR_EXTEND_SPACE) * 2;
1884 else
1885 h = 0;
1886 while (1) {
1887 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1888 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1889 if (!checkMouseSamplingRate(&event))
1890 continue;
1892 switch (event.type) {
1893 case KeyPress:
1894 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1895 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1896 && started) {
1897 drawTransparentFrame(wwin, fx, fy, fw, fh);
1898 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1899 drawTransparentFrame(wwin, fx, fy, fw, fh);
1901 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1902 break;
1904 case MotionNotify:
1905 if (started) {
1906 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1908 dw = 0;
1909 dh = 0;
1911 orig_fx = fx;
1912 orig_fy = fy;
1913 orig_fw = fw;
1914 orig_fh = fh;
1916 if (res & LEFT)
1917 dw = orig_x - event.xmotion.x_root;
1918 else if (res & RIGHT)
1919 dw = event.xmotion.x_root - orig_x;
1920 if (res & UP)
1921 dh = orig_y - event.xmotion.y_root;
1922 else if (res & DOWN)
1923 dh = event.xmotion.y_root - orig_y;
1925 orig_x = event.xmotion.x_root;
1926 orig_y = event.xmotion.y_root;
1928 rw += dw;
1929 rh += dh;
1930 fw = rw;
1931 fh = rh - vert_border;
1932 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1933 fh += vert_border;
1934 if (res & LEFT)
1935 fx = rx2 - fw + 1;
1936 else if (res & RIGHT)
1937 fx = rx1;
1938 if (res & UP)
1939 fy = ry2 - fh + 1;
1940 else if (res & DOWN)
1941 fy = ry1;
1942 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1943 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1944 int tx, ty;
1945 Window junkw;
1946 int flags;
1948 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1949 orig_x, orig_y, &tx, &ty, &junkw);
1951 /* check if resizing through resizebar */
1952 if (is_resizebar)
1953 flags = RESIZEBAR;
1954 else
1955 flags = 0;
1957 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1958 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1959 flags |= HCONSTRAIN;
1961 res = getResizeDirection(wwin, tx, ty,
1962 orig_x - event.xmotion.x_root,
1963 orig_y - event.xmotion.y_root, flags);
1965 if (res == (UP | LEFT))
1966 XChangeActivePointerGrab(dpy, ButtonMotionMask
1967 | ButtonReleaseMask | ButtonPressMask,
1968 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1969 else if (res == (UP | RIGHT))
1970 XChangeActivePointerGrab(dpy, ButtonMotionMask
1971 | ButtonReleaseMask | ButtonPressMask,
1972 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1973 else if (res == (DOWN | LEFT))
1974 XChangeActivePointerGrab(dpy, ButtonMotionMask
1975 | ButtonReleaseMask | ButtonPressMask,
1976 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
1977 else if (res == (DOWN | RIGHT))
1978 XChangeActivePointerGrab(dpy, ButtonMotionMask
1979 | ButtonReleaseMask | ButtonPressMask,
1980 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
1981 else if (res == DOWN || res == UP)
1982 XChangeActivePointerGrab(dpy, ButtonMotionMask
1983 | ButtonReleaseMask | ButtonPressMask,
1984 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
1985 else if (res & (DOWN | UP))
1986 XChangeActivePointerGrab(dpy, ButtonMotionMask
1987 | ButtonReleaseMask | ButtonPressMask,
1988 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
1989 else if (res & (LEFT | RIGHT))
1990 XChangeActivePointerGrab(dpy, ButtonMotionMask
1991 | ButtonReleaseMask | ButtonPressMask,
1992 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
1994 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1996 XGrabServer(dpy);
1998 /* Draw the resize frame for the first time. */
1999 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2001 drawTransparentFrame(wwin, fx, fy, fw, fh);
2003 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2005 started = 1;
2007 if (started) {
2008 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2009 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2010 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2011 drawTransparentFrame(wwin, fx, fy, fw, fh);
2012 } else {
2013 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2014 drawTransparentFrame(wwin, fx, fy, fw, fh);
2016 if (fh != orig_fh || fw != orig_fw) {
2017 if (wPreferences.size_display == WDIS_NEW) {
2018 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2019 orig_fy + orig_fh, res);
2021 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2024 break;
2026 case ButtonPress:
2027 break;
2029 case ButtonRelease:
2030 if (event.xbutton.button != ev->xbutton.button)
2031 break;
2033 if (started) {
2034 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2036 drawTransparentFrame(wwin, fx, fy, fw, fh);
2038 XUngrabKeyboard(dpy, CurrentTime);
2039 WMUnmapWidget(scr->gview);
2040 XUngrabServer(dpy);
2042 if (wwin->client.width != fw) {
2043 wwin->flags.user_changed_width = 1;
2044 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_MAXIMUS);
2047 if (wwin->client.height != fh - vert_border) {
2048 wwin->flags.user_changed_height = 1;
2049 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2052 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2054 return;
2056 default:
2057 WMHandleEvent(&event);
2061 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin)) {
2062 wArrangeIcons(scr, True);
2066 #undef LEFT
2067 #undef RIGHT
2068 #undef HORIZONTAL
2069 #undef UP
2070 #undef DOWN
2071 #undef VERTICAL
2072 #undef HCONSTRAIN
2073 #undef RESIZEBAR
2075 void wUnselectWindows(WScreen * scr)
2077 WWindow *wwin;
2079 if (!scr->selected_windows)
2080 return;
2082 while (WMGetArrayItemCount(scr->selected_windows)) {
2083 wwin = WMGetFromArray(scr->selected_windows, 0);
2084 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2085 wIconSelect(wwin->icon);
2087 wSelectWindow(wwin, False);
2089 WMFreeArray(scr->selected_windows);
2090 scr->selected_windows = NULL;
2093 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2095 WWindow *tmpw;
2097 /* select the windows and put them in the selected window list */
2098 tmpw = scr->focused_window;
2099 while (tmpw != NULL) {
2100 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2101 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2102 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2103 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2104 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2105 wSelectWindow(tmpw, True);
2108 tmpw = tmpw->prev;
2112 void wSelectWindows(WScreen * scr, XEvent * ev)
2114 XEvent event;
2115 Window root = scr->root_win;
2116 GC gc = scr->frame_gc;
2117 int xp = ev->xbutton.x_root;
2118 int yp = ev->xbutton.y_root;
2119 int w = 0, h = 0;
2120 int x = xp, y = yp;
2122 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2123 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2124 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2125 return;
2127 XGrabServer(dpy);
2129 wUnselectWindows(scr);
2131 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2132 while (1) {
2133 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2135 switch (event.type) {
2136 case MotionNotify:
2137 XDrawRectangle(dpy, root, gc, x, y, w, h);
2138 x = event.xmotion.x_root;
2139 if (x < xp) {
2140 w = xp - x;
2141 } else {
2142 w = x - xp;
2143 x = xp;
2145 y = event.xmotion.y_root;
2146 if (y < yp) {
2147 h = yp - y;
2148 } else {
2149 h = y - yp;
2150 y = yp;
2152 XDrawRectangle(dpy, root, gc, x, y, w, h);
2153 break;
2155 case ButtonPress:
2156 break;
2158 case ButtonRelease:
2159 if (event.xbutton.button != ev->xbutton.button)
2160 break;
2162 XDrawRectangle(dpy, root, gc, x, y, w, h);
2163 XUngrabServer(dpy);
2164 XUngrabPointer(dpy, CurrentTime);
2165 selectWindowsInside(scr, x, y, x + w, y + h);
2166 return;
2168 default:
2169 WMHandleEvent(&event);
2170 break;
2175 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2177 WScreen *scr = wwin->screen_ptr;
2178 Window root = scr->root_win;
2179 int x, y, h = 0;
2180 XEvent event;
2181 KeyCode shiftl, shiftr;
2182 Window junkw;
2183 int junk;
2185 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2186 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2187 *x_ret = 0;
2188 *y_ret = 0;
2189 return;
2191 if (HAS_TITLEBAR(wwin)) {
2192 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2193 TITLEBAR_EXTEND_SPACE) * 2;
2194 height += h;
2196 if (HAS_RESIZEBAR(wwin)) {
2197 height += RESIZEBAR_HEIGHT;
2199 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2200 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2201 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2203 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2205 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2206 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2207 while (1) {
2208 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2210 if (!checkMouseSamplingRate(&event))
2211 continue;
2213 switch (event.type) {
2214 case KeyPress:
2215 if ((event.xkey.keycode == shiftl)
2216 || (event.xkey.keycode == shiftr)) {
2217 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2218 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2219 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2221 break;
2223 case MotionNotify:
2224 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2226 x = event.xmotion.x_root;
2227 y = event.xmotion.y_root;
2229 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2230 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2232 showPosition(wwin, x - width / 2, y - h / 2);
2234 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2236 break;
2238 case ButtonPress:
2239 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2240 XSync(dpy, 0);
2241 *x_ret = x - width / 2;
2242 *y_ret = y - h / 2;
2243 XUngrabPointer(dpy, CurrentTime);
2244 XUngrabKeyboard(dpy, CurrentTime);
2245 /* get rid of the geometry window */
2246 WMUnmapWidget(scr->gview);
2247 return;
2249 default:
2250 WMHandleEvent(&event);
2251 break;