source: release/1.3/source/cocoa-ide/altconsole/AltConsoleDocument.m @ 11922

Last change on this file since 11922 was 11922, checked in by rme, 11 years ago

Merge r11826, r11826 from trunk.

File size: 5.9 KB
RevLine 
[11540]1/*
2   Copyright (C) 2003 Clozure Associates
3   This file is part of OpenMCL. 
4
5   OpenMCL is licensed under the terms of the Lisp Lesser GNU Public
6   License , known as the LLGPL and distributed with OpenMCL as the
7   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
8   which is distributed with OpenMCL as the file "LGPL".  Where these
9   conflict, the preamble takes precedence. 
10
11   OpenMCL is referenced in the preamble as the "LIBRARY."
12
13   The LLGPL is also available online at
14   http://opensource.franz.com/preamble.html
15
16  $Log: AltConsoleDocument.m,v $
17  Revision 1.2  2003/11/17 07:30:39  gb
18  update copyright/license
19
20  Revision 1.1.1.1  2003/11/17 07:14:42  gb
21  initial checkin
22
23*/
24
25#import "AltConsoleDocument.h"
26#import "AltConsoleDocumentController.h"
27#include <sys/signal.h>
28
29@implementation AltConsoleDocument
30- (id)init {
31    self = [super init];
32    if (self) {
33      NSMutableDictionary *dict;
34
35      peerDied = NO;
36      in = [[[NSFileHandle alloc] initWithFileDescriptor: 0] retain];
37      out =[[[NSFileHandle alloc] initWithFileDescriptor: 1] retain];
38      err =[[[NSFileHandle alloc] initWithFileDescriptor: 2] retain];
39      dict = [[NSMutableDictionary alloc] initWithCapacity: 3];
40      [dict setObject: [NSFont fontWithName: @"Courier" size:12.0] forKey: @"NSFont"];
41      [dict setObject: [NSColor redColor] forKey: @"NSColor"];
42      [dict setObject: [NSParagraphStyle defaultParagraphStyle] forKey: @"NSParagraphStyle"];
43      system_output_attributes = [dict retain];
44      dict = [NSMutableDictionary dictionaryWithDictionary: dict];
45      [dict setObject: [NSColor blackColor] forKey: @"NSColor"];
46      local_typing_attributes = [dict retain];
47      outpos = 0;
48    }
49    return self;
50}
51
52- (NSString *)windowNibName {
53    return @"AltConsole";
54}
55
56- (void) peerDied:(NSNotification *)notification {
57  peerDied = YES;
58  [indicator setTitle: @"Disconnected"];
59  [textView setEditable: NO];
60  if (watchdog) {
61    [watchdog invalidate];
62    watchdog = nil;
63  }
[11922]64   [[NSApplication sharedApplication] terminate:nil];
[11540]65}
66
67- (void) gotData:(NSNotification *)notification {
68  NSData *data = [[notification userInfo] objectForKey: NSFileHandleNotificationDataItem];
69  if ([data length] != 0) {
70    NSTextStorage *buffer_text = [textView textStorage];
71    NSString *s = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
72    NSAttributedString *str = [[NSAttributedString alloc] initWithString: s attributes: system_output_attributes];
73    int textlen;
74   
75    [buffer_text beginEditing];
76    [buffer_text appendAttributedString: str];
77    [buffer_text endEditing];
78    textlen = [buffer_text length];
79    [textView scrollRangeToVisible: NSMakeRange(textlen, 0)];
80    [str release];
81    [in readInBackgroundAndNotify];
82    [self updateChangeCount: NSChangeDone];
83    outpos = textlen;
84  }
85}
86
87
88- (void) watchPeer: (id) theTimer {
89  pid_t peer = getppid();
90
91  if (kill(peer, 0) < 0) {
92    [[NSNotificationCenter defaultCenter]
93      postNotificationName: @"peerDied" object: nil];
94  }
95}
96
97
98- (void)windowControllerDidLoadNib:(NSWindowController *) aController {
99    [super windowControllerDidLoadNib:aController];
100    [[NSNotificationCenter defaultCenter]
101     addObserver: self
102     selector: @selector(gotData:)
103     name: NSFileHandleReadCompletionNotification
104     object: in];
105    [[NSNotificationCenter defaultCenter]
106     addObserver: self
107     selector: @selector(peerDied:)
108     name: @"peerDied"
109     object: nil];
110    [in readInBackgroundAndNotify];
111    [textView setUsesFontPanel: NO];
112    [textView setRichText: NO];
113    [textView setSmartInsertDeleteEnabled: NO];
114    [textView setUsesRuler: NO];
115    [textView setDelegate: self];
116    [textView setContinuousSpellCheckingEnabled: NO];
117    [self setFileName: [[AltConsoleDocumentController sharedDocumentController] herald]];
118    [[AltConsoleDocumentController sharedDocumentController]
119      add_console_document];
120    watchdog = [NSTimer scheduledTimerWithTimeInterval: 1.0
121                        target: self
122                        selector: @selector(watchPeer:)
123                        userInfo: nil
124                        repeats: YES];
125    [NSApp activateIgnoringOtherApps: YES];
126}
127
128- (NSData *)dataRepresentationOfType:(NSString *)aType {
129  [[textView string]
130    dataUsingEncoding: NSASCIIStringEncoding
131    allowLossyConversion: YES];
132}
133
134- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
135    return YES;
136}
137
138- (BOOL)textView: tv shouldChangeTextInRange: (NSRange)r replacementString: s {
139  if (peerDied) {
140    return NO;
141  }
142  if (r.location < outpos) {
143    return NO;
144  }
145  [tv setTypingAttributes: local_typing_attributes];
146  return YES;
147}
148
149- (BOOL)textView: tv doCommandBySelector: (SEL) aselector {
150  if (! [self respondsToSelector: aselector]) {
151    return NO;
152  }
153  [self performSelector: aselector withObject: tv];
154  return YES;
155}
156
157- (void) sendString: (NSString *)s {
158  [out writeData: [s dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES]];
159}
160
161- (void) insertNewline:(NSTextView *)tv {
162  if (peerDied) {
163    [tv insertNewline: self];
164  } else {
165    NSTextStorage *textbuf = [tv textStorage];
166    int textlen = [textbuf length];
167    NSString *textstring = [textbuf string];
168    NSRange r = [tv selectedRange];
169    int curpos = r.location, curlen = r.length;
170   
171    if (curpos >= outpos) {
172      curpos += curlen;
173      [tv setSelectedRange: NSMakeRange(curpos, 0)];
174      [tv insertNewline: self];
175      curpos++;
176      textlen++;
177      if (curpos == textlen) {
178        [self sendString: [textstring substringWithRange: NSMakeRange(outpos, textlen-outpos)]];
179        outpos = textlen;
180      }
181    } else if (curlen > 0) {
182      [tv setSelectedRange: NSMakeRange(textlen,0)];
183      [tv insertText: [textstring substringWithRange: r]];
184      [tv scrollRangeToVisible: NSMakeRange([textbuf length],0)];
185    }
186  }
187}
188
189- (void) close {
190  [[NSNotificationCenter defaultCenter] removeObserver: self];
191  [[AltConsoleDocumentController sharedDocumentController]
192    remove_console_document];
193  if (watchdog) {
194    [watchdog invalidate];
195    watchdog = nil;
196  }
197  [super close];
198}
199 
200
201
202@end
Note: See TracBrowser for help on using the repository browser.