home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap15 / poppadp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  4.8 KB  |  153 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.      psBuffer = (NPSTR) LocalAlloc (LPTR, nCharsPerLine) ;
  91.  
  92.      EnableWindow (hwnd, FALSE) ;
  93.  
  94.      bUserAbort = FALSE ;
  95.      lpfnPrintDlgProc = MakeProcInstance (PrintDlgProc, hInst) ;
  96.      hDlgPrint = CreateDialog (hInst, "PrintDlgBox", hwnd, lpfnPrintDlgProc) ;
  97.      SetDlgItemText (hDlgPrint, IDD_FNAME, szFileName) ;
  98.  
  99.      lpfnAbortProc = MakeProcInstance (AbortProc, hInst) ;
  100.      Escape (hdcPrn, SETABORTPROC, 0, (LPSTR) lpfnAbortProc, NULL) ;
  101.  
  102.      strcat (strcat (strcpy (szMsg, szAppName), " - "), szFileName) ;
  103.                                         
  104.      if (Escape (hdcPrn, STARTDOC, strlen (szMsg), szMsg, NULL) > 0)
  105.           {
  106.           for (nPage = 0 ; nPage < nTotalPages ; nPage++)
  107.                {
  108.                for (nLine = 0 ; nLine < nLinesPerPage &&
  109.                                 nLineNum < nTotalLines ; nLine++, nLineNum++)
  110.                     {
  111.                     *(short *) psBuffer = nCharsPerLine ;
  112.  
  113.                     TextOut (hdcPrn, 0, yChar * nLine, psBuffer,
  114.                          (short) SendMessage (hwndEdit, EM_GETLINE,
  115.                                         nLineNum, (LONG) (LPSTR) psBuffer)) ;
  116.                     }
  117.  
  118.                if (Escape (hdcPrn, NEWFRAME, 0, NULL, (LPSTR) &rect) < 0)
  119.                     {
  120.                     bError = TRUE ;
  121.                     break ;
  122.                     }
  123.  
  124.                if (bUserAbort)
  125.                     break ;
  126.                }
  127.           }
  128.      else
  129.           bError = TRUE ;
  130.  
  131.      if (!bError)
  132.           Escape (hdcPrn, ENDDOC, 0, NULL, NULL) ;
  133.  
  134.      if (!bUserAbort)
  135.           {
  136.           EnableWindow (hwnd, TRUE) ;
  137.           DestroyWindow (hDlgPrint) ;
  138.           }
  139.  
  140.      if (bError || bUserAbort)
  141.           {
  142.           strcat (strcpy (szMsg, "Could not print: "), szFileName) ;
  143.           MessageBox (hwnd, szMsg, szAppName, MB_OK | MB_ICONEXCLAMATION) ;
  144.           }
  145.  
  146.      LocalFree ((LOCALHANDLE) psBuffer) ;
  147.      FreeProcInstance (lpfnPrintDlgProc) ;
  148.      FreeProcInstance (lpfnAbortProc) ;
  149.      DeleteDC (hdcPrn) ;
  150.  
  151.      return bError || bUserAbort ;
  152.      }
  153.