Pass a pointer to the entire dialog box structure to event handlers, rather
[sgt/putty] / mac / macdlg.c
CommitLineData
5cec2fd9 1/* $Id: macdlg.c,v 1.14 2003/03/17 22:38:18 ben Exp $ */
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>
6cb61a05 38#include <Windows.h>
39
98f59a35 40#include <assert.h>
6cb61a05 41#include <string.h>
42
43#include "putty.h"
8a7e67ec 44#include "dialog.h"
6cb61a05 45#include "mac.h"
46#include "macresid.h"
2b8dca9e 47#include "storage.h"
6cb61a05 48
f854b482 49static void mac_clickdlg(WindowPtr, EventRecord *);
50static void mac_activatedlg(WindowPtr, EventRecord *);
51static void mac_updatedlg(WindowPtr);
52static void mac_adjustdlgmenus(WindowPtr);
8a7e67ec 53static void mac_closedlg(WindowPtr);
f854b482 54
6cb61a05 55void mac_newsession(void)
56{
57 Session *s;
f854b482 58 WinInfo *wi;
8a7e67ec 59 static struct sesslist sesslist;
6cb61a05 60
6cb61a05 61 s = smalloc(sizeof(*s));
62 memset(s, 0, sizeof(*s));
63 do_defaults(NULL, &s->cfg);
98f59a35 64 s->hasfile = FALSE;
6cb61a05 65
8a7e67ec 66 if (HAVE_COLOR_QD())
67 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
68 else
69 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
70
71 get_sesslist(&sesslist, TRUE);
72 s->ctrlbox = ctrl_new_box();
73 setup_config_box(s->ctrlbox, &sesslist, FALSE, 0);
74
75 s->settings_ctrls.data = &s->cfg;
76 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
6cb61a05 77
f854b482 78 wi = smalloc(sizeof(*wi));
79 memset(wi, 0, sizeof(*wi));
80 wi->s = s;
5cec2fd9 81 wi->mcs = &s->settings_ctrls;
f854b482 82 wi->wtype = wSettings;
8a7e67ec 83 wi->update = &macctrl_update;
84 wi->click = &macctrl_click;
85 wi->activate = &macctrl_activate;
86 wi->adjustmenus = &macctrl_adjustmenus;
87 wi->close = &macctrl_close;
f854b482 88 SetWRefCon(s->settings_window, (long)wi);
6cb61a05 89 ShowWindow(s->settings_window);
90}
91
5211281f 92void mac_dupsession(void)
93{
347bfcd7 94 Session *s1 = mac_windowsession(FrontWindow());
5211281f 95 Session *s2;
96
97 s2 = smalloc(sizeof(*s2));
98 memset(s2, 0, sizeof(*s2));
99 s2->cfg = s1->cfg;
100 s2->hasfile = s1->hasfile;
101 s2->savefile = s1->savefile;
102
103 mac_startsession(s2);
104}
105
d70e3b83 106static OSErr mac_opensessionfrom(FSSpec *fss)
107{
108 FInfo fi;
2b8dca9e 109 Session *s;
2b8dca9e 110 void *sesshandle;
d70e3b83 111 OSErr err;
2b8dca9e 112
113 s = smalloc(sizeof(*s));
114 memset(s, 0, sizeof(*s));
115
d70e3b83 116 err = FSpGetFInfo(fss, &fi);
117 if (err != noErr) return err;
118 if (fi.fdFlags & kIsStationery)
98f59a35 119 s->hasfile = FALSE;
120 else {
121 s->hasfile = TRUE;
d70e3b83 122 s->savefile = *fss;
98f59a35 123 }
2b8dca9e 124
d70e3b83 125 sesshandle = open_settings_r_fsp(fss);
126 if (sesshandle == NULL) {
127 /* XXX need a way to pass up an error number */
128 err = -9999;
129 goto fail;
130 }
131 load_open_settings(sesshandle, TRUE, &s->cfg);
132 close_settings_r(sesshandle);
133
2b8dca9e 134 mac_startsession(s);
d70e3b83 135 return noErr;
2b8dca9e 136
137 fail:
138 sfree(s);
d70e3b83 139 return err;
140}
141
9ef3c1ca 142static OSErr mac_openlist(AEDesc docs)
143{
144 OSErr err;
145 long ndocs, i;
146 FSSpec fss;
147 AEKeyword keywd;
148 DescType type;
149 Size size;
150
151 err = AECountItems(&docs, &ndocs);
152 if (err != noErr) return err;
153
154 for (i = 0; i < ndocs; i++) {
155 err = AEGetNthPtr(&docs, i + 1, typeFSS,
156 &keywd, &type, &fss, sizeof(fss), &size);
157 if (err != noErr) return err;;
158 err = mac_opensessionfrom(&fss);
159 if (err != noErr) return err;
160 }
161 return noErr;
162}
163
cef180ab 164void mac_opensession(void)
165{
9ef3c1ca 166
167 if (mac_gestalts.navsvers > 0) {
168 NavReplyRecord navr;
169 NavDialogOptions navopts;
170 NavTypeListHandle navtypes;
171 AEDesc defaultloc = { 'null', NULL };
172 AEDesc *navdefault = NULL;
173 short vol;
174 long dirid;
175 FSSpec fss;
176
177 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
178 /* XXX should we create sessions dir? */
179 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
180 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
181 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
182 navdefault = &defaultloc;
183 /* Can't meaningfully preview a saved session yet */
184 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
185 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
186 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
187 NULL) == noErr && navr.validRecord)
188 mac_openlist(navr.selection);
189 NavDisposeReply(&navr);
190 if (navtypes != NULL)
191 ReleaseResource((Handle)navtypes);
192 }
cef180ab 193#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
9ef3c1ca 194 else {
195 StandardFileReply sfr;
196 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
d70e3b83 197
9ef3c1ca 198 StandardGetFile(NULL, 1, sftypes, &sfr);
199 if (!sfr.sfGood) return;
d70e3b83 200
9ef3c1ca 201 mac_opensessionfrom(&sfr.sfFile);
202 /* XXX handle error */
203 }
cef180ab 204#endif
2b8dca9e 205}
206
b537dd42 207void mac_savesession(void)
208{
347bfcd7 209 Session *s = mac_windowsession(FrontWindow());
98f59a35 210 void *sesshandle;
b537dd42 211
98f59a35 212 assert(s->hasfile);
213 sesshandle = open_settings_w_fsp(&s->savefile);
214 if (sesshandle == NULL) return; /* XXX report error */
215 save_open_settings(sesshandle, TRUE, &s->cfg);
216 close_settings_w(sesshandle);
b537dd42 217}
218
219void mac_savesessionas(void)
220{
cef180ab 221#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
347bfcd7 222 Session *s = mac_windowsession(FrontWindow());
b537dd42 223 StandardFileReply sfr;
224 void *sesshandle;
225
98f59a35 226 StandardPutFile("\pSave session as:",
227 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
b537dd42 228 if (!sfr.sfGood) return;
229
230 if (!sfr.sfReplacing) {
231 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
232 if (ResError() != noErr) return; /* XXX report error */
233 }
234 sesshandle = open_settings_w_fsp(&sfr.sfFile);
235 if (sesshandle == NULL) return; /* XXX report error */
236 save_open_settings(sesshandle, TRUE, &s->cfg);
237 close_settings_w(sesshandle);
98f59a35 238 s->hasfile = TRUE;
239 s->savefile = sfr.sfFile;
cef180ab 240#endif
b537dd42 241}
242
d70e3b83 243pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
244 long refcon)
245{
246 DescType type;
247 Size size;
248
249 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
250 &type, NULL, 0, &size) == noErr)
251 return errAEParamMissed;
252
253 /* XXX we should do something here. */
254 return noErr;
255}
256
257pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
258 long refcon)
259{
260 DescType type;
d70e3b83 261 Size size;
262 AEDescList docs = { typeNull, NULL };
263 OSErr err;
d70e3b83 264
265 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
266 if (err != noErr) goto out;
267
268 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
269 &type, NULL, 0, &size) == noErr) {
270 err = errAEParamMissed;
271 goto out;
272 }
273
9ef3c1ca 274 err = mac_openlist(docs);
d70e3b83 275
276 out:
277 AEDisposeDesc(&docs);
278 return err;
279}
280
281pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
282 long refcon)
283{
284 DescType type;
285 Size size;
286
287 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
288 &type, NULL, 0, &size) == noErr)
289 return errAEParamMissed;
290
291 /* We can't meaningfully do anything here. */
292 return errAEEventNotHandled;
293}
294
6cb61a05 295/*
296 * Local Variables:
297 * c-file-style: "simon"
298 * End:
299 */