Index: chrome/browser/ui/cocoa/tab_contents/sad_tab_view.mm |
diff --git a/chrome/browser/ui/cocoa/tab_contents/sad_tab_view.mm b/chrome/browser/ui/cocoa/tab_contents/sad_tab_view.mm |
index 53ffcd928b9198308b4e375c32b2e501159249d0..ec26414a2ce2431ac6ef9d3ab960f67e924d87cb 100644 |
--- a/chrome/browser/ui/cocoa/tab_contents/sad_tab_view.mm |
+++ b/chrome/browser/ui/cocoa/tab_contents/sad_tab_view.mm |
@@ -1,13 +1,19 @@ |
-// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
#include "chrome/browser/ui/cocoa/tab_contents/sad_tab_view.h" |
#include "base/logging.h" |
+#include "base/sys_string_conversions.h" |
#import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" |
+#include "chrome/browser/ui/cocoa/tab_contents/sad_tab_controller.h" |
+#include "chrome/common/url_constants.h" |
+#include "grit/generated_resources.h" |
#include "grit/theme_resources.h" |
#import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/base/l10n/l10n_util_mac.h" |
#include "ui/base/resource/resource_bundle.h" |
#include "ui/gfx/image/image.h" |
@@ -22,6 +28,56 @@ static const CGFloat kMessageLinkSpacing = 15; |
// Paddings on left and right of page. |
static const CGFloat kTabHorzMargin = 13; |
+// This simple subclass of |NSTextView| just doesn't show the (text) cursor |
+// (|NSTextView| displays the cursor with full keyboard accessibility enabled). |
+@interface HelpTextView : NSTextView |
+- (void)fixupCursor; |
+@end |
+ |
+@implementation HelpTextView |
+ |
+// Never draw the insertion point (otherwise, it shows up without any user |
+// action if full keyboard accessibility is enabled). |
+- (BOOL)shouldDrawInsertionPoint { |
+ return NO; |
+} |
+ |
+- (NSRange)selectionRangeForProposedRange:(NSRange)proposedSelRange |
+ granularity:(NSSelectionGranularity)granularity { |
+ // Do not allow selections. |
+ return NSMakeRange(0, 0); |
+} |
+ |
+// Convince NSTextView to not show an I-Beam cursor when the cursor is over the |
+// text view but not over actual text. |
+// |
+// http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg10791.html |
+// "NSTextView sets the cursor over itself dynamically, based on considerations |
+// including the text under the cursor. It does so in -mouseEntered:, |
+// -mouseMoved:, and -cursorUpdate:, so those would be points to consider |
+// overriding." |
+- (void)mouseMoved:(NSEvent*)e { |
+ [super mouseMoved:e]; |
+ [self fixupCursor]; |
+} |
+ |
+- (void)mouseEntered:(NSEvent*)e { |
+ [super mouseEntered:e]; |
+ [self fixupCursor]; |
+} |
+ |
+- (void)cursorUpdate:(NSEvent*)e { |
+ [super cursorUpdate:e]; |
+ [self fixupCursor]; |
+} |
+ |
+- (void)fixupCursor { |
+ if ([[NSCursor currentCursor] isEqual:[NSCursor IBeamCursor]]) |
+ [[NSCursor arrowCursor] set]; |
+} |
+ |
+@end |
+ |
@implementation SadTabView |
- (void)awakeFromNib { |
@@ -39,11 +95,7 @@ static const CGFloat kTabHorzMargin = 13; |
NSFont* messageFont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; |
[message_ setFont:messageFont]; |
Nico
2011/08/12 02:29:56
Instead, you would then DCHECK(controller_) here;
msw
2011/08/30 04:09:06
Done.
|
- // If necessary, set font and color for link. |
- if (linkButton_) { |
- [linkButton_ setFont:messageFont]; |
- [linkCell_ setTextColor:[NSColor whiteColor]]; |
- } |
+ [self initializeHelpText]; |
// Initialize background color. |
NSColor* backgroundColor = [[NSColor colorWithCalibratedRed:(35.0f/255.0f) |
@@ -106,23 +158,103 @@ static const CGFloat kTabHorzMargin = 13; |
titleY - kTitleMessageSpacing - NSHeight(messageFrame); |
[message_ setFrame:messageFrame]; |
- if (linkButton_) { |
+ // Set new frame for help text and link. |
+ if (help_.get()) { |
Nico
2011/08/12 02:29:56
The .get() shouldn't be necessary I think.
msw
2011/08/30 04:09:06
Done.
|
if (callSizeToFit) |
- [linkButton_ sizeToFit]; |
+ [help_.get() sizeToFit]; |
+ CGFloat helpHeight = [help_.get() frame].size.height; |
+ [help_.get() setFrameSize:NSMakeSize(maxWidth, helpHeight)]; |
// Set new frame origin for link. |
- NSRect linkFrame = [linkButton_ frame]; |
- CGFloat linkX = (maxWidth - NSWidth(linkFrame)) / 2; |
- CGFloat linkY = |
- NSMinY(messageFrame) - kMessageLinkSpacing - NSHeight(linkFrame); |
- [linkButton_ setFrameOrigin:NSMakePoint(linkX, linkY)]; |
+ NSRect helpFrame = [help_.get() frame]; |
+ CGFloat helpX = (maxWidth - NSWidth(helpFrame)) / 2; |
+ CGFloat helpY = |
+ NSMinY(messageFrame) - kMessageLinkSpacing - NSHeight(helpFrame); |
+ [help_.get() setFrameOrigin:NSMakePoint(helpX, helpY)]; |
} |
} |
-- (void)removeLinkButton { |
- if (linkButton_) { |
- [linkButton_ removeFromSuperview]; |
- linkButton_ = nil; |
+- (void)setController:(SadTabController*)controller { |
+ controller_ = controller; |
+} |
+ |
+ |
+- (void)removeHelpText { |
+ if (help_.get()) { |
+ [help_.get() removeFromSuperview]; |
+ help_.reset(nil); |
} |
} |
+- (void)initializeHelpText { |
Nico
2011/08/12 02:29:56
For stuff like this, it's very useful to say "Take
msw
2011/08/30 04:09:06
Done.
|
+ // Replace the help placeholder NSTextField with the real help NSTextView. |
+ // The former doesn't show links in a nice way, but the latter can't be added |
+ // in IB without a containing scroll view, so create the NSTextView |
+ // programmatically. |
+ help_.reset([[HelpTextView alloc] initWithFrame:[helpPlaceholder_ frame]]); |
+ [help_.get() setAutoresizingMask:[helpPlaceholder_ autoresizingMask]]; |
+ [[helpPlaceholder_ superview] |
+ replaceSubview:helpPlaceholder_ with:help_.get()]; |
+ helpPlaceholder_ = nil; // Now released. |
+ [help_.get() setDelegate:self]; |
+ [help_.get() setEditable:NO]; |
+ [help_.get() setDrawsBackground:NO]; |
+ [help_.get() setHorizontallyResizable:NO]; |
+ [help_.get() setVerticallyResizable:NO]; |
+ [help_.get() setAlignment:NSCenterTextAlignment]; |
+ |
+ // Get the help text and link. |
+ size_t linkOffset = 0; |
+ NSString* helpMessage(base::SysUTF16ToNSString(l10n_util::GetStringFUTF16( |
+ IDS_SAD_TAB_HELP_MESSAGE, string16(), &linkOffset))); |
+ NSString* helpLink = l10n_util::GetNSString(IDS_SAD_TAB_HELP_LINK); |
+ |
+ // Create an attributes dictionary for the help text and link. |
+ NSMutableDictionary* helpAttributes = [NSMutableDictionary dictionary]; |
+ [helpAttributes setObject:[NSCursor arrowCursor] |
+ forKey:NSCursorAttributeName]; |
+ NSFont* font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; |
+ [helpAttributes setObject:font |
+ forKey:NSFontAttributeName]; |
+ [helpAttributes setObject:[NSColor whiteColor] |
+ forKey:NSForegroundColorAttributeName]; |
Nico
2011/08/12 02:29:56
This is very similiar to setLabelToMessage:withLin
msw
2011/08/30 04:09:06
Done.
|
+ |
+ // Create the attributed string for the main help message text. |
+ scoped_nsobject<NSMutableAttributedString> helpText( |
+ [[NSMutableAttributedString alloc] initWithString:helpMessage]); |
+ [helpText.get() addAttributes:helpAttributes |
+ range:NSMakeRange(0, [helpText.get() length])]; |
+ |
+ // Add additional attributes to the embedded link. |
+ [helpAttributes setObject:[NSNumber numberWithBool:YES] |
+ forKey:NSUnderlineStyleAttributeName]; |
+ [helpAttributes setObject:[NSCursor pointingHandCursor] |
+ forKey:NSCursorAttributeName]; |
+ [helpAttributes setObject:[NSNumber numberWithInt:NSSingleUnderlineStyle] |
+ forKey:NSUnderlineStyleAttributeName]; |
+ [helpAttributes setObject:[NSString string] // dummy value |
+ forKey:NSLinkAttributeName]; |
+ |
+ // Insert the link text into the string at the appropriate offset. |
+ scoped_nsobject<NSAttributedString> attributedString( |
+ [[NSAttributedString alloc] initWithString:helpLink |
+ attributes:helpAttributes]); |
+ [helpText.get() insertAttributedString:attributedString.get() |
+ atIndex:linkOffset]; |
+ |
+ // Ensure the TextView doesn't override the link style. |
+ [help_.get() setLinkTextAttributes:helpAttributes]; |
+ |
+ // Update the label view with the new text. |
+ [[help_.get() textStorage] setAttributedString:helpText]; |
+} |
+ |
+// Called when someone clicks on the embedded link. |
+- (BOOL) textView:(NSTextView*)textView |
+ clickedOnLink:(id)link |
+ atIndex:(NSUInteger)charIndex { |
+ if (controller_) |
+ [controller_ openLearnMoreAboutCrashLink:nil]; |
+ return YES; |
+} |
+ |
@end |