home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Fortran.51 / DISK6 / MULMIX.C$ / MULMIX.bin
Text File  |  1991-04-05  |  10KB  |  284 lines

  1. // mulmix.c
  2. // Mixed-language program example for demonstrating
  3. // Windows support in Microsoft FORTRAN 5.1
  4.  
  5. #include "windows.h"
  6. #include "mulmix.h"
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11.  
  12. HANDLE hInst;
  13. long dNums[2];
  14. BOOL   bNumsOK[2] = {FALSE, FALSE};
  15.  
  16. int PASCAL WinMain( HANDLE hInstance,     // current instance
  17.                     HANDLE hPrevInstance, // previous instance
  18.                     LPSTR lpCmdLine,      // command line
  19.                     int nCmdShow )        // show-window type (open/icon)
  20. {
  21.     MSG msg;
  22.  
  23.     if (!hPrevInstance)                  // Other instances of app running?
  24.         if (!InitApplication(hInstance)) // Initialize shared things
  25.             return (FALSE);              // Exit if unable to initialize
  26.  
  27.     // Perform initializations that apply to a specific instance
  28.  
  29.     if (!InitInstance( hInstance, nCmdShow ))
  30.         return( FALSE );
  31.  
  32.     // Get, translate, and dispatch messages until WM_QUIT is received
  33.  
  34.     while( GetMessage( &msg,    // Message structure
  35.                        NULL,    // Handle of window receiving the message
  36.                        NULL,    // Lowest message to examine
  37.                        NULL ) ) // Highest message to examine
  38.     {
  39.         TranslateMessage( &msg );    // Translates virtual key codes
  40.         DispatchMessage( &msg );     // Dispatches message to window
  41.     }
  42.     return( msg.wParam );       // Returns the value from PostQuitMessage
  43. } // WinMain
  44.  
  45. // Function: InitApplication
  46. // Initialize the application: fill in the window-class structure
  47. // and register the window class. Return TRUE on success, FALSE
  48. // on failure.
  49.  
  50. BOOL InitApplication( HANDLE hInstance )        // current instance
  51. {
  52.     WNDCLASS  wc;
  53.  
  54.     // Fill in window class structure with parameters that describe the
  55.     // main window.
  56.  
  57.     wc.style = NULL;                    // Class style(s).
  58.     wc.lpfnWndProc = MainWndProc;       // Function to retrieve messages for
  59.                                         //     windows of this class.
  60.     wc.cbClsExtra = 0;                  // No per-class extra data.
  61.     wc.cbWndExtra = 0;                  // No per-window extra data.
  62.     wc.hInstance = hInstance;           // Application that owns the class.
  63.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  64.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  65.     wc.hbrBackground = GetStockObject( WHITE_BRUSH );
  66.     wc.lpszMenuName =  "MulMixMenu";   // Name of menu resource in .RC file.
  67.     wc.lpszClassName = "MulMixWinClass"; // Name used in call to CreateWindow.
  68.  
  69.     // Register the window class and return success/failure code.
  70.  
  71.     return ( RegisterClass( &wc ) );
  72.  
  73. } // InitApplication
  74.  
  75.  
  76. // Function: InitInstance
  77. // Save the instance handle and create the main window for this instance.
  78.  
  79. BOOL InitInstance( HANDLE hInstance, // Current instance identifier.
  80.                    int nCmdShow )     // Param for first ShowWindow() call.
  81. {
  82.     HWND hWnd;               // Main window handle.
  83.     // Save the instance handle in a static variable, which will be used in
  84.     // many subsequent calls from this application to Windows.
  85.  
  86.     hInst = hInstance;
  87.  
  88.     // Create a main window for this application instance.
  89.  
  90.     hWnd = CreateWindow(
  91.         "MulMixWinClass",               // See RegisterClass() call.
  92.         "FORTRAN 5.1 Mixed-Language Example",   // Text for window title bar.
  93.         WS_OVERLAPPEDWINDOW,            // Window style.
  94.         CW_USEDEFAULT,                  // Default horizontal position.
  95.         CW_USEDEFAULT,                  // Default vertical position.
  96.         CW_USEDEFAULT,                  // Default width.
  97.         CW_USEDEFAULT,                  // Default height.
  98.         NULL,                           // Overlapped windows have no parent.
  99.         NULL,                           // Use the window class menu.
  100.         hInstance,                      // This instance owns this window.
  101.         NULL                            // Pointer not needed.
  102.     );
  103.  
  104.     // If window could not be created, return "failure"
  105.  
  106.     if( !hWnd )
  107.         return( FALSE );
  108.  
  109.     // Make the window visible; update its client area; and return "success"
  110.  
  111.     ShowWindow( hWnd, nCmdShow );  // Show the window
  112.     UpdateWindow( hWnd );          // Sends WM_PAINT message
  113.     return( TRUE );                // Returns the value from PostQuitMessage
  114.  
  115. } // InitInstance
  116.  
  117.  
  118. // Function: MainWndProc
  119. // Process messages sent to the main window. Display results
  120. // when user clicks the "Ok" button on the "Multiply" dialog box.
  121.  
  122. long FAR PASCAL MainWndProc( HWND     hWnd,    // window handle
  123.                              unsigned message, // type of message
  124.                              WORD     wParam,  // additional information
  125.                              LONG     lParam ) // additional information
  126. {
  127.     FARPROC lpProc;          // pointer to dialog-box functions
  128.     long dResult;
  129.     BOOL bReadyToGo;
  130.  
  131.     switch( message )
  132.     {
  133.     case WM_COMMAND:       // message: command from application menu
  134.         switch( wParam )
  135.         {
  136.         case IDM_ABOUT:
  137.             lpProc = MakeProcInstance( AboutBoxFunc, hInst );
  138.             DialogBox( hInst,         // current instance
  139.                        "AboutBox",    // resource to use
  140.                        hWnd,          // parent handle
  141.                        lpProc );      // AboutBoxFunc() instance address
  142.             FreeProcInstance(lpProc);
  143.             break;
  144.  
  145.         case IDM_MULTIPLY:
  146.             lpProc = MakeProcInstance( MultiplyBoxFunc, hInst );
  147.             bReadyToGo = DialogBox( hInst,         // current instance
  148.                                     "MultiplyBox", // resource to use
  149.                                     hWnd,          // parent handle
  150.                                     lpProc );      // MultiplyBoxFunc() instance address
  151.             FreeProcInstance( lpProc );
  152.             if( bReadyToGo )
  153.             {   // Show the result
  154.                 mul( dNums[0], dNums[1] );
  155.             }
  156.             break;
  157.  
  158.         default:                // Let Windows process it
  159.             return( DefWindowProc( hWnd, message, wParam, lParam ) );
  160.         } // switch( wParam )
  161.         break;
  162.  
  163.     case WM_DESTROY:          // message: window being destroyed
  164.         PostQuitMessage( 0 );
  165.         break;
  166.  
  167.     default:              // Passes message on if unproccessed
  168.         return( DefWindowProc( hWnd, message, wParam, lParam ) );
  169.     }
  170.     return( NULL );
  171. } // MainWndProc
  172.  
  173.  
  174. // Function: AboutBoxFunc
  175. // Process messages sent to the "About MulMix..." dialog box.
  176.  
  177. BOOL FAR PASCAL AboutBoxFunc(
  178.                       HWND hDlg,        // window handle of the dialog box
  179.                       unsigned message, // type of message
  180.                       WORD     wParam,  // message-specific information
  181.                       LONG     lParam )
  182. {
  183.     switch( message )
  184.     {
  185.  
  186.     case WM_INITDIALOG:             // message: initialize dialog box
  187.         return( TRUE );
  188.  
  189.     case WM_COMMAND:                      // message: received a command
  190.         if( wParam == IDOK                // "OK" box selected?
  191.             || wParam == IDCANCEL)        // System menu close command?
  192.         {
  193.             EndDialog( hDlg, TRUE );      // Exits the dialog box
  194.             return( TRUE );
  195.         }
  196.     break;
  197.     }
  198.     return( FALSE );               // Didn't process a message
  199. } // AboutBoxFunc
  200.  
  201.  
  202. // Function: MultiplyBoxFunc
  203. // Process messages sent to the "Multiply" dialog box. When
  204. // user's input is valid, stuff values into the global variables
  205. // dNums[0] and dNums[1].
  206.  
  207. BOOL FAR PASCAL MultiplyBoxFunc(
  208.                     HWND hDlg,        // window handle of the dialog box
  209.                     unsigned message, // type of message
  210.                     WORD     wParam,  // message-specific information
  211.                     LONG     lParam )
  212. {
  213.     DWORD nNumChars;
  214.     int n;
  215.     char szBuffer[BUFFERSIZE];
  216.     char * pc;
  217.  
  218.     switch( message )
  219.     {
  220.     case WM_INITDIALOG:             // message: initialize dialog box
  221.         for ( n = 0; n <= 1; n++ )
  222.         {
  223.             if( bNumsOK[n] )
  224.             {
  225.                 sprintf(szBuffer, "%ld", dNums[n]);
  226.                 SetDlgItemText( hDlg, ID_MULT_NUM1 + n, szBuffer );
  227.             }
  228.         }
  229.  
  230.         // disable the "OK" button unless both numbers are valid
  231.         EnableWindow( GetDlgItem( hDlg, IDOK ), bNumsOK[0] && bNumsOK[1] );
  232.         return( TRUE );
  233.  
  234.     case WM_COMMAND:        // message: received a command
  235.         switch( wParam )    // wParam is ID of control
  236.         {
  237.         case ID_MULT_NUM1:
  238.         case ID_MULT_NUM2:
  239.  
  240.             if( HIWORD( lParam ) == EN_KILLFOCUS      // notification code
  241.                 || HIWORD( lParam ) == EN_UPDATE)
  242.             {
  243.  
  244.                 szBuffer[0] = (char)BUFFERSIZE - 1;
  245.  
  246.                 // Which edit control is it?
  247.                 n = ( wParam == ID_MULT_NUM1 ) ? 0 : 1;
  248.  
  249.                 // Copy edit-control contents to szBuffer
  250.                 nNumChars = SendMessage( (HWND) LOWORD( lParam ),
  251.                                          EM_GETLINE,
  252.                                          0, (LONG)(LPSTR)szBuffer );
  253.  
  254.                 if( nNumChars )
  255.                 {
  256.                     dNums[n] = strtol( szBuffer, &pc, 10 );
  257.                     bNumsOK[n] = (*pc == '\0') || (*pc == ' ');
  258.                 }
  259.                 else
  260.                 {
  261.                     bNumsOK[n] = FALSE;
  262.                 }
  263.  
  264.                 // disable the "OK" button unless both numbers are valid
  265.                 EnableWindow( GetDlgItem( hDlg, IDOK ),
  266.                               bNumsOK[0] && bNumsOK[1] );
  267.                 return( bNumsOK[0] && bNumsOK[1] );
  268.             }
  269.             else
  270.             {
  271.                 return( FALSE );
  272.             }
  273.         case IDOK:       // "OK" box selected?
  274.             EndDialog( hDlg, TRUE );  // Exits the dialog box
  275.             return( TRUE );
  276.         case IDCANCEL:   // System menu Close command or Cancel button?
  277.             EndDialog( hDlg, FALSE );  // Exits the dialog box
  278.             return( FALSE );
  279.         }
  280.         break;
  281.     }
  282.     return( FALSE );               // Didn't process a message
  283. } // MultiplyBoxFunc
  284.