Debian Codegraph @

terps/tads/tads2/msdos/oswin.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/OSWIN.C,v 1.4 1999/07/11 00:46:37 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1997, 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
15 oswin.c - os implementation for Windows
16 Function
17
18 Notes
19
20 Modified
21 11/05/97 MJRoberts - Creation
22 */
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #include <windows.h>
29 #include <Wincrypt.h>
30
31 #include "std.h"
32 #include "os.h"
33 #include "trd.h"
34
35 /* saved game file extension */
36 static char default_saved_game_ext[OSFNMAX] = "";
37
38 /* initial directory for os_askfile dialogs */
39 static char S_open_file_dir[OSFNMAX];
40
41 /* ------------------------------------------------------------------------ */
42 /*
43 * Application instance handle - the WinMain procedure (defined
44 * elsewhere) must define this variable and initialize it to the
45 * application instance handle.
46 */
47 extern HINSTANCE oss_G_hinstance;
48
49
50 /* ------------------------------------------------------------------------ */
51 /*
52 * Set the initial directory for os_askfile dialogs
53 */
54 void oss_set_open_file_dir(const char *dir)
55 {
56 /*
57 * if there's a string, copy it to our buffer; otherwise, clear our
58 * buffer
59 */
60 if (dir != 0)
61 strcpy(S_open_file_dir, dir);
62 else
63 S_open_file_dir[0] = '\0';
64 }
65
66 /* ------------------------------------------------------------------------ */
67 /*
68 * dialog hook for standard file dialog - centers it on the screen
69 */
70 static UINT APIENTRY filedlg_hook(HWND dlg, UINT msg,
71 WPARAM wpar, LPARAM lpar)
72 {
73 /* if this is the post-initialization message, center the dialog */
74 if (msg == WM_NOTIFY
75 && ((NMHDR *)lpar)->code == CDN_INITDONE)
76 {
77 RECT deskrc;
78 RECT dlgrc;
79 int deskwid, deskht;
80 int dlgwid, dlght;
81
82 /* get the desktop area */
83 GetWindowRect(GetDesktopWindow(), &deskrc);
84 deskwid = deskrc.right - deskrc.left;
85 deskht = deskrc.bottom - deskrc.top;
86
87 /* get the dialog box area */
88 GetWindowRect(((NMHDR *)lpar)->hwndFrom, &dlgrc);
89 dlgwid = dlgrc.right - dlgrc.left;
90 dlght = dlgrc.bottom - dlgrc.top;
91
92 /* center the dialog on the screen */
93 MoveWindow(((NMHDR *)lpar)->hwndFrom,
94 deskrc.left + (deskwid - dlgwid)/2,
95 deskrc.top + (deskht - dlght)/2, dlgwid, dlght, TRUE);
96
97 /* message handled */
98 return 1;
99 }
100
101 /* not handled */
102 return 0;
103 }
104
105 /*
106 * Ask the user for a filename, using a system-dependent dialog or other
107 * mechanism. Returns zero on success, non-zero on error.
108 */
109 int os_askfile(const char *prompt, char *fname_buf, int fname_buf_len,
110 int prompt_type, int file_type)
111 {
112 OPENFILENAME info;
113 int ret;
114 char filter[256];
115 const char *def_ext;
116 const char *save_def_ext;
117 static char lastsave[OSFNMAX] = "";
118
119 /* presume we won't need a default extension */
120 def_ext = 0;
121 save_def_ext = 0;
122
123 /*
124 * Build the appropriate filter. Most of the filters are boilerplate,
125 * but the "saved game" filter (for both TADS 2 and TADS 3 saved game
126 * files) is special because we need to use the dynamically-selected
127 * default saved game extension for our filter. This means that the
128 * "unknown" type is equally complicated, because it needs to include
129 * the dynamic saved game extension as well.
130 */
131 if (file_type == OSFTSAVE
132 || file_type == OSFTT3SAV
133 || file_type == OSFTUNK)
134 {
135 static const char filter_start[] =
136 "Saved Game Positions\0*.";
137 static const char filter_end_sav[] =
138 "All Files\0*.*\0\0";
139 static const char filter_end_unk[] =
140 "TADS Games\0*.GAM\0"
141 "Text Files\0*.TXT\0"
142 "Log Files\0*.LOG\0"
143 "All Files\0*.*\0\0";
144 size_t totlen;
145 size_t extlen;
146 const char *save_filter;
147
148 /*
149 * Get the default saved game extension. If the game program has
150 * explicitly set a custom extension, use that. Otherwise, use the
151 * appropriate extension for the file type.
152 */
153 if (default_saved_game_ext[0] != '\0')
154 {
155 /*
156 * there's an explicit filter, so use it as the filter and as
157 * the default extension
158 */
159 save_filter = default_saved_game_ext;
160 save_def_ext = default_saved_game_ext;
161 }
162 else if (file_type == OSFTSAVE)
163 {
164 /* tads 2 saved game, so use the ".sav" suffix */
165 save_filter = "sav";
166 save_def_ext = "sav";
167 }
168 else if (file_type == OSFTT3SAV)
169 {
170 /* tads 3 saved game, so use the ".t3v" suffix */
171 save_filter = "t3v";
172 save_def_ext = "t3v";
173 }
174 else
175 {
176 /*
177 * Unknown file type - use BOTH tads 2 and tads 3 saved game
178 * extensions for the filter, but do not apply any default
179 * extension to files we're creating.
180 *
181 * Note that we will append this filter to "*.", so the first
182 * extension in our list doesn't need the "*.", but the second
183 * one does.
184 */
185 save_filter = "sav;*.t3v";
186 save_def_ext = 0;
187 }
188
189 /*
190 * Build the filter string for saved game files. Note that we
191 * need to use memcpy rather than strcpy, because we need to
192 * include a bunch of embedded null characters within the
193 * string. First, copy the prefix up to the saved game
194 * extension...
195 */
196 memcpy(filter, filter_start, sizeof(filter_start) - 1);
197 totlen = sizeof(filter_start) - 1;
198
199 /* now add the saved game extension that we're to use... */
200 extlen = strlen(save_filter) + 1;
201 memcpy(filter + totlen, save_filter, extlen);
202 totlen += extlen;
203
204 /* finish with the whole rest of it */
205 if (file_type == OSFTSAVE || file_type == OSFTT3SAV)
206 {
207 /* saved game: use the saved game extra filter list */
208 memcpy(filter + totlen, filter_end_sav, sizeof(filter_end_sav));
209
210 /*
211 * if we're creating a new saved game file, apply the default
212 * suffix to the name the user enters for the new file
213 */
214 if (prompt_type == OS_AFP_SAVE)
215 def_ext = save_def_ext;
216 }
217 else
218 {
219 /* not a saved game: use the unknown file type filter list */
220 memcpy(filter + totlen, filter_end_unk, sizeof(filter_end_unk));
221 }
222 }
223 else
224 {
225 static const struct
226 {
227 const char *disp;
228 const char *pat;
229 const char *def_ext;
230 }
231 filters[] =
232 {
233 { "TADS Games", "*.gam", "gam" }, /* OSFTGAME */
234 { 0, 0 }, /* unused - OSFTSAVE */
235 { "Transcripts", "*.log", "log" }, /* OSFTLOG */
236 { "Swap Files", "*.dat", "dat" }, /* OSFTSWAP */
237 { "Data Files", "*.dat", "dat" }, /* OSFTDATA */
238 { "Command Scripts", "*.cmd", "cmd" }, /* OSFTCMD */
239 { "Message Files", "*.msg", "msg" }, /* OSFTERRS */
240 { "Text Files", "*.txt", "txt" }, /* OSFTTEXT */
241 { "Binary Files", "*.dat", "dat" }, /* OSFTBIN */
242 { "Character Maps", "*.tcp", "tcp" }, /* OSFTCMAP */
243 { "Preference Files","*.dat", "dat" }, /* OSFTPREF */
244 { 0, 0, 0 }, /* unused - OSFTUNK */
245 { "T3 Applications", "*.t3" /* was t3x */, "t3" }, /* OSFTT3IMG */
246 { "T3 Object Files", "*.t3o", "t3o" }, /* OSFTT3OBJ */
247 { "T3 Symbol Files", "*.t3s", "t3s" }, /* OSFTT3SYM */
248 { 0, 0, 0 } /* unused - OSFTT3SAV */
249 };
250 static const char filter_end[] = "All Files\0*.*\0\0";
251 char *p;
252
253 /*
254 * We have an ordinary, static filter -- choose the appropriate
255 * filter from our list. Start with the display name...
256 */
257 strcpy(filter, filters[file_type].disp);
258
259 /* add the file filter pattern after the null character */
260 p = filter + strlen(filter) + 1;
261 strcpy(p, filters[file_type].pat);
262
263 /* add the filter terminator after the null */
264 p += strlen(p) + 1;
265 memcpy(p, filter_end, sizeof(filter_end));
266
267 /* remember the default extension */
268 save_def_ext = def_ext = filters[file_type].def_ext;
269 }
270
271 /* set up the open-file structure */
272 info.lStructSize = sizeof(info);
273 info.hwndOwner = GetActiveWindow();
274 info.hInstance = oss_G_hinstance;
275 info.lpstrFilter = filter;
276 info.lpstrCustomFilter = 0;
277 info.nFilterIndex = 0;
278 info.lpstrFile = fname_buf;
279 fname_buf[0] = '\0';
280 info.nMaxFile = fname_buf_len;
281 info.lpstrFileTitle = 0;
282 info.nMaxFileTitle = 0;
283 info.lpstrInitialDir = (S_open_file_dir[0] != '\0' ? S_open_file_dir : 0);
284 info.lpstrTitle = prompt;
285 info.nFileOffset = 0;
286 info.nFileExtension = 0;
287 info.lpstrDefExt = def_ext;
288 info.lCustData = 0;
289 info.lpfnHook = filedlg_hook;
290 info.Flags = OFN_ENABLEHOOK | OFN_EXPLORER | OFN_NOCHANGEDIR;
291 info.lpTemplateName = 0;
292
293 /*
294 * If we're asking for a saved game file, pick a default. If we've
295 * asked before, use the same default as last time; if not, use the
296 * game name as the default. If we're opening a saved game rather then
297 * saving one, don't apply the default if the file we'd get by applying
298 * the default doesn't exist.
299 */
300 if (file_type == OSFTSAVE || file_type == OSFTT3SAV)
301 {
302 /* try using the last saved game name */
303 strncpy(fname_buf, lastsave, fname_buf_len);
304 fname_buf[fname_buf_len - 1] = '\0';
305
306 /* if that's empty, generate a default name based on the game name */
307 if (fname_buf[0] == '\0' && G_os_gamename[0] != '\0')
308 {
309 /* copy the game name as the base of the save-file name */
310 strncpy(fname_buf, G_os_gamename, fname_buf_len);
311 fname_buf[fname_buf_len - 1] = '\0';
312
313 /* remove the old extension */
314 os_remext(fname_buf);
315
316 /* ... and replace it with the saved-game extension */
317 if (save_def_ext != 0
318 && strlen(fname_buf) + strlen(save_def_ext) + 1
319 < (size_t)fname_buf_len)
320 os_defext(fname_buf, save_def_ext);
321 }
322
323 /*
324 * if we're asking about a file to open, make sure our default
325 * exists - if not, don't offer it as the default, since it's a
326 * waste of time and attention for the user to have to change it
327 */
328 if (prompt_type == OS_AFP_OPEN && osfacc(fname_buf))
329 fname_buf[0] = '\0';
330 }
331
332 /* display the dialog */
333 if (prompt_type == OS_AFP_SAVE)
334 {
335 info.Flags |= OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;
336 ret = GetSaveFileName(&info);
337 }
338 else
339 {
340 /* ask for a file to open */
341 ret = GetOpenFileName(&info);
342 }
343
344 /* translate the result code to an OS_AFE_xxx value */
345 if (ret == 0)
346 {
347 /*
348 * an error occurred - check to see what happened: if the extended
349 * error is zero, it means that the user simply canceled the
350 * dialog, otherwise it means that an error occurred
351 */
352 ret = (CommDlgExtendedError() == 0 ? OS_AFE_CANCEL : OS_AFE_FAILURE);
353 }
354 else
355 {
356 char path[OSFNMAX];
357
358 /* success */
359 ret = OS_AFE_SUCCESS;
360
361 /*
362 * remember the file's directory for the next file dialog (we need
363 * to do this explicitly because we're not allowing the dialog to
364 * set the working directory)
365 */
366 os_get_path_name(path, sizeof(path), fname_buf);
367 oss_set_open_file_dir(path);
368
369 /*
370 * if this is a saved game prompt, remember the response as the
371 * default for next time
372 */
373 if (file_type == OSFTSAVE || file_type == OSFTT3SAV)
374 strcpy(lastsave, fname_buf);
375 }
376
377 /* return the result */
378 return ret;
379 }
380
381 /* ------------------------------------------------------------------------ */
382 /*
383 * get filename from startup parameter, if possible; returns true and
384 * fills in the buffer with the parameter filename on success, false if
385 * no parameter file could be found
386 */
387 int os_paramfile(char *buf)
388 {
389 /* this operation is not meaningful on windows */
390 return 0;
391 }
392
393 /* ------------------------------------------------------------------------ */
394 /*
395 * Switch to a working directory
396 */
397 void os_set_pwd(const char *dir)
398 {
399 SetCurrentDirectory(dir);
400 }
401
402 /*
403 * Switch the working directory to the path containing the given file
404 */
405 void os_set_pwd_file(const char *fname)
406 {
407 char buf[OSFNMAX];
408 char *p;
409 char *last_sep;
410
411 /* make a copy of the filename */
412 strcpy(buf, fname);
413
414 /* find the last path separator */
415 for (p = buf, last_sep = 0 ; *p != '\0' ; ++p)
416 {
417 switch(*p)
418 {
419 case '/':
420 case '\\':
421 case ':':
422 /* remember the location of this separator character */
423 last_sep = p;
424 break;
425 }
426 }
427
428 /*
429 * if we found a separator, the part up to the last separator is the
430 * directory, and the rest is the file; terminate the buffer at the
431 * last separator and switch to that directory
432 */
433 if (last_sep != 0)
434 {
435 *last_sep = '\0';
436 os_set_pwd(buf);
437 }
438 }
439
440 /* ------------------------------------------------------------------------ */
441 /*
442 * Service routine: set a registry key
443 */
444 static void set_reg_key(char *keyname, char *valname, char *val)
445 {
446 DWORD disposition;
447 HKEY hkey;
448
449 /* create or open the key */
450 if (RegCreateKeyEx(HKEY_CLASSES_ROOT, keyname, 0, 0, 0, KEY_ALL_ACCESS,
451 0, &hkey, &disposition) == ERROR_SUCCESS)
452 {
453 /* set the value */
454 RegSetValueEx(hkey, valname, 0, REG_SZ, (BYTE *)val, strlen(val) + 1);
455
456 /* done with the key */
457 RegCloseKey(hkey);
458 }
459 }
460
461 /* ------------------------------------------------------------------------ */
462 /*
463 * Set the saved-game extension
464 */
465 void os_set_save_ext(const char *ext)
466 {
467 HKEY hkey;
468 char keyname[MAX_PATH];
469
470 /* copy the saved game extension into our buffer */
471 strcpy(default_saved_game_ext, ext);
472
473 /*
474 * check to see if the registry keys for the saved game association
475 * are present
476 */
477 sprintf(keyname, ".%s", ext);
478 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, keyname, 0, KEY_ALL_ACCESS, &hkey)
479 == ERROR_SUCCESS)
480 {
481 /*
482 * the suffix key exists, so assume the association is all set
483 * up -- all we need to do is close the key
484 */
485 RegCloseKey(hkey);
486 }
487 else
488 {
489 char modname[MAX_PATH];
490 char val[MAX_PATH];
491 char classname[MAX_PATH];
492 char cmd[MAX_PATH + 30];
493 char *p;
494 char *rootname;
495 char *lastdot;
496
497 /*
498 * The suffix key doesn't exit, so we must create the
499 * associations.
500 */
501
502 /*
503 * Generate a class name. Use the name of our executable as the
504 * basis of the class name, and add ".SavedGame" to it. Note
505 * that we must remove the directory prefix from the executable
506 * filename, since we want it to be a valid key value (i.e., no
507 * backslashes).
508 */
509 GetModuleFileName(0, modname, sizeof(modname));
510 for (p = rootname = modname ; *p != '\0' ; ++p)
511 {
512 /*
513 * if this is a path separator, assume the root starts on
514 * the next character; if we find another path separator
515 * later we'll ignore the current one
516 */
517 if (*p == '\\' || *p == '/' || *p == ':')
518 rootname = p + 1;
519 }
520
521 /* generate the class name -- module name + ".SavedGame" */
522 sprintf(classname, "%s.SavedGame", rootname);
523
524 /*
525 * Set the suffix key -- this associates the suffix with the
526 * class name (for example, ".DeepSave" -> "Deep.exe.SavedGame")
527 */
528 set_reg_key(keyname, "", classname);
529
530 /*
531 * Set the class key's default value -- this gives a display
532 * name for files with our suffix. We don't know the display
533 * name, so we'll just call these "<exe-name> Saved Game" files,
534 * with the ".EXE" removed from the executable's name.
535 */
536 for (p = rootname, lastdot = p + strlen(p) ; *p != '\0' ; ++p)
537 {
538 /* if this is a dot, note it */
539 if (*p == '.')
540 lastdot = p;
541 }
542 sprintf(val, "\"%.*s\" Saved Game", lastdot - rootname, rootname);
543 set_reg_key(classname, "", val);
544
545 /*
546 * Now create the shell command for restoring these games. For
547 * this, we need the name of the executable (in quotes), plus
548 * the -r option, plus the parameters ("%1").
549 */
550 sprintf(cmd, "\"%s\" -r \"%%1\"", modname);
551 sprintf(keyname, "%s\\Shell\\Open\\Command", classname);
552 set_reg_key(keyname, "", cmd);
553
554 /*
555 * Set the icon for our file suffix - saved games use icon #2 in
556 * the executable.
557 */
558 sprintf(cmd, "%s,2", modname);
559 sprintf(keyname, "%s\\DefaultIcon", classname);
560 set_reg_key(keyname, "", cmd);
561 }
562 }
563
564 const char *os_get_save_ext()
565 {
566 return (default_saved_game_ext[0] != '\0' ? default_saved_game_ext : 0);
567 }
568
569
570 /* ------------------------------------------------------------------------ */
571 /*
572 * Generate a filename for a character mapping table. On Windows, the
573 * filename is always simply "win" plus the internal ID plus ".tcp".
574 */
575 void os_gen_charmap_filename(char *filename, char *internal_id,
576 char *argv0)
577 {
578 char *p;
579 char *rootname;
580 size_t pathlen;
581
582 /* find the path prefix of the original executable filename */
583 for (p = rootname = argv0 ; *p != '\0' ; ++p)
584 {
585 if (*p == '/' || *p == '\\' || *p == ':')
586 rootname = p + 1;
587 }
588
589 /* copy the path prefix */
590 pathlen = rootname - argv0;
591 memcpy(filename, argv0, pathlen);
592
593 /* if there's no trailing backslash, add one */
594 if (pathlen == 0 || filename[pathlen - 1] != '\\')
595 filename[pathlen++] = '\\';
596
597 /* add "win_", plus the character set ID, plus the extension */
598 strcpy(filename + pathlen, "win_");
599 strcat(filename + pathlen, internal_id);
600 strcat(filename + pathlen, ".tcp");
601 }
602
603 /*
604 * Get the filename character mapping for Unicode characters. We use
605 * the current ANSI character set for all mappings.
606 */
607 void os_get_charmap(char *mapname, int charmap_id)
608 {
609 /* build the name as "CPnnnn", where nnnn is the ANSI code page */
610 sprintf(mapname, "CP%d", GetACP());
611 }
612
613
614 /* ------------------------------------------------------------------------ */
615 /*
616 * Sleep until the specified time. To avoid locking up our window,
617 * rather than using the system Sleep() API, we'll instead process and
618 * discard events until the delay is finished. Fortunately, we have the
619 * handy os_get_event() routine to process events with a timeout; we'll
620 * just call this repeatedly, discarding the events, until the timeout
621 * expires.
622 */
623 void os_sleep_ms(long delay_in_milliseconds)
624 {
625 long done_time;
626
627 /* calculate when we'll be done */
628 done_time = os_get_sys_clock_ms() + delay_in_milliseconds;
629
630 /* discard events until the timeout expires */
631 for (;;)
632 {
633 long cur_time;
634 os_event_info_t info;
635
636 /* get the current time */
637 cur_time = GetTickCount();
638
639 /* if we've reached the expiration point, delay no longer */
640 if (cur_time >= done_time)
641 break;
642
643 /*
644 * Read an event, timing out after what's left of our delay
645 * interval. If we get an end-of-file event, abort the wait.
646 */
647 if (os_get_event(done_time - cur_time, TRUE, &info) == OS_EVT_EOF)
648 break;
649 }
650 }
651
652 /* ------------------------------------------------------------------------ */
653 /*
654 * Get user input through a dialog
655 */
656
657 /*
658 * dialog parameters object - we can show only one dialog at a time, so
659 * we use a static instance of this structure for simplicity
660 */
661 static struct
662 {
663 /* default button ID - zero if no default */
664 int default_id;
665
666 /* cancel button ID - zero if none */
667 int cancel_id;
668 } S_dlg_params;
669
670 /*
671 * dialog box procedure
672 */
673 static BOOL CALLBACK oss_input_dialog_proc(HWND hwnd, UINT msg,
674 WPARAM wpar, LPARAM lpar)
675 {
676 switch(msg)
677 {
678 case WM_INITDIALOG:
679 /* if there's a defined default button, set initial focus there */
680 if (S_dlg_params.default_id != 0)
681 {
682 /* set the focus */
683 SetFocus(GetDlgItem(hwnd, S_dlg_params.default_id));
684
685 /* return zero to indicate that we've set focus */
686 return 0;
687 }
688
689 /* return non-zero to use the default button */
690 return 1;
691
692 case WM_COMMAND:
693 /*
694 * if it's a cancel or close command, allow it only if we have a
695 * cancel button defined
696 */
697 if (wpar == IDCANCEL || wpar == IDCLOSE)
698 {
699 /*
700 * if it's the cancel button, dismiss the dialog with the
701 * defined cancel button, not with the generic cancel
702 * message
703 */
704 if (S_dlg_params.cancel_id != 0)
705 EndDialog(hwnd, S_dlg_params.cancel_id);
706
707 /* handled */
708 return 1;
709 }
710
711 /*
712 * if it's the a return key, translate it to the default key, if
713 * there is one
714 */
715 if (wpar == IDOK)
716 {
717 /* use the default button, if defined */
718 if (S_dlg_params.default_id != 0)
719 EndDialog(hwnd, S_dlg_params.default_id);
720
721 /* handled */
722 return 1;
723 }
724
725 /*
726 * it's a command, so they must have pushed one of our buttons;
727 * all of our buttons are dismissal buttons, so simply dismiss
728 * the dialog with the command
729 */
730 EndDialog(hwnd, wpar);
731 return 1;
732
733 default:
734 /* we don't process other messages */
735 return 0;
736 }
737 }
738
739 /* base of our command ID's */
740 #define OS_ID_CMD_BASE 100
741
742 /* size we allocate to our icon */
743 #define OS_ID_ICON_X 32
744 #define OS_ID_ICON_Y 32
745
746
747 /*
748 * show the dialog
749 */
750 int os_input_dialog(int icon_id, const char *prompt, int standard_button_set,
751 const char **buttons, int button_count,
752 int default_index, int cancel_index)
753 {
754 /* check for standard dialogs */
755 if (standard_button_set != 0)
756 {
757 DWORD flags;
758 int ret;
759
760 /* select the dialog style based on the button set */
761 switch(standard_button_set)
762 {
763 case OS_INDLG_OK:
764 flags = MB_OK;
765 break;
766
767 case OS_INDLG_OKCANCEL:
768 flags = MB_OKCANCEL;
769 break;
770
771 case OS_INDLG_YESNO:
772 flags = MB_YESNO;
773 break;
774
775 case OS_INDLG_YESNOCANCEL:
776 flags = MB_YESNOCANCEL;
777 break;
778
779 default:
780 /* we don't recognize other button sets - return failure */
781 return 0;
782 }
783
784 /* select the icon ID */
785 switch(icon_id)
786 {
787 case OS_INDLG_ICON_NONE:
788 break;
789
790 case OS_INDLG_ICON_WARNING:
791 flags |= MB_ICONWARNING;
792 break;
793
794 case OS_INDLG_ICON_INFO:
795 flags |= MB_ICONINFORMATION;
796 break;
797
798 case OS_INDLG_ICON_QUESTION:
799 flags |= MB_ICONQUESTION;
800 break;
801
802 case OS_INDLG_ICON_ERROR:
803 flags |= MB_ICONERROR;
804 break;
805 }
806
807 /* show the message box modally to the task */
808 flags |= MB_TASKMODAL;
809
810 /* select the appropriate default button */
811 switch(default_index)
812 {
813 case 1:
814 flags |= MB_DEFBUTTON1;
815 break;
816
817 case 2:
818 flags |= MB_DEFBUTTON2;
819 break;
820
821 case 3:
822 flags |= MB_DEFBUTTON3;
823 break;
824
825 case 4:
826 flags |= MB_DEFBUTTON4;
827 break;
828 }
829
830 /* show the dialog */
831 ret = MessageBox(0, prompt, "TADS", flags);
832
833 /* determine which button they pushed */
834 switch(ret)
835 {
836 case 0:
837 default:
838 /* failure */
839 return 0;
840
841 case IDYES:
842 /* They clicked yes - this is always at button index 1 */
843 return 1;
844
845 case IDNO:
846 /* they clicked no - this is always at button index 2 */
847 return 2;
848
849 case IDCANCEL:
850 /*
851 * they clicked cancel - this could be at 2 or 3, depending
852 * on the dialog
853 */
854 switch(standard_button_set)
855 {
856 case OS_INDLG_OKCANCEL:
857 return 2;
858
859 case OS_INDLG_YESNOCANCEL:
860 return 3;
861 }
862
863 /* we shouldn't have gotten cancel in other cases */
864 return 0;
865
866 case IDOK:
867 /* this is always at index 1 */
868 return 1;
869 }
870 }
871 else
872 {
873 #define MAX_BUTTONS 16
874 #define MAX_LABEL_CHARS 1024
875 struct
876 {
877 DLGTEMPLATE hdr;
878 WORD menu_arr[1];
879 WORD class_arr[1];
880
881 /* space for the title and font information */
882 wchar_t title_arr[128];
883
884 /*
885 * Space for at least 16 dialog item templates, and 1k for the
886 * prompt text and button label text. Also leave space for
887 * internal structure padding to 4-byte boundaries - we need
888 * padding for each button.
889 */
890 WORD buf[sizeof(DLGITEMTEMPLATE)*MAX_BUTTONS/2 /* buttons */
891 + MAX_LABEL_CHARS /* prompt & button labels */
892 + 2*MAX_BUTTONS]; /* per-button padding */
893 } tpl;
894 DLGITEMTEMPLATE *itm;
895 int chrem = MAX_LABEL_CHARS;
896 int chcur;
897 wchar_t *p;
898 int i;
899 HDC dc = GetDC(GetDesktopWindow());
900 SIZE txtsiz;
901 TEXTMETRIC tm;
902 LONG dlg_base_units;
903 int dlgx, dlgy;
904 int btn_total_wid;
905 int curx;
906
907 /*
908 * to ensure we don't overflow our dialog template memory, limit
909 * the button count to the maximum
910 */
911 if (button_count > MAX_BUTTONS)
912 button_count = MAX_BUTTONS;
913
914 /* get the dialog-box-unit conversion factors */
915 dlg_base_units = GetDialogBaseUnits();
916 dlgx = LOWORD(dlg_base_units);
917 dlgy = HIWORD(dlg_base_units);
918
919 /* set up the template header */
920 tpl.hdr.style = DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SETFONT;
921 tpl.hdr.dwExtendedStyle = 0;
922 tpl.hdr.x = 0;
923 tpl.hdr.y = 0;
924 tpl.hdr.cx = 300;
925 tpl.hdr.cy = 94;
926
927 /*
928 * measure the height of some sample text, to see how tall the
929 * buttons will be - if we need more space than the 14 dialog units
930 * we've assumed in our fixed dialog height, add more height to the
931 * dialog to make room
932 */
933 GetTextMetrics(dc, &tm);
934 i = MulDiv(tm.tmHeight, 8, dlgy) + 8;
935 if (i > 14)
936 tpl.hdr.cy += 14 - i;
937
938 /*
939 * we have one control for the prompt text, another for the icon
940 * if specified, and then one for each button
941 */
942 tpl.hdr.cdit = 1 + (icon_id != 0 ? 1 : 0) + button_count;
943
944 /* no menu or class entries */
945 tpl.menu_arr[0] = 0;
946 tpl.class_arr[0] = 0;
947
948 /* set up the title */
949 wcscpy(tpl.title_arr, (const wchar_t *)L"TADS");
950
951 /* get the next available entry after the title string */
952 p = tpl.title_arr + wcslen(tpl.title_arr) + 1;
953
954 /* set the point size and font name */
955 *p++ = 8;
956 wcscpy(p, (const wchar_t *)L"MS Sans Serif");
957
958 /* skip past the font name */
959 p += wcslen(p) + 1;
960
961 /*
962 * we're now pointing to the first item template slot - align it
963 * on a DWORD boundary
964 */
965 itm = (DLGITEMTEMPLATE *)((((unsigned long)(p)) + 3) & ~3);
966
967 /* adjust to a system icon ID */
968 switch(icon_id)
969 {
970 case OS_INDLG_ICON_WARNING:
971 icon_id = (int)IDI_EXCLAMATION;
972 break;
973
974 case OS_INDLG_ICON_INFO:
975 icon_id = (int)IDI_ASTERISK;
976 break;
977
978 case OS_INDLG_ICON_QUESTION:
979 icon_id = (int)IDI_QUESTION;
980 break;
981
982 case OS_INDLG_ICON_ERROR:
983 icon_id = (int)IDI_HAND;
984 break;
985
986 default:
987 /* no icon */
988 icon_id = 0;
989 break;
990 }
991
992 /* add the icon */
993 if (icon_id != 0)
994 {
995 /* add the icon */
996 itm->style = SS_ICON | WS_VISIBLE | WS_CHILD;
997 itm->dwExtendedStyle = 0;
998 itm->x = 10;
999 itm->y = 10;
1000 itm->cx = OS_ID_ICON_X;
1001 itm->cy = OS_ID_ICON_Y;
1002 itm->id = (WORD)-1;
1003
1004 /* set the STATIC class */
1005 p = (WORD *)(itm + 1);
1006 *p++ = 0xffff;
1007 *p++ = 0x0082;
1008
1009 /* set the icon ID */
1010 *p++ = 0xffff;
1011 *p++ = icon_id;
1012
1013 /* no creation data */
1014 *p++ = 0;
1015
1016 /* get the next item */
1017 itm = (DLGITEMTEMPLATE *)((((unsigned long)(p)) + 3) & ~3);
1018 }
1019
1020 /* add the text string */
1021 itm->style = WS_VISIBLE | WS_CHILD;
1022 itm->dwExtendedStyle = 0;
1023 itm->x = 10 + (icon_id == 0 ? 0 : OS_ID_ICON_X + 2);
1024 itm->y = 10;
1025 itm->cx = tpl.hdr.cx - 14 - (icon_id == 0 ? 0 : OS_ID_ICON_X + 2);
1026 itm->cy = 32;
1027 itm->id = (WORD)-1;
1028
1029 /* set the STATIC class */
1030 p = (WORD *)(itm + 1);
1031 *p++ = 0xffff;
1032 *p++ = 0x0082;
1033
1034 /* copy the prompt text, translating to unicode */
1035 chcur = MultiByteToWideChar(CP_ACP, 0, prompt, -1, p, chrem);
1036 chrem -= chcur;
1037 p += chcur;
1038
1039 /* no creation data */
1040 *p++ = 0;
1041
1042 /*
1043 * Before we set the button positions, run through the buttons and
1044 * calculate their widths. This will let us figure out how to
1045 * center the line of buttons.
1046 */
1047 for (btn_total_wid = 0, i = 0 ; i < button_count ; ++i)
1048 {
1049 short wid;
1050
1051 /* measure the button text */
1052 GetTextExtentPoint32(dc, buttons[i], strlen(buttons[i]), &txtsiz);
1053 wid = (short)MulDiv(txtsiz.cx, 4, dlgx);
1054
1055 /* add in some padding at either end of the text */
1056 wid += 8;
1057
1058 /*
1059 * for a better appearance for short button names, use a
1060 * minimum width of 64 dialog units for each button
1061 */
1062 if (wid < 64)
1063 wid = 64;
1064
1065 /* count this in the total width */
1066 btn_total_wid += wid;
1067
1068 /* if this isn't the first button, add inter-button spacing */
1069 if (i != 0)
1070 btn_total_wid += 4;
1071 }
1072
1073 /*
1074 * If the total of the button widths, plus 4 units on either side
1075 * for spacing, exceeds our dialog width, increase the dialog width
1076 * accordingly.
1077 */
1078 if (tpl.hdr.cx < btn_total_wid + 8)
1079 tpl.hdr.cx = btn_total_wid + 8;
1080
1081 /* get the next item */
1082 itm = (DLGITEMTEMPLATE *)((((unsigned long)(p)) + 3) & ~3);
1083
1084 /*
1085 * Figure the position of the left edge of the buttons such that we
1086 * center the overall line of buttons in the dialog.
1087 */
1088 curx = (tpl.hdr.cx - btn_total_wid) / 2;
1089
1090 /* add the buttons */
1091 for (i = 0 ; i < button_count ; ++i)
1092 {
1093 /* add the button */
1094 itm->style = BS_PUSHBUTTON | WS_VISIBLE | WS_TABSTOP
1095 | WS_CHILD;
1096 itm->dwExtendedStyle = 0;
1097 itm->id = i + 1 + OS_ID_CMD_BASE;
1098
1099 /* measure the button text */
1100 GetTextExtentPoint32(dc, buttons[i], strlen(buttons[i]), &txtsiz);
1101 GetTextMetrics(dc, &tm);
1102 itm->cx = (short)MulDiv(txtsiz.cx, 4, dlgx) + 8;
1103 itm->cy = (short)MulDiv(tm.tmHeight, 8, dlgy) + 8;
1104
1105 /* apply a minimum button width of 64 dialog units */
1106 if (itm->cx < 64)
1107 itm->cx = 64;
1108
1109 /* set the position of the button */
1110 itm->x = curx;
1111 itm->y = tpl.hdr.cy - itm->cy - 18;
1112
1113 /*
1114 * advance to the next position (include the button width plus
1115 * inter-button spacing)
1116 */
1117 curx += itm->cx + 4;
1118
1119 /* set the BUTTON class */
1120 p = (WORD *)(itm + 1);
1121 *p++ = 0xffff;
1122 *p++ = 0x0080;
1123
1124 /* copy the button label text */
1125 chcur = MultiByteToWideChar(CP_ACP, 0, buttons[i], -1, p, chrem);
1126 chrem -= chcur;
1127 p += chcur;
1128
1129 /* no creation data */
1130 *p++ = 0;
1131
1132 /* get the next item, DWORD-aligned */
1133 itm = (DLGITEMTEMPLATE *)((((unsigned long)(p)) + 3) & ~3);
1134 }
1135
1136 /*
1137 * set up the default and cancel buttons in the dialog
1138 * parameters struture, so that the dialog procedure has access
1139 * to them; adjust our indices by our command offset
1140 */
1141 S_dlg_params.default_id = (default_index == 0
1142 ? 0 : default_index + OS_ID_CMD_BASE);
1143 S_dlg_params.cancel_id = (cancel_index == 0
1144 ? 0 : cancel_index + OS_ID_CMD_BASE);
1145
1146 /* we're done with the desktop DC */
1147 ReleaseDC(GetDesktopWindow(), dc);
1148
1149 /* make sure the text output is flushed */
1150 os_flush();
1151
1152 /* show the dialog */
1153 i = DialogBoxIndirect(oss_G_hinstance, &tpl.hdr, GetActiveWindow(),
1154 (DLGPROC)oss_input_dialog_proc);
1155
1156 /* check the result */
1157 if (i >= OS_ID_CMD_BASE)
1158 {
1159 /* they selected one of the buttons - adjust to a 1-base */
1160 i -= OS_ID_CMD_BASE;
1161 }
1162 else
1163 {
1164 /* unrecognized result */
1165 i = 0;
1166 }
1167
1168 /* return the result */
1169 return i;
1170
1171 #undef MAX_LABEL_CHARS
1172 #undef MAX_BUTTONS
1173 }
1174 }
1175
1176 /* ------------------------------------------------------------------------ */
1177 /*
1178 * String Resource loader. We'll go to the executable's resource list
1179 * to load these strings, using a base resource ID of 61000. An
1180 * application (HTML TADS, for example) must provide these resources in
1181 * the .res file it links in to its executable.
1182 */
1183 int os_get_str_rsc(int id, char *buf, size_t buflen)
1184 {
1185 /*
1186 * load the string - adjust the resource ID by adding our base value
1187 * of 61000
1188 */
1189 return (LoadString(oss_G_hinstance, id + 61000, buf, buflen) == 0);
1190 }
1191
1192 /*
1193 * Use the system strlwr() implementation for os_strlwr()
1194 */
1195 char *os_strlwr(char *s)
1196 {
1197 return strlwr(s);
1198 }
1199