home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Ph 1.1.1 / PhClient / ph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-04  |  23.6 KB  |  942 lines  |  [TEXT/MPS ]

  1. /*_____________________________________________________________________
  2.  
  3.       Ph - Mac Ph Client.
  4.     
  5.     John Norstad
  6.     Academic Computing and Network Services
  7.     Northwestern University
  8.     j-norstad@nwu.edu
  9.     
  10.     Copyright © Northwestern University, 1991, 1992.
  11.  
  12. Version 1.2b1. 11/4/92.
  13.  
  14. - (Pete Resnick). The site list popup menu sometimes extends beyond the
  15. left edge of query windows. Fixed.
  16. - (Pete Resnick). The Cancel button did not properly cancel in the
  17. change default server window. Fixed.
  18.  
  19. ____________________________________________________________________*/
  20.  
  21. #pragma load "precompile"
  22. #include "rez.h"
  23. #include "utl.h"
  24. #include "mtcp.h"
  25. #include "glob.h"
  26. #include "edit.h"
  27. #include "abou.h"
  28. #include "oop.h"
  29. #include "query.h"
  30. #include "help.h"
  31. #include "serv.h"
  32. #include "fsu.h"
  33. #include "site.h"
  34.  
  35. /*______________________________________________________________________
  36.  
  37.     Global Variables.
  38. _____________________________________________________________________*/
  39.  
  40. static Ptr        ReserveBlock = nil;            /* reserve memory block */
  41. static Ptr        LastGaspBlock = nil;            /* last gasp memory block */
  42.  
  43. /*______________________________________________________________________
  44.  
  45.     GrowZone - Grow Zone Procedure.
  46.     
  47.     Entry:    cbNeeded = number of bytes needed.
  48. _____________________________________________________________________*/
  49.  
  50.  
  51. static pascal long GrowZone (Size cbNeeded)
  52.  
  53. {
  54. #pragma unused (cbNeeded)
  55.  
  56.     if (ReserveBlock) {
  57.         DisposPtr(ReserveBlock);
  58.         ReserveBlock = nil;
  59.         glob_Error(stringsID, memoryLow, nil);
  60.         return 1;
  61.     } else {
  62.         if (LastGaspBlock) {
  63.             DisposPtr(LastGaspBlock);
  64.             LastGaspBlock = nil;
  65.         }
  66.         glob_BringToFront();
  67.         utl_StopAlert(noMemID, nil, 0);
  68.         ExitToShell();
  69.         return 0;
  70.     }
  71. }
  72.  
  73. /*_____________________________________________________________________
  74.  
  75.     BuildMenu - Build a Menu from an Item List.
  76.     
  77.     Entry:        h = handle to item list.
  78.                     menu = handle to menu.
  79. _____________________________________________________________________*/
  80.  
  81. static void BuildMenu (Handle h, MenuHandle menu)
  82.  
  83. {
  84.     short            item;                /* item number */
  85.     char            *p;                /* pointer into item list */
  86.     char            *pEnd;            /* pointer to end of item list */
  87.     
  88.     HLock(h);
  89.     p = *h;
  90.     pEnd = p + GetHandleSize(h);
  91.     item = CountMItems(menu);
  92.     while (p < pEnd) {
  93.         if (*p) {
  94.             item++;
  95.             AppendMenu(menu, "\p ");
  96.             SetItem(menu, item, p);
  97.         }
  98.         p += *p+1;
  99.     }
  100.     HUnlock(h);
  101.     CalcMenuSize(menu);
  102. }
  103.  
  104. /*_____________________________________________________________________
  105.  
  106.     ReadPrefs - Read Prefs File.
  107. _____________________________________________________________________*/
  108.  
  109. #pragma segment init
  110.  
  111. static void ReadPrefs (void)
  112.  
  113. {
  114.     FSSpec                fSpec;            /* prefs file spec */
  115.     short                    refNum;            /* resource file refnum */
  116.     Boolean                error;            /* true if error encountered */
  117.     OSErr                    rCode;            /* error code */
  118.     Handle                h;                    /* handle to resource */
  119.     short                    size;                /* size of resource */
  120.     WindState            *pState;            /* pointer to window state */
  121.     short                    i;                    /* loop index */
  122.     Str255                newDefaultServer;    /* new default server */
  123.     Boolean                isFolderAlias;    /* true if folder alias */
  124.     Boolean                isAlias;            /* true if alias */
  125.     
  126.     /* Build the prefs file spec. */
  127.     
  128.     GetIndString(fSpec.name, stringsID, prefsFileName);
  129.     error = fsu_FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  130.         &fSpec.vRefNum, &fSpec.parID);
  131.         
  132.     /* Resolve aliases. */
  133.     
  134.     if (utl_GestaltFlag(gestaltAliasMgrAttr, gestaltAliasMgrPresent)) {
  135.         rCode = ResolveAliasFile(&fSpec, true, &isFolderAlias, &isAlias);
  136.         error = rCode || isFolderAlias;
  137.     }
  138.     
  139.     /* Open the prefs resource file with read-only permission. */
  140.     
  141.     refNum = -1;
  142.     if (!error) {
  143.         error = fsu_FSpOpenResFile(&fSpec, fsRdPerm, &refNum);
  144.     };
  145.     
  146.     /* Read the WPOS resource. */
  147.     
  148.     if (!error) {
  149.         h = GetResource('WPOS', 128);
  150.         error = !h || GetHandleSize(h) != (6 + 3*numPosSave)*sizeof(WindState);
  151.     }
  152.     if (!error) {
  153.         pState = (WindState*)*h;
  154.         TranState = *pState++;
  155.         NewState = *pState++;
  156.         OpenState = *pState++;
  157.         PswdState = *pState++;
  158.         LoginState = *pState++;
  159.         SiteState = *pState++;
  160.         memcpy(QueryStates, pState, numPosSave*sizeof(WindState));
  161.         pState += numPosSave;
  162.         memcpy(EditStates, pState, numPosSave*sizeof(WindState));
  163.         pState += numPosSave;
  164.         memcpy(HelpStates, pState, numPosSave*sizeof(WindState));
  165.     }
  166.     
  167.     /* Read the MISC resource. */
  168.     
  169.     if (!error) {
  170.         h = GetResource('MISC', 128);
  171.         error = !h || GetHandleSize(h) != 11;
  172.     }
  173.     if (!error) {
  174.         NumSites = *(short*)(*h);
  175.         LastHelpUpdate = *(unsigned long*)(*h+2);
  176.         LastSiteUpdate = *(unsigned long*)(*h+6);
  177.         HelpNative = *(Boolean*)(*h+10);
  178.     }
  179.     
  180.     /* Read the DSRV resurce. */
  181.     
  182.     if (!error) {
  183.         h = GetResource('DSRV', 128);
  184.         error = !h;
  185.     }
  186.     if (!error) {
  187.         utl_CopyPString(DefaultServer, *h);
  188.     }
  189.     
  190.     /* Read the SITE resource and build the site popup menu. */
  191.     
  192.     if (!error) {
  193.         h = GetResource('SITE', 128);
  194.         error = !h;
  195.     }
  196.     if (!error) {
  197.         size = GetHandleSize(h);
  198.         if (size) {
  199.             SiteMenu = NewMenu(popupID, "\p");
  200.             BuildMenu(h, SiteMenu);
  201.         } else {
  202.             SiteMenu = nil;
  203.         }
  204.     }
  205.     
  206.     /* Read the SERV and DOMA resources. */
  207.     
  208.     if (SiteMenu) {
  209.         if (!error) {
  210.             Servers = GetResource('SERV', 128);
  211.             error = !Servers;
  212.         }
  213.         if (!error) DetachResource(Servers);
  214.         if (!error) {
  215.             Domains = GetResource('DOMA', 128);
  216.             error = !Domains;
  217.         }
  218.         if (!error) DetachResource(Domains);
  219.     }
  220.     
  221.     /* Read the HSRV resource. */
  222.     
  223.     if (!error) {
  224.         h = GetResource('HSRV', 128);
  225.         error = !h;
  226.     }
  227.     if (!error) {
  228.         utl_CopyPString(HelpServer, *h);
  229.     }
  230.     
  231.     /* Read the HELP resource and build the HELP menu. */
  232.     
  233.     if (!error) {
  234.         h = GetResource('HELP', 128);
  235.         error = !h;
  236.     }
  237.     if (!error) {
  238.         if (GetHandleSize(h)) {
  239.             DetachResource(h);
  240.             HelpTopics = h;
  241.         } else {
  242.             HelpTopics = nil;
  243.         }
  244.         help_BuildHelpMenu();
  245.     }
  246.     
  247.     /* If any error was encountered, initialize everything for the
  248.         first time using defaults. */
  249.         
  250.     if (error) {
  251.         TranState.moved = false;
  252.         LoginState.moved = false;
  253.         NewState.moved = false;
  254.         OpenState.moved = false;
  255.         PswdState.moved = false;
  256.         SiteState.moved = false;
  257.         for (i = 1; i <= numPosSave; i++)
  258.             QueryStates[i].moved = EditStates[i].moved = HelpStates[i].moved = false;
  259.         SiteMenu = nil;
  260.         h = GetResource('STR ', defServerID);
  261.         utl_CopyPString(DefaultServer, *h);
  262.         serv_GetSiteList(DefaultServer);
  263.         utl_CopyPString(newDefaultServer, DefaultServer);
  264.         while (true) {
  265.             if (site_DoDialog(newDefaultServer, siteLabelMsg1, siteLabelMsg2)) 
  266.                 ExitToShell();
  267.             if (!EqualString(newDefaultServer, DefaultServer, true, true)) {
  268.                 if (rCode = serv_GetSiteList(newDefaultServer)) {
  269.                     glob_ErrorCode(rCode);
  270.                     continue;
  271.                 }
  272.                 utl_CopyPString(DefaultServer, newDefaultServer);
  273.             }
  274.             break;
  275.         }
  276.         if (rCode = serv_GetHelpList(DefaultServer)) {
  277.             glob_ErrorCode(rCode);
  278.             ExitToShell();
  279.         }
  280.     }
  281.     
  282.     /* Close the resource file. */
  283.     
  284.     if (refNum != -1) CloseResFile(refNum);
  285. }
  286.  
  287. #pragma segment Main
  288.  
  289. /*_____________________________________________________________________
  290.  
  291.     BuildMenuItemList - Build List of Menu Items.
  292.     
  293.     Entry:    menu = handle to menu, or nil if none.
  294.                 first = index in menu of first item in list.
  295.     
  296.     Exit:        function result = handle to list of menu items.
  297. _____________________________________________________________________*/
  298.  
  299. static Handle BuildMenuItemList (MenuHandle menu, short first)
  300.  
  301. {
  302.     Handle            h;                /* handle to list */
  303.     short                size;            /* size of list */
  304.     short                numItems;    /* number of items */
  305.     short                i;                /* loop index */
  306.     Str255            str;            /* menu item */
  307.     
  308.     h = NewHandle(0);
  309.     if (!menu) return h;
  310.     size = 0;
  311.     numItems = CountMItems(menu);
  312.     for (i = first; i <= numItems; i++) {
  313.         GetItem(menu, i, str);
  314.         if (str[1] == '-') break;
  315.         SetHandleSize(h, size + *str + 1);
  316.         utl_CopyPString(*h + size, str);
  317.         size += *str+1;
  318.     }
  319.     return h;
  320. }
  321.  
  322. /*_____________________________________________________________________
  323.  
  324.     WritePrefs - Write Prefs File.
  325. _____________________________________________________________________*/
  326.  
  327. static void WritePrefs (void)
  328.  
  329. {
  330.     FSSpec                fSpec;            /* prefs file spec */
  331.     short                    refNum;            /* resource file refnum */
  332.     OSErr                    rCode;            /* error code */
  333.     Boolean                error;            /* true if error encountered */
  334.     Handle                h;                    /* handle to resource */
  335.     WindState            *pState;            /* pointer to window state */
  336.     short                    numTypes;        /* number of resource types */
  337.     ResType                theType;            /* resource type */
  338.     short                    numRez;            /* number of resources of type theType */
  339.     Boolean                isFolderAlias;    /* true if folder alias */
  340.     Boolean                isAlias;            /* true if alias */
  341.     
  342.     /* Build the prefs file spec. */
  343.     
  344.     GetIndString(fSpec.name, stringsID, prefsFileName);
  345.     error = fsu_FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  346.         &fSpec.vRefNum, &fSpec.parID);
  347.         
  348.     /* Resolve aliases. */
  349.     
  350.     if (utl_GestaltFlag(gestaltAliasMgrAttr, gestaltAliasMgrPresent)) {
  351.         rCode = ResolveAliasFile(&fSpec, true, &isFolderAlias, &isAlias);
  352.         error = !rCode && isFolderAlias;
  353.     }
  354.     
  355.     /* Open the prefs resource file with read/write permission.
  356.         If it doesn't exist, create it. */
  357.         
  358.     refNum = -1;
  359.     if (!error) {
  360.         rCode = fsu_FSpOpenResFile(&fSpec, fsRdWrPerm, &refNum);
  361.         if (rCode == fnfErr) {
  362.             rCode = fsu_FSpCreateResFile(&fSpec, 'PHED', 'PHEP');
  363.             if (!rCode) rCode = fsu_FSpOpenResFile(&fSpec, fsRdWrPerm, &refNum);
  364.         };
  365.         error = rCode;
  366.     };
  367.     
  368.     if (!error) {
  369.     
  370.         /* Remove any existing resources. */
  371.         
  372.         numTypes = Count1Types();
  373.         while (numTypes--) {
  374.             Get1IndType(&theType, 1);
  375.             numRez = Count1Resources(theType);
  376.             while (numRez--) RmveResource(Get1IndResource(theType, 1));
  377.         }
  378.         
  379.         /* Write the WPOS resource. */
  380.         
  381.         h = NewHandle((6 + 3*numPosSave)*sizeof(WindState));
  382.         pState = (WindState*)*h;
  383.         *pState++ = TranState;
  384.         *pState++ = NewState;
  385.         *pState++ = OpenState;
  386.         *pState++ = PswdState;
  387.         *pState++ = LoginState;
  388.         *pState++ = SiteState;
  389.         memcpy(pState, QueryStates, numPosSave*sizeof(WindState));
  390.         pState += numPosSave;
  391.         memcpy(pState, EditStates, numPosSave*sizeof(WindState));
  392.         pState += numPosSave;
  393.         memcpy(pState, HelpStates, numPosSave*sizeof(WindState));
  394.         AddResource(h, 'WPOS', 128, "\p");
  395.         
  396.         /* Write the MISC resource. */
  397.         
  398.         h = NewHandle(11);
  399.         *(short*)(*h) = NumSites;
  400.         *(unsigned long*)(*h+2) = LastHelpUpdate;
  401.         *(unsigned long*)(*h+6) = LastSiteUpdate;
  402.         *(Boolean*)(*h+10) = HelpNative;
  403.         AddResource(h, 'MISC', 128, "\p");
  404.         
  405.         /* Write the DSRV resource. */
  406.         
  407.         h = NewHandle(*DefaultServer+1);
  408.         utl_CopyPString(*h, DefaultServer);
  409.         AddResource(h, 'DSRV', 128, "\p");
  410.         
  411.         /* Write the HELP resource. */
  412.         
  413.         h = HelpTopics;
  414.         if (!h) h = NewHandle(0);
  415.         AddResource(h, 'HELP', 128, "\p");
  416.         
  417.         /* Write the HSRV resource. */
  418.         
  419.         h = NewHandle(*HelpServer+1);
  420.         utl_CopyPString(*h, HelpServer);
  421.         AddResource(h, 'HSRV', 128, "\p");
  422.         
  423.         /* Write the SITE resource. */
  424.         
  425.         h = BuildMenuItemList(SiteMenu, 1);
  426.         AddResource(h, 'SITE', 128, "\p");
  427.         
  428.         /* Write the SERV and DOMA resources. */
  429.         
  430.         if (SiteMenu) {
  431.             AddResource(Servers, 'SERV', 128, "\p");
  432.             AddResource(Domains, 'DOMA', 128, "\p");
  433.         }
  434.     }
  435.         
  436.     /* Close the resource file. */
  437.     
  438.     if (refNum != -1) CloseResFile(refNum);
  439. }
  440.  
  441. /*_____________________________________________________________________
  442.  
  443.     Terminate - Terminate program.
  444.     
  445.     Entry:    interactionPermitted = true if interaction permitted.
  446. _____________________________________________________________________*/
  447.  
  448. static void Terminate (Boolean interactionPermitted) 
  449.  
  450. {
  451.     if (edit_Terminate(interactionPermitted)) return;
  452.     help_Terminate();
  453.     query_Terminate();
  454.     WritePrefs();
  455.     mtcp_Terminate();
  456.     Done = true;
  457. }
  458.  
  459. /*_____________________________________________________________________
  460.  
  461.     DoCommand - Process a Menu Command.
  462.     
  463.     Entry:    w = nil.
  464.                 theMenu = menu index.
  465.                 theItem = item index.
  466.                 
  467.     Exit:        function result = true if command handled.
  468. _____________________________________________________________________*/
  469.  
  470. #pragma segment command
  471.  
  472. Boolean DoCommand (WindowPtr w, short theMenu, short theItem)
  473.  
  474. {
  475. #pragma unused (w)
  476.  
  477.     WindowPtr            v;                        /* traverses window list */
  478.     OSErr                    rCode;                /* error code */
  479.     Str255                newDefaultServer;    /* new default server */
  480.  
  481.     switch (theMenu) {
  482.         case fileID:
  483.             switch (theItem) {
  484.                 case updateCmd:
  485.                     if (rCode = serv_GetSiteList(DefaultServer)) glob_ErrorCode(rCode);
  486.                     return true;
  487.                 case changeDefCmd:
  488.                     HiliteMenu(0);
  489.                     utl_CopyPString(newDefaultServer, DefaultServer);
  490.                     while (true) {
  491.                         if (site_DoDialog(newDefaultServer, 
  492.                             siteLabelMsg1, siteLabelMsg2)) return true;
  493.                         if (!EqualString(newDefaultServer, DefaultServer, true, true)) {
  494.                             if (rCode = serv_GetSiteList(newDefaultServer)) {
  495.                                 glob_ErrorCode(rCode);
  496.                                 continue;
  497.                             }
  498.                             utl_CopyPString(DefaultServer, newDefaultServer);
  499.                             query_NewDefaultSite();
  500.                             if (rCode = serv_GetHelpList(DefaultServer)) 
  501.                                 glob_ErrorCode(rCode);
  502.                         }
  503.                         return true;
  504.                     }
  505.                 case quitCmd:
  506.                     Terminate(true);
  507.                     return true;
  508.             }
  509.             return false;
  510.         case windowsID:
  511.             if (theItem == 1) return true;
  512.             v = FrontWindow();
  513.             while (v && --theItem) v = (WindowPtr)((WindowPeek)v)->nextWindow;
  514.             if (v) SelectWindow(v);
  515.             return true;
  516.     }
  517.     return false;
  518. }
  519.  
  520. #pragma segment Main
  521.  
  522. /*_____________________________________________________________________
  523.  
  524.     IgnoreEvent - Ignore an AppleEvent.
  525.     
  526.     Entry:    event = pointer to event.
  527.                 reply = pointer to reply event.
  528.                 refCon = AppleEvent refCon.
  529.     
  530.     Exit:        function result = noErr.
  531.     
  532.     This is the handler for the Open Application, Open Documents,
  533.     and Print Documents events.
  534. _____________________________________________________________________*/
  535.  
  536. pascal OSErr IgnoreEvent (AppleEvent *event, AppleEvent *reply, long refCon)
  537.  
  538. {
  539. #pragma unused (event, reply, refCon)
  540.  
  541.     return noErr;
  542. }
  543.  
  544. /*_____________________________________________________________________
  545.  
  546.     QuitEvent - Quit Event Handler.
  547.     
  548.     Entry:    event = pointer to event.
  549.                 reply = pointer to reply event.
  550.                 refCon = AppleEvent refCon.
  551.     
  552.     Exit:        function result = result code.
  553.                 Done = true.
  554. _____________________________________________________________________*/
  555.  
  556. pascal OSErr QuitEvent (AppleEvent *event, AppleEvent *reply, long refCon)
  557.  
  558. {
  559. #pragma unused (reply, refCon)
  560.  
  561.     OSErr            rCode;            /* result code */
  562.         
  563.     if (rCode = utl_CheckAERequiredParams(event)) return rCode;
  564.     Terminate(utl_InteractionPermitted());
  565.     return noErr;
  566. }
  567.  
  568. /*_____________________________________________________________________
  569.  
  570.     Initialize - Initialize the Program.
  571. _____________________________________________________________________*/
  572.  
  573. #pragma segment init
  574.  
  575. static void Initialize (void)
  576.  
  577. {
  578.     OSErr                rCode;            /* result code */
  579.     short                i;                    /* loop index */
  580.     WindowPtr        w;                    /* pointer to dummy window */
  581.     FontInfo            fInfo;            /* font info */
  582.     Handle            h;                    /* handle to resource */
  583.     long                port;                /* server port number */
  584.     long                SysVersion;        /* system version */
  585.     MenuHandle        helpMenu7;        /* handle to System 7 help menu */
  586.     short                numItems;        /* number of items in System 6 help menu */
  587.     Str255            itemString;        /* help menu command */
  588.     
  589.     /* Establish a grow zone procedure to catch out of memory conditions. */ 
  590.     
  591.     CouldAlert(noMemID);
  592.     SetGrowZone(GrowZone);
  593.     ReserveBlock = NewPtr(140*1024);
  594.     LastGaspBlock = NewPtr(2*1024);
  595.     
  596.     /* Initialize the menus. */
  597.  
  598.     SetMenuBar(GetNewMBar(mBarID));
  599.     AddResMenu(GetMHandle(appleID), (ResType) 'DRVR');
  600.     AppleMenu = GetMHandle(appleID);
  601.     FileMenu = GetMHandle(fileID);
  602.     EditMenu = GetMHandle(editID);
  603.     ProxyMenu = GetMHandle(proxyID);
  604.     WindowsMenu = GetMHandle(windowsID);
  605.     HelpMenu = GetMHandle(helpID);
  606.     HaveBalloons = utl_GestaltFlag(gestaltHelpMgrAttr, gestaltHelpMgrPresent) && 
  607.         !HMGetHelpMenuHandle(&helpMenu7);    
  608.     if (HaveBalloons) {
  609.         numItems = CountMItems(HelpMenu);
  610.         for (i = 1; i <= numItems; i++) {
  611.             GetItem(HelpMenu, i, itemString);
  612.             AppendMenu(helpMenu7, itemString);
  613.         }
  614.         DeleteMenu(helpID);
  615.         NumMenus = numMenus6-1;
  616.         HelpMenu = helpMenu7;
  617.     } else {
  618.         HelpMenuLength = CountMItems(HelpMenu);
  619.         NumMenus = numMenus6;
  620.     }
  621.     HelpMenuLength = CountMItems(HelpMenu);
  622.     DrawMenuBar();
  623.     
  624.     /* Check for System 6.0.5 or later. */
  625.     
  626.     if (Gestalt(gestaltSystemVersion, &SysVersion) || SysVersion < 0x0605) {
  627.         glob_Error(stringsID, msgNeed604, nil);
  628.         ExitToShell();
  629.     }
  630.         
  631.     /* Initialize the cursors. */
  632.  
  633.     IBeamHandle = GetCursor(iBeamCursor);
  634.     WatchHandle = GetCursor(watchCursor);
  635.     SetCursor(*WatchHandle);
  636.     for (i = 0; i < numBB; i++) BBArray[i] = GetCursor(beachCursorID + i);
  637.     
  638.     /* Initialize the regions. */
  639.     
  640.     BigClipRgn = NewRgn();
  641.     SetRectRgn(BigClipRgn, 0x8000, 0x8000, 0x7FFF, 0x7FFF);
  642.         
  643.     /* Initialize the font. */
  644.     
  645.     w = GetNewWindow(editWindID, nil, (WindowPtr)-1);
  646.     SetPort(w);
  647.     GetFNum(font, &FontNum);
  648.     if (!FontNum) {
  649.         glob_Error (stringsID, msgNeedMonaco, nil);
  650.         ExitToShell();
  651.     }
  652.     TextFont(FontNum);
  653.     TextSize(fontSize);
  654.     GetFontInfo(&fInfo);
  655.     Ascent = fInfo.ascent;
  656.     Descent = fInfo.descent;
  657.     Leading = fInfo.leading;
  658.     LineHeight = Ascent + Descent + Leading;
  659.     if (LineHeight & 1) LineHeight++;
  660.     DisposeWindow(w);
  661.     
  662.     /* Initialize Apple events. */
  663.     
  664.     if (utl_GestaltFlag(gestaltAppleEventsAttr, gestaltAppleEventsPresent)) {
  665.         AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  666.             (ProcPtr)IgnoreEvent, 0, false);
  667.         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  668.             (ProcPtr)IgnoreEvent, 0, false);
  669.         AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  670.             (ProcPtr)IgnoreEvent, 0, false);
  671.         AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  672.             (ProcPtr)QuitEvent, 0, false);
  673.     }
  674.     
  675.     /* Get the server port number */
  676.     
  677.     h = GetResource('STR ', portID);
  678.     HLock(h);
  679.     StringToNum(*h, &port);
  680.     PhPort = port;
  681.     ReleaseResource(h);
  682.     
  683.     /* Initialize MacTCP */
  684.     
  685.     rCode = mtcp_Initialize();
  686.     if (rCode) {
  687.         if (rCode == fnfErr) rCode = badUnitErr;
  688.         glob_ErrorCode(rCode);
  689.         ExitToShell();
  690.     }
  691.     
  692.     /* Read the Prefs file. */
  693.     
  694.     ReadPrefs();
  695.     
  696.     /* Add DoCommand to the list of command handlers. */
  697.     
  698.     oop_RegisterCommandHandler(DoCommand);
  699.     
  700.     /* Initialize the other modules. */
  701.     
  702.     edit_Init();
  703.     query_Init();
  704.     help_Init();
  705.     abou_Init();
  706.     
  707.     InitCursor();
  708. }
  709.  
  710. #pragma segment Main
  711.  
  712. /*_____________________________________________________________________
  713.  
  714.     AdjustWindowsMenu - Adjust Windows Menu
  715. _____________________________________________________________________*/
  716.  
  717. static void AdjustWindowsMenu (void)
  718.  
  719. {
  720.     short                    nItems;        /* number of items in menu */
  721.     WindowPtr            v;                /* traverses window list */
  722.     short                    item;            /* menu item number */
  723.     Str255                ttl;            /* window title */
  724.     Str255                cmd;            /* menu command */
  725.  
  726.     nItems = CountMItems(WindowsMenu);
  727.     v = FrontWindow();
  728.     item = 0;
  729.     while (v) {
  730.         GetWTitle(v, ttl);
  731.         if (*ttl) {
  732.             item++;
  733.             if (item > nItems) break;
  734.             GetItem(WindowsMenu, item, cmd);
  735.             if (!EqualString(ttl, cmd, true, true)) break;
  736.         }
  737.         v = (WindowPtr)((WindowPeek)v)->nextWindow;
  738.     }
  739.     if (!v && item == nItems) {
  740.         if (nItems) {
  741.             EnableItem(WindowsMenu, 0);
  742.         } else {
  743.             DisableItem(WindowsMenu, 0);
  744.         }
  745.         return;
  746.     }
  747.     for (item = nItems; item > 0; item--) DelMenuItem(WindowsMenu, item);
  748.     v = FrontWindow();
  749.     item = 0;
  750.     while (v) {
  751.         GetWTitle(v, ttl);
  752.         if (*ttl) {
  753.             item++;
  754.             AppendMenu(WindowsMenu, "\p ");
  755.             SetItem(WindowsMenu, item, ttl);
  756.         }
  757.         v = (WindowPtr)((WindowPeek)v)->nextWindow;
  758.     }
  759.     CalcMenuSize(WindowsMenu);
  760.     if (item) {
  761.         CheckItem(WindowsMenu, 1, true);
  762.         EnableItem(WindowsMenu, 0);
  763.     } else {
  764.         DisableItem(WindowsMenu, 0);
  765.     }
  766. }
  767.  
  768. /*_____________________________________________________________________
  769.  
  770.     AdjustMenus - Adjust Menus.
  771. _____________________________________________________________________*/
  772.  
  773. static void AdjustMenus (WindowPtr top)
  774.  
  775. {
  776.     static Boolean            mBarState[numMenus6] = {true, true, false,
  777.                                                                 false, true, true};
  778.                                                             /* menu enabled flags */
  779.     WindKind                    kind;                        /* top window kind */
  780.     EditLoginStatus        editLoginStatus;        /* edit login status */
  781.     short                        i;                            /* loop index */
  782.     short                        numItems;                /* number of items in help menu */
  783.     short                        editFieldNum;            /* edit field number */
  784.         
  785.     /* Hack to fix problem with old versions of Moire. */
  786.     
  787.     if (***(short***)MenuList < 6*NumMenus) return;
  788.     
  789.     /* Get top window and top window kind. */
  790.     
  791.     kind = oop_GetWindKind(top);
  792.     
  793.     /* Adjust menu commands. */
  794.     
  795.     if (kind >= firstModal) {
  796.         DisableItem(AppleMenu, 0);
  797.         DisableItem(FileMenu, newQueryCmd);
  798.         DisableItem(FileMenu, newPhCmd);
  799.         DisableItem(FileMenu, openPhCmd);
  800.         DisableItem(FileMenu, closeCmd);
  801.         DisableItem(FileMenu, savePhCmd);
  802.         DisableItem(FileMenu, loginCmd);
  803.         DisableItem(FileMenu, logoutCmd);
  804.         DisableItem(FileMenu, paswdCmd);
  805.         DisableItem(FileMenu, deletePhCmd);
  806.         DisableItem(FileMenu, updateCmd);
  807.         DisableItem(FileMenu, changeDefCmd);
  808.         DisableItem(ProxyMenu, 0);
  809.         if (HaveBalloons) {
  810.             numItems = CountMItems(HelpMenu);
  811.             DisableItem(HelpMenu, HelpMenuLength-1);
  812.             for (i = HelpMenuLength+1; i <= numItems; i++) 
  813.                 DisableItem(HelpMenu, i);
  814.         } else {
  815.             DisableItem(HelpMenu, 0);
  816.         }
  817.         DisableItem(WindowsMenu, 0);
  818.         if (kind == tranModal) {
  819.             DisableItem(EditMenu, 0);
  820.         } else {
  821.             DisableItem(EditMenu, undoCmd);
  822.             editFieldNum = utl_GetDialogEditFieldNum(top);
  823.             if (kind == pswdModal ||
  824.                 kind == loginModal && editFieldNum == loginPswdField ||
  825.                 kind == newModal && 
  826.                     (editFieldNum == newPswdField1 || editFieldNum == newPswdField2)) {
  827.                 DisableItem(EditMenu, cutCmd);
  828.                 DisableItem(EditMenu, copyCmd);
  829.             } else {
  830.                 EnableItem(EditMenu, cutCmd);
  831.                 EnableItem(EditMenu, copyCmd);
  832.             }
  833.             EnableItem(EditMenu, pasteCmd);
  834.             EnableItem(EditMenu, clearCmd);
  835.             EnableItem(EditMenu, selectAllCmd);
  836.             EnableItem(EditMenu, 0);
  837.         }
  838.     } else {
  839.         AdjustWindowsMenu();
  840.         EnableItem(AppleMenu, 0);
  841.         if (HaveBalloons) {
  842.             numItems = CountMItems(HelpMenu);
  843.             EnableItem(HelpMenu, HelpMenuLength-1);
  844.             for (i = HelpMenuLength+1; i <= numItems; i++) 
  845.                 EnableItem(HelpMenu, i);
  846.         } else {
  847.             EnableItem(HelpMenu, 0);
  848.         }
  849.         EnableItem(FileMenu, newQueryCmd);
  850.         EnableItem(FileMenu, loginCmd);
  851.         EnableItem(FileMenu, updateCmd);
  852.         EnableItem(FileMenu, changeDefCmd);
  853.         if (CountMItems(ProxyMenu)) {
  854.             EnableItem(ProxyMenu, 0);
  855.         } else {
  856.             DisableItem(ProxyMenu, 0);
  857.         }
  858.         editLoginStatus = edit_GetStatus(nil, nil);
  859.         if (editLoginStatus == editHero) {
  860.             EnableItem(FileMenu, newPhCmd);
  861.         } else {
  862.             DisableItem(FileMenu, newPhCmd);
  863.         }
  864.         if (editLoginStatus == editHero && kind == editWind) {
  865.             EnableItem(FileMenu, deletePhCmd);
  866.         } else {
  867.             DisableItem(FileMenu, deletePhCmd);
  868.         }
  869.         if (editLoginStatus == editNotLoggedIn) {
  870.             DisableItem(FileMenu, openPhCmd);
  871.             DisableItem(FileMenu, logoutCmd);
  872.         } else {
  873.             EnableItem(FileMenu, openPhCmd);
  874.             EnableItem(FileMenu, logoutCmd);
  875.         }
  876.         if (top) {
  877.             EnableItem(FileMenu, closeCmd);
  878.             EnableItem(EditMenu, copyCmd);
  879.             EnableItem(EditMenu, 0);
  880.         } else {
  881.             DisableItem(FileMenu, closeCmd);
  882.             DisableItem(EditMenu, 0);
  883.         }
  884.         if (kind == editWind && edit_IsDirty(top)) {
  885.             EnableItem(FileMenu, savePhCmd);
  886.         } else {
  887.             DisableItem(FileMenu, savePhCmd);
  888.         }
  889.         if (kind == editWind) {
  890.             EnableItem(FileMenu, paswdCmd);
  891.         } else {
  892.             DisableItem(FileMenu, paswdCmd);
  893.         }
  894.         if (kind == daWind) {
  895.             EnableItem(EditMenu, undoCmd);
  896.         } else {
  897.             DisableItem(EditMenu, undoCmd);
  898.         }
  899.         if (kind == daWind || kind == editWind ||
  900.             (kind == queryWind && query_CanEditField(top))) {
  901.             EnableItem(EditMenu, cutCmd);
  902.             EnableItem(EditMenu, pasteCmd);
  903.             EnableItem(EditMenu, clearCmd);
  904.         } else {
  905.             DisableItem(EditMenu, cutCmd);
  906.             DisableItem(EditMenu, pasteCmd);
  907.             DisableItem(EditMenu, clearCmd);
  908.         }
  909.         if (top && kind != daWind) {
  910.             EnableItem(EditMenu, selectAllCmd);
  911.         } else {
  912.             DisableItem(EditMenu, selectAllCmd);
  913.         }
  914.     }
  915.     
  916.     /* Update the menu enabled flags, and redraw the menu bar
  917.         if necessary. */
  918.     
  919.     utl_RedrawMenuBar(mBarState, NumMenus);
  920. }
  921.  
  922. /*_____________________________________________________________________
  923.  
  924.     Main Program.
  925. _____________________________________________________________________*/
  926.  
  927. void main(void)
  928.  
  929. {
  930.     /* Initialize. */
  931.  
  932.     oop_Init(appleID, aboutCmd, fileID, closeCmd, editID, selectAllCmd, 
  933.         AdjustMenus, nil, 20, 20000);
  934.     Initialize();
  935.     UnloadSeg(Initialize);
  936.     
  937.     /* Handle events until done. */
  938.     
  939.     while (!Done) oop_DoEvent(nil, everyEvent, 
  940.         oop_InForeground() ? shortSleep : longSleep, nil);
  941. }
  942.