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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/image_button_cell.h"
7 #include "base/logging.h"
8 #import "chrome/browser/themes/theme_service.h"
9 #import "chrome/browser/ui/cocoa/rect_path_utils.h"
10 #import "chrome/browser/ui/cocoa/themed_window.h"
11 #import "ui/base/cocoa/nsview_additions.h"
12 #include "ui/gfx/image/image.h"
13 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
15 // 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 const CGFloat kImageNoFocusAlpha = 0.65;
19 @interface ImageButtonCell (Private)
21 - (image_button_cell::ButtonState)currentButtonState;
22 - (NSImage*)imageForID:(NSInteger)imageID
23 controlView:(NSView*)controlView;
26 @implementation ImageButtonCell
28 @synthesize isMouseInside = isMouseInside_;
30 // For nib instantiations
31 - (id)initWithCoder:(NSCoder*)decoder {
32 if ((self = [super initWithCoder:decoder])) {
38 // For programmatic instantiations
39 - (id)initTextCell:(NSString*)string {
40 if ((self = [super initTextCell:string])) {
47 [self setHighlightsBy:NSNoCellMask];
49 // We need to set this so that we can override |-mouseEntered:| and
50 // |-mouseExited:| to change the button image on hover states.
51 [self setShowsBorderOnlyWhileMouseInside:YES];
54 - (NSImage*)imageForState:(image_button_cell::ButtonState)state
55 view:(NSView*)controlView{
56 if (image_[state].imageId)
57 return [self imageForID:image_[state].imageId controlView:controlView];
58 return image_[state].image;
61 - (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
62 image_button_cell::ButtonState state = [self currentButtonState];
63 BOOL windowHasFocus = [[controlView window] isMainWindow] ||
64 [[controlView window] isKeyWindow];
65 CGFloat alpha = [self imageAlphaForWindowState:[controlView window]];
66 NSImage* image = [self imageForState:state view:controlView];
68 if (!windowHasFocus) {
69 NSImage* defaultImage = [self
70 imageForState:image_button_cell::kDefaultStateBackground
72 NSImage* hoverImage = [self
73 imageForState:image_button_cell::kHoverStateBackground
75 if ([self currentButtonState] == image_button_cell::kDefaultState &&
79 } else if ([self currentButtonState] == image_button_cell::kHoverState &&
87 imageRect.size = [image size];
88 imageRect.origin.x = cellFrame.origin.x +
89 roundf((NSWidth(cellFrame) - NSWidth(imageRect)) / 2.0);
90 imageRect.origin.y = cellFrame.origin.y +
91 roundf((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0);
93 [image drawInRect:imageRect
95 operation:NSCompositeSourceOver
101 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
102 [self drawImageWithFrame:cellFrame inView:controlView];
103 // Only draw custom focus ring if the 10.7 focus ring APIs are not available.
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];
109 - (void)setImageID:(NSInteger)imageID
110 forButtonState:(image_button_cell::ButtonState)state {
112 DCHECK_LT(state, image_button_cell::kButtonStateCount);
114 image_[state].image.reset();
115 image_[state].imageId = imageID;
116 [[self controlView] setNeedsDisplay:YES];
119 // Sets the image for the given button state using an image.
120 - (void)setImage:(NSImage*)image
121 forButtonState:(image_button_cell::ButtonState)state {
123 DCHECK_LT(state, image_button_cell::kButtonStateCount);
125 image_[state].image.reset([image retain]);
126 image_[state].imageId = 0;
127 [[self controlView] setNeedsDisplay:YES];
130 - (CGFloat)imageAlphaForWindowState:(NSWindow*)window {
131 BOOL windowHasFocus = [window isMainWindow] || [window isKeyWindow];
132 return windowHasFocus ? 1.0 : kImageNoFocusAlpha;
135 - (ui::ThemeProvider*)themeProviderForWindow:(NSWindow*)window {
136 return [window themeProvider];
139 - (void)drawFocusRingWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
140 if (![self showsFirstResponder])
142 gfx::ScopedNSGraphicsContextSaveGState scoped_state;
143 const CGFloat lineWidth = [controlView cr_lineWidth];
144 rect_path_utils::FrameRectWithInset(rect_path_utils::RoundedCornerAll,
145 NSInsetRect(cellFrame, 0, lineWidth),
149 lineWidth * 2, // lineWidth
151 cr_keyboardFocusIndicatorColor]);
154 - (image_button_cell::ButtonState)currentButtonState {
155 bool (^has)(image_button_cell::ButtonState) =
156 ^(image_button_cell::ButtonState state) {
157 return image_[state].image || image_[state].imageId;
159 if (![self isEnabled] && has(image_button_cell::kDisabledState))
160 return image_button_cell::kDisabledState;
161 if ([self isHighlighted] && has(image_button_cell::kPressedState))
162 return image_button_cell::kPressedState;
163 if ([self isMouseInside] && has(image_button_cell::kHoverState))
164 return image_button_cell::kHoverState;
165 return image_button_cell::kDefaultState;
168 - (NSImage*)imageForID:(NSInteger)imageID
169 controlView:(NSView*)controlView {
173 ui::ThemeProvider* themeProvider =
174 [self themeProviderForWindow:[controlView window]];
178 return themeProvider->GetNSImageNamed(imageID);
181 - (void)setIsMouseInside:(BOOL)isMouseInside {
182 if (isMouseInside_ != isMouseInside) {
183 isMouseInside_ = isMouseInside;
184 NSView<ImageButton>* control =
185 static_cast<NSView<ImageButton>*>([self controlView]);
186 if ([control respondsToSelector:@selector(mouseInsideStateDidChange:)]) {
187 [control mouseInsideStateDidChange:isMouseInside];
189 [control setNeedsDisplay:YES];
193 - (void)setShowsBorderOnlyWhileMouseInside:(BOOL)show {
194 VLOG_IF(1, !show) << "setShowsBorderOnlyWhileMouseInside:NO ignored";
197 - (BOOL)showsBorderOnlyWhileMouseInside {
198 // Always returns YES so that buttons always get mouse tracking even when
199 // disabled. The reload button (and possibly others) depend on this.
203 - (void)mouseEntered:(NSEvent*)theEvent {
204 [self setIsMouseInside:YES];
207 - (void)mouseExited:(NSEvent*)theEvent {
208 [self setIsMouseInside:NO];