Skip to content
Newer
Older
100644 246 lines (192 sloc) 5.01 KB
f1ecb58 @jessegrosjean new version of quick cursor
jessegrosjean authored Sep 3, 2009
1 //
2 // PTKeyCombo.m
3 // Protein
4 //
5 // Created by Quentin Carnicelli on Sat Aug 02 2003.
6 // Copyright (c) 2003 Quentin D. Carnicelli. All rights reserved.
7 //
8
9 #import "PTKeyCombo.h"
10 #import "PTKeyCodeTranslator.h"
11
12 @implementation PTKeyCombo
13
14 + (id)clearKeyCombo
15 {
16 return [self keyComboWithKeyCode: -1 modifiers: -1];
17 }
18
19 + (id)keyComboWithKeyCode: (NSInteger)keyCode modifiers: (NSUInteger)modifiers
20 {
21 return [[[self alloc] initWithKeyCode: keyCode modifiers: modifiers] autorelease];
22 }
23
24 - (id)initWithKeyCode: (NSInteger)keyCode modifiers: (NSUInteger)modifiers
25 {
26 self = [super init];
27
28 if( self )
29 {
30 mKeyCode = keyCode;
31 mModifiers = modifiers;
32 }
33
34 return self;
35 }
36
37 - (id)initWithPlistRepresentation: (id)plist
38 {
39 int keyCode, modifiers;
40
41 if( !plist || ![plist count] )
42 {
43 keyCode = -1;
44 modifiers = -1;
45 }
46 else
47 {
48 keyCode = [[plist objectForKey: @"keyCode"] intValue];
49 if( keyCode < 0 ) keyCode = -1;
50
51 modifiers = [[plist objectForKey: @"modifiers"] intValue];
52 if( modifiers <= 0 ) modifiers = -1;
53 }
54
55 return [self initWithKeyCode: keyCode modifiers: modifiers];
56 }
57
58 - (id)plistRepresentation
59 {
60 return [NSDictionary dictionaryWithObjectsAndKeys:
61 [NSNumber numberWithInteger: [self keyCode]], @"keyCode",
62 [NSNumber numberWithInteger: [self modifiers]], @"modifiers",
63 nil];
64 }
65
66 - (id)copyWithZone:(NSZone*)zone;
67 {
68 return [self retain];
69 }
70
71 - (BOOL)isEqual: (PTKeyCombo*)combo
72 {
73 return [self keyCode] == [combo keyCode] &&
74 [self modifiers] == [combo modifiers];
75 }
76
77 #pragma mark -
78
79 - (NSInteger)keyCode
80 {
81 return mKeyCode;
82 }
83
84 - (NSUInteger)modifiers
85 {
86 return mModifiers;
87 }
88
89 - (BOOL)isValidHotKeyCombo
90 {
91 return mKeyCode >= 0 && mModifiers > 0;
92 }
93
94 - (BOOL)isClearCombo
95 {
96 return mKeyCode == -1 && mModifiers == -1;
97 }
98
99 @end
100
101 #pragma mark -
102
103 @implementation PTKeyCombo (UserDisplayAdditions)
104
105 + (NSString*)_stringForModifiers: (long)modifiers
106 {
107 static unichar modToChar[4][2] =
108 {
109 { cmdKey, kCommandUnicode },
110 { optionKey, kOptionUnicode },
111 { controlKey, kControlUnicode },
112 { shiftKey, kShiftUnicode }
113 };
114
115 NSString* str = [NSString string];
116 long i;
117
118 for( i = 0; i < 4; i++ )
119 {
120 if( modifiers & modToChar[i][0] )
121 str = [str stringByAppendingString: [NSString stringWithCharacters: &modToChar[i][1] length: 1]];
122 }
123
124 return str;
125 }
126
127 + (NSDictionary*)_keyCodesDictionary
128 {
129 static NSDictionary* keyCodes = nil;
130
131 if( keyCodes == nil )
132 {
133 NSURL *url = [NSURL fileURLWithPath:[[NSBundle bundleForClass: self] pathForResource: @"PTKeyCodes" ofType: @"plist"]];
134 NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
135 keyCodes = [[contents propertyList] retain];
136 }
137
138 return keyCodes;
139 }
140
141 + (NSString*)_stringForKeyCode: (short)keyCode legacyKeyCodeMap: (NSDictionary*)dict
142 {
143 id key;
144 NSString* str;
145
146 key = [NSString stringWithFormat: @"%d", keyCode];
147 str = [dict objectForKey: key];
148
149 if( !str )
150 str = [NSString stringWithFormat: @"%X", keyCode];
151
152 return str;
153 }
154
155 + (NSString*)_stringForKeyCode: (short)keyCode newKeyCodeMap: (NSDictionary*)dict
156 {
157 NSString* result;
158 NSString* keyCodeStr;
159 NSDictionary* unmappedKeys;
160 NSArray* padKeys;
161
162 keyCodeStr = [NSString stringWithFormat: @"%d", keyCode];
163
164 //Handled if its not handled by translator
165 unmappedKeys = [dict objectForKey:@"unmappedKeys"];
166 result = [unmappedKeys objectForKey: keyCodeStr];
167 if( result )
168 return result;
169
170 //Translate it
171 result = [[[PTKeyCodeTranslator currentTranslator] translateKeyCode:keyCode] uppercaseString];
172
173 //Handle if its a key-pad key
174 padKeys = [dict objectForKey:@"padKeys"];
175 if( [padKeys indexOfObject: keyCodeStr] != NSNotFound )
176 {
177 result = [NSString stringWithFormat:@"%@ %@", [dict objectForKey:@"padKeyString"], result];
178 }
179
180 return result;
181 }
182
183 + (NSString*)_stringForKeyCode: (short)keyCode
184 {
185 NSDictionary* dict;
186
187 dict = [self _keyCodesDictionary];
188 if( [[dict objectForKey: @"version"] intValue] <= 0 )
189 return [self _stringForKeyCode: keyCode legacyKeyCodeMap: dict];
190
191 return [self _stringForKeyCode: keyCode newKeyCodeMap: dict];
192 }
193
194 - (NSString*)keyCodeString
195 {
196 // special case: the modifiers for the "clear" key are 0x0
197 if ( [self isClearCombo] ) return @"";
198
199 return [[self class] _stringForKeyCode:[self keyCode]];
200 }
201
202 - (NSUInteger)modifierMask
203 {
204 // special case: the modifiers for the "clear" key are 0x0
205 if ( [self isClearCombo] ) return 0;
206
207 static NSUInteger modToChar[4][2] =
208 {
209 { cmdKey, NSCommandKeyMask },
210 { optionKey, NSAlternateKeyMask },
211 { controlKey, NSControlKeyMask },
212 { shiftKey, NSShiftKeyMask }
213 };
214
215 NSUInteger i, ret = 0;
216
217 for ( i = 0; i < 4; i++ )
218 {
219 if ( [self modifiers] & modToChar[i][0] ) {
220 ret |= modToChar[i][1];
221 }
222 }
223
224 return ret;
225 }
226
227 - (NSString*)description
228 {
229 NSString* desc;
230
231 if( [self isValidHotKeyCombo] ) //This might have to change
232 {
233 desc = [NSString stringWithFormat: @"%@%@",
234 [[self class] _stringForModifiers: [self modifiers]],
235 [[self class] _stringForKeyCode: [self keyCode]]];
236 }
237 else
238 {
239 desc = NSLocalizedString( @"(None)", @"Hot Keys: Key Combo text for 'empty' combo" );
240 }
241
242 return desc;
243 }
244
245 @end
Something went wrong with that request. Please try again.