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