Remember which file a session was opened from, so it can be the default one
[u/mdw/putty] / mac / macdlg.c
1 /* $Id: macdlg.c,v 1.5 2003/01/18 20:52:59 ben Exp $ */
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>
33 #include <Dialogs.h>
34 #include <Resources.h>
35 #include <StandardFile.h>
36 #include <Windows.h>
37
38 #include <assert.h>
39 #include <string.h>
40
41 #include "putty.h"
42 #include "mac.h"
43 #include "macresid.h"
44 #include "storage.h"
45
46 void mac_newsession(void)
47 {
48 Session *s;
49
50 /* This should obviously be initialised by other means */
51 s = smalloc(sizeof(*s));
52 memset(s, 0, sizeof(*s));
53 do_defaults(NULL, &s->cfg);
54 s->back = &loop_backend;
55 s->hasfile = FALSE;
56
57 s->settings_window = GetNewDialog(wSettings, NULL, (WindowPtr)-1);
58
59 SetWRefCon(s->settings_window, (long)s);
60 ShowWindow(s->settings_window);
61 }
62
63 void mac_opensession(void) {
64 Session *s;
65 StandardFileReply sfr;
66 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
67 void *sesshandle;
68 int i;
69
70 s = smalloc(sizeof(*s));
71 memset(s, 0, sizeof(*s));
72
73 StandardGetFile(NULL, 1, sftypes, &sfr);
74 if (!sfr.sfGood) goto fail;
75
76 sesshandle = open_settings_r_fsp(&sfr.sfFile);
77 if (sesshandle == NULL) goto fail;
78 load_open_settings(sesshandle, TRUE, &s->cfg);
79 close_settings_r(sesshandle);
80 if (sfr.sfFlags & kIsStationery)
81 s->hasfile = FALSE;
82 else {
83 s->hasfile = TRUE;
84 s->savefile = sfr.sfFile;
85 }
86
87 /*
88 * Select protocol. This is farmed out into a table in a
89 * separate file to enable an ssh-free variant.
90 */
91 s->back = NULL;
92 for (i = 0; backends[i].backend != NULL; i++)
93 if (backends[i].protocol == s->cfg.protocol) {
94 s->back = backends[i].backend;
95 break;
96 }
97 if (s->back == NULL) {
98 fatalbox("Unsupported protocol number found");
99 }
100 mac_startsession(s);
101 return;
102
103 fail:
104 sfree(s);
105 return;
106 }
107
108 void mac_savesession(void)
109 {
110 Session *s = (Session *)GetWRefCon(FrontWindow());
111 void *sesshandle;
112
113 assert(s->hasfile);
114 sesshandle = open_settings_w_fsp(&s->savefile);
115 if (sesshandle == NULL) return; /* XXX report error */
116 save_open_settings(sesshandle, TRUE, &s->cfg);
117 close_settings_w(sesshandle);
118 }
119
120 void mac_savesessionas(void)
121 {
122 Session *s = (Session *)GetWRefCon(FrontWindow());
123 StandardFileReply sfr;
124 void *sesshandle;
125
126 StandardPutFile("\pSave session as:",
127 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
128 if (!sfr.sfGood) return;
129
130 if (!sfr.sfReplacing) {
131 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
132 if (ResError() != noErr) return; /* XXX report error */
133 }
134 sesshandle = open_settings_w_fsp(&sfr.sfFile);
135 if (sesshandle == NULL) return; /* XXX report error */
136 save_open_settings(sesshandle, TRUE, &s->cfg);
137 close_settings_w(sesshandle);
138 s->hasfile = TRUE;
139 s->savefile = sfr.sfFile;
140 }
141
142 void mac_activatedlg(WindowPtr window, EventRecord *event)
143 {
144 DialogItemType itemtype;
145 Handle itemhandle;
146 short item;
147 Rect itemrect;
148 int active;
149
150 active = (event->modifiers & activeFlag) != 0;
151 GetDialogItem(window, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
152 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
153 DialogSelect(event, &window, &item);
154 }
155
156 void mac_clickdlg(WindowPtr window, EventRecord *event)
157 {
158 short item;
159 Session *s = (Session *)GetWRefCon(window);
160
161 if (DialogSelect(event, &window, &item))
162 switch (item) {
163 case wiSettingsOpen:
164 CloseWindow(window);
165 mac_startsession(s);
166 break;
167 }
168 }
169
170 /*
171 * Local Variables:
172 * c-file-style: "simon"
173 * End:
174 */