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