diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-29 15:34:32 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-29 15:34:32 +0000 |
commit | c1275b92a99f3e4163c3cc133132e983a424cc90 (patch) | |
tree | f271e81aa7f157d7328f30d8fd97c738e7cb22cc /chrome/browser/cocoa/hyperlink_button_cell.mm | |
parent | a5f44c93e83cb964fa4c0529d6333f50ee5e73cf (diff) | |
download | chromium_src-c1275b92a99f3e4163c3cc133132e983a424cc90.zip chromium_src-c1275b92a99f3e4163c3cc133132e983a424cc90.tar.gz chromium_src-c1275b92a99f3e4163c3cc133132e983a424cc90.tar.bz2 |
[Mac] Create HyperlinkButtonCell and make the "Get themes" button an instance
* Creates a HyperlinkButtonCell that can be used in conjuction with a NSButton
to display the control as a hyperlink (blue underliend text), rather than
a normal Cocoa control.
* Switch the "Get themes..." button in Preferences to be one.
BUG=17989,21414
TEST=Preferences-->Personal Stuff, "Get themes..." is a hyperlink.
Review URL: http://codereview.chromium.org/248019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27479 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa/hyperlink_button_cell.mm')
-rw-r--r-- | chrome/browser/cocoa/hyperlink_button_cell.mm | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/hyperlink_button_cell.mm b/chrome/browser/cocoa/hyperlink_button_cell.mm new file mode 100644 index 0000000..8493a8c --- /dev/null +++ b/chrome/browser/cocoa/hyperlink_button_cell.mm @@ -0,0 +1,105 @@ +// 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; + +// 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:[NSColor blueColor]]; + + 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; + 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, 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 { + NSAttributedString* attrString = + [[NSAttributedString alloc] initWithString:[title string] + attributes:[self linkAttributes]]; + [attrString autorelease]; + return [super drawTitle:attrString 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); +} + +@end |