test_normalization should skip and not crash when the resource isn't available
[python.git] / Mac / PythonLauncher / MyDocument.m
blob86112c4b15d4740ca1c50611409237f9ddcca2d0
1 //
2 //  MyDocument.m
3 //  PythonLauncher
4 //
5 //  Created by Jack Jansen on Fri Jul 19 2002.
6 //  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7 //
9 #import "MyDocument.h"
10 #import "MyAppDelegate.h"
11 #import "doscript.h"
13 @implementation MyDocument
15 - (id)init
17     self = [super init];
18     if (self) {
19     
20         // Add your subclass-specific initialization here.
21         // If an error occurs here, send a [self dealloc] message and return nil.
22         script = [@"<no script>.py" retain];
23         filetype = [@"Python Script" retain];
24         settings = NULL;
25     }
26     return self;
29 - (NSString *)windowNibName
31     // Override returning the nib file name of the document
32     // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
33     return @"MyDocument";
36 - (void)close
38     NSApplication *app = [NSApplication sharedApplication];
39     [super close];
40     if ([[app delegate] shouldTerminate])
41         [app terminate: self];
44 - (void)load_defaults
46 //    if (settings) [settings release];
47     settings = [FileSettings newSettingsForFileType: filetype];
50 - (void)update_display
52 //    [[self window] setTitle: script];
53     
54     [interpreter setStringValue: [settings interpreter]];
55     [honourhashbang setState: [settings honourhashbang]];
56     [debug setState: [settings debug]];
57     [verbose setState: [settings verbose]];
58     [inspect setState: [settings inspect]];
59     [optimize setState: [settings optimize]];
60     [nosite setState: [settings nosite]];
61     [tabs setState: [settings tabs]];
62     [others setStringValue: [settings others]];
63     [scriptargs setStringValue: [settings scriptargs]];
64     [with_terminal setState: [settings with_terminal]];
65     
66     [commandline setStringValue: [settings commandLineForScript: script]];
69 - (void)update_settings
71     [settings updateFromSource: self];
74 - (BOOL)run
76     const char *cmdline;
77     int sts;
78     
79      cmdline = [[settings commandLineForScript: script] cString];
80    if ([settings with_terminal]) {
81         sts = doscript(cmdline);
82     } else {
83         sts = system(cmdline);
84     }
85     if (sts) {
86         NSLog(@"Exit status: %d\n", sts);
87         return NO;
88     }
89     return YES;
92 - (void)windowControllerDidLoadNib:(NSWindowController *) aController
94     [super windowControllerDidLoadNib:aController];
95     // Add any code here that need to be executed once the windowController has loaded the document's window.
96     [self load_defaults];
97     [self update_display];
100 - (NSData *)dataRepresentationOfType:(NSString *)aType
102     // Insert code here to write your document from the given data.  You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
103     return nil;
106 - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
108     // Insert code here to read your document from the given data.  You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
109     BOOL show_ui;
110     
111     // ask the app delegate whether we should show the UI or not. 
112     show_ui = [[[NSApplication sharedApplication] delegate] shouldShowUI];
113     [script release];
114     script = [fileName retain];
115     [filetype release];
116     filetype = [type retain];
117 //    if (settings) [settings release];
118     settings = [FileSettings newSettingsForFileType: filetype];
119     if (show_ui) {
120         [self update_display];
121         return YES;
122     } else {
123         [self run];
124         [self performSelector:@selector(close) withObject:nil afterDelay:0.0];
125         return YES;
126     }
129 - (IBAction)do_run:(id)sender
131     [self update_settings];
132     [self update_display];
133     if ([self run])
134         [self close];
137 - (IBAction)do_cancel:(id)sender
139     [self close];
143 - (IBAction)do_reset:(id)sender
145     [settings reset];
146     [self update_display];
149 - (IBAction)do_apply:(id)sender
151     [self update_settings];
152     [self update_display];
155 // FileSettingsSource protocol 
156 - (NSString *) interpreter { return [interpreter stringValue];};
157 - (BOOL) honourhashbang { return [honourhashbang state];};
158 - (BOOL) debug { return [debug state];};
159 - (BOOL) verbose { return [verbose state];};
160 - (BOOL) inspect { return [inspect state];};
161 - (BOOL) optimize { return [optimize state];};
162 - (BOOL) nosite { return [nosite state];};
163 - (BOOL) tabs { return [tabs state];};
164 - (NSString *) others { return [others stringValue];};
165 - (NSString *) scriptargs { return [scriptargs stringValue];};
166 - (BOOL) with_terminal { return [with_terminal state];};
168 // Delegates
169 - (void)controlTextDidChange:(NSNotification *)aNotification
171     [self update_settings];
172     [self update_display];
175 @end