home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RCPOPUP.ZIP / POPUPLST.C < prev    next >
C/C++ Source or Header  |  1992-10-14  |  13KB  |  328 lines

  1. /**************************************************************************
  2. *
  3. *   Function: popuplst
  4. *
  5. *    Purpose: pop up a list of choices, accept a choice or cancel, return
  6. *             the choice to the caller via pipe.
  7. *
  8. *   To build: pmake popuplst
  9. *
  10. *     To use: popuplst hRespPipe cMaxWidth "comma,delimited,args" ["Title"]
  11. *             Decimal handle # ^         ^                      ^        ^
  12. *                Width of column in list ^                      ^        ^
  13. *                     The list of choices (seperated by commas) ^        ^
  14. *                             The title bar to be placed in the list box ^
  15. *
  16. *   Defaults: none
  17. *
  18. *       File: popuplst.c
  19. *
  20. *   Abstract: Typical PM main function which initializes PM, creates a
  21. *             message queue, registers a window class, creates a window,
  22. *             gets and dispatches messages to its winproc until its time
  23. *             to quit, and then tidies up before terminating.
  24. *
  25. *             This particular PM function supports only a single list box.
  26. *             The selections to be presented in the list are one of the
  27. *             command line arguments of popuplst.  Popup sends the string
  28. *             selected (or a NULL string for CANCEL) to the response pipe
  29. *             designated in the 1st argument.  If a filename is given
  30. *             instead of a number as the 1st argument, that filename will
  31. *             be opened instead and the terminal string will be placed in
  32. *             the file.
  33. *
  34. **************************************************************************/
  35.  
  36. #define INCL_WINDIALOGS
  37. #define INCL_WINERRORS
  38. #define INCL_WINFRAMEMGR
  39. #define INCL_WINLISTBOXES
  40. #define INCL_WINMESSAGEMGR
  41. #define INCL_WINSYS
  42. #define INCL_WINWINDOWMGR
  43. #define M_I86L
  44. #define INCL_DOSFILEMGR
  45. #define INCL_VIO
  46. #include <os2.h>
  47.  
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51.  
  52. #include "popuplst.h"
  53.  
  54. /* Local Routines */ 
  55. static void errex(char *str, unsigned short api_err);
  56. VOID cdecl CenterDlgBox(HWND);
  57.  
  58. /* Window Procedures */
  59. static MRESULT EXPENTRY fnwpMainWnd(HWND, USHORT, MPARAM, MPARAM);
  60. static MRESULT EXPENTRY fnwpListBoxDlg(HWND, USHORT, MPARAM, MPARAM);
  61.  
  62. /*  Global variables */
  63. #define MAXLEN_LISTBOXENTRY         80
  64.  
  65. CHAR  szSelection[MAXLEN_LISTBOXENTRY] = "";  /* Common variable for list */
  66.                                         /* code to copy selected value to */
  67. HAB   hab;                              /* Anchor block handle     */
  68. HWND  hwndClient;                       /* Client Window handle    */
  69. HWND  hwndFrame;                        /* Frame Window handle     */
  70. short cNumChoices, cChoiceWidth;        /* Length and Width of box */
  71. HFILE hfWrite;                          /* Handle of pipe to respond to */
  72. FILE  *fpOutFile;                       /* File pointer to write term string */
  73. char  *psChoiceStr;                     /* String of choices */
  74. char  *psTitleStr;                      /* Optional list box title */
  75.  
  76. /**************************************************************************
  77. *
  78. *   Function: main
  79. *
  80. *    Purpose: Window setup and argument parsing.  Sends response.
  81. *
  82. **************************************************************************/
  83.  
  84. VOID NEAR cdecl main( int argc, char *argv[] )
  85. {
  86.   HMQ     hmq;                          /* Message Queue handle         */
  87.   QMSG    qmsg;                         /* Message                      */
  88.   ULONG   flCreate;
  89.   ERRORID erid;
  90.   USHORT  erno;
  91.  
  92.   cChoiceWidth = atoi(argv[2]);         /* Width of list box            */
  93.   psChoiceStr = argv[3];                /* String of choices            */
  94.   psTitleStr = argv[4];                 /* Title block initializer      */
  95.  
  96.   hab   = WinInitialize( 0 );           /* Initialize PM                */
  97.   if (hab == NULLHANDLE) {
  98.     erid = WinGetLastError(hab);
  99.     erno = ERRORIDERROR(erid);
  100.     errex("WinInitialize failed", erno);
  101.   }
  102.   hmq   = WinCreateMsgQueue( hab, 0 );  /* Create application msg queue */
  103.   if (hmq == NULLHANDLE) {
  104.     erid = WinGetLastError(hab);
  105.     erno = ERRORIDERROR(erid);
  106.     errex("WinCreateMsgQueue failed", erno);
  107.   }
  108.  
  109.   WinRegisterClass(                     /* Register Window Class        */
  110.       hab,                              /* Anchor block handle          */
  111.       "popuplst Class",                 /* Window Class name            */
  112.       (PFNWP)fnwpMainWnd,               /* Address of Window Procedure  */
  113.       (ULONG) NULL,                     /* No special class style       */
  114.       0                                 /* No extra window words        */
  115.       );
  116.  
  117.         /* Create only a minimal window -- tasklist is necessary for
  118.            DosSelectSession (see pltest.c)                              */
  119.   flCreate = FCF_TITLEBAR | FCF_ICON | FCF_TASKLIST;
  120.  
  121.         /* Create a pseudo (invisible) window to handle PM babble */
  122.         /* OS/2 1.x always would use DESKTOP. 2.0 usually uses HWND_OBJECT */
  123.   hwndFrame = WinCreateStdWindow(
  124.         HWND_DESKTOP,                   /* Desktop Window is parent     */
  125.         0,                              /* Window styles (INVISIBLE)    */
  126.         (PVOID)&flCreate,               /* Window creation parameters   */
  127.         "popuplst Class",               /* Window Class name            */
  128.         "",                             /* Window Text                  */
  129.         0L,                             /* Client style                 */
  130.         0,                              /* Module handle                */
  131.         ID_MAINWND,                     /* Window ID                    */
  132.         (HWND FAR *)&hwndClient         /* Client Window handle         */
  133.         );
  134.  
  135.         /* Pop up the list box */
  136.   WinDlgBox(  HWND_DESKTOP,             /* Parent                    */
  137.               hwndFrame,                /* Owner                     */
  138.               (PFNWP)fnwpListBoxDlg,    /* Address of dialog proc    */
  139.               0,                        /* Module handle             */
  140.               DLG_LISTBOX,              /* Id of dialog in resource  */
  141.               NULL );                   /* Initialisation data       */
  142.  
  143.   /* Message Loop */
  144.   while( WinGetMsg( hab, (PQMSG)&qmsg, (HWND)NULL, 0, 0 ) )
  145.     WinDispatchMsg( hab, (PQMSG)&qmsg );
  146.  
  147.   /* Cleanup code */
  148.   WinDestroyWindow( hwndFrame );
  149.   WinDestroyMsgQueue( hmq );
  150.   WinTerminate( hab );
  151.  
  152.   hfWrite = atoi(argv[1]);              /* 1st arg is pipe response handle */
  153.   if (hfWrite != 0) {                   /* If user supplied a pipe */
  154.     USHORT api_err;                     /* generic err rtn code from API calls*/
  155.     ULONG actual;                       /* Dummy # bytes written              */
  156.  
  157.     api_err = DosWrite(hfWrite, szSelection, strlen(szSelection)+1, &actual);
  158.     if (api_err != 0) errex("DosWrite failed", api_err);
  159.     api_err = DosBufReset(hfWrite);     /* Guarantee msg delivery */
  160.     if (api_err != 0) errex("DosBufReset failed", api_err);
  161.   }
  162.   else {                                /* Assume we should write to a file */
  163.     fpOutFile = fopen(argv[1], "w");
  164.     if (fpOutFile != NULL) {
  165.       fprintf(fpOutFile, "DLG complete with: '%s'\n", szSelection);
  166.       fclose(fpOutFile);
  167.     }
  168.   }
  169. }
  170.  
  171. /***********************************************************************
  172. *
  173. *   WinProc: fnwpMainWnd
  174. *
  175. *   Controls the state of the menu, and loads various dialogs.  The
  176. *   dialogs will be modal or modeless depending on the setting of the
  177. *   Modality menuitem.
  178. *
  179. ***********************************************************************/
  180.  
  181. static MRESULT EXPENTRY fnwpMainWnd(HWND hwnd, USHORT message, MPARAM mp1, MPARAM mp2 )
  182. {
  183.   switch(message)
  184.   {
  185.     case WM_CLOSE:
  186.       WinPostMsg( hwnd, WM_QUIT, 0L, 0L );  /* Cause termination    */
  187.       break;
  188.     default:
  189.       return WinDefWindowProc( hwnd, message, mp1, mp2 );
  190.   }
  191.   return FALSE;
  192. }
  193.  
  194. /***********************************************************************
  195. *
  196. *  DlgProc:  fnwpListBoxDlg
  197. *
  198. ***********************************************************************/
  199.  
  200. static MRESULT EXPENTRY fnwpListBoxDlg(HWND hwndDlg, USHORT message, MPARAM mp1, MPARAM mp2 )
  201. {
  202.   char *psNxtArg;
  203.   SHORT  id;
  204.  
  205.   switch (message)
  206.   {
  207.     case WM_INITDLG:
  208.  
  209.       CenterDlgBox( hwndDlg );
  210.  
  211.         /* Initialize the listbox command line argument strings. */
  212.       for ( psNxtArg = strtok(psChoiceStr, ","), cNumChoices=0;
  213.             psNxtArg != NULL;
  214.             psNxtArg = strtok(NULL, ","), cNumChoices++ ) {
  215.         WinSendDlgItemMsg( hwndDlg,
  216.                            LB_1,
  217.                            LM_INSERTITEM,
  218.                            MPFROM2SHORT( LIT_END, 0 ),
  219.                            MPFROMP( psNxtArg )
  220.                          );
  221.       }
  222.       if (cNumChoices==0) goto DO_CANCEL;   /* Misuse of this program! */
  223.         /* If user has provided his own title */
  224.       if ( (psTitleStr!=NULL) && (*psTitleStr!='\0') ) {
  225.         WNDPARAMS wp;
  226.  
  227.         wp.fsStatus = WPM_TEXT;
  228.         wp.cchText = strlen(psTitleStr);
  229.         wp.pszText = psTitleStr;
  230.         wp.cbPresParams = 0;
  231.         wp.pPresParams = NULL;
  232.         wp.cbCtlData = 0;
  233.         wp.pCtlData = NULL;
  234.         WinSendDlgItemMsg( hwndDlg,
  235.                            DLG_TITLE,
  236.                            WM_SETWINDOWPARAMS,
  237.                            MPFROMP( &wp ),
  238.                            NULL
  239.                          );
  240.       }
  241.       break;
  242.     case WM_COMMAND:
  243.       switch( SHORT1FROMMP( mp1 ) )
  244.       {
  245.         case DID_OK:     /* Enter key or pushbutton pressed/ selected */
  246.  
  247. /***********************************************************************
  248. *
  249. *   Find out which item (if any) was selected and return the selected
  250. *   item text.
  251. *
  252. ***********************************************************************/
  253.  
  254.             id = SHORT1FROMMR( WinSendDlgItemMsg( hwndDlg,
  255.                                                   LB_1,
  256.                                                   LM_QUERYSELECTION,
  257.                                                   0L,
  258.                                                   0L ) );
  259.             if( id == LIT_NONE )        /* If no entry selected */
  260.               strcpy( szSelection, "" );
  261.             else                        /* Copy sel. entry to szSelection */
  262.               WinSendDlgItemMsg( hwndDlg,
  263.                                  LB_1,
  264.                                  LM_QUERYITEMTEXT,
  265.                                  MPFROM2SHORT( id, cChoiceWidth ),
  266.                                  MPFROMP( szSelection ) );
  267.               /* Fall through to CANCEL cleanup code */
  268.  
  269.         case DID_CANCEL: /* Escape key or CANCEL pushbutton pressed/selected */
  270.         DO_CANCEL:
  271.             WinDismissDlg( hwndDlg, TRUE );
  272.             WinPostMsg( hwndClient, WM_CLOSE, 0L, 0L );  /* Cause termination    */
  273.           return FALSE;
  274.         default:
  275.           break;
  276.       }
  277.       break;
  278.  
  279.     default:  /* Pass all other messages to the default dialog proc */
  280.       return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  281.   }
  282.   return FALSE;
  283. }
  284.  
  285. /*************************************************************************
  286. *
  287. *   FUNCTION : CenterDlgBox
  288. *
  289. *   Positions the dialog box in the center of the screen
  290. *
  291. *************************************************************************/
  292.  
  293. VOID cdecl CenterDlgBox(HWND hwnd )
  294. {
  295.   SHORT ix, iy;
  296.   SHORT iwidth, idepth;
  297.   SWP   swp;
  298.  
  299.   iwidth = (SHORT)WinQuerySysValue( HWND_DESKTOP, SV_CXSCREEN );
  300.   idepth = (SHORT)WinQuerySysValue( HWND_DESKTOP, SV_CYSCREEN );
  301.   WinQueryWindowPos( hwnd, (PSWP)&swp );
  302.   ix = ( iwidth  - swp.cx ) / 2;
  303.   iy = ( idepth  - swp.cy ) / 2;
  304.         /* Attempt to also put us on the top window so that everyone sees us */
  305.   WinSetWindowPos( hwnd, HWND_TOP, ix, iy, 0, 0, SWP_MOVE|SWP_SHOW|SWP_ZORDER|SWP_ACTIVATE );
  306. }
  307.  
  308. /**************************************************************************
  309. *
  310. *   Function: errex
  311. *
  312. *    Purpose: Do a popup if necessary to show errors
  313. *
  314. **************************************************************************/
  315. static void errex(char *str, unsigned short api_err)
  316. {
  317.   USHORT fWait = VP_WAIT | VP_OPAQUE;
  318.   char sOutStr[512];
  319.  
  320.   VioPopUp(&fWait, 0);
  321.   sprintf(sOutStr, "%s with error %d\n", str, api_err);
  322.   puts(sOutStr);
  323.   puts("hit return to continue...");
  324.   getchar();
  325.   VioEndPopUp(0);
  326.   DosExit(EXIT_PROCESS, api_err);
  327. }
  328.