Fixed a possible crasher where an ITMultilineTextFieldCell is treated as
[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)keyComboWithPlistRepresentation: (id)plist
25 {
26     return [[[self alloc] initWithPlistRepresentation: plist] autorelease];
27 }
28
29 - (id)initWithKeyCode: (int)keyCode modifiers: (int)modifiers
30 {
31         self = [super init];
32         
33         if( self )
34         {
35                 mKeyCode = keyCode;
36                 mModifiers = modifiers;
37         }
38         
39         return self;
40 }
41
42 - (id)initWithPlistRepresentation: (id)plist
43 {
44         int keyCode, modifiers;
45                 
46         keyCode = [[plist objectForKey: @"keyCode"] intValue];
47         if( keyCode <= 0 ) keyCode = -1;
48
49         modifiers = [[plist objectForKey: @"modifiers"] intValue];
50         if( modifiers <= 0 ) modifiers = -1;
51
52         return [self initWithKeyCode: keyCode modifiers: modifiers];
53 }
54
55 - (id)plistRepresentation
56 {
57         return [NSDictionary dictionaryWithObjectsAndKeys:
58                                 [NSNumber numberWithInt: [self keyCode]], @"keyCode",
59                                 [NSNumber numberWithInt: [self modifiers]], @"modifiers",
60                                 nil];
61 }
62
63 - (id)copyWithZone:(NSZone*)zone;
64 {
65         return [self retain];
66 }
67
68 - (BOOL)isEqual: (ITKeyCombo*)combo
69 {
70         return  [self keyCode] == [combo keyCode] &&
71                         [self modifiers] == [combo modifiers];
72 }
73
74 #pragma mark -
75
76 - (int)keyCode
77 {
78         return mKeyCode;
79 }
80
81 - (int)modifiers
82 {
83         return mModifiers;
84 }
85
86 - (BOOL)isValidHotKeyCombo
87 {
88         return mKeyCode >= 0 && mModifiers > 0;
89 }
90
91 - (BOOL)isClearCombo
92 {
93         return mKeyCode == -1 && mModifiers == -1;
94 }
95
96 @end
97
98 #pragma mark -
99
100 @implementation ITKeyCombo (UserDisplayAdditions)
101
102 + (NSDictionary*)_keyCodesDictionary
103 {
104         static NSDictionary* keyCodes = nil;
105         
106         if( keyCodes == nil )
107         {
108                 NSString* path;
109                 NSString* contents;
110                 
111                 path = [[NSBundle bundleForClass: self] pathForResource: @"ITKeyCodes" ofType: @"plist"];
112                 contents = [NSString stringWithContentsOfFile: path];
113                 keyCodes = [[contents propertyList] retain];
114         }
115         
116         return keyCodes;
117 }
118
119 + (NSString*)_stringForModifiers: (long)modifiers
120 {
121         static long modToChar[4][2] =
122         {
123                 { cmdKey,               0x23180000 },
124                 { optionKey,    0x23250000 },
125                 { controlKey,   0x005E0000 },
126                 { shiftKey,             0x21e70000 }
127         };
128
129         NSString* str = nil;
130         NSString* charStr;
131         long i;
132
133         str = [NSString string];
134
135         for( i = 0; i < 4; i++ )
136         {
137                 if( modifiers & modToChar[i][0] )
138                 {
139                         charStr = [NSString stringWithCharacters: (const unichar*)&modToChar[i][1] length: 1];
140                         str = [str stringByAppendingString: charStr];
141                 }
142         }
143         
144         if( !str )
145                 str = @"";
146         
147         return str;
148 }
149
150 + (NSString*)_stringForKeyCode: (short)keyCode
151 {
152         NSDictionary* dict;
153         id key;
154         NSString* str;
155         
156         dict = [self _keyCodesDictionary];
157         key = [NSString stringWithFormat: @"%d", keyCode];
158         str = [dict objectForKey: key];
159         
160         if( !str )
161                 str = [NSString stringWithFormat: @"%X", keyCode];
162         
163         return str;
164 }
165
166 - (NSString*)description
167 {
168         NSString* desc;
169         
170         if( [self isValidHotKeyCombo] ) //This might have to change
171         {
172                 desc = [NSString stringWithFormat: @"%@%@",
173                                 [[self class] _stringForModifiers: [self modifiers]],
174                                 [[self class] _stringForKeyCode: [self keyCode]]];
175         }
176         else
177                 desc = NSLocalizedString( @"(None)", @"Hot Keys: Key Combo text for 'empty' combo" );
178
179         return desc;
180 }
181
182 @end