home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / GetFileIcon ƒ / GetFileIconExample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-02  |  5.8 KB  |  344 lines  |  [TEXT/KAHL]

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