-
Notifications
You must be signed in to change notification settings - Fork 2
/
PreferencesController.m
354 lines (277 loc) · 12.9 KB
/
PreferencesController.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
//---------------------------------------------------------------------------------------
// PreferencesController.m created by erik on Sat Feb 01 2003
// @(#)$Id: PreferencesController.m,v 1.12 2004-02-15 18:55:05 erik Exp $
//
// Copyright (c) 2003 by Mulle Kybernetik. All rights reserved.
//
// Permission to use, copy, modify and distribute this software and its documentation
// is hereby granted, provided that both the copyright notice and this permission
// notice appear in all copies of the software, derivative works or modified versions,
// and any portions thereof, and that both notices appear in supporting documentation,
// and that credit is given to Mulle Kybernetik in all documents and publicity
// pertaining to direct or indirect use of this code or its derivatives.
//
// THIS IS EXPERIMENTAL SOFTWARE AND IT IS KNOWN TO HAVE BUGS, SOME OF WHICH MAY HAVE
// SERIOUS CONSEQUENCES. THE COPYRIGHT HOLDER ALLOWS FREE USE OF THIS SOFTWARE IN ITS
// "AS IS" CONDITION. THE COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY
// DAMAGES WHATSOEVER RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE
// OR OF ANY DERIVATIVE WORK.
//---------------------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
#import "NSColor+Extensions.h"
#import "AppController.h"
#import "WindowManager.h"
#import "MKConsoleWindowController.h"
#import "PreferencesController.h"
@interface PreferencesController(PrivateAPI)
- (WindowManager *)_getWindowManager;
- (MKConsoleWindowController *)_getFirstConsoleWindowController;
- (void)_showSettings:(NSDictionary *)settings;
- (NSDictionary *)_getSettings;
@end
//---------------------------------------------------------------------------------------
@implementation PreferencesController
//---------------------------------------------------------------------------------------
static PreferencesController *sharedInstance = nil;
//---------------------------------------------------------------------------------------
// FACTORY
//---------------------------------------------------------------------------------------
+ (id)sharedInstance
{
if(sharedInstance == nil)
sharedInstance = [[PreferencesController alloc] init];
return sharedInstance;
}
//---------------------------------------------------------------------------------------
// WINDOW MANAGEMENT
//---------------------------------------------------------------------------------------
- (IBAction)showWindow:(id)sender
{
if(panel == nil)
{
[NSBundle loadNibNamed:@"Preferences" owner:self];
NSAssert(panel != nil, @"Problem with Preferences.nib");
[self _showSettings:[[[NSUserDefaults standardUserDefaults] objectForKey:@"Windows"] objectAtIndex:0]];
[[self _getFirstConsoleWindowController] enterSetUpModeWithListener:self];
}
[panel makeKeyAndOrderFront:self];
}
- (void)windowWillClose:(NSNotification *)notification
{
[[self _getFirstConsoleWindowController] leaveSetUpMode];
panel = nil;
}
- (void)awakeFromNib
{
NSArray *familyList;
NSEnumerator *familyEnum;
NSString *family;
if(panel == nil)
return;
[fontFamilyPopup removeAllItems];
familyList = [[[NSFontManager sharedFontManager] availableFontFamilies] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
familyEnum = [familyList objectEnumerator];
while((family = [familyEnum nextObject]) != nil)
{
if(([family hasPrefix:@"."] == NO) && ([family hasPrefix:@"#"] == NO))
[fontFamilyPopup addItemWithTitle:family];
}
[fileTableView registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
//---------------------------------------------------------------------------------------
// ACCESSING THE LOG WINDOW
//---------------------------------------------------------------------------------------
- (WindowManager *)_getWindowManager
{
return [(AppController *)[[NSApplication sharedApplication] delegate] windowManager];
}
- (MKConsoleWindowController *)_getFirstConsoleWindowController
{
NSArray *windowControllerList;
windowControllerList = [[self _getWindowManager] windowControllerList];
return [windowControllerList count] > 0 ? [windowControllerList objectAtIndex:0] : nil;
}
//---------------------------------------------------------------------------------------
// READING/WRITING DEFAULTS
//---------------------------------------------------------------------------------------
- (void)_showSettings:(NSDictionary *)settings
{
NSRect frame;
NSString *fontname;
frame = NSRectFromString([settings objectForKey:@"Frame"]);
[frameXField setFloatValue:frame.origin.x];
[frameYField setFloatValue:frame.origin.y];
[frameWField setFloatValue:frame.size.width];
[frameHField setFloatValue:frame.size.height];
[textColorWell setColor:[NSColor colorWithCalibratedStringRep:[settings objectForKey:@"TextColor"]]];
[backgroundColorWell setColor:[NSColor colorWithCalibratedStringRep:[settings objectForKey:@"BackgroundColor"]]];
[floatCheckBox setState:[[settings objectForKey:@"Float"] isEqualToString:@"Yes"]];
[exposeStickCheckBox setState:[[settings objectForKey:@"Sticky"] isEqualToString:@"Yes"]];
filenames = [[settings objectForKey:@"Files"] mutableCopy];
[fileTableView reloadData];
fontname = [settings objectForKey:@"FontName"];
[fontFamilyPopup selectItemWithTitle:[[NSFont fontWithName:fontname size:12] familyName]];
[fontSizePopup selectItemWithTitle:[settings objectForKey:@"FontSize"]];
[boldCheckBox setState:[[NSFontManager sharedFontManager] fontNamed:fontname hasTraits:NSBoldFontMask]];
[italicCheckBox setState:[[NSFontManager sharedFontManager] fontNamed:fontname hasTraits:NSItalicFontMask]];
[antialiasCheckBox setState:[[settings objectForKey:@"AntiAlias"] isEqualToString:@"No"] == NO];
}
- (NSDictionary *)_getSettings
{
NSMutableDictionary *settings;
NSRect frame;
NSFont *font;
settings = [NSMutableDictionary dictionary];
frame.origin.x = [frameXField floatValue];
frame.origin.y = [frameYField floatValue];
frame.size.width = [frameWField floatValue];
frame.size.height = [frameHField floatValue];
[settings setObject:NSStringFromRect(frame) forKey:@"Frame"];
[settings setObject:[[textColorWell color] stringRep] forKey:@"TextColor"];
[settings setObject:[[backgroundColorWell color] stringRep] forKey:@"BackgroundColor"];
[settings setObject:([floatCheckBox state] == NSOnState) ? @"Yes" : @"No" forKey:@"Float"];
[settings setObject:([exposeStickCheckBox state] == NSOnState) ? @"Yes" : @"No" forKey:@"Sticky"];
[settings setObject:filenames forKey:@"Files"];
font = [[NSFontManager sharedFontManager] fontWithFamily:[fontFamilyPopup titleOfSelectedItem] traits:0 weight:5 size:[[fontSizePopup titleOfSelectedItem] floatValue]];
if([boldCheckBox state] == NSOnState)
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSBoldFontMask];
if([italicCheckBox state] == NSOnState)
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSItalicFontMask];
NSLog(@"%s font = %@", __PRETTY_FUNCTION__, font);
[settings setObject:[font fontName] forKey:@"FontName"];
[settings setObject:[[NSNumber numberWithFloat:[font pointSize]] stringValue] forKey:@"FontSize"] ;
[settings setObject:([antialiasCheckBox state] == NSOnState) ? @"Yes" : @"No" forKey:@"AntiAlias"];
return settings;
}
//---------------------------------------------------------------------------------------
// TABLEVIEW DATASOURCE
//---------------------------------------------------------------------------------------
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [filenames count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
return [filenames objectAtIndex:rowIndex];
}
- (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
{
[tableView setDropRow:((row == -1) ? [filenames count] : row) dropOperation:NSTableViewDropAbove];
return NSDragOperationGeneric;
}
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
{
NSArray *draggedFilenames;
NSEnumerator *filenameEnum;
NSString *filename;
int previousRow;
draggedFilenames = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
filenameEnum = [draggedFilenames reverseObjectEnumerator];
while((filename = [filenameEnum nextObject]) != nil)
{
if((previousRow = [filenames indexOfObject:filename]) != NSNotFound)
{
if(row > previousRow)
row -= 1;
[filenames removeObjectAtIndex:previousRow];
}
[filenames insertObject:filename atIndex:row];
}
[tableView reloadData];
return YES;
}
- (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows toPasteboard:(NSPasteboard *)pboard
{
NSMutableArray *filenamesToDrag;
NSEnumerator *rowEnum;
NSNumber *rowIdx;
filenamesToDrag = [NSMutableArray array];
rowEnum = [rows objectEnumerator];
while((rowIdx = [rowEnum nextObject]) != nil)
[filenamesToDrag addObject:[filenames objectAtIndex:[rowIdx intValue]]];
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
[pboard setPropertyList:filenamesToDrag forType:NSFilenamesPboardType];
return YES;
}
- (BOOL)tableView:(NSTableView *)tableView handleKeyDownEvent:(NSEvent *)theEvent
{
unichar c = [[theEvent characters] characterAtIndex:0];
if((c != NSDeleteFunctionKey) && (c != 127))
return NO;
[self removeSelectedFiles:self];
return YES;
}
//---------------------------------------------------------------------------------------
// MENU VALIDATION
//---------------------------------------------------------------------------------------
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
SEL action = [anItem action];
if(action == @selector(revealSelectedFilesInFinder:) ||
action == @selector(removeSelectedFiles:))
return [fileTableView numberOfSelectedRows] > 0;
return YES;
}
//---------------------------------------------------------------------------------------
// WINDOW NOTIFICATIONS
//---------------------------------------------------------------------------------------
- (void)windowDidMove:(NSNotification *)notification
{
if([notification object] == panel)
return;
[frameXField setFloatValue:[[notification object] frame].origin.x];
[frameYField setFloatValue:[[notification object] frame].origin.y];
}
- (void)windowDidResize:(NSNotification *)notification
{
if([notification object] == panel)
return;
[frameYField setFloatValue:[[notification object] frame].origin.y];
[frameWField setFloatValue:[[notification object] frame].size.width];
[frameHField setFloatValue:[[notification object] frame].size.height];
}
//---------------------------------------------------------------------------------------
// ACTIONS
//---------------------------------------------------------------------------------------
- (IBAction)revealSelectedFilesInFinder:(id)sender
{
int row;
if((row = [fileTableView selectedRow]) == -1)
return NSBeep();
[[NSWorkspace sharedWorkspace] selectFile:[filenames objectAtIndex:row] inFileViewerRootedAtPath:@"/"];
}
- (IBAction)removeSelectedFiles:(id)sender
{
int row;
if((row = [fileTableView selectedRow]) == -1)
return NSBeep();
[filenames removeObjectAtIndex:row];
[fileTableView reloadData];
}
- (IBAction)applyChanges:(id)sender
{
NSMutableArray *windowListDefault;
NSMutableDictionary *windowSettings;
windowListDefault = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"Windows"] mutableCopy] autorelease];
windowSettings = [[windowListDefault objectAtIndex:0] mutableCopy];
[windowSettings addEntriesFromDictionary:[self _getSettings]];
[windowListDefault replaceObjectAtIndex:0 withObject:windowSettings];
[[NSUserDefaults standardUserDefaults] setObject:windowListDefault forKey:@"Windows"];
[[NSUserDefaults standardUserDefaults] synchronize]; // in case wm is out-of proc
[[self _getWindowManager] rebuildWindowControllers];
[[self _getFirstConsoleWindowController] enterSetUpModeWithListener:self];
[self _showSettings:windowSettings];
}
- (IBAction)discardChanges:(id)sender
{
[[self _getWindowManager] rebuildWindowControllers];
[panel close];
}
- (IBAction)acceptChanges:(id)sender
{
[self applyChanges:self];
[panel close];
}
//---------------------------------------------------------------------------------------
@end
//---------------------------------------------------------------------------------------