WPrefs: Code formatting in TexturePanel.c; minimizes checkpatch.pl warnings.
[wmaker-crm.git] / src / moveres.c
blob9f7a91de477a79b47af58151a4771850741e78c6
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "client.h"
35 #include "icon.h"
36 #include "dock.h"
37 #include "actions.h"
38 #include "workspace.h"
39 #include "placement.h"
41 #include "geomview.h"
42 #include "screen.h"
43 #include "xinerama.h"
45 #include <WINGs/WINGsP.h>
47 /* How many different types of geometry/position
48 display thingies are there? */
49 #define NUM_DISPLAYS 5
51 #define LEFT 1
52 #define RIGHT 2
53 #define UP 4
54 #define DOWN 8
56 /* True if window currently has a border. This also includes borderless
57 * windows which are currently selected
59 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
63 *----------------------------------------------------------------------
64 * checkMouseSamplingRate-
65 * For lowering the mouse motion sampling rate for machines where
66 * it's too high (SGIs). If it returns False then the event should be
67 * ignored.
68 *----------------------------------------------------------------------
70 static Bool checkMouseSamplingRate(XEvent * ev)
72 static Time previousMotion = 0;
74 if (ev->type == MotionNotify) {
75 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
76 return False;
77 } else {
78 previousMotion = ev->xmotion.time;
81 return True;
85 *----------------------------------------------------------------------
86 * moveGeometryDisplayCentered
88 * routine that moves the geometry/position window on scr so it is
89 * centered over the given coordinates (x,y). Also the window position
90 * is clamped so it stays on the screen at all times.
91 *----------------------------------------------------------------------
93 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
95 unsigned int w = WMWidgetWidth(scr->gview);
96 unsigned int h = WMWidgetHeight(scr->gview);
97 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
99 x -= w / 2;
100 y -= h / 2;
102 /* dead area check */
103 if (scr->xine_info.count) {
104 WMRect rect;
105 int head, flags;
107 rect.pos.x = x;
108 rect.pos.y = y;
109 rect.size.width = w;
110 rect.size.height = h;
112 head = wGetRectPlacementInfo(scr, rect, &flags);
114 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
115 rect = wGetRectForHead(scr, head);
116 x1 = rect.pos.x;
117 y1 = rect.pos.y;
118 x2 = x1 + rect.size.width;
119 y2 = y1 + rect.size.height;
123 if (x < x1 + 1)
124 x = x1 + 1;
125 else if (x > (x2 - w))
126 x = x2 - w;
128 if (y < y1 + 1)
129 y = y1 + 1;
130 else if (y > (y2 - h))
131 y = y2 - h;
133 WMMoveWidget(scr->gview, x, y);
136 static void showPosition(WWindow * wwin, int x, int y)
138 WScreen *scr = wwin->screen_ptr;
140 if (wPreferences.move_display == WDIS_NEW) {
141 #if 0
142 int width = wwin->frame->core->width;
143 int height = wwin->frame->core->height;
145 GC lgc = scr->line_gc;
146 XSetForeground(dpy, lgc, scr->line_pixel);
147 sprintf(num, "%i", x);
149 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
150 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
151 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
152 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
153 #endif
154 } else {
155 WSetGeometryViewShownPosition(scr->gview, x, y);
159 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
161 WScreen *scr = wwin->screen_ptr;
162 WMRect rect;
164 wPreferences.move_display++;
165 wPreferences.move_display %= NUM_DISPLAYS;
167 if (wPreferences.move_display == WDIS_NEW) {
168 wPreferences.move_display++;
169 wPreferences.move_display %= NUM_DISPLAYS;
172 if (wPreferences.move_display == WDIS_NONE) {
173 WMUnmapWidget(scr->gview);
174 } else {
175 if (wPreferences.move_display == WDIS_CENTER) {
176 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
177 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
178 rect.pos.y + rect.size.height / 2);
179 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
180 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
181 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
182 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
183 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
185 WMMapWidget(scr->gview);
189 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
191 WScreen *scr = wwin->screen_ptr;
192 WMRect rect;
194 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
195 return;
196 } else if (wPreferences.move_display == WDIS_CENTER) {
197 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
198 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
199 rect.pos.y + rect.size.height / 2);
200 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
201 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
202 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
203 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
204 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
206 WMMapWidget(scr->gview);
207 WSetGeometryViewShownPosition(scr->gview, x, y);
210 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
212 WScreen *scr = wwin->screen_ptr;
213 Window root = scr->root_win;
214 GC gc = scr->line_gc;
215 int ty, by, my, x, y, mx, s;
216 char num[16];
217 XSegment segment[4];
218 int fw, fh;
220 /* This seems necessary for some odd reason (too lazy to write x1-1 and
221 * x2-1 everywhere below in the code). But why only for x? */
222 x1--;
223 x2--;
225 if (HAS_BORDER_WITH_SELECT(wwin)) {
226 x1 += scr->frame_border_width;
227 x2 += scr->frame_border_width;
228 y1 += scr->frame_border_width;
229 y2 += scr->frame_border_width;
232 ty = y1 + wwin->frame->top_width;
233 by = y2 - wwin->frame->bottom_width;
235 if (wPreferences.size_display == WDIS_NEW) {
236 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
237 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
239 XSetForeground(dpy, gc, scr->line_pixel);
241 /* vertical geometry */
242 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
243 x = x2;
244 s = -15;
245 } else {
246 x = x1;
247 s = 15;
249 my = (ty + by) / 2;
251 /* top arrow & end bar */
252 segment[0].x1 = x - (s + 6);
253 segment[0].y1 = ty;
254 segment[0].x2 = x - (s - 10);
255 segment[0].y2 = ty;
257 /* arrowhead */
258 segment[1].x1 = x - (s - 2);
259 segment[1].y1 = ty + 1;
260 segment[1].x2 = x - (s - 5);
261 segment[1].y2 = ty + 7;
263 segment[2].x1 = x - (s - 2);
264 segment[2].y1 = ty + 1;
265 segment[2].x2 = x - (s + 1);
266 segment[2].y2 = ty + 7;
268 /* line */
269 segment[3].x1 = x - (s - 2);
270 segment[3].y1 = ty + 1;
271 segment[3].x2 = x - (s - 2);
272 segment[3].y2 = my - fh / 2 - 1;
274 XDrawSegments(dpy, root, gc, segment, 4);
276 /* bottom arrow & end bar */
277 segment[0].y1 = by;
278 segment[0].y2 = by;
280 /* arrowhead */
281 segment[1].y1 = by - 1;
282 segment[1].y2 = by - 7;
284 segment[2].y1 = by - 1;
285 segment[2].y2 = by - 7;
287 /* line */
288 segment[3].y1 = my + fh / 2 + 2;
289 segment[3].y2 = by - 1;
291 XDrawSegments(dpy, root, gc, segment, 4);
293 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
294 wwin->normal_hints->height_inc);
295 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
297 /* Display the height. */
298 XSetFont(dpy, gc, scr->tech_draw_font->fid);
299 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
300 strlen(num));
302 /* horizontal geometry */
303 if (y1 < 15) {
304 y = y2;
305 s = -15;
306 } else {
307 y = y1;
308 s = 15;
310 mx = x1 + (x2 - x1) / 2;
311 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
312 wwin->normal_hints->width_inc);
313 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
315 /* left arrow & end bar */
316 segment[0].x1 = x1;
317 segment[0].y1 = y - (s + 6);
318 segment[0].x2 = x1;
319 segment[0].y2 = y - (s - 10);
321 /* arrowhead */
322 segment[1].x1 = x1 + 7;
323 segment[1].y1 = y - (s + 1);
324 segment[1].x2 = x1 + 1;
325 segment[1].y2 = y - (s - 2);
327 segment[2].x1 = x1 + 1;
328 segment[2].y1 = y - (s - 2);
329 segment[2].x2 = x1 + 7;
330 segment[2].y2 = y - (s - 5);
332 /* line */
333 segment[3].x1 = x1 + 1;
334 segment[3].y1 = y - (s - 2);
335 segment[3].x2 = mx - fw / 2 - 2;
336 segment[3].y2 = y - (s - 2);
338 XDrawSegments(dpy, root, gc, segment, 4);
340 /* right arrow & end bar */
341 segment[0].x1 = x2 + 1;
342 segment[0].x2 = x2 + 1;
344 /* arrowhead */
345 segment[1].x1 = x2 - 6;
346 segment[1].x2 = x2;
348 segment[2].x1 = x2;
349 segment[2].x2 = x2 - 6;
351 /* line */
352 segment[3].x1 = mx + fw / 2 + 2;
353 segment[3].x2 = x2;
355 XDrawSegments(dpy, root, gc, segment, 4);
357 /* Display the width. */
358 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
359 strlen(num));
360 } else {
361 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
362 / wwin->normal_hints->width_inc,
363 (by - ty - wwin->normal_hints->base_height)
364 / wwin->normal_hints->height_inc);
368 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
370 WScreen *scr = wwin->screen_ptr;
371 WMRect rect;
373 wPreferences.size_display++;
374 wPreferences.size_display %= NUM_DISPLAYS;
376 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
377 WMUnmapWidget(scr->gview);
378 } else {
379 if (wPreferences.size_display == WDIS_CENTER) {
380 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
381 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
382 rect.pos.y + rect.size.height / 2);
383 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
384 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
385 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
386 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
387 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
389 WMMapWidget(scr->gview);
390 showGeometry(wwin, x, y, x + w, y + h, dir);
394 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
396 WScreen *scr = wwin->screen_ptr;
397 WMRect rect;
399 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
400 return;
402 if (wPreferences.size_display == WDIS_CENTER) {
403 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
404 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
405 rect.pos.y + rect.size.height / 2);
406 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
407 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
408 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
409 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
410 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
412 WMMapWidget(scr->gview);
413 showGeometry(wwin, x, y, x + w, y + h, 0);
416 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
418 WWindow *tmpw;
419 WScreen *scr = wwin->screen_ptr;
420 int x, y;
422 if (!array || !WMGetArrayItemCount(array)) {
423 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
424 } else {
425 WMArrayIterator iter;
427 WM_ITERATE_ARRAY(array, tmpw, iter) {
428 x = tmpw->frame_x + dx;
429 y = tmpw->frame_y + dy;
431 #if 1 /* XXX: with xinerama patch was #if 0, check this */
432 /* don't let windows become unreachable */
434 if (x + (int)tmpw->frame->core->width < 20)
435 x = 20 - (int)tmpw->frame->core->width;
436 else if (x + 20 > scr->scr_width)
437 x = scr->scr_width - 20;
439 if (y + (int)tmpw->frame->core->height < 20)
440 y = 20 - (int)tmpw->frame->core->height;
441 else if (y + 20 > scr->scr_height)
442 y = scr->scr_height - 20;
443 #else
444 wScreenBringInside(scr, &x, &y,
445 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
446 #endif
448 wWindowMove(tmpw, x, y);
453 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
455 Window root = wwin->screen_ptr->root_win;
456 GC gc = wwin->screen_ptr->frame_gc;
457 int h = 0;
458 int bottom = 0;
460 if (HAS_BORDER_WITH_SELECT(wwin)) {
461 x += wwin->screen_ptr->frame_border_width;
462 y += wwin->screen_ptr->frame_border_width;
465 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
466 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
467 TITLEBAR_EXTEND_SPACE) * 2;
469 if (h > wPreferences.window_title_max_height)
470 h = wPreferences.window_title_max_height;
472 if (h < wPreferences.window_title_min_height)
473 h = wPreferences.window_title_min_height;
475 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
476 /* Can't use wwin-frame->bottom_width because, in some cases
477 (e.g. interactive placement), frame does not point to anything. */
478 bottom = RESIZEBAR_HEIGHT;
480 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
482 if (h > 0) {
483 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
485 if (bottom > 0) {
486 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
490 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
492 WWindow *tmpw;
493 int scr_width = wwin->screen_ptr->scr_width;
494 int scr_height = wwin->screen_ptr->scr_height;
495 int x, y;
497 if (!array) {
499 x = wwin->frame_x + dx;
500 y = wwin->frame_y + dy;
502 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
504 } else {
505 WMArrayIterator iter;
507 WM_ITERATE_ARRAY(array, tmpw, iter) {
508 x = tmpw->frame_x + dx;
509 y = tmpw->frame_y + dy;
511 /* don't let windows become unreachable */
512 #if 1 /* XXX: was 0 in XINERAMA patch, check */
513 if (x + (int)tmpw->frame->core->width < 20)
514 x = 20 - (int)tmpw->frame->core->width;
515 else if (x + 20 > scr_width)
516 x = scr_width - 20;
518 if (y + (int)tmpw->frame->core->height < 20)
519 y = 20 - (int)tmpw->frame->core->height;
520 else if (y + 20 > scr_height)
521 y = scr_height - 20;
523 #else
524 wScreenBringInside(wwin->screen_ptr, &x, &y,
525 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
526 #endif
528 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
533 static void flushMotion(void)
535 XEvent ev;
537 XSync(dpy, False);
538 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
541 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
543 /* do not let window be unmapped */
544 if (opaque_move) {
545 wwin->flags.changing_workspace = 1;
546 wWindowChangeWorkspace(wwin, new_workspace);
548 /* go to new workspace */
549 wWorkspaceChange(scr, new_workspace);
551 wwin->flags.changing_workspace = 0;
553 if (rewind)
554 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
555 else
556 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
558 flushMotion();
560 if (!opaque_move) {
561 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
562 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
563 GrabModeAsync, None, wPreferences.cursor[WCUR_MOVE], CurrentTime);
567 typedef struct {
568 /* arrays of WWindows sorted by the respective border position */
569 WWindow **topList; /* top border */
570 WWindow **leftList; /* left border */
571 WWindow **rightList; /* right border */
572 WWindow **bottomList; /* bottom border */
573 int count;
575 /* index of window in the above lists indicating the relative position
576 * of the window with the others */
577 int topIndex;
578 int leftIndex;
579 int rightIndex;
580 int bottomIndex;
582 int rubCount; /* for workspace switching */
584 int winWidth, winHeight; /* width/height of the window */
585 int realX, realY; /* actual position of the window */
586 int calcX, calcY; /* calculated position of window */
587 int omouseX, omouseY; /* old mouse position */
588 int mouseX, mouseY; /* last known position of the pointer */
590 enum {
591 SNAP_NONE,
592 SNAP_LEFT,
593 SNAP_RIGHT,
594 SNAP_TOP,
595 SNAP_BOTTOM,
596 SNAP_TOPLEFT,
597 SNAP_TOPRIGHT,
598 SNAP_BOTTOMLEFT,
599 SNAP_BOTTOMRIGHT
600 } snap;
601 } MoveData;
603 #define WTOP(w) (w)->frame_y
604 #define WLEFT(w) (w)->frame_x
605 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
606 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
607 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
608 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
610 static int compareWTop(const void *a, const void *b)
612 WWindow *wwin1 = *(WWindow **) a;
613 WWindow *wwin2 = *(WWindow **) b;
615 if (WTOP(wwin1) > WTOP(wwin2))
616 return -1;
617 else if (WTOP(wwin1) < WTOP(wwin2))
618 return 1;
619 else
620 return 0;
623 static int compareWLeft(const void *a, const void *b)
625 WWindow *wwin1 = *(WWindow **) a;
626 WWindow *wwin2 = *(WWindow **) b;
628 if (WLEFT(wwin1) > WLEFT(wwin2))
629 return -1;
630 else if (WLEFT(wwin1) < WLEFT(wwin2))
631 return 1;
632 else
633 return 0;
636 static int compareWRight(const void *a, const void *b)
638 WWindow *wwin1 = *(WWindow **) a;
639 WWindow *wwin2 = *(WWindow **) b;
641 if (WRIGHT(wwin1) < WRIGHT(wwin2))
642 return -1;
643 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
644 return 1;
645 else
646 return 0;
649 static int compareWBottom(const void *a, const void *b)
651 WWindow *wwin1 = *(WWindow **) a;
652 WWindow *wwin2 = *(WWindow **) b;
654 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
655 return -1;
656 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
657 return 1;
658 else
659 return 0;
662 static void updateResistance(MoveData *data, int newX, int newY)
664 int i;
665 int newX2 = newX + data->winWidth;
666 int newY2 = newY + data->winHeight;
667 Bool ok = False;
669 if (newX < data->realX) {
670 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
671 ok = True;
672 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
673 ok = True;
675 } else if (newX > data->realX) {
676 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
677 ok = True;
678 } else if (data->rightIndex <= data->count - 1
679 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
680 ok = True;
684 if (!ok) {
685 if (newY < data->realY) {
686 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
687 ok = True;
688 } else if (data->topIndex <= data->count - 1
689 && newY2 <= WTOP(data->topList[data->topIndex])) {
690 ok = True;
692 } else if (newY > data->realY) {
693 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
694 ok = True;
695 } else if (data->bottomIndex <= data->count - 1
696 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
697 ok = True;
702 if (!ok)
703 return;
705 /* TODO: optimize this */
706 if (data->realY < WBOTTOM(data->bottomList[0])) {
707 data->bottomIndex = 0;
709 if (data->realX < WRIGHT(data->rightList[0])) {
710 data->rightIndex = 0;
712 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
713 data->leftIndex = 0;
715 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
716 data->topIndex = 0;
718 for (i = 0; i < data->count; i++) {
719 if (data->realY > WBOTTOM(data->bottomList[i])) {
720 data->bottomIndex = i + 1;
722 if (data->realX > WRIGHT(data->rightList[i])) {
723 data->rightIndex = i + 1;
725 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
726 data->leftIndex = i + 1;
728 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
729 data->topIndex = i + 1;
734 static void freeMoveData(MoveData * data)
736 if (data->topList)
737 wfree(data->topList);
738 if (data->leftList)
739 wfree(data->leftList);
740 if (data->rightList)
741 wfree(data->rightList);
742 if (data->bottomList)
743 wfree(data->bottomList);
746 static void updateMoveData(WWindow * wwin, MoveData * data)
748 WScreen *scr = wwin->screen_ptr;
749 WWindow *tmp;
750 int i;
752 data->count = 0;
753 tmp = scr->focused_window;
754 while (tmp) {
755 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
756 && !tmp->flags.miniaturized
757 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
758 data->topList[data->count] = tmp;
759 data->leftList[data->count] = tmp;
760 data->rightList[data->count] = tmp;
761 data->bottomList[data->count] = tmp;
762 data->count++;
764 tmp = tmp->prev;
767 if (data->count == 0) {
768 data->topIndex = 0;
769 data->leftIndex = 0;
770 data->rightIndex = 0;
771 data->bottomIndex = 0;
772 return;
775 /* order from closest to the border of the screen to farthest */
777 qsort(data->topList, data->count, sizeof(data->topList[0]), compareWTop);
778 qsort(data->leftList, data->count, sizeof(data->leftList[0]), compareWLeft);
779 qsort(data->rightList, data->count, sizeof(data->rightList[0]), compareWRight);
780 qsort(data->bottomList, data->count, sizeof(data->bottomList[0]), compareWBottom);
782 /* figure the position of the window relative to the others */
784 data->topIndex = -1;
785 data->leftIndex = -1;
786 data->rightIndex = -1;
787 data->bottomIndex = -1;
789 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
790 data->bottomIndex = 0;
792 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
793 data->rightIndex = 0;
795 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
796 data->leftIndex = 0;
798 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
799 data->topIndex = 0;
801 for (i = 0; i < data->count; i++) {
802 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
803 data->bottomIndex = i + 1;
805 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
806 data->rightIndex = i + 1;
808 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
809 data->leftIndex = i + 1;
811 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
812 data->topIndex = i + 1;
817 static void initMoveData(WWindow * wwin, MoveData * data)
819 int i;
820 WWindow *tmp;
822 memset(data, 0, sizeof(MoveData));
824 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
826 if (i > 1) {
827 data->topList = wmalloc(sizeof(WWindow *) * i);
828 data->leftList = wmalloc(sizeof(WWindow *) * i);
829 data->rightList = wmalloc(sizeof(WWindow *) * i);
830 data->bottomList = wmalloc(sizeof(WWindow *) * i);
832 updateMoveData(wwin, data);
835 data->realX = wwin->frame_x;
836 data->realY = wwin->frame_y;
837 data->calcX = wwin->frame_x;
838 data->calcY = wwin->frame_y;
840 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
841 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
843 data->snap = SNAP_NONE;
846 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
848 WScreen *scr = wwin->screen_ptr;
849 Bool changed = False;
851 if (data->mouseX <= 1) {
852 if (scr->current_workspace > 0) {
853 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1, True);
854 changed = True;
855 data->rubCount = 0;
856 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
857 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1, True);
858 changed = True;
859 data->rubCount = 0;
861 } else if (data->mouseX >= scr->scr_width - 2) {
862 if (scr->current_workspace == scr->workspace_count - 1) {
863 if (wPreferences.ws_cycle || scr->workspace_count == MAX_WORKSPACES) {
864 crossWorkspace(scr, wwin, opaqueMove, 0, False);
865 changed = True;
866 data->rubCount = 0;
868 /* if user insists on trying to go to next workspace even when
869 * it's already the last, create a new one */
870 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
872 /* detect user "rubbing" the window against the edge */
873 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
875 data->rubCount = -(data->rubCount + 1);
877 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
879 data->rubCount = -data->rubCount + 1;
882 /* create a new workspace */
883 if (abs(data->rubCount) > 2) {
884 /* go to next workspace */
885 wWorkspaceNew(scr);
887 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
888 changed = True;
889 data->rubCount = 0;
891 } else if (scr->current_workspace < scr->workspace_count) {
892 /* go to next workspace */
893 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace + 1, False);
894 changed = True;
895 data->rubCount = 0;
897 } else {
898 data->rubCount = 0;
901 return changed;
904 static void
905 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
906 Bool opaqueMove, int newMouseX, int newMouseY)
908 WScreen *scr = wwin->screen_ptr;
909 int dx, dy; /* how much mouse moved */
910 int winL, winR, winT, winB; /* requested new window position */
911 int newX, newY; /* actual new window position */
912 Bool hresist, vresist;
913 Bool attract;
915 hresist = False;
916 vresist = False;
918 /* check the direction of the movement */
919 dx = newMouseX - data->mouseX;
920 dy = newMouseY - data->mouseY;
922 data->omouseX = data->mouseX;
923 data->omouseY = data->mouseY;
924 data->mouseX = newMouseX;
925 data->mouseY = newMouseY;
927 winL = data->calcX + dx;
928 winR = data->calcX + data->winWidth + dx;
929 winT = data->calcY + dy;
930 winB = data->calcY + data->winHeight + dy;
932 newX = data->realX;
933 newY = data->realY;
935 if (doResistance) {
936 int l_edge, r_edge;
937 int edge_l, edge_r;
938 int t_edge, b_edge;
939 int edge_t, edge_b;
940 int resist;
942 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
943 attract = wPreferences.attract;
944 /* horizontal movement: check horizontal edge resistances */
945 if (dx || dy) {
946 WMRect rect;
947 int i, head;
948 /* window is the leftmost window: check against screen edge */
950 /* Add inter head resistance 1/2 (if needed) */
951 head = wGetHeadForPointerLocation(scr);
952 rect = wGetRectForHead(scr, head);
954 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
955 edge_l = l_edge - resist;
956 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
957 r_edge = edge_r + resist;
959 /* 1 */
960 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
961 WWindow *looprw;
963 for (i = data->rightIndex - 1; i >= 0; i--) {
964 looprw = data->rightList[i];
965 if (!(data->realY > WBOTTOM(looprw)
966 || (data->realY + data->winHeight) < WTOP(looprw))) {
967 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
968 l_edge = WRIGHT(looprw) + 1;
969 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
971 break;
975 if (attract) {
976 for (i = data->rightIndex; i < data->count; i++) {
977 looprw = data->rightList[i];
978 if (!(data->realY > WBOTTOM(looprw)
979 || (data->realY + data->winHeight) < WTOP(looprw))) {
980 r_edge = WRIGHT(looprw) + 1;
981 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
982 break;
988 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
989 WWindow *looprw;
991 for (i = data->leftIndex - 1; i >= 0; i--) {
992 looprw = data->leftList[i];
993 if (!(data->realY > WBOTTOM(looprw)
994 || (data->realY + data->winHeight) < WTOP(looprw))) {
995 if (attract
996 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
997 && dx > 0)) {
998 edge_r = WLEFT(looprw);
999 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1001 break;
1005 if (attract)
1006 for (i = data->leftIndex; i < data->count; i++) {
1007 looprw = data->leftList[i];
1008 if (!(data->realY > WBOTTOM(looprw)
1009 || (data->realY + data->winHeight) < WTOP(looprw))) {
1010 edge_l = WLEFT(looprw);
1011 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1012 break;
1018 printf("%d %d\n",winL,winR);
1019 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1022 if ((winL - l_edge) < (r_edge - winL)) {
1023 if (resist > 0) {
1024 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1025 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1026 newX = l_edge;
1027 hresist = True;
1030 } else {
1031 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1032 newX = r_edge;
1033 hresist = True;
1037 if ((winR - edge_l) < (edge_r - winR)) {
1038 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1039 newX = edge_l - data->winWidth;
1040 hresist = True;
1042 } else {
1043 if (resist > 0) {
1044 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1045 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1046 newX = edge_r - data->winWidth;
1047 hresist = True;
1052 /* VeRT */
1053 /* Add inter head resistance 2/2 (if needed) */
1054 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1055 edge_t = t_edge - resist;
1056 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1057 b_edge = edge_b + resist;
1059 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1060 WWindow *looprw;
1062 for (i = data->bottomIndex - 1; i >= 0; i--) {
1063 looprw = data->bottomList[i];
1064 if (!(data->realX > WRIGHT(looprw)
1065 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1066 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1067 t_edge = WBOTTOM(looprw) + 1;
1068 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1070 break;
1074 if (attract) {
1075 for (i = data->bottomIndex; i < data->count; i++) {
1076 looprw = data->bottomList[i];
1077 if (!(data->realX > WRIGHT(looprw)
1078 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1079 b_edge = WBOTTOM(looprw) + 1;
1080 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1081 break;
1087 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1088 WWindow *looprw;
1090 for (i = data->topIndex - 1; i >= 0; i--) {
1091 looprw = data->topList[i];
1092 if (!(data->realX > WRIGHT(looprw)
1093 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1094 if (attract
1095 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1096 && dy > 0)) {
1097 edge_b = WTOP(looprw);
1098 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1100 break;
1104 if (attract)
1105 for (i = data->topIndex; i < data->count; i++) {
1106 looprw = data->topList[i];
1107 if (!(data->realX > WRIGHT(looprw)
1108 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1109 edge_t = WTOP(looprw);
1110 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1111 break;
1116 if ((winT - t_edge) < (b_edge - winT)) {
1117 if (resist > 0) {
1118 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1119 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1120 newY = t_edge;
1121 vresist = True;
1124 } else {
1125 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1126 newY = b_edge;
1127 vresist = True;
1131 if ((winB - edge_t) < (edge_b - winB)) {
1132 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1133 newY = edge_t - data->winHeight;
1134 vresist = True;
1136 } else {
1137 if (resist > 0) {
1138 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1139 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1140 newY = edge_b - data->winHeight;
1141 vresist = True;
1146 /* END VeRT */
1150 /* update window position */
1151 data->calcX += dx;
1152 data->calcY += dy;
1154 if (((dx > 0 && data->calcX - data->realX > 0)
1155 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1156 newX = data->calcX;
1158 if (((dy > 0 && data->calcY - data->realY > 0)
1159 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1160 newY = data->calcY;
1162 if (data->realX != newX || data->realY != newY) {
1164 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1165 showPosition(wwin, data->realX, data->realY);
1167 if (opaqueMove) {
1168 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1169 } else {
1170 /* erase frames */
1171 drawFrames(wwin, scr->selected_windows,
1172 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1175 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1177 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1180 if (!opaqueMove) {
1181 /* draw frames */
1182 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1185 if (!scr->selected_windows) {
1186 showPosition(wwin, newX, newY);
1190 /* recalc relative window position */
1191 if (doResistance && (data->realX != newX || data->realY != newY)) {
1192 updateResistance(data, newX, newY);
1195 data->realX = newX;
1196 data->realY = newY;
1199 static void draw_snap_frame(WWindow *wwin, int direction)
1201 WScreen *scr;
1203 scr = wwin->screen_ptr;
1205 switch (direction) {
1206 case SNAP_LEFT:
1207 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height);
1208 break;
1210 case SNAP_RIGHT:
1211 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2, scr->scr_height);
1212 break;
1214 case SNAP_TOP:
1215 if (wPreferences.snap_to_top_maximizes_fullscreen)
1216 drawTransparentFrame(wwin, 0, 0, scr->scr_width, scr->scr_height);
1217 else
1218 drawTransparentFrame(wwin, 0, 0, scr->scr_width, scr->scr_height/2);
1219 break;
1221 case SNAP_BOTTOM:
1222 drawTransparentFrame(wwin, 0, scr->scr_height/2, scr->scr_width, scr->scr_height/2);
1223 break;
1225 case SNAP_TOPLEFT:
1226 drawTransparentFrame(wwin, 0, 0, scr->scr_width/2, scr->scr_height/2);
1227 break;
1229 case SNAP_TOPRIGHT:
1230 drawTransparentFrame(wwin, scr->scr_width/2, 0, scr->scr_width/2, scr->scr_height/2);
1231 break;
1233 case SNAP_BOTTOMLEFT:
1234 drawTransparentFrame(wwin, 0, scr->scr_height/2, scr->scr_width/2, scr->scr_height/2);
1235 break;
1237 case SNAP_BOTTOMRIGHT:
1238 drawTransparentFrame(wwin, scr->scr_width/2, scr->scr_height/2,
1239 scr->scr_width/2, scr->scr_height/2);
1240 break;
1244 static int get_snap_direction(WScreen *scr, int x, int y)
1246 int edge, corner;
1248 edge = wPreferences.snap_edge_detect;
1249 corner = wPreferences.snap_corner_detect;
1251 if (x < corner && y < corner)
1252 return SNAP_TOPLEFT;
1253 if (x < corner && y >= scr->scr_height - corner)
1254 return SNAP_BOTTOMLEFT;
1255 if (x < edge)
1256 return SNAP_LEFT;
1258 if (x >= scr->scr_width - corner && y < corner)
1259 return SNAP_TOPRIGHT;
1260 if (x >= scr->scr_width - corner && y >= scr->scr_height - corner)
1261 return SNAP_BOTTOMRIGHT;
1262 if (x >= scr->scr_width - edge)
1263 return SNAP_RIGHT;
1265 if (y < edge)
1266 return SNAP_TOP;
1267 if (y >= scr->scr_height - edge)
1268 return SNAP_BOTTOM;
1269 return SNAP_NONE;
1272 static void do_snap(WWindow *wwin, MoveData *data, Bool opaqueMove)
1274 int directions;
1275 WScreen *scr;
1277 directions = 0;
1278 scr = wwin->screen_ptr;
1280 /* erase frames */
1281 if (!opaqueMove)
1282 drawFrames(wwin, scr->selected_windows, data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1283 draw_snap_frame(wwin, data->snap);
1285 switch (data->snap) {
1286 case SNAP_NONE:
1287 return;
1288 case SNAP_LEFT:
1289 directions = MAX_VERTICAL | MAX_LEFTHALF;
1290 break;
1291 case SNAP_RIGHT:
1292 directions = MAX_VERTICAL | MAX_RIGHTHALF;
1293 break;
1294 case SNAP_TOP:
1295 if (wPreferences.snap_to_top_maximizes_fullscreen)
1296 directions = MAX_HORIZONTAL | MAX_VERTICAL;
1297 else
1298 directions = MAX_HORIZONTAL | MAX_TOPHALF;
1299 break;
1300 case SNAP_BOTTOM:
1301 directions = MAX_HORIZONTAL | MAX_BOTTOMHALF;
1302 break;
1303 case SNAP_TOPLEFT:
1304 directions = MAX_TOPHALF | MAX_LEFTHALF;
1305 break;
1306 case SNAP_TOPRIGHT:
1307 directions = MAX_TOPHALF | MAX_RIGHTHALF;
1308 break;
1309 case SNAP_BOTTOMLEFT:
1310 directions = MAX_BOTTOMHALF | MAX_LEFTHALF;
1311 break;
1312 case SNAP_BOTTOMRIGHT:
1313 directions = MAX_BOTTOMHALF | MAX_RIGHTHALF;
1314 break;
1317 if (directions)
1318 handleMaximize(wwin, directions);
1319 data->snap = SNAP_NONE;
1322 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1324 #define MOVABLE_BIT 0x01
1325 #define RESIZABLE_BIT 0x02
1327 int wKeyboardMoveResizeWindow(WWindow * wwin)
1329 WScreen *scr = wwin->screen_ptr;
1330 Window root = scr->root_win;
1331 XEvent event;
1332 int w = wwin->frame->core->width;
1333 int h = wwin->frame->core->height;
1334 int scr_width = wwin->screen_ptr->scr_width;
1335 int scr_height = wwin->screen_ptr->scr_height;
1336 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1337 int src_x = wwin->frame_x;
1338 int src_y = wwin->frame_y;
1339 int original_w = w;
1340 int original_h = h;
1341 int done, off_x, off_y, ww, wh;
1342 int kspeed = _KS;
1343 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1344 Time lastTime = 0;
1345 KeyCode shiftl, shiftr, ctrlmode;
1346 KeySym keysym = NoSymbol;
1347 int moment = 0;
1348 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1349 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1350 ? wGetHeadForWindow(wwin)
1351 : scr->xine_info.primary_head);
1353 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1354 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1355 ctrlmode = done = off_x = off_y = 0;
1357 if (modes == RESIZABLE_BIT) {
1358 ctrlmode = 1;
1361 XSync(dpy, False);
1362 wusleep(10000);
1363 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1365 if (!wwin->flags.selected) {
1366 wUnselectWindows(scr);
1368 XGrabServer(dpy);
1369 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1370 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1371 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime);
1375 if (!opaqueMoveResize) {
1376 if (wwin->flags.shaded || scr->selected_windows) {
1377 if (scr->selected_windows)
1378 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1379 else
1380 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1381 } else {
1382 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1385 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1386 mapPositionDisplay(wwin, src_x, src_y, w, h);
1389 ww = w;
1390 wh = h;
1391 while (1) {
1393 looper.ox=off_x;
1394 looper.oy=off_y;
1396 do {
1397 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1398 | ButtonPressMask | ExposureMask, &event);
1399 if (event.type == Expose) {
1400 WMHandleEvent(&event);
1402 } while (event.type == Expose);
1404 if (!opaqueMoveResize) {
1405 if (wwin->flags.shaded || scr->selected_windows) {
1406 if (scr->selected_windows)
1407 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1408 else
1409 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1410 /*** I HATE EDGE RESISTANCE - ]d ***/
1411 } else {
1412 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1416 if (ctrlmode)
1417 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1420 XUngrabServer(dpy);
1421 XSync(dpy, False);
1423 switch (event.type) {
1424 case KeyPress:
1425 /* accelerate */
1426 if (event.xkey.time - lastTime > 50) {
1427 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1428 } else {
1429 if (kspeed < 20) {
1430 kspeed++;
1433 if (kspeed < _KS)
1434 kspeed = _KS;
1435 lastTime = event.xkey.time;
1436 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1437 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1438 ctrlmode = 1;
1439 wUnselectWindows(scr);
1440 } else {
1441 ctrlmode = 0;
1444 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1445 if (ctrlmode)
1446 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1447 else
1448 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1449 } else {
1451 keysym = XLookupKeysym(&event.xkey, 0);
1452 switch (keysym) {
1453 case XK_Return:
1454 #ifdef XK_KP_Enter
1455 case XK_KP_Enter:
1456 #endif
1457 done = 2;
1458 break;
1459 case XK_Escape:
1460 done = 1;
1461 break;
1462 case XK_Up:
1463 #ifdef XK_KP_Up
1464 case XK_KP_Up:
1465 #endif
1466 case XK_k:
1467 if (ctrlmode) {
1468 if (moment != UP)
1469 h = wh;
1470 h -= kspeed;
1471 moment = UP;
1472 if (h < 1)
1473 h = 1;
1474 } else
1475 off_y -= kspeed;
1476 break;
1477 case XK_Down:
1478 #ifdef XK_KP_Down
1479 case XK_KP_Down:
1480 #endif
1481 case XK_j:
1482 if (ctrlmode) {
1483 if (moment != DOWN)
1484 h = wh;
1485 h += kspeed;
1486 moment = DOWN;
1487 } else
1488 off_y += kspeed;
1489 break;
1490 case XK_Left:
1491 #ifdef XK_KP_Left
1492 case XK_KP_Left:
1493 #endif
1494 case XK_h:
1495 if (ctrlmode) {
1496 if (moment != LEFT)
1497 w = ww;
1498 w -= kspeed;
1499 if (w < 1)
1500 w = 1;
1501 moment = LEFT;
1502 } else
1503 off_x -= kspeed;
1504 break;
1505 case XK_Right:
1506 #ifdef XK_KP_Right
1507 case XK_KP_Right:
1508 #endif
1509 case XK_l:
1510 if (ctrlmode) {
1511 if (moment != RIGHT)
1512 w = ww;
1513 w += kspeed;
1514 moment = RIGHT;
1515 } else
1516 off_x += kspeed;
1517 break;
1520 ww = w;
1521 wh = h;
1522 wh -= vert_border;
1523 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1524 wh += vert_border;
1526 if (wPreferences.ws_cycle) {
1527 if (src_x + off_x + ww < 20) {
1528 if (!scr->current_workspace)
1529 wWorkspaceChange(scr, scr->workspace_count - 1);
1530 else
1531 wWorkspaceChange(scr, scr->current_workspace - 1);
1533 off_x += scr_width;
1534 } else if (src_x + off_x + 20 > scr_width) {
1535 if (scr->current_workspace == scr->workspace_count - 1)
1536 wWorkspaceChange(scr, 0);
1537 else
1538 wWorkspaceChange(scr, scr->current_workspace + 1);
1540 off_x -= scr_width;
1542 } else {
1543 if (src_x + off_x + ww < 20)
1544 off_x = 20 - ww - src_x;
1545 else if (src_x + off_x + 20 > scr_width)
1546 off_x = scr_width - 20 - src_x;
1549 if (src_y + off_y + wh < 20) {
1550 off_y = 20 - wh - src_y;
1551 } else if (src_y + off_y + 20 > scr_height) {
1552 off_y = scr_height - 20 - src_y;
1555 break;
1556 case ButtonPress:
1557 case ButtonRelease:
1558 done = 1;
1559 break;
1560 case Expose:
1561 WMHandleEvent(&event);
1562 while (XCheckTypedEvent(dpy, Expose, &event)) {
1563 WMHandleEvent(&event);
1565 break;
1567 default:
1568 WMHandleEvent(&event);
1569 break;
1572 XGrabServer(dpy);
1573 /*xxx */
1575 if (wwin->flags.shaded && !scr->selected_windows) {
1576 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1577 } else {
1578 if (ctrlmode) {
1579 WMUnmapWidget(scr->gview);
1580 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1581 } else if (!scr->selected_windows) {
1582 WMUnmapWidget(scr->gview);
1583 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1587 if (!opaqueMoveResize) {
1588 if (wwin->flags.shaded || scr->selected_windows) {
1589 if (scr->selected_windows)
1590 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1591 else
1592 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1593 } else {
1594 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1598 if (ctrlmode) {
1599 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1601 } else if (!scr->selected_windows)
1602 showPosition(wwin, src_x + off_x, src_y + off_y);
1604 if (opaqueMoveResize) {
1605 XUngrabServer(dpy);
1606 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1609 if (done) {
1610 if (!opaqueMoveResize) { /* ctrlmode => resize */
1611 if (wwin->flags.shaded || scr->selected_windows) {
1612 if (scr->selected_windows)
1613 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1614 else
1615 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1616 } else {
1617 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1621 if (ctrlmode) {
1622 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1623 src_y + off_y + wh, 0);
1624 WMUnmapWidget(scr->gview);
1625 } else
1626 WMUnmapWidget(scr->gview);
1628 XUngrabKeyboard(dpy, CurrentTime);
1629 XUngrabPointer(dpy, CurrentTime);
1630 XUngrabServer(dpy);
1632 if (done == 2) {
1633 if (wwin->flags.shaded || scr->selected_windows) {
1634 if (!scr->selected_windows) {
1635 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1636 wWindowSynthConfigureNotify(wwin);
1637 } else {
1638 WMArrayIterator iter;
1639 WWindow *foo;
1641 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1643 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1644 wWindowSynthConfigureNotify(foo);
1647 } else {
1648 if (ww != original_w)
1649 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
1651 if (wh != original_h)
1652 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1654 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1655 wWindowSynthConfigureNotify(wwin);
1657 wWindowChangeWorkspace(wwin, scr->current_workspace);
1658 wSetFocusTo(scr, wwin);
1661 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1662 head != wGetHeadForWindow(wwin)) {
1663 wArrangeIcons(scr, True);
1666 update_saved_geometry(wwin);
1668 return 1;
1674 *----------------------------------------------------------------------
1675 * wMouseMoveWindow--
1676 * Move the named window and the other selected ones (if any),
1677 * interactively. Also shows the position of the window, if only one
1678 * window is being moved.
1679 * If the window is not on the selected window list, the selected
1680 * windows are deselected.
1681 * If shift is pressed during the operation, the position display
1682 * is changed to another type.
1684 * Returns:
1685 * True if the window was moved, False otherwise.
1687 * Side effects:
1688 * The window(s) position is changed, and the client(s) are
1689 * notified about that.
1690 * The position display configuration may be changed.
1691 *----------------------------------------------------------------------
1693 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1695 WScreen *scr = wwin->screen_ptr;
1696 XEvent event;
1697 Window root = scr->root_win;
1698 KeyCode shiftl, shiftr;
1699 Bool done = False;
1700 int started = 0;
1701 int warped = 0;
1702 /* This needs not to change while moving, else bad things can happen */
1703 int opaqueMove = wPreferences.opaque_move;
1704 MoveData moveData;
1705 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1706 ? wGetHeadForWindow(wwin)
1707 : scr->xine_info.primary_head);
1709 if (!IS_MOVABLE(wwin))
1710 return False;
1712 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1713 XSetWindowAttributes attr;
1715 attr.save_under = True;
1716 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1719 initMoveData(wwin, &moveData);
1721 moveData.mouseX = ev->xmotion.x_root;
1722 moveData.mouseY = ev->xmotion.y_root;
1724 if (!wwin->flags.selected) {
1725 /* this window is not selected, unselect others and move only wwin */
1726 wUnselectWindows(scr);
1728 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1729 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1730 while (!done) {
1731 if (warped) {
1732 int junk;
1733 Window junkw;
1735 /* XWarpPointer() doesn't seem to generate Motion events, so
1736 * we've got to simulate them */
1737 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1738 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1739 } else {
1740 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1741 | PointerMotionHintMask
1742 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1744 if (event.type == MotionNotify) {
1745 /* compress MotionNotify events */
1746 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1747 if (!checkMouseSamplingRate(&event))
1748 continue;
1751 switch (event.type) {
1752 case KeyPress:
1753 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1754 && started && !scr->selected_windows) {
1756 if (!opaqueMove) {
1757 drawFrames(wwin, scr->selected_windows,
1758 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1761 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1762 showPosition(wwin, moveData.realX, moveData.realY);
1763 XUngrabServer(dpy);
1765 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1766 moveData.winWidth, moveData.winHeight);
1768 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1769 XGrabServer(dpy);
1770 showPosition(wwin, moveData.realX, moveData.realY);
1773 if (!opaqueMove) {
1774 drawFrames(wwin, scr->selected_windows,
1775 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1777 /*} else {
1778 WMHandleEvent(&event); this causes problems needs fixing */
1780 break;
1782 case MotionNotify:
1783 if (IS_RESIZABLE(wwin) && wPreferences.window_snapping) {
1784 int snap_direction;
1786 snap_direction = get_snap_direction(scr, moveData.mouseX, moveData.mouseY);
1788 if (!wPreferences.no_autowrap &&
1789 snap_direction != SNAP_TOP &&
1790 snap_direction != SNAP_BOTTOM)
1791 snap_direction = SNAP_NONE;
1793 if (moveData.snap != snap_direction) {
1794 /* erase old frame */
1795 if (moveData.snap)
1796 draw_snap_frame(wwin, moveData.snap);
1797 /* draw new frame */
1798 if (snap_direction)
1799 draw_snap_frame(wwin, snap_direction);
1800 moveData.snap = snap_direction;
1804 if (started) {
1805 /* erase snap frame */
1806 if (moveData.snap)
1807 draw_snap_frame(wwin, moveData.snap);
1809 updateWindowPosition(wwin, &moveData,
1810 scr->selected_windows == NULL
1811 && wPreferences.edge_resistance > 0,
1812 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1814 /* redraw snap frame */
1815 if (moveData.snap)
1816 draw_snap_frame(wwin, moveData.snap);
1818 if (!warped && !wPreferences.no_autowrap) {
1819 int oldWorkspace = scr->current_workspace;
1821 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1822 showPosition(wwin, moveData.realX, moveData.realY);
1823 XUngrabServer(dpy);
1825 if (!opaqueMove) {
1826 drawFrames(wwin, scr->selected_windows,
1827 moveData.realX - wwin->frame_x,
1828 moveData.realY - wwin->frame_y);
1830 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1831 if (scr->current_workspace != oldWorkspace
1832 && wPreferences.edge_resistance > 0
1833 && scr->selected_windows == NULL)
1834 updateMoveData(wwin, &moveData);
1835 warped = 1;
1837 if (!opaqueMove) {
1838 drawFrames(wwin, scr->selected_windows,
1839 moveData.realX - wwin->frame_x,
1840 moveData.realY - wwin->frame_y);
1842 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1843 XSync(dpy, False);
1844 showPosition(wwin, moveData.realX, moveData.realY);
1845 XGrabServer(dpy);
1847 } else {
1848 warped = 0;
1850 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1851 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1853 if (wwin->flags.maximized) {
1854 if (wPreferences.drag_maximized_window == DRAGMAX_RESTORE) {
1855 float titlebar_ratio;
1856 int new_x, new_y;
1858 titlebar_ratio = (moveData.mouseX - wwin->frame_x) /
1859 (float)wwin->frame->core->width;
1860 new_y = wwin->frame_y;
1861 wUnmaximizeWindow(wwin);
1862 new_x = moveData.mouseX - titlebar_ratio * wwin->frame->core->width;
1863 wWindowMove(wwin, new_x, new_y);
1864 moveData.realX = moveData.calcX = wwin->frame_x;
1865 moveData.realY = moveData.calcY = wwin->frame_y;
1867 if (wPreferences.drag_maximized_window == DRAGMAX_UNMAXIMIZE) {
1868 wwin->flags.maximized = 0;
1869 wwin->flags.old_maximized = 0;
1873 XChangeActivePointerGrab(dpy, ButtonMotionMask
1874 | ButtonReleaseMask | ButtonPressMask,
1875 wPreferences.cursor[WCUR_MOVE], CurrentTime);
1876 started = 1;
1877 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1879 if (!scr->selected_windows)
1880 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1881 moveData.winWidth, moveData.winHeight);
1883 if (started && !opaqueMove)
1884 drawFrames(wwin, scr->selected_windows, 0, 0);
1886 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1887 && !scr->selected_windows)) {
1888 XGrabServer(dpy);
1889 if (wPreferences.move_display == WDIS_NEW)
1890 showPosition(wwin, moveData.realX, moveData.realY);
1894 break;
1896 case ButtonPress:
1897 break;
1899 case ButtonRelease:
1900 if (event.xbutton.button != ev->xbutton.button)
1901 break;
1903 if (started) {
1904 XEvent e;
1906 if (moveData.snap)
1907 do_snap(wwin, &moveData, opaqueMove);
1908 else if (!opaqueMove) {
1909 drawFrames(wwin, scr->selected_windows,
1910 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1911 XSync(dpy, 0);
1912 doWindowMove(wwin, scr->selected_windows,
1913 moveData.realX - wwin->frame_x,
1914 moveData.realY - wwin->frame_y);
1916 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1917 wWindowSynthConfigureNotify(wwin);
1918 #endif
1919 XUngrabKeyboard(dpy, CurrentTime);
1920 XUngrabServer(dpy);
1921 if (!opaqueMove) {
1922 wWindowChangeWorkspace(wwin, scr->current_workspace);
1923 wSetFocusTo(scr, wwin);
1925 if (wPreferences.move_display == WDIS_NEW)
1926 showPosition(wwin, moveData.realX, moveData.realY);
1928 /* discard all enter/leave events that happened until
1929 * the time the button was released */
1930 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1931 if (e.xcrossing.time > event.xbutton.time) {
1932 XPutBackEvent(dpy, &e);
1933 break;
1936 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1937 if (e.xcrossing.time > event.xbutton.time) {
1938 XPutBackEvent(dpy, &e);
1939 break;
1943 if (!scr->selected_windows) {
1944 /* get rid of the geometry window */
1945 WMUnmapWidget(scr->gview);
1948 done = True;
1949 break;
1951 default:
1952 if (started && !opaqueMove) {
1953 drawFrames(wwin, scr->selected_windows,
1954 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1955 XUngrabServer(dpy);
1956 WMHandleEvent(&event);
1957 XSync(dpy, False);
1958 XGrabServer(dpy);
1959 drawFrames(wwin, scr->selected_windows,
1960 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1961 } else {
1962 WMHandleEvent(&event);
1964 break;
1968 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1969 XSetWindowAttributes attr;
1971 attr.save_under = False;
1972 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1976 freeMoveData(&moveData);
1978 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1979 head != wGetHeadForWindow(wwin)) {
1980 wArrangeIcons(scr, True);
1983 if (started)
1984 update_saved_geometry(wwin);
1986 return started;
1989 #define RESIZEBAR 1
1990 #define HCONSTRAIN 2
1992 static int getResizeDirection(WWindow * wwin, int x, int y, int dy, int flags)
1994 int w = wwin->frame->core->width - 1;
1995 int cw = wwin->frame->resizebar_corner_width;
1996 int dir;
1998 /* if not resizing through the resizebar */
1999 if (!(flags & RESIZEBAR)) {
2001 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
2002 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
2004 /* How much resize space is allowed */
2005 int spacew = abs(wwin->client.width / 3);
2006 int spaceh = abs(wwin->client.height / 3);
2008 /* Determine where x fits */
2009 if ((abs(x) > wwin->client.width/2 - spacew/2) &&
2010 (abs(x) < wwin->client.width/2 + spacew/2)) {
2011 /* Resize vertically */
2012 xdir = 0;
2013 } else if ((abs(y) > wwin->client.height/2 - spaceh/2) &&
2014 (abs(y) < wwin->client.height/2 + spaceh/2)) {
2015 /* Resize horizontally */
2016 ydir = 0;
2018 return (xdir | ydir);
2021 /* window is too narrow. allow diagonal resize */
2022 if (cw * 2 >= w) {
2023 int ydir;
2025 if (flags & HCONSTRAIN)
2026 ydir = 0;
2027 else
2028 ydir = DOWN;
2029 if (x < cw)
2030 return (LEFT | ydir);
2031 else
2032 return (RIGHT | ydir);
2034 /* vertical resize */
2035 if ((x > cw) && (x < w - cw))
2036 return DOWN;
2038 if (x < cw)
2039 dir = LEFT;
2040 else
2041 dir = RIGHT;
2043 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
2044 dir |= DOWN;
2046 return dir;
2049 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
2051 XEvent event;
2052 WScreen *scr = wwin->screen_ptr;
2053 Window root = scr->root_win;
2054 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
2055 int fw = wwin->frame->core->width;
2056 int fh = wwin->frame->core->height;
2057 int fx = wwin->frame_x;
2058 int fy = wwin->frame_y;
2059 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
2060 int orig_x, orig_y;
2061 int started;
2062 int dw, dh;
2063 int rw = fw, rh = fh;
2064 int rx1, ry1, rx2, ry2;
2065 int res = 0;
2066 KeyCode shiftl, shiftr;
2067 int orig_fx = fx;
2068 int orig_fy = fy;
2069 int orig_fw = fw;
2070 int orig_fh = fh;
2071 int original_fw = fw;
2072 int original_fh = fh;
2073 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
2074 ? wGetHeadForWindow(wwin)
2075 : scr->xine_info.primary_head);
2076 int opaqueResize = wPreferences.opaque_resize;
2078 if (!IS_RESIZABLE(wwin))
2079 return;
2081 if (wwin->flags.shaded) {
2082 wwarning("internal error: tryein");
2083 return;
2085 orig_x = ev->xbutton.x_root;
2086 orig_y = ev->xbutton.y_root;
2088 started = 0;
2089 wUnselectWindows(scr);
2090 rx1 = fx;
2091 rx2 = fx + fw - 1;
2092 ry1 = fy;
2093 ry2 = fy + fh - 1;
2094 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2095 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2097 while (1) {
2098 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
2099 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
2100 if (!checkMouseSamplingRate(&event))
2101 continue;
2103 switch (event.type) {
2104 case KeyPress:
2105 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2106 if (!opaqueResize) {
2107 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
2108 && started) {
2109 drawTransparentFrame(wwin, fx, fy, fw, fh);
2110 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
2111 drawTransparentFrame(wwin, fx, fy, fw, fh);
2114 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2115 break;
2117 case MotionNotify:
2118 if (started) {
2119 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
2121 dw = 0;
2122 dh = 0;
2124 orig_fx = fx;
2125 orig_fy = fy;
2126 orig_fw = fw;
2127 orig_fh = fh;
2129 if (res & LEFT)
2130 dw = orig_x - event.xmotion.x_root;
2131 else if (res & RIGHT)
2132 dw = event.xmotion.x_root - orig_x;
2133 if (res & UP)
2134 dh = orig_y - event.xmotion.y_root;
2135 else if (res & DOWN)
2136 dh = event.xmotion.y_root - orig_y;
2138 orig_x = event.xmotion.x_root;
2139 orig_y = event.xmotion.y_root;
2141 rw += dw;
2142 rh += dh;
2143 fw = rw;
2144 fh = rh - vert_border;
2145 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
2146 fh += vert_border;
2147 if (res & LEFT)
2148 fx = rx2 - fw + 1;
2149 else if (res & RIGHT)
2150 fx = rx1;
2151 if (res & UP)
2152 fy = ry2 - fh + 1;
2153 else if (res & DOWN)
2154 fy = ry1;
2155 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
2156 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
2157 int tx, ty;
2158 Window junkw;
2159 int flags;
2161 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
2162 orig_x, orig_y, &tx, &ty, &junkw);
2164 /* check if resizing through resizebar */
2165 if (is_resizebar)
2166 flags = RESIZEBAR;
2167 else
2168 flags = 0;
2170 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
2171 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
2172 flags |= HCONSTRAIN;
2174 res = getResizeDirection(wwin, tx, ty, orig_y - event.xmotion.y_root, flags);
2176 if (res == (UP | LEFT))
2177 XChangeActivePointerGrab(dpy, ButtonMotionMask
2178 | ButtonReleaseMask | ButtonPressMask,
2179 wPreferences.cursor[WCUR_TOPLEFTRESIZE], CurrentTime);
2180 else if (res == (UP | RIGHT))
2181 XChangeActivePointerGrab(dpy, ButtonMotionMask
2182 | ButtonReleaseMask | ButtonPressMask,
2183 wPreferences.cursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
2184 else if (res == (DOWN | LEFT))
2185 XChangeActivePointerGrab(dpy, ButtonMotionMask
2186 | ButtonReleaseMask | ButtonPressMask,
2187 wPreferences.cursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2188 else if (res == (DOWN | RIGHT))
2189 XChangeActivePointerGrab(dpy, ButtonMotionMask
2190 | ButtonReleaseMask | ButtonPressMask,
2191 wPreferences.cursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2192 else if (res == DOWN || res == UP)
2193 XChangeActivePointerGrab(dpy, ButtonMotionMask
2194 | ButtonReleaseMask | ButtonPressMask,
2195 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2196 else if (res & (DOWN | UP))
2197 XChangeActivePointerGrab(dpy, ButtonMotionMask
2198 | ButtonReleaseMask | ButtonPressMask,
2199 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2200 else if (res & (LEFT | RIGHT))
2201 XChangeActivePointerGrab(dpy, ButtonMotionMask
2202 | ButtonReleaseMask | ButtonPressMask,
2203 wPreferences.cursor[WCUR_HORIZONRESIZE], CurrentTime);
2205 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2207 XGrabServer(dpy);
2209 /* Draw the resize frame for the first time. */
2210 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2212 if (!opaqueResize)
2213 drawTransparentFrame(wwin, fx, fy, fw, fh);
2215 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2217 started = 1;
2219 if (started) {
2220 if (!opaqueResize)
2221 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2223 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2224 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2226 if (!opaqueResize)
2227 drawTransparentFrame(wwin, fx, fy, fw, fh);
2229 if (fh != orig_fh || fw != orig_fw) {
2230 if (wPreferences.size_display == WDIS_NEW)
2231 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2232 orig_fy + orig_fh, res);
2234 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2237 if (opaqueResize) {
2238 /* Fist clean the geometry line */
2239 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2240 /* Now, continue drawing */
2241 XUngrabServer(dpy);
2242 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2243 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2244 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2247 break;
2249 case ButtonPress:
2250 break;
2252 case ButtonRelease:
2253 if (event.xbutton.button != ev->xbutton.button)
2254 break;
2256 if (started) {
2257 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2259 if (!opaqueResize)
2260 drawTransparentFrame(wwin, fx, fy, fw, fh);
2262 XUngrabKeyboard(dpy, CurrentTime);
2263 WMUnmapWidget(scr->gview);
2264 XUngrabServer(dpy);
2266 if (fw != original_fw)
2267 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
2269 if (fh != original_fh)
2270 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2272 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2273 wWindowSynthConfigureNotify(wwin);
2275 return;
2277 default:
2278 WMHandleEvent(&event);
2282 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2283 wArrangeIcons(scr, True);
2286 #undef LEFT
2287 #undef RIGHT
2288 #undef UP
2289 #undef DOWN
2290 #undef HCONSTRAIN
2291 #undef RESIZEBAR
2293 void wUnselectWindows(WScreen * scr)
2295 WWindow *wwin;
2297 if (!scr->selected_windows)
2298 return;
2300 while (WMGetArrayItemCount(scr->selected_windows)) {
2301 wwin = WMGetFromArray(scr->selected_windows, 0);
2302 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2303 wIconSelect(wwin->icon);
2305 wSelectWindow(wwin, False);
2307 WMFreeArray(scr->selected_windows);
2308 scr->selected_windows = NULL;
2311 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2313 WWindow *tmpw;
2315 /* select the windows and put them in the selected window list */
2316 tmpw = scr->focused_window;
2317 while (tmpw != NULL) {
2318 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2319 if ((tmpw->frame->workspace == scr->current_workspace || IS_OMNIPRESENT(tmpw))
2320 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2321 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2322 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2323 wSelectWindow(tmpw, True);
2326 tmpw = tmpw->prev;
2330 void wSelectWindows(WScreen * scr, XEvent * ev)
2332 XEvent event;
2333 Window root = scr->root_win;
2334 GC gc = scr->frame_gc;
2335 int xp = ev->xbutton.x_root;
2336 int yp = ev->xbutton.y_root;
2337 int w = 0, h = 0;
2338 int x = xp, y = yp;
2340 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2341 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2342 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2343 return;
2345 XGrabServer(dpy);
2347 wUnselectWindows(scr);
2349 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2350 while (1) {
2351 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2353 switch (event.type) {
2354 case MotionNotify:
2355 XDrawRectangle(dpy, root, gc, x, y, w, h);
2356 x = event.xmotion.x_root;
2357 if (x < xp) {
2358 w = xp - x;
2359 } else {
2360 w = x - xp;
2361 x = xp;
2363 y = event.xmotion.y_root;
2364 if (y < yp) {
2365 h = yp - y;
2366 } else {
2367 h = y - yp;
2368 y = yp;
2370 XDrawRectangle(dpy, root, gc, x, y, w, h);
2371 break;
2373 case ButtonPress:
2374 break;
2376 case ButtonRelease:
2377 if (event.xbutton.button != ev->xbutton.button)
2378 break;
2380 XDrawRectangle(dpy, root, gc, x, y, w, h);
2381 XUngrabServer(dpy);
2382 XUngrabPointer(dpy, CurrentTime);
2383 selectWindowsInside(scr, x, y, x + w, y + h);
2384 return;
2386 default:
2387 WMHandleEvent(&event);
2388 break;
2393 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2395 WScreen *scr = wwin->screen_ptr;
2396 Window root = scr->root_win;
2397 int x, y, h = 0;
2398 XEvent event;
2399 KeyCode shiftl, shiftr;
2400 Window junkw;
2401 int junk;
2403 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2404 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2405 *x_ret = 0;
2406 *y_ret = 0;
2407 return;
2409 if (HAS_TITLEBAR(wwin)) {
2410 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2411 TITLEBAR_EXTEND_SPACE) * 2;
2413 if (h > wPreferences.window_title_max_height)
2414 h = wPreferences.window_title_max_height;
2416 if (h < wPreferences.window_title_min_height)
2417 h = wPreferences.window_title_min_height;
2419 height += h;
2421 if (HAS_RESIZEBAR(wwin)) {
2422 height += RESIZEBAR_HEIGHT;
2424 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2425 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2426 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2428 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2430 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2431 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2432 while (1) {
2433 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2435 if (!checkMouseSamplingRate(&event))
2436 continue;
2438 switch (event.type) {
2439 case KeyPress:
2440 if ((event.xkey.keycode == shiftl)
2441 || (event.xkey.keycode == shiftr)) {
2442 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2443 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2444 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2446 break;
2448 case MotionNotify:
2449 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2451 x = event.xmotion.x_root;
2452 y = event.xmotion.y_root;
2454 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2455 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2457 showPosition(wwin, x - width / 2, y - h / 2);
2459 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2461 break;
2463 case ButtonPress:
2464 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2465 XSync(dpy, 0);
2466 *x_ret = x - width / 2;
2467 *y_ret = y - h / 2;
2468 XUngrabPointer(dpy, CurrentTime);
2469 XUngrabKeyboard(dpy, CurrentTime);
2470 /* get rid of the geometry window */
2471 WMUnmapWidget(scr->gview);
2472 return;
2474 default:
2475 WMHandleEvent(&event);
2476 break;