home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / EleOfC++ vrs 0.1 / PictView / TPICTDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-14  |  3.9 KB  |  248 lines  |  [TEXT/KAHL]

  1. /*
  2. ** This is the TPICTDoc class definition.  This class is an adaptation of 
  3. ** the class of the same name in the book, Elements of C++ Macintosh 
  4. ** Programming, by Dan Weston.
  5. **
  6. ** Copyright © 1991 Mark Gross 
  7. ** (RR2, Box 84, Clayville, NY 13322);         
  8. ** this file may be published everywhere, but no charge
  9. ** may be made for its use.  Please contact me about any bugs found.
  10. ** I'm also looking for Macintosh development work, so drop me a line
  11. ** if you think I could help.  
  12. */
  13.  
  14. #include    "TPICTDoc.h"
  15. #include    "magics.h"
  16.  
  17. TDoc *TPICTDoc::Init(OSType theCreator, SFReply *reply)
  18. {
  19.     inherited::Init(theCreator, reply);
  20.     
  21.     fPict = NULL;
  22.     fHeader = NULL;
  23.     fPrintRecord = NULL;
  24.     return this;
  25. }/*end of function*/
  26.  
  27.  
  28. Boolean TPICTDoc::InitDoc(void)
  29. {
  30.     if(TScrollDoc::InitDoc())
  31.     {
  32.         fPrintRecord = (THPrint) NewHandle(sizeof(TPrint) );
  33.         if(fPrintRecord != NULL)
  34.         {
  35.             PrOpen();
  36.             PrintDefault(fPrintRecord);
  37.             PrClose();
  38.             return TRUE;
  39.         }
  40.     }
  41.     return FALSE;
  42. }/*end of function*/
  43.  
  44.  
  45.  
  46. void    TPICTDoc::Delete(void)
  47. {
  48.  
  49.     if(fHeader != NULL)
  50.     {
  51.         DisposHandle(fHeader);
  52.         fHeader = NULL;
  53.     }
  54.     if (fPict != NULL)
  55.     {
  56.         KillPicture((PicHandle) fPict);
  57.         fPict = NULL;
  58.     }
  59.     if(fPrintRecord != NULL)
  60.     {
  61.         DisposHandle((Handle) fPrintRecord);
  62.         fPrintRecord = NULL;
  63.     }
  64.     inherited::Delete();
  65. }/*end of function*/
  66.  
  67.     
  68. short    TPICTDoc::GetVertSize(void)
  69. {
  70.     Rect r;
  71.     
  72.     if(fPict)
  73.     {
  74.         r = (**((PicHandle)fPict)).picFrame;
  75.         return r.bottom - r.top;
  76.     }
  77.     else
  78.         return 0;
  79. }/*end of function*/
  80.  
  81.  
  82. short    TPICTDoc::GetHorizSize(void)
  83. {
  84.     Rect r;
  85.     
  86.     if(fPict)
  87.     {
  88.         r = (**((PicHandle)fPict)).picFrame;
  89.         return r.right - r.left;
  90.     }
  91.     else
  92.         return 0;
  93. }/*end of function*/
  94.  
  95.  
  96. short    TPICTDoc::GetVertLineScrollAmount(void)
  97. {
  98.     return 16;
  99. }
  100.  
  101. short     TPICTDoc::GetHorizLineScrollAmount(void)
  102. {
  103.     return 16;
  104. }
  105.  
  106.     
  107.     
  108. void    TPICTDoc::Draw(Rect *r)
  109. {
  110.     Rect r_temp;
  111.     
  112.     if (fPict != NULL)
  113.     {
  114.         EraseRect(r);
  115.         r_temp = (**(PicHandle)fPict).picFrame;
  116.         DrawPicture( (PicHandle) fPict, &r_temp );
  117.     }
  118. }/*end of function*/
  119.  
  120.     
  121. OSType    TPICTDoc::GetDocType(void)
  122. {
  123.     return 'PICT';
  124. }/*end of function*/
  125.  
  126.  
  127.  
  128. Boolean    TPICTDoc::ReadDocFile(short refNum)
  129. {
  130.     long    PictLength, headerLength=kPictHeaderSize;
  131.     OSErr    err;
  132.     Handle    thePic, theHeader;
  133.     
  134.     if (fDocWindow)
  135.     {
  136.         err = GetEOF(refNum, &PictLength);
  137.         PictLength -= kPictHeaderSize;
  138.         thePic = NewHandle(PictLength);
  139.         
  140.         if(thePic == NULL)
  141.         {
  142.             ErrorAlert(rDocErrorStrings, sNoMem);
  143.             return FALSE;
  144.         }
  145.         theHeader = NewHandle(headerLength);
  146.         
  147.         if (theHeader == NULL)
  148.         {
  149.             ErrorAlert(rDocErrorStrings, sNoMem);
  150.             return FALSE;
  151.         }
  152.         HLock(thePic);
  153.         HLock(theHeader);
  154.         
  155.         err = SetFPos(refNum, fsFromStart, 0);
  156.         err = FSRead(refNum, &headerLength, *theHeader);
  157.         err = FSRead(refNum, &PictLength, *thePic);
  158.         
  159.         HUnlock(thePic);
  160.         HUnlock(theHeader);
  161.         
  162.         if(err == noErr)
  163.         {
  164.             fPict = thePic;
  165.             fHeader = theHeader;
  166.             AdjustScrollBars();
  167.             return TRUE;
  168.         }
  169.         else
  170.         {
  171.             DisposHandle(thePic);
  172.             DisposHandle(theHeader);
  173.             return FALSE;
  174.         }
  175.     }/*end if fDocWindow*/
  176.     return FALSE;
  177. }/*end of function*/
  178.  
  179.  
  180.     
  181. Boolean    TPICTDoc::CanSaveAs(void)
  182. {
  183.     return FALSE;
  184. }
  185.  
  186.  
  187.  
  188. void    TPICTDoc::DoPageSetup(void)
  189. {
  190.     PrOpen();
  191.     (void)PrStlDialog(fPrintRecord);
  192.     PrClose();
  193. }/*end of function*/
  194.  
  195.  
  196. void    TPICTDoc::DoPrint(void)
  197. {
  198.     TPPrPort    printPort;
  199.     Rect        r;
  200.     TPrStatus    status;
  201.     THPrint        tmpPrintRecord;
  202.     
  203.     tmpPrintRecord = fPrintRecord;
  204.     PrOpen();
  205.     if (PrValidate(tmpPrintRecord) )
  206.         if (!PrStlDialog(tmpPrintRecord) )
  207.         {
  208.             PrClose();
  209.             return;
  210.         }
  211.         
  212.     if (!PrJobDialog(tmpPrintRecord) )
  213.     {
  214.         PrClose();
  215.         return;
  216.     }
  217.     
  218.     printPort = PrOpenDoc(tmpPrintRecord, NULL, NULL);
  219.     
  220.     PrOpenPage(printPort, NULL);
  221.     
  222.     SetRect(&r, 0, 0, 0, 0);
  223.     Draw(&r);
  224.     
  225.     PrClosePage(printPort);
  226.     
  227.     PrCloseDoc(printPort);
  228.     
  229.     if ((**tmpPrintRecord).prJob.bJDocLoop != 0)
  230.         PrPicFile(tmpPrintRecord, NULL, NULL, NULL, &status);
  231.         
  232.     PrClose();
  233. }/*end of function*/
  234.  
  235.         
  236.  
  237.  
  238.     
  239. Boolean    TPICTDoc::CanPrint(void)
  240. {
  241.     return TRUE;
  242. }
  243.  
  244. Boolean    TPICTDoc::CanPageSetup(void)
  245. {
  246.     return TRUE;
  247. }
  248.