-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSButton.m
167 lines (132 loc) · 3.73 KB
/
SSButton.m
1
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
#import "SSButton.h"
#import "NS(Attributed)String+Geometrics.h"
//#import "SSViewSizing.h"
@implementation SSButtonCell
- (id)initWithTitleFont:(NSFont*)font {
if ((self = [super init])) {
[self setFont:font] ;
}
return self ;
}
- (void)setColor:(NSColor*)color {
[color retain] ;
[_color release] ;
_color = color ;
}
- (NSColor*)color {
return _color ;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
[super drawInteriorWithFrame:cellFrame inView:controlView];
if ([self color] != nil) {
[[[self color] colorWithAlphaComponent:0.9] set];
NSRectFillUsingOperation(cellFrame, NSCompositingOperationPlusDarker);
}
}
- (void)mouseDown:(NSEvent *)theEvent {
[[self target] performSelector:[self action]] ;
//[super mouseDown:theEvent] ;
}
@end
@implementation SSButton
- (BOOL)keyEquivalentWithOrWithoutAltKey {
return keyEquivalentWithOrWithoutAltKey;
}
- (void)setKeyEquivalentWithOrWithoutAltKey:(BOOL)value {
if (keyEquivalentWithOrWithoutAltKey != value) {
keyEquivalentWithOrWithoutAltKey = value;
}
}
- (void)setKeyEquivalentSuffix:(NSString*)suffix {
[suffix retain] ;
[_keyEquivalentSuffix release] ;
_keyEquivalentSuffix = suffix ;
}
- (NSString*)keyEquivalentSuffix
{
return _keyEquivalentSuffix ;
}
- (void)setTitleText:(NSString*)titleText {
[titleText retain] ;
[_titleText release] ;
_titleText = titleText ;
if (keyEquivalentShowing) {
NSString* newTitle = [[NSString alloc] initWithFormat:@"%@ %@",
_titleText,
[self keyEquivalentSuffix]] ;
[self setTitle:newTitle] ;
[newTitle release] ;
}
else {
[self setTitle:_titleText] ;
}
}
- (NSString*)titleText
{
return _titleText ;
}
- (void)setColor:(NSColor*)color {
[[self cell] setColor:color] ;
}
- (id)initWithCoder:(NSCoder *)coder {
if ((self = [super initWithCoder:coder])) {
// I don't know why this was there. Commented out in Bookdog 4.0.1...
// [self setCell:[[[SSButtonCell alloc] initWithTitleFont:[self font]] autorelease]] ;
[self setKeyEquivalentWithOrWithoutAltKey:NO] ; // default behavior
}
return self ;
}
// Over-ride this so it will not highlight for milliseconds
// Just send the action to the target
//[[self target] performSelector:[self action]] ;
- (void)showKeyEquivalent {
NSString* newTitle = [[NSString alloc] initWithFormat:@"%@ %@",
[self titleText],
[self keyEquivalentSuffix]] ;
NSFont* font = [self font] ;
CGFloat height = [font boundingRectForFont].size.height ;
CGFloat width ;
width = [newTitle widthForHeight:height
font:font] ;
if (width > [self frame].size.width - widthMargin) {
[newTitle release] ;
newTitle = [[NSString alloc] initWithFormat:@"%@\n%@",
[self titleText],
[self keyEquivalentSuffix]] ;
}
[self setTitle:newTitle] ;
[newTitle release] ;
keyEquivalentShowing = YES ;
}
- (void)setWidthMargin:(CGFloat)wm {
widthMargin = wm ;
}
- (void)hideKeyEquivalent {
[self setTitle:[self titleText]] ;
keyEquivalentShowing = NO ;
}
- (void)dealloc {
[_titleText release] ;
[_keyEquivalentSuffix release] ;
[super dealloc] ;
}
- (BOOL)performKeyEquivalent:(NSEvent *)event {
NSEvent* tweakedEvent ;
if ([self keyEquivalentWithOrWithoutAltKey]) {
tweakedEvent = [NSEvent keyEventWithType:[event type]
location:[event locationInWindow]
modifierFlags:[event modifierFlags] & ~NSEventModifierFlagOption
timestamp:[event timestamp]
windowNumber:[event windowNumber]
context:nil
characters:[event characters]
charactersIgnoringModifiers:[event charactersIgnoringModifiers]
isARepeat:[event isARepeat]
keyCode:[event keyCode]] ;
}
else {
tweakedEvent = event ;
}
return [super performKeyEquivalent:tweakedEvent] ;
}
@end