-
Notifications
You must be signed in to change notification settings - Fork 2
/
SRValidator.m
265 lines (211 loc) · 11.1 KB
/
SRValidator.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
//
// SRValidator.h
// ShortcutRecorder
//
// Copyright 2006-2007 Contributors. All rights reserved.
//
// License: BSD
//
// Contributors:
// David Dauer
// Jesper
// Jamie Kirkpatrick
#import "SRValidator.h"
#import "SRCommon.h"
@implementation SRValidator
//----------------------------------------------------------
// iinitWithDelegate:
//----------------------------------------------------------
- (id)initWithDelegate:(id)theDelegate;
{
self = [super init];
if (!self) return nil;
[self setDelegate:theDelegate];
return self;
}
//----------------------------------------------------------
// isKeyCode:andFlagsTaken:error:
//----------------------------------------------------------
- (BOOL)isKeyCode:(signed short)keyCode andFlagsTaken:(unsigned int)flags error:(NSError **)error;
{
// if we have a delegate, it goes first...
if (delegate) {
NSString *delegateReason = nil;
if ([delegate shortcutValidator:self
isKeyCode:keyCode
andFlagsTaken:SRCarbonToCocoaFlags(flags)
reason:&delegateReason]) {
if (error) {
NSString *description = [NSString stringWithFormat:
SRLoc(@"The key combination %@ can't be used!"),
SRStringForCarbonModifierFlagsAndKeyCode(flags, keyCode)];
NSString *recoverySuggestion = [NSString stringWithFormat:
SRLoc(@"The key combination \"%@\" can't be used because %@."),
SRReadableStringForCarbonModifierFlagsAndKeyCode(flags, keyCode),
(delegateReason && [delegateReason length]) ?
delegateReason:@"it's already used"];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description,
@"NSLocalizedDescriptionKey",
recoverySuggestion,
@"NSLocalizedRecoverySuggestionErrorKey",
[NSArray arrayWithObject:@"OK"],
@"NSLocalizedRecoveryOptionsErrorKey",
nil];
*error = [NSError errorWithDomain:@"NSCocoaErrorDomain" code:0 userInfo:userInfo];
}
return YES;
}
}
// then our implementation...
NSArray *globalHotKeys;
// get global hot keys...
if (CopySymbolicHotKeys((CFArrayRef *)&globalHotKeys) != noErr) return YES;
NSEnumerator *globalHotKeysEnumerator = [globalHotKeys objectEnumerator];
NSDictionary *globalHotKeyInfoDictionary;
SInt32 gobalHotKeyFlags;
signed short globalHotKeyCharCode;
unichar globalHotKeyUniChar;
unichar localHotKeyUniChar;
BOOL globalCommandMod = NO, globalOptionMod = NO, globalShiftMod = NO, globalCtrlMod = NO;
BOOL localCommandMod = NO, localOptionMod = NO, localShiftMod = NO, localCtrlMod = NO;
// Prepare local carbon comparison flags
if (flags & cmdKey) localCommandMod = YES;
if (flags & optionKey) localOptionMod = YES;
if (flags & shiftKey) localShiftMod = YES;
if (flags & controlKey) localCtrlMod = YES;
while ((globalHotKeyInfoDictionary = [globalHotKeysEnumerator nextObject])) {
// Only check if global hotkey is enabled
if ( (CFBooleanRef)[globalHotKeyInfoDictionary objectForKey : (NSString *)kHISymbolicHotKeyEnabled] != kCFBooleanTrue) continue;
globalCommandMod = NO;
globalOptionMod = NO;
globalShiftMod = NO;
globalCtrlMod = NO;
globalHotKeyCharCode = [(NSNumber *)[globalHotKeyInfoDictionary objectForKey:(NSString *)kHISymbolicHotKeyCode] unsignedShortValue];
globalHotKeyUniChar = [[[NSString stringWithFormat:@"%C", globalHotKeyCharCode] uppercaseString] characterAtIndex:0];
CFNumberGetValue((CFNumberRef)[globalHotKeyInfoDictionary objectForKey: (NSString *)kHISymbolicHotKeyModifiers], kCFNumberSInt32Type, &gobalHotKeyFlags);
if (gobalHotKeyFlags & cmdKey) globalCommandMod = YES;
if (gobalHotKeyFlags & optionKey) globalOptionMod = YES;
if (gobalHotKeyFlags & shiftKey) globalShiftMod = YES;
if (gobalHotKeyFlags & controlKey) globalCtrlMod = YES;
NSString *localKeyString = SRStringForKeyCode(keyCode);
if (![localKeyString length]) return YES;
localHotKeyUniChar = [localKeyString characterAtIndex:0];
// compare unichar value and modifier flags
if ( (globalHotKeyUniChar == localHotKeyUniChar)
&& (globalCommandMod == localCommandMod)
&& (globalOptionMod == localOptionMod)
&& (globalShiftMod == localShiftMod)
&& (globalCtrlMod == localCtrlMod) ) {
if (error) {
NSString *description = [NSString stringWithFormat:
SRLoc(@"The key combination %@ can't be used!"),
SRStringForCarbonModifierFlagsAndKeyCode(flags, keyCode)];
NSString *recoverySuggestion = [NSString stringWithFormat:
SRLoc(@"The key combination \"%@\" can't be used because it's already used by a system-wide keyboard shortcut. (If you really want to use this key combination, most shortcuts can be changed in the Keyboard & Mouse panel in System Preferences.)"),
SRReadableStringForCarbonModifierFlagsAndKeyCode(flags, keyCode)];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description,
@"NSLocalizedDescriptionKey",
recoverySuggestion,
@"NSLocalizedRecoverySuggestionErrorKey",
[NSArray arrayWithObject:@"OK"],
@"NSLocalizedRecoveryOptionsErrorKey",
nil];
*error = [NSError errorWithDomain:@"NSCocoaErrorDomain" code:0 userInfo:userInfo];
}
return YES;
}
}
// Check menus too
return [self isKeyCode:keyCode andFlags:flags takenInMenu:[NSApp mainMenu] error:error];
}
//----------------------------------------------------------
// isKeyCode:andFlags:takenInMenu:error:
//----------------------------------------------------------
- (BOOL)isKeyCode:(signed short)keyCode andFlags:(unsigned int)flags takenInMenu:(NSMenu *)menu error:(NSError **)error;
{
NSArray *menuItemsArray = [menu itemArray];
NSEnumerator *menuItemsEnumerator = [menuItemsArray objectEnumerator];
NSMenuItem *menuItem;
unsigned int menuItemModifierFlags;
NSString *menuItemKeyEquivalent;
BOOL menuItemCommandMod = NO, menuItemOptionMod = NO, menuItemShiftMod = NO, menuItemCtrlMod = NO;
BOOL localCommandMod = NO, localOptionMod = NO, localShiftMod = NO, localCtrlMod = NO;
// Prepare local carbon comparison flags
if (flags & cmdKey) localCommandMod = YES;
if (flags & optionKey) localOptionMod = YES;
if (flags & shiftKey) localShiftMod = YES;
if (flags & controlKey) localCtrlMod = YES;
while ((menuItem = [menuItemsEnumerator nextObject])) {
// rescurse into all submenus...
if ([menuItem hasSubmenu]) {
if ([self isKeyCode:keyCode andFlags:flags takenInMenu:[menuItem submenu] error:error]) {
return YES;
}
}
if ( (menuItemKeyEquivalent = [menuItem keyEquivalent])
&& (![menuItemKeyEquivalent isEqualToString:@""]) ) {
menuItemCommandMod = NO;
menuItemOptionMod = NO;
menuItemShiftMod = NO;
menuItemCtrlMod = NO;
menuItemModifierFlags = [menuItem keyEquivalentModifierMask];
if (menuItemModifierFlags & NSCommandKeyMask) menuItemCommandMod = YES;
if (menuItemModifierFlags & NSAlternateKeyMask) menuItemOptionMod = YES;
if (menuItemModifierFlags & NSShiftKeyMask) menuItemShiftMod = YES;
if (menuItemModifierFlags & NSControlKeyMask) menuItemCtrlMod = YES;
NSString *localKeyString = SRStringForKeyCode(keyCode);
// Compare translated keyCode and modifier flags
if ( ([[menuItemKeyEquivalent uppercaseString] isEqualToString:localKeyString])
&& (menuItemCommandMod == localCommandMod)
&& (menuItemOptionMod == localOptionMod)
&& (menuItemShiftMod == localShiftMod)
&& (menuItemCtrlMod == localCtrlMod) ) {
if (error) {
NSString *description = [NSString stringWithFormat:
SRLoc(@"The key combination %@ can't be used!"),
SRStringForCarbonModifierFlagsAndKeyCode(flags, keyCode)];
NSString *recoverySuggestion = [NSString stringWithFormat:
SRLoc(@"The key combination \"%@\" can't be used because it's already used by the menu item \"%@\"."),
SRReadableStringForCocoaModifierFlagsAndKeyCode(menuItemModifierFlags, keyCode),
[menuItem title]];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description,
@"NSLocalizedDescriptionKey",
recoverySuggestion,
@"NSLocalizedRecoverySuggestionErrorKey",
[NSArray arrayWithObject:@"OK"],
@"NSLocalizedRecoveryOptionsErrorKey",
nil];
*error = [NSError errorWithDomain:@"NSCocoaErrorDomain" code:0 userInfo:userInfo];
}
return YES;
}
}
}
return NO;
}
#pragma mark -
#pragma mark accessors
//----------------------------------------------------------
// delegate
//----------------------------------------------------------
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)theDelegate {
delegate = [theDelegate retain];
}
@end
#pragma mark -
#pragma mark default delegate implementation
@implementation NSObject (SRValidation)
//----------------------------------------------------------
// shortcutValidator:isKeyCode:andFlagsTaken:reason:
//----------------------------------------------------------
- (BOOL)shortcutValidator:(SRValidator *)validator isKeyCode:(signed short)keyCode andFlagsTaken:(unsigned int)flags reason:(NSString **)aReason;
{
return NO;
}
@end