home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / PMHPENV.ZIP / PMHPENV.C < prev    next >
C/C++ Source or Header  |  1990-09-22  |  14KB  |  498 lines

  1.  
  2. /*
  3.  *  OS/2 Presentation Manager HP LaserJet Envelope Printing Program
  4.  *
  5.  *  Dave Briccetti & Associates         CompuServe: 74475,1072
  6.  *  P.O. Box 1713
  7.  *  Lafayette, CA  94549-7013
  8.  *  USA
  9.  *
  10.  *  Copyright (c) 1990, David C. Briccetti
  11.  *
  12.  *  See the extended help in the program for more information.
  13.  */
  14.  
  15. #include <io.h>
  16. #include <malloc.h>
  17. #include <process.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define INCL_WINDIALOGS
  23. #define INCL_WINFRAMEMGR
  24. #define INCL_WINHELP
  25. #define INCL_WINMESSAGEMGR
  26. #define INCL_WINMENUS
  27. #define INCL_WINMLE
  28. #define INCL_WINRECTANGLES
  29. #define INCL_WINSHELLDATA
  30. #define INCL_WINSYS
  31. #define INCL_WINWINDOWMGR
  32. #include <os2.h>
  33.  
  34. #include "pmenv.h"
  35.  
  36.  
  37. #define OUTSTREAM stdprn
  38. #define MLE_BUFSIZE 2000
  39.  
  40.  
  41. static  HAB     hab;
  42. static  HWND    hwndHelpInstance;
  43.  
  44.  
  45. static MRESULT EXPENTRY
  46. DlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  47.  
  48. static ULONG
  49. GetAllText (HWND hwnd, USHORT idMle, USHORT cbBuf);
  50.  
  51. static ULONG
  52. SetAllText (HWND hwnd, USHORT idMle, ULONG cbCopy);
  53.  
  54. static VOID PrintEnvelope (HWND hwnd, USHORT idAddr,
  55.     USHORT idRetAddr, PBYTE pabBufAddr, PBYTE pabBufRetAddr);
  56.  
  57. static HWND InitializeHelp (HAB hab, HWND hwndFrame);
  58.  
  59. static MRESULT ProcessHmMessages (HWND hwndHelpInstance,
  60.     USHORT msg, MPARAM mp1, MPARAM mp2);
  61.  
  62. static VOID CenterDlgBox( HWND hwnd );
  63.  
  64. static VOID SetSystemMenu( HWND hwndDlg );
  65.  
  66.  
  67.  
  68. /* ----------------------------------------------------------------- */
  69.  
  70. VOID main (VOID)
  71. {
  72.     HMQ     hmq;
  73.     QMSG    qmsg;
  74.     HWND    hwndDlg;
  75.  
  76.     hab = WinInitialize (0);
  77.     hmq = WinCreateMsgQueue (hab, DEFAULT_QUEUE_SIZE);
  78.  
  79.     hwndDlg = WinLoadDlg (HWND_DESKTOP, HWND_DESKTOP,
  80.         DlgProc, 0, DID_DLG, 0);
  81.  
  82.     hwndHelpInstance = InitializeHelp (hab, hwndDlg);
  83.  
  84.     while (WinGetMsg (hab, &qmsg, 0, 0, 0))
  85.         WinDispatchMsg (hab, &qmsg);
  86.  
  87.     if (hwndHelpInstance)
  88.     WinDestroyHelpInstance (hwndHelpInstance);
  89.  
  90.     WinDestroyMsgQueue (hmq);
  91.     WinTerminate (hab);
  92. }
  93.  
  94.  
  95.  
  96. /* ----------------------------------------------------------------- */
  97.  
  98. static MRESULT EXPENTRY
  99. DlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  100. {
  101.     static  PBYTE   pabBufAddr;
  102.     static  PBYTE   pabBufRetAddr;
  103.     ULONG   cbData;
  104.  
  105.     switch (msg)
  106.     {
  107.         case WM_INITDLG:
  108.         {
  109.             HWND    hwndStamp;
  110.             RECTL   rclStamp;
  111.             SWP     swpEnvelope;
  112.  
  113.  
  114.             /* Make cosmetic changes to dialog box */
  115.  
  116.             CenterDlgBox (hwnd);
  117.             SetSystemMenu (hwnd);
  118.  
  119.  
  120.             /* Place stamp in upper right corner of envelope */
  121.  
  122.             WinQueryWindowRect (hwndStamp =
  123.                 WinWindowFromID (hwnd, DID_STAMP),
  124.                 &rclStamp);
  125.             WinQueryWindowPos (
  126.                 WinWindowFromID (hwnd, DID_ENVELOPE),
  127.                 &swpEnvelope);
  128.  
  129.             WinSetWindowPos (hwndStamp, 0,
  130.                 (SHORT) (swpEnvelope.x +
  131.                     swpEnvelope.cx - (rclStamp.xRight * 6 / 5)),
  132.                 (SHORT) (swpEnvelope.y +
  133.                     swpEnvelope.cy - (rclStamp.yTop * 6 / 5)),
  134.                 0, 0, SWP_MOVE);
  135.  
  136.  
  137.             /* Allocate MLE buffers and assign them to MLE controls */
  138.  
  139.             pabBufAddr    = halloc (MLE_BUFSIZE, sizeof (BYTE));
  140.             pabBufRetAddr = halloc (MLE_BUFSIZE, sizeof (BYTE));
  141.  
  142.             WinSendDlgItemMsg (hwnd, DID_ADDR,
  143.                 MLM_SETIMPORTEXPORT, MPFROMP (pabBufAddr),
  144.                 MPFROMSHORT (MLE_BUFSIZE));
  145.             WinSendDlgItemMsg (hwnd, DID_RETURN_ADDR,
  146.                 MLM_SETIMPORTEXPORT, MPFROMP (pabBufRetAddr),
  147.                 MPFROMSHORT (MLE_BUFSIZE));
  148.  
  149.             /* Retrieve saved data from user profile */
  150.  
  151.             cbData = MLE_BUFSIZE;
  152.             if (PrfQueryProfileData (HINI_USERPROFILE,
  153.                 "PMHPENV", "Address", pabBufAddr, &cbData))
  154.                 SetAllText (hwnd, DID_ADDR, cbData);
  155.  
  156.             cbData = MLE_BUFSIZE;
  157.             if (PrfQueryProfileData (HINI_USERPROFILE, "PMHPENV",
  158.                 "ReturnAddress", pabBufRetAddr, &cbData))
  159.                 SetAllText (hwnd, DID_RETURN_ADDR, cbData);
  160.  
  161.             return 0;
  162.         }
  163.  
  164.  
  165.         case WM_COMMAND:
  166.             switch (SHORT1FROMMP (mp1))
  167.             {
  168.                 case DID_SAVE:
  169.  
  170.                     /* Save address in profile */
  171.  
  172.                     cbData = GetAllText (hwnd, DID_ADDR,
  173.                         MLE_BUFSIZE);
  174.  
  175.                     PrfWriteProfileData (HINI_USERPROFILE,
  176.                         "PMHPENV", "Address", pabBufAddr, cbData);
  177.  
  178.                     break;
  179.  
  180.  
  181.                 case DID_SAVE_RET:
  182.  
  183.                     /* Save return address in profile */
  184.  
  185.                     cbData = GetAllText (hwnd, DID_RETURN_ADDR,
  186.                         MLE_BUFSIZE);
  187.  
  188.                     PrfWriteProfileData (HINI_USERPROFILE, "PMHPENV",
  189.                         "ReturnAddress", pabBufRetAddr, cbData);
  190.  
  191.                     break;
  192.  
  193.  
  194.                 case DID_PRINT:
  195.                     PrintEnvelope (hwnd, DID_ADDR,
  196.                         DID_RETURN_ADDR, pabBufAddr,
  197.                         pabBufRetAddr);
  198.                     break;
  199.  
  200.  
  201.                 case DID_CANCEL:
  202.                     WinPostMsg (hwnd, WM_CLOSE, 0L, 0L);
  203.                     break;
  204.             }
  205.  
  206.             break;
  207.  
  208.  
  209.         case WM_CLOSE:
  210.             WinPostMsg (0, WM_QUIT, 0L, 0L);
  211.             WinDestroyWindow (hwnd);
  212.             hfree (pabBufAddr);
  213.             hfree (pabBufRetAddr);
  214.             break;
  215.  
  216.  
  217.         case HM_ERROR:
  218.             return ProcessHmMessages (hwndHelpInstance,
  219.                 msg, mp1, mp2);
  220.  
  221.         default:
  222.             return WinDefDlgProc (hwnd, msg, mp1, mp2);
  223.     }
  224.  
  225.     return (MRESULT) 0;
  226. }
  227.  
  228.  
  229.  
  230. /* ----------------------------------------------------------------- */
  231.  
  232. static ULONG
  233. GetAllText (HWND hwnd, USHORT idMle, USHORT cbBuf)
  234. {
  235.     /*
  236.      *  Export text from MLE
  237.      */
  238.  
  239.     ULONG   lOffset = 0;
  240.     ULONG   cbCopy = cbBuf;
  241.     ULONG   cbText;
  242.  
  243.     cbText = LONGFROMMR (WinSendDlgItemMsg (hwnd, idMle,
  244.         MLM_QUERYTEXTLENGTH, 0, 0));
  245.  
  246.     return WinSendDlgItemMsg (hwnd, idMle,
  247.         MLM_EXPORT, MPFROMP (&lOffset), MPFROMP (&cbCopy)) ?
  248.         cbText : 0;
  249. }
  250.  
  251.  
  252.  
  253.  
  254. /* ----------------------------------------------------------------- */
  255.  
  256. static ULONG
  257. SetAllText (HWND hwnd, USHORT idMle, ULONG cbCopy)
  258. {
  259.     /*
  260.      *  Export text to MLE
  261.      */
  262.  
  263.     ULONG   lOffset = 0;
  264.  
  265.     return LONGFROMMR (WinSendDlgItemMsg (hwnd, idMle,
  266.         MLM_IMPORT, MPFROMP (&lOffset),
  267.         MPFROMP (&cbCopy)));
  268. }
  269.  
  270.  
  271.  
  272. /* ----------------------------------------------------------------- */
  273.  
  274. static VOID PrintEnvelope (HWND hwnd, USHORT idAddr,
  275.     USHORT idRetAddr, PBYTE pabBufAddr, PBYTE pabBufRetAddr)
  276. {
  277.     /*
  278.      *  Print Envelope
  279.      */
  280.  
  281.     FILE    *pfPrint;
  282.     static  PSZ pszTempTemplate = "HPPXXXXX";
  283.     CHAR    szTempFilename [9];
  284.     CHAR    szCommand [40];
  285.     ULONG   cbData;
  286.  
  287.  
  288.     /* Export MLE data to buffers */
  289.  
  290.     cbData = GetAllText (hwnd, idRetAddr,
  291.         MLE_BUFSIZE);
  292.     pabBufRetAddr [cbData] = 0;
  293.  
  294.     cbData = GetAllText (hwnd, idAddr,
  295.         MLE_BUFSIZE);
  296.     pabBufAddr [cbData] = 0;
  297.  
  298.  
  299.     /* Set up temporary print file */
  300.  
  301.     strcpy (szTempFilename, pszTempTemplate);
  302.     mktemp (szTempFilename);
  303.     pfPrint = fopen (szTempFilename, "w");
  304.  
  305.     if (! pfPrint)
  306.     {
  307.         WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  308.             "Can not open temporary print file",
  309.             "Printing Error", 1, MB_OK | MB_MOVEABLE);
  310.  
  311.         return;
  312.     }
  313.  
  314.     /* Print return address */
  315.  
  316.     fprintf( pfPrint,
  317.         "\x01b" "E\x1b&l1O\x1b&l3H\x1b&l15E" );
  318.     fprintf( pfPrint, "\x1b&a1r&a15L" );
  319.     fprintf( pfPrint, pabBufRetAddr );
  320.  
  321.  
  322.     /* Print address */
  323.  
  324.     fprintf( pfPrint, "\x1b&a11R" );  /* Line position */
  325.     fprintf( pfPrint, "\x1b&a58L" );  /* Column position */
  326.  
  327.     fprintf( pfPrint, pabBufAddr );
  328.  
  329.     fprintf( pfPrint, "\f\x01b" "E" );
  330.  
  331.  
  332.     /* Close temp file, copy to prn and delete */
  333.  
  334.     fclose (pfPrint);
  335.  
  336.     sprintf (szCommand, "copy %s prn", szTempFilename);
  337.     system (szCommand);
  338.     unlink (szTempFilename);
  339. }
  340.  
  341.  
  342.  
  343. /* ----------------------------------------------------------------- */
  344.  
  345. static HWND InitializeHelp (HAB hab, HWND hwndFrame)
  346. {
  347.     HELPINIT    hmiHelpData;   /* Help initialization structure */
  348.     HWND        hwndHelpInstance;
  349.     CHAR            szMsg[100];
  350.  
  351.     memset (&hmiHelpData, 0, sizeof (HELPINIT));
  352.     hmiHelpData.cb = sizeof (HELPINIT);
  353.     hmiHelpData.phtHelpTable = (PVOID) (0xffff0000 | DID_DLG);
  354.     hmiHelpData.pszHelpWindowTitle = "Print Envelope Help Window";
  355.     hmiHelpData.pszHelpLibraryName = "PMHPENV.HLP";
  356.  
  357.     hwndHelpInstance = WinCreateHelpInstance (hab, &hmiHelpData);
  358.  
  359.     if (hwndHelpInstance && hmiHelpData.ulReturnCode)
  360.     {
  361.     WinDestroyHelpInstance (hwndHelpInstance);
  362.     hwndHelpInstance = 0;
  363.     }
  364.  
  365.     if (!hwndHelpInstance)
  366.     {
  367.     sprintf (szMsg, "WinCreateHelpInstance failed, rc = %lx.  "
  368.         "Program will continue without help.",
  369.         hmiHelpData.ulReturnCode);
  370.  
  371.     WinMessageBox (HWND_DESKTOP, HWND_DESKTOP, szMsg,
  372.         (PSZ) "Help Creation Error", 1,
  373.         MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  374.     }
  375.     else
  376.     WinAssociateHelpInstance (hwndHelpInstance, hwndFrame);
  377.  
  378.     return hwndHelpInstance;
  379. }
  380.  
  381.  
  382.  
  383. /* ----------------------------------------------------------------- */
  384.  
  385. static MRESULT ProcessHmMessages (HWND hwndHelpInstance,
  386.     USHORT msg, MPARAM mp1, MPARAM mp2)
  387. {
  388.     switch (msg)
  389.     {
  390.     case HM_ERROR:
  391.     if (hwndHelpInstance && LONGFROMMP(mp1) == HMERR_NO_MEMORY)
  392.     {
  393.         WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  394.         (PSZ) "Help Terminated Due to Error",
  395.         (PSZ) "Help Error",
  396.         1,
  397.         MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  398.         WinDestroyHelpInstance (hwndHelpInstance);
  399.     }
  400.     else
  401.     {
  402.             CHAR    szMsg[ 50 ];
  403.  
  404.             sprintf (szMsg, "Help Error %lx Occurred",
  405.                 LONGFROMMP (mp1));
  406.         WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  407.         szMsg, "Help Error", 0,
  408.         MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  409.     }
  410.  
  411.     return (MRESULT) 0;
  412.     }
  413.  
  414.     return (MRESULT) 0;
  415. }
  416.  
  417.  
  418.  
  419. /*********************************************************************/
  420. /*                                                                   */
  421. /*  FUNCTION : CenterDlgBox                                          */
  422. /*                                                                   */
  423. /*  Positions the dialog box in the center of the screen             */
  424. /*                                                                   */
  425. /*********************************************************************/
  426.  
  427. static VOID CenterDlgBox( HWND hwnd )
  428. {
  429.   SHORT ix, iy;
  430.   SHORT iwidth, idepth;
  431.   SWP   swp;
  432.  
  433.   /* Query width and depth of screen device                          */
  434.   iwidth = (SHORT)WinQuerySysValue( HWND_DESKTOP, SV_CXSCREEN );
  435.   idepth = (SHORT)WinQuerySysValue( HWND_DESKTOP, SV_CYSCREEN );
  436.  
  437.   /* Query width and depth of dialog box                             */
  438.   WinQueryWindowPos( hwnd, (PSWP)&swp );
  439.  
  440.   /* Center dialog box within the screen                             */
  441.   ix = ( iwidth  - swp.cx ) / 2;
  442.   iy = ( idepth  - swp.cy ) / 2;
  443.   WinSetWindowPos( hwnd, HWND_TOP, ix, iy, 0, 0, SWP_MOVE );
  444. }
  445.  
  446.  
  447.  
  448.  
  449. /*********************************************************************/
  450. /*                                                                   */
  451. /* FUNCTION: SetSystemMenu                                           */
  452. /*                                                                   */
  453. /* Edit items in the system menu to leave "Move", "Close", and       */
  454. /* "Switch to Task Manager".                                         */
  455. /*                                                                   */
  456. /*********************************************************************/
  457. static VOID SetSystemMenu( HWND hwndDlg )
  458. {
  459.   HWND     hwndSysMenu;                 /* sys menu pull-down handle */
  460.   MENUITEM miTemp;                      /* menu item template        */
  461.   SHORT    sItemId;                     /* system menu item ID       */
  462.   SHORT    sItemIndex;                  /* system menu item index    */
  463.  
  464.   /*******************************************************************/
  465.   /* Get the handle of the system menu pull-down.                    */
  466.   /*******************************************************************/
  467.   hwndSysMenu = WinWindowFromID( hwndDlg, FID_SYSMENU );
  468.   WinSendMsg( hwndSysMenu,
  469.               MM_QUERYITEM,
  470.               MPFROM2SHORT( SC_SYSMENU, FALSE ),
  471.               MPFROMP( (PSZ)&miTemp ));
  472.   hwndSysMenu = miTemp.hwndSubMenu;
  473.  
  474.   /*******************************************************************/
  475.   /* Remove all items from the system menu pull-down that are no     */
  476.   /* longer wanted.                                                  */
  477.   /*******************************************************************/
  478.   for ( sItemIndex = 0, sItemId = 0; sItemId != MIT_ERROR; sItemIndex++)
  479.   {
  480.     sItemId = SHORT1FROMMR (WinSendMsg( hwndSysMenu,
  481.                                  MM_ITEMIDFROMPOSITION,
  482.                                  MPFROMSHORT( sItemIndex ),
  483.                 (MPARAM) 0 ));
  484.     if ( sItemId != MIT_ERROR &&
  485.          sItemId != SC_MOVE   &&
  486.          sItemId != SC_CLOSE  &&
  487.          sItemId != SC_TASKMANAGER )
  488.     {
  489.       WinSendMsg( hwndSysMenu,
  490.                   MM_DELETEITEM,
  491.                   MPFROM2SHORT( sItemId, FALSE ),
  492.                  (MPARAM)NULL );
  493.       sItemIndex--;
  494.     }
  495.   }
  496.  
  497. }
  498.