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 @interface GradientButtonCell (Private) |
| 10 - (void)drawUnderlayImageWithFrame:(NSRect)cellFrame |
| 11 inView:(NSView*)controlView; |
| 12 @end |
| 13 |
| 14 |
9 @implementation GradientButtonCell | 15 @implementation GradientButtonCell |
10 | 16 |
11 // For nib instantiations | 17 // For nib instantiations |
12 - (id)initWithCoder:(NSCoder*)decoder { | 18 - (id)initWithCoder:(NSCoder*)decoder { |
13 if ((self = [super initWithCoder:decoder])) { | 19 if ((self = [super initWithCoder:decoder])) { |
14 shouldTheme_ = YES; | 20 shouldTheme_ = YES; |
15 } | 21 } |
16 return self; | 22 return self; |
17 } | 23 } |
18 | 24 |
19 // For programmatic instantiations | 25 // For programmatic instantiations |
20 - (id)initTextCell:(NSString*)string { | 26 - (id)initTextCell:(NSString*)string { |
21 if ((self = [super initTextCell:string])) { | 27 if ((self = [super initTextCell:string])) { |
22 shouldTheme_ = YES; | 28 shouldTheme_ = YES; |
23 } | 29 } |
24 return self; | 30 return self; |
25 } | 31 } |
26 | 32 |
27 - (void)setShouldTheme:(BOOL)shouldTheme { | 33 - (void)setShouldTheme:(BOOL)shouldTheme { |
28 shouldTheme_ = shouldTheme; | 34 shouldTheme_ = shouldTheme; |
29 } | 35 } |
30 | 36 |
| 37 - (NSImage*)underlayImage { |
| 38 return underlayImage_; |
| 39 } |
| 40 |
| 41 - (void)setUnderlayImage:(NSImage*)image { |
| 42 underlayImage_.reset([image retain]); |
| 43 |
| 44 [[self controlView] setNeedsDisplay:YES]; |
| 45 } |
| 46 |
31 - (NSBackgroundStyle)interiorBackgroundStyle { | 47 - (NSBackgroundStyle)interiorBackgroundStyle { |
32 return [self isHighlighted] ? | 48 return [self isHighlighted] ? |
33 NSBackgroundStyleLowered : NSBackgroundStyleRaised; | 49 NSBackgroundStyleLowered : NSBackgroundStyleRaised; |
34 } | 50 } |
35 | 51 |
36 - (void)mouseEntered:(NSEvent *)theEvent { | 52 - (void)mouseEntered:(NSEvent *)theEvent { |
37 isMouseInside_ = YES; | 53 isMouseInside_ = YES; |
38 [[self controlView] setNeedsDisplay:YES]; | 54 [[self controlView] setNeedsDisplay:YES]; |
39 } | 55 } |
40 | 56 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 (CGContextRef)([[NSGraphicsContext currentContext] graphicsPort]); | 216 (CGContextRef)([[NSGraphicsContext currentContext] graphicsPort]); |
201 | 217 |
202 if (isTemplate) { | 218 if (isTemplate) { |
203 scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); | 219 scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); |
204 [shadow setShadowColor:[NSColor whiteColor]]; | 220 [shadow setShadowColor:[NSColor whiteColor]]; |
205 [shadow setShadowOffset:NSMakeSize(0, -1.0)]; | 221 [shadow setShadowOffset:NSMakeSize(0, -1.0)]; |
206 [shadow setShadowBlurRadius:1.0]; | 222 [shadow setShadowBlurRadius:1.0]; |
207 [shadow set]; | 223 [shadow set]; |
208 } | 224 } |
209 | 225 |
| 226 [self drawUnderlayImageWithFrame:cellFrame inView:controlView]; |
| 227 |
210 CGContextBeginTransparencyLayer(context, 0); | 228 CGContextBeginTransparencyLayer(context, 0); |
211 NSRect imageRect = NSZeroRect; | 229 NSRect imageRect = NSZeroRect; |
212 imageRect.size = [[self image] size]; | 230 imageRect.size = [[self image] size]; |
213 [[self image] setFlipped:[controlView isFlipped]]; | 231 [[self image] setFlipped:[controlView isFlipped]]; |
214 [[self image] drawInRect:[self imageRectForBounds:cellFrame] | 232 [[self image] drawInRect:[self imageRectForBounds:cellFrame] |
215 fromRect:imageRect | 233 fromRect:imageRect |
216 operation:NSCompositeSourceOver | 234 operation:NSCompositeSourceOver |
217 fraction:[self isEnabled] ? 1.0 : 0.5]; | 235 fraction:[self isEnabled] ? 1.0 : 0.5]; |
218 if (isTemplate) { | 236 if (isTemplate) { |
219 NSColor* color = [theme iconColorForStyle:GTMThemeStyleToolBarButton | 237 NSColor* color = [theme iconColorForStyle:GTMThemeStyleToolBarButton |
220 state:YES]; | 238 state:YES]; |
221 [color set]; | 239 [color set]; |
222 NSRectFillUsingOperation(cellFrame,NSCompositeSourceAtop); | 240 NSRectFillUsingOperation(cellFrame,NSCompositeSourceAtop); |
223 } | 241 } |
224 | 242 |
225 CGContextEndTransparencyLayer(context); | 243 CGContextEndTransparencyLayer(context); |
226 [NSGraphicsContext restoreGraphicsState]; | 244 [NSGraphicsContext restoreGraphicsState]; |
227 } else { | 245 } else { |
| 246 [self drawUnderlayImageWithFrame:cellFrame inView:controlView]; |
| 247 |
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 |
| 255 - (void)drawUnderlayImageWithFrame:(NSRect)cellFrame |
| 256 inView:(NSView*)controlView { |
| 257 if (underlayImage_) { |
| 258 NSRect imageRect = NSZeroRect; |
| 259 imageRect.size = [underlayImage_ size]; |
| 260 [underlayImage_ setFlipped:[controlView isFlipped]]; |
| 261 [underlayImage_ drawInRect:[self imageRectForBounds:cellFrame] |
| 262 fromRect:imageRect |
| 263 operation:NSCompositeSourceOver |
| 264 fraction:[self isEnabled] ? 1.0 : 0.5]; |
| 265 } |
| 266 } |
| 267 |
235 @end | 268 @end |
OLD | NEW |