home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckmpri.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  387 lines

  1. /* $Id: ckmpri.c,v 1.5 91/12/15 23:17:46 rick Exp $
  2.  * $Source: /uw/mackermit/RCS/ckmpri.c,v $
  3.  *------------------------------------------------------------------
  4.  * $Log:    ckmpri.c,v $
  5.  * Revision 1.5  91/12/15  23:17:46  rick
  6.  * ut9
  7.  * 
  8.  * Revision 1.4  91/10/13  13:44:33  rick
  9.  * UT(7)
  10.  * 
  11.  * Revision 1.3  91/10/03  12:43:06  rick
  12.  * UT(5)
  13.  * 
  14.  *------------------------------------------------------------------
  15.  * $Endlog$
  16.  */
  17.  
  18. /*
  19.  * file ckmpri.c
  20.  *
  21.  * Module of mackermit containing code for handling printing.  This code was
  22.  * originally put into ckmusr by John A. Oberschelp of Emory U.
  23.  *
  24.  */
  25.  
  26. /*
  27.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  28.   York.  Permission is granted to any individual or institution to use this
  29.   software as long as it is not sold for profit.  This copyright notice must be
  30.   retained.  This software may not be included in commercial products without
  31.   written permission of Columbia University.
  32. */
  33.  
  34. #include "ckcdeb.h"
  35. #include "ckcker.h"
  36.  
  37. #include "ckmdef.h"        /* General Mac defs */
  38. #include "ckmres.h"        /* Mac resource equates */
  39. #include "ckmasm.h"        /* new A8 and A9 traps */
  40. #include "ckmcon.h"        /* for fonts */
  41. #include "ckmptp.h"        /* ckm* Prototypes */
  42. #include "ckmwin.h"        /* text edit window structures */
  43.  
  44. int        to_printer = FALSE;
  45. int        to_screen = TRUE;
  46. int        printer_is_on_line_num;
  47. #ifdef COMMENT
  48. Handle    hPrintBuffer = 0L;
  49. long    lPrintBufferSize;
  50. long    lPrintBufferChars = 0L;
  51. long    lPrintBufferAt;
  52. #endif /* COMMENT */
  53.  
  54. Boolean printer_is_setup = false;
  55. THPrint    PrintStuff = NULL;
  56.  
  57. extern    MenuHandle menus[];    /* handle on our menus */
  58.  
  59. extern struct cmdw *prntw;        /* local "printer" window */
  60.  
  61. DialogPtr    printingDialog;
  62. DialogPtr    bufferingDialog;
  63. DialogPtr    overflowingDialog;
  64. DialogPtr    overflowedDialog;
  65. DialogPtr    pauseDialog;
  66.  
  67. #define MIN(a,b) ((a)<(b))?(a):(b)
  68. #define MAX(a,b) ((a)>(b))?(a):(b)
  69.  
  70. /****************************************************************************/
  71. /*  add_to_print  --  Add string of length len to "printer" text edit record  */
  72. /*              Made from conxo();                    */
  73. /****************************************************************************/
  74. void
  75. add_to_print (int len, char *s)
  76. {
  77. #ifdef COMMENT    
  78.     register char *t;
  79.  
  80.     /* change NLs to CRs for the Mac */
  81.     for (t = s; *t && (t - s < len); t++)
  82.         if (*t == LF)
  83.         *t = CR;
  84. #endif
  85.  
  86.     /* debug (F101, "add_to_print here: ", s, len); */
  87.     if ((prntw->teh == NULL) || (len <= 0))
  88.     return;
  89.  
  90.     TEDeactivate(prntw->teh);
  91.     if (trimcon(prntw, len)) {
  92.         /* put up a dialog saying that things are overflowing */
  93.     }
  94.     TEInsert (s, (long) len, prntw->teh);    /* insert the string */
  95.     TESetSelect(TE_MAX, TE_MAX, prntw->teh);
  96.     TEActivate(prntw->teh);
  97.     rcdwscroll (prntw);        /* possibly scroll it */
  98.     updatepstat();        /* possibly enable print menus */
  99. }                /* add_to_print */
  100.  
  101. /*
  102.  * Update the capturing printer output dialog
  103.  */
  104. void
  105. upd_ptr_dlg()
  106. {
  107.     if (to_printer && !bufferingDialog) {
  108.     bufferingDialog = GetNewDialog(BUFFERINGBOXID, NILPTR, (WindowPtr)-1);
  109.     DrawDialog(bufferingDialog);
  110.     } else if (!to_printer && bufferingDialog) {
  111.     DisposDialog(bufferingDialog);
  112.     bufferingDialog = NULL;
  113.     if (overflowingDialog) {
  114.         DisposDialog(overflowingDialog);
  115.         overflowingDialog = NULL;
  116.     }
  117.     }
  118. }
  119.  
  120. /*
  121.  * Print the internal text capture buffer
  122.  */
  123. void
  124. now_print()
  125. {
  126.     long length;
  127.     CharsHandle h;
  128.     /* CursHandle watchcurs; */
  129.  
  130. #ifdef COMMENT
  131.     short itemhit;
  132.     if (lPrintBufferChars >= lPrintBufferSize) {
  133.         overflowedDialog = GetNewDialog(OVERFLOWEDBOXID, NILPTR,
  134.                         (WindowPtr) - 1);
  135.         circleOK(overflowedDialog);
  136.  
  137.         ModalDialog ((ModalFilterProcPtr) NILPROC, &itemhit);
  138.         DisposDialog(overflowedDialog);
  139.         if (itemhit == 2)    /* if Cancel */
  140.             return;
  141.         if (itemhit == 3) {
  142.             DisposHandle(hPrintBuffer);
  143.             hPrintBuffer = 0L;
  144.             lPrintBufferChars = 0L;
  145.             updatepstat();
  146.             return;
  147.         }
  148.     }
  149. #endif /* COMMENT */
  150.  
  151. #ifdef COMMENT
  152.     if (lPrintBufferChars > lPrintBufferSize) {    /* if buffer wrapped */
  153.         length = lPrintBufferSize;
  154.         index = lPrintBufferAt;
  155.     } else {
  156.         length = lPrintBufferChars;
  157.         index = 0L;
  158.     }
  159.     printer(hPrintBuffer, length, index, lPrintBufferSize);
  160.  
  161.     DisposHandle(hPrintBuffer);
  162.     hPrintBuffer = 0L;
  163.     lPrintBufferChars = 0L;
  164. #endif /* COMMENT */
  165.  
  166.     if (!prntw) {
  167.         printerr("prntw is NULL!!!!", 0);
  168.         return;
  169.     }
  170.     
  171.     length = (*prntw->teh)->teLength;
  172.     h = TEGetText(prntw->teh);
  173.  
  174.     printer((Handle) h, length, 0L, 0x7FFFFFFFL);
  175.  
  176.     updatepstat();
  177. }
  178.  
  179. /*
  180.  * printer_init
  181.  * initialize the printer stuff
  182.  */
  183. int
  184. printer_init()
  185. {
  186.     Boolean brc;
  187.     
  188.     PrintStuff = (THPrint)NewHandle(sizeof(TPrint));
  189.     PrOpen();
  190.     if (PrError() != noErr) {
  191.     DisposHandle((Handle) PrintStuff);
  192.     PrintStuff = NULL;
  193.     printerr("Could not open printer", 0);
  194.     return 0;
  195.     }
  196.     brc = PrValidate(PrintStuff);    /* load reasonable defaults into PrintStuff */
  197.     return 1;
  198. }
  199.  
  200. /*
  201.  * Release any resources used by the printing system.
  202.  */
  203. void
  204. printer_close()
  205. {
  206.     if (!PrintStuff)
  207.     return;
  208.  
  209.     PrClose();        /* close the printer file and release resources */
  210.     DisposHandle((Handle) PrintStuff);
  211.     PrintStuff = NULL;
  212. }
  213.  
  214. /*
  215.  * dopagesetup
  216.  * Call the page setup dialog
  217.  */
  218. void
  219. dopagesetup()
  220. {
  221.     if (!PrintStuff)
  222.     if (!printer_init())
  223.         return;
  224.     
  225.     printer_is_setup = PrStlDialog(PrintStuff);
  226. }
  227.  
  228. /*
  229.  * printer
  230.  * h - handle to text to print
  231.  * length - number of chars in buffer
  232.  * index - starting index in buffer
  233.  * limit - buffer limit (to wrap index)
  234.  */
  235. void
  236. printer (Handle h, long length, long index, long limit)
  237. {
  238.     char    PrintBufferChar;
  239.     CursHandle    watchcurs;
  240.     int        typeOfDriver;
  241.     int        chrExtra;
  242.     int        leftMargin;
  243.     GrafPtr    oldPort;
  244.     int        temp;
  245.     TPPrPort    myPrPort;
  246.     TPrStatus    myStRec;
  247.  
  248.     if (!PrintStuff)        /* make sure we have enabled printer stuff */
  249.     if (!printer_init())
  250.         return;
  251.  
  252.     if (!printer_is_setup)    /* give the user a chance to set page settings */
  253.     dopagesetup();
  254.  
  255.     GetPort(&oldPort);
  256.  
  257.     temp = PrJobDialog(PrintStuff);    /* do print job dialog */
  258.     if (!temp) {
  259.     PrClose();
  260.     SetPort(oldPort);
  261.     return;
  262.     }
  263.     printingDialog = GetNewDialog(PRINTINGBOXID, NILPTR, (WindowPtr) - 1);
  264.     DrawDialog (printingDialog);
  265.     watchcurs = GetCursor(watchCursor);
  266.     SetCursor(*watchcurs);
  267.  
  268.     myPrPort = PrOpenDoc(PrintStuff, nil, nil);
  269.     typeOfDriver = ((*PrintStuff)->prStl.wDev) >> 8;
  270.     if (typeOfDriver == 3) {         /* PostScript (laser) */
  271.     TextFont(courier);
  272.     TextSize(10);
  273.     chrExtra = 0;
  274.     leftMargin = 36;
  275.     } else {
  276.     TextFont(VT100FONT);
  277.     TextSize(9);
  278.     chrExtra = 1;
  279.     leftMargin = 36;
  280.     }
  281.  
  282.     printer_is_on_line_num = 1;
  283.     PrOpenPage(myPrPort, nil);
  284.     MoveTo(leftMargin, 1 * 12);
  285.  
  286.     do {
  287.     if (PrError() != noErr) 
  288.         break;
  289.     PrintBufferChar = (*h)[index];
  290.     switch (PrintBufferChar) {
  291.       case 015:        /* CR */
  292.         if (++printer_is_on_line_num > 60) {
  293.         PrClosePage(myPrPort);
  294.         PrOpenPage(myPrPort, nil);
  295.         printer_is_on_line_num = 1;
  296.         }
  297.         MoveTo(leftMargin, printer_is_on_line_num * 12);
  298.         break;
  299.  
  300.       case 014:        /* FF */
  301.         if (printer_is_on_line_num != 1) {
  302.             PrClosePage(myPrPort);
  303.             PrOpenPage(myPrPort, nil);
  304.             printer_is_on_line_num = 1;
  305.         }
  306.         MoveTo(leftMargin, printer_is_on_line_num * 12);
  307.         break;
  308.  
  309.       default:
  310.         DrawChar(PrintBufferChar);
  311.         Move(chrExtra, 0);
  312.         break;
  313.     }
  314.  
  315.     if (++index == limit)    /* if time to wrap buffer */
  316.         index = 0L;
  317.  
  318.     } while (--length);
  319.     PrClosePage(myPrPort);
  320.  
  321.     PrCloseDoc(myPrPort);
  322.     if ((PrError() == noErr) &&
  323.     ((**PrintStuff).prJob.bJDocLoop == bSpoolLoop))
  324.     PrPicFile(PrintStuff, nil, nil, nil, &myStRec);
  325.     if ((PrError() != noErr) && (PrError() != 128)) {
  326.     printerr ("Printer error: ", PrError());
  327.     }
  328.  
  329.     DisposDialog(printingDialog);
  330.  
  331.     SetPort(oldPort);
  332.     InitCursor();
  333. }
  334.  
  335.  
  336. void
  337. pr_stat()
  338. {
  339.     DialogPtr    printDialog;
  340.     Str255    arg1, arg2, arg3;
  341.     long    length;
  342.     short    itemhit;
  343.  
  344.  
  345.     printDialog = GetNewDialog(PRINTBOXID, NILPTR, (WindowPtr) - 1);
  346.     circleOK(printDialog);
  347.  
  348. #ifdef COMMENT
  349.     NumToString(MAX(lPrintBufferChars - lPrintBufferSize, 0L), arg1);
  350.     NumToString(MIN(lPrintBufferSize, lPrintBufferChars), arg2);
  351.     NumToString(lPrintBufferSize, arg3);
  352.     ParamText (arg1, arg2, arg3, "");
  353. #endif
  354.     length = (*prntw->teh)->teLength;
  355.  
  356.     /* NumToString(MAX(lPrintBufferChars - lPrintBufferSize, 0L), arg1); */
  357.     arg1[0] = 1;
  358.     arg1[1] = '?';                /* chars lost */
  359.     NumToString(length, arg2);            /* chars buffered */
  360.     NumToString(TE_TOOBIG, arg3);        /* buffer size */
  361.     ParamText (arg1, arg2, arg3, "");
  362.  
  363.     do {
  364.     ModalDialog ((ModalFilterProcPtr) NILPROC, &itemhit);
  365.  
  366.     switch (itemhit) {
  367.       case 1:
  368.       case 2:
  369.       case 3:
  370.       break;
  371.  
  372.     }
  373.     } while (itemhit > 3);
  374.  
  375.     DisposDialog(printDialog);
  376. }
  377.  
  378.  
  379. /*
  380.  * Junk so Emacs will set local variables to be compatible with Mac/MPW.
  381.  * Should be at end of file.
  382.  * 
  383.  * Local Variables:
  384.  * tab-width: 8
  385.  * End:
  386.  */
  387.