next up previous contents
Next: copying and printing Views Up: RECView.m program listing Previous: RECView.m program listing

declarations

The declarations shown below contain several extraneous elements, and are only included for consistency with the rest of the program, in case the definition of terms used elsewhere is needed.

/* 
 * RECView.m -- Implementation file for the RECView class 
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or implied, as to 
 * its fitness for any particular use, or that it even has a use.
 *
 */

#import "rec.h"
#import "recdtbl.h"
#import "RECView.h"
#import <NXCType.h>
#import <math.h>
#import <stdlib.h>
#import <string.h>

# define CLEN  600	/* buffer length for rec source        */
# define PLEN 2000	/* buffer length for rec object        */
# define HMAX   50	/* maximum depth pushdown stack        */
# define CMAX   10	/* number of saved constants           */
# define  NMU    5	/* number of items in first column     */
# define   NY    8	/* number of demonstration programs    */
# define  NIP   12	/* number of initial polygons          */
# define  NSK    5	/* number of prototype skeletons       */
# define PI 3.14159

//int  rec(char *z);

char   cstr[CLEN] = { " (()) " };       /* console rec program     */
Inst   rprg[PLEN];                      /* rec program array       */
int    phil;                            /* length of cstr	   */
char   idef[1][30] = { " (()) " };  //(z(!100!0r:;)1r2r(0r:;);) a€" };
char   recrule[NY][CLEN] = {
	" ([pentagonal] X5 P CN CN CN CN C p ;)",
	" ([bipentag] X5 P CN CN CN Cn Cn Cn Cn C p ;)",
	" ([two sectors] B C R2 C R1 C N C ;)"
	};
char   skeletons[NSK][CLEN] = {
	" ([the polygon] B C ;)"
	" ([top polygon] O C ;)",
	" ([sector] O (!n! C N :;) ;)",
	" ([binary] O (!n-1! C N :;) (!n-1! C n :;);)",
	" ([second] O (!n! C N :;) ;)"
	};

int    isErrm=FALSE;
int    drawBack=YES;
int    exSource=YES;
int    options[4]={FALSE,FALSE,FALSE,FALSE};
char   errm[50];
double cgrad=0.15;    /* threshold in tanh compression                    */
double gmulp=10.0;    /* scale factor turning complex numbers into pixels */
double qsize=1.50;    /* size of small pixel squares on screen            */

double exoff=0.0, wyoff=0.0;
extern double r_dblpar;
int    plev=0;                                           /* pushdown depth  */
double constant[CMAX][2];                                /* saved constants */
double thestack[HMAX][2];                                /* operating stack */

@implementation RECView

/* ---------------  A P P   I N I T I A L I Z A T I O N   ----------------- */

- appWillInit:sender {printf("appWillInit = REC/C\n"); return self;}

- appDidInit:sender {
 char c[3], fecha[30], tiempo[30], *df;
 int i; 
 struct tm  *timeptr;
 time_t  secsnow;
 printf("appDidInit = REC/C for Complex Arithmetic\n");

   time(&secsnow);
   timeptr = localtime(&secsnow);
   sprintf(fecha,"%d-%d-19%02d\n",
	((timeptr->tm_mon) + 1), 
	timeptr->tm_mday,
        timeptr->tm_year);
   [tiDat setStringValue: fecha];

   sprintf(tiempo,"%02d:%02d:%02d\n",
	timeptr->tm_hour, 
	timeptr->tm_min,
        timeptr->tm_sec);
//   [titime setStringValue: tiempo];

///* initialize r_vst with select standard procedures */
//   rec_c(idef[0],xprg[0],XLEN,dtbl);
//   r_vst['a']=xprg[0];

  [browser getTitleFromPreviousColumn: YES];
  [browser setDoubleAction: @selector(browserDoubleClick:)];
  [browser setPath: "/demonstrations"];

  [self loadExternal: self];

  return self;
}

- appWillTerminate:sender {printf("goodbye\n"); return self;}

/* ----------------------------- G E N E R A L ----------------------------- */

/* = = = = = = =   P a r a m e t e r   A d j u s t m e n t   = = = = = = = */

- rexsli:sender {int i;					/* set x-offset */
	exoff=120.0*[sender doubleValue];
 	[planeView display];
return self;
}

- rwysli:sender {					/* set y-offset */
	wyoff=120.0*[sender doubleValue];
 	[planeView display];
return self;
}

/* = = = = = = = = = =   P r o g r a m   E d i t o r   = = = = = = = = = = */

- recMatrix:sender { printf("selected row = %3d\n ",[sender selectedRow]);
  [comment setStringValue: 
	dtbl[ [sender selectedRow] * 32 + [sender selectedCol] ].r_cmnt];
return self;
}

- ascii:sender {
  dtbl[ [sender selectedRow] * 32 + [sender selectedCol] ].xfun();
return self;
}

/* = = = = = = = = =   P r o g r a m   E x e c u t o r   = = = = = = = = = */

- setOption:sender {int i;
  i=[sender selectedCol];
  options[i]=[[sender selectedCell] state];
return self;
}

- run:sender {[planeView display]; return self;}

- recompile:sender {
  [execWin getSubstring: cstr start: 0 length: [execWin textLength]];
  [window setDocEdited:YES];
return self;
}

- browserDoubleClick:sender {int i, j, k; 
   if (strcmp([browser titleOfColumn: [browser selectedColumn]],
	"initial polygons") == 0) {}
   else if (strcmp([browser titleOfColumn: [browser selectedColumn]],
	"demonstrations") == 0) {
   	[RECValue setStringValue: " "];
   	[execLine setStringValue: [sender stringValue]];
  	strcpy(cstr,[sender stringValue]);
	}
   else if (strcmp([browser titleOfColumn: [browser selectedColumn]],
	"skeletons") == 0) {
   	[RECValue setStringValue: " "];
   	[execLine setStringValue: [sender stringValue]];
	}
   [planeView display];
return self;
  }

- gho:sender {
   strcpy(cstr,[sender stringValue]);
//printf("went and ghoed: %s\n",[sender stringValue]);
//printf("went and ghoed: %s\n",cstr);
   [planeView display];
   return self;
  }

/* - - - - - - - -   F I L E   A N D   D I R E C T O R Y   - - - - - - - - */

- setFilename:(const char *)aFilename {
if (filename) free(filename);
filename = malloc(strlen(aFilename)+1);
strcpy(filename, aFilename);
[window setTitleAsFilename:aFilename];
return self;
}

- open:sender {
  char *types[2] = {"rec",0};
  int  i, fd;
  NXStream *theStream;

  [[OpenPanel new] allowMultipleFiles: NO];
  [[OpenPanel new] chooseDirectories: NO];

  if ([ [OpenPanel new] runModalForTypes:types]) {
    [self setFilename: [[OpenPanel new] filename]];
    fd = open(filename, O_RDONLY, 06666);
    theStream = NXOpenFile(fd, NX_READONLY);
//      NXScanf(theStream,"%s",cstr);
      i=0;
      while (NXScanf(theStream,"%c",&cstr[i])!=EOF) i++;
      phil=strlen(cstr);
    NXClose(theStream);
    close(fd);
    [window setTitleAsFilename: filename];
    [window setDocEdited:NO];
  [execWin setSel: 0: [execWin textLength]];
  [execWin replaceSel: cstr];
  [execWin scrollSelToVisible];
  [execWin display];
    }
return self;
}

- saveAs:sender {
  id panel;
  const char *dir;
  char *file;

/* prompt user for the file name and save to that file */

  if (filename==0) {
    /* no filename; set up defaults */ 
    dir  = NXHomeDirectory();
    file = (char *)[window title];
    } else {
    file=rindex(filename,'/');
    if (file) {
      dir   = filename;
      *file = 0;
      file++;
      } else {
      dir  = filename;
      file = (char *)[window title];
      }
    }
  panel = [SavePanel new]; 
  [panel setRequiredFileType: ""];
  if ([panel runModalForDirectory: dir file: file]) {
    [self setFilename: [panel filename]];
    return [self save: sender];
    }
    return nil;  /* didn't save */
  }
    
- save:sender {
  int      fd;
  NXStream *theStream;

  if (filename==0) return [self saveAs: sender];
  [window setTitle: "Saving ..."];

  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 06666);
  if (fd < 0) {
    NXRunAlertPanel(0, "Cannot save file: %s",0,0,0,strerror(errno));
    return self;
    }
  theStream = NXOpenFile(fd, NX_WRITEONLY);
  [execWin writeText: theStream];
  NXClose(theStream);
  close(fd);
  [window setTitleAsFilename: filename];
  [window setDocEdited:NO];
  return self;
}

- close:sender {const char *fname;
int         q;
  if ([window isDocEdited])
//    fname=filename ? filename : [sender title];
//    if (rindex(fname,'/')) fname = rindex(fname,'/') + 1;
    q = NXRunAlertPanel(
      "Save",
      "Save changes to %s?",
      "Save",
      "Don't Save",
      "Cancel",
      filename);
    if (q==1) {if (![self save:nil]) return nil;}
    if(q==-1) return nil;
//  [sender setDelegate: nil];
//  [proc free];
//  [self free];
  return self;
}

- windowWillClose:sender {
[self close: self];
printf("windowWillClose\n");
return self;
}

/*******   REC program management ********/

- loadRECPanel:sender {
	if ([sender state]) [self loadInternal: sender]; 
	else                [self loadExternal: sender];}

- loadInternal:sender {
  printf("Internal\n ");
  [self loadLowerPaneltwo: internalRECBox ];
  return self;
  }

- loadExternal:sender { 
  printf("External\n ");
  [self loadLowerPaneltwo: externalRECBox ];
  return self;
  }

- loadLowerPaneltwo:thisRuleBox {NXRect bxRect, loRect;
  [theBigBox getFrame: &bxRect];
  [thisRuleBox getFrame: &loRect];
  [theLilBox setContentView: [thisRuleBox contentView]];
  loRect.origin.x=(bxRect.size.width-loRect.size.width)/2.0;
  loRect.origin.y=(bxRect.size.height-loRect.size.height)/2.0;
  [theLilBox setFrame: &loRect];
  [theBigBox display];
  [theLilBox display];
return self;
}

- textDidChange:textObject {
  return  [windows  setDocEdited: YES];
}



Microcomputadoras
2001-02-12