Problems with cursor methods
Problems with cursor methods
- Subject: Problems with cursor methods
- From: Bertrand Mansion <email@hidden>
- Date: Sun, 20 Jan 2002 21:25:30 +0100
Hi all,
I have a problem with my cursor handling methods.
In an app, I have a window, containing an NSView subclass called MyView
which contains an image. I also have a slider (resizeFactor) which I use to
resize the image.
I draw a rect (which looks like a handle) on the left-top corner of the
image in the view. On top of this rect, I define another rect, this one to
be a sensitive zone for cursor change.
If I resize the image and move the rects in order to keep them in the
left-top corner, it seems that the sensitive zone (for cursor change)
extends instead of being reset.
The only way I found to really reset the rects is to resize the window. It
positions the rect where I want and delete the other ones.
I have read the docs and the methods they give to call resetCursorRects
can't work for my app. Here is my code :
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSImage *zoomCursorImage = [NSImage imageNamed:@"zoom"];
zoomCursor = [[NSCursor alloc] initWithImage:zoomCursorImage
hotSpot:NSMakePoint(0.0,0.0)]; // zoomCursor is an iVar
image = [NSImage imageNamed:@"berty1"];
originalSize = [image size];
newSize = originalSize;
[self setNeedsDisplay:YES];
}
return self;
}
- (void)drawRect:(NSRect)rect {
NSPoint imageCompositeCenter;
NSSize imageSize;
windowCenter = NSMakePoint(NSMidX([self bounds]), NSMidY([self
bounds]));
imageSize = [image size];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(originalSize.width * resizeFactor,
originalSize.height * resizeFactor)];
imageSize = [image size];
imageCompositeCenter = NSMakePoint(windowCenter.x - imageSize.width /
2.0, windowCenter.y - imageSize.height / 2.0);
[image drawAtPoint:imageCompositeCenter fromRect:NSMakeRect(0,0,
imageSize.width, imageSize.height) operation:NSCompositeCopy fraction:1.0];
[self drawHandlesForSize:imageSize];
newSize = imageSize; // newSize is an iVar used for cursor rect
}
- (IBAction)takeFactorFrom:(id)sender
{
float factorFromSlider;
factorFromSlider = [sender floatValue];
if (factorFromSlider != resizeFactor) {
resizeFactor = factorFromSlider;
[self resetCursorRects]; // I call it manually, I maybe shouldn't
but I found no other way...
[self setNeedsDisplay:YES];
}
}
- (void)drawHandlesForSize:(NSSize)imageSize
{
NSPoint currentHandle; // Point for new handle
currentHandle = NSMakePoint(windowCenter.x - imageSize.width / 2.0,
windowCenter.y + imageSize.height / 2.0);
[self drawHandleAtPoint:currentHandle];
}
- (void)drawHandleAtPoint:(NSPoint)point
{
NSRect handleRect;
handleRect.origin.x = point.x - 3.0 + 1.0;
handleRect.origin.y = point.y - 3.0 + 1.0;
handleRect.size.width = 6.0 - 1.0;
handleRect.size.height = 6.0 - 1.0;
handleRect = [self centerScanRect:handleRect];
[[NSColor blackColor] set];
NSRectFill(handleRect);
handleRect = NSOffsetRect(handleRect, -1.0, -1.0);
[[NSColor redColor] set];
NSRectFill(handleRect);
}
- (NSRect)makeCursorRects:(NSPoint)point
// This could probably be optimized to make handle and cursor rects at the
same time.
{
NSRect handleRect;
handleRect.origin.x = point.x - 5.0;
handleRect.origin.y = point.y - 5.0;
handleRect.size.width = 10.0;
handleRect.size.height = 10.0;
return handleRect;
}
- (void)makeZoomCursors
{
NSPoint currentHandle; // Point for new handle
NSRect zoomCursorRect;
currentHandle = NSMakePoint(windowCenter.x - newSize.width / 2.0,
windowCenter.y + newSize.height / 2.0);
zoomCursorRect = [self makeCursorRects:currentHandle];
[self addCursorRect:zoomCursorRect cursor:zoomCursor];
}
- (void)dealloc
{
[zoomCursor release];
zoomCursor = nil;
[super dealloc];
}
- (void)resetCursorRects
{
[self makeZoomCursors];
[super resetCursorRects]; // this method is NOT overridden in super.
[zoomCursor setOnMouseEntered:YES];
}
@end
I would like to know if there is a way to reset the cursor rects each time
my image is resized without touching neither the window nor the view ?
Thanks for your help.
Bertrand Mansion
Mamasam