home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Plotting / nxyplot / Source / PlotDelegate.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  6.7 KB  |  270 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. /*
  5.  * Some of this code is essentially copied from the AtYourService example
  6.  * of Henry Krempel.
  7.  */
  8.  
  9. #import "PlotDelegate.h"
  10. #import "ServicesHandler.h"
  11. #import "Plot.h"
  12. #import "FormatHandler.h"
  13. #import "PlotView.h"
  14. #import "ScrollWindow.h"             // added 10-7-91 by pdhowell
  15. #import <strings.h>
  16. #import <appkit/appkit.h>
  17. #import <defaults/defaults.h>
  18. #import <streams/streams.h>
  19. #import <sys/types.h>        /* only needed in the iconReleasedAt::: code */
  20. #import <sys/stat.h>        /* ditto */
  21.  
  22. @implementation PlotDelegate
  23.  
  24. - appWillInit:sender
  25. {
  26.   // We used to do this in appDidInit, but ran into trouble when we got
  27.   // the Workspace Manager to automatically run nxyplot on files with a
  28.   // selected extension.  The problem was that the 3 windows which get
  29.   // turned into ScrollWindows by the makeSomeScrollWindows method had not been
  30.   // turned into ScrollWindows when a message was sent to them that only
  31.   // a ScrollView (not an ordinary View) could understand.  The solution seems
  32.   // to be to make them into ScrollWindows very early in the game, in the
  33.   // appWillInit method.
  34.   [plotParam makeSomeScrollWindows];
  35.  
  36.   NXUpdateDefaults();
  37.   if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
  38.     [plotParam colorOn:YES];
  39.     [colorOptionButton setState:1];
  40.   }
  41.   else {
  42.     [plotParam colorOn:NO];
  43.     [colorOptionButton setState:0];
  44.   }
  45.   if (strncmp(NXGetDefaultValue("nxyplot", "colorPrinting"), "YES", 3) == 0) {
  46.     [printColorButton setState:1];
  47.     [accPrintColorButton setState:1];
  48.   }
  49.   else {
  50.     [printColorButton setState:0];
  51.     [accPrintColorButton setState:0];
  52.   }
  53.   if (strncmp(NXGetDefaultValue("nxyplot", "cycleLineStyles"), "YES", 3) == 0) {
  54.     [printLineStyleButton setState:1];
  55.     [accPrintLineStyleButton setState:1];
  56.   }
  57.   else {
  58.     [printLineStyleButton setState:0];
  59.     [accPrintLineStyleButton setState:0];
  60.   }
  61.   if (strncmp(NXGetDefaultValue("nxyplot", "opaqueBackground"), "YES", 3) == 0) {
  62.     [opaqueBackgroundButton setState:0];
  63.   }
  64.   else {
  65.     [opaqueBackgroundButton setState:1];
  66.   }
  67.  
  68.   return self;
  69. }
  70.  
  71. - appDidInit:sender
  72. {
  73.  
  74.   [[NXApp appListener] setServicesDelegate:self];
  75.   [servicesHandler serviceSetState];
  76.  
  77.   [canvasWindow setMiniwindowIcon:"nxyplot.tiff"];
  78.   [controlPanel setMiniwindowIcon:"nxyplot.tiff"];
  79.  
  80.   /*
  81.    * Look for command line arguments (this used to be in nxyplot_main.m).
  82.    */
  83.   if (NXArgc > 1) {
  84.     [self getArgs];
  85.   }
  86.  
  87.   return self;
  88. }
  89.  
  90. - resetDefaults:sender
  91. {
  92.   if ([colorOptionButton state] == 0) {
  93.     NXWriteDefault("nxyplot", "colorOption", "NO");
  94.     [plotParam colorOn:NO];
  95.   }
  96.   else {
  97.     NXWriteDefault("nxyplot", "colorOption", "YES");
  98.     [plotParam colorOn:YES];
  99.   }
  100.   if ([printColorButton state] == 0) {
  101.     NXWriteDefault("nxyplot", "colorPrinting", "NO");
  102.     [accPrintColorButton setState:0];
  103.   }
  104.   else {
  105.     NXWriteDefault("nxyplot", "colorPrinting", "YES");
  106.     [accPrintColorButton setState:1];
  107.   }
  108.   if ([printLineStyleButton state] == 0) {
  109.     NXWriteDefault("nxyplot", "cycleLineStyles", "NO");
  110.     [accPrintLineStyleButton setState:0];
  111.   }
  112.   else {
  113.     NXWriteDefault("nxyplot", "cycleLineStyles", "YES");
  114.     [accPrintLineStyleButton setState:1];
  115.   }
  116.   if ([opaqueBackgroundButton state] == 0) {
  117.     NXWriteDefault("nxyplot", "opaqueBackground", "YES");
  118.   }
  119.   else {
  120.     NXWriteDefault("nxyplot", "opaqueBackground", "NO");
  121.   }
  122.  
  123.   return self;
  124. }
  125.  
  126.  
  127. /* This method takes data from a pasteboard and sends it to be plotted. */
  128. - plotFromPasteboard:pb userData:(const char *)userData error:(char **)errorMessage
  129. {
  130.   char *data;
  131.   int length;
  132.   const char *const *types;
  133.   int hasAscii, i;
  134.  
  135.   types = [pb types];
  136.   hasAscii = 0;
  137.   for (i = 0; !hasAscii && types[i] ; i++) 
  138.     if (!strcmp(types[i], NXAsciiPboardType)
  139.     || !strcmp(types[i], NXTabularTextPboardType)) hasAscii = 1;
  140.  
  141.   if (hasAscii) {
  142.     [pb readType:NXAsciiPboardType data:&data length:&length];
  143.  
  144.     if (data && length) {
  145.       NXStream *dataStream = NXOpenMemory(data, length, NX_READONLY);
  146.       if ([plotParam readData:dataStream :"pasteboard"] == 0) {
  147.     NXRunAlertPanel("Read", "Couldn't read any data from %s", "OK",
  148.             NULL, NULL, "pasteboard");
  149.       }
  150.       [plotParam plotPrepAndDraw];
  151.     }
  152.     else *errorMessage = "No data in your selection.";
  153.   } 
  154.   else *errorMessage = "No ASCII text found in your selection.";
  155.  
  156.   return self;
  157. }
  158.  
  159. // The next 2 methods suggested by Ralph Zazula.  They are needed to get
  160. // WorkspaceManager automatically to run nxyplot when you double-click on
  161. // files with our registered extension(s).
  162.  
  163. - (BOOL)appAcceptsAnotherFile:sender
  164. {
  165.   return YES;
  166. }
  167.  
  168. - (int)app:sender openFile:(const char *)fullPath type:(const char *)aType
  169. {
  170.   [plotParam openFile:(char *)fullPath :(char *)fullPath];
  171.   return YES;
  172. }
  173.  
  174. - getArgs
  175. {
  176.   int i, formatindex = 0, psindex = 0;
  177.   NXStream *formatStream;
  178.   BOOL quit = NO;
  179.  
  180.   [plotParam makeSomeScrollWindows]; /* must do this here! */
  181.   NXUpdateDefaults();        /* ditto */
  182.   if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
  183.     [plotParam colorOn:YES];
  184.     [colorOptionButton setState:1];
  185.   }
  186.   else {
  187.     [plotParam colorOn:NO];
  188.     [colorOptionButton setState:0];
  189.   }
  190.  
  191.   i = 1;
  192.   while (i<NXArgc) {
  193.     if (NXArgv[i][0] == '-') {
  194.       if (NXArgv[i][1] == 'f') { /* format file */
  195.     formatindex = ++i;
  196.       }
  197.       else if (NXArgv[i][1] == 'o') { /* eps file */
  198.     psindex = ++i;
  199.       }
  200.       else if (NXArgv[i][1] == 'q') { /* quit after plotting */
  201.     i++;
  202.     quit = YES;
  203.       }
  204.       else if (NXArgv[i][1] == 'h') {
  205.     NXLogError("usage: nxyplot file [file...] [-f formatfile] [-o epsfile] [-q] [-h]\n");
  206.     exit(0);
  207.       }
  208.       else
  209.     NXLogError("Unrecognized option %s\n", NXArgv[i++]);
  210.     }
  211.     else {
  212.       if (NXMapFile(NXArgv[i], NX_READONLY) == NULL) {
  213.     NXLogError("Unable to open file %s\n", NXArgv[i]);
  214.       }
  215.       else {
  216.     [plotParam openFile:NXArgv[i] :NXArgv[i]];
  217.       }
  218.     }
  219.     i++;
  220.   }
  221.  
  222.   if (formatindex > 0)
  223.     if ((formatStream = NXMapFile(NXArgv[formatindex], NX_READONLY)) == NULL)
  224.       NXLogError("Unable to open format file %s\n", NXArgv[formatindex]);
  225.     else
  226.       [formatHandler readFormatData:formatStream];
  227.  
  228.   if (psindex > 0)
  229.     [canvas savePSCode:NXArgv[psindex]];
  230.  
  231.   if (quit) {
  232.     NXLogError("nxyplot: quit option selected\n");
  233.     [NXApp free];
  234.     exit(0);
  235.   }
  236.   else
  237.     [plotParam drawPlot:self];
  238.  
  239.   return self;
  240. }
  241.  
  242. // Next method copied from Garfinkel and Mahoney (Chapter 6)
  243. - showInfo:sender
  244. {
  245.   if (infoPanel == nil) {
  246.     if (![NXApp loadNibSection: "info.nib"
  247.         owner:self
  248.         withNames: NO]) {
  249.       return nil;        // load failed
  250.     }
  251.   }
  252.   [infoPanel makeKeyAndOrderFront:nil];
  253.   return self;
  254. }
  255.  
  256. - showHelp:sender
  257.   if (helpPanel == nil) {
  258.     if (![NXApp loadNibSection: "help.nib"
  259.         owner:self
  260.         withNames: NO]) {
  261.       return nil;        // load failed
  262.     }
  263.   }
  264.   [helpPanel makeKeyAndOrderFront:nil];
  265.   return self;
  266. }
  267.  
  268. @end
  269.