ad7a1ffac4a95d13d4ae7a08f13abda0057b0000
[sgt/putty] / mac / macdlg.c
1 /* $Id: macdlg.c,v 1.12 2003/02/15 16:22:15 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 <Dialogs.h>
36 #include <Navigation.h>
37 #include <Resources.h>
38 #include <StandardFile.h>
39 #include <Windows.h>
40
41 #include <assert.h>
42 #include <string.h>
43
44 #include "putty.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
54 void mac_newsession(void)
55 {
56 Session *s;
57 WinInfo *wi;
58
59 /* This should obviously be initialised by other means */
60 s = smalloc(sizeof(*s));
61 memset(s, 0, sizeof(*s));
62 do_defaults(NULL, &s->cfg);
63 s->hasfile = FALSE;
64
65 s->settings_window =
66 GetDialogWindow(GetNewDialog(wSettings, NULL, (WindowPtr)-1));
67
68 wi = smalloc(sizeof(*wi));
69 memset(wi, 0, sizeof(*wi));
70 wi->s = s;
71 wi->wtype = wSettings;
72 wi->update = &mac_updatedlg;
73 wi->click = &mac_clickdlg;
74 wi->activate = &mac_activatedlg;
75 wi->adjustmenus = &mac_adjustdlgmenus;
76 SetWRefCon(s->settings_window, (long)wi);
77 ShowWindow(s->settings_window);
78 }
79
80 void mac_dupsession(void)
81 {
82 Session *s1 = mac_windowsession(FrontWindow());
83 Session *s2;
84
85 s2 = smalloc(sizeof(*s2));
86 memset(s2, 0, sizeof(*s2));
87 s2->cfg = s1->cfg;
88 s2->hasfile = s1->hasfile;
89 s2->savefile = s1->savefile;
90
91 mac_startsession(s2);
92 }
93
94 static OSErr mac_opensessionfrom(FSSpec *fss)
95 {
96 FInfo fi;
97 Session *s;
98 void *sesshandle;
99 OSErr err;
100
101 s = smalloc(sizeof(*s));
102 memset(s, 0, sizeof(*s));
103
104 err = FSpGetFInfo(fss, &fi);
105 if (err != noErr) return err;
106 if (fi.fdFlags & kIsStationery)
107 s->hasfile = FALSE;
108 else {
109 s->hasfile = TRUE;
110 s->savefile = *fss;
111 }
112
113 sesshandle = open_settings_r_fsp(fss);
114 if (sesshandle == NULL) {
115 /* XXX need a way to pass up an error number */
116 err = -9999;
117 goto fail;
118 }
119 load_open_settings(sesshandle, TRUE, &s->cfg);
120 close_settings_r(sesshandle);
121
122 mac_startsession(s);
123 return noErr;
124
125 fail:
126 sfree(s);
127 return err;
128 }
129
130 static OSErr mac_openlist(AEDesc docs)
131 {
132 OSErr err;
133 long ndocs, i;
134 FSSpec fss;
135 AEKeyword keywd;
136 DescType type;
137 Size size;
138
139 err = AECountItems(&docs, &ndocs);
140 if (err != noErr) return err;
141
142 for (i = 0; i < ndocs; i++) {
143 err = AEGetNthPtr(&docs, i + 1, typeFSS,
144 &keywd, &type, &fss, sizeof(fss), &size);
145 if (err != noErr) return err;;
146 err = mac_opensessionfrom(&fss);
147 if (err != noErr) return err;
148 }
149 return noErr;
150 }
151
152 void mac_opensession(void)
153 {
154
155 if (mac_gestalts.navsvers > 0) {
156 NavReplyRecord navr;
157 NavDialogOptions navopts;
158 NavTypeListHandle navtypes;
159 AEDesc defaultloc = { 'null', NULL };
160 AEDesc *navdefault = NULL;
161 short vol;
162 long dirid;
163 FSSpec fss;
164
165 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
166 /* XXX should we create sessions dir? */
167 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
168 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
169 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
170 navdefault = &defaultloc;
171 /* Can't meaningfully preview a saved session yet */
172 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
173 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
174 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
175 NULL) == noErr && navr.validRecord)
176 mac_openlist(navr.selection);
177 NavDisposeReply(&navr);
178 if (navtypes != NULL)
179 ReleaseResource((Handle)navtypes);
180 }
181 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
182 else {
183 StandardFileReply sfr;
184 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
185
186 StandardGetFile(NULL, 1, sftypes, &sfr);
187 if (!sfr.sfGood) return;
188
189 mac_opensessionfrom(&sfr.sfFile);
190 /* XXX handle error */
191 }
192 #endif
193 }
194
195 void mac_savesession(void)
196 {
197 Session *s = mac_windowsession(FrontWindow());
198 void *sesshandle;
199
200 assert(s->hasfile);
201 sesshandle = open_settings_w_fsp(&s->savefile);
202 if (sesshandle == NULL) return; /* XXX report error */
203 save_open_settings(sesshandle, TRUE, &s->cfg);
204 close_settings_w(sesshandle);
205 }
206
207 void mac_savesessionas(void)
208 {
209 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
210 Session *s = mac_windowsession(FrontWindow());
211 StandardFileReply sfr;
212 void *sesshandle;
213
214 StandardPutFile("\pSave session as:",
215 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
216 if (!sfr.sfGood) return;
217
218 if (!sfr.sfReplacing) {
219 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
220 if (ResError() != noErr) return; /* XXX report error */
221 }
222 sesshandle = open_settings_w_fsp(&sfr.sfFile);
223 if (sesshandle == NULL) return; /* XXX report error */
224 save_open_settings(sesshandle, TRUE, &s->cfg);
225 close_settings_w(sesshandle);
226 s->hasfile = TRUE;
227 s->savefile = sfr.sfFile;
228 #endif
229 }
230
231 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
232 long refcon)
233 {
234 DescType type;
235 Size size;
236
237 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
238 &type, NULL, 0, &size) == noErr)
239 return errAEParamMissed;
240
241 /* XXX we should do something here. */
242 return noErr;
243 }
244
245 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
246 long refcon)
247 {
248 DescType type;
249 Size size;
250 AEDescList docs = { typeNull, NULL };
251 OSErr err;
252
253 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
254 if (err != noErr) goto out;
255
256 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
257 &type, NULL, 0, &size) == noErr) {
258 err = errAEParamMissed;
259 goto out;
260 }
261
262 err = mac_openlist(docs);
263
264 out:
265 AEDisposeDesc(&docs);
266 return err;
267 }
268
269 pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
270 long refcon)
271 {
272 DescType type;
273 Size size;
274
275 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
276 &type, NULL, 0, &size) == noErr)
277 return errAEParamMissed;
278
279 /* We can't meaningfully do anything here. */
280 return errAEEventNotHandled;
281 }
282
283 static void mac_activatedlg(WindowPtr window, EventRecord *event)
284 {
285 DialogItemType itemtype;
286 Handle itemhandle;
287 short item;
288 Rect itemrect;
289 int active;
290 DialogRef dialog = GetDialogFromWindow(window);
291
292 active = (event->modifiers & activeFlag) != 0;
293 GetDialogItem(dialog, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
294 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
295 DialogSelect(event, &dialog, &item);
296 }
297
298 static void mac_clickdlg(WindowPtr window, EventRecord *event)
299 {
300 short item;
301 Session *s = mac_windowsession(window);
302 DialogRef dialog = GetDialogFromWindow(window);
303
304 if (DialogSelect(event, &dialog, &item))
305 switch (item) {
306 case wiSettingsOpen:
307 HideWindow(window);
308 mac_startsession(s);
309 break;
310 }
311 }
312
313 static void mac_updatedlg(WindowPtr window)
314 {
315 #if TARGET_API_MAC_CARBON
316 RgnHandle rgn;
317 #endif
318
319 BeginUpdate(window);
320 #if TARGET_API_MAC_CARBON
321 rgn = NewRgn();
322 GetPortVisibleRegion(GetWindowPort(window), rgn);
323 UpdateDialog(GetDialogFromWindow(window), rgn);
324 DisposeRgn(rgn);
325 #else
326 UpdateDialog(window, window->visRgn);
327 #endif
328 EndUpdate(window);
329 }
330
331 #if TARGET_API_MAC_CARBON
332 #define EnableItem EnableMenuItem
333 #define DisableItem DisableMenuItem
334 #endif
335 static void mac_adjustdlgmenus(WindowPtr window)
336 {
337 MenuHandle menu;
338
339 menu = GetMenuHandle(mFile);
340 DisableItem(menu, iSave); /* XXX enable if modified */
341 EnableItem(menu, iSaveAs);
342 EnableItem(menu, iDuplicate);
343
344 menu = GetMenuHandle(mEdit);
345 DisableItem(menu, 0);
346 }
347
348 /*
349 * Local Variables:
350 * c-file-style: "simon"
351 * End:
352 */