One Quickie

Performing relative (grab-hand) scrolling (NSView->General)
Grab-hand scrolling is a handy feature in several apps. It's nice being able to scroll around a large document diagonally, fer instance. Here's one way to do it (assuming you have a standard NSScrollView -> NSClipView -> YourView setup)

@interface Blah : NSView
    NSPoint grabOrigin;
    NSPoint scrollOrigin;
@end // Blah


@implementation Blah


- (void) mouseDown: (NSEvent *) event
    // deal in window coordinates.  there is a scrolling problem
    // if using view coordinates because view coordinates
    // can get transformed
    grabOrigin = [event locationInWindow];

    NSClipView *contentView;
    contentView = (NSClipView*)[layerView superview];

    scrollOrigin = [contentView bounds].origin;

} // mouseDown

- (void) mouseDragged: (NSEvent *) event
    NSPoint mousePoint;
    mousePoint = [event locationInWindow];

    float deltaX, deltaY;
    deltaX = grabOrigin.x - mousePoint.x;
    deltaY = mousePoint.y - grabOrigin.y;

    NSPoint newOrigin;
    newOrigin = NSMakePoint (scrollOrigin.x + deltaX,
                             scrollOrigin.y + deltaY);

    [layerView scrollPoint: newOrigin];

} // mouseDragged

@end // Blah
You can be fancy and check for the option key by look at [event modifierFlags] and looking for NSAlternateKeyMask, and also use the NSCursor open/closedHandCursor.

borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book