/* 
  This class was created to control all portions of the graphical interface
  from within logo for the NeXT.  The old logo code only has the id of
  Controller.m

  Created by :
          Gary F. Desrochers (garyd@slate.mines.colorado.edu)
          Sean Kerstiens     (skerstie@slate.mines.colorado.edu)

  Created For:
          Colorado School of Mines.

  Last edited: 
          June 22,1992

*/
#import <streams/streams.h>
#import "Controller.h"
#import <appkit/View.h>
#import <appkit/TextField.h>
#import <appkit/Text.h>
#import <stdio.h>
#import <appkit/Button.h>
#import <appkit/MenuCell.h>
#import <appkit/ButtonCell.h>
#import <appkit/ScrollView.h>
#import <appkit/Control.h>
#import "CommandScroll.h"
#import <setjmp.h>
#import "DrawView.h"


//  This is the New logo routine that sends a string to the logo part of the
//  program.(must end in a carrage return.

extern  int actOnString();

@implementation Controller
  
+new
{
    id newId = [super new];

    //  Call our initialize routine.
    return [newId initialize];
}

- sendString:(char *) cBuffer;
{
    //  sendString sends out one string to the lexical analyser.
    //  It could be changed to allow for multiple strings being sent.
    //  This is not allowed right now.
    int  retval;
    
    //  This is the Memory stream that the logo readchar will get 
    //  it's information from.
    aStream = NXOpenMemory(NULL,0,NX_READWRITE);
    if (aStream == NULL)
        {
        fprintf(stderr,"Error: STDERR NXStream not made.\n");
        return self;
        }

    //  Print the temporary buffer to the Stream.
    NXPrintf(aStream,"%s\0",cBuffer);

    //  Now send it.  If it returns with a 0 that means it was a bye 
    //  statement so quit.  
    if ((retval=actOnString(aStream,self)))
        {
        [self flush];
        [idQuit performClick:self];
        }

    //  Now close the memory stream.
    NXCloseMemory(aStream, NX_FREEBUFFER);
    return [self flush];
}

-initialize
{
   // Initialize now only sets up stdout to print to the character array
   // tbuff.
   setbuf(stdout,tbuff);
   return self;
}

- drawTurtle:(int)hide: (double)angle :(double)tox :(double)toy;
{
    //  This is for comunication to the DrawView routine to tell it to 
    //  display or erase the turtle.
    return [idDrawing drawTurtleWith:angle :tox: toy: hide];
}

- drawLine:(double)type :(double)fromx :(double)fromy :(double)tox: (double)toy;
{
    //  This comunicates to the DrawView to draw the line needed.
    return [idDrawing drawLine:type :fromx :fromy :tox: toy];
}

- clearScreen
{
     //  This comunicates to the DrawView when to clear the screen.
    [idDrawing clearScreen];
    return self;
}

- flush
//  flush is undefined in the logo.h file and this routine is
//  put in it's place.  There is an error here.  If the stdout 
//  gets bigger then IPBUFSIZE it will dump to the Console.
//  It shouldn't get that big unless there is some big unix command
//  being run from within logo. (Yes you can run unix stuff while
//  within logo.
{

    int index;

    //  First check if anything in it.  If there isn't then skip fflush.
    //  If there is then append it using appendString inside CommandScroll.
    //  Then clear the tbuff because otherwise the stuff inside it, on a 
    //  rewind will spill into the Console.
    //  Then rewind.
    if(tbuff[0] != '\0')
    {
        [idErrorOut appendString:tbuff];
	for(index=0;index != IPBUFSIZE;index++)
        tbuff[index] = '\0';
        rewind(stdout);
    }
    return self;
}

// CommandScroll Delegation

- userEntered:(char *)buffer;
//  This is the userEntered routine that is called from CommandScroll.
//  It gets a string and sends it to sendString within this class.
//  If the string starts with a ? then it is not sent.  Right now it is
//  Just echoed out.
{
     if(buffer[0] == '?')
     {
         return [idErrorOut appendString:buffer];
     }
     else
     {  
         return [self sendString:buffer];
     }
}

- printRequest:sender
// This sends a print request to the Draw View..
{
    return [idDrawing print];
}

@end
