1 #import "MVMenuButton.h"
3 @implementation MVMenuButton
4 - (void) encodeWithCoder:(NSCoder *) coder {
5 [super encodeWithCoder:coder];
8 - (id) initWithFrame:(NSRect) frame {
9 if( ( self = [super initWithFrame:frame] ) ) {
10 [self setBordered:NO];
11 [self setButtonType:NSMomentaryChangeButton];
16 - (id) initWithCoder:(NSCoder *) coder {
17 if( ( self = [super initWithCoder:coder] ) ) {
18 _size = NSRegularControlSize;
20 _orgImage = [[self image] copy];
29 [_smallImage release];
38 - (void) drawRect:(NSRect) rect {
39 [super drawRect:rect];
41 if( [self drawsArrow] ) {
42 NSBezierPath *path = [NSBezierPath bezierPath];
44 if( _size == NSRegularControlSize ) {
45 [path moveToPoint:NSMakePoint( NSWidth( [self frame] ) - 6, NSHeight( [self frame] ) - 3 )];
46 [path relativeLineToPoint:NSMakePoint( 6, 0 )];
47 [path relativeLineToPoint:NSMakePoint( -3, 3 )];
48 } else if( _size == NSSmallControlSize ) {
49 [path moveToPoint:NSMakePoint( NSWidth( [self frame] ) - 4, NSHeight( [self frame] ) - 3 )];
50 [path relativeLineToPoint:NSMakePoint( 4, 0 )];
51 [path relativeLineToPoint:NSMakePoint( -2, 3 )];
55 [[[NSColor blackColor] colorWithAlphaComponent:0.75] set];
60 - (void) mouseDown:(NSEvent *) theEvent {
61 if( ! [self isEnabled] ) return;
63 [super mouseDown:theEvent];
69 NSPoint point = [self convertPoint:[self bounds].origin toView:nil];
70 point.y -= NSHeight( [self frame] ) + 2.;
73 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]];
74 [NSMenu popUpContextMenu:[self menu] withEvent:event forView:self];
76 [self mouseUp:[[NSApplication sharedApplication] currentEvent]];
79 - (void) mouseUp:(NSEvent *) theEvent {
81 [super mouseUp:theEvent];
84 - (void) mouseDragged:(NSEvent *) theEvent {
88 - (NSControlSize) controlSize {
89 return ( _size ? _size : NSRegularControlSize );
92 - (void) setControlSize:(NSControlSize) controlSize {
93 if( ! _orgImage ) _orgImage = [[self image] copy];
94 if( controlSize == NSRegularControlSize ) {
95 [super setImage:_orgImage];
96 [_toolbarItem setMinSize:NSMakeSize( 32., 32. )];
97 [_toolbarItem setMaxSize:NSMakeSize( 32., 32. )];
98 } else if( controlSize == NSSmallControlSize ) {
100 NSImageRep *sourceImageRep = [_orgImage bestRepresentationForDevice:nil];
101 _smallImage = [[NSImage alloc] initWithSize:NSMakeSize( 24., 24. )];
102 [_smallImage lockFocus];
103 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
104 [sourceImageRep drawInRect:NSMakeRect( 0., 0., 24., 24. )];
105 [_smallImage unlockFocus];
107 [super setImage:_smallImage];
108 [_toolbarItem setMinSize:NSMakeSize( 24., 24. )];
109 [_toolbarItem setMaxSize:NSMakeSize( 24., 24. )];
114 - (void) setImage:(NSImage *) image {
115 [_orgImage autorelease];
116 _orgImage = [[self image] copy];
118 NSImageRep *sourceImageRep = [image bestRepresentationForDevice:nil];
119 [_smallImage autorelease];
120 _smallImage = [[NSImage alloc] initWithSize:NSMakeSize( 24., 24. )];
121 [_smallImage lockFocus];
122 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
123 [sourceImageRep drawInRect:NSMakeRect( 0., 0., 24., 24. )];
124 [_smallImage unlockFocus];
126 if( _size == NSRegularControlSize ) [super setImage:image];
127 else if( _size == NSSmallControlSize ) [super setImage:_smallImage];
130 - (NSImage *) smallImage {
134 - (void) setSmallImage:(NSImage *) image {
135 [_smallImage autorelease];
136 _smallImage = [image copy];
139 - (NSToolbarItem *) toolbarItem {
143 - (void) setToolbarItem:(NSToolbarItem *) item {
147 - (BOOL) drawsArrow {
151 - (void) setDrawsArrow:(BOOL) arrow {
155 - (id) accessibilityAttributeValue:(NSString *) attribute {
156 if( [attribute isEqualToString:NSAccessibilityTitleAttribute] )
157 return [_toolbarItem label];
158 if( [attribute isEqualToString:NSAccessibilityHelpAttribute] )
159 return [_toolbarItem toolTip];
160 if( [attribute isEqualToString:NSAccessibilityToolbarButtonAttribute] )
162 return [super accessibilityAttributeValue:attribute];