3 * Copyright (c) 2002 Ben Harris
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
29 * macdlg.c - settings dialogue box for Mac OS.
33 #include <AEDataModel.h>
34 #include <AppleEvents.h>
35 #include <Navigation.h>
36 #include <Resources.h>
37 #include <StandardFile.h>
38 #include <TextUtils.h>
50 static void mac_config(int);
51 static void mac_closedlg(WindowPtr
);
52 static void mac_enddlg_config(WindowPtr
, int);
53 static void mac_enddlg_reconfig(WindowPtr
, int);
55 void mac_newsession(void)
60 void mac_reconfig(void)
65 static void mac_config(int midsession
)
69 static struct sesslist sesslist
;
74 s
= mac_windowsession(FrontWindow());
77 memset(s
, 0, sizeof(*s
));
78 do_defaults(NULL
, &s
->cfg
);
83 s
->settings_window
= GetNewCWindow(wSettings
, NULL
, (WindowPtr
)-1);
85 s
->settings_window
= GetNewWindow(wSettings
, NULL
, (WindowPtr
)-1);
87 get_sesslist(&sesslist
, TRUE
);
88 s
->ctrlbox
= ctrl_new_box();
89 setup_config_box(s
->ctrlbox
, &sesslist
, midsession
, 0, 0);
91 s
->settings_ctrls
.data
= &s
->cfg
;
93 s
->settings_ctrls
.end
= &mac_enddlg_reconfig
;
95 s
->settings_ctrls
.end
= &mac_enddlg_config
;
97 macctrl_layoutbox(s
->ctrlbox
, s
->settings_window
, &s
->settings_ctrls
);
100 memset(wi
, 0, sizeof(*wi
));
102 wi
->mcs
= &s
->settings_ctrls
;
103 wi
->wtype
= wSettings
;
104 wi
->update
= &macctrl_update
;
105 wi
->click
= &macctrl_click
;
106 wi
->key
= &macctrl_key
;
107 wi
->activate
= &macctrl_activate
;
108 wi
->adjustmenus
= &macctrl_adjustmenus
;
109 wi
->close
= &mac_closedlg
;
110 SetWRefCon(s
->settings_window
, (long)wi
);
112 str
= dupprintf("%s Reconfiguration", appname
);
114 str
= dupprintf("%s Configuration", appname
);
115 c2pstrcpy(mactitle
, str
);
117 SetWTitle(s
->settings_window
, mactitle
);
118 ShowWindow(s
->settings_window
);
121 static void mac_closedlg(WindowPtr window
)
123 Session
*s
= mac_windowsession(window
);
125 macctrl_close(window
);
126 DisposeWindow(window
);
127 if (s
->window
== NULL
)
131 static void mac_enddlg_config(WindowPtr window
, int value
)
133 Session
*s
= mac_windowsession(window
);
136 mac_closedlg(window
);
139 mac_closedlg(window
);
143 static void mac_enddlg_reconfig(WindowPtr window
, int value
)
145 Session
*s
= mac_windowsession(window
);
148 mac_closedlg(window
);
150 mac_closedlg(window
);
154 void mac_dupsession(void)
156 Session
*s1
= mac_windowsession(FrontWindow());
160 memset(s2
, 0, sizeof(*s2
));
162 s2
->hasfile
= s1
->hasfile
;
163 s2
->savefile
= s1
->savefile
;
165 mac_startsession(s2
);
168 static OSErr
mac_opensessionfrom(FSSpec
*fss
)
176 memset(s
, 0, sizeof(*s
));
178 err
= FSpGetFInfo(fss
, &fi
);
179 if (err
!= noErr
) return err
;
180 if (fi
.fdFlags
& kIsStationery
)
187 sesshandle
= open_settings_r_fsp(fss
);
188 if (sesshandle
== NULL
) {
189 /* XXX need a way to pass up an error number */
193 load_open_settings(sesshandle
, TRUE
, &s
->cfg
);
194 close_settings_r(sesshandle
);
204 static OSErr
mac_openlist(AEDesc docs
)
213 err
= AECountItems(&docs
, &ndocs
);
214 if (err
!= noErr
) return err
;
216 for (i
= 0; i
< ndocs
; i
++) {
217 err
= AEGetNthPtr(&docs
, i
+ 1, typeFSS
,
218 &keywd
, &type
, &fss
, sizeof(fss
), &size
);
219 if (err
!= noErr
) return err
;;
220 err
= mac_opensessionfrom(&fss
);
221 if (err
!= noErr
) return err
;
226 void mac_opensession(void)
229 if (mac_gestalts
.navsvers
> 0) {
231 NavDialogOptions navopts
;
232 NavTypeListHandle navtypes
;
233 AEDesc defaultloc
= { 'null', NULL
};
234 AEDesc
*navdefault
= NULL
;
239 if (NavGetDefaultDialogOptions(&navopts
) != noErr
) return;
240 /* XXX should we create sessions dir? */
241 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
&&
242 FSMakeFSSpec(vol
, dirid
, NULL
, &fss
) == noErr
&&
243 AECreateDesc(typeFSS
, &fss
, sizeof(fss
), &defaultloc
) == noErr
)
244 navdefault
= &defaultloc
;
245 /* Can't meaningfully preview a saved session yet */
246 navopts
.dialogOptionFlags
&= ~kNavAllowPreviews
;
247 navtypes
= (NavTypeListHandle
)GetResource('open', open_pTTY
);
248 if (NavGetFile(navdefault
, &navr
, &navopts
, NULL
, NULL
, NULL
, navtypes
,
249 NULL
) == noErr
&& navr
.validRecord
)
250 mac_openlist(navr
.selection
);
251 NavDisposeReply(&navr
);
252 if (navtypes
!= NULL
)
253 ReleaseResource((Handle
)navtypes
);
255 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
257 StandardFileReply sfr
;
258 static const OSType sftypes
[] = { 'Sess', 0, 0, 0 };
260 StandardGetFile(NULL
, 1, sftypes
, &sfr
);
261 if (!sfr
.sfGood
) return;
263 mac_opensessionfrom(&sfr
.sfFile
);
264 /* XXX handle error */
269 void mac_savesession(void)
271 Session
*s
= mac_windowsession(FrontWindow());
275 sesshandle
= open_settings_w_fsp(&s
->savefile
);
276 if (sesshandle
== NULL
) return; /* XXX report error */
277 save_open_settings(sesshandle
, TRUE
, &s
->cfg
);
278 close_settings_w(sesshandle
);
281 void mac_savesessionas(void)
283 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
284 Session
*s
= mac_windowsession(FrontWindow());
285 StandardFileReply sfr
;
288 StandardPutFile("\pSave session as:",
289 s
->hasfile ? s
->savefile
.name
: "\puntitled", &sfr
);
290 if (!sfr
.sfGood
) return;
292 if (!sfr
.sfReplacing
) {
293 FSpCreateResFile(&sfr
.sfFile
, PUTTY_CREATOR
, SESS_TYPE
, sfr
.sfScript
);
294 if (ResError() != noErr
) return; /* XXX report error */
296 sesshandle
= open_settings_w_fsp(&sfr
.sfFile
);
297 if (sesshandle
== NULL
) return; /* XXX report error */
298 save_open_settings(sesshandle
, TRUE
, &s
->cfg
);
299 close_settings_w(sesshandle
);
301 s
->savefile
= sfr
.sfFile
;
305 pascal OSErr
mac_aevt_oapp(const AppleEvent
*req
, AppleEvent
*reply
,
311 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
312 &type
, NULL
, 0, &size
) == noErr
)
313 return errAEParamMissed
;
315 /* XXX we should do something here. */
319 pascal OSErr
mac_aevt_odoc(const AppleEvent
*req
, AppleEvent
*reply
,
324 AEDescList docs
= { typeNull
, NULL
};
327 err
= AEGetParamDesc(req
, keyDirectObject
, typeAEList
, &docs
);
328 if (err
!= noErr
) goto out
;
330 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
331 &type
, NULL
, 0, &size
) == noErr
) {
332 err
= errAEParamMissed
;
336 err
= mac_openlist(docs
);
339 AEDisposeDesc(&docs
);
343 pascal OSErr
mac_aevt_pdoc(const AppleEvent
*req
, AppleEvent
*reply
,
349 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
350 &type
, NULL
, 0, &size
) == noErr
)
351 return errAEParamMissed
;
353 /* We can't meaningfully do anything here. */
354 return errAEEventNotHandled
;
359 * c-file-style: "simon"