| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/cocoa/gradient_button_cell.h" | 5 #include "chrome/browser/cocoa/gradient_button_cell.h" |
| 6 #import "third_party/GTM/AppKit/GTMTheme.h" | 6 #import "third_party/GTM/AppKit/GTMTheme.h" |
| 7 #import "base/scoped_nsobject.h" | 7 #import "base/scoped_nsobject.h" |
| 8 | 8 |
| 9 @implementation GradientButtonCell | 9 @implementation GradientButtonCell |
| 10 | 10 |
| 11 - (id)initTextCell:(NSString*)string { |
| 12 if ((self = [super initTextCell:string])) { |
| 13 shouldTheme_ = YES; |
| 14 } |
| 15 return self; |
| 16 } |
| 17 |
| 18 - (void)setShouldTheme:(BOOL)shouldTheme { |
| 19 shouldTheme_ = shouldTheme; |
| 20 } |
| 21 |
| 11 - (NSBackgroundStyle)interiorBackgroundStyle { | 22 - (NSBackgroundStyle)interiorBackgroundStyle { |
| 12 return [self isHighlighted] ? | 23 return [self isHighlighted] ? |
| 13 NSBackgroundStyleLowered : NSBackgroundStyleRaised; | 24 NSBackgroundStyleLowered : NSBackgroundStyleRaised; |
| 14 } | 25 } |
| 15 | 26 |
| 27 - (void)mouseEntered:(NSEvent *)theEvent { |
| 28 isMouseInside_ = YES; |
| 29 [[self controlView] setNeedsDisplay:YES]; |
| 30 } |
| 31 |
| 32 - (void)mouseExited:(NSEvent *)theEvent { |
| 33 isMouseInside_ = NO; |
| 34 [[self controlView] setNeedsDisplay:YES]; |
| 35 } |
| 36 |
| 37 - (BOOL)isMouseInside { |
| 38 return trackingArea_ && isMouseInside_; |
| 39 } |
| 40 |
| 41 // Since we have our own drawWithFrame:, we need to also have our own |
| 42 // logic for determining when the mouse is inside for honoring this |
| 43 // request. |
| 44 - (void)setShowsBorderOnlyWhileMouseInside:(BOOL)showOnly { |
| 45 [super setShowsBorderOnlyWhileMouseInside:showOnly]; |
| 46 if (showOnly) { |
| 47 if (trackingArea_.get()) { |
| 48 [self setShowsBorderOnlyWhileMouseInside:NO]; |
| 49 } |
| 50 trackingArea_.reset([[NSTrackingArea alloc] |
| 51 initWithRect:[[self controlView] |
| 52 bounds] |
| 53 options:(NSTrackingMouseEnteredAndExited | |
| 54 NSTrackingActiveInActiveApp) |
| 55 owner:self |
| 56 userInfo:nil]); |
| 57 [[self controlView] addTrackingArea:trackingArea_]; |
| 58 } else { |
| 59 if (trackingArea_) { |
| 60 [[self controlView] removeTrackingArea:trackingArea_]; |
| 61 trackingArea_.reset(nil); |
| 62 isMouseInside_ = NO; |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 16 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 67 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 17 // Constants from Cole. Will kConstant them once the feedback loop | 68 // Constants from Cole. Will kConstant them once the feedback loop |
| 18 // is complete. | 69 // is complete. |
| 19 NSRect drawFrame = NSInsetRect(cellFrame, 1.5, 1.5); | 70 NSRect drawFrame = NSInsetRect(cellFrame, 1.5, 1.5); |
| 20 NSRect innerFrame = NSInsetRect(cellFrame, 2, 2); | 71 NSRect innerFrame = NSInsetRect(cellFrame, 2, 2); |
| 21 ButtonType type = [[(NSControl*)controlView cell] tag]; | 72 ButtonType type = [[(NSControl*)controlView cell] tag]; |
| 22 switch (type) { | 73 switch (type) { |
| 23 case kRightButtonType: | 74 case kRightButtonType: |
| 24 drawFrame.origin.x -= 20; | 75 drawFrame.origin.x -= 20; |
| 25 innerFrame.origin.x -= 2; | 76 innerFrame.origin.x -= 2; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 43 [NSBezierPath bezierPathWithRoundedRect:drawFrame | 94 [NSBezierPath bezierPathWithRoundedRect:drawFrame |
| 44 xRadius:radius | 95 xRadius:radius |
| 45 yRadius:radius]; | 96 yRadius:radius]; |
| 46 NSBezierPath* outerPath = | 97 NSBezierPath* outerPath = |
| 47 [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(drawFrame, -1, -1) | 98 [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(drawFrame, -1, -1) |
| 48 xRadius:radius + 1 | 99 xRadius:radius + 1 |
| 49 yRadius:radius + 1]; | 100 yRadius:radius + 1]; |
| 50 | 101 |
| 51 // Stroke the borders and appropriate fill gradient. If we're borderless, | 102 // Stroke the borders and appropriate fill gradient. If we're borderless, |
| 52 // the only time we want to draw the inner gradient is if we're highlighted. | 103 // the only time we want to draw the inner gradient is if we're highlighted. |
| 53 if ([self isBordered] || pressed) { | 104 if (([self isBordered] && ![self showsBorderOnlyWhileMouseInside]) || |
| 105 pressed || |
| 106 [self isMouseInside]) { |
| 54 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.25] set]; | 107 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.25] set]; |
| 55 [outerPath stroke]; | 108 [outerPath stroke]; |
| 56 | 109 |
| 57 NSImage* backgroundImage = | 110 NSImage* backgroundImage = |
| 58 [theme backgroundImageForStyle:GTMThemeStyleToolBarButton state:YES]; | 111 [theme backgroundImageForStyle:GTMThemeStyleToolBarButton state:YES]; |
| 59 | 112 |
| 60 if (backgroundImage) { | 113 if (backgroundImage) { |
| 61 NSColor* patternColor = [NSColor colorWithPatternImage:backgroundImage]; | 114 NSColor* patternColor = [NSColor colorWithPatternImage:backgroundImage]; |
| 62 [patternColor set]; | 115 [patternColor set]; |
| 63 // Set the phase to match window. | 116 // Set the phase to match window. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 NSRectFillUsingOperation(NSInsetRect(borderRect, 0, 2), | 175 NSRectFillUsingOperation(NSInsetRect(borderRect, 0, 2), |
| 123 NSCompositeSourceOver); | 176 NSCompositeSourceOver); |
| 124 innerFrame.size.width -= 1.0; | 177 innerFrame.size.width -= 1.0; |
| 125 } | 178 } |
| 126 [self drawInteriorWithFrame:innerFrame inView:controlView]; | 179 [self drawInteriorWithFrame:innerFrame inView:controlView]; |
| 127 } | 180 } |
| 128 | 181 |
| 129 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 182 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 130 GTMTheme* theme = [controlView gtm_theme]; | 183 GTMTheme* theme = [controlView gtm_theme]; |
| 131 | 184 |
| 132 BOOL shouldTheme = YES; | 185 if (shouldTheme_) { |
| 133 if (shouldTheme) { | |
| 134 BOOL isTemplate = [[self image] isTemplate]; | 186 BOOL isTemplate = [[self image] isTemplate]; |
| 135 | 187 |
| 136 [NSGraphicsContext saveGraphicsState]; | 188 [NSGraphicsContext saveGraphicsState]; |
| 137 | 189 |
| 138 CGContextRef context = | 190 CGContextRef context = |
| 139 (CGContextRef)([[NSGraphicsContext currentContext] graphicsPort]); | 191 (CGContextRef)([[NSGraphicsContext currentContext] graphicsPort]); |
| 140 | 192 |
| 141 if (isTemplate) { | 193 if (isTemplate) { |
| 142 scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); | 194 scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); |
| 143 [shadow setShadowColor:[NSColor whiteColor]]; | 195 [shadow setShadowColor:[NSColor whiteColor]]; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 165 [NSGraphicsContext restoreGraphicsState]; | 217 [NSGraphicsContext restoreGraphicsState]; |
| 166 } else { | 218 } else { |
| 167 // NSCell draws these uncentered for some reason, probably because of the | 219 // NSCell draws these uncentered for some reason, probably because of the |
| 168 // of control in the xib | 220 // of control in the xib |
| 169 [super drawInteriorWithFrame:NSOffsetRect(cellFrame, 0, 1) | 221 [super drawInteriorWithFrame:NSOffsetRect(cellFrame, 0, 1) |
| 170 inView:controlView]; | 222 inView:controlView]; |
| 171 } | 223 } |
| 172 } | 224 } |
| 173 | 225 |
| 174 @end | 226 @end |
| OLD | NEW |