blob: 25718570b9e39f9b5ed6fc1391d58b7ea3c9dafa [file] [log] [blame]
Jack Jansen3bbb6172002-07-29 21:36:35 +00001//
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//
8
9#import "MyDocument.h"
10#import "MyAppDelegate.h"
Jack Jansenb7276cd2002-07-31 13:15:59 +000011#import "doscript.h"
Jack Jansen3bbb6172002-07-29 21:36:35 +000012
13@implementation MyDocument
14
15- (id)init
16{
17 [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";
23 filetype = @"Python Script";
24 }
25 return self;
26}
27
28- (NSString *)windowNibName
29{
30 // Override returning the nib file name of the document
31 // 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.
32 return @"MyDocument";
33}
34
35- (void)close
36{
37 NSApplication *app = [NSApplication sharedApplication];
38 [super close];
39 if ([[app delegate] shouldTerminate])
40 [app terminate: self];
41}
42
43- (void)load_defaults
44{
45 settings = [FileSettings newSettingsForFileType: filetype];
46}
47
48- (void)update_display
49{
50// [[self window] setTitle: script];
51
52 [interpreter setStringValue: [settings interpreter]];
53 [debug setState: [settings debug]];
54 [verbose setState: [settings verbose]];
55 [inspect setState: [settings inspect]];
56 [optimize setState: [settings optimize]];
57 [nosite setState: [settings nosite]];
58 [tabs setState: [settings tabs]];
59 [others setStringValue: [settings others]];
60 [with_terminal setState: [settings with_terminal]];
61
62 [commandline setStringValue: [settings commandLineForScript: script]];
63}
64
65- (void)update_settings
66{
67 [settings updateFromSource: self];
68}
69
70- (BOOL)run
71{
72 const char *cmdline;
73 int sts;
74
Jack Jansenb7276cd2002-07-31 13:15:59 +000075 cmdline = [[settings commandLineForScript: script] cString];
76 if ([settings with_terminal]) {
77 sts = doscript(cmdline);
78 } else {
79 sts = system(cmdline);
Jack Jansen3bbb6172002-07-29 21:36:35 +000080 }
Jack Jansen3bbb6172002-07-29 21:36:35 +000081 if (sts) {
82 NSLog(@"Exit status: %d\n", sts);
83 return NO;
84 }
85 return YES;
86}
87
88- (void)windowControllerDidLoadNib:(NSWindowController *) aController
89{
90 [super windowControllerDidLoadNib:aController];
91 // Add any code here that need to be executed once the windowController has loaded the document's window.
92 [self load_defaults];
93 [self update_display];
94}
95
96- (NSData *)dataRepresentationOfType:(NSString *)aType
97{
98 // Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
99 return nil;
100}
101
102- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
103{
104 // Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
105 BOOL show_ui;
106
107 // ask the app delegate whether we should show the UI or not.
108 show_ui = [[[NSApplication sharedApplication] delegate] shouldShowUI];
109 script = [fileName retain];
110 filetype = [type retain];
111 settings = [FileSettings newSettingsForFileType: filetype];
112 if (show_ui) {
113 [self update_display];
114 return YES;
115 } else {
116 [self run];
117 [self close];
118 return NO;
119 }
120}
121
122- (IBAction)do_run:(id)sender
123{
124 [self update_settings];
125 [self update_display];
126 if ([self run])
127 [self close];
128}
129
130- (IBAction)do_cancel:(id)sender
131{
132 [self close];
133}
134
135
136- (IBAction)do_reset:(id)sender
137{
138 [self load_defaults];
139 [self update_display];
140}
141
142- (IBAction)do_apply:(id)sender
143{
144 [self update_settings];
145 [self update_display];
146}
147
148// FileSettingsSource protocol
149- (NSString *) interpreter { return [interpreter stringValue];};
150- (BOOL) debug { return [debug state];};
151- (BOOL) verbose { return [verbose state];};
152- (BOOL) inspect { return [inspect state];};
153- (BOOL) optimize { return [optimize state];};
154- (BOOL) nosite { return [nosite state];};
155- (BOOL) tabs { return [tabs state];};
156- (NSString *) others { return [others stringValue];};
157- (BOOL) with_terminal { return [with_terminal state];};
158
159// Delegates
160- (void)controlTextDidChange:(NSNotification *)aNotification
161{
162 [self update_settings];
163 [self update_display];
164};
165
166@end