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