Slightly improve Carbon handling: we still have to deal with the "About..."
[sgt/putty] / mac / macdlg.c
index e8970d4..3da5083 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macdlg.c,v 1.8 2003/01/25 15:15:40 ben Exp $ */
+/* $Id: macdlg.c,v 1.10 2003/02/02 15:59:00 ben Exp $ */
 /*
  * Copyright (c) 2002 Ben Harris
  * All rights reserved.
@@ -33,6 +33,7 @@
 #include <AEDataModel.h>
 #include <AppleEvents.h>
 #include <Dialogs.h>
+#include <Navigation.h>
 #include <Resources.h>
 #include <StandardFile.h>
 #include <Windows.h>
@@ -55,7 +56,8 @@ void mac_newsession(void)
     do_defaults(NULL, &s->cfg);
     s->hasfile = FALSE;
 
-    s->settings_window = GetNewDialog(wSettings, NULL, (WindowPtr)-1);
+    s->settings_window =
+       GetDialogWindow(GetNewDialog(wSettings, NULL, (WindowPtr)-1));
 
     SetWRefCon(s->settings_window, (long)s);
     ShowWindow(s->settings_window);
@@ -111,15 +113,69 @@ static OSErr mac_opensessionfrom(FSSpec *fss)
     return err;
 }
 
-void mac_opensession(void) {
-    StandardFileReply sfr;
-    static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
+static OSErr mac_openlist(AEDesc docs)
+{
+    OSErr err;
+    long ndocs, i;
+    FSSpec fss;
+    AEKeyword keywd;
+    DescType type;
+    Size size;
 
-    StandardGetFile(NULL, 1, sftypes, &sfr);
-    if (!sfr.sfGood) return;
+    err = AECountItems(&docs, &ndocs);
+    if (err != noErr) return err;
 
-    mac_opensessionfrom(&sfr.sfFile);
-    /* XXX handle error */
+    for (i = 0; i < ndocs; i++) {
+       err = AEGetNthPtr(&docs, i + 1, typeFSS,
+                         &keywd, &type, &fss, sizeof(fss), &size);
+       if (err != noErr) return err;;
+       err = mac_opensessionfrom(&fss);
+       if (err != noErr) return err;
+    }
+    return noErr;
+}
+
+void mac_opensession(void)
+{
+
+    if (mac_gestalts.navsvers > 0) {
+       NavReplyRecord navr;
+       NavDialogOptions navopts;
+       NavTypeListHandle navtypes;
+       AEDesc defaultloc = { 'null', NULL };
+       AEDesc *navdefault = NULL;
+       short vol;
+       long dirid;
+       FSSpec fss;
+
+       if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
+       /* XXX should we create sessions dir? */
+       if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
+           FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
+           AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
+           navdefault = &defaultloc;
+       /* Can't meaningfully preview a saved session yet */
+       navopts.dialogOptionFlags &= ~kNavAllowPreviews;
+       navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
+       if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
+                      NULL) == noErr && navr.validRecord)
+           mac_openlist(navr.selection);
+       NavDisposeReply(&navr);
+       if (navtypes != NULL)
+           ReleaseResource((Handle)navtypes);
+    }
+#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
+    else {
+       StandardFileReply sfr;
+       static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
+
+       StandardGetFile(NULL, 1, sftypes, &sfr);
+       if (!sfr.sfGood) return;
+
+       mac_opensessionfrom(&sfr.sfFile);
+       /* XXX handle error */
+    }
+#endif
 }
 
 void mac_savesession(void)
@@ -136,6 +192,7 @@ void mac_savesession(void)
 
 void mac_savesessionas(void)
 {
+#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
     Session *s = (Session *)GetWRefCon(FrontWindow());
     StandardFileReply sfr;
     void *sesshandle;
@@ -154,6 +211,7 @@ void mac_savesessionas(void)
     close_settings_w(sesshandle);
     s->hasfile = TRUE;
     s->savefile = sfr.sfFile;
+#endif
 }
 
 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
@@ -174,12 +232,9 @@ pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
                           long refcon)
 {
     DescType type;
-    AEKeyword keywd;
     Size size;
     AEDescList docs = { typeNull, NULL };
     OSErr err;
-    long ndocs, i;
-    FSSpec fss;
 
     err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
     if (err != noErr) goto out;
@@ -190,16 +245,7 @@ pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
        goto out;
     }
 
-    err = AECountItems(&docs, &ndocs);
-    if (err != noErr) goto out;
-
-    for (i = 0; i < ndocs; i++) {
-       err = AEGetNthPtr(&docs, i + 1, typeFSS,
-                         &keywd, &type, &fss, sizeof(fss), &size);
-       if (err != noErr) goto out;
-       err = mac_opensessionfrom(&fss);
-       if (err != noErr) goto out;
-    }
+    err = mac_openlist(docs);
 
   out:
     AEDisposeDesc(&docs);
@@ -227,22 +273,24 @@ void mac_activatedlg(WindowPtr window, EventRecord *event)
     short item;
     Rect itemrect;
     int active;
+    DialogRef dialog = GetDialogFromWindow(window);
 
     active = (event->modifiers & activeFlag) != 0;
-    GetDialogItem(window, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
+    GetDialogItem(dialog, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
     HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
-    DialogSelect(event, &window, &item);
+    DialogSelect(event, &dialog, &item);
 }
 
 void mac_clickdlg(WindowPtr window, EventRecord *event)
 {
     short item;
     Session *s = (Session *)GetWRefCon(window);
+    DialogRef dialog = GetDialogFromWindow(window);
 
-    if (DialogSelect(event, &window, &item))
+    if (DialogSelect(event, &dialog, &item))
        switch (item) {
          case wiSettingsOpen:
-           CloseWindow(window);
+           HideWindow(window);
            mac_startsession(s);
            break;
        }