3 * Copyright (c) 2002 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * macdlg.c - settings dialogue box for Mac OS.
33 #include <AEDataModel.h>
34 #include <AppleEvents.h>
35 #include <Navigation.h>
36 #include <Resources.h>
37 #include <StandardFile.h>
38 #include <TextUtils.h>
50 static void mac_closedlg(WindowPtr);
51 static void mac_enddlg(WindowPtr, int);
53 void mac_newsession(void)
57 static struct sesslist sesslist;
61 memset(s, 0, sizeof(*s));
62 do_defaults(NULL, &s->cfg);
66 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
68 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
70 get_sesslist(&sesslist, TRUE);
71 s->ctrlbox = ctrl_new_box();
72 setup_config_box(s->ctrlbox, &sesslist, FALSE, 0, 0);
74 s->settings_ctrls.data = &s->cfg;
75 s->settings_ctrls.end = &mac_enddlg;
76 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
79 memset(wi, 0, sizeof(*wi));
81 wi->mcs = &s->settings_ctrls;
82 wi->wtype = wSettings;
83 wi->update = &macctrl_update;
84 wi->click = &macctrl_click;
85 wi->key = &macctrl_key;
86 wi->activate = &macctrl_activate;
87 wi->adjustmenus = &macctrl_adjustmenus;
88 wi->close = &mac_closedlg;
89 SetWRefCon(s->settings_window, (long)wi);
90 c2pstrcpy(mactitle, "PuTTY Configuration");
91 SetWTitle(s->settings_window, mactitle);
92 ShowWindow(s->settings_window);
95 static void mac_closedlg(WindowPtr window)
97 Session *s = mac_windowsession(window);
99 macctrl_close(window);
100 DisposeWindow(window);
101 if (s->window == NULL)
105 static void mac_enddlg(WindowPtr window, int value)
107 Session *s = mac_windowsession(window);
110 mac_closedlg(window);
113 mac_closedlg(window);
117 void mac_dupsession(void)
119 Session *s1 = mac_windowsession(FrontWindow());
123 memset(s2, 0, sizeof(*s2));
125 s2->hasfile = s1->hasfile;
126 s2->savefile = s1->savefile;
128 mac_startsession(s2);
131 static OSErr mac_opensessionfrom(FSSpec *fss)
139 memset(s, 0, sizeof(*s));
141 err = FSpGetFInfo(fss, &fi);
142 if (err != noErr) return err;
143 if (fi.fdFlags & kIsStationery)
150 sesshandle = open_settings_r_fsp(fss);
151 if (sesshandle == NULL) {
152 /* XXX need a way to pass up an error number */
156 load_open_settings(sesshandle, TRUE, &s->cfg);
157 close_settings_r(sesshandle);
167 static OSErr mac_openlist(AEDesc docs)
176 err = AECountItems(&docs, &ndocs);
177 if (err != noErr) return err;
179 for (i = 0; i < ndocs; i++) {
180 err = AEGetNthPtr(&docs, i + 1, typeFSS,
181 &keywd, &type, &fss, sizeof(fss), &size);
182 if (err != noErr) return err;;
183 err = mac_opensessionfrom(&fss);
184 if (err != noErr) return err;
189 void mac_opensession(void)
192 if (mac_gestalts.navsvers > 0) {
194 NavDialogOptions navopts;
195 NavTypeListHandle navtypes;
196 AEDesc defaultloc = { 'null', NULL };
197 AEDesc *navdefault = NULL;
202 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
203 /* XXX should we create sessions dir? */
204 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
205 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
206 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
207 navdefault = &defaultloc;
208 /* Can't meaningfully preview a saved session yet */
209 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
210 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
211 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
212 NULL) == noErr && navr.validRecord)
213 mac_openlist(navr.selection);
214 NavDisposeReply(&navr);
215 if (navtypes != NULL)
216 ReleaseResource((Handle)navtypes);
218 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
220 StandardFileReply sfr;
221 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
223 StandardGetFile(NULL, 1, sftypes, &sfr);
224 if (!sfr.sfGood) return;
226 mac_opensessionfrom(&sfr.sfFile);
227 /* XXX handle error */
232 void mac_savesession(void)
234 Session *s = mac_windowsession(FrontWindow());
238 sesshandle = open_settings_w_fsp(&s->savefile);
239 if (sesshandle == NULL) return; /* XXX report error */
240 save_open_settings(sesshandle, TRUE, &s->cfg);
241 close_settings_w(sesshandle);
244 void mac_savesessionas(void)
246 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
247 Session *s = mac_windowsession(FrontWindow());
248 StandardFileReply sfr;
251 StandardPutFile("\pSave session as:",
252 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
253 if (!sfr.sfGood) return;
255 if (!sfr.sfReplacing) {
256 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
257 if (ResError() != noErr) return; /* XXX report error */
259 sesshandle = open_settings_w_fsp(&sfr.sfFile);
260 if (sesshandle == NULL) return; /* XXX report error */
261 save_open_settings(sesshandle, TRUE, &s->cfg);
262 close_settings_w(sesshandle);
264 s->savefile = sfr.sfFile;
268 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
274 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
275 &type, NULL, 0, &size) == noErr)
276 return errAEParamMissed;
278 /* XXX we should do something here. */
282 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
287 AEDescList docs = { typeNull, NULL };
290 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
291 if (err != noErr) goto out;
293 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
294 &type, NULL, 0, &size) == noErr) {
295 err = errAEParamMissed;
299 err = mac_openlist(docs);
302 AEDisposeDesc(&docs);
306 pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
312 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
313 &type, NULL, 0, &size) == noErr)
314 return errAEParamMissed;
316 /* We can't meaningfully do anything here. */
317 return errAEEventNotHandled;
322 * c-file-style: "simon"