OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" |
| 6 |
| 7 #include "base/memory/scoped_nsobject.h" |
| 8 |
| 9 // The baseline shift for text in the NSTextView. |
| 10 const float kTextBaselineShift = -1.0; |
| 11 |
| 12 @interface HyperlinkTextView(Private) |
| 13 // Initialize the NSTextView properties for this subclass. |
| 14 - (void)configureTextView; |
| 15 |
| 16 // Change the current IBeamCursor to an arrowCursor. |
| 17 - (void)fixupCursor; |
| 18 @end |
| 19 |
| 20 @implementation HyperlinkTextView |
| 21 |
| 22 - (id)initWithCoder:(NSCoder*)decoder { |
| 23 if ((self = [super initWithCoder:decoder])) |
| 24 [self configureTextView]; |
| 25 return self; |
| 26 } |
| 27 |
| 28 - (id)initWithFrame:(NSRect)frameRect { |
| 29 if ((self = [super initWithFrame:frameRect])) |
| 30 [self configureTextView]; |
| 31 return self; |
| 32 } |
| 33 |
| 34 // Never draw the insertion point (otherwise, it shows up without any user |
| 35 // action if full keyboard accessibility is enabled). |
| 36 - (BOOL)shouldDrawInsertionPoint { |
| 37 return NO; |
| 38 } |
| 39 |
| 40 - (NSRange)selectionRangeForProposedRange:(NSRange)proposedSelRange |
| 41 granularity:(NSSelectionGranularity)granularity { |
| 42 // Do not allow selections. |
| 43 return NSMakeRange(0, 0); |
| 44 } |
| 45 |
| 46 // Convince NSTextView to not show an I-Beam cursor when the cursor is over the |
| 47 // text view but not over actual text. |
| 48 // |
| 49 // http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg10791.html |
| 50 // "NSTextView sets the cursor over itself dynamically, based on considerations |
| 51 // including the text under the cursor. It does so in -mouseEntered:, |
| 52 // -mouseMoved:, and -cursorUpdate:, so those would be points to consider |
| 53 // overriding." |
| 54 - (void)mouseMoved:(NSEvent*)e { |
| 55 [super mouseMoved:e]; |
| 56 [self fixupCursor]; |
| 57 } |
| 58 |
| 59 - (void)mouseEntered:(NSEvent*)e { |
| 60 [super mouseEntered:e]; |
| 61 [self fixupCursor]; |
| 62 } |
| 63 |
| 64 - (void)cursorUpdate:(NSEvent*)e { |
| 65 [super cursorUpdate:e]; |
| 66 [self fixupCursor]; |
| 67 } |
| 68 |
| 69 - (void)configureTextView { |
| 70 [self setEditable:NO]; |
| 71 [self setDrawsBackground:NO]; |
| 72 [self setHorizontallyResizable:NO]; |
| 73 [self setVerticallyResizable:NO]; |
| 74 } |
| 75 |
| 76 - (void)fixupCursor { |
| 77 if ([[NSCursor currentCursor] isEqual:[NSCursor IBeamCursor]]) |
| 78 [[NSCursor arrowCursor] set]; |
| 79 } |
| 80 |
| 81 - (void)setMessageAndLink:(NSString*)message |
| 82 withLink:(NSString*)link |
| 83 atOffset:(NSUInteger)linkOffset |
| 84 font:(NSFont*)font |
| 85 messageColor:(NSColor*)messageColor |
| 86 linkColor:(NSColor*)linkColor { |
| 87 // Create an attributes dictionary for the message and link. |
| 88 NSMutableDictionary* attributes = [NSMutableDictionary dictionary]; |
| 89 [attributes setObject:messageColor |
| 90 forKey:NSForegroundColorAttributeName]; |
| 91 [attributes setObject:[NSCursor arrowCursor] |
| 92 forKey:NSCursorAttributeName]; |
| 93 [attributes setObject:font |
| 94 forKey:NSFontAttributeName]; |
| 95 [attributes setObject:[NSNumber numberWithFloat:kTextBaselineShift] |
| 96 forKey:NSBaselineOffsetAttributeName]; |
| 97 |
| 98 // Create the attributed string for the message. |
| 99 scoped_nsobject<NSMutableAttributedString> attributedMessage( |
| 100 [[NSMutableAttributedString alloc] initWithString:message |
| 101 attributes:attributes]); |
| 102 |
| 103 if ([link length] != 0) { |
| 104 // Add additional attributes to style the link text appropriately as |
| 105 // well as linkify it. |
| 106 [attributes setObject:linkColor |
| 107 forKey:NSForegroundColorAttributeName]; |
| 108 [attributes setObject:[NSNumber numberWithBool:YES] |
| 109 forKey:NSUnderlineStyleAttributeName]; |
| 110 [attributes setObject:[NSCursor pointingHandCursor] |
| 111 forKey:NSCursorAttributeName]; |
| 112 [attributes setObject:[NSNumber numberWithInt:NSSingleUnderlineStyle] |
| 113 forKey:NSUnderlineStyleAttributeName]; |
| 114 [attributes setObject:[NSString string] // dummy value |
| 115 forKey:NSLinkAttributeName]; |
| 116 |
| 117 // Insert the link into the message at the appropriate offset. |
| 118 scoped_nsobject<NSAttributedString> attributedLink( |
| 119 [[NSAttributedString alloc] initWithString:link |
| 120 attributes:attributes]); |
| 121 [attributedMessage.get() insertAttributedString:attributedLink.get() |
| 122 atIndex:linkOffset]; |
| 123 // Ensure the TextView doesn't override the link style. |
| 124 [self setLinkTextAttributes:attributes]; |
| 125 } |
| 126 |
| 127 // Update the text view with the new text. |
| 128 [[self textStorage] setAttributedString:attributedMessage]; |
| 129 } |
| 130 |
| 131 @end |
OLD | NEW |