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/static_text_view.h" |
| 6 |
| 7 #include "base/memory/scoped_nsobject.h" |
| 8 |
| 9 namespace { |
| 10 // The baseline shift for text in the NSTextView. |
| 11 const float kTextBaselineShift = -1.0; |
| 12 } // namespace |
| 13 |
| 14 @implementation StaticTextView |
| 15 |
| 16 // Never draw the insertion point (otherwise, it shows up without any user |
| 17 // action if full keyboard accessibility is enabled). |
| 18 - (BOOL)shouldDrawInsertionPoint { |
| 19 return NO; |
| 20 } |
| 21 |
| 22 - (NSRange)selectionRangeForProposedRange:(NSRange)proposedSelRange |
| 23 granularity:(NSSelectionGranularity)granularity { |
| 24 // Do not allow selections. |
| 25 return NSMakeRange(0, 0); |
| 26 } |
| 27 |
| 28 // Convince NSTextView to not show an I-Beam cursor when the cursor is over the |
| 29 // text view but not over actual text. |
| 30 // |
| 31 // http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg10791.html |
| 32 // "NSTextView sets the cursor over itself dynamically, based on considerations |
| 33 // including the text under the cursor. It does so in -mouseEntered:, |
| 34 // -mouseMoved:, and -cursorUpdate:, so those would be points to consider |
| 35 // overriding." |
| 36 - (void)mouseMoved:(NSEvent*)e { |
| 37 [super mouseMoved:e]; |
| 38 [self fixupCursor]; |
| 39 } |
| 40 |
| 41 - (void)mouseEntered:(NSEvent*)e { |
| 42 [super mouseEntered:e]; |
| 43 [self fixupCursor]; |
| 44 } |
| 45 |
| 46 - (void)cursorUpdate:(NSEvent*)e { |
| 47 [super cursorUpdate:e]; |
| 48 [self fixupCursor]; |
| 49 } |
| 50 |
| 51 - (void)fixupCursor { |
| 52 if ([[NSCursor currentCursor] isEqual:[NSCursor IBeamCursor]]) |
| 53 [[NSCursor arrowCursor] set]; |
| 54 } |
| 55 |
| 56 - (void)setMessageAndLink:(NSString*)message |
| 57 withLink:(NSString*)link |
| 58 atOffset:(NSUInteger)linkOffset |
| 59 font:(NSFont*)font |
| 60 messageColor:(NSColor*)messageColor |
| 61 linkColor:(NSColor*)linkColor { |
| 62 // Create an attributes dictionary for the message and link. |
| 63 NSMutableDictionary* attributes = [NSMutableDictionary dictionary]; |
| 64 [attributes setObject:messageColor |
| 65 forKey:NSForegroundColorAttributeName]; |
| 66 [attributes setObject:[NSCursor arrowCursor] |
| 67 forKey:NSCursorAttributeName]; |
| 68 [attributes setObject:font |
| 69 forKey:NSFontAttributeName]; |
| 70 [attributes setObject:[NSNumber numberWithFloat:kTextBaselineShift] |
| 71 forKey:NSBaselineOffsetAttributeName]; |
| 72 |
| 73 // Create the attributed string for the message. |
| 74 scoped_nsobject<NSMutableAttributedString> attributedMessage( |
| 75 [[NSMutableAttributedString alloc] initWithString:message |
| 76 attributes:attributes]); |
| 77 |
| 78 if ([link length] != 0) { |
| 79 // Add additional attributes to style the link text appropriately as |
| 80 // well as linkify it. |
| 81 [attributes setObject:linkColor |
| 82 forKey:NSForegroundColorAttributeName]; |
| 83 [attributes setObject:[NSNumber numberWithBool:YES] |
| 84 forKey:NSUnderlineStyleAttributeName]; |
| 85 [attributes setObject:[NSCursor pointingHandCursor] |
| 86 forKey:NSCursorAttributeName]; |
| 87 [attributes setObject:[NSNumber numberWithInt:NSSingleUnderlineStyle] |
| 88 forKey:NSUnderlineStyleAttributeName]; |
| 89 [attributes setObject:[NSString string] // dummy value |
| 90 forKey:NSLinkAttributeName]; |
| 91 |
| 92 // Insert the link into the message at the appropriate offset. |
| 93 scoped_nsobject<NSAttributedString> attributedLink( |
| 94 [[NSAttributedString alloc] initWithString:link |
| 95 attributes:attributes]); |
| 96 [attributedMessage.get() insertAttributedString:attributedLink.get() |
| 97 atIndex:linkOffset]; |
| 98 // Ensure the TextView doesn't override the link style. |
| 99 [self setLinkTextAttributes:attributes]; |
| 100 } |
| 101 |
| 102 // Update the text view with the new text. |
| 103 [[self textStorage] setAttributedString:attributedMessage]; |
| 104 } |
| 105 |
| 106 @end |
OLD | NEW |