home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / GWSIM.C < prev    next >
C/C++ Source or Header  |  1993-08-26  |  30KB  |  824 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. /*          Simulation window        */
  14. /*                                   */
  15. /*           QuickC/WIN 1.0          */
  16. /*                                   */
  17. /*   (include here compilers that    */
  18. /*   compiled GWSIM successfully)    */
  19. /*                                   */
  20. /*************************************/
  21.  
  22.  
  23. /*
  24.   be sure that IOTOP.C is recompiled and that
  25.   iotop.obj from a GWTOP compilation is not
  26.   being used now. To be sure, delete iotop.obj
  27. */
  28.  
  29. #include <windows.h>
  30. #include <string.h>
  31. #include <direct.h>
  32. #include "commdlg.h"
  33. #include "toolhelp.h"
  34. #include "basic.h"                        /* basic windows function prototypes    */
  35. #include "defines.h"                    /* symbols also used in .DLG files        */
  36. #include "globals.h"                    /* gepasi's own symbols                    */
  37. #include "gwsim.h"                        /* macros, function prototypes, etc.    */
  38. #include "gep2.h"                        /* gepasi's variables                    */
  39. #include "strtbl.h"                        /* symbols for the string table            */
  40. #include "iosim.h"
  41. #include "iotop.h"
  42.  
  43. #define HELP_PARTIALKEY   0x0105
  44.  
  45. /* Struct pointed to by WM_GETMINMAXINFO lParam
  46. typedef struct tagMINMAXINFO
  47. {
  48.  POINT ptReserved;
  49.  POINT ptMaxSize;
  50.  POINT ptMaxPosition;
  51.  POINT ptMinTrackSize;
  52.  POINT ptMaxTrackSize;
  53. } MINMAXINFO;                                   */
  54.  
  55. /* global variables    */
  56. char             szString[256];            /* variable to load resource strings    */
  57. char            szHelpFile[] =
  58.                 "GEPASI.HLP";            /* help file name                        */
  59. char            szAppName[20];            /* class name for the window            */
  60. HCURSOR            hHourGlass;                /* handle for the wait cursor            */
  61. WORD            lbWidth;                /* width of largest string in list box    */
  62. HANDLE            hInst;                  /* handle to this instance of the appl.    */
  63. HWND            hWndMain;                /* handle to the main window            */
  64. HWND            hButton1;                /* handle to push button                */
  65. HWND            hButton2;                /* handle to push button                */
  66. HMENU            hMenu;                    /* handle to the menu                    */
  67. GLOBALHANDLE    hPrm;                    /* handle to memory block with prm        */
  68. GLOBALHANDLE    hOel;                    /* handle to memory block w/ oel        */
  69. GLOBALHANDLE    hScp;                    /* handle to memory block w/ scp        */
  70. double huge        *prm[MAX_STEP];            /* mirror of params                        */
  71. struct    ou huge *oel;                    /* mirror of outpel                        */
  72. struct    sp huge *scp;                    /* mirror of spar                        */
  73. double            x[MAX_MET];                /* initial concentrations                 */
  74. int             notsaved;                /* 1 if file has not been saved            */
  75. int                eqefl;                    /* for compatibility with IOTOP.C        */
  76. int                lno;                    /* number of link to edit                */
  77. int             nlks;                    /* mirror of nlinks                        */
  78. char            GepasiIni[256];            /* GEPASI's INI file with full path        */
  79. char            GnuplotExe[256];        /* name of gnuplot.exe with full path    */
  80. char            CommandLine[1024];
  81. char            FileName[256];            /* filename of sim file with full path    */
  82. char            FileTitle[13];            /* filename of sim file  w/o full path    */
  83. char            TopFile[256];            /* filename of top file with full path    */
  84. char            TopTitle[13];            /* filename of top file w/o full path    */
  85. char            DatName[256];            /* filename of dat file with full path    */
  86. char            DatTitle[13];            /* filename of dat file w/o full path    */
  87. char            LogFile[256];            /* filename of log file with full path    */
  88. FARPROC            fpCheckSimEng;            /* MakeProcInstance of callback            */
  89. char            TempFile[256];            /* filename of temporary file            */
  90. int                TimerId;                /* identifies the timer                    */
  91. int                running;                /* set when simulation engine is active    */
  92.  
  93. #pragma alloc_text( CODE0, WinMain, clear_all, WndProc, RegisterClasses, DeleteTmp, CleanUp )
  94.  
  95.  
  96. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  97. {
  98.  /***********************************************************************/
  99.  /* HANDLE hInstance;       handle for this instance                    */
  100.  /* HANDLE hPrevInstance;   handle for possible previous instances      */
  101.  /* LPSTR  lpszCmdLine;     long pointer to exec command line           */
  102.  /* int    nCmdShow;        Show code for main window display           */
  103.  /***********************************************************************/
  104.  
  105.  MSG        msg;           /* MSG structure to store your messages        */
  106.  int        nRc;           /* return value from Register Classes          */
  107.  char       *ptr;
  108.  LPSTR        lp;
  109.  OFSTRUCT    OfStruct;
  110.  HANDLE        hAcc;
  111.  
  112.  strcpy(szAppName, "GWSIM");
  113.  hInst = hInstance;
  114.  if(!hPrevInstance)
  115.    {
  116.     /* register window classes if first instance of application         */
  117.     if ((nRc = RegisterClasses()) == -1)
  118.       {
  119.        /* registering one of the windows failed                         */
  120.        LoadString(hInst, IDS_ERR_REGISTER_CLASS, szString, sizeof(szString));
  121.        MessageBeep( MB_ICONHAND );
  122.        MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
  123.        return nRc;
  124.       }
  125.    }
  126.  
  127.  /* setup the device independent screen units    */
  128.  InitScrScale();
  129.  
  130.  /* load the wait cursor                                                */
  131.  hHourGlass = LoadCursor(NULL, IDC_WAIT);
  132.  
  133.  /* load the accelerators table    */
  134.  hAcc = LoadAccelerators( hInst, (LPSTR) "ACCTABLE" );
  135.  
  136.  if( InitGepasiVar() != 0 )                    /* initialize GEPASI's vars */
  137.  {
  138.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  139.   MessageBeep( MB_ICONHAND );
  140.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  141.   if(!hPrevInstance) UnregisterClass(szAppName, hInst);
  142.   return IDS_ERR_OUT_OF_MEM;
  143.  }
  144.  
  145.  /* get the path of GEPASI's binary files directory    */
  146.  GetModuleFileName( hInst, (LPSTR) GepasiIni, sizeof( GepasiIni ) );
  147.  ptr = strrchr( GepasiIni, '\\' );
  148.  *(ptr+1) = '\0';
  149.  /* append the filename of the INI file    */
  150.  lstrcat( (LPSTR) GepasiIni, (LPSTR) "GEPASI.INI" );
  151.  
  152.  /* create application's Main window                                    */
  153.  hWndMain = CreateWindow(
  154.                 szAppName,               /* Window class name           */
  155.                 "GEPASI - Simulation",   /* Window's title              */
  156.                 WS_CAPTION      |        /* Title and Min/Max           */
  157.                 WS_SYSMENU      |        /* Add system menu box         */
  158.                 WS_MINIMIZEBOX  |        /* Add minimize box            */
  159.                 WS_CLIPCHILDREN |        /* don't draw in child windows areas */
  160.                 WS_OVERLAPPED,
  161.                 CW_USEDEFAULT, 0,        /* Use default X, Y            */
  162.                 ScrX(180), ScrY(90),
  163.                 NULL,                    /* Parent window's handle      */
  164.                 NULL,                    /* Default to Class Menu       */
  165.                 hInst,                   /* Instance of window          */
  166.                 NULL);                   /* Create struct for WM_CREATE */
  167.  
  168.  if(hWndMain == NULL)
  169.  {
  170.   LoadString(hInst, IDS_ERR_CREATE_WINDOW, szString, sizeof(szString));
  171.   MessageBeep( MB_ICONEXCLAMATION );
  172.   MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
  173.   TidyGepasiVar();
  174.   return IDS_ERR_CREATE_WINDOW;
  175.  }
  176.  
  177.  InitTxtMetrics( hWndMain );
  178.  
  179.  /* create the button controls */
  180.  hButton1 = CreateWindow( "Button", "&Top",
  181.                           WS_CHILD | WS_VISIBLE | WS_TABSTOP,
  182.                           ScrX(114), ScrY(40),
  183.                           ScrX(30),  ScrY(24),
  184.                           hWndMain,  IDC_BUTTON1, hInst, NULL );
  185.  if(hButton1 == NULL)
  186.  {
  187.   LoadString(hInst, IDS_ERR_CREATE_WINDOW, szString, sizeof(szString));
  188.   MessageBeep( MB_ICONHAND );
  189.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  190.   CleanUp( hWndMain, hPrevInstance );
  191.   return IDS_ERR_CREATE_WINDOW;
  192.  }
  193.  hButton2 = CreateWindow( "Button", "&Run!",
  194.                           WS_CHILD | WS_VISIBLE | WS_TABSTOP,
  195.                           ScrX(145), ScrY(40),
  196.                           ScrX(30),  ScrY(24),
  197.                           hWndMain,  IDC_BUTTON2, hInst, NULL );
  198.  if(hButton2 == NULL)
  199.  {
  200.   LoadString(hInst, IDS_ERR_CREATE_WINDOW, szString, sizeof(szString));
  201.   MessageBeep( MB_ICONHAND );
  202.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  203.   CleanUp( hWndMain, hPrevInstance );
  204.   return IDS_ERR_CREATE_WINDOW;
  205.  }
  206.  
  207.  /* store the pointer to the timer callback function                */
  208.  fpCheckSimEng = MakeProcInstance(CheckSimEng, hInst);
  209.  
  210.  /* get options from GEPASI.INI file                                */
  211.  /* get the complete filename path of wgnuplot.exe                    */
  212.  GetPrivateProfileString( (LPSTR) "Gnuplot",
  213.                           (LPSTR) "Path",
  214.                           (LPSTR) "\0",
  215.                           (LPSTR) GnuplotExe,
  216.                           sizeof( GnuplotExe ),
  217.                           (LPSTR) GepasiIni
  218.                         );
  219.  /* test if the file really exists */
  220.  nRc = OpenFile( (LPSTR) GnuplotExe, &OfStruct, OF_READ );
  221.  if( nRc==-1 ) *GnuplotExe = '\0';
  222.  else _lclose( nRc );
  223.  
  224.  ShowWindow(hWndMain, nCmdShow);        /* display main window      */
  225.  
  226.  /* if called with a .sim filename argument, store it in FileName    */
  227.  FileName[0] = '\0';
  228.  if( *lpszCmdLine)
  229.  {
  230.   lp = _fstrchr( lpszCmdLine, ' ' );
  231.   if( lp != NULL ) *lp = '\0';
  232.   lstrcat( (LPSTR) FileName, lpszCmdLine );
  233.   SendMessage( hWndMain, WM_USER+1, 0, (DWORD) 0 );
  234.  }
  235.  
  236.  while(GetMessage(&msg, NULL, 0, 0))    /* Until WM_QUIT message    */
  237.  {
  238.   if( !TranslateAccelerator( hWndMain, hAcc, &msg ) )
  239.   {
  240.    TranslateMessage(&msg);
  241.    DispatchMessage(&msg);
  242.   }
  243.  }
  244.  
  245.  /* clean up before exiting from the application                    */
  246.  CleanUp(hWndMain, hPrevInstance);
  247.  
  248.  return msg.wParam;
  249. } /*  End of WinMain                                                */
  250.  
  251. void clear_all( HWND hWnd )
  252. {
  253.  int i;
  254.  
  255.  totmet = nsteps = nscanpar = totsel =
  256.  totscan = nloops = noutpel = nlinks =
  257.  nudf = 0;
  258.  nrateq = MAX_TYP;
  259.  notsaved = 0;
  260.  FileName[0] = FileTitle[0] = '\0';
  261.  for( i=0; i<MAX_MET; i++ ) xu[i] = 0;
  262.  get_dfts();
  263.  SendMessage( hWnd, WM_CREATE, 0, 0 );
  264. }
  265.  
  266.  
  267. LONG FAR PASCAL WndProc(HWND hWnd, WORD Message, WORD wParam, LONG lParam)
  268. {
  269.  HDC        hDC;                /* handle for the display device        */
  270.  MINMAXINFO FAR* lpmmi;            /* pointer to minmaxinfo structure        */
  271.  PAINTSTRUCT    ps;             /* holds PAINT information                */
  272.  FARPROC        lpfnProc;        /* pointer to dialog procedures            */
  273.  int            nRc=0;          /* return code                            */
  274.  char            szASTR[128];
  275.  char            tmpFile[256];
  276.  int            i, j, dim;
  277.  unsigned long    perm;
  278.  char            *ptr;
  279.  
  280.  switch (Message)
  281.  {
  282.   case WM_CREATE:
  283.    hMenu = GetMenu( hWnd );
  284.    EnableMenuItem( hMenu, IDM_F_SAVE, MF_GRAYED );
  285.    EnableMenuItem( hMenu, IDM_F_SAVEAS, MF_GRAYED );
  286.    EnableMenuItem( hMenu, IDM_E_METABOLITES, MF_GRAYED );
  287.    EnableMenuItem( hMenu, IDM_E_KINETICS, MF_GRAYED );
  288.    EnableMenuItem( hMenu, IDM_O_TASKS, MF_GRAYED );
  289.    EnableMenuItem( hMenu, IDM_O_UNITS, MF_GRAYED );
  290.    EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_GRAYED );
  291.    EnableMenuItem( hMenu, IDM_O_REPORT, MF_GRAYED );
  292.    EnableMenuItem( hMenu, IDM_GO, MF_GRAYED );
  293.    EnableMenuItem( hMenu, IDM_PLOT, MF_GRAYED );
  294.    EnableWindow( hButton2, FALSE );
  295.    SendMessage( hWnd, WM_COMMAND, IDM_S_NONE, 0 );
  296.    DrawMenuBar( hWnd );
  297.    break;       /*  End of WM_CREATE                                    */
  298.  
  299.   case WM_CLOSE:  /* close the window                                 */
  300.    if( notsaved )
  301.     AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNO );
  302.    /* tell WinHelp we're closing                                */
  303.    WinHelp( hWnd, (LPSTR) szHelpFile, HELP_QUIT, 0 );
  304.    /* kill the timer (it will fail if one is not active)        */
  305.    KillTimer( hWnd, TimerId );
  306.    DestroyWindow(hWnd);
  307.    if (hWnd == hWndMain)
  308.     PostQuitMessage(0);  /* Quit the application                */
  309.    break;
  310.  
  311.   case WM_QUERYENDSESSION:
  312.    /* windows is about to shut-down...                            */
  313.    if( notsaved )
  314.    {
  315.     nRc = AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNOCANCEL );
  316.     if ( nRc != IDCANCEL ) return 1;
  317.     else return 0;
  318.    }
  319.    return 1;
  320.  
  321.   case WM_COMPACTING:
  322.    /* windows is taking a lot of time compacting memory: FULL!    */
  323.    LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  324.    MessageBeep( MB_ICONEXCLAMATION );
  325.    MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
  326.    return 1;
  327.  
  328.   case WM_PAINT:
  329.    /* Obtain a handle to the device context                       */
  330.    memset( &ps, 0x00, sizeof( PAINTSTRUCT ) );
  331.    /* BeginPaint will sends WM_ERASEBKGND if appropriate          */
  332.    hDC = BeginPaint(hWnd, &ps);
  333.    /* Included in case the background is not a pure color         */
  334.    SetBkMode( hDC, TRANSPARENT );
  335.    i=0;
  336.    if( FileName[0] != '\0' )
  337.     wsprintf( szASTR, "%s", (LPSTR) FileName );
  338.    else
  339.     wsprintf( szASTR, "<untitled>");
  340.    TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  341.    wsprintf( szASTR, "%i metabolites", totmet );
  342.    TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  343.    wsprintf( szASTR, "%i steps", nsteps );
  344.    TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  345.    if( options.scan )
  346.    {
  347.     /* count the number of dimensions and permutations */
  348.     for( j=dim=0, perm=1; j<nscanpar; j++ )
  349.      if( spar[j].idx!=0 )
  350.      {
  351.       dim++;
  352.       perm *= spar[j].dens+1;
  353.      }
  354.     if( options.scan == 1 )
  355.      wsprintf( szASTR, "%dD regular grid", dim );
  356.     else
  357.     {
  358.      wsprintf( szASTR, "%dD random fill", dim );
  359.      perm = options.scandens;
  360.     }
  361.     TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  362.     wsprintf( szASTR, "%lu iterations", perm );
  363.     TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  364.     wsprintf( szASTR, "%i links", nlinks );
  365.     TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  366.    }
  367.    EndPaint( hWnd, &ps );
  368.    break;       /*  End of WM_PAINT                               */
  369.  
  370.   case WM_USER+1:
  371.    if( !(nRc = ReadSim( FileName ) ) )
  372.    {
  373.     /* kill the timer (it will fail if one is not active)        */
  374.     KillTimer( hWnd, TimerId );
  375.     GetFileTitle( (LPSTR) FileName, (LPSTR) FileTitle, sizeof(FileTitle) );
  376.     GetFileTitle( (LPSTR) DatName, (LPSTR) DatTitle, sizeof(DatTitle) );
  377.     EnableMenuItem( hMenu, IDM_F_SAVE , MF_ENABLED );
  378.     EnableMenuItem( hMenu, IDM_F_SAVEAS , MF_ENABLED );
  379.     EnableMenuItem( hMenu, IDM_E_METABOLITES , MF_ENABLED );
  380.     EnableMenuItem( hMenu, IDM_E_KINETICS , MF_ENABLED );
  381.     EnableMenuItem( hMenu, IDM_E_OPTIONS , MF_ENABLED );
  382.     EnableMenuItem( hMenu, IDM_O_TASKS, MF_ENABLED );
  383.     EnableMenuItem( hMenu, IDM_O_UNITS, MF_ENABLED );
  384.     EnableMenuItem( hMenu, IDM_GO, MF_ENABLED );
  385.     EnableMenuItem( hMenu, IDM_PLOT, MF_ENABLED );
  386.     EnableWindow( hButton2, TRUE );
  387.     if( options.dat ) EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_ENABLED );
  388.     if( options.txt ) EnableMenuItem( hMenu, IDM_O_REPORT, MF_ENABLED );
  389.     if( !(options.dyn || options.ss) )
  390.     {
  391.      EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_GRAYED );
  392.      EnableMenuItem( hMenu, IDM_O_REPORT, MF_GRAYED );
  393.      EnableMenuItem( hMenu, IDM_GO, MF_GRAYED );
  394.      EnableWindow( hButton2, FALSE );
  395.     }
  396.     switch( options.scan )
  397.     {
  398.      case 0: SendMessage( hWnd, WM_COMMAND, IDM_S_NONE, 0 ); break;
  399.      case 1: SendMessage( hWnd, WM_COMMAND, IDM_S_GRID, 0 ); break;
  400.      case 2: SendMessage( hWnd, WM_COMMAND, IDM_S_FILL, 0 ); break;
  401.     }
  402.     wsprintf( (LPSTR) szASTR, "GEPASI - %s", (LPSTR) FileTitle );
  403.     SendMessage( hWnd, WM_SETTEXT, 0, (DWORD) (LPSTR) szASTR );
  404.     InvalidateRect( hWnd, NULL, TRUE );
  405.     DrawMenuBar( hWnd );
  406.     notsaved = 0;
  407.    }
  408.    else
  409.    {
  410.     LoadString(hInst, nRc, szString, sizeof(szString));
  411.     wsprintf( (LPSTR) szASTR, "GEPASI - Simulation" );
  412.     SendMessage( hWnd, WM_SETTEXT, 0, (DWORD) (LPSTR) szASTR );
  413.     MessageBeep( MB_ICONEXCLAMATION );
  414.     MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
  415.     InvalidateRect( hWnd, NULL, TRUE );
  416.     clear_all( hWnd );
  417.     /* SEND MESSAGE TO CLEAR_ALL!!!!!*/
  418.    }
  419.    break;
  420.  
  421.   case WM_USER+2:
  422.    SimEnd();
  423.    break;
  424.  
  425.   case WM_COMMAND:
  426.    switch (wParam)
  427.    {
  428.     case IDA_F1HELP:
  429.     case IDM_H_CONTENTS:
  430.         WinHelp( hWnd, (LPSTR) szHelpFile, HELP_INDEX, 0 );
  431.         break;
  432.  
  433.     case IDM_H_SEARCH:
  434.         WinHelp( hWnd, (LPSTR) szHelpFile, HELP_PARTIALKEY, (DWORD) (LPSTR) "" );
  435.         break;
  436.  
  437.     case IDM_H_HELPONHELP:
  438.         WinHelp( hWnd, (LPSTR) szHelpFile, HELP_HELPONHELP, 0 );
  439.         break;
  440.  
  441.     case IDM_H_ABOUT:
  442.      lpfnProc = MakeProcInstance((FARPROC)AboutBox, hInst);
  443.      DialogBox(hInst, (LPSTR)"ABOUTBOX", hWnd, lpfnProc);
  444.      FreeProcInstance(lpfnProc);
  445.      break;
  446.  
  447.     case IDC_BUTTON1:
  448.      GetModuleFileName( hInst, (LPSTR) szASTR, sizeof( szASTR ) );
  449.      ptr = strrchr( szASTR, '\\' );
  450.      *(ptr+1) = '\0';
  451.      strcat( szASTR, "GWTOP.EXE" );
  452.         nRc = WinExec( (LPSTR) szASTR, SW_SHOWNORMAL );
  453.         ErrorHandler( hInst, nRc );
  454.      break;
  455.  
  456.     case IDM_F_OPEN:
  457.      if( notsaved )
  458.       AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNO );
  459.      if( GetOFileName( hWnd,
  460.                          (LPSTR) FileName, 256,
  461.                         (LPSTR) "Load Simulation File",
  462.                          (LPSTR) "simulation (*.SIM)\0*.SIM\0" ) )
  463.       SendMessage( hWnd, WM_USER+1, 0, (DWORD) 0 );
  464.      break;
  465.  
  466.     case IDM_F_CLOSE:
  467.      SendMessage( hWnd, WM_CLOSE, 0, 0 );
  468.      break;
  469.  
  470.     case IDM_F_SAVE:
  471.      WriteSim( FileName );
  472.      notsaved = 0;
  473.      break;
  474.  
  475.     case IDM_F_SAVEAS:
  476.      _fstrcpy( (LPSTR) tmpFile, (LPSTR) FileName );
  477.      if( GetSFileName( hWnd,
  478.                          (LPSTR) tmpFile, sizeof( tmpFile ),
  479.                         (LPSTR) "Save Simulation File",
  480.                          (LPSTR) "simulation (*.SIM)\0*.SIM\0" ) )
  481.      {
  482.       _fstrcpy( (LPSTR) FileName, (LPSTR) tmpFile );
  483.       if( !(nRc = WriteSim( FileName ) ) )
  484.       {
  485.        GetFileTitle( (LPSTR) FileName, (LPSTR) FileTitle, sizeof(FileTitle) );
  486.        EnableMenuItem( hMenu, IDM_GO, MF_ENABLED );
  487.        EnableWindow( hButton2, TRUE );
  488.        EnableMenuItem( hMenu, IDM_F_SAVE , MF_ENABLED );
  489.        DrawMenuBar( hWnd );
  490.        wsprintf( (LPSTR) szASTR, "GEPASI - %s", (LPSTR) FileTitle );
  491.        SendMessage( hWnd, WM_SETTEXT, 0, (DWORD) (LPSTR) szASTR );
  492.        notsaved = 0;
  493.        InvalidateRect( hWnd, NULL, TRUE );
  494.       }
  495.      }
  496.      break;
  497.  
  498.     case IDM_F_NEW:
  499.      if( notsaved )
  500.       nRc = AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNOCANCEL );
  501.      if( nRc == IDCANCEL ) return TRUE;
  502.      if( GetOFileName( hWnd,
  503.                          (LPSTR) TopFile, sizeof( TopFile ),
  504.                         (LPSTR) "Load Topology File",
  505.                          (LPSTR) "topology (*.TOP)\0*.TOP\0" ) )
  506.      {
  507.       clear_all( hWnd );
  508.       FileName[0] = '\0';
  509.       FileTitle[0] = '\0';
  510.       DatName[0] = '\0';
  511.       DatTitle[0] = '\0';
  512.       if( nRc = TopIn( TopFile ) )
  513.       {
  514.        /* kill the timer (it will fail if one is not active)        */
  515.        KillTimer( hWnd, TimerId );
  516.        clear_all( hWnd );
  517.        LoadString(hInst, nRc, szString, sizeof(szString));
  518.        MessageBeep( MB_ICONEXCLAMATION );
  519.        MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
  520.        InvalidateRect( hWnd, NULL, TRUE );
  521.        break;
  522.       }
  523.       else
  524.       {
  525.        notsaved = 0;
  526.        /* get the filename without the path                        */
  527.        GetFileTitle( (LPSTR) TopFile, (LPSTR) TopTitle, sizeof(FileTitle) );
  528.        /* setup the filename for the dat file            */
  529.        _getcwd( DatName, sizeof(DatName) );
  530.        lstrcat( (LPSTR) DatName, (LPSTR) "\\RESULTS.DAT" );
  531.        GetFileTitle( (LPSTR) DatName, (LPSTR) DatTitle, sizeof(DatTitle) );
  532.        EnableMenuItem( hMenu, IDM_F_SAVE , MF_GRAYED );
  533.        EnableMenuItem( hMenu, IDM_F_SAVEAS , MF_ENABLED );
  534.        EnableMenuItem( hMenu, IDM_E_METABOLITES , MF_ENABLED );
  535.        EnableMenuItem( hMenu, IDM_E_KINETICS , MF_ENABLED );
  536.        EnableMenuItem( hMenu, IDM_O_TASKS, MF_ENABLED );
  537.        EnableMenuItem( hMenu, IDM_O_UNITS, MF_ENABLED );
  538.        EnableMenuItem( hMenu, IDM_GO, MF_ENABLED );
  539.        EnableMenuItem( hMenu, IDM_PLOT, MF_ENABLED );
  540.        EnableWindow( hButton2, TRUE );
  541.        wsprintf( (LPSTR) szASTR, "GEPASI - Simulation" );
  542.        SendMessage( hWnd, WM_SETTEXT, 0, (DWORD) (LPSTR) szASTR );
  543.        if( options.dat ) EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_ENABLED );
  544.        if( options.txt ) EnableMenuItem( hMenu, IDM_O_REPORT, MF_ENABLED );
  545.        switch( options.scan )
  546.        {
  547.         case 0: SendMessage( hWnd, WM_COMMAND, IDM_S_NONE, 0 ); break;
  548.         case 1: SendMessage( hWnd, WM_COMMAND, IDM_S_GRID, 0 ); break;
  549.         case 2: SendMessage( hWnd, WM_COMMAND, IDM_S_FILL, 0 ); break;
  550.        }
  551.        DrawMenuBar( hWnd );
  552.        InvalidateRect( hWnd, NULL, TRUE );
  553.        break;
  554.       }
  555.      }
  556.      break;
  557.  
  558.     case IDM_E_METABOLITES:
  559.      lpfnProc = MakeProcInstance((FARPROC)EdConc, hInst);
  560.      DialogBox(hInst, (LPSTR)"ED_CONC", hWnd, lpfnProc);
  561.      FreeProcInstance(lpfnProc);
  562.      InvalidateRect( hWnd, NULL, TRUE );
  563.      break;
  564.  
  565.     case IDM_E_KINETICS:
  566.      lpfnProc = MakeProcInstance((FARPROC) EdKConst, hInst);
  567.      DialogBox(hInst, (LPSTR)"ED_CONST", hWnd, lpfnProc);
  568.      FreeProcInstance(lpfnProc);
  569.      InvalidateRect( hWnd, NULL, TRUE );
  570.      break;
  571.  
  572.     case IDM_E_CLEAR:
  573.      conc_dft();
  574.      InvalidateRect( hWnd, NULL, TRUE );
  575.      break;
  576.  
  577.     case IDM_O_TASKS:
  578.      lpfnProc = MakeProcInstance((FARPROC) EdOTask, hInst);
  579.      DialogBox(hInst, (LPSTR)"TASK_OPT", hWnd, lpfnProc);
  580.      FreeProcInstance(lpfnProc);
  581.      if( options.dat ) EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_ENABLED );
  582.      else EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_GRAYED );
  583.      if( options.txt ) EnableMenuItem( hMenu, IDM_O_REPORT, MF_ENABLED );
  584.      else EnableMenuItem( hMenu, IDM_O_REPORT, MF_GRAYED );
  585.      if( !(options.dyn || options.ss) )
  586.      {
  587.       EnableMenuItem( hMenu, IDM_O_OUTPUT, MF_GRAYED );
  588.       EnableMenuItem( hMenu, IDM_O_REPORT, MF_GRAYED );
  589.       EnableMenuItem( hMenu, IDM_GO, MF_GRAYED );
  590.       EnableWindow( hButton2, FALSE );
  591.      }
  592.      DrawMenuBar( hWnd );
  593.      InvalidateRect( hWnd, NULL, TRUE );
  594.      break;
  595.  
  596.     case IDM_O_UNITS:
  597.      lpfnProc = MakeProcInstance((FARPROC) EdOUnits, hInst);
  598.      DialogBox(hInst, (LPSTR)"ED_UNITS", hWnd, lpfnProc);
  599.      FreeProcInstance(lpfnProc);
  600.      InvalidateRect( hWnd, NULL, TRUE );
  601.      break;
  602.  
  603.     case IDM_O_REPORT:
  604.      lpfnProc = MakeProcInstance((FARPROC) EdReport, hInst);
  605.      DialogBox(hInst, (LPSTR)"REP_OPT", hWnd, lpfnProc);
  606.      FreeProcInstance(lpfnProc);
  607.      InvalidateRect( hWnd, NULL, TRUE );
  608.      break;
  609.  
  610.     case IDM_O_OUTPUT:
  611.      lpfnProc = MakeProcInstance((FARPROC) EdDat, hInst);
  612.      nRc = DialogBox(hInst, (LPSTR)"DAT_OPT", hWnd, lpfnProc);
  613.      FreeProcInstance(lpfnProc);
  614.      if( nRc == IDOK )
  615.       /* kill the timer (it will fail if one is not active)        */
  616.       KillTimer( hWnd, TimerId );
  617.      InvalidateRect( hWnd, NULL, TRUE );
  618.      break;
  619.  
  620.     case IDM_O_METHODS:
  621.      lpfnProc = MakeProcInstance((FARPROC) EdNumer, hInst);
  622.      DialogBox(hInst, (LPSTR)"NUM_OPT", hWnd, lpfnProc);
  623.      FreeProcInstance(lpfnProc);
  624.      InvalidateRect( hWnd, NULL, TRUE );
  625.      break;
  626.  
  627.     case IDM_S_LINKS:
  628.      lpfnProc = MakeProcInstance((FARPROC) Links, hInst);
  629.      DialogBox(hInst, (LPSTR)"LINKS", hWnd, lpfnProc);
  630.      FreeProcInstance(lpfnProc);
  631.      InvalidateRect( hWnd, NULL, TRUE );
  632.      break;
  633.  
  634.     case IDM_S_PARAM:
  635.      lpfnProc = MakeProcInstance((FARPROC) EdScanP, hInst);
  636.      DialogBox(hInst, (LPSTR)"ED_SCANP", hWnd, lpfnProc);
  637.      FreeProcInstance(lpfnProc);
  638.      InvalidateRect( hWnd, NULL, TRUE );
  639.      break;
  640.  
  641.     case IDM_S_BOUND:
  642.      lpfnProc = MakeProcInstance((FARPROC) EdScanB, hInst);
  643.      DialogBox(hInst, (LPSTR)"ED_SCANB", hWnd, lpfnProc);
  644.      FreeProcInstance(lpfnProc);
  645.      InvalidateRect( hWnd, NULL, TRUE );
  646.      break;
  647.  
  648.     case IDM_S_NONE:
  649.      CheckMenuItem( hMenu, IDM_S_NONE, MF_CHECKED );
  650.      CheckMenuItem( hMenu, IDM_S_GRID, MF_UNCHECKED );
  651.      CheckMenuItem( hMenu, IDM_S_FILL, MF_UNCHECKED );
  652.      EnableMenuItem( hMenu, IDM_S_PARAM, MF_GRAYED );
  653.      EnableMenuItem( hMenu, IDM_S_BOUND, MF_GRAYED );
  654.      EnableMenuItem( hMenu, IDM_S_LINKS, MF_GRAYED );
  655.      DrawMenuBar( hWnd );
  656.      options.scan = 0;
  657.      notsaved = 0;
  658.      InvalidateRect( hWnd, NULL, TRUE );
  659.      break;
  660.  
  661.     case IDM_S_GRID:
  662.      CheckMenuItem( hMenu, IDM_S_NONE, MF_UNCHECKED );
  663.      CheckMenuItem( hMenu, IDM_S_GRID, MF_CHECKED );
  664.      CheckMenuItem( hMenu, IDM_S_FILL, MF_UNCHECKED );
  665.      EnableMenuItem( hMenu, IDM_S_PARAM, MF_ENABLED );
  666.      EnableMenuItem( hMenu, IDM_S_BOUND, MF_ENABLED );
  667.      EnableMenuItem( hMenu, IDM_S_LINKS, MF_ENABLED );
  668.      DrawMenuBar( hWnd );
  669.      options.scan = 1;
  670.      notsaved = 0;
  671.      InvalidateRect( hWnd, NULL, TRUE );
  672.      break;
  673.  
  674.     case IDM_S_FILL:
  675.      CheckMenuItem( hMenu, IDM_S_NONE, MF_UNCHECKED );
  676.      CheckMenuItem( hMenu, IDM_S_GRID, MF_UNCHECKED );
  677.      CheckMenuItem( hMenu, IDM_S_FILL, MF_CHECKED );
  678.      EnableMenuItem( hMenu, IDM_S_PARAM, MF_ENABLED );
  679.      EnableMenuItem( hMenu, IDM_S_BOUND, MF_ENABLED );
  680.      EnableMenuItem( hMenu, IDM_S_LINKS, MF_ENABLED );
  681.      DrawMenuBar( hWnd );
  682.      options.scan = 2;
  683.      notsaved = 0;
  684.      InvalidateRect( hWnd, NULL, TRUE );
  685.      break;
  686.  
  687.     case IDM_PLOT:
  688.      if(*GnuplotExe)
  689.      {
  690.       lpfnProc = MakeProcInstance((FARPROC) Plot, hInst);
  691.       nRc = DialogBox(hInst, (LPSTR)"ED_PLOT", hWnd, lpfnProc);
  692.       FreeProcInstance(lpfnProc);
  693.       if( nRc==IDOK ) GnuPlot( hWnd );
  694. /*      else
  695.        if( nRc!=IDCANCEL )
  696.        {
  697.         LoadString(hInst, IDS_ERR_NO_GNUPLOT, szString, sizeof(szString));
  698.         MessageBeep( MB_ICONEXCLAMATION );
  699.         MessageBox( hWnd, szString, (LPSTR) "Error", MB_ICONEXCLAMATION );
  700.        }*/
  701.      }
  702.      else
  703.      {
  704.       LoadString(hInst, IDS_INF_GNUPLOT, szString, sizeof(szString));
  705.       MessageBeep( MB_ICONQUESTION );
  706.       nRc = MessageBox( hWnd, szString, (LPSTR) "Link GNUPLOT with GEPASI", MB_ICONINFORMATION | MB_OKCANCEL );
  707.       if( nRc==IDOK )
  708.       if( GetOFileName( hWnd,
  709.                            (LPSTR) GnuplotExe, sizeof( GnuplotExe ),
  710.                           (LPSTR) "Gnuplot Path",
  711.                            (LPSTR) "executable (*.exe)\0*.exe\0"
  712.                          )
  713.         )
  714.        WritePrivateProfileString( (LPSTR) "Gnuplot",
  715.                                   (LPSTR) "Path",
  716.                                   (LPSTR) GnuplotExe,
  717.                                   (LPSTR) GepasiIni
  718.                                 );
  719.      }
  720.      break;
  721.  
  722.     case IDC_BUTTON2:
  723.     case IDM_GO:
  724.      if( notsaved )
  725.       SendMessage( hWnd, WM_COMMAND, FileName[0] ? IDM_F_SAVE : IDM_F_SAVEAS, 0 );
  726.      Execute( hWnd, hInst );
  727.      break;
  728.  
  729.     default:
  730.      return DefWindowProc(hWnd, Message, wParam, lParam);
  731.    }
  732.    break;        /* End of WM_COMMAND                             */
  733.  
  734.   default:
  735.    return DefWindowProc(hWnd, Message, wParam, lParam);
  736.  }
  737.  return 0L;
  738. }     /* End of WndProc                                         */
  739.  
  740.  
  741. /*
  742.   ABOUTMsgProc - About dialog box procedure
  743. */
  744.  
  745. BOOL FAR PASCAL AboutBox(HWND hWndDlg, WORD Message, WORD wParam, LONG lParam)
  746. {
  747.  switch(Message)
  748.  {
  749.   case WM_INITDIALOG:
  750.    SendDlgItemMessage( hWndDlg, IDSTAT_2, WM_SETTEXT, 0, (DWORD) (LPSTR) VERSION );
  751.    break;
  752.  
  753.   case WM_CLOSE:
  754.    /* Closing the Dialog behaves the same as Cancel               */
  755.    PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  756.    break;
  757.  
  758.   case WM_COMMAND:
  759.    if( wParam == IDOK || wParam == IDCANCEL )
  760.    EndDialog(hWndDlg, FALSE);
  761.    break;
  762.  
  763.   default:
  764.    return FALSE;
  765.  }
  766.  return TRUE;
  767. }
  768.  
  769.  
  770. /************************************************************************/
  771. /*                                                                      */
  772. /* RegisterClasses Function                                          */
  773. /*                                                                      */
  774. /* The following function registers all the classes of all the windows  */
  775. /* associated with this application. The function returns an error code */
  776. /* if unsuccessful, otherwise it returns 0.                             */
  777. /*                                                                      */
  778. /************************************************************************/
  779.  
  780. int RegisterClasses(void)
  781. {
  782.  WNDCLASS   wndclass;    /* struct to define a window class             */
  783.  
  784.  memset(&wndclass, 0x00, sizeof(WNDCLASS));
  785.  /* load WNDCLASS with window's characteristics                         */
  786.  wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW;
  787.  wndclass.lpfnWndProc = WndProc;
  788.  /* Extra storage for Class and Window objects                          */
  789.  wndclass.cbClsExtra = 0;
  790.  wndclass.cbWndExtra = 0;
  791.  wndclass.hInstance = hInst;
  792.  wndclass.hIcon = LoadIcon(hInst,"GWSIM");
  793.  wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  794.  /* Create brush for erasing background                                 */
  795.  wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
  796.  wndclass.lpszMenuName = szAppName;   /* Menu Name is App Name            */
  797.  wndclass.lpszClassName = szAppName;  /* Class Name is App Name            */
  798.  if(!RegisterClass(&wndclass))
  799.    return -1;
  800.  return(0);
  801. } /* End of RegisterClasses                                              */
  802.  
  803. void DeleteTmp( void )
  804. {
  805.  OFSTRUCT OfStruct;
  806.  LPSTR p1, p2;
  807.  
  808.  p1 = TmpFiles;
  809.  for(;;)
  810.  {
  811.   p2 = _fstrchr( p1, '+' );
  812.   if( p2==NULL ) return;
  813.   *p2 = '\0';
  814.   OpenFile( p1, &OfStruct, OF_DELETE );
  815.   p1 = p2+1;
  816.  }
  817. }
  818.  
  819. void CleanUp( HWND hWnd, HANDLE hPrevInstance )
  820. {
  821.  if(!hPrevInstance) UnregisterClass(szAppName, hInst);
  822.  DeleteTmp();
  823.  TidyGepasiVar();
  824. }