// // cursors.m // BoE // // Created by Celtic Minstrel on 03/06/09. // Wrappers for NSCursor, based on an Apple example // #import #import "cursors.h" NSAutoreleasePool *pool; NSImage* ImageFromURL(CFURLRef url){ CGImageSourceRef imageSource = CGImageSourceCreateWithURL(url, NULL); CGImageRef theImage = nil; if(imageSource == nil) return nil; theImage = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL); if(theImage == nil) return nil; CFRelease( imageSource ); NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0); // Get the image dimensions. imageRect.size.height = CGImageGetHeight(theImage); imageRect.size.width = CGImageGetWidth(theImage); // Create a new image to receive the Quartz image data. NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size]; [newImage lockFocus]; // Get the Quartz context and draw. CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; CGContextDrawImage(imageContext, *(CGRect*)&imageRect, theImage); [newImage unlockFocus]; return newImage; } CursorRef CreateCursorFromFile(CFURLRef imgPath, float hotSpotX, float hotSpotY){ static BOOL inited = NO; if(!inited){ NSApplicationLoad(); pool = [[NSAutoreleasePool alloc] init]; [[[NSWindow alloc] init] release]; inited = YES; } NSImage *img = ImageFromURL(imgPath); NSCursor *cursor = [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(hotSpotX, hotSpotY)]; [img release]; CursorRef theCursor = malloc(sizeof(CocoaCursor)); theCursor->ptr = cursor; return theCursor; } void DisposeNSCursor(CursorRef cursor){ [(NSCursor*)cursor->ptr release]; free(cursor); } void SetNSCursor(CursorRef cursor){ [(NSCursor*)cursor->ptr set]; } void HideNSCursor(){ [NSCursor hide]; } void ShowNSCursor(){ [NSCursor unhide]; } void CleanUp(){ [pool release]; }