Donovan
Donovan

Reputation: 6122

Can I move window using an object inside that window?

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?

Upvotes: 3

Views: 1806

Answers (1)

kubi
kubi

Reputation: 49364

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

Upvotes: 5

Related Questions