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
Родитель 173ef8e602
Коммит f8304d9717
28 изменённых файлов: 472 добавлений и 380 удалений

Просмотреть файл

@ -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];

Просмотреть файл

@ -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", @"")];
}

Просмотреть файл

@ -1,160 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2002 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Joe Hewitt <hewitt@netscape.com> (Original Author)
*/
#import "NSString+Utils.h"
#import "CHGoMenu.h"
#import "MainController.h"
#import "BrowserWindowController.h"
#import "CHBrowserWrapper.h"
#import "CHBrowserView.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsIWebBrowser.h"
#include "nsISHistory.h"
#include "nsIWebNavigation.h"
#include "nsIHistoryEntry.h"
#include "nsCRT.h"
// the tag of the separator after which to insert history menu items
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 = @"...";
@implementation CHGoMenu
- (void) update
{
[self emptyHistoryItems];
[self addHistoryItems];
[super update];
}
- (nsIWebNavigation*) currentWebNavigation
{
// get controller for current window
BrowserWindowController *controller = [(MainController *)[NSApp delegate] getMainWindowBrowserController];
if (!controller) return nsnull;
// get web navigation for current browser
CHBrowserWrapper* wrapper = [controller getBrowserWrapper];
if (!wrapper) return nsnull;
CHBrowserView* view = [wrapper getBrowserView];
if (!view) return nsnull;
nsCOMPtr<nsIWebBrowser> webBrowser = [view getWebBrowser];
if (!webBrowser) return nsnull;
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(webBrowser));
return webNav.get();
}
- (void) historyItemAction:(id)sender
{
// get web navigation for current browser
nsCOMPtr<nsIWebNavigation> webNav = [self currentWebNavigation];
if (!webNav) return;
// browse to the history entry for the menuitem that was selected
PRInt32 historyIndex = ([sender tag] - 1) - kDividerTag;
webNav->GotoIndex(historyIndex);
}
- (void) emptyHistoryItems
{
// remove every history item after the insertion point
int insertionIndex = [self indexOfItemWithTag:kDividerTag];
for (int i = [self numberOfItems]-1; i > insertionIndex ; --i) {
[self removeItemAtIndex:i];
}
}
- (void) addHistoryItems
{
// get session history for current browser
nsCOMPtr<nsIWebNavigation> webNav = [self currentWebNavigation];
if (!webNav) return;
nsCOMPtr<nsISHistory> sessionHistory;
webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
PRInt32 count;
sessionHistory->GetCount(&count);
PRInt32 currentIndex;
sessionHistory->GetIndex(&currentIndex);
// determine the range of items to display
int rangeStart, rangeEnd;
int above = kMaxItems/2;
int below = (kMaxItems-above)-1;
if (count <= kMaxItems) {
// if the whole history fits within our menu, fit range to show all
rangeStart = count-1;
rangeEnd = 0;
} else {
// if the whole history is too large for menu, try to put current index as close to
// the middle of the list as possible, so the user can see both back and forward in session
rangeStart = currentIndex + above;
rangeEnd = currentIndex - below;
if (rangeStart >= count-1) {
rangeEnd -= (rangeStart-count)+1; // shift overflow to the end
rangeStart = count-1;
} else if (rangeEnd < 0) {
rangeStart -= rangeEnd; // shift overflow to the start
rangeEnd = 0;
}
}
// create a new menu item for each history entry (up to MAX_MENUITEM entries)
for (PRInt32 i = rangeStart; i >= rangeEnd; --i) {
nsCOMPtr<nsIHistoryEntry> entry;
sessionHistory->GetEntryAtIndex(i, PR_FALSE, getter_AddRefs(entry));
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;
}
NSMenuItem *newItem = [self addItemWithTitle:title action:@selector(historyItemAction:) keyEquivalent:@""];
[newItem setTarget:self];
[newItem setTag:kDividerTag+1+i];
if (currentIndex == i)
[newItem setState:NSOnState];
}
}
@end

Просмотреть файл

@ -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;

Просмотреть файл

@ -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];

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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;

Просмотреть файл

@ -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];

Просмотреть файл

@ -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", @"")];
}

Просмотреть файл

@ -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];

Просмотреть файл

@ -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];

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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];

Просмотреть файл

@ -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", @"")];
}

Просмотреть файл

@ -1,160 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 2002 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Joe Hewitt <hewitt@netscape.com> (Original Author)
*/
#import "NSString+Utils.h"
#import "CHGoMenu.h"
#import "MainController.h"
#import "BrowserWindowController.h"
#import "CHBrowserWrapper.h"
#import "CHBrowserView.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsIWebBrowser.h"
#include "nsISHistory.h"
#include "nsIWebNavigation.h"
#include "nsIHistoryEntry.h"
#include "nsCRT.h"
// the tag of the separator after which to insert history menu items
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 = @"...";
@implementation CHGoMenu
- (void) update
{
[self emptyHistoryItems];
[self addHistoryItems];
[super update];
}
- (nsIWebNavigation*) currentWebNavigation
{
// get controller for current window
BrowserWindowController *controller = [(MainController *)[NSApp delegate] getMainWindowBrowserController];
if (!controller) return nsnull;
// get web navigation for current browser
CHBrowserWrapper* wrapper = [controller getBrowserWrapper];
if (!wrapper) return nsnull;
CHBrowserView* view = [wrapper getBrowserView];
if (!view) return nsnull;
nsCOMPtr<nsIWebBrowser> webBrowser = [view getWebBrowser];
if (!webBrowser) return nsnull;
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(webBrowser));
return webNav.get();
}
- (void) historyItemAction:(id)sender
{
// get web navigation for current browser
nsCOMPtr<nsIWebNavigation> webNav = [self currentWebNavigation];
if (!webNav) return;
// browse to the history entry for the menuitem that was selected
PRInt32 historyIndex = ([sender tag] - 1) - kDividerTag;
webNav->GotoIndex(historyIndex);
}
- (void) emptyHistoryItems
{
// remove every history item after the insertion point
int insertionIndex = [self indexOfItemWithTag:kDividerTag];
for (int i = [self numberOfItems]-1; i > insertionIndex ; --i) {
[self removeItemAtIndex:i];
}
}
- (void) addHistoryItems
{
// get session history for current browser
nsCOMPtr<nsIWebNavigation> webNav = [self currentWebNavigation];
if (!webNav) return;
nsCOMPtr<nsISHistory> sessionHistory;
webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
PRInt32 count;
sessionHistory->GetCount(&count);
PRInt32 currentIndex;
sessionHistory->GetIndex(&currentIndex);
// determine the range of items to display
int rangeStart, rangeEnd;
int above = kMaxItems/2;
int below = (kMaxItems-above)-1;
if (count <= kMaxItems) {
// if the whole history fits within our menu, fit range to show all
rangeStart = count-1;
rangeEnd = 0;
} else {
// if the whole history is too large for menu, try to put current index as close to
// the middle of the list as possible, so the user can see both back and forward in session
rangeStart = currentIndex + above;
rangeEnd = currentIndex - below;
if (rangeStart >= count-1) {
rangeEnd -= (rangeStart-count)+1; // shift overflow to the end
rangeStart = count-1;
} else if (rangeEnd < 0) {
rangeStart -= rangeEnd; // shift overflow to the start
rangeEnd = 0;
}
}
// create a new menu item for each history entry (up to MAX_MENUITEM entries)
for (PRInt32 i = rangeStart; i >= rangeEnd; --i) {
nsCOMPtr<nsIHistoryEntry> entry;
sessionHistory->GetEntryAtIndex(i, PR_FALSE, getter_AddRefs(entry));
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;
}
NSMenuItem *newItem = [self addItemWithTitle:title action:@selector(historyItemAction:) keyEquivalent:@""];
[newItem setTarget:self];
[newItem setTag:kDividerTag+1+i];
if (currentIndex == i)
[newItem setState:NSOnState];
}
}
@end

Просмотреть файл

@ -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;

Просмотреть файл

@ -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];

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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;

Просмотреть файл

@ -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];

Просмотреть файл

@ -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", @"")];
}

Просмотреть файл

@ -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];

Просмотреть файл

@ -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];

Просмотреть файл

@ -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

Просмотреть файл

@ -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