Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

infernooo

macrumors newbie
Original poster
Oct 14, 2005
2
0
Hi everyone,

Firstly I wanted to say thank you to all the contributors who have provided good advice for myself and others to use, while some people might not be thankful, I assure you that there are many of us out there who really do appreciate the help given.

I have written a small cocoa app which is basically just a simple user interface. It consists of a window with shapes that you can draw inside it. It also has scrollbars for when the background (or image) is smaller than the window itself. The problem I am having is that when I click and drag a shape (to move it), it does not automatically scroll (for example, if I click inside the shape and drag the mouse down below the bottom of the window, it will not scroll down as I would like it to).

Any help would be greatly appreciated.

p.s. I have googled all over the place for this but the closest thing I have found is grab-scrolling code on this page: http://iratescotsman.com/products/source/)

Thanks!
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
From Borkware (http://borkware.com/quickies/one?topic=NSView)
Performing relative (grab-hand) scrolling
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.