diff options
Diffstat (limited to 'Source/SPFieldEditorController.m')
-rw-r--r-- | Source/SPFieldEditorController.m | 234 |
1 files changed, 63 insertions, 171 deletions
diff --git a/Source/SPFieldEditorController.m b/Source/SPFieldEditorController.m index 9c2de5fc..c791dfc5 100644 --- a/Source/SPFieldEditorController.m +++ b/Source/SPFieldEditorController.m @@ -29,9 +29,6 @@ // More info at <https://github.com/sequelpro/sequelpro> #import "SPFieldEditorController.h" -#ifndef SP_CODA -#import "QLPreviewPanel.h" -#endif #import "RegexKitLite.h" #import "SPTooltip.h" #import "SPGeometryDataView.h" @@ -43,6 +40,12 @@ #import <SPMySQL/SPMySQL.h> +typedef enum { + TextSegment = 0, + ImageSegment, + HexSegment +} FieldEditorSegment; + @interface SPFieldEditorController (SPFieldEditorControllerDelegate) - (void)processFieldEditorResult:(id)data contextInfo:(NSDictionary*)contextInfo; @@ -52,6 +55,10 @@ @implementation SPFieldEditorController @synthesize editedFieldInfo; +@synthesize textMaxLength = maxTextLength; +@synthesize fieldType; +@synthesize fieldEncoding; +@synthesize allowNULL = _allowNULL; /** * Initialise an instance of SPFieldEditorController using the XIB “FieldEditorSheet.xib”. Init the available Quciklook format by reading @@ -143,7 +150,7 @@ } } - qlTypes = [[NSDictionary dictionaryWithObject:qlTypesItems forKey:SPQuickLookTypes] retain]; + qlTypes = [@{SPQuickLookTypes : qlTypesItems} retain]; [qlTypesItems release]; #endif @@ -164,19 +171,19 @@ #ifndef SP_CODA // On Mac OSX 10.6 QuickLook runs non-modal thus order out the panel // if still visible - if ([[NSClassFromString(@"QLPreviewPanel") sharedPreviewPanel] isVisible]) { - [[NSClassFromString(@"QLPreviewPanel") sharedPreviewPanel] orderOut:nil]; + if ([[QLPreviewPanel sharedPreviewPanel] isVisible]) { + [[QLPreviewPanel sharedPreviewPanel] orderOut:nil]; } #endif - if ( sheetEditData ) [sheetEditData release]; + if ( sheetEditData ) SPClear(sheetEditData); #ifndef SP_CODA - if ( qlTypes ) [qlTypes release]; + if ( qlTypes ) SPClear(qlTypes); #endif - if ( tmpFileName ) [tmpFileName release]; - if ( tmpDirPath ) [tmpDirPath release]; - if ( esUndoManager ) [esUndoManager release]; - if ( contextInfo ) [contextInfo release]; + if ( tmpFileName ) SPClear(tmpFileName); + if ( tmpDirPath ) SPClear(tmpDirPath); + if ( esUndoManager ) SPClear(esUndoManager); + if ( contextInfo ) SPClear(contextInfo); [super dealloc]; } @@ -343,7 +350,7 @@ [editSheetQuickLookButton setHidden:((!_isBlob && !isBinary) || _isGeometry)]; [editSheetSegmentControl setHidden:(!_isBlob && !isBinary && !_isGeometry)]; - [editSheetSegmentControl setEnabled:YES forSegment:1]; + [editSheetSegmentControl setEnabled:YES forSegment:ImageSegment]; // Set window's min size since no segment and quicklook buttons are hidden if (_isBlob || isBinary || _isGeometry) { @@ -398,7 +405,7 @@ [editImage setHidden:YES]; [editTextView setHidden:YES]; [editTextScrollView setHidden:YES]; - [editSheetSegmentControl setSelectedSegment:2]; + [editSheetSegmentControl setSelectedSegment:HexSegment]; } else if ([sheetEditData isKindOfClass:[SPMySQLGeometryData class]]) { SPGeometryDataView *v = [[[SPGeometryDataView alloc] initWithCoordinates:[sheetEditData coordinates] targetDimension:2000.0f] autorelease]; @@ -410,8 +417,8 @@ [hexTextView setString:@""]; [hexTextView setHidden:YES]; [hexTextScrollView setHidden:YES]; - [editSheetSegmentControl setEnabled:NO forSegment:2]; - [editSheetSegmentControl setSelectedSegment:0]; + [editSheetSegmentControl setEnabled:NO forSegment:HexSegment]; + [editSheetSegmentControl setSelectedSegment:TextSegment]; [editTextView setHidden:NO]; [editTextScrollView setHidden:NO]; } @@ -425,7 +432,7 @@ [editImage setHidden:YES]; [editTextView setHidden:NO]; [editTextScrollView setHidden:NO]; - [editSheetSegmentControl setSelectedSegment:0]; + [editSheetSegmentControl setSelectedSegment:TextSegment]; } if (image) { @@ -436,7 +443,7 @@ if(!_isGeometry) { [editTextView setHidden:YES]; [editTextScrollView setHidden:YES]; - [editSheetSegmentControl setSelectedSegment:1]; + [editSheetSegmentControl setSelectedSegment:ImageSegment]; } } else { @@ -452,13 +459,13 @@ [hexTextScrollView setHidden:YES]; } else { - [editSheetSegmentControl setEnabled:NO forSegment:1]; + [editSheetSegmentControl setEnabled:NO forSegment:ImageSegment]; } [editImage setHidden:YES]; [editTextView setHidden:NO]; [editTextScrollView setHidden:NO]; - [editSheetSegmentControl setSelectedSegment:0]; + [editSheetSegmentControl setSelectedSegment:TextSegment]; } // Locate the caret in editTextView @@ -477,7 +484,7 @@ [usedSheet makeFirstResponder:image == nil || _isGeometry ? editTextView : editImage]; } - if (stringValue) [stringValue release], stringValue = nil; + if (stringValue) SPClear(stringValue); editSheetWillBeInitialized = NO; @@ -486,52 +493,12 @@ } /** - * Set the maximum text length of the underlying table field for input validation. - * - * @param length The maximum text length - */ -- (void)setTextMaxLength:(NSUInteger)length -{ - maxTextLength = length; -} - -/** - * Set the field type of the underlying table field for input validation. - * - * @param aType The field type which will be used for dispatching which sheet will be shown. If type == BIT the bitSheet will be used otherwise the editSheet. - */ -- (void)setFieldType:(NSString*)aType -{ - fieldType = aType; -} - -/** - * Set the field encoding of the underlying table field for displaying it to the user. - * - * @param aEncoding encoding - */ -- (void)setFieldEncoding:(NSString*)aEncoding -{ - fieldEncoding = aEncoding; -} - -/** - * Set if underlying table field allows NULL for several validations. - * - * @param allowNULL If allowNULL is YES NULL value is allowed for the underlying table field - */ -- (void)setAllowNULL:(BOOL)allowNULL -{ - _allowNULL = allowNULL; -} - -/** * Segement controller for text/image/hex buttons in editSheet */ - (IBAction)segmentControllerChanged:(id)sender { - switch([sender selectedSegment]){ - case 0: // text + switch((FieldEditorSegment)[sender selectedSegment]){ + case TextSegment: [editTextView setHidden:NO]; [editTextScrollView setHidden:NO]; [editImage setHidden:YES]; @@ -542,7 +509,7 @@ [[NSApp mainWindow] makeFirstResponder:editTextView]; #endif break; - case 1: // image + case ImageSegment: [editTextView setHidden:YES]; [editTextScrollView setHidden:YES]; [editImage setHidden:NO]; @@ -550,7 +517,7 @@ [hexTextScrollView setHidden:YES]; [usedSheet makeFirstResponder:editImage]; break; - case 2: // hex - load on demand + case HexSegment: [usedSheet makeFirstResponder:hexTextView]; if([[hexTextView string] isEqualToString:@""]) { [editSheetProgressBar startAnimation:self]; @@ -590,7 +557,7 @@ { NSSavePanel *panel = [NSSavePanel savePanel]; - if ([editSheetSegmentControl selectedSegment] == 1 && [sheetEditData isKindOfClass:[SPMySQLGeometryData class]]) { + if ([editSheetSegmentControl selectedSegment] == ImageSegment && [sheetEditData isKindOfClass:[SPMySQLGeometryData class]]) { [panel setAllowedFileTypes:@[@"pdf"]]; [panel setAllowsOtherFileTypes:NO]; } @@ -718,7 +685,7 @@ // If the image cell now contains a valid image, select the image view if (image) { - [editSheetSegmentControl setSelectedSegment:1]; + [editSheetSegmentControl setSelectedSegment:ImageSegment]; [hexTextView setHidden:YES]; [hexTextScrollView setHidden:YES]; [editImage setHidden:NO]; @@ -727,7 +694,7 @@ // Otherwise deselect the image view } else { - [editSheetSegmentControl setSelectedSegment:0]; + [editSheetSegmentControl setSelectedSegment:TextSegment]; [hexTextView setHidden:YES]; [hexTextScrollView setHidden:YES]; [editImage setHidden:YES]; @@ -761,7 +728,7 @@ } else if ( [sheetEditData isKindOfClass:[SPMySQLGeometryData class]] ) { - if ( [editSheetSegmentControl selectedSegment] == 0 || editImage == nil ) { + if ( [editSheetSegmentControl selectedSegment] == TextSegment || editImage == nil ) { [[editTextView string] writeToURL:fileURL atomically:YES @@ -882,90 +849,34 @@ - (void)invokeQuickLookOfType:(NSString *)type treatAsText:(BOOL)isText { #ifndef SP_CODA - // Load QL via private framework (SDK 10.5) - if([[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/QuickLookUI.framework"] load]) { - - [editSheetProgressBar startAnimation:self]; - - [self createTemporaryQuickLookFileOfType:type treatAsText:isText]; - - counter++; - - // Init QuickLook - id ql = [NSClassFromString(@"QLPreviewPanel") sharedPreviewPanel]; - - [[ql delegate] setDelegate:self]; - [ql setURLs:[NSArray arrayWithObject: - [NSURL fileURLWithPath:tmpFileName]] currentIndex:0 preservingDisplayState:YES]; - - // TODO: No interaction with iChat and iPhoto due to .scriptSuite warning: - // unknown image format - [ql setShowsAddToiPhotoButton:NO]; - [ql setShowsiChatTheaterButton:NO]; - // Since we are inside of editSheet we have to avoid full-screen zooming - // otherwise QuickLook hangs - [ql setShowsFullscreenButton:NO]; - [ql setEnableDragNDrop:NO]; - // Order out QuickLook with animation effect according to self:previewPanel:frameForURL: - [ql makeKeyAndOrderFrontWithEffect:2]; // 1 = fade in - - // quickLookCloseMarker == 1 break the modal session - quickLookCloseMarker = 0; - - [editSheetProgressBar stopAnimation:self]; - - // Run QuickLook in its own modal seesion for event handling - NSModalSession session = [NSApp beginModalSessionForWindow:ql]; - for (;;) { - // Conditions for closing QuickLook - if ([NSApp runModalSession:session] != NSRunContinuesResponse - || quickLookCloseMarker == 1 - || ![ql isVisible]) - break; - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode - beforeDate:[NSDate distantFuture]]; - - } - [NSApp endModalSession:session]; - - // set ql's delegate to nil for dealloc - [[ql windowController] setDelegate:nil]; - - } - // Load QL via framework (SDK 10.5 but SP runs on 10.6) - // TODO: This is an hack in order to be able to support QuickLook on Mac OS X 10.5 and 10.6 - // as long as SP will be compiled against SDK 10.5. - // If SP will be compiled against SDK 10.6 we can use the standard way by using - // the QuickLookUI which is part of the Quartz.framework. See Developer example "QuickLookDownloader" + // See Developer example "QuickLookDownloader" // file:///Developer/Documentation/DocSets/com.apple.adc.documentation.AppleSnowLeopard.CoreReference.docset/Contents/Resources/Documents/samplecode/QuickLookDownloader/index.html#//apple_ref/doc/uid/DTS40009082 - else if([[NSBundle bundleWithPath:@"/System/Library/Frameworks/Quartz.framework/Frameworks/QuickLookUI.framework"] load]) { - [editSheetProgressBar startAnimation:self]; + [editSheetProgressBar startAnimation:self]; - [self createTemporaryQuickLookFileOfType:type treatAsText:isText]; + [self createTemporaryQuickLookFileOfType:type treatAsText:isText]; - counter++; + counter++; - // TODO: If QL is visible reload it - but how? - // Up to now QL will close and the user has to redo it. - if([[NSClassFromString(@"QLPreviewPanel") sharedPreviewPanel] isVisible]) { - [[NSClassFromString(@"QLPreviewPanel") sharedPreviewPanel] orderOut:nil]; - } + // TODO: If QL is visible reload it - but how? + // Up to now QL will close and the user has to redo it. + if([[QLPreviewPanel sharedPreviewPanel] isVisible]) { + [[QLPreviewPanel sharedPreviewPanel] orderOut:nil]; + } - [[NSClassFromString(@"QLPreviewPanel") sharedPreviewPanel] makeKeyAndOrderFront:nil]; + [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil]; - [editSheetProgressBar stopAnimation:self]; + [editSheetProgressBar stopAnimation:self]; - } else { - [SPTooltip showWithObject:[NSString stringWithFormat:@"QuickLook is not available on that platform."]]; - } #endif } +#pragma mark - QLPreviewPanelController methods + /** * QuickLook delegate for SDK 10.6. Set the Quicklook delegate to self and suppress setShowsAddToiPhotoButton since the format is unknow. */ -- (void)beginPreviewPanelControl:(id)panel +- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel { #ifndef SP_CODA @@ -973,15 +884,13 @@ [panel setDelegate:self]; [panel setDataSource:self]; - // Due to the unknown image format disable image sharing - [panel setShowsAddToiPhotoButton:NO]; #endif } /** * QuickLook delegate for SDK 10.6 - not in usage. */ -- (void)endPreviewPanelControl:(id)panel +- (void)endPreviewPanelControl:(QLPreviewPanel *)panel { // This document loses its responsisibility on the preview panel // Until the next call to -beginPreviewPanelControl: it must not @@ -991,22 +900,19 @@ /** * QuickLook delegate for SDK 10.6 */ -- (BOOL)acceptsPreviewPanelControl:(id)panel; +- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel; { return YES; } -// QuickLook delegates for SDK 10.6 -// - (BOOL)previewPanel:(QLPreviewPanel *)panel handleEvent:(NSEvent *)event -// { -// } +#pragma mark - QLPreviewPanelDataSource methods /** * QuickLook delegate for SDK 10.6. * * @return It always returns 1. */ -- (NSInteger)numberOfPreviewItemsInPreviewPanel:(id)panel +- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel { return 1; } @@ -1016,7 +922,7 @@ * * @return It returns as NSURL the temporarily created file. */ -- (id)previewPanel:(id)panel previewItemAtIndex:(NSInteger)anIndex +- (id)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)anIndex { if(tmpFileName) return [NSURL fileURLWithPath:tmpFileName]; @@ -1024,33 +930,19 @@ return nil; } -/** - * QuickLook delegate for SDK 10.5. - * - * @return It returns the frame of the application's middle. If an empty frame is returned then the panel will fade in/out instead. - */ -- (NSRect)previewPanel:(NSPanel*)panel frameForURL:(NSURL*)URL -{ - - // Close modal session defined in invokeQuickLookOfType: - // if user closes the QuickLook view - quickLookCloseMarker = 1; - - // Return the App's middle point - NSRect mwf = [[NSApp mainWindow] frame]; - return NSMakeRect( - mwf.origin.x+mwf.size.width/2, - mwf.origin.y+mwf.size.height/2, - 5, 5); +#pragma mark - QLPreviewPanelDelegate methods -} +// QuickLook delegates for SDK 10.6 +// - (BOOL)previewPanel:(QLPreviewPanel *)panel handleEvent:(NSEvent *)event +// { +// } /** * QuickLook delegate for SDK 10.6. * * @return It returns the frame of the application's middle. If an empty frame is returned then the panel will fade in/out instead. */ -- (NSRect)previewPanel:(id)panel sourceFrameOnScreenForPreviewItem:(id)item +- (NSRect)previewPanel:(QLPreviewPanel *)panel sourceFrameOnScreenForPreviewItem:(id)item { // Return the App's middle point NSRect mwf = [[NSApp mainWindow] frame]; @@ -1066,6 +958,8 @@ // return [NSImage imageNamed:@"database"]; // } +#pragma mark - + /** * Called by (SPImageView) if an image was pasted into the editSheet */ @@ -1079,8 +973,6 @@ image = [[[NSImage alloc] initWithPasteboard:[NSPasteboard generalPasteboard]] autorelease]; if (image) { - if (nil != sheetEditData) [sheetEditData release]; - [editImage setImage:image]; if( sheetEditData ) [sheetEditData release]; |