home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / PLOT.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  17KB  |  556 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         GWSIM - Simulation        */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*            GNUPLOT shell          */
  14. /*             dialog box            */
  15. /*                                   */
  16. /*           QuickC/WIN 1.0          */
  17. /*                                   */
  18. /*   (include here compilers that    */
  19. /*   compiled GWSIM successfully)    */
  20. /*                                   */
  21. /*************************************/
  22.  
  23.  
  24. #include <windows.h>
  25. #include <string.h>
  26. #include <io.h>
  27. #include "globals.h"
  28. #include "defines.h"
  29. #include "strtbl.h"
  30. #include "gwsim.h"
  31. #include "simgvar.h"
  32. #include "basic.h"
  33. #include "gep2.h"
  34.  
  35. void AddXYZLst( HWND hControl, LPSTR text );
  36. void plot_text( LPSTR Buff );
  37. void label_dyn( int idx, LPSTR str );
  38. void label_dat( int idx, LPSTR str );
  39.  
  40. #pragma alloc_text( CODE16, AddXYZLst, Plot, plot_text, label_dyn, label_dat, GnuPlot )
  41.  
  42.  
  43. /* function to add column titles on the X, Y and Z lists    */
  44.  
  45. void AddXYZLst( HWND hControl, LPSTR text )
  46. {
  47.  int i;
  48.  WORD ElWidth;
  49.  HANDLE hDC;
  50.  
  51.  hDC = GetDC( hControl );
  52.  ElWidth = 5 + LOWORD( GetTextExtent( hDC, text, _fstrlen(text) ) );
  53.  SetTextJustification( hDC, 0, 0 );
  54.  ReleaseDC( hControl, hDC );
  55.  if( ElWidth > lbWidth )
  56.  {
  57.   lbWidth = ElWidth;
  58.   SendMessage( hControl, LB_SETHORIZONTALEXTENT, lbWidth, 0 );
  59.  }
  60.  SendMessage( hControl, LB_INSERTSTRING, -1, (DWORD) text );
  61. }
  62.  
  63.  
  64.  
  65. /* Function for the Plot... dialog box    */
  66.  
  67. int FAR PASCAL Plot(HWND hDlg, WORD Message, WORD wParam, LONG lParam)
  68. {
  69.  static HWND hX, hY, hZ, hZlog, hZtext, hDyn, hSs, hCont, hHidd;
  70.  static int file, type;
  71.  int  i,j;
  72.  char auxstr[NAME_L+5];
  73.  
  74.  switch(Message)
  75.  {
  76.   case WM_INITDIALOG:
  77.    /* save file and type                            */
  78.    file = plot.file;
  79.    type = plot.type;
  80.  
  81.    /* check if any data can be available            */
  82.    if( ( (totsel==0) ) && (!options.dyn) )
  83.     SendMessage( hDlg, WM_COMMAND, IDCANCEL, 0 );
  84.  
  85.    /* get handles to controls                        */
  86.    hX     = GetDlgItem( hDlg, IDC_X );
  87.    hY     = GetDlgItem( hDlg, IDC_Y );
  88.    hZ     = GetDlgItem( hDlg, IDC_Z );
  89.    hZlog  = GetDlgItem( hDlg, IDRB_3E );
  90.    hZtext = GetDlgItem( hDlg, IDSTAT_5 );
  91.    hDyn   = GetDlgItem( hDlg, IDC_DYNAMICS );
  92.    hSs    = GetDlgItem( hDlg, IDC_SS );
  93.    hCont  = GetDlgItem( hDlg, IDRB_5E );
  94.    hHidd  = GetDlgItem( hDlg, IDRB_5I );
  95.  
  96.    /* disable Z and check boxes if type is 2D */
  97.    if( type==0 )
  98.    {
  99.     EnableWindow( hZ, FALSE );
  100.     EnableWindow( hZlog, FALSE );
  101.     EnableWindow( hZtext, FALSE );
  102.     EnableWindow( hCont, FALSE );
  103.     EnableWindow( hHidd, FALSE );
  104.    }
  105.  
  106.    /* check the appropriate file radio button        */
  107.    if( ! options.dyn )
  108.    {
  109.     EnableWindow( hDyn, FALSE );
  110.     file = 1;
  111.    }
  112.    if( totsel==0 )
  113.    {
  114.     EnableWindow( hSs, FALSE );
  115.     file = 0;
  116.    }
  117.  
  118.    if( file==0 )
  119.     SendMessage( hDyn, BM_SETCHECK, (WORD) 1, 0 );
  120.    else
  121.     SendMessage( hSs, BM_SETCHECK, (WORD) 1, 0 );
  122.  
  123.  
  124.    /* check the appropriate type radio button        */
  125.    SendDlgItemMessage( hDlg, IDRB_0E+plot.type, BM_SETCHECK, (WORD) 1, 0 );
  126.  
  127.    /* check boxes                                    */
  128.    SendDlgItemMessage( hDlg, IDRB_4E, BM_SETCHECK, (WORD) plot.lines, 0 );
  129.    SendDlgItemMessage( hDlg, IDRB_2E, BM_SETCHECK, (WORD) plot.logx, 0 );
  130.    SendDlgItemMessage( hDlg, IDRB_2I, BM_SETCHECK, (WORD) plot.logy, 0 );
  131.    SendDlgItemMessage( hDlg, IDRB_3E, BM_SETCHECK, (WORD) plot.logz, 0 );
  132.    SendDlgItemMessage( hDlg, IDRB_4I, BM_SETCHECK, (WORD) plot.colour, 0 );
  133.    SendDlgItemMessage( hDlg, IDRB_5E, BM_SETCHECK, (WORD) plot.contour, 0 );
  134.    SendDlgItemMessage( hDlg, IDRB_5I, BM_SETCHECK, (WORD) plot.hidden, 0 );
  135.  
  136.    /* initialize hX, hY and hZ: add all columns        */
  137.    if( file==1 )
  138.    {
  139.     if( plot.ny > totsel )
  140.      plot.ny = 0;
  141.     else
  142.      for(i=0; i<plot.ny; i++ )
  143.       if( plot.y[i] > totsel )
  144.        for( j=i; j<plot.ny; j++ )
  145.        {
  146.         plot.y[j] = plot.y[j+1];
  147.         i--;
  148.        }
  149.     for( i=0, lbWidth=0; i<totsel; i++ )
  150.      for( j=0; j<noutpel; j++ )
  151.       if( (outpel[j].idx-1) == (unsigned int) i )
  152.       {
  153.        AddElLst( hX, outpel, j, FALSE );
  154.        AddElLst( hY, outpel, j, FALSE );
  155.        AddElLst( hZ, outpel, j, FALSE );
  156.       }
  157.    }
  158.    else
  159.    {
  160.     lbWidth=0;
  161.     AddXYZLst( hX, (LPSTR) "time" );
  162.     AddXYZLst( hY, (LPSTR) "time" );
  163.     AddXYZLst( hZ, (LPSTR) "time" );
  164.     for( i=0; i<nmetab; i++ )
  165.     {
  166.      wsprintf( (LPSTR) auxstr, "[%s]", (LPSTR) metname[i] );
  167.      AddXYZLst( hX, (LPSTR) auxstr );
  168.      AddXYZLst( hY, (LPSTR) auxstr );
  169.      AddXYZLst( hZ, (LPSTR) auxstr );
  170.     }
  171.     for( i=0; i<nsteps; i++ )
  172.     {
  173.      wsprintf( (LPSTR) auxstr, "J(%s)", (LPSTR) stepname[i] );
  174.      AddXYZLst( hX, (LPSTR) auxstr );
  175.      AddXYZLst( hY, (LPSTR) auxstr );
  176.      AddXYZLst( hZ, (LPSTR) auxstr );
  177.     }
  178.    }
  179.  
  180.    /* highlight the current selections            */
  181.    SendMessage( hX, LB_SETCURSEL, (WORD) plot.x, 0 );
  182.    SendMessage( hZ, LB_SETCURSEL, (WORD) plot.z, 0 );
  183.    for( i=0; i<plot.ny; i++)
  184.     SendMessage( hY, LB_SELITEMRANGE, 1, MAKELONG( plot.y[i], plot.y[i] ) );
  185.  
  186.    /* cause a WM_PAINT to be sent to the window    */
  187.    InvalidateRect( hDlg, NULL, FALSE );
  188.    return TRUE;
  189.  
  190.   case WM_COMMAND:
  191.    switch(wParam)
  192.    {
  193.     case IDC_Y:
  194.      if( HIWORD( lParam ) == LBN_SELCHANGE )
  195.      {
  196.       if( type!=0 )
  197.       {
  198.        for( i=0; i<plot.ny; i++ )
  199.         SendMessage( hY, LB_SELITEMRANGE, 0, MAKELONG( plot.y[i], plot.y[i] ) );
  200.       }
  201.       plot.ny = (int) SendMessage( hY, LB_GETSELITEMS, (WORD) 10, (DWORD) (int huge *) plot.y );
  202.      }
  203.      return TRUE;
  204.  
  205.  
  206.     case IDRB_0E:
  207.      if( SendDlgItemMessage( hDlg, IDRB_0E, BM_GETCHECK, 0, 0 ) )
  208.      {
  209.       type = 0;
  210.       SendMessage( hZ, LB_SETCURSEL, -1, 0 );
  211.       EnableWindow( hZ, FALSE );
  212.       EnableWindow( hZlog, FALSE );
  213.       EnableWindow( hZtext, FALSE );
  214.       EnableWindow( hCont, FALSE );
  215.       EnableWindow( hHidd, FALSE );
  216.       InvalidateRect( hDlg, NULL, FALSE );
  217.      }
  218.      return TRUE;
  219.  
  220.     case IDRB_0I:
  221.      if( SendDlgItemMessage( hDlg, IDRB_0I, BM_GETCHECK, 0, 0 ) )
  222.       if( type == 0 )
  223.       {
  224.        plot.ny = (int) SendMessage( hY, LB_GETSELITEMS, (WORD) 10, (DWORD) (int huge *) plot.y );
  225.        for( i=0; i<plot.ny; i++ )
  226.         SendMessage( hY, LB_SELITEMRANGE, 0, MAKELONG( plot.y[i], plot.y[i] ) );
  227.        EnableWindow( hZ, TRUE );
  228.        EnableWindow( hZlog, TRUE );
  229.        EnableWindow( hZtext, TRUE );
  230.        EnableWindow( hCont, TRUE );
  231.        EnableWindow( hHidd, TRUE );
  232.        InvalidateRect( hDlg, NULL, FALSE );
  233.       }
  234.       type = 1;
  235.      return TRUE;
  236.  
  237.     case IDC_SS:
  238.      if(  ( SendDlgItemMessage( hDlg, IDC_SS, BM_GETCHECK, 0, 0 ) )
  239.         &&
  240.           ( file==0 )
  241.        )
  242.      {
  243.       file=1;
  244.       SendMessage( hX, LB_RESETCONTENT, 0, 0 );
  245.       SendMessage( hY, LB_RESETCONTENT, 0, 0 );
  246.       SendMessage( hZ, LB_RESETCONTENT, 0, 0 );
  247.       for( i=0, lbWidth=0; i<totsel; i++ )
  248.        for( j=0; j<noutpel; j++ )
  249.         if( (outpel[j].idx-1) == (unsigned int) i )
  250.         {
  251.          AddElLst( hX, outpel, j, FALSE );
  252.          AddElLst( hY, outpel, j, FALSE );
  253.          AddElLst( hZ, outpel, j, FALSE );
  254.         }
  255.      }
  256.      return TRUE;
  257.  
  258.     case IDC_DYNAMICS:
  259.      if(  ( SendDlgItemMessage( hDlg, IDC_DYNAMICS, BM_GETCHECK, 0, 0 ) )
  260.         &&
  261.           ( file==1 )
  262.        )
  263.      {
  264.       file=0;
  265.       SendMessage( hX, LB_RESETCONTENT, 0, 0 );
  266.       SendMessage( hY, LB_RESETCONTENT, 0, 0 );
  267.       SendMessage( hZ, LB_RESETCONTENT, 0, 0 );
  268.       lbWidth=0;
  269.       AddXYZLst( hX, (LPSTR) "time" );
  270.       AddXYZLst( hY, (LPSTR) "time" );
  271.       AddXYZLst( hZ, (LPSTR) "time" );
  272.       for( i=0; i<nmetab; i++ )
  273.       {
  274.        wsprintf( (LPSTR) auxstr, "[%s]", (LPSTR) metname[i] );
  275.        AddXYZLst( hX, (LPSTR) auxstr );
  276.        AddXYZLst( hY, (LPSTR) auxstr );
  277.        AddXYZLst( hZ, (LPSTR) auxstr );
  278.       }
  279.       for( i=0; i<nsteps; i++ )
  280.       {
  281.        wsprintf( (LPSTR) auxstr, "J(%s)", (LPSTR) stepname[i] );
  282.        AddXYZLst( hX, (LPSTR) auxstr );
  283.        AddXYZLst( hY, (LPSTR) auxstr );
  284.        AddXYZLst( hZ, (LPSTR) auxstr );
  285.       }
  286.      }
  287.      return TRUE;
  288.  
  289.     case IDC_HELP:            /* Help on this Dialog Box                */
  290.        WinHelp( hDlg, (LPSTR) szHelpFile, HELP_KEY, (DWORD) (LPSTR) "Plot" );
  291.         return TRUE;
  292.  
  293.     case IDOK:
  294.      /* save type and file */
  295.      plot.file = file;
  296.      plot.type = type;
  297.      /* get x, y and z */
  298.      plot.x = (int) SendMessage( hX, LB_GETCURSEL, 0, 0 );
  299.      plot.ny = (int) SendMessage( hY, LB_GETSELITEMS, (WORD) 10, (DWORD) (int huge *) plot.y );
  300.      plot.z = (int) SendMessage( hZ, LB_GETCURSEL, 0, 0 );
  301.      /* get lines, xlog, ylog and zlog */
  302.      plot.logx = (int) SendDlgItemMessage( hDlg, IDRB_2E, BM_GETCHECK, 0, 0 );
  303.      plot.logy = (int) SendDlgItemMessage( hDlg, IDRB_2I, BM_GETCHECK, 0, 0 );
  304.      plot.logz = (int) SendDlgItemMessage( hDlg, IDRB_3E, BM_GETCHECK, 0, 0 );
  305.      plot.lines = (int) SendDlgItemMessage( hDlg, IDRB_4E, BM_GETCHECK, 0, 0 );
  306.      plot.colour = (int) SendDlgItemMessage( hDlg, IDRB_4I, BM_GETCHECK, 0, 0 );
  307.      plot.contour = (int) SendDlgItemMessage( hDlg, IDRB_5E, BM_GETCHECK, 0, 0 );
  308.      plot.hidden = (int) SendDlgItemMessage( hDlg, IDRB_5I, BM_GETCHECK, 0, 0 );
  309.      /* close this dialog box */
  310.      EndDialog(hDlg, IDOK);
  311.      return TRUE;
  312.  
  313.     case IDCANCEL:
  314.      EndDialog(hDlg, IDCANCEL);
  315.      return TRUE;
  316.    }    /* End of WM_COMMAND                                 */
  317.    return FALSE;
  318.  
  319.   default:
  320.    return FALSE;
  321.  }
  322. }
  323.  
  324. /* Function of dialog box to point GnuplotExe    */
  325.  
  326. int FAR PASCAL EdGnuplot(HWND hDlg, WORD Message, WORD wParam, LONG lParam)
  327. {
  328.  switch(Message)
  329.  {
  330.   case WM_INITDIALOG:
  331.    return TRUE;
  332.  
  333.   case WM_COMMAND:
  334.    switch(wParam)
  335.    {
  336.     case IDC_HELP:            /* Help on this Dialog Box                */
  337.         WinHelp( hDlg, (LPSTR) szHelpFile, HELP_KEY, (DWORD) (LPSTR) "Plot" );
  338.         return TRUE;
  339.  
  340.     case IDOK:
  341.      /* close this dialog box */
  342.      EndDialog(hDlg, IDOK);
  343.      return TRUE;
  344.  
  345.     case IDCANCEL:
  346.      EndDialog(hDlg, IDCANCEL);
  347.      return TRUE;
  348.    }    /* End of WM_COMMAND                                 */
  349.    return FALSE;
  350.  
  351.   default:
  352.    return FALSE;
  353.  }
  354. }
  355.  
  356.  
  357. /* sets the labels for a plot of the dynamics */
  358.  
  359. void label_dyn( int idx, LPSTR str )
  360. {
  361.  if( idx == 0 )
  362.   wsprintf( (LPSTR) str, "'time'" );
  363.  else
  364.   if( idx <= nmetab )
  365.    wsprintf( (LPSTR) str, "'[%s]'", (LPSTR) metname[idx-1] );
  366.    else
  367.     if( idx > nmetab )
  368.      wsprintf( (LPSTR) str, "'J(%s)'", (LPSTR) stepname[idx-1-nmetab] );
  369. }
  370.  
  371. void label_dat( int idx, LPSTR str )
  372. {
  373.  int i;
  374.  
  375.  for( i=0; i<noutpel; i++ )
  376.   if( outpel[i].idx-1 == (unsigned int) idx ) break;
  377.  wsprintf( (LPSTR) str, "'%s'", outpel[i].title );
  378. }
  379.  
  380.  
  381. /* builds the commands for gnuplot to draw the plot */
  382.  
  383. void plot_text( LPSTR Buff )
  384. {
  385.  char File[256];
  386.  char auxstr[128];
  387.  char *p;
  388.  int i, j;
  389.  
  390.  *Buff = '\0';
  391.  if( plot.ny == 1 ) lstrcat( (LPSTR) Buff, (LPSTR) "set nokey\n" );
  392.  if( plot.type != 0 )
  393.  {
  394.   lstrcat( (LPSTR) Buff, (LPSTR) "set parametric\nset ticslevel 0\n" );
  395.   lstrcat( (LPSTR) Buff, (LPSTR) "set nopolar\nset surface\n" );
  396.   lstrcat( (LPSTR) Buff, (LPSTR) "set urange [1e-20:]\nset vrange[1e-20:]\n" );
  397.   if( plot.hidden )
  398.    lstrcat( (LPSTR) Buff, (LPSTR) "set hidden3d\n" );
  399.   else
  400.    lstrcat( (LPSTR) Buff, (LPSTR) "set nohidden3d\n" );
  401.   if( plot.contour )
  402.    lstrcat( (LPSTR) Buff, (LPSTR) "set contour base\nset cntrparam bspline\n" );
  403.   else
  404.    lstrcat( (LPSTR) Buff, (LPSTR) "set nocontour\n" );
  405.  }
  406.  if( plot.logx ) lstrcat( (LPSTR) Buff, (LPSTR) "set logscale x\n" );
  407.  if( plot.logy ) lstrcat( (LPSTR) Buff, (LPSTR) "set logscale y\n" );
  408.  if( plot.logz ) lstrcat( (LPSTR) Buff, (LPSTR) "set logscale z\n" );
  409.  if( plot.file==0 )
  410.  {
  411.   lstrcpy( (LPSTR) File, (LPSTR) FileName );
  412.   if ( p = strrchr( File, '.' ) ) strcpy( p, ".dyn" );
  413.   else strcat( File, ".dyn" );
  414.   wsprintf( (LPSTR) auxstr, (LPSTR) "set title '%s'\n", (LPSTR) FileTitle );
  415.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  416.   wsprintf( (LPSTR) auxstr, (LPSTR) "set xlabel " );
  417.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  418.   label_dyn( plot.x, (LPSTR) auxstr );
  419.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  420.   lstrcat( (LPSTR) Buff, (LPSTR) "\n" );
  421.   if( plot.ny==1 )
  422.   {
  423.    wsprintf( (LPSTR) auxstr, (LPSTR) "set ylabel " );
  424.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  425.    label_dyn( plot.y[0], (LPSTR) auxstr );
  426.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  427.    lstrcat( (LPSTR) Buff, (LPSTR) "\n" );
  428.   }
  429.   if( plot.type==1 )
  430.   {
  431.    wsprintf( (LPSTR) auxstr, (LPSTR) "set zlabel " );
  432.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  433.    label_dyn( plot.z, (LPSTR) auxstr );
  434.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  435.    lstrcat( (LPSTR) Buff, (LPSTR) "\n" );
  436.   }
  437.  }
  438.  else
  439.  {
  440.   lstrcpy( (LPSTR) File, (LPSTR) DatName );
  441.   wsprintf( (LPSTR) auxstr, (LPSTR) "set title '%s'\n", (LPSTR) DatTitle );
  442.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  443.   wsprintf( (LPSTR) auxstr, (LPSTR) "set xlabel " );
  444.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  445.   label_dat( plot.x, (LPSTR) auxstr );
  446.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  447.   lstrcat( (LPSTR) Buff, (LPSTR) "\n" );
  448.   if( plot.type != 0 )
  449.   {
  450.    wsprintf( (LPSTR) auxstr, (LPSTR) "set zlabel " );
  451.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  452.    label_dat( plot.z, (LPSTR) auxstr );
  453.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  454.    lstrcat( (LPSTR) Buff, (LPSTR) "\n" );
  455.   }
  456.   if( plot.ny==1 )
  457.   {
  458.    wsprintf( (LPSTR) auxstr, (LPSTR) "set ylabel " );
  459.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  460.    label_dat( plot.y[0], (LPSTR) auxstr );
  461.    lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  462.    lstrcat( (LPSTR) Buff, (LPSTR) "\n" );
  463.   }
  464.  }
  465.  if( plot.type != 0 )  lstrcat( (LPSTR) Buff, (LPSTR) "s" );
  466.  wsprintf( (LPSTR) auxstr, (LPSTR) "plot '%s' using %d:%d", (LPSTR) File, plot.x+1, plot.y[0]+1 );
  467.  lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  468.  if( plot.ny > 1 )
  469.  {
  470.   lstrcat( (LPSTR) Buff, (LPSTR) " title " );
  471.   if( plot.file==0 ) label_dyn( plot.y[0], (LPSTR) auxstr );
  472.   else label_dat( plot.y[0], (LPSTR) auxstr );
  473.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  474.  }
  475.  if( plot.type != 0 )
  476.  {
  477.   wsprintf( (LPSTR) auxstr, (LPSTR) ":%d", plot.z+1 );
  478.   lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  479.  }
  480.  if( plot.lines ) lstrcat( (LPSTR) Buff, (LPSTR) " with lines" );
  481.  else lstrcat( (LPSTR) Buff, (LPSTR) " with dots" );
  482.  if( plot.type==0 )
  483.  {
  484.   if( plot.ny > 1 )
  485.    for( i=1; i<plot.ny; i++ )
  486.    {
  487.     wsprintf( (LPSTR) auxstr, (LPSTR) ", '%s' using %d:%d",
  488.               (LPSTR) File, plot.x+1, plot.y[i]+1 );
  489.     lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  490.     lstrcat( (LPSTR) Buff, (LPSTR) " title " );
  491.     if( plot.file==0 ) label_dyn( plot.y[i], (LPSTR) auxstr );
  492.     else label_dat( plot.y[i], (LPSTR) auxstr );
  493.     lstrcat( (LPSTR) Buff, (LPSTR) auxstr );
  494.     if( plot.lines ) lstrcat( (LPSTR) Buff, (LPSTR) " with lines" );
  495.    }
  496.  }
  497.  lstrcat( (LPSTR) Buff, (LPSTR) "\npause -1 \"Press button to close\"\n" );
  498. }
  499.  
  500.  
  501. /* launches gnuplot to draw a plot (2D or 3D) */
  502.  
  503. int GnuPlot( HWND hWnd )
  504. {
  505.  OFSTRUCT OfStruct;
  506.  int hFile, nRc;
  507.  GLOBALHANDLE hBuff;
  508.  LPSTR Buff;
  509.  char szString[256];
  510.  char GnuplotFile[256];
  511.  
  512.  hBuff = GlobalAlloc( GMEM_MOVEABLE | GMEM_DISCARDABLE, 1024 );
  513.  Buff = GlobalLock( hBuff );
  514.  
  515.  /* generate a name for temporary file    */
  516.  GetTempFileName( 0, (LPSTR) "GPL", 0, (LPSTR) GnuplotFile );
  517.  /* and add it to the list of files to delete in the end */
  518.  GlobalUnlock( hTmpF );
  519.  hTmpF = GlobalReAlloc( hTmpF, lstrlen( TmpFiles ) + lstrlen( GnuplotFile ) + 2, 0 );
  520.  TmpFiles = GlobalLock( hTmpF );
  521.  lstrcat( TmpFiles, GnuplotFile );
  522.  lstrcat( TmpFiles, (LPSTR) "+" );
  523.  /* put the gnuplot commands in Buff */
  524.  plot_text( Buff );
  525.  /* open the temporary file */
  526.  hFile = OpenFile( (LPSTR) GnuplotFile, &OfStruct, OF_CREATE | OF_WRITE | OF_SHARE_COMPAT );
  527.  {
  528.   /* write the buffer to the file and close it                            */
  529.   nRc = _lwrite( hFile, (LPSTR) Buff, (WORD) lstrlen( (LPSTR) Buff ) );
  530.   _lclose( hFile );
  531.  }
  532.  if( nRc < lstrlen( Buff ) )
  533.  {
  534.   LoadString(hInst, IDS_ERR_NO_TEMP, szString, sizeof(szString));
  535.   MessageBeep( MB_OK );
  536.   MessageBox( hWnd, szString, FileTitle, MB_ICONINFORMATION );
  537.   return IDS_ERR_NO_TEMP;
  538.  }
  539.  wsprintf( (LPSTR) szString, (LPSTR) "%s %s", (LPSTR) GnuplotExe, (LPSTR) GnuplotFile );
  540.  if( options.debug )
  541.  {
  542.   lstrcat( (LPSTR) Buff, "\n\n" );
  543.   lstrcat( (LPSTR) Buff, (LPSTR) szString );
  544.   MessageBox( hWnd, Buff, (LPSTR) GnuplotFile, MB_ICONINFORMATION );
  545.  }
  546.  nRc = WinExec( (LPSTR) szString, SW_MINIMIZE );
  547.  if( nRc<32 )
  548.  {
  549.   if( (nRc==2) || (nRc==3) )
  550.    *GnuplotExe = '\0';
  551.   else
  552.    ErrorHandler( hInst, nRc );
  553.   return nRc;
  554.  }
  555.  return 0;
  556. }