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.
5 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "ui/base/cocoa/nsview_additions.h"
10 // The baseline shift for text in the NSTextView.
11 const float kTextBaselineShift = -1.0;
13 @interface HyperlinkTextView(Private)
14 // Initialize the NSTextView properties for this subclass.
15 - (void)configureTextView;
17 // Change the current IBeamCursor to an arrowCursor.
21 @implementation HyperlinkTextView
23 @synthesize drawsBackgroundUsingSuperview = drawsBackgroundUsingSuperview_;
25 - (id)initWithCoder:(NSCoder*)decoder {
26 if ((self = [super initWithCoder:decoder]))
27 [self configureTextView];
31 - (id)initWithFrame:(NSRect)frameRect {
32 if ((self = [super initWithFrame:frameRect]))
33 [self configureTextView];
37 - (BOOL)acceptsFirstResponder {
38 return !refusesFirstResponder_;
41 - (void)drawViewBackgroundInRect:(NSRect)rect {
42 if (drawsBackgroundUsingSuperview_)
43 [self cr_drawUsingAncestor:[self superview] inRect:rect];
45 [super drawViewBackgroundInRect:rect];
48 // Never draw the insertion point (otherwise, it shows up without any user
49 // action if full keyboard accessibility is enabled).
50 - (BOOL)shouldDrawInsertionPoint {
54 - (NSRange)selectionRangeForProposedRange:(NSRange)proposedSelRange
55 granularity:(NSSelectionGranularity)granularity {
56 // Do not allow selections.
57 return NSMakeRange(0, 0);
60 // Convince NSTextView to not show an I-Beam cursor when the cursor is over the
61 // text view but not over actual text.
63 // http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg10791.html
64 // "NSTextView sets the cursor over itself dynamically, based on considerations
65 // including the text under the cursor. It does so in -mouseEntered:,
66 // -mouseMoved:, and -cursorUpdate:, so those would be points to consider
68 - (void)mouseMoved:(NSEvent*)e {
73 - (void)mouseEntered:(NSEvent*)e {
74 [super mouseEntered:e];
78 - (void)cursorUpdate:(NSEvent*)e {
79 [super cursorUpdate:e];
83 - (void)configureTextView {
84 [self setEditable:NO];
85 [self setDrawsBackground:NO];
86 [self setHorizontallyResizable:NO];
87 [self setVerticallyResizable:NO];
89 // When text is rendered, linkTextAttributes override anything set via
90 // addAttributes for text that has NSLinkAttributeName. Set to nil to allow
91 // custom attributes to take precedence.
92 [self setLinkTextAttributes:nil];
93 [self setDisplaysLinkToolTips:NO];
95 refusesFirstResponder_ = NO;
96 drawsBackgroundUsingSuperview_ = NO;
100 if ([[NSCursor currentCursor] isEqual:[NSCursor IBeamCursor]])
101 [[NSCursor arrowCursor] set];
104 - (void)setMessage:(NSString*)message
105 withFont:(NSFont*)font
106 messageColor:(NSColor*)messageColor {
107 // Create an attributes dictionary for the message and link.
108 NSDictionary* attributes = @{
109 NSForegroundColorAttributeName : messageColor,
110 NSCursorAttributeName : [NSCursor arrowCursor],
111 NSFontAttributeName : font,
112 NSBaselineOffsetAttributeName : @(kTextBaselineShift)
115 // Create the attributed string for the message.
116 base::scoped_nsobject<NSAttributedString> attributedMessage(
117 [[NSMutableAttributedString alloc] initWithString:message
118 attributes:attributes]);
120 // Update the text view with the new text.
121 [[self textStorage] setAttributedString:attributedMessage];
124 - (void)addLinkRange:(NSRange)range
126 linkColor:(NSColor*)linkColor {
127 NSDictionary* attributes = @{
128 NSForegroundColorAttributeName : linkColor,
129 NSUnderlineStyleAttributeName : @(YES),
130 NSCursorAttributeName : [NSCursor pointingHandCursor],
131 NSLinkAttributeName : name,
132 NSUnderlineStyleAttributeName : @(NSSingleUnderlineStyle)
135 [[self textStorage] addAttributes:attributes range:range];
138 - (void)setRefusesFirstResponder:(BOOL)refusesFirstResponder {
139 refusesFirstResponder_ = refusesFirstResponder;