MyDocument.m 4.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//
//  MyDocument.m
//  PythonLauncher
//
//  Created by Jack Jansen on Fri Jul 19 2002.
//  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
//

#import "MyDocument.h"
#import "MyAppDelegate.h"
11
#import "doscript.h"
12 13 14 15 16

@implementation MyDocument

- (id)init
{
17
    self = [super init];
18
    if (self) {
19

20 21
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self dealloc] message and return nil.
22 23 24
        script = [@"<no script>.py" retain];
        filetype = [@"Python Script" retain];
        settings = NULL;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    }
    return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // 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.
    return @"MyDocument";
}

- (void)close
{
    NSApplication *app = [NSApplication sharedApplication];
    [super close];
40
    if ([(MyAppDelegate*)[app delegate] shouldTerminate])
41 42 43 44 45 46 47 48 49 50 51
        [app terminate: self];
}

- (void)load_defaults
{
    settings = [FileSettings newSettingsForFileType: filetype];
}

- (void)update_display
{
    [interpreter setStringValue: [settings interpreter]];
52
    [honourhashbang setState: [settings honourhashbang]];
53 54 55 56 57 58 59
    [debug setState: [settings debug]];
    [verbose setState: [settings verbose]];
    [inspect setState: [settings inspect]];
    [optimize setState: [settings optimize]];
    [nosite setState: [settings nosite]];
    [tabs setState: [settings tabs]];
    [others setStringValue: [settings others]];
60
    [scriptargs setStringValue: [settings scriptargs]];
61
    [with_terminal setState: [settings with_terminal]];
62

63 64 65 66 67 68 69 70 71 72 73 74
    [commandline setStringValue: [settings commandLineForScript: script]];
}

- (void)update_settings
{
    [settings updateFromSource: self];
}

- (BOOL)run
{
    const char *cmdline;
    int sts;
75

76
     cmdline = [[settings commandLineForScript: script] UTF8String];
77 78 79 80
   if ([settings with_terminal]) {
        sts = doscript(cmdline);
    } else {
        sts = system(cmdline);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    }
    if (sts) {
        NSLog(@"Exit status: %d\n", sts);
        return NO;
    }
    return YES;
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that need to be executed once the windowController has loaded the document's window.
    [self load_defaults];
    [self update_display];
}

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
    // Insert code here to write your document from the given data.  You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
    return nil;
}

- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
{
    // Insert code here to read your document from the given data.  You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
    BOOL show_ui;
107 108 109

    // ask the app delegate whether we should show the UI or not.
    show_ui = [(MyAppDelegate*)[[NSApplication sharedApplication] delegate] shouldShowUI];
110
    [script release];
111
    script = [fileName retain];
112
    [filetype release];
113 114 115 116 117 118 119
    filetype = [type retain];
    settings = [FileSettings newSettingsForFileType: filetype];
    if (show_ui) {
        [self update_display];
        return YES;
    } else {
        [self run];
Christian Heimes's avatar
Christian Heimes committed
120 121
	[self performSelector:@selector(close) withObject:nil afterDelay:0.0];
        return YES;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }
}

- (IBAction)do_run:(id)sender
{
    [self update_settings];
    [self update_display];
    if ([self run])
        [self close];
}

- (IBAction)do_cancel:(id)sender
{
    [self close];
}


- (IBAction)do_reset:(id)sender
{
141
    [settings reset];
142 143 144 145 146 147 148 149 150
    [self update_display];
}

- (IBAction)do_apply:(id)sender
{
    [self update_settings];
    [self update_display];
}

151
// FileSettingsSource protocol
152
- (NSString *) interpreter { return [interpreter stringValue];};
153
- (BOOL) honourhashbang { return [honourhashbang state];};
154 155 156 157 158 159 160
- (BOOL) debug { return [debug state];};
- (BOOL) verbose { return [verbose state];};
- (BOOL) inspect { return [inspect state];};
- (BOOL) optimize { return [optimize state];};
- (BOOL) nosite { return [nosite state];};
- (BOOL) tabs { return [tabs state];};
- (NSString *) others { return [others stringValue];};
161
- (NSString *) scriptargs { return [scriptargs stringValue];};
162 163 164 165 166 167 168 169 170 171
- (BOOL) with_terminal { return [with_terminal state];};

// Delegates
- (void)controlTextDidChange:(NSNotification *)aNotification
{
    [self update_settings];
    [self update_display];
};

@end