-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferences.m
180 lines (148 loc) · 4.13 KB
/
Preferences.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* vim: set ft=objc ts=4 nowrap: */
#include <Cynthiune/Preference.h>
#include "Preferences.h"
#ifdef CDDB
#include "FreeDBView.h"
#endif
#include "GeneralView.h"
static Preferences *singleInstance = nil;
@implementation Preferences
+ (id) singleInstance
{
if (singleInstance == nil) {
singleInstance = [[Preferences alloc] init];
}
return singleInstance;
}
- (id) init
{
id module = nil;
self = [super init];
modules = [[NSMutableDictionary alloc] initWithCapacity: 2];
[self layoutWindow];
// initialise all sub panels
module = [GeneralView singleInstance];
if (module) {
[modules setObject: module forKey: [(GeneralView*)module name]];
}
[panelList addItemWithTitle: [(GeneralView*)module name]];
[module release];
#ifdef CDDB
module = [FreeDBView singleInstance];
if (module) {
[modules setObject: module forKey: [(FreeDBView*)module name]];
}
[panelList addItemWithTitle: [(FreeDBView*)module name]];
[module release];
#endif
return self;
}
- (void) dealloc
{
[window release];
[panelList release];
[saveButton release];
[closeButton release];
[panelBox release];
[modules release];
[super dealloc];
}
- (void) windowWillClose: (NSNotification *)theNotification
{
AUTORELEASE(self);
singleInstance = nil;
}
- (void) showPanel: (id) sender
{
[panelList selectItemAtIndex: 0];
[self showSubPanel: self];
[NSApp runModalForWindow: window];
}
- (void) save: (id) sender
{
int i;
id theModules = [modules allValues];
for (i = 0; i < [theModules count]; i++) {
id module = [theModules objectAtIndex: i];
if ([module conformsToProtocol: @protocol(Preference)]) {
[module save];
} else {
[module saveChanges];
}
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void) close: (id) sender
{
[NSApp stopModal];
[window performClose: self];
}
- (void) showSubPanel: (id) sender
{
id module = nil;
module = [modules objectForKey: [panelList titleOfSelectedItem]];
if (module) {
NSView *moduleView;
if ([module conformsToProtocol: @protocol(Preference)]) {
moduleView = [module preferenceSheet];
} else {
moduleView = [module view];
}
if ([panelBox contentView] != moduleView) {
[panelBox setContentView: moduleView];
[panelBox setTitle: [panelList titleOfSelectedItem]];
}
}
}
- (void) layoutWindow
{
NSRect frame;
unsigned int style = NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask;
frame = NSMakeRect(100, 100, 370,395);
window = [[NSPanel alloc] initWithContentRect: frame
styleMask: style
backing: NSBackingStoreRetained
defer: NO];
[window setTitle: _(@"Preferences")];
[window setMaxSize: frame.size];
[window setMinSize: frame.size];
frame = NSMakeRect(195, 10, 80, 28);
saveButton = [[NSButton alloc] initWithFrame: frame];
[saveButton setButtonType: NSMomentaryPushButton];
[saveButton setTitle: _(@"Save")];
[saveButton setTarget: self];
[saveButton setAction: @selector(save:)];
[[window contentView] addSubview: saveButton];
frame = NSMakeRect(285, 10, 80, 28);
closeButton = [[NSButton alloc] initWithFrame: frame];
[closeButton setTitle: _(@"Close")];
[closeButton setTarget: self];
[closeButton setAction: @selector(close:)];
[[window contentView] addSubview: closeButton];
frame = NSMakeRect(135, 315, 100, 25);
panelList = [[NSPopUpButton alloc] initWithFrame: frame pullsDown: NO];
[panelList setAutoenablesItems: NO];
[panelList setEnabled: YES];
[panelList setTarget: self];
[panelList setAction: @selector(showSubPanel:)];
[[window contentView] addSubview: panelList];
panelBox = [[NSBox alloc] initWithFrame: NSMakeRect(5,45,360,265)];
[panelBox setTitlePosition: NSAtTop];
[panelBox setBorderType: NSGrooveBorder];
[[window contentView] addSubview: panelBox];
[window center];
}
- (void) registerPreferenceClass: (Class) aClass
{
NSString *cname = NSStringFromClass (aClass);
if ([aClass conformsToProtocol: @protocol(Preference)]
&& (nil == [modules objectForKey: cname])) {
id<Preference> module = [aClass instance];
if (module) {
[modules setObject: module forKey: [module preferenceTitle]];
[panelList addItemWithTitle: [module preferenceTitle]];
[module release];
}
}
}
@end