home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Telnet 2.7b5 / source / printing / printing.c next >
Encoding:
Text File  |  1995-04-13  |  12.3 KB  |  299 lines  |  [TEXT/CWIE]

  1. /*
  2. *    printing.c
  3. *
  4. *    This now contains the PrintPages() code from Roland Mansson, from Lund 
  5. *    University Computing Center.  This allows for printer redirection, and also
  6. *    printing page numbers (and other goodies).  Thanks for all of your work!
  7. *
  8. *
  9. *
  10. *****************************************************************
  11. *    NCSA Telnet for the Macintosh                                *
  12. *                                                                *
  13. *    National Center for Supercomputing Applications                *
  14. *    Software Development Group                                    *
  15. *    152 Computing Applications Building                            *
  16. *    605 E. Springfield Ave.                                        *
  17. *    Champaign, IL  61820                                        *
  18. *                                                                *
  19. *    Copyright (c) 1986-1992,                                    *
  20. *    Board of Trustees of the University of Illinois                *
  21. *****************************************************************
  22. *    Code to handle printing.
  23. */
  24.  
  25. #ifdef MPW
  26. #pragma segment Printing
  27. #endif
  28.  
  29.  
  30. #include "wind.h"
  31. #include "rsinterf.proto.h"    /* For RSGetSelText */
  32. #include "netevent.proto.h"
  33. #include "vgtek.proto.h"
  34. #include "rgmp.proto.h"
  35. #include "event.proto.h"
  36. #include "tekrgmac.proto.h"
  37.  
  38.  
  39.  
  40. #include "vsdata.h"
  41. #include "vsinterf.proto.h"
  42.  
  43. extern Cursor *theCursors[];
  44. extern WindRec
  45.     *screens;            /* The screen array from Maclook */
  46. extern short scrn;
  47.  
  48. PROTO_UPP(printsleep, PrIdle);
  49. extern    void printsleep(void);
  50.  
  51. THPrint    gPrRecHandle = NULL;
  52.  
  53. #define PGRECT    (*PrRecHandle)->prInfo.rPage        /* Macro for making the pagerect more accessible */
  54. #define VMARGINS 4
  55. #define PAPRECT (*PrRecHandle)->rPaper /* Macro for making the paperrect more accessible */
  56. #define bDevCItoh 1 /* from Printing.h */
  57. #define ascLF 10
  58. #define ascFF 12
  59. #define ascCR 13
  60.  
  61. #include "printing.proto.h"
  62.  
  63. void    PrintingUnload(void) {}
  64.  
  65. /*    userabort - check to see if the user wants to abort */
  66. short userabort(void)
  67. {
  68.     EventRecord theEvent;
  69.  
  70.     while (GetNextEvent(24,&theEvent)) {
  71.         if ((theEvent.modifiers & cmdKey) &&
  72.             (theEvent.message & 0xff) =='.') 
  73.             return(-1);
  74.         }
  75.     return(0);
  76. }
  77.  
  78. /*    printsleep()    - Provide the network keep alive while we are in print
  79.  *                      mode.  Also set the abort flag if necessary. */
  80. SI current font */
  81.         TextFont((*(screens[theScreen].wind)).txFont);        /* NCSA: SB */
  82.         TextFace((*(screens[theScreen].wind)).txFace);        /* NCSA: SB */
  83.         v = (*(screens[theScreen].wind)).txSize;            /* NCSA: SB */
  84.         }                                                    /* NCSA: SB */
  85.     
  86.     do {
  87.         TextSize(v);
  88.         FontMetrics( &info );
  89.         pFheight = FixRound( info.ascent + info.descent + info.leading);
  90.         pFwidth  = FixRound( info.widMax);
  91.         //pFwidth  = CharWidth('W');            /* Re-assign to largest char */
  92.         v--;
  93.     } while ( TextWidth(stupidarray,0,columns+1) > (PGRECT.right - PGRECT.left - indent));
  94.     sprintf (tmp,"Fheight:%d, Fwidth:%d, TextSize:%d",pFheight,pFwidth,v+1); putln (tmp);
  95.  
  96.     if (charh!=NULL) {
  97.         HLock(charh);
  98.         start=charp=*charh;
  99.         charlen= GetHandleSize(charh);
  100.     } else {
  101.         if ((sts=GetFPos(refNum, &charlen)) != noErr)
  102.             {
  103.             sprintf(tmp,"GetFPos: ERROR %d",(short)sts);
  104.             putln(tmp);
  105.             }
  106.         charlen-=3;                    /* skip last 3 chars, as they are part of ESC seq */
  107.         if ((sts=SetFPos(refNum,(short) fsFromStart,0L)) != noErr)    /* rewind to beginning of file */
  108.             { sprintf(tmp,"SetFPos: ERROR %d",sts); putln(tmp); }
  109.         start = (char *)buf;                /* always (the array doesn't move) */
  110.         dummyCount=1;
  111.         if ((sts=FSRead(refNum,&dummyCount,&nextchar)) != noErr)        /* get first char */
  112.             { sprintf(tmp,"FSRead: ERROR %d",sts); putln(tmp); }
  113.     }
  114.     
  115.     h=PGRECT.right - PGRECT.left - indent;    /* Get the width */
  116.     v=PGRECT.bottom- PGRECT.top;            /* Get the height */
  117.  
  118.     maxlines = v/pFheight-1;
  119.     pgcount = 0;
  120.     while (count<charlen) {
  121.         pgcount++;
  122.         lines = 1;
  123.         PrOpenPage(prPort, 0L);        /* Open the Printer Page */
  124.         if ((sts=PrError()) != noErr) { sprintf(tmp,"PrOpenPage: ERROR %d",sts); putln(tmp); }
  125.         (*PrRecHandle)->prJob.pIdleProc= printsleepUPP;            /* our netsleep */
  126.         sprintf (tmp,"printing page:%d",pgcount); putln(tmp);
  127.  
  128.         if (useTitle) {
  129.             Str255 SessionNameString;
  130.             GetIndString(SessionNameString,MISC_STRINGS,SESSION_NAME_STRING);
  131.             MoveTo( indent, lines*pFheight);
  132.             DrawString(SessionNameString);            /* BYU LSC */
  133.     
  134.             DrawString( Title);            /* BYU LSC */
  135.             sprintf(pagetemp, "Page %d", pgcount);
  136.             pgtmplen=strlen(pagetemp);
  137.             MoveTo( PGRECT.right-(pgtmplen * pFwidth)-1, lines++*pFheight);
  138.             DrawText( pagetemp, 0, pgtmplen);
  139.             lines++;                    /* one blank line after title line */
  140.         }
  141.  
  142.         if (charh!=NULL) {                                    /* print from handle */
  143.             while (lines <= maxlines && count<charlen) {
  144.                 scount=0;
  145.                 while ((count<charlen) && (*charp++!='\r')) { count++; scount++; }
  146.                 MoveTo(indent,lines++*pFheight);
  147.                 if (scount>0)
  148.                     DrawText(start, 0, scount);
  149.                 count++;
  150.                 start=charp;
  151.             }
  152.         } else {                                            /* print from file */
  153.             while (lines <= maxlines && count<charlen) {
  154.                 rdy = 0;
  155.                 scount = 0;
  156.                 while ((count<charlen) && (!rdy)) {
  157.                     switch (nextchar) {
  158.                         case ascCR:
  159.                             rdy=1;
  160.                             break;
  161.                         case ascLF:
  162.                             rdy=1;
  163.                             break;
  164.                         case ascFF:
  165.                             rdy=1;
  166.                             break;
  167.                         default:
  168.                             buf[scount++]=nextchar;
  169.                             count++;
  170.                             dummyCount=1;
  171.                             if ((sts=FSRead (refNum,&dummyCount,&nextchar)) != noErr)
  172.                                 { sprintf(tmp,"FSRead: ERROR %d",sts); putln(tmp); }
  173.                             break;
  174.                     }
  175.                 }
  176.                 MoveTo(indent,lines*pFheight);
  177.                 if (scount>0)
  178.                     DrawText(start, 0, scount);
  179.                 if (nextchar==ascLF)
  180.                     lines++;                        /* LF -> new line */
  181.                 if (nextchar==ascFF)
  182.                     lines = maxlines+1;                /* FF -> new page */
  183.                 dummyCount=1;
  184.                 if ((sts=FSRead (refNum,&dummyCount,&nextchar)) != noErr)
  185.                     { sprintf(tmp,"FSRead: ERROR %d",sts); putln(tmp); }
  186.                 count++;
  187.             }
  188.         }
  189.         PrClosePage(prPort);        /* Close the Printer page */
  190.         if ((sts=PrError()) != noErr) { sprintf(tmp,"PrClosePage: ERROR %d",sts); putln(tmp); }
  191.     }
  192.     if (charh!=NULL)
  193.         HUnlock(charh);
  194. }
  195.  
  196.  
  197.  
  198. /*    printText -    Print the text selected on the screen 
  199.  *        vs - which vs to print from */
  200. void printText
  201.   (
  202.     short vs,                /* Which screen to print */
  203.     Str255 Title,        /* The title string */
  204.       short scrn
  205.   )
  206. {
  207.     char        **charh;        /* The character handle */
  208.     TPrStatus    prStatus;        /* Status record */
  209.     TPPrPort    prPort;            /* the Printer port */
  210.     THPrint        PrRecHandle;    /* our print record handle */
  211.  
  212.  
  213.     charh = RSGetTextSel(vs,0);                /* Get the characters to print */
  214.  
  215.     if ( charh==0L)
  216.         return;                                /* don't print anything.... */
  217.  
  218.     PrRecHandle = PrintSetupRecord();
  219.  
  220.     SetCursor(theCursors[normcurs]);
  221.     
  222.     if (PrJobDialog(PrRecHandle)) {            /* Cancel the print if FALSE */
  223.         prPort=PrOpenDoc(PrRecHandle,0L,0L);
  224.             if (PrError() == 0)    {
  225.                 printPages( prPort, PrRecHandle, Title, VSmaxwidth(vs), charh, (short) -1, TRUE,scrn);
  226.                 }
  227.             PrCloseDoc(prPort);
  228.  
  229.         if (((*PrRecHandle)->prJob.bJDocLoop == bSpoolLoop) && (PrError()==0))
  230.             PrPicFile(PrRecHandle,0L,0L,0L,&prStatus); /* Spool the print */
  231.         }
  232.     HPurge(charh);                            /* Kill the printed chars */
  233.     updateCursor(1);
  234.     DisposeHandle((Handle)PrRecHandle);
  235. }
  236.  
  237. void PrintPageSetup(void)
  238. {
  239.     THPrint        PrRecHandle;    /* our print record handle */
  240.     
  241.     PrOpen();
  242.  
  243.     PrRecHandle = (THPrint)GetResource('JOHN',169);                    /* AISD: JSA */
  244.     if ( PrRecHandle != NULL ) {                            /* AISD: JSA */
  245.         PrStlDialog(PrRecHandle);                    /* AISD: JSA */
  246.         ChangedResource((Handle)PrRecHandle);        /* AISD: JSA */
  247.         UpdateResFile(TelInfo->SettingsFile);        /* AISD: JSA */
  248.         }                                            /* AISD: JSA */
  249.     else {                                            /* AISD: JSA */
  250.         PrRecHandle=(THPrint)myNewHandle((long)sizeof(TPrint));    /* AISD: JSA */
  251.         PrintDefault(PrRecHandle);                    /* AISD: JSA */
  252.         PrStlDialog(PrRecHandle);                    /* AISD: JSA */
  253.         UseResFile(TelInfo->SettingsFile);            /* AISD: JSA */
  254.         AddResource((Handle)PrRecHandle,'JOHN',169,"\p");    /* AISD: JSA */
  255.         UpdateResFile(TelInfo->SettingsFile);        /* AISD: JSA */
  256.         }                                            /* AISD: JSA */
  257.  
  258.     ReleaseResource((Handle)PrRecHandle);        /* AISD: JSA */
  259.     PrClose();
  260. }
  261.  
  262. void PrintSelection(void)
  263. {
  264.         short    i;
  265.  
  266.         PrOpen();
  267.             
  268.         i=RGgetdnum(FrontWindow());
  269.         if (i>-1)         
  270.             printGraph( i);                        /* Print Graphics */
  271.         else 
  272.             if ( (i=RSfindvwind(FrontWindow())) >-1 ) {
  273.                 Str255 Title;
  274.  
  275.                 GetWTitle( FrontWindow(), Title);
  276.                 printText(i, Title,scrn);    /* Print Text selection */
  277.                 }
  278.         PrClose();
  279. }
  280.  
  281. THPrint PrintSetupRecord(void)
  282. {
  283.     THPrint    PrRecHandle;
  284.     
  285.     PrRecHandle = (THPrint)GetResource('JOHN',169);                    /* AISD: JSA */
  286.  
  287.     if ( PrRecHandle != NULL ) {
  288.         PrValidate(PrRecHandle);
  289.         DetachResource((Handle)PrRecHandle);
  290.         }
  291.     else {
  292.         PrRecHandle=(THPrint)myNewHandle((long)sizeof(TPrint));
  293.         PrintDefault(PrRecHandle);
  294.         }
  295.         
  296.     gPrRecHandle = PrRecHandle;
  297.     return (PrRecHandle);
  298. }
  299.