f89c3294 |
1 | /* $Id$ */ |
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> |
9ef3c1ca |
35 | #include <Navigation.h> |
b537dd42 |
36 | #include <Resources.h> |
2b8dca9e |
37 | #include <StandardFile.h> |
f8d7de79 |
38 | #include <TextUtils.h> |
6cb61a05 |
39 | #include <Windows.h> |
40 | |
98f59a35 |
41 | #include <assert.h> |
6cb61a05 |
42 | #include <string.h> |
43 | |
44 | #include "putty.h" |
8a7e67ec |
45 | #include "dialog.h" |
6cb61a05 |
46 | #include "mac.h" |
47 | #include "macresid.h" |
2b8dca9e |
48 | #include "storage.h" |
6cb61a05 |
49 | |
71581f96 |
50 | static void mac_config(int); |
8a7e67ec |
51 | static void mac_closedlg(WindowPtr); |
71581f96 |
52 | static void mac_enddlg_config(WindowPtr, int); |
53 | static void mac_enddlg_reconfig(WindowPtr, int); |
f854b482 |
54 | |
6cb61a05 |
55 | void mac_newsession(void) |
56 | { |
71581f96 |
57 | mac_config(FALSE); |
58 | } |
59 | |
60 | void mac_reconfig(void) |
61 | { |
62 | mac_config(TRUE); |
63 | } |
64 | |
65 | static void mac_config(int midsession) |
66 | { |
6cb61a05 |
67 | Session *s; |
f854b482 |
68 | WinInfo *wi; |
8a7e67ec |
69 | static struct sesslist sesslist; |
f8d7de79 |
70 | Str255 mactitle; |
71581f96 |
71 | char *str; |
72 | |
73 | if (midsession) { |
74 | s = mac_windowsession(FrontWindow()); |
75 | } else { |
76 | s = snew(Session); |
77 | memset(s, 0, sizeof(*s)); |
78 | do_defaults(NULL, &s->cfg); |
79 | s->hasfile = FALSE; |
80 | } |
6cb61a05 |
81 | |
d36f85ac |
82 | /* Copy the configuration somewhere else in case this is a * |
83 | * reconfiguration and the user cancels the operation */ |
84 | |
85 | s->temp_cfg = s->cfg; |
86 | |
8a7e67ec |
87 | if (HAVE_COLOR_QD()) |
88 | s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1); |
89 | else |
90 | s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1); |
91 | |
92 | get_sesslist(&sesslist, TRUE); |
93 | s->ctrlbox = ctrl_new_box(); |
71581f96 |
94 | setup_config_box(s->ctrlbox, &sesslist, midsession, 0, 0); |
8a7e67ec |
95 | |
d36f85ac |
96 | s->settings_ctrls.data = &s->temp_cfg; |
71581f96 |
97 | if (midsession) |
98 | s->settings_ctrls.end = &mac_enddlg_reconfig; |
99 | else |
100 | s->settings_ctrls.end = &mac_enddlg_config; |
101 | |
8a7e67ec |
102 | macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls); |
6cb61a05 |
103 | |
34773273 |
104 | wi = snew(WinInfo); |
f854b482 |
105 | memset(wi, 0, sizeof(*wi)); |
106 | wi->s = s; |
5cec2fd9 |
107 | wi->mcs = &s->settings_ctrls; |
f854b482 |
108 | wi->wtype = wSettings; |
8a7e67ec |
109 | wi->update = &macctrl_update; |
110 | wi->click = &macctrl_click; |
1f1ec0e5 |
111 | wi->key = &macctrl_key; |
8a7e67ec |
112 | wi->activate = &macctrl_activate; |
113 | wi->adjustmenus = &macctrl_adjustmenus; |
ee10bc56 |
114 | wi->close = &mac_closedlg; |
f854b482 |
115 | SetWRefCon(s->settings_window, (long)wi); |
71581f96 |
116 | if (midsession) |
117 | str = dupprintf("%s Reconfiguration", appname); |
118 | else |
119 | str = dupprintf("%s Configuration", appname); |
120 | c2pstrcpy(mactitle, str); |
66dba8af |
121 | sfree(str); |
f8d7de79 |
122 | SetWTitle(s->settings_window, mactitle); |
6cb61a05 |
123 | ShowWindow(s->settings_window); |
124 | } |
125 | |
ee10bc56 |
126 | static void mac_closedlg(WindowPtr window) |
127 | { |
128 | Session *s = mac_windowsession(window); |
129 | |
130 | macctrl_close(window); |
131 | DisposeWindow(window); |
132 | if (s->window == NULL) |
133 | sfree(s); |
134 | } |
135 | |
71581f96 |
136 | static void mac_enddlg_config(WindowPtr window, int value) |
f73f2f31 |
137 | { |
138 | Session *s = mac_windowsession(window); |
139 | |
140 | if (value == 0) |
141 | mac_closedlg(window); |
142 | else { |
d36f85ac |
143 | s->cfg = s->temp_cfg; |
f73f2f31 |
144 | mac_startsession(s); |
145 | mac_closedlg(window); |
146 | } |
147 | } |
ee10bc56 |
148 | |
71581f96 |
149 | static void mac_enddlg_reconfig(WindowPtr window, int value) |
150 | { |
151 | Session *s = mac_windowsession(window); |
152 | |
153 | if (value == 0) |
154 | mac_closedlg(window); |
155 | else { |
d36f85ac |
156 | Config prev_cfg = s->cfg; |
157 | s->cfg = s->temp_cfg; |
71581f96 |
158 | mac_closedlg(window); |
d36f85ac |
159 | |
160 | /* Pass new config data to the logging module */ |
161 | log_reconfig(s->logctx, &s->cfg); |
162 | |
163 | /* |
164 | * Flush the line discipline's edit buffer in the |
165 | * case where local editing has just been disabled. |
166 | */ |
167 | if (s->ldisc) |
168 | ldisc_send(s->ldisc, NULL, 0, 0); |
169 | |
170 | /* Change the palette */ |
171 | palette_reset(s); |
172 | |
173 | /* Pass new config data to the terminal */ |
174 | term_reconfig(s->term, &s->cfg); |
175 | |
176 | /* Pass new config data to the back end */ |
177 | if (s->back) |
178 | s->back->reconfig(s->backhandle, &s->cfg); |
179 | |
180 | /* Screen size changed ? */ |
181 | if (s->cfg.height != prev_cfg.height || |
182 | s->cfg.width != prev_cfg.width || |
183 | s->cfg.savelines != prev_cfg.savelines) { |
184 | term_size(s->term, s->cfg.height, s->cfg.width, s->cfg.savelines); |
185 | request_resize(s, s->cfg.width, s->cfg.height); |
186 | } |
187 | |
188 | /* Set the window title */ |
189 | if (s->cfg.wintitle[0]) |
190 | set_title(s, s->cfg.wintitle); |
191 | |
192 | /* TODO: zoom, scroll bar, font */ |
71581f96 |
193 | } |
194 | } |
195 | |
5211281f |
196 | void mac_dupsession(void) |
197 | { |
347bfcd7 |
198 | Session *s1 = mac_windowsession(FrontWindow()); |
5211281f |
199 | Session *s2; |
200 | |
34773273 |
201 | s2 = snew(Session); |
5211281f |
202 | memset(s2, 0, sizeof(*s2)); |
203 | s2->cfg = s1->cfg; |
204 | s2->hasfile = s1->hasfile; |
205 | s2->savefile = s1->savefile; |
206 | |
207 | mac_startsession(s2); |
208 | } |
209 | |
d70e3b83 |
210 | static OSErr mac_opensessionfrom(FSSpec *fss) |
211 | { |
212 | FInfo fi; |
2b8dca9e |
213 | Session *s; |
2b8dca9e |
214 | void *sesshandle; |
d70e3b83 |
215 | OSErr err; |
2b8dca9e |
216 | |
34773273 |
217 | s = snew(Session); |
2b8dca9e |
218 | memset(s, 0, sizeof(*s)); |
219 | |
d70e3b83 |
220 | err = FSpGetFInfo(fss, &fi); |
221 | if (err != noErr) return err; |
222 | if (fi.fdFlags & kIsStationery) |
98f59a35 |
223 | s->hasfile = FALSE; |
224 | else { |
225 | s->hasfile = TRUE; |
d70e3b83 |
226 | s->savefile = *fss; |
98f59a35 |
227 | } |
2b8dca9e |
228 | |
d70e3b83 |
229 | sesshandle = open_settings_r_fsp(fss); |
230 | if (sesshandle == NULL) { |
231 | /* XXX need a way to pass up an error number */ |
232 | err = -9999; |
233 | goto fail; |
234 | } |
235 | load_open_settings(sesshandle, TRUE, &s->cfg); |
236 | close_settings_r(sesshandle); |
237 | |
2b8dca9e |
238 | mac_startsession(s); |
d70e3b83 |
239 | return noErr; |
2b8dca9e |
240 | |
241 | fail: |
242 | sfree(s); |
d70e3b83 |
243 | return err; |
244 | } |
245 | |
9ef3c1ca |
246 | static OSErr mac_openlist(AEDesc docs) |
247 | { |
248 | OSErr err; |
249 | long ndocs, i; |
250 | FSSpec fss; |
251 | AEKeyword keywd; |
252 | DescType type; |
253 | Size size; |
254 | |
255 | err = AECountItems(&docs, &ndocs); |
256 | if (err != noErr) return err; |
257 | |
258 | for (i = 0; i < ndocs; i++) { |
259 | err = AEGetNthPtr(&docs, i + 1, typeFSS, |
260 | &keywd, &type, &fss, sizeof(fss), &size); |
261 | if (err != noErr) return err;; |
262 | err = mac_opensessionfrom(&fss); |
263 | if (err != noErr) return err; |
264 | } |
265 | return noErr; |
266 | } |
267 | |
cef180ab |
268 | void mac_opensession(void) |
269 | { |
9ef3c1ca |
270 | |
271 | if (mac_gestalts.navsvers > 0) { |
272 | NavReplyRecord navr; |
273 | NavDialogOptions navopts; |
274 | NavTypeListHandle navtypes; |
275 | AEDesc defaultloc = { 'null', NULL }; |
276 | AEDesc *navdefault = NULL; |
277 | short vol; |
278 | long dirid; |
279 | FSSpec fss; |
280 | |
281 | if (NavGetDefaultDialogOptions(&navopts) != noErr) return; |
282 | /* XXX should we create sessions dir? */ |
283 | if (get_session_dir(FALSE, &vol, &dirid) == noErr && |
284 | FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr && |
285 | AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr) |
286 | navdefault = &defaultloc; |
287 | /* Can't meaningfully preview a saved session yet */ |
288 | navopts.dialogOptionFlags &= ~kNavAllowPreviews; |
289 | navtypes = (NavTypeListHandle)GetResource('open', open_pTTY); |
290 | if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes, |
291 | NULL) == noErr && navr.validRecord) |
292 | mac_openlist(navr.selection); |
293 | NavDisposeReply(&navr); |
294 | if (navtypes != NULL) |
295 | ReleaseResource((Handle)navtypes); |
296 | } |
cef180ab |
297 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
9ef3c1ca |
298 | else { |
299 | StandardFileReply sfr; |
300 | static const OSType sftypes[] = { 'Sess', 0, 0, 0 }; |
d70e3b83 |
301 | |
9ef3c1ca |
302 | StandardGetFile(NULL, 1, sftypes, &sfr); |
303 | if (!sfr.sfGood) return; |
d70e3b83 |
304 | |
9ef3c1ca |
305 | mac_opensessionfrom(&sfr.sfFile); |
306 | /* XXX handle error */ |
307 | } |
cef180ab |
308 | #endif |
2b8dca9e |
309 | } |
310 | |
b537dd42 |
311 | void mac_savesession(void) |
312 | { |
347bfcd7 |
313 | Session *s = mac_windowsession(FrontWindow()); |
98f59a35 |
314 | void *sesshandle; |
b537dd42 |
315 | |
98f59a35 |
316 | assert(s->hasfile); |
317 | sesshandle = open_settings_w_fsp(&s->savefile); |
318 | if (sesshandle == NULL) return; /* XXX report error */ |
319 | save_open_settings(sesshandle, TRUE, &s->cfg); |
320 | close_settings_w(sesshandle); |
b537dd42 |
321 | } |
322 | |
323 | void mac_savesessionas(void) |
324 | { |
cef180ab |
325 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
347bfcd7 |
326 | Session *s = mac_windowsession(FrontWindow()); |
b537dd42 |
327 | StandardFileReply sfr; |
328 | void *sesshandle; |
329 | |
98f59a35 |
330 | StandardPutFile("\pSave session as:", |
331 | s->hasfile ? s->savefile.name : "\puntitled", &sfr); |
b537dd42 |
332 | if (!sfr.sfGood) return; |
333 | |
334 | if (!sfr.sfReplacing) { |
335 | FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript); |
336 | if (ResError() != noErr) return; /* XXX report error */ |
337 | } |
338 | sesshandle = open_settings_w_fsp(&sfr.sfFile); |
339 | if (sesshandle == NULL) return; /* XXX report error */ |
340 | save_open_settings(sesshandle, TRUE, &s->cfg); |
341 | close_settings_w(sesshandle); |
98f59a35 |
342 | s->hasfile = TRUE; |
343 | s->savefile = sfr.sfFile; |
cef180ab |
344 | #endif |
b537dd42 |
345 | } |
346 | |
d70e3b83 |
347 | pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply, |
348 | long refcon) |
349 | { |
350 | DescType type; |
351 | Size size; |
352 | |
353 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
354 | &type, NULL, 0, &size) == noErr) |
355 | return errAEParamMissed; |
356 | |
357 | /* XXX we should do something here. */ |
358 | return noErr; |
359 | } |
360 | |
361 | pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply, |
362 | long refcon) |
363 | { |
364 | DescType type; |
d70e3b83 |
365 | Size size; |
366 | AEDescList docs = { typeNull, NULL }; |
367 | OSErr err; |
d70e3b83 |
368 | |
369 | err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs); |
370 | if (err != noErr) goto out; |
371 | |
372 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
373 | &type, NULL, 0, &size) == noErr) { |
374 | err = errAEParamMissed; |
375 | goto out; |
376 | } |
377 | |
9ef3c1ca |
378 | err = mac_openlist(docs); |
d70e3b83 |
379 | |
380 | out: |
381 | AEDisposeDesc(&docs); |
382 | return err; |
383 | } |
384 | |
385 | pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply, |
386 | long refcon) |
387 | { |
388 | DescType type; |
389 | Size size; |
390 | |
391 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
392 | &type, NULL, 0, &size) == noErr) |
393 | return errAEParamMissed; |
394 | |
395 | /* We can't meaningfully do anything here. */ |
396 | return errAEEventNotHandled; |
397 | } |
398 | |
6cb61a05 |
399 | /* |
400 | * Local Variables: |
401 | * c-file-style: "simon" |
402 | * End: |
403 | */ |