Revision: 26738 http://sourceforge.net/p/bibdesk/svn/26738 Author: hofman Date: 2021-08-25 22:58:54 +0000 (Wed, 25 Aug 2021) Log Message: ----------- Display and drag icon in imageview added to address field rather than an imagecell in the text field cell.
Modified Paths: -------------- trunk/bibdesk/BDSKAddressTextField.h trunk/bibdesk/BDSKAddressTextField.m trunk/bibdesk/BDSKAddressTextFieldCell.h trunk/bibdesk/BDSKAddressTextFieldCell.m trunk/bibdesk/BDSKDragImageView.h trunk/bibdesk/BDSKDragImageView.m trunk/bibdesk/BDSKWebGroupViewController.m Modified: trunk/bibdesk/BDSKAddressTextField.h =================================================================== --- trunk/bibdesk/BDSKAddressTextField.h 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKAddressTextField.h 2021-08-25 22:58:54 UTC (rev 26738) @@ -39,21 +39,15 @@ #import <Cocoa/Cocoa.h> #import "BDSKDragTextField.h" -@protocol BDSKAddressTextFieldDelegate; @interface BDSKAddressTextField : BDSKDragTextField { + NSImageView *imageView; NSButton *button; } @property (nonatomic, readonly) NSButton *button; +@property (nonatomic, readonly) NSImageView *imageView; -- (id <BDSKAddressTextFieldDelegate>)delegate; -- (void)setDelegate:(id <BDSKAddressTextFieldDelegate>)newDelegate; +@property (nonatomic, retain) NSURL *URL; @end - - -@protocol BDSKAddressTextFieldDelegate <BDSKDragTextFieldDelegate> -@optional -- (NSArray *)draggedItemsForAddressTextField:(BDSKAddressTextField *)textField; -@end Modified: trunk/bibdesk/BDSKAddressTextField.m =================================================================== --- trunk/bibdesk/BDSKAddressTextField.m 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKAddressTextField.m 2021-08-25 22:58:54 UTC (rev 26738) @@ -38,17 +38,18 @@ #import "BDSKAddressTextField.h" #import "BDSKAddressTextFieldCell.h" +#import "BDSKDragImageView.h" -#define BUTTON_SIZE 16.0 -#define BUTTON_MARGIN_X 3.0 -#define BUTTON_MARGIN_Y 3.0 +#define CONTROL_SIZE 16.0 +#define CONTROL_MARGIN_X 3.0 +#define CONTROL_MARGIN_Y 3.0 -@interface BDSKAddressTextField () <NSDraggingSource> +@interface BDSKAddressTextField () <BDSKDragImageViewDelegate> @end @implementation BDSKAddressTextField -@synthesize button; +@synthesize imageView, button, URL; + (Class)cellClass { return [BDSKAddressTextFieldCell class]; @@ -57,11 +58,23 @@ - (void)commonInit { if (RUNNING_BEFORE(10_10)) [[self cell] setBezelStyle:NSTextFieldSquareBezel]; + if (imageView == nil) { + NSRect rect, bounds = [self bounds]; + rect.origin.x = CONTROL_MARGIN_X; + rect.origin.y = [self isFlipped] ? NSMaxY(bounds) - CONTROL_SIZE - CONTROL_MARGIN_Y : NSMinY(bounds) + CONTROL_MARGIN_Y; + rect.size.width = rect.size.height = CONTROL_SIZE; + imageView = [[BDSKDragImageView alloc] initWithFrame:rect]; + [imageView setImageScaling:NSImageScaleProportionallyUpOrDown]; + [imageView setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin]; + [imageView setRefusesFirstResponder:YES]; + [(BDSKDragImageView *)imageView setDelegate:self]; + [self addSubview:imageView]; + } if (button == nil) { NSRect rect, bounds = [self bounds]; - rect.origin.x = NSMaxX(bounds) - BUTTON_SIZE - BUTTON_MARGIN_X; - rect.origin.y = [self isFlipped] ? NSMaxY(bounds) - BUTTON_SIZE - BUTTON_MARGIN_Y : NSMinY(bounds) + BUTTON_MARGIN_Y; - rect.size.width = rect.size.height = BUTTON_SIZE; + rect.origin.x = NSMaxX(bounds) - CONTROL_SIZE - CONTROL_MARGIN_X; + rect.origin.y = [self isFlipped] ? NSMaxY(bounds) - CONTROL_SIZE - CONTROL_MARGIN_Y : NSMinY(bounds) + CONTROL_MARGIN_Y; + rect.size.width = rect.size.height = CONTROL_SIZE; button = [[NSButton alloc] initWithFrame:rect]; [button setButtonType:NSMomentaryChangeButton]; [button setBordered:NO]; @@ -104,6 +117,7 @@ [self setCell:myCell]; [myCell release]; } + imageView = [[aDecoder decodeObjectForKey:@"imageView"] retain]; button = [[aDecoder decodeObjectForKey:@"button"] retain]; [self commonInit]; } @@ -112,15 +126,27 @@ - (void)encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; + [aCoder encodeConditionalObject:button forKey:@"imageView"]; [aCoder encodeConditionalObject:button forKey:@"button"]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; + BDSKDESTROY(imageView); BDSKDESTROY(button); [super dealloc]; } +- (NSURL *)URL { + NSString *string = [self stringValue]; + return [NSString isEmptyString:string] ? nil : [NSURL URLWithString:string]; +} + +- (void)setURL:(NSURL *)url { + [self setStringValue:[url absoluteString] ?: @""]; + [[imageView cell] setRepresentedObject:url]; +} + - (void)handleKeyOrMainStateChangedNotification:(NSNotification *)note { [self setNeedsDisplay:YES]; } @@ -150,60 +176,21 @@ return [super setKeyboardFocusRingNeedsDisplayInRect:[self bounds]]; } -- (void)dragItems:(NSArray *)items withImage:(NSImage *)image fromFrame:(NSRect)frame forEvent:(NSEvent *)event { - NSMutableArray *dragItems = [NSMutableArray array]; - - for (NSPasteboardItem *item in items) { - NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:item]; - [dragItem setDraggingFrame:frame contents:image]; - [dragItems addObject:dragItem]; - [dragItem release]; - } - - [self beginDraggingSessionWithItems:dragItems event:event source:self]; +- (NSArray *)draggedItemsForDragImageView:(BDSKDragImageView *)view { + NSURL *url = [[imageView cell] representedObject]; + return url ? [NSArray arrayWithObject:url] : nil; } -- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context { - return NSDragOperationCopy | NSDragOperationLink | NSDragOperationGeneric | NSDragOperationPrivate; +- (NSImage *)dragImageForDragImageView:(BDSKDragImageView *)view { + NSImage *dragImage = [[[NSImage alloc] initWithSize:[view frame].size] autorelease]; + NSBitmapImageRep *imageRep = [view bitmapImageRepForCachingDisplayInRect:[view bounds]]; + [view cacheDisplayInRect:[view bounds] toBitmapImageRep:imageRep]; + [dragImage addRepresentation:imageRep]; + return dragImage; } -// flag changes during a drag are not forwarded to the application, so we fix that at the end of the drag -- (void)draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation { - [[NSNotificationCenter defaultCenter] postNotificationName:BDSKFlagsChangedNotification object:NSApp]; +- (NSDragOperation)dragImageView:(BDSKDragImageView *)view sourceOperationMaskForDraggingContext:(NSDraggingContext)context { + return NSDragOperationCopy | NSDragOperationLink | NSDragOperationGeneric | NSDragOperationPrivate; } -- (void)mouseDown:(NSEvent *)theEvent { - if ([[self delegate] respondsToSelector:@selector(draggedItemsForAddressTextField:)]) { - NSRect iconRect = [[self cell] iconRectForBounds:[[self cell] adjustedFrame:[self bounds] inView:self]]; - NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - if (NSMouseInRect(mouseLoc, iconRect, [self isFlipped])) { - NSEvent *nextEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask]; - - if (NSLeftMouseDragged == [nextEvent type]) { - NSArray *items = [[self delegate] draggedItemsForAddressTextField:self]; - - if ([items count] > 0) { - - NSImage *dragImage = nil; - CGFloat boundsHeight = NSHeight([self bounds]); - NSRect drawRect = NSMakeRect(-NSMinX(iconRect), [self isFlipped] ? NSHeight(iconRect) - boundsHeight : -NSMinY(iconRect), NSMaxX(iconRect), boundsHeight); - if ([self isFlipped]) - iconRect.origin.y = boundsHeight - NSHeight(iconRect); - dragImage = [[[NSImage alloc] initWithSize:iconRect.size] autorelease]; - [dragImage lockFocusFlipped:[self isFlipped]]; - [[self cell] drawInteriorWithFrame:drawRect inView:self]; - [dragImage unlockFocus]; - - [self dragItems:items withImage:dragImage fromFrame:iconRect forEvent:theEvent]; - } - } - return; - } - } - [super mouseDown:theEvent]; -} - -- (id <BDSKAddressTextFieldDelegate>)delegate { return (id <BDSKAddressTextFieldDelegate>)[super delegate]; } -- (void)setDelegate:(id <BDSKAddressTextFieldDelegate>)newDelegate { [super setDelegate:newDelegate]; } - @end Modified: trunk/bibdesk/BDSKAddressTextFieldCell.h =================================================================== --- trunk/bibdesk/BDSKAddressTextFieldCell.h 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKAddressTextFieldCell.h 2021-08-25 22:58:54 UTC (rev 26738) @@ -39,14 +39,5 @@ #import <Cocoa/Cocoa.h> -@interface BDSKAddressTextFieldCell : NSTextFieldCell { - NSImageCell *imageCell; -} - -@property (nonatomic, retain) NSImage *icon; - -- (NSRect)iconRectForBounds:(NSRect)aRect; - -- (NSRect)adjustedFrame:(NSRect)aRect inView:(NSView *)controlView; - +@interface BDSKAddressTextFieldCell : NSTextFieldCell @end Modified: trunk/bibdesk/BDSKAddressTextFieldCell.m =================================================================== --- trunk/bibdesk/BDSKAddressTextFieldCell.m 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKAddressTextFieldCell.m 2021-08-25 22:58:54 UTC (rev 26738) @@ -39,79 +39,17 @@ #import "BDSKAddressTextFieldCell.h" #import "NSGeometry_BDSKExtensions.h" +#define MARGIN 17.0 +#define MARGIN_ROUNDED 9.0 -#define BORDER_BETWEEN_EDGE_AND_IMAGE 3.0 -#define BORDER_BETWEEN_IMAGE_AND_TEXT -2.0 -#define BORDER_BETWEEN_IMAGE_AND_TEXT_ROUNDED -10.0 -#define BORDER_BETWEEN_EDGE_AND_TEXT 17.0; -#define BORDER_BETWEEN_EDGE_AND_TEXT_ROUNDED 9.0; - @implementation BDSKAddressTextFieldCell -@dynamic icon; - -- (void)commonInit { - if (imageCell == nil) { - imageCell = [[NSImageCell alloc] init]; - [imageCell setImageScaling:NSImageScaleProportionallyUpOrDown]; - } -} - -- (id)initTextCell:(NSString *)aString { - self = [super initTextCell:aString]; - if (self) { - [self commonInit]; - } - return self; -} - -- (id)initWithCoder:(NSCoder *)decoder { - self = [super initWithCoder:decoder]; - if (self) { - imageCell = [[decoder decodeObjectForKey:@"imageCell"] retain]; - [self commonInit]; - } - return self; -} - -- (void)encodeWithCoder:(NSCoder *)encoder { - [super encodeWithCoder:encoder]; - [encoder encodeObject:imageCell forKey:@"imageCell"]; -} - -- (id)copyWithZone:(NSZone *)zone { - BDSKAddressTextFieldCell *copy = [super copyWithZone:zone]; - copy->imageCell = [imageCell copyWithZone:zone]; - return copy; -} - -- (void)dealloc { - BDSKDESTROY(imageCell); - [super dealloc]; -} - -- (void)setBackgroundStyle:(NSBackgroundStyle)style { - [super setBackgroundStyle:style]; - [imageCell setBackgroundStyle:style]; -} - -- (NSImage *)icon { - return [imageCell image]; -} - -- (void)setIcon:(NSImage *)newIcon { - if ([imageCell image] != newIcon) { - [imageCell setImage:newIcon]; - [(NSControl *)[self controlView] updateCellInside:self]; - } -} - - (NSSize)cellSizeForBounds:(NSRect)aRect { NSSize cellSize = [super cellSizeForBounds:aRect]; if ([self bezelStyle] == NSTextFieldRoundedBezel) - cellSize.width += cellSize.height - BORDER_BETWEEN_EDGE_AND_IMAGE + BORDER_BETWEEN_IMAGE_AND_TEXT_ROUNDED; + cellSize.width += 2.0 * MARGIN_ROUNDED; else - cellSize.width += cellSize.height - BORDER_BETWEEN_EDGE_AND_IMAGE + BORDER_BETWEEN_IMAGE_AND_TEXT; + cellSize.width += 2.0 * MARGIN; cellSize.width = fmin(cellSize.width, NSWidth(aRect)); if ([self bezelStyle] != NSTextFieldRoundedBezel) cellSize.height = fmin(cellSize.height + 1.0, NSHeight(aRect)); @@ -120,13 +58,6 @@ return cellSize; } -- (NSRect)iconRectForBounds:(NSRect)aRect { - CGFloat border = BORDER_BETWEEN_EDGE_AND_IMAGE; - CGFloat imageWidth = NSHeight(aRect) - 2.0 * border; - - return BDSKSliceRect(BDSKShrinkRect(aRect, border, NSMinXEdge), imageWidth, NSMinXEdge); -} - - (NSRect)adjustedFrame:(NSRect)aRect inView:(NSView *)controlView { if ([self bezelStyle] == NSTextFieldRoundedBezel) return aRect; @@ -175,31 +106,18 @@ } - (NSRect)drawingRectForBounds:(NSRect)aRect { - CGFloat leftBorder, rightBorder; - - if ([self bezelStyle] == NSTextFieldRoundedBezel) { - leftBorder = NSHeight(aRect) - BORDER_BETWEEN_EDGE_AND_IMAGE + BORDER_BETWEEN_IMAGE_AND_TEXT_ROUNDED; - rightBorder = BORDER_BETWEEN_EDGE_AND_TEXT_ROUNDED; - } else { - leftBorder = NSHeight(aRect) - BORDER_BETWEEN_EDGE_AND_IMAGE + BORDER_BETWEEN_IMAGE_AND_TEXT; - rightBorder = BORDER_BETWEEN_EDGE_AND_TEXT; - } - - aRect = BDSKShrinkRect(BDSKShrinkRect(aRect, leftBorder, NSMinXEdge), rightBorder, NSMaxXEdge); - + if ([self bezelStyle] == NSTextFieldRoundedBezel) + aRect = NSInsetRect(aRect, MARGIN_ROUNDED, 0.0); + else + aRect = NSInsetRect(aRect, MARGIN, 0.0); return [super drawingRectForBounds:aRect]; } - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { - cellFrame = [self adjustedFrame:cellFrame inView:controlView]; - - // let super draw the text in our reduced drawing rect + if ([self bezelStyle] != NSTextFieldRoundedBezel) + cellFrame = [self adjustedFrame:cellFrame inView:controlView]; [super drawInteriorWithFrame:cellFrame inView:controlView]; - - // Draw the image - NSRect imageRect = [self iconRectForBounds:cellFrame]; - imageRect = BDSKCenterRectVertically(imageRect, NSWidth(imageRect), [controlView isFlipped]); - [imageCell drawInteriorWithFrame:imageRect inView:controlView]; + } - (void)drawFocusRingMaskWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { @@ -223,20 +141,4 @@ }); } -- (NSCellHitResult)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView { - cellFrame = [self adjustedFrame:cellFrame inView:controlView]; - NSRect textRect = [self drawingRectForBounds:cellFrame]; - NSPoint mouseLoc = [controlView convertPoint:[event locationInWindow] fromView:nil]; - NSUInteger hit = NSCellHitNone; - if (NSMouseInRect(mouseLoc, textRect, [controlView isFlipped])) - hit = [super hitTestForEvent:event inRect:textRect ofView:controlView]; - else if (NSMouseInRect(mouseLoc, [self iconRectForBounds:cellFrame], [controlView isFlipped])) - hit = NSCellHitContentArea; - return hit; -} - -- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { - return nil; -} - @end Modified: trunk/bibdesk/BDSKDragImageView.h =================================================================== --- trunk/bibdesk/BDSKDragImageView.h 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKDragImageView.h 2021-08-25 22:58:54 UTC (rev 26738) @@ -46,6 +46,7 @@ - (BOOL)dragImageView:(BDSKDragImageView *)view acceptDrop:(id <NSDraggingInfo>)sender; - (NSArray *)draggedItemsForDragImageView:(BDSKDragImageView *)view; - (NSImage *)dragImageForDragImageView:(BDSKDragImageView *)view; +- (NSDragOperation)dragImageView:(BDSKDragImageView *)view sourceOperationMaskForDraggingContext:(NSDraggingContext)context; @end Modified: trunk/bibdesk/BDSKDragImageView.m =================================================================== --- trunk/bibdesk/BDSKDragImageView.m 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKDragImageView.m 2021-08-25 22:58:54 UTC (rev 26738) @@ -101,6 +101,8 @@ } - (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context { + if ([[self delegate] respondsToSelector:@selector(dragImageView:sourceOperationMaskForDraggingContext:)]) + return [[self delegate] dragImageView:self sourceOperationMaskForDraggingContext:context]; return context == NSDraggingContextWithinApplication ? NSDragOperationNone : NSDragOperationCopy; } Modified: trunk/bibdesk/BDSKWebGroupViewController.m =================================================================== --- trunk/bibdesk/BDSKWebGroupViewController.m 2021-08-25 06:30:31 UTC (rev 26737) +++ trunk/bibdesk/BDSKWebGroupViewController.m 2021-08-25 22:58:54 UTC (rev 26738) @@ -163,11 +163,11 @@ #pragma mark BDSKWebViewNavigationDelegate protocol - (void)webView:(WebView *)sender setIcon:(NSImage *)icon { - [(BDSKAddressTextFieldCell *)[urlField cell] setIcon:icon ?: [NSImage imageNamed:@"Bookmark"]]; + [[urlField imageView] setImage:icon ?: [NSImage imageNamed:@"Bookmark"]]; } - (void)webView:(WebView *)sender setURL:(NSURL *)aURL { - [urlField setStringValue:[aURL absoluteString] ?: @""]; + [urlField setURL:aURL]; } - (void)webView:(WebView *)sender setLoading:(BOOL)loading { @@ -225,7 +225,7 @@ } - (NSDragOperation)dragTextField:(BDSKDragTextField *)textField validateDrop:(id <NSDraggingInfo>)sender { - if ([sender draggingSource] != textField && + if ([sender draggingSource] != [urlField imageView] && [[sender draggingPasteboard] canReadURL]) return NSDragOperationEvery; return NSDragOperationNone; @@ -249,10 +249,6 @@ return NO; } -- (NSArray *)draggedItemsForAddressTextField:(BDSKAddressTextField *)textField { - return [NSArray arrayWithObjects:[[self webView] URL], nil]; -} - - (BOOL)control:(NSControl *)control textViewShouldAutoComplete:(NSTextView *)textView { return control == urlField; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit