ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/PrefsEditor.mm
Revision: 1.2
Committed: 2002-05-25T23:56:05Z (22 years, 4 months ago) by nigel
Branch: MAIN
Changes since 1.1: +28 -24 lines
Log Message:
Fix for screen radio-button selection bug, general tidyup.

File Contents

# User Rev Content
1 nigel 1.1 /*
2     * PrefsEditor.m - GUI stuff for Basilisk II preferences
3     * (which is a text file in the user's home directory)
4     *
5 nigel 1.2 * $Id: PrefsEditor.mm,v 1.1 2002/03/16 04:00:30 nigel Exp $
6 nigel 1.1 *
7     * Basilisk II (C) 1997-2001 Christian Bauer
8     *
9     * This program is free software; you can redistribute it and/or modify
10     * it under the terms of the GNU General Public License as published by
11     * the Free Software Foundation; either version 2 of the License, or
12     * (at your option) any later version.
13     *
14     * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18     *
19     * You should have received a copy of the GNU General Public License
20     * along with this program; if not, write to the Free Software
21     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22     */
23    
24     #import "PrefsEditor.h"
25    
26     @implementation TableDS
27    
28     - (TableDS *) init
29     {
30     self = [super init];
31    
32     numItems = 0;
33     col1 = [[NSMutableArray alloc] init];
34     col2 = [[NSMutableArray alloc] init];
35    
36     return self;
37     }
38    
39     - (void) dealloc
40     {
41     [col1 dealloc];
42     [col2 dealloc];
43     [super dealloc];
44     }
45    
46     - (void) addInt: (int)target
47     withPath: (NSString *)path
48     {
49     [col1 addObject: [NSNumber numberWithInt: target]];
50     [col2 addObject: path];
51     ++numItems;
52     }
53    
54     - (void) addObject: (NSObject *)obj
55     withPath: (NSString *)path
56     {
57     [col1 addObject: obj];
58     [col2 addObject: path];
59     ++numItems;
60     }
61    
62     - (void) deleteAll
63     {
64     numItems = 0;
65     [col1 removeAllObjects];
66     [col2 removeAllObjects];
67     }
68    
69     - (BOOL) deleteRow: (int)row
70     {
71     if ( row > numItems )
72     return NO;
73    
74     [col1 removeObjectAtIndex: row];
75     [col2 removeObjectAtIndex: row];
76     -- numItems;
77    
78     return YES;
79     }
80    
81     - (int)intAtRow: (int)row
82     {
83     return [[col1 objectAtIndex: row] intValue];
84     }
85    
86     - (int) numberOfRowsInTableView: (NSTableView *)tView
87     {
88     return numItems;
89     }
90    
91     - (NSString *)pathAtRow: (int)row
92     {
93     return (NSString *) [col2 objectAtIndex: row];
94     }
95    
96     - (id) tableView: (NSTableView *)tView
97     objectValueForTableColumn: (NSTableColumn *)tColumn
98     row: (int)row
99     {
100     if ( [[tColumn identifier] isEqualToString:@"path"] )
101     return [col2 objectAtIndex: row];
102     else
103     return [col1 objectAtIndex: row];
104     }
105    
106     @end
107    
108     @implementation PrefsEditor
109    
110     #import <AppKit/NSImage.h> // For [NSBundle pathForImageResource:] proto
111    
112     #import "sysdeps.h" // Types used in Basilisk C++ code
113     #import "video_macosx.h" // some items that we edit here
114     #import "misc_macosx.h" // WarningSheet() prototype
115    
116     #import <prefs.h>
117    
118 nigel 1.2 #define DEBUG 1
119 nigel 1.1 #import <debug.h>
120    
121     - (PrefsEditor *) init
122     {
123     self = [super init];
124    
125     edited = NO;
126    
127     devs = @"/dev";
128     home = NSHomeDirectory();
129     volsDS = [[TableDS alloc] init];
130     SCSIds = [[TableDS alloc] init];
131    
132     lockCell = [[NSImageCell alloc] init];
133     if ( lockCell == nil )
134     NSLog (@"%s - Can't create NSImageCell?", __PRETTY_FUNCTION__);
135    
136     blank = [[NSImage alloc] init];
137     locked = [NSImage alloc];
138     if ( [locked initWithContentsOfFile:
139     [[NSBundle mainBundle]
140     pathForImageResource: @"nowrite.icns"]] == nil )
141     NSLog(@"%s - Couldn't open write protection image", __PRETTY_FUNCTION__);
142    
143     return self;
144     }
145    
146     - (void) dealloc
147     {
148     [volsDS dealloc];
149     [SCSIds dealloc];
150     [lockCell dealloc];
151     [locked dealloc];
152     [blank dealloc];
153     [super dealloc];
154     }
155    
156     - (void) awakeFromNib
157     {
158     emuFreq = [theEmulator speed];
159     #if DEBUG
160     [self ShowPrefs: self]; // For testing
161     #endif
162     }
163    
164     - (BOOL) hasEdited
165     {
166     return edited;
167     }
168    
169     - (NSWindow *) window
170     {
171     return panel;
172     }
173    
174     - (IBAction) AddSCSI: (id)sender
175     {
176     NSOpenPanel *oP = [NSOpenPanel openPanel];
177    
178     if ( [oP runModalForDirectory:home file:nil types:nil] == NSOKButton )
179     {
180     [SCSIds addInt: -1
181     withPath: [oP filename] ];
182     [SCSIdisks reloadData];
183     edited = YES;
184     }
185     }
186    
187     - (IBAction) AddVolume: (id)sender
188     {
189     NSOpenPanel *oP = [NSOpenPanel openPanel];
190    
191     if ( [oP runModalForDirectory:home file:nil types:nil] == NSOKButton )
192     {
193     [volsDS addObject: (NSObject *) locked
194     withPath: [oP filename] ];
195     PrefsAddString("disk", [[oP filename] cString]);
196     [diskImages reloadData];
197     edited = YES;
198     }
199     }
200    
201     - (IBAction) BrowseExtFS: (id)sender
202     {
203     NSOpenPanel *oP = [NSOpenPanel openPanel];
204    
205     [oP setCanChooseDirectories: YES];
206     [oP setCanChooseFiles: NO];
207     [oP setPrompt: @"Select"];
208     [oP setTitle: @"Select a directory to mount"];
209     D(NSLog(@"%s - home = %@, [extFS stringValue] = %@",
210     __PRETTY_FUNCTION__, home, [extFS stringValue]));
211     if ( [oP runModalForDirectory: ([extFS stringValue] ? [extFS stringValue] : home)
212     file:nil
213     types:nil] == NSOKButton )
214     {
215     [extFS setStringValue: [oP directory] ];
216     PrefsReplaceString("extfs", [[oP directory] cString]);
217     edited = YES;
218     }
219     }
220    
221     - (IBAction) BrowseROM: (id)sender
222     {
223     NSOpenPanel *oP = [NSOpenPanel openPanel];
224    
225     [oP setCanChooseFiles: YES];
226     [oP setTitle: @"Open a ROM file"];
227     D(NSLog(@"%s - home = %@", __PRETTY_FUNCTION__, home));
228     if ( [oP runModalForDirectory: ([ROMfile stringValue] ? [ROMfile stringValue] : home)
229     file:nil
230     types:nil] == NSOKButton )
231     {
232     [ROMfile setStringValue: [oP filename] ];
233     PrefsReplaceString("rom", [[oP filename] cString]);
234     edited = YES;
235     }
236     }
237    
238     #include <cdrom.h> // for CDROMRefNum
239    
240     - (IBAction) ChangeBootFrom: (NSMatrix *)sender
241     {
242     if ( [sender selectedCell] == bootFromCD )
243     PrefsReplaceInt32("bootdriver", CDROMRefNum);
244     else
245     PrefsReplaceInt32("bootdriver", 0);
246     edited = YES;
247     }
248    
249     - (IBAction) ChangeCPU: (NSMatrix *)sender
250     {
251     PrefsReplaceInt32("cpu", [[sender selectedCell] tag]);
252     edited = YES;
253     }
254    
255     - (IBAction) ChangeDisableCD: (NSButton *)sender
256     {
257     PrefsReplaceBool("nocdrom", [disableCD state]);
258     edited = YES;
259     }
260    
261     - (IBAction) ChangeDisableSound: (NSButton *)sender
262     {
263 nigel 1.2 BOOL noSound = [disableSound state];
264    
265     if ( ! noSound )
266     WarningSheet(@"Sound is currently unimplemented", panel);
267    
268     PrefsReplaceBool("nosound", noSound);
269 nigel 1.1 edited = YES;
270     }
271    
272     - (IBAction) ChangeFPU: (NSButton *)sender
273     {
274     PrefsReplaceBool("fpu", [FPU state]);
275     edited = YES;
276     }
277    
278     - (IBAction) ChangeModel: (NSMatrix *)sender
279     {
280     PrefsReplaceInt32("modelid", [[sender selectedCell] tag]);
281     edited = YES;
282     }
283    
284     // Screen/window changing stuff
285    
286     // This is called when any of the screen/window, width, height or depth is changed
287    
288 nigel 1.2 - (IBAction) ChangeScreen: (id)sender
289 nigel 1.1 {
290     short newx = [width intValue];
291     short newy = [height intValue];
292     short newbpp = [depth intValue];
293 nigel 1.2 short newtype;
294 nigel 1.1 char str[20];
295    
296 nigel 1.2 if ( [openGL state] )
297 nigel 1.1 newtype = DISPLAY_OPENGL;
298 nigel 1.2 else if ( [screen state] )
299 nigel 1.1 newtype = DISPLAY_SCREEN;
300 nigel 1.2 else if ( [window state] )
301     newtype = DISPLAY_WINDOW;
302     else
303     newtype = display_type;
304 nigel 1.1
305     // Check that a field actually changed
306     if ( newbpp == init_depth && newx == init_width &&
307     newy == init_height && newtype == display_type )
308 nigel 1.2 {
309     NSLog(@"No changed GUI items in ChangeScreen");
310 nigel 1.1 return;
311 nigel 1.2 }
312 nigel 1.1
313     // If we are changing type, supply some sensible defaults
314     if ( newtype != display_type )
315     {
316 nigel 1.2 NSLog(@"Changing disylay type in ChangeScreen");
317 nigel 1.1 if ( newtype == DISPLAY_SCREEN ) // If changing to full screen
318     {
319     // supply main screen dimensions as a default
320     NSScreen *s = [NSScreen mainScreen];
321     NSRect sr = [s frame];
322    
323     newx = (short) sr.size.width;
324     newy = (short) sr.size.height;
325     // This always returns 24, despite the mode
326     //newbpp = NSBitsPerPixelFromDepth([s depth]);
327     newbpp = CGDisplayBitsPerPixel(kCGDirectMainDisplay);
328     }
329    
330     if ( display_type == DISPLAY_SCREEN ) // If changing from full screen
331     newx = MIN_WIDTH, newy = MIN_HEIGHT;
332    
333     [width setIntValue: newx];
334     [height setIntValue: newy];
335     [depth setIntValue: newbpp];
336     }
337     else
338     {
339     // Check size is within ranges of MIN_WIDTH ... MAX_WIDTH
340     // and MIN_HEIGHT ... MAX_HEIGHT
341     // ???
342     }
343    
344    
345     // Store new prefs
346     *str = '\0';
347     switch ( newtype )
348     {
349     case DISPLAY_WINDOW:
350     if ( newbpp )
351     sprintf(str, "win/%hd/%hd/%hd", newx, newy, newbpp);
352     else
353     sprintf(str, "win/%hd/%hd", newx, newy);
354     break;
355     case DISPLAY_OPENGL:
356     if ( newbpp )
357     sprintf(str, "opengl/%hd/%hd/%hd", newx, newy, newbpp);
358     else
359     sprintf(str, "opengl/%hd/%hd", newx, newy);
360     break;
361     case DISPLAY_SCREEN:
362     if ( newbpp )
363     sprintf(str, "full/%hd/%hd/%hd", newx, newy, newbpp);
364     else
365     sprintf(str, "full/%hd/%hd", newx, newy);
366     break;
367     };
368     PrefsReplaceString("screen", str);
369    
370     parse_screen_prefs(str);
371    
372     edited = YES;
373    
374     if ( display_type != DISPLAY_SCREEN )
375 nigel 1.2 {
376     NSLog(@"Display type is not SCREEN (%d), resizing window", display_type);
377 nigel 1.1 resizeWinTo(newx, newy);
378 nigel 1.2 }
379 nigel 1.1 }
380    
381     - (IBAction) CreateVolume: (id)sender
382     {
383     NSSavePanel *sP = [NSSavePanel savePanel];
384    
385     [sP setAccessoryView: newVolumeView];
386     [sP setPrompt: @"Create"];
387     [sP setTitle: @"Create new volume as"];
388    
389     if ( [sP runModalForDirectory:NSHomeDirectory() file:@"basilisk-II.vol"] == NSOKButton )
390     {
391     char cmd[1024];
392     const char *filename = [[sP filename] cString];
393     int retVal,
394     size = [newVolumeSize intValue];
395    
396     sprintf(cmd, "dd if=/dev/zero \"of=%s\" bs=1024k count=%d", filename, size);
397     retVal = system(cmd);
398     if (retVal != 0)
399     {
400     NSString *details = [NSString stringWithFormat:
401     @"The dd command failed.\nReturn status %d (%s)",
402     retVal, strerror(errno)];
403 nigel 1.2 WarningSheet(@"Unable to create volume", details, nil, panel);
404 nigel 1.1 }
405     else
406     {
407     [volsDS addObject: (NSObject *) blank
408     withPath: [sP filename] ];
409     PrefsAddString("disk", filename);
410     [diskImages reloadData];
411     }
412     }
413     }
414    
415     - (IBAction) DeleteVolume: (id)sender
416     {
417     const char *path = [self RemoveVolumeEntry];
418     if ( unlink(path) == -1 )
419     {
420     NSLog(@"%s unlink(%s) failed", __PRETTY_FUNCTION__, path, strerror(errno));
421     }
422     }
423    
424     - (IBAction) EditDelay: (NSTextField *)sender
425     {
426     int ticks = [delay intValue];
427     float freq;
428    
429     if ( ticks )
430     freq = 60.0 / ticks;
431     else
432     freq = 60.0;
433    
434     [frequency setFloatValue: freq];
435     [emuFreq setFloatValue: freq];
436     PrefsReplaceInt32("frameskip", ticks);
437     edited = YES;
438     }
439    
440     - (IBAction) EditBytes: (NSTextField *)sender
441     {
442     int B = (int) [bytes floatValue];
443     float M = B / 1024 / 1024;
444    
445 nigel 1.2 D(NSLog(@"%s = %f %d", __PRETTY_FUNCTION__, M, B));
446 nigel 1.1 PrefsReplaceInt32("ramsize", B);
447     [MB setFloatValue: M];
448     edited = YES;
449     }
450    
451     - (IBAction) EditEtherNetDevice: (NSTextField *)sender
452     {
453     NSString *path = [etherNet stringValue];
454    
455     PrefsReplaceString("ether", [path cString]);
456     edited = YES;
457     }
458    
459     - (IBAction) EditExtFS: (NSTextField *)sender
460     {
461     NSString *path = [extFS stringValue];
462    
463     PrefsReplaceString("extfs", [path cString]);
464     edited = YES;
465     }
466    
467     - (IBAction) EditFrequency: (NSTextField *)sender
468     {
469     float freq = [frequency floatValue];
470    
471     [delay setIntValue: frequencyToTickDelay(freq)];
472     [emuFreq setFloatValue: freq];
473     edited = YES;
474     }
475    
476     - (IBAction) EditModemDevice: (NSTextField *)sender
477     {
478     NSString *path = [modem stringValue];
479    
480     PrefsReplaceString("seriala", [path cString]);
481     edited = YES;
482     }
483    
484     - (IBAction) EditMB: (NSTextField *)sender
485     {
486     float M = [MB floatValue];
487     int B = (int) (M * 1024 * 1024);
488    
489 nigel 1.2 D(NSLog(@"%s = %f %d", __PRETTY_FUNCTION__, M, B));
490 nigel 1.1 PrefsReplaceInt32("ramsize", B);
491     [bytes setIntValue: B];
492     edited = YES;
493     }
494    
495     - (IBAction) EditPrinterDevice: (NSTextField *)sender
496     {
497     NSString *path = [printer stringValue];
498    
499     PrefsReplaceString("serialb", [path cString]);
500     edited = YES;
501     }
502    
503     - (IBAction) EditROMpath: (NSTextField *)sender
504     {
505     NSString *path = [ROMfile stringValue];
506    
507     PrefsReplaceString("rom", [path cString]);
508     }
509    
510     - (IBAction) RemoveSCSI: (id)sender
511     {
512     char pref[6];
513     int row = [SCSIdisks selectedRow],
514     SCSIid = [SCSIds intAtRow: row];
515    
516     if ( ! [SCSIds deleteRow: row] )
517     NSLog (@"%s - [SCSIds deleteRow: %d] failed", __PRETTY_FUNCTION__, row);
518     [SCSIdisks reloadData];
519     sprintf(pref, "scsi%d", SCSIid);
520     PrefsRemoveItem(pref,0);
521     }
522    
523     - (const char *) RemoveVolumeEntry
524     {
525     int row = [diskImages selectedRow];
526    
527     if ( row != -1 )
528     {
529     const char *path = [[volsDS pathAtRow: row] cString],
530     *str;
531     int tmp = 0;
532    
533     while ( (str = PrefsFindString("disk", tmp) ) != NULL )
534     {
535     if ( strcmp(str, path) == 0 )
536     {
537     PrefsRemoveItem("disk", tmp);
538     D(NSLog(@"%s - Deleted prefs entry \"disk\", %d", __PRETTY_FUNCTION__, tmp));
539     edited = YES;
540     break;
541     }
542     ++tmp;
543     }
544    
545     if ( str == NULL )
546     {
547     NSLog(@"%s - Couldn't find any disk preference to match %s", __PRETTY_FUNCTION__, path);
548     return NULL;
549     }
550    
551     if ( ! [volsDS deleteRow: row] )
552     NSLog (@"%s - RemoveVolume %d failed", __PRETTY_FUNCTION__, tmp);
553     [diskImages reloadData];
554     return path;
555     }
556     else
557     {
558 nigel 1.2 WarningSheet(@"Please select a volume first", panel);
559 nigel 1.1 return NULL;
560     }
561     }
562    
563     - (IBAction) RemoveVolume: (id)sender
564     {
565     [self RemoveVolumeEntry];
566     }
567    
568     - (IBAction) ResetPrefs: (id)sender
569     {
570     int argc = 0;
571     char **argv = NULL;
572    
573     [panel close]; // Temporarily hide preferences panel
574    
575     PrefsExit(); // Purge all the old pref values
576    
577     PrefsInit(argc, argv);
578     AddPrefsDefaults();
579     AddPlatformPrefsDefaults(); // and only create basic ones
580    
581     [SCSIds deleteAll]; // Clear out datasources for the tables
582     [volsDS deleteAll];
583    
584     [self ShowPrefs: self]; // Reset items in panel, and redisplay
585     edited = NO;
586     }
587    
588     - (void) setStringOf: (NSTextField *) field
589     fromPref: (const char *) prefName
590     {
591     const char *value = PrefsFindString(prefName, 0);
592    
593     if ( value )
594     [field setStringValue: [NSString stringWithCString: value] ];
595     }
596    
597     - (IBAction) SavePrefs: (id)sender
598     {
599     SavePrefs();
600     edited = NO;
601     }
602    
603     - (IBAction) ShowPrefs: (id)sender
604     {
605     NSTableColumn *locks;
606     const char *str;
607     int cpu, tmp;
608    
609    
610     // Set simple single field items
611    
612     tmp = PrefsFindInt32("frameskip");
613     [delay setIntValue: tmp];
614     if ( tmp )
615     [frequency setFloatValue: 60.0 / tmp];
616     else
617     [frequency setFloatValue: 60.0];
618    
619     tmp = PrefsFindInt32("ramsize");
620     [bytes setIntValue: tmp];
621     [MB setFloatValue: tmp / (1024.0 * 1024.0)];
622    
623     [disableCD setState: PrefsFindBool("nocdrom")];
624     [disableSound setState: PrefsFindBool("nosound")];
625     [FPU setState: PrefsFindBool("fpu") ];
626    
627     [self setStringOf: etherNet fromPref: "ether" ];
628     [self setStringOf: extFS fromPref: "extfs" ];
629     [self setStringOf: modem fromPref: "seriala"];
630     [self setStringOf: printer fromPref: "serialb"];
631     [self setStringOf: ROMfile fromPref: "rom" ];
632    
633    
634     parse_screen_prefs(PrefsFindString("screen"));
635    
636     [width setIntValue: init_width];
637     [height setIntValue: init_height];
638     [depth setIntValue: init_depth];
639    
640     [window setState: NO];
641     switch ( display_type )
642     {
643     case DISPLAY_WINDOW: [window setState: YES]; break;
644     case DISPLAY_OPENGL: [openGL setState: YES]; break;
645     case DISPLAY_SCREEN: [screen setState: YES]; break;
646     }
647    
648     [newVolumeSize setIntValue: 10];
649    
650     // Radio button groups:
651    
652     tmp = PrefsFindInt32("bootdriver");
653     [bootFromAny setState: tmp != CDROMRefNum];
654     [bootFromCD setState: tmp == CDROMRefNum];
655    
656     cpu = PrefsFindInt32("cpu");
657     tmp = PrefsFindInt32("modelid");
658    
659     #if REAL_ADDRESSING || DIRECT_ADDRESSING
660     puts("Current memory model does not support 24bit addressing");
661     if ( tmp == [classic tag] )
662     {
663     // Window already created by NIB file, just display
664     [panel makeKeyAndOrderFront:self];
665     WarningSheet(@"Compiled-in memory model does not support 24bit",
666 nigel 1.2 @"Disabling Mac Classic emulation", nil, panel);
667 nigel 1.1 cpu = [CPU68030 tag];
668     PrefsReplaceInt32("cpu", cpu);
669     tmp = [IIci tag];
670     PrefsReplaceInt32("modelid", tmp);
671     }
672    
673     puts("Disabling 68000 & Mac Classic buttons");
674     [CPU68000 setEnabled:FALSE];
675     [classic setEnabled:FALSE];
676     #endif
677    
678     [CPU68000 setState: [CPU68000 tag] == cpu];
679     [CPU68020 setState: [CPU68020 tag] == cpu];
680     [CPU68030 setState: [CPU68030 tag] == cpu];
681     [CPU68040 setState: [CPU68040 tag] == cpu];
682    
683     [classic setState: [classic tag] == tmp];
684     [IIci setState: [IIci tag] == tmp];
685     [quadra900 setState: [quadra900 tag] == tmp];
686    
687    
688     // Lists of thingies:
689    
690     for ( tmp = 0; tmp < 7; ++tmp)
691     {
692     char pref[6];
693    
694     pref[0] = '\0';
695    
696     sprintf (pref, "scsi%d", tmp);
697     if ( (str = PrefsFindString(pref, 0) ) )
698     [SCSIds addInt: tmp
699     withPath: [NSString stringWithCString: str] ];
700     }
701    
702     [SCSIdisks setDataSource: SCSIds];
703    
704     locks = [diskImages tableColumnWithIdentifier: @"locked"];
705     if ( locks == nil )
706     NSLog (@"%s - Can't find column for lock images", __PRETTY_FUNCTION__);
707     [locks setDataCell: lockCell];
708    
709     tmp = 0;
710     while ( (str = PrefsFindString("disk", tmp++) ) != NULL )
711     {
712     if ( *str == '*' )
713     [volsDS addObject: (NSObject *) locked
714     withPath: [NSString stringWithCString: str+1]];
715     else
716     [volsDS addObject: (NSObject *) blank
717     withPath: [NSString stringWithCString: str]];
718     }
719    
720     [diskImages setDataSource: volsDS];
721    
722    
723     [panel makeKeyAndOrderFront:self]; // Window already created by NIB file, just display
724     }
725    
726     @end