| 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 #import "chrome/browser/themes/theme_service.h" | 8 #import "chrome/browser/themes/theme_service.h" |
| 9 #import "chrome/browser/ui/cocoa/themed_window.h" | 9 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 } | 51 } |
| 52 | 52 |
| 53 - (void)sharedInit { | 53 - (void)sharedInit { |
| 54 [self setHighlightsBy:NSNoCellMask]; | 54 [self setHighlightsBy:NSNoCellMask]; |
| 55 | 55 |
| 56 // We need to set this so that we can override |-mouseEntered:| and | 56 // We need to set this so that we can override |-mouseEntered:| and |
| 57 // |-mouseExited:| to change the button image on hover states. | 57 // |-mouseExited:| to change the button image on hover states. |
| 58 [self setShowsBorderOnlyWhileMouseInside:YES]; | 58 [self setShowsBorderOnlyWhileMouseInside:YES]; |
| 59 } | 59 } |
| 60 | 60 |
| 61 - (NSImage*)imageForState:(image_button_cell::ButtonState)state |
| 62 view:(NSView*)controlView{ |
| 63 if (image_[state].type == image_button_cell::kImageId) |
| 64 return [self imageForID:image_[state].imageId controlView:controlView]; |
| 65 return image_[state].image; |
| 66 } |
| 67 |
| 61 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 68 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 69 image_button_cell::ButtonState state = [self currentButtonState]; |
| 62 BOOL windowHasFocus = [[controlView window] isMainWindow] || | 70 BOOL windowHasFocus = [[controlView window] isMainWindow] || |
| 63 [[controlView window] isKeyWindow]; | 71 [[controlView window] isKeyWindow]; |
| 64 CGFloat alpha = windowHasFocus ? 1.0 : kImageNoFocusAlpha; | 72 CGFloat alpha = windowHasFocus ? 1.0 : kImageNoFocusAlpha; |
| 65 NSImage* image = image_[[self currentButtonState]]; | 73 NSImage* image = [self imageForState:state view:controlView]; |
| 66 | 74 |
| 67 if (!windowHasFocus) { | 75 if (!windowHasFocus) { |
| 76 NSImage* defaultImage = [self |
| 77 imageForState:image_button_cell::kDefaultStateBackground |
| 78 view:controlView]; |
| 79 NSImage* hoverImage = [self |
| 80 imageForState:image_button_cell::kHoverStateBackground |
| 81 view:controlView]; |
| 68 if ([self currentButtonState] == image_button_cell::kDefaultState && | 82 if ([self currentButtonState] == image_button_cell::kDefaultState && |
| 69 image_[image_button_cell::kDefaultStateBackground]) { | 83 defaultImage) { |
| 70 image = image_[image_button_cell::kDefaultStateBackground]; | 84 image = defaultImage; |
| 71 alpha = 1.0; | 85 alpha = 1.0; |
| 72 } else if ([self currentButtonState] == image_button_cell::kHoverState && | 86 } else if ([self currentButtonState] == image_button_cell::kHoverState && |
| 73 image_[image_button_cell::kHoverStateBackground]) { | 87 hoverImage) { |
| 74 image = image_[image_button_cell::kHoverStateBackground]; | 88 image = hoverImage; |
| 75 alpha = 1.0; | 89 alpha = 1.0; |
| 76 } | 90 } |
| 77 } | 91 } |
| 78 | 92 |
| 79 NSRect imageRect; | 93 NSRect imageRect; |
| 80 imageRect.size = [image size]; | 94 imageRect.size = [image size]; |
| 81 imageRect.origin.x = cellFrame.origin.x + | 95 imageRect.origin.x = cellFrame.origin.x + |
| 82 roundf((NSWidth(cellFrame) - NSWidth(imageRect)) / 2.0); | 96 roundf((NSWidth(cellFrame) - NSWidth(imageRect)) / 2.0); |
| 83 imageRect.origin.y = cellFrame.origin.y + | 97 imageRect.origin.y = cellFrame.origin.y + |
| 84 roundf((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); | 98 roundf((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 103 fromRect:NSZeroRect | 117 fromRect:NSZeroRect |
| 104 operation:NSCompositeSourceOver | 118 operation:NSCompositeSourceOver |
| 105 fraction:1.0 | 119 fraction:1.0 |
| 106 respectFlipped:YES | 120 respectFlipped:YES |
| 107 hints:nil]; | 121 hints:nil]; |
| 108 } | 122 } |
| 109 } | 123 } |
| 110 | 124 |
| 111 - (void)setImageID:(NSInteger)imageID | 125 - (void)setImageID:(NSInteger)imageID |
| 112 forButtonState:(image_button_cell::ButtonState)state { | 126 forButtonState:(image_button_cell::ButtonState)state { |
| 113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 127 DCHECK_GE(state, 0); |
| 114 NSImage* image = imageID ? rb.GetNativeImageNamed(imageID).ToNSImage() : nil; | 128 DCHECK_LT(state, image_button_cell::kButtonStateCount); |
| 115 [self setImage:image forButtonState:state]; | 129 |
| 130 image_[state].type = image_button_cell::kImageId; |
| 131 image_[state].image.reset(); |
| 132 image_[state].imageId = imageID; |
| 133 [[self controlView] setNeedsDisplay:YES]; |
| 116 } | 134 } |
| 117 | 135 |
| 118 // Sets the image for the given button state using an image. | 136 // Sets the image for the given button state using an image. |
| 119 - (void)setImage:(NSImage*)image | 137 - (void)setImage:(NSImage*)image |
| 120 forButtonState:(image_button_cell::ButtonState)state { | 138 forButtonState:(image_button_cell::ButtonState)state { |
| 121 DCHECK_GE(state, 0); | 139 DCHECK_GE(state, 0); |
| 122 DCHECK_LT(state, image_button_cell::kButtonStateCount); | 140 DCHECK_LT(state, image_button_cell::kButtonStateCount); |
| 123 image_[state].reset([image retain]); | 141 |
| 142 image_[state].type = image_button_cell::kImage; |
| 143 image_[state].image.reset([image retain]); |
| 144 image_[state].imageId = 0; |
| 124 [[self controlView] setNeedsDisplay:YES]; | 145 [[self controlView] setNeedsDisplay:YES]; |
| 125 } | 146 } |
| 126 | 147 |
| 127 - (void)setOverlayImageID:(NSInteger)imageID { | 148 - (void)setOverlayImageID:(NSInteger)imageID { |
| 128 if (overlayImageID_ != imageID) { | 149 if (overlayImageID_ != imageID) { |
| 129 overlayImageID_ = imageID; | 150 overlayImageID_ = imageID; |
| 130 [[self controlView] setNeedsDisplay:YES]; | 151 [[self controlView] setNeedsDisplay:YES]; |
| 131 } | 152 } |
| 132 } | 153 } |
| 133 | 154 |
| 134 - (image_button_cell::ButtonState)currentButtonState { | 155 - (image_button_cell::ButtonState)currentButtonState { |
| 135 if (![self isEnabled] && image_[image_button_cell::kDisabledState]) | 156 bool (^has)(image_button_cell::ButtonState) = |
| 157 ^(image_button_cell::ButtonState state) { |
| 158 return (image_[state].type == image_button_cell::kImage) ? |
| 159 image_[state].image != nil : image_[state].imageId != 0; |
| 160 }; |
| 161 if (![self isEnabled] && has(image_button_cell::kDisabledState)) |
| 136 return image_button_cell::kDisabledState; | 162 return image_button_cell::kDisabledState; |
| 137 else if ([self isHighlighted] && image_[image_button_cell::kPressedState]) | 163 if ([self isHighlighted] && has(image_button_cell::kPressedState)) |
| 138 return image_button_cell::kPressedState; | 164 return image_button_cell::kPressedState; |
| 139 else if ([self isMouseInside] && image_[image_button_cell::kHoverState]) | 165 if ([self isMouseInside] && has(image_button_cell::kHoverState)) |
| 140 return image_button_cell::kHoverState; | 166 return image_button_cell::kHoverState; |
| 141 else | 167 return image_button_cell::kDefaultState; |
| 142 return image_button_cell::kDefaultState; | |
| 143 } | 168 } |
| 144 | 169 |
| 145 - (NSImage*)imageForID:(NSInteger)imageID | 170 - (NSImage*)imageForID:(NSInteger)imageID |
| 146 controlView:(NSView*)controlView { | 171 controlView:(NSView*)controlView { |
| 147 if (!imageID) | 172 if (!imageID) |
| 148 return nil; | 173 return nil; |
| 149 | 174 |
| 150 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider]; | 175 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider]; |
| 151 if (!themeProvider) | 176 if (!themeProvider) |
| 152 return nil; | 177 return nil; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 178 | 203 |
| 179 - (void)mouseEntered:(NSEvent*)theEvent { | 204 - (void)mouseEntered:(NSEvent*)theEvent { |
| 180 [self setIsMouseInside:YES]; | 205 [self setIsMouseInside:YES]; |
| 181 } | 206 } |
| 182 | 207 |
| 183 - (void)mouseExited:(NSEvent*)theEvent { | 208 - (void)mouseExited:(NSEvent*)theEvent { |
| 184 [self setIsMouseInside:NO]; | 209 [self setIsMouseInside:NO]; |
| 185 } | 210 } |
| 186 | 211 |
| 187 @end | 212 @end |
| OLD | NEW |