0
votes

I'm trying to programmatically create a borderless NSWindow close to the mouse cursor and with the size of the image it should display. However I always get a much larger window (each side is about three times the size it should be)! I double-checked that my imageObj has the correct size and that all the NSSize and NSRect structures are created with correct values. The commented-out line doesn't work either (however which would be correct?)

I already searched through this site but I couldn't find any similar issue... What am I doing wrong? Here is my code:

NSString* imageName = [[NSBundle mainBundle] pathForResource:@"os_unknown" ofType:@"icns"];
NSImage* imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];
[view setImage:imageObj];

NSSize s = [imageObj size];
NSPoint p = [NSEvent mouseLocation];
NSRect r = [NSWindow frameRectForContentRect:NSMakeRect(p.x, p.y, s.width, s.height)
                                   styleMask:NSBorderlessWindowMask];
//NSRect r = {p, s};

if (win) [win release];
win = [[NSWindow alloc] initWithContentRect:r
                                  styleMask:NSBorderlessWindowMask
                                    backing:NSBackingStoreBuffered
                                      defer:false];
[win setLevel:kCGUtilityWindowLevel];

[view setBounds:NSMakeRect(0, 0, s.width, s.height)];
[[win contentView] addSubview:view];
[win orderFront:sender];    

Here *win is an NSWindow and *view is an NSImageView. Both are declared on top of my source file. I'm running Snow Leopard so any strictly >10.6.8 code won't work (already tried something).

Thank you in advance for any help.

1
I copied your code, and it worked fine for me. I'm using 10.8, but I'm not sure that would make any difference. One thing I don't see in your code is how you create your image view, view. What code are you using to create it? Do you set any of its properties elsewhere in the app?rdelmar
One more thought -- I don't know if icon files behave any differently than other images. You might try it with a jpeg and see if you still have the same problem.rdelmar
Solved! Thank you, you made me get the solution! Actually, the NSImageView was created inside InterfaceBuilder and (you made me think of this) it contained an NSImageCell. I noticed that the size I got always was the size that had been set in Interface Builder, no matter what I did programmatically. Obviously there is some very important piece of information I missed to get the things right in that way... I now created the NSImageView ex novo programmatically and everything works just fine. Thank you very much!gianluca
Ok, that's why mine worked then -- since you didn't show how you created your image view, I did it in code to test what you posted.rdelmar
Still, thank you very much! If you want to put an answer down here I would be happy to mark it as the correct answer and lift your reputation on the site!gianluca

1 Answers

0
votes

Solved! The NSImageView *view was created in Interface Builder. The size I got was the size set in there, no matter what code I used to resize it. By creating the NSImageView programmatically everything works great!

Special thanks to rdelmar who made me think and get the solution!