| 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 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 [[self controlView] addTrackingArea:trackingArea_]; | 66 [[self controlView] addTrackingArea:trackingArea_]; |
| 67 } else { | 67 } else { |
| 68 if (trackingArea_) { | 68 if (trackingArea_) { |
| 69 [[self controlView] removeTrackingArea:trackingArea_]; | 69 [[self controlView] removeTrackingArea:trackingArea_]; |
| 70 trackingArea_.reset(nil); | 70 trackingArea_.reset(nil); |
| 71 isMouseInside_ = NO; | 71 isMouseInside_ = NO; |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 - (void)drawBorderAndFillForTheme:(GTMTheme*)theme |
| 77 controlView:(NSView*)controlView |
| 78 outerPath:(NSBezierPath*)outerPath |
| 79 innerPath:(NSBezierPath*)innerPath |
| 80 showHighlightGradient:(BOOL)showHighlightGradient |
| 81 showClickedGradient:(BOOL)showClickedGradient |
| 82 active:(BOOL)active |
| 83 cellFrame:(NSRect)cellFrame { |
| 84 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.25] set]; |
| 85 [outerPath stroke]; |
| 86 |
| 87 NSImage* backgroundImage = |
| 88 [theme backgroundImageForStyle:GTMThemeStyleToolBarButton state:YES]; |
| 89 |
| 90 if (backgroundImage) { |
| 91 NSColor* patternColor = [NSColor colorWithPatternImage:backgroundImage]; |
| 92 [patternColor set]; |
| 93 // Set the phase to match window. |
| 94 NSRect trueRect = [controlView convertRectToBase:cellFrame]; |
| 95 [[NSGraphicsContext currentContext] |
| 96 setPatternPhase:NSMakePoint(NSMinX(trueRect), NSMaxY(trueRect))]; |
| 97 [innerPath fill]; |
| 98 } else { |
| 99 if (showClickedGradient) { |
| 100 NSGradient* gradient = |
| 101 [theme gradientForStyle:GTMThemeStyleToolBarButtonPressed |
| 102 state:active]; |
| 103 [gradient drawInBezierPath:innerPath angle:90.0]; |
| 104 } |
| 105 } |
| 106 |
| 107 if (!showClickedGradient && showHighlightGradient) { |
| 108 [NSGraphicsContext saveGraphicsState]; |
| 109 [innerPath addClip]; |
| 110 |
| 111 // Draw the inner glow. |
| 112 [innerPath setLineWidth:2]; |
| 113 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.9] setStroke]; |
| 114 [innerPath stroke]; |
| 115 |
| 116 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.9] setStroke]; |
| 117 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.2] setFill]; |
| 118 |
| 119 // Draw the top inner highlight. |
| 120 NSAffineTransform* highlightTransform = [NSAffineTransform transform]; |
| 121 [highlightTransform translateXBy:1 yBy:1]; |
| 122 scoped_nsobject<NSBezierPath> highlightPath([innerPath copy]); |
| 123 [highlightPath transformUsingAffineTransform:highlightTransform]; |
| 124 |
| 125 [highlightPath stroke]; |
| 126 |
| 127 NSColor* startColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.666]; |
| 128 NSColor* endColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.333]; |
| 129 scoped_nsobject<NSBezierPath> gradient([[NSGradient alloc] |
| 130 initWithColorsAndLocations:startColor, 0.33, endColor, 1.0, nil]); |
| 131 |
| 132 [gradient drawInBezierPath:innerPath angle:90.0]; |
| 133 |
| 134 [NSGraphicsContext restoreGraphicsState]; |
| 135 } |
| 136 |
| 137 NSColor* stroke = [theme strokeColorForStyle:GTMThemeStyleToolBarButton |
| 138 state:active]; |
| 139 [stroke setStroke]; |
| 140 |
| 141 [innerPath setLineWidth:1]; |
| 142 [innerPath stroke]; |
| 143 } |
| 144 |
| 145 |
| 76 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 146 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 77 // Constants from Cole. Will kConstant them once the feedback loop | 147 // Constants from Cole. Will kConstant them once the feedback loop |
| 78 // is complete. | 148 // is complete. |
| 79 NSRect drawFrame = NSInsetRect(cellFrame, 1.5, 1.5); | 149 NSRect drawFrame = NSInsetRect(cellFrame, 1.5, 1.5); |
| 80 NSRect innerFrame = NSInsetRect(cellFrame, 2, 2); | 150 NSRect innerFrame = NSInsetRect(cellFrame, 2, 2); |
| 81 ButtonType type = [[(NSControl*)controlView cell] tag]; | 151 ButtonType type = [[(NSControl*)controlView cell] tag]; |
| 82 switch (type) { | 152 switch (type) { |
| 83 case kRightButtonType: | 153 case kRightButtonType: |
| 84 drawFrame.origin.x -= 20; | 154 drawFrame.origin.x -= 20; |
| 85 innerFrame.origin.x -= 2; | 155 innerFrame.origin.x -= 2; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 106 NSBezierPath* outerPath = | 176 NSBezierPath* outerPath = |
| 107 [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(drawFrame, -1, -1) | 177 [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(drawFrame, -1, -1) |
| 108 xRadius:radius + 1 | 178 xRadius:radius + 1 |
| 109 yRadius:radius + 1]; | 179 yRadius:radius + 1]; |
| 110 | 180 |
| 111 // Stroke the borders and appropriate fill gradient. If we're borderless, | 181 // Stroke the borders and appropriate fill gradient. If we're borderless, |
| 112 // the only time we want to draw the inner gradient is if we're highlighted. | 182 // the only time we want to draw the inner gradient is if we're highlighted. |
| 113 if (([self isBordered] && ![self showsBorderOnlyWhileMouseInside]) || | 183 if (([self isBordered] && ![self showsBorderOnlyWhileMouseInside]) || |
| 114 pressed || | 184 pressed || |
| 115 [self isMouseInside]) { | 185 [self isMouseInside]) { |
| 116 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.25] set]; | |
| 117 [outerPath stroke]; | |
| 118 | 186 |
| 119 NSImage* backgroundImage = | 187 [self drawBorderAndFillForTheme:theme |
| 120 [theme backgroundImageForStyle:GTMThemeStyleToolBarButton state:YES]; | 188 controlView:controlView |
| 121 | 189 outerPath:outerPath |
| 122 if (backgroundImage) { | 190 innerPath:innerPath |
| 123 NSColor* patternColor = [NSColor colorWithPatternImage:backgroundImage]; | 191 showHighlightGradient:YES |
| 124 [patternColor set]; | 192 showClickedGradient:pressed |
| 125 // Set the phase to match window. | 193 active:active |
| 126 NSRect trueRect = [controlView convertRectToBase:cellFrame]; | 194 cellFrame:cellFrame]; |
| 127 [[NSGraphicsContext currentContext] | |
| 128 setPatternPhase:NSMakePoint(NSMinX(trueRect), NSMaxY(trueRect))]; | |
| 129 [innerPath fill]; | |
| 130 } else { | |
| 131 if (pressed) { | |
| 132 NSGradient* gradient = | |
| 133 [theme gradientForStyle:GTMThemeStyleToolBarButtonPressed | |
| 134 state:active]; | |
| 135 [gradient drawInBezierPath:innerPath angle:90.0]; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 if (!pressed) { | |
| 140 [NSGraphicsContext saveGraphicsState]; | |
| 141 [innerPath addClip]; | |
| 142 | |
| 143 // Draw the inner glow. | |
| 144 [innerPath setLineWidth:2]; | |
| 145 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.9] setStroke]; | |
| 146 [innerPath stroke]; | |
| 147 | |
| 148 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.9] setStroke]; | |
| 149 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.2] setFill]; | |
| 150 | |
| 151 // Draw the top inner highlight. | |
| 152 NSAffineTransform* highlightTransform = [NSAffineTransform transform]; | |
| 153 [highlightTransform translateXBy:1 yBy:1]; | |
| 154 scoped_nsobject<NSBezierPath> highlightPath([innerPath copy]); | |
| 155 [highlightPath transformUsingAffineTransform:highlightTransform]; | |
| 156 | |
| 157 [highlightPath stroke]; | |
| 158 | |
| 159 NSColor* startColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.666]; | |
| 160 NSColor* endColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.333]; | |
| 161 scoped_nsobject<NSBezierPath> gradient([[NSGradient alloc] | |
| 162 initWithColorsAndLocations:startColor, 0.33, endColor, 1.0, nil]); | |
| 163 | |
| 164 [gradient drawInBezierPath:innerPath angle:90.0]; | |
| 165 | |
| 166 [NSGraphicsContext restoreGraphicsState]; | |
| 167 } | |
| 168 | |
| 169 NSColor* stroke = [theme strokeColorForStyle:GTMThemeStyleToolBarButton | |
| 170 state:active]; | |
| 171 [stroke setStroke]; | |
| 172 | |
| 173 [innerPath setLineWidth:1]; | |
| 174 [innerPath stroke]; | |
| 175 } | 195 } |
| 176 | 196 |
| 177 // If this is the left side of a segmented button, draw a slight shadow. | 197 // If this is the left side of a segmented button, draw a slight shadow. |
| 178 if (type == kLeftButtonWithShadowType) { | 198 if (type == kLeftButtonWithShadowType) { |
| 179 NSRect borderRect, contentRect; | 199 NSRect borderRect, contentRect; |
| 180 NSDivideRect(cellFrame, &borderRect, &contentRect, 1.0, NSMaxXEdge); | 200 NSDivideRect(cellFrame, &borderRect, &contentRect, 1.0, NSMaxXEdge); |
| 181 NSColor* stroke = [theme strokeColorForStyle:GTMThemeStyleToolBarButton | 201 NSColor* stroke = [theme strokeColorForStyle:GTMThemeStyleToolBarButton |
| 182 state:active]; | 202 state:active]; |
| 183 [[stroke colorWithAlphaComponent:0.2] set]; | 203 [[stroke colorWithAlphaComponent:0.2] set]; |
| 184 NSRectFillUsingOperation(NSInsetRect(borderRect, 0, 2), | 204 NSRectFillUsingOperation(NSInsetRect(borderRect, 0, 2), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 [NSGraphicsContext restoreGraphicsState]; | 246 [NSGraphicsContext restoreGraphicsState]; |
| 227 } else { | 247 } else { |
| 228 // NSCell draws these uncentered for some reason, probably because of the | 248 // NSCell draws these uncentered for some reason, probably because of the |
| 229 // of control in the xib | 249 // of control in the xib |
| 230 [super drawInteriorWithFrame:NSOffsetRect(cellFrame, 0, 1) | 250 [super drawInteriorWithFrame:NSOffsetRect(cellFrame, 0, 1) |
| 231 inView:controlView]; | 251 inView:controlView]; |
| 232 } | 252 } |
| 233 } | 253 } |
| 234 | 254 |
| 235 @end | 255 @end |
| OLD | NEW |