forked from gnachman/iTerm2
/
PointerPrefsController.m
1121 lines (1016 loc) · 42.1 KB
/
PointerPrefsController.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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PointerPrefsController.m
// iTerm
//
// Created by George Nachman on 11/7/11.
// Copyright (c) 2011 George Nachman. All rights reserved.
//
#import "PointerPrefsController.h"
#import "PointerController.h"
#import "PreferencePanel.h"
#import "ITAddressBookMgr.h"
static NSString *kPointerActionsKey = @"PointerActions"; // Used in NSUserDefaults
static NSString *kActionKey = @"Action"; // Used within values
static NSString *kArgumentKey = @"Argument"; // Used within values
static NSString *kCommandKeyChar = @"c";
static NSString *kOptionKeyChar = @"o";
static NSString *kShiftKeyChar = @"s";
static NSString *kControlKeyChar = @"^";
#define kLeftButton 0
#define kRightButton 1
#define kMiddleButton 2
static int kMaxClicks = 4;
static const int kMinGestureTag = 10;
#define kThreeFingerTapGestureTag 10
#define kThreeFingerSwipeRightGestureTag 11
#define kThreeFingerSwipeLeftGestureTag 12
#define kThreeFingerSwipeUpGestureTag 13
#define kThreeFingerSwipeDownGestureTag 14
static NSString *kButtonSchema = @"Button"; // First field of action key
static NSString *kGestureSchema = @"Gesture"; // First field of action key
NSString *kThreeFingerClickGesture = @"ThreeFingerClick"; // Second field of action key (gesture type)
NSString *kThreeFingerSwipeRight = @"ThreeFingerSwipeRight"; // Second field of action key (gesture type)
NSString *kThreeFingerSwipeLeft = @"ThreeFingerSwipeLeft"; // Second field of action key (gesture type)
NSString *kThreeFingerSwipeUp = @"ThreeFingerSwipeUp"; // Second field of action key (gesture type)
NSString *kThreeFingerSwipeDown = @"ThreeFingerSwipeDown"; // Second field of action key (gesture type)
NSString *kPasteFromClipboardPointerAction = @"kPasteFromClipboardPointerAction";
NSString *kPasteFromSelectionPointerAction = @"kPasteFromSelectionPointerAction";
NSString *kOpenTargetPointerAction = @"kOpenTargetPointerAction";
NSString *kOpenTargetInBackgroundPointerAction = @"kOpenTargetInBackgroundPointerAction";
NSString *kSmartSelectionPointerAction = @"kSmartSelectionPointerAction";
NSString *kSmartSelectionIgnoringNewlinesPointerAction = @"kSmartSelectionIgnoringNewlinesPointerAction";
NSString *kContextMenuPointerAction = @"kContextMenuPointerAction";
NSString *kNextTabPointerAction = @"kNextTabPointerAction";
NSString *kPrevTabPointerAction = @"kPrevTabPointerAction";
NSString *kNextWindowPointerAction = @"kNextWindowPointerAction";
NSString *kPrevWindowPointerAction = @"kPrevWindowPointerAction";
NSString *kMovePanePointerAction = @"kMovePanePointerAction";
NSString *kSendEscapeSequencePointerAction = @"kSendEscapeSequencePointerAction";
NSString *kSendHexCodePointerAction = @"kSendHexCodePointerAction";
NSString *kSendTextPointerAction = @"kSendTextPointerAction";
NSString *kSelectPaneLeftPointerAction = @"kSelectPaneLeftPointerAction";
NSString *kSelectPaneRightPointerAction = @"kSelectPaneRightPointerAction";
NSString *kSelectPaneAbovePointerAction = @"kSelectPaneAbovePointerAction";
NSString *kSelectPaneBelowPointerAction = @"kSelectPaneBelowPointerAction";
NSString *kNewWindowWithProfilePointerAction = @"kNewWindowWithProfilePointerAction";
NSString *kNewTabWithProfilePointerAction = @"kNewTabWithProfilePointerAction";
NSString *kNewVerticalSplitWithProfilePointerAction = @"kNewVerticalSplitWithProfilePointerAction";
NSString *kNewHorizontalSplitWithProfilePointerAction = @"kNewHorizontalSplitWithProfilePointerAction";
NSString *kSelectNextPanePointerAction = @"kSelectNextPanePointerAction";
NSString *kSelectPreviousPanePointerAction = @"kSelectPreviousPanePointerAction";
NSString *kExtendSelectionPointerAction = @"kExtendSelectionPointerAction";
NSString *kPointerPrefsChangedNotification = @"kPointerPrefsChangedNotification";
typedef enum {
kNoArg,
kEscPlusArg,
kHexCodeArg,
kTextArg,
kProfileArg
} ArgumentType;
@interface NSString (PointerPrefsController)
- (NSComparisonResult)comparePointerActions:(NSString *)other;
@end
@interface NSDictionary (PointerPrefsController)
- (NSComparisonResult)comparePointerPrefsValue:(NSDictionary *)other;
@end
@interface PointerPrefsController (Private)
+ (NSDictionary *)dictForAction:(NSString *)action;
+ (NSString *)modCharsForMask:(int)modifiers;
+ (int)maskForModChars:(NSString *)modChars;
+ (NSString *)keyForButton:(int)button clicks:(int)clicks modifiers:(int)modifiers;
+ (NSString *)keyForGesture:(NSString *)gestureDescription modifiers:(int)modifiers;
+ (BOOL)keyIsButton:(NSString *)key;
+ (NSArray *)buttonKeyComponents:(NSString *)key;
+ (NSArray *)gestureKeyComponents:(NSString *)key;
+ (int)buttonForKey:(NSString *)key;
+ (int)numClicksForKey:(NSString *)key;
+ (NSString *)localizedNumClicks:(int)n;
+ (NSString *)localizedButtonNameForButtonNumber:(int)n;
+ (NSString *)localizedGestureNameForGestureIdentifier:(NSString *)ident;
+ (NSString *)localizedModifers:(int)keyMods;
+ (NSDictionary *)localizedActionMap;
+ (NSString *)localizedActionForDict:(NSDictionary *)dict;
+ (NSString *)localizedActionForKey:(NSString *)key;
+ (NSString *)gestureIdentifierForKey:(NSString *)key;
+ (int)modifiersForKey:(NSString *)key;
+ (NSDictionary *)defaultActions;
+ (NSDictionary *)settings;
+ (void)setSettings:(NSDictionary *)newSettings;
+ (NSArray *)sortedKeys;
+ (NSString *)keyForRowIndex:(int)rowIndex;
+ (int)tagForGestureIdentifier:(NSString *)ident;
- (BOOL)okShouldBeEnabled;
- (void)editKey:(NSString *)key;
+ (BOOL)keyIsThreeFingerTap:(NSString *)key;
@end
@implementation PointerPrefsController
@synthesize hasSelection = hasSelection_;
- (void)dealloc
{
[origKey_ release];
[super dealloc];
}
+ (NSDictionary *)dictForAction:(NSString *)action
{
return [NSDictionary dictionaryWithObject:action forKey:kActionKey];
}
+ (NSString *)modCharsForMask:(int)modifiers
{
NSMutableString *modStr = [NSMutableString string];
if (modifiers & NSCommandKeyMask) {
[modStr appendString:kCommandKeyChar];
}
if (modifiers & NSAlternateKeyMask) {
[modStr appendString:kOptionKeyChar];
}
if (modifiers & NSShiftKeyMask) {
[modStr appendString:kShiftKeyChar];
}
if (modifiers & NSControlKeyMask) {
[modStr appendString:kControlKeyChar];
}
return modStr;
}
+ (int)maskForModChars:(NSString *)modChars
{
int mask = 0;
if ([modChars rangeOfString:kCommandKeyChar].location != NSNotFound) {
mask |= NSCommandKeyMask;
}
if ([modChars rangeOfString:kOptionKeyChar].location != NSNotFound) {
mask |= NSAlternateKeyMask;
}
if ([modChars rangeOfString:kShiftKeyChar].location != NSNotFound) {
mask |= NSShiftKeyMask;
}
if ([modChars rangeOfString:kControlKeyChar].location != NSNotFound) {
mask |= NSControlKeyMask;
}
return mask;
}
+ (NSString *)keyForButton:(int)button clicks:(int)clicks modifiers:(int)modifiers
{
NSString *modStr = [PointerPrefsController modCharsForMask:modifiers];
return [NSString stringWithFormat:@"%@,%d,%d,%@,", kButtonSchema, button, clicks, modStr];
}
+ (NSString *)keyForGesture:(NSString *)gestureDescription modifiers:(int)modifiers
{
return [NSString stringWithFormat:@"%@,%@,%@,",
kGestureSchema,
gestureDescription,
[PointerPrefsController modCharsForMask:modifiers]];
}
+ (BOOL)keyIsButton:(NSString *)key
{
return [key hasPrefix:kButtonSchema];
}
+ (BOOL)keyIsThreeFingerTap:(NSString *)key
{
if (![key hasPrefix:kGestureSchema]) {
return NO;
}
NSArray *components = [PointerPrefsController gestureKeyComponents:key];
NSString *gesture = [components objectAtIndex:1];
return [gesture isEqualToString:kThreeFingerClickGesture];
}
+ (NSArray *)buttonKeyComponents:(NSString *)key
{
// Parse string like "Button,1,2,cso,freeform text"
// Field 1: "Button"
// Field 2: Button number (0-maxint)
// Field 3: Number of clicks (0-4)
// Field 4: Modifiers mask, including c, o, s, and ^ optionally (cmd, opt, shift, ctrl).
// Field 5: Arbitrary textual parameter [may be empty]
NSArray *a = [key componentsSeparatedByString:@","];
if (a.count == 5 && [[a objectAtIndex:0] isEqualToString:kButtonSchema]) {
return a;
} else {
return nil;
}
}
+ (NSArray *)gestureKeyComponents:(NSString *)key
{
// Parse string like "Gesture,Three Finger Click,cso,free form text"
// Field 1: "Gesture"
// Field 2: Gesture identifier string
// Field 3: Modifiers mask, including c, o, s, and ^ optionally (cmd, opt, shift, ctrl).
// Field 4: Arbitrary textual parameter [may be empty]
NSArray *a = [key componentsSeparatedByString:@","];
if (a.count == 4 && [[a objectAtIndex:0] isEqualToString:kGestureSchema]) {
return a;
} else {
return nil;
}
}
+ (int)buttonForKey:(NSString *)key
{
NSArray *parts = [PointerPrefsController buttonKeyComponents:key];
if (parts) {
return [[parts objectAtIndex:1] intValue];
} else {
return -1;
}
}
+ (int)numClicksForKey:(NSString *)key
{
NSArray *parts = [PointerPrefsController buttonKeyComponents:key];
if (parts) {
return [[parts objectAtIndex:2] intValue];
} else {
return -1;
}
}
+ (NSString *)localizedNumClicks:(int)n
{
switch (n) {
case 1:
return @"single click";
case 2:
return @"double click";
case 3:
return @"triple click";
case 4:
return @"quad click";
default:
return @"(error)"; // shouldn't happen
}
}
+ (NSString *)localizedButtonNameForButtonNumber:(int)n
{
switch (n) {
case -1:
return @"Unknown button";
case kLeftButton:
return @"Left button";
case kRightButton:
return @"Right button";
case kMiddleButton:
return @"Middle button";
default:
return [NSString stringWithFormat:@"Button #%d", n+1];
}
}
+ (NSDictionary *)gestureNamesDict
{
NSDictionary *names = [NSDictionary dictionaryWithObjectsAndKeys:
@"Three-finger Tap", kThreeFingerClickGesture,
@"Three-finger Swipe Right", kThreeFingerSwipeRight,
@"Three-finger Swipe Left", kThreeFingerSwipeLeft,
@"Three-finger Swipe Up", kThreeFingerSwipeUp,
@"Three-finger Swipe Down", kThreeFingerSwipeDown,
nil];
return names;
}
+ (NSString *)localizedGestureNameForGestureIdentifier:(NSString *)ident
{
NSDictionary *names = [PointerPrefsController gestureNamesDict];
NSString *name = [names objectForKey:ident];
if (name) {
return name;
} else {
// Shouldn't happen
return ident;
}
}
+ (int)tagForGestureIdentifier:(NSString *)ident
{
NSArray *keys = [NSArray arrayWithObjects:
kThreeFingerClickGesture,
kThreeFingerSwipeRight,
kThreeFingerSwipeLeft,
kThreeFingerSwipeUp,
kThreeFingerSwipeDown,
nil];
NSUInteger i = [keys indexOfObject:ident];
if (i == NSNotFound) {
return -1;
}
return i + kMinGestureTag;
}
+ (NSString *)actionWithLocalizedName:(NSString *)localizedName
{
NSDictionary *actionMap = [PointerPrefsController localizedActionMap];
for (NSString *action in actionMap) {
NSString *curName = [actionMap objectForKey:action];
if ([curName isEqualToString:localizedName]) {
return action;
}
}
return [NSString stringWithFormat:@"Bad name: %@", localizedName];
}
+ (NSString *)gestureIdentifierForTag:(int)tag
{
switch (tag) {
case kThreeFingerTapGestureTag:
return kThreeFingerClickGesture;
case kThreeFingerSwipeRightGestureTag:
return kThreeFingerSwipeRight;
case kThreeFingerSwipeLeftGestureTag:
return kThreeFingerSwipeLeft;
case kThreeFingerSwipeUpGestureTag:
return kThreeFingerSwipeUp;
case kThreeFingerSwipeDownGestureTag:
return kThreeFingerSwipeDown;
default:
return [NSString stringWithFormat:@"Bad tag %d", tag];
}
}
+ (NSString *)localizedModifers:(int)keyMods
{
NSMutableString *theKeyString = [NSMutableString string];
if (keyMods & NSControlKeyMask) {
[theKeyString appendString: @"^"];
}
if (keyMods & NSAlternateKeyMask) {
[theKeyString appendString: @"⌥"];
}
if (keyMods & NSShiftKeyMask) {
[theKeyString appendString: @"⇧"];
}
if (keyMods & NSCommandKeyMask) {
[theKeyString appendString: @"⌘"];
}
return theKeyString;
}
+ (NSDictionary *)localizedActionMap
{
NSDictionary *names = [NSDictionary dictionaryWithObjectsAndKeys:
@"Paste from Clipboard", kPasteFromClipboardPointerAction,
@"Paste from Selection", kPasteFromSelectionPointerAction,
@"Extend Selection", kExtendSelectionPointerAction,
@"Open URL/Semantic History", kOpenTargetPointerAction,
@"Open URL in background", kOpenTargetInBackgroundPointerAction,
@"Smart Selection", kSmartSelectionPointerAction,
@"Smart Selection Ignoring Newlines", kSmartSelectionIgnoringNewlinesPointerAction,
@"Open Context Menu", kContextMenuPointerAction,
@"Next Tab", kNextTabPointerAction,
@"Previous Tab", kPrevTabPointerAction,
@"Next Window", kNextWindowPointerAction,
@"Previous Window", kPrevWindowPointerAction,
@"Move Pane", kMovePanePointerAction,
@"Send Escape Sequence…", kSendEscapeSequencePointerAction,
@"Send Hex Code…", kSendHexCodePointerAction,
@"Send Text…", kSendTextPointerAction,
@"Select Pane Left", kSelectPaneLeftPointerAction,
@"Select Pane Right", kSelectPaneRightPointerAction,
@"Select Pane Above", kSelectPaneAbovePointerAction,
@"Select Pane Below", kSelectPaneBelowPointerAction,
@"New Window With Profile…", kNewWindowWithProfilePointerAction,
@"New Tab With Profile…", kNewWindowWithProfilePointerAction,
@"New Tab With Profile…", kNewTabWithProfilePointerAction,
@"New Vertical Split With Profile…", kNewVerticalSplitWithProfilePointerAction,
@"New Horizontal Split With Profile…", kNewHorizontalSplitWithProfilePointerAction,
@"Select Next Pane", kSelectNextPanePointerAction,
@"Select Previous Pane", kSelectPreviousPanePointerAction,
nil];
return names;
}
+ (ArgumentType)argumentTypeForAction:(NSString *)action
{
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kEscPlusArg], kSendEscapeSequencePointerAction,
[NSNumber numberWithInt:kHexCodeArg], kSendHexCodePointerAction,
[NSNumber numberWithInt:kTextArg], kSendTextPointerAction,
[NSNumber numberWithInt:kProfileArg], kNewWindowWithProfilePointerAction,
[NSNumber numberWithInt:kProfileArg], kNewTabWithProfilePointerAction,
[NSNumber numberWithInt:kProfileArg], kNewVerticalSplitWithProfilePointerAction,
[NSNumber numberWithInt:kProfileArg], kNewHorizontalSplitWithProfilePointerAction,
nil];
NSNumber *n = [args objectForKey:action];
if (n) {
return (ArgumentType) [n intValue];
} else {
return (ArgumentType) kNoArg;
}
}
+ (NSString *)localizedActionForDict:(NSDictionary *)dict
{
NSDictionary *names = [PointerPrefsController localizedActionMap];
NSString *action = [dict objectForKey:kActionKey];
NSString *name = nil;
if (action) {
name = [names objectForKey:action];
}
if (!name) {
name = @"(Unknown)";
}
return name;
}
+ (NSString *)formattedLocalizedActionForDict:(NSDictionary *)dict
{
NSDictionary *names = [PointerPrefsController localizedActionMap];
NSString *action = [dict objectForKey:kActionKey];
NSString *argument = [dict objectForKey:kArgumentKey];
NSString *name = nil;
if (action) {
name = [names objectForKey:action];
}
if (!name) {
name = @"(Unknown)";
}
if (action) {
switch ([PointerPrefsController argumentTypeForAction:action]) {
case kNoArg:
break;
case kEscPlusArg:
return [name stringByReplacingOccurrencesOfString:@"…"
withString:[NSString stringWithFormat:@" Esc + %@", argument]];
case kHexCodeArg:
case kTextArg:
return [name stringByReplacingOccurrencesOfString:@"…"
withString:[NSString stringWithFormat:@" \"%@\"", argument]];
case kProfileArg: {
NSString *bookmarkName = [[[ProfileModel sharedInstance] bookmarkWithGuid:argument] objectForKey:KEY_NAME];
if (!bookmarkName) {
bookmarkName = @"?";
}
return [name stringByReplacingOccurrencesOfString:@"…"
withString:[NSString stringWithFormat:@" \"%@\"", bookmarkName]];
}
}
}
return name;
}
+ (NSString *)localizedActionForKey:(NSString *)key
{
NSDictionary *dict = [[PointerPrefsController settings] objectForKey:key];
return [PointerPrefsController localizedActionForDict:dict];
}
+ (NSString *)gestureIdentifierForKey:(NSString *)key
{
NSArray *parts = [PointerPrefsController gestureKeyComponents:key];
if (parts) {
return [parts objectAtIndex:1];
} else {
return nil;
}
}
+ (int)modifiersForKey:(NSString *)key
{
NSArray *parts;
int i;
if ([PointerPrefsController keyIsButton:key]) {
parts = [PointerPrefsController buttonKeyComponents:key];
i = 3;
} else {
parts = [PointerPrefsController gestureKeyComponents:key];
i = 2;
}
if (parts) {
return [PointerPrefsController maskForModChars:[parts objectAtIndex:i]];
} else {
return 0;
}
}
+ (NSDictionary *)defaultSettings
{
NSString* plistFile = [[NSBundle bundleForClass:[self class]] pathForResource:@"DefaultPointerActions"
ofType:@"plist"];
return [NSDictionary dictionaryWithContentsOfFile:plistFile];
}
+ (NSDictionary *)defaultActions
{
static NSDictionary *defaultDict;
if (!defaultDict) {
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:[PointerPrefsController defaultSettings]];
// Migrate old global prefs into the dict.
if (![[PreferencePanel sharedInstance] legacyPasteFromClipboard]) {
NSDictionary *middleButtonPastesFromSelection = [PointerPrefsController dictForAction:kPasteFromSelectionPointerAction];
[temp setObject:middleButtonPastesFromSelection
forKey:[PointerPrefsController keyForButton:kMiddleButton
clicks:1
modifiers:0]];
}
if ([[PreferencePanel sharedInstance] legacyThreeFingerEmulatesMiddle]) {
// Find all actions that use middle button and add corresponding three-finger gesture.
NSMutableDictionary *tempCopy = [[temp mutableCopy] autorelease];
for (NSString *key in temp) {
if ([PointerPrefsController keyIsButton:key] &&
[PointerPrefsController buttonForKey:key] == kMiddleButton) {
NSDictionary *middleAction = [temp objectForKey:key];
NSString *gestureKey = [PointerPrefsController keyForGesture:kThreeFingerClickGesture
modifiers:[PointerPrefsController modifiersForKey:key]];
[tempCopy setObject:middleAction forKey:gestureKey];
}
}
temp = tempCopy;
int modMasks[] = { NSCommandKeyMask, NSAlternateKeyMask, NSControlKeyMask, NSShiftKeyMask };
int numModCombos = 1 << (sizeof(modMasks) / sizeof(int));
for (int numClicks = 0; numClicks <= kMaxClicks; numClicks++) {
// i is a bitmask over the modMasks array indices.
for (int i = 0; i < numModCombos; i++) {
int modifiers = 0;
// Set modifiers to the OR of the NS...KeyMask values given the bits in i.
for (int j = 0; j < sizeof(modMasks)/sizeof(int); j++) {
if (i & (1 << j)) {
// The j'th bit is set in i, so OR in the j'th modifier mask.
modifiers |= modMasks[j];
}
}
NSString *key = [PointerPrefsController keyForButton:kMiddleButton
clicks:numClicks
modifiers:modifiers];
NSDictionary *middleAction = [temp objectForKey:key];
if (middleAction) {
[temp setObject:middleAction forKey:key];
}
}
}
}
defaultDict = [temp retain];
}
return defaultDict;
}
+ (NSDictionary *)settings
{
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kPointerActionsKey];
if (!dict) {
dict = [PointerPrefsController defaultActions];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:kPointerActionsKey];
}
return dict;
}
+ (void)setSettings:(NSDictionary *)newSettings
{
[[NSUserDefaults standardUserDefaults] setObject:newSettings forKey:kPointerActionsKey];
[[NSNotificationCenter defaultCenter] postNotificationName:kPointerPrefsChangedNotification
object:nil];
}
+ (NSArray *)sortedKeys
{
// NSArray *keys = [[PointerPrefsController settings] keysSortedByValueUsingSelector:@selector(comparePointerPrefsValue:)];
NSArray *keys = [[[PointerPrefsController settings] allKeys] sortedArrayUsingSelector:@selector(comparePointerActions:)];
return keys;
}
- (void)awakeFromNib
{
[tableView_ setDoubleAction:@selector(tableViewRowDoubleClicked:)];
[tableView_ setTarget:self];
NSArray *actions = [[[PointerPrefsController localizedActionMap] allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[editAction_ addItemsWithTitles:actions];
}
+ (NSString *)keyForRowIndex:(int)rowIndex
{
NSArray *sortedKeys = [PointerPrefsController sortedKeys];
NSString *key = [sortedKeys objectAtIndex:rowIndex];
return key;
}
+ (NSString *)argumentWithButton:(int)buttonNumber
numClicks:(int)numClicks
modifiers:(int)modMask
{
NSString *key = [PointerPrefsController keyForButton:buttonNumber
clicks:numClicks
modifiers:modMask];
return [[[PointerPrefsController settings] objectForKey:key] objectForKey:kArgumentKey];
}
+ (NSString *)actionWithButton:(int)buttonNumber
numClicks:(int)numClicks
modifiers:(int)modMask
{
NSString *key = [PointerPrefsController keyForButton:buttonNumber
clicks:numClicks
modifiers:modMask];
NSString *action = [[[PointerPrefsController settings] objectForKey:key] objectForKey:kActionKey];
return action;
}
+ (NSString *)actionForTapWithTouches:(int)numTouches
modifiers:(int)modMask
{
NSString *gesture = @"";
if (numTouches == 3) {
gesture = kThreeFingerClickGesture;
} else {
return nil;
}
return [PointerPrefsController actionForGesture:gesture modifiers:modMask];
}
+ (NSString *)argumentForTapWithTouches:(int)numTouches
modifiers:(int)modMask
{
NSString *gesture = @"";
if (numTouches == 3) {
gesture = kThreeFingerClickGesture;
} else {
return nil;
}
return [PointerPrefsController argumentForGesture:gesture modifiers:modMask];
}
+ (NSString *)actionForGesture:(NSString *)gesture
modifiers:(int)modMask
{
NSString *key;
key = [PointerPrefsController keyForGesture:gesture
modifiers:modMask];
return [[[PointerPrefsController settings] objectForKey:key] objectForKey:kActionKey];
}
+ (NSString *)argumentForGesture:(NSString *)gesture
modifiers:(int)modMask
{
NSString *key;
key = [PointerPrefsController keyForGesture:gesture
modifiers:modMask];
return [[[PointerPrefsController settings] objectForKey:key] objectForKey:kArgumentKey];
}
+ (BOOL)haveThreeFingerTapEvents
{
for (NSString *key in [PointerPrefsController sortedKeys]) {
if ([PointerPrefsController keyIsThreeFingerTap:key]) {
return YES;
}
}
return NO;
}
#pragma mark NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [[PointerPrefsController settings] count];
}
+ (NSString *)localizedButton:(int)buttonNumber numClicks:(int)clicks modifiers:(int)modFlags
{
NSString *button = [PointerPrefsController localizedButtonNameForButtonNumber:buttonNumber];
NSString *numClicks = [PointerPrefsController localizedNumClicks:clicks];
NSString *modifiers = [PointerPrefsController localizedModifers:modFlags];
if ([modifiers length]) {
modifiers = [modifiers stringByAppendingString:@" + "];
}
return [NSString stringWithFormat:@"%@%@ %@", modifiers, button, numClicks];
}
+ (NSString *)localizedButtonKey:(NSString *)key
{
return [PointerPrefsController localizedButton:[PointerPrefsController buttonForKey:key]
numClicks:[PointerPrefsController numClicksForKey:key]
modifiers:[PointerPrefsController modifiersForKey:key]];
}
- (void)setModifierButtons:(int)modMask
{
[editModifiersCommand_ setState:(modMask & NSCommandKeyMask) ? NSOnState : NSOffState];
[editModifiersOption_ setState:(modMask & NSAlternateKeyMask) ? NSOnState : NSOffState];
[editModifiersShift_ setState:(modMask & NSShiftKeyMask) ? NSOnState : NSOffState];
[editModifiersControl_ setState:(modMask & NSControlKeyMask) ? NSOnState : NSOffState];
}
- (void)setButtonNumber:(int)buttonNumber clickCount:(int)clickCount modifiers:(int)modMask
{
if (buttonNumber >= 1 && clickCount > 0 && clickCount < 5) {
[editButton_ selectItemWithTag:buttonNumber];
[editClickType_ selectItemWithTag:clickCount];
[self setModifierButtons:modMask];
[self buttonOrGestureChanged:nil];
}
}
- (void)setGesture:(NSString *)gesture modifiers:(int)modMask
{
[editButton_ selectItemWithTag:[PointerPrefsController tagForGestureIdentifier:gesture]];
[self setModifierButtons:modMask];
[self buttonOrGestureChanged:nil];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex {
NSString *key = [PointerPrefsController keyForRowIndex:rowIndex];
NSDictionary *action = [[PointerPrefsController settings] objectForKey:key];
BOOL isButton = [PointerPrefsController keyIsButton:key];
if (aTableColumn == buttonColumn_) {
if (isButton) {
return [PointerPrefsController localizedButtonKey:key];
} else {
NSString *modifiers = [PointerPrefsController localizedModifers:[PointerPrefsController modifiersForKey:key]];
if ([modifiers length]) {
modifiers = [modifiers stringByAppendingString:@" + "];
}
NSString *gesture = [PointerPrefsController localizedGestureNameForGestureIdentifier:[PointerPrefsController gestureIdentifierForKey:key]];
return [NSString stringWithFormat:@"%@%@", modifiers, gesture];
}
} else {
// Action
return [PointerPrefsController formattedLocalizedActionForDict:action];
}
}
- (BOOL)okShouldBeEnabled
{
if (![editButton_ selectedItem]) {
return NO;
}
if (![editAction_ selectedItem]) {
return NO;
}
if ([editButton_ selectedTag] >= kMinGestureTag) {
// Gesture
return YES;
} else {
// Button
return [editClickType_ selectedItem] != nil;
}
}
+ (NSString *)actionForKey:(NSString *)key
{
return [[[PointerPrefsController settings] objectForKey:key] objectForKey:kActionKey];
}
+ (NSString *)argumentForKey:(NSString *)key
{
return [[[PointerPrefsController settings] objectForKey:key] objectForKey:kArgumentKey];
}
- (void)updateArgumentFieldsForAction:(NSString *)actionIdent argument:(NSString *)currentArg
{
ArgumentType argType = kNoArg;
if (actionIdent) {
argType = [PointerPrefsController argumentTypeForAction:actionIdent];
}
switch (argType) {
case kNoArg:
[editArgumentLabel_ setHidden:YES];
[editArgumentField_ setHidden:YES];
[editArgumentButton_ setHidden:YES];
break;
case kEscPlusArg:
[editArgumentLabel_ setHidden:NO];
[editArgumentField_ setHidden:NO];
[editArgumentField_ setEnabled:YES];
[editArgumentButton_ setHidden:YES];
[editArgumentLabel_ setStringValue:@"Esc +"];
[[editArgumentField_ cell] setPlaceholderString:@"characters to send"];
[editArgumentField_ setStringValue:currentArg];
[editArgumentField_ setRefusesFirstResponder:NO];
[editArgumentField_ setSelectable:YES];
break;
case kHexCodeArg:
[editArgumentLabel_ setHidden:NO];
[editArgumentField_ setHidden:NO];
[editArgumentField_ setEnabled:YES];
[editArgumentButton_ setHidden:YES];
[editArgumentLabel_ setStringValue:@"Hex codes:"];
[[editArgumentField_ cell] setPlaceholderString:@"ex: 0x7f 0x20"];
[editArgumentField_ setStringValue:currentArg];
break;
case kTextArg:
[editArgumentLabel_ setHidden:NO];
[editArgumentField_ setHidden:NO];
[editArgumentField_ setEnabled:YES];
[editArgumentButton_ setHidden:YES];
[editArgumentLabel_ setStringValue:@"Text:"];
[[editArgumentField_ cell] setPlaceholderString:@"Enter value to send"];
[editArgumentField_ setStringValue:currentArg];
break;
case kProfileArg:
[editArgumentLabel_ setHidden:NO];
[editArgumentField_ setHidden:YES];
[editArgumentButton_ setHidden:NO];
[editArgumentLabel_ setStringValue:@"Profile:"];
[PreferencePanel populatePopUpButtonWithBookmarks:editArgumentButton_
selectedGuid:currentArg];
break;
}
}
- (void)loadKeyIntoEditPane:(NSString *)key
{
int modMask;
if (key) {
modMask = [PointerPrefsController modifiersForKey:key];
} else {
modMask = 0;
}
NSString *localizedAction = @"";
if (key) {
localizedAction = [PointerPrefsController localizedActionForKey:key];
}
NSString *actionIdent = [PointerPrefsController actionForKey:key];
NSString *currentArg = [PointerPrefsController argumentForKey:key];
[self updateArgumentFieldsForAction:actionIdent argument:currentArg];
[editModifiersCommand_ setState:(modMask & NSCommandKeyMask) ? NSOnState : NSOffState];
[editModifiersOption_ setState:(modMask & NSAlternateKeyMask) ? NSOnState : NSOffState];
[editModifiersShift_ setState:(modMask & NSShiftKeyMask) ? NSOnState : NSOffState];
[editModifiersControl_ setState:(modMask & NSControlKeyMask) ? NSOnState : NSOffState];
[editAction_ selectItemWithTitle:localizedAction];
BOOL isButton = !key || [PointerPrefsController keyIsButton:key];
if (isButton) {
int button = key ? [PointerPrefsController buttonForKey:key] : 2;
int numClicks = key ? [PointerPrefsController numClicksForKey:key] : 1;
[editButton_ selectItemWithTag:button];
[editClickType_ selectItemWithTag:numClicks];
} else {
NSString *gestureIdent = [PointerPrefsController gestureIdentifierForKey:key];
[editButton_ selectItemWithTag:[PointerPrefsController tagForGestureIdentifier:gestureIdent]];
[editClickType_ selectItem:nil];
}
[origKey_ autorelease];
origKey_ = [key retain];
[self buttonOrGestureChanged:nil];
[ok_ setEnabled:[self okShouldBeEnabled]];
}
- (IBAction)buttonOrGestureChanged:(id)sender
{
if ([editButton_ selectedTag] >= kMinGestureTag) {
[editClickTypeLabel_ setTextColor:[NSColor disabledControlTextColor]];
[editClickType_ setEnabled:NO];
} else {
[editClickTypeLabel_ setTextColor:[NSColor blackColor]];
[editClickType_ setEnabled:YES];
}
}
#pragma mark NSTableViewDelegate
- (BOOL)tableView:(NSTableView *)aTableView
shouldEditTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
return NO;
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
self.hasSelection = [tableView_ numberOfSelectedRows] > 0;
int rowIndex = [tableView_ selectedRow];
NSColor *textColor = [NSColor disabledControlTextColor];
if (self.hasSelection) {
textColor = [NSColor blackColor];
NSString *key = [PointerPrefsController keyForRowIndex:rowIndex];
NSDictionary *action = [[PointerPrefsController settings] objectForKey:key];
[editButton_ selectItemWithTag:[PointerPrefsController buttonForKey:key]];
[editAction_ selectItemWithTitle:[PointerPrefsController localizedActionForDict:action]];
int modflags = [PointerPrefsController modifiersForKey:key];
[editModifiersCommand_ setState:(modflags & NSCommandKeyMask) ? NSOnState : NSOffState];
[editModifiersOption_ setState:(modflags & NSAlternateKeyMask) ? NSOnState : NSOffState];
[editModifiersShift_ setState:(modflags & NSShiftKeyMask) ? NSOnState : NSOffState];
[editModifiersControl_ setState:(modflags & NSControlKeyMask) ? NSOnState : NSOffState];
}
[editButtonLabel_ setTextColor:textColor];
[editModifiersLabel_ setTextColor:textColor];
[editActionLabel_ setTextColor:textColor];
}
- (void)tableViewRowDoubleClicked:(id)sender
{
if ([tableView_ selectedRow] >= 0) {
NSString *key = [PointerPrefsController keyForRowIndex:[tableView_ selectedRow]];
[self editKey:key];
}
}
- (void)editKey:(NSString *)key
{
[self loadKeyIntoEditPane:key];
[NSApp beginSheet:panel_
modalForWindow:[[PreferencePanel sharedInstance] window]
modalDelegate:self
didEndSelector:@selector(genericCloseSheet:returnCode:contextInfo:)
contextInfo:key];
}
- (void)genericCloseSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
[sheet close];
}
- (IBAction)ok:(id)sender
{
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:[PointerPrefsController settings]];
if (origKey_) {
[temp removeObjectForKey:origKey_];
}
NSString *theAction = [PointerPrefsController actionWithLocalizedName:[[editAction_ selectedItem] title]];
NSMutableDictionary *newValue = [NSMutableDictionary dictionaryWithObject:theAction
forKey:kActionKey];
if (![editArgumentField_ isHidden]) {
[newValue setObject:[editArgumentField_ stringValue]
forKey:kArgumentKey];
} else if (![editArgumentButton_ isHidden]) {
if ([PointerPrefsController argumentTypeForAction:theAction] == kProfileArg) {
NSString *profileName = [[editArgumentButton_ selectedItem] title];
NSString *guid = [[[ProfileModel sharedInstance] bookmarkWithName:profileName] objectForKey:KEY_GUID];
if (guid) {
[newValue setObject:guid forKey:kArgumentKey];
} else {
[newValue setObject:@"" forKey:kArgumentKey];
}
} else {
[newValue setObject:[[editArgumentButton_ selectedItem] title]
forKey:kArgumentKey];
}
}
NSString *newKey;
int modMask = 0;
if ([editModifiersCommand_ state] == NSOnState) {
modMask |= NSCommandKeyMask;
}
if ([editModifiersOption_ state] == NSOnState) {
modMask |= NSAlternateKeyMask;
}
if ([editModifiersShift_ state] == NSOnState) {
modMask |= NSShiftKeyMask;
}
if ([editModifiersControl_ state] == NSOnState) {
modMask |= NSControlKeyMask;
}
if ([editButton_ selectedTag] >= kMinGestureTag) {
// Gesture
newKey = [PointerPrefsController keyForGesture:[PointerPrefsController gestureIdentifierForTag:[editButton_ selectedTag]]
modifiers:modMask];
} else {
// Button
newKey = [PointerPrefsController keyForButton:[editButton_ selectedTag]
clicks:[editClickType_ selectedTag]
modifiers:modMask];
}
[temp setObject:newValue forKey:newKey];
[PointerPrefsController setSettings:temp];