views:

76

answers:

1

Hi all.

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?

Any help would be very appreciated!

—Albé

+1  A: 

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

kubi
Thank you! It seems that WebView is the only object which cannot respond to mouseDragged and mouseDown events.
Alberto