home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 May / macformat-050.iso / Shareware Plus / Developers / Find_icon folder / Sources / Find_icon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-25  |  3.3 KB  |  125 lines  |  [TEXT/CWIE]

  1. /*    ---------------------------------------------------------------------------------------------
  2.     Find_icon, code for constructing icon suites for files and folders
  3.     
  4.     by James W. Walker
  5.     preferred e-mail: <mailto:jwwalker@kagi.com>
  6.     alternate e-mail: <mailto:jwwalker@aol.com>, <jim@nisus-soft.com>
  7.     web: <http://users.aol.com/jwwalker/>
  8.     
  9.     File: Find_icon.c
  10.     
  11.     Copyright ©1997 by James W. Walker
  12.     
  13.     You may incorporate this sample code into your applications without
  14.     restriction, though the sample code has been provided "AS IS" and the
  15.     responsibility for its operation is 100% yours.
  16.     If you're going to re-distribute the source, please make it clear
  17.     that the code was descended from James W. Walker's code,
  18.     but that you've made changes.
  19.     ---------------------------------------------------------------------------------------------
  20. */
  21. #include <A4Stuff.h>
  22. #include <Icons.h>
  23. #include "Find_icon.h"
  24. #include "cheap-exceptions.h"
  25. #include <Finder.h>
  26. #include "Get_custom_folder_icon.h"
  27. #include "Get_normal_folder_icon.h"
  28. #include "Get_volume_icon.h"
  29. #include "Get_custom_file_icon.h"
  30. #include "Get_normal_file_icon.h"
  31. #include "Is_vol_ejected.h"
  32.  
  33. #if __MC68K__ && !__A5__    // 68K code resource, I assume standalone
  34.     #define    Find_icon    main
  35. #endif
  36.  
  37. /*    ------------------------------------------------------------------
  38.     Find_icon        Given a file specification for a file, folder, or
  39.                     volume, create an appropriate icon suite
  40.                     and find its label color.
  41.     ------------------------------------------------------------------
  42. */
  43. pascal OSErr Find_icon(
  44. /* --> */    const FSSpec    *thing,
  45. /* --> */    MetaSelectorValue    icon_selector,
  46. /* <-- */    Handle    *the_suite
  47. )
  48. {
  49.     CInfoPBRec        cpb;
  50.     Str31            name;
  51.     OSErr            err;
  52.     #if __MC68K__ && !__A5__
  53.     EnterCodeResource();    // set up A4 register
  54.     #endif
  55.     
  56.     *the_suite = NULL;
  57.     
  58.     forbid_action( Is_vol_ejected( thing->vRefNum ), ejected,
  59.         err = volOffLinErr );
  60.     
  61.     BlockMoveData( thing->name, name, sizeof(name) );
  62.     cpb.hFileInfo.ioVRefNum = thing->vRefNum;
  63.     cpb.hFileInfo.ioDirID = thing->parID;
  64.     cpb.hFileInfo.ioNamePtr = name;
  65.     cpb.hFileInfo.ioFDirIndex = 0;
  66.     err = PBGetCatInfoSync( &cpb );
  67.     forbid( err, PBGetCatInfoSync );
  68.         
  69.     if ( (cpb.hFileInfo.ioFlAttrib & ioDirMask) == 0 )    // file
  70.     {
  71.         if (cpb.hFileInfo.ioFlFndrInfo.fdFlags & kHasCustomIcon)
  72.         {
  73.             err = Get_custom_file_icon( thing,
  74.                 icon_selector, the_suite );
  75.         }
  76.         else    // no custom icon
  77.         {
  78.             err = Get_normal_file_icon( &cpb,
  79.                 icon_selector, the_suite );
  80.         }
  81.     }
  82.     else    // directory
  83.     {
  84.         if ( ((cpb.hFileInfo.ioFlFndrInfo.fdFlags & kHasCustomIcon) == 0)
  85.             ||
  86.             (noErr !=  Get_custom_folder_icon( thing->vRefNum,
  87.                 cpb.hFileInfo.ioDirID, icon_selector,
  88.                 the_suite ))
  89.         )    // failed to find a custom icon, try normal kind
  90.         {
  91.             if (cpb.hFileInfo.ioDirID == fsRtDirID)    // a volume
  92.             {
  93.                 err = Get_volume_icon( thing->vRefNum, the_suite );
  94.             }
  95.             else    // a folder
  96.             {
  97.                 err = Get_normal_folder_icon( &cpb,
  98.                     icon_selector, the_suite );
  99.             }
  100.         }
  101.     }
  102.     
  103.     if (*the_suite != NULL)
  104.     {
  105.         SetSuiteLabel( *the_suite,
  106.             (cpb.hFileInfo.ioFlFndrInfo.fdFlags & kColor) >> 1 );
  107.     }
  108.     goto getout;
  109.     // ----------- end of normal case --------------
  110.  
  111. ejected:
  112. PBGetCatInfoSync:    // ------- error handler ---------
  113.     if (thing->parID == fsRtParID)    // a volume
  114.     {
  115.         err = Get_volume_icon( thing->vRefNum, the_suite );
  116.     }
  117.     
  118. getout:
  119.     #if __MC68K__ && !__A5__
  120.     ExitCodeResource();    // restore A4 register
  121.     #endif
  122.  
  123.     return err;
  124. }
  125.