From 905b230a3cdf6786e80f9ddbcf4ae4b9a01ffa72 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 4 Mar 2011 09:18:23 +0000 Subject: Allow dragging of URLs from the URL field out of NetSurf. svn path=/trunk/netsurf/; revision=11899 --- cocoa/URLFieldCell.m | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'cocoa/URLFieldCell.m') diff --git a/cocoa/URLFieldCell.m b/cocoa/URLFieldCell.m index ceba83cb3..95e26cccf 100644 --- a/cocoa/URLFieldCell.m +++ b/cocoa/URLFieldCell.m @@ -18,6 +18,7 @@ #import "cocoa/URLFieldCell.h" +#import "content/urldb.h" @interface URLFieldCell () @@ -73,11 +74,57 @@ delegate: anObject event: theEvent]; } +- (void) startDragURLAt: (NSPoint) point inView: (NSView *) view; +{ + NSString *url = [self stringValue]; + NSString *title = url; + + const struct url_data *data = urldb_get_url_data( [url UTF8String] ); + if (data && data->title) title = [NSString stringWithUTF8String: data->title]; + + NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard]; + [pb declareTypes: [NSArray arrayWithObjects: NSStringPboardType, NSURLPboardType, + @"public.url", @"public.url-name", nil] owner: nil]; + [pb setString: url forType: NSStringPboardType]; + [pb setString: url forType: @"public.url"]; + [pb setString: title forType: @"public.url-name"]; + [[NSURL URLWithString: url] writeToPasteboard: pb]; + + NSRect urlBounds = NSZeroRect; + urlBounds.size = [title sizeWithAttributes: nil]; + urlBounds.size.width += urlBounds.size.height + 2; + + NSImage *image = [[NSImage alloc] initWithSize: urlBounds.size]; + + [image lockFocus]; + [favicon drawInRect: NSMakeRect( urlBounds.origin.x, urlBounds.origin.y, urlBounds.size.height, urlBounds.size.height ) + fromRect: NSZeroRect operation: NSCompositeCopy fraction: 1.0]; + urlBounds.origin.x += urlBounds.size.height + 2; + [title drawInRect: urlBounds withAttributes: nil]; + [image unlockFocus]; + + point.x -= urlBounds.size.height / 2; + point.y += urlBounds.size.height / 2; + + [view dragImage: image at: point offset: NSZeroSize event: [NSApp currentEvent] + pasteboard: pb source: self slideBack: YES]; + + [image release]; +} + +- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal +{ + return NSDragOperationCopy | NSDragOperationGeneric; +} + - (BOOL) trackMouse: (NSEvent *)theEvent inRect: (NSRect)cellFrame ofView: (NSView *)controlView untilMouseUp: (BOOL)flag; { const NSPoint point = [controlView convertPoint: [theEvent locationInWindow] fromView: nil]; const NSRect buttonRect = [self buttonFrame: cellFrame]; - if (NSPointInRect( point, buttonRect )) { + if (NSPointInRect( point, [self iconFrame: cellFrame] )) { + [self startDragURLAt: point inView: controlView]; + return NO; + } else if (NSPointInRect( point, buttonRect )) { return [[self refreshCell] trackMouse: theEvent inRect: buttonRect ofView: controlView untilMouseUp: flag]; } else { -- cgit v1.2.3