| 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 | int err; |
| 59 | static const struct |
| 60 | { |
| 61 | const char *buttons[3]; |
| 62 | int button_count; |
| 63 | } std_btns[] = |
| 64 | { |
| 65 | { { "&OK" }, 1 }, |
| 66 | { { "&OK", "&Cancel" }, 2 }, |
| 67 | { { "&Yes", "&No" }, 2 }, |
| 68 | { { "&Yes", "&No", "&Cancel" }, 3 } |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * if we have a standard button set selected, get our button |
| 73 | * labels |
| 74 | */ |
| 75 | switch(standard_button_set) |
| 76 | { |
| 77 | case 0: |
| 78 | /* use the explicit buttons provided */ |
| 79 | break; |
| 80 | |
| 81 | case OS_INDLG_OK: |
| 82 | i = 0; |
| 83 | |
| 84 | use_std_btns: |
| 85 | /* use the selected standard button set */ |
| 86 | buttons = (const char **)std_btns[i].buttons; |
| 87 | button_count = std_btns[i].button_count; |
| 88 | break; |
| 89 | |
| 90 | case OS_INDLG_OKCANCEL: |
| 91 | i = 1; |
| 92 | goto use_std_btns; |
| 93 | |
| 94 | case OS_INDLG_YESNO: |
| 95 | i = 2; |
| 96 | goto use_std_btns; |
| 97 | |
| 98 | case OS_INDLG_YESNOCANCEL: |
| 99 | i = 3; |
| 100 | goto use_std_btns; |
| 101 | |
| 102 | default: |
| 103 | /* |
| 104 | * we don't recognize other standard button sets - return an |
| 105 | * error |
| 106 | */ |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * if there are no buttons defined, they'll never be able to |
| 112 | * respond, so we'd just loop forever - rather than let that |
| 113 | * happen, return failure |
| 114 | */ |
| 115 | if (button_count == 0) |
| 116 | return 0; |
| 117 | |
| 118 | /* display a newline and the prompt string */ |
| 119 | format_text(vmg_ "\n"); |
| 120 | format_text(vmg_ prompt); |
| 121 | format_text(vmg_ " "); |
| 122 | |
| 123 | /* display the response */ |
| 124 | for (i = 0 ; i < button_count ; ++i) |
| 125 | { |
| 126 | /* |
| 127 | * display a slash to separate responses, if this isn't the |
| 128 | * first one |
| 129 | */ |
| 130 | if (i != 0) |
| 131 | format_text(vmg_ "/"); |
| 132 | |
| 133 | /* get the current button */ |
| 134 | cur = buttons[i]; |
| 135 | |
| 136 | /* |
| 137 | * Look for a "&" in the response string. If we find it, |
| 138 | * remove the "&" and enclose the shortcut key in parens. |
| 139 | */ |
| 140 | for (p = cur ; *p != '\0' && *p != '&' ; |
| 141 | p = utf8_ptr::s_inc((char *)p)) ; |
| 142 | |
| 143 | /* if we found the "&", put the next character in parens */ |
| 144 | if (*p != '\0') |
| 145 | { |
| 146 | size_t pre_len; |
| 147 | size_t post_len; |
| 148 | |
| 149 | /* |
| 150 | * note the length of the part up to the '&', but limit it |
| 151 | * to avoid overflowing the buffer |
| 152 | */ |
| 153 | pre_len = p - cur; |
| 154 | if (pre_len > sizeof(buf) - 5) |
| 155 | pre_len = sizeof(buf) - 5; |
| 156 | |
| 157 | /* |
| 158 | * note the length of the part after the '&', limiting it |
| 159 | * as well |
| 160 | */ |
| 161 | post_len = strlen(p+2); |
| 162 | if (post_len > sizeof(buf) - 5 - pre_len) |
| 163 | post_len = sizeof(buf) - 5 - pre_len; |
| 164 | |
| 165 | /* reformat the response string */ |
| 166 | sprintf(buf, "%.*s(%c)%.*s", |
| 167 | (int)pre_len, cur, *(p + 1), (int)post_len, p + 2); |
| 168 | |
| 169 | /* display it */ |
| 170 | format_text(vmg_ buf); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | /* no '&' - just display the response string as-is */ |
| 175 | format_text(vmg_ cur); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* switch to input font */ |
| 180 | format_text(vmg_ "<font face='TADS-Input'>"); |
| 181 | |
| 182 | /* read the response */ |
| 183 | format_text(vmg_ " >"); |
| 184 | err = read_line(vmg_ buf, sizeof(buf), bypass_script); |
| 185 | |
| 186 | /* close the input font tag */ |
| 187 | format_text(vmg_ "</font>"); |
| 188 | |
| 189 | /* on error, return 0 */ |
| 190 | if (err) |
| 191 | return 0; |
| 192 | |
| 193 | /* skip any leading spaces in the reply */ |
| 194 | for (resp = buf ; isspace(*resp) ; ++resp) ; |
| 195 | |
| 196 | /* if it's one character, check it against the shortcut keys */ |
| 197 | if (strlen(resp) == 1) |
| 198 | { |
| 199 | /* scan the responses */ |
| 200 | for (i = 0 ; i < button_count ; ++i) |
| 201 | { |
| 202 | /* look for a '&' in this button */ |
| 203 | for (p = buttons[i] ; *p != '&' && *p != '\0' ; ++p) ; |
| 204 | |
| 205 | /* if we found the '&', check the shortcut */ |
| 206 | if (*p == '&' && toupper(*(p+1)) == toupper(*resp)) |
| 207 | { |
| 208 | /* |
| 209 | * this is the one - return the current index |
| 210 | * (bumping it by one to get a 1-based value) |
| 211 | */ |
| 212 | return i + 1; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Either it's not a one-character reply, or it didn't match a |
| 219 | * short-cut - check it against the leading substrings of the |
| 220 | * responses. If it matches exactly one of the responses in its |
| 221 | * leading substring, use that response. |
| 222 | */ |
| 223 | for (i = 0, match_cnt = 0 ; i < button_count ; ++i) |
| 224 | { |
| 225 | const char *p1; |
| 226 | const char *p2; |
| 227 | |
| 228 | /* |
| 229 | * compare this response to the user's response; skip any |
| 230 | * '&' in the button label |
| 231 | */ |
| 232 | for (p1 = resp, p2 = buttons[i] ; *p1 != '\0' && *p2 != '\0' ; |
| 233 | ++p1, ++p2) |
| 234 | { |
| 235 | /* if this is a '&' in the button label, skip it */ |
| 236 | if (*p2 == '&') |
| 237 | ++p2; |
| 238 | |
| 239 | /* if these characters don't match, it's no match */ |
| 240 | if (toupper(*p1) != toupper(*p2)) |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * if we reached the end of the user's response, we have a |
| 246 | * match in the leading substring - count it and remember |
| 247 | * this as the last one, but keep looking, since we need to |
| 248 | * make sure we don't have any other matches |
| 249 | */ |
| 250 | if (*p1 == '\0') |
| 251 | { |
| 252 | ++match_cnt; |
| 253 | last_found = i; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * if we found exactly one match, return it (adjusting to a |
| 259 | * 1-based index); if we found more or less than one match, it's |
| 260 | * not a valid response, so start over with a new prompt |
| 261 | */ |
| 262 | if (match_cnt == 1) |
| 263 | return last_found + 1; |
| 264 | } |
| 265 | } |
| 266 |