Adding HotKeys to ITKit... This is the new HotKey code by Quentin of
[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         [mTitleFormat release];
47
48         [super dealloc];
49 }
50
51 - (void)windowDidLoad
52 {
53         mTitleFormat = [[mTitleField stringValue] retain];
54
55         [[NSNotificationCenter defaultCenter]
56                 addObserver: self
57                 selector: @selector( noteKeyBroadcast: )
58                 name: ITKeyBroadcasterKeyEvent
59                 object: mKeyBcaster];
60 }
61
62 - (void)_refreshContents
63 {
64         if( mComboField )
65                 [mComboField setStringValue: [mKeyCombo description]];
66
67         if( mTitleField )
68                 [mTitleField setStringValue: [NSString stringWithFormat: mTitleFormat, mKeyName]];
69 }
70
71 - (int)runModal
72 {
73         int resultCode;
74
75         [self window]; //Force us to load
76
77         [self _refreshContents];
78         [[self window] center];
79         [self showWindow: self];
80         resultCode = [[NSApplication sharedApplication] runModalForWindow: [self window]];
81         [self close];
82
83         return resultCode;
84 }
85
86 - (void)runModalForHotKey: (ITHotKey*)hotKey
87 {
88         int resultCode;
89
90         [self setKeyBindingName: [hotKey name]];
91         [self setKeyCombo: [hotKey keyCombo]];
92
93         resultCode = [self runModal];
94         
95         if( resultCode == NSOKButton )
96         {
97                 [hotKey setKeyCombo: [self keyCombo]];
98                 [[ITHotKeyCenter sharedCenter] registerHotKey: hotKey];
99         }
100 }
101
102 #pragma mark -
103
104 - (void)setKeyCombo: (ITKeyCombo*)combo
105 {
106         [combo retain];
107         [mKeyCombo release];
108         mKeyCombo = combo;
109         [self _refreshContents];
110 }
111
112 - (ITKeyCombo*)keyCombo
113 {
114         return mKeyCombo;
115 }
116
117 - (void)setKeyBindingName: (NSString*)name
118 {
119         [name retain];
120         [mKeyName release];
121         mKeyName = name;
122         [self _refreshContents];
123 }
124
125 - (NSString*)keyBindingName
126 {
127         return mKeyName;
128 }
129
130 #pragma mark -
131
132 - (IBAction)ok: (id)sender
133 {
134         [[NSApplication sharedApplication] stopModalWithCode: NSOKButton];
135 }
136
137 - (IBAction)cancel: (id)sender
138 {
139         [[NSApplication sharedApplication] stopModalWithCode: NSCancelButton];
140 }
141
142 - (IBAction)clear: (id)sender
143 {
144         [self setKeyCombo: [ITKeyCombo clearKeyCombo]];
145         [[NSApplication sharedApplication] stopModalWithCode: NSOKButton];
146 }
147
148 - (void)noteKeyBroadcast: (NSNotification*)note
149 {
150         ITKeyCombo* keyCombo;
151         
152         keyCombo = [[note userInfo] objectForKey: @"keyCombo"];
153
154         [self setKeyCombo: keyCombo];
155 }
156
157 @end