Can I move window using an object inside that window?

1.8k views Asked by At

Is it possible in Cocoa to move a window by dragging an object which is inside that window? For example: I have a webview inside a window, big as the window, so setMovableByWindowBackground will obviously not work. Is there a way to click and drag the web view and move the whole window?

1

There are 1 answers

1
kubi On BEST ANSWER

Sure, you've just got to track the mouse movements using mouseDragged. Something similar to this should work:

- (void)mouseDragged:(NSEvent *)theEvent
{
   NSPoint currentLocation;
   NSPoint newOrigin;

   NSRect  screenFrame = [[NSScreen mainScreen] frame];
   NSRect  windowFrame = [self frame];

    currentLocation = [NSEvent mouseLocation];
    newOrigin.x = currentLocation.x - initialLocation.x;
    newOrigin.y = currentLocation.y - initialLocation.y;

    // Don't let window get dragged up under the menu bar
    if( (newOrigin.y+windowFrame.size.height) > (screenFrame.origin.y+screenFrame.size.height) ){
        newOrigin.y=screenFrame.origin.y + (screenFrame.size.height-windowFrame.size.height);
    }

    //go ahead and move the window to the new location
    [self setFrameOrigin:newOrigin];
}

Which I got from here: http://www.cocoadev.com/index.pl?SetMovableByWindowBackground