Fix for bug 155920: added string truncation utility on our NSString category, and used it to truncat strings that are put into menu items in the UI and window titles.

This commit is contained in:
sfraser%netscape.com 2002-08-02 21:08:24 +00:00
parent a78c1cf8e4
commit dd5f5f9830
28 changed files with 476 additions and 96 deletions

View File

@ -287,7 +287,8 @@ BookmarksService::BookmarkChanged(nsIContent* aItem, bool shouldFlush = true)
NSMenuItem* childItem = [menu itemWithTag: contentID];
nsAutoString name;
aItem->GetAttr(kNameSpaceID_None, gNameAtom, name);
[childItem setTitle: [NSString stringWith_nsAString: name]];
NSString* bookmarkTitle = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
[childItem setTitle: bookmarkTitle];
}
}
@ -740,7 +741,7 @@ BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent
{
nsAutoString name;
aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
NSString* title = [NSString stringWith_nsAString: name];
NSString* title = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
// Create a menu or menu item for the child.
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];

View File

@ -35,6 +35,8 @@
*
* ***** END LICENSE BLOCK ***** */
#import "NSString+Utils.h"
#import "CHBrowserWrapper.h"
#import "BrowserWindowController.h"
#import "BookmarksService.h"
@ -358,7 +360,7 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
NSString* newTitle = nil;
if (mOffline) {
if (title && ![title isEqualToString:@""])
newTitle = [title stringByAppendingString: @" [Working Offline]"];
newTitle = [title stringByAppendingString: @" [Working Offline]"]; // XXX localize me
else
newTitle = [NSString stringWithString:@"Untitled [Working Offline]"];
mTitle = [newTitle retain];
@ -368,12 +370,13 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
title = [NSString stringWithString:NSLocalizedString(@"UntitledPageTitle", @"")];
mTitle = [title retain];
}
if ( mIsPrimary && mWindowController )
[[mWindowController window] setTitle:mTitle];
[[mWindowController window] setTitle:[mTitle stringByTruncatingTo:80 at:kTruncateAtEnd]];
// Always set the tab.
if (title && ![title isEqualToString:@""])
[mTab setLabel:title];
[mTab setLabel:title]; // tab titles get truncated when setting them to tabs
else
[mTab setLabel:NSLocalizedString(@"UntitledPageTitle", @"")];
}

View File

@ -41,9 +41,7 @@ static const int kDividerTag = 4000;
// the maximum number of history entry menuitems to display
static const int kMaxItems = 15;
// the maximum number of characters in a menu title before cropping it
static const unsigned int kMaxTitleLength = 60;
// the ellipsis string to insert into cropped strings
static const NSString *kEllipsis = @"...";
static const unsigned int kMaxTitleLength = 80;
@implementation CHGoMenu
@ -134,21 +132,7 @@ static const NSString *kEllipsis = @"...";
nsXPIDLString textStr;
entry->GetTitle(getter_Copies(textStr));
NSString* title = [NSString stringWith_nsAString: textStr];
// if the title is too long, crop it in the middle
if ([title length] > kMaxTitleLength) {
NSMutableString *croppedTitle = [NSMutableString stringWithCapacity:kMaxTitleLength+[kEllipsis length]];
int len1 = kMaxTitleLength/2;
int len2 = kMaxTitleLength - len1;
NSString *part1 = [title substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [title substringWithRange:NSMakeRange([title length]-len2, len2)];
[croppedTitle appendString:part1];
[croppedTitle appendString:kEllipsis];
[croppedTitle appendString:part2];
title = croppedTitle;
}
NSString* title = [[NSString stringWith_nsAString: textStr] stringByTruncatingTo:kMaxTitleLength at:kTruncateAtMiddle];
NSMenuItem *newItem = [self addItemWithTitle:title action:@selector(historyItemAction:) keyEquivalent:@""];
[newItem setTarget:self];
[newItem setTag:kDividerTag+1+i];

View File

@ -1644,6 +1644,48 @@
settings = {
};
};
F522EFC502F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseClipboard.cpp;
path = ../widget/src/xpwidgets/nsBaseClipboard.cpp;
refType = 2;
};
F522EFC602F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseDragService.cpp;
path = ../widget/src/xpwidgets/nsBaseDragService.cpp;
refType = 2;
};
F522EFC702F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseWidget.cpp;
path = ../widget/src/xpwidgets/nsBaseWidget.cpp;
refType = 2;
};
F522EFC802F73CF901A96654 = {
isa = PBXFileReference;
name = nsClipboardHelper.cpp;
path = ../widget/src/xpwidgets/nsClipboardHelper.cpp;
refType = 2;
};
F522EFC902F73CF901A96654 = {
isa = PBXFileReference;
name = nsDataFlavor.cpp;
path = ../widget/src/xpwidgets/nsDataFlavor.cpp;
refType = 2;
};
F522EFCA02F73CF901A96654 = {
isa = PBXFileReference;
name = nsPrimitiveHelpers.cpp;
path = ../widget/src/xpwidgets/nsPrimitiveHelpers.cpp;
refType = 2;
};
F522EFCB02F73CF901A96654 = {
isa = PBXFileReference;
name = nsTransferable.cpp;
path = ../widget/src/xpwidgets/nsTransferable.cpp;
refType = 2;
};
F5247C2D02289FD9013DD99A = {
isa = PBXFileReference;
path = alert.nib;
@ -7422,6 +7464,13 @@
F5EA337C02EF889001A96654,
F5EA337D02EF889001A96654,
F5EA337E02EF889001A96654,
F522EFC502F73CF901A96654,
F522EFC602F73CF901A96654,
F522EFC702F73CF901A96654,
F522EFC802F73CF901A96654,
F522EFC902F73CF901A96654,
F522EFCA02F73CF901A96654,
F522EFCB02F73CF901A96654,
);
isa = PBXGroup;
name = widget;

View File

@ -180,7 +180,7 @@ ContentClickListener::MouseDown(nsIDOMEvent* aEvent)
option->GetLabel(text);
if (text.IsEmpty())
option->GetText(text);
NSString* title = [NSString stringWith_nsAString: text];
NSString* title = [[NSString stringWith_nsAString: text] stringByTruncatingTo:75 at:kTruncateAtMiddle];
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
[menu addItem: menuItem];
[menuItem setTag: contentID];

View File

@ -40,6 +40,14 @@
class nsAString;
typedef enum
{
kTruncateAtStart,
kTruncateAtMiddle,
kTruncateAtEnd
} ETruncationType;
// a category to extend NSString
@interface NSString (ChimeraStringUtils)
@ -49,5 +57,6 @@ class nsAString;
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet;
- (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet withString:(NSString*)string;
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType;
@end

View File

@ -117,4 +117,53 @@
return cleanString;
}
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
{
unichar ellipsisChar = 0x2026;
NSString* ellipsisString = [NSString stringWithCharacters:&ellipsisChar length:1]; // @"...";
if ([self length] > maxCharacters)
{
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
switch (truncationType)
{
case kTruncateAtStart:
[croppedString appendString:ellipsisString];
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
break;
case kTruncateAtMiddle:
{
int len1 = maxCharacters / 2;
int len2 = maxCharacters - len1;
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
[croppedString appendString:part1];
[croppedString appendString:ellipsisString];
[croppedString appendString:part2];
}
break;
case kTruncateAtEnd:
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
[croppedString appendString:ellipsisString];
break;
default:
#if DEBUG
NSLog(@"Unknown truncation type in stringByTruncatingTo::");
#endif
break;
}
return croppedString;
}
else
{
return [[self copy] autorelease];
}
}
@end

View File

@ -1644,6 +1644,48 @@
settings = {
};
};
F522EFC502F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseClipboard.cpp;
path = ../widget/src/xpwidgets/nsBaseClipboard.cpp;
refType = 2;
};
F522EFC602F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseDragService.cpp;
path = ../widget/src/xpwidgets/nsBaseDragService.cpp;
refType = 2;
};
F522EFC702F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseWidget.cpp;
path = ../widget/src/xpwidgets/nsBaseWidget.cpp;
refType = 2;
};
F522EFC802F73CF901A96654 = {
isa = PBXFileReference;
name = nsClipboardHelper.cpp;
path = ../widget/src/xpwidgets/nsClipboardHelper.cpp;
refType = 2;
};
F522EFC902F73CF901A96654 = {
isa = PBXFileReference;
name = nsDataFlavor.cpp;
path = ../widget/src/xpwidgets/nsDataFlavor.cpp;
refType = 2;
};
F522EFCA02F73CF901A96654 = {
isa = PBXFileReference;
name = nsPrimitiveHelpers.cpp;
path = ../widget/src/xpwidgets/nsPrimitiveHelpers.cpp;
refType = 2;
};
F522EFCB02F73CF901A96654 = {
isa = PBXFileReference;
name = nsTransferable.cpp;
path = ../widget/src/xpwidgets/nsTransferable.cpp;
refType = 2;
};
F5247C2D02289FD9013DD99A = {
isa = PBXFileReference;
path = alert.nib;
@ -7422,6 +7464,13 @@
F5EA337C02EF889001A96654,
F5EA337D02EF889001A96654,
F5EA337E02EF889001A96654,
F522EFC502F73CF901A96654,
F522EFC602F73CF901A96654,
F522EFC702F73CF901A96654,
F522EFC802F73CF901A96654,
F522EFC902F73CF901A96654,
F522EFCA02F73CF901A96654,
F522EFCB02F73CF901A96654,
);
isa = PBXGroup;
name = widget;

View File

@ -287,7 +287,8 @@ BookmarksService::BookmarkChanged(nsIContent* aItem, bool shouldFlush = true)
NSMenuItem* childItem = [menu itemWithTag: contentID];
nsAutoString name;
aItem->GetAttr(kNameSpaceID_None, gNameAtom, name);
[childItem setTitle: [NSString stringWith_nsAString: name]];
NSString* bookmarkTitle = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
[childItem setTitle: bookmarkTitle];
}
}
@ -740,7 +741,7 @@ BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent
{
nsAutoString name;
aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
NSString* title = [NSString stringWith_nsAString: name];
NSString* title = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
// Create a menu or menu item for the child.
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];

View File

@ -35,6 +35,8 @@
*
* ***** END LICENSE BLOCK ***** */
#import "NSString+Utils.h"
#import "CHBrowserWrapper.h"
#import "BrowserWindowController.h"
#import "BookmarksService.h"
@ -358,7 +360,7 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
NSString* newTitle = nil;
if (mOffline) {
if (title && ![title isEqualToString:@""])
newTitle = [title stringByAppendingString: @" [Working Offline]"];
newTitle = [title stringByAppendingString: @" [Working Offline]"]; // XXX localize me
else
newTitle = [NSString stringWithString:@"Untitled [Working Offline]"];
mTitle = [newTitle retain];
@ -368,12 +370,13 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
title = [NSString stringWithString:NSLocalizedString(@"UntitledPageTitle", @"")];
mTitle = [title retain];
}
if ( mIsPrimary && mWindowController )
[[mWindowController window] setTitle:mTitle];
[[mWindowController window] setTitle:[mTitle stringByTruncatingTo:80 at:kTruncateAtEnd]];
// Always set the tab.
if (title && ![title isEqualToString:@""])
[mTab setLabel:title];
[mTab setLabel:title]; // tab titles get truncated when setting them to tabs
else
[mTab setLabel:NSLocalizedString(@"UntitledPageTitle", @"")];
}

View File

@ -180,7 +180,7 @@ ContentClickListener::MouseDown(nsIDOMEvent* aEvent)
option->GetLabel(text);
if (text.IsEmpty())
option->GetText(text);
NSString* title = [NSString stringWith_nsAString: text];
NSString* title = [[NSString stringWith_nsAString: text] stringByTruncatingTo:75 at:kTruncateAtMiddle];
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
[menu addItem: menuItem];
[menuItem setTag: contentID];

View File

@ -41,9 +41,7 @@ static const int kDividerTag = 4000;
// the maximum number of history entry menuitems to display
static const int kMaxItems = 15;
// the maximum number of characters in a menu title before cropping it
static const unsigned int kMaxTitleLength = 60;
// the ellipsis string to insert into cropped strings
static const NSString *kEllipsis = @"...";
static const unsigned int kMaxTitleLength = 80;
@implementation CHGoMenu
@ -134,21 +132,7 @@ static const NSString *kEllipsis = @"...";
nsXPIDLString textStr;
entry->GetTitle(getter_Copies(textStr));
NSString* title = [NSString stringWith_nsAString: textStr];
// if the title is too long, crop it in the middle
if ([title length] > kMaxTitleLength) {
NSMutableString *croppedTitle = [NSMutableString stringWithCapacity:kMaxTitleLength+[kEllipsis length]];
int len1 = kMaxTitleLength/2;
int len2 = kMaxTitleLength - len1;
NSString *part1 = [title substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [title substringWithRange:NSMakeRange([title length]-len2, len2)];
[croppedTitle appendString:part1];
[croppedTitle appendString:kEllipsis];
[croppedTitle appendString:part2];
title = croppedTitle;
}
NSString* title = [[NSString stringWith_nsAString: textStr] stringByTruncatingTo:kMaxTitleLength at:kTruncateAtMiddle];
NSMenuItem *newItem = [self addItemWithTitle:title action:@selector(historyItemAction:) keyEquivalent:@""];
[newItem setTarget:self];
[newItem setTag:kDividerTag+1+i];

View File

@ -40,6 +40,14 @@
class nsAString;
typedef enum
{
kTruncateAtStart,
kTruncateAtMiddle,
kTruncateAtEnd
} ETruncationType;
// a category to extend NSString
@interface NSString (ChimeraStringUtils)
@ -49,5 +57,6 @@ class nsAString;
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet;
- (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet withString:(NSString*)string;
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType;
@end

View File

@ -117,4 +117,53 @@
return cleanString;
}
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
{
unichar ellipsisChar = 0x2026;
NSString* ellipsisString = [NSString stringWithCharacters:&ellipsisChar length:1]; // @"...";
if ([self length] > maxCharacters)
{
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
switch (truncationType)
{
case kTruncateAtStart:
[croppedString appendString:ellipsisString];
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
break;
case kTruncateAtMiddle:
{
int len1 = maxCharacters / 2;
int len2 = maxCharacters - len1;
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
[croppedString appendString:part1];
[croppedString appendString:ellipsisString];
[croppedString appendString:part2];
}
break;
case kTruncateAtEnd:
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
[croppedString appendString:ellipsisString];
break;
default:
#if DEBUG
NSLog(@"Unknown truncation type in stringByTruncatingTo::");
#endif
break;
}
return croppedString;
}
else
{
return [[self copy] autorelease];
}
}
@end

View File

@ -287,7 +287,8 @@ BookmarksService::BookmarkChanged(nsIContent* aItem, bool shouldFlush = true)
NSMenuItem* childItem = [menu itemWithTag: contentID];
nsAutoString name;
aItem->GetAttr(kNameSpaceID_None, gNameAtom, name);
[childItem setTitle: [NSString stringWith_nsAString: name]];
NSString* bookmarkTitle = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
[childItem setTitle: bookmarkTitle];
}
}
@ -740,7 +741,7 @@ BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent
{
nsAutoString name;
aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
NSString* title = [NSString stringWith_nsAString: name];
NSString* title = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
// Create a menu or menu item for the child.
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];

View File

@ -35,6 +35,8 @@
*
* ***** END LICENSE BLOCK ***** */
#import "NSString+Utils.h"
#import "CHBrowserWrapper.h"
#import "BrowserWindowController.h"
#import "BookmarksService.h"
@ -358,7 +360,7 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
NSString* newTitle = nil;
if (mOffline) {
if (title && ![title isEqualToString:@""])
newTitle = [title stringByAppendingString: @" [Working Offline]"];
newTitle = [title stringByAppendingString: @" [Working Offline]"]; // XXX localize me
else
newTitle = [NSString stringWithString:@"Untitled [Working Offline]"];
mTitle = [newTitle retain];
@ -368,12 +370,13 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
title = [NSString stringWithString:NSLocalizedString(@"UntitledPageTitle", @"")];
mTitle = [title retain];
}
if ( mIsPrimary && mWindowController )
[[mWindowController window] setTitle:mTitle];
[[mWindowController window] setTitle:[mTitle stringByTruncatingTo:80 at:kTruncateAtEnd]];
// Always set the tab.
if (title && ![title isEqualToString:@""])
[mTab setLabel:title];
[mTab setLabel:title]; // tab titles get truncated when setting them to tabs
else
[mTab setLabel:NSLocalizedString(@"UntitledPageTitle", @"")];
}

View File

@ -41,9 +41,7 @@ static const int kDividerTag = 4000;
// the maximum number of history entry menuitems to display
static const int kMaxItems = 15;
// the maximum number of characters in a menu title before cropping it
static const unsigned int kMaxTitleLength = 60;
// the ellipsis string to insert into cropped strings
static const NSString *kEllipsis = @"...";
static const unsigned int kMaxTitleLength = 80;
@implementation CHGoMenu
@ -134,21 +132,7 @@ static const NSString *kEllipsis = @"...";
nsXPIDLString textStr;
entry->GetTitle(getter_Copies(textStr));
NSString* title = [NSString stringWith_nsAString: textStr];
// if the title is too long, crop it in the middle
if ([title length] > kMaxTitleLength) {
NSMutableString *croppedTitle = [NSMutableString stringWithCapacity:kMaxTitleLength+[kEllipsis length]];
int len1 = kMaxTitleLength/2;
int len2 = kMaxTitleLength - len1;
NSString *part1 = [title substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [title substringWithRange:NSMakeRange([title length]-len2, len2)];
[croppedTitle appendString:part1];
[croppedTitle appendString:kEllipsis];
[croppedTitle appendString:part2];
title = croppedTitle;
}
NSString* title = [[NSString stringWith_nsAString: textStr] stringByTruncatingTo:kMaxTitleLength at:kTruncateAtMiddle];
NSMenuItem *newItem = [self addItemWithTitle:title action:@selector(historyItemAction:) keyEquivalent:@""];
[newItem setTarget:self];
[newItem setTag:kDividerTag+1+i];

View File

@ -1644,6 +1644,48 @@
settings = {
};
};
F522EFC502F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseClipboard.cpp;
path = ../widget/src/xpwidgets/nsBaseClipboard.cpp;
refType = 2;
};
F522EFC602F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseDragService.cpp;
path = ../widget/src/xpwidgets/nsBaseDragService.cpp;
refType = 2;
};
F522EFC702F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseWidget.cpp;
path = ../widget/src/xpwidgets/nsBaseWidget.cpp;
refType = 2;
};
F522EFC802F73CF901A96654 = {
isa = PBXFileReference;
name = nsClipboardHelper.cpp;
path = ../widget/src/xpwidgets/nsClipboardHelper.cpp;
refType = 2;
};
F522EFC902F73CF901A96654 = {
isa = PBXFileReference;
name = nsDataFlavor.cpp;
path = ../widget/src/xpwidgets/nsDataFlavor.cpp;
refType = 2;
};
F522EFCA02F73CF901A96654 = {
isa = PBXFileReference;
name = nsPrimitiveHelpers.cpp;
path = ../widget/src/xpwidgets/nsPrimitiveHelpers.cpp;
refType = 2;
};
F522EFCB02F73CF901A96654 = {
isa = PBXFileReference;
name = nsTransferable.cpp;
path = ../widget/src/xpwidgets/nsTransferable.cpp;
refType = 2;
};
F5247C2D02289FD9013DD99A = {
isa = PBXFileReference;
path = alert.nib;
@ -7422,6 +7464,13 @@
F5EA337C02EF889001A96654,
F5EA337D02EF889001A96654,
F5EA337E02EF889001A96654,
F522EFC502F73CF901A96654,
F522EFC602F73CF901A96654,
F522EFC702F73CF901A96654,
F522EFC802F73CF901A96654,
F522EFC902F73CF901A96654,
F522EFCA02F73CF901A96654,
F522EFCB02F73CF901A96654,
);
isa = PBXGroup;
name = widget;

View File

@ -180,7 +180,7 @@ ContentClickListener::MouseDown(nsIDOMEvent* aEvent)
option->GetLabel(text);
if (text.IsEmpty())
option->GetText(text);
NSString* title = [NSString stringWith_nsAString: text];
NSString* title = [[NSString stringWith_nsAString: text] stringByTruncatingTo:75 at:kTruncateAtMiddle];
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
[menu addItem: menuItem];
[menuItem setTag: contentID];

View File

@ -40,6 +40,14 @@
class nsAString;
typedef enum
{
kTruncateAtStart,
kTruncateAtMiddle,
kTruncateAtEnd
} ETruncationType;
// a category to extend NSString
@interface NSString (ChimeraStringUtils)
@ -49,5 +57,6 @@ class nsAString;
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet;
- (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet withString:(NSString*)string;
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType;
@end

View File

@ -117,4 +117,53 @@
return cleanString;
}
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
{
unichar ellipsisChar = 0x2026;
NSString* ellipsisString = [NSString stringWithCharacters:&ellipsisChar length:1]; // @"...";
if ([self length] > maxCharacters)
{
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
switch (truncationType)
{
case kTruncateAtStart:
[croppedString appendString:ellipsisString];
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
break;
case kTruncateAtMiddle:
{
int len1 = maxCharacters / 2;
int len2 = maxCharacters - len1;
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
[croppedString appendString:part1];
[croppedString appendString:ellipsisString];
[croppedString appendString:part2];
}
break;
case kTruncateAtEnd:
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
[croppedString appendString:ellipsisString];
break;
default:
#if DEBUG
NSLog(@"Unknown truncation type in stringByTruncatingTo::");
#endif
break;
}
return croppedString;
}
else
{
return [[self copy] autorelease];
}
}
@end

View File

@ -1644,6 +1644,48 @@
settings = {
};
};
F522EFC502F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseClipboard.cpp;
path = ../widget/src/xpwidgets/nsBaseClipboard.cpp;
refType = 2;
};
F522EFC602F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseDragService.cpp;
path = ../widget/src/xpwidgets/nsBaseDragService.cpp;
refType = 2;
};
F522EFC702F73CF901A96654 = {
isa = PBXFileReference;
name = nsBaseWidget.cpp;
path = ../widget/src/xpwidgets/nsBaseWidget.cpp;
refType = 2;
};
F522EFC802F73CF901A96654 = {
isa = PBXFileReference;
name = nsClipboardHelper.cpp;
path = ../widget/src/xpwidgets/nsClipboardHelper.cpp;
refType = 2;
};
F522EFC902F73CF901A96654 = {
isa = PBXFileReference;
name = nsDataFlavor.cpp;
path = ../widget/src/xpwidgets/nsDataFlavor.cpp;
refType = 2;
};
F522EFCA02F73CF901A96654 = {
isa = PBXFileReference;
name = nsPrimitiveHelpers.cpp;
path = ../widget/src/xpwidgets/nsPrimitiveHelpers.cpp;
refType = 2;
};
F522EFCB02F73CF901A96654 = {
isa = PBXFileReference;
name = nsTransferable.cpp;
path = ../widget/src/xpwidgets/nsTransferable.cpp;
refType = 2;
};
F5247C2D02289FD9013DD99A = {
isa = PBXFileReference;
path = alert.nib;
@ -7422,6 +7464,13 @@
F5EA337C02EF889001A96654,
F5EA337D02EF889001A96654,
F5EA337E02EF889001A96654,
F522EFC502F73CF901A96654,
F522EFC602F73CF901A96654,
F522EFC702F73CF901A96654,
F522EFC802F73CF901A96654,
F522EFC902F73CF901A96654,
F522EFCA02F73CF901A96654,
F522EFCB02F73CF901A96654,
);
isa = PBXGroup;
name = widget;

View File

@ -287,7 +287,8 @@ BookmarksService::BookmarkChanged(nsIContent* aItem, bool shouldFlush = true)
NSMenuItem* childItem = [menu itemWithTag: contentID];
nsAutoString name;
aItem->GetAttr(kNameSpaceID_None, gNameAtom, name);
[childItem setTitle: [NSString stringWith_nsAString: name]];
NSString* bookmarkTitle = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
[childItem setTitle: bookmarkTitle];
}
}
@ -740,7 +741,7 @@ BookmarksService::AddMenuBookmark(NSMenu* aMenu, nsIContent* aParent, nsIContent
{
nsAutoString name;
aChild->GetAttr(kNameSpaceID_None, gNameAtom, name);
NSString* title = [NSString stringWith_nsAString: name];
NSString* title = [[NSString stringWith_nsAString: name] stringByTruncatingTo:80 at:kTruncateAtMiddle];
// Create a menu or menu item for the child.
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];

View File

@ -35,6 +35,8 @@
*
* ***** END LICENSE BLOCK ***** */
#import "NSString+Utils.h"
#import "CHBrowserWrapper.h"
#import "BrowserWindowController.h"
#import "BookmarksService.h"
@ -358,7 +360,7 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
NSString* newTitle = nil;
if (mOffline) {
if (title && ![title isEqualToString:@""])
newTitle = [title stringByAppendingString: @" [Working Offline]"];
newTitle = [title stringByAppendingString: @" [Working Offline]"]; // XXX localize me
else
newTitle = [NSString stringWithString:@"Untitled [Working Offline]"];
mTitle = [newTitle retain];
@ -368,12 +370,13 @@ static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
title = [NSString stringWithString:NSLocalizedString(@"UntitledPageTitle", @"")];
mTitle = [title retain];
}
if ( mIsPrimary && mWindowController )
[[mWindowController window] setTitle:mTitle];
[[mWindowController window] setTitle:[mTitle stringByTruncatingTo:80 at:kTruncateAtEnd]];
// Always set the tab.
if (title && ![title isEqualToString:@""])
[mTab setLabel:title];
[mTab setLabel:title]; // tab titles get truncated when setting them to tabs
else
[mTab setLabel:NSLocalizedString(@"UntitledPageTitle", @"")];
}

View File

@ -180,7 +180,7 @@ ContentClickListener::MouseDown(nsIDOMEvent* aEvent)
option->GetLabel(text);
if (text.IsEmpty())
option->GetText(text);
NSString* title = [NSString stringWith_nsAString: text];
NSString* title = [[NSString stringWith_nsAString: text] stringByTruncatingTo:75 at:kTruncateAtMiddle];
NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
[menu addItem: menuItem];
[menuItem setTag: contentID];

View File

@ -41,9 +41,7 @@ static const int kDividerTag = 4000;
// the maximum number of history entry menuitems to display
static const int kMaxItems = 15;
// the maximum number of characters in a menu title before cropping it
static const unsigned int kMaxTitleLength = 60;
// the ellipsis string to insert into cropped strings
static const NSString *kEllipsis = @"...";
static const unsigned int kMaxTitleLength = 80;
@implementation CHGoMenu
@ -134,21 +132,7 @@ static const NSString *kEllipsis = @"...";
nsXPIDLString textStr;
entry->GetTitle(getter_Copies(textStr));
NSString* title = [NSString stringWith_nsAString: textStr];
// if the title is too long, crop it in the middle
if ([title length] > kMaxTitleLength) {
NSMutableString *croppedTitle = [NSMutableString stringWithCapacity:kMaxTitleLength+[kEllipsis length]];
int len1 = kMaxTitleLength/2;
int len2 = kMaxTitleLength - len1;
NSString *part1 = [title substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [title substringWithRange:NSMakeRange([title length]-len2, len2)];
[croppedTitle appendString:part1];
[croppedTitle appendString:kEllipsis];
[croppedTitle appendString:part2];
title = croppedTitle;
}
NSString* title = [[NSString stringWith_nsAString: textStr] stringByTruncatingTo:kMaxTitleLength at:kTruncateAtMiddle];
NSMenuItem *newItem = [self addItemWithTitle:title action:@selector(historyItemAction:) keyEquivalent:@""];
[newItem setTarget:self];
[newItem setTag:kDividerTag+1+i];

View File

@ -40,6 +40,14 @@
class nsAString;
typedef enum
{
kTruncateAtStart,
kTruncateAtMiddle,
kTruncateAtEnd
} ETruncationType;
// a category to extend NSString
@interface NSString (ChimeraStringUtils)
@ -49,5 +57,6 @@ class nsAString;
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet;
- (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet withString:(NSString*)string;
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType;
@end

View File

@ -117,4 +117,53 @@
return cleanString;
}
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
{
unichar ellipsisChar = 0x2026;
NSString* ellipsisString = [NSString stringWithCharacters:&ellipsisChar length:1]; // @"...";
if ([self length] > maxCharacters)
{
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
switch (truncationType)
{
case kTruncateAtStart:
[croppedString appendString:ellipsisString];
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
break;
case kTruncateAtMiddle:
{
int len1 = maxCharacters / 2;
int len2 = maxCharacters - len1;
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
[croppedString appendString:part1];
[croppedString appendString:ellipsisString];
[croppedString appendString:part2];
}
break;
case kTruncateAtEnd:
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
[croppedString appendString:ellipsisString];
break;
default:
#if DEBUG
NSLog(@"Unknown truncation type in stringByTruncatingTo::");
#endif
break;
}
return croppedString;
}
else
{
return [[self copy] autorelease];
}
}
@end