1a705284710f227f552f5a306d93f8890beeb6a9
[ITKit.git] / ITKeyCombo.m
1 //
2 //  ITKeyCombo.m
3 //
4 //  Created by Quentin Carnicelli on Sat Aug 02 2003.
5 //  Copyright (c) 2003 iThink Software. All rights reserved.
6 //
7
8 #import "ITKeyCombo.h"
9
10 #import <Carbon/Carbon.h>
11
12 @implementation ITKeyCombo
13
14 + (id)clearKeyCombo
15 {
16         return [self keyComboWithKeyCode: -1 modifiers: -1];
17 }
18
19 + (id)keyComboWithKeyCode: (int)keyCode modifiers: (int)modifiers
20 {
21         return [[[self alloc] initWithKeyCode: keyCode modifiers: modifiers] autorelease];
22 }
23
24 - (id)initWithKeyCode: (int)keyCode modifiers: (int)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         keyCode = [[plist objectForKey: @"keyCode"] intValue];
42         if( keyCode <= 0 ) keyCode = -1;
43
44         modifiers = [[plist objectForKey: @"modifiers"] intValue];
45         if( modifiers <= 0 ) modifiers = -1;
46
47         return [self initWithKeyCode: keyCode modifiers: modifiers];
48 }
49
50 - (id)plistRepresentation
51 {
52         return [NSDictionary dictionaryWithObjectsAndKeys:
53                                 [NSNumber numberWithInt: [self keyCode]], @"keyCode",
54                                 [NSNumber numberWithInt: [self modifiers]], @"modifiers",
55                                 nil];
56 }
57
58 - (id)copyWithZone:(NSZone*)zone;
59 {
60         return [self retain];
61 }
62
63 - (BOOL)isEqual: (ITKeyCombo*)combo
64 {
65         return  [self keyCode] == [combo keyCode] &&
66                         [self modifiers] == [combo modifiers];
67 }
68
69 #pragma mark -
70
71 - (int)keyCode
72 {
73         return mKeyCode;
74 }
75
76 - (int)modifiers
77 {
78         return mModifiers;
79 }
80
81 - (BOOL)isValidHotKeyCombo
82 {
83         return mKeyCode >= 0 && mModifiers > 0;
84 }
85
86 - (BOOL)isClearCombo
87 {
88         return mKeyCode == -1 && mModifiers == -1;
89 }
90
91 @end
92
93 #pragma mark -
94
95 @implementation ITKeyCombo (UserDisplayAdditions)
96
97 + (NSDictionary*)_keyCodesDictionary
98 {
99         static NSDictionary* keyCodes = nil;
100         
101         if( keyCodes == nil )
102         {
103                 NSString* path;
104                 NSString* contents;
105                 
106                 path = [[NSBundle bundleForClass: self] pathForResource: @"ITKeyCodes" ofType: @"plist"];
107                 contents = [NSString stringWithContentsOfFile: path];
108                 keyCodes = [[contents propertyList] retain];
109         }
110         
111         return keyCodes;
112 }
113
114 + (NSString*)_stringForModifiers: (long)modifiers
115 {
116         static long modToChar[4][2] =
117         {
118                 { cmdKey,               0x23180000 },
119                 { optionKey,    0x23250000 },
120                 { controlKey,   0x005E0000 },
121                 { shiftKey,             0x21e70000 }
122         };
123
124         NSString* str = nil;
125         NSString* charStr;
126         long i;
127
128         str = [NSString string];
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         if( !str )
140                 str = @"";
141         
142         return str;
143 }
144
145 + (NSString*)_stringForKeyCode: (short)keyCode
146 {
147         NSDictionary* dict;
148         id key;
149         NSString* str;
150         
151         dict = [self _keyCodesDictionary];
152         key = [NSString stringWithFormat: @"%d", keyCode];
153         str = [dict objectForKey: key];
154         
155         if( !str )
156                 str = [NSString stringWithFormat: @"%X", keyCode];
157         
158         return str;
159 }
160
161 - (NSString*)description
162 {
163         NSString* desc;
164         
165         if( [self isValidHotKeyCombo] ) //This might have to change
166         {
167                 desc = [NSString stringWithFormat: @"%@%@",
168                                 [[self class] _stringForModifiers: [self modifiers]],
169                                 [[self class] _stringForKeyCode: [self keyCode]]];
170         }
171         else
172                 desc = NSLocalizedString( @"(None)", @"Hot Keys: Key Combo text for 'empty' combo" );
173
174         return desc;
175 }
176
177 @end