From 3946e0c6c1e26af2b65590a1f0f50ab25c1c3bfb Mon Sep 17 00:00:00 2001 From: Bibiko Date: Wed, 12 Aug 2009 15:05:15 +0000 Subject: =?UTF-8?q?=E2=80=A2=20SPFieldEditor=20sheet=20now=20runs=20in=20a?= =?UTF-8?q?=20NSModalSession=20run=20loop=20to=20allow=20the=20execution?= =?UTF-8?q?=20of=20code=20in=20NSDefaultRunLoopMode=20(including=20showing?= =?UTF-8?q?=20Tooltips)=20-=20improved=20max=20text=20length=20checking=20?= =?UTF-8?q?while=20inserting=20a=20text=20chunk=20if=20a=20selection=20is?= =?UTF-8?q?=20given=20(now=20it=20truncates=20it=20correctly)=20-=20instea?= =?UTF-8?q?d=20of=20using=20NSBeep()=20while=20text=20length=20checking=20?= =?UTF-8?q?a=20Tooltip=20will=20be=20shown=20=E2=80=A2=20fine-tuned=20SPTo?= =?UTF-8?q?oltip:=20=E2=80=A2=20max=20text=20validation=20in=20TableConten?= =?UTF-8?q?t=20(cell=20editing)=20shows=20now=20a=20tooltip=20if=20text=20?= =?UTF-8?q?too=20long?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: Tooltip messages are tentative so far --- Source/SPDataCellFormatter.m | 6 +++--- Source/SPFieldEditorController.h | 2 ++ Source/SPFieldEditorController.m | 46 ++++++++++++++++++++++++++++------------ Source/SPTooltip.m | 16 +++++++------- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Source/SPDataCellFormatter.m b/Source/SPDataCellFormatter.m index 3860c058..5a768686 100644 --- a/Source/SPDataCellFormatter.m +++ b/Source/SPDataCellFormatter.m @@ -24,7 +24,7 @@ // More info at #import "SPDataCellFormatter.h" - +#import "SPTooltip.h" @implementation SPDataCellFormatter @@ -82,13 +82,13 @@ // A single character over the length of the string - likely typed. Prevent the change. if ([partialString length] == textLimit + 1) { - NSBeep(); + [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %d.", @"Maximum text length is set to %d."), textLimit]]; return NO; } // If the string is considerably longer than the limit, likely pasted. Accept but truncate. if ([partialString length] > textLimit) { - NSBeep(); + [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %d. Inserted text was truncated.", @"Maximum text length is set to %d. Inserted text was truncated."), textLimit]]; *newString = [NSString stringWithString:[partialString substringToIndex:textLimit]]; return NO; } diff --git a/Source/SPFieldEditorController.h b/Source/SPFieldEditorController.h index 4205140f..a330108e 100644 --- a/Source/SPFieldEditorController.h +++ b/Source/SPFieldEditorController.h @@ -53,6 +53,8 @@ BOOL editTextViewWasChanged; NSUserDefaults *prefs; + + int editSheetReturnCode; } - (IBAction)closeEditSheet:(id)sender; diff --git a/Source/SPFieldEditorController.m b/Source/SPFieldEditorController.m index 40088a2b..739e5b08 100644 --- a/Source/SPFieldEditorController.m +++ b/Source/SPFieldEditorController.m @@ -31,6 +31,7 @@ #import "SPDataCellFormatter.h" #import "RegexKitLite.h" #import "SPDataCellFormatter.h" +#import "SPTooltip.h" @implementation SPFieldEditorController @@ -205,33 +206,47 @@ editSheetWillBeInitialized = NO; [editSheetProgressBar stopAnimation:self]; - + // wait for editSheet - int code = [NSApp runModalForWindow:editSheet]; - - [NSApp endSheet:editSheet]; + NSModalSession session = [NSApp beginModalSessionForWindow:editSheet]; + int response; + for (;;) { + if (response = [NSApp runModalSession:session] != NSRunContinuesResponse + || ![editSheet isVisible]) + break; + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode + beforeDate:[NSDate distantFuture]]; + + } + [NSApp endModalSession:session]; [editSheet orderOut:nil]; - + [NSApp endSheet:editSheet]; + // For safety reasons inform QuickLook to quit quickLookCloseMarker = 1; - return ( code && isEditable ) ? [sheetEditData retain] : nil; + return ( editSheetReturnCode && isEditable ) ? [sheetEditData retain] : nil; } - (IBAction)closeEditSheet:(id)sender { + editSheetReturnCode = 0; + // Validate the sheet data before saving them. // - for max text length select the part which won't be saved - if(sender == editSheetOkButton) + if(sender == editSheetOkButton) { if (maxTextLength > 0 && [[editTextView textStorage] length] > maxTextLength) { [editTextView setSelectedRange:NSMakeRange(maxTextLength, [[editTextView textStorage] length] - maxTextLength)]; - NSBeep(); + [editTextView scrollRangeToVisible:NSMakeRange([editTextView selectedRange].location,0)]; + [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Text is too long. Maximum text length is set to %d.", @"Text is too long. Maximum text length is set to %d."), maxTextLength]]; return; } + [NSApp stopModal]; + editSheetReturnCode = 1; + } + [NSApp abortModal]; - [NSApp stopModalWithCode:[sender tag]]; - } - (IBAction)openEditSheet:(id)sender @@ -654,7 +669,7 @@ 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 ) { - NSBeep(); + [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %d.", @"Maximum text length is set to %d."), maxTextLength]]; [textView unmarkText]; return NO; } @@ -674,10 +689,13 @@ // to insert a text chunk partially to maxTextLength. if (newLength>maxTextLength) { - if(maxTextLength-[[textView textStorage] length] < [replacementString length]) { - [textView insertText:[replacementString substringToIndex:maxTextLength-[[textView textStorage] length]]]; + 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 %d. Inserted text was truncated.", @"Maximum text length is set to %d. Inserted text was truncated."), maxTextLength]]; + else + [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Maximum text length is set to %d.", @"Maximum text length is set to %d."), maxTextLength]]; + [textView insertText:[replacementString substringToIndex:maxTextLength-[[textView textStorage] length]+[textView selectedRange].length]]; } - NSBeep(); return NO; } diff --git a/Source/SPTooltip.m b/Source/SPTooltip.m index c45ccebb..9503262c 100644 --- a/Source/SPTooltip.m +++ b/Source/SPTooltip.m @@ -116,8 +116,7 @@ static float slow_in_out (float t) SPTooltip* tip = [SPTooltip new]; [tip initMeWithOptions:displayOptions]; [tip setFrameTopLeftPoint:point]; - - + if([type isEqualToString:@"text"]) { NSString* html = nil; NSMutableString* text = [[(NSString*)content mutableCopy] autorelease]; @@ -125,8 +124,8 @@ static float slow_in_out (float t) { [text replaceOccurrencesOfString:@"&" withString:@"&" options:0 range:NSMakeRange(0, [text length])]; [text replaceOccurrencesOfString:@"<" withString:@"<" options:0 range:NSMakeRange(0, [text length])]; - [text insertString:@"
" atIndex:0];
-			[text appendString:@"
"]; + // [text insertString:@"
" atIndex:0];
+			// [text appendString:@"
"]; html = text; } else @@ -143,6 +142,7 @@ static float slow_in_out (float t) NSBeep(); NSLog(@"SPTooltip: Type '%@' is not supported. Please use 'text' or 'html'. Tooltip is displayed as type 'html'", type); } + } - (void)initMeWithOptions:(NSDictionary *)displayOptions @@ -210,7 +210,7 @@ static float slow_in_out (float t) //If first responder is a textview return the caret position if([fr respondsToSelector:@selector(getRangeForCurrentWord)] ) { - NSRange range = NSMakeRange([fr getRangeForCurrentWord].location,0); + NSRange range = NSMakeRange([fr selectedRange].location,0); NSRange glyphRange = [[fr layoutManager] glyphRangeForCharacterRange:range actualCharacterRange:NULL]; NSRect boundingRect = [[fr layoutManager] boundingRectForGlyphRange:glyphRange inTextContainer:[fr textContainer]]; boundingRect = [fr convertRect: boundingRect toView: NULL]; @@ -304,7 +304,7 @@ static float slow_in_out (float t) // ================== - (BOOL)shouldCloseForMousePosition:(NSPoint)aPoint { - float ignorePeriod = 1.0f; + float ignorePeriod = 0.05f; if(-[didOpenAtDate timeIntervalSinceNow] < ignorePeriod) return NO; @@ -362,12 +362,12 @@ static float slow_in_out (float t) [self stopAnimation:self]; [self setValue:[NSDate date] forKey:@"animationStart"]; - [self setValue:[NSTimer scheduledTimerWithTimeInterval:0.02f target:self selector:@selector(animationTick:) userInfo:nil repeats:YES] forKey:@"animationTimer"]; + [self setValue:[NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(animationTick:) userInfo:nil repeats:YES] forKey:@"animationTimer"]; } - (void)animationTick:(id)sender { - float alpha = 0.97f * (1.0f - slow_in_out(-1.5 * [animationStart timeIntervalSinceNow])); + float alpha = 0.97f * (1.0f - slow_in_out(-2.8 * [animationStart timeIntervalSinceNow])); if(alpha > 0.0f) { [self setAlphaValue:alpha]; -- cgit v1.2.3