/
PrefsWindowController.m
executable file
·461 lines (354 loc) · 16.8 KB
/
PrefsWindowController.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
#import "PrefsWindowController.h"
#import "PTKeyComboPanel.h"
#import "PTKeyCombo.h"
#import "NotationPrefsViewController.h"
#import "NSData_transformations.h"
#import "NSString_NV.h"
#import "GlobalPrefs.h"
@implementation PrefsWindowController
- (id)init {
if ([super init]) {
notationPrefsViewController = [[NotationPrefsViewController alloc] init];
prefsController = [GlobalPrefs defaultPrefs];
fontPanelWasOpen = NO;
EditingPref = NSLocalizedString(@"Editing", @"title of Editing preference pane");
GeneralPref = NSLocalizedString(@"General", @"title of General preference pane");
NotesPref = NSLocalizedString(@"Notes", @"title of Notes preference pane");
[prefsController registerForSettingChange:@selector(resolveNoteBodyFontFromNotationPrefsFromSender:) withTarget:self];
[prefsController registerForSettingChange:@selector(setCheckSpellingAsYouType:sender:) withTarget:self];
}
return self;
}
- (void)showWindow:(id)sender {
if (!window) {
if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
NSLog(@"Failed to load Preferences.nib");
return;
}
}
if (![window isVisible])
[window center];
[window makeKeyAndOrderFront:self];
}
- (void)windowWillClose:(NSNotification *)aNotification {
[prefsController performSelector:@selector(synchronize) withObject:nil afterDelay:0.0];
[[NSFontPanel sharedFontPanel] close];
}
- (void)windowDidResignKey:(NSNotification *)aNotification {
//hide the font panel--don't want to confuse people into thinking it will affect some other part of the program
fontPanelWasOpen = [[NSFontPanel sharedFontPanel] isVisible];
[[NSFontPanel sharedFontPanel] orderOut:nil];
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification {
if (fontPanelWasOpen) {
[self changeBodyFont:self];
}
}
- (void)menuNeedsUpdate:(NSMenu *)menu {
NSLog(@"I need an update: %@", [menu description]);
}
- (IBAction)setAppShortcut:(id)sender {
[[PTKeyComboPanel sharedPanel] showSheetForHotkey:[prefsController appActivationHotKey] forWindow:window modalDelegate:self];
}
- (void)keyComboPanelEnded:(PTKeyComboPanel*)panel {
PTKeyCombo *oldKeyCombo = [[prefsController appActivationKeyCombo] retain];
[prefsController setAppActivationKeyCombo:[panel keyCombo] sender:self];
[appShortcutField setStringValue:[[prefsController appActivationKeyCombo] description]];
if (![prefsController registerAppActivationKeystrokeWithTarget:[NSApp delegate] selector:@selector(bringFocusToControlField:)]) {
[prefsController setAppActivationKeyCombo:oldKeyCombo sender:self];
NSLog(@"reverting to old (hopefully working key combo");
}
[oldKeyCombo release];
}
- (IBAction)changeBodyFont:(id)sender {
[[NSFontManager sharedFontManager] setSelectedFont:[prefsController noteBodyFont] isMultiple:NO];
[[NSFontManager sharedFontManager] orderFrontFontPanel:self];
}
- (void)changeFont:(id)sender {
NSFontManager *fontMan = [NSFontManager sharedFontManager];
NSFont *panelFont = [fontMan convertFont:[fontMan selectedFont]];
if (/*![fontMan fontNamed:[panelFont fontName] hasTraits:NSUnboldFontMask | NSUnitalicFontMask]*/
([fontMan traitsOfFont:panelFont] & NSItalicFontMask) == NSItalicFontMask ||
([fontMan traitsOfFont:panelFont] & NSBoldFontMask) == NSBoldFontMask) {
//revert the font--using a bold or italic variant as the default could cause some notes to lose styles
// NSLog(@"traits: %u", [fontMan traitsOfFont:panelFont]);
[self performSelector:@selector(changeBodyFont:) withObject:sender afterDelay:0.0];
NSBeep();
} else {
[prefsController setNoteBodyFont:panelFont sender:self];
[self previewNoteBodyFont];
}
}
- (NSUInteger)validModesForFontPanel:(NSFontPanel *)fontPanel {
return NSFontPanelSizeModeMask | NSFontPanelCollectionModeMask;
}
- (void)previewNoteBodyFont {
if (!centerStyle) {
centerStyle = [[NSMutableParagraphStyle alloc] init];
[centerStyle setAlignment:NSCenterTextAlignment];
}
NSFont *font = [prefsController noteBodyFont];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font ? font : [NSFont systemFontOfSize:12.0],
NSFontAttributeName, [NSColor blackColor], NSForegroundColorAttributeName, centerStyle, NSParagraphStyleAttributeName, nil];
NSString *fontNameAndSize = font ? [NSString stringWithFormat:@"%@ %g", [font fontName], [font pointSize]] : @"Unknown";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:fontNameAndSize attributes:attributes];
[[bodyTextFontField cell] setAttributedStringValue:attributedString];
[bodyTextFontField updateCell:[bodyTextFontField cell]];
[attributedString autorelease];
}
- (IBAction)changedSearchHighlightColorWell:(id)sender {
[prefsController setSearchTermHighlightColor:[searchHighlightColorWell color] sender:self];
}
- (IBAction)changedStyledTextBehavior:(id)sender {
[prefsController setPastePreservesStyle:[styledTextButton state] sender:self];
}
- (IBAction)changedAutoSuggestLinks:(id)sender {
[prefsController setLinksAutoSuggested:[autoSuggestLinksButton state] sender:self];
}
- (IBAction)changedMakeURLsClickable:(id)sender {
[prefsController setMakeURLsClickable:[makeURLsClickable state] sender:self];
}
- (IBAction)changedNoteDeletion:(id)sender {
[prefsController setConfirmNoteDeletion:[confirmDeletionButton state] sender:self];
}
- (IBAction)changedNotesFolderLocation:(id)sender {
NSLog(@"Changed notes folder menu");
}
- (IBAction)changedQuitBehavior:(id)sender {
[prefsController setQuitWhenClosingWindow:[quitWhenClosingButton state] sender:self];
}
- (IBAction)changedSpellChecking:(id)sender {
[prefsController setCheckSpellingAsYouType:[checkSpellingButton state] sender:self];
}
- (IBAction)changedTabBehavior:(id)sender {
if (sender != self)
[self performSelector:@selector(changedTabBehavior:) withObject:self afterDelay:0.0];
else
[prefsController setTabIndenting:[[tabKeyRadioMatrix cellAtRow:0 column:0] state] sender:self];
}
- (IBAction)changedTableText:(id)sender {
if (sender == tableTextMenuButton) {
if ([tableTextSizeField selectedTag] != 3) [tableTextSizeField setFloatValue:[prefsController tableFontSize]];
[self performSelector:@selector(changedTableText:) withObject:nil afterDelay:0.0];
} else {
[window makeFirstResponder:window];
float newFontSize = 0.0;
switch ([tableTextMenuButton selectedTag]) {
case 1:
newFontSize = [NSFont smallSystemFontSize];
break;
case 2:
newFontSize = [NSFont systemFontSize];
break;
case 3:
newFontSize = [tableTextSizeField floatValue];
}
[tableTextSizeField setHidden:([tableTextMenuButton selectedTag] != 3)];
if (![tableTextSizeField isHidden])
[tableTextSizeField selectText:sender];
[prefsController setTableFontSize:newFontSize sender:self];
}
}
- (IBAction)changedTitleCompletion:(id)sender {
[prefsController setAutoCompleteSearches:[completeNoteTitlesButton state] sender:self];
}
- (IBAction)changedSoftTabs:(id)sender {
[prefsController setSoftTabs:[softTabsButton state] sender:self];
}
- (void)settingChangedForSelectorString:(NSString*)selectorString {
if ([selectorString isEqualToString:SEL_STR(resolveNoteBodyFontFromNotationPrefsFromSender:)]) {
[self previewNoteBodyFont];
} else if ([selectorString isEqualToString:SEL_STR(setCheckSpellingAsYouType:sender:)]) {
[checkSpellingButton setState:[prefsController checkSpellingAsYouType]];
}
}
- (NSMenu*)directorySelectionMenu {
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"Note Directory Menu"] autorelease];
FSRef targetRef = {{0}};
NSString *name = [prefsController displayNameForDefaultDirectoryWithFSRef:&targetRef];
if (!name)
name = NSLocalizedString(@"<Directory unknown>", nil);
NSImage *iconImage = [prefsController iconForDefaultDirectoryWithFSRef:&targetRef];
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:name action:nil keyEquivalent:@""] autorelease];
if (iconImage)
[theMenuItem setImage:iconImage];
[theMenu addItem:theMenuItem];
[theMenu addItem:[NSMenuItem separatorItem]];
theMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Other...", @"title of menu item for selecting a different notes folder")
action:@selector(changeDefaultDirectory) keyEquivalent:@""] autorelease];
[theMenuItem setTarget:self];
[theMenu addItem:theMenuItem];
return theMenu;
}
- (void)changeDefaultDirectory {
FSRef notesDirectoryRef;
NSData *aliasData = nil;
if ([self getNewNotesRefFromOpenPanel:¬esDirectoryRef returnedPath:nil]) {
//make sure we're not choosing the same folder as what we started with, because:
//-[NotationController initWithAliasData:] might attempt to initialize journaling, which will already be in use
FSRef currentNotesDirectoryRef;
[[prefsController aliasDataForDefaultDirectory] fsRefAsAlias:¤tNotesDirectoryRef];
if (FSCompareFSRefs(¬esDirectoryRef, ¤tNotesDirectoryRef) != noErr) {
if ((aliasData = [NSData aliasDataForFSRef:¬esDirectoryRef]))
[prefsController setAliasDataForDefaultDirectory:aliasData sender:self];
} else {
NSLog(@"This folder is already chosen!");
}
}
[folderLocationsMenuButton setMenu:[self directorySelectionMenu]];
if ([folderLocationsMenuButton numberOfItems] > 0)
[folderLocationsMenuButton selectItemAtIndex:0];
}
- (BOOL)getNewNotesRefFromOpenPanel:(FSRef*)notesDirectoryRef returnedPath:(NSString**)path {
NSString *startingDirectory = nil;
if (!notesDirectoryRef) {
NSLog(@"notesDirectoryRef is NULL!");
return NO;
}
FSRef currentNotesDirectoryRef;
//resolve alias to fsref; get path from fsref
if ([[prefsController aliasDataForDefaultDirectory] fsRefAsAlias:¤tNotesDirectoryRef]) {
NSString *resolvedPath = [NSString pathWithFSRef:¤tNotesDirectoryRef];
if (resolvedPath) startingDirectory = resolvedPath;
}
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanCreateDirectories:YES];
[openPanel setCanChooseFiles:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setResolvesAliases:YES];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setTreatsFilePackagesAsDirectories:NO];
[openPanel setTitle:NSLocalizedString(@"Select a folder",@"title of open panel for selecting a notes folder")];
[openPanel setPrompt:NSLocalizedString(@"Select", @"title of open panel button to select a folder")];
[openPanel setMessage:NSLocalizedString(@"Select the folder that Notational Velocity should use for reading and storing notes.",nil)];
if ([openPanel runModalForDirectory:startingDirectory file:@"Notational Data" types:nil] == NSOKButton) {
CFStringRef filename = (CFStringRef)[openPanel filename];
if (!filename)
return NO;
if (path)
*path = [[[openPanel filename] copy] autorelease];
//yes, I know that navigation services uses uses FSRefs, but NSSavePanel saves us much more work
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filename, kCFURLPOSIXPathStyle, true);
[(id)url autorelease];
if (!url || !CFURLGetFSRef(url, notesDirectoryRef))
return NO;
return YES;
}
return NO;
}
- (NSView*)databaseView {
if (![notationPrefsView subviews] || ![[notationPrefsView subviews] count])
[notationPrefsView addSubview:[notationPrefsViewController view]];
return databaseView;
}
- (void)addToolbarItemWithName:(NSString*)name {
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:name];
[item setPaletteLabel:name];
[item setLabel:name];
//[item setToolTip:@"General settings: appearance and behavior"];
[item setImage:[[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"tiff"]] autorelease]];
[item setTarget:self];
[item setAction:@selector(switchViews:)];
[items setObject:item forKey:name];
[item release];
}
- (void)awakeFromNib {
[window setDelegate:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changedTableText:)
name:NSControlTextDidEndEditingNotification object:tableTextSizeField];
[tabKeyRadioMatrix setState:[prefsController tabKeyIndents] atRow:0 column:0];
[tabKeyRadioMatrix setState:![prefsController tabKeyIndents] atRow:1 column:0];
float fontSize = [prefsController tableFontSize];
int fontButtonIndex = 3;
if (fontSize == [NSFont smallSystemFontSize]) fontButtonIndex = 0;
else if (fontSize == [NSFont systemFontSize]) fontButtonIndex = 1;
[tableTextMenuButton selectItemAtIndex:fontButtonIndex];
[tableTextSizeField setFloatValue:fontSize];
[tableTextSizeField setHidden:(fontButtonIndex != 3)];
[completeNoteTitlesButton setState:[prefsController autoCompleteSearches]];
[checkSpellingButton setState:[prefsController checkSpellingAsYouType]];
[confirmDeletionButton setState:[prefsController confirmNoteDeletion]];
[quitWhenClosingButton setState:[prefsController quitWhenClosingWindow]];
[styledTextButton setState:[prefsController pastePreservesStyle]];
[autoSuggestLinksButton setState:[prefsController linksAutoSuggested]];
[softTabsButton setState:[prefsController softTabs]];
[makeURLsClickable setState:[prefsController URLsAreClickable]];
[searchHighlightColorWell setColor:[prefsController searchTermHighlightColor]];
[self previewNoteBodyFont];
[appShortcutField setStringValue:[[prefsController appActivationKeyCombo] description]];
items = [[NSMutableDictionary alloc] init];
[self addToolbarItemWithName:GeneralPref];
[self addToolbarItemWithName:NotesPref];
[self addToolbarItemWithName:EditingPref];
toolbar = [[NSToolbar alloc] initWithIdentifier:@"preferencePanes"];
[toolbar setDelegate:self];
[toolbar setAllowsUserCustomization:NO];
[toolbar setAutosavesConfiguration:NO];
[window setToolbar:toolbar];
[toolbar release]; //setToolbar retains the toolbar we pass, so release the one we used.
[[window standardWindowButton:NSWindowToolbarButton] setFrame:NSZeroRect];
[self switchViews:nil]; //select last selected pane by default
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
return [items objectForKey:itemIdentifier];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)theToolbar {
return [self toolbarDefaultItemIdentifiers:theToolbar];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)theToolbar {
return [NSArray arrayWithObjects:GeneralPref, NotesPref, EditingPref, nil];
}
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar {
//make all of them selectable. This puts that little grey outline thing around an item when you select it.
return [items allKeys];
}
- (void)switchViews:(NSToolbarItem *)item {
NSString *sender = nil;
if (item == nil) {
sender = [prefsController lastSelectedPreferencesPane];
[toolbar setSelectedItemIdentifier:sender];
} else {
sender = [item label];
[prefsController setLastSelectedPreferencesPane:sender sender:self];
}
NSView *prefsView = nil;
[window setTitle:sender];
if ([sender isEqualToString:GeneralPref]){
prefsView = generalView;
} else if([sender isEqualToString:NotesPref]) {
prefsView = [self databaseView];
} else if([sender isEqualToString:EditingPref]) {
prefsView = editingView;
}
if (prefsView == databaseView)
[folderLocationsMenuButton setMenu:[self directorySelectionMenu]];
NSAssert(prefsView != nil, @"switching to a nil prefs view!");
[[NSFontPanel sharedFontPanel] close];
//fix this math to convert between window and view coordinates for resolution independence
float userSpaceScaleFactor = 1.0;
if (RunningTigerAppKitOrHigher) // on 10.4
userSpaceScaleFactor = [window userSpaceScaleFactor];
//to stop flicker, we make a temp blank view.
NSRect windowContentFrame = ScaleRectWithFactor([[window contentView] frame], userSpaceScaleFactor);
NSView *tempView = [[NSView alloc] initWithFrame:[[window contentView] frame]];
[window setContentView:tempView];
[tempView release];
NSRect newFrame = [window frame];
NSRect viewFrameForWindow = ScaleRectWithFactor([prefsView frame], userSpaceScaleFactor);
newFrame.size.height = viewFrameForWindow.size.height + ([window frame].size.height - windowContentFrame.size.height);
newFrame.size.width = viewFrameForWindow.size.width;
newFrame.origin.y += (windowContentFrame.size.height - viewFrameForWindow.size.height);
[window setShowsResizeIndicator:YES];
[window setFrame:newFrame display:YES animate:YES];
[window setContentView:prefsView];
}
NSRect ScaleRectWithFactor(NSRect rect, float factor) {
NSRect newRect = rect;
newRect.size.width *= factor;
newRect.size.height *= factor;
newRect.origin.x *= factor;
newRect.origin.y *= factor;
//these may still need to be rounded up
return newRect;
}
@end