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_config(int);
51 static void mac_closedlg(WindowPtr);
52 static void mac_enddlg_config(WindowPtr, int);
53 static void mac_enddlg_reconfig(WindowPtr, int);
55 void mac_newsession(void)
60 void mac_reconfig(void)
65 static void mac_config(int midsession)
73 s = mac_windowsession(FrontWindow());
76 memset(s, 0, sizeof(*s));
77 do_defaults(NULL, &s->cfg);
79 s->session_closed = FALSE;
82 /* Copy the configuration somewhere else in case this is a *
83 * reconfiguration and the user cancels the operation */
88 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
90 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
92 s->ctrlbox = ctrl_new_box();
93 setup_config_box(s->ctrlbox, midsession, 0, 0);
95 s->settings_ctrls.data = &s->temp_cfg;
97 s->settings_ctrls.end = &mac_enddlg_reconfig;
99 s->settings_ctrls.end = &mac_enddlg_config;
101 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
104 memset(wi, 0, sizeof(*wi));
106 wi->mcs = &s->settings_ctrls;
107 wi->wtype = wSettings;
108 wi->update = &macctrl_update;
109 wi->click = &macctrl_click;
110 wi->key = &macctrl_key;
111 wi->activate = &macctrl_activate;
112 wi->adjustmenus = &macctrl_adjustmenus;
113 wi->close = &mac_closedlg;
114 SetWRefCon(s->settings_window, (long)wi);
116 str = dupprintf("%s Reconfiguration", appname);
118 str = dupprintf("%s Configuration", appname);
119 c2pstrcpy(mactitle, str);
121 SetWTitle(s->settings_window, mactitle);
122 ShowWindow(s->settings_window);
125 static void mac_closedlg(WindowPtr window)
127 Session *s = mac_windowsession(window);
129 macctrl_close(window);
130 DisposeWindow(window);
131 if (s->window == NULL)
135 static void mac_enddlg_config(WindowPtr window, int value)
137 Session *s = mac_windowsession(window);
140 mac_closedlg(window);
142 s->cfg = s->temp_cfg;
144 mac_closedlg(window);
148 static void mac_enddlg_reconfig(WindowPtr window, int value)
150 Session *s = mac_windowsession(window);
153 mac_closedlg(window);
155 Config prev_cfg = s->cfg;
156 s->cfg = s->temp_cfg;
157 mac_closedlg(window);
159 /* Pass new config data to the logging module */
160 log_reconfig(s->logctx, &s->cfg);
163 * Flush the line discipline's edit buffer in the
164 * case where local editing has just been disabled.
167 ldisc_send(s->ldisc, NULL, 0, 0);
169 /* Change the palette */
172 /* Reinitialise line codepage */
175 /* Pass new config data to the terminal */
176 term_reconfig(s->term, &s->cfg);
178 /* Pass new config data to the back end */
180 s->back->reconfig(s->backhandle, &s->cfg);
182 /* Screen size changed ? */
183 if (s->cfg.height != prev_cfg.height ||
184 s->cfg.width != prev_cfg.width ||
185 s->cfg.savelines != prev_cfg.savelines) {
186 request_resize(s, s->cfg.width, s->cfg.height);
189 /* Set the window title */
190 if (s->cfg.wintitle[0])
191 set_title(s, s->cfg.wintitle);
194 if (s->cfg.scrollbar != prev_cfg.scrollbar)
195 request_resize(s, s->cfg.width, s->cfg.height);
197 /* TODO: zoom, font */
201 void mac_dupsession(void)
203 Session *s1 = mac_windowsession(FrontWindow());
207 memset(s2, 0, sizeof(*s2));
209 s2->hasfile = s1->hasfile;
210 s2->savefile = s1->savefile;
212 mac_startsession(s2);
215 static OSErr mac_opensessionfrom(FSSpec *fss)
223 memset(s, 0, sizeof(*s));
225 err = FSpGetFInfo(fss, &fi);
226 if (err != noErr) return err;
227 if (fi.fdFlags & kIsStationery)
234 sesshandle = open_settings_r_fsp(fss);
235 if (sesshandle == NULL) {
236 /* XXX need a way to pass up an error number */
240 load_open_settings(sesshandle, &s->cfg);
241 close_settings_r(sesshandle);
251 static OSErr mac_openlist(AEDesc docs)
260 err = AECountItems(&docs, &ndocs);
261 if (err != noErr) return err;
263 for (i = 0; i < ndocs; i++) {
264 err = AEGetNthPtr(&docs, i + 1, typeFSS,
265 &keywd, &type, &fss, sizeof(fss), &size);
266 if (err != noErr) return err;;
267 err = mac_opensessionfrom(&fss);
268 if (err != noErr) return err;
273 void mac_opensession(void)
276 if (mac_gestalts.navsvers > 0) {
278 NavDialogOptions navopts;
279 NavTypeListHandle navtypes;
280 AEDesc defaultloc = { 'null', NULL };
281 AEDesc *navdefault = NULL;
286 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
287 /* XXX should we create sessions dir? */
288 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
289 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
290 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
291 navdefault = &defaultloc;
292 /* Can't meaningfully preview a saved session yet */
293 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
294 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
295 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
296 NULL) == noErr && navr.validRecord)
297 mac_openlist(navr.selection);
298 NavDisposeReply(&navr);
299 if (navtypes != NULL)
300 ReleaseResource((Handle)navtypes);
302 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
304 StandardFileReply sfr;
305 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
307 StandardGetFile(NULL, 1, sftypes, &sfr);
308 if (!sfr.sfGood) return;
310 mac_opensessionfrom(&sfr.sfFile);
311 /* XXX handle error */
316 void mac_savesession(void)
318 Session *s = mac_windowsession(FrontWindow());
322 sesshandle = open_settings_w_fsp(&s->savefile);
323 if (sesshandle == NULL) return; /* XXX report error */
324 save_open_settings(sesshandle, &s->cfg);
325 close_settings_w(sesshandle);
328 void mac_savesessionas(void)
330 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
331 Session *s = mac_windowsession(FrontWindow());
332 StandardFileReply sfr;
335 StandardPutFile("\pSave session as:",
336 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
337 if (!sfr.sfGood) return;
339 if (!sfr.sfReplacing) {
340 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
341 if (ResError() != noErr) return; /* XXX report error */
343 sesshandle = open_settings_w_fsp(&sfr.sfFile);
344 if (sesshandle == NULL) return; /* XXX report error */
345 save_open_settings(sesshandle, &s->cfg);
346 close_settings_w(sesshandle);
348 s->savefile = sfr.sfFile;
352 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
358 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
359 &type, NULL, 0, &size) == noErr)
360 return errAEParamMissed;
362 /* XXX we should do something here. */
366 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
371 AEDescList docs = { typeNull, NULL };
374 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
375 if (err != noErr) goto out;
377 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
378 &type, NULL, 0, &size) == noErr) {
379 err = errAEParamMissed;
383 err = mac_openlist(docs);
386 AEDisposeDesc(&docs);
390 pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
396 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
397 &type, NULL, 0, &size) == noErr)
398 return errAEParamMissed;
400 /* We can't meaningfully do anything here. */
401 return errAEEventNotHandled;
406 * c-file-style: "simon"