blob: 86112c4b15d4740ca1c50611409237f9ddcca2d0 [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]];
Jack Jansend7cccdd2003-06-20 22:21:03 +000063 [scriptargs setStringValue: [settings scriptargs]];
Jack Jansen3bbb6172002-07-29 21:36:35 +000064 [with_terminal setState: [settings with_terminal]];
65
66 [commandline setStringValue: [settings commandLineForScript: script]];
67}
68
69- (void)update_settings
70{
71 [settings updateFromSource: self];
72}
73
74- (BOOL)run
75{
76 const char *cmdline;
77 int sts;
78
Jack Jansenb7276cd2002-07-31 13:15:59 +000079 cmdline = [[settings commandLineForScript: script] cString];
80 if ([settings with_terminal]) {
81 sts = doscript(cmdline);
82 } else {
83 sts = system(cmdline);
Jack Jansen3bbb6172002-07-29 21:36:35 +000084 }
Jack Jansen3bbb6172002-07-29 21:36:35 +000085 if (sts) {
86 NSLog(@"Exit status: %d\n", sts);
87 return NO;
88 }
89 return YES;
90}
91
92- (void)windowControllerDidLoadNib:(NSWindowController *) aController
93{
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];
98}
99
100- (NSData *)dataRepresentationOfType:(NSString *)aType
101{
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;
104}
105
106- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
107{
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];
Jack Jansen2095c062002-11-25 13:11:06 +0000113 [script release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000114 script = [fileName retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000115 [filetype release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000116 filetype = [type retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000117// if (settings) [settings release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000118 settings = [FileSettings newSettingsForFileType: filetype];
119 if (show_ui) {
120 [self update_display];
121 return YES;
122 } else {
123 [self run];
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000124 [self performSelector:@selector(close) withObject:nil afterDelay:0.0];
125 return YES;
Jack Jansen3bbb6172002-07-29 21:36:35 +0000126 }
127}
128
129- (IBAction)do_run:(id)sender
130{
131 [self update_settings];
132 [self update_display];
133 if ([self run])
134 [self close];
135}
136
137- (IBAction)do_cancel:(id)sender
138{
139 [self close];
140}
141
142
143- (IBAction)do_reset:(id)sender
144{
Jack Jansen2095c062002-11-25 13:11:06 +0000145 [settings reset];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000146 [self update_display];
147}
148
149- (IBAction)do_apply:(id)sender
150{
151 [self update_settings];
152 [self update_display];
153}
154
155// FileSettingsSource protocol
156- (NSString *) interpreter { return [interpreter stringValue];};
Jack Jansen3d3b7462003-02-17 15:40:00 +0000157- (BOOL) honourhashbang { return [honourhashbang state];};
Jack Jansen3bbb6172002-07-29 21:36:35 +0000158- (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];};
Jack Jansend7cccdd2003-06-20 22:21:03 +0000165- (NSString *) scriptargs { return [scriptargs stringValue];};
Jack Jansen3bbb6172002-07-29 21:36:35 +0000166- (BOOL) with_terminal { return [with_terminal state];};
167
168// Delegates
169- (void)controlTextDidChange:(NSNotification *)aNotification
170{
171 [self update_settings];
172 [self update_display];
173};
174
175@end