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
);
116 SetWTitle(s
->settings_window
, mactitle
);
117 ShowWindow(s
->settings_window
);
120 static void mac_closedlg(WindowPtr window
)
122 Session
*s
= mac_windowsession(window
);
124 macctrl_close(window
);
125 DisposeWindow(window
);
126 if (s
->window
== NULL
)
130 static void mac_enddlg_config(WindowPtr window
, int value
)
132 Session
*s
= mac_windowsession(window
);
135 mac_closedlg(window
);
138 mac_closedlg(window
);
142 static void mac_enddlg_reconfig(WindowPtr window
, int value
)
144 Session
*s
= mac_windowsession(window
);
147 mac_closedlg(window
);
149 mac_closedlg(window
);
153 void mac_dupsession(void)
155 Session
*s1
= mac_windowsession(FrontWindow());
159 memset(s2
, 0, sizeof(*s2
));
161 s2
->hasfile
= s1
->hasfile
;
162 s2
->savefile
= s1
->savefile
;
164 mac_startsession(s2
);
167 static OSErr
mac_opensessionfrom(FSSpec
*fss
)
175 memset(s
, 0, sizeof(*s
));
177 err
= FSpGetFInfo(fss
, &fi
);
178 if (err
!= noErr
) return err
;
179 if (fi
.fdFlags
& kIsStationery
)
186 sesshandle
= open_settings_r_fsp(fss
);
187 if (sesshandle
== NULL
) {
188 /* XXX need a way to pass up an error number */
192 load_open_settings(sesshandle
, TRUE
, &s
->cfg
);
193 close_settings_r(sesshandle
);
203 static OSErr
mac_openlist(AEDesc docs
)
212 err
= AECountItems(&docs
, &ndocs
);
213 if (err
!= noErr
) return err
;
215 for (i
= 0; i
< ndocs
; i
++) {
216 err
= AEGetNthPtr(&docs
, i
+ 1, typeFSS
,
217 &keywd
, &type
, &fss
, sizeof(fss
), &size
);
218 if (err
!= noErr
) return err
;;
219 err
= mac_opensessionfrom(&fss
);
220 if (err
!= noErr
) return err
;
225 void mac_opensession(void)
228 if (mac_gestalts
.navsvers
> 0) {
230 NavDialogOptions navopts
;
231 NavTypeListHandle navtypes
;
232 AEDesc defaultloc
= { 'null', NULL
};
233 AEDesc
*navdefault
= NULL
;
238 if (NavGetDefaultDialogOptions(&navopts
) != noErr
) return;
239 /* XXX should we create sessions dir? */
240 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
&&
241 FSMakeFSSpec(vol
, dirid
, NULL
, &fss
) == noErr
&&
242 AECreateDesc(typeFSS
, &fss
, sizeof(fss
), &defaultloc
) == noErr
)
243 navdefault
= &defaultloc
;
244 /* Can't meaningfully preview a saved session yet */
245 navopts
.dialogOptionFlags
&= ~kNavAllowPreviews
;
246 navtypes
= (NavTypeListHandle
)GetResource('open', open_pTTY
);
247 if (NavGetFile(navdefault
, &navr
, &navopts
, NULL
, NULL
, NULL
, navtypes
,
248 NULL
) == noErr
&& navr
.validRecord
)
249 mac_openlist(navr
.selection
);
250 NavDisposeReply(&navr
);
251 if (navtypes
!= NULL
)
252 ReleaseResource((Handle
)navtypes
);
254 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
256 StandardFileReply sfr
;
257 static const OSType sftypes
[] = { 'Sess', 0, 0, 0 };
259 StandardGetFile(NULL
, 1, sftypes
, &sfr
);
260 if (!sfr
.sfGood
) return;
262 mac_opensessionfrom(&sfr
.sfFile
);
263 /* XXX handle error */
268 void mac_savesession(void)
270 Session
*s
= mac_windowsession(FrontWindow());
274 sesshandle
= open_settings_w_fsp(&s
->savefile
);
275 if (sesshandle
== NULL
) return; /* XXX report error */
276 save_open_settings(sesshandle
, TRUE
, &s
->cfg
);
277 close_settings_w(sesshandle
);
280 void mac_savesessionas(void)
282 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
283 Session
*s
= mac_windowsession(FrontWindow());
284 StandardFileReply sfr
;
287 StandardPutFile("\pSave session as:",
288 s
->hasfile ? s
->savefile
.name
: "\puntitled", &sfr
);
289 if (!sfr
.sfGood
) return;
291 if (!sfr
.sfReplacing
) {
292 FSpCreateResFile(&sfr
.sfFile
, PUTTY_CREATOR
, SESS_TYPE
, sfr
.sfScript
);
293 if (ResError() != noErr
) return; /* XXX report error */
295 sesshandle
= open_settings_w_fsp(&sfr
.sfFile
);
296 if (sesshandle
== NULL
) return; /* XXX report error */
297 save_open_settings(sesshandle
, TRUE
, &s
->cfg
);
298 close_settings_w(sesshandle
);
300 s
->savefile
= sfr
.sfFile
;
304 pascal OSErr
mac_aevt_oapp(const AppleEvent
*req
, AppleEvent
*reply
,
310 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
311 &type
, NULL
, 0, &size
) == noErr
)
312 return errAEParamMissed
;
314 /* XXX we should do something here. */
318 pascal OSErr
mac_aevt_odoc(const AppleEvent
*req
, AppleEvent
*reply
,
323 AEDescList docs
= { typeNull
, NULL
};
326 err
= AEGetParamDesc(req
, keyDirectObject
, typeAEList
, &docs
);
327 if (err
!= noErr
) goto out
;
329 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
330 &type
, NULL
, 0, &size
) == noErr
) {
331 err
= errAEParamMissed
;
335 err
= mac_openlist(docs
);
338 AEDisposeDesc(&docs
);
342 pascal OSErr
mac_aevt_pdoc(const AppleEvent
*req
, AppleEvent
*reply
,
348 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
349 &type
, NULL
, 0, &size
) == noErr
)
350 return errAEParamMissed
;
352 /* We can't meaningfully do anything here. */
353 return errAEEventNotHandled
;
358 * c-file-style: "simon"