Added a commented-out line, template for when making builds for beta testers.
[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     self = [self init];
59     
60     if( self )
61     {
62         [aDecoder decodeValueOfObjCType: @encode(short) at: &mKeyCode];
63         [aDecoder decodeValueOfObjCType: @encode(long) at: &mModifiers];
64     }
65     
66     return self;
67 }
68
69 - (void)encodeWithCoder:(NSCoder *)aCoder
70 {       
71     [aCoder encodeValueOfObjCType:@encode(short) at:&mKeyCode];
72     [aCoder encodeValueOfObjCType:@encode(long) at:&mModifiers];
73 }
74
75 - (BOOL)isEqual:(KeyCombo *)object
76 {
77     return ([object isKindOfClass:[KeyCombo class]]) &&
78            ([object keyCode] == [self keyCode]) &&
79            ([object modifiers] == [self modifiers]);
80 }
81
82 - (NSString *)description
83 {
84     return [self userDisplayRep];
85 }
86
87 - (short)keyCode
88 {
89     return mKeyCode;
90 }
91
92 - (short)modifiers
93 {
94     return mModifiers;
95 }
96
97 - (BOOL)isValid
98 {
99     return ((mKeyCode >= 0) && (mModifiers >= 0));
100 }
101
102 - (NSString *)userDisplayRep
103 {
104     if ([self isValid] == NO)
105     {
106         return @"None";
107     }
108     else
109     {
110         return [NSString stringWithFormat: @"%@%@",
111             [KeyCombo _stringForModifiers: mModifiers],
112             [KeyCombo _stringForKeyCode: mKeyCode]];
113     }
114 }
115
116 + (NSString *)_stringForModifiers: (long)modifiers
117 {
118     static long modToChar[4][2] =
119     {
120             { cmdKey,   0x23180000 },
121             { optionKey,        0x23250000 },
122             { controlKey,       0x005E0000 },
123             { shiftKey, 0x21e70000 }
124     };
125     
126     NSString *str = [NSString string];
127     NSString *charStr;
128     long i;
129     
130     for (i = 0; i < 4; i++)
131     {
132         if (modifiers & modToChar[i][0])
133         {
134             charStr = [NSString stringWithCharacters:(const unichar *)&modToChar[i][1] length:1];
135             str = [str stringByAppendingString:charStr];
136         }
137     }
138     
139     return str;
140 }
141
142 + (NSString *)_stringForKeyCode:(short)keyCode
143 {
144         NSDictionary *dict;
145         id key;
146         NSString *str;
147         
148         dict = [self keyCodesDictionary];
149         key = [NSString stringWithFormat: @"%d", keyCode];
150         str = [dict objectForKey: key];
151         
152         if( !str )
153                 str = [NSString stringWithFormat: @"%X", keyCode];
154         
155         return str;
156 }
157
158 + (NSDictionary *)keyCodesDictionary
159 {
160     static NSDictionary *keyCodes = nil;
161     
162     if (keyCodes == nil)
163     {
164         NSString *path;
165         NSString *contents;
166         
167         path = [[NSBundle bundleForClass: [KeyCombo class]] pathForResource: @"KeyCodes" ofType: @"plist"];
168         
169         contents = [NSString stringWithContentsOfFile: path];
170         keyCodes = [[contents propertyList] retain];
171     }
172     
173     return keyCodes;
174 }
175
176 @end
177
178 @implementation NSUserDefaults (KeyComboAdditions)
179
180 - (void)setKeyCombo:(KeyCombo *)combo forKey:(NSString *)key
181 {
182     NSData *data;
183     if (combo)
184     {
185         data = [NSArchiver archivedDataWithRootObject:combo];
186     }
187     else
188     {
189         data = nil;
190     }
191     [self setObject:data forKey:key];
192 }
193
194 - (KeyCombo *)keyComboForKey:(NSString *)key
195 {
196     NSData *data = [self objectForKey:key];
197     KeyCombo *combo;
198     if (data)
199     {
200         combo =  [[NSUnarchiver unarchiveObjectWithData:data] retain];
201     }
202     
203     if (combo == nil)
204     {
205         combo = [[KeyCombo alloc] init];
206     }
207     return combo;
208 }
209
210 @end
211
212
213
214
215