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)
69 static struct sesslist sesslist;
74 s = mac_windowsession(FrontWindow());
77 memset(s, 0, sizeof(*s));
78 do_defaults(NULL, &s->cfg);
80 s->session_closed = FALSE;
83 /* Copy the configuration somewhere else in case this is a *
84 * reconfiguration and the user cancels the operation */
89 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
91 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
93 get_sesslist(&sesslist, TRUE);
94 s->ctrlbox = ctrl_new_box();
95 setup_config_box(s->ctrlbox, &sesslist, midsession, 0, 0);
97 s->settings_ctrls.data = &s->temp_cfg;
99 s->settings_ctrls.end = &mac_enddlg_reconfig;
101 s->settings_ctrls.end = &mac_enddlg_config;
103 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
106 memset(wi, 0, sizeof(*wi));
108 wi->mcs = &s->settings_ctrls;
109 wi->wtype = wSettings;
110 wi->update = &macctrl_update;
111 wi->click = &macctrl_click;
112 wi->key = &macctrl_key;
113 wi->activate = &macctrl_activate;
114 wi->adjustmenus = &macctrl_adjustmenus;
115 wi->close = &mac_closedlg;
116 SetWRefCon(s->settings_window, (long)wi);
118 str = dupprintf("%s Reconfiguration", appname);
120 str = dupprintf("%s Configuration", appname);
121 c2pstrcpy(mactitle, str);
123 SetWTitle(s->settings_window, mactitle);
124 ShowWindow(s->settings_window);
127 static void mac_closedlg(WindowPtr window)
129 Session *s = mac_windowsession(window);
131 macctrl_close(window);
132 DisposeWindow(window);
133 if (s->window == NULL)
137 static void mac_enddlg_config(WindowPtr window, int value)
139 Session *s = mac_windowsession(window);
142 mac_closedlg(window);
144 s->cfg = s->temp_cfg;
146 mac_closedlg(window);
150 static void mac_enddlg_reconfig(WindowPtr window, int value)
152 Session *s = mac_windowsession(window);
155 mac_closedlg(window);
157 Config prev_cfg = s->cfg;
158 s->cfg = s->temp_cfg;
159 mac_closedlg(window);
161 /* Pass new config data to the logging module */
162 log_reconfig(s->logctx, &s->cfg);
165 * Flush the line discipline's edit buffer in the
166 * case where local editing has just been disabled.
169 ldisc_send(s->ldisc, NULL, 0, 0);
171 /* Change the palette */
174 /* Reinitialise line codepage */
177 /* Pass new config data to the terminal */
178 term_reconfig(s->term, &s->cfg);
180 /* Pass new config data to the back end */
182 s->back->reconfig(s->backhandle, &s->cfg);
184 /* Screen size changed ? */
185 if (s->cfg.height != prev_cfg.height ||
186 s->cfg.width != prev_cfg.width ||
187 s->cfg.savelines != prev_cfg.savelines) {
188 request_resize(s, s->cfg.width, s->cfg.height);
191 /* Set the window title */
192 if (s->cfg.wintitle[0])
193 set_title(s, s->cfg.wintitle);
196 if (s->cfg.scrollbar != prev_cfg.scrollbar)
197 request_resize(s, s->cfg.width, s->cfg.height);
199 /* TODO: zoom, font */
203 void mac_dupsession(void)
205 Session *s1 = mac_windowsession(FrontWindow());
209 memset(s2, 0, sizeof(*s2));
211 s2->hasfile = s1->hasfile;
212 s2->savefile = s1->savefile;
214 mac_startsession(s2);
217 static OSErr mac_opensessionfrom(FSSpec *fss)
225 memset(s, 0, sizeof(*s));
227 err = FSpGetFInfo(fss, &fi);
228 if (err != noErr) return err;
229 if (fi.fdFlags & kIsStationery)
236 sesshandle = open_settings_r_fsp(fss);
237 if (sesshandle == NULL) {
238 /* XXX need a way to pass up an error number */
242 load_open_settings(sesshandle, TRUE, &s->cfg);
243 close_settings_r(sesshandle);
253 static OSErr mac_openlist(AEDesc docs)
262 err = AECountItems(&docs, &ndocs);
263 if (err != noErr) return err;
265 for (i = 0; i < ndocs; i++) {
266 err = AEGetNthPtr(&docs, i + 1, typeFSS,
267 &keywd, &type, &fss, sizeof(fss), &size);
268 if (err != noErr) return err;;
269 err = mac_opensessionfrom(&fss);
270 if (err != noErr) return err;
275 void mac_opensession(void)
278 if (mac_gestalts.navsvers > 0) {
280 NavDialogOptions navopts;
281 NavTypeListHandle navtypes;
282 AEDesc defaultloc = { 'null', NULL };
283 AEDesc *navdefault = NULL;
288 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
289 /* XXX should we create sessions dir? */
290 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
291 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
292 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
293 navdefault = &defaultloc;
294 /* Can't meaningfully preview a saved session yet */
295 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
296 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
297 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
298 NULL) == noErr && navr.validRecord)
299 mac_openlist(navr.selection);
300 NavDisposeReply(&navr);
301 if (navtypes != NULL)
302 ReleaseResource((Handle)navtypes);
304 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
306 StandardFileReply sfr;
307 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
309 StandardGetFile(NULL, 1, sftypes, &sfr);
310 if (!sfr.sfGood) return;
312 mac_opensessionfrom(&sfr.sfFile);
313 /* XXX handle error */
318 void mac_savesession(void)
320 Session *s = mac_windowsession(FrontWindow());
324 sesshandle = open_settings_w_fsp(&s->savefile);
325 if (sesshandle == NULL) return; /* XXX report error */
326 save_open_settings(sesshandle, TRUE, &s->cfg);
327 close_settings_w(sesshandle);
330 void mac_savesessionas(void)
332 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
333 Session *s = mac_windowsession(FrontWindow());
334 StandardFileReply sfr;
337 StandardPutFile("\pSave session as:",
338 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
339 if (!sfr.sfGood) return;
341 if (!sfr.sfReplacing) {
342 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
343 if (ResError() != noErr) return; /* XXX report error */
345 sesshandle = open_settings_w_fsp(&sfr.sfFile);
346 if (sesshandle == NULL) return; /* XXX report error */
347 save_open_settings(sesshandle, TRUE, &s->cfg);
348 close_settings_w(sesshandle);
350 s->savefile = sfr.sfFile;
354 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
360 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
361 &type, NULL, 0, &size) == noErr)
362 return errAEParamMissed;
364 /* XXX we should do something here. */
368 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
373 AEDescList docs = { typeNull, NULL };
376 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
377 if (err != noErr) goto out;
379 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
380 &type, NULL, 0, &size) == noErr) {
381 err = errAEParamMissed;
385 err = mac_openlist(docs);
388 AEDisposeDesc(&docs);
392 pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
398 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
399 &type, NULL, 0, &size) == noErr)
400 return errAEParamMissed;
402 /* We can't meaningfully do anything here. */
403 return errAEEventNotHandled;
408 * c-file-style: "simon"