If a new session was saved from Change Settings, a side-effect on Windows was
[u/mdw/putty] / mac / macdlg.c
CommitLineData
f89c3294 1/* $Id$ */
6cb61a05 2/*
3 * Copyright (c) 2002 Ben Harris
4 * All rights reserved.
5 *
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
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
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
25 * SOFTWARE.
26 */
27
28/*
29 * macdlg.c - settings dialogue box for Mac OS.
30 */
31
32#include <MacTypes.h>
d70e3b83 33#include <AEDataModel.h>
34#include <AppleEvents.h>
9ef3c1ca 35#include <Navigation.h>
b537dd42 36#include <Resources.h>
2b8dca9e 37#include <StandardFile.h>
f8d7de79 38#include <TextUtils.h>
6cb61a05 39#include <Windows.h>
40
98f59a35 41#include <assert.h>
6cb61a05 42#include <string.h>
43
44#include "putty.h"
8a7e67ec 45#include "dialog.h"
6cb61a05 46#include "mac.h"
47#include "macresid.h"
2b8dca9e 48#include "storage.h"
6cb61a05 49
71581f96 50static void mac_config(int);
8a7e67ec 51static void mac_closedlg(WindowPtr);
71581f96 52static void mac_enddlg_config(WindowPtr, int);
53static void mac_enddlg_reconfig(WindowPtr, int);
f854b482 54
6cb61a05 55void mac_newsession(void)
56{
71581f96 57 mac_config(FALSE);
58}
59
60void mac_reconfig(void)
61{
62 mac_config(TRUE);
63}
64
65static void mac_config(int midsession)
66{
6cb61a05 67 Session *s;
f854b482 68 WinInfo *wi;
f8d7de79 69 Str255 mactitle;
71581f96 70 char *str;
71
72 if (midsession) {
73 s = mac_windowsession(FrontWindow());
74 } else {
75 s = snew(Session);
76 memset(s, 0, sizeof(*s));
77 do_defaults(NULL, &s->cfg);
78 s->hasfile = FALSE;
8154b602 79 s->session_closed = FALSE;
71581f96 80 }
6cb61a05 81
d36f85ac 82 /* Copy the configuration somewhere else in case this is a *
83 * reconfiguration and the user cancels the operation */
84
85 s->temp_cfg = s->cfg;
86
8a7e67ec 87 if (HAVE_COLOR_QD())
88 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
89 else
90 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
91
8a7e67ec 92 s->ctrlbox = ctrl_new_box();
12745e35 93 setup_config_box(s->ctrlbox, midsession, 0, 0);
8a7e67ec 94
d36f85ac 95 s->settings_ctrls.data = &s->temp_cfg;
71581f96 96 if (midsession)
97 s->settings_ctrls.end = &mac_enddlg_reconfig;
98 else
99 s->settings_ctrls.end = &mac_enddlg_config;
100
8a7e67ec 101 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
6cb61a05 102
34773273 103 wi = snew(WinInfo);
f854b482 104 memset(wi, 0, sizeof(*wi));
105 wi->s = s;
5cec2fd9 106 wi->mcs = &s->settings_ctrls;
f854b482 107 wi->wtype = wSettings;
8a7e67ec 108 wi->update = &macctrl_update;
109 wi->click = &macctrl_click;
1f1ec0e5 110 wi->key = &macctrl_key;
8a7e67ec 111 wi->activate = &macctrl_activate;
112 wi->adjustmenus = &macctrl_adjustmenus;
ee10bc56 113 wi->close = &mac_closedlg;
f854b482 114 SetWRefCon(s->settings_window, (long)wi);
71581f96 115 if (midsession)
116 str = dupprintf("%s Reconfiguration", appname);
117 else
118 str = dupprintf("%s Configuration", appname);
119 c2pstrcpy(mactitle, str);
66dba8af 120 sfree(str);
f8d7de79 121 SetWTitle(s->settings_window, mactitle);
6cb61a05 122 ShowWindow(s->settings_window);
123}
124
ee10bc56 125static void mac_closedlg(WindowPtr window)
126{
127 Session *s = mac_windowsession(window);
128
129 macctrl_close(window);
130 DisposeWindow(window);
131 if (s->window == NULL)
132 sfree(s);
133}
134
71581f96 135static void mac_enddlg_config(WindowPtr window, int value)
f73f2f31 136{
137 Session *s = mac_windowsession(window);
138
139 if (value == 0)
140 mac_closedlg(window);
141 else {
d36f85ac 142 s->cfg = s->temp_cfg;
f73f2f31 143 mac_startsession(s);
144 mac_closedlg(window);
145 }
146}
ee10bc56 147
71581f96 148static void mac_enddlg_reconfig(WindowPtr window, int value)
149{
150 Session *s = mac_windowsession(window);
151
152 if (value == 0)
153 mac_closedlg(window);
154 else {
d36f85ac 155 Config prev_cfg = s->cfg;
156 s->cfg = s->temp_cfg;
71581f96 157 mac_closedlg(window);
d36f85ac 158
159 /* Pass new config data to the logging module */
160 log_reconfig(s->logctx, &s->cfg);
161
162 /*
163 * Flush the line discipline's edit buffer in the
164 * case where local editing has just been disabled.
165 */
166 if (s->ldisc)
167 ldisc_send(s->ldisc, NULL, 0, 0);
168
169 /* Change the palette */
170 palette_reset(s);
171
0d737aa4 172 /* Reinitialise line codepage */
173 init_ucs(s);
174
d36f85ac 175 /* Pass new config data to the terminal */
176 term_reconfig(s->term, &s->cfg);
177
178 /* Pass new config data to the back end */
179 if (s->back)
180 s->back->reconfig(s->backhandle, &s->cfg);
181
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) {
d36f85ac 186 request_resize(s, s->cfg.width, s->cfg.height);
187 }
188
189 /* Set the window title */
190 if (s->cfg.wintitle[0])
191 set_title(s, s->cfg.wintitle);
192
fb783caf 193 /* Scroll bar */
194 if (s->cfg.scrollbar != prev_cfg.scrollbar)
195 request_resize(s, s->cfg.width, s->cfg.height);
196
197 /* TODO: zoom, font */
71581f96 198 }
199}
200
5211281f 201void mac_dupsession(void)
202{
347bfcd7 203 Session *s1 = mac_windowsession(FrontWindow());
5211281f 204 Session *s2;
205
34773273 206 s2 = snew(Session);
5211281f 207 memset(s2, 0, sizeof(*s2));
208 s2->cfg = s1->cfg;
209 s2->hasfile = s1->hasfile;
210 s2->savefile = s1->savefile;
211
212 mac_startsession(s2);
213}
214
d70e3b83 215static OSErr mac_opensessionfrom(FSSpec *fss)
216{
217 FInfo fi;
2b8dca9e 218 Session *s;
2b8dca9e 219 void *sesshandle;
d70e3b83 220 OSErr err;
2b8dca9e 221
34773273 222 s = snew(Session);
2b8dca9e 223 memset(s, 0, sizeof(*s));
224
d70e3b83 225 err = FSpGetFInfo(fss, &fi);
226 if (err != noErr) return err;
227 if (fi.fdFlags & kIsStationery)
98f59a35 228 s->hasfile = FALSE;
229 else {
230 s->hasfile = TRUE;
d70e3b83 231 s->savefile = *fss;
98f59a35 232 }
2b8dca9e 233
d70e3b83 234 sesshandle = open_settings_r_fsp(fss);
235 if (sesshandle == NULL) {
236 /* XXX need a way to pass up an error number */
237 err = -9999;
238 goto fail;
239 }
240 load_open_settings(sesshandle, TRUE, &s->cfg);
241 close_settings_r(sesshandle);
242
2b8dca9e 243 mac_startsession(s);
d70e3b83 244 return noErr;
2b8dca9e 245
246 fail:
247 sfree(s);
d70e3b83 248 return err;
249}
250
9ef3c1ca 251static OSErr mac_openlist(AEDesc docs)
252{
253 OSErr err;
254 long ndocs, i;
255 FSSpec fss;
256 AEKeyword keywd;
257 DescType type;
258 Size size;
259
260 err = AECountItems(&docs, &ndocs);
261 if (err != noErr) return err;
262
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;
269 }
270 return noErr;
271}
272
cef180ab 273void mac_opensession(void)
274{
9ef3c1ca 275
276 if (mac_gestalts.navsvers > 0) {
277 NavReplyRecord navr;
278 NavDialogOptions navopts;
279 NavTypeListHandle navtypes;
280 AEDesc defaultloc = { 'null', NULL };
281 AEDesc *navdefault = NULL;
282 short vol;
283 long dirid;
284 FSSpec fss;
285
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);
301 }
cef180ab 302#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
9ef3c1ca 303 else {
304 StandardFileReply sfr;
305 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
d70e3b83 306
9ef3c1ca 307 StandardGetFile(NULL, 1, sftypes, &sfr);
308 if (!sfr.sfGood) return;
d70e3b83 309
9ef3c1ca 310 mac_opensessionfrom(&sfr.sfFile);
311 /* XXX handle error */
312 }
cef180ab 313#endif
2b8dca9e 314}
315
b537dd42 316void mac_savesession(void)
317{
347bfcd7 318 Session *s = mac_windowsession(FrontWindow());
98f59a35 319 void *sesshandle;
b537dd42 320
98f59a35 321 assert(s->hasfile);
322 sesshandle = open_settings_w_fsp(&s->savefile);
323 if (sesshandle == NULL) return; /* XXX report error */
324 save_open_settings(sesshandle, TRUE, &s->cfg);
325 close_settings_w(sesshandle);
b537dd42 326}
327
328void mac_savesessionas(void)
329{
cef180ab 330#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
347bfcd7 331 Session *s = mac_windowsession(FrontWindow());
b537dd42 332 StandardFileReply sfr;
333 void *sesshandle;
334
98f59a35 335 StandardPutFile("\pSave session as:",
336 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
b537dd42 337 if (!sfr.sfGood) return;
338
339 if (!sfr.sfReplacing) {
340 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
341 if (ResError() != noErr) return; /* XXX report error */
342 }
343 sesshandle = open_settings_w_fsp(&sfr.sfFile);
344 if (sesshandle == NULL) return; /* XXX report error */
345 save_open_settings(sesshandle, TRUE, &s->cfg);
346 close_settings_w(sesshandle);
98f59a35 347 s->hasfile = TRUE;
348 s->savefile = sfr.sfFile;
cef180ab 349#endif
b537dd42 350}
351
d70e3b83 352pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
353 long refcon)
354{
355 DescType type;
356 Size size;
357
358 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
359 &type, NULL, 0, &size) == noErr)
360 return errAEParamMissed;
361
362 /* XXX we should do something here. */
363 return noErr;
364}
365
366pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
367 long refcon)
368{
369 DescType type;
d70e3b83 370 Size size;
371 AEDescList docs = { typeNull, NULL };
372 OSErr err;
d70e3b83 373
374 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
375 if (err != noErr) goto out;
376
377 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
378 &type, NULL, 0, &size) == noErr) {
379 err = errAEParamMissed;
380 goto out;
381 }
382
9ef3c1ca 383 err = mac_openlist(docs);
d70e3b83 384
385 out:
386 AEDisposeDesc(&docs);
387 return err;
388}
389
390pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
391 long refcon)
392{
393 DescType type;
394 Size size;
395
396 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
397 &type, NULL, 0, &size) == noErr)
398 return errAEParamMissed;
399
400 /* We can't meaningfully do anything here. */
401 return errAEEventNotHandled;
402}
403
6cb61a05 404/*
405 * Local Variables:
406 * c-file-style: "simon"
407 * End:
408 */