Handle the Aqua menu layout, in which the system provides a "quit" option
[sgt/putty] / mac / mac.c
index 78e2803..a9a327f 100644 (file)
--- a/mac/mac.c
+++ b/mac/mac.c
@@ -1,6 +1,6 @@
-/* $Id: mac.c,v 1.48 2003/02/12 23:53:15 ben Exp $ */
+/* $Id: mac.c,v 1.52 2003/02/27 23:34:59 ben Exp $ */
 /*
- * Copyright (c) 1999 Ben Harris
+ * Copyright (c) 1999, 2003 Ben Harris
  * All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person
@@ -76,13 +76,13 @@ static void mac_event(EventRecord *);
 static void mac_contentclick(WindowPtr, EventRecord *);
 static void mac_growwindow(WindowPtr, EventRecord *);
 static void mac_activatewindow(WindowPtr, EventRecord *);
+static void mac_suspendresume(EventRecord *);
 static void mac_activateabout(WindowPtr, EventRecord *);
 static void mac_updatewindow(WindowPtr);
 static void mac_updatelicence(WindowPtr);
 static void mac_keypress(EventRecord *);
 static int mac_windowtype(WindowPtr);
 static void mac_menucommand(long);
-static void mac_openabout(void);
 static void mac_openlicence(void);
 static void mac_adjustcursor(RgnHandle);
 static void mac_adjustmenus(void);
@@ -157,6 +157,9 @@ static void mac_startup(void) {
     if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
        &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
        mac_gestalts.windattr = 0;
+    /* Mac OS 8.5 Menu Manager? */
+    if (Gestalt(gestaltMenuMgrAttr, &mac_gestalts.menuattr) != noErr)
+       mac_gestalts.menuattr = 0;
 #endif
     /* Text Encoding Conversion Manager? */
     if (
@@ -189,6 +192,8 @@ static void mac_startup(void) {
        fatalbox("Unable to create menu bar.");
     SetMenuBar(menuBar);
     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
+    if (mac_gestalts.menuattr & gestaltMenuMgrAquaLayoutMask)
+       DeleteMenuItem(GetMenuHandle(mFile), iQuit);
     mac_adjustmenus();
     DrawMenuBar();
     InitCursor();
@@ -324,143 +329,46 @@ static void mac_event(EventRecord *event) {
         }
         break;
 #endif
+      case osEvt:
+       switch ((event->message & osEvtMessageMask) >> 24) {
+         case suspendResumeMessage:
+           mac_suspendresume(event);
+           break;
+       }
+       break;
       case kHighLevelEvent:
        AEProcessAppleEvent(event); /* errors? */
        break;
     }
 }
 
-static void mac_contentclick(WindowPtr window, EventRecord *event) {
-    short item;
-    DialogRef dialog;
+static void mac_contentclick(WindowPtr window, EventRecord *event)
+{
 
-    switch (mac_windowtype(window)) {
-      case wTerminal:
-       mac_clickterm(window, event);
-       break;
-      case wAbout:
-       dialog = GetDialogFromWindow(window);
-       if (DialogSelect(event, &dialog, &item))
-           switch (item) {
-             case wiAboutLicence:
-               mac_openlicence();
-               break;
-           }
-       break;
-      case wSettings:
-       mac_clickdlg(window, event);
-       break;
-      case wEventLog:
-       mac_clickeventlog(window, event);
-       break;
-    }
+    if (mac_wininfo(window)->click != NULL)
+       (*mac_wininfo(window)->click)(window, event);
 }
 
-static void mac_growwindow(WindowPtr window, EventRecord *event) {
+static void mac_growwindow(WindowPtr window, EventRecord *event)
+{
 
-    switch (mac_windowtype(window)) {
-      case wTerminal:
-       mac_growterm(window, event);
-       break;
-      case wEventLog:
-       mac_groweventlog(window, event);
-       break;
-    }
+    if (mac_wininfo(window)->grow != NULL)
+       (*mac_wininfo(window)->grow)(window, event);
 }
 
-static void mac_activatewindow(WindowPtr window, EventRecord *event) {
-    int active;
+static void mac_activatewindow(WindowPtr window, EventRecord *event)
+{
 
-    active = (event->modifiers & activeFlag) != 0;
     mac_adjustmenus();
-    switch (mac_windowtype(window)) {
-      case wTerminal:
-       mac_activateterm(window, active);
-       break;
-      case wSettings:
-       mac_activatedlg(window, event);
-       break;
-      case wAbout:
-       mac_activateabout(window, event);
-       break;
-      case wEventLog:
-       mac_activateeventlog(window, event);
-       break;
-    }
-}
-
-static void mac_activateabout(WindowPtr window, EventRecord *event) {
-    DialogRef dialog;
-    DialogItemType itemtype;
-    Handle itemhandle;
-    short item;
-    Rect itemrect;
-    int active;
-
-    dialog = GetDialogFromWindow(window);
-    active = (event->modifiers & activeFlag) != 0;
-    GetDialogItem(dialog, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
-    HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
-    DialogSelect(event, &dialog, &item);
+    if (mac_wininfo(window)->activate != NULL)
+       (*mac_wininfo(window)->activate)(window, event);
 }
 
 static void mac_updatewindow(WindowPtr window)
 {
-#if TARGET_API_MAC_CARBON
-    RgnHandle rgn;
-#endif
-
-    switch (mac_windowtype(window)) {
-      case wTerminal:
-       mac_updateterm(window);
-       break;
-      case wAbout:
-      case wSettings:
-       BeginUpdate(window);
-#if TARGET_API_MAC_CARBON
-       rgn = NewRgn();
-       GetPortVisibleRegion(GetWindowPort(window), rgn);
-       UpdateDialog(GetDialogFromWindow(window), rgn);
-       DisposeRgn(rgn);
-#else
-       UpdateDialog(window, window->visRgn);
-#endif
-       EndUpdate(window);
-       break;
-      case wLicence:
-       mac_updatelicence(window);
-       break;
-      case wEventLog:
-       mac_updateeventlog(window);
-       break;
-    }
-}
 
-static void mac_updatelicence(WindowPtr window)
-{
-    Handle h;
-    int len;
-    long fondsize;
-    Rect textrect;
-
-    SetPort((GrafPtr)GetWindowPort(window));
-    BeginUpdate(window);
-    fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
-    TextFont(HiWord(fondsize));
-    TextSize(LoWord(fondsize));
-    h = Get1Resource('TEXT', wLicence);
-    len = GetResourceSizeOnDisk(h);
-#if TARGET_API_MAC_CARBON
-    GetPortBounds(GetWindowPort(window), &textrect);
-#else
-    textrect = window->portRect;
-#endif
-    if (h != NULL) {
-       HLock(h);
-       TETextBox(*h, len, &textrect, teFlushDefault);
-       HUnlock(h);
-    }
-    EndUpdate(window);
+    if (mac_wininfo(window)->update != NULL)
+       (*mac_wininfo(window)->update)(window);
 }
 
 /*
@@ -482,7 +390,7 @@ static int mac_windowtype(WindowPtr window)
 static void mac_keypress(EventRecord *event) {
     WindowPtr window;
 
-    window = FrontWindow();
+    window = mac_frontwindow();
     /*
      * Check for a command-key combination, but ignore it if it counts
      * as a meta-key combination and we're in a terminal window.
@@ -493,11 +401,8 @@ static void mac_keypress(EventRecord *event) {
        mac_adjustmenus();
        mac_menucommand(MenuKey(event->message & charCodeMask));
     } else {
-       switch (mac_windowtype(window)) {
-         case wTerminal:
-           mac_keyterm(window, event);
-           break;
-       }
+       if (window != NULL && mac_wininfo(window)->key != NULL)
+           (*mac_wininfo(window)->key)(window, event);
     }       
 }
 
@@ -510,7 +415,7 @@ static void mac_menucommand(long result) {
 
     menu = HiWord(result);
     item = LoWord(result);
-    window = FrontWindow();
+    window = mac_frontwindow();
     /* Things which do the same whatever window we're in. */
     switch (menu) {
       case mApple:
@@ -553,59 +458,13 @@ static void mac_menucommand(long result) {
         break;
     }
     /* If we get here, handling is up to window-specific code. */
-    switch (mac_windowtype(window)) {
-      case wTerminal:
-       mac_menuterm(window, menu, item);
-       break;
-    }
+    if (window != NULL && mac_wininfo(window)->menu != NULL)
+       (*mac_wininfo(window)->menu)(window, menu, item);
+
   done:
     HiliteMenu(0);
 }
 
-static void mac_openabout(void) {
-    DialogItemType itemtype;
-    Handle item;
-    VersRecHndl vers;
-    Rect box;
-    StringPtr longvers;
-    WinInfo *wi;
-
-    if (windows.about)
-       SelectWindow(windows.about);
-    else {
-       windows.about =
-           GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
-       wi = smalloc(sizeof(*wi));
-       wi->s = NULL;
-       wi->wtype = wAbout;
-       SetWRefCon(windows.about, (long)wi);
-       vers = (VersRecHndl)Get1Resource('vers', 1);
-       if (vers != NULL && *vers != NULL) {
-           longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
-           GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
-                         &itemtype, &item, &box);
-           assert(itemtype & kStaticTextDialogItem);
-           SetDialogItemText(item, longvers);
-       }
-       ShowWindow(windows.about);
-    }
-}
-
-static void mac_openlicence(void) {
-    WinInfo *wi;
-
-    if (windows.licence)
-       SelectWindow(windows.licence);
-    else {
-       windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
-       wi = smalloc(sizeof(*wi));
-       wi->s = NULL;
-       wi->wtype = wLicence;
-       SetWRefCon(windows.licence, (long)wi);
-       ShowWindow(windows.licence);
-    }
-}
-
 static void mac_closewindow(WindowPtr window) {
 
     switch (mac_windowtype(window)) {
@@ -614,20 +473,34 @@ static void mac_closewindow(WindowPtr window) {
        CloseDeskAcc(GetWindowKind(window));
        break;
 #endif
-      case wTerminal:
-       mac_closeterm(window);
-       break;
-      case wAbout:
-       windows.about = NULL;
-       DisposeDialog(GetDialogFromWindow(window));
-       break;
-      case wLicence:
-       windows.licence = NULL;
-       DisposeWindow(window);
+      default:
+       if (mac_wininfo(window)->close != NULL)
+           (*mac_wininfo(window)->close)(window);
        break;
     }
 }
 
+static void mac_suspendresume(EventRecord *event)
+{
+    WindowPtr front;
+    EventRecord fakeevent;
+
+    /*
+     * We're called either before we're suspended or after we're
+     * resumed, so we're the front application at this point.
+     */
+    front = FrontWindow();
+    if (front != NULL) {
+       fakeevent.what = activateEvt;
+       fakeevent.message = (UInt32)front;
+       fakeevent.when = event->when;
+       fakeevent.where = event->where;
+       fakeevent.modifiers =
+           (event->message & resumeFlag) ? activeFlag : 0;
+       mac_activatewindow(front, &fakeevent);
+    }
+}
+
 static void mac_zoomwindow(WindowPtr window, short part) {
 
     /* FIXME: do something */
@@ -644,7 +517,7 @@ static void mac_adjustmenus(void) {
     WindowPtr window;
     MenuHandle menu;
 
-    window = FrontWindow();
+    window = mac_frontwindow();
     menu = GetMenuHandle(mApple);
     EnableItem(menu, 0);
     EnableItem(menu, iAbout);
@@ -658,18 +531,9 @@ static void mac_adjustmenus(void) {
        DisableItem(menu, iClose);
     EnableItem(menu, iQuit);
 
-    switch (mac_windowtype(window)) {
-      case wSettings:
-       DisableItem(menu, iSave); /* XXX enable if modified */
-       EnableItem(menu, iSaveAs);
-       EnableItem(menu, iDuplicate);
-       menu = GetMenuHandle(mEdit);
-       DisableItem(menu, 0);
-       break;
-      case wTerminal:
-       mac_adjusttermmenus(window);
-       break;
-      default:
+    if (window != NULL && mac_wininfo(window)->adjustmenus != NULL)
+       (*mac_wininfo(window)->adjustmenus)(window);
+    else {
        DisableItem(menu, iSave);
        DisableItem(menu, iSaveAs);
        DisableItem(menu, iDuplicate);
@@ -677,7 +541,6 @@ static void mac_adjustmenus(void) {
        DisableItem(menu, 0);
        menu = GetMenuHandle(mWindow);
        DisableItem(menu, 0); /* Until we get more than 1 item on it. */
-       break;
     }
     DrawMenuBar();
 }
@@ -718,11 +581,9 @@ static void mac_adjustcursor(RgnHandle cursrgn) {
 #endif
        }
     } else {
-       switch (mac_windowtype(window)) {
-         case wTerminal:
-           mac_adjusttermcursor(window, mouse, cursrgn);
-           break;
-         default:
+       if (mac_wininfo(window)->adjustcursor != NULL)
+           (*mac_wininfo(window)->adjustcursor)(window, mouse, cursrgn);
+       else {
 #if TARGET_API_MAC_CARBON
            GetQDGlobalsArrow(&arrow);
            SetCursor(&arrow);
@@ -731,7 +592,6 @@ static void mac_adjustcursor(RgnHandle cursrgn) {
            SetCursor(&qd.arrow);
            CopyRgn(window->visRgn, cursrgn);
 #endif
-           break;
        }
     }
 }