RE: Hyperlinks in Table Views
RE: Hyperlinks in Table Views
- Subject: RE: Hyperlinks in Table Views
- From: David Doyle <email@hidden>
- Date: Wed, 27 Jan 2010 09:23:31 +0000
- Acceptlanguage: en-US, en-GB
- Thread-topic: Hyperlinks in Table Views
I think I may have got this to work, so here goes:
The data cell for the table column displaying the hyperlink is now a custom NSTextFieldCell with the following implementation over-ridden:
- (NSUInteger)hitTestForEvent: (NSEvent *)event
inRect: (NSRect)cellFrame
ofView: (NSView *)controlView
{
if([event type] == NSLeftMouseDown)
{
// User has clicked on the cell (assume for now that the entire cell is clickable).
#ifdef DEBUG
NSAssert([self attributedStringValue] != nil, @"There should be an attributed string value for this cell!");
#endif
NSAttributedString *string = [self attributedStringValue];
NSSize stringSize = [string size];
NSPoint mouseLocation = [controlView convertPoint: [event locationInWindow] fromView:nil];
#ifdef DEBUG
NSAssert((cellFrame.origin.y <= mouseLoc.y) && (mouseLoc.y <= cellFrame.origin.y + cellFrame.size.height), @"Click was not inside the cell rect!");
NSAssert(cellFrame.size.height == stringSize.height, @"The cell frame is not equal to the height of the cell name string!");
#endif
if(cellFrame.origin.x + stringSize.width < mouseLocation.x)
{
// The mouse click missed the bounds of the displayed string, so don't open the URL.
return NSCellHitNone;
}
NSRange stringRange = NSMakeRange(0, [string length]);
NSURL *stringURL = [string attribute: NSLinkAttributeName
atIndex: 0
effectiveRange: &stringRange];
#ifdef DEBUG
NSAssert(stringURL != nil, @"A URL should be attached to every attributed string for this column!");
NSAssert([stringURL isKindOfClass: [NSURL class]], @"we didn't get a URL from the attributed string for this cell!");
#endif
LSOpenCFURLRef((CFURLRef)stringURL, NULL);
}
return NSCellHitContentArea;
}
This still has the issue that the mouse won't change cursor when it enters the string's bounds, but at least the hyperlink actually opens now.
Thanks for your help!
________________________________________
From: cocoa-dev-bounces+david.doyle=email@hidden [cocoa-dev-bounces+david.doyle=email@hidden] On Behalf Of Jerry Krinock [email@hidden]
Sent: 11 January 2010 19:30
To: cocoa-dev
Subject: Re: Hyperlinks in Table Views
This might help you:
http://developer.apple.com/mac/library/releasenotes/cocoa/AppKitOlderNotes.html#NSTableView
Note especially the section titled "NSTableView/NSOutlineView - Single click to edit"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden
Sophos Plc, The Pentagon, Abingdon Science Park, Abingdon, OX14 3YP, United Kingdom.
Company Reg No 2096520. VAT Reg No GB 348 3873 20.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden