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