Huge audit of ITKit, mostly everything has been updated to current coding
[ITKit.git] / ITKeyComboPanel.m
1 #import "ITKeyComboPanel.h"
2 #import "ITHotKey.h"
3 #import "ITKeyCombo.h"
4 #import "ITKeyBroadcaster.h"
5 #import "ITHotKeyCenter.h"
6
7 @implementation ITKeyComboPanel
8
9 static id _sharedKeyComboPanel = nil;
10
11 + (id)sharedPanel {
12         if (!_sharedKeyComboPanel) {
13                 _sharedKeyComboPanel = [[self alloc] init];
14         }
15         return _sharedKeyComboPanel;
16 }
17
18 - (id)init {
19         return [self initWithWindowNibName:@"ITKeyComboPanel"];
20 }
21
22 - (void)dealloc {
23         [[NSNotificationCenter defaultCenter] removeObserver:self];
24         [mKeyName release];
25         [super dealloc];
26 }
27
28 - (void)windowDidLoad {
29         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noteKeyBroadcast:) name:ITKeyBroadcasterKeyEvent object:mKeyBcaster];
30 }
31
32 - (void)_refreshContents {
33         if (mComboField) {
34                 [mComboField setStringValue:[mKeyCombo description]];
35         }
36         if (mTitleField) {
37                 [mTitleField setStringValue:mKeyName];
38         }
39 }
40
41 - (int)runModal {
42         int resultCode;
43         
44         [self window]; //Force us to load
45         
46         [self _refreshContents];
47         [[self window] center];
48         [self showWindow:self];
49         resultCode = [[NSApplication sharedApplication] runModalForWindow:[self window]];
50         [self close];
51         
52         return resultCode;
53 }
54
55 - (void)runModalForHotKey:(ITHotKey *)hotKey {
56         int resultCode;
57
58         [self setKeyBindingName:[hotKey name]];
59         [self setKeyCombo:[hotKey keyCombo]];
60
61         resultCode = [self runModal];
62         
63         if (resultCode == NSOKButton) {
64                 [hotKey setKeyCombo:[self keyCombo]];
65                 [[ITHotKeyCenter sharedCenter] registerHotKey:hotKey];
66         }
67 }
68
69 - (void)setKeyCombo:(ITKeyCombo *)combo {
70         [mKeyCombo autorelease];
71         mKeyCombo = [combo retain];
72         [self _refreshContents];
73 }
74
75 - (ITKeyCombo *)keyCombo {
76         return mKeyCombo;
77 }
78
79 - (void)setKeyBindingName:(NSString *)name {
80         [mKeyName autorelease];
81         mKeyName = [name retain];
82         [self _refreshContents];
83 }
84
85 - (NSString *)keyBindingName {
86         return mKeyName;
87 }
88
89 - (IBAction)ok:(id)sender {
90         [[NSApplication sharedApplication] stopModalWithCode:NSOKButton];
91 }
92
93 - (IBAction)cancel:(id)sender {
94         [[NSApplication sharedApplication] stopModalWithCode:NSCancelButton];
95 }
96
97 - (IBAction)clear:(id)sender {
98         [self setKeyCombo:[ITKeyCombo clearKeyCombo]];
99         [[NSApplication sharedApplication] stopModalWithCode:NSOKButton];
100 }
101
102 - (void)noteKeyBroadcast:(NSNotification *)note {
103         ITKeyCombo *keyCombo = [[note userInfo] objectForKey:@"keyCombo"];
104         [self setKeyCombo:keyCombo];
105 }
106
107 @end