2 // AIWindowDraggingView.m
5 // Created by Evan Schoenberg on 3/6/06.
8 #import "AIWindowDraggingView.h"
11 * @class AIWindowDraggingView
12 * @brief This NSView subclass makes the window move when it is dragged by the view itself (but not by subviews).
14 * The code is, for reference, a stripped down version of the code powering AIBorderlessWindow's dragging movements.
16 * On mouse down, the window's frame is noted; deltas as the mouse moves are used to determine the window's
19 @implementation AIWindowDraggingView
21 * @brief Mouse dragged
23 - (void)mouseDragged:(NSEvent *)theEvent
25 NSWindow *window = [self window];
26 NSPoint currentLocation, newOrigin;
27 NSRect newWindowFrame;
29 /* If we get here and aren't yet in a left mouse event, which can happen if the user began dragging while
30 * a contextual menu is showing, start off from the right position by getting our originalMouseLocation.
32 if (!inLeftMouseEvent) {
33 //Grab the mouse location in global coordinates
34 originalMouseLocation = [window convertBaseToScreen:[theEvent locationInWindow]];
35 windowFrame = [window frame];
36 inLeftMouseEvent = YES;
39 newOrigin = windowFrame.origin;
40 newWindowFrame = windowFrame;
42 //Grab the current mouse location to compare with the location of the mouse when the drag started (stored in mouseDown:)
43 currentLocation = [NSEvent mouseLocation];
44 newOrigin.x += (currentLocation.x - originalMouseLocation.x);
45 newOrigin.y += currentLocation.y - originalMouseLocation.y;
47 newWindowFrame.origin = newOrigin;
49 [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowWillMoveNotification object:window];
50 [window setFrameOrigin:newWindowFrame.origin];
51 [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowDidMoveNotification object:window];
57 * We start tracking the a drag operation here when the user first clicks the mouse without command presed
58 * to establish the initial location.
60 - (void)mouseDown:(NSEvent *)theEvent
62 NSWindow *window = [self window];
64 //grab the mouse location in global coordinates
65 originalMouseLocation = [window convertBaseToScreen:[theEvent locationInWindow]];
66 windowFrame = [window frame];
67 inLeftMouseEvent = YES;
73 - (void)mouseUp:(NSEvent *)theEvent
75 inLeftMouseEvent = NO;