Partial implementation of the platform-independent dialogue-box interface
[sgt/putty] / mac / macdlg.c
1 /* $Id: macdlg.c,v 1.13 2003/03/17 21:40:37 ben Exp $ */
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 <Windows.h>
39
40 #include <assert.h>
41 #include <string.h>
42
43 #include "putty.h"
44 #include "dialog.h"
45 #include "mac.h"
46 #include "macresid.h"
47 #include "storage.h"
48
49 static void mac_clickdlg(WindowPtr, EventRecord *);
50 static void mac_activatedlg(WindowPtr, EventRecord *);
51 static void mac_updatedlg(WindowPtr);
52 static void mac_adjustdlgmenus(WindowPtr);
53 static void mac_closedlg(WindowPtr);
54
55 void mac_newsession(void)
56 {
57 Session *s;
58 WinInfo *wi;
59 static struct sesslist sesslist;
60
61 s = smalloc(sizeof(*s));
62 memset(s, 0, sizeof(*s));
63 do_defaults(NULL, &s->cfg);
64 s->hasfile = FALSE;
65
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);
77
78 wi = smalloc(sizeof(*wi));
79 memset(wi, 0, sizeof(*wi));
80 wi->s = s;
81 wi->mcs = s->settings_ctrls;
82 wi->wtype = wSettings;
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;
88 SetWRefCon(s->settings_window, (long)wi);
89 ShowWindow(s->settings_window);
90 }
91
92 void mac_dupsession(void)
93 {
94 Session *s1 = mac_windowsession(FrontWindow());
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
106 static OSErr mac_opensessionfrom(FSSpec *fss)
107 {
108 FInfo fi;
109 Session *s;
110 void *sesshandle;
111 OSErr err;
112
113 s = smalloc(sizeof(*s));
114 memset(s, 0, sizeof(*s));
115
116 err = FSpGetFInfo(fss, &fi);
117 if (err != noErr) return err;
118 if (fi.fdFlags & kIsStationery)
119 s->hasfile = FALSE;
120 else {
121 s->hasfile = TRUE;
122 s->savefile = *fss;
123 }
124
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
134 mac_startsession(s);
135 return noErr;
136
137 fail:
138 sfree(s);
139 return err;
140 }
141
142 static 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
164 void mac_opensession(void)
165 {
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 }
193 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
194 else {
195 StandardFileReply sfr;
196 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
197
198 StandardGetFile(NULL, 1, sftypes, &sfr);
199 if (!sfr.sfGood) return;
200
201 mac_opensessionfrom(&sfr.sfFile);
202 /* XXX handle error */
203 }
204 #endif
205 }
206
207 void mac_savesession(void)
208 {
209 Session *s = mac_windowsession(FrontWindow());
210 void *sesshandle;
211
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);
217 }
218
219 void mac_savesessionas(void)
220 {
221 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
222 Session *s = mac_windowsession(FrontWindow());
223 StandardFileReply sfr;
224 void *sesshandle;
225
226 StandardPutFile("\pSave session as:",
227 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
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);
238 s->hasfile = TRUE;
239 s->savefile = sfr.sfFile;
240 #endif
241 }
242
243 pascal 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
257 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
258 long refcon)
259 {
260 DescType type;
261 Size size;
262 AEDescList docs = { typeNull, NULL };
263 OSErr err;
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
274 err = mac_openlist(docs);
275
276 out:
277 AEDisposeDesc(&docs);
278 return err;
279 }
280
281 pascal 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
295 /*
296 * Local Variables:
297 * c-file-style: "simon"
298 * End:
299 */