
/* Generated by Interface Builder */

/*
 * Some of this code is essentially copied from the AtYourService example
 * of Henry Krempel.
 */

#import "PlotDelegate.h"
#import "ServicesHandler.h"
#import "Plot.h"
#import "FormatHandler.h"
#import "PlotView.h"
#import "ScrollWindow.h"             // added 10-7-91 by pdhowell
#import <strings.h>
#import <appkit/appkit.h>
#import <defaults/defaults.h>
#import <streams/streams.h>
#import <sys/types.h>		/* only needed in the iconReleasedAt::: code */
#import <sys/stat.h>		/* ditto */

@implementation PlotDelegate

- appWillInit:sender
{
  // We used to do this in appDidInit, but ran into trouble when we got
  // the Workspace Manager to automatically run nxyplot on files with a
  // selected extension.  The problem was that the 3 windows which get
  // turned into ScrollWindows by the makeSomeScrollWindows method had not been
  // turned into ScrollWindows when a message was sent to them that only
  // a ScrollView (not an ordinary View) could understand.  The solution seems
  // to be to make them into ScrollWindows very early in the game, in the
  // appWillInit method.
  [plotParam makeSomeScrollWindows];

  NXUpdateDefaults();
  if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
    [plotParam colorOn:YES];
    [colorOptionButton setState:1];
  }
  else {
    [plotParam colorOn:NO];
    [colorOptionButton setState:0];
  }
  if (strncmp(NXGetDefaultValue("nxyplot", "colorPrinting"), "YES", 3) == 0) {
    [printColorButton setState:1];
    [accPrintColorButton setState:1];
  }
  else {
    [printColorButton setState:0];
    [accPrintColorButton setState:0];
  }
  if (strncmp(NXGetDefaultValue("nxyplot", "cycleLineStyles"), "YES", 3) == 0) {
    [printLineStyleButton setState:1];
    [accPrintLineStyleButton setState:1];
  }
  else {
    [printLineStyleButton setState:0];
    [accPrintLineStyleButton setState:0];
  }
  if (strncmp(NXGetDefaultValue("nxyplot", "opaqueBackground"), "YES", 3) == 0) {
    [opaqueBackgroundButton setState:0];
  }
  else {
    [opaqueBackgroundButton setState:1];
  }

  return self;
}

- appDidInit:sender
{

  [[NXApp appListener] setServicesDelegate:self];
  [servicesHandler serviceSetState];

  [canvasWindow setMiniwindowIcon:"nxyplot.tiff"];
  [controlPanel setMiniwindowIcon:"nxyplot.tiff"];

  /*
   * Look for command line arguments (this used to be in nxyplot_main.m).
   */
  if (NXArgc > 1) {
    [self getArgs];
  }

  return self;
}

- resetDefaults:sender
{
  if ([colorOptionButton state] == 0) {
    NXWriteDefault("nxyplot", "colorOption", "NO");
    [plotParam colorOn:NO];
  }
  else {
    NXWriteDefault("nxyplot", "colorOption", "YES");
    [plotParam colorOn:YES];
  }
  if ([printColorButton state] == 0) {
    NXWriteDefault("nxyplot", "colorPrinting", "NO");
    [accPrintColorButton setState:0];
  }
  else {
    NXWriteDefault("nxyplot", "colorPrinting", "YES");
    [accPrintColorButton setState:1];
  }
  if ([printLineStyleButton state] == 0) {
    NXWriteDefault("nxyplot", "cycleLineStyles", "NO");
    [accPrintLineStyleButton setState:0];
  }
  else {
    NXWriteDefault("nxyplot", "cycleLineStyles", "YES");
    [accPrintLineStyleButton setState:1];
  }
  if ([opaqueBackgroundButton state] == 0) {
    NXWriteDefault("nxyplot", "opaqueBackground", "YES");
  }
  else {
    NXWriteDefault("nxyplot", "opaqueBackground", "NO");
  }

  return self;
}


/* This method takes data from a pasteboard and sends it to be plotted. */
- plotFromPasteboard:pb userData:(const char *)userData error:(char **)errorMessage
{
  char *data;
  int length;
  const char *const *types;
  int hasAscii, i;

  types = [pb types];
  hasAscii = 0;
  for (i = 0; !hasAscii && types[i] ; i++) 
    if (!strcmp(types[i], NXAsciiPboardType)
	|| !strcmp(types[i], NXTabularTextPboardType)) hasAscii = 1;

  if (hasAscii) {
    [pb readType:NXAsciiPboardType data:&data length:&length];

    if (data && length) {
      NXStream *dataStream = NXOpenMemory(data, length, NX_READONLY);
      if ([plotParam readData:dataStream :"pasteboard"] == 0) {
	NXRunAlertPanel("Read", "Couldn't read any data from %s", "OK",
			NULL, NULL, "pasteboard");
      }
      [plotParam plotPrepAndDraw];
    }
    else *errorMessage = "No data in your selection.";
  } 
  else *errorMessage = "No ASCII text found in your selection.";

  return self;
}

// The next 2 methods suggested by Ralph Zazula.  They are needed to get
// WorkspaceManager automatically to run nxyplot when you double-click on
// files with our registered extension(s).

- (BOOL)appAcceptsAnotherFile:sender
{
  return YES;
}

- (int)app:sender openFile:(const char *)fullPath type:(const char *)aType
{
  [plotParam openFile:(char *)fullPath :(char *)fullPath];
  return YES;
}

- getArgs
{
  int i, formatindex = 0, psindex = 0;
  NXStream *formatStream;
  BOOL quit = NO;

  [plotParam makeSomeScrollWindows]; /* must do this here! */
  NXUpdateDefaults();		/* ditto */
  if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
    [plotParam colorOn:YES];
    [colorOptionButton setState:1];
  }
  else {
    [plotParam colorOn:NO];
    [colorOptionButton setState:0];
  }

  i = 1;
  while (i<NXArgc) {
    if (NXArgv[i][0] == '-') {
      if (NXArgv[i][1] == 'f') { /* format file */
	formatindex = ++i;
      }
      else if (NXArgv[i][1] == 'o') { /* eps file */
	psindex = ++i;
      }
      else if (NXArgv[i][1] == 'q') { /* quit after plotting */
	i++;
	quit = YES;
      }
      else if (NXArgv[i][1] == 'h') {
	NXLogError("usage: nxyplot file [file...] [-f formatfile] [-o epsfile] [-q] [-h]\n");
	exit(0);
      }
      else
	NXLogError("Unrecognized option %s\n", NXArgv[i++]);
    }
    else {
      if (NXMapFile(NXArgv[i], NX_READONLY) == NULL) {
	NXLogError("Unable to open file %s\n", NXArgv[i]);
      }
      else {
	[plotParam openFile:NXArgv[i] :NXArgv[i]];
      }
    }
    i++;
  }

  if (formatindex > 0)
    if ((formatStream = NXMapFile(NXArgv[formatindex], NX_READONLY)) == NULL)
      NXLogError("Unable to open format file %s\n", NXArgv[formatindex]);
    else
      [formatHandler readFormatData:formatStream];

  if (psindex > 0)
    [canvas savePSCode:NXArgv[psindex]];

  if (quit) {
    NXLogError("nxyplot: quit option selected\n");
    [NXApp free];
    exit(0);
  }
  else
    [plotParam drawPlot:self];

  return self;
}

// Next method copied from Garfinkel and Mahoney (Chapter 6)
- showInfo:sender
{
  if (infoPanel == nil) {
    if (![NXApp loadNibSection: "info.nib"
		owner:self
		withNames: NO]) {
      return nil;		// load failed
    }
  }
  [infoPanel makeKeyAndOrderFront:nil];
  return self;
}

- showHelp:sender
{ 
  if (helpPanel == nil) {
    if (![NXApp loadNibSection: "help.nib"
		owner:self
		withNames: NO]) {
      return nil;		// load failed
    }
  }
  [helpPanel makeKeyAndOrderFront:nil];
  return self;
}

@end
