-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreferenceController.m
executable file
·521 lines (386 loc) · 18 KB
/
PreferenceController.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
---------------------------------------------------------------------------
CocoaPad -- PreferenceController.m
By Henry Weiss
---------------------------------------------------------------------------
Makes the preference feature.
Major events:
- 01/17/04: CocoaPad started
- 03/10/04: Added notification handling
- 03/11/04: Mysterious flakiness from the system causes Xcode to fail writing
to the document, and then cannot save the project. Before I can
copy the code, it switches me to MyDocument.m. I eventually
reinstall Mac OS X and Xcode.
- 03/16/04: Rewrote PreferenceController.m, and it's better than ever.
- 03/31/04: Added choice of new document format
- 03/31/04: Changed the reset alert to a sheet
- 03/31/04: Made all strings localized
- 04/05/04: Added choice of font and font size
- 04/08/04: Removed all of the debugging stuff, and cleaned up the code
- 04/26/04: Completed CocoaPad 1.0b1 beta Release 1
- 06/18/04: Completed CocoaPad 1.0b2 beta Release 2
- 09/21/04: Added toolbar preference and notifications
- 11/16/04: Removed irritating notification code, made an Apply To All button.
- 01/31/05: Removed debugging stuff, cleaned up the code
- 02/02/05: Fixed reset preferences, which didn't have Show Toolbar
- 02/02/05: Finished CocoaPad 1.0 pre-release
- 03/30/05: Added Automatic Backup support
- 05/16/05: Added “smart quotes”
- 05/23/05: Added word count
- 06/07/05: FINALLY COMPLETED COCOAPAD 1.0!!!
- 10/14/07: Released CocoaPad v1.1, with 2 bug fixes!
*/
#import "PreferenceController.h"
#import "MyDocument.h"
#import "InterfaceController.h"
#import "LocalizedStrings.h"
/*************/
/* Constants */
/*************/
// These are the tags for the default format checkboxes
enum FORMAT { CPDFORMAT = 0, RTFFORMAT = 1, RTFDFORMAT = 2, DOCFORMAT = 3, TXTFORMAT = 4 };
@implementation PreferenceController
/**************************/
/* Initialization methods */
/**************************/
- (id)init
{
if (self = [super initWithWindowNibName:@"Preferences"])
{
[self setWindowFrameAutosaveName:@"PreferencePanelLocation"]; // Keep the location of preference panel
fontText = L_FORMATTING_STRING; // The format string used to display the font
[[NSFontManager sharedFontManager] setDelegate:self]; // Set ourselves as the font manager's delegate
}
return self;
}
/***** Initialize the preference panel *****/
- (void)windowDidLoad
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; // Load the preferences;
NSData *colorAsData, *textColorAsData;
NSFont *rtfFont, *textFont;
BOOL supportsWordFormat = [[[NSDocumentController sharedDocumentController] currentDocument] supportsWordFormat];
int cell = 0;
//
// Extract some preferences that aren't regular booleans or strings
//
colorAsData = [prefs objectForKey:CP_BackgroundColor];
textColorAsData = [prefs objectForKey:CP_TextColor];
rtfFont = [NSFont userFontOfSize:0.0];
textFont = [NSFont userFixedPitchFontOfSize:0.0];
//
// Now set the pref window's controls to what they're supposed to be
//
// The two color wells
[textColorWell setColor:[NSUnarchiver unarchiveObjectWithData:textColorAsData]];
[colorWell setColor:[NSUnarchiver unarchiveObjectWithData:colorAsData]];
// The checkboxes
[showRulerCheckBox setState:[prefs boolForKey:CP_ShowRuler]];
[continuousSpellCheck setState:[prefs boolForKey:CP_SpellChecking]];
[showToolbarCheckBox setState:[prefs boolForKey:CP_ShowToolbar]];
[ignoreFormattingCheckBox setState:[prefs boolForKey:CP_IgnoreFormatting]];
[enableSmartQuotesCheckBox setState:[prefs boolForKey:CP_EnableSmartQuotes]];
[openNewDoc setState:[prefs boolForKey:CP_OpenNewDoc]];
[saveBackupCheckBox setState:[prefs boolForKey:CP_SaveBackup]];
[saveBackupInterval setIntValue:[prefs integerForKey:CP_SaveBackupInterval]];
// The font display
[richTextFontNameField setStringValue:[NSString stringWithFormat:fontText, [rtfFont displayName], [rtfFont pointSize]]];
[plainTextFontNameField setStringValue:[NSString stringWithFormat:fontText, [textFont displayName], [textFont pointSize]]];
// Just in case the OS doesn't support Word...
[[formatCheckboxes cellWithTag:DOCFORMAT] setEnabled:supportsWordFormat];
// Set the default cell for documat format (switch doesn't work here, because we don't have integers)
if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_CPDFormat])
{
cell = CPDFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_RTFFormat])
{
cell = RTFFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_RTFDFormat])
{
cell = RTFDFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_TextFormat])
{
cell = TXTFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_WordFormat] && supportsWordFormat)
{
cell = DOCFORMAT;
}
[formatCheckboxes selectCellWithTag:cell]; // Actually select the checkbox
}
/**************************/
/* Toggle the preferences */
/**************************/
/***** Toggle ruler visibility *****/
- (IBAction)changeShowRuler:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_ShowRuler];
}
/***** Spell check as you type? *****/
- (IBAction)changeContinuousSpellCheck:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_SpellChecking];
}
/***** Show the toolbar? *****/
- (IBAction)changeShowToolbar:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_ShowToolbar];
}
/***** Ignore formatting commands? *****/
- (IBAction)changeIgnoreFormatting:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_IgnoreFormatting];
}
/***** Enable “smart quotes?” *****/
- (IBAction)changeEnableSmartQuotes:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_EnableSmartQuotes];
}
/***** Save a backup file? *****/
- (IBAction)changeSaveBackup:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_SaveBackup];
// Enable/disable the text field
[saveBackupInterval setEnabled:[sender state]];
}
/***** Set the document background color *****/
- (IBAction)changeBackgroundColor:(id)sender
{
// Since XML property lists don't know how to store color objects, we must encode the color object
NSColor *color = [sender color]; // Get what the color chosen it
NSData *colorAsData = [NSArchiver archivedDataWithRootObject:color]; // Now encode it
[[NSUserDefaults standardUserDefaults] setObject:colorAsData forKey:CP_BackgroundColor];
}
/***** Set the default text color *****/
- (IBAction)changeTextColor:(id)sender
{
NSColor *color = [sender color];
NSData *colorAsData = [NSArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorAsData forKey:CP_TextColor];
}
/***** Set if we should open a new document on startup *****/
- (IBAction)changeNewEmptyDoc:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:CP_OpenNewDoc];
}
/***** Change the rich text/CPD default font *****/
- (IBAction)changeRTFFont:(id)sender
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[[self window] makeFirstResponder:[self window]]; // Make sure the font panel ONLY applies to US
[fontManager setSelectedFont:[NSFont userFontOfSize:0.0] isMultiple:NO];
[fontManager orderFrontFontPanel:self]; // Show the font panel
changingRTFFont = YES; // We need to know if this is the RTF font or not
}
/***** Change the plain text font *****/
- (IBAction)changeTextFont:(id)sender
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[[self window] makeFirstResponder:[self window]]; // Make sure the font panel ONLY applies to US
[fontManager setSelectedFont:[NSFont userFixedPitchFontOfSize:0.0] isMultiple:NO];
[fontManager orderFrontFontPanel:self]; // Show the font panel
changingRTFFont = NO; // We need to know if this is the RTF font or not
}
/***** Get the font changes *****/
/*
* This is implemented by the font manager's delegate. Previously,
* we set ourselves as the delegate. So, when the font changes, the
* delegate's changeFont: method is called.
*/
- (void)changeFont:(id)sender
{
NSFont *oldFont, *newFont;
// Get the fonts
oldFont = (changingRTFFont) ? [NSFont userFontOfSize:0.0] : [NSFont userFixedPitchFontOfSize:0.0];
newFont = [sender convertFont:oldFont]; // Convert the font to an NSFont -- just to make sure
// Set the RTF font
if (changingRTFFont)
{
[NSFont setUserFont:newFont];
[richTextFontNameField setStringValue:[NSString stringWithFormat:fontText, [newFont displayName], [newFont pointSize]]];
}
// Otherwise, set the plain text font
else
{
[NSFont setUserFixedPitchFont:newFont];
[plainTextFontNameField setStringValue:[NSString stringWithFormat:fontText, [newFont displayName], [newFont pointSize]]];
}
}
/***** Change the default document format *****/
- (IBAction)changeFormat:(id)sender
{
int choice = [[sender selectedCell] tag]; // Get the selected radio button
// Find out which format was selected
switch (choice)
{
case CPDFORMAT:
[[NSUserDefaults standardUserDefaults] setObject:CP_CPDFormat forKey:CP_NewDocFormat];
break;
case RTFFORMAT:
[[NSUserDefaults standardUserDefaults] setObject:CP_RTFFormat forKey:CP_NewDocFormat];
break;
case RTFDFORMAT:
[[NSUserDefaults standardUserDefaults] setObject:CP_RTFDFormat forKey:CP_NewDocFormat];
break;
case TXTFORMAT:
[[NSUserDefaults standardUserDefaults] setObject:CP_TextFormat forKey:CP_NewDocFormat];
break;
// We disabled the checkbox if the OS is not Panther
case DOCFORMAT:
[[NSUserDefaults standardUserDefaults] setObject:CP_WordFormat forKey:CP_NewDocFormat];
break;
}
}
/***** Change how often we should save a backup *****/
- (IBAction)changeBackupInterval:(id)sender
{
if ([sender isEnabled])
{
[[NSUserDefaults standardUserDefaults] setInteger:[sender intValue] forKey:CP_SaveBackupInterval];
// Notify InterfaceController that the backup interval changed
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BackupIntervalDidChange" object:nil]];
}
}
/***** Same as above, except this is for when the text field loses focus *****/
- (void)textDidChange:(NSNotification *)notification
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:CP_SaveBackup] && [saveBackupInterval isEnabled])
{
[[NSUserDefaults standardUserDefaults] setInteger:[saveBackupInterval intValue] forKey:CP_SaveBackupInterval];
// Notify InterfaceController that the backup interval changed
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BackupIntervalDidChange" object:nil]];
}
}
/***** Same as above, except this is for when the prefs panel loses focus *****/
- (void)windowDidResignKey:(NSNotification *)notification
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:CP_SaveBackup] && [saveBackupInterval isEnabled])
{
[[NSUserDefaults standardUserDefaults] setInteger:[saveBackupInterval intValue] forKey:CP_SaveBackupInterval];
// Notify InterfaceController that the backup interval changed
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BackupIntervalDidChange" object:nil]];
}
}
/***** Revert to the default settings *****/
- (IBAction)revertToOriginal:(id)sender
{
SEL selector = @selector(sheetDidEnd:returnCode:contextInfo:); // The sheet handler
// Run an alert sheet to see if they really want to revert to defaults
NSBeginAlertSheet(L_RESET_PREFS_SHEET_TITLE, L_RESET_BUTTON, L_CANCEL_BUTTON, nil, [self window], self, nil, selector, nil, L_RESET_PREFS_SHEET_DESCRIPTION, nil);
}
/***** Handle post-sheet stuff *****/
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)result contextInfo:(void *)contextInfo
{
if (result == NSAlertDefaultReturn)
{
[self resetPreferences]; // They clicked OK, so go ahead and reset the preferences
}
[self showWindow:nil]; // Make sure the window is still visible
}
/***** Apply settings to all open documents *****/
/*
* This is much faster -- and much more reliable --
* than the notification system, which we used to use.
*/
- (IBAction)applyToAll:(id)sender
{
SEL selector = @selector(applySheetDidEnd:returnCode:contextInfo:); // The sheet handler
// Run an alert sheet to see if they really want to apply to all
NSBeginAlertSheet(L_APPLY_TO_ALL_SHEET_TITLE, L_APPLY_TO_ALL_BUTTON, L_CANCEL_BUTTON, nil, [self window], self, nil, selector, nil, L_APPLY_TO_ALL_SHEET_DESCRIPTION, nil);
}
- (void)applySheetDidEnd:(NSWindow *)sheet returnCode:(int)result contextInfo:(void *)contextInfo
{
if (result == NSAlertDefaultReturn)
{
// Run through all the documents and apply changes
NSEnumerator *documentList = [[[NSDocumentController sharedDocumentController] documents] objectEnumerator];
MyDocument *document;
NSTextView *textView;
while (document = [documentList nextObject])
{
textView = [document textView];
// Set the background color
[textView setBackgroundColor:[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:CP_BackgroundColor]]];
// Set the ruler's visibility
[textView setRulerVisible:[[NSUserDefaults standardUserDefaults] boolForKey:CP_ShowRuler]];
// Set continuous spell checking enabled or not
[textView setContinuousSpellCheckingEnabled:[[NSUserDefaults standardUserDefaults] boolForKey:CP_SpellChecking]];
// Set toolbar's visibility
[[[textView window] toolbar] setVisible:[[NSUserDefaults standardUserDefaults] boolForKey:CP_ShowToolbar]];
// And update the text view
[document updateView];
}
}
[self showWindow:nil]; // Make sure the window is still visible
}
/***** Remove the changed preferences *****/
- (void)resetPreferences
{
NSData *colorAsData, *textColorAsData;
NSFont *rtfFont, *textFont;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
int cell = 0;
/*
* The user gave us the go ahead to erase all of the preference settings.
* This just involves deleting the stuff that changed so it will revert
* to the factory defaults.
*/
[prefs removeObjectForKey:CP_ShowRuler];
[prefs removeObjectForKey:CP_SpellChecking];
[prefs removeObjectForKey:CP_BackgroundColor];
[prefs removeObjectForKey:CP_TextColor];
[prefs removeObjectForKey:CP_ShowToolbar];
[prefs removeObjectForKey:CP_IgnoreFormatting];
[prefs removeObjectForKey:CP_EnableSmartQuotes];
[prefs removeObjectForKey:CP_OpenNewDoc];
[prefs removeObjectForKey:CP_NewDocFormat];
[prefs removeObjectForKey:CP_SaveBackup];
[prefs removeObjectForKey:CP_SaveBackupInterval];
[NSFont setUserFont:[NSFont fontWithName:CP_RTFFont size:12.0]];
[NSFont setUserFixedPitchFont:[NSFont fontWithName:CP_TextFont size:10.0]];
// Extract some preferences that aren't regular booleans or strings
colorAsData = [prefs objectForKey:CP_BackgroundColor];
textColorAsData = [prefs objectForKey:CP_TextColor];
rtfFont = [[NSFontManager sharedFontManager] convertFont:[NSFont userFontOfSize:0.0]];
textFont = [[NSFontManager sharedFontManager] convertFont:[NSFont userFixedPitchFontOfSize:0.0]];
// Now update the preference panel to reflect their new settings
[colorWell setColor:[NSUnarchiver unarchiveObjectWithData:colorAsData]];
[textColorWell setColor:[NSUnarchiver unarchiveObjectWithData:textColorAsData]];
[showRulerCheckBox setState:[prefs boolForKey:CP_ShowRuler]];
[continuousSpellCheck setState:[prefs boolForKey:CP_SpellChecking]];
[showToolbarCheckBox setState:[prefs boolForKey:CP_ShowToolbar]];
[ignoreFormattingCheckBox setState:[prefs boolForKey:CP_IgnoreFormatting]];
[enableSmartQuotesCheckBox setState:[prefs boolForKey:CP_EnableSmartQuotes]];
[openNewDoc setState:[prefs boolForKey:CP_OpenNewDoc]];
[saveBackupCheckBox setState:[prefs boolForKey:CP_SaveBackup]];
[saveBackupInterval setIntValue:[prefs integerForKey:CP_SaveBackupInterval]];
// Update the fonts
[richTextFontNameField setStringValue:[NSString stringWithFormat:fontText, [rtfFont displayName], [rtfFont pointSize]]];
[plainTextFontNameField setStringValue:[NSString stringWithFormat:fontText, [textFont displayName], [textFont pointSize]]];
// Set the format checkboxes
if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_CPDFormat])
{
cell = CPDFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_RTFFormat])
{
cell = RTFFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_RTFDFormat])
{
cell = RTFDFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_TextFormat])
{
cell = TXTFORMAT;
}
else if ([[prefs stringForKey:CP_NewDocFormat] isEqualToString:CP_WordFormat])
{
cell = DOCFORMAT;
}
[formatCheckboxes selectCellWithTag:cell];
}
@end