summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/hyperlink_button_cell.mm
blob: 91d3a51a077212e503fbcc6447c547299854aeff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2009 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.

#import "chrome/browser/cocoa/hyperlink_button_cell.h"

@interface HyperlinkButtonCell (Private)
- (NSDictionary*)linkAttributres;
- (void)customizeButtonCell;
@end

@implementation HyperlinkButtonCell
@dynamic textColor;

+ (NSColor*)defaultTextColor {
  return [NSColor blueColor];
}

// Designated initializer.
- (id)init {
  if ((self = [super init])) {
    [self customizeButtonCell];
  }
  return self;
}

// Initializer called when the cell is loaded from the NIB.
- (id)initWithCoder:(NSCoder*)aDecoder {
  if ((self = [super initWithCoder:aDecoder])) {
    [self customizeButtonCell];
  }
  return self;
}

// Initializer for code-based creation.
- (id)initTextCell:(NSString*)title {
  if ((self = [super initTextCell:title])) {
    [self customizeButtonCell];
  }
  return self;
}

// Because an NSButtonCell has multiple initializers, this method performs the
// common cell customization code.
- (void)customizeButtonCell {
  [self setBordered:NO];
  [self setTextColor:[HyperlinkButtonCell defaultTextColor]];

  CGFloat fontSize = [NSFont systemFontSizeForControlSize:[self controlSize]];
  NSFont* font = [NSFont controlContentFontOfSize:fontSize];
  [self setFont:font];

  // Do not change button appearance when we are pushed.
  // TODO(rsesek): Change text color to red?
  [self setHighlightsBy:NSNoCellMask];

  // We need to set this so that we can override |-mouseEntered:| and
  // |-mouseExited:| to change the cursor style on hover states.
  [self setShowsBorderOnlyWhileMouseInside:YES];
}

// Creates the NSDictionary of attributes for the attributed string.
- (NSDictionary*)linkAttributes {
  NSUInteger underlineMask = NSUnderlinePatternSolid | NSUnderlineStyleSingle;
  scoped_nsobject<NSMutableParagraphStyle> paragraphStyle(
    [[NSParagraphStyle defaultParagraphStyle] mutableCopy]);
  [paragraphStyle setAlignment:[self alignment]];

  return [NSDictionary dictionaryWithObjectsAndKeys:
      [self textColor], NSForegroundColorAttributeName,
      [NSNumber numberWithInt:underlineMask], NSUnderlineStyleAttributeName,
      [self font], NSFontAttributeName,
      [NSCursor pointingHandCursor], NSCursorAttributeName,
      paragraphStyle.get(), NSParagraphStyleAttributeName,
      nil
  ];
}

// Override the drawing point for the cell so that the custom style attributes
// can always be applied.
- (NSRect)drawTitle:(NSAttributedString*)title
          withFrame:(NSRect)frame
             inView:(NSView*)controlView {
  scoped_nsobject<NSAttributedString> attrString(
      [[NSAttributedString alloc] initWithString:[title string]
                                      attributes:[self linkAttributes]]);
  return [super drawTitle:attrString.get() withFrame:frame inView:controlView];
}

// Override the default behavior to draw the border. Instead, change the cursor.
- (void)mouseEntered:(NSEvent*)event {
  [[NSCursor pointingHandCursor] push];
}

- (void)mouseExited:(NSEvent*)event {
  [NSCursor pop];
}

// Setters and getters.
- (NSColor*)textColor {
  return textColor_.get();
}

- (void)setTextColor:(NSColor*)color {
  textColor_.reset([color retain]);
}

@end