home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / GetFileIcon 1.1 / Old Headers / GetFileIconExample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-07  |  6.1 KB  |  353 lines  |  [TEXT/KAHL]

  1. /*
  2. GetFileIconExample.c
  3. 3/10/95
  4. ver 1.1
  5. -------
  6.  
  7.  
  8. GetFileIconExample.c is based on one of the 'Getting Started' articles
  9. in MacTech magazine.  It is used solely to provide a backbone for the
  10. GetFileIcon routine.
  11.  
  12. The purpose of this program is to demonstrate how to get a files' icon
  13. and display it.
  14.  
  15. For a more thorough description, please see the file, GetFileIcon.c
  16. */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <GestaltEqu.h>
  21. #include "GetFileIcon.h"
  22.  
  23.  
  24.  
  25.  
  26.  
  27. #define kBaseResID            128
  28. #define kErrorALRTid        128
  29. #define kAboutALRTid        129
  30.  
  31. #define kWINDResID            128
  32.  
  33. #define kVisible            true
  34. #define    kMoveToFront        (WindowPtr)-1L
  35. #define kSleep                60L
  36. #define kNilFilterProc        0L
  37.  
  38. #define kWindowStartX        20
  39. #define kWindowStartY        50
  40.  
  41. #define mApple                kBaseResID
  42. #define iAbout                1
  43.  
  44. #define mFile                kBaseResID+1
  45. #define iGetFile            1
  46. #define iQuit                3
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. /*************/
  54. /*  Globals  */
  55. /*************/
  56.  
  57. Boolean                gDone;
  58. Handle                gTheSuite;
  59. StandardFileReply    gTheReply;
  60.  
  61.  
  62. /***************/
  63. /*  Functions  */
  64. /***************/
  65.  
  66. void        ToolboxInit( void );
  67. void        MenuBarInit( void );
  68. WindowPtr    CreateWindow( Str255 name );
  69. void        DoCloseWindow( WindowPtr window );
  70. void        EventLoop( void );
  71. void        DoEvent( EventRecord *eventPtr );
  72. void        HandleMouseDown( EventRecord *eventPtr );
  73. void        HandleMenuChoice( long menuChoice );
  74. void        HandleAppleChoice( short item );
  75. void        HandleFileChoice( short item );
  76. void        DoUpdate( EventRecord *eventPtr );
  77. void        DoError( Str255 errorString );
  78.  
  79.  
  80. /******************************** main *********/
  81.  
  82. void    main( void )
  83. {
  84.     ToolboxInit();
  85.     MenuBarInit();
  86.  
  87.     CreateWindow( "\p<Untitled>" );
  88.  
  89.     EventLoop();
  90. }
  91.  
  92.  
  93. /*********************************** ToolboxInit */
  94.  
  95. void    ToolboxInit( void )
  96. {
  97. #ifdef THINK_C
  98.     InitGraf( &thePort );
  99. #else
  100.     InitGraf( &qd.thePort );
  101. #endif
  102.     InitFonts();
  103.     InitWindows();
  104.     InitMenus();
  105.     TEInit();
  106.     InitDialogs( 0L );
  107.     InitCursor();
  108. }
  109.  
  110.  
  111. /****************** MenuBarInit ***********************/
  112.  
  113. void    MenuBarInit( void )
  114. {
  115.     Handle            menuBar;
  116.     MenuHandle        menu;
  117.     
  118.     menuBar = GetNewMBar( kBaseResID );
  119.     
  120.     if(menuBar == NULL)
  121.         DoError( "\pCouldn't load the MBAR resource..." );
  122.  
  123.     SetMenuBar( menuBar );
  124.  
  125.     menu = GetMHandle( mApple );
  126.     AddResMenu( menu, 'DRVR' );
  127.  
  128.     DrawMenuBar();
  129. }
  130.  
  131.  
  132.  
  133. /****************** CreateWindow ***********************/
  134. WindowPtr CreateWindow( Str255 name )
  135. {
  136.     WindowPtr    window;
  137.     short        windowWidth, windowHeight;
  138.     
  139.     window = GetNewCWindow( kWINDResID, nil, kMoveToFront );
  140.     SetWTitle( window, name );
  141.  
  142.     
  143.     ShowWindow( window );
  144.     SetPort( window );
  145.     
  146.     return window;
  147. }
  148.  
  149.  
  150. /****************** DoCloseWindow ***********************/
  151.  
  152. void    DoCloseWindow( WindowPtr window )
  153. {
  154.     if(window != nil)
  155.         DisposeWindow( window );
  156. }
  157.  
  158.  
  159. /******************************** EventLoop *********/
  160.  
  161. void    EventLoop( void )
  162. {        
  163.     EventRecord        event;
  164.     
  165.     gDone = false;
  166.     while ( gDone == false )
  167.     {
  168.         if( WaitNextEvent(everyEvent, &event, kSleep, nil) )
  169.             DoEvent( &event );
  170.     }
  171. }
  172.  
  173.  
  174. /************************************* DoEvent     */
  175.  
  176. void    DoEvent( EventRecord *eventPtr )
  177. {
  178.     char        theChar;
  179.     
  180.     switch ( eventPtr->what )
  181.     {
  182.         case mouseDown: 
  183.             HandleMouseDown( eventPtr );
  184.             break;
  185.  
  186.         case keyDown:
  187.         case autoKey:
  188.             theChar = eventPtr->message & charCodeMask;
  189.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  190.                 HandleMenuChoice( MenuKey( theChar ) );
  191.             break;
  192.  
  193.         case updateEvt:
  194.             DoUpdate( eventPtr );
  195.             break;
  196.     }
  197. }
  198.  
  199.  
  200. /************************************* HandleMouseDown */
  201.  
  202. void    HandleMouseDown( EventRecord *eventPtr )
  203. {
  204.     WindowPtr        window;
  205.     short            thePart;
  206.     long            menuChoice;
  207.     
  208.     thePart = FindWindow( eventPtr->where, &window );
  209.     
  210.     switch ( thePart )
  211.     {
  212.         case inMenuBar:
  213.             menuChoice = MenuSelect( eventPtr->where );
  214.             HandleMenuChoice( menuChoice );
  215.             break;
  216.         case inSysWindow : 
  217.             SystemClick( eventPtr, window );
  218.             break;
  219.         case inGoAway:
  220.             if ( TrackGoAway( window, eventPtr->where ) )
  221.                 gDone = TRUE;
  222.             break;
  223.         case inContent:
  224.             SelectWindow( window );
  225.             break;
  226.         case inDrag : 
  227.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  228.             break;
  229.     }
  230. }
  231.  
  232.  
  233. /****************** HandleMenuChoice ***********************/
  234.  
  235. void    HandleMenuChoice( long menuChoice )
  236. {
  237.     short    menu;
  238.     short    item;
  239.     
  240.     if ( menuChoice != 0 )
  241.     {
  242.         menu = HiWord( menuChoice );
  243.         item = LoWord( menuChoice );
  244.         
  245.         switch ( menu )
  246.         {
  247.             case mApple:
  248.                 HandleAppleChoice( item );
  249.                 break;
  250.  
  251.             case mFile:
  252.                 HandleFileChoice( item );
  253.                 break;
  254.         }
  255.         HiliteMenu( 0 );
  256.     }
  257. }
  258.  
  259.  
  260. /****************** HandleAppleChoice ***********************/
  261.  
  262. void    HandleAppleChoice( short item )
  263. {
  264.     MenuHandle    appleMenu;
  265.     Str255        accName;
  266.     short        accNumber;
  267.     
  268.     switch ( item )
  269.     {
  270.         case iAbout:
  271.             Alert(kAboutALRTid, nil);
  272.             break;
  273.  
  274.         default:
  275.             appleMenu = GetMHandle( mApple );
  276.             GetItem( appleMenu, item, accName );
  277.             accNumber = OpenDeskAcc( accName );
  278.             break;
  279.     }
  280. }
  281.  
  282.  
  283. /****************** HandleFileChoice ***********************/
  284. void    HandleFileChoice( short item )
  285. {
  286.     WindowPtr            window;
  287.  
  288.  
  289.     switch ( item )
  290.     {
  291.         case iGetFile:
  292.             StandardGetFile(0L, -1, 0L, &gTheReply);
  293.             if( gTheReply.sfGood )
  294.             {
  295.                 GetFileIcon(&gTheReply.sfFile, svAllAvailableData, &gTheSuite);
  296.                 /* Note: if you are just using large icons (32x32), just use
  297.                     svLarge1Bit + svLarge4Bit + svLarge8Bit instead of svAllAvailableData */
  298.                 window = FrontWindow();
  299.                 EraseRgn( window->visRgn );
  300.                 InvalRgn( window->visRgn );
  301.             }
  302.             break;
  303.  
  304.         case iQuit:
  305.             gDone = true;
  306.             DoCloseWindow( FrontWindow() );
  307.             break;
  308.     }
  309. }
  310.  
  311.  
  312.  
  313. /************************************* DoUpdate     */
  314. void    DoUpdate( EventRecord *eventPtr )
  315. {
  316.     WindowPtr    window;
  317.     Rect        r;
  318.     char        temp[40];
  319.  
  320.  
  321.  
  322.     window = (WindowPtr )eventPtr->message;
  323.     BeginUpdate( window );
  324.  
  325.     if(gTheSuite == nil)
  326.     {
  327.         MoveTo(20, 50);
  328.         DrawString("\pPlease press CMD-O to display a file's icon");
  329.     }
  330.     else
  331.     {
  332.         SetRect(&r, 24, 30, 24 + 32, 30 + 32);
  333.         PlotIconSuite(&r, 0, 0, gTheSuite);
  334.         MoveTo(70, 50);
  335.         DrawString( gTheReply.sfFile.name );
  336.  
  337.         sprintf(temp, "vRefNum = %d, parID = %ld",
  338.             gTheReply.sfFile.vRefNum, gTheReply.sfFile.parID);
  339.         MoveTo(70, 64);
  340.         DrawString( CtoPstr(temp) );
  341.     }
  342.     EndUpdate(window);
  343. }
  344.  
  345.  
  346. /***************** DoError ********************/
  347.  
  348. void    DoError( Str255 errorString )
  349. {
  350.     ParamText(errorString, "\p", "\p", "\p");
  351.     StopAlert(kErrorALRTid, kNilFilterProc);
  352.     ExitToShell();
  353. }