Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / image_button_cell.mm
blobac155151058123f93839eaef46275fec062a4525
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)
20 - (void)sharedInit;
21 - (image_button_cell::ButtonState)currentButtonState;
22 - (NSImage*)imageForID:(NSInteger)imageID
23            controlView:(NSView*)controlView;
24 @end
26 @implementation ImageButtonCell
28 @synthesize isMouseInside = isMouseInside_;
30 // For nib instantiations
31 - (id)initWithCoder:(NSCoder*)decoder {
32   if ((self = [super initWithCoder:decoder])) {
33     [self sharedInit];
34   }
35   return self;
38 // For programmatic instantiations
39 - (id)initTextCell:(NSString*)string {
40   if ((self = [super initTextCell:string])) {
41     [self sharedInit];
42   }
43   return self;
46 - (void)sharedInit {
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
71                view:controlView];
72     NSImage* hoverImage = [self
73       imageForState:image_button_cell::kHoverStateBackground
74                view:controlView];
75     if ([self currentButtonState] == image_button_cell::kDefaultState &&
76         defaultImage) {
77       image = defaultImage;
78       alpha = 1.0;
79     } else if ([self currentButtonState] == image_button_cell::kHoverState &&
80         hoverImage) {
81       image = hoverImage;
82       alpha = 1.0;
83     }
84   }
86   NSRect imageRect;
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
94            fromRect:NSZeroRect
95           operation:NSCompositeSourceOver
96            fraction:alpha
97      respectFlipped:YES
98               hints:nil];
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 {
111   DCHECK_GE(state, 0);
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 {
122   DCHECK_GE(state, 0);
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])
141     return;
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),
146                                       0.0,            // insetX
147                                       0.0,            // insetY
148                                       3.0,            // outerRadius
149                                       lineWidth * 2,  // lineWidth
150                                       [controlView
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;
158       };
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 {
170   if (!imageID)
171     return nil;
173   ui::ThemeProvider* themeProvider =
174       [self themeProviderForWindow:[controlView window]];
175   if (!themeProvider)
176     return nil;
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];
188     }
189     [control setNeedsDisplay:YES];
190   }
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.
200   return YES;
203 - (void)mouseEntered:(NSEvent*)theEvent {
204   [self setIsMouseInside:YES];
207 - (void)mouseExited:(NSEvent*)theEvent {
208   [self setIsMouseInside:NO];
211 @end