blob: fdcf86a6d5a7003c0b23d550a28331443d1dbe94 [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{
Jack Jansen2095c062002-11-25 13:11:06 +000017 self = [super init];
Jack Jansen3bbb6172002-07-29 21:36:35 +000018 if (self) {
19
20 // Add your subclass-specific initialization here.
21 // If an error occurs here, send a [self dealloc] message and return nil.
Jack Jansen2095c062002-11-25 13:11:06 +000022 script = [@"<no script>.py" retain];
23 filetype = [@"Python Script" retain];
24 settings = NULL;
Jack Jansen3bbb6172002-07-29 21:36:35 +000025 }
26 return self;
27}
28
29- (NSString *)windowNibName
30{
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";
34}
35
36- (void)close
37{
38 NSApplication *app = [NSApplication sharedApplication];
39 [super close];
40 if ([[app delegate] shouldTerminate])
41 [app terminate: self];
42}
43
44- (void)load_defaults
45{
Jack Jansen2095c062002-11-25 13:11:06 +000046// if (settings) [settings release];
Jack Jansen3bbb6172002-07-29 21:36:35 +000047 settings = [FileSettings newSettingsForFileType: filetype];
48}
49
50- (void)update_display
51{
52// [[self window] setTitle: script];
53
54 [interpreter setStringValue: [settings interpreter]];
55 [debug setState: [settings debug]];
56 [verbose setState: [settings verbose]];
57 [inspect setState: [settings inspect]];
58 [optimize setState: [settings optimize]];
59 [nosite setState: [settings nosite]];
60 [tabs setState: [settings tabs]];
61 [others setStringValue: [settings others]];
62 [with_terminal setState: [settings with_terminal]];
63
64 [commandline setStringValue: [settings commandLineForScript: script]];
65}
66
67- (void)update_settings
68{
69 [settings updateFromSource: self];
70}
71
72- (BOOL)run
73{
74 const char *cmdline;
75 int sts;
76
Jack Jansenb7276cd2002-07-31 13:15:59 +000077 cmdline = [[settings commandLineForScript: script] cString];
78 if ([settings with_terminal]) {
79 sts = doscript(cmdline);
80 } else {
81 sts = system(cmdline);
Jack Jansen3bbb6172002-07-29 21:36:35 +000082 }
Jack Jansen3bbb6172002-07-29 21:36:35 +000083 if (sts) {
84 NSLog(@"Exit status: %d\n", sts);
85 return NO;
86 }
87 return YES;
88}
89
90- (void)windowControllerDidLoadNib:(NSWindowController *) aController
91{
92 [super windowControllerDidLoadNib:aController];
93 // Add any code here that need to be executed once the windowController has loaded the document's window.
94 [self load_defaults];
95 [self update_display];
96}
97
98- (NSData *)dataRepresentationOfType:(NSString *)aType
99{
100 // Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
101 return nil;
102}
103
104- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
105{
106 // Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
107 BOOL show_ui;
108
109 // ask the app delegate whether we should show the UI or not.
110 show_ui = [[[NSApplication sharedApplication] delegate] shouldShowUI];
Jack Jansen2095c062002-11-25 13:11:06 +0000111 [script release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000112 script = [fileName retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000113 [filetype release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000114 filetype = [type retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000115// if (settings) [settings release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000116 settings = [FileSettings newSettingsForFileType: filetype];
117 if (show_ui) {
118 [self update_display];
119 return YES;
120 } else {
121 [self run];
122 [self close];
123 return NO;
124 }
125}
126
127- (IBAction)do_run:(id)sender
128{
129 [self update_settings];
130 [self update_display];
131 if ([self run])
132 [self close];
133}
134
135- (IBAction)do_cancel:(id)sender
136{
137 [self close];
138}
139
140
141- (IBAction)do_reset:(id)sender
142{
Jack Jansen2095c062002-11-25 13:11:06 +0000143 [settings reset];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000144 [self update_display];
145}
146
147- (IBAction)do_apply:(id)sender
148{
149 [self update_settings];
150 [self update_display];
151}
152
153// FileSettingsSource protocol
154- (NSString *) interpreter { return [interpreter stringValue];};
155- (BOOL) debug { return [debug state];};
156- (BOOL) verbose { return [verbose state];};
157- (BOOL) inspect { return [inspect state];};
158- (BOOL) optimize { return [optimize state];};
159- (BOOL) nosite { return [nosite state];};
160- (BOOL) tabs { return [tabs state];};
161- (NSString *) others { return [others stringValue];};
162- (BOOL) with_terminal { return [with_terminal state];};
163
164// Delegates
165- (void)controlTextDidChange:(NSNotification *)aNotification
166{
167 [self update_settings];
168 [self update_display];
169};
170
171@end