Added notes to the TODO. Matt, please check and modify TODO as required :)
[MenuTunes.git] / KeyCombo.m
1 //
2 //  KeyCombo.m
3 //
4 //  Created by Quentin D. Carnicelli on Tue Jun 18 2002.
5 //  Copyright (c) 2001 Subband inc.. All rights reserved.
6 //
7
8 #import "KeyCombo.h"
9
10 #import <AppKit/NSEvent.h>
11 #import <Carbon/Carbon.h>
12
13 @interface KeyCombo (Private)
14     + (NSString*)_stringForModifiers:(long)modifiers;
15     + (NSString*)_stringForKeyCode:(short)keyCode;
16 @end
17
18
19 @implementation KeyCombo
20
21 + (id)keyCombo
22 {
23     return [[[self alloc] init] autorelease];
24 }
25
26 + (id)clearKeyCombo
27 {
28     return [self keyComboWithKeyCode:-1 andModifiers:-1];
29 }
30
31 + (id)keyComboWithKeyCode: (short)keycode andModifiers: (long)modifiers
32 {
33     return [[[self alloc] initWithKeyCode:keycode andModifiers:modifiers] autorelease];
34 }
35
36 - (id)initWithKeyCode: (short)keycode andModifiers: (long)modifiers
37 {
38     if ( (self = [super init]) )
39     {
40         mKeyCode = keycode;
41         mModifiers = modifiers;
42     }
43     return self;
44 }
45
46 - (id)init
47 {
48     return [self initWithKeyCode: -1 andModifiers: -1];
49 }
50
51 - (id)copyWithZone:(NSZone *)zone;
52 {
53     return [self retain];
54 }
55
56 - (id)initWithCoder:(NSCoder *)aDecoder
57 {
58     if ( (self = [super init]) ) {
59         [aDecoder decodeValueOfObjCType: @encode(short) at: &mKeyCode];
60         [aDecoder decodeValueOfObjCType: @encode(long) at: &mModifiers];
61     }
62     
63     return self;
64 }
65
66 - (void)encodeWithCoder:(NSCoder *)aCoder
67 {       
68     [aCoder encodeValueOfObjCType:@encode(short) at:&mKeyCode];
69     [aCoder encodeValueOfObjCType:@encode(long) at:&mModifiers];
70 }
71
72 - (BOOL)isEqual:(KeyCombo *)object
73 {
74     return ( ([object isKindOfClass:[KeyCombo class]]) &&
75              ([object keyCode] == [self keyCode])      &&
76              ([object modifiers] == [self modifiers]) );
77 }
78
79 - (NSString *)description
80 {
81     return [self userDisplayRep];
82 }
83
84 - (short)keyCode
85 {
86     return mKeyCode;
87 }
88
89 - (short)modifiers
90 {
91     return mModifiers;
92 }
93
94 - (BOOL)isValid
95 {
96     return ((mKeyCode >= 0) && (mModifiers >= 0));
97 }
98
99 - (NSString *)userDisplayRep
100 {
101     if ( ! [self isValid] ) {
102         return @"";
103     } else {
104         return [NSString stringWithFormat: @"%@%@",
105             [KeyCombo _stringForModifiers: mModifiers],
106             [KeyCombo _stringForKeyCode: mKeyCode]];
107     }
108 }
109
110 + (NSString *)_stringForModifiers: (long)modifiers
111 {
112     static long modToChar[4][2] = {
113             { cmdKey,   0x23180000 },
114             { optionKey,        0x23250000 },
115             { controlKey,       0x23030000 },
116             { shiftKey, 0x21e70000 }
117     };
118     
119     NSString *str = [NSString string];
120     NSString *charStr;
121     long i;
122     
123     for (i = 0; i < 4; i++) {
124         if (modifiers & modToChar[i][0]) {
125             charStr = [NSString stringWithCharacters:(const unichar *)&modToChar[i][1] length:1];
126             str = [str stringByAppendingString:charStr];
127         }
128     }
129     
130     return str;
131 }
132
133 + (NSString *)_stringForKeyCode:(short)keyCode
134 {
135         NSDictionary *dict;
136         id key;
137         NSString *str;
138         
139         dict = [self keyCodesDictionary];
140         key = [NSString stringWithFormat: @"%d", keyCode];
141         str = [dict objectForKey: key];
142
143     if( !str ) {
144                 str = [NSString stringWithFormat: @"%X", keyCode];
145     }
146         
147         return str;
148 }
149
150 + (NSDictionary *)keyCodesDictionary
151 {
152     static NSDictionary *keyCodes = nil;
153     
154     if (keyCodes == nil) {
155         NSString *path;
156         NSString *contents;
157         
158         path = [[NSBundle bundleForClass: [KeyCombo class]] pathForResource: @"KeyCodes" ofType: @"plist"];
159         
160         contents = [NSString stringWithContentsOfFile: path];
161         keyCodes = [[contents propertyList] retain];
162     }
163     
164     return keyCodes;
165 }
166
167 @end
168
169 @implementation NSUserDefaults (KeyComboAdditions)
170
171 - (void)setKeyCombo:(KeyCombo *)combo forKey:(NSString *)key
172 {
173     NSData *data;
174     if (combo) {
175         data = [NSArchiver archivedDataWithRootObject:combo];
176     } else {
177         data = nil;
178     }
179     [self setObject:data forKey:key];
180 }
181
182 - (KeyCombo *)keyComboForKey:(NSString *)key
183 {
184     NSData *data = [self objectForKey:key];
185     KeyCombo *combo;
186     
187     if (data) {
188         combo = [NSUnarchiver unarchiveObjectWithData:data];
189     } else {
190         combo = nil;
191     }
192     
193     return combo;
194 }
195
196 @end
197
198
199
200
201