home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / T5 < prev    next >
Encoding:
Text File  |  1991-10-23  |  5.0 KB  |  165 lines

  1. /*----------------------------------------------
  2.    POPPADP.C -- Popup Editor Printing Functions
  3.   ----------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <string.h>
  7. #include "filedlg.h"                    // for IDD_FNAME definition
  8.  
  9. extern char szAppName [] ;              // in POPPAD.C
  10.  
  11. BOOL bUserAbort ;
  12. HWND hDlgPrint ;
  13.  
  14. BOOL FAR PASCAL PrintDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  15.      {
  16.      switch (message)
  17.           {
  18.           case WM_INITDIALOG:
  19.                EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE,
  20.                                                             MF_GRAYED) ;
  21.                return TRUE ;
  22.  
  23.           case WM_COMMAND:
  24.                bUserAbort = TRUE ;
  25.                EnableWindow (GetParent (hDlg), TRUE) ;
  26.                DestroyWindow (hDlg) ;
  27.                hDlgPrint = 0 ;
  28.                return TRUE ;
  29.           }
  30.      return FALSE ;
  31.      }          
  32.  
  33. BOOL FAR PASCAL AbortProc (HDC hPrinterDC, short nCode)
  34.      {
  35.      MSG   msg ;
  36.  
  37.      while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  38.           {
  39.           if (!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
  40.                {
  41.                TranslateMessage (&msg) ;
  42.                DispatchMessage (&msg) ;
  43.                }
  44.           }
  45.      return !bUserAbort ;
  46.      }
  47.  
  48. HDC GetPrinterDC (void)
  49.      {
  50.      static char szPrinter [80] ;
  51.      char        *szDevice, *szDriver, *szOutput ;
  52.  
  53.      GetProfileString ("windows", "device", ",,,", szPrinter, 80) ;
  54.  
  55.      if ((szDevice = strtok (szPrinter, "," )) &&
  56.          (szDriver = strtok (NULL,      ", ")) && 
  57.          (szOutput = strtok (NULL,      ", ")))
  58.           
  59.                return CreateDC (szDriver, szDevice, szOutput, NULL) ;
  60.  
  61.      return 0 ;
  62.      }
  63.  
  64. BOOL PrintFile (HANDLE hInst, HWND hwnd, HWND hwndEdit, char *szFileName)
  65.      {
  66.      BOOL       bError = FALSE ;
  67.      char       szMsg [40] ;
  68.      FARPROC    lpfnAbortProc, lpfnPrintDlgProc ;
  69.      HDC        hdcPrn ;
  70.      NPSTR      psBuffer ;
  71.      RECT       rect ;
  72.      short      yChar, nCharsPerLine, nLinesPerPage,
  73.                 nTotalLines, nTotalPages, nPage, nLine, nLineNum = 0 ;
  74.      TEXTMETRIC tm ;
  75.  
  76.      if (0 == (nTotalLines = (short) SendMessage (hwndEdit,
  77.                                              EM_GETLINECOUNT, 0, 0L)))
  78.           return FALSE ;
  79.  
  80.      if (NULL == (hdcPrn = GetPrinterDC ()))
  81.           return TRUE ;
  82.  
  83.      GetTextMetrics (hdcPrn, &tm) ;
  84.      yChar = tm.tmHeight + tm.tmExternalLeading ;
  85.  
  86.      nCharsPerLine = GetDeviceCaps (hdcPrn, HORZRES) / tm.tmAveCharWidth ;
  87.      nLinesPerPage = GetDeviceCaps (hdcPrn, VERTRES) / yChar ;
  88.      nTotalPages   = (nTotalLines + nLinesPerPage - 1) / nLinesPerPage ;
  89.  
  90. #ifdef __HIGHC__
  91.      psBuffer = (NPSTR) malloc (nCharsPerLine) ;
  92. #else
  93.      psBuffer = (NPSTR) LocalAlloc (LPTR, nCharsPerLine) ;
  94. #endif
  95.  
  96.      EnableWindow (hwnd, FALSE) ;
  97.  
  98.      bUserAbort = FALSE ;
  99.      lpfnPrintDlgProc = MakeProcInstance (PrintDlgProc, hInst) ;
  100.      hDlgPrint = CreateDialog (hInst, "PrintDlgBox", hwnd, lpfnPrintDlgProc) ;
  101.      SetDlgItemText (hDlgPrint, IDD_FNAME, szFileName) ;
  102.  
  103.      lpfnAbortProc = MakeProcInstance (AbortProc, hInst) ;
  104.      Escape (hdcPrn, SETABORTPROC, 0, (LPSTR) lpfnAbortProc, NULL) ;
  105.  
  106.      strcat (strcat (strcpy (szMsg, szAppName), " - "), szFileName) ;
  107.                                         
  108.      if (Escape (hdcPrn, STARTDOC, strlen (szMsg), szMsg, NULL) > 0)
  109.           {
  110.           for (nPage = 0 ; nPage < nTotalPages ; nPage++)
  111.                {
  112.                for (nLine = 0 ; nLine < nLinesPerPage &&
  113.                                 nLineNum < nTotalLines ; nLine++, nLineNum++)
  114.                     {
  115.                     *(short *) psBuffer = nCharsPerLine ;
  116.  
  117.                     TextOut (hdcPrn, 0, yChar * nLine, psBuffer,
  118.                          (short) SendMessage (hwndEdit, EM_GETLINE,
  119. #ifdef __HIGHC__
  120.                                         nLineNum, (LONG) psBuffer)) ;
  121. #else
  122.                                         nLineNum, (LONG) (LPSTR) psBuffer)) ;
  123. #endif
  124.                     }
  125.  
  126.                if (Escape (hdcPrn, NEWFRAME, 0, NULL, (LPSTR) &rect) < 0)
  127.                     {
  128.                     bError = TRUE ;
  129.                     break ;
  130.                     }
  131.  
  132.                if (bUserAbort)
  133.                     break ;
  134.                }
  135.           }
  136.      else
  137.           bError = TRUE ;
  138.  
  139.      if (!bError)
  140.           Escape (hdcPrn, ENDDOC, 0, NULL, NULL) ;
  141.  
  142.      if (!bUserAbort)
  143.           {
  144.           EnableWindow (hwnd, TRUE) ;
  145.           DestroyWindow (hDlgPrint) ;
  146.           }
  147.  
  148.      if (bError || bUserAbort)
  149.           {
  150.           strcat (strcpy (szMsg, "Could not print: "), szFileName) ;
  151.           MessageBox (hwnd, szMsg, szAppName, MB_OK | MB_ICONEXCLAMATION) ;
  152.           }
  153.  
  154. #ifdef __HIGHC__
  155.      free (psBuffer) ;
  156. #else
  157.      LocalFree ((LOCALHANDLE) psBuffer) ;
  158. #endif
  159.      FreeProcInstance (lpfnPrintDlgProc) ;
  160.      FreeProcInstance (lpfnAbortProc) ;
  161.      DeleteDC (hdcPrn) ;
  162.  
  163.      return bError || bUserAbort ;
  164.      }
  165.