home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 329.lha / MultiPlot / source / mp.c < prev    next >
C/C++ Source or Header  |  1990-01-05  |  12KB  |  370 lines

  1. /***************************************************************************
  2. *                    Copyright (c) 1986, 87, 88 Tim Mooney                 *
  3. *                      also Copyright 1989 Alan G Baxter                   *
  4. *                                                                          *
  5. *   Data input, screen & HPLG output- TM. Interface, Plt: support & con-   *
  6. *   version files- AGB. See docs for distribution restrictions.            *
  7. ***************************************************************************/
  8.  
  9. /***************************************************************************
  10. *   mp.c - PLOT DATA EMBEDDED IN TEXT FILE
  11. *
  12. *      Mp reads a text file and extracts data from it in a way defined by
  13. *   interactive gadget selection.
  14. *   When the data are collected, mp opens a high-res screen, and plots some
  15. *   or all of the data -- again, in a user-defined way.  Some of the
  16. *   details of plotting specifications can be altered in the "get how to"
  17. *   window callable from menu selection in the main window.
  18. ****************************************************************************
  19. *   Capabilities, limitations, and implementation notes:
  20. *
  21. *      Mp can plot lines or points of varying sizes; with or without error
  22. *   bars; in 10 colors.  There is no internal limit on the number of data
  23. *   sets plotted simultaneously, or on the number of points in a single
  24. *   data set, or on the total size of all data sets.  Memory is allocated
  25. *   dynamically; when it's used up, mp will stop processing the data file
  26. *   and crash when it tries to open a new screen.
  27. *
  28. *      Points are actually drawn as squares, except for the smallest sizes.
  29. *   Mp doesn't plot any other kinds of marks.  Points may be any size; one
  30. *   may fill the screen, in fact.  All lines are solid, error bars are
  31. *   lines.
  32. *
  33. *      GetDat() can scale data on input, directed by information in the
  34. *   data file it's reading.  Although GetDat() can also extract a title and
  35. *   labels from the data file, mp makes no use of them at present.
  36. *
  37. *      Once the data are plotted on the screen, the user can zoom in and
  38. *   out, disable axes and grid, etc.  Also, the current picture can be
  39. *   dumped to a file for plotting.  The only plotter language supported
  40. *   is HPGL (Hewlett-Packard) since it's the only one I know.
  41. *
  42. *      IEEE maths libraries are used throughout.
  43. ***************************************************************************/
  44. #include <graphics/display.h>
  45. #include <libraries/dosextens.h>
  46. #include <libraries/diskfont.h>
  47. #include <exec/exec.h>
  48. #include <intuition/intuitionbase.h>
  49. #include <graphics/regions.h>
  50. #include <devices/keymap.h>
  51. #include <stdio.h>
  52. #include <workbench/startup.h>
  53. #include <graphics/gfxmacros.h>
  54. #include <graphics/gfxbase.h>
  55. #include <math.h>
  56.  
  57. #define MAIN_MODULE 1
  58.  
  59. #include "struct.h"
  60. #include "plotlim.h"
  61. #include "front.h"
  62.  
  63. struct IntuitionBase *IntuitionBase; /* Pointer aus exec */
  64. struct GfxBase *GfxBase;
  65. struct DiskfontBase  *DiskfontBase;
  66.  
  67.  
  68. int MAXVERT=512;       /* Globally declared int replaces #define to allow */
  69.                        /* switches in screen size for Australia  Vs USA   */
  70. int CHARWIDTH=8;
  71. int CHARHEIGHT=8;
  72. int LMARGIN=57;  /* CHARWIDTH x 7 */
  73. int TMARGIN=8;
  74. int BMARGIN=24;    /* CHARHEIGHT x 3 */
  75. int XMINP=57;      /* LMARGIN */
  76. int YMAXP=504;   /* MAXVERT - TMARGIN */
  77. int YMINP=24;    /* BMARGIN */
  78.  
  79. extern short firstcall;
  80. extern int GetDat();
  81. extern void plot();
  82. extern struct Screen *screen;
  83. extern struct NewScreen newscreen;
  84. extern struct NewWindow newwindow;
  85.  
  86. #define QUIT 0
  87. #define GO 1
  88. int QuitFrontFlag=GO;  /* Quit flag for Front Window requester */
  89. int KEEP_GOING=1;      /* Quit flag for the whole program which all but */
  90.                        /* closes the screen when "New" is selected.     */
  91. long IconBase;
  92. int debug = FALSE;
  93.  
  94. FFP xtic[MAXTICS],ytic[MAXTICS];
  95. short xticp[MAXTICS],yticp[MAXTICS];
  96. char filename[150];
  97. struct RastPort *p;
  98. struct Pict *Pict;
  99.  
  100. extern struct ViewPort *vp;
  101. char StartDir[150];
  102.  
  103. main(argc,argv)
  104. int argc;
  105. union {
  106.    char **args;
  107.    struct WBStartup *msg;
  108. } argv;
  109. {
  110.    struct WBArg *arg;
  111.    int xcol=1, ycol=2, ecol=0, terse=TRUE, fpal=1;
  112.    FILE *fp;
  113.    struct IntuiMessage  *p_message;         /* pointer to message */
  114.    void ProcMes();
  115.    filename[0] = 0;
  116.  
  117.    /*** PARSE ARGS ***/
  118.    if (argc != 0) { /* called from CLI */
  119.       stcgfp(StartDir,argv.args[0]);
  120.       if (argc>1)   {
  121.          if (argv.args[argc-1][0] == '?') {
  122.           printf("usage: MultiPlot [filename]\n");
  123.           exit(0);
  124.          }
  125.          else   {
  126.            strcpy(filename,argv.args[argc-1]);
  127.            strcpy(Gadget4SIBuff,filename);
  128.          }
  129.       }
  130.     }
  131.  
  132.    else { /* called from workbench */
  133.       arg = argv.msg->sm_ArgList; /* point to command */
  134.       if (arg->wa_Lock != NULL) {
  135.          getpath(arg->wa_Lock,StartDir);
  136.          if (isdev(StartDir)) strcat(StartDir,":");
  137.       }
  138.       else stcgfp(StartDir,arg->wa_Name);
  139.       arg++;
  140.       if (argv.msg->sm_NumArgs > 1) {
  141.          getpath(arg->wa_Lock,Gadget4SIBuff);
  142.          if (isdev(Gadget4SIBuff)) strcat(Gadget4SIBuff,":");
  143.          strmfp(Gadget4SIBuff,Gadget4SIBuff,arg->wa_Name);
  144.       }
  145.    }
  146.  
  147.    /*** OPEN LIBRARIES ***/
  148.    FFPLARGE = 1.0e10; FFPSMALL = 1.0e-10;
  149.    DiskfontBase = (struct DiskfontBase *) OpenLibrary("diskfont.library",0);
  150.    if( DiskfontBase == NULL )
  151.       {
  152.         printf("Can't open diskfont Library\n");
  153.         sexit (FALSE);
  154.       }
  155.    GfxBase=NULL;
  156.    GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",33);
  157.    if(GfxBase == NULL)
  158.       {
  159.         printf("Can't open GfxBase Library\n");
  160.         sexit (FALSE);
  161.       }
  162.    IntuitionBase=NULL;
  163.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",33);  /* PAL Revision */
  164.    if (IntuitionBase == NULL)
  165.       { fpal=0;
  166.         IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  167.         if(IntuitionBase == NULL) {
  168.            printf("Can't open intuition library...\n");
  169.           sexit(FALSE);
  170.         }
  171.       }
  172.    /***** DO PAL - NTSC MAGIC. FROM SETPAL_NTSC *******/
  173.    if (fpal > 0)
  174.       { if (!  ( (GfxBase->DisplayFlags) & PAL )  ) fpal=0; }
  175.    if (fpal == 0)  {
  176.        MAXVERT=400;
  177.        newscreen.Height=400;
  178.        newwindow.Height=400;
  179.     }
  180.    if (!(IconBase = OpenLibrary("icon.library", 0))) {
  181.       printf("Can't open Icon library...\n");
  182.       sexit(FALSE);
  183.    }
  184.    screen = (struct Screen *)OpenScreen(&newscreen);
  185.    vp = &screen->ViewPort;
  186.    InitColors();
  187.  
  188.    while (KEEP_GOING)
  189.    {
  190.  
  191.      /***  RESET  THE WINDOW IN CASE THIS IS NOT FIRST TIME THROUGH ***/
  192.      NewFrontWindow.Screen = screen;
  193.      NewFrontWindow.Title ="                Data Selection Window            ";
  194.      NewFrontWindow.FirstGadget = &Gadget1;
  195.      FrontWindow = (struct Window *)OpenWindow(&NewFrontWindow);
  196.      ActivateGadget(&Gadget4,FrontWindow,0);
  197.      p = FrontWindow->RPort;
  198.      PrintIText(p,&IText2,0,0);
  199.      QuitFrontFlag=GO;     /*** RESET FLAG IN CASE NOT FIRST TIME ***/
  200.      firstcall=TRUE;
  201.  
  202.  
  203.      while (QuitFrontFlag !=QUIT)
  204.       {
  205.         Wait(1l<<FrontWindow->UserPort->mp_SigBit);        /* wait for a message */
  206.         while (p_message = (struct IntuiMessage *)GetMsg(FrontWindow->UserPort))
  207.           ProcMes(p_message);
  208.  
  209.       }
  210.      SetPointer(FrontWindow,WaitSprite,26,14,-4,-4);
  211.  
  212.      strcpy(filename,Gadget4SIBuff);
  213.      xcol=Gadget1SInfo.LongInt;
  214.      ycol=Gadget2SInfo.LongInt;
  215.      ecol=Gadget3SInfo.LongInt;
  216.  
  217.      /*** OPEN FILE ***/
  218.      fp = fopen(filename,"r");
  219.      if (!fp) {
  220.         printf("Can't open %s\n",filename);
  221.         exit(1);
  222.      }
  223.  
  224.      /*** ALLOCATE/INITIALIZE MEMORY FOR struct Pict ***/
  225.      Pict = (struct Pict *)AllocMem(sizeof(struct Pict),MEMF_CLEAR);
  226.      Pict->ErrBar = (ecol != 0);
  227.      Pict->Tics = (struct Tics *)AllocMem(sizeof(struct Tics),MEMF_CLEAR);
  228.      Pict->Tics->x = xtic;Pict->Tics->y = ytic;
  229.      Pict->Tics->xp = xticp;Pict->Tics->yp = yticp;
  230.      Pict->XRegionLock = Pict->YRegionLock = FALSE;
  231.      Pict->Axes = TRUE;
  232.      Pict->CurrReg =
  233.         (struct PlotRegion *)AllocMem(sizeof(struct PlotRegion),MEMF_CLEAR);
  234.      Pict->NewReg =
  235.         (struct PlotRegion *)AllocMem(sizeof(struct PlotRegion),MEMF_CLEAR);
  236.      Pict->Title = NULL;
  237.      Pict->XLabel = NULL;
  238.      Pict->YLabel = NULL;
  239.      Pict->Plot = NULL;
  240.  
  241.      /*** GET DATA FROM FILE ***/
  242.      GetDat(fp, xcol, ycol, ecol, terse, Pict);
  243.      if (debug) printf("MultiPlot: closing data file\n");
  244.      (void) fclose(fp);
  245.  
  246.  
  247.      /*** PLOT DATA ***/
  248.      CloseWindow(FrontWindow);
  249.      ClearPointer(FrontWindow);
  250.  
  251.      plot(Pict);
  252.  
  253.      /*** DEALLOCATE MEMORY ***/
  254.      FreeMem(Pict->NewReg,sizeof(struct PlotRegion));
  255.      FreeMem(Pict->CurrReg,sizeof(struct PlotRegion));
  256.      FreeMem(Pict->Tics,sizeof(struct Tics));
  257.      FreeMem(Pict,sizeof(struct Pict));
  258.      FreeStructPlot();
  259.    }
  260.  
  261.    CloseScreen(screen);
  262.    sexit(TRUE);
  263. }
  264.  
  265. int isdev(string)
  266. char *string;
  267. {
  268. while ((*string!=':') && (*string!='\0')) string++;
  269. if (*string==':') return(FALSE);
  270. else return(TRUE);
  271. }
  272.  
  273.  
  274.  
  275.  
  276. void ProcMes(p_message)
  277. struct IntuiMessage *p_message;
  278. {
  279. ULONG MesClass;        /*     Fields for storing      */
  280. USHORT MesCode;        /*     intuimessage data       */
  281. APTR Pointer;          /*                             */
  282. int HandleEvent();
  283.  
  284.    MesClass = p_message->Class;             /* Store values */
  285.    MesCode = p_message->Code;
  286.    Pointer = p_message->IAddress;
  287.    ReplyMsg(p_message);                     /* Reply to message */
  288.    HandleEvent(MesClass,MesCode,Pointer);
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295. int HandleEvent(MesClass,MesCode,Pointer)
  296. ULONG MesClass;        /*     Fields for storing      */
  297. USHORT MesCode;        /*     intuimessage data       */
  298. APTR Pointer;          /*                             */
  299. {
  300. FILE *fp;
  301. struct Process  *OurTask;
  302. struct Window   *old_pr_WindowPtr;
  303. static char drive[150], path[100], node[30], extn[20];
  304.  
  305.  
  306.  
  307.   if ( MesClass == GADGETDOWN)
  308.     {
  309.  
  310.       if (Pointer == (APTR)&Gadget6)
  311.          {
  312.               if (fp = fopen(Gadget4SIBuff,"r") )
  313.                 {
  314.                    fclose(fp);
  315.                    QuitFrontFlag = QUIT;
  316.                 }
  317.               else Message("     Can't open file        ");
  318.          }
  319.       if (Pointer == (APTR)&Gadget5)
  320.          {
  321.           strsfn(Gadget4SIBuff,drive,path,node,extn);
  322.           strcat(drive,path);
  323.           strmfe(node,node,extn);
  324.  
  325.           OurTask = (struct Process *)FindTask(0L);
  326.           old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
  327.           OurTask->pr_WindowPtr = (APTR)FrontWindow;
  328.           if (get_fname(FrontWindow,screen,"Select File to Open...",node,drive)==NULL)
  329.                {
  330.                   OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
  331.                   return(0);
  332.                }
  333.            OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
  334.            RemoveGadget(FrontWindow,&Gadget4);
  335.            strmfp(Gadget4SIBuff,drive,node);
  336.            AddGadget(FrontWindow,&Gadget4,-1L);
  337.            RefreshGadgets(&Gadget4,FrontWindow,NULL);
  338.            DrawBorder(p,&Border2,108,97);
  339.           }
  340.        else ;
  341.     }
  342.   if ( MesClass == RAWKEY)
  343.     {
  344.       if (MesCode ==196)  /* RETURN key RELEASED */
  345.          {
  346.               if (fp = fopen(Gadget4SIBuff,"r") )
  347.                 {
  348.                    fclose(fp);
  349.                    QuitFrontFlag = QUIT;
  350.                 }
  351.               else Message("     Can't open file        ");
  352.          }
  353.        else ;
  354.     }
  355.    else ;
  356.    return(1);
  357. }
  358.  
  359. sexit(Err)
  360. int Err;
  361. {
  362. if (IconBase) CloseLibrary(IconBase);
  363. if (IntuitionBase) CloseLibrary(IntuitionBase);
  364. if (GfxBase) CloseLibrary(GfxBase);
  365. if (DiskfontBase) CloseLibrary(DiskfontBase);
  366. exit(Err);
  367. }
  368.  
  369.  
  370.