1 /* $Id: macdlg.c,v 1.12 2003/02/15 16:22:15 ben Exp $ */
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>
36 #include <Navigation.h>
37 #include <Resources.h>
38 #include <StandardFile.h>
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
);
54 void mac_newsession(void)
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
);
66 GetDialogWindow(GetNewDialog(wSettings
, NULL
, (WindowPtr
)-1));
68 wi
= smalloc(sizeof(*wi
));
69 memset(wi
, 0, sizeof(*wi
));
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
);
80 void mac_dupsession(void)
82 Session
*s1
= mac_windowsession(FrontWindow());
85 s2
= smalloc(sizeof(*s2
));
86 memset(s2
, 0, sizeof(*s2
));
88 s2
->hasfile
= s1
->hasfile
;
89 s2
->savefile
= s1
->savefile
;
94 static OSErr
mac_opensessionfrom(FSSpec
*fss
)
101 s
= smalloc(sizeof(*s
));
102 memset(s
, 0, sizeof(*s
));
104 err
= FSpGetFInfo(fss
, &fi
);
105 if (err
!= noErr
) return err
;
106 if (fi
.fdFlags
& kIsStationery
)
113 sesshandle
= open_settings_r_fsp(fss
);
114 if (sesshandle
== NULL
) {
115 /* XXX need a way to pass up an error number */
119 load_open_settings(sesshandle
, TRUE
, &s
->cfg
);
120 close_settings_r(sesshandle
);
130 static OSErr
mac_openlist(AEDesc docs
)
139 err
= AECountItems(&docs
, &ndocs
);
140 if (err
!= noErr
) return err
;
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
;
152 void mac_opensession(void)
155 if (mac_gestalts
.navsvers
> 0) {
157 NavDialogOptions navopts
;
158 NavTypeListHandle navtypes
;
159 AEDesc defaultloc
= { 'null', NULL
};
160 AEDesc
*navdefault
= NULL
;
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
);
181 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
183 StandardFileReply sfr
;
184 static const OSType sftypes
[] = { 'Sess', 0, 0, 0 };
186 StandardGetFile(NULL
, 1, sftypes
, &sfr
);
187 if (!sfr
.sfGood
) return;
189 mac_opensessionfrom(&sfr
.sfFile
);
190 /* XXX handle error */
195 void mac_savesession(void)
197 Session
*s
= mac_windowsession(FrontWindow());
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
);
207 void mac_savesessionas(void)
209 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
210 Session
*s
= mac_windowsession(FrontWindow());
211 StandardFileReply sfr
;
214 StandardPutFile("\pSave session as:",
215 s
->hasfile ? s
->savefile
.name
: "\puntitled", &sfr
);
216 if (!sfr
.sfGood
) return;
218 if (!sfr
.sfReplacing
) {
219 FSpCreateResFile(&sfr
.sfFile
, PUTTY_CREATOR
, SESS_TYPE
, sfr
.sfScript
);
220 if (ResError() != noErr
) return; /* XXX report error */
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
);
227 s
->savefile
= sfr
.sfFile
;
231 pascal OSErr
mac_aevt_oapp(const AppleEvent
*req
, AppleEvent
*reply
,
237 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
238 &type
, NULL
, 0, &size
) == noErr
)
239 return errAEParamMissed
;
241 /* XXX we should do something here. */
245 pascal OSErr
mac_aevt_odoc(const AppleEvent
*req
, AppleEvent
*reply
,
250 AEDescList docs
= { typeNull
, NULL
};
253 err
= AEGetParamDesc(req
, keyDirectObject
, typeAEList
, &docs
);
254 if (err
!= noErr
) goto out
;
256 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
257 &type
, NULL
, 0, &size
) == noErr
) {
258 err
= errAEParamMissed
;
262 err
= mac_openlist(docs
);
265 AEDisposeDesc(&docs
);
269 pascal OSErr
mac_aevt_pdoc(const AppleEvent
*req
, AppleEvent
*reply
,
275 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
276 &type
, NULL
, 0, &size
) == noErr
)
277 return errAEParamMissed
;
279 /* We can't meaningfully do anything here. */
280 return errAEEventNotHandled
;
283 static void mac_activatedlg(WindowPtr window
, EventRecord
*event
)
285 DialogItemType itemtype
;
290 DialogRef dialog
= GetDialogFromWindow(window
);
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
);
298 static void mac_clickdlg(WindowPtr window
, EventRecord
*event
)
301 Session
*s
= mac_windowsession(window
);
302 DialogRef dialog
= GetDialogFromWindow(window
);
304 if (DialogSelect(event
, &dialog
, &item
))
313 static void mac_updatedlg(WindowPtr window
)
315 #if TARGET_API_MAC_CARBON
320 #if TARGET_API_MAC_CARBON
322 GetPortVisibleRegion(GetWindowPort(window
), rgn
);
323 UpdateDialog(GetDialogFromWindow(window
), rgn
);
326 UpdateDialog(window
, window
->visRgn
);
331 #if TARGET_API_MAC_CARBON
332 #define EnableItem EnableMenuItem
333 #define DisableItem DisableMenuItem
335 static void mac_adjustdlgmenus(WindowPtr window
)
339 menu
= GetMenuHandle(mFile
);
340 DisableItem(menu
, iSave
); /* XXX enable if modified */
341 EnableItem(menu
, iSaveAs
);
342 EnableItem(menu
, iDuplicate
);
344 menu
= GetMenuHandle(mEdit
);
345 DisableItem(menu
, 0);
350 * c-file-style: "simon"