[SOLVED] Not reliable trackingRect (move the mouse quickly)
[SOLVED] Not reliable trackingRect (move the mouse quickly)
- Subject: [SOLVED] Not reliable trackingRect (move the mouse quickly)
- From: Yann Bizeul <email@hidden>
- Date: Wed, 26 May 2004 09:50:08 +0200
I finally have something that work.
In fact, it seem to be a bad idea to use - (void)setImage: of NSControl
in its mouse[Entered|Exited] methods, I really got strange behavior.
Instead I keep the state of the button in an instance variable, and
just set it and setNeedsDisplay: YES in the mouseE* methods, as shown
in the code below.
The "caption" stuffs are left for inspiration only, in fact I set an
informal text field to display what this button will do.
This code does display the button's alternateImage when the mouse is
over it, and change the cursor to a pointing hand.
//
// RollOverButton.h
// BuddyPop
//
// Created by Yann Bizeul on Fri May 21 2004.
// Copyright (c) 2004 Tynsoe.org. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef enum {
ButtonOutsideState,
ButtonOverState
} ButtonState;
@interface RollOverButton : NSButton {
// NSString *caption;
NSTrackingRectTag trackingRectTag;
ButtonState state;
}
- (void)setCaption:(NSString*)aCaption;
- (NSString*)caption;
@end
====================================================================
//
// RollOverButton.m
// BuddyPop
//
// Created by Yann Bizeul on Fri May 21 2004.
// Copyright (c) 2004 Tynsoe.org. All rights reserved.
//
#import "RollOverButton.h"
@implementation RollOverButton
- (id)initWithCoder:(NSCoder*)aCoder;
{
self = [super initWithCoder:aCoder];
state = ButtonOutsideState;
return self;
}
- (void)awakeFromNib
{
[[self cell] setHighlightsBy:NSContentsCellMask];
}
- (void)drawRect:(NSRect)rect
{
NSImage *image;
switch (state)
{
case ButtonOverState:
image = [self alternateImage];
break;
case ButtonOutsideState:
image = [self image];
break;
default:
image = nil;
}
[image setFlipped: YES];
[image drawAtPoint:NSMakePoint(0,0)
fromRect:NSMakeRect(0,0,[image size].width,[image size].height)
operation:NSCompositeSourceOver
fraction:1];
}
- (void)resetCursorRects
{
[self addCursorRect:[self bounds]
cursor:[NSCursor pointingHandCursor]];
if (trackingRectTag)
[self removeTrackingRect:trackingRectTag];
trackingRectTag = [self addTrackingRect:[self bounds] owner:self
user
Data:nil
assumeInside:NO];
}
- (void)mouseEntered:(NSEvent*)theEvent
{
state = ButtonOverState;
// [[BPCaptionController sharedCaptionController]push:[self
caption]];
[self setNeedsDisplay: YES];
}
- (void)mouseExited:(NSEvent*)theEvent
{
state = ButtonOutsideState;
// [[BPCaptionController sharedCaptionController]pop];
[self setNeedsDisplay: YES];
}
/*
- (void)setCaption:(NSString*)aCaption;
{
[aCaption retain];
[caption release];
caption = aCaption;
}
- (NSString*)caption;
{
return caption;
}
*/
@end
Le 25 mai 04, ` 00:22, Yann Bizeul a icrit :
>
Hi list.
>
>
I have a problem subclassing NSButton to get a rollover effect on it.
>
I just want to place an alternate icon when the mouse enters the
>
NSButton view, the dispose of it when the mouse leave.
>
So I set up trackingRect according to the doc, and do my stuffs in
>
mouseEntered and mouseExited methods;
>
That works fine as long as I don't move the mouse too quickly.
>
>
If I ferbrily move the mouse between two of my custom NSButtons, some
>
events are skipped and my button sometimes keep a wrong state.
>
>
How can I avoid to ask the window to catch mouseMoved to make this
>
work fine ? Is there a way to get reliable mouseE* events ?
>
>
Thanks !
>
--
>
Yann Bizeul - yann at tynsoe.org
>
http://projects.tynsoe.org/
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
>
>
>
--
Yann Bizeul - yann at tynsoe.org
http://projects.tynsoe.org/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.