aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPFieldEditorController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Source/SPFieldEditorController.m')
-rw-r--r--Source/SPFieldEditorController.m128
1 files changed, 93 insertions, 35 deletions
diff --git a/Source/SPFieldEditorController.m b/Source/SPFieldEditorController.m
index 7bdd2eb9..7c8a960c 100644
--- a/Source/SPFieldEditorController.m
+++ b/Source/SPFieldEditorController.m
@@ -23,7 +23,9 @@
// More info at <http://code.google.com/p/sequel-pro/>
#import "SPFieldEditorController.h"
+#ifndef SP_REFACTOR
#import "QLPreviewPanel.h"
+#endif
#import "SPDataCellFormatter.h"
#import "RegexKitLite.h"
#import "SPDataCellFormatter.h"
@@ -51,7 +53,12 @@
*/
- (id)init
{
- if ((self = [super initWithWindowNibName:@"FieldEditorSheet"])) {
+#ifndef SP_REFACTOR
+ if ((self = [super initWithWindowNibName:@"FieldEditorSheet"]))
+#else
+ if ((self = [super initWithWindowNibName:@"SQLFieldEditorSheet"]))
+#endif
+ {
// force the nib to be loaded
(void) [self window];
counter = 0;
@@ -86,6 +93,7 @@
[menuItem setEnabled:NO];
[menu addItem:menuItem];
[menuItem release];
+#ifndef SP_REFACTOR
NSUInteger tag = 2;
// Load default QL types
@@ -112,7 +120,6 @@
[qlTypesItems addObject:type];
}
}
-#ifndef SP_REFACTOR
// Load user-defined QL types
if([prefs objectForKey:SPQuickLookTypes]) {
for(id type in [prefs objectForKey:SPQuickLookTypes]) {
@@ -125,10 +132,10 @@
[qlTypesItems addObject:type];
}
}
-#endif
qlTypes = [[NSDictionary dictionaryWithObject:qlTypesItems forKey:SPQuickLookTypes] retain];
[qlTypesItems release];
+#endif
fieldType = @"";
fieldEncoding = @"";
@@ -144,13 +151,18 @@
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
+#ifndef SP_REFACTOR
// 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];
+#endif
if ( sheetEditData ) [sheetEditData release];
+#ifndef SP_REFACTOR
if ( qlTypes ) [qlTypes release];
+#endif
+ if ( tmpFileName ) [tmpFileName release];
if ( tmpDirPath ) [tmpDirPath release];
if ( esUndoManager ) [esUndoManager release];
if ( contextInfo ) [contextInfo release];
@@ -309,10 +321,10 @@
// Set window's min size since no segment and quicklook buttons are hidden
if (_isBlob || _isBINARY || _isGeometry) {
[usedSheet setFrameAutosaveName:@"SPFieldEditorBlobSheet"];
- [usedSheet setMinSize:NSMakeSize(560, 200)];
+ [usedSheet setMinSize:NSMakeSize(650, 200)];
} else {
[usedSheet setFrameAutosaveName:@"SPFieldEditorTextSheet"];
- [usedSheet setMinSize:NSMakeSize(340, 150)];
+ [usedSheet setMinSize:NSMakeSize(390, 150)];
}
[editTextView setEditable:_isEditable];
@@ -420,8 +432,10 @@
// After ordering out this sheet SPCopyTable remains the first responder thus set it hard.
// This only works in conjunction with [NSTextView becomeFirstResponder] and [NSTextView resignFirstResponder]
// which has to return YES.
+#ifndef SP_REFACTOR
if([[self window] firstResponder] == editTextView)
[[NSApp mainWindow] makeFirstResponder:[[self window] firstResponder]];
+#endif
}
@@ -480,7 +494,9 @@
[hexTextView setHidden:YES];
[hexTextScrollView setHidden:YES];
[usedSheet makeFirstResponder:editTextView];
+#ifndef SP_REFACTOR
[[NSApp mainWindow] makeFirstResponder:editTextView];
+#endif
break;
case 1: // image
[editTextView setHidden:YES];
@@ -563,23 +579,31 @@
*/
- (IBAction)closeEditSheet:(id)sender
{
-
editSheetReturnCode = 0;
// Validate the sheet data before saving them.
// - for max text length (except for NULL value string) select the part which won't be saved
// and suppress closing the sheet
- if(sender == editSheetOkButton) {
- if (maxTextLength > 0 && [[editTextView textStorage] length] > maxTextLength && ![[[editTextView textStorage] string] isEqualToString:[prefs objectForKey:SPNullValue]]) {
- [editTextView setSelectedRange:NSMakeRange((NSUInteger)maxTextLength, [[editTextView textStorage] length] - (NSUInteger)maxTextLength)];
+ if (sender == editSheetOkButton) {
+
+ unsigned long long maxLength = maxTextLength;
+
+ // For FLOAT fields ignore the decimal point in the text when comparing lengths
+ if ([[fieldType uppercaseString] isEqualToString:@"FLOAT"] && ([[[editTextView textStorage] string] rangeOfString:@"."].location != NSNotFound)) {
+ maxLength++;
+ }
+
+ if (maxLength > 0 && [[editTextView textStorage] length] > maxLength && ![[[editTextView textStorage] string] isEqualToString:[prefs objectForKey:SPNullValue]]) {
+ [editTextView setSelectedRange:NSMakeRange((NSUInteger)maxLength, [[editTextView textStorage] length] - (NSUInteger)maxLength)];
[editTextView scrollRangeToVisible:NSMakeRange([editTextView selectedRange].location,0)];
[SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Text is too long. Maximum text length is set to %llu.", @"Text is too long. Maximum text length is set to %llu."), maxTextLength]];
+
return;
}
editSheetReturnCode = 1;
}
- else if(sender == bitSheetOkButton && _isEditable) {
+ else if (sender == bitSheetOkButton && _isEditable) {
editSheetReturnCode = 1;
}
@@ -610,7 +634,6 @@
[callerInstance processFieldEditorResult:returnData contextInfo:contextInfo];
#endif
}
-
}
/**
@@ -750,10 +773,12 @@
*/
- (IBAction)quickLookFormatButton:(id)sender
{
+#ifndef SP_REFACTOR
if(qlTypes != nil && [[qlTypes objectForKey:@"QuickLookTypes"] count] > (NSUInteger)[sender tag] - 2) {
NSDictionary *type = [[qlTypes objectForKey:@"QuickLookTypes"] objectAtIndex:[sender tag] - 2];
[self invokeQuickLookOfType:[type objectForKey:@"Extension"] treatAsText:([[type objectForKey:@"treatAsText"] integerValue])];
}
+#endif
}
/**
@@ -770,7 +795,8 @@
// Create a temporary file name to store the data as file
// since QuickLook only works on files.
// Alternate the file name to suppress caching by using counter%2.
- tmpFileName = [NSString stringWithFormat:@"%@SequelProQuickLook%d.%@", tmpDirPath, counter%2, type];
+ if (tmpFileName) [tmpFileName release];
+ tmpFileName = [[NSString alloc] initWithFormat:@"%@SequelProQuickLook%d.%@", tmpDirPath, counter%2, type];
// if data are binary
if ( [sheetEditData isKindOfClass:[NSData class]] && !isText) {
@@ -816,7 +842,7 @@
*/
- (void)invokeQuickLookOfType:(NSString *)type treatAsText:(BOOL)isText
{
-
+#ifndef SP_REFACTOR
// Load QL via private framework (SDK 10.5)
if([[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/QuickLookUI.framework"] load]) {
@@ -894,7 +920,7 @@
} else {
[SPTooltip showWithObject:[NSString stringWithFormat:@"QuickLook is not available on that platform."]];
}
-
+#endif
}
/**
@@ -902,6 +928,7 @@
*/
- (void)beginPreviewPanelControl:(id)panel
{
+#ifndef SP_REFACTOR
// This document is now responsible of the preview panel
[panel setDelegate:self];
@@ -909,6 +936,7 @@
// Due to the unknown image format disable image sharing
[panel setShowsAddToiPhotoButton:NO];
+#endif
}
@@ -1289,10 +1317,10 @@
*/
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)r replacementString:(NSString *)replacementString
{
-
- if(textView == editTextView && (maxTextLength > 0)
- && ![ [[[editTextView textStorage] string] stringByAppendingString:replacementString] isEqualToString:[prefs objectForKey:SPNullValue]]) {
-
+ if (textView == editTextView &&
+ (maxTextLength > 0) &&
+ ![[[[editTextView textStorage] string] stringByAppendingString:replacementString] isEqualToString:[prefs objectForKey:SPNullValue]])
+ {
NSInteger newLength;
// Auxilary to ensure that eg textViewDidChangeSelection:
@@ -1301,52 +1329,83 @@
// (OK button).
editTextViewWasChanged = ([replacementString length] == 1);
- // Pure attribute changes are ok.
+ // Pure attribute changes are ok
if (!replacementString) return YES;
// The exact change isn't known. Disallow the change to be safe.
- if (r.location==NSNotFound) return NO;
+ if (r.location == NSNotFound) return NO;
// Length checking while using the Input Manager (eg for Japanese)
if ([textView hasMarkedText] && (maxTextLength > 0) && (r.location < maxTextLength)) {
-
+
// User tries to insert a new char but max text length was already reached - return NO
- if( !r.length && ([[textView textStorage] length] >= maxTextLength) ) {
+ if (!r.length && ([[textView textStorage] length] >= maxTextLength)) {
[SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %llu.", @"Maximum text length is set to %llu."), maxTextLength]];
[textView unmarkText];
+
return NO;
}
- // otherwise allow it if insertion point is valid for eg
+ // Otherwise allow it if insertion point is valid for eg
// a VARCHAR(3) field filled with two Chinese chars and one inserts the
// third char by typing its pronounciation "wo" - 2 Chinese chars plus "wo" would give
// 4 which is larger than max length.
// TODO this doesn't solve the problem of inserting more than one char. For now
// that part which won't be saved will be hilited if user pressed the OK button.
- else if (r.location < maxTextLength)
+ else if (r.location < maxTextLength) {
return YES;
+ }
}
// Calculate the length of the text after the change.
- newLength=[[[textView textStorage] string] length]+[replacementString length]-r.length;
+ newLength = [[[textView textStorage] string] length] + [replacementString length] - r.length;
+ NSUInteger textLength = [[[textView textStorage] string] length];
+
+ unsigned long long originalMaxTextLength = maxTextLength;
+
+ // For FLOAT fields ignore the decimal point in the text when comparing lengths
+ if ([[fieldType uppercaseString] isEqualToString:@"FLOAT"] &&
+ ([[[textView textStorage] string] rangeOfString:@"."].location != NSNotFound)) {
+
+ if ((NSUInteger)newLength == (maxTextLength + 1)) {
+ maxTextLength++;
+ textLength--;
+ }
+ else if ((NSUInteger)newLength > maxTextLength) {
+ textLength--;
+ }
+ }
+
// If it's too long, disallow the change but try
// to insert a text chunk partially to maxTextLength.
if ((NSUInteger)newLength > maxTextLength) {
-
- if((maxTextLength-[[textView textStorage] length]+[textView selectedRange].length) <= [replacementString length]) {
- if(maxTextLength-[[textView textStorage] length]+[textView selectedRange].length)
- [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %llu. Inserted text was truncated.", @"Maximum text length is set to %llu. Inserted text was truncated."), maxTextLength]];
- else
- [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %llu.", @"Maximum text length is set to %llu."), maxTextLength]];
- [textView insertText:[replacementString substringToIndex:(NSUInteger)maxTextLength-[[textView textStorage] length]+[textView selectedRange].length]];
+ if ((maxTextLength - textLength + [textView selectedRange].length) <= [replacementString length]) {
+
+ NSString *tooltip = nil;
+
+ if (maxTextLength - textLength + [textView selectedRange].length) {
+ tooltip = [NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %llu. Inserted text was truncated.", @"Maximum text length is set to %llu. Inserted text was truncated."), maxTextLength];
+ }
+ else {
+ tooltip = [NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %llu.", @"Maximum text length is set to %llu."), maxTextLength];
+ }
+
+ [SPTooltip showWithObject:tooltip];
+
+ [textView insertText:[replacementString substringToIndex:(NSUInteger)maxTextLength - textLength +[textView selectedRange].length]];
}
+
+ maxTextLength = originalMaxTextLength;
+
return NO;
}
+
+ maxTextLength = originalMaxTextLength;
- // Otherwise, allow it.
+ // Otherwise, allow it
return YES;
-
}
+
return YES;
}
@@ -1355,7 +1414,6 @@
*/
- (void)textViewDidChangeSelection:(NSNotification *)notification
{
-
if([notification object] == editTextView) {
// Do nothing if user really didn't changed text (e.g. for font size changing return)
if(!editTextViewWasChanged && (editSheetWillBeInitialized