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].imageId) |
| 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].image.reset(); |
| 131 image_[state].imageId = imageID; |
| 132 [[self controlView] setNeedsDisplay:YES]; |
116 } | 133 } |
117 | 134 |
118 // Sets the image for the given button state using an image. | 135 // Sets the image for the given button state using an image. |
119 - (void)setImage:(NSImage*)image | 136 - (void)setImage:(NSImage*)image |
120 forButtonState:(image_button_cell::ButtonState)state { | 137 forButtonState:(image_button_cell::ButtonState)state { |
121 DCHECK_GE(state, 0); | 138 DCHECK_GE(state, 0); |
122 DCHECK_LT(state, image_button_cell::kButtonStateCount); | 139 DCHECK_LT(state, image_button_cell::kButtonStateCount); |
123 image_[state].reset([image retain]); | 140 |
| 141 image_[state].image.reset([image retain]); |
| 142 image_[state].imageId = 0; |
124 [[self controlView] setNeedsDisplay:YES]; | 143 [[self controlView] setNeedsDisplay:YES]; |
125 } | 144 } |
126 | 145 |
127 - (void)setOverlayImageID:(NSInteger)imageID { | 146 - (void)setOverlayImageID:(NSInteger)imageID { |
128 if (overlayImageID_ != imageID) { | 147 if (overlayImageID_ != imageID) { |
129 overlayImageID_ = imageID; | 148 overlayImageID_ = imageID; |
130 [[self controlView] setNeedsDisplay:YES]; | 149 [[self controlView] setNeedsDisplay:YES]; |
131 } | 150 } |
132 } | 151 } |
133 | 152 |
134 - (image_button_cell::ButtonState)currentButtonState { | 153 - (image_button_cell::ButtonState)currentButtonState { |
135 if (![self isEnabled] && image_[image_button_cell::kDisabledState]) | 154 bool (^has)(image_button_cell::ButtonState) = |
| 155 ^(image_button_cell::ButtonState state) { |
| 156 return image_[state].image || image_[state].imageId; |
| 157 }; |
| 158 if (![self isEnabled] && has(image_button_cell::kDisabledState)) |
136 return image_button_cell::kDisabledState; | 159 return image_button_cell::kDisabledState; |
137 else if ([self isHighlighted] && image_[image_button_cell::kPressedState]) | 160 if ([self isHighlighted] && has(image_button_cell::kPressedState)) |
138 return image_button_cell::kPressedState; | 161 return image_button_cell::kPressedState; |
139 else if ([self isMouseInside] && image_[image_button_cell::kHoverState]) | 162 if ([self isMouseInside] && has(image_button_cell::kHoverState)) |
140 return image_button_cell::kHoverState; | 163 return image_button_cell::kHoverState; |
141 else | 164 return image_button_cell::kDefaultState; |
142 return image_button_cell::kDefaultState; | |
143 } | 165 } |
144 | 166 |
145 - (NSImage*)imageForID:(NSInteger)imageID | 167 - (NSImage*)imageForID:(NSInteger)imageID |
146 controlView:(NSView*)controlView { | 168 controlView:(NSView*)controlView { |
147 if (!imageID) | 169 if (!imageID) |
148 return nil; | 170 return nil; |
149 | 171 |
150 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider]; | 172 ui::ThemeProvider* themeProvider = [[controlView window] themeProvider]; |
151 if (!themeProvider) | 173 if (!themeProvider) |
152 return nil; | 174 return nil; |
(...skipping 25 matching lines...) Expand all Loading... |
178 | 200 |
179 - (void)mouseEntered:(NSEvent*)theEvent { | 201 - (void)mouseEntered:(NSEvent*)theEvent { |
180 [self setIsMouseInside:YES]; | 202 [self setIsMouseInside:YES]; |
181 } | 203 } |
182 | 204 |
183 - (void)mouseExited:(NSEvent*)theEvent { | 205 - (void)mouseExited:(NSEvent*)theEvent { |
184 [self setIsMouseInside:NO]; | 206 [self setIsMouseInside:NO]; |
185 } | 207 } |
186 | 208 |
187 @end | 209 @end |
OLD | NEW |