blob: 09a0024d5e779ba2e8dd4075c56197ef35c3dbda [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]];
Jack Jansen3d3b7462003-02-17 15:40:00 +000055 [honourhashbang setState: [settings honourhashbang]];
Jack Jansen3bbb6172002-07-29 21:36:35 +000056 [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 [with_terminal setState: [settings with_terminal]];
64
65 [commandline setStringValue: [settings commandLineForScript: script]];
66}
67
68- (void)update_settings
69{
70 [settings updateFromSource: self];
71}
72
73- (BOOL)run
74{
75 const char *cmdline;
76 int sts;
77
Jack Jansenb7276cd2002-07-31 13:15:59 +000078 cmdline = [[settings commandLineForScript: script] cString];
79 if ([settings with_terminal]) {
80 sts = doscript(cmdline);
81 } else {
82 sts = system(cmdline);
Jack Jansen3bbb6172002-07-29 21:36:35 +000083 }
Jack Jansen3bbb6172002-07-29 21:36:35 +000084 if (sts) {
85 NSLog(@"Exit status: %d\n", sts);
86 return NO;
87 }
88 return YES;
89}
90
91- (void)windowControllerDidLoadNib:(NSWindowController *) aController
92{
93 [super windowControllerDidLoadNib:aController];
94 // Add any code here that need to be executed once the windowController has loaded the document's window.
95 [self load_defaults];
96 [self update_display];
97}
98
99- (NSData *)dataRepresentationOfType:(NSString *)aType
100{
101 // Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
102 return nil;
103}
104
105- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
106{
107 // Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
108 BOOL show_ui;
109
110 // ask the app delegate whether we should show the UI or not.
111 show_ui = [[[NSApplication sharedApplication] delegate] shouldShowUI];
Jack Jansen2095c062002-11-25 13:11:06 +0000112 [script release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000113 script = [fileName retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000114 [filetype release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000115 filetype = [type retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000116// if (settings) [settings release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000117 settings = [FileSettings newSettingsForFileType: filetype];
118 if (show_ui) {
119 [self update_display];
120 return YES;
121 } else {
122 [self run];
123 [self close];
124 return NO;
125 }
126}
127
128- (IBAction)do_run:(id)sender
129{
130 [self update_settings];
131 [self update_display];
132 if ([self run])
133 [self close];
134}
135
136- (IBAction)do_cancel:(id)sender
137{
138 [self close];
139}
140
141
142- (IBAction)do_reset:(id)sender
143{
Jack Jansen2095c062002-11-25 13:11:06 +0000144 [settings reset];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000145 [self update_display];
146}
147
148- (IBAction)do_apply:(id)sender
149{
150 [self update_settings];
151 [self update_display];
152}
153
154// FileSettingsSource protocol
155- (NSString *) interpreter { return [interpreter stringValue];};
Jack Jansen3d3b7462003-02-17 15:40:00 +0000156- (BOOL) honourhashbang { return [honourhashbang state];};
Jack Jansen3bbb6172002-07-29 21:36:35 +0000157- (BOOL) debug { return [debug state];};
158- (BOOL) verbose { return [verbose state];};
159- (BOOL) inspect { return [inspect state];};
160- (BOOL) optimize { return [optimize state];};
161- (BOOL) nosite { return [nosite state];};
162- (BOOL) tabs { return [tabs state];};
163- (NSString *) others { return [others stringValue];};
164- (BOOL) with_terminal { return [with_terminal state];};
165
166// Delegates
167- (void)controlTextDidChange:(NSNotification *)aNotification
168{
169 [self update_settings];
170 [self update_display];
171};
172
173@end