| 1 | //
|
|---|
| 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) {
|
|---|
| 19 |
|
|---|
| 20 | // 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];
|
|---|
| 40 | if ([[app delegate] shouldTerminate])
|
|---|
| 41 | [app terminate: self];
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | - (void)load_defaults
|
|---|
| 45 | {
|
|---|
| 46 | // if (settings) [settings release];
|
|---|
| 47 | settings = [FileSettings newSettingsForFileType: filetype];
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | - (void)update_display
|
|---|
| 51 | {
|
|---|
| 52 | // [[self window] setTitle: script];
|
|---|
| 53 |
|
|---|
| 54 | [interpreter setStringValue: [settings interpreter]];
|
|---|
| 55 | [honourhashbang setState: [settings honourhashbang]];
|
|---|
| 56 | [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 | [scriptargs setStringValue: [settings scriptargs]];
|
|---|
| 64 | [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 |
|
|---|
| 79 | cmdline = [[settings commandLineForScript: script] cString];
|
|---|
| 80 | if ([settings with_terminal]) {
|
|---|
| 81 | sts = doscript(cmdline);
|
|---|
| 82 | } else {
|
|---|
| 83 | sts = system(cmdline);
|
|---|
| 84 | }
|
|---|
| 85 | 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];
|
|---|
| 113 | [script release];
|
|---|
| 114 | script = [fileName retain];
|
|---|
| 115 | [filetype release];
|
|---|
| 116 | filetype = [type retain];
|
|---|
| 117 | // if (settings) [settings release];
|
|---|
| 118 | settings = [FileSettings newSettingsForFileType: filetype];
|
|---|
| 119 | if (show_ui) {
|
|---|
| 120 | [self update_display];
|
|---|
| 121 | return YES;
|
|---|
| 122 | } else {
|
|---|
| 123 | [self run];
|
|---|
| 124 | [self performSelector:@selector(close) withObject:nil afterDelay:0.0];
|
|---|
| 125 | return YES;
|
|---|
| 126 | }
|
|---|
| 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 | {
|
|---|
| 145 | [settings reset];
|
|---|
| 146 | [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];};
|
|---|
| 157 | - (BOOL) honourhashbang { return [honourhashbang state];};
|
|---|
| 158 | - (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];};
|
|---|
| 165 | - (NSString *) scriptargs { return [scriptargs stringValue];};
|
|---|
| 166 | - (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
|
|---|