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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
Preferences.m
Copyright (c) 1995-1996, NeXT Software, Inc.
All rights reserved.
Author: Ali Ozer
You may freely copy, distribute and reuse the code in this example.
NeXT disclaims any warranty of any kind, expressed or implied,
as to its fitness for any particular use.
Preferences controller for Edit... To add new defaults search for one of the existing keys. Some keys have UI, others don't; use one similar to the one you're adding.
displayedValues is a mirror of the UI. These are committed by copying these values to curValues.
This module allows for UI where there is or there isn't an OK button. If you wish to have an OK button, connect OK to ok:, Revert to revert:, and don't call commitDisplayedValues from the various action messages.
*/
#import "Preferences.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
static NSDictionary *defaultValues() {
static NSDictionary *dict = nil;
if (!dict) {
dict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], DeleteBackup,
[NSNumber numberWithBool:NO], SaveFilesWritable,
[NSNumber numberWithBool:YES], RichText,
[NSNumber numberWithBool:NO], ShowPageBreaks,
[NSNumber numberWithBool:NO], OpenPanelFollowsMainWindow,
[NSNumber numberWithInt:80], WindowWidth,
[NSNumber numberWithInt:30], WindowHeight,
[NSNumber numberWithInt:UnknownStringEncoding], PlainTextEncoding,
[NSNumber numberWithInt:8], TabWidth,
[NSNumber numberWithInt:100000], ForegroundLayoutToIndex,
[NSFont userFixedPitchFontOfSize:0.0], PlainTextFont,
[NSFont userFontOfSize:0.0], RichTextFont,
nil];
}
return dict;
}
@implementation Preferences
static Preferences *sharedInstance = nil;
+ (id)objectForKey:(id)key {
return [[[self sharedInstance] preferences] objectForKey:key];
}
+ (void)saveDefaults {
if (sharedInstance) {
[Preferences savePreferencesToDefaults:[sharedInstance preferences]];
}
}
+ (Preferences *)sharedInstance {
return sharedInstance ? sharedInstance : [[self alloc] init];
}
- (id)init {
if (sharedInstance) {
[self dealloc];
} else {
[super init];
curValues = [[[self class] preferencesFromDefaults] copyWithZone:[self zone]];
[self discardDisplayedValues];
sharedInstance = self;
}
return sharedInstance;
}
- (void)dealloc {
}
- (NSDictionary *)preferences {
return curValues;
}
- (void)showPanel:(id)sender {
if (!richTextFontNameField) {
if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
NSLog(@"Failed to load Preferences.nib");
NSBeep();
return;
}
[[richTextFontNameField window] setExcludedFromWindowsMenu:YES];
[[richTextFontNameField window] setMenu:nil];
[self updateUI];
[[richTextFontNameField window] center];
}
[[richTextFontNameField window] makeKeyAndOrderFront:nil];
}
static void showFontInField(NSFont *font, NSTextField *field) {
[field setStringValue:font ? [NSString stringWithFormat:@"%@ %g", [font fontName], [font pointSize]] : @""];
}
- (void)updateUI {
showFontInField([displayedValues objectForKey:RichTextFont], richTextFontNameField);
showFontInField([displayedValues objectForKey:PlainTextFont], plainTextFontNameField);
[deleteBackupMatrix selectCellWithTag:[[displayedValues objectForKey:DeleteBackup] boolValue] ? 1 : 0];
[saveFilesWritableButton setState:[[displayedValues objectForKey:SaveFilesWritable] boolValue]];
[richTextMatrix selectCellWithTag:[[displayedValues objectForKey:RichText] boolValue] ? 1 : 0];
[showPageBreaksButton setState:[[displayedValues objectForKey:ShowPageBreaks] boolValue]];
[windowWidthField setIntValue:[[displayedValues objectForKey:WindowWidth] intValue]];
[windowHeightField setIntValue:[[displayedValues objectForKey:WindowHeight] intValue]];
SetUpEncodingPopupButton(plainTextEncodingPopup, [[displayedValues objectForKey:PlainTextEncoding] intValue], YES);
}
/* Gets everything from UI except for fonts...
*/
- (void)miscChanged:(id)sender {
static NSNumber *yes = nil;
static NSNumber *no = nil;
int anInt;
if (!yes) {
yes = [[NSNumber alloc] initWithBool:YES];
no = [[NSNumber alloc] initWithBool:NO];
}
[displayedValues setObject:[[deleteBackupMatrix selectedCell] tag] ? yes : no forKey:DeleteBackup];
[displayedValues setObject:[[richTextMatrix selectedCell] tag] ? yes : no forKey:RichText];
[displayedValues setObject:[saveFilesWritableButton state] ? yes : no forKey:SaveFilesWritable];
[displayedValues setObject:[showPageBreaksButton state] ? yes : no forKey:ShowPageBreaks];
[displayedValues setObject:[NSNumber numberWithInt:[[plainTextEncodingPopup selectedItem] tag]] forKey:PlainTextEncoding];
if ((anInt = [windowWidthField intValue]) < 1 || anInt > 10000) {
if ((anInt = [[displayedValues objectForKey:WindowWidth] intValue]) < 1 || anInt > 10000) anInt = [[defaultValues() objectForKey:WindowWidth] intValue];
[windowWidthField setIntValue:anInt];
} else {
[displayedValues setObject:[NSNumber numberWithInt:anInt] forKey:WindowWidth];
}
if ((anInt = [windowHeightField intValue]) < 1 || anInt > 10000) {
if ((anInt = [[displayedValues objectForKey:WindowHeight] intValue]) < 1 || anInt > 10000) anInt = [[defaultValues() objectForKey:WindowHeight] intValue];
[windowHeightField setIntValue:[[displayedValues objectForKey:WindowHeight] intValue]];
} else {
[displayedValues setObject:[NSNumber numberWithInt:anInt] forKey:WindowHeight];
}
[self commitDisplayedValues];
}
/**** Font changing code ****/
static BOOL changingRTFFont = NO;
- (void)changeRichTextFont:(id)sender {
changingRTFFont = YES;
[[richTextFontNameField window] makeFirstResponder:[richTextFontNameField window]];
[[NSFontManager sharedFontManager] setSelectedFont:[curValues objectForKey:RichTextFont] isMultiple:NO];
[[NSFontManager sharedFontManager] orderFrontFontPanel:self];
}
- (void)changePlainTextFont:(id)sender {
changingRTFFont = NO;
[[richTextFontNameField window] makeFirstResponder:[richTextFontNameField window]];
[[NSFontManager sharedFontManager] setSelectedFont:[curValues objectForKey:PlainTextFont] isMultiple:NO];
[[NSFontManager sharedFontManager] orderFrontFontPanel:self];
}
- (void)changeFont:(id)fontManager {
if (changingRTFFont) {
[displayedValues setObject:[fontManager convertFont:[curValues objectForKey:RichTextFont]] forKey:RichTextFont];
showFontInField([displayedValues objectForKey:RichTextFont], richTextFontNameField);
} else {
[displayedValues setObject:[fontManager convertFont:[curValues objectForKey:PlainTextFont]] forKey:PlainTextFont];
showFontInField([displayedValues objectForKey:PlainTextFont], plainTextFontNameField);
}
[self commitDisplayedValues];
}
/**** Commit/revert etc ****/
- (void)commitDisplayedValues {
if (curValues != displayedValues) {
[curValues release];
curValues = [displayedValues copyWithZone:[self zone]];
}
}
- (void)discardDisplayedValues {
if (curValues != displayedValues) {
[displayedValues release];
displayedValues = [curValues mutableCopyWithZone:[self zone]];
[self updateUI];
}
}
- (void)ok:(id)sender {
[self commitDisplayedValues];
}
- (void)revertToDefault:(id)sender {
curValues = [defaultValues() copyWithZone:[self zone]];
[self discardDisplayedValues];
}
- (void)revert:(id)sender {
[self discardDisplayedValues];
}
/**** Code to deal with defaults ****/
#define getBoolDefault(name) \
{NSString *str = [defaults stringForKey:name]; \
[dict setObject:str ? [NSNumber numberWithBool:[str hasPrefix:@"Y"]] : [defaultValues() objectForKey:name] forKey:name];}
#define getIntDefault(name) \
{NSString *str = [defaults stringForKey:name]; \
[dict setObject:str ? [NSNumber numberWithInt:[str intValue]] : [defaultValues() objectForKey:name] forKey:name];}
+ (NSDictionary *)preferencesFromDefaults {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
getBoolDefault(RichText);
getBoolDefault(DeleteBackup);
getBoolDefault(ShowPageBreaks);
getBoolDefault(SaveFilesWritable);
getBoolDefault(OpenPanelFollowsMainWindow);
getIntDefault(WindowWidth);
getIntDefault(WindowHeight);
getIntDefault(PlainTextEncoding);
getIntDefault(TabWidth);
getIntDefault(ForegroundLayoutToIndex);
[dict setObject:[NSFont userFontOfSize:0.0] forKey:RichTextFont];
[dict setObject:[NSFont userFixedPitchFontOfSize:0.0] forKey:PlainTextFont];
return dict;
}
#define setBoolDefault(name) \
{if ([[defaultValues() objectForKey:name] isEqual:[dict objectForKey:name]]) [defaults removeObjectForKey:name]; else [defaults setBool:[[dict objectForKey:name] boolValue] forKey:name];}
#define setIntDefault(name) \
{if ([[defaultValues() objectForKey:name] isEqual:[dict objectForKey:name]]) [defaults removeObjectForKey:name]; else [defaults setInteger:[[dict objectForKey:name] intValue] forKey:name];}
+ (void)savePreferencesToDefaults:(NSDictionary *)dict {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
setBoolDefault(RichText);
setBoolDefault(DeleteBackup);
setBoolDefault(ShowPageBreaks);
setBoolDefault(SaveFilesWritable);
setBoolDefault(OpenPanelFollowsMainWindow);
setIntDefault(WindowWidth);
setIntDefault(WindowHeight);
setIntDefault(PlainTextEncoding);
setIntDefault(TabWidth);
setIntDefault(ForegroundLayoutToIndex);
if (![[dict objectForKey:RichTextFont] isEqual:[NSFont userFontOfSize:0.0]]) [NSFont setUserFont:[dict objectForKey:RichTextFont]];
if (![[dict objectForKey:PlainTextFont] isEqual:[NSFont userFixedPitchFontOfSize:0.0]]) [NSFont setUserFixedPitchFont:[dict objectForKey:PlainTextFont]];
}
@end
/*
10/24/95 aozer Created.
*/
|