home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / EDREACT.C < prev    next >
C/C++ Source or Header  |  1993-03-14  |  12KB  |  374 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*          GWTOP - Topology         */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*        Reactions dialog box       */
  14. /*                                   */
  15. /*           QuickC/WIN 1.0          */
  16. /*                                   */
  17. /*   (include here compilers that    */
  18. /*   compiled GWSIM successfully)    */
  19. /*                                   */
  20. /*************************************/
  21.  
  22.  
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "defines.h"                    /* symbols also used in .DLG files        */
  27. #include "globals.h"                    /* gepasi's own symbols                    */
  28. #include "gwtop.h"                        /* macros, function prototypes, etc.    */
  29. #include "gep1.h"                        /* gepasi's variables                    */
  30. #include "topgvar.h"                    /* global variables                        */
  31. #include "strtbl.h"                        /* symbols for the string table            */
  32.  
  33. #pragma alloc_text( CODE2, ClearReactions, AddReactLst, EdReact )
  34.  
  35. void ClearReactions( void )
  36. {
  37.  int i, j;
  38.  
  39.  for( i=0; i<totmet; i++ )
  40.  {
  41.   metname[i][0] = '\0';
  42.   intmet[i] = 1;
  43.   for( j=0; j<nsteps; j++ )
  44.    stoiu[i*MAX_MET + j] = 0;
  45.  }
  46.  for( i=0; i<nsteps; i++ )
  47.   for( j=0; j<MAX_MOL; j++ )
  48.    (*rs)[i][j] = 0;
  49.  nsteps  = 0;
  50.  totmet  = 0;
  51. }
  52.  
  53.  
  54. void AddReactLst( HWND hControl, int j, int is_index, int scroll,
  55.                   int *totm, char (huge *metn)[NAME_L],
  56.                   int huge *sto, int *im, unsigned char *rev,
  57.                   int (huge *rs)[MAX_STEP][MAX_MOL] )
  58. {
  59.  int i, k, st, first, buffWidth, tempstoi[MAX_MET];
  60.  char auxstr[256], buff[256];
  61.  HANDLE hDC;
  62.  
  63.  *buff = '\0';
  64.  /* setup the stoicheiometries of the substrates                    */
  65.  for( i=0; i<*totm; i++ ) tempstoi[i] = 0;
  66.  for( i=0; i<MAX_MOL; i++ )
  67.   if ((*rs)[j][i]<0) tempstoi[ -(*rs)[j][i]-1 ]--;
  68.  for( k=0, first=1; k<MAX_MOL; k++ )
  69.  {
  70.   if ((*rs)[j][k]<0)
  71.   {
  72.    i = -(*rs)[j][k]-1;
  73.    st = tempstoi[i];
  74.    if( st < 0 )
  75.    {
  76.     if ( st == -1 )
  77.      wsprintf( auxstr, "%s%s", first==1 ? (LPSTR) "" : (LPSTR) " + ", &metn[i][0] );
  78.     else
  79.      wsprintf( auxstr, "%s%i*%s", first==1 ? (LPSTR) "" : (LPSTR) " + ", -st, &metn[i][0] );
  80.     _fstrcat( buff, auxstr );
  81.     first = 0;
  82.    }
  83.    for( ; (i == -(*rs)[j][k]-1) && (k<=MAX_MOL) ; k++);
  84.    k--;
  85.   }
  86.  }
  87.  if( rev[j] )                                            /* the connector        */
  88.   _fstrcat( buff, " = " );
  89.  else
  90.   _fstrcat( buff, " -> " );
  91.  
  92.  /* setup the stoicheiometries of the products                        */
  93.  for( i=0; i<*totm; i++ ) tempstoi[i] = 0;
  94.  for( i=0; i<MAX_MOL; i++ )
  95.   if ((*rs)[j][i]>0) tempstoi[ (*rs)[j][i]-1 ]++;
  96.  for( k=0, first=1; k<MAX_MOL; k++ )
  97.  {
  98.   if ((*rs)[j][k]>0)
  99.   {
  100.    i = (*rs)[j][k]-1;
  101.    st = tempstoi[i];
  102.    if( st > 0 )
  103.    {
  104.     if( st == 1 )
  105.      wsprintf( auxstr, "%s%s", first==1 ? (LPSTR) "" : (LPSTR) " + ", &metn[i][0] );
  106.     else
  107.      wsprintf( auxstr, "%s%i*%s", first==1 ? (LPSTR) "" : (LPSTR) " + ", st, &metn[i][0] );
  108.     _fstrcat( buff, auxstr );
  109.     first = 0;
  110.    }
  111.    for( ; (i == (*rs)[j][k]-1) && (k<=MAX_MOL) ; k++);
  112.    k--;
  113.   }
  114.  }
  115.  
  116.  hDC = GetDC( hControl );
  117.  buffWidth = 5 + LOWORD( GetTextExtent( hDC, buff, _fstrlen(buff) ) );
  118.  SetTextJustification( hDC, 0, 0 );
  119.  ReleaseDC( hControl, hDC );
  120.  if( buffWidth > lbWidth )
  121.  {
  122.   lbWidth = buffWidth;
  123.   SendMessage( hControl, LB_SETHORIZONTALEXTENT, lbWidth, (DWORD) 0 );
  124.  }
  125.  if( is_index )
  126.  {
  127.   SendMessage( hControl, LB_DELETESTRING, j, 0 );
  128.   SendMessage( hControl, LB_INSERTSTRING, j, (DWORD) (LPSTR) buff );
  129.  }
  130.  else
  131.   SendMessage( hControl, LB_INSERTSTRING, -1, (DWORD) (LPSTR) buff );
  132.  if( scroll ) SendMessage( hControl, WM_VSCROLL, SB_BOTTOM, 0 );
  133. }
  134.  
  135. /*
  136.   EdReact - dialog box procedure for TOP_PARSER dialog box
  137. */
  138.  
  139. BOOL FAR PASCAL EdReact( HWND hDlg, WORD message, WORD wParam, LONG lParam )
  140. {
  141.  static HWND   hEditBox, hReacLst, hStepNam;
  142.  char    buffer[256], buff2[256], step_name[NAME_L];
  143.  int     i, j, nr, rv;
  144.  static int idx;
  145.  
  146.  
  147.  switch( message )
  148.  {
  149.   case WM_INITDIALOG:                                   /*message: initialize dialog box        */
  150.  
  151.    lbWidth = 0;                                            /* set the width of the list box to 0    */
  152.    idx = -1;                                            /* set an invalid alteration index        */
  153.  
  154.    /* get handles to the controls                            */
  155.    hEditBox = GetDlgItem( hDlg, IDC_REACT );
  156.    hReacLst = GetDlgItem( hDlg, IDC_REACTLST );
  157.    hStepNam = GetDlgItem( hDlg, IDC_REACTNAM );
  158.  
  159.    /* setup the mirrors                                        */
  160.    _fmemcpy( (void __far *) metn,
  161.                 (void __far *) metname,
  162.                 (size_t) MAX_MET * NAME_L * sizeof( char ) );
  163.    _fmemcpy( (void __far *) stepn,
  164.                 (void __far *) stepname,
  165.                 (size_t) MAX_STEP * NAME_L * sizeof( char ) );
  166.    _fmemcpy( (void __far *) sto,
  167.                 (void __far *) stoiu,
  168.              (size_t) MAX_MET * MAX_STEP * sizeof( int ) );
  169.    _fmemcpy( (void __far *) lp,
  170.                 (void __far *) loop,
  171.              (size_t) MAX_STEP * MAX_MET * sizeof( unsigned char ) );
  172.    _fmemcpy( (void __far *) intm,
  173.                 (void __far *) intmet,
  174.                 (size_t) MAX_MET * sizeof( int ) );
  175.    _fmemcpy( (void __far *) kinet,
  176.                 (void __far *) kinetu,
  177.              (size_t) MAX_STEP * sizeof( int ) );
  178.    _fmemcpy( (void __far *) nmd,
  179.               (void __far *) nmod,
  180.              (size_t) MAX_STEP * sizeof( unsigned char ) );
  181.    _fmemcpy( (void __far *) rvr,
  182.               (void __far *) revers,
  183.              (size_t) MAX_STEP * sizeof( unsigned char ) );
  184.    _fmemcpy( (void __far *) rs,
  185.               (void __far *) rstr,
  186.              (size_t) MAX_STEP * MAX_MOL * sizeof( int ) );
  187.    nst = nsteps;
  188.    totm = totmet;
  189.    nlps = nloops;
  190.    kass = kinass;
  191.    lass = loopass;
  192.  
  193.    /* add all steps to the list box                                        */
  194.    for( j=0; j<nst; j++ ) AddReactLst( hReacLst, j, 1, (j==nst), &totm, metn, sto, intm, rvr, rs );
  195.  
  196.    /* write the name of the new reaction in the name edit box            */
  197.    wsprintf( (LPSTR) step_name, "R%d", nst+1 );
  198.    SendMessage( hStepNam, WM_SETTEXT, 0, (DWORD) (LPSTR) step_name );
  199.  
  200.    return( TRUE );
  201.  
  202.   case WM_COMMAND:
  203.    switch( wParam )
  204.    {
  205.     case IDC_ADD:
  206.      SendMessage( hEditBox, WM_GETTEXT, 256, (DWORD) (LPSTR) buffer );
  207.      if( idx == -1 )                                    /* new item        */
  208.      {
  209.       /* verify if we reached the maximum number of steps                */
  210.       if( nst == MAX_STEP )
  211.       {
  212.        LoadString(hInst, IDS_ERR_STEP_EXCEED, szString, sizeof(szString));
  213.        MessageBeep( MB_OK );
  214.        MessageBox(NULL, szString, NULL, MB_ICONINFORMATION);
  215.        return( TRUE );
  216.       }
  217.       /* if not assign the next step number to this one                    */
  218.       j = nst;
  219.      }
  220.      else j = idx;                                        /* alterion        */
  221.      rv = reaction( buffer, j, &totm, metn, sto, rs, intm, rvr );
  222.      if( rv == -2 )
  223.      {
  224.       LoadString(hInst, IDS_ERR_MET_EXCEED, szString, sizeof(szString));
  225.       MessageBeep( MB_OK );
  226.       MessageBox(NULL, szString, NULL, MB_ICONINFORMATION);
  227.       return( TRUE );
  228.      }
  229.      if( rv == 0 )
  230.      {
  231.       SendMessage( hStepNam, WM_GETTEXT, NAME_L, (DWORD) (LPSTR) stepn[j] );
  232.       AddReactLst( hReacLst, j, 1, (j==nst), &totm, metn, sto, intm, rvr, rs );
  233.       if ( idx == -1 ) nst++;
  234.       else
  235.       {
  236.        idx = -1;                                /* reset idx            */
  237.        if( ktype[kinet[j]].nmodf>0 )            /* if there were modfs    */
  238.        {
  239.         nlps--;
  240.         for(i=0;i<MAX_MET;i++) (*lp)[j][i] = 0;    /* reset row of loop    */
  241.         if( ktype[kinet[j]].nmodf == nmd[j] ) lass--;    /* decrease lass                */
  242.        }
  243.        if( kinet[j] != NOT ) kass --;
  244.        kinet[j] = 0;                            /* reset kinetic type    */
  245.       }
  246.       /* clear the reaction edit box                                    */
  247.       SetWindowText( hEditBox, (LPSTR) "" );
  248.       /* write the name of the new reaction in the name edit box        */
  249.       wsprintf( (LPSTR) step_name, "R%d", nst+1 );
  250.       SendMessage( hStepNam, WM_SETTEXT, 0, (DWORD) (LPSTR) step_name );
  251.      }
  252.      else
  253.      {
  254.       LoadString(hInst, IDS_ERR_SYNTAX, szString, sizeof(szString));
  255.       MessageBox(NULL, szString, NULL, MB_ICONINFORMATION);
  256.      }
  257.      return( TRUE );
  258.  
  259.     case IDC_DEL:
  260.      if( ( idx = (int) SendMessage( hReacLst, LB_GETCURSEL, 0, 0 ) ) == LB_ERR )
  261.      {
  262.       LoadString(hInst, IDS_ERR_DEL, szString, sizeof(szString));
  263.       MessageBox(NULL, szString, NULL, MB_ICONINFORMATION);
  264.      }
  265.      if( SendMessage( hReacLst, LB_DELETESTRING, idx, 0 ) == LB_ERR )
  266.      {
  267.       LoadString(hInst, IDS_ERR_DEL, szString, sizeof(szString));
  268.       MessageBox(NULL, szString, NULL, MB_ICONINFORMATION);
  269.      }
  270.      else
  271.      {
  272.       nst--;
  273.       if( kinet[idx] != NOT )
  274.       {
  275.        kass--;
  276.        if( ktype[kinet[idx]].nmodf>0 ) nlps--;        /* if there were modfs    */
  277.        if( ktype[kinet[idx]].nmodf==nmd[idx] ) lass--;    /* decrease lass                */
  278.       }
  279.       for( j=idx; j<nst; j++ )
  280.       {
  281.        lstrcpy( (LPSTR) stepn[j], (LPSTR) stepn[j+1] );
  282.        kinet[j] = kinet[j+1];
  283.        nmd[j] = nmd[j+1];
  284.        for( i=0; i<MAX_MET; i++ )
  285.        {
  286.         (*lp)[j][i] = (*lp)[j+1][i];
  287.         sto[i*MAX_MET + j] = sto[i*MAX_MET + j+1];
  288.        }
  289.       }
  290.       kinet[nst] = 0;
  291.       nmd[nst] = 0;
  292.       idx = -1;
  293.      }
  294.       SetFocus( hEditBox );
  295.      return( TRUE );
  296.  
  297.     case IDC_REACT:
  298.      switch( HIWORD( lParam ) )
  299.      {
  300.       case EN_ERRSPACE:
  301.       case EN_MAXTEXT:
  302.        LoadString(hInst, IDS_ERR_BUF_FUL, szString, sizeof(szString));
  303.        MessageBox(NULL, szString, NULL, MB_ICONINFORMATION);
  304.        GlobalFree( hMetn );
  305.        GlobalFree( hSto );
  306.        EndDialog( hDlg, FALSE );
  307.        return( TRUE );
  308.      }
  309.      break;
  310.  
  311.     case IDC_REACTLST:
  312.      if( HIWORD( lParam ) == LBN_DBLCLK )
  313.      {
  314.       idx = (int) SendMessage( hReacLst, LB_GETCURSEL, 0, 0 );
  315.       SendMessage( hReacLst, LB_GETTEXT, idx, (DWORD) (LPSTR) buffer );
  316.       SendMessage( hEditBox, WM_SETTEXT, 0, (DWORD) (LPSTR) buffer );
  317.       SendMessage( hStepNam, WM_SETTEXT, 0, (DWORD) (LPSTR) stepn[idx] );
  318.       SetFocus( hEditBox );
  319.       SendMessage( hEditBox, WM_KEYDOWN, 0x23, 0 );        /* put cursor at the end    */
  320.       return( TRUE );
  321.      }
  322.      else return( FALSE );
  323.  
  324.     case IDOK:
  325.      ClearReactions();
  326.      nr = (int) SendMessage( hReacLst, LB_GETCOUNT, 0, 0 );
  327.      for( i=0; i<nr; i++ )
  328.      {
  329.       SendMessage( hReacLst, LB_GETTEXT, i, (DWORD) (LPSTR) buffer );
  330.       reaction( buffer, i, &totmet, metname, stoiu, rstr, intmet, revers );
  331.       nsteps++;
  332.      }
  333. /*     _fmemcpy( (void __far *) order,
  334.                   (void __far *) ord,
  335.                (size_t) MAX_STEP * MAX_MET * sizeof( signed char ) );*/
  336.      _fmemcpy( (void __far *) intmet,
  337.                   (void __far *) intm,
  338.                   (size_t) MAX_MET * sizeof( int ) );
  339.      _fmemcpy( (void __far *) kinetu,
  340.                   (void __far *) kinet,
  341.                (size_t) MAX_STEP * sizeof( int ) );
  342.      _fmemcpy( (void __far *) loop,
  343.                (void __far *) lp,
  344.                (size_t) MAX_STEP * MAX_MET * sizeof( unsigned char ) );
  345.      _fmemcpy( (void __far *) nmod,
  346.                 (void __far *) nmd,
  347.                (size_t) MAX_STEP * sizeof( unsigned char ) );
  348.      _fmemcpy( (void __far *) revers,
  349.                 (void __far *) rvr,
  350.                (size_t) MAX_STEP * sizeof( unsigned char ) );
  351.      _fmemcpy( (void __far *) stepname,
  352.                   (void __far *) stepn,
  353.                   (size_t) MAX_STEP * NAME_L * sizeof( char ) );
  354.      nloops = nlps;
  355.      kinass = kass;
  356.      loopass = lass;
  357.      notsaved = 1;
  358.      EndDialog( hDlg, IDOK );
  359.      return( TRUE );
  360.  
  361.     case IDC_HELP:                        /* Help on this Dialog Box              */
  362.         WinHelp( hDlg, (LPSTR) szHelpFile, HELP_KEY, (DWORD) (LPSTR) "Reaction editor" );
  363.      return( TRUE );
  364.  
  365.     case IDCANCEL:                        /*Cancel box or sys menu close command  */
  366.      EndDialog( hDlg, IDCANCEL );
  367.      return( TRUE );
  368.    }
  369.    break;
  370.  }
  371.  return( FALSE );                                       /*didn't process a message              */
  372. } /* End of EdReact                                           */
  373.  
  374.