Fixed a crash when Core Image was available.
[ITKit.git] / ITKeyCombo.m
1 #import "ITKeyCombo.h"
2 #import <Carbon/Carbon.h>
3
4 @implementation ITKeyCombo
5
6 + (id)clearKeyCombo {
7         return [self keyComboWithKeyCode:-1 modifiers:-1];
8 }
9
10 + (id)keyComboWithKeyCode:(int)keyCode modifiers:(int)modifiers {
11         return [[[self alloc] initWithKeyCode:keyCode modifiers:modifiers] autorelease];
12 }
13
14 + (id)keyComboWithPlistRepresentation:(id)plist {
15         return [[[self alloc] initWithPlistRepresentation:plist] autorelease];
16 }
17
18 - (id)initWithKeyCode:(int)keyCode modifiers:(int)modifiers {
19         if ((self = [super init])) {
20                 mKeyCode = keyCode;
21                 mModifiers = modifiers;
22         }
23         return self;
24 }
25
26 - (id)initWithPlistRepresentation:(id)plist {
27         int keyCode, modifiers;
28         
29         keyCode = [[plist objectForKey:@"keyCode"] intValue];
30         if (keyCode <= 0) {
31                 keyCode = -1;
32         }
33         
34         modifiers = [[plist objectForKey:@"modifiers"] intValue];
35         if (modifiers <= 0) {
36                 modifiers = -1;
37         }
38         
39         return [self initWithKeyCode:keyCode modifiers:modifiers];
40 }
41
42 - (id)plistRepresentation {
43         return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[self keyCode]], @"keyCode", [NSNumber numberWithInt:[self modifiers]], @"modifiers", nil];
44 }
45
46 - (id)copyWithZone:(NSZone *)zone {
47         return [self retain];
48 }
49
50 - (BOOL)isEqual:(ITKeyCombo *)combo {
51         return (([self keyCode] == [combo keyCode]) && ([self modifiers] == [combo modifiers]));
52 }
53
54 - (int)keyCode {
55         return mKeyCode;
56 }
57
58 - (int)modifiers {
59         return mModifiers;
60 }
61
62 - (BOOL)isValidHotKeyCombo {
63         return ((mKeyCode >= 0) && (mModifiers > 0));
64 }
65
66 - (BOOL)isClearCombo {
67         return ((mKeyCode == -1) && (mModifiers == -1));
68 }
69
70 @end
71
72 @implementation ITKeyCombo (UserDisplayAdditions)
73
74 + (NSDictionary *)_keyCodesDictionary {
75         static NSDictionary *keyCodes = nil;
76         
77         if (!keyCodes) {
78                 NSString *path;
79                 NSString *contents;
80                 
81                 path = [[NSBundle bundleForClass:self] pathForResource:@"ITKeyCodes" ofType:@"plist"];
82                 contents = [NSString stringWithContentsOfFile:path];
83                 keyCodes = [[contents propertyList] retain];
84         }
85         
86         return keyCodes;
87 }
88
89 + (NSString *)_stringForModifiers:(long)modifiers {
90         static long modToChar[4][2] = {
91                 { cmdKey, 0x23180000 },
92                 { optionKey, 0x23250000 },
93                 { controlKey, 0x23030000 },
94                 { shiftKey, 0x21e70000 }
95         };
96         
97         NSString *str = @"";
98         NSString *charStr;
99         long i;
100         
101         for (i = 0;i < 4;i++) {
102                 if (modifiers & modToChar[i][0]) {
103                         charStr = [NSString stringWithCharacters:(const unichar *)&modToChar[i][1] length:1];
104                         str = [str stringByAppendingString:charStr];
105                 }
106         }
107         
108         return str;
109 }
110
111 + (NSString *)_stringForKeyCode:(short)keyCode {
112         NSDictionary *dict = [self _keyCodesDictionary];
113         NSString *key = [NSString stringWithFormat:@"%d", keyCode];
114         NSString *str = [dict objectForKey:key];
115         
116         if (!str) {
117                 str = [NSString stringWithFormat:@"%X", keyCode];
118         }
119         
120         return str;
121 }
122
123 - (NSString *)description {
124         if ([self isValidHotKeyCombo]) { //This might have to change
125                 return [NSString stringWithFormat:@"%@%@", [[self class] _stringForModifiers:[self modifiers]], [[self class] _stringForKeyCode:[self keyCode]]];
126         }
127         
128         return NSLocalizedString(@"(None)", @"Hot Keys: Key Combo text for 'empty' combo");
129 }
130
131 @end