home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Argus Frameworks 2.1 / Argus Starter 2.1 / Main / Main.cp next >
Encoding:
Text File  |  1996-01-04  |  14.4 KB  |  595 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  
  3.     Main.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     Starter Application 2.1
  9.  
  10.     Requires Argus Libraries 2.1
  11.     
  12.     Funct_()   All functions and sub-routines in Main.cp are identified
  13.                with an underscore '_' at the end of the function name.
  14.                This hopefully helps to identify program functions 
  15.                from Toolbox calls.
  16.  
  17.     MyFunct()  All other functions are identified by "My" at the
  18.                beginning of the function name.
  19.  
  20. */
  21.  
  22. /********** Standard Includes */
  23. #include <Types.h>
  24. #include <Quickdraw.h>
  25. #include <Controls.h>
  26. #include <Desk.h>
  27. #include <Dialogs.h>
  28. #include <DiskInit.h>
  29. #include <Editions.h>
  30. #include <EPPC.h>
  31. #include <Events.h>
  32. #include <Fonts.h>
  33. #include <GestaltEqu.h>
  34. #include <Lists.h>
  35. #include <Menus.h>
  36. #include <OSEvents.h>
  37. #include <TextEdit.h>
  38. #include <ToolUtils.h>
  39. #include <Traps.h>
  40. #include <AppleEvents.h>
  41. #include <Balloons.h>
  42.  
  43. #include "Fn_Prototypes.h"
  44. #include "ArgusAbout.h"
  45. #include "ArgusHelp.h"
  46. #include "MainWindow.h"
  47. #include "SampleDialog.h"
  48. #include "SampleList.h"
  49.  
  50. /********** Defines */
  51. #define BASE_RES             400
  52. #define NIL_PTR              0L
  53. #define MOVE_TO_FRONT        -1L
  54. #define LEAVE_IT             FALSE
  55. #define DRAG_THRESHOLD       30
  56. #define REMOVE_ALL_EVENTS    0
  57. #define MIN_SLEEP            60L
  58. #define NIL_MOUSE_REGION     0L
  59. #define WNE_TRAP_NUM         0x60
  60. #define UNIMPL_TRAP_NUM      0x9F
  61. #define NIL_STR              "\p"
  62. #define MIN_SYS_VERSION      0x700  // identify min sys req
  63. #define HELP_MENU            "\pApplication Help..."
  64.  
  65. /********** General */
  66. #define ZERO             0
  67. #define FIRST            1
  68. #define SECOND           2
  69. #define THIRD            3
  70. #define FOURTH           4
  71.  
  72. #define LINE_FEED        10
  73. #define RETURN           13
  74. #define SPACE            32
  75. #define TAB              9
  76.  
  77. /********** Window definitions */
  78. #define WIND_LEFT        10
  79. #define WIND_TOP         50
  80. #define WIND_OFFSET      10
  81.  
  82. /*
  83.     The following define statements cross-reference
  84.     names with resource ID numbers.
  85. */
  86.  
  87. /********** Menus */
  88. #define APPLE_MENU     BASE_RES
  89. #define FILE_MENU      BASE_RES + 1
  90. #define EDIT_MENU      BASE_RES + 2
  91.  
  92. /********** Error Strings */
  93. #define GENERAL_ERR    900
  94. #define BAD_SYS        901
  95. #define N0_RESOURCE    902
  96.  
  97. /********** Apple Menu Stuff */
  98. #define ABOUT          1
  99.  
  100. /********** File Menu Stuff */
  101. #define NEW            1
  102. #define CLOSE          2
  103. #define QUIT           3
  104.  
  105. /********** Edit Menu Stuff */
  106. #define UNDO           1
  107. #define CUT            3
  108. #define COPY           4
  109. #define PASTE          5
  110. #define CLEAR          6
  111.  
  112. /********** Structures */
  113. struct SysConfigRec
  114. {
  115.     Boolean hasGestalt;
  116.     Boolean hasWNE;
  117.     Boolean hasColorQD;
  118.     Boolean hasAppleEvents;
  119.     Boolean hasEditionMgr;
  120.     Boolean hasHelpMgr;
  121.  
  122.     long    sysVersion;
  123. };
  124.  
  125. /********** Variables */
  126. Boolean             gDone;
  127. EventRecord         gTheEvent;
  128. MenuHandle          gAppleMenu;
  129. MenuHandle          gFileMenu;
  130. MenuHandle          gEditMenu;
  131. Rect                gDragRect;
  132. short               gAppResourceFile;
  133. struct SysConfigRec gSysConfig;
  134. int                 gNewWindowLeft = WIND_LEFT;
  135. int                 gNewWindowTop = WIND_TOP;
  136.  
  137. /********** Prototypes */
  138. void main( void );
  139. void ToolBoxInit_( void );
  140. Boolean TrapAvailable_( short tNumber, TrapType tType );
  141. static void GetSysConfig_( void );
  142. void MenuBarInit_( void );
  143. void SetUpDragRect_( void );
  144. void MainLoop_( void );
  145. void HandleEvent_( void );
  146. void HandleMouseDown_( void );
  147. void AdjustMenus_( void );
  148. int  IsDAWindow_( WindowPtr whichWindow );
  149. void HandleMenuChoice_( long int menuChoice );
  150. void HandleAppleChoice_( int theItem );
  151. void HandleFileChoice_( int theItem );
  152. void HandleEditChoice_( int theItem );
  153. void NewWindow_( void );
  154. void Quit_( void );
  155.  
  156. /********** main */
  157.  
  158. void main( void )
  159. {
  160.     ToolBoxInit_();
  161.     GetSysConfig_();
  162.     
  163.     if( gSysConfig.hasAppleEvents )
  164.         FnAE_InitAE();
  165.     
  166.     MenuBarInit_();
  167.     SetUpDragRect_();
  168.     
  169.     if( !gSysConfig.hasAppleEvents )
  170.         /* put OpenApp procedure here */
  171.         MyCreateWindow( BASE_RES, NIL_PTR, (WindowPtr)MOVE_TO_FRONT,
  172.             WIND_TOP, WIND_LEFT, WIND_OFFSET, N0_RESOURCE );
  173.  
  174.     MainLoop_();
  175. }
  176.  
  177.  
  178. /********** ToolBoxInit */
  179.  
  180. void ToolBoxInit_( void )
  181. /*
  182.     Standard initialization procedure per IM:Overview p4-75
  183. */
  184. {
  185.     MaxApplZone();
  186.     MoreMasters();
  187.     
  188.     InitGraf( &qd.thePort );
  189.     InitFonts();
  190.     InitWindows();
  191.     InitMenus();
  192.     TEInit();
  193.     InitDialogs( NIL_PTR );
  194.     
  195.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );    
  196.     InitCursor();
  197. }
  198.  
  199.  
  200. /********** TrapAvailable */
  201.  
  202. Boolean TrapAvailable_( short tNumber, TrapType tType )
  203. {
  204.     return( NGetTrapAddress( tNumber, tType ) 
  205.     != GetTrapAddress( _Unimplemented ) );
  206. }
  207.  
  208.  
  209. /********** GetSysConfig */
  210.  
  211. static void GetSysConfig_( void )
  212. {   
  213.     OSErr        ignoreError;
  214.     long         tempLong;
  215.     SysEnvRec    environs;
  216.     short        myBit;
  217.  
  218.  
  219.     gAppResourceFile = CurResFile();  /* set app resource fork ID */
  220.  
  221.     /* Check to see if Gestalt Manager is supported */
  222.     gSysConfig.hasGestalt = TrapAvailable_( _Gestalt, ToolTrap );
  223.     if( !gSysConfig.hasGestalt )
  224.         /* Something has got to be wrong */
  225.         FnErr_DisplayStrID( BAD_SYS, TRUE );
  226.     else
  227.     {
  228.         /* Determine various system configuration checks */
  229.  
  230.         /* Check to see if WaitNextEvent is supported */
  231.         gSysConfig.hasWNE = TrapAvailable_( _WaitNextEvent, ToolTrap );
  232.         
  233.         /* Check for Color Capability */
  234.         ignoreError = Gestalt( gestaltQuickdrawVersion, &tempLong );
  235.         gSysConfig.hasColorQD = ( tempLong != gestaltOriginalQD );
  236.         
  237.         /* Check to see if AppleEvents are supported in OS */
  238.         gSysConfig.hasAppleEvents = ( Gestalt( gestaltAppleEventsAttr,
  239.             &tempLong ) == noErr );
  240.  
  241.         /* Check for Edition Manager */
  242.         gSysConfig.hasEditionMgr = ( Gestalt( gestaltEditionMgrAttr,
  243.             &tempLong ) == noErr );
  244.         if( gSysConfig.hasEditionMgr )
  245.             if( InitEditionPack() != noErr )
  246.                 gSysConfig.hasEditionMgr = false;
  247.                 
  248.         /* Check for Help Manager */
  249.         ignoreError = Gestalt( gestaltHelpMgrAttr, &tempLong );
  250.         myBit = gestaltHelpMgrPresent;
  251.         gSysConfig.hasHelpMgr =
  252.             BitTst( &tempLong, 31 - myBit );
  253.             
  254.         /* Determine and Check OS Version */
  255.         gSysConfig.sysVersion = 0.0;
  256.         ignoreError = Gestalt( gestaltSystemVersion, &tempLong );
  257.         gSysConfig.sysVersion = tempLong;
  258.         if( MIN_SYS_VERSION > gSysConfig.sysVersion )
  259.             FnErr_DisplayStrID( BAD_SYS, TRUE );
  260.     }
  261. }
  262.  
  263.  
  264. /********** MenuBarInit    */
  265.  
  266. void MenuBarInit_( void )
  267. {
  268.     Handle     myMenuBar;
  269.     MenuHandle helpMenu;
  270.     OSErr      err;
  271.  
  272.     myMenuBar = GetNewMBar( BASE_RES ) ;
  273.     if ( myMenuBar == NIL_PTR )
  274.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  275.     SetMenuBar( myMenuBar );
  276.     
  277.     if ( ( gAppleMenu = GetMHandle( APPLE_MENU ) ) == NIL_PTR )
  278.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  279.     if ( ( gFileMenu = GetMHandle( FILE_MENU ) ) == NIL_PTR )
  280.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  281.     if ( ( gEditMenu = GetMHandle( EDIT_MENU ) ) == NIL_PTR )
  282.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  283.         
  284.     AddResMenu( gAppleMenu, 'DRVR' );
  285.     DrawMenuBar();
  286.     
  287.     if( gSysConfig.hasHelpMgr )
  288.     {
  289.         err = HMGetHelpMenuHandle( &helpMenu );
  290.         if( err == noErr )
  291.         {
  292.             if( helpMenu != nil )
  293.             {
  294.                  AppendMenu( helpMenu, HELP_MENU );   
  295.             }
  296.         }
  297.     }
  298. }
  299.  
  300.  
  301. /********** SetUpDragRect */
  302.  
  303. void SetUpDragRect_( void )
  304. {
  305.     gDragRect = qd.screenBits.bounds;
  306.     gDragRect.left += DRAG_THRESHOLD;
  307.     gDragRect.right -= DRAG_THRESHOLD;
  308.     gDragRect.bottom -= DRAG_THRESHOLD;
  309. }
  310.  
  311.  
  312. /********** MainLoop */
  313.  
  314. void MainLoop_( void )
  315. {
  316.     gDone = FALSE;
  317.     while( gDone == FALSE )
  318.     {
  319.         HandleEvent_();
  320.     }
  321. }
  322.  
  323.  
  324. /********** HandleEvent */
  325.  
  326. void HandleEvent_( void )
  327. {
  328.     char    theChar;
  329.     GrafPtr oldPort;
  330.     Point   displayPoint;
  331.     
  332.     if ( gSysConfig.hasWNE )
  333.         WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP,
  334.             NIL_MOUSE_REGION );
  335.     else
  336.     {
  337.         SystemTask();
  338.         GetNextEvent( everyEvent, &gTheEvent );
  339.     }
  340.  
  341.     switch ( gTheEvent.what )
  342.     {
  343.         case mouseDown: 
  344.             HandleMouseDown_();
  345.             break;
  346.         case mouseUp:
  347.             // this application doesn't use mouseUp events
  348.             break;
  349.         case keyDown:
  350.         case autoKey:
  351.             theChar = gTheEvent.message & charCodeMask;
  352.             if (( gTheEvent.modifiers & cmdKey ) != 0)
  353.             {
  354.                 AdjustMenus_(); 
  355.                 HandleMenuChoice_( MenuKey( theChar ) );
  356.             }
  357.             else
  358.             {
  359.                 /* Handle text from keyboard */
  360.             }
  361.             break;
  362.         case updateEvt:
  363.             if ( !IsDAWindow_( (WindowPtr)gTheEvent.message ) )
  364.             {
  365.                 /* Handle update event */
  366.                 MyDoUpdateWindow( (WindowPtr)gTheEvent.message );
  367.              }
  368.             break;
  369.         case diskEvt:
  370.             // most applications don't need to worry about diskEvt's
  371.              break;
  372.         case activateEvt:
  373.             if ( !IsDAWindow_( (WindowPtr)gTheEvent.message ) )
  374.             {
  375.                 if ( gTheEvent.modifiers & activeFlag )
  376.                 {
  377.                     /* Handle activate event. */
  378.                     MyDoActivateWindow((WindowPtr)gTheEvent.message);
  379.                 }
  380.                 else
  381.                 {
  382.                     /* Handle deactivate event. */
  383.                     MyDoDeactivateWindow((WindowPtr)gTheEvent.message);
  384.                 }
  385.             }
  386.             break;
  387.         case osEvt:
  388.             // this application doesn't support operating sys events
  389.             break;
  390.         case nullEvent:
  391.             // ignore
  392.             break;
  393.         case kHighLevelEvent:
  394.             // need to #include <Events.h> to define kHighLevelEvent
  395.             FnAE_DoHighLevelEvent( &gTheEvent );
  396.             break;
  397.     }
  398. }
  399.  
  400.  
  401. /********** HandleMouseDown */
  402.  
  403. void HandleMouseDown_( void )
  404. {
  405.     WindowPtr   whichWindow;
  406.     short int   thePart;
  407.     long int    menuChoice;    
  408.     Point       theLocation;
  409.     
  410.     thePart = FindWindow( gTheEvent.where, &whichWindow );
  411.     switch ( thePart )
  412.     {
  413.         case inMenuBar:
  414.             AdjustMenus_();
  415.             menuChoice = MenuSelect( gTheEvent.where );
  416.             HandleMenuChoice_( menuChoice );
  417.             break;
  418.         case inSysWindow: 
  419.             SystemClick( &gTheEvent, whichWindow );
  420.             break;
  421.         case inContent:
  422.             if( whichWindow != FrontWindow() )
  423.                 SelectWindow( whichWindow );
  424.             else if( !IsDAWindow_( (WindowPtr)gTheEvent.message ) )
  425.                 /* Handle click in window content */ 
  426.                 MyDoContent( whichWindow, &gTheEvent );
  427.             break;
  428.         case inDrag:
  429.             if( whichWindow != FrontWindow() )
  430.             {
  431.                 SelectWindow( whichWindow );
  432.                 DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  433.             }
  434.             else
  435.                 DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  436.             break;
  437.         case inGrow:
  438.             // not used in this application
  439.             break;
  440.         case inGoAway:
  441.             theLocation = gTheEvent.where;
  442.             GlobalToLocal( &theLocation );
  443.             if( TrackGoAway( whichWindow, theLocation ) )
  444.                 DisposeWindow( whichWindow );
  445.             break;
  446.         case inZoomIn:
  447.         case inZoomOut:
  448.             // not used in this application
  449.             break;
  450.     }
  451. }
  452.  
  453.  
  454. /********** AdjustMenus */
  455.  
  456. void AdjustMenus_( void )
  457. {
  458.     WindowPtr currentWindow;
  459.  
  460.     if (IsDAWindow_( FrontWindow() ) )
  461.     {
  462.         EnableItem( gEditMenu, UNDO );
  463.         EnableItem( gEditMenu, CUT );
  464.         EnableItem( gEditMenu, COPY );
  465.         EnableItem( gEditMenu, PASTE );
  466.         EnableItem( gEditMenu, CLEAR );
  467.     }
  468.     else
  469.     {
  470.         DisableItem( gEditMenu, UNDO );
  471.         DisableItem( gEditMenu, CUT );
  472.         DisableItem( gEditMenu, COPY );
  473.         DisableItem( gEditMenu, PASTE );
  474.         DisableItem( gEditMenu, CLEAR );
  475.     }
  476.         
  477.     if ( ( currentWindow = FrontWindow() ) == NIL_PTR )
  478.         DisableItem( gFileMenu, CLOSE );
  479.     else
  480.         EnableItem( gFileMenu, CLOSE );
  481. }
  482.  
  483.  
  484. /********** IsDAWindow */
  485.  
  486. int IsDAWindow_( WindowPtr whichWindow )
  487. {
  488.     if ( whichWindow == NIL_PTR )
  489.         return( FALSE );
  490.     else /* DA windows have negative windowKinds */
  491.         return( ( (WindowPeek)whichWindow )->windowKind < 0 );
  492. }
  493.  
  494.  
  495. /********** HandleMenuChoice */
  496.  
  497. void HandleMenuChoice_( long int menuChoice )
  498. {
  499.     int    theMenu;
  500.     int    theItem;
  501.     
  502.     if ( menuChoice != 0 )
  503.     {
  504.         theMenu = HiWord( menuChoice );
  505.         theItem = LoWord( menuChoice );
  506.         switch ( theMenu )
  507.         {
  508.             case APPLE_MENU :
  509.                 HandleAppleChoice_( theItem );
  510.                 break;
  511.             case FILE_MENU :
  512.                 HandleFileChoice_( theItem );
  513.                 break;
  514.             case EDIT_MENU :
  515.                 HandleEditChoice_( theItem );
  516.                 break;
  517.             case kHMHelpMenuID :
  518.                 MyHelpDialog();
  519.                 break;
  520.         }
  521.         HiliteMenu( 0 );
  522.     }
  523. }
  524.  
  525.  
  526. /********** HandleAppleChoice */
  527.  
  528. void HandleAppleChoice_( int theItem )
  529. {
  530.     Str255  accName;
  531.     int     accNumber;
  532.     
  533.     switch ( theItem )
  534.     {
  535.         case ABOUT : 
  536.             MyAboutDialog();
  537.             break;
  538.         default :
  539.             GetItem( gAppleMenu, theItem, accName );
  540.             accNumber = OpenDeskAcc( accName );
  541.             break;
  542.     }
  543. }
  544.  
  545.  
  546. /********** HandleFileChoice */
  547.  
  548. void HandleFileChoice_( int theItem )
  549. {
  550.     WindowPtr   whichWindow;
  551.  
  552.     switch ( theItem )
  553.     {
  554.         case NEW :
  555.             NewWindow_();
  556.             break;
  557.         case CLOSE :
  558.             if ( ( whichWindow = FrontWindow() ) != NIL_PTR )
  559.                 DisposeWindow( whichWindow );
  560.             break;
  561.         case QUIT :
  562.             Quit_();
  563.             break;
  564.     }
  565. }
  566.  
  567.  
  568. /********** HandleEditChoice */
  569.  
  570. void HandleEditChoice_( int theItem )
  571. {
  572.     if( SystemEdit( theItem - 1 ) == 0 )
  573.     {
  574.         /* Add Edit menu switch statement here */
  575.     }
  576. }
  577.  
  578.  
  579. /********** NewWindow */
  580.  
  581. void NewWindow_( void )
  582. {
  583.     MyCreateWindow( BASE_RES, NIL_PTR, (WindowPtr)MOVE_TO_FRONT,
  584.         WIND_TOP, WIND_LEFT, WIND_OFFSET, N0_RESOURCE );
  585. }
  586.  
  587.  
  588. /********** Quit */
  589.  
  590. void Quit_( void )
  591. {
  592.     gDone = TRUE;
  593. }
  594.  
  595. // End of File