Re: Getting NSScroller to respond to mouseUp: method
Re: Getting NSScroller to respond to mouseUp: method
- Subject: Re: Getting NSScroller to respond to mouseUp: method
- From: "Louis C. Sacha" <email@hidden>
- Date: Sat, 4 Dec 2004 19:37:11 -0800
Hello...
The reason that mouseUp: isn't called in most NSControl type classes
is that the mouse up event is eaten by the implementation of the
mouseDown: method. Most controls start a seperate event tracking loop
to handle dragging of the mouse in the mouseDown: method and remain
in that event loop until a mouse up event occurs. Since the events
that occur during the seperate event tracking loop are handled
directly by the code in the NSControl, they are not dispatched using
the standard AppKit techniques and don't trigger the normal methods
that are inherited from NSResponder.
For more info on the mouse event tracking done by controls, take a
look at the conceptual topic on "Basic Event Handling", specifically
the section "Handling Mouse Events in Views" which has an example of
how this sort of thing might be implemented near the end.
<http://developer.apple.com/documentation/Cocoa/Conceptual/BasicEventHandling/Tasks/HandlingMouseEvents.html#//apple_ref/doc/uid/20000906>
So if you wanted to force your subclass of NSScroller to use mouseUp:
or add code which runs when the mouse button is released, all you
have to do is override mouseDown: and put the code after the call to
the superclass implementation.
- (void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];
/* ... whatever you wanted to do at mouse up here ... */
}
If the code needs to run any time that the NSScrollView is scrolled,
you could avoid subclassing NSScroller by listening for the
NSViewBoundsDidChangeNotification notifications that occur when the
bounds origin of the scroll view's NSClipView is changed (which is
what creates the scroll effect).
You can also take advantage of the fact that the run loop is NOT
being run in NSDefaultRunLoopMode while the scroller is being
manipulated to delay the method invocation until right after the
mouse button is released (most controls and other things like menus
that do their own event handling loops use NSWindow's
nextEventMatchingMask: method which runs the event loop using
NSEventTrackingRunLoopMode).
- (void)awakeFromNib
{
/* ... any other stuff ... */
/* register for bounds changed notification for relevant clip view */
NSNotificationCenter *noteboard = [NSNotificationCenter defaultCenter];
[noteboard addObserver:self
selector:@selector(clipViewDidScroll:)
name:NSViewBoundsDidChangeNotification object:[theScrollView
contentView]];
}
- (void)clipViewDidScroll:(NSNotification *)note
{
/* set up delayed action (and coalesce since should only
happen once) */
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(someImportantButSlowMethod:) object:[note object]];
[self performSelector:@selector(someImportantButSlowMethod:)
withObject:[note object] afterDelay:0.0];
}
- (void)someImportantButSlowMethod:(id)sender
{
/* ... do whatever expensive stuff you wanted to do after
scrolling ... */
NSLog(@"doing someImportantButSlowMethod: only once");
}
If the same code needs to run any time the size of the visible area
within the scrollview is changed due to the window being resized,
then you could also register for the NSViewFrameDidChangeNotification
for the clipview and send it through the same clipViewDidScroll:
selector...
Hope that helps,
Louis
Hi all, I am having some troubles and I wanted to shoot a question off and
see if anyone could offer some wisdom.
I am attempting to get a subclassed NSScroller to respond to mouseUp:
method. When overriding the default behaviour for mouseDown: (by tossing an
NSLog entry in to let me know it's been called then and calling [super
mouseDown:theEvent]) it works just fine, but the instance does not seem to
respond to mouseUp: at all... (which according to my logic, it should as it
inherits from NSResponder...right?)
I did notice that NSControl overrides mouseDown: but does not include any
reference to mouseUp: would this level of inheritence remove the
functionality/presence of NSResponder's inhereted mouseUp: method?
Just to provide some scenery... The app I am building requires thumbnail
previews of images and currently bogs down quite a bit when dealing with a
large number of images (1000+). The reason that I am trying to get
NSScroller to respond to mouseUp: is to defer some of the repetitive and
expensive drawing operations performed on a subclassed NSMatrix of
NSImageCells in a subclassed NSScrollView, until after the user releases the
scroll knob.
Does anybody know what I am overlooking in regards to mouseUp: ?
Thanks in advance,
Joe Striedl
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden