blob: 90c5db9dc5b60d142d62964d8479f80c1ece249a [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) {
Ronald Oussoren4e327c92013-07-07 09:53:08 +020019
Jack Jansen3bbb6172002-07-29 21:36:35 +000020 // 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];
Ronald Oussoren4e327c92013-07-07 09:53:08 +020040 if ([(MyAppDelegate*)[app delegate] shouldTerminate])
Jack Jansen3bbb6172002-07-29 21:36:35 +000041 [app terminate: self];
42}
43
44- (void)load_defaults
45{
46 settings = [FileSettings newSettingsForFileType: filetype];
47}
48
49- (void)update_display
50{
Jack Jansen3bbb6172002-07-29 21:36:35 +000051 [interpreter setStringValue: [settings interpreter]];
Jack Jansen3d3b7462003-02-17 15:40:00 +000052 [honourhashbang setState: [settings honourhashbang]];
Jack Jansen3bbb6172002-07-29 21:36:35 +000053 [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]];
Jack Jansend7cccdd2003-06-20 22:21:03 +000060 [scriptargs setStringValue: [settings scriptargs]];
Jack Jansen3bbb6172002-07-29 21:36:35 +000061 [with_terminal setState: [settings with_terminal]];
Ronald Oussoren4e327c92013-07-07 09:53:08 +020062
Jack Jansen3bbb6172002-07-29 21:36:35 +000063 [commandline setStringValue: [settings commandLineForScript: script]];
64}
65
66- (void)update_settings
67{
68 [settings updateFromSource: self];
69}
70
71- (BOOL)run
72{
73 const char *cmdline;
74 int sts;
Ronald Oussoren4e327c92013-07-07 09:53:08 +020075
Ronald Oussorenfd1c69e22013-07-06 13:20:57 +020076 cmdline = [[settings commandLineForScript: script] UTF8String];
Jack Jansenb7276cd2002-07-31 13:15:59 +000077 if ([settings with_terminal]) {
78 sts = doscript(cmdline);
79 } else {
80 sts = system(cmdline);
Jack Jansen3bbb6172002-07-29 21:36:35 +000081 }
Jack Jansen3bbb6172002-07-29 21:36:35 +000082 if (sts) {
83 NSLog(@"Exit status: %d\n", sts);
84 return NO;
85 }
86 return YES;
87}
88
89- (void)windowControllerDidLoadNib:(NSWindowController *) aController
90{
91 [super windowControllerDidLoadNib:aController];
92 // Add any code here that need to be executed once the windowController has loaded the document's window.
93 [self load_defaults];
94 [self update_display];
95}
96
97- (NSData *)dataRepresentationOfType:(NSString *)aType
98{
99 // Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
100 return nil;
101}
102
103- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
104{
105 // Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
106 BOOL show_ui;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200107
108 // ask the app delegate whether we should show the UI or not.
109 show_ui = [(MyAppDelegate*)[[NSApplication sharedApplication] delegate] shouldShowUI];
Jack Jansen2095c062002-11-25 13:11:06 +0000110 [script release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000111 script = [fileName retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000112 [filetype release];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000113 filetype = [type retain];
114 settings = [FileSettings newSettingsForFileType: filetype];
115 if (show_ui) {
116 [self update_display];
117 return YES;
118 } else {
119 [self run];
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000120 [self performSelector:@selector(close) withObject:nil afterDelay:0.0];
121 return YES;
Jack Jansen3bbb6172002-07-29 21:36:35 +0000122 }
123}
124
125- (IBAction)do_run:(id)sender
126{
127 [self update_settings];
128 [self update_display];
129 if ([self run])
130 [self close];
131}
132
133- (IBAction)do_cancel:(id)sender
134{
135 [self close];
136}
137
138
139- (IBAction)do_reset:(id)sender
140{
Jack Jansen2095c062002-11-25 13:11:06 +0000141 [settings reset];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000142 [self update_display];
143}
144
145- (IBAction)do_apply:(id)sender
146{
147 [self update_settings];
148 [self update_display];
149}
150
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200151// FileSettingsSource protocol
Jack Jansen3bbb6172002-07-29 21:36:35 +0000152- (NSString *) interpreter { return [interpreter stringValue];};
Jack Jansen3d3b7462003-02-17 15:40:00 +0000153- (BOOL) honourhashbang { return [honourhashbang state];};
Jack Jansen3bbb6172002-07-29 21:36:35 +0000154- (BOOL) debug { return [debug state];};
155- (BOOL) verbose { return [verbose state];};
156- (BOOL) inspect { return [inspect state];};
157- (BOOL) optimize { return [optimize state];};
158- (BOOL) nosite { return [nosite state];};
159- (BOOL) tabs { return [tabs state];};
160- (NSString *) others { return [others stringValue];};
Jack Jansend7cccdd2003-06-20 22:21:03 +0000161- (NSString *) scriptargs { return [scriptargs stringValue];};
Jack Jansen3bbb6172002-07-29 21:36:35 +0000162- (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