Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2010 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_button_cell.h"
      6 
      7 @interface HyperlinkButtonCell (Private)
      8 - (NSDictionary*)linkAttributres;
      9 - (void)customizeButtonCell;
     10 @end
     11 
     12 @implementation HyperlinkButtonCell
     13 @dynamic textColor;
     14 
     15 + (NSColor*)defaultTextColor {
     16   return [NSColor blueColor];
     17 }
     18 
     19 // Designated initializer.
     20 - (id)init {
     21   if ((self = [super init])) {
     22     [self customizeButtonCell];
     23   }
     24   return self;
     25 }
     26 
     27 // Initializer called when the cell is loaded from the NIB.
     28 - (id)initWithCoder:(NSCoder*)aDecoder {
     29   if ((self = [super initWithCoder:aDecoder])) {
     30     [self customizeButtonCell];
     31   }
     32   return self;
     33 }
     34 
     35 // Initializer for code-based creation.
     36 - (id)initTextCell:(NSString*)title {
     37   if ((self = [super initTextCell:title])) {
     38     [self customizeButtonCell];
     39   }
     40   return self;
     41 }
     42 
     43 // Because an NSButtonCell has multiple initializers, this method performs the
     44 // common cell customization code.
     45 - (void)customizeButtonCell {
     46   [self setBordered:NO];
     47   [self setTextColor:[HyperlinkButtonCell defaultTextColor]];
     48 
     49   CGFloat fontSize = [NSFont systemFontSizeForControlSize:[self controlSize]];
     50   NSFont* font = [NSFont controlContentFontOfSize:fontSize];
     51   [self setFont:font];
     52 
     53   // Do not change button appearance when we are pushed.
     54   [self setHighlightsBy:NSNoCellMask];
     55 
     56   // We need to set this so that we can override |-mouseEntered:| and
     57   // |-mouseExited:| to change the cursor style on hover states.
     58   [self setShowsBorderOnlyWhileMouseInside:YES];
     59 }
     60 
     61 - (void)setControlSize:(NSControlSize)size {
     62   [super setControlSize:size];
     63   [self customizeButtonCell];  // recompute |font|.
     64 }
     65 
     66 // Creates the NSDictionary of attributes for the attributed string.
     67 - (NSDictionary*)linkAttributes {
     68   NSUInteger underlineMask = NSUnderlinePatternSolid | NSUnderlineStyleSingle;
     69   scoped_nsobject<NSMutableParagraphStyle> paragraphStyle(
     70     [[NSParagraphStyle defaultParagraphStyle] mutableCopy]);
     71   [paragraphStyle setAlignment:[self alignment]];
     72 
     73   return [NSDictionary dictionaryWithObjectsAndKeys:
     74       [self textColor], NSForegroundColorAttributeName,
     75       [NSNumber numberWithInt:underlineMask], NSUnderlineStyleAttributeName,
     76       [self font], NSFontAttributeName,
     77       [NSCursor pointingHandCursor], NSCursorAttributeName,
     78       paragraphStyle.get(), NSParagraphStyleAttributeName,
     79       nil
     80   ];
     81 }
     82 
     83 // Override the drawing for the cell so that the custom style attributes
     84 // can always be applied and so that ellipses will appear when appropriate.
     85 - (NSRect)drawTitle:(NSAttributedString*)title
     86           withFrame:(NSRect)frame
     87              inView:(NSView*)controlView {
     88   NSDictionary* linkAttributes = [self linkAttributes];
     89   NSString* plainTitle = [title string];
     90   [plainTitle drawWithRect:frame
     91                    options:(NSStringDrawingUsesLineFragmentOrigin |
     92                             NSStringDrawingTruncatesLastVisibleLine)
     93                 attributes:linkAttributes];
     94   return frame;
     95 }
     96 
     97 // Override the default behavior to draw the border. Instead, change the cursor.
     98 - (void)mouseEntered:(NSEvent*)event {
     99   [[NSCursor pointingHandCursor] push];
    100 }
    101 
    102 - (void)mouseExited:(NSEvent*)event {
    103   [NSCursor pop];
    104 }
    105 
    106 // Setters and getters.
    107 - (NSColor*)textColor {
    108   return textColor_.get();
    109 }
    110 
    111 - (void)setTextColor:(NSColor*)color {
    112   textColor_.reset([color retain]);
    113 }
    114 
    115 @end
    116