blob: 90c5db9dc5b60d142d62964d8479f80c1ece249a [file] [log] [blame]
Ronald Oussorenc629be82006-06-07 18:58:01 +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"
11#import "doscript.h"
12
13@implementation MyDocument
14
15- (id)init
16{
17 self = [super init];
18 if (self) {
Ronald Oussorencc879a02013-07-07 09:49:23 +020019
Ronald Oussorenc629be82006-06-07 18:58:01 +000020 // 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" retain];
23 filetype = [@"Python Script" retain];
24 settings = NULL;
25 }
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 Oussorencc879a02013-07-07 09:49:23 +020040 if ([(MyAppDelegate*)[app delegate] shouldTerminate])
Ronald Oussorenc629be82006-06-07 18:58:01 +000041 [app terminate: self];
42}
43
44- (void)load_defaults
45{
Ronald Oussorenc629be82006-06-07 18:58:01 +000046 settings = [FileSettings newSettingsForFileType: filetype];
47}
48
49- (void)update_display
50{
Ronald Oussorenc629be82006-06-07 18:58:01 +000051 [interpreter setStringValue: [settings interpreter]];
52 [honourhashbang setState: [settings honourhashbang]];
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 [scriptargs setStringValue: [settings scriptargs]];
61 [with_terminal setState: [settings with_terminal]];
Ronald Oussorencc879a02013-07-07 09:49:23 +020062
Ronald Oussorenc629be82006-06-07 18:58:01 +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 Oussorencc879a02013-07-07 09:49:23 +020075
Ronald Oussoren51ee6562013-07-06 13:19:58 +020076 cmdline = [[settings commandLineForScript: script] UTF8String];
Ronald Oussorenc629be82006-06-07 18:58:01 +000077 if ([settings with_terminal]) {
78 sts = doscript(cmdline);
79 } else {
80 sts = system(cmdline);
81 }
82 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 Oussorencc879a02013-07-07 09:49:23 +0200107
108 // ask the app delegate whether we should show the UI or not.
109 show_ui = [(MyAppDelegate*)[[NSApplication sharedApplication] delegate] shouldShowUI];
Ronald Oussorenc629be82006-06-07 18:58:01 +0000110 [script release];
111 script = [fileName retain];
112 [filetype release];
113 filetype = [type retain];
Ronald Oussorenc629be82006-06-07 18:58:01 +0000114 settings = [FileSettings newSettingsForFileType: filetype];
115 if (show_ui) {
116 [self update_display];
117 return YES;
118 } else {
119 [self run];
Ronald Oussorenf2ef92c2008-05-02 21:42:35 +0000120 [self performSelector:@selector(close) withObject:nil afterDelay:0.0];
121 return YES;
Ronald Oussorenc629be82006-06-07 18:58:01 +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{
141 [settings reset];
142 [self update_display];
143}
144
145- (IBAction)do_apply:(id)sender
146{
147 [self update_settings];
148 [self update_display];
149}
150
Ronald Oussorencc879a02013-07-07 09:49:23 +0200151// FileSettingsSource protocol
Ronald Oussorenc629be82006-06-07 18:58:01 +0000152- (NSString *) interpreter { return [interpreter stringValue];};
153- (BOOL) honourhashbang { return [honourhashbang state];};
154- (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];};
161- (NSString *) scriptargs { return [scriptargs 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