OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #import "chrome/browser/ui/cocoa/image_button_cell.h" | 5 #import "chrome/browser/ui/cocoa/image_button_cell.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/mac_util.h" |
8 #import "chrome/browser/themes/theme_service.h" | 9 #import "chrome/browser/themes/theme_service.h" |
9 #import "chrome/browser/ui/cocoa/rect_path_utils.h" | 10 #import "chrome/browser/ui/cocoa/rect_path_utils.h" |
10 #import "chrome/browser/ui/cocoa/themed_window.h" | 11 #import "chrome/browser/ui/cocoa/themed_window.h" |
11 #import "ui/base/cocoa/nsview_additions.h" | 12 #import "ui/base/cocoa/nsview_additions.h" |
12 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
13 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 14 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
14 | 15 |
15 // When the window doesn't have focus then we want to draw the button with a | 16 // When the window doesn't have focus then we want to draw the button with a |
16 // slightly lighter color. We do this by just reducing the alpha. | 17 // slightly lighter color. We do this by just reducing the alpha. |
17 const CGFloat kImageNoFocusAlpha = 0.65; | 18 const CGFloat kImageNoFocusAlpha = 0.65; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 [image drawInRect:imageRect | 94 [image drawInRect:imageRect |
94 fromRect:NSZeroRect | 95 fromRect:NSZeroRect |
95 operation:NSCompositeSourceOver | 96 operation:NSCompositeSourceOver |
96 fraction:alpha | 97 fraction:alpha |
97 respectFlipped:YES | 98 respectFlipped:YES |
98 hints:nil]; | 99 hints:nil]; |
99 } | 100 } |
100 | 101 |
101 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 102 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
102 [self drawImageWithFrame:cellFrame inView:controlView]; | 103 [self drawImageWithFrame:cellFrame inView:controlView]; |
103 // Only draw custom focus ring if the 10.7 focus ring APIs are not available. | 104 [self drawFocusRingWithFrame:cellFrame inView:controlView]; |
104 // TODO(groby): Remove once we build against the 10.7 SDK. | |
105 if (![self respondsToSelector:@selector(drawFocusRingMaskWithFrame:inView:)]) | |
106 [self drawFocusRingWithFrame:cellFrame inView:controlView]; | |
107 } | 105 } |
108 | 106 |
109 - (void)setImageID:(NSInteger)imageID | 107 - (void)setImageID:(NSInteger)imageID |
110 forButtonState:(image_button_cell::ButtonState)state { | 108 forButtonState:(image_button_cell::ButtonState)state { |
111 DCHECK_GE(state, 0); | 109 DCHECK_GE(state, 0); |
112 DCHECK_LT(state, image_button_cell::kButtonStateCount); | 110 DCHECK_LT(state, image_button_cell::kButtonStateCount); |
113 | 111 |
114 image_[state].image.reset(); | 112 image_[state].image.reset(); |
115 image_[state].imageId = imageID; | 113 image_[state].imageId = imageID; |
116 [[self controlView] setNeedsDisplay:YES]; | 114 [[self controlView] setNeedsDisplay:YES]; |
(...skipping 13 matching lines...) Expand all Loading... |
130 - (CGFloat)imageAlphaForWindowState:(NSWindow*)window { | 128 - (CGFloat)imageAlphaForWindowState:(NSWindow*)window { |
131 BOOL windowHasFocus = [window isMainWindow] || [window isKeyWindow]; | 129 BOOL windowHasFocus = [window isMainWindow] || [window isKeyWindow]; |
132 return windowHasFocus ? 1.0 : kImageNoFocusAlpha; | 130 return windowHasFocus ? 1.0 : kImageNoFocusAlpha; |
133 } | 131 } |
134 | 132 |
135 - (ui::ThemeProvider*)themeProviderForWindow:(NSWindow*)window { | 133 - (ui::ThemeProvider*)themeProviderForWindow:(NSWindow*)window { |
136 return [window themeProvider]; | 134 return [window themeProvider]; |
137 } | 135 } |
138 | 136 |
139 - (void)drawFocusRingWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 137 - (void)drawFocusRingWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 138 // Draw custom focus ring only if AppKit won't draw one automatically. |
| 139 // The new focus ring APIs became available with 10.7, but did not get |
| 140 // applied to buttons (only editable text fields) until 10.8. |
| 141 if (base::mac::IsOSMountainLionOrLater()) |
| 142 return; |
| 143 |
140 if (![self showsFirstResponder]) | 144 if (![self showsFirstResponder]) |
141 return; | 145 return; |
142 gfx::ScopedNSGraphicsContextSaveGState scoped_state; | 146 gfx::ScopedNSGraphicsContextSaveGState scoped_state; |
143 const CGFloat lineWidth = [controlView cr_lineWidth]; | 147 const CGFloat lineWidth = [controlView cr_lineWidth]; |
144 rect_path_utils::FrameRectWithInset(rect_path_utils::RoundedCornerAll, | 148 rect_path_utils::FrameRectWithInset(rect_path_utils::RoundedCornerAll, |
145 NSInsetRect(cellFrame, 0, lineWidth), | 149 NSInsetRect(cellFrame, 0, lineWidth), |
146 0.0, // insetX | 150 0.0, // insetX |
147 0.0, // insetY | 151 0.0, // insetY |
148 3.0, // outerRadius | 152 3.0, // outerRadius |
149 lineWidth * 2, // lineWidth | 153 lineWidth * 2, // lineWidth |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 206 |
203 - (void)mouseEntered:(NSEvent*)theEvent { | 207 - (void)mouseEntered:(NSEvent*)theEvent { |
204 [self setIsMouseInside:YES]; | 208 [self setIsMouseInside:YES]; |
205 } | 209 } |
206 | 210 |
207 - (void)mouseExited:(NSEvent*)theEvent { | 211 - (void)mouseExited:(NSEvent*)theEvent { |
208 [self setIsMouseInside:NO]; | 212 [self setIsMouseInside:NO]; |
209 } | 213 } |
210 | 214 |
211 @end | 215 @end |
OLD | NEW |