diff options
author | rowanbeentje <rowan@beent.je> | 2009-03-05 21:38:51 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2009-03-05 21:38:51 +0000 |
commit | 68dd0c0f1bd028725d697ed2fc1f2ce33bb9682f (patch) | |
tree | f3db7f49eabccd09f21b0479145bb8baa9f76134 /Source | |
parent | 0ebdbeb0201580e5abcded23dd37b6b3516cbb84 (diff) | |
download | sequelpro-68dd0c0f1bd028725d697ed2fc1f2ce33bb9682f.tar.gz sequelpro-68dd0c0f1bd028725d697ed2fc1f2ce33bb9682f.tar.bz2 sequelpro-68dd0c0f1bd028725d697ed2fc1f2ce33bb9682f.zip |
- Fix a number of edit sheet crashers regarding opening and saving files, image deletions, and image drag and dropping (fixes Issue #85 and google groups report)
- Improves compatbility of drag-and-drops onto the image well, including support for image drags from other applications
- Attempt to automatically select the image or text tab in the edit sheet as appropriate
- Fixes build-from-clean warnings caused by an unexposed function added in r375
Diffstat (limited to 'Source')
-rw-r--r-- | Source/CMImageView.h | 13 | ||||
-rw-r--r-- | Source/CMImageView.m | 84 | ||||
-rw-r--r-- | Source/TableContent.h | 2 | ||||
-rw-r--r-- | Source/TableContent.m | 92 | ||||
-rw-r--r-- | Source/TableSource.h | 1 |
5 files changed, 156 insertions, 36 deletions
diff --git a/Source/CMImageView.h b/Source/CMImageView.h index f52e8e5b..f4684cdc 100644 --- a/Source/CMImageView.h +++ b/Source/CMImageView.h @@ -24,13 +24,18 @@ #import <Cocoa/Cocoa.h> +@interface NSObject (CMImageViewDelegate) + +- (void)processUpdatedImageData:(NSData *)data; + +@end @interface CMImageView : NSImageView { - - NSString *draggedFilePath; - + + IBOutlet id delegate; + } -- (NSString *)draggedFilePath; +- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender; @end diff --git a/Source/CMImageView.m b/Source/CMImageView.m index 0eb4c250..de96fac5 100644 --- a/Source/CMImageView.m +++ b/Source/CMImageView.m @@ -27,22 +27,82 @@ @implementation CMImageView -- (NSString *)draggedFilePath + /* -returns the path of the dragged file -*/ + * On a drag and drop, read in dragged files and convert dragged images before passing + * them to the delegate for further processing + */ +- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { - return [NSString stringWithString:draggedFilePath]; -} + id delegateForUse = nil; -- (void)concludeDragOperation:(id <NSDraggingInfo>)sender -{ - if ( draggedFilePath ) - [draggedFilePath release]; + // If the delegate or the delegate's content instance doesn't implement processUpdatedImageData:, + // return the super's implementation + if (delegate) { + if ([delegate respondsToSelector:@selector(processUpdatedImageData:)]) { + delegateForUse = delegate; + } else if ( [delegate valueForKey:@"tableContentInstance"] + && [[delegate valueForKey:@"tableContentInstance"] respondsToSelector:@selector(processUpdatedImageData:)] ) { + delegateForUse = [delegate valueForKey:@"tableContentInstance"]; + } + } + if (!delegateForUse) { + return [super performDragOperation:sender]; + } + + // If a filename is available, attempt to read it and pass it to the delegate + if ([[[sender draggingPasteboard] propertyListForType:@"NSFilenamesPboardType"] count]) { + [delegateForUse processUpdatedImageData:[NSData dataWithContentsOfFile:[[[sender draggingPasteboard] propertyListForType:@"NSFilenamesPboardType"] objectAtIndex:0]]]; + return [super performDragOperation:sender]; + } + + // Otherwise, see if a dragged image is available via file contents or TIFF and pass to delegate + if ([[sender draggingPasteboard] dataForType:@"NSFileContentsPboardType"]) { + [delegateForUse processUpdatedImageData:[[sender draggingPasteboard] dataForType:@"NSFileContentsPboardType"]]; + return [super performDragOperation:sender]; + } - draggedFilePath = [[NSString stringWithString:[[[sender draggingPasteboard] propertyListForType:@"NSFilenamesPboardType"] objectAtIndex:0]] retain]; + // For dragged image representations (in TIFF format), convert to PNG data for compatibility + if ([[sender draggingPasteboard] dataForType:@"NSTIFFPboardType"]) { + NSData *pngData = nil; + NSBitmapImageRep *draggedImage = [[NSBitmapImageRep alloc] initWithData:[[sender draggingPasteboard] dataForType:@"NSTIFFPboardType"]]; + if (draggedImage) { + pngData = [draggedImage representationUsingType:NSPNGFileType properties:nil]; + [draggedImage release]; + } + if (pngData) { + [delegateForUse processUpdatedImageData:pngData]; + return [super performDragOperation:sender]; + } + } + + // For dragged image representations (in PICT format), convert to PNG data for compatibility + if ([[sender draggingPasteboard] dataForType:@"NSPICTPboardType"]) { + NSData *pngData = nil; + NSPICTImageRep *draggedImage = [[NSPICTImageRep alloc] initWithData:[[sender draggingPasteboard] dataForType:@"NSPICTPboardType"]]; + if (draggedImage) { + NSImage *convertImage = [[NSImage alloc] initWithSize:[draggedImage size]]; + [convertImage lockFocus]; + [draggedImage drawInRect:[draggedImage boundingBox]]; + NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[draggedImage boundingBox]]; + if (bitmapImageRep) { + pngData = [bitmapImageRep representationUsingType:NSPNGFileType properties:nil]; + [bitmapImageRep release]; + } + [convertImage unlockFocus]; + [convertImage release]; + [draggedImage release]; + } + if (pngData) { + [delegateForUse processUpdatedImageData:pngData]; + return [super performDragOperation:sender]; + } + } - [super concludeDragOperation:sender]; + // The image was not processed - return failure and clear image representation. + [delegateForUse processUpdatedImageData:nil]; + [self setImage:nil]; + return NO; } -@end +@end
\ No newline at end of file diff --git a/Source/TableContent.h b/Source/TableContent.h index 5847b868..e999e5e4 100644 --- a/Source/TableContent.h +++ b/Source/TableContent.h @@ -39,6 +39,7 @@ IBOutlet id tableWindow; IBOutlet CMCopyTable *tableContentView; IBOutlet id editSheet; + IBOutlet id editSheetTabView; IBOutlet id editImage; IBOutlet id editTextView; IBOutlet id hexTextView; @@ -86,6 +87,7 @@ - (IBAction)closeEditSheet:(id)sender; - (IBAction)openEditSheet:(id)sender; - (IBAction)saveEditSheet:(id)sender; +- (void)processUpdatedImageData:(NSData *)data; - (IBAction)dropImage:(id)sender; - (void)textDidChange:(NSNotification *)notification; - (NSString *)dataToHex:(NSData *)data; diff --git a/Source/TableContent.m b/Source/TableContent.m index 9a85b84a..156ce5c8 100644 --- a/Source/TableContent.m +++ b/Source/TableContent.m @@ -746,52 +746,91 @@ // load new data/images editData = [[NSData alloc] initWithContentsOfFile:fileName]; - NSImage *image = [[[NSImage alloc] initByReferencingFile:fileName] autorelease]; - NSString *contents = [[NSString stringWithContentsOfFile:fileName] autorelease]; + NSImage *image = [[NSImage alloc] initWithData:editData]; + NSString *contents = [NSString stringWithContentsOfFile:fileName]; // set the image preview, string contents and hex representation [editImage setImage:image]; [editTextView setString:contents]; [hexTextView setString:[self dataToHex:editData]]; + + // If the image cell now contains a valid image, select the image tab + if (image) { + [editSheetTabView selectTabViewItemAtIndex:1]; + + // Otherwise deselect the image tab if it's selected but now not showing anything + } else { + if ([editSheetTabView indexOfTabViewItem:[editSheetTabView selectedTabViewItem]] == 1) + [editSheetTabView selectTabViewItemAtIndex:0]; + } + + [image release]; } } -- (IBAction)saveEditSheet:(id)sender /* - saves a file containing the content of the editSheet + * Saves a file containing the content of the editSheet */ +- (IBAction)saveEditSheet:(id)sender { NSSavePanel *panel = [NSSavePanel savePanel]; if ( [panel runModal] == NSOKButton ) { NSString *fileName = [panel filename]; - NSString *data; + // Write binary field types directly to the file if ( [editData isKindOfClass:[NSData class]] ) { - data = editData; + [editData writeToFile:fileName atomically:YES]; + + // Write other field types' representations to the file via the current encoding } else { - data = [editData description]; + [[editData description] writeToFile:fileName + atomically:YES + encoding:[CMMCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]] + error:NULL]; } - [editData writeToFile:fileName atomically:YES encoding:[CMMCPConnection encodingForMySQLEncoding:[(NSString *)[tableDocumentInstance encoding] UTF8String]] error:NULL]; } } -- (IBAction)dropImage:(id)sender /* - invoked when user drag&drops image on imageView + * Invoked when the imageView in the connection sheet has the contents deleted + * or a file dragged and dropped onto it. */ +- (void)processUpdatedImageData:(NSData *)data { - // load new data/images - if (nil != editData) - { - [editData release]; + if (nil != editData) [editData release]; + + // If the image was not processed, set a blank string as the contents of the edit and hex views. + if ( data == nil ) { + editData = [[NSData alloc] init]; + [editTextView setString:@""]; + [hexTextView setString:@""]; + return; } - editData = [[[NSData alloc] initWithContentsOfFile:[sender draggedFilePath]] retain]; - NSString *contents = [NSString stringWithContentsOfFile:[sender draggedFilePath]]; - - // set the string contents and hex representation + + // Process the provided image + editData = [[NSData alloc] initWithData:data]; + NSString *contents = [[NSString alloc] initWithData:data encoding:[CMMCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]]]; + + // Set the string contents and hex representation [editTextView setString:contents]; [hexTextView setString:[self dataToHex:editData]]; + + [contents release]; +} + +- (IBAction)dropImage:(id)sender +{ + + // If the image was deleted, set a blank string as the contents of the edit and hex views. + // The actual dropped image processing is handled by processUpdatedImageData:. + if ( [editImage image] == nil ) { + if (nil != editData) [editData release]; + editData = [[NSData alloc] init]; + [editTextView setString:@""]; + [hexTextView setString:@""]; + return; + } } - (void)textDidChange:(NSNotification *)notification @@ -1842,7 +1881,7 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn editData = [theValue retain]; if ( [theValue isKindOfClass:[NSData class]] ) { - image = [[NSImage alloc] initWithData:theValue]; + image = [[[NSImage alloc] initWithData:theValue] autorelease]; [hexTextView setString:[self dataToHex:theValue]]; stringValue = [[NSString alloc] initWithData:theValue encoding:[mySQLConnection encoding]]; if (stringValue == nil) @@ -1852,12 +1891,25 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn stringValue = [[NSString alloc] initWithString:[theValue description]]; } - [editImage setImage:image]; + if (image) { + [editImage setImage:image]; + } else { + [editImage setImage:nil]; + } if (stringValue) { [editTextView setString:stringValue]; [editTextView setSelectedRange:NSMakeRange(0,[[editTextView string] length])]; [stringValue release]; } + + // If the cell contains a valid image, select the image tab + if (image) { + [editSheetTabView selectTabViewItemAtIndex:1]; + + // Otherwise default to text tab + } else { + [editSheetTabView selectTabViewItemAtIndex:0]; + } [NSApp beginSheet:editSheet modalForWindow:tableWindow modalDelegate:self didEndSelector:nil contextInfo:nil]; code = [NSApp runModalForWindow:editSheet]; diff --git a/Source/TableSource.h b/Source/TableSource.h index 4b15bc6f..c66ec71f 100644 --- a/Source/TableSource.h +++ b/Source/TableSource.h @@ -88,6 +88,7 @@ //additional methods - (void)setConnection:(CMMCPConnection *)theConnection; - (NSArray *)fetchResultAsArray:(CMMCPResult *)theResult; +- (BOOL)saveRowOnDeselect; - (BOOL)addRowToDB; - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo; |