home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pc3270sa.zip / spl2file / spwinman.c < prev   
Text File  |  2002-02-28  |  22KB  |  596 lines

  1. //******************************************************************************
  2. //
  3. //  File name   : SPWINMAN.C
  4. //
  5. //  Description : make ascii file from host spool file
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //  WinMain()                  - Calls init functions, processes msg loop
  10. //  RegisterApplicationClass() - Register's window class
  11. //  InitInstance()             - Saves instance handle and creates main window
  12. //  SpoolToFileWndProc()       - Processes messages
  13. //  PCFilenameDialog()         - Processes messages for "PCfilename" dialog box
  14. //  About()                    - Processes messages for "About" dialog box
  15. //
  16. //  COMMENTS:
  17. //
  18. //  1.  Windows can have several copies of this application running at the
  19. //      same time.  The variable hGlobalInst keeps track of which instance this
  20. //      application is so that processing will be to the correct window.
  21. //
  22. //  2.  Each instance of this application corresponds to one session
  23. //      instance. This allows menu sensitivity on a session by session
  24. //      basis.
  25. //
  26. //  3.  Invocation:  SPL2FILE /x   where "x" is the alpha session ID. A .. Z
  27. //
  28. //  Copyright  (C) 1993, 1996 IBM Corporation
  29. //                        All rights reserved.
  30. //
  31. //******************************************************************************
  32.  
  33. #include <windows.h>                // required for all Windows applications
  34. #include <windowsx.h>               // Windows Macro APIs, window message crackers
  35.  
  36. #include <string.h>                 // String functions
  37. #include <stdio.h>                  // sscanf  function
  38. #include <ctype.h>                  // toupper function
  39.  
  40. #include "spl2file.h"               // specific to this program
  41.  
  42. #define MAIN                        // Instance the data in this module
  43. #include "spdata.h"                 // Global Data
  44.  
  45. //******************************************************************************
  46. //
  47. // Local Function Definitions
  48. //
  49. //******************************************************************************
  50.  
  51. BOOL NEAR GetSessionIDs( LPSTR );
  52. BOOL NEAR SpawnOtherSessions( void );
  53.  
  54. //******************************************************************************
  55. //
  56. //  FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  57. //
  58. //  PURPOSE: calls initialization function, processes message loop
  59. //
  60. //  COMMENTS:
  61. //
  62. //      Windows recognizes this function by name as the initial entry point
  63. //      for the program.  This function calls the application initialization
  64. //      routine, if no other instance of the program is running, and always
  65. //      calls the instance initialization routine.  It then executes a message
  66. //      retrieval and dispatch loop that is the top-level control structure
  67. //      for the remainder of execution.  The loop is terminated when a WM_QUIT
  68. //      message is received, at which time this function exits the application
  69. //      instance by returning the value passed by PostQuitMessage().
  70. //
  71. //      If this function must abort before entering the message loop, it
  72. //      returns the conventional value NULL.
  73. //
  74. //******************************************************************************
  75. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpzCmdLine, int nCmdShow)
  76. {
  77.   MSG msg;
  78.  
  79.   GetSessionIDs(lpzCmdLine);                        // Get our Session ID A .. H
  80.                                                     //
  81.   lstrcpy(zSpoolToFileClassName, zSpoolToFileClassPrefix ); // Build SpoolToFile Class Name
  82.   lstrcat(zSpoolToFileClassName, zSessionID      ); //
  83.                                                     //
  84.   lstrcpy(zSessionClassName, zSessionClassPrefix ); // Build PC/5250 Session
  85.   lstrcat(zSessionClassName, zSessionID          ); // Class Name
  86.                                                     //
  87.   if( FindWindow(zSpoolToFileClassName, NULL) == NULL ) // Does prev class ins
  88.   {                                                 //
  89.     if( !RegisterApplicationClass(hInstance) )      //
  90.     {                                               //
  91.       LoadString(hInstance, LS_ERROR2_TEXT, zFmtBuffer, sizeof(zFmtBuffer));
  92.       wsprintf( (LPSTR) zMsgBuffer, (LPSTR) zFmtBuffer, (LPSTR) zSessionID);
  93.       MessageBox(NULL, (LPSTR) zMsgBuffer, (LPSTR) zSpoolToFileClassPrefix, MB_OK | MB_ICONQUESTION );
  94.       return( FALSE );                              //
  95.     }                                               //
  96.   }                                                 //
  97.   else                                              //
  98.   {                                                 //
  99.     LoadString(hInstance, LS_ERROR1_TEXT, zFmtBuffer, sizeof(zFmtBuffer));
  100.     wsprintf( (LPSTR) zMsgBuffer, (LPSTR) zFmtBuffer, (LPSTR) zSessionID);
  101.     MessageBox(NULL, (LPSTR) zMsgBuffer, (LPSTR) zSpoolToFileClassPrefix, MB_OK | MB_ICONQUESTION );
  102.     return( FALSE );                                //
  103.   }                                                 //
  104.  
  105.   //** Perform initializations that apply to a specific instance
  106.  
  107.   if (!InitInstance(hInstance, nCmdShow))           //
  108.   {                                                 //
  109.     return( FALSE );                                //
  110.   }                                                 //
  111.  
  112.   if( !InitDDE() )                                  // Init DDE
  113.   {                                                 //
  114.     return( FALSE );                                // Return if ERROR
  115.   }                                                 //
  116.  
  117.   // Dispatch messages until a WM_QUIT message is received.
  118.  
  119.   SpawnOtherSessions();                             // Spawn other sessions
  120.  
  121.   while( GetMessage(&msg, NULL, (unsigned int)NULL, (unsigned int)NULL) )       //
  122.   {                                                 //
  123.     TranslateMessage(&msg);                         // Translates virtual key codes
  124.     DispatchMessage(&msg);                          // Dispatches message to window
  125.   }                                                 //
  126.  
  127.   return (msg.wParam);                              // Returns the value from PostQuitMessage
  128. }                                                   //
  129.  
  130. //******************************************************************************
  131. //
  132. //  FUNCTION: RegisterApplicationClass(HANDLE)
  133. //
  134. //  PURPOSE: Initializes window data and registers window class
  135. //
  136. //  COMMENTS:
  137. //
  138. //      This function is called at initialization time only if no other
  139. //      instances of the application are running.  This function performs
  140. //      initialization tasks that can be done once for any number of running
  141. //      instances.
  142. //
  143. //      In this case, we initialize a window class by filling out a data
  144. //      structure of type WNDCLASS and calling the Windows RegisterClass()
  145. //      function.  Since all instances of this application use the same window
  146. //      class, we only need to do this when the first instance is initialized.
  147. //
  148. //
  149. //******************************************************************************
  150. BOOL RegisterApplicationClass( HANDLE hInstance )
  151. {
  152.   WNDCLASS  wc;
  153.  
  154.   // Fill in window class structure with parameters that describe the
  155.   // main window.
  156.  
  157.  
  158.   wc.hInstance     = hInstance;
  159.   wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(SPICON));
  160.   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  161.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  162.   wc.cbClsExtra    = (int)NULL;
  163.   wc.cbWndExtra    = (int)NULL;
  164.   wc.style         = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  165.   wc.lpszMenuName  = MAKEINTRESOURCE(SPMENU);
  166.   wc.lpszClassName = zSpoolToFileClassName;
  167.   wc.lpfnWndProc   = SpoolToFileWndProc;
  168.  
  169.   // Register the window class and return success/failure code.
  170.  
  171.   return ( RegisterClass(&wc) );
  172. }
  173.  
  174. //******************************************************************************
  175. //
  176. //  FUNCTION:  InitInstance(HANDLE, int)
  177. //
  178. //  PURPOSE:  Saves instance handle and creates main window
  179. //
  180. //  COMMENTS:
  181. //
  182. //      This function is called at initialization time for every instance of
  183. //      this application.  This function performs initialization tasks that
  184. //      cannot be shared by multiple instances.
  185. //
  186. //      In this case, we save the instance handle in a static variable and
  187. //      create and display the main program window.
  188. //
  189. //******************************************************************************
  190. BOOL InitInstance(HANDLE hInstance, int iCmdShow)
  191. {
  192.   HWND hWnd;
  193.   int iX, iY;
  194.   uchar zTag[33];                        //
  195.  
  196.   // Save the instance handle in static variable, which will be used in
  197.   // many subsequence calls from this application to Windows.
  198.  
  199.   hGlobalInst = hInstance;
  200.  
  201.   // Create a main window for this application instance.
  202.  
  203.   iX = GetSystemMetrics( SM_CXSCREEN );
  204.   iY = GetSystemMetrics( SM_CYSCREEN );
  205.  
  206.   LoadString(hInstance, LS_TITLE_TEXT, zFmtBuffer, sizeof(zFmtBuffer));
  207.  
  208.   wsprintf( (LPSTR) zMsgBuffer, (LPSTR) zFmtBuffer, (LPSTR) zSessionID);
  209.  
  210.   hWnd = CreateWindow(
  211.       zSpoolToFileClassName,           // See RegisterClass() call.
  212.       zMsgBuffer,                      // Text for window title bar.
  213.       WS_OVERLAPPEDWINDOW,             // Window style.
  214.       (iX / 4),                        // Default horizontal position.
  215.       (iY / 4),                        // Default horizontal position.
  216.       (iX / 2),                        // Default Width
  217.       (iY / 2),                        // Default Height
  218.       NULL,                            // Overlapped windows have no parent.
  219.       NULL,                            // Use the window class menu.
  220.       hInstance,                       // This instance owns this window.
  221.       NULL                             // Pointer not needed.
  222.   );
  223.  
  224.   // If window could not be created, return "failure"
  225.  
  226.   if ( hWnd == NULL )
  227.   {
  228.     return(FALSE);
  229.   }
  230.  
  231.   hMainWnd = hWnd;                         // Save
  232.                                            //
  233.   LoadString(hGlobalInst, LS_OPTIONS_TAG, zTag, sizeof(zTag) );
  234.   lstrcat(zTag, zSessionID );              // Qualify to session
  235.                                            //
  236.   GetProfileString(zSpoolToFileClassPrefix,// WIN.INI section name
  237.                    zTag,                   // value tag
  238.                    "1 1",                  // Default String to use
  239.                    zMsgBuffer,             // receiving buffer
  240.                    sizeof(zMsgBuffer));    // size of receiving buf
  241.  
  242.   sscanf(zMsgBuffer, "%d %d", &bVisible, &bRemoveSoSi);
  243.  
  244.   // Show the window based on user's choice
  245.  
  246.   if( bVisible )
  247.   {
  248.     ShowWindow(hWnd, SW_MINIMIZE);  // Show the window
  249.                                     //
  250.     UpdateWindow(hWnd);             // Sends WM_PAINT message
  251.   }                                 //
  252.   else                              //
  253.   {                                 //
  254.     ShowWindow(hWnd, SW_HIDE);      // Show the window HIDDEN
  255.   }                                 //
  256.                                     //
  257.   return(TRUE);                     // Return OK
  258. }
  259.  
  260. //****************************************************************************
  261. //
  262. //  FUNCTION: SpoolToFileWndProc(HWND, unsigned, WORD, LONG)
  263. //
  264. //  PURPOSE:  Processes messages
  265. //
  266. //  MESSAGES:
  267. //
  268. //      WM_COMMAND    - application menu (About dialog box)
  269. //      WM_DESTROY    - destroy window
  270. //
  271. //  COMMENTS:
  272. //
  273. //      To process the ID_ABOUT message, call MakeProcInstance() to get the
  274. //      current instance address of the About() function.  Then call Dialog
  275. //      box which will create the box according to the information in your
  276. //      spl2file.rc file and turn control over to the About() function.  When
  277. //      it returns, free the intance address.
  278. //
  279. //**************************************************************************
  280. long FAR PASCAL SpoolToFileWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  281. {
  282.   FARPROC      lpProcModalDialog;
  283.   BYTE         Event;
  284.  
  285.   switch (message)
  286.   {
  287.     case WM_COMMAND:
  288.     {
  289.       switch (GET_WM_COMMAND_ID(wParam,lParam))
  290.       {
  291.         case ID_ABOUT:
  292.           lpProcModalDialog = MakeProcInstance(About, hGlobalInst);
  293.           DialogBox(hGlobalInst, MAKEINTRESOURCE(ABOUTBOX), GET_WM_COMMAND_HWND(wParam,lParam), lpProcModalDialog);
  294.           FreeProcInstance(lpProcModalDialog);
  295.           break;
  296.  
  297.         case ID_SPOOL:   // Spool to File
  298.         {
  299.           TerminateConversation();
  300.           StartSpoolToFile( GET_WM_COMMAND_HWND(wParam,lParam) );
  301.         }
  302.         break;
  303.  
  304.         case ID_ZCONFG: // Configure SpoolToFile
  305.         {
  306.           TerminateConversation();
  307.           ConfigureSpoolToFile( GET_WM_COMMAND_HWND(wParam,lParam) );
  308.         }
  309.         break;
  310.  
  311.         default:
  312.           return( DefWindowProc(hWnd, message, wParam, lParam) );
  313.           break;
  314.       }
  315.     }
  316.     break;
  317.  
  318.     case WM_QUERYOPEN:  // Do not allow ICON to open
  319.       return( FALSE );
  320.       break;
  321.  
  322.     case WM_DESTROY:                  // message: window being destroyed
  323.       TerminateConversation();
  324.       PostQuitMessage(0);
  325.       break;
  326.  
  327. // ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  328. //
  329. //  DDE Messages .....
  330. //
  331. // ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  332.  
  333.     case WM_DDE_INITIATE:
  334.       return( SpDDEInitiate(hWnd, message, wParam, lParam) );
  335.       break;
  336.  
  337.     case WM_DDE_ACK:
  338.       return( SpDDEAck(hWnd, message, wParam, lParam) );
  339.       break;
  340.  
  341.     case WM_DDE_DATA:
  342.       return( SpDDEData(hWnd, message, wParam, lParam) );
  343.       break;
  344.  
  345.     case WM_DDE_REQUEST:
  346.       return( SpDDERequest(hWnd, message, wParam, lParam) );
  347.       break;
  348.  
  349.     case WM_DDE_TERMINATE:
  350.       return( SpDDETerminate(hWnd, message, wParam, lParam) );
  351.       break;
  352.  
  353. // ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  354. //
  355. //  User defined
  356. //
  357. // ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  358.     case WM_USER_END:
  359.       Event = EV_END;
  360.       return ( FSM_Event(Event, (unsigned short)NULL, NULL) );
  361.       break;
  362.  
  363.     case WM_USER_ERROR:
  364.       Event = EV_ERROR;
  365.       return ( FSM_Event(Event, (unsigned short)NULL, NULL) );
  366.       break;
  367.  
  368.     case WM_USER_ABORT:
  369.       Event = EV_ABORT;
  370.       return ( FSM_Event(Event, (unsigned short)NULL, NULL) );
  371.       break;
  372.  
  373.     case WM_USER_CONT:
  374.       Event = EV_CONT;
  375.       return ( FSM_Event(Event, (unsigned short)NULL, NULL) );
  376.       break;
  377.  
  378.     case WM_USER_BREAK:
  379.       Event = EV_BRK;
  380.       return ( FSM_Event(Event, (unsigned short)NULL, NULL) );
  381.       break;
  382.  
  383.     default:                          /* Passes it on if unproccessed    */
  384.       return( DefWindowProc(hWnd, message, wParam, lParam) );
  385.       break;
  386.   }
  387.   return( FALSE );
  388. }
  389.  
  390. //******************************************************************************
  391. //
  392. //  GetSessionIDs - Retrieve the session ID's from the command line.
  393. //
  394. //  Abstract:
  395. //
  396. //  If sessions ID's are specified on the command line start them. If no
  397. //  ID's are specified on the command line use this instance as session A's
  398. //  instance as a default.
  399. //
  400. //  The command line for a session instance will be
  401. //
  402. //     session  /[a][b][c]...[z]  allowed session ID's
  403. //
  404. //
  405. //  Notes:
  406. //
  407. //
  408. //  Attributes:
  409. //
  410. //  Returns: BOOL  (FALSE = ERROR)
  411. //
  412. //******************************************************************************
  413. BOOL NEAR GetSessionIDs( LPSTR lpzCmdLine )
  414. {
  415.   int  i, j;
  416.   int  iLength          = lstrlen(lpzCmdLine);
  417.   int  iOtherCount      = (int)NULL;
  418.   BOOL bForThisInstance = TRUE;
  419.  
  420.   zSessionID[1] = (unsigned char)NULL;        // Null Terminator
  421.                                               //
  422.   for( i = 0; i < iLength; i++ )              // Scan across for "/"
  423.   {                                           //
  424.     if( *lpzCmdLine++ == '/' )                //
  425.     {                                         //
  426.       for( j = i+1; j < iLength; j++ )        //
  427.       {                                       //
  428.         uchar cID = *lpzCmdLine++;            //
  429.                                               //
  430.         cID = (uchar) toupper( cID );         // Upcase the character
  431.                                               //
  432.         if( 'A' <= cID && cID <= 'Z' )        //
  433.         {                                     //
  434.           if( bForThisInstance )              //
  435.           {                                   //
  436.             zSessionID[0] = (uchar) cID;      //
  437.             bForThisInstance = FALSE;         //
  438.           }                                   //
  439.           else                                //
  440.           {                                   //
  441.             zOtherSessionIDs[iOtherCount++] = (uchar) cID;
  442.           }                                   //
  443.         }                                     //
  444.       }                                       //
  445.       zOtherSessionIDs[iOtherCount] = (unsigned char)NULL;   // Terminate it
  446.       return(TRUE);                           // Never an Error
  447.     }                                         //
  448.   }                                           //
  449.                                               //
  450.   zSessionID[0] = (uchar) 'A';                // Default to 'A'
  451.                                               //
  452.   return(TRUE);                               //
  453. }                                             //
  454.  
  455. //******************************************************************************
  456. //
  457. //  SpawnOtherSessions - Spawns other sessions when multiple sessions
  458. //                       are started on the spl2file command line.
  459. //
  460. //  Abstract:
  461. //
  462. //  The command line for a session instance will be
  463. //
  464. //     session  /[a][b][c][d][e][f][g][h]  allowed session ID's
  465. //
  466. //
  467. //  Notes:
  468. //
  469. //
  470. //  Attributes:
  471. //
  472. //  Returns: BOOL  (FALSE = ERROR)
  473. //
  474. //******************************************************************************
  475. BOOL NEAR SpawnOtherSessions( void )
  476. {
  477.   int  iRC, i;
  478.   int  iLength = lstrlen(zOtherSessionIDs);
  479.  
  480.   if( iLength > 0 )
  481.   {
  482.     for( i = 0; i < iLength; i++ )
  483.     {
  484.       wsprintf(zMsgBuffer,"Spl2File /%c", (uchar) zOtherSessionIDs[i]);
  485.       iRC = WinExec((LPSTR) zMsgBuffer, SW_SHOWMINIMIZED);
  486.       if( iRC <= 32 )
  487.       {
  488.         MessageBox(NULL, (LPSTR) "Winexec for Spl2File Failed", (LPSTR) "Spl2File", MB_OK | MB_ICONQUESTION );
  489.       }
  490.     }
  491.   }
  492.   return( TRUE );
  493. }
  494.  
  495. //******************************************************************************
  496. //
  497. //  FUNCTION: PCFilenameDialog(HWND, unsigned, WORD, LONG)
  498. //
  499. //  PURPOSE:  Processes messages for PC filename dialog box
  500. //
  501. //  MESSAGES:
  502. //
  503. //      WM_INITDIALOG - initialize dialog box
  504. //      WM_COMMAND    - Input received
  505. //
  506. //  COMMENTS:
  507. //
  508. //      No initialization is needed for this particular dialog box, but TRUE
  509. //      must be returned to Windows.
  510. //
  511. //      Wait for user to click on "Ok" button, then close the dialog box.
  512. //
  513. //******************************************************************************
  514. BOOL FAR PASCAL PCFilenameDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  515. {
  516.   switch (message)
  517.   {
  518.     case WM_INITDIALOG:                     // message: initialize dialog box
  519.       CenterDialogOnScreen( hDlg );         // Center it
  520.       return(TRUE);
  521.       break;
  522.  
  523.     case WM_COMMAND:                        // message: received a command
  524.       switch (GET_WM_COMMAND_ID(wParam,lParam))
  525.       {
  526.         case IDOK:
  527.           {
  528.             HWND hEditControl = GetDlgItem(hDlg, ID_EDIT1);
  529.             if (GetWindowText(hEditControl, zPCFilename, sizeof(zPCFilename) ) == 0)
  530.               EndDialog(hDlg, FALSE);         // Exits the dialog box
  531.             else
  532.               EndDialog(hDlg, TRUE);          // Exits the dialog box
  533.           }
  534.           return(TRUE);
  535.           break;
  536.  
  537.         case IDCANCEL:
  538.           EndDialog(hDlg, FALSE);           // Exits the dialog box
  539.           return(TRUE);
  540.           break;
  541.  
  542.         default:
  543.           break;
  544.  
  545.       }
  546.       break;
  547.   }
  548.   return(FALSE);                            // Didn't process a message
  549. }
  550.  
  551. //******************************************************************************
  552. //
  553. //  FUNCTION: About(HWND, unsigned, WORD, LONG)
  554. //
  555. //  PURPOSE:  Processes messages for "About" dialog box
  556. //
  557. //  MESSAGES:
  558. //
  559. //      WM_INITDIALOG - initialize dialog box
  560. //      WM_COMMAND    - Input received
  561. //
  562. //  COMMENTS:
  563. //
  564. //      No initialization is needed for this particular dialog box, but TRUE
  565. //      must be returned to Windows.
  566. //
  567. //      Wait for user to click on "Ok" button, then close the dialog box.
  568. //
  569. //******************************************************************************
  570. BOOL FAR PASCAL About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  571. {
  572.   switch (message)
  573.   {
  574.     case WM_INITDIALOG:                     // message: initialize dialog box
  575.       CenterDialogOnScreen( hDlg );         // Center it
  576.       return(TRUE);
  577.       break;
  578.  
  579.     case WM_COMMAND:                        // message: received a command
  580.       switch (GET_WM_COMMAND_ID(wParam,lParam))
  581.       {
  582.         case IDOK:
  583.         case IDCANCEL:
  584.           EndDialog(hDlg, TRUE);            // Exits the dialog box
  585.           return(TRUE);
  586.           break;
  587.  
  588.         default:
  589.           break;
  590.  
  591.       }
  592.       break;
  593.   }
  594.   return(FALSE);                            // Didn't process a message
  595. }
  596.