If I() and S() aren't inline, provide a prototype even if we're defining
[sgt/putty] / mac / macdlg.c
CommitLineData
f854b482 1/* $Id: macdlg.c,v 1.12 2003/02/15 16:22:15 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>
6cb61a05 35#include <Dialogs.h>
9ef3c1ca 36#include <Navigation.h>
b537dd42 37#include <Resources.h>
2b8dca9e 38#include <StandardFile.h>
6cb61a05 39#include <Windows.h>
40
98f59a35 41#include <assert.h>
6cb61a05 42#include <string.h>
43
44#include "putty.h"
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);
53
6cb61a05 54void mac_newsession(void)
55{
56 Session *s;
f854b482 57 WinInfo *wi;
6cb61a05 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);
98f59a35 63 s->hasfile = FALSE;
6cb61a05 64
cef180ab 65 s->settings_window =
66 GetDialogWindow(GetNewDialog(wSettings, NULL, (WindowPtr)-1));
6cb61a05 67
f854b482 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);
6cb61a05 77 ShowWindow(s->settings_window);
78}
79
5211281f 80void mac_dupsession(void)
81{
347bfcd7 82 Session *s1 = mac_windowsession(FrontWindow());
5211281f 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
d70e3b83 94static OSErr mac_opensessionfrom(FSSpec *fss)
95{
96 FInfo fi;
2b8dca9e 97 Session *s;
2b8dca9e 98 void *sesshandle;
d70e3b83 99 OSErr err;
2b8dca9e 100
101 s = smalloc(sizeof(*s));
102 memset(s, 0, sizeof(*s));
103
d70e3b83 104 err = FSpGetFInfo(fss, &fi);
105 if (err != noErr) return err;
106 if (fi.fdFlags & kIsStationery)
98f59a35 107 s->hasfile = FALSE;
108 else {
109 s->hasfile = TRUE;
d70e3b83 110 s->savefile = *fss;
98f59a35 111 }
2b8dca9e 112
d70e3b83 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
2b8dca9e 122 mac_startsession(s);
d70e3b83 123 return noErr;
2b8dca9e 124
125 fail:
126 sfree(s);
d70e3b83 127 return err;
128}
129
9ef3c1ca 130static 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
cef180ab 152void mac_opensession(void)
153{
9ef3c1ca 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 }
cef180ab 181#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
9ef3c1ca 182 else {
183 StandardFileReply sfr;
184 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
d70e3b83 185
9ef3c1ca 186 StandardGetFile(NULL, 1, sftypes, &sfr);
187 if (!sfr.sfGood) return;
d70e3b83 188
9ef3c1ca 189 mac_opensessionfrom(&sfr.sfFile);
190 /* XXX handle error */
191 }
cef180ab 192#endif
2b8dca9e 193}
194
b537dd42 195void mac_savesession(void)
196{
347bfcd7 197 Session *s = mac_windowsession(FrontWindow());
98f59a35 198 void *sesshandle;
b537dd42 199
98f59a35 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);
b537dd42 205}
206
207void mac_savesessionas(void)
208{
cef180ab 209#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
347bfcd7 210 Session *s = mac_windowsession(FrontWindow());
b537dd42 211 StandardFileReply sfr;
212 void *sesshandle;
213
98f59a35 214 StandardPutFile("\pSave session as:",
215 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
b537dd42 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);
98f59a35 226 s->hasfile = TRUE;
227 s->savefile = sfr.sfFile;
cef180ab 228#endif
b537dd42 229}
230
d70e3b83 231pascal 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
245pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
246 long refcon)
247{
248 DescType type;
d70e3b83 249 Size size;
250 AEDescList docs = { typeNull, NULL };
251 OSErr err;
d70e3b83 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
9ef3c1ca 262 err = mac_openlist(docs);
d70e3b83 263
264 out:
265 AEDisposeDesc(&docs);
266 return err;
267}
268
269pascal 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
f854b482 283static void mac_activatedlg(WindowPtr window, EventRecord *event)
6cb61a05 284{
285 DialogItemType itemtype;
286 Handle itemhandle;
287 short item;
288 Rect itemrect;
289 int active;
cef180ab 290 DialogRef dialog = GetDialogFromWindow(window);
6cb61a05 291
292 active = (event->modifiers & activeFlag) != 0;
cef180ab 293 GetDialogItem(dialog, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
6cb61a05 294 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
cef180ab 295 DialogSelect(event, &dialog, &item);
6cb61a05 296}
297
f854b482 298static void mac_clickdlg(WindowPtr window, EventRecord *event)
6cb61a05 299{
300 short item;
347bfcd7 301 Session *s = mac_windowsession(window);
cef180ab 302 DialogRef dialog = GetDialogFromWindow(window);
6cb61a05 303
cef180ab 304 if (DialogSelect(event, &dialog, &item))
6cb61a05 305 switch (item) {
306 case wiSettingsOpen:
cef180ab 307 HideWindow(window);
6cb61a05 308 mac_startsession(s);
309 break;
310 }
311}
312
f854b482 313static 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
335static 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
6cb61a05 348/*
349 * Local Variables:
350 * c-file-style: "simon"
351 * End:
352 */