diff options
author | rowanbeentje <rowan@beent.je> | 2011-09-21 00:38:56 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2011-09-21 00:38:56 +0000 |
commit | 549d28e50a35d432531926a6916755dbe26d91c4 (patch) | |
tree | cf5ef7d9440fedb7226ea60e7f2b22d5907d428e /Source/SPDataCellFormatter.m | |
parent | 6278cbbf9d76d69f44d1d7ddf34792b9e0310272 (diff) | |
download | sequelpro-549d28e50a35d432531926a6916755dbe26d91c4.tar.gz sequelpro-549d28e50a35d432531926a6916755dbe26d91c4.tar.bz2 sequelpro-549d28e50a35d432531926a6916755dbe26d91c4.zip |
Rework linebreak handling in content and custom query result views, as triggered by Issue #1184:
- Display table cells on a single line for preview purposes
- Display gray pilcrow/reverse pilcrow placeholders instead of linebreaks
- If a cell contains linebreaks, automatically trigger sheet editing mode
- Handle newly displayed linebreaks in column width detection
- If using the up/down arrow keys in a field editor, allow them to select the previous/next line within an editor if appropriat (instead of always moving to the previous/next row)
Diffstat (limited to 'Source/SPDataCellFormatter.m')
-rw-r--r-- | Source/SPDataCellFormatter.m | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/Source/SPDataCellFormatter.m b/Source/SPDataCellFormatter.m index 29b22cd7..70660223 100644 --- a/Source/SPDataCellFormatter.m +++ b/Source/SPDataCellFormatter.m @@ -34,7 +34,7 @@ - (NSString *)stringForObjectValue:(id)anObject { // Truncate the string for speed purposes if it's very long - improves table scrolling speed. - if ([(NSString *)anObject length] > 150) { + if ([anObject isKindOfClass:[NSString class]] && [(NSString *)anObject length] > 150) { return ([NSString stringWithFormat:@"%@...", [anObject substringToIndex:147]]); } @@ -53,9 +53,52 @@ return YES; } +/** + * When producing an attributed string, take the opportunity to convert to a single + * line for display, displaying placeholders for CR and LF characters. + */ - (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes { - return [[[NSAttributedString alloc] initWithString:[self stringForObjectValue:anObject] attributes:attributes] autorelease]; + + // Start with a base string which has been shortened for fast display + NSString *baseString = [self stringForObjectValue:anObject]; + + // Look for any linebreaks within the string + NSRange linebreakRange = [baseString rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet] options:NSLiteralSearch]; + + // If there's no linebreaks, return a non-mutable string + if (linebreakRange.location == NSNotFound) { + return [[[NSAttributedString alloc] initWithString:baseString attributes:attributes] autorelease]; + } + + NSMutableAttributedString *mutableString; + NSUInteger i, j, stringLength = [baseString length]; + unichar c; + + // Otherwise, prepare a mutable attributed string to alter, and walk along the string. + mutableString = [[[NSMutableAttributedString alloc] initWithString:baseString attributes:attributes] autorelease]; + for (i = linebreakRange.location, j = i; i < stringLength; i++, j++) { + c = [baseString characterAtIndex:i]; + switch (c) { + case '\n': + [mutableString replaceCharactersInRange:NSMakeRange(j, 1) withString:@"¶"]; + [mutableString addAttribute:NSForegroundColorAttributeName value:[NSColor lightGrayColor] range:NSMakeRange(j, 1)]; + break; + case '\r': + case 0x0085: + case 0x000b: + case 0x000c: + [mutableString replaceCharactersInRange:NSMakeRange(j, 1) withString:@"⁋"]; + [mutableString addAttribute:NSForegroundColorAttributeName value:[NSColor lightGrayColor] range:NSMakeRange(j, 1)]; + if (c == '\r' && i + 1 < stringLength && [baseString characterAtIndex:i+1] == '\n') { + [mutableString deleteCharactersInRange:NSMakeRange(j+1, 1)]; + i++; + } + break; + } + } + + return mutableString; } - (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error |