On 19-Nov-09, at 7:46 AM, Andy Lee wrote:

...I just did a quick test with an NSButton and a trivial custom view, and it worked. It also worked when I replaced TestView below with NSView.

- (void)awakeFromNib
{
NSWindow *testWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 500, 500) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered
                                                          defer:NO];
   [testWindow makeKeyAndOrderFront:self];

NSButton *testButton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 50, 100, 20)];
   [testButton setTitle:@"Hover Over Me"];
   [testButton sizeToFit];
   [testButton setToolTip:@"This is an NSButton"];
   [[testWindow contentView] addSubview:testButton];

NSView *testView = [[TestView alloc] initWithFrame:NSMakeRect(100, 100, 100, 20)];
   [testView setToolTip:@"This is a test view"];
   [[testWindow contentView] addSubview:testView];
}

Maybe you can temporarily add similar lines to your code and see if you see tooltips?

Now we're getting somewhere. I thought my app's code was simple enough that it essentially represented a simple test case, but of course I was wrong. If I put Andy's code in as the -awakeFromNib for my app delegate, the tooltip works fine. But if I copy and paste his button creation code into my NSDocument subclass's - windowControllerDidLoadNib: method, it does not work. That gave me some traction. After commenting out various chunks of my code, I have narrowed it down to a call that I make at the end of my windowControllerDidLoadNib: method:

        [window setContentSize:NSMakeSize(...)];

If I comment out this line, then the tooltip works. Aha. This led me to suspect the custom NSView subclass that I am using as a content view, and indeed, if I use that custom subclass as the content view in Andy's code, it breaks the tooltip there too. So now we have:

@interface AKDocumentContentView : NSView
{ }
@end

@implementation AKDocumentContentView
- (BOOL)isFlipped { return YES; }
@end

- (void)awakeFromNib
{
NSWindow *testWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 500, 500)
                                                                                
                           styleMask:NSTitledWindowMask
                                                                                
                                 backing:NSBackingStoreBuffered
                                                                                
                                   defer:YES];
        
        NSRect contentFrame = [[testWindow contentView] frame];
AKDocumentContentView *newContentView = [[AKDocumentContentView alloc] initWithFrame:contentFrame];
        [testWindow setContentView:newContentView];
        
NSButton *testButton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 50, 100, 20)];
        [testButton setTitle:@"Hover Over Me"];
        [testButton sizeToFit];
        [testButton setToolTip:@"This is an NSButton"];
        [[testWindow contentView] addSubview:testButton];

NSView *testView = [[NSView alloc] initWithFrame:NSMakeRect(100, 100, 100, 20)];
        [testView setToolTip:@"This is a test view"];
        [[testWindow contentView] addSubview:testView];
        
        [testWindow setContentSize:NSMakeSize(500, 600)];
        [testWindow makeKeyAndOrderFront:self];
        
}

Commenting out the -setContentSize: call makes the tooltip work. Commenting out the three-line block that replaces the window's content view with the custom subclass makes the tooltip work. But if both sections of code are live, as above, then the tooltip is broken. Changing -isFlipped to return NO, however, makes the tooltip work again. Also, moving the setToolTip: call to be after the - setContentSize: call makes the tooltip work.

So it appears that when you set a tooltip on a view that is inside a flipped content view, and then set a new content size for the window, the tooltip gets lost somehow. The only reason I could think of why this could be my fault was that my content view was not resizing to fit the window properly, because of its springs; but I added a -drawRect: call to it that just frames [self bounds], and it is correctly resizing to continue to fit the window, so that is not the issue.

Adding NSResizableWindowMask to the window's style mask is also interesting. In this case, if the -setContentSize: call is commented out, the tooltip starts out working and stays working even when the user resizes the window. But if the -setContentSize: call is left in, the tooltip starts out broken and remains broken, even after the user resizes the window. So whatever the issue is with -setContentSize:, it is persistent.

So, is this a bug in AppKit, or are content views officially not allowed to be flipped and I'm violating the rules? Or am I missing some important step in defining or setting up my custom content view? Or am I misusing -setContentSize: somehow, and ought to be setting the size of my window in a different way?

I now have a workaround, at least; I can simply postpone all of my - setToolTip: calls until after I have set my window to its final size. It would be nice to have a better fix, though...

Ben Haller
Stick Software

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to