home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / WASTE Object Handlers 1.2.5 / Handler Source / WE_hfs_Handler.c next >
Encoding:
Text File  |  1997-07-21  |  7.9 KB  |  309 lines  |  [TEXT/CWIE]

  1. // File Object Handler for the WASTE Text Engine
  2. // by Michael F. Kamprath, kamprath@kagi.com
  3. // maintenance by John C. Daub, hsoi@eden.com
  4. //
  5. // v1.0, 12 March 1995
  6. //
  7. // v1.1, 1 April.  Fixed a a bug with draging hard drives to a WASTE instance
  8. //                 and a bix with insert file objects via InsertFileRefFromFSSpec()
  9. //
  10. // v1.2, 28 March 1996.    Minor touch-ups and updates. Added more precompiler directives.
  11. //                        Added compatability with WASTE 1.2a5.
  12. //
  13. // v1.2.2, 16 July 1996. Added compatability with WASTE 1.2 and CW9
  14. //                         Restructured installation routines
  15.  
  16. #ifndef __ICONS__
  17. #include <Icons.h>
  18. #endif
  19. #ifndef __DRAG__
  20. #include <Drag.h>
  21. #endif
  22. #ifndef __LOWMEM__
  23. #include <LowMem.h>
  24. #endif
  25. #ifndef _WASTE_
  26. #include "WASTE.h"
  27. #endif
  28.  
  29. #ifndef _WASTEOBJECTS_
  30.     #include "WASTE_Objects.h"
  31. #endif
  32.  
  33. #include "WE_hfs_Handler.h"
  34. #include "GetFileIcon.h"
  35. #include "SendFinderOpen.h"
  36.  
  37. #ifndef true
  38. #define true 1
  39. #endif
  40.  
  41. #ifndef false
  42. #define false 0
  43. #endif
  44.  
  45. // Local Functions
  46. static void        SizeHFSObject( Point *objectSize, HFSFlavor **theHFS );
  47.  
  48.  
  49. //
  50. // InstallHFSObject()
  51. //        Installs the HFS Object handler into WASTE.
  52. //
  53.  
  54. OSErr    InstallHFSObject( WEReference theWE )
  55. {
  56.     OSErr    iErr;
  57.  
  58. #ifdef __cplusplus
  59.     static WENewObjectUPP        newHFSUPP = NewWENewObjectProc(HandleNewHFS);
  60.     static WEDisposeObjectUPP     disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
  61.     static WEDrawObjectUPP        drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
  62.     static WEClickObjectUPP        clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
  63. #else
  64.     static WENewObjectUPP        newHFSUPP = NULL;
  65.     static WEDisposeObjectUPP     disposeHFSUPP = NULL;
  66.     static WEDrawObjectUPP        drawHFSUPP = NULL;
  67.     static WEClickObjectUPP        clickHFSUPP = NULL;
  68.     
  69.     if ( newHFSUPP == NULL )
  70.         newHFSUPP = NewWENewObjectProc(HandleNewHFS);
  71.     if ( disposeHFSUPP == NULL )
  72.         disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
  73.     if ( drawHFSUPP == NULL )    
  74.         drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
  75.     if ( clickHFSUPP == NULL )
  76.         clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
  77. #endif
  78.  
  79.     if ( newHFSUPP != NULL )
  80.         iErr = WEInstallObjectHandler(flavorTypeHFS, weNewHandler, (UniversalProcPtr)newHFSUPP, theWE);
  81.     else
  82.         iErr = weUnknownObjectTypeErr;
  83.     if (iErr) return(iErr);
  84.     
  85.     if ( disposeHFSUPP != NULL )
  86.         iErr = WEInstallObjectHandler(flavorTypeHFS, weDisposeHandler, (UniversalProcPtr)disposeHFSUPP, theWE);
  87.     else
  88.         iErr = weUnknownObjectTypeErr;
  89.     if (iErr) return(iErr);
  90.     
  91.     if ( drawHFSUPP != NULL)
  92.         iErr = WEInstallObjectHandler(flavorTypeHFS, weDrawHandler, (UniversalProcPtr)drawHFSUPP, theWE);
  93.     else
  94.         iErr = weUnknownObjectTypeErr;
  95.     if (iErr) return(iErr);
  96.     
  97.     if ( clickHFSUPP != NULL )
  98.         iErr = WEInstallObjectHandler(flavorTypeHFS, weClickHandler, (UniversalProcPtr)clickHFSUPP, theWE);
  99.     else
  100.         iErr = weUnknownObjectTypeErr;
  101.     if (iErr) return(iErr);
  102.     
  103.     return(noErr);
  104. }
  105.  
  106. //
  107. // New Object Handler for HFS Objects
  108. //
  109. pascal OSErr    HandleNewHFS(Point *defaultObjectSize,WEObjectReference objectRef)
  110. {
  111.     HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  112.  
  113.     // First size the object as WASTE requires
  114.     SizeHFSObject( defaultObjectSize, theHFS);
  115.     
  116.     // Set the object refcon to 0.  This is for safety
  117.     // as later we use the refcon to store the icon handle
  118.     // for this object.
  119.     WESetObjectRefCon( objectRef, 0 );
  120.     
  121.     return(noErr);
  122. }
  123.  
  124. //
  125. // Dispose Object handler for HFS Objects
  126. //
  127. pascal OSErr    HandleDisposeHFS(WEObjectReference objectRef )
  128. {
  129.     Handle    theHFS = WEGetObjectDataHandle(objectRef);
  130.     Handle    iconCache = (Handle)WEGetObjectRefCon( objectRef );
  131.  
  132.     // If the object has an icon handle loaded, dispose of it.
  133.     if (iconCache != 0)
  134.     {
  135.         DisposeIconSuite( iconCache, true );
  136.     }
  137.     
  138.     // Dispos of the HFS data
  139.     DisposeHandle(theHFS);
  140.  
  141.     return(MemError());
  142. }
  143.  
  144. //
  145. // Draw Object Handler for HFS objects
  146. //
  147.  
  148. pascal OSErr    HandleDrawHFS(Rect *destRect, WEObjectReference objectRef )
  149. {
  150. HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  151. Handle        iconCache = (Handle)WEGetObjectRefCon( objectRef );
  152. FontInfo    fInfo;
  153. GrafPtr        thePort;
  154. Rect        iconRect;
  155. OSErr        iErr;
  156. short        oldTextFont, oldTextSize, oldTextFace;
  157. short        sysTextFont = LMGetSysFontFam();
  158. short        sysTextSize = LMGetSysFontSize();
  159.  
  160.     // if the object's refcon did not contain an icon handle, load one.
  161.     if (iconCache == 0)
  162.     {
  163.         HLockHi( (Handle)theHFS );
  164.         if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='fold') )
  165.             iErr = GetIconSuite(&iconCache,genericFolderIconResource,svAllAvailableData);
  166.         else if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='disk') )
  167.         {
  168.             iErr = GetIconSuite(&iconCache,genericHardDiskIconResource,svAllAvailableData);
  169.         }
  170.         else
  171.         {
  172.             iErr = GetFileIcon( &(*theHFS)->fileSpec, svAllAvailableData, &iconCache );
  173.             
  174.             if (iErr)
  175.                 iErr = GetIconSuite(&iconCache,genericDocumentIconResource,svAllAvailableData);
  176.         }    
  177.         HUnlock( (Handle)theHFS );
  178.         if (iErr)
  179.             return(iErr);
  180.         WESetObjectRefCon( objectRef, (long)iconCache );
  181.     }
  182.     
  183.     // Determine the icon's rectangle and plot it
  184.     SetRect( &iconRect, (destRect->right + destRect->left)/2 - 16, destRect->top,
  185.                         (destRect->right + destRect->left)/2 + 16, destRect->top + 32 );
  186.     iErr = PlotIconSuite(&iconRect,(IconAlignmentType)(atVerticalCenter + atHorizontalCenter),
  187.                                             (IconTransformType)ttNone,    iconCache);
  188.                                             
  189.     // Draw the file's title.  First save old font info for port.
  190.     GetPort(&thePort);
  191.     oldTextFont = thePort->txFont;
  192.     oldTextSize = thePort->txSize;
  193.     oldTextFace = thePort->txFace;
  194.     
  195.     TextFont( sysTextFont );
  196.     TextSize( sysTextSize );
  197.     TextFace( 0 );
  198.     GetFontInfo(&fInfo);
  199.     
  200.     MoveTo( destRect->left + 1, destRect->bottom - fInfo.descent - fInfo.leading );
  201.     DrawString( (*theHFS)->fileSpec.name );
  202.     
  203.     TextFont( oldTextFont );
  204.     TextSize( oldTextSize );
  205.     TextFace( oldTextFace );
  206.     
  207.     return( iErr );
  208. }
  209.  
  210. //
  211. // Click Handler for HFS Objects.  
  212. //        Will send the finder an open selection apple event when object is clicked.
  213. //
  214.  
  215.  
  216. pascal Boolean    HandleClickHFS(    Point hitPt, 
  217.                                     short modifiers, 
  218.                                     long clickTime, 
  219.                                     WEObjectReference objectRef)
  220. {
  221. #pragma unused (hitPt, clickTime)
  222.  
  223. HFSFlavor    **theHFS;
  224.  
  225.     if (modifiers & 0x0001)         // look for double-clicks
  226.     {
  227.         theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  228.         
  229.         HLockHi( (Handle)theHFS );
  230.         SendFinderOpenAE(&(*theHFS)->fileSpec);
  231.         HUnlock( (Handle)theHFS );
  232.         
  233.         return(true);
  234.     }
  235.     else
  236.         return(false);
  237. }
  238.  
  239. //
  240. // SizeHFSObject()
  241. //        Used to return the display size of an HFS object
  242. //
  243. static void    SizeHFSObject( Point *objectSize, HFSFlavor **theHFS )
  244. {
  245. FontInfo    fInfo;
  246. GrafPtr        thePort;
  247. short        oldTextFont, oldTextSize, oldTextFace;
  248. short        strWidth, strHeight;
  249. short        sysTextFont = LMGetSysFontFam();
  250. short        sysTextSize = LMGetSysFontSize();
  251.  
  252.     GetPort(&thePort);
  253.     oldTextFont = thePort->txFont;
  254.     oldTextSize = thePort->txSize;
  255.     oldTextFace = thePort->txFace;
  256.     
  257.     TextFont( sysTextFont );
  258.     TextSize( sysTextSize );
  259.     TextFace( 0 );
  260.     
  261.     strWidth = StringWidth( (*theHFS)->fileSpec.name ) + 4;
  262.     GetFontInfo(&fInfo);
  263.     strHeight = fInfo.ascent + fInfo.descent + fInfo.leading;
  264.     
  265.     TextFont( oldTextFont );
  266.     TextSize( oldTextSize );
  267.     TextFace( oldTextFace );
  268.  
  269.     objectSize->h = ( strWidth > 32 ) ? strWidth : 32 ;
  270.     objectSize->v = 32 + strHeight + 1;    
  271. }
  272.  
  273. //
  274. // InsertFileRefFromFSSpec()
  275. //        Inserts a file object into a WASTE instance.
  276. //
  277.  
  278. OSErr    InsertFileRefFromFSSpec( FSSpec *theFile, WEReference theWE )
  279. {
  280. FInfo        fndrInfo;
  281. HFSFlavor    **theHFS;
  282. Point        theSize;
  283. OSErr        iErr;
  284.     
  285.     theHFS = (HFSFlavor**)NewHandleClear( sizeof(HFSFlavor) );
  286.     if (theHFS)
  287.     {
  288.         // First construct the HFSFlavor object from the FSSpec
  289.         iErr = FSpGetFInfo(theFile,&fndrInfo);
  290.         if (iErr)
  291.         {
  292.             DisposeHandle( (Handle)theHFS );
  293.             return(iErr);
  294.         }
  295.         HLock( (Handle)theHFS );
  296.         (*theHFS)->fileType = fndrInfo.fdType;
  297.         (*theHFS)->fileCreator = fndrInfo.fdCreator;
  298.         (*theHFS)->fdFlags = fndrInfo.fdFlags;
  299.         (*theHFS)->fileSpec = (*theFile);
  300.         HUnlock( (Handle)theHFS );
  301.         
  302.         SizeHFSObject( &theSize, theHFS );
  303.         
  304.         // Now insert it into the WEReference
  305.         WEInsertObject( flavorTypeHFS, (Handle)theHFS, theSize, theWE );
  306.     }
  307.     return(noErr);
  308. }
  309.