Debian Codegraph @

terps/tads/tads3/indlg_tx.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 indlg_tx.cpp - formatted text implementation of input_dialog
15 Function
16 Implements the input dialog using formatted text
17 Notes
18 Only one of indlg_tx.c or indlg_os.c should be included in a given
19 executable. For a text-only version, include indlg_tx. For a version
20 where os_input_dialog() provides a system dialog, use indlg_os instead.
21
22 We provide a choice of input_dialog() implementations in the portable
23 code (rather than only through the OS code) so that we can call
24 the formatted text output routines in this version. An OS-layer
25 implementation could not call the formatted output routines (it would
26 have to call os_printf directly), which would result in poor prompt
27 formatting any time a prompt exceeded a single line of text.
28 Modified
29 09/27/99 MJRoberts - Creation
30 */
31
32
33 #include "os.h"
34 #include "t3std.h"
35 #include "vmglob.h"
36 #include "vmconsol.h"
37 #include "charmap.h"
38
39 /*
40 * formatted text-only file prompt
41 */
42 int CVmConsole::input_dialog(VMG_ int /*icon_id*/,
43 const char *prompt, int standard_button_set,
44 const char **buttons, int button_count,
45 int default_index, int cancel_index,
46 int bypass_script)
47 {
48 /* keep going until we get a valid response */
49 for (;;)
50 {
51 int i;
52 char buf[256];
53 const char *p;
54 const char *cur;
55 char *resp;
56 int match_cnt;
57 int last_found;
58 static const struct
59 {
60 const char *buttons[3];
61 int button_count;
62 } std_btns[] =
63 {
64 { { "&OK" }, 1 },
65 { { "&OK", "&Cancel" }, 2 },
66 { { "&Yes", "&No" }, 2 },
67 { { "&Yes", "&No", "&Cancel" }, 3 }
68 };
69
70 /*
71 * if we have a standard button set selected, get our button
72 * labels
73 */
74 switch(standard_button_set)
75 {
76 case 0:
77 /* use the explicit buttons provided */
78 break;
79
80 case OS_INDLG_OK:
81 i = 0;
82
83 use_std_btns:
84 /* use the selected standard button set */
85 buttons = (const char **)std_btns[i].buttons;
86 button_count = std_btns[i].button_count;
87 break;
88
89 case OS_INDLG_OKCANCEL:
90 i = 1;
91 goto use_std_btns;
92
93 case OS_INDLG_YESNO:
94 i = 2;
95 goto use_std_btns;
96
97 case OS_INDLG_YESNOCANCEL:
98 i = 3;
99 goto use_std_btns;
100
101 default:
102 /*
103 * we don't recognize other standard button sets - return an
104 * error
105 */
106 return 0;
107 }
108
109 /*
110 * if there are no buttons defined, they'll never be able to
111 * respond, so we'd just loop forever - rather than let that
112 * happen, return failure
113 */
114 if (button_count == 0)
115 return 0;
116
117 /* display a newline and the prompt string */
118 format_text(vmg_ "\n");
119 format_text(vmg_ prompt);
120 format_text(vmg_ " ");
121
122 /* display the response */
123 for (i = 0 ; i < button_count ; ++i)
124 {
125 /*
126 * display a slash to separate responses, if this isn't the
127 * first one
128 */
129 if (i != 0)
130 format_text(vmg_ "/");
131
132 /* get the current button */
133 cur = buttons[i];
134
135 /*
136 * Look for a "&" in the response string. If we find it,
137 * remove the "&" and enclose the shortcut key in parens.
138 */
139 for (p = cur ; *p != '\0' && *p != '&' ;
140 p = utf8_ptr::s_inc((char *)p)) ;
141
142 /* if we found the "&", put the next character in parens */
143 if (*p != '\0')
144 {
145 size_t pre_len;
146 size_t post_len;
147
148 /*
149 * note the length of the part up to the '&', but limit it
150 * to avoid overflowing the buffer
151 */
152 pre_len = p - cur;
153 if (pre_len > sizeof(buf) - 5)
154 pre_len = sizeof(buf) - 5;
155
156 /*
157 * note the length of the part after the '&', limiting it
158 * as well
159 */
160 post_len = strlen(p+2);
161 if (post_len > sizeof(buf) - 5 - pre_len)
162 post_len = sizeof(buf) - 5 - pre_len;
163
164 /* reformat the response string */
165 sprintf(buf, "%.*s(%c)%.*s",
166 (int)pre_len, cur, *(p + 1), (int)post_len, p + 2);
167
168 /* display it */
169 format_text(vmg_ buf);
170 }
171 else
172 {
173 /* no '&' - just display the response string as-is */
174 format_text(vmg_ cur);
175 }
176 }
177
178 /* switch to input font */
179 format_text(vmg_ "<font face='TADS-Input'>");
180
181 /* read the response */
182 format_text(vmg_ " >");
183 read_line(vmg_ buf, sizeof(buf));
184
185 /* close the input font tag */
186 format_text(vmg_ "</font>");
187
188 /* skip any leading spaces in the reply */
189 for (resp = buf ; isspace(*resp) ; ++resp) ;
190
191 /* if it's one character, check it against the shortcut keys */
192 if (strlen(resp) == 1)
193 {
194 /* scan the responses */
195 for (i = 0 ; i < button_count ; ++i)
196 {
197 /* look for a '&' in this button */
198 for (p = buttons[i] ; *p != '&' && *p != '\0' ; ++p) ;
199
200 /* if we found the '&', check the shortcut */
201 if (*p == '&' && toupper(*(p+1)) == toupper(*resp))
202 {
203 /*
204 * this is the one - return the current index
205 * (bumping it by one to get a 1-based value)
206 */
207 return i + 1;
208 }
209 }
210 }
211
212 /*
213 * Either it's not a one-character reply, or it didn't match a
214 * short-cut - check it against the leading substrings of the
215 * responses. If it matches exactly one of the responses in its
216 * leading substring, use that response.
217 */
218 for (i = 0, match_cnt = 0 ; i < button_count ; ++i)
219 {
220 const char *p1;
221 const char *p2;
222
223 /*
224 * compare this response to the user's response; skip any
225 * '&' in the button label
226 */
227 for (p1 = resp, p2 = buttons[i] ; *p1 != '\0' && *p2 != '\0' ;
228 ++p1, ++p2)
229 {
230 /* if this is a '&' in the button label, skip it */
231 if (*p2 == '&')
232 ++p2;
233
234 /* if these characters don't match, it's no match */
235 if (toupper(*p1) != toupper(*p2))
236 break;
237 }
238
239 /*
240 * if we reached the end of the user's response, we have a
241 * match in the leading substring - count it and remember
242 * this as the last one, but keep looking, since we need to
243 * make sure we don't have any other matches
244 */
245 if (*p1 == '\0')
246 {
247 ++match_cnt;
248 last_found = i;
249 }
250 }
251
252 /*
253 * if we found exactly one match, return it (adjusting to a
254 * 1-based index); if we found more or less than one match, it's
255 * not a valid response, so start over with a new prompt
256 */
257 if (match_cnt == 1)
258 return last_found + 1;
259 }
260 }
261