-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCalculatorAction.m
303 lines (253 loc) · 13.1 KB
/
CalculatorAction.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
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
294
295
296
297
298
299
300
301
302
303
//
// CalculatorAction.m
// Quicksilver
//
// Created by Kevin Ballard, modified by Patrick Robertson
// Copyright QSApp.com 2011
#import <QSCore/QSLibrarian.h>
#import <QSCore/QSNotifyMediator.h>
#import <QSFoundation/QSGCD.h>
#import "CalculatorAction.h"
#import "CalculatorPrefPane.h"
/* CalculatePrivate.h is from a private framework, reverse engineered by Nicholas Jitkoff.
There are no guarantees that this will work indefinitely. It may break in a future version of OS X */
#import "CalculatePrivate.h"
@implementation CalculatorActionProvider
- (id) init {
if ((self = [super init])) {
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:CalculatorDisplayNormal], kCalculatorDisplayPref,
[NSNumber numberWithInteger:kQSCalculatorModeCalculate], kQSCalculatorMode,
nil]];
}
return self;
}
- (QSObject *)calculate:(QSObject *)dObject {
QSObject *result = [self performCalculation:dObject fromAction:YES];
[result setPrimaryType:QSFormulaType];
[result setIcon:[self iconForObject:result]];
NSString *outString = [result objectForType:QSTextType];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Copy the result to the clipboard
if ([defaults objectForKey:kCalculatorCopyResultToClipboard] && [[defaults objectForKey:kCalculatorCopyResultToClipboard] boolValue]) {
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[pb setString:outString forType:NSStringPboardType];
}
switch ([[defaults objectForKey:kCalculatorDisplayPref] integerValue]) {
case CalculatorDisplayNormal:
// Do nothing - we're popping the result back up
break;
case CalculatorDisplayLargeType: {
// Display result as large type
QSGCDMainSync(^{
QSShowLargeType(outString);
[[QSReg preferredCommandInterface] selectObject:result];
});
result = nil;
break;
} case CalculatorDisplayNotification: {
// Display result as notification
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[QSResourceManager imageNamed:@"com.apple.calculator"], QSNotifierIcon,
@"Calculation Result", QSNotifierTitle,
outString, QSNotifierText,
@"QSCalculatorResultNotification", QSNotifierType, nil];
QSGCDMainSync(^{
QSShowNotifierWithAttributes(attributes);
[[QSReg preferredCommandInterface] selectObject:result];
});
result = nil;
}
}
return result;
}
- (QSObject *)performCalculation:(QSObject *)dObject fromAction:(BOOL)fromAction {
NSString *value;
if ([[dObject primaryType] isEqualToString:QSFormulaType]) {
value = [dObject objectForType:QSFormulaType];
} else {
value = [dObject objectForType:QSTextType];
}
// Remove leading '=' sign
if ([value length] && [value characterAtIndex:0] == [@"=" characterAtIndex:0]) {
value = [value stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
}
// Convert float separators
NSLocale *locale = [NSLocale autoupdatingCurrentLocale];
NSString *decimalSeparator = [locale objectForKey:NSLocaleDecimalSeparator];
NSString *groupingSeparator = [locale objectForKey:NSLocaleGroupingSeparator];
NSString *ungrouped = [value stringByReplacingOccurrencesOfString:groupingSeparator
withString:@""
options:0
range:NSMakeRange(0, [value length])];
// Check to see if the user used grouping in their expression. If they did, remember it and format the answer to use grouping as well.
BOOL usedGrouping = FALSE;
if (![ungrouped isEqualToString:value]) {
usedGrouping = TRUE;
}
value = ungrouped;
value = [value stringByReplacingOccurrencesOfString:decimalSeparator
withString:@"."
options:0
range:NSMakeRange(0, [value length])];
NSString *outString = nil;
QSCalculatorMode mode = kQSCalculatorModeCalculate;
NSNumber *modeNumber = [[NSUserDefaults standardUserDefaults] objectForKey:kQSCalculatorMode];
if (modeNumber)
mode = [[NSUserDefaults standardUserDefaults] integerForKey:kQSCalculatorMode];
switch (mode) {
case kQSCalculatorModeCalculate: {
// Source taken from QSB (BELOW) See COPYING in the Resource folder for full copyright details
// Fix up separators and decimals (for current user's locale). The Calculator framework wants
// '.' for decimals, and no grouping separators.
char answer[1024];
answer[0] = '\0';
int success = CalculatePerformExpression((char *)[value UTF8String], 20, 1, answer);
if (!success) {
// calculation failed
if (fromAction) {
QSShowAppNotifWithAttributes(@"calculator", NSLocalizedStringFromTableInBundle(@"Calculation failed", nil, [NSBundle bundleForClass:[self class]], @"title of the calculation failed notif"), [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"Unable to calculate %@", nil, [NSBundle bundleForClass:[self class]], @"title of the calculation failed notif"), value]);
}
return dObject;
}
outString = [NSString stringWithUTF8String:answer];
// Source taken from QSB Source Code (ABOVE)
break;
}
case kQSCalculatorModeBC: {
NSString *bcScript = [[NSArray arrayWithObjects:value, @"quit", @"", nil] componentsJoinedByString:@"\n"];
NSData *inputData = [bcScript dataUsingEncoding:NSUTF8StringEncoding];
NSTask *calculationTask = [NSTask taskWithLaunchPath:@"/usr/bin/bc" arguments:[NSArray arrayWithObjects:@"-q", @"-l", nil] input:inputData];
NSData *output = [calculationTask launchAndReturnOutput];
outString = [[[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding] autorelease];
outString = [outString trimWhitespace];
break;
}
case kQSCalculatorModeDC: {
NSString *dcScript = [[NSArray arrayWithObjects:value, @"p", @"", nil] componentsJoinedByString:@"\n"];
NSTask *calculationTask = [NSTask taskWithLaunchPath:@"/usr/bin/dc" arguments:[NSArray arrayWithObjects:@"-e", dcScript, nil]];
NSData *output = [calculationTask launchAndReturnOutput];
outString = [[[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding] autorelease];
outString = [outString trimWhitespace];
break;
}
}
if (![decimalSeparator isEqualToString:@"."]) {
outString = [outString stringByReplacingOccurrencesOfString:@"." withString:decimalSeparator];
}
NSArray *components = [outString componentsSeparatedByString:decimalSeparator];
NSArray *groupedComponents = components;
// format to use the grouping separator if the user used it in their original expression
if (usedGrouping && [[components objectAtIndex:0] length] > 3) {
ungrouped = outString;
unichar ans[1024];
NSMutableString *formatted_ans = [NSMutableString string];
[[components objectAtIndex:0] getCharacters:ans];
NSInteger i;
for (i = [[components objectAtIndex:0] length] - 3; i > 0; i = i-3) {
[formatted_ans prependFormat:@"%@%@", groupingSeparator, [[components objectAtIndex:0] substringWithRange:NSMakeRange(i, 3)]];
}
if (i != -3) {
[formatted_ans prependString:[[components objectAtIndex:0] substringWithRange:NSMakeRange(0, i+3)]];
}
if ([components count] > 1) {
[formatted_ans appendFormat:@"%@%@", decimalSeparator, [components lastObject]];
}
outString = [[formatted_ans copy] autorelease];
groupedComponents = [outString componentsSeparatedByString:decimalSeparator];
}
// Format the outstring to a certain number of decimal places
if ([components count] > 1) {
NSInteger numberOfDecimalPlaces = 7 - ([[components objectAtIndex:0] length]);
if (numberOfDecimalPlaces > 0) {
NSUInteger decimalLength = numberOfDecimalPlaces > (NSInteger)[[components lastObject] length] ? [[components lastObject] length] : numberOfDecimalPlaces;
NSRange powerQualifierRange = [[components lastObject] rangeOfString:@"e"];
NSString *powerQualifierString = nil;
if (powerQualifierRange.location != NSNotFound) {
NSUInteger powerQualifierLength = [[components lastObject] length] - powerQualifierRange.location;
decimalLength = decimalLength - powerQualifierLength;
powerQualifierString = [[components lastObject] substringFromIndex:powerQualifierRange.location];
}
outString = [NSString stringWithFormat:@"%@%@%@%@", [groupedComponents objectAtIndex:0], decimalLength > 0 ? decimalSeparator : @"", decimalLength > 0 ? [[groupedComponents lastObject] substringWithRange:NSMakeRange(0, decimalLength)] : @"", powerQualifierString ? powerQualifierString : @""];
} else {
outString = [groupedComponents objectAtIndex:0];
}
}
QSObject *result = [QSObject objectWithName:outString];
[result setObject:outString forType:QSTextType];
return result;
}
-(NSImage *)iconForObject:(QSObject *)object {
NSString *resultString = [object objectForType:QSTextType];
// Max icon size for the current command interface
NSSize maxIconSize = QSSize128;
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:maxIconSize.width
pixelsHigh:maxIconSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0
bytesPerRow:0
bitsPerPixel:0]
autorelease];
if(bitmap) {
// Sort The text format
NSData *data = [[NSUserDefaultsController sharedUserDefaultsController] valueForKeyPath:@"values.QSAppearance1T"];
NSColor *textColor = [NSUnarchiver unarchiveObjectWithData:data];
// Text font size
int size;
NSSize textSize;
NSFont *textFont;
for (size = 10; size<300; size = size+2) {
textFont = [NSFont systemFontOfSize:size+1];
textSize = [resultString sizeWithAttributes:[NSDictionary dictionaryWithObject:textFont forKey:NSFontAttributeName]];
if (textSize.width> maxIconSize.width - 10 || textSize.height > maxIconSize.height - 10) {
break;
}
}
// Text shadow
NSShadow *textShadow = [[NSShadow alloc] init];
int textShadowSize = (size > 20) ? size : 0;
[textShadow setShadowOffset:NSMakeSize(textShadowSize/40, -textShadowSize/40)];
[textShadow setShadowBlurRadius:textShadowSize/10];
[textShadow setShadowColor:[NSColor colorWithDeviceWhite:0 alpha:0.64]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:size-2],NSFontAttributeName,
textColor, NSForegroundColorAttributeName,
textShadow, NSShadowAttributeName, nil];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap]];
NSRect boundingRect = [resultString boundingRectWithSize:maxIconSize options:0 attributes:nil];
[resultString drawInRect:NSMakeRect(boundingRect.origin.x+(maxIconSize.width-textSize.width)/2, boundingRect.origin.y+(maxIconSize.height-textSize.height)/2, textSize.width, textSize.height) withAttributes:attributes];
[NSGraphicsContext restoreGraphicsState];
NSImage *icon = [[NSImage alloc] initWithSize:[bitmap size]];
[icon addRepresentation:bitmap];
// release objects
[textShadow release];
return icon;
}
return nil;
}
- (void)setQuickIconForObject:(QSObject *)object {
QSObject *result = [self performCalculation:object fromAction:NO];
// Still a formula object (i.e. there was a problem with the syntax) Use a clip icon
if ([[result primaryType] isEqualToString:QSFormulaType]) {
[object setIcon:[[NSWorkspace sharedWorkspace] iconForFileType:@"'clpt'"]];
return;
}
// Use the result (a number) as the icon
else {
NSImage *icon = [self iconForObject:result];
if (icon) {
[object setIcon:icon];
// Set the object's details to show the result
NSString *resultString = [result objectForType:QSTextType];
[object setDetails:resultString];
}
}
return;
}
@end