home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / zap.arj / Z5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-26  |  6.2 KB  |  205 lines

  1. /***********************************************************************
  2. * Zap                                                                  *
  3. * ===                                                                  *
  4. *                                                                      *
  5. * Windows 3 Text Editor                                                *
  6. *                                                                      *
  7. * Printing module                                                      *
  8. *  BOOL PrintFile(int);                                                *
  9. *  int FAR PASCAL AbortProc(HDC, int);                                 *
  10. *  int FAR PASCAL AbortDlg(HWND, unsigned, WORD, LONG);                *
  11. *                                                                      *
  12. * This programme was developed using Microsoft C6.0 and the Microsoft  *
  13. * SDK, however any ANSI C could be used if suitable libraries and the  *
  14. * Windows header are available.                                        *
  15. ***********************************************************************/
  16.  
  17. #include <windows.h>
  18. #include <commdlg.h>
  19. #include <string.h>
  20. #include "zap.h"
  21. #include "zapdlg.h"
  22.  
  23.  
  24. /***********************************************************************
  25. * External variables                                                   *
  26. ***********************************************************************/
  27.  
  28. extern HANDLE hInst;
  29. extern HWND   hMainWnd;
  30.  
  31. extern EDIT_WND EditWnd[MAX_FILES];
  32. extern int NumFiles, CurrentFile;
  33.  
  34. extern FARPROC lpNewEditWndProc;
  35.  
  36. extern HANDLE hTransBuf;
  37. extern LPSTR  TransBuf;
  38.  
  39. extern int  CharWd, CharDp;
  40.  
  41. extern char WorkStr[WORK_STR_LEN];
  42.  
  43.  
  44. /***********************************************************************
  45. * Global variables                                                     *
  46. ***********************************************************************/
  47.  
  48. static PRINTDLG PrintDlgStruct;
  49. static HDC  hPrintDC;
  50. static RECT PrintRect;
  51. static int  CharDp;
  52.  
  53. static HWND hAbortDlgWnd;
  54. static BOOL AbortPrintJob;
  55.  
  56.  
  57. /***********************************************************************
  58. * Routine to print a file or files                                     *
  59. ***********************************************************************/
  60.  
  61. BOOL PrintFile(int Id)
  62. { int tabs[16], cur_line_pos, len, i, j;
  63.   FARPROC lpAbortProc, lpAbortDlg;
  64.   TEXTMETRIC tm;
  65.  
  66. /*** Open printer */
  67.  
  68.   PrintDlgStruct.lStructSize = sizeof(PRINTDLG);
  69.   PrintDlgStruct.hwndOwner = hMainWnd;
  70.   PrintDlgStruct.hDevMode = (HANDLE) NULL;
  71.   PrintDlgStruct.hDevNames = (HANDLE) NULL;
  72.   PrintDlgStruct.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
  73.   PrintDlgStruct.nCopies = 0;
  74.   PrintDlgStruct.hInstance = (HANDLE) NULL;
  75.  
  76.   if (!PrintDlg(&PrintDlgStruct)) return(FALSE);
  77.  
  78.   hPrintDC = PrintDlgStruct.hDC;
  79.  
  80. /*** Set abort print routine */
  81.  
  82.   lpAbortProc = MakeProcInstance(AbortProc, hInst);
  83.   Escape(hPrintDC, SETABORTPROC, NULL, (LPSTR)(LONG)lpAbortProc, (LPSTR) NULL);
  84.  
  85. /*** Start print run */
  86.  
  87.   if (Escape(hPrintDC, STARTDOC, 3, "Zap", (LPSTR) NULL) < 0)
  88.   { Comment("Unable to start print job");
  89.     FreeProcInstance(lpAbortProc);
  90.     DeleteDC(hPrintDC);
  91.     return(FALSE);
  92.   }
  93.  
  94. /*** Open abort print dialog box */
  95.  
  96.   AbortPrintJob = FALSE;
  97.   lpAbortDlg =  MakeProcInstance(AbortDlg, hInst);
  98.   if (!(hAbortDlgWnd = CreateDialog(hInst, "AbortBox", hMainWnd, lpAbortDlg)))
  99.   { Comment("Cannot open printer dialog box");
  100.     FreeProcInstance(lpAbortDlg);
  101.     FreeProcInstance(lpAbortProc);
  102.     DeleteDC(hPrintDC);
  103.     return(FALSE);
  104.   }
  105.  
  106.   ShowWindow (hAbortDlgWnd, SW_NORMAL);
  107.   UpdateWindow(hAbortDlgWnd);
  108.  
  109. /*** Disable main window to avoid reentrancy problems */
  110.  
  111.   EnableWindow(hMainWnd, FALSE);
  112.  
  113. /*** Set page layout */
  114.  
  115.   GetTextMetrics(hPrintDC, &tm);
  116.   CharDp = tm.tmHeight + tm.tmExternalLeading;
  117.   for (i = 0; i < 16; i++) tabs[i] = tm.tmAveCharWidth*8*i;
  118.  
  119.   PrintRect.right  = GetDeviceCaps(hPrintDC, HORZRES);
  120.   PrintRect.bottom = GetDeviceCaps(hPrintDC, VERTRES);
  121.  
  122.   PrintRect.left = min(PrintRect.right/10,  GetDeviceCaps(hPrintDC, LOGPIXELSX));
  123.   PrintRect.top  = min(PrintRect.bottom/10, GetDeviceCaps(hPrintDC, LOGPIXELSY));
  124.   PrintRect.bottom -= PrintRect.top + CharDp;
  125.  
  126. /*** Now print the file */
  127.  
  128.   len = GetWindowText(EditWnd[Id].hwnd, TransBuf, TRANS_BUF_SIZE);
  129.   cur_line_pos = PrintRect.top;
  130.  
  131.   i = 0;
  132.   while (i < len)
  133.   { for (j = 0; TransBuf[i+j] != '\r' && TransBuf[i+j] != '\0'; j++);
  134.  
  135.     TabbedTextOut(hPrintDC, PrintRect.left, cur_line_pos, TransBuf + i, j, 16, tabs, PrintRect.left);
  136.     cur_line_pos += CharDp;
  137.     if (cur_line_pos > PrintRect.bottom)
  138.     { Escape(hPrintDC, NEWFRAME, 0, 0L, 0L);
  139.       cur_line_pos = PrintRect.top;
  140.     }
  141.  
  142.     i += j + 2;
  143.   }
  144.  
  145.   if (cur_line_pos > PrintRect.top) Escape(hPrintDC, NEWFRAME, 0, 0L, 0L);
  146.  
  147. /*** Clean up and exit */
  148.  
  149.   if (!AbortPrintJob)
  150.   { SetDlgItemText(hAbortDlgWnd, IDD_ABORT_STATUS, "Formatting page");
  151.     Escape(hPrintDC, ENDDOC, 0, 0L, 0L);
  152.   }
  153.   EnableWindow(hMainWnd, TRUE);
  154.  
  155.   DestroyWindow(hAbortDlgWnd);
  156.   FreeProcInstance(lpAbortDlg);
  157.   FreeProcInstance(lpAbortProc);
  158.   DeleteDC(hPrintDC);
  159.  
  160. /*** Return signalling success */
  161.  
  162.   return(TRUE);
  163. }
  164.  
  165.  
  166. /***********************************************************************
  167. * Abort routine for printer                                            *
  168. ***********************************************************************/
  169.  
  170. int FAR PASCAL AbortProc(HDC hPr, int Code)
  171. { MSG msg;
  172.  
  173.   while (!AbortPrintJob && PeekMessage(&msg, NULL, NULL, NULL, TRUE))
  174.   if (!IsDialogMessage(hAbortDlgWnd, &msg))
  175.   { TranslateMessage(&msg);
  176.     DispatchMessage(&msg);
  177.   }
  178.  
  179.   return (!AbortPrintJob);
  180. }
  181.  
  182.  
  183. /***********************************************************************
  184. * Message handler for abort printer dialog box                         *
  185. ***********************************************************************/
  186.  
  187. int FAR PASCAL AbortDlg(HWND hDlg, unsigned msg, WORD wParam, LONG lParam)
  188. {
  189.   switch(msg)
  190.   { case WM_COMMAND:
  191.       if (wParam == IDCANCEL)
  192.       { AbortPrintJob = TRUE;
  193.         return(TRUE);
  194.       }
  195.       break;
  196.  
  197.     case WM_INITDIALOG:
  198.       SetFocus(GetDlgItem(hDlg, IDCANCEL));
  199.       return (TRUE);
  200.   }
  201.  
  202.   return (FALSE);
  203. }
  204.  
  205.