-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.m
executable file
·759 lines (671 loc) · 18.3 KB
/
InputManager.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// ##############################################################
// InputManager.m
// Magic Number Machine
//
// Created by Matt Gallagher on Sun Apr 20 2003.
// Copyright (c) 2003 Matt Gallagher. All rights reserved.
// ##############################################################
#import "InputManager.h"
#import "Expression.h"
#import "CallBack.h"
#import "DataManager.h"
#import "OpEnumerations.h"
#import "BigCFloat.h"
#import "Value.h"
// #import "SYFlatButton.h"
//
// About the InputManager
//
// The InputManager coordinates input from most of the buttons in the window.
// Not much more than that. It uses the DataManager for everything else. It does
// talk directly to the current input point, but only after requesting it from the
// DataManager.
//
// Naturally, since there is only one window in the application, there is only one
// instance of this class inthe application.
//
@implementation InputManager
//
// allClearPressed
//
// Tells the DataManager to clear the expression when the AC button is pressed.
//
- (IBAction)allClearPressed:(id)sender
{
[dataManager clearExpression];
}
//
// binaryOpPressed
//
// Inserts the appropriate operation into the expression.
//
- (IBAction)binaryOpPressed:(id)sender
{
Expression *inputPoint;
int buttonTag = (int)[sender tag];
[dataManager ensureInputWithValue:YES];
inputPoint = [dataManager getInputPoint];
[inputPoint binaryOpPressed:buttonTag];
[dataManager valueChanged];
}
//
// bracketPressed
//
// Inserts the appropriate bracket into the expression.
//
- (IBAction)bracketPressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:YES];
inputPoint = [dataManager getInputPoint];
if ([sender tag] == 0)
[inputPoint bracketPressed];
else
[inputPoint closeBracketPressed];
[dataManager valueChanged];
}
//
// cancelPreferences
//
// User dismissed the preferences dialog.
//
- (IBAction)cancelPreferences:(id)sender
{
[NSApp stopModal];
}
//
// clearPressed
//
// Clears the current value.
//
- (IBAction)clearPressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
[inputPoint clear];
[dataManager valueChanged];
}
//
// commitPreferences
//
// User saved the preferences dialog.
//
- (IBAction)commitPreferences:(id)sender
{
// dennis ---------------------------------
BOOL errorFlag = NO;
if ([defaultFixed intValue] > 10)
{
[defaultFixed setIntValue:10];
errorFlag = YES;
}
if ([defaultFixed intValue] < 0)
{
[defaultFixed setIntValue:0];
errorFlag = YES;
}
if ([defaultSignificant intValue] > [dataManager getMaximumLength])
{
[defaultSignificant setIntValue:[dataManager getMaximumLength]];
errorFlag = YES;
}
if ([defaultSignificant intValue] < 1)
{
[defaultSignificant setIntValue:1];
errorFlag = YES;
}
if ([defaultDigits intValue] > [dataManager getMaximumLength])
{
[defaultDigits setIntValue:[dataManager getMaximumLength]];
errorFlag = YES;
}
if ([defaultDigits intValue] < 3)
{
[defaultDigits setIntValue:3];
errorFlag = YES;
}
if (errorFlag)
{
NSBeep();
return;
}
[dataManager saveDefaultsForThousands:[thousandsSeparator intValue] != 0
fractions:[fractionSeparator intValue] != 0
digits:[defaultDigits intValue]
significant:[defaultSignificant intValue]
fixed:[defaultFixed intValue]
display:(int)[[defaultDisplay selectedCell] tag]];
[dataManager updateLengthLimit];
[NSApp stopModal];
}
//
// copy
//
// Copies the current expression onto the clipboard.
//
- (IBAction)copy:(id)sender
{
NSPasteboard *pasteBoard;
Expression *expression;
NSData *data;
NSString *stringValue;
expression = [[dataManager getCurrentExpression] child];
data = [NSKeyedArchiver archivedDataWithRootObject:expression requiringSecureCoding:NO error:nil];
pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard declareTypes:@[@"MNMExpression", @"NSStringPboardType"] owner:self];
[pasteBoard setData:data forType:@"MNMExpression"];
stringValue = [expression getExpressionString];
if ([dataManager getEqualsPressed]) {
Value *result = [[Value alloc] initWithParent:nil value:[expression getValue] andManager:dataManager];
stringValue = [[stringValue stringByAppendingString:@" = "] stringByAppendingString:[result getExpressionString]];
}
[pasteBoard setString:stringValue forType:@"NSStringPboardType"];
}
//
// copyResult
//
// Copies the result onto the clipboard.
//
- (IBAction)copyResult:(id)sender
{
NSPasteboard *pasteBoard;
BigCFloat *value;
NSData *data;
NSString *stringValue;
Value *result;
value = [[dataManager getCurrentExpression] getValue];
data = [NSKeyedArchiver archivedDataWithRootObject:value requiringSecureCoding:NO error:nil];
pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard declareTypes:@[@"BigCFloat", @"NSStringPboardType"] owner:self];
[pasteBoard setData:data forType:@"BigCFloat"];
result = [[Value alloc] initWithParent:nil value:value andManager:dataManager];
stringValue = [result getExpressionString];
[pasteBoard setString:stringValue forType:@"NSStringPboardType"];
}
//
// delPressed
//
// Deletes the digit or operation at the current insertion point.
//
- (IBAction)delPressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
[inputPoint deleteDigit];
[dataManager valueChanged];
}
//
// digitPressed
//
// Inserts another digit into the current number.
//
- (IBAction)digitPressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
[inputPoint appendDigit:(int)[sender tag]];
[dataManager valueChanged];
}
//
// equalsPressed
//
// Tells the DataManager to show the result.
//
- (IBAction)equalsPressed:(id)sender
{
[dataManager equalsPressed];
}
//
// exponentShiftPressed
//
// Shifts the result left or right accordingly.
//
- (IBAction)exponentShiftPressed:(NSButton *)sender
{
Expression *inputPoint;
if ([dataManager getShift] == 0) {
[dataManager ensureInputWithValue:YES];
inputPoint = [dataManager getInputPoint];
if (sender.tag < 0) {
[inputPoint binaryOpPressed:xorOp];
} else {
[inputPoint preOpPressed:notOp];
}
[dataManager valueChanged];
} else {
[dataManager shiftResult:sender.tag < 0];
}
}
//
// expPressed
//
// Adds a scientific notation style exponent to the current number.
//
- (IBAction)expPressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
[inputPoint exponentPressed];
[dataManager valueChanged];
}
//
// optionPressed
//
// Toggles the option state.
//
- (IBAction)optionPressed:(id)sender
{
// We use this wierd CallBack class because the button is already highlighted
// when this method is called and will be unhighlighted shortly after. By
// using the CallBack, optionToggled will get called after the unhighlight has
// occurred, so that we can set the button highlight without it getting mucked
// up as the stack unwinds.
// [CallBack callBack:dataManager method:^id(id param) {
// [self->dataManager optionToggled];
// return nil;
// }];
// [CallBack callBack:dataManager method:@selector(optionToggled)];
}
//
// paste
//
// Inserts the number or expression from the clipboard into the current expression.
//
- (IBAction)paste:(id)sender
{
NSPasteboard *pasteBoard;
NSArray *pasteBoardTypes;
NSData *pasteData;
Expression *inputPoint;
pasteBoard = [NSPasteboard generalPasteboard];
pasteBoardTypes = [pasteBoard types];
if ([pasteBoardTypes containsObject:@"MNMExpression"])
{
pasteData = [pasteBoard dataForType:@"MNMExpression"];
if (pasteData != nil)
{
Expression *pasteExpression;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
pasteExpression = [NSKeyedUnarchiver unarchiveObjectWithData:pasteData];
#pragma GCC diagnostic pop
// pasteExpression = [NSKeyedUnarchiver unarchivedObjectOfClass:Expression.class fromData:pasteData error:nil];
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
// [inputPoint bracketPressed]; // Mike: why do we need brackets?
// inputPoint = [dataManager getInputPoint];
[inputPoint expressionInserted:pasteExpression];
// [inputPoint closeBracketPressed];
[dataManager valueChanged];
}
}
else if([pasteBoardTypes containsObject:@"BigCFloat"])
{
pasteData = [pasteBoard dataForType:@"BigCFloat"];
if(pasteData != nil)
{
BigCFloat *pasteValue;
// pasteValue = [NSKeyedUnarchiver unarchivedObjectOfClass:BigCFloat.class fromData:pasteData error:nil];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
pasteValue = [NSKeyedUnarchiver unarchiveObjectWithData:pasteData];
#pragma GCC diagnostic pop
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
[inputPoint valueInserted:pasteValue];
[dataManager valueChanged];
}
}
else if([pasteBoardTypes containsObject:@"NSStringPboardType"])
{
NSString *pasteString;
pasteString = [pasteBoard stringForType:@"NSStringPboardType"];
if(pasteString != nil && ![pasteString isEqualToString:@""])
{
double value = [pasteString doubleValue];
if (value != 0.0 || [pasteString isEqualToString:@"0"] || [pasteString isEqualToString:@"0.0"])
{
BigCFloat *bcf_value = [BigCFloat bigFloatWithDouble:value radix:[dataManager getRadix]];
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
[inputPoint valueInserted:bcf_value];
[dataManager valueChanged];
}
}
}
}
//
// postOpPressed
//
// Inserts the relevant operation into the current expression.
//
- (IBAction)postOpPressed:(id)sender
{
Expression *inputPoint;
int buttonTag = (int)[sender tag];
[dataManager ensureInputWithValue:YES];
inputPoint = [dataManager getInputPoint];
if (buttonTag == factorialOp && [dataManager getShift])
{
[self preOpPressed:sender];
return;
}
[inputPoint postOpPressed:buttonTag];
[dataManager valueChanged];
}
//
// precisionCancel
//
// Dismiss any sheets over the window because the cancel button is pressed.
//
- (IBAction)precisionCancel:(id)sender
{
[mainWindow endSheet:[sender window] returnCode:NSModalResponseCancel];
}
//
// precisionOk
//
// Change the precision (if the number is valid) and dismiss the sheet.
//
- (IBAction)precisionOk:(id)sender
{
if ([[sender window] isEqual:fixSettings])
{
if ([fixValue intValue] > 10)
{
[fixValue setIntValue:10];
NSBeep();
return;
}
if ([fixValue intValue] < 0)
{
[fixValue setIntValue:0];
NSBeep();
return;
}
[dataManager setDefaultDisplayType:2]; // dennis
[dataManager setDefaultFixed:[fixValue intValue]]; // dennis
// [dataManager lengthLimit:20 fillLimit:YES fixedPlaces:[fixValue intValue]];
}
else if ([[sender window] isEqual:sciSettings])
{
if ([sciValue intValue] > [dataManager getMaximumLength])
{
[sciValue setIntValue:[dataManager getMaximumLength]];
NSBeep();
return;
}
if ([sciValue intValue] < 1)
{
[sciValue setIntValue:1];
NSBeep();
return;
}
[dataManager setDefaultDisplayType:1]; // dennis
[dataManager setDefaultSignificant:[sciValue intValue]]; // dennis
// [dataManager lengthLimit:[sciValue intValue] fillLimit:YES fixedPlaces:0];
}
else
{
if ([dispValue intValue] > [dataManager getMaximumLength])
{
[dispValue setIntValue:[dataManager getMaximumLength]];
NSBeep();
return;
}
if ([dispValue intValue] < 3)
{
[dispValue setIntValue:3];
NSBeep();
return;
}
[dataManager setDefaultDisplayType:0]; // dennis
[dataManager setDefaultDigits:[dispValue intValue]]; // dennis
// [dataManager lengthLimit:[dispValue intValue] fillLimit:NO fixedPlaces:0];
}
[dataManager updateLengthLimit]; // dennis
[mainWindow endSheet:[sender window] returnCode:NSModalResponseCancel];
}
//
// precisionPressed
//
// Display a sheet over the window so that the user can change the precision.
//
- (IBAction)precisionPressed:(id)sender
{
BOOL shift = [dataManager getShift];
BOOL option = [dataManager getOption];
NSWindow *sheet;
NSWindow *targetWindow = [self window];
if (option)
{
sheet = fixSettings;
[fixValue setIntValue: [dataManager getDefaultFixedFromPref]]; // dennis
}
else if (shift)
{
sheet = sciSettings;
[sciValue setIntValue: [dataManager getDefaultSignificantFromPref]]; // dennis
}
else
{
sheet = dispSettings;
[dispValue setIntValue: [dataManager getDefaultDigitsFromPref]]; // dennis
}
[targetWindow beginSheet:sheet completionHandler:nil];
// [NSApp beginSheet:sheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
// [NSApp runModalForWindow:sheet];
// [NSApp endSheet:sheet];
// [sheet orderOut:self];
// The modal sheet can mess with our shift/option key detection. Clear them here.
// [dataManager shiftIsPressed:NO];
}
//
// preferences
//
// Brings up the preferences panel and alters settings accordingly.
//
- (IBAction)preferences:(id)sender
{
// dennis:
[self setDefaultsForThousands: [dataManager getDefaultThousandsSeparatorFromPref]
fractions: [dataManager getDefaultFractionSeparatorFromPref]
digits: [dataManager getDefaultDigitsFromPref]
significant: [dataManager getDefaultSignificantFromPref]
fixed: [dataManager getDefaultFixedFromPref]
display: [dataManager getDefaultDisplayTypeFromPref] ];
[NSApp runModalForWindow:preferencesPanel];
[preferencesPanel orderOut:self];
}
//
// preOpPressed
//
// Insert the relevant operation into the current expression.
//
- (IBAction)preOpPressed:(id)sender
{
Expression *inputPoint;
int buttonTag = (int)[sender tag];
[dataManager ensureInputWithValue:YES];
inputPoint = [dataManager getInputPoint];
switch (buttonTag)
{
case sinOp:
case cosOp:
case tanOp:
case sinhOp:
case coshOp:
case tanhOp:
if ([dataManager getShift]) buttonTag += 3;
break;
case modOp:
if ([dataManager getShift]) buttonTag = argOp;
break;
case tenOp:
if ([dataManager getShift]) buttonTag = twoOp;
break;
case logOp:
if ([dataManager getShift]) buttonTag = log2Op;
break;
case factorialOp:
NSAssert([dataManager getShift], @"Button input misdirected.");
buttonTag = sigmaOp;
default:
break;
}
[inputPoint preOpPressed:buttonTag];
[dataManager valueChanged];
}
//
// setDefaultsForThousands
//
// Default states for panels.
//
- (void)setDefaultsForThousands:(BOOL)thousands fractions:(BOOL) fractions digits:(int)digits significant:(int)significant fixed:(int)fixed display:(int)display
{
[thousandsSeparator setIntValue:thousands ? 1 : 0];
[fractionSeparator setIntValue:fractions ? 1 : 0];
[defaultDigits setIntValue:digits];
[defaultSignificant setIntValue:significant];
[defaultFixed setIntValue:fixed];
[defaultDisplay selectCellAtRow:display column:0];
}
//
// shiftPressed
//
// Toggles the shift button.
//
- (IBAction)shiftPressed:(id)sender
{
[dataManager shiftIsPressed]; // toggle the shift flag
}
//
// trigModePressed
//
// Tells the DataManager to cycle the trigonometric mode.
//
- (IBAction)trigModePressed:(NSButton *)sender
{
[dataManager trigModePressedWithButton:sender];
}
//
// userPointPressed
//
// Inserts a fractional point into the number in the current expression.
//
- (IBAction)userPointPressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:YES];
inputPoint = [dataManager getInputPoint];
[inputPoint userPointPressed];
[dataManager valueChanged];
}
//
// valuePressed
//
// Inserts pi or i into the current expression.
//
- (IBAction)valuePressed:(id)sender
{
Expression *inputPoint;
[dataManager ensureInputWithValue:NO];
inputPoint = [dataManager getInputPoint];
if ([sender tag] == 0) {
// "π" Pressed
[inputPoint constantPressed:Pi];
} else {
// "i" Pressed
[inputPoint constantPressed:RootOfMinusOne];
}
[dataManager valueChanged];
}
//
// acceptsFirstResponder
//
// We want to get the focus (this is for copy and paste and stuff).
//
- (BOOL)acceptsFirstResponder
{
return YES;
}
//
// applicationDidFinishLaunching
//
// Handles this for the main window. Tells the DataManager to dispatch the
// result to everything else.
//
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[dataManager setStartupState];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
}
- (void)applicationDidUnhide:(NSNotification *)aNotification
{
[mainWindow makeKeyAndOrderFront:self];
}
- (void)enable:(NSButton *)b enable:(BOOL) enable {
[b setEnabled: enable];
// b.titleNormalColor = enable ? NSColor.labelColor : NSColor.grayColor;
}
//
// setControlsForRadix
//
// Enable only the digits that are valid for the current radix.
//
// Simplified and cleaned up to work for any radix 2 - 16
- (void)setControlsForRadix:(short)radix
{
[self enable:twoButton enable:radix > 2];
[self enable:threeButton enable:radix > 3];
[self enable:fourButton enable:radix > 4];
[self enable:fiveButton enable:radix > 5];
[self enable:sixButton enable:radix > 6];
[self enable:sevenButton enable:radix > 7];
[self enable:eightButton enable:radix > 8];
[self enable:nineButton enable:radix > 9];
[self enable:aButton enable:radix > 10];
[self enable:bButton enable:radix > 11];
[self enable:cButton enable:radix > 12];
[self enable:dButton enable:radix > 13];
[self enable:eButton enable:radix > 14];
[self enable:fButton enable:radix > 15];
}
//
// window
//
// Returns a pointer to the current window.
//
- (id)window
{
return [dataManager window];
}
//
// Help main menu:
//
// Invoked from the help menu.
//
- (IBAction)showHelp:(id)sender
{
NSURL *path = [[NSBundle mainBundle] URLForResource:@"Magic-Number-Machine" withExtension:@"pdf"];
// NSURL *path = [[NSBundle mainBundle] urlForResource:@"Magic-Number-Machine" ofType:@"pdf"];
[[NSWorkspace sharedWorkspace] openURL:path];
}
//
// showKeyboardShortcuts:
//
// Invoked from the help menu.
//
- (void)showKeyboardShortcuts:(id)sender
{
// NSString *path = [[NSBundle mainBundle] pathForResource:@"Magic_Number_Machine_Buttons" ofType:@"html"];
NSURL *path = [[NSBundle mainBundle] URLForResource:@"Magic_Number_Machine_Buttons" withExtension:@"html"];
[[NSWorkspace sharedWorkspace] openURL:path];
}
@end