home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / Argus TE 2.0 / TE Main / Main.cp next >
Encoding:
Text File  |  1995-07-27  |  22.9 KB  |  844 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  
  3.     Main.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     Argus Text Editor
  9.     (Written using THINK C 6.0)
  10.  
  11.     Argus Starter used as program template.
  12.     Uses Argus Function Libraries
  13. */
  14.  
  15.  
  16. /********** Includes */
  17. #include <Types.h>
  18. #include <Quickdraw.h>
  19. #include <Controls.h>
  20. #include <Desk.h>
  21. #include <Dialogs.h>
  22. #include <DiskInit.h>
  23. #include <Editions.h>
  24. #include <EPPC.h>
  25. #include <Events.h>        
  26. #include <Fonts.h>
  27. #include <GestaltEqu.h>
  28. #include <Lists.h>
  29. #include <Menus.h>
  30. #include <OSEvents.h>
  31. #include <TextEdit.h>
  32. #include <ToolUtils.h>
  33. #include <Traps.h>
  34. #include <AppleEvents.h>   
  35. #include <Balloons.h>      
  36.  
  37. #include "Fn_Prototypes.h"
  38. #include "TE Window.h"
  39. #include "ArgusAbout.h"
  40. #include "ArgusHelp.h"
  41.  
  42. #include <stdlib.h>
  43.  
  44. /********** Defines */
  45. #define BASE_RES             400
  46. #define NIL_PTR              0L
  47. #define MOVE_TO_FRONT        -1L
  48. #define LEAVE_IT             FALSE
  49. #define DRAG_THRESHOLD       30
  50. #define REMOVE_ALL_EVENTS    0
  51. #define MIN_SLEEP            60L
  52. #define NIL_MOUSE_REGION     0L
  53. #define WNE_TRAP_NUM         0x60
  54. #define UNIMPL_TRAP_NUM      0x9F
  55. #define NIL_STR              "\p"
  56. #define MIN_SYS_VERSION      0x700  // identify min sys req
  57. #define HELP_MENU            "\pArgus TE Help..."
  58.  
  59. /********** General */
  60. #define ZERO             0
  61. #define FIRST            1
  62. #define SECOND           2
  63. #define THIRD            3
  64. #define FOURTH           4
  65.  
  66. #define LINE_FEED        10
  67. #define RETURN           13
  68. #define SPACE            32
  69. #define TAB              9
  70.  
  71. /********** Window definitions */
  72. #define WIND_LEFT        5
  73. #define WIND_TOP         45
  74. #define WIND_OFFSET      20
  75.  
  76. /********** Menus */
  77. #define APPLE_MENU     BASE_RES
  78. #define FILE_MENU      BASE_RES + 1
  79. #define EDIT_MENU      BASE_RES + 2
  80.  
  81. /********** Error Strings */
  82. #define GENERAL_ERR    900
  83. #define BAD_SYS        901
  84. #define N0_RESOURCE    902
  85.  
  86. /********** Apple Menu Stuff */
  87. #define ABOUT          1
  88.  
  89. /********** File Menu Stuff */
  90. #define NEW            1
  91. #define OPEN           2
  92. #define CLOSE          3
  93. #define SAVE           5
  94. #define SAVEAS         6
  95. #define PAGESETUP      8
  96. #define PRINT          9
  97. #define QUIT           11
  98.  
  99. /********** Edit Menu Stuff */
  100. #define UNDO           1
  101. #define CUT            3
  102. #define COPY           4
  103. #define PASTE          5
  104. #define CLEAR          6
  105. #define SELECTALL      8
  106.  
  107. /********** Structures */
  108. struct SysConfigRec
  109. {
  110.     Boolean hasGestalt;
  111.     Boolean hasWNE;
  112.     Boolean hasColorQD;
  113.     Boolean hasAppleEvents;
  114.     Boolean hasEditionMgr;
  115.     Boolean hasHelpMgr;
  116.  
  117.     long    sysVersion;
  118. };
  119.  
  120. extern struct WindowData
  121. {
  122.     TEHandle      wTEH;
  123.     ControlHandle wSBH;
  124.     Boolean       wDirty;
  125.     Boolean       wSaved;
  126.     Str255        wFilename;
  127.     short         wVRef;
  128.     short         wRefNum;
  129.     THPrint       wPrintH;
  130. };
  131.  
  132. /********** Variables */
  133. Boolean             gDone;
  134. EventRecord         gTheEvent;
  135. MenuHandle          gAppleMenu;
  136. MenuHandle          gFileMenu;
  137. MenuHandle          gEditMenu;
  138. Rect                gDragRect;
  139. short               gAppResourceFile;
  140. struct SysConfigRec gSysConfig;
  141. int                 gNewWindowLeft = WIND_LEFT;
  142. int                 gNewWindowTop = WIND_TOP;
  143.  
  144. WindowPtr           gWindow;        // current window
  145. TEHandle            gTEH;           // current TE record
  146. ControlHandle       gSBH;           // current scroll bar
  147. Boolean             gDirty;         // current TE dirty status
  148. Boolean             gSaved;         // current TE saved status
  149. Str255              gFilename;      // current filename
  150. short               gVRef;          // current file VRef
  151. short               gRefNum;        // current file RefNum
  152. THPrint             gPrintH = NULL; // current print record
  153.  
  154.  
  155. /********** Prototypes */
  156. void ToolBoxInit_( void );
  157. Boolean TrapAvailable_( short tNumber, TrapType tType );
  158. static void GetSysConfig_( void );
  159. void MenuBarInit_( void );
  160. void SetUpDragRect_( void );
  161. void MainLoop_( void );
  162. void HandleEvent_( void );
  163. void HandleMouseDown_( void );
  164. void AdjustMenus_( void );
  165. int  IsDAWindow_( WindowPtr whichWindow );
  166. void HandleMenuChoice_( long int menuChoice );
  167. void HandleAppleChoice_( int theItem );
  168. void HandleFileChoice_( int theItem );
  169. void HandleEditChoice_( int theItem );
  170. void MySetGlobals( WindowPtr w );
  171.  
  172. extern void FnTE_AdjustText( TEHandle *te, ControlHandle *vScroll );
  173. extern void FnIO_pStrCopy( StringPtr p1, StringPtr p2 );
  174.  
  175.  
  176. /********** main */
  177.  
  178. void main( void )
  179. {
  180.     ToolBoxInit_();
  181.     GetSysConfig_();
  182.     
  183.     if( gSysConfig.hasAppleEvents )
  184.         FnAE_InitAE();
  185.     
  186.     MenuBarInit_();
  187.     SetUpDragRect_();
  188.     
  189.     if( !gSysConfig.hasAppleEvents )
  190.         /* put OpenApp procedure here */
  191.         MyCreateWindow( BASE_RES, NIL_PTR, (WindowPtr)MOVE_TO_FRONT,
  192.             WIND_TOP, WIND_LEFT, WIND_OFFSET, N0_RESOURCE );
  193.  
  194.     MainLoop_();
  195. }
  196.  
  197.  
  198. /********** ToolBoxInit */
  199.  
  200. void ToolBoxInit_( void )
  201. /*
  202.     Standard initialization procedure per IM:Overview p4-75
  203. */
  204. {
  205.     MaxApplZone();
  206.     MoreMasters();
  207.     
  208.     InitGraf( &thePort );
  209.     InitFonts();
  210.     InitWindows();
  211.     InitMenus();
  212.     TEInit();
  213.     InitDialogs( NIL_PTR );
  214.     
  215.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );    
  216.     InitCursor();
  217. }
  218.  
  219.  
  220. /********** TrapAvailable */
  221.  
  222. Boolean TrapAvailable_( short tNumber, TrapType tType )
  223. {
  224.     return( NGetTrapAddress( tNumber, tType ) 
  225.     != GetTrapAddress( _Unimplemented ) );
  226. }
  227.  
  228.  
  229. /********** GetSysConfig */
  230.  
  231. static void GetSysConfig_( void )
  232. {   
  233.     OSErr        ignoreError;
  234.     long         tempLong;
  235.     SysEnvRec    environs;
  236.     short        myBit;
  237.  
  238.  
  239.     gAppResourceFile = CurResFile();  /* set app resource fork ID */
  240.  
  241.     /* Check to see if Gestalt Manager is supported */
  242.     gSysConfig.hasGestalt = TrapAvailable_( _Gestalt, ToolTrap );
  243.     if( !gSysConfig.hasGestalt )
  244.         /* Something has got to be wrong */
  245.         FnErr_DisplayStrID( BAD_SYS, TRUE );
  246.     else
  247.     {
  248.         /* Determine various system configuration checks */
  249.  
  250.         /* Check to see if WaitNextEvent is supported */
  251.         gSysConfig.hasWNE = TrapAvailable_( _WaitNextEvent, ToolTrap );
  252.         
  253.         /* Check for Color Capability */
  254.         ignoreError = Gestalt( gestaltQuickdrawVersion, &tempLong );
  255.         gSysConfig.hasColorQD = ( tempLong != gestaltOriginalQD );
  256.         
  257.         /* Check to see if AppleEvents are supported in OS */
  258.         gSysConfig.hasAppleEvents = ( Gestalt( gestaltAppleEventsAttr,
  259.             &tempLong ) == noErr );
  260.  
  261.         /* Check for Edition Manager */
  262.         gSysConfig.hasEditionMgr = ( Gestalt( gestaltEditionMgrAttr,
  263.             &tempLong ) == noErr );
  264.         if( gSysConfig.hasEditionMgr )
  265.             if( InitEditionPack() != noErr )
  266.                 gSysConfig.hasEditionMgr = false;
  267.                 
  268.         /* Check for Help Manager */
  269.         ignoreError = Gestalt( gestaltHelpMgrAttr, &tempLong );
  270.         myBit = gestaltHelpMgrPresent;
  271.         gSysConfig.hasHelpMgr =
  272.             BitTst( &tempLong, 31 - myBit );
  273.             
  274.         /* Determine and Check OS Version */
  275.         gSysConfig.sysVersion = 0.0;
  276.         ignoreError = Gestalt( gestaltSystemVersion, &tempLong );
  277.         gSysConfig.sysVersion = tempLong;
  278.         if( MIN_SYS_VERSION > gSysConfig.sysVersion )
  279.             FnErr_DisplayStrID( BAD_SYS, TRUE );
  280.     }
  281. }
  282.  
  283.  
  284. /********** MenuBarInit    */
  285.  
  286. void MenuBarInit_( void )
  287. {
  288.     Handle     myMenuBar;
  289.     MenuHandle helpMenu;
  290.     OSErr      err;
  291.  
  292.     myMenuBar = GetNewMBar( BASE_RES ) ;
  293.     if ( myMenuBar == NIL_PTR )
  294.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  295.     SetMenuBar( myMenuBar );
  296.     
  297.     if ( ( gAppleMenu = GetMHandle( APPLE_MENU ) ) == NIL_PTR )
  298.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  299.     if ( ( gFileMenu = GetMHandle( FILE_MENU ) ) == NIL_PTR )
  300.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  301.     if ( ( gEditMenu = GetMHandle( EDIT_MENU ) ) == NIL_PTR )
  302.         FnErr_DisplayStrID( N0_RESOURCE, TRUE );
  303.         
  304.     AddResMenu( gAppleMenu, 'DRVR' );
  305.     DrawMenuBar();
  306.     
  307.     if( gSysConfig.hasHelpMgr )
  308.     {
  309.         err = HMGetHelpMenuHandle( &helpMenu );
  310.         if( err == noErr )
  311.         {
  312.             if( helpMenu != nil )
  313.             {
  314.                  AppendMenu( helpMenu, HELP_MENU );   
  315.             }
  316.         }
  317.     }
  318. }
  319.  
  320.  
  321. /********** SetUpDragRect */
  322.  
  323. void SetUpDragRect_( void )
  324. {
  325.     gDragRect = screenBits.bounds;
  326.     gDragRect.left += DRAG_THRESHOLD;
  327.     gDragRect.right -= DRAG_THRESHOLD;
  328.     gDragRect.bottom -= DRAG_THRESHOLD;
  329. }
  330.  
  331.  
  332. /********** MainLoop */
  333.  
  334. void MainLoop_( void )
  335. {
  336.     gDone = FALSE;
  337.     while( gDone == FALSE )
  338.     {
  339.         HandleEvent_();
  340.     }
  341. }
  342.  
  343.  
  344. /********** HandleEvent */
  345.  
  346. void HandleEvent_( void )
  347. {
  348.     char    theChar;
  349.     GrafPtr oldPort;
  350.     Point   displayPoint;
  351.     struct  WindowData *wd;
  352.  
  353.     if ( gSysConfig.hasWNE )
  354.         WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP,
  355.             NIL_MOUSE_REGION );
  356.     else
  357.     {
  358.         SystemTask();
  359.         GetNextEvent( everyEvent, &gTheEvent );
  360.     }
  361.  
  362.     if((gWindow != 0) && (gWindow == FrontWindow()))
  363.     {
  364.         FnTE_MaintainCursor( gWindow, &gTEH );
  365.         TEIdle( gTEH );
  366.     }
  367.  
  368.     switch ( gTheEvent.what )
  369.     {
  370.         case mouseDown: 
  371.             HandleMouseDown_();
  372.             break;
  373.         case mouseUp:
  374.             // this application doesn't use mouseUp events
  375.             break;
  376.         case keyDown:
  377.         case autoKey:
  378.             theChar = gTheEvent.message & charCodeMask;
  379.             if (( gTheEvent.modifiers & cmdKey ) != 0)
  380.             {
  381.                 AdjustMenus_(); 
  382.                 HandleMenuChoice_( MenuKey( theChar ) );
  383.             }
  384.             else
  385.             {
  386.                 if((gWindow == FrontWindow()) && (gWindow != 0) )
  387.                 {
  388.                     wd = (struct WindowData *)GetWRefCon(gWindow);
  389.                     FnTE_DoKeyDown(
  390.                         theChar,
  391.                         &gTEH,
  392.                         &gSBH,
  393.                         (char *)&gDirty );
  394.                     wd->wDirty = gDirty;
  395.                 }
  396.             }
  397.             break;
  398.         case updateEvt:
  399.             if ( !IsDAWindow_( (WindowPtr)gTheEvent.message ) )
  400.             {
  401.                 /* Handle update event */
  402.                 MyDoUpdateWindow( (WindowPtr)gTheEvent.message );
  403.              }
  404.             break;
  405.         case diskEvt:
  406.             // most applications don't need to worry about diskEvt's
  407.              break;
  408.         case activateEvt:
  409.             if ( !IsDAWindow_( (WindowPtr)gTheEvent.message ) )
  410.             {
  411.                 if ( gTheEvent.modifiers & activeFlag )
  412.                 {
  413.                     /* Handle activate event. */
  414.                     MyDoActivateWindow((WindowPtr)gTheEvent.message);
  415.                 }
  416.                 else
  417.                 {
  418.                     /* Handle deactivate event. */
  419.                     MyDoDeactivateWindow((WindowPtr)gTheEvent.message);
  420.                 }
  421.             }
  422.             break;
  423.         case osEvt:
  424.             // this application doesn't support operating sys events
  425.             break;
  426.         case nullEvent:
  427.             // ignore
  428.             break;
  429.         case kHighLevelEvent:
  430.             FnAE_DoHighLevelEvent( &gTheEvent );
  431.             break;
  432.     }
  433. }
  434.  
  435.  
  436. /********** HandleMouseDown */
  437.  
  438. void HandleMouseDown_( void )
  439. {
  440.     WindowPtr         whichWindow;
  441.     WindowPtr         w;
  442.     short int         thePart;
  443.     long int          menuChoice;    
  444.     Point             theLocation;
  445.     GrafPtr           savePort;
  446.     struct WindowData *wd;
  447.     int               SBarWidth = 15;
  448.     Rect              oldHorizBar;
  449.     
  450.     thePart = FindWindow( gTheEvent.where, &whichWindow );
  451.     switch ( thePart )
  452.     {
  453.         case inMenuBar:
  454.             AdjustMenus_();
  455.             menuChoice = MenuSelect( gTheEvent.where );
  456.             HandleMenuChoice_( menuChoice );
  457.             break;
  458.         case inSysWindow: 
  459.             SystemClick( &gTheEvent, whichWindow );
  460.             break;
  461.         case inContent:
  462.             if( whichWindow != FrontWindow() )
  463.                 SelectWindow( whichWindow );
  464.             else if( !IsDAWindow_( (WindowPtr)gTheEvent.message ) )
  465.                 MyDoContent( whichWindow, &gTheEvent );
  466.             break;
  467.         case inDrag:
  468.             if( whichWindow != FrontWindow() )
  469.             {
  470.                 SelectWindow( whichWindow );
  471.                 DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  472.             }
  473.             else
  474.                 DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  475.             break;
  476.         case inGrow:
  477.             MyDoGrow( whichWindow, gTheEvent.where );
  478.             break;
  479.         case inGoAway:
  480.             theLocation = gTheEvent.where;
  481.             GlobalToLocal( &theLocation );
  482.             if( TrackGoAway( whichWindow, theLocation ) )
  483.                 DisposeWindow( whichWindow );
  484.             break;
  485.         case inZoomIn:
  486.         case inZoomOut:
  487.             // only supports zoomout
  488.             w = whichWindow;
  489.             if( w == gWindow )
  490.             {
  491.                 if( TrackBox( w,gTheEvent.where,thePart ) )
  492.                 {
  493.                     wd = (struct WindowData *)GetWRefCon(gWindow);
  494.                     //HideWindow( w );
  495.                     
  496.                     
  497.                         
  498.                     GetPort(&savePort);
  499.                     SetPort(w);
  500.                         EraseRect(&w->portRect);
  501.                         MoveWindow( w, WIND_LEFT, WIND_TOP, FALSE );
  502.                         SizeWindow( w, screenBits.bounds.right - 10, 
  503.                             screenBits.bounds.bottom - WIND_TOP - 5,
  504.                             FALSE );
  505.                         
  506.                         oldHorizBar = w->portRect;
  507.                         oldHorizBar.top = oldHorizBar.bottom - 
  508.                             (SBarWidth+1);
  509.                         //InvalRect(&w->portRect);
  510.                         //EraseRect(&oldHorizBar);
  511.                         
  512.                         MoveControl(wd->wSBH, w->portRect.right - 
  513.                             SBarWidth, w->portRect.top-1);
  514.                         SizeControl(wd->wSBH, SBarWidth+1,
  515.                             w->portRect.bottom - w->portRect.top -
  516.                             (SBarWidth-2));
  517.                         //ValidRect(&((**wd->wSBH).contrlRect));
  518.                         //BringToFront( w );
  519.                         //ShowWindow( w );
  520.                         
  521.                         // reset dest and view rects
  522.                         (**(wd->wTEH)).viewRect = w->portRect;
  523.                         (**(wd->wTEH)).viewRect.right -= SBarWidth;
  524.                         (**(wd->wTEH)).viewRect.bottom -= SBarWidth;
  525.                         InsetRect(
  526.                             &(**(wd->wTEH)).viewRect,
  527.                             3,   // inset pixels
  528.                             3 ); // inset pixels
  529.                         (**(wd->wTEH)).destRect = 
  530.                             (**(wd->wTEH)).viewRect;
  531.                             
  532.                         TECalText( wd->wTEH );
  533.                         FnTE_DetSBarIncr( &(wd->wTEH), &(wd->wSBH) );
  534.                         FnTE_AdjustText( &(wd->wTEH), &(wd->wSBH) );
  535.                         DrawGrowIcon( w );
  536.                         TEUpdate( &w->portRect, wd->wTEH );
  537.                     SetPort(savePort);
  538.                 }
  539.             }
  540.             break;
  541.     }
  542. }
  543.  
  544.  
  545. /********** AdjustMenus */
  546.  
  547. void AdjustMenus_( void )
  548. {
  549.     WindowPtr currentWindow;
  550.  
  551.     if (IsDAWindow_( FrontWindow() ) )
  552.     {   // DA Window
  553.         EnableItem( gEditMenu, UNDO );
  554.         EnableItem( gEditMenu, CUT );
  555.         EnableItem( gEditMenu, COPY );
  556.         EnableItem( gEditMenu, PASTE );
  557.         EnableItem( gEditMenu, CLEAR );
  558.         DisableItem( gEditMenu, SELECTALL );
  559.     }
  560.     else
  561.     {
  562.         if ( ( currentWindow = FrontWindow() ) == NIL_PTR )
  563.         {   // no window currently open
  564.             DisableItem( gFileMenu, CLOSE );
  565.             DisableItem( gFileMenu, SAVE );
  566.             DisableItem( gFileMenu, SAVEAS );
  567.             DisableItem( gFileMenu, PAGESETUP );
  568.             DisableItem( gFileMenu, PRINT );
  569.  
  570.             DisableItem( gEditMenu, UNDO );
  571.             DisableItem( gEditMenu, CUT );
  572.             DisableItem( gEditMenu, COPY );
  573.             DisableItem( gEditMenu, PASTE );
  574.             DisableItem( gEditMenu, CLEAR );
  575.             DisableItem( gEditMenu, SELECTALL );
  576.         }
  577.         else
  578.         {   // window open
  579.             EnableItem( gFileMenu, CLOSE );
  580.             if( gDirty ) // text updated since last saved
  581.             {
  582.                 if( *gFilename != 0 )
  583.                     EnableItem( gFileMenu, SAVE );
  584.             }
  585.             else
  586.                 DisableItem( gFileMenu, SAVE );
  587.             EnableItem( gFileMenu, SAVEAS );
  588.             EnableItem( gFileMenu, PAGESETUP );
  589.             EnableItem( gFileMenu, PRINT );
  590.  
  591.             DisableItem( gEditMenu, UNDO );
  592.             if((**gTEH).selStart==(**gTEH).selEnd)
  593.             {   // no text selected
  594.                 DisableItem( gEditMenu, CUT );
  595.                 DisableItem( gEditMenu, COPY );
  596.                 if(TEGetScrapLen() > 0)
  597.                     EnableItem( gEditMenu, PASTE );
  598.                 else
  599.                     DisableItem( gEditMenu, PASTE );
  600.                 DisableItem( gEditMenu, CLEAR );
  601.                 if((**gTEH).teLength > 0)
  602.                     EnableItem( gEditMenu, SELECTALL );
  603.                 else
  604.                     DisableItem( gEditMenu, SELECTALL );
  605.             }
  606.             else
  607.             {   // some text selected
  608.                 EnableItem( gEditMenu, CUT );
  609.                 EnableItem( gEditMenu, COPY );
  610.                 if(TEGetScrapLen() > 0)
  611.                     EnableItem( gEditMenu, PASTE );
  612.                 else
  613.                     DisableItem( gEditMenu, PASTE );
  614.                 EnableItem( gEditMenu, CLEAR );
  615.                 if((**gTEH).teLength > 0)
  616.                     EnableItem( gEditMenu, SELECTALL );
  617.                 else
  618.                     DisableItem( gEditMenu, SELECTALL );
  619.             }
  620.         }
  621.     }
  622. }
  623.  
  624.  
  625. /********** IsDAWindow */
  626.  
  627. int IsDAWindow_( WindowPtr whichWindow )
  628. {
  629.     if ( whichWindow == NIL_PTR )
  630.         return( FALSE );
  631.     else /* DA windows have negative windowKinds */
  632.         return( ( (WindowPeek)whichWindow )->windowKind < 0 );
  633. }
  634.  
  635.  
  636. /********** HandleMenuChoice */
  637.  
  638. void HandleMenuChoice_( long int menuChoice )
  639. {
  640.     int    theMenu;
  641.     int    theItem;
  642.     
  643.     if ( menuChoice != 0 )
  644.     {
  645.         theMenu = HiWord( menuChoice );
  646.         theItem = LoWord( menuChoice );
  647.         switch ( theMenu )
  648.         {
  649.             case APPLE_MENU :
  650.                 HandleAppleChoice_( theItem );
  651.                 break;
  652.             case FILE_MENU :
  653.                 HandleFileChoice_( theItem );
  654.                 break;
  655.             case EDIT_MENU :
  656.                 HandleEditChoice_( theItem );
  657.                 break;
  658.             case kHMHelpMenuID :
  659.                 MyHelpDialog();
  660.                 break;
  661.         }
  662.         HiliteMenu( 0 );
  663.     }
  664. }
  665.  
  666.  
  667. /********** HandleAppleChoice */
  668.  
  669. void HandleAppleChoice_( int theItem )
  670. {
  671.     Str255  accName;
  672.     int     accNumber;
  673.     
  674.     switch ( theItem )
  675.     {
  676.         case ABOUT : 
  677.             MyAboutDialog();
  678.             break;
  679.         default :
  680.             GetItem( gAppleMenu, theItem, accName );
  681.             accNumber = OpenDeskAcc( accName );
  682.             break;
  683.     }
  684. }
  685.  
  686.  
  687. /********** HandleFileChoice */
  688.  
  689. void HandleFileChoice_( int theItem )
  690. {
  691.     WindowPtr         whichWindow;
  692.     struct WindowData *wd;
  693.     Rect              tempViewRect;
  694.     Rect              tempDestRect;
  695.     Str255            fn;
  696.  
  697.     switch ( theItem )
  698.     {
  699.         case NEW:
  700.             MyCreateWindow( BASE_RES, NIL_PTR, (WindowPtr)MOVE_TO_FRONT,
  701.                 WIND_TOP, WIND_LEFT, WIND_OFFSET, N0_RESOURCE );
  702.             break;
  703.         case OPEN:
  704.             whichWindow = gWindow;
  705.             MyCreateHiddenWindow( BASE_RES, NIL_PTR,
  706.                 (WindowPtr)MOVE_TO_FRONT, WIND_TOP, WIND_LEFT,
  707.                 WIND_OFFSET, N0_RESOURCE );
  708.             if(FnIO_OpenTextFile(&gTEH,fn,&gVRef,&gRefNum))
  709.             {
  710.                 SetWTitle(gWindow,fn);
  711.                 wd = (struct WindowData *)GetWRefCon(gWindow);
  712.                 wd->wVRef = gVRef;
  713.                 wd->wRefNum = gRefNum;
  714.                 wd->wSaved = TRUE;
  715.                 FnIO_pStrCopy( fn, wd->wFilename );
  716.                 MySetGlobals( gWindow );
  717.                 TECalText( gTEH );
  718.                 FnTE_UpdateWindow( gWindow, &gTEH, TRUE );
  719.                 FnTE_DetSBarIncr( &gTEH, &gSBH );
  720.                 
  721.                 BringToFront( gWindow );
  722.                 ShowWindow( gWindow );
  723.                 DrawControls( gWindow );
  724.             }
  725.             else
  726.             {
  727.                 wd = (struct WindowData *)GetWRefCon(gWindow);
  728.                 TEDispose( gTEH );
  729.                 DisposeHandle((Handle)wd->wTEH);
  730.                 free(wd);
  731.                 DisposeWindow(gWindow);
  732.                 MySetGlobals( whichWindow );
  733.             }
  734.             break;
  735.         case CLOSE:
  736.             whichWindow = FrontWindow();
  737.             if((gWindow == whichWindow) && (whichWindow != NIL_PTR))
  738.             {
  739.                 wd = (struct WindowData *)GetWRefCon(gWindow);
  740.                 TEDispose( gTEH );
  741.                 DisposeHandle((Handle)wd->wTEH);
  742.                 free(wd);
  743.                 DisposeWindow(gWindow);
  744.                 gWindow = FrontWindow();
  745.                 if( gWindow != NIL_PTR )
  746.                 {
  747.                     MySetGlobals( whichWindow );
  748.                 }
  749.             }
  750.             break;
  751.         case SAVE:
  752.             wd = (struct WindowData *)GetWRefCon(gWindow);
  753.             if( gSaved )
  754.             { // save with existing filename
  755.                 if( FnIO_SaveTextFile(&(wd->wTEH),wd->wFilename,
  756.                     &(wd->wVRef),&(wd->wRefNum) ) )
  757.                 {
  758.                     wd->wDirty = FALSE;
  759.                     wd->wSaved = TRUE;
  760.                     MySetGlobals(gWindow);
  761.                 }
  762.             }
  763.             else
  764.             { // saveas
  765.                 if( FnIO_SaveAsTextFile(&(wd->wTEH),wd->wFilename,
  766.                     &(wd->wVRef),&(wd->wRefNum) ) )
  767.                 {
  768.                     wd->wDirty = FALSE;
  769.                     wd->wSaved = TRUE;
  770.                     SetWTitle( gWindow, wd->wFilename );
  771.                     MySetGlobals(gWindow);
  772.                 }
  773.             }
  774.             break;
  775.         case SAVEAS:
  776.             wd = (struct WindowData *)GetWRefCon(gWindow);
  777.             if( FnIO_SaveAsTextFile(&(wd->wTEH),wd->wFilename,
  778.                 &(wd->wVRef),&(wd->wRefNum) ) )
  779.             {
  780.                 wd->wDirty = FALSE;
  781.                 wd->wSaved = TRUE;
  782.                 SetWTitle( gWindow, wd->wFilename );
  783.                 MySetGlobals(gWindow);
  784.             }
  785.             break;
  786.         case PAGESETUP:
  787.             wd = (struct WindowData *)GetWRefCon(gWindow);
  788.             FnIO_PageSetup(&(wd->wPrintH));
  789.             MySetGlobals(gWindow);
  790.             break;
  791.         case PRINT:
  792.             FnIO_PrintTERecord(&gTEH,&gPrintH);
  793.             break;
  794.         case QUIT:
  795.             gDone = TRUE;
  796.             break;
  797.     }
  798. }
  799.  
  800.  
  801. /********** HandleEditChoice */
  802.  
  803. void HandleEditChoice_( int theItem )
  804. {
  805.     struct WindowData *wd;
  806.  
  807.     if( SystemEdit( theItem - 1 ) == 0 )
  808.     {
  809.         FnTE_DoEditMenu(
  810.             theItem,
  811.             &gTEH,
  812.             &gSBH,
  813.             (char *)&gDirty,
  814.             CUT,
  815.             COPY,
  816.             PASTE,
  817.             CLEAR,
  818.             SELECTALL );
  819.         wd = (struct WindowData *)GetWRefCon(gWindow);
  820.         wd->wDirty = gDirty;
  821.     }
  822. }
  823.  
  824.  
  825. /********** MySetGlobals */
  826.  
  827. void MySetGlobals( WindowPtr w )
  828. {
  829.     struct WindowData *wd;
  830.  
  831.     wd = (struct WindowData *)GetWRefCon(w);
  832.  
  833.     gWindow = w;
  834.     gTEH = wd->wTEH;
  835.     gSBH = wd->wSBH;
  836.     gDirty = wd->wDirty;
  837.     gSaved = wd->wSaved;
  838.     FnIO_pStrCopy( wd->wFilename, gFilename );
  839.     gVRef = wd->wVRef;
  840.     gRefNum = wd->wRefNum;
  841.     gPrintH = wd->wPrintH;
  842. }
  843.  
  844. // End of File