home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / gw / oddev.exe / MPPRINT.C < prev    next >
Text File  |  1994-07-01  |  11KB  |  372 lines

  1. /***************************************************************************
  2.  *                                       *
  3.  *  MODULE    : MpPrint()                           *
  4.  *                                       *
  5.  *  PURPOSE    : Printing code for MultODMA.                   *
  6.  *                                       *
  7.  *  FUNCTIONS    : GetPrinterDC ()       -  Creates a printer DC for the *
  8.  *                          default device.           *
  9.  *                                       *
  10.  *          AbortProc ()           -  Export proc. for GDI to check*
  11.  *                          print abort.           *
  12.  *                                       *
  13.  *          PrintDlgProc ()       -  Dialog function for the print*
  14.  *                          cancel dialog.           *
  15.  *                                       *
  16.  *          PrintFile ()           -  Prints the contents of the   *
  17.  *                          edit control.           *
  18.  *                                       *
  19.  *          GetInitializationData () -  Gets DC initialisation data  *
  20.  *                          from a DC supporting       *
  21.  *                          ExtDeviceMode().           *
  22.  *                                       *
  23.  ***************************************************************************/
  24. #include "multipad.h"
  25. #include "commdlg.h"
  26.  
  27. extern PRINTDLG pd;      /* Common print dialog structure */
  28. BOOL fAbort;        /* TRUE if the user has aborted the print job     */
  29. HWND hwndPDlg;        /* Handle to the cancel print dialog         */
  30. PSTR szTitle;        /* Global pointer to job title             */
  31. HANDLE hInitData=NULL;    /* handle to initialization data         */
  32.  
  33.  
  34. /****************************************************************************
  35.  *                                        *
  36.  *  FUNCTION   : GetPrinterDC ()                        *
  37.  *                                        *
  38.  *  PURPOSE    : Creates a printer display context for the printer  *
  39.  *                                        *
  40.  *  RETURNS    : HDC   - A handle to printer DC.                *
  41.  *                                        *
  42.  ****************************************************************************/
  43. HDC FAR PASCAL GetPrinterDC(void)
  44. {
  45.  
  46.     HDC         hDC;
  47.     LPDEVMODE   lpDevMode = NULL;
  48.     LPDEVNAMES  lpDevNames;
  49.     LPSTR       lpszDriverName;
  50.     LPSTR       lpszDeviceName;
  51.     LPSTR       lpszPortName;
  52.  
  53.     if (!PrintDlg((LPPRINTDLG)&pd))
  54.         return(NULL);
  55.  
  56.     if (pd.hDC)
  57.       {
  58.         hDC = pd.hDC;
  59.       }
  60.     else
  61.       {
  62.  
  63.         if (!pd.hDevNames)
  64.             return(NULL);
  65.  
  66.         lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
  67.         lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
  68.         lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
  69.         lpszPortName   = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
  70.         GlobalUnlock(pd.hDevNames);
  71.  
  72.         if (pd.hDevMode)
  73.             lpDevMode = (LPDEVMODE)GlobalLock(pd.hDevMode);
  74.  
  75.         hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode);
  76.  
  77.         if (pd.hDevMode && lpDevMode)
  78.             GlobalUnlock(pd.hDevMode);
  79.       }
  80.  
  81.     if (pd.hDevNames)
  82.         GlobalFree(pd.hDevNames);
  83.     if (pd.hDevMode)
  84.         GlobalFree(pd.hDevMode);
  85.     return(hDC);
  86. }
  87.  
  88.  
  89. /****************************************************************************
  90.  *                                        *
  91.  *  FUNCTION   : AbortProc()                            *
  92.  *                                        *
  93.  *  PURPOSE    : To be called by GDI print code to check for user abort.    *
  94.  *                                        *
  95.  ****************************************************************************/
  96. int FAR PASCAL AbortProc ( hdc, reserved )
  97. HDC hdc;
  98. WORD reserved;
  99. {
  100.     MSG msg;
  101.  
  102.     /* Allow other apps to run, or get abort messages */
  103.     while (!fAbort && PeekMessage (&msg, NULL, NULL, NULL, TRUE))
  104.     if (!hwndPDlg || !IsDialogMessage (hwndPDlg, &msg)){
  105.         TranslateMessage (&msg);
  106.         DispatchMessage  (&msg);
  107.     }
  108.     return !fAbort;
  109. }
  110.  
  111. /****************************************************************************
  112.  *                                        *
  113.  *  FUNCTION   : PrintDlgProc ()                        *
  114.  *                                        *
  115.  *  PURPOSE    : Dialog function for the print cancel dialog box.        *
  116.  *                                        *
  117.  *  RETURNS    : TRUE  - OK to abort/ not OK to abort                *
  118.  *         FALSE - otherwise.                        *
  119.  *                                        *
  120.  ****************************************************************************/
  121. BOOL FAR PASCAL PrintDlgProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  122. {
  123.    switch (msg){
  124.        case WM_COMMAND:
  125.            /* abort printing if the only button gets hit */
  126.            fAbort = TRUE;
  127.            break;
  128.  
  129.     default:
  130.         return FALSE;
  131.     }
  132.     return TRUE;
  133. }
  134.  
  135. /****************************************************************************
  136.  *                                        *
  137.  *  FUNCTION   : PrintFile ()                            *
  138.  *                                        *
  139.  *  PURPOSE    : Prints the contents of the edit control.            *
  140.  *                                        *
  141.  ****************************************************************************/
  142.  
  143. VOID FAR PASCAL PrintFile(HWND hwnd)
  144. {
  145.     HDC     hdc;
  146.     int     yExtPage;
  147.     char    sz[32];
  148.     WORD    cch;
  149.     WORD    ich;
  150.     PSTR    pch;
  151.     WORD    iLine;
  152.     WORD    nLinesEc;
  153.     HANDLE  hT;
  154.     FARPROC lpfnAbort;
  155.     FARPROC lpfnPDlg;
  156.     HWND    hwndPDlg;
  157.     WORD    dy;
  158.     int     yExtSoFar;
  159.     WORD    fError = TRUE;
  160.     HWND    hwndEdit;
  161.  
  162.     hwndEdit = (HWND)GetWindowWord(hwnd,GWW_HWNDEDIT);
  163.  
  164.     /* Create the job title by loading the title string from STRINGTABLE */
  165.     cch = LoadString (hInst, IDS_PRINTJOB, sz, sizeof(sz));
  166.     szTitle = sz + cch;
  167.     cch += GetWindowText (hwnd, sz + cch, 32 - cch);
  168.     sz[31] = 0;
  169.  
  170.     /* Make instances of the Abort proc. and the Print dialog function */
  171.     lpfnAbort = MakeProcInstance (AbortProc, hInst);
  172.     if (!lpfnAbort)
  173.     goto getout;
  174.     lpfnPDlg = MakeProcInstance (PrintDlgProc, hInst);
  175.     if (!lpfnPDlg)
  176.     goto getout4;
  177.  
  178.     /* Initialize the printer */
  179.     hdc = GetPrinterDC();
  180.     if (!hdc)
  181.     goto getout5;
  182.  
  183.     /* Disable the main application window and create the Cancel dialog */
  184.     EnableWindow (hwndFrame, FALSE);
  185.     hwndPDlg = CreateDialog (hInst, IDD_PRINT, hwnd, lpfnPDlg);
  186.     if (!hwndPDlg)
  187.     goto getout3;
  188.     ShowWindow (hwndPDlg, SW_SHOW);
  189.     UpdateWindow (hwndPDlg);
  190.  
  191.     /* Allow the app. to inform GDI of the escape function to call */
  192.     if (Escape (hdc, SETABORTPROC, 0, (LPSTR)lpfnAbort, NULL) < 0)
  193.     goto getout1;
  194.  
  195.     /* Initialize the document */
  196.     if (Escape (hdc, STARTDOC, cch, (LPSTR)sz, NULL) < 0)
  197.     goto getout1;
  198.  
  199.     /* Get the height of one line and the height of a page */
  200.     dy = HIWORD (GetTextExtent (hdc, "CC", 2));
  201.     yExtPage = GetDeviceCaps (hdc, VERTRES);
  202.  
  203.     /* Get the lines in document and and a handle to the text buffer */
  204.     iLine     = 0;
  205.     yExtSoFar = 0;
  206.     nLinesEc  = (WORD)SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0L);
  207.     hT          = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  208.  
  209.     /* While more lines print out the text */
  210.     while (iLine < nLinesEc){
  211.     if (yExtSoFar + (int)dy > yExtPage){
  212.         /* Reached the end of a page. Tell the device driver to eject a
  213.          * page
  214.          */
  215.         if (Escape (hdc, NEWFRAME, 0, NULL, NULL) < 0 || fAbort)
  216.         goto getout2;
  217.         yExtSoFar = 0;
  218.     }
  219.  
  220.     /* Get the length and position of the line in the buffer
  221.      * and lock from that offset into the buffer */
  222.     ich = (WORD)SendMessage (hwndEdit, EM_LINEINDEX, iLine, 0L);
  223.     cch = (WORD)SendMessage (hwndEdit, EM_LINELENGTH, ich, 0L);
  224.     pch = LocalLock(hT) + ich;
  225.  
  226.     /* Print the line and unlock the text handle */
  227.     TextOut (hdc, 0, yExtSoFar, (LPSTR)pch, cch);
  228.     LocalUnlock (hT);
  229.  
  230.     /* Test and see if the Abort flag has been set. If yes, exit. */
  231.     if (fAbort)
  232.         goto getout2;
  233.  
  234.     /* Move down the page */
  235.     yExtSoFar += dy;
  236.     iLine++;
  237.     }
  238.  
  239.     /* Eject the last page. */
  240.     if (Escape (hdc, NEWFRAME, 0, NULL, NULL) < 0)
  241.     goto getout2;
  242.  
  243.     /* Complete the document. */
  244.     if (Escape (hdc, ENDDOC, 0, NULL, NULL) < 0){
  245. getout2:
  246.     /* Ran into a problem before NEWFRAME? Abort the document */
  247.     Escape( hdc, ABORTDOC, 0, NULL, NULL);
  248.     }
  249.     else
  250.     fError=FALSE;
  251.  
  252. getout3:
  253.     /* Close the cancel dialog and re-enable main app. window */
  254.     EnableWindow (hwndFrame, TRUE);
  255.     DestroyWindow (hwndPDlg);
  256.  
  257. getout1:
  258.     DeleteDC(hdc);
  259.  
  260. getout5:
  261.     /* Get rid of dialog procedure instances */
  262.     FreeProcInstance (lpfnPDlg);
  263.  
  264. getout4:
  265.     FreeProcInstance (lpfnAbort);
  266.  
  267. getout:
  268.  
  269.     /* Error? make sure the user knows... */
  270.     if (fError)
  271.     MPError (hwnd, MB_OK | MB_ICONEXCLAMATION, IDS_PRINTERROR, (LPSTR)szTitle);
  272.  
  273.     return;
  274. }
  275.  
  276. #if 0
  277.  
  278. /****************************************************************************
  279.  *                                        *
  280.  *  FUNCTION   : GetInitializationData()                    *
  281.  *                                        *
  282.  *  PURPOSE    : Gets DC initialization data from a printer driver        *
  283.  *         supporting ExtDeviceMode(). Called in response to the        *
  284.  *         File/Printer setup menu selection.                *
  285.  *                                        *
  286.  *         This function allows the user to change the printer        *
  287.  *         settings FOR MULTODMA ONLY.  This allows MultODMA to print *
  288.  *         in a variety of settings without messing up any other        *
  289.  *         applications. In a more sophisticated application, this    *
  290.  *         setup could even be saved on a document-by-document basis. *
  291.  *                                        *
  292.  ****************************************************************************/
  293. BOOL FAR PASCAL GetInitializationData( hwnd )
  294. HWND hwnd ;
  295. {
  296.     LPSTR     lpOld;
  297.     LPSTR     lpNew;
  298.     FARPROC   lpfn;
  299.     HANDLE    hT,hDrv;
  300.     char      sz[32];
  301.     WORD      cb;
  302.     int       flag;
  303.  
  304.     /* Pop up dialog for user and retain data in app buffer */
  305.     flag = DM_PROMPT | DM_COPY;
  306.  
  307.     /* Load the device driver and find the ExtDeviceMode() function */
  308.     wsprintf (sz, "%s.drv", (LPSTR)szDriver);
  309.     if ((hDrv = LoadLibrary (sz)) < 32)
  310.     return FALSE;
  311.     if (!(lpfn = GetProcAddress (hDrv, szExtDeviceMode)))
  312.     return FALSE;
  313.  
  314.     if (hInitData){
  315.     /* We have some old data... we want to modify the previously specified
  316.      * setup rather than starting with the default setup.
  317.      */
  318.     lpOld = (LPSTR)LocalLock(hInitData);
  319.     flag |= DM_MODIFY;
  320.     }
  321.     else
  322.     lpOld = NULL;
  323.  
  324.     /* Get the number of bytes needed for the init data */
  325.     cb = (*lpfn) (hwnd,
  326.           hDrv,
  327.           NULL,
  328.           (LPSTR)szDevice,
  329.           (LPSTR)szPort,
  330.           (LPDEVMODE)NULL,
  331.           (LPSTR)NULL,
  332.           0);
  333.  
  334.     /* Grab some memory for the new data and lock it. */
  335.     hT      = LocalAlloc (LHND,cb);
  336.     lpNew = (LPSTR)LocalLock (hT);
  337.  
  338.     /* Post the device mode dialog. 0 flag iff user hits OK button */
  339.     if ((*lpfn) (hwnd,
  340.          hDrv,
  341.          (LPDEVMODE)lpNew,
  342.          (LPSTR)szDevice,
  343.          (LPSTR)szPort,
  344.          (LPDEVMODE)lpOld,
  345.          (LPSTR)NULL,
  346.          flag)==IDOK)
  347.     flag = 0;
  348.  
  349.     /* Unlock the input structures */
  350.     LocalUnlock (hT);
  351.     if (hInitData)
  352.     LocalUnlock (hInitData);
  353.  
  354.     /* If the user hit OK and everything worked, free the original init.
  355.      * data and retain the new one.  Otherwise, toss the new buffer.
  356.      */
  357.     if (flag)
  358.     LocalFree (hT);
  359.     else{
  360.     if (hInitData)
  361.         LocalFree (hInitData);
  362.     hInitData = hT;
  363.     }
  364.  
  365.     FreeLibrary(hDrv);
  366.     return (!flag);
  367. }
  368.  
  369.  
  370. #endif
  371.  
  372.