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