Trevor Rudolph
Trevor Rudolph

Reputation: 1063

Cursor overlay, Mac objective-c

ok so im trying to make it so that when my app opens, the cursor changes, but to change it im putting an image overlay. So here is the code i have...

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // get the cursor image 
        NSPoint mouseLoc; 
        mouseLoc = [NSEvent mouseLocation]; //get cur

        NSLog(@"Mouse location is x=%d,y=%d",(int)mouseLoc.x,(int)mouseLoc.y);

        // get the mouse image 
        NSImage *overlay    =   [[[NSCursor arrowCursor] image] copy];

        NSLog(@"Mouse location is x=%d,y=%d cursor width = %d, cursor height = %d",(int)mouseLoc.x,(int)mouseLoc.y,(int)[overlay size].width,(int)[overlay size].height);

        int x = (int)mouseLoc.x;
        int y = (int)mouseLoc.y;
        int w = (int)[overlay size].width;
        int h = (int)[overlay size].height;
        int org_x = x;
        int org_y = y;

        size_t height = CGImageGetHeight([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);
        size_t width =  CGImageGetWidth([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);
        int bytesPerRow = CGImageGetBytesPerRow([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        unsigned int * imgData = (unsigned int*)malloc(height*bytesPerRow);

        // have the graphics context now, 
        CGRect bgBoundingBox = CGRectMake (0, 0, width,height);

        CGContextRef context =  CGBitmapContextCreate(imgData, width, 
                                                      height, 
                                                      8, // 8 bits per component 
                                                      bytesPerRow, 
                                                      CGImageGetColorSpace([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]), 
                                                      CGImageGetBitmapInfo([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]));

        // first draw the image 
        CGContextDrawImage(context,bgBoundingBox,[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        // then mouse cursor 
        CGContextDrawImage(context,CGRectMake(0, 0, width,height),[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        // then mouse cursor 
        CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL] );


        // assuming both the image has been drawn then create an Image Ref for that 

        CGImageRef pFinalImage = CGBitmapContextCreateImage(context);

        CGContextRelease(context);

        return pFinalImage; /* to be released by the caller */
    }

So ive tried this and it compiles with warnings regarding the url path but it has no errors. But when i open the app, it crashes! So can anyone help me out?

Upvotes: 0

Views: 600

Answers (2)

NSResponder
NSResponder

Reputation: 16861

If you want to change the cursor, why aren't you just creating a new instance of NSCursor with whatever image you want to show?

Upvotes: 1

JWWalker
JWWalker

Reputation: 22707

You're taking an NSURL* and passing it to functions that take a CGImageRef. That isn't even close to making sense, so I don't know why you didn't get errors. To load a CGImage from a URL, you need to use the CGImageSource functions like CGImageSourceCreateWithURL.

P.S., rather than just saying it crashes, run it under the debugger and find out exactly where it crashes.

Upvotes: 0

Related Questions