3f7163627571e42268beb147c34e72b527ce4fbb
[ITKit.git] / ITKeyComboPanel.m
1 //
2 //  ITKeyComboPanel.m
3 //
4 //  Created by Quentin Carnicelli on Sun Aug 03 2003.
5 //  Copyright (c) 2003 iThink Software. All rights reserved.
6 //
7
8 #import "ITKeyComboPanel.h"
9
10 #import "ITHotKey.h"
11 #import "ITKeyCombo.h"
12 #import "ITKeyBroadcaster.h"
13 #import "ITHotKeyCenter.h"
14
15 #if __PROTEIN__
16 #import "NSObjectAdditions.h"
17 #endif
18
19 @implementation ITKeyComboPanel
20
21 static id _sharedKeyComboPanel = nil;
22
23 + (id)sharedPanel
24 {
25         if( _sharedKeyComboPanel == nil )
26         {
27                 _sharedKeyComboPanel = [[self alloc] init];
28         
29                 #if __PROTEIN__
30                         [_sharedKeyComboPanel releaseOnTerminate];
31                 #endif
32         }
33
34         return _sharedKeyComboPanel;
35 }
36
37 - (id)init
38 {
39         return [self initWithWindowNibName: @"ITKeyComboPanel"];
40 }
41
42 - (void)dealloc
43 {
44         [[NSNotificationCenter defaultCenter] removeObserver: self];
45         [mKeyName release];
46
47         [super dealloc];
48 }
49
50 - (void)windowDidLoad
51 {
52         [[NSNotificationCenter defaultCenter]
53                 addObserver: self
54                 selector: @selector( noteKeyBroadcast: )
55                 name: ITKeyBroadcasterKeyEvent
56                 object: mKeyBcaster];
57 }
58
59 - (void)_refreshContents
60 {
61         if( mComboField )
62                 [mComboField setStringValue: [mKeyCombo description]];
63
64         if( mTitleField )
65                 [mTitleField setStringValue:mKeyName];
66 }
67
68 - (int)runModal
69 {
70         int resultCode;
71
72         [self window]; //Force us to load
73
74         [self _refreshContents];
75         [[self window] center];
76         [self showWindow: self];
77         resultCode = [[NSApplication sharedApplication] runModalForWindow: [self window]];
78         [self close];
79
80         return resultCode;
81 }
82
83 - (void)runModalForHotKey: (ITHotKey*)hotKey
84 {
85         int resultCode;
86
87         [self setKeyBindingName: [hotKey name]];
88         [self setKeyCombo: [hotKey keyCombo]];
89
90         resultCode = [self runModal];
91         
92         if( resultCode == NSOKButton )
93         {
94                 [hotKey setKeyCombo: [self keyCombo]];
95                 [[ITHotKeyCenter sharedCenter] registerHotKey: hotKey];
96         }
97 }
98
99 #pragma mark -
100
101 - (void)setKeyCombo: (ITKeyCombo*)combo
102 {
103         [combo retain];
104         [mKeyCombo release];
105         mKeyCombo = combo;
106         [self _refreshContents];
107 }
108
109 - (ITKeyCombo*)keyCombo
110 {
111         return mKeyCombo;
112 }
113
114 - (void)setKeyBindingName: (NSString*)name
115 {
116         [name retain];
117         [mKeyName release];
118         mKeyName = name;
119         [self _refreshContents];
120 }
121
122 - (NSString*)keyBindingName
123 {
124         return mKeyName;
125 }
126
127 #pragma mark -
128
129 - (IBAction)ok: (id)sender
130 {
131         [[NSApplication sharedApplication] stopModalWithCode: NSOKButton];
132 }
133
134 - (IBAction)cancel: (id)sender
135 {
136         [[NSApplication sharedApplication] stopModalWithCode: NSCancelButton];
137 }
138
139 - (IBAction)clear: (id)sender
140 {
141         [self setKeyCombo: [ITKeyCombo clearKeyCombo]];
142         [[NSApplication sharedApplication] stopModalWithCode: NSOKButton];
143 }
144
145 - (void)noteKeyBroadcast: (NSNotification*)note
146 {
147         ITKeyCombo* keyCombo;
148         
149         keyCombo = [[note userInfo] objectForKey: @"keyCombo"];
150
151         [self setKeyCombo: keyCombo];
152 }
153
154 @end