+
+void BlackboxWindow::beginMove(int x_root, int y_root) {
+ assert(! (flags.resizing || flags.moving));
+
+ /*
+ Only one window can be moved/resized at a time. If another window is already
+ being moved or resized, then stop it before whating to work with this one.
+ */
+ BlackboxWindow *changing = blackbox->getChangingWindow();
+ if (changing && changing != this) {
+ if (changing->flags.moving)
+ changing->endMove();
+ else // if (changing->flags.resizing)
+ changing->endResize();
+ }
+
+ XGrabPointer(blackbox->getXDisplay(), frame.window, False,
+ PointerMotionMask | ButtonReleaseMask,
+ GrabModeAsync, GrabModeAsync,
+ None, blackbox->getMoveCursor(), CurrentTime);
+
+ if (windowmenu && windowmenu->isVisible())
+ windowmenu->hide();
+
+ flags.moving = True;
+ blackbox->setChangingWindow(this);
+
+ if (! screen->doOpaqueMove()) {
+ XGrabServer(blackbox->getXDisplay());
+
+ frame.changing = frame.rect;
+ screen->showPosition(frame.changing.x(), frame.changing.y());
+
+ XDrawRectangle(blackbox->getXDisplay(), screen->getRootWindow(),
+ screen->getOpGC(),
+ frame.changing.x(),
+ frame.changing.y(),
+ frame.changing.width() - 1,
+ frame.changing.height() - 1);
+ }
+
+ frame.grab_x = x_root - frame.rect.x() - frame.border_w;
+ frame.grab_y = y_root - frame.rect.y() - frame.border_w;
+}
+
+
+void BlackboxWindow::doMove(int x_root, int y_root) {
+ assert(flags.moving);
+ assert(blackbox->getChangingWindow() == this);
+
+ int dx = x_root - frame.grab_x, dy = y_root - frame.grab_y;
+ dx -= frame.border_w;
+ dy -= frame.border_w;
+
+ const int snap_distance = screen->getEdgeSnapThreshold();
+
+ if (snap_distance) {
+ // window corners
+ const int wleft = dx,
+ wright = dx + frame.rect.width() - 1,
+ wtop = dy,
+ wbottom = dy + frame.rect.height() - 1;
+
+ if (screen->getWindowToWindowSnap()) {
+ Workspace *w = screen->getWorkspace(getWorkspaceNumber());
+ assert(w);
+
+ // try snap to another window
+ for (unsigned int i = 0, c = w->getCount(); i < c; ++i) {
+ BlackboxWindow *snapwin = w->getWindow(i);
+ if (snapwin == this)
+ continue; // don't snap to self
+
+ bool snapped = False;
+
+ const Rect &winrect = snapwin->frameRect();
+ int dleft = std::abs(wright - winrect.left()),
+ dright = std::abs(wleft - winrect.right()),
+ dtop = std::abs(wbottom - winrect.top()),
+ dbottom = std::abs(wtop - winrect.bottom());
+
+ if (wtop >= (signed)(winrect.y() - frame.rect.height() + 1) &&
+ wtop < (signed)(winrect.y() + winrect.height() - 1)) {
+
+ // snap left of other window?
+ if (dleft < snap_distance && dleft <= dright) {
+ dx = winrect.left() - frame.rect.width();
+ snapped = True;
+ }
+ // snap right of other window?
+ else if (dright < snap_distance) {
+ dx = winrect.right() + 1;
+ snapped = True;
+ }
+
+ if (snapped) {
+ if (screen->getWindowCornerSnap()) {
+ // try corner-snap to its other sides
+ dtop = std::abs(wtop - winrect.top());
+ dbottom = std::abs(wbottom - winrect.bottom());
+ if (dtop < snap_distance && dtop <= dbottom)
+ dy = winrect.top();
+ else if (dbottom < snap_distance)
+ dy = winrect.bottom() - frame.rect.height() + 1;
+ }
+
+ continue;
+ }
+ }
+
+ if (wleft >= (signed)(winrect.x() - frame.rect.width() + 1) &&
+ wleft < (signed)(winrect.x() + winrect.width() - 1)) {
+
+ // snap top of other window?
+ if (dtop < snap_distance && dtop <= dbottom) {
+ dy = winrect.top() - frame.rect.height();
+ snapped = True;
+ }
+ // snap bottom of other window?
+ else if (dbottom < snap_distance) {
+ dy = winrect.bottom() + 1;
+ snapped = True;
+ }
+
+ if (snapped) {
+ if (screen->getWindowCornerSnap()) {
+ // try corner-snap to its other sides
+ dleft = std::abs(wleft - winrect.left());
+ dright = std::abs(wright - winrect.right());
+ if (dleft < snap_distance && dleft <= dright)
+ dx = winrect.left();
+ else if (dright < snap_distance)
+ dx = winrect.right() - frame.rect.width() + 1;
+ }
+
+ continue;
+ }
+ }
+ }