Forge updates. Preferences is no more, since each bundle will have its

own prefs anyway. The "sample" bundle MainPrefs is more complete, and is
now loading its interface from a .gorm file -- this would be a .nib on
NeXTstep or Mac OS X, and it should be ready to run on those OSes
already -- since it doesn't do much. :)
This commit is contained in:
Jeff Teunissen 2002-01-25 08:45:28 +00:00
parent 4c8154a224
commit 9585b6af92
11 changed files with 284 additions and 461 deletions

View file

@ -4,6 +4,10 @@ BUNDLE_NAME= MainPrefs
BUNDLE_EXTENSION= .forgeb
BUNDLE_INSTALL_DIR= $(GNUSTEP_LOCAL_ROOT)/Library/Forge
MainPrefs_MAIN_MODEL_FILE= MainPrefs.gorm
MainPrefs_RESOURCE_FILES= \
MainPrefs.gorm
MainPrefs_OBJC_FILES= \
MainPrefsView.m \
MainPrefs.m

View file

@ -0,0 +1,84 @@
{
IBInspector = {
Actions = (
ok:,
revert:,
touch:
);
Outlets = (
window
);
Super = NSObject;
};
MainPrefs = {
Actions = (
loadPrefs:,
projectPathChanged:,
projectPathFindButtonClicked:,
resetPrefsToDefault:,
savePrefs:
);
Outlets = (
projectPathField,
projectPathFindButton,
view,
window
);
Super = NSObject;
};
NSApplication = {
Outlets = (
delegate
);
Super = NSResponder;
};
NSButton = {
Super = NSControl;
};
NSControl = {
Actions = (
takeDoubleValueFrom:,
takeFloatValueFrom:,
takeIntValueFrom:,
takeObjectValueFrom:,
takeStringValueFrom:
);
Outlets = (
target
);
Super = NSView;
};
NSMenu = {
Super = NSObject;
};
NSMenuItem = {
Outlets = (
target
);
Super = NSObject;
};
NSPanel = {
Super = NSWindow;
};
NSResponder = {
Super = NSObject;
};
NSSlider = {
Super = NSControl;
};
NSTextField = {
Outlets = (
delegate
);
Super = NSControl;
};
NSView = {
Super = NSResponder;
};
NSWindow = {
Outlets = (
delegate
);
Super = NSResponder;
};
}

Binary file not shown.

View file

@ -32,12 +32,20 @@
# include "Config.h"
#endif
#import <AppKit/NSNibDeclarations.h>
#import "BundleController.h"
#import "PrefsView.h"
#define ProjectPath @"projectPath"
@interface MainPrefs: NSObject <PrefsViewController, ForgeBundle>
{
NSView *view;
IBOutlet id projectPathField;
IBOutlet id projectPathFindButton;
IBOutlet id window;
IBOutlet id view;
}
@end

View file

@ -39,11 +39,98 @@ static const char rcsid[] =
# import <AppKit/NSNibLoading.h>
#endif
#import <AppKit/NSOpenPanel.h>
#import "PrefsPanel.h"
#import "PrefsController.h"
#import "MainPrefs.h"
#import "MainPrefsView.h"
@interface MainPrefs (Private)
- (NSDictionary *) preferencesFromDefaults;
- (void) savePreferencesToDefaults: (NSDictionary *) dict;
- (void) commitDisplayedValues;
- (void) discardDisplayedValues;
- (void) updateUI;
@end
@implementation MainPrefs (Private)
static NSDictionary *currentValues = nil;
static NSMutableDictionary *displayedValues = nil;
static NSMutableDictionary *
defaultValues (void) {
static NSMutableDictionary *dict = nil;
if (!dict) {
dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"/Local/Forge/Projects", ProjectPath,
nil];
}
return dict;
}
static NSString *
getStringDefault (NSMutableDictionary *dict, NSString *name)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
if (!str)
str = [defaultValues() objectForKey: name];
[dict setObject: str forKey: name];
return str;
}
- (NSDictionary *) preferencesFromDefaults
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 5];
getStringDefault (dict, ProjectPath);
return dict;
}
- (void) savePreferencesToDefaults: (NSDictionary *) dict
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
#define setStringDefault(name) \
[defaults setObject: [dict objectForKey: (name)] forKey: (name)]
NSDebugLog (@"Updating Main Preferences...");
setStringDefault (ProjectPath);
[defaults synchronize];
}
- (void) commitDisplayedValues
{
[currentValues release];
currentValues = [[displayedValues copy] retain];
[self savePreferencesToDefaults: currentValues];
[self updateUI];
}
- (void) discardDisplayedValues
{
[displayedValues release];
displayedValues = [[currentValues mutableCopy] retain];
[self updateUI];
}
- (void) updateUI
{
[projectPathField setStringValue: [displayedValues objectForKey: ProjectPath]];
[projectPathField setNeedsDisplay: YES];
}
@end // MainPrefs (Private)
@implementation MainPrefs
static MainPrefs *sharedInstance = nil;
@ -57,17 +144,19 @@ static id <BundleDelegate> owner = nil;
self = [super init];
owner = anOwner;
[owner registerPrefsController: self];
#ifdef USING_NIBS
if (![NSBundle loadNibNamed: @"MainPrefsView" owner: self]) {
NSLog (@"MainPrefs: Could not load nib \"MainPrefsView\", using compiled version");
#endif
view = [[MainPrefsView alloc] initWithFrame: PrefsRect];
#ifdef USING_NIBS
}
#endif
view = [[MainPrefsView alloc] initWithOwner: self andFrame: PrefsRect];
// hook up to our outlet(s)
projectPathField = [view directoryField];
} else {
view = [window contentView];
}
[view retain];
[self loadPrefs: self];
sharedInstance = self;
}
return sharedInstance;
@ -75,17 +164,26 @@ static id <BundleDelegate> owner = nil;
- (void) loadPrefs: (id) sender
{
return;
if (currentValues)
[currentValues release];
currentValues = [[[self preferencesFromDefaults] copyWithZone: [self zone]] retain];
[self discardDisplayedValues];
}
- (void) savePrefs: (id) sender
{
return;
[self commitDisplayedValues];
}
- (void) resetPrefsToDefault: (id) sender
{
return;
if (currentValues)
[currentValues release];
currentValues = [[defaultValues () copyWithZone: [self zone]] retain];
[self discardDisplayedValues];
}
- (void) showView: (id) sender;
@ -114,4 +212,33 @@ static id <BundleDelegate> owner = nil;
return @selector(showView:);
}
- (id) projectPath
{
return [displayedValues objectForKey: ProjectPath];
}
- (id) projectPathFindButtonClicked: (id) sender
{
int result;
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection: NO];
[oPanel setCanChooseFiles: NO];
[oPanel setCanChooseDirectories: YES];
result = [oPanel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
if (result == NSOKButton) { // got a new dir
NSArray *pathArray = [oPanel filenames];
[displayedValues setObject: [pathArray objectAtIndex: 0] forKey: ProjectPath];
[self updateUI];
}
}
- (void) projectPathChanged: (id) sender
{
[displayedValues setObject: [sender stringValue] forKey: ProjectPath];
[self updateUI];
}
@end

View file

@ -34,4 +34,14 @@
#import "PrefsView.h"
@interface MainPrefsView: NSView
{
id directoryField;
id findButton;
id owner;
}
- (id) initWithOwner: (id) anOwner andFrame: (NSRect) frameRect;
- (id) directoryField;
@end

View file

@ -35,26 +35,57 @@ static const char rcsid[] =
#import <AppKit/NSBezierPath.h>
#import <AppKit/NSButton.h>
#import <AppKit/NSTextField.h>
#import <AppKit/NSColor.h>
#import "MainPrefsView.h"
#import "MainPrefs.h"
@implementation MainPrefsView
- (id) initWithFrame: (NSRect) frameRect
- (id) initWithOwner: (id) anOwner andFrame: (NSRect) frameRect
{
id button;
id label = nil;
owner = anOwner;
if ((self = [super initWithFrame: frameRect])) {
button = [[NSButton alloc] initWithFrame: NSMakeRect (0, 0, 60, 24)];
[button autorelease];
label = [[NSTextField alloc] initWithFrame: NSMakeRect (3, 201, 480, 24)];
[label setEditable: NO];
[label setSelectable: NO];
[label setAllowsEditingTextAttributes: NO];
[label setImportsGraphics: NO];
[label setTextColor: [NSColor blackColor]];
[label setBackgroundColor: [NSColor controlColor]];
[label setBezeled: NO];
[label setStringValue: @"Project load path"];
[self addSubview: [label autorelease]];
[button setTitle: @"Default"];
[button setTarget: self];
[button setAction: @selector(resetToDefaults:)];
[self addSubview: button];
directoryField = [[NSTextField alloc] initWithFrame: NSMakeRect (3, 174, 410, 24)];
[directoryField setEditable: YES];
[directoryField setSelectable: YES];
[directoryField setAllowsEditingTextAttributes: NO];
[directoryField setImportsGraphics: NO];
[directoryField setTextColor: [NSColor blackColor]];
[directoryField setBackgroundColor: [NSColor whiteColor]];
[directoryField setBezeled: YES];
[directoryField setTarget: owner];
[directoryField setAction: @selector(projectPathChanged:)];
[self addSubview: [directoryField autorelease]];
findButton = [[NSButton alloc] initWithFrame: NSMakeRect (419, 174, 64, 24)];
[findButton setTitle: @"Find..."];
[findButton setTarget: owner];
[findButton setAction: @selector(projectPathFindButtonClicked:)];
[self addSubview: [findButton autorelease]];
}
return self;
}
- (id) directoryField
{
return directoryField;
}
@end

View file

@ -74,21 +74,6 @@
);
Super = NSResponder;
};
Preferences = {
Actions = (
ok,
revert,
revertToDefault,
prefsChanged,
setObject
);
Outlets = (
"e\n\tfloat\t\tyLight",
"e\n\tfloat\t\tzLight",
"e\n\tBOOL\t\tshowBSPOutput"
);
Super = NSObject;
};
PrefsPanel = {
Actions = (
);

View file

@ -70,7 +70,6 @@ Forge_HEADERS= \
Config.h \
BundleController.h \
Controller.h \
Preferences.h \
PrefsPanel.h \
PrefsController.h
@ -80,7 +79,6 @@ Forge_HEADERS= \
Forge_OBJC_FILES= \
BundleController.m \
Controller.m \
Preferences.m \
PrefsPanel.m \
PrefsController.m \
main.m

View file

@ -1,89 +0,0 @@
/*
Preferences.h
Preferences class
Copyright (C) 2001 Dusk to Dawn Computing, Inc.
Author: Jeff Teunissen <deek@d2dc.net>
Date: 9 Feb 2001
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "Config.h"
#endif
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
/*
Keys in the dictionary
*/
#define ProjectPath @"projectPath"
#define BspSoundPath @"bspSoundPath"
#define ShowBSPOutput @"showBSPOutput"
#define OffsetBrushCopy @"offsetBrushCopy"
#define StartWad @"startWad"
#define XLight @"xLight"
#define YLight @"yLight"
#define ZLight @"zLight"
@interface Preferences: NSObject
{
// UI targets
NSString *projectPath; // path to the project to load on startup
NSString *bspSoundPath; // location of BSP sounds
NSString *startWad; // which wadfile to load on startup
float xLight; // Lighting for X side
float yLight; // Lighting for Y side
float zLight; // Lighting for Z side
BOOL showBSPOutput; // "Show BSP Output" checkbox
BOOL offsetBrushCopy; // "Brush offset" checkbox
}
+ (Preferences *) sharedInstance; // Return the shared instance
- (void) saveDefaults;
- (void) loadDefaults;
- (NSDictionary *) preferences; // current prefs
- (void) updateUI; // Update the displayed values in the UI
- (void) commitDisplayedValues; // Make displayed settings current
- (void) discardDisplayedValues; // Replace displayed settings with current
// UI notifications
- (void) ok: (id) sender; // commit displayed values
- (void) revert: (id) sender; // revert to current values
- (void) revertToDefault: (id) sender; // revert current values to defaults and
// discard displayed values
- (void) prefsChanged: (id) sender; // Notify the object to update the UI
- (id) objectForKey: (id) key;
- (void) setObject: (id) obj forKey: (id) key;
- (NSDictionary *) preferencesFromDefaults;
- (void) savePreferencesToDefaults: (NSDictionary *) dict;
@end

View file

@ -1,335 +0,0 @@
/*
Preferences.m
Preferences class
Copyright (C) 2001 Dusk to Dawn Computing, Inc.
Author: Jeff Teunissen <deek@d2dc.net>
Date: 9 Feb 2001
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
static const char rcsid[] =
"$Id$";
#ifdef HAVE_CONFIG_H
# include "Config.h"
#endif
#import <Foundation/NSDictionary.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSString.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSValue.h>
#import <AppKit/NSButton.h>
#import <AppKit/NSControl.h>
#import "Preferences.h"
static NSMutableDictionary *
defaultValues (void) {
static NSMutableDictionary *dict = nil;
if (!dict) {
dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"/Local/Forge/Projects", ProjectPath,
@"/Local/Library/Sounds", BspSoundPath,
@"none", StartWad,
[NSNumber numberWithFloat: 1.0], XLight,
[NSNumber numberWithFloat: 0.6], YLight,
[NSNumber numberWithFloat: 0.75], ZLight,
[NSNumber numberWithBool: NO], ShowBSPOutput,
[NSNumber numberWithBool: NO], OffsetBrushCopy,
nil];
}
return dict;
}
/***
Code to deal with defaults
***/
static BOOL
getBoolDefault (NSMutableDictionary *dict, NSString *name)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
NSNumber *num;
if (!str)
str = [[defaultValues() objectForKey: name] stringValue];
num = [NSNumber numberWithBool: [str hasPrefix: @"Y"]];
[dict setObject: num forKey: name];
return [num boolValue];
}
static float
getFloatDefault (NSMutableDictionary *dict, NSString *name)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
NSNumber *num;
if (!str)
str = [[defaultValues() objectForKey: name] stringValue];
num = [NSNumber numberWithFloat: [str floatValue]];
[dict setObject: num forKey: name];
return [num floatValue];
}
#if 0 // unneeded
static int
getIntDefault (NSMutableDictionary *dict, NSString *name)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
NSNumber *num;
if (!str)
str = [[defaultValues() objectForKey: name] stringValue];
num = [NSNumber numberWithInt: [str intValue]];
[dict setObject: num forKey: name];
return [num intValue];
}
#endif
static NSString *
getStringDefault (NSMutableDictionary *dict, NSString *name)
{
NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name];
if (!str)
str = [defaultValues() objectForKey: name];
[dict setObject: str forKey: name];
return str;
}
@implementation Preferences
static Preferences *sharedInstance = nil;
static NSDictionary *currentValues = nil;
static NSMutableDictionary *displayedValues = nil;
- (id) objectForKey: (id) key
{
id obj = [[NSUserDefaults standardUserDefaults] objectForKey: key];
if (!obj)
return [defaultValues () objectForKey: key];
else
return obj;
}
- (void) setObject: (id) obj forKey: (id) key
{
if ((!key) || (!obj))
return;
[displayedValues setObject: obj forKey: key];
[self commitDisplayedValues];
}
- (void) saveDefaults
{
[self savePreferencesToDefaults: currentValues];
}
- (void) loadDefaults
{
if (currentValues)
[currentValues release];
currentValues = [[self preferencesFromDefaults] copyWithZone: [self zone]];
[self discardDisplayedValues];
}
+ (Preferences *) sharedInstance
{
return (sharedInstance ? sharedInstance : [[self alloc] init]);
}
- (id) init
{
if (sharedInstance) {
[self dealloc];
} else {
self = [super init];
currentValues = [[self preferencesFromDefaults] mutableCopy];
[self discardDisplayedValues];
sharedInstance = self;
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(saveDefaults)
name: @"NSApplicationWillTerminateNotification"
object: nil];
}
[self prefsChanged: self];
return sharedInstance;
}
- (NSDictionary *) preferences
{
return currentValues;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[currentValues release];
[displayedValues release];
currentValues = displayedValues = nil;
[super dealloc];
}
/*
updateUI
Update the user interface with new preferences
*/
- (void) updateUI
{
return;
}
- (void) prefsChanged: (id) sender
{
float aFloat;
[displayedValues setObject: projectPath forKey: ProjectPath];
[displayedValues setObject: bspSoundPath forKey: BspSoundPath];
[displayedValues setObject: startWad forKey: StartWad];
if ((aFloat = [[displayedValues objectForKey: XLight] floatValue]) < 0.0 || aFloat > 1.0) {
aFloat = [[defaultValues () objectForKey: XLight] floatValue];
}
xLight = aFloat;
[displayedValues setObject: [NSNumber numberWithFloat: xLight] forKey: XLight];
if ((aFloat = [[displayedValues objectForKey: YLight] floatValue]) < 0.0 || aFloat > 1.0) {
aFloat = [[defaultValues () objectForKey: YLight] floatValue];
}
yLight = aFloat;
[displayedValues setObject: [NSNumber numberWithFloat: yLight] forKey: YLight];
if ((aFloat = [[displayedValues objectForKey: ZLight] floatValue]) < 0.0 || aFloat > 1.0) {
aFloat = [[defaultValues () objectForKey: ZLight] floatValue];
}
zLight = aFloat;
[displayedValues setObject: [NSNumber numberWithFloat: zLight] forKey: ZLight];
[displayedValues setObject: [NSNumber numberWithBool: showBSPOutput] forKey: ShowBSPOutput];
[displayedValues setObject: [NSNumber numberWithBool: offsetBrushCopy] forKey: OffsetBrushCopy];
[self commitDisplayedValues];
}
- (void) commitDisplayedValues
{
[currentValues release];
currentValues = [[displayedValues copy] retain];
[self saveDefaults];
[self updateUI];
}
- (void) discardDisplayedValues
{
[displayedValues release];
displayedValues = [[currentValues mutableCopy] retain];
[self updateUI];
}
- (void) ok: (id) sender
{
[self commitDisplayedValues];
}
- (void) revert: (id) sender
{
[self discardDisplayedValues];
}
- (void) revertToDefault: (id) sender
{
if (currentValues)
[currentValues release];
currentValues = defaultValues ();
[self discardDisplayedValues];
}
- (NSDictionary *) preferencesFromDefaults
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 10];
projectPath = getStringDefault (dict, ProjectPath);
bspSoundPath = getStringDefault (dict, BspSoundPath);
startWad = getStringDefault (dict, StartWad);
xLight = getFloatDefault (dict, XLight);
yLight = getFloatDefault (dict, YLight);
zLight = getFloatDefault (dict, ZLight);
showBSPOutput = getBoolDefault (dict, ShowBSPOutput);
offsetBrushCopy = getBoolDefault (dict, OffsetBrushCopy);
return dict;
}
#define setBoolDefault(name) \
{ \
[defaults setBool:[[dict objectForKey:name] boolValue] forKey: name]; \
}
#define setFloatDefault(name) \
{ \
[defaults setFloat:[[dict objectForKey:name] floatValue] forKey: name]; \
}
#define setIntDefault(name) \
{ \
[defaults setInteger:[[dict objectForKey:name] intValue] forKey:name]; \
}
#define setStringDefault(name) \
{ \
[defaults setObject: [dict objectForKey: name] forKey: name]; \
}
- (void) savePreferencesToDefaults: (NSDictionary *) dict
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog (@"Updating defaults...");
setStringDefault(ProjectPath);
setStringDefault(BspSoundPath);
setStringDefault(StartWad);
setFloatDefault (XLight);
setFloatDefault (YLight);
setFloatDefault (ZLight);
setBoolDefault (ShowBSPOutput);
setBoolDefault (OffsetBrushCopy);
[defaults synchronize];
}
@end