home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / Text Processing / HexEdit Release.sit / HexEdit Release / Project / Source / Menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-30  |  20.7 KB  |  764 lines  |  [TEXT/CWIE]

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is Copyright 1993 Jim Bumgardner.
  13.  * 
  14.  * The Initial Developer of the Original Code is Jim Bumgardner
  15.  * Portions created by Lane Roathe (LR) are
  16.  * Copyright (C) Copyright © 1996-2002.
  17.  * All Rights Reserved.
  18.  *
  19.  * Modified: $Date: 2004/09/10 04:05:47 $
  20.  * Revision: $Id: Menus.c,v 1.34 2004/09/10 04:05:47 raving Exp $
  21.  *
  22.  * Contributor(s):
  23.  *        Lane Roathe
  24.  *        Nick Shanks (NS)
  25.  *        Scott E. Lasley (SEL) 
  26.  */
  27.  
  28. // 05/10/01 - GAB: MPW environment support
  29. #ifdef __MPW__
  30. #include "MPWIncludes.h"
  31. #endif
  32.  
  33. #include "Prefs.h"
  34. #include "Menus.h"
  35. #include "EditWindow.h"
  36. #include "EditRoutines.h"
  37. #include "EditScrollbar.h"
  38. #include "HexCompare.h"
  39. #include "HexSearch.h"
  40. #include "AboutBox.h"
  41. #include "Utility.h"
  42.  
  43. static short _cmCheckedItem = 0;
  44.  
  45. // Menu Handles
  46. static MenuRef appleMenu, fileMenu, editMenu, findMenu, optionsMenu, colorMenu, windowMenu;
  47.  
  48. // Externals
  49. extern WindowRef CompWind1, CompWind2;
  50.  
  51. /*** SMART ENABLE MENU ITEM ***/
  52. static OSStatus _enableMenuItem( MenuRef menu, short item, short ok )
  53. {
  54. //    Code to simplify enabling/disabling menu items.
  55.     if( ok )
  56.         EnableMenuItem( menu, item );
  57.     else
  58.         DisableMenuItem( menu, item );
  59.  
  60.     if( item == 0 )
  61.         DrawMenuBar();
  62.  
  63.     return noErr;
  64. }
  65.  
  66. #if TARGET_API_MAC_CARBON // SEL: 1.7 - carbon session based printing
  67.  
  68. /*------------------------------------------------------------------------------
  69.     Allow the user define the page setup for printing to current printer
  70.  
  71.     Parameters:
  72.         printSession    -   current printing session
  73.         pageFormat      -   a PageFormat object addr
  74.  
  75.     Description:
  76.         If the caller passes in an empty PageFormat object, create a new one,
  77.         otherwise validate the one provided by the caller.
  78.         Invokes the Page Setup dialog and checks for Cancel.
  79.         Flattens the PageFormat object so it can be saved with the document.
  80.         Note that the PageFormat object is modified by this function.
  81.  
  82. ------------------------------------------------------------------------------*/
  83. static void _doPageSetupDialog(PMPageFormat* pageFormat)
  84. {
  85.     OSStatus            status;
  86.     Boolean             accepted;
  87.     PMPrintSession    printSession;
  88.  
  89.     status = PMCreateSession(&printSession);
  90.     if ( noErr != status )
  91.     {
  92.         PostPrintingErrors(status);
  93.         return;
  94.     }
  95.  
  96.     //  Set up a valid PageFormat object.
  97.     if (*pageFormat == kPMNoPageFormat)
  98.     {
  99.       status = PMCreatePageFormat(pageFormat);
  100.  
  101.       //  Note that PMPageFormat is not session-specific, but calling
  102.       //  PMSessionDefaultPageFormat assigns values specific to the printer
  103.       //  associated with the current printing session.
  104.         if ((status == noErr) && (*pageFormat != kPMNoPageFormat))
  105.             status = PMSessionDefaultPageFormat(printSession, *pageFormat);
  106.     }
  107.     else
  108.     {
  109.         status = PMSessionValidatePageFormat(printSession, *pageFormat, kPMDontWantBoolean);
  110.     }
  111.  
  112.     //  Display the Page Setup dialog.
  113.     if (status == noErr)
  114.     {
  115.         status = PMSessionPageSetupDialog(printSession, *pageFormat, &accepted);
  116.         if (!accepted)
  117.         {
  118.             status = kPMCancel; // user clicked Cancel button
  119.         }
  120.     }
  121.  
  122.     status = PMRelease(printSession);
  123.     return;
  124. }
  125.  
  126. /*------------------------------------------------------------------------------
  127.     Display error alert to end user
  128.  
  129.     Parameters:
  130.         status  -   error code
  131.  
  132.     Description:
  133.         This is where we post an alert to report any problem encountered by the Printing Manager.
  134.  
  135. ------------------------------------------------------------------------------*/
  136. void PostPrintingErrors( OSStatus status )
  137. {
  138.     ErrorAlert(ES_Caution, status == kPMNoDefaultPrinter ? errDefaultPrinter : errGenericPrinting, "Error = %d", status );
  139. }
  140.  
  141. #endif
  142.  
  143.  
  144. /* --------------------
  145.  Return resID of a color menu item
  146.     ( also, marks the menu item passed in as checked )
  147.  
  148.     ENTRY:    menu item
  149.      EXIT:    resID
  150. */
  151.  
  152. short GetColorMenuResID( short menuItem )
  153. {
  154.     Handle    h;
  155.     Str255    menuText;
  156.     short    resID;
  157.     ResType    resType;
  158.  
  159.     GetMenuItemText( colorMenu, menuItem, menuText );
  160.     h = GetNamedResource( 'HEct', menuText );
  161.     if( h )
  162.     {
  163.         GetResInfo( h, &resID, &resType, menuText );
  164.         return resID;
  165.     }
  166.     else
  167.         return( CM_StartingResourceID );
  168. }
  169.  
  170. /* --------------------
  171.     Return the ID of the window menu item with the passed title
  172.  
  173.     ENTRY:    ptr to pascal string to match
  174.      EXIT:    item ID, or 0 if not found
  175. */
  176.  
  177. short GetWindowMenuItemID( StringPtr title )
  178. {
  179.     short i;
  180.     Str255 menuItemTitle;
  181.  
  182.     i = CountMenuItems( windowMenu );
  183.     while( i )
  184.     {
  185.         GetMenuItemText( windowMenu, i, menuItemTitle );        // if you open more than one file with the same name (or edit the other fork)…
  186.         if( EqualPStrings( title, menuItemTitle ) )
  187.             break;
  188.         i--;
  189.     }
  190.     return( i );
  191. }
  192.  
  193.  
  194. /*** INITALISE MENUBAR ***/
  195. OSStatus InitMenubar( void )
  196. {
  197.     // set menu bar
  198.     Handle    menuBar;
  199.  
  200. #if TARGET_API_MAC_CARBON
  201.     long result;
  202.     if ((Gestalt(gestaltMenuMgrAttr, &result) == noErr) && (result & gestaltMenuMgrAquaLayoutMask))
  203.         menuBar = GetNewMBar( kMenuXBaseID);
  204.     else
  205. #endif
  206.         menuBar = GetNewMBar( kMenuBaseID );
  207.     SetMenuBar( menuBar );
  208.  
  209.     // get menu references
  210.     appleMenu    = GetMenuRef( kAppleMenu );
  211.     fileMenu    = GetMenuRef( kFileMenu );
  212.     editMenu    = GetMenuRef( kEditMenu );
  213.     findMenu    = GetMenuRef( kFindMenu );
  214.     optionsMenu    = GetMenuRef( kOptionsMenu );
  215.     colorMenu    = GetMenuRef( kColorMenu );
  216.     windowMenu    = GetMenuRef( kWindowMenu );
  217.  
  218. #if !TARGET_API_MAC_CARBON
  219.     AppendResMenu( appleMenu, 'DRVR' );
  220. #endif
  221.     AppendResMenu( colorMenu, 'HEct' );    // LR: add color scheme menu
  222.  
  223.     DrawMenuBar();
  224.     return noErr;
  225. }
  226.  
  227. /*** ADJUST MENUS ***/
  228. OSStatus AdjustMenus( void )
  229. {
  230.     register        WindowRef theWin;
  231.     short             windowKind;
  232.     Boolean         isDA, isObjectWin, selection, scrapExists, undoExists, isGotoWin, isFindWin;
  233.     EditWindowPtr    dWin = NULL;
  234.     Str31            menuStr;
  235.     short             i;
  236.     long            scrapSize;    // LR: v1.6.5
  237.     Str255            frontWindowName, menuItemTitle;
  238.     Boolean            namesMatch;
  239.  
  240.     theWin = FrontNonFloatingWindow();
  241.     if( theWin )
  242.     {
  243.         isGotoWin = (g.gotoDlg && theWin == GetDialogWindow( g.gotoDlg ));        //LR: 1.7 - don't get window info on NULL!
  244.         isFindWin = (g.searchDlg && theWin == GetDialogWindow( g.searchDlg ));
  245.  
  246.         windowKind = GetWindowKind( theWin );
  247.         isDA = ( windowKind < 0 );
  248.         isObjectWin = GetWindowKind( theWin ) == kHexEditWindowTag;
  249.         if( isObjectWin )
  250.         {
  251.             dWin = (EditWindowPtr)GetWRefCon( theWin );    //LR: 1.66 - don't set unless an edit window!
  252.             selection = dWin->endSel > dWin->startSel;
  253.         }
  254.         else
  255.         {
  256.             selection = (isGotoWin || isFindWin);
  257.         }
  258.     }
  259.     else    // LR: v1.6.5 if no window is visible, then nothing is true!
  260.     {
  261.         isGotoWin = isFindWin = isObjectWin = isDA = selection = 0;
  262.     }
  263.  
  264.     // LR: v1.6.5 - rewrite of scrap check
  265.     if( isObjectWin || isFindWin || isGotoWin )
  266.     {
  267. #if TARGET_API_MAC_CARBON
  268.         ScrapFlavorFlags flavorFlags;
  269.         ScrapRef scrapRef;
  270.         OSErr anErr;
  271.  
  272.         anErr = GetCurrentScrap( &scrapRef );
  273.         if( !anErr )
  274.             anErr = GetScrapFlavorFlags( scrapRef, kScrapFlavorTypeText, &flavorFlags );    // non-blocking check for scrap data
  275.         if( !anErr )
  276.             anErr = GetScrapFlavorSize( scrapRef, kScrapFlavorTypeText, &scrapSize );        // blocking call to get size
  277. #else
  278.         long offset;
  279.  
  280.         scrapSize = GetScrap( NULL, 'TEXT', &offset );
  281. #endif
  282.         scrapExists = scrapSize > 0;
  283.     }
  284.     else
  285.         scrapExists = false;
  286.  
  287.     undoExists = (isObjectWin && gUndo.type != 0 && gUndo.theWin == dWin);    // check for NULL gUndo!
  288.     
  289. // LR: - enable file menu items during search, via Aaron D.
  290. // LR:    _enableMenuItem( fileMenu, FM_New, g.searchDlg == NULL );
  291. // LR:    _enableMenuItem( fileMenu, FM_Open, g.searchDlg == NULL );
  292.  
  293. // LR: 1.65 moved print names to string for localization
  294.     GetIndString( menuStr, strPrint, (isObjectWin && dWin->startSel < dWin->endSel) ? 2 : 1 );
  295.  
  296.     SetMenuItemText( fileMenu, FM_Print, menuStr );
  297.  
  298. //LR 188 -- page setup should always be enabled
  299. //    _enableMenuItem( fileMenu, FM_PageSetup, isObjectWin );    //SEL: 1.7 - enabled for carbon
  300.     _enableMenuItem( fileMenu, FM_Print, isObjectWin );
  301.  
  302.     _enableMenuItem( fileMenu, FM_OtherFork, isObjectWin );
  303.     _enableMenuItem( fileMenu, FM_Close, isDA || isObjectWin || isFindWin || isGotoWin );    // LR: v1.6.5 rewrite via Max Horn
  304.     _enableMenuItem( fileMenu, FM_Save, isObjectWin && dWin->dirtyFlag );
  305.     _enableMenuItem( fileMenu, FM_SaveAs, isObjectWin );
  306.     _enableMenuItem( fileMenu, FM_Revert, isObjectWin && dWin->refNum && dWin->dirtyFlag );
  307.  
  308.     _enableMenuItem( editMenu, 0, theWin != NULL );
  309.     _enableMenuItem( editMenu, EM_Undo, isDA || undoExists );
  310.     _enableMenuItem( editMenu, EM_Cut,  isDA || (selection && (!gPrefs.overwrite || (gPrefs.overwrite && !gPrefs.nonDestructive))) );
  311.     _enableMenuItem( editMenu, EM_Copy, isDA || selection );
  312.     _enableMenuItem( editMenu, EM_Paste, isDA || (scrapExists && (!gPrefs.overwrite || (gPrefs.overwrite && !gPrefs.nonDestructive))) );
  313.     _enableMenuItem( editMenu, EM_Clear, isDA || selection );
  314.  
  315.     _enableMenuItem( editMenu, EM_SelectAll, isDA || isObjectWin || isFindWin || isGotoWin );
  316.  
  317.     _enableMenuItem( findMenu, 0, isObjectWin || isFindWin || isGotoWin );
  318. /* 1.65
  319.     _enableMenuItem( findMenu, SM_Find, isObjectWin );
  320.     _enableMenuItem( findMenu, SM_GotoAddress, isObjectWin );
  321. */
  322.     _enableMenuItem( findMenu, SM_FindForward, selection || (isObjectWin && dWin->fileSize && g.searchBuffer[0]) );    //LR 1.72 -- only enable w/something to search :)
  323.     _enableMenuItem( findMenu, SM_FindBackward, selection || (isObjectWin && dWin->fileSize && g.searchBuffer[0]) );
  324.     _enableMenuItem( findMenu, SM_Replace, selection || (isObjectWin && dWin->fileSize && g.searchBuffer[0]) );
  325.  
  326.     _enableMenuItem( optionsMenu, OM_NonDestructive, gPrefs.overwrite );    //LR 1.74 -- only available in overwrite mode
  327.  
  328.     CheckMenuItem( optionsMenu, OM_HiAscii, gPrefs.asciiMode );
  329.     CheckMenuItem( optionsMenu, OM_DecimalAddr, gPrefs.decimalAddr );
  330.     CheckMenuItem( optionsMenu, OM_Backups, gPrefs.backupFlag );
  331.     CheckMenuItem( optionsMenu, OM_WinSize, gPrefs.constrainSize );
  332.     CheckMenuItem( optionsMenu, OM_Overwrite, gPrefs.overwrite );
  333.     CheckMenuItem( optionsMenu, OM_NonDestructive, !gPrefs.nonDestructive );    //LR 190 -- REVERSE (updated text, not code)
  334.     CheckMenuItem( optionsMenu, OM_MoveOnlyPaging, gPrefs.moveOnlyPaging );    //LR 180 -- optional move only paging
  335.     CheckMenuItem( optionsMenu, OM_Unformatted, !gPrefs.formatCopies );
  336.     CheckMenuItem( optionsMenu, OM_VertBars, gPrefs.vertBars );
  337.  
  338.     // LR: v1.6.5 Lots of re-writing on handling the color scheme menu
  339. #if !TARGET_API_MAC_CARBON
  340.     // no color usage if not displayable!
  341.     if( !g.colorQDFlag )
  342.         gPrefs.useColor = false;
  343. #endif
  344.  
  345. // LR: v1.6.5    CheckMenuItem( gColorMenu, CM_UseColor, gPrefs.useColor );    // allow turning on even if not usable
  346. // LR: v1.6.5 Try to show status of color in new windows to help "intuitive" nature of menu
  347.     GetIndString( menuStr, strColor, (gPrefs.useColor) ? 2 : 1 );
  348.     SetMenuItemText( colorMenu, CM_UseColor, menuStr );
  349.  
  350.     //LR 181 -- show the current window color (or default if no windows)
  351.     if( _cmCheckedItem )
  352.         CheckMenuItem( colorMenu, _cmCheckedItem, false );
  353.     _cmCheckedItem = isObjectWin ? dWin->csMenuID : gPrefs.csMenuID;
  354.     CheckMenuItem( colorMenu, _cmCheckedItem, true );
  355.  
  356.     selection = gPrefs.useColor;    //LR 190 -- && isObjectWin && dWin->csResID > 0;
  357.     i = CountMenuItems( colorMenu );
  358.     do
  359.     {
  360.         _enableMenuItem( colorMenu, i, selection );    // LR: v1.6.5 only enable for color windows
  361.     } while( --i > 2 );
  362.     
  363.     // NS: v1.6.6 checkmark front window in window menu
  364.     if( theWin )
  365.         GetWTitle( theWin, frontWindowName );    //LR: 1.66 - don't use NULL window!
  366.     else
  367.         frontWindowName[0] = 0;
  368.  
  369.     i = CountMenuItems( windowMenu );
  370.     _enableMenuItem(windowMenu, 0, i != 0);    // dim out Window menu if no windows up.
  371.     while( i )
  372.     {
  373.         GetMenuItemText( windowMenu, i, menuItemTitle );        // if you open more than one file with the same name (or edit the other fork)…
  374.         namesMatch = EqualPStrings( frontWindowName, menuItemTitle );    // …you will have multiple items in the menu with the same text, and all will be checkmarked
  375.         CheckMenuItem( windowMenu, i, namesMatch );
  376.  
  377.         i--;
  378.     }
  379.  
  380.     return( noErr );
  381. }
  382.  
  383. //     Handle the menu selection. mSelect is what MenuSelect() and
  384. //     MenuKey() return: the high word is the menu ID, the low word
  385. //     is the menu item
  386.  
  387. /*** HANDLE MENU ***/
  388. // LR 1.66 -- complete rewrite (basically) of this entire routine...it was UGLY!
  389. OSStatus HandleMenu( long mSelect, short modifiers )
  390. {
  391.     short            menuID = HiWord( mSelect );
  392.     short            menuItem = LoWord( mSelect );
  393.     short         colorResID;
  394.     WindowRef        frontWindow;
  395.     DialogPtr        dlgRef = NULL;
  396.     EditWindowPtr    dWin = NULL;
  397.  
  398.     Str255            currentWindowName, newFrontWindowName;        // NS: v1.6.6, for window menu
  399.     WindowRef        currentWindow;                                // NS:            this too
  400.     
  401.     // Predetermine what type of window we have to work with
  402.     frontWindow = FrontNonFloatingWindow();
  403.     if( frontWindow )
  404.     {
  405.         DialogPtr dlg = GetDialogFromWindow( frontWindow );
  406.  
  407.         if( kHexEditWindowTag == GetWindowKind( frontWindow ) )
  408.             dWin = (EditWindowPtr) GetWRefCon( frontWindow );
  409.         else if( g.gotoDlg == dlg || g.searchDlg == dlg )
  410.             dlgRef = dlg;
  411.     }
  412.  
  413.     switch( menuID )
  414.     {
  415.         case kAppleMenu:
  416.             if( menuItem == AM_About )
  417.                 HexEditAboutBox();
  418. #if !TARGET_API_MAC_CARBON
  419.             else
  420.             {
  421.                 GrafPtr savePort;
  422.                 Str255 name;
  423.  
  424.                 GetPort( &savePort );
  425.                 GetMenuItemText( appleMenu, menuItem, name );
  426.                 OpenDeskAcc( name );
  427.                 SetPort( savePort );
  428.             }
  429. #endif
  430.             break;
  431.         
  432.     case kFileMenu:
  433.         switch( menuItem )
  434.         {
  435.         case FM_New:
  436.             gPrefs.overwrite = false;  //LR 190 -- overwrite mode makes no sense in a new document
  437.             NewEditWindow();
  438.             break;
  439.  
  440.         case FM_Open:
  441.             AskEditWindow( kWindowNormal );
  442.             break;
  443.  
  444.         case FM_OtherFork:    // LR: I want to see both!
  445.             if( dWin )
  446.             {
  447.                 short fork;
  448. //LR 180                EditWindowPtr ewin;
  449.  
  450.                 if( dWin->fork == FT_Data )
  451.                     fork = FT_Resource;
  452.                 else
  453.                     fork = FT_Data;
  454.  
  455. /*LR 180 -- OpenEditWindow checks for this
  456.                 if( NULL != (ewin = LocateEditWindow( &dWin->fsSpec, fork )) )    // LR: 1.7 - boolean typecast causes failure!
  457.                 {
  458.                     SelectWindow( ewin->oWin.theWin );    // just select existing theWin
  459.                 }
  460.                 else    // try to open other fork in new theWin!
  461. */                {
  462.                     g.forkMode = fork;
  463.                     OpenEditWindow( &dWin->fsSpec, kWindowNormal, true );
  464.                 }
  465.             }
  466.             break;
  467.  
  468.         case FM_CompareFiles:        //LR 180 -- now pass in modifiers to allow select override
  469.             if( GetCompareFiles( modifiers ) )
  470.                 DoComparison();
  471.             break;
  472.  
  473.         //LR: 1.66 - NOTE: dWin == NULL == frontWindow!
  474.         case FM_Save:
  475.             if( dWin && dWin->oWin.Save )
  476.                 dWin->oWin.Save( frontWindow );
  477.             break;
  478.  
  479.         case FM_SaveAs:
  480.             if( dWin && dWin->oWin.SaveAs )
  481.                 dWin->oWin.SaveAs( frontWindow );
  482.             break;
  483.  
  484.         case FM_Revert:
  485.             if( dWin && dWin->oWin.Revert )    //LR 1.72 -- check before reverting (could be dangerous!)
  486.             {
  487.                 ParamText( dWin->fsSpec.name, NULL, NULL, NULL );
  488.                 switch( CautionAlert( alertRevert, NULL ) )
  489.                 {
  490.                     case ok:
  491.                         dWin->oWin.Revert( frontWindow );
  492.                         break;
  493.                 }
  494.             }
  495.             break;
  496.  
  497.         case FM_Close:
  498.             if( dWin )
  499.                 CloseEditWindow( frontWindow );
  500.             else if( dlgRef )
  501.             {
  502.                 HideWindow( frontWindow );    //LR: 1.7 -- no need.GetDialogWindow( dlgRef ) );
  503.             }
  504.             break;
  505.  
  506.         case FM_Quit:
  507.             if( CloseAllEditWindows() )
  508.                 g.quitFlag = true;
  509.             break;
  510.  
  511.         case FM_PageSetup:
  512. #if TARGET_API_MAC_CARBON  // sel - carbon session based printing
  513.             _doPageSetupDialog(&g.pageFormat);
  514. #else
  515.             PrOpen();
  516.             PrStlDialog( g.HPrint );
  517.             PrClose();
  518. #endif
  519.             break;
  520.  
  521.         case FM_Print:
  522.             if( dWin )
  523.                 PrintWindow( dWin );
  524.             break;
  525.         }
  526.         break;
  527.  
  528.     case kEditMenu:
  529. #if !TARGET_API_MAC_CARBON
  530.         if( !SystemEdit( menuItem -1 ) )
  531. #endif
  532.         {
  533.             if( dWin ) switch( menuItem ) 
  534.             {
  535.                 case EM_Undo:
  536.                     UndoOperation();
  537.                     break;
  538.  
  539.                 case EM_Cut:
  540.                     CutSelection( dWin );                
  541.                     break;
  542.  
  543.                 case EM_Copy:
  544.                     CopySelection( dWin );    
  545.                     break;
  546.  
  547.                 case EM_Paste:
  548.                     PasteSelection( dWin );
  549.                     break;
  550.  
  551.                 case EM_Clear:
  552.                     ClearSelection( dWin );            
  553.                     break;
  554.  
  555.                 case EM_SelectAll:
  556.                     dWin->startSel = 0;
  557.                     dWin->endSel = dWin->fileSize;
  558.                     UpdateOnscreen( dWin->oWin.theWin );
  559.                     break;
  560.             }
  561.             else if( dlgRef ) switch( menuItem )
  562.             {
  563.                 case EM_Cut:
  564.                     DialogCut( dlgRef );
  565.                     TEToScrap();
  566.                     break;
  567.  
  568.                 case EM_Copy:
  569.                     DialogCopy( dlgRef );
  570.                     TEToScrap();
  571.                     break;
  572.  
  573.                 case EM_Paste:
  574.                     TEFromScrap();
  575.                     DialogPaste( dlgRef );
  576.                     break;
  577.  
  578.                 case EM_Clear:
  579.                     DialogDelete( dlgRef );
  580.                     break;
  581.  
  582.                 case EM_SelectAll:
  583.                     break;
  584.             }
  585.         }
  586.         break;
  587.  
  588.     case kFindMenu:
  589.         switch ( menuItem )
  590.         {
  591.             case SM_Find:
  592. openfind:
  593.                 OpenSearchDialog();
  594.                 break;
  595.  
  596.             case SM_FindForward:
  597.                 gPrefs.searchForward = true;
  598.                 PerformTextSearch( dWin, kSearchUpdateUI );  //LR 190 -- if dWin is NULL will operate on first edit window, if any (allows search in find dialog)
  599.                 break;
  600.  
  601.             case SM_FindBackward:
  602.                 gPrefs.searchForward = false;
  603.                 PerformTextSearch( dWin, kSearchUpdateUI );  //LR 190 -- if dWin is NULL will operate on first edit window
  604.                 break;
  605.  
  606.             case SM_Replace:    //LR 190 -- add replace & find next (must have a window with selection to start!)
  607.                 if( !dWin )
  608.                     dWin = FindFirstEditWindow(); // allow this to work in find dialog, etc.
  609.  
  610.                 if( dWin  && dWin->startSel != dWin->endSel )
  611.                 {
  612.                     EditChunk    **replaceChunk;
  613.  
  614.                     if( !g.searchBuffer[0] )    // if nothing to find open dialog
  615.                         goto openfind;
  616.  
  617.                     replaceChunk = NewChunk( g.replaceText[0], 0, 0, CT_Unwritten );
  618.                     if( replaceChunk )
  619.                     {
  620.                         // Copy replacement text to chunk buffer
  621.                         BlockMoveData( g.replaceText+1, *(*replaceChunk)->data, g.replaceText[0] );
  622.  
  623.                         // Do the replacement (with undo)
  624.                         g.replaceAll = false;
  625.                         RememberOperation( dWin, EO_Paste, &gUndo );
  626.                         PasteOperation( dWin, replaceChunk );
  627.  
  628.                         // We're done with the chunk now
  629.                         DisposeChunk( NULL, replaceChunk );
  630.                     }
  631.  
  632.                     // Then try to find the next occurance (in LAST direction searched!) and display it
  633.                     if( !PerformTextSearch( dWin, kSearchUpdateUI ) )
  634.                         ScrollToSelection( dWin, dWin->startSel, true );
  635.                 }
  636.                 break;
  637.  
  638.             case SM_GotoAddress:
  639.                 OpenGotoAddress();
  640.                 break;
  641.         }
  642.         break;
  643.  
  644.     case kOptionsMenu:
  645.         switch ( menuItem )
  646.         {
  647.             case OM_HiAscii:
  648.                 gPrefs.asciiMode = !gPrefs.asciiMode;
  649.                 if( gPrefs.asciiMode )    g.highChar = 0xFF;
  650.                 else                    g.highChar = 0x7F;
  651.                 UpdateEditWindows();
  652.                 break;
  653.  
  654.             case OM_DecimalAddr:
  655.                 gPrefs.decimalAddr = !gPrefs.decimalAddr;
  656.                 UpdateEditWindows();
  657.                 break;
  658.  
  659.             case OM_Backups:
  660.                 gPrefs.backupFlag = !gPrefs.backupFlag;
  661.                 break;
  662.  
  663.             case OM_WinSize:
  664.                 gPrefs.constrainSize = !gPrefs.constrainSize;
  665.                 break;
  666.  
  667.             case OM_Overwrite:
  668.                 gPrefs.overwrite = !gPrefs.overwrite;
  669.                 break;
  670.  
  671.             case OM_NonDestructive:
  672.                 gPrefs.nonDestructive = !gPrefs.nonDestructive;
  673.                 break;
  674.  
  675.             case OM_MoveOnlyPaging:
  676.                 gPrefs.moveOnlyPaging = !gPrefs.moveOnlyPaging;
  677.                 break;
  678.  
  679.             case OM_Unformatted:
  680.                 gPrefs.formatCopies = !gPrefs.formatCopies;
  681.                 break;
  682.  
  683.             case OM_VertBars:
  684.                 gPrefs.vertBars = !gPrefs.vertBars;
  685.                 UpdateEditWindows();
  686.                 break;
  687.  
  688.             case OM_ComparePref:    // LR: compare options
  689.                 ComparisonPreferences();
  690.                 break;
  691.         }
  692.         break;
  693.  
  694.     // LR: Add color scheme menu
  695.     case kColorMenu:
  696.         colorResID = GetColorMenuResID( menuItem );
  697.  
  698.         if( menuItem == CM_UseColor )
  699.         {
  700.             gPrefs.useColor = !gPrefs.useColor;        // toggle color usage
  701.         }
  702.         else if( dWin && dWin->csResID > 0 )        // can't color B&W windows!
  703.         {
  704.             if( _cmCheckedItem )
  705.                 CheckMenuItem( colorMenu, _cmCheckedItem, false );
  706.  
  707.             if( (modifiers & optionKey) )    // option down == change all windows (set default color)
  708.             {
  709.                 EditWindowPtr eWin = FindFirstEditWindow();
  710.  
  711.                 while( eWin )
  712.                 {
  713.                     if( GetWindowKind( eWin->oWin.theWin ) == kHexEditWindowTag )
  714.                     {
  715.                         eWin->csResID = colorResID;
  716.                         eWin->csMenuID = menuItem;    //LR 181 -- for menu tagging
  717.                     }
  718.  
  719.                     eWin = FindNextEditWindow( eWin );
  720.                 }
  721.                 goto savepref;
  722.             }
  723.             else    //LR 181 -- default is (back) to changing color of a single window!
  724.             {
  725.                 if( GetWindowKind( dWin->oWin.theWin ) == kHexEditWindowTag )
  726.                 {
  727.                     dWin->csResID = colorResID;
  728.                     dWin->csMenuID = menuItem;    //LR 181 -- for menu tagging
  729.                 }
  730.             }
  731.         }
  732.         else
  733.         {
  734. savepref:    //LR 190 -- no window open == set preferred color
  735.             gPrefs.csResID = colorResID;    //LR 180 -- save prefs when changing all
  736.             gPrefs.csMenuID = menuItem;
  737.         }
  738.  
  739.         UpdateEditWindows();
  740.         break;
  741.  
  742.     // LR : 1.7 - rewrite with bug checking (could crash accessing NULL window)
  743.     case kWindowMenu:
  744.         GetMenuItemText( windowMenu, menuItem, newFrontWindowName );
  745.         currentWindow = FrontNonFloatingWindow();
  746.         while( currentWindow )
  747.         {
  748.             GetWTitle( currentWindow, currentWindowName );
  749.             if( EqualPStrings( currentWindowName, newFrontWindowName ) )
  750.             {
  751.                 SelectWindow( currentWindow );
  752.                 break;
  753.             }
  754.             currentWindow = GetNextWindow( currentWindow );
  755.         }
  756.         break;
  757.     }
  758.  
  759.     HiliteMenu( 0 );
  760.     AdjustMenus();
  761.  
  762.     return( noErr );
  763. }
  764.