OSDN Git Service

Fix hb_log to truncate the message at the correct point, fixes 1244.
[handbrake-jp/handbrake-jp-git.git] / macosx / MVMenuButton.m
1 #import "MVMenuButton.h"
2
3 @implementation MVMenuButton
4 - (void) encodeWithCoder:(NSCoder *) coder {
5         [super encodeWithCoder:coder];
6 }
7
8 - (id) initWithFrame:(NSRect) frame {
9         if( ( self = [super initWithFrame:frame] ) ) {
10                 [self setBordered:NO];
11                 [self setButtonType:NSMomentaryChangeButton];
12         }
13         return self;
14 }
15
16 - (id) initWithCoder:(NSCoder *) coder {
17         if( ( self = [super initWithCoder:coder] ) ) {
18                 _size = NSRegularControlSize;
19                 _drawsArrow = NO;
20                 _orgImage = [[self image] copy];
21                 _smallImage = nil;
22                 _toolbarItem = nil;
23         }
24         return self;
25 }
26
27 - (void) dealloc {
28         [_orgImage release];
29         [_smallImage release];
30
31         _orgImage = nil;
32         _smallImage = nil;
33         _toolbarItem = nil;
34
35         [super dealloc];
36 }
37
38 - (void) drawRect:(NSRect) rect {
39     [super drawRect:rect];
40
41     if( [self drawsArrow] ) {
42             NSBezierPath *path = [NSBezierPath bezierPath];
43
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 )];
52                 }
53
54                 [path closePath];
55                 [[[NSColor blackColor] colorWithAlphaComponent:0.75] set];
56                 [path fill];
57     }
58 }
59
60 - (void) mouseDown:(NSEvent *) theEvent {
61         if( ! [self isEnabled] ) return;
62         if( ! [self menu] ) {
63                 [super mouseDown:theEvent];
64                 return;
65         }
66
67         [self highlight:YES];
68
69         NSPoint point = [self convertPoint:[self bounds].origin toView:nil];
70         point.y -= NSHeight( [self frame] ) + 2.;
71         point.x -= 1.;
72
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];
75
76         [self mouseUp:[[NSApplication sharedApplication] currentEvent]];
77 }
78
79 - (void) mouseUp:(NSEvent *) theEvent {
80         [self highlight:NO];
81         [super mouseUp:theEvent];
82 }
83
84 - (void) mouseDragged:(NSEvent *) theEvent {
85         return;
86 }
87
88 - (NSControlSize) controlSize {
89         return ( _size ? _size : NSRegularControlSize );
90 }
91
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 ) {
99                 if( ! _smallImage ) {
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];
106                 }
107                 [super setImage:_smallImage];
108                 [_toolbarItem setMinSize:NSMakeSize( 24., 24. )];
109                 [_toolbarItem setMaxSize:NSMakeSize( 24., 24. )];
110         }
111         _size = controlSize;
112 }
113
114 - (void) setImage:(NSImage *) image {
115         [_orgImage autorelease];
116         _orgImage = [[self image] copy];
117
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];
125
126         if( _size == NSRegularControlSize ) [super setImage:image];
127         else if( _size == NSSmallControlSize ) [super setImage:_smallImage];
128 }
129
130 - (NSImage *) smallImage {
131         return _smallImage;
132 }
133
134 - (void) setSmallImage:(NSImage *) image {
135         [_smallImage autorelease];
136         _smallImage = [image copy];
137 }
138
139 - (NSToolbarItem *) toolbarItem {
140         return _toolbarItem;
141 }
142
143 - (void) setToolbarItem:(NSToolbarItem *) item {
144         _toolbarItem = item;
145 }
146
147 - (BOOL) drawsArrow {
148         return _drawsArrow;
149 }
150
151 - (void) setDrawsArrow:(BOOL) arrow {
152         _drawsArrow = arrow;
153 }
154
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] )
161                 return _toolbarItem;
162         return [super accessibilityAttributeValue:attribute];
163 }
164 @end