William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | //
// AIHoveringPopUpButton.m
// Adium
//
// Created by Evan Schoenberg on 12/16/05.
//
#import "AIHoveringPopUpButton.h"
#import "AIHoveringPopUpButtonCell.h"
@implementation AIHoveringPopUpButton
+ (void)initialize {
if (self == [AIHoveringPopUpButton class])
[self setCellClass:[AIHoveringPopUpButtonCell class]];
}
- (void)initHoveringPopUpButton
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(frameDidChange:)
name:NSViewFrameDidChangeNotification
object:self];
[self setPostsFrameChangedNotifications:YES];
trackingTag = -1;
[self resetCursorRects];
highlightOnHoverAndClick = YES;
}
- (id)initWithFrame:(NSRect)inFrame
{
if ((self = [super initWithFrame:inFrame])) {
[self initHoveringPopUpButton];
}
return self;
}
- (void)awakeFromNib
{
if ([[AIHoveringPopUpButton superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
[super awakeFromNib];
}
[self initHoveringPopUpButton];
}
- (id)copyWithZone:(NSZone *)zone
{
AIHoveringPopUpButton *newButton = [[[self class] allocWithZone:zone] initWithFrame:[self frame]];
[newButton setMenu:[[[self menu] copy] autorelease]];
return newButton;
}
//silly NSControl...
- (void)setMenu:(NSMenu *)menu {
[super setMenu:menu];
[[self cell] setMenu:menu];
}
- (void)setDoubleAction:(SEL)inDoubleAction
{
doubleAction = inDoubleAction;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (trackingTag != -1) {
[self removeTrackingRect:trackingTag];
trackingTag = -1;
}
[super dealloc];
}
//Mouse Tracking -------------------------------------------------------------------------------------------------------
#pragma mark Mouse Tracking
//Custom mouse down tracking to display our menu and highlight
- (void)mouseDown:(NSEvent *)theEvent
{
if (![self menu]) {
if (doubleAction && (([theEvent clickCount] % 2) == 0)) {
[[self target] performSelector:doubleAction
withObject:self];
} else {
[super mouseDown:theEvent];
}
} else {
if ([self isEnabled]) {
[self highlight:YES];
[self setNeedsDisplay:YES];
//2 pt down, 1 pt to the left.
NSPoint point = [self convertPoint:[self bounds].origin toView:nil];
point.y -= NSHeight([self frame]) + 2;
point.x -= 1;
NSEvent *event = [NSEvent mouseEventWithType:[theEvent type]
location:point
modifierFlags:[theEvent modifierFlags]
timestamp:[theEvent timestamp]
windowNumber:[[theEvent window] windowNumber]
context:[theEvent context]
eventNumber:[theEvent eventNumber]
clickCount:[theEvent clickCount]
pressure:[theEvent pressure]];
[NSMenu popUpContextMenu:[self menu] withEvent:event forView:self];
[self mouseUp:[[NSApplication sharedApplication] currentEvent]];
}
}
}
/*!
* @brief Only pass on a highlight message if we're highlighting on click or this is turning off a highlight
*/
- (void)highlight:(BOOL)inFlag
{
if (!inFlag || highlightOnHoverAndClick) {
[super highlight:inFlag];
}
}
//Remove highlight on mouse up
- (void)mouseUp:(NSEvent *)theEvent
{
[self highlight:NO];
[super mouseUp:theEvent];
}
//Ignore dragging
- (void)mouseDragged:(NSEvent *)theEvent
{
//Empty
}
- (NSView *)hitTest:(NSPoint)aPoint
{
NSRect myFrame = [self frame];
myFrame.size.width = [[self cell] trackingWidth];
if (NSPointInRect(aPoint, myFrame)) {
return [super hitTest:aPoint];
} else {
return nil;
}
}
/*!
* @brief Set the title
*/
- (void)setTitle:(NSString *)inTitle
{
[[self cell] setTitle:inTitle];
[self setNeedsDisplay:YES];
[self resetCursorRects];
}
- (void)setFont:(NSFont *)inFont
{
[[self cell] setFont:inFont];
[self setNeedsDisplay:YES];
}
- (void)setImage:(NSImage *)inImage
{
[[self cell] setImage:inImage];
[self setNeedsDisplay:YES];
[self resetCursorRects];
}
- (void)setHighlightOnHoverAndClick:(BOOL)inHighlightOnHoverAndClick
{
highlightOnHoverAndClick = inHighlightOnHoverAndClick;
//If we are not going to highlight, ensure we're not currently doing so
if (!highlightOnHoverAndClick) {
[[self cell] setHovered:NO animate:NO];
[self setNeedsDisplay:YES];
}
}
#pragma mark Tracking rects
//Remove old tracking rects when we change superviews
- (void)viewWillMoveToSuperview:(NSView *)newSuperview
{
if (trackingTag != -1) {
[self removeTrackingRect:trackingTag];
trackingTag = -1;
}
[super viewWillMoveToSuperview:newSuperview];
}
- (void)viewDidMoveToSuperview
{
[super viewDidMoveToSuperview];
[self resetCursorRects];
}
- (void)viewWillMoveToWindow:(NSWindow *)newWindow
{
if (trackingTag != -1) {
[self removeTrackingRect:trackingTag];
trackingTag = -1;
}
[super viewWillMoveToWindow:newWindow];
}
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
[self resetCursorRects];
}
- (void)frameDidChange:(NSNotification *)inNotification
{
[self resetCursorRects];
}
- (NSRect)trackingRect
{
return NSMakeRect(0, 0, [[self cell] trackingWidth], [self frame].size.height);
}
//Reset our cursor tracking
- (void)resetCursorRects
{
//Stop any existing tracking
if (trackingTag != -1) {
[self removeTrackingRect:trackingTag];
trackingTag = -1;
}
//Add a tracking rect if our superview and window are ready
if ([self superview] && [self window]) {
NSRect myFrame = [self frame];
NSRect trackRect = [self trackingRect];
if (trackRect.size.width > myFrame.size.width) {
trackRect.size.width = myFrame.size.width;
}
NSPoint localPoint = [self convertPoint:[[self window] convertScreenToBase:[NSEvent mouseLocation]]
fromView:nil];
BOOL mouseInside = NSPointInRect(localPoint, trackRect);
trackingTag = [self addTrackingRect:trackRect owner:self userData:nil assumeInside:mouseInside];
if (mouseInside) {
[self mouseEntered:nil];
} else {
[self mouseExited:nil];
}
}
}
//Cursor entered our view
- (void)mouseEntered:(NSEvent *)theEvent
{
if (highlightOnHoverAndClick && ![[self cell] hovered]) {
[[self cell] setHovered:YES animate:YES];
[self setNeedsDisplay:YES];
}
[super mouseEntered:theEvent];
}
//Cursor left our view
- (void)mouseExited:(NSEvent *)theEvent
{
if (highlightOnHoverAndClick && [[self cell] hovered]) {
[[self cell] setHovered:NO animate:YES];
[self setNeedsDisplay:YES];
}
[super mouseExited:theEvent];
}
@end
|