Add an "open" command to the "file" (now "session") menu on the Mac to
[u/mdw/putty] / mac / mac.c
index 9e53c63..0abb337 100644 (file)
--- a/mac/mac.c
+++ b/mac/mac.c
@@ -1,4 +1,4 @@
-/* $Id: mac.c,v 1.3 2002/11/23 18:22:47 ben Exp $ */
+/* $Id: mac.c,v 1.10 2002/12/30 18:21:17 ben Exp $ */
 /*
  * Copyright (c) 1999 Ben Harris
  * All rights reserved.
 #include <DiskInit.h>
 #include <Gestalt.h>
 #include <Resources.h>
+#include <Script.h>
+#include <TextCommon.h>
 #include <ToolUtils.h>
+#include <UnicodeConverter.h>
 
 #include <assert.h>
 #include <limits.h>
@@ -69,10 +72,12 @@ static void mac_growwindow(WindowPtr, EventRecord *);
 static void mac_activatewindow(WindowPtr, 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);
 static void mac_closewindow(WindowPtr);
@@ -97,6 +102,7 @@ int main (int argc, char **argv) {
 
 static void mac_startup(void) {
     Handle menuBar;
+    TECInfoHandle ti;
 
     /* Init Memory Manager */
     MaxApplZone();
@@ -114,6 +120,11 @@ static void mac_startup(void) {
     InitDialogs(nil);
     cold = 0;
     
+    /* Get base system version (only used if there's no better selector) */
+    if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
+       (mac_gestalts.sysvers & 0xffff) < 0x700)
+       fatalbox("PuTTY requires System 7 or newer");
+    mac_gestalts.sysvers &= 0xffff;
     /* Find out if we've got Color Quickdraw */
     if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
        mac_gestalts.qdvers = gestaltOriginalQD;
@@ -134,6 +145,19 @@ static void mac_startup(void) {
     /* Mac OS 8.5 Window Manager? */
     if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr)
        mac_gestalts.windattr = 0;
+    /* Text Encoding Conversion Manager? */
+    if (
+#if TARGET_RT_MAC_CFM
+       &TECGetInfo == kUnresolvedCFragSymbolAddress ||
+#else
+       InitializeUnicodeConverter(NULL) != noErr ||
+#endif
+       TECGetInfo(&ti) != noErr)
+       mac_gestalts.encvvers = 0;
+    else {
+       mac_gestalts.encvvers = (*ti)->tecVersion;
+       DisposeHandle((Handle)ti);
+    }
 
     /* We've been tested with the Appearance Manager */
     if (mac_gestalts.apprvers != 0)
@@ -240,7 +264,7 @@ static void mac_contentclick(WindowPtr window, EventRecord *event) {
        if (DialogSelect(event, &(DialogPtr)window, &item))
            switch (item) {
              case wiAboutLicence:
-               /* XXX: Do something */
+               mac_openlicence();
                break;
            }
        break;
@@ -295,9 +319,30 @@ static void mac_updatewindow(WindowPtr window) {
        EndUpdate(window);
        break;
       case wLicence:
-        /* Do something */
-        break;
+       mac_updatelicence(window);
+       break;
+    }
+}
+
+static void mac_updatelicence(WindowPtr window)
+{
+    Handle h;
+    int len;
+    long fondsize;
+
+    SetPort(window);
+    BeginUpdate(window);
+    fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
+    TextFont(HiWord(fondsize));
+    TextSize(LoWord(fondsize));
+    h = Get1Resource('TEXT', wLicence);
+    len = GetResourceSizeOnDisk(h);
+    if (h != NULL) {
+       HLock(h);
+       TETextBox(*h, len, &window->portRect, teFlushDefault);
+       HUnlock(h);
     }
+    EndUpdate(window);
 }
 
 /*
@@ -368,6 +413,9 @@ static void mac_menucommand(long result) {
           case iNew:
             mac_newsession();
             goto done;
+         case iOpen:
+           mac_opensession();
+           goto done;
           case iClose:
             mac_closewindow(window);
             goto done;
@@ -410,6 +458,16 @@ static void mac_openabout(void) {
     }
 }
 
+static void mac_openlicence(void) {
+
+    if (windows.licence)
+       SelectWindow(windows.licence);
+    else {
+       windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
+       ShowWindow(windows.licence);
+    }
+}
+
 static void mac_closewindow(WindowPtr window) {
 
     switch (mac_windowtype(window)) {
@@ -423,6 +481,10 @@ static void mac_closewindow(WindowPtr window) {
        windows.about = NULL;
        CloseWindow(window);
        break;
+      case wLicence:
+       windows.licence = NULL;
+       CloseWindow(window);
+       break;
       default:
        CloseWindow(window);
        break;
@@ -500,6 +562,10 @@ static void mac_adjustcursor(RgnHandle cursrgn) {
 
 static void mac_shutdown(void) {
 
+#if !TARGET_RT_MAC_CFM
+    if (mac_gestalts.encvvers != 0)
+       TerminateUnicodeConverter();
+#endif
     exit(0);
 }