home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-05  |  5.6 KB  |  244 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        utils.c
  4. #
  5. #        This segment handles file parsing, and basic utility functions.
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            4/3/96        MWM     Initial coding                     
  13. #
  14. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  15. #
  16. #
  17. #        You may incorporate this sample code into your applications without
  18. #        restriction, though the sample code has been provided "AS IS" and the
  19. #        responsibility for its operation is 100% yours.  However, what you are
  20. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  21. #        after having made changes. If you're going to re-distribute the source,
  22. #        we require that you make it clear in the source that the code was
  23. #        descended from Apple Sample Code, but that you've made changes.
  24. #
  25. *************************************************************************************/
  26.  
  27. #include <Events.h>
  28. #include <ToolUtils.h>
  29. #include <Gestalt.h>
  30. #include <OSUtils.h>
  31. #include <StandardFile.h>
  32.  
  33. #include "App.h"
  34. #include "Proto.h"
  35.  
  36.  
  37. //----------------------------------------------------------------------
  38. //
  39. //    DoOpenNew - query user for new file of type 'PICT'. Open it and 
  40. //                display in a new window.
  41. //
  42. //----------------------------------------------------------------------
  43.  
  44. void DoOpenNew(void)
  45. {
  46.     OSErr                    err;
  47.     PicHandle                pict;
  48.     StandardFileReply        reply;
  49.     SFTypeList                 typeList;
  50.     WindowRef                window;
  51.     Rect                    bounds;
  52.     Rect                    screenRect;
  53.     short                    numTypes;
  54.     DocHnd                    doc;
  55.  
  56.     numTypes = 2;
  57.     typeList[0] = 'PICT';
  58.     typeList[1] = 'JPEG';
  59.     
  60.     StandardGetFile(nil, numTypes, (unsigned long *)&typeList, &reply);
  61.     if ( reply.sfGood )
  62.     {
  63.         if (reply.sfType == 'JPEG')
  64.         {
  65.             SetRect(&bounds, 3, GetMBarHeight() + kTitleBarHeight + 3, 200, 300);
  66.             window = CreateWindow(nil, nil, &bounds, reply.sfFile.name, false,
  67.                                   zoomDocProc, kDocKind, (WindowPtr)-1, true, nil );
  68.         
  69.             if (window != nil) 
  70.             {
  71.                 doc = (DocHnd)GetWRefCon(window);
  72.                 if (doc != nil)
  73.                 {
  74.                     HLock((Handle)doc);
  75.                     err = ReadJPEG(reply.sfFile, &(**doc).world);
  76.                     if ((**doc).world != nil && err == noErr) 
  77.                     {
  78.                         SetPort(window);
  79.                         InvalRect(&window->portRect);
  80.                         AdjustScrollbars(window,true);
  81.                     }    
  82.                     else
  83.                     {
  84.                         RemoveWindow(window);
  85.                         HandleError(err, false);
  86.                     }
  87.                     HUnlock((Handle)doc);
  88.                 }
  89.             }    
  90.         }                    
  91.         else
  92.         {    
  93.             pict = ReadFile(reply.sfFile);
  94.             if (pict != nil) 
  95.             {
  96.                 bounds = (**pict).picFrame;
  97.                 ZeroRect(&bounds);
  98.                 OffsetRect(&bounds, 3, GetMBarHeight() + kTitleBarHeight + 3);
  99.                 bounds.right += kScrollWidth;
  100.                 bounds.bottom += kScrollWidth;
  101.                 screenRect = qd.screenBits.bounds;
  102.                 if (bounds.right > screenRect.right) 
  103.                     bounds.right = screenRect.right;
  104.                 if (bounds.bottom > screenRect.bottom) 
  105.                     bounds.bottom = screenRect.bottom;
  106.     
  107.                 window = CreateWindow(nil, nil, &bounds, reply.sfFile.name, false,
  108.                                       zoomDocProc, kDocKind, (WindowPtr)-1, true, nil );
  109.     
  110.                 if (window != nil) 
  111.                 {
  112.                     doc = (DocHnd)GetWRefCon(window);
  113.                     if (doc != nil)
  114.                     {
  115.                         HLock((Handle)doc);
  116.                         (**doc).world = PictToWorld(pict, &err);
  117.                         if ((**doc).world == nil || err != noErr) 
  118.                         {
  119.                             KillPicture(pict);
  120.                             RemoveWindow(window);
  121.                             HandleError(err, false);
  122.                         }
  123.                         else
  124.                         {
  125.                             AdjustScrollbars(window,true);
  126.                         }
  127.                         HUnlock((Handle)doc);
  128.                     }
  129.                     
  130.                 }
  131.                 if (pict != nil)
  132.                     KillPicture(pict);
  133.             }
  134.         }                              
  135.     }
  136.     
  137. }
  138.  
  139.  
  140. //----------------------------------------------------------------------
  141. //
  142. //    ReadFile - open and read disk data, returning PICT.
  143. //                
  144. //
  145. //----------------------------------------------------------------------
  146.  
  147. PicHandle ReadFile(FSSpec spec)
  148. {
  149.     OSErr                    err;
  150.     PicHandle                pict;
  151.     long                    pictSize;
  152.     long                    fileSize;
  153.     short                    refNum;
  154.  
  155.     SetCursor(*GetCursor(watchCursor));            // set the cursor to a watch while busy
  156.     
  157.     err = FSpOpenDF( &spec, fsRdWrShPerm, &refNum );
  158.     if ( err == noErr ) {
  159.         err = GetEOF( refNum, &fileSize );
  160.         if ( err == noErr ) {
  161.             err = SetFPos(refNum, fsFromMark, 512);   // set position to our picture data
  162.             if ( err == noErr ) {
  163.                 pictSize = fileSize - 512;
  164.                 pict = (PicHandle)NewHandle(pictSize);
  165.                 err = MemError();
  166.  
  167.                 if ( err == noErr && pict != nil ) {
  168.                     HLock((Handle) pict);
  169.                     err = FSRead(refNum, &pictSize, *pict);        // read in the pict data
  170.                     HUnlock((Handle) pict);
  171.                 }
  172.             }
  173.         }
  174.     
  175.         FSClose( refNum );            // close the file
  176.         SetCursor( &qd.arrow );        // set cursor back to arrow
  177.     }
  178.     
  179.     if (err != noErr)
  180.         pict = nil;
  181.     
  182.     return pict;
  183.  
  184. }
  185.     
  186.  
  187. //----------------------------------------------------------------------
  188. //
  189. //    ZeroRect - zero the top-left points of a rect
  190. //                
  191. //
  192. //----------------------------------------------------------------------
  193.  
  194. void ZeroRect(Rect *r)
  195. {
  196.     Rect        zr;
  197.     
  198.     zr = *r;
  199.     
  200.     if (zr.left < 0)
  201.         OffsetRect(&zr,zr.left,0);
  202.     if (zr.top < 0)
  203.         OffsetRect(&zr,0,zr.top);
  204.     
  205.     OffsetRect(&zr,-zr.left,-zr.top);
  206.     
  207.     *r = zr;
  208.  
  209.  
  210.  
  211. //----------------------------------------------------------------------
  212. //
  213. //    pstrcpy - pascal string copy
  214. //                
  215. //
  216. //----------------------------------------------------------------------
  217.  
  218. void pstrcpy(StringPtr dst, StringPtr src)
  219. {
  220.     short    c;
  221.     
  222.     for (c = *src; c > -1; c--)
  223.         dst[c] = src[c];
  224. }
  225.  
  226.  
  227. //----------------------------------------------------------------------
  228. //
  229. //    pstrcat - pascal string concat
  230. //                
  231. //
  232. //----------------------------------------------------------------------
  233.  
  234. void pstrcat(StringPtr dst, StringPtr src)
  235. {
  236.     
  237.     short    c;
  238.  
  239.     for (c = 1; c < src[0] + 1; c++)
  240.         dst[dst[0] + c] = src[c];
  241.     dst[0] += src[0];
  242. }
  243.