]> icculus.org git repositories - btb/d2x.git/blob - main/newmenu.c
comments
[btb/d2x.git] / main / newmenu.c
1 /* $Id: newmenu.c,v 1.17 2003-02-14 03:42:24 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Routines for menus.
18  *
19  * Old Log:
20  * Revision 1.26  1996/04/14  21:07:35  allender
21  * some change for update
22  *
23  * Revision 1.25  1995/10/31  10:19:25  allender
24  * shareware stuff
25  *
26  * Revision 1.24  1995/10/27  11:09:04  allender
27  * always cd to descent dir before any menu processing --
28  * sanity check because of way mac does volume mounting and
29  * other file stuff
30  *
31  * Revision 1.23  1995/10/24  18:11:19  allender
32  * do_appl_quit saves and restores background -- don't
33  * do any special processing.  fixed newmenu_do3 to pass width
34  * and height to ...do_4
35  *
36  * Revision 1.22  1995/10/21  23:26:25  allender
37  * can't do cmd-q in multiplayer games
38  *
39  * Revision 1.21  1995/10/21  22:53:25  allender
40  * menus need to be 8 byte multiple wide -- printscreen stuff
41  *
42  * Revision 1.20  1995/10/20  00:50:15  allender
43  * redbook stuff and make esc leave menu always
44  *
45  * Revision 1.19  1995/10/17  13:14:52  allender
46  * mouse support for other menus now operational -- dragging
47  * mouse will also move sliders
48  *
49  * Revision 1.18  1995/10/12  17:35:19  allender
50  * mouse support for pilot and demos
51  *
52  * Revision 1.17  1995/10/11  12:17:47  allender
53  * removed event loop processing
54  *
55  * Revision 1.16  1995/10/10  11:51:29  allender
56  * align menus to 8 byte boundry
57  *
58  * Revision 1.15  1995/10/05  14:09:36  allender
59  * lowered starting x location on menus to fixup network
60  * status screen chopping off characters
61  *
62  * Revision 1.14  1995/09/24  10:52:27  allender
63  * made close boxes work
64  * maybe some other minor stuff?
65  *
66  * Revision 1.13  1995/09/15  09:02:04  allender
67  * started putting mouse support in listboxes
68  *
69  * Revision 1.12  1995/09/13  08:50:14  allender
70  * close box in this rev?  Worked on getting list box better
71  * looking
72  *
73  * Revision 1.11  1995/09/01  10:52:37  allender
74  * fixed up list boxes
75  *
76  * Revision 1.10  1995/08/24  16:23:08  allender
77  * mouse support for most menus
78  *
79  * Revision 1.9  1995/08/08  13:46:56  allender
80  * added macsys header file
81  *
82  * Revision 1.8  1995/08/01  16:03:54  allender
83  * added file list with Desalvo's directory stuff
84  *
85  * Revision 1.7  1995/07/26  17:03:37  allender
86  * show then hide cursor during newmenu operations -- not quite
87  * right yet
88  *
89  * Revision 1.6  1995/07/17  08:56:26  allender
90  * fixed up menus to look better with new high res fonts
91  *
92  * Revision 1.5  1995/07/12  10:35:52  allender
93  * removed old debug thing
94  *
95  * Revision 1.4  1995/06/23  10:23:21  allender
96  * menus should work in any resolution now
97  *
98  * Revision 1.3  1995/06/20  09:11:19  allender
99  * changed menu system to support 640x480 instead of 320x200
100  *
101  * Revision 1.2  1995/06/13  13:07:30  allender
102  * added special key to move 320x200 window to upper left corner of screen
103  *
104  * Revision 1.1  1995/05/16  15:29:13  allender
105  * Initial revision
106  *
107  * Revision 2.8  1995/05/26  16:16:28  john
108  * Split SATURN into define's for requiring cd, using cd, etc.
109  * Also started adding all the Rockwell stuff.
110  *
111  * Revision 2.7  1995/04/23  14:54:17  john
112  * Fixed bug with background breaking in first menu.
113  *
114  * Revision 2.6  1995/03/21  14:38:46  john
115  * Ifdef'd out the NETWORK code.
116  *
117  * Revision 2.5  1995/03/15  14:33:29  john
118  * Added code to force the Descent CD-rom in the drive.
119  *
120  * Revision 2.4  1995/03/14  18:24:28  john
121  * Force Destination Saturn to use CD-ROM drive.
122  *
123  * Revision 2.3  1995/03/14  16:22:23  john
124  * Added cdrom alternate directory stuff.
125  *
126  * Revision 2.2  1995/03/06  18:30:51  john
127  * Fixed bug with newmenu trashing editor font.
128  *
129  * Revision 2.1  1995/03/06  15:23:17  john
130  * New screen techniques.
131  *
132  * Revision 2.0  1995/02/27  11:27:55  john
133  * New version 2.0, which has no anonymous unions, builds with
134  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
135  *
136  * Revision 1.129  1995/02/11  16:19:56  john
137  * Added code to make the default mission be the one last played.
138  *
139  * Revision 1.128  1995/02/02  19:41:33  john
140  * Added 10 save game slots.
141  *
142  * Revision 1.127  1995/02/01  18:13:52  john
143  * Fixed some constants.
144  *
145  * Revision 1.126  1995/02/01  18:04:01  yuan
146  * Added 50 characters to list.
147  *
148  * Revision 1.125  1995/02/01  13:39:35  john
149  * Made menu text that changes not overwrite.
150  *
151  * Revision 1.124  1995/01/31  10:47:57  john
152  * Added menu that you can specify the width of the menu.
153  *
154  * Revision 1.123  1995/01/28  17:18:12  john
155  * Added file list box.
156  *
157  * Revision 1.122  1995/01/27  17:15:55  john
158  * Made prev comment actually work.
159  *
160  * Revision 1.121  1995/01/27  16:49:03  john
161  * *** empty log message ***
162  *
163  * Revision 1.120  1995/01/27  16:46:40  john
164  * Made so that input_menu only clears text if -empty-.
165  *
166  * Revision 1.119  1995/01/27  15:25:04  john
167  * Delete saved game when deleteing a pilot file.
168  *
169  * Revision 1.118  1995/01/25  16:36:09  john
170  * Made so that when you hit enter during
171  * game save, -empty- goes away.
172  *
173  * Revision 1.117  1995/01/24  16:59:30  john
174  * took out mono debugging text.
175  *
176  * Revision 1.116  1995/01/24  16:23:16  john
177  * Fixed some bugs with listboxes.
178  *
179  * Revision 1.115  1995/01/23  23:47:36  matt
180  * Made keypad enter work in menus
181  *
182  * Revision 1.114  1995/01/23  19:29:59  john
183  * Added scrolling listbox menus.
184  *
185  * Revision 1.113  1995/01/19  12:33:34  john
186  * Made keys jump to sliders, etc in menus.
187  *
188  * Revision 1.112  1995/01/15  14:33:08  rob
189  * Fixed problem with nested nm_messageboxes.
190  *
191  * Revision 1.111  1995/01/03  17:33:40  john
192  * Made scrolling textbox. Used it for scores cool saying.
193  *
194  * Revision 1.110  1994/12/28  10:42:58  john
195  * More VFX tweaking.
196  *
197  * Revision 1.109  1994/12/28  10:26:39  john
198  * Fixed some VFX problems.
199  *
200  * Revision 1.108  1994/12/15  23:18:10  john
201  * Added fix so that VFX mode doesn't hang.
202  *
203  * Revision 1.107  1994/12/15  12:19:55  john
204  * Made menu use clipped bitblt functions.
205  *
206  * Revision 1.106  1994/12/09  00:41:30  mike
207  * fix hang in automap print screen
208  *
209  * Revision 1.105  1994/12/08  10:01:34  john
210  * Changed the way the player callsign stuff works.
211  *
212  * Revision 1.104  1994/12/04  15:34:30  john
213  * Fixed bug with newmenu not restoring font properly.
214  *
215  * Revision 1.103  1994/12/03  17:47:09  john
216  * Fixed bug that didn't free filename mem when not .plr files found.
217  *
218  * Revision 1.102  1994/12/03  15:06:15  john
219  * If no pilot exists, bring up box asking for name.
220  *
221  * Revision 1.101  1994/12/03  11:04:02  john
222  * Changed newmenu code a bit to fix bug with bogus
223  * backgrounds occcasionally.
224  *
225  * Revision 1.100  1994/12/01  20:15:48  yuan
226  * Localization.
227  *
228  * Revision 1.99  1994/12/01  10:33:28  john
229  * Fixed bug with large menu backgrounds not drawing correctly.
230  *
231  * Revision 1.98  1994/12/01  02:41:56  john
232  * Fixed warnining.
233  *
234  * Revision 1.97  1994/11/30  22:52:43  john
235  * Fixed bug in code that made the backgrounds behind menus.
236  *
237  * Revision 1.96  1994/11/30  19:47:42  john
238  * Added a check for out o' memory when mallocing background.
239  *
240  * Revision 1.95  1994/11/30  19:39:10  john
241  * ..
242  *
243  * Revision 1.94  1994/11/30  19:38:27  john
244  * fixed bug with previous.
245  *
246  * Revision 1.93  1994/11/30  19:36:47  john
247  * Made Gravis Ultrasound work again.  Made the scores blink
248  * at a constant rate.  Revamped the newmenu background storage,
249  * which hopefully fixed some bugs.  Made menus in ame not pause
250  * sound, except for the pause key.               ^== Game!
251  *
252  * Revision 1.92  1994/11/30  18:06:05  matt
253  * When player types space in callsign, comes up as underscore
254  *
255  * Revision 1.91  1994/11/30  12:28:22  adam
256  * added PCX support
257  *
258  * Revision 1.90  1994/11/30  12:10:59  adam
259  * added support for PCX titles/brief screens
260  *
261  * Revision 1.89  1994/11/29  00:59:12  allender
262  * change newmenu_get_filename so demo files can be deleted too
263  *
264  * Revision 1.88  1994/11/27  21:16:18  allender
265  * made some return values in newmenu_get_filename 0 instead of -1
266  *
267  * Revision 1.87  1994/11/27  16:58:17  matt
268  * Made printscreen work all the time (not just when no ndebug) and made it
269  * work when getting a filename.
270  *
271  * Revision 1.86  1994/11/27  16:47:51  john
272  * Made the call to fade in palette only happen if it needs to be, just
273  * because I thought it might reduce code paging with vm in menus.
274  *
275  * Revision 1.85  1994/11/26  15:30:16  matt
276  * Allow escape out of change pilot menu
277  *
278  * Revision 1.84  1994/11/26  14:17:26  matt
279  * Player can now only enter valid chars for his name
280  *
281  * Revision 1.83  1994/11/23  14:13:17  allender
282  * if no demo files, displays less "techy" message
283  *
284  * Revision 1.82  1994/11/21  11:55:52  john
285  * Fixed some sound pausing in menus bugs.
286  *
287  * Revision 1.81  1994/11/19  15:14:58  mike
288  * remove unused code and data
289  *
290  * Revision 1.80  1994/11/18  23:37:54  john
291  * Changed some shorts to ints.
292  *
293  * Revision 1.79  1994/11/15  09:29:21  john
294  * Made it so that pressing a letter when selecting players moves to
295  * a matching choice.
296  *
297  * Revision 1.78  1994/11/14  17:12:28  adam
298  * *** empty log message ***
299  *
300  * Revision 1.77  1994/11/14  16:58:31  rob
301  * Tried to fix a problem with save demo dialog.
302  *
303  * Revision 1.76  1994/11/14  16:13:46  matt
304  * Fixed handling of players with DOS device names
305  *
306  * Revision 1.75  1994/11/13  18:12:53  matt
307  * Fixed handling of filenames that are the same as DOS devices
308  *
309  * Revision 1.74  1994/11/13  17:20:44  john
310  * Fixed text a bit.
311  *
312  * Revision 1.73  1994/11/13  17:18:22  john
313  * Changed wording of new pilot.
314  *
315  * Revision 1.72  1994/11/13  17:14:21  john
316  * Fixed bug with player list box.
317  *
318  * Revision 1.71  1994/11/13  17:12:48  john
319  * Fixed broken demo file list.
320  *
321  * Revision 1.70  1994/11/13  17:04:49  john
322  * Made the callsign entry be a list box and gave the ability
323  * to delete players.
324  *
325  * Revision 1.69  1994/11/13  15:38:03  john
326  * Added critical error handler to game.  Took out -editor command line
327  * option because it didn't work anymore and wasn't worth fixing.  Made scores
328  * not use MINER enviroment variable on release version, and made scores
329  * not print an error if there is no descent.hi.
330  *
331  * Revision 1.68  1994/11/11  18:17:03  rob
332  * Made multi_menu_poll return a value to exit menus.
333  *
334  * Revision 1.67  1994/11/11  11:07:06  rob
335  * Added include of multi.h
336  *
337  * Revision 1.66  1994/11/10  20:25:16  rob
338  * John's stuff to make network menus work.
339  *
340  * Revision 1.65  1994/11/08  14:51:39  john
341  * Added nm_messagebox1, (like the original, only you can pass a function).
342  *
343  * Revision 1.64  1994/11/08  08:30:39  john
344  * Fixed bug with centering titles.
345  *
346  * Revision 1.63  1994/11/08  08:27:00  john
347  * Made titles and subtitles center.
348  *
349  * Revision 1.62  1994/11/07  09:40:48  john
350  * Neatend file list box some.
351  *
352  * Revision 1.61  1994/11/05  17:22:41  john
353  * Fixed lots of sequencing problems with newdemo stuff.
354  *
355  * Revision 1.60  1994/11/05  15:04:08  john
356  * Added non-popup menu for the main menu, so that scores and credits don't have to save
357  * the background.
358  *
359  * Revision 1.59  1994/11/05  14:03:52  john
360  * Fixed fade transitions between all screens by making gr_palette_fade_in and out keep
361  * track of whether the palette is faded in or not.  Then, wherever the code needs to fade out,
362  * it just calls gr_palette_fade_out and it will fade out if it isn't already.  The same with fade_in.
363  * This eliminates the need for all the flags like Menu_fade_out, game_fade_in palette, etc.
364  *
365  * Revision 1.58  1994/11/04  20:11:50  john
366  * Neatening up palette stuff with demos.
367  *
368  * Revision 1.57  1994/11/04  13:49:24  allender
369  * fixed newmenu_get_filename to work with less than 10 files
370  *
371  * Revision 1.56  1994/11/03  19:37:44  john
372  * Added scrolling file list box
373  *
374  * Revision 1.55  1994/10/31  18:16:42  john
375  * Made Pad arrows work with menus.
376  *
377  * Revision 1.54  1994/10/28  14:54:25  john
378  * Added forward dec. for newmenu_close.
379  * .\
380  *
381  * Revision 1.53  1994/10/28  14:53:00  john
382  * Fixed hideous bug that would bomb if you called newmenu_draw_background
383  * before any menus were ever displayed.
384  *
385  * Revision 1.52  1994/10/24  19:56:53  john
386  * Made the new user setup prompt for config options.
387  *
388  * Revision 1.51  1994/10/24  15:15:49  john
389  * Made Esc exit nm_messagebox's,
390  * ,
391  *
392  * Revision 1.50  1994/10/21  15:20:20  john
393  * Made PrtScr do screen dump, not F2.
394  *
395  * Revision 1.49  1994/10/18  12:33:38  john
396  * Only used copy the item text into the saved_text field
397  * if it is an inputbox or inputbox_menu.
398  *
399  * Revision 1.48  1994/10/17  11:04:01  john
400  * Made backtab work also.
401  *
402  * Revision 1.47  1994/10/17  10:47:49  john
403  * MAde Tab work like down arrow.
404  *
405  * Revision 1.46  1994/10/17  10:45:10  john
406  * Made the player able to abort death by pressing any button or key.
407  *
408  * Revision 1.45  1994/10/13  21:52:02  john
409  * Made it so that if a messagebox has 1 choice, then
410  * Esc will return -1.
411  *
412  * Revision 1.44  1994/10/13  11:35:38  john
413  * Made Thrustmaster FCS Hat work.  Put a background behind the
414  * keyboard configure.  Took out turn_sensitivity.  Changed sound/config
415  * menu to new menu. Made F6 be calibrate joystick.
416  *
417  * Revision 1.43  1994/10/11  17:18:52  john
418  * Fixed bug with sliders always starting at -1.
419  *
420  * Revision 1.42  1994/10/11  17:08:29  john
421  * Added sliders for volume controls.
422  *
423  * Revision 1.41  1994/10/06  16:04:40  john
424  * Made text items color differently than others. Adam
425  * is gonna make a diff colored font for these.
426  *
427  * Revision 1.40  1994/10/06  15:08:23  rob
428  * Allowed any negative key value to abort the menu and return.
429  *
430  * Revision 1.39  1994/10/04  10:26:06  matt
431  * Changed fade in to happen every time a global var is set
432  *
433  * Revision 1.38  1994/10/04  09:16:08  john
434  * If you pass -1 as choice in newmenu_do1, then
435  * no item is highlighted until you press up or
436  * down arrows.
437  *
438  * Revision 1.37  1994/10/03  23:44:37  matt
439  * Save & restore palette effect around menus & pause message
440  *
441  * Revision 1.36  1994/10/03  22:59:40  matt
442  * Re-enabled backspace to generate Int3()
443  *
444  * Revision 1.35  1994/10/03  19:11:21  matt
445  * Changed string input cursor to blinking underscore
446  *
447  * Revision 1.34  1994/10/03  14:44:15  john
448  * Added newmenu_do1, which allows you to pass the starting
449  * item to the menu system
450  *
451  * Revision 1.33  1994/09/30  11:51:21  john
452  * Added Matt's NM_TYPE_INPUT_MENU
453  *
454  * Revision 1.32  1994/09/28  17:22:56  matt
455  * Added extra space between subtitle and menu items
456  * Made shortcut key check ignore leading spaces in text
457  *
458  * Revision 1.31  1994/09/15  16:11:22  john
459  * Added support for VFX1 head tracking. Fixed bug with memory over-
460  * write when using stereo mode.
461  *
462  * Revision 1.30  1994/09/12  09:52:59  john
463  * Made global flush function that flushes keyboard,mouse, and joystick.
464  *
465  * Revision 1.29  1994/09/10  19:10:54  matt
466  * Fixed a few things (like arrow key handling) for menus with all
467  * text items, such as the key help message.
468  *
469  * Revision 1.28  1994/09/01  18:55:38  john
470  * freed scores.lbm
471  *
472  * Revision 1.27  1994/09/01  18:03:50  john
473  * Neatened up scores a bit.
474  *
475  * Revision 1.26  1994/08/30  20:38:13  john
476  * Passed citem in newmenu sub.
477  *
478  * Revision 1.25  1994/08/30  11:13:01  john
479  * Added beveled edges to menus.
480  *
481  * Revision 1.24  1994/08/26  13:01:58  john
482  * Put high score system in.
483  *
484  * Revision 1.23  1994/08/16  00:18:44  john
485  * Made pressing the first letter of a menu
486  * item move to it.
487  *
488  * Revision 1.22  1994/08/15  23:17:43  john
489  * *** empty log message ***
490  *
491  * Revision 1.21  1994/08/15  23:15:28  john
492  * Made 1 menu/checkbox return with any keypress.
493  *
494  * Revision 1.20  1994/08/12  10:18:23  john
495  * *** empty log message ***
496  *
497  * Revision 1.19  1994/08/12  10:09:11  john
498  * Made borders better.
499  *
500  * Revision 1.18  1994/08/12  03:11:16  john
501  * Made network be default off; Moved network options into
502  * main menu.  Made starting net game check that mines are the
503  * same.
504  *
505  * Revision 1.17  1994/08/11  22:14:43  john
506  * Free'd up some memory that I forgot to free.
507  *
508  * Revision 1.16  1994/08/11  19:27:35  john
509  * Made the Backspace drop into the debugger only
510  * if you're not in an inputbox.
511  *
512  * Revision 1.15  1994/08/11  18:01:49  matt
513  * Added F2 and BACKSPACE keys to new menu system
514  *
515  * Revision 1.14  1994/08/11  14:25:58  john
516  * *** empty log message ***
517  *
518  * Revision 1.13  1994/08/11  14:25:40  john
519  * *** empty log message ***
520  *
521  * Revision 1.12  1994/08/11  13:47:02  john
522  * Made newmenu have subtitles, passed key through to
523  * the newmenu subfunctions.
524  *
525  * Revision 1.11  1994/08/11  12:45:08  john
526  * *** empty log message ***
527  *
528  * Revision 1.10  1994/08/11  12:25:45  john
529  * Made right_offset spacing different
530  *
531  * Revision 1.9  1994/08/11  12:09:49  john
532  * Made work with bitmapped fonts.
533  *
534  * Revision 1.8  1994/08/10  19:56:16  john
535  * Changed font stuff; Took out old menu; messed up lots of
536  * other stuff like game sequencing messages, etc.
537  *
538  * Revision 1.7  1994/07/27  16:12:23  john
539  * Changed newmenu system to have a callback function.
540  * /.
541  *
542  * Revision 1.6  1994/07/25  15:10:23  john
543  * *** empty log message ***
544  *
545  * Revision 1.5  1994/07/25  12:33:35  john
546  * Network "pinging" in.
547  *
548  * Revision 1.4  1994/07/24  18:21:27  john
549  * Took out first time stuff.
550  *
551  * Revision 1.3  1994/07/24  17:32:47  john
552  * Added percent item.  Also neatend up a bit.
553  *
554  * Revision 1.2  1994/07/22  17:48:13  john
555  * Added new menuing system.
556  *
557  * Revision 1.1  1994/07/22  13:55:38  john
558  * Initial revision
559  *
560  *
561  */
562
563 #ifdef HAVE_CONFIG_H
564 #include <conf.h>
565 #endif
566
567 #ifdef WINDOWS
568 #include "desw.h"
569 #endif
570
571 #include <stdio.h>
572 #include <stdlib.h>
573 #include <string.h>
574 #include <stdarg.h>
575 #include <ctype.h>
576 #include <unistd.h>
577
578 #include "pa_enabl.h"                   //$$POLY_ACC
579 #include "error.h"
580 #include "pstypes.h"
581 #include "gr.h"
582 #include "mono.h"
583 #include "songs.h"
584 #include "key.h"
585 #include "palette.h"
586 #include "game.h"
587 #include "text.h"
588 #include "findfile.h"
589
590 #include "menu.h"
591 #include "newmenu.h"
592 #include "gamefont.h"
593 #include "gamepal.h"
594 #ifdef NETWORK
595 #include "network.h"
596 #endif
597 #include "iff.h"
598 #include "pcx.h"
599 #include "u_mem.h"
600 #include "mouse.h"
601 #include "joy.h"
602 #include "digi.h"
603
604 #include "multi.h"
605 #include "endlevel.h"
606 #include "screens.h"
607 #include "config.h"
608 #include "player.h"
609 #include "newdemo.h"
610 #include "kconfig.h"
611 #include "strutil.h"
612
613 #ifdef MACINTOSH
614 #include <Events.h>
615 #endif
616
617 #if defined (TACTILE)
618  #include "tactile.h"
619 #endif
620
621 #if defined(POLY_ACC)
622 #include "poly_acc.h"
623 #endif
624
625 #define MAXDISPLAYABLEITEMS 15
626
627 #define LHX(x)      ((x)*(MenuHires?2:1))
628 #define LHY(y)      ((y)*(MenuHires?2.4:1))
629
630 #define TITLE_FONT      HUGE_FONT
631 #define NORMAL_FONT     MEDIUM1_FONT    //normal, non-highlighted item
632 #define SELECTED_FONT   MEDIUM2_FONT    //highlighted item
633 #define SUBTITLE_FONT   MEDIUM3_FONT
634
635 #define NORMAL_CHECK_BOX    "\81"
636 #define CHECKED_CHECK_BOX   "\82"
637
638 #define NORMAL_RADIO_BOX    "\7f"
639 #define CHECKED_RADIO_BOX   "\80"
640 #define CURSOR_STRING       "_"
641 #define SLIDER_LEFT         "\83"  // 131
642 #define SLIDER_RIGHT        "\84"  // 132
643 #define SLIDER_MIDDLE       "\85"  // 133
644 #define SLIDER_MARKER       "\86"  // 134
645 #define UP_ARROW_MARKER     "\87"  // 135
646 #define DOWN_ARROW_MARKER   "\88"  // 136
647
648 int newmenu_do4( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int citem, char * filename, int width, int height, int TinyMode );
649 void show_extra_netgame_info(int choice);
650
651
652 int Newmenu_first_time = 1;
653 //--unused-- int Newmenu_fade_in = 1;
654
655 typedef struct bkg {
656         WINDOS (dd_grs_canvas *menu_canvas, grs_canvas * menu_canvas);
657         grs_bitmap * saved;                     // The background under the menu.
658         grs_bitmap * background;
659 } bkg;
660
661 grs_bitmap nm_background,nm_background_save;
662
663 #define MESSAGEBOX_TEXT_SIZE 300                // How many characters in messagebox
664 #define MAX_TEXT_WIDTH  200                             // How many pixels wide a input box can be
665
666 extern void gr_bm_bitblt(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
667
668 ubyte MenuReordering=0;
669 ubyte SurfingNet=0;
670 char Pauseable_menu=0;
671 char already_showing_info=0;
672
673
674 void newmenu_close()    {
675         if ( nm_background.bm_data )
676                 d_free(nm_background.bm_data);
677
678         if ( nm_background_save.bm_data )
679                 d_free(nm_background_save.bm_data);
680         Newmenu_first_time = 1;
681 }
682
683 ubyte background_palette[768];
684
685 //should be called whenever the palette changes
686 void nm_remap_background()
687 {
688         if (!Newmenu_first_time) {
689                 if (!nm_background.bm_data)
690                         nm_background.bm_data = d_malloc(nm_background.bm_w * nm_background.bm_h);
691
692                 memcpy(nm_background.bm_data,nm_background_save.bm_data,nm_background.bm_w * nm_background.bm_h);
693
694                 gr_remap_bitmap_good( &nm_background, background_palette, -1, -1 );
695         }
696 }
697
698 #include <math.h>
699
700 extern char last_palette_loaded[];
701
702 void nm_draw_background1(char * filename)
703 {
704         int pcx_error;
705         grs_bitmap *bmp;
706         ubyte pal[256*3];
707         int width, height;
708
709         //@@//I think this only gets called to fill the whole screen
710         //@@Assert(grd_curcanv->cv_bitmap.bm_w == 320);
711         //@@Assert(grd_curcanv->cv_bitmap.bm_h == 200);
712
713         pcx_error = pcx_get_dimensions(filename, &width, &height);
714         if (pcx_error != PCX_ERROR_NONE)
715                 Error("Could not open pcx file <%s>\n", filename);
716
717         bmp = gr_create_bitmap(width, height);
718
719         pcx_error = pcx_read_bitmap(filename,bmp,bmp->bm_type,pal);
720         Assert(pcx_error == PCX_ERROR_NONE);
721
722         //@@gr_remap_bitmap_good( bmp, pal, -1, -1 );
723
724
725         {       //remap stuff. this code is kindof a hack
726
727                 //now, before we bring up the menu, we need to
728                 //do some stuff to make sure the palette is ok.  First, we need to
729                 //get our current palette into the 2d's array, so the remapping will
730                 //work.  Second, we need to remap the fonts.  Third, we need to fill
731                 //in part of the fade tables so the darkening of the menu edges works
732
733                 gr_copy_palette(gr_palette, pal, sizeof(gr_palette));
734                 remap_fonts_and_menus(1);
735
736         }
737
738 WIN(DDGRLOCK(dd_grd_curcanv));
739 #if defined(POLY_ACC)
740     pa_save_clut();
741     pa_update_clut(gr_palette, 0, 256, 0);
742 #endif
743         show_fullscr(bmp);
744 #if defined(POLY_ACC)
745     pa_restore_clut();
746 #endif
747 WIN(DDGRUNLOCK(dd_grd_curcanv));
748
749         gr_free_bitmap(bmp);
750
751         strcpy(last_palette_loaded,"");         //force palette load next time
752
753 }
754
755 #define MENU_BACKGROUND_BITMAP_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
756 #define MENU_BACKGROUND_BITMAP_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
757
758 #define MENU_BACKGROUND_BITMAP (MenuHires?MENU_BACKGROUND_BITMAP_HIRES:MENU_BACKGROUND_BITMAP_LORES)
759
760 int Background_hires;
761 int No_darkening=0;
762
763 void nm_draw_background(int x1, int y1, int x2, int y2 )
764 {
765         int w,h;
766
767         if (Newmenu_first_time || MenuHires!=Background_hires)  {
768                 int pcx_error;
769
770                 if (Newmenu_first_time) {
771                         atexit( newmenu_close );
772                         Newmenu_first_time = 0;
773                         nm_background_save.bm_data=NULL;                
774                 }
775                 else {
776                         if (nm_background_save.bm_data)
777                                 d_free(nm_background_save.bm_data);
778                         if (nm_background.bm_data)
779                                 d_free(nm_background.bm_data);
780                 }
781
782                 pcx_error = pcx_read_bitmap(MENU_BACKGROUND_BITMAP,&nm_background_save,BM_LINEAR,background_palette);
783                 Assert(pcx_error == PCX_ERROR_NONE);
784
785                 nm_background = nm_background_save;
786                 nm_background.bm_data=NULL;             
787                 nm_remap_background();
788
789                 Background_hires = MenuHires;
790         }
791
792         if ( x1 < 0 ) x1 = 0;
793         if ( y1 < 0 ) y1 = 0;
794
795         w = x2-x1+1;
796         h = y2-y1+1;
797
798         //if ( w > nm_background.bm_w ) w = nm_background.bm_w;
799         //if ( h > nm_background.bm_h ) h = nm_background.bm_h;
800         
801         x2 = x1 + w - 1;
802         y2 = y1 + h - 1;
803
804         {
805                 grs_bitmap *tmp = gr_create_bitmap(w, h);
806
807                 gr_bitmap_scale_to(&nm_background, tmp);
808
809                 WIN(DDGRLOCK(dd_grd_curcanv));
810                 if (No_darkening)
811                         gr_bm_bitblt(w, h, x1, y1, LHX(10), LHY(10), tmp, &(grd_curcanv->cv_bitmap) );
812                 else
813                         gr_bm_bitblt(w, h, x1, y1, 0, 0, tmp, &(grd_curcanv->cv_bitmap) );
814                 gr_free_bitmap(tmp);
815         }
816
817         if (!No_darkening) {
818                 Gr_scanline_darkening_level = 2*7;
819
820                 gr_setcolor( BM_XRGB(0,0,0) );
821                 gr_urect( x2-5, y1+5, x2-5, y2-5 );
822                 gr_urect( x2-4, y1+4, x2-4, y2-5 );
823                 gr_urect( x2-3, y1+3, x2-3, y2-5 );
824                 gr_urect( x2-2, y1+2, x2-2, y2-5 );
825                 gr_urect( x2-1, y1+1, x2-1, y2-5 );
826                 gr_urect( x2+0, y1+0, x2-0, y2-5 );
827
828                 gr_urect( x1+5, y2-5, x2, y2-5 );
829                 gr_urect( x1+4, y2-4, x2, y2-4 );
830                 gr_urect( x1+3, y2-3, x2, y2-3 );
831                 gr_urect( x1+2, y2-2, x2, y2-2 );
832                 gr_urect( x1+1, y2-1, x2, y2-1 );
833                 gr_urect( x1+0, y2, x2, y2-0 );
834         }
835         WIN(DDGRUNLOCK(dd_grd_curcanv));
836
837         Gr_scanline_darkening_level = GR_FADE_LEVELS;
838 }
839
840 void nm_restore_background( int x, int y, int w, int h )
841 {
842         int x1, x2, y1, y2;
843
844         x1 = x; x2 = x+w-1;
845         y1 = y; y2 = y+h-1;
846
847         if ( x1 < 0 ) x1 = 0;
848         if ( y1 < 0 ) y1 = 0;
849
850         if ( x2 >= nm_background.bm_w ) x2=nm_background.bm_w-1;
851         if ( y2 >= nm_background.bm_h ) y2=nm_background.bm_h-1;
852
853         w = x2 - x1 + 1;
854         h = y2 - y1 + 1;
855
856         WIN(DDGRLOCK(dd_grd_curcanv));
857                 gr_bm_bitblt(w, h, x1, y1, x1, y1, &nm_background, &(grd_curcanv->cv_bitmap) );
858         WIN(DDGRUNLOCK(dd_grd_curcanv));
859 }
860
861 // Draw a left justfied string
862 void nm_string( bkg * b, int w1,int x, int y, char * s)
863 {
864         int w,h,aw,tx=0,t=0,i;
865         char *p,*s1,*s2,measure[2];
866         int XTabs[]={15,87,124,162,228,253};
867    
868         p=s1=NULL;
869         s2 = d_strdup(s);
870
871         for (i=0;i<6;i++) {
872                 XTabs[i]=(LHX(XTabs[i]));
873                 XTabs[i]+=x;
874         }
875  
876         measure[1]=0;
877
878         if (!SurfingNet) {
879                 p = strchr( s2, '\t' );
880                 if (p && (w1>0) ) {
881                         *p = '\0';
882                         s1 = p+1;
883                 }
884         }
885
886         gr_get_string_size(s2, &w, &h, &aw  );
887
888         if (w1 > 0)
889                 w = w1;
890
891         // CHANGED
892         gr_bm_bitblt(b->background->bm_w-15, h+2, 5, y-1, 5, y-1, b->background, &(grd_curcanv->cv_bitmap) );
893         //gr_bm_bitblt(w, h, x, y, x, y, b->background, &(grd_curcanv->cv_bitmap) );
894
895         if (SurfingNet) {
896                 for (i=0;i<strlen(s2);i++) {
897                         if (s2[i]=='\t' && SurfingNet) {
898                                 x=XTabs[t];
899                                 t++;
900                                 continue;
901                         }
902                         measure[0]=s2[i];
903                         gr_get_string_size(measure,&tx,&h,&aw);
904                         gr_string(x,y,measure);
905                         x+=tx;
906                 }
907         }
908         else
909                 gr_string (x,y,s2);
910          
911         if (!SurfingNet && p && (w1>0) ) {
912                 gr_get_string_size(s1, &w, &h, &aw  );
913
914                 gr_string( x+w1-w, y, s1 );
915
916                 *p = '\t';
917         }
918         d_free(s2);
919 }
920
921 // Draw a slider and it's string
922 void nm_string_slider( bkg * b, int w1,int x, int y, char * s )
923 {
924         int w,h,aw;
925         char *p,*s1;
926
927         s1=NULL;
928
929         p = strchr( s, '\t' );
930         if (p)  {
931                 *p = '\0';
932                 s1 = p+1;
933         }
934
935         gr_get_string_size(s, &w, &h, &aw  );
936         // CHANGED
937
938                 gr_bm_bitblt(b->background->bm_w-15, h, 5, y, 5, y, b->background, &(grd_curcanv->cv_bitmap) );
939                 //gr_bm_bitblt(w, h, x, y, x, y, b->background, &(grd_curcanv->cv_bitmap) );
940
941                 gr_string( x, y, s );
942
943                 if (p)  {
944                         gr_get_string_size(s1, &w, &h, &aw  );
945
946                         // CHANGED
947                         gr_bm_bitblt(w, 1, x+w1-w, y, x+w1-w, y, b->background, &(grd_curcanv->cv_bitmap) );
948                         // CHANGED
949                         gr_bm_bitblt(w, 1, x+w1-w, y+h-1, x+w1-w, y, b->background, &(grd_curcanv->cv_bitmap) );
950
951                         gr_string( x+w1-w, y, s1 );
952
953                         *p = '\t';
954                 }
955 }
956
957
958 // Draw a left justfied string with black background.
959 void nm_string_black( bkg * b, int w1,int x, int y, char * s )
960 {
961         int w,h,aw;
962         gr_get_string_size(s, &w, &h, &aw  );
963         b = b;                                  
964         if (w1 == 0) w1 = w;
965
966         WIN(DDGRLOCK(dd_grd_curcanv));
967                 gr_setcolor( BM_XRGB(2,2,2) );
968                 gr_rect( x-1, y-1, x-1, y+h-1 );
969                 gr_rect( x-1, y-1, x+w1-1, y-1 );
970
971          
972                 gr_setcolor( BM_XRGB(5,5,5) );
973                 gr_rect( x, y+h, x+w1, y+h);
974                 gr_rect( x+w1, y-1, x+w1, y+h );
975      
976                 gr_setcolor( BM_XRGB(0,0,0) );
977                 gr_rect( x, y, x+w1-1, y+h-1 );
978         
979                 gr_string( x+1, y+1, s );
980         WIN(DDGRUNLOCK(dd_grd_curcanv));
981 }
982
983
984 // Draw a right justfied string
985 void nm_rstring( bkg * b,int w1,int x, int y, char * s )
986 {
987         int w,h,aw;
988         gr_get_string_size(s, &w, &h, &aw  );
989         x -= 3;
990
991         if (w1 == 0) w1 = w;
992
993         //mprintf( 0, "Width = %d, string='%s'\n", w, s );
994
995         // CHANGED
996         WIN(DDGRLOCK(dd_grd_curcanv));
997                 gr_bm_bitblt(w1, h, x-w1, y, x-w1, y, b->background, &(grd_curcanv->cv_bitmap) );
998                 gr_string( x-w, y, s );
999         WIN(DDGRUNLOCK(dd_grd_curcanv));
1000 }
1001
1002 #include "timer.h"
1003
1004 //for text items, constantly redraw cursor (to achieve flash)
1005 void update_cursor( newmenu_item *item)
1006 {
1007         int w,h,aw;
1008         fix time = timer_get_approx_seconds();
1009         int x,y;
1010         char * text = item->text;
1011
1012         Assert(item->type==NM_TYPE_INPUT_MENU || item->type==NM_TYPE_INPUT);
1013
1014         while( *text )  {
1015                 gr_get_string_size(text, &w, &h, &aw  );
1016                 if ( w > item->w-10 )
1017                         text++;
1018                 else
1019                         break;
1020         }
1021         if (*text==0) 
1022                 w = 0;
1023         x = item->x+w; y = item->y;
1024
1025 WIN(DDGRLOCK(dd_grd_curcanv));
1026         if (time & 0x8000)
1027                 gr_string( x, y, CURSOR_STRING );
1028         else {
1029                 gr_setcolor( BM_XRGB(0,0,0) );
1030                 gr_rect( x, y, x+grd_curcanv->cv_font->ft_w-1, y+grd_curcanv->cv_font->ft_h-1 );
1031         }
1032 WIN(DDGRUNLOCK(dd_grd_curcanv));
1033 }
1034
1035 void nm_string_inputbox( bkg *b, int w, int x, int y, char * text, int current )
1036 {
1037         int w1,h1,aw;
1038
1039         while( *text )  {
1040                 gr_get_string_size(text, &w1, &h1, &aw  );
1041                 if ( w1 > w-10 )
1042                         text++;
1043                 else
1044                         break;
1045         }
1046         if ( *text == 0 )
1047                 w1 = 0;
1048
1049    nm_string_black( b, w, x, y, text );
1050                 
1051         if ( current )  {
1052                 gr_string( x+w1+1, y, CURSOR_STRING );
1053         }
1054 }
1055
1056 void draw_item( bkg * b, newmenu_item *item, int is_current,int tiny )
1057 {
1058        if (tiny)
1059         {
1060          if (is_current)
1061           gr_set_fontcolor(gr_find_closest_color_current(57,49,20),-1);
1062          else
1063           gr_set_fontcolor(gr_find_closest_color_current(29,29,47),-1);
1064
1065          if (item->text[0]=='\t')
1066           gr_set_fontcolor (gr_find_closest_color_current(63,63,63),-1);
1067         }
1068        else
1069         {
1070          if (is_current)
1071           grd_curcanv->cv_font = SELECTED_FONT;
1072          else
1073           grd_curcanv->cv_font = NORMAL_FONT;
1074
1075                 #ifdef WINDOWS
1076                         if (is_current && item->type == NM_TYPE_TEXT) 
1077                                 grd_curcanv->cv_font = NORMAL_FONT;
1078                 #endif
1079         }
1080
1081 WIN(DDGRLOCK(dd_grd_curcanv));  
1082         switch( item->type )    {
1083         case NM_TYPE_TEXT:
1084       // grd_curcanv->cv_font=TEXT_FONT;
1085                 // fall through on purpose
1086
1087         case NM_TYPE_MENU:
1088                 nm_string( b, item->w, item->x, item->y, item->text );
1089                 break;
1090         case NM_TYPE_SLIDER:    {
1091                 int j;
1092                 if (item->value < item->min_value) item->value=item->min_value;
1093                 if (item->value > item->max_value) item->value=item->max_value;
1094                 sprintf( item->saved_text, "%s\t%s", item->text, SLIDER_LEFT );
1095                 for (j=0; j<(item->max_value-item->min_value+1); j++ )  {
1096                         sprintf( item->saved_text, "%s%s", item->saved_text,SLIDER_MIDDLE );
1097                 }
1098                 sprintf( item->saved_text, "%s%s", item->saved_text,SLIDER_RIGHT );
1099                 
1100                 item->saved_text[item->value+1+strlen(item->text)+1] = SLIDER_MARKER[0];
1101                 
1102                 nm_string_slider( b, item->w, item->x, item->y, item->saved_text );
1103                 }
1104                 break;
1105         case NM_TYPE_INPUT_MENU:
1106                 if ( item->group==0 )           {
1107                         nm_string( b, item->w, item->x, item->y, item->text );
1108                 } else {
1109                         nm_string_inputbox( b, item->w, item->x, item->y, item->text, is_current );
1110                 }
1111                 break;
1112         case NM_TYPE_INPUT:
1113                 nm_string_inputbox( b, item->w, item->x, item->y, item->text, is_current );
1114                 break;
1115         case NM_TYPE_CHECK:
1116                 nm_string( b, item->w, item->x, item->y, item->text );
1117                 if (item->value)
1118                         nm_rstring( b,item->right_offset,item->x, item->y, CHECKED_CHECK_BOX );
1119                 else                                                                                                              
1120                         nm_rstring( b,item->right_offset,item->x, item->y, NORMAL_CHECK_BOX );
1121                 break;
1122         case NM_TYPE_RADIO:
1123                 nm_string( b, item->w, item->x, item->y, item->text );
1124                 if (item->value)
1125                         nm_rstring( b,item->right_offset, item->x, item->y, CHECKED_RADIO_BOX );
1126                 else
1127                         nm_rstring( b,item->right_offset, item->x, item->y, NORMAL_RADIO_BOX );
1128                 break;
1129         case NM_TYPE_NUMBER:    {
1130                 char text[10];
1131                 if (item->value < item->min_value) item->value=item->min_value;
1132                 if (item->value > item->max_value) item->value=item->max_value;
1133                 nm_string( b, item->w, item->x, item->y, item->text );
1134                 sprintf( text, "%d", item->value );
1135                 nm_rstring( b,item->right_offset,item->x, item->y, text );
1136                 }
1137                 break;
1138         }
1139 WIN(DDGRUNLOCK(dd_grd_curcanv));
1140
1141 }
1142
1143 char *Newmenu_allowed_chars=NULL;
1144
1145 //returns true if char is allowed
1146 int char_allowed(char c)
1147 {
1148         char *p = Newmenu_allowed_chars;
1149
1150         if (!p)
1151                 return 1;
1152
1153         while (*p) {
1154                 Assert(p[1]);
1155
1156                 if (c>=p[0] && c<=p[1])
1157                         return 1;
1158
1159                 p += 2;
1160         }
1161
1162         return 0;
1163 }
1164
1165 void strip_end_whitespace( char * text )
1166 {
1167         int i,l;
1168         l = strlen( text );
1169         for (i=l-1; i>=0; i-- ) {
1170                 if ( isspace(text[i]) )
1171                         text[i] = 0;
1172                 else
1173                         return;
1174         }
1175 }
1176
1177 int newmenu_do( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
1178 {
1179         return newmenu_do3( title, subtitle, nitems, item, subfunction, 0, NULL, -1, -1 );
1180 }
1181 int newmenu_dotiny( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
1182 {
1183         return newmenu_do4( title, subtitle, nitems, item, subfunction, 0, NULL, LHX(310), -1, 1 );
1184 }
1185
1186
1187 int newmenu_dotiny2( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
1188 {
1189         return newmenu_do4( title, subtitle, nitems, item, subfunction, 0, NULL, -1, -1, 1 );
1190 }
1191
1192
1193 int newmenu_do1( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int citem )
1194 {
1195         return newmenu_do3( title, subtitle, nitems, item, subfunction, citem, NULL, -1, -1 );
1196 }
1197
1198
1199 int newmenu_do2( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int citem, char * filename )
1200 {
1201         return newmenu_do3( title, subtitle, nitems, item, subfunction, citem, filename, -1, -1 );
1202 }
1203 int newmenu_do3( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int citem, char * filename, int width, int height )
1204  {
1205   return newmenu_do4( title, subtitle, nitems, item, subfunction, citem, filename, width, height,0 );
1206  }
1207
1208 int newmenu_do_fixedfont( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int citem, char * filename, int width, int height){
1209         set_screen_mode(SCREEN_MENU);//hafta set the screen mode before calling or fonts might get changed/freed up if screen res changes
1210 //      return newmenu_do3_real( title, subtitle, nitems, item, subfunction, citem, filename, width,height, GAME_FONT, GAME_FONT, GAME_FONT, GAME_FONT);
1211         return newmenu_do4( title, subtitle, nitems, item, subfunction, citem, filename, width,height, 0);
1212 }
1213
1214 //returns 1 if a control device button has been pressed
1215 int check_button_press()
1216 {
1217         int i;
1218
1219         switch (Config_control_type) {
1220         case    CONTROL_JOYSTICK:
1221         case    CONTROL_FLIGHTSTICK_PRO:
1222         case    CONTROL_THRUSTMASTER_FCS:
1223         case    CONTROL_GRAVIS_GAMEPAD:
1224                 for (i=0; i<4; i++ )    
1225                         if (joy_get_button_down_cnt(i)>0) return 1;
1226                 break;
1227         case    CONTROL_MOUSE:
1228         case    CONTROL_CYBERMAN:
1229         #ifndef MACINTOSH                       // don't allow mouse to continue from menu
1230                 for (i=0; i<3; i++ )    
1231                         if (mouse_button_down_count(i)>0) return 1;
1232                 break;
1233         #endif
1234         case    CONTROL_WINJOYSTICK:
1235         #ifdef WINDOWS  
1236                 for (i=0; i<4; i++ )    
1237                         if (joy_get_button_down_cnt(i)>0) return 1;
1238         #endif  
1239                 break;
1240         case    CONTROL_NONE:           //keyboard only
1241                 #ifdef APPLE_DEMO
1242                         if (key_checkch())      return 1;                       
1243                 #endif
1244
1245                 break;
1246         default:
1247                 Error("Bad control type (Config_control_type):%i",Config_control_type);
1248         }
1249
1250         return 0;
1251 }
1252
1253 extern int network_request_player_names(int);
1254 extern int RestoringMenu;
1255
1256 #if defined(WINDOWS) || defined(MACINTOSH)
1257 ubyte Hack_DblClick_MenuMode=0;
1258 #endif
1259
1260 MAC(extern ubyte joydefs_calibrating;)
1261
1262 #define CLOSE_X         15
1263 #define CLOSE_Y         15
1264 #define CLOSE_SIZE      10
1265
1266 void draw_close_box(int x,int y)
1267 {
1268         WIN (DDGRLOCK(dd_grd_curcanv));
1269         gr_setcolor( BM_XRGB(0, 0, 0) );
1270         gr_rect(x + CLOSE_X, y + CLOSE_Y, x + CLOSE_X + CLOSE_SIZE, y + CLOSE_Y + CLOSE_SIZE);
1271         gr_setcolor( BM_XRGB(21, 21, 21) );
1272         gr_rect( x + CLOSE_X + 2, y + CLOSE_Y + 2, x + CLOSE_X + CLOSE_SIZE - 2, y + CLOSE_Y + CLOSE_SIZE - 2 );
1273         WIN (DDGRUNLOCK(dd_grd_curcanv));
1274 }
1275
1276 int newmenu_do4( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int citem, char * filename, int width, int height, int TinyMode )
1277 {
1278         int old_keyd_repeat, done;
1279         int  choice,old_choice,i,j,x,y,w,h,aw, tw, th, twidth,fm,right_offset;
1280         int k, nmenus, nothers,ScrollOffset=0,LastScrollCheck=-1,MaxDisplayable,sx,sy;
1281         grs_font * save_font;
1282         int string_width, string_height, average_width;
1283         int ty;
1284         bkg bg;
1285         int all_text=0;         //set true if all text items
1286         int sound_stopped=0,time_stopped=0;
1287    int TopChoice,IsScrollBox=0;   // Is this a scrolling box? Set to false at init
1288    char *Temp,TempVal;
1289         int dont_restore=0;
1290    int MaxOnMenu=MAXDISPLAYABLEITEMS;
1291         WINDOS(dd_grs_canvas *save_canvas, grs_canvas *save_canvas );   
1292 #if defined(MACINTOSH) || defined(WINDOWS)
1293         int mouse_state, omouse_state, dblclick_flag=0;
1294         int mx=0, my=0, x1, x2, y1, y2;
1295         int close_box=0;
1296 #endif
1297 #ifdef MACINTOSH
1298         EventRecord event;              // looking for disk inserted events for CD mounts
1299 #endif
1300
1301         PA_DFX (pa_set_frontbuffer_current());
1302         PA_DFX (pa_set_front_to_read());
1303
1304         WIN(if (!_AppActive) return -1);                // Don't draw message if minimized!
1305         WIN(HideCursorW());
1306         MAC(hide_cursor();)
1307
1308         if (nitems < 1 )
1309     {
1310                 return -1;
1311     } 
1312
1313         WIN(mouse_set_mode(0));         //disable centering mode
1314
1315         MaxDisplayable=nitems;
1316
1317 #ifdef OGL
1318         set_screen_mode(SCREEN_MENU);
1319 #endif
1320         set_popup_screen();
1321
1322         if ( Function_mode == FMODE_GAME && !(Game_mode & GM_MULTI)) {
1323                 digi_pause_digi_sounds();
1324                 sound_stopped = 1;
1325         }
1326
1327         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
1328         {
1329                 time_stopped = 1;
1330                 stop_time();
1331                 #ifdef TACTILE 
1332                   if (TactileStick)     
1333                           DisableForces();      
1334                 #endif
1335         }
1336
1337 #ifdef WINDOWS
1338 RePaintNewmenu4:
1339 #endif
1340         WINDOS( save_canvas = dd_grd_curcanv, save_canvas = grd_curcanv );
1341
1342         WINDOS( dd_gr_set_current_canvas(NULL), gr_set_current_canvas(NULL) );
1343
1344         save_font = grd_curcanv->cv_font;
1345
1346         tw = th = 0;
1347
1348         if ( title )    {
1349                 grd_curcanv->cv_font = TITLE_FONT;
1350                 gr_get_string_size(title,&string_width,&string_height,&average_width );
1351                 tw = string_width;
1352                 th = string_height;
1353         }
1354         if ( subtitle ) {
1355                 grd_curcanv->cv_font = SUBTITLE_FONT;
1356                 gr_get_string_size(subtitle,&string_width,&string_height,&average_width );
1357                 if (string_width > tw )
1358                         tw = string_width;
1359                 th += string_height;
1360         }
1361
1362         th += LHY(8);           //put some space between titles & body
1363
1364         if (TinyMode)
1365         grd_curcanv->cv_font = SMALL_FONT;
1366         else 
1367         grd_curcanv->cv_font = NORMAL_FONT;
1368
1369         w = aw = 0;
1370         h = th;
1371         nmenus = nothers = 0;
1372
1373         // Find menu height & width (store in w,h)
1374         for (i=0; i<nitems; i++ )       {
1375                 item[i].redraw=1;
1376                 item[i].y = h;
1377                 gr_get_string_size(item[i].text,&string_width,&string_height,&average_width );
1378                 item[i].right_offset = 0;
1379                 
1380                 if (SurfingNet)
1381                         string_height+=LHY(3);
1382
1383                 item[i].saved_text[0] = '\0';
1384
1385                 if ( item[i].type == NM_TYPE_SLIDER )   {
1386                         int w1,h1,aw1;
1387                         nothers++;
1388                         sprintf( item[i].saved_text, "%s", SLIDER_LEFT );
1389                         for (j=0; j<(item[i].max_value-item[i].min_value+1); j++ )      {
1390                                 sprintf( item[i].saved_text, "%s%s", item[i].saved_text,SLIDER_MIDDLE );
1391                         }
1392                         sprintf( item[i].saved_text, "%s%s", item[i].saved_text,SLIDER_RIGHT );
1393                         gr_get_string_size(item[i].saved_text,&w1,&h1,&aw1 );
1394                         string_width += w1 + aw;
1395                 }
1396
1397                 if ( item[i].type == NM_TYPE_MENU )     {
1398                         nmenus++;
1399                 }
1400
1401                 if ( item[i].type == NM_TYPE_CHECK )    {
1402                         int w1,h1,aw1;
1403                         nothers++;
1404                         gr_get_string_size(NORMAL_CHECK_BOX, &w1, &h1, &aw1  );
1405                         item[i].right_offset = w1;
1406                         gr_get_string_size(CHECKED_CHECK_BOX, &w1, &h1, &aw1  );
1407                         if (w1 > item[i].right_offset)
1408                                 item[i].right_offset = w1;
1409                 }
1410                 
1411                 if (item[i].type == NM_TYPE_RADIO ) {
1412                         int w1,h1,aw1;
1413                         nothers++;
1414                         gr_get_string_size(NORMAL_RADIO_BOX, &w1, &h1, &aw1  );
1415                         item[i].right_offset = w1;
1416                         gr_get_string_size(CHECKED_RADIO_BOX, &w1, &h1, &aw1  );
1417                         if (w1 > item[i].right_offset)
1418                                 item[i].right_offset = w1;
1419                 }
1420
1421                 if  (item[i].type==NM_TYPE_NUMBER )     {
1422                         int w1,h1,aw1;
1423                         char test_text[20];
1424                         nothers++;
1425                         sprintf( test_text, "%d", item[i].max_value );
1426                         gr_get_string_size( test_text, &w1, &h1, &aw1 );
1427                         item[i].right_offset = w1;
1428                         sprintf( test_text, "%d", item[i].min_value );
1429                         gr_get_string_size( test_text, &w1, &h1, &aw1 );
1430                         if ( w1 > item[i].right_offset)
1431                                 item[i].right_offset = w1;
1432                 }
1433
1434                 if ( item[i].type == NM_TYPE_INPUT )    {
1435                         Assert( strlen(item[i].text) < NM_MAX_TEXT_LEN );
1436                         strcpy(item[i].saved_text, item[i].text );
1437                         nothers++;
1438                         string_width = item[i].text_len*grd_curcanv->cv_font->ft_w+((MenuHires?3:1)*item[i].text_len);
1439                         if ( string_width > MAX_TEXT_WIDTH ) 
1440                                 string_width = MAX_TEXT_WIDTH;
1441                         item[i].value = -1;
1442                 }
1443
1444                 if ( item[i].type == NM_TYPE_INPUT_MENU )       {
1445                         Assert( strlen(item[i].text) < NM_MAX_TEXT_LEN );
1446                         strcpy(item[i].saved_text, item[i].text );
1447                         nmenus++;
1448                         string_width = item[i].text_len*grd_curcanv->cv_font->ft_w+((MenuHires?3:1)*item[i].text_len);
1449                         item[i].value = -1;
1450                         item[i].group = 0;
1451                 }
1452
1453                 item[i].w = string_width;
1454                 item[i].h = string_height;
1455
1456                 if ( string_width > w )
1457                         w = string_width;               // Save maximum width
1458                 if ( average_width > aw )
1459                         aw = average_width;
1460                 h += string_height+1;           // Find the height of all strings
1461         }
1462
1463    // Big hack for allowing the netgame options menu to spill over
1464
1465    MaxOnMenu=MAXDISPLAYABLEITEMS;
1466    if (ExtGameStatus==GAMESTAT_NETGAME_OPTIONS || ExtGameStatus==GAMESTAT_MORE_NETGAME_OPTIONS)
1467                 MaxOnMenu++;
1468
1469    if (!TinyMode && (h>((MaxOnMenu+1)*(string_height+1))+(LHY(8))))
1470     {
1471      IsScrollBox=1;
1472      h=(MaxOnMenu*(string_height+1)+LHY(8));
1473      MaxDisplayable=MaxOnMenu;
1474      mprintf ((0,"Hey, this is a scroll box!\n"));
1475     }
1476    else
1477     IsScrollBox=0;
1478
1479         right_offset=0;
1480
1481         if ( width > -1 )
1482                 w = width;
1483
1484         if ( height > -1 )
1485                 h = height;
1486
1487         for (i=0; i<nitems; i++ )       {
1488                 item[i].w = w;
1489                 if (item[i].right_offset > right_offset )
1490                         right_offset = item[i].right_offset;
1491         }
1492         if (right_offset > 0 )
1493                 right_offset += 3;
1494
1495         //gr_get_string_size("",&string_width,&string_height,&average_width );
1496
1497         w += right_offset;
1498
1499
1500         twidth = 0;
1501         if ( tw > w )   {
1502                 twidth = ( tw - w )/2;
1503                 w = tw;
1504         }
1505
1506    if (RestoringMenu)
1507          { right_offset=0; twidth=0;}
1508
1509         mprintf(( 0, "Right offset = %d\n", right_offset ));
1510
1511
1512         // Find min point of menu border
1513 //      x = (grd_curscreen->sc_w-w)/2;
1514 //      y = (grd_curscreen->sc_h-h)/2;
1515
1516         w += MenuHires?60:30;
1517         h += MenuHires?60:30;
1518
1519         if ( w > grd_curcanv->cv_bitmap.bm_w ) w = grd_curcanv->cv_bitmap.bm_w;
1520         if ( h > grd_curcanv->cv_bitmap.bm_h ) h = grd_curcanv->cv_bitmap.bm_h;
1521
1522         x = (grd_curcanv->cv_bitmap.bm_w-w)/2;
1523         y = (grd_curcanv->cv_bitmap.bm_h-h)/2;
1524
1525         if ( x < 0 ) x = 0;
1526         if ( y < 0 ) y = 0;
1527
1528         if ( filename != NULL ) {
1529                 nm_draw_background1( filename );
1530                 gr_palette_load(gr_palette);
1531         }
1532
1533 // Save the background of the display
1534 //              Win95 must refer to the screen as a dd_grs_canvas, so...
1535         WINDOS (        bg.menu_canvas = dd_gr_create_sub_canvas( dd_grd_screencanv, x, y, w, h ),
1536                         bg.menu_canvas = gr_create_sub_canvas( &grd_curscreen->sc_canvas, x, y, w, h )
1537         );
1538         WINDOS (        dd_gr_set_current_canvas( bg.menu_canvas ), 
1539                         gr_set_current_canvas(bg.menu_canvas)   );
1540
1541         if ( filename == NULL ) {
1542                 // Save the background under the menu...
1543                 #ifdef TACTILE
1544                         if (TactileStick)
1545                                 DisableForces();
1546                 #endif
1547                 
1548 #if defined(POLY_ACC)
1549                 bg.saved = gr_create_bitmap2( w, h, grd_curcanv->cv_bitmap.bm_type, NULL );
1550 #else
1551                 bg.saved = gr_create_bitmap( w, h );
1552 #endif
1553                 Assert( bg.saved != NULL );
1554
1555                 WIN (DDGRLOCK(dd_grd_curcanv));
1556                         gr_bm_bitblt(w, h, 0, 0, 0, 0, &grd_curcanv->cv_bitmap, bg.saved );
1557                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1558
1559                 WINDOS (        dd_gr_set_current_canvas(NULL), 
1560                                         gr_set_current_canvas( NULL ) 
1561                 );
1562
1563                 nm_draw_background(x,y,x+w-1,y+h-1);
1564
1565                 WINDOS (        dd_gr_set_current_canvas(bg.menu_canvas),
1566                                         gr_set_current_canvas( bg.menu_canvas )
1567                 );
1568
1569                 bg.background = gr_create_sub_bitmap(&nm_background,0,0,w,h);
1570
1571         } else {
1572                 bg.saved = NULL;
1573 #if defined(POLY_ACC)
1574                 bg.background = gr_create_bitmap2( w, h, grd_curcanv->cv_bitmap.bm_type, NULL );
1575 #else
1576                 bg.background = gr_create_bitmap( w, h );
1577 #endif
1578                 Assert( bg.background != NULL );
1579                 
1580                 WIN (DDGRLOCK(dd_grd_curcanv));
1581                         gr_bm_bitblt(w, h, 0, 0, 0, 0, &grd_curcanv->cv_bitmap, bg.background );
1582                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1583         }
1584
1585 // ty = 15 + (yborder/4);
1586
1587         ty = MenuHires?30:15;
1588
1589         if ( title )    {
1590                 grd_curcanv->cv_font = TITLE_FONT;
1591                 gr_set_fontcolor( GR_GETCOLOR(31,31,31), -1 );
1592                 gr_get_string_size(title,&string_width,&string_height,&average_width );
1593                 tw = string_width;
1594                 th = string_height;
1595                 WIN (DDGRLOCK(dd_grd_curcanv));
1596                         gr_printf( 0x8000, ty, title );
1597                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1598                 ty += th;
1599         }
1600
1601         if ( subtitle ) {
1602                 grd_curcanv->cv_font = SUBTITLE_FONT;
1603                 gr_set_fontcolor( GR_GETCOLOR(21,21,21), -1 );
1604                 gr_get_string_size(subtitle,&string_width,&string_height,&average_width );
1605                 tw = string_width;
1606                 th = string_height;
1607                 WIN (DDGRLOCK(dd_grd_curcanv));
1608                         gr_printf( 0x8000, ty, subtitle );
1609                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1610                 ty += th;
1611         }
1612
1613         if (TinyMode)
1614         grd_curcanv->cv_font = SMALL_FONT;
1615         else 
1616         grd_curcanv->cv_font = NORMAL_FONT;
1617         
1618         // Update all item's x & y values.
1619         for (i=0; i<nitems; i++ )       {
1620                 item[i].x = (MenuHires?30:15) + twidth + right_offset;
1621                 item[i].y += (MenuHires?30:15);
1622                 if ( item[i].type==NM_TYPE_RADIO )      {
1623                         fm = -1;        // find first marked one
1624                         for ( j=0; j<nitems; j++ )      {
1625                                 if ( item[j].type==NM_TYPE_RADIO && item[j].group==item[i].group )      {
1626                                         if (fm==-1 && item[j].value)
1627                                                 fm = j;
1628                                         item[j].value = 0;
1629                                 }
1630                         }
1631                         if ( fm>=0 )    
1632                                 item[fm].value=1;
1633                         else
1634                                 item[i].value=1;
1635                 }
1636         }
1637
1638         old_keyd_repeat = keyd_repeat;
1639         keyd_repeat = 1;
1640
1641         if (citem==-1)  {
1642                 choice = -1;
1643         } else {
1644                 if (citem < 0 ) citem = 0;
1645                 if (citem > nitems-1 ) citem = nitems-1;
1646                 choice = citem;
1647         
1648         #if defined(WINDOWS) || defined(MACINTOSH) 
1649                 dblclick_flag = 1;
1650         #endif
1651
1652                 while ( item[choice].type==NM_TYPE_TEXT )       {
1653                         choice++;
1654                         if (choice >= nitems ) {
1655                                 choice=0; 
1656                         }
1657                         if (choice == citem ) {
1658                                 choice=0; 
1659                                 all_text=1;
1660                                 break; 
1661                         }
1662                 }
1663         } 
1664         done = 0;
1665    TopChoice=choice;
1666
1667         gr_update();
1668         // Clear mouse, joystick to clear button presses.
1669         game_flush_inputs();
1670
1671 #if defined(WINDOWS) || defined(MACINTOSH)
1672         mouse_state = omouse_state = 0;
1673         if (filename == NULL && !MenuReordering) {
1674                 draw_close_box(0,0);
1675                 close_box = 1;
1676         }
1677 #endif
1678
1679 #ifdef WINDOWS
1680         if (!MenuReordering) {
1681                 ShowCursorW();
1682                 SetCursor (LoadCursor(NULL,IDC_ARROW));
1683         }
1684 #endif
1685
1686 #ifdef MACINTOSH  
1687         if (!joydefs_calibrating)
1688                 show_cursor();
1689 #endif
1690
1691    mprintf ((0,"Set to true!\n"));
1692
1693         while(!done)    {
1694         #ifdef WINDOWS
1695                 MSG msg;
1696
1697                 DoMessageStuff(&msg);
1698
1699                 if (_RedrawScreen) {
1700                         _RedrawScreen = FALSE;
1701                 
1702                         if (!filename) {
1703                                 gr_free_bitmap(bg.saved);
1704                                 d_free( bg.background );
1705                         }
1706                         else    
1707                                 gr_free_bitmap(bg.background);
1708
1709                         dd_gr_free_sub_canvas( bg.menu_canvas );
1710                         grd_curcanv->cv_font = save_font;
1711                         dd_grd_curcanv = save_canvas;
1712
1713                         goto RePaintNewmenu4;
1714                 }
1715
1716                 DDGRRESTORE;
1717
1718         #endif
1719
1720         
1721 #ifdef MACINTOSH
1722                 omouse_state = mouse_state;     
1723                 mouse_state = mouse_button_state(0);
1724                 if (!joydefs_calibrating)
1725                         show_cursor();          // possibly hidden
1726 #endif
1727
1728 #ifdef WINDOWS
1729                 omouse_state = mouse_state;     
1730                 if (!MenuReordering)
1731                         mouse_state = mouse_button_state(0);
1732 //@@      mprintf ((0,"mouse state:%d\n",mouse_state));
1733 #endif
1734
1735                 //see if redbook song needs to be restarted
1736                 songs_check_redbook_repeat();
1737
1738                 //network_listen();
1739
1740                 k = key_inkey();
1741
1742         if (subfunction)
1743         (*subfunction)(nitems,item,&k,choice);
1744
1745 #ifdef NETWORK
1746                 if (!time_stopped)      {
1747                         // Save current menu box
1748                         if (multi_menu_poll() == -1)
1749                                 k = -2;
1750                 }
1751 #endif
1752
1753                 if ( k<-1 ) {
1754                         dont_restore = (k == -3);               //-3 means don't restore
1755                         choice = k;
1756                         k = -1;
1757                         done = 1;
1758                 }
1759 #ifndef WINDOWS
1760                 if (check_button_press())
1761                         done = 1;
1762 #endif
1763
1764 //              if ( (nmenus<2) && (k>0) && (nothers==0) )
1765 //                      done=1;
1766
1767                 old_choice = choice;
1768         
1769                 switch( k )     {
1770
1771 #ifdef NETWORK
1772                 case KEY_I:
1773                  if (SurfingNet && !already_showing_info)
1774                    {
1775                          show_extra_netgame_info(choice-2);
1776                    }
1777                  if (SurfingNet && already_showing_info)
1778                         {
1779                          done=1;
1780                          choice=-1;
1781                         }
1782                  break;
1783                 case KEY_U:
1784                  if (SurfingNet && !already_showing_info)
1785                    {
1786                          network_request_player_names(choice-2);
1787                    }
1788                  if (SurfingNet && already_showing_info)
1789                         {
1790                          done=1;
1791                          choice=-1;
1792                         }
1793                  break;
1794 #endif
1795                 case KEY_PAUSE:
1796                  if (Pauseable_menu)
1797                    {    
1798                          Pauseable_menu=0;
1799                          done=1;
1800                          choice=-1;
1801                    }
1802                  break;
1803                 case KEY_TAB + KEY_SHIFTED:
1804                 case KEY_UP:
1805                 case KEY_PAD8:
1806                         if (all_text) break;
1807                         do {
1808                                 choice--;
1809
1810                 if (IsScrollBox)
1811                 {
1812                         LastScrollCheck=-1;
1813                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1814                                    
1815                 if (choice<TopChoice)
1816                         { choice=TopChoice; break; }
1817
1818                 if (choice<ScrollOffset)
1819                {
1820                         for (i=0;i<nitems;i++)
1821                                 item[i].redraw=1;
1822                      ScrollOffset--;
1823                      mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1824                }
1825                 }
1826                 else
1827                 {
1828                         if (choice >= nitems ) choice=0;
1829                 if (choice < 0 ) choice=nitems-1;
1830                 }
1831                         } while ( item[choice].type==NM_TYPE_TEXT );
1832                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1833                                 item[choice].value = -1;
1834                         if ((old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))     {
1835                                 item[old_choice].group=0;
1836                                 strcpy(item[old_choice].text, item[old_choice].saved_text );
1837                                 item[old_choice].value = -1;
1838                         }
1839                         if (old_choice>-1) 
1840                                 item[old_choice].redraw = 1;
1841                         item[choice].redraw=1;
1842                         break;
1843                 case KEY_TAB:
1844                 case KEY_DOWN:
1845                 case KEY_PAD2:
1846         // ((0,"Pressing down! IsScrollBox=%d",IsScrollBox));
1847                 if (all_text) break;
1848                         do {
1849                                 choice++;
1850
1851                         if (IsScrollBox)
1852                 {
1853                 LastScrollCheck=-1;
1854                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1855                                    
1856                 if (choice==nitems)
1857                 { choice--; break; }
1858
1859                 if (choice>=MaxOnMenu+ScrollOffset)
1860                 {
1861                 for (i=0;i<nitems;i++)
1862                                 item[i].redraw=1;
1863                   ScrollOffset++;
1864                   mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1865                 }
1866                 }
1867             else
1868             {
1869                         if (choice < 0 ) choice=nitems-1;
1870                         if (choice >= nitems ) choice=0;
1871                 }
1872
1873                         } while ( item[choice].type==NM_TYPE_TEXT );
1874                                                       
1875                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1876                                 item[choice].value = -1;
1877                         if ( (old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))    {
1878                                 item[old_choice].group=0;
1879                                 strcpy(item[old_choice].text, item[old_choice].saved_text );    
1880                                 item[old_choice].value = -1;
1881                         }
1882                         if (old_choice>-1)
1883                                 item[old_choice].redraw=1;
1884                         item[choice].redraw=1;
1885                         break;
1886                 case KEY_SPACEBAR:
1887                         if ( choice > -1 )      {
1888                                 switch( item[choice].type )     {
1889                                 case NM_TYPE_MENU:
1890                                 case NM_TYPE_INPUT:
1891                                 case NM_TYPE_INPUT_MENU:
1892                                         break;
1893                                 case NM_TYPE_CHECK:
1894                                         if ( item[choice].value )
1895                                                 item[choice].value = 0;
1896                                         else
1897                                                 item[choice].value = 1;
1898                                         mprintf ((0,"ISB=%d MDI=%d SO=%d choice=%d\n",IsScrollBox,MAXDISPLAYABLEITEMS,ScrollOffset,choice));
1899                                         if (IsScrollBox)
1900                                          {
1901                                                 if (choice==(MaxOnMenu+ScrollOffset-1) || choice==ScrollOffset)
1902                                                  {
1903                                                    mprintf ((0,"Special redraw!\n"));
1904                                                         LastScrollCheck=-1;                                     
1905                                                  }
1906                                          }
1907                                 
1908                                         item[choice].redraw=1;
1909                                         break;
1910                                 case NM_TYPE_RADIO:
1911                                         for (i=0; i<nitems; i++ )       {
1912                                                 if ((i!=choice) && (item[i].type==NM_TYPE_RADIO) && (item[i].group==item[choice].group) && (item[i].value) )    {
1913                                                         item[i].value = 0;
1914                                                         item[i].redraw = 1;
1915                                                 }
1916                                         }
1917                                         item[choice].value = 1;
1918                                         item[choice].redraw = 1;
1919                                         break;
1920                                 }       
1921                         }
1922                         break;
1923
1924                 case KEY_SHIFTED+KEY_UP:
1925                  if (MenuReordering && choice!=TopChoice)
1926                   {
1927                    Temp=item[choice].text;
1928                    TempVal=item[choice].value;
1929                    item[choice].text=item[choice-1].text;
1930                    item[choice].value=item[choice-1].value;
1931                    item[choice-1].text=Temp;
1932                    item[choice-1].value=TempVal;
1933                    item[choice].redraw=1;
1934                    item[choice-1].redraw=1;
1935                    choice--;
1936                   }
1937                  break;
1938                 case KEY_SHIFTED+KEY_DOWN:
1939                  if (MenuReordering && choice!=(nitems-1))
1940                   {
1941                    Temp=item[choice].text;
1942                    TempVal=item[choice].value;
1943                    item[choice].text=item[choice+1].text;
1944                    item[choice].value=item[choice+1].value;
1945                    item[choice+1].text=Temp;
1946                    item[choice+1].value=TempVal;
1947                    item[choice].redraw=1;
1948                    item[choice+1].redraw=1;
1949                    choice++;
1950                   }
1951                  break;
1952                 
1953                 case KEY_ENTER:
1954                 case KEY_PADENTER:
1955                         if ( (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==0)) {
1956                                 item[choice].group = 1;
1957                                 item[choice].redraw = 1;
1958                                 if ( !strnicmp( item[choice].saved_text, TXT_EMPTY, strlen(TXT_EMPTY) ) )       {
1959                                         item[choice].text[0] = 0;
1960                                         item[choice].value = -1;
1961                                 } else {        
1962                                         strip_end_whitespace(item[choice].text);
1963                                 }
1964                         } else
1965                                 done = 1;
1966                         break;
1967
1968                 case KEY_ESC:
1969                         if ( (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==1)) {
1970                                 item[choice].group=0;
1971                                 strcpy(item[choice].text, item[choice].saved_text );    
1972                                 item[choice].redraw=1;
1973                                 item[choice].value = -1;
1974                         } else {
1975                                 done = 1;
1976                                 choice = -1;
1977                         }
1978                         break;
1979
1980                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
1981                 case KEY_PRINT_SCREEN:
1982                         MAC(hide_cursor());
1983                         save_screen_shot(0);
1984                         PA_DFX (pa_set_frontbuffer_current());
1985                         PA_DFX (pa_set_front_to_read());
1986                         for (i=0;i<nitems;i++)
1987                                 item[i].redraw=1;
1988                         
1989                         MAC(show_cursor());
1990                         MAC(key_flush());
1991                         break;
1992
1993                 #ifdef MACINTOSH
1994
1995                 case KEY_COMMAND+KEY_RIGHT:
1996                         songs_goto_next_song();
1997                         break;
1998                 case KEY_COMMAND+KEY_LEFT:
1999                         songs_goto_prev_song();
2000                         break;
2001                 case KEY_COMMAND+KEY_UP:
2002                         songs_play_level_song(1);
2003                         break;
2004                 case KEY_COMMAND+KEY_DOWN:
2005                         songs_stop_redbook();
2006                         break;
2007
2008                 case KEY_COMMAND+KEY_M:
2009                         k = -1;
2010                         #if !defined(SHAREWARE) || defined(APPLE_DEMO)
2011                         if ( (Game_mode & GM_MULTI) )           // don't process in multiplayer games
2012                                 break;
2013
2014                         key_close();            // no processing of keys with keyboard handler.. jeez                           
2015                         stop_time();
2016                         hide_cursor();
2017                         show_boxed_message ("Mounting CD\nESC to quit");        
2018                         RBAMountDisk();         // OS has totaly control of the CD.
2019                         if (Function_mode == FMODE_MENU)
2020                                 songs_play_song(SONG_TITLE,1);
2021                         else if (Function_mode == FMODE_GAME)
2022                                 songs_play_level_song( Current_level_num );
2023                         clear_boxed_message();
2024                         show_cursor();
2025                         key_init();
2026                         key_flush();
2027                         start_time();
2028                         #endif
2029                         
2030                         break;
2031
2032                 case KEY_COMMAND+KEY_E:
2033                         songs_stop_redbook();
2034                         RBAEjectDisk();
2035                         k = -1;         // force key not to register
2036                         break;
2037                         
2038                 case KEY_COMMAND+KEY_Q: {
2039                         extern void macintosh_quit();
2040                         
2041                         if ( !(Game_mode & GM_MULTI) )
2042                                 macintosh_quit();
2043                         if (!joydefs_calibrating)
2044                                 show_cursor();
2045                         k = -1;         // force key not to register
2046                         break;
2047                 }
2048                 #endif
2049
2050                 #ifndef NDEBUG
2051                 case KEY_BACKSP:        
2052                         if ( (choice>-1) && (item[choice].type!=NM_TYPE_INPUT)&&(item[choice].type!=NM_TYPE_INPUT_MENU))
2053                                 Int3(); 
2054                         break;
2055                 #endif
2056
2057                 }
2058
2059 #if defined(MACINTOSH) || defined(WINDOWS) // for mouse selection of menu's etc.
2060                 WIN(Sleep(100));
2061                 if ( !done && mouse_state && !omouse_state && !all_text ) {
2062                         mouse_get_pos(&mx, &my);
2063                         for (i=0; i<nitems; i++ )       {
2064                                 x1 = grd_curcanv->cv_bitmap.bm_x + item[i].x - item[i].right_offset - 6;
2065                                 x2 = x1 + item[i].w;
2066                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[i].y;
2067                                 y2 = y1 + item[i].h;
2068                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
2069                                         if (i+ScrollOffset != choice) {
2070                                                 if(Hack_DblClick_MenuMode) dblclick_flag = 0; 
2071                                         }
2072                                         
2073                                         choice = i + ScrollOffset;
2074
2075                                         switch( item[choice].type )     {
2076                                         case NM_TYPE_CHECK:
2077                                                 if ( item[choice].value )
2078                                                         item[choice].value = 0;
2079                                                 else
2080                                                         item[choice].value = 1;
2081                                                 item[choice].redraw=1;
2082
2083                                                 if (IsScrollBox)
2084                                                         LastScrollCheck=-1;
2085 #if 0
2086                                                 if (IsScrollBox)
2087                                                  {
2088                                                         if (choice==(MaxOnMenu+ScrollOffset-1) || choice==ScrollOffset)
2089                                                          {
2090                                                            mprintf ((0,"Special redraw!\n"));
2091                                                                 LastScrollCheck=-1;                                     
2092                                                          }
2093                                                  }
2094 #endif
2095                                                 break;
2096                                         case NM_TYPE_RADIO:
2097                                                 for (i=0; i<nitems; i++ )       {
2098                                                         if ((i!=choice) && (item[i].type==NM_TYPE_RADIO) && (item[i].group==item[choice].group) && (item[i].value) )    {
2099                                                                 item[i].value = 0;
2100                                                                 item[i].redraw = 1;
2101                                                         }
2102                                                 }
2103                                                 item[choice].value = 1;
2104                                                 item[choice].redraw = 1;
2105                                                 break;
2106                                         }
2107                                         item[old_choice].redraw=1;
2108                                         break;
2109                                 }
2110                         }
2111                 }
2112
2113                 if (mouse_state && all_text)
2114                         done = 1;
2115                 
2116                 if ( !done && mouse_state && !all_text ) {
2117                         mouse_get_pos(&mx, &my);
2118                         
2119                         // check possible scrollbar stuff first
2120                         if (IsScrollBox) {
2121                                 int arrow_width, arrow_height, aw;
2122                                 
2123                                 if (ScrollOffset != 0) {
2124                                         gr_get_string_size(UP_ARROW_MARKER, &arrow_width, &arrow_height, &aw);
2125                                         x2 = grd_curcanv->cv_bitmap.bm_x + item[ScrollOffset].x-(MenuHires?24:12);
2126                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[ScrollOffset].y-((string_height+1)*ScrollOffset);
2127                                         x1 = x1 - arrow_width;
2128                                         y2 = y1 + arrow_height;
2129                                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2130                                                 choice--;
2131                                         LastScrollCheck=-1;
2132                                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
2133                                                    
2134                                 if (choice<ScrollOffset)
2135                                {
2136                                         for (i=0;i<nitems;i++)
2137                                                 item[i].redraw=1;
2138                                      ScrollOffset--;
2139                                      mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
2140                                }
2141                                         }
2142                                 }
2143                                 if (ScrollOffset+MaxDisplayable<nitems) {
2144                                         gr_get_string_size(DOWN_ARROW_MARKER, &arrow_width, &arrow_height, &aw);
2145                                         x2 = grd_curcanv->cv_bitmap.bm_x + item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
2146                                         y1 = grd_curcanv->cv_bitmap.bm_y + item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
2147                                         x1 = x1 - arrow_width;
2148                                         y2 = y1 + arrow_height;
2149                                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2150                                                 choice++;
2151                                 LastScrollCheck=-1;
2152                                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
2153                                                    
2154                                 if (choice>=MaxOnMenu+ScrollOffset)
2155                                 {
2156                                 for (i=0;i<nitems;i++)
2157                                                 item[i].redraw=1;
2158                                   ScrollOffset++;
2159                                   mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
2160                                 }
2161                                         }
2162                                 }
2163                         }
2164                         
2165                         for (i=0; i<nitems; i++ )       {
2166                                 x1 = grd_curcanv->cv_bitmap.bm_x + item[i].x - item[i].right_offset - 6;
2167                                 x2 = x1 + item[i].w;
2168                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[i].y;
2169                                 y2 = y1 + item[i].h;
2170                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && (item[i].type != NM_TYPE_TEXT) ) {
2171                                         if (i+ScrollOffset != choice) {
2172                                                 if(Hack_DblClick_MenuMode) dblclick_flag = 0; 
2173                                         }
2174
2175                                         choice = i + ScrollOffset;
2176
2177                                         if ( item[choice].type == NM_TYPE_SLIDER ) {
2178                                                 char slider_text[NM_MAX_TEXT_LEN+1], *p, *s1;
2179                                                 int slider_width, height, aw, sleft_width, sright_width, smiddle_width;
2180                                                 
2181                                                 strcpy(slider_text, item[choice].saved_text);
2182                                                 p = strchr(slider_text, '\t');
2183                                                 if (p) {
2184                                                         *p = '\0';
2185                                                         s1 = p+1;
2186                                                 }
2187                                                 if (p) {
2188                                                         gr_get_string_size(s1, &slider_width, &height, &aw);
2189                                                         gr_get_string_size(SLIDER_LEFT, &sleft_width, &height, &aw);
2190                                                         gr_get_string_size(SLIDER_RIGHT, &sright_width, &height, &aw);
2191                                                         gr_get_string_size(SLIDER_MIDDLE, &smiddle_width, &height, &aw);
2192
2193                                                         x1 = grd_curcanv->cv_bitmap.bm_x + item[choice].x + item[choice].w - slider_width;
2194                                                         x2 = x1 + slider_width + sright_width;
2195                                                         if ( (mx > x1) && (mx < (x1 + sleft_width)) && (item[choice].value != item[choice].min_value) ) {
2196                                                                 item[choice].value = item[choice].min_value;
2197                                                                 item[choice].redraw = 2;
2198                                                         } else if ( (mx < x2) && (mx > (x2 - sright_width)) && (item[choice].value != item[choice].max_value) ) {
2199                                                                 item[choice].value = item[choice].max_value;
2200                                                                 item[choice].redraw = 2;
2201                                                         } else if ( (mx > (x1 + sleft_width)) && (mx < (x2 - sright_width)) ) {
2202                                                                 int num_values, value_width, new_value;
2203                                                                 
2204                                                                 num_values = item[choice].max_value - item[choice].min_value + 1;
2205                                                                 value_width = (slider_width - sleft_width - sright_width) / num_values;
2206                                                                 new_value = (mx - x1 - sleft_width) / value_width;
2207                                                                 if ( item[choice].value != new_value ) {
2208                                                                         item[choice].value = new_value;
2209                                                                         item[choice].redraw = 2;
2210                                                                 }
2211                                                         }
2212                                                         *p = '\t';
2213                                                 }
2214                                         }
2215                                         if (choice == old_choice)
2216                                                 break;
2217                                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
2218                                                 item[choice].value = -1;
2219                                         if ((old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))     {
2220                                                 item[old_choice].group=0;
2221                                                 strcpy(item[old_choice].text, item[old_choice].saved_text );
2222                                                 item[old_choice].value = -1;
2223                                         }
2224                                         if (old_choice>-1) 
2225                                                 item[old_choice].redraw = 1;
2226                                         item[choice].redraw=1;
2227                                         break;
2228                                 }
2229                         }
2230                 }
2231                 
2232                 if ( !done && !mouse_state && omouse_state && !all_text && (choice != -1) && (item[choice].type == NM_TYPE_MENU) ) {
2233                         mouse_get_pos(&mx, &my);
2234                         x1 = grd_curcanv->cv_bitmap.bm_x + item[choice].x;
2235                         x2 = x1 + item[choice].w;
2236                         y1 = grd_curcanv->cv_bitmap.bm_y + item[choice].y;
2237                         y2 = y1 + item[choice].h;
2238                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
2239                                 if (Hack_DblClick_MenuMode) {
2240                                         if (dblclick_flag) done = 1;
2241                                         else dblclick_flag = 1;
2242                                 }
2243                                 else done = 1;
2244                         }
2245                 }
2246                 
2247                 if ( !done && !mouse_state && omouse_state && (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==0))        {
2248                         item[choice].group = 1;
2249                         item[choice].redraw = 1;
2250                         if ( !strnicmp( item[choice].saved_text, TXT_EMPTY, strlen(TXT_EMPTY) ) )       {
2251                                 item[choice].text[0] = 0;
2252                                 item[choice].value = -1;
2253                         } else {        
2254                                 strip_end_whitespace(item[choice].text);
2255                         }
2256                 }
2257                 
2258                 if ( !done && !mouse_state && omouse_state && close_box ) {
2259                         mouse_get_pos(&mx, &my);
2260                         x1 = grd_curcanv->cv_bitmap.bm_x + CLOSE_X;
2261                         x2 = x1 + CLOSE_SIZE;
2262                         y1 = grd_curcanv->cv_bitmap.bm_y + CLOSE_Y;
2263                         y2 = y1 + CLOSE_SIZE;
2264                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2265                                 choice = -1;
2266                                 done = 1;
2267                         }
2268                 }
2269
2270 //       HACK! Don't redraw loadgame preview
2271                 if (RestoringMenu) item[0].redraw = 0;
2272 #endif          // ifdef MACINTOSH
2273
2274                 if ( choice > -1 )      {
2275                         int ascii;
2276
2277                         if ( ((item[choice].type==NM_TYPE_INPUT)||((item[choice].type==NM_TYPE_INPUT_MENU)&&(item[choice].group==1)) )&& (old_choice==choice) ) {
2278                                 if ( k==KEY_LEFT || k==KEY_BACKSP || k==KEY_PAD4 )      {
2279                                         if (item[choice].value==-1) item[choice].value = strlen(item[choice].text);
2280                                         if (item[choice].value > 0)
2281                                                 item[choice].value--;
2282                                         item[choice].text[item[choice].value] = 0;
2283                                         item[choice].redraw = 1;        
2284                                 } else {
2285                                         ascii = key_to_ascii(k);
2286                                         if ((ascii < 255 ) && (item[choice].value < item[choice].text_len ))
2287                                         {
2288                                                 int allowed;
2289
2290                                                 if (item[choice].value==-1) {
2291                                                         item[choice].value = 0;
2292                                                 }
2293
2294                                                 allowed = char_allowed(ascii);
2295
2296                                                 if (!allowed && ascii==' ' && char_allowed('_')) {
2297                                                         ascii = '_';
2298                                                         allowed=1;
2299                                                 }
2300
2301                                                 if (allowed) {
2302                                                         item[choice].text[item[choice].value++] = ascii;
2303                                                         item[choice].text[item[choice].value] = 0;
2304                                                         item[choice].redraw=1;  
2305                                                 }
2306                                         }
2307                                 }
2308                         } else if ((item[choice].type!=NM_TYPE_INPUT) && (item[choice].type!=NM_TYPE_INPUT_MENU) ) {
2309                                 ascii = key_to_ascii(k);
2310                                 if (ascii < 255 ) {
2311                                         int choice1 = choice;
2312                                         ascii = toupper(ascii);
2313                                         do {
2314                                                 int i,ch;
2315                                                 choice1++;
2316                                                 if (choice1 >= nitems ) choice1=0;
2317                                                 for (i=0;(ch=item[choice1].text[i])!=0 && ch==' ';i++);
2318                                                 if ( ( (item[choice1].type==NM_TYPE_MENU) ||
2319                                                                  (item[choice1].type==NM_TYPE_CHECK) ||
2320                                                                  (item[choice1].type==NM_TYPE_RADIO) ||
2321                                                                  (item[choice1].type==NM_TYPE_NUMBER) ||
2322                                                                  (item[choice1].type==NM_TYPE_SLIDER) )
2323                                                                 && (ascii==toupper(ch)) )       {
2324                                                         k = 0;
2325                                                         choice = choice1;
2326                                                         if (old_choice>-1)
2327                                                                 item[old_choice].redraw=1;
2328                                                         item[choice].redraw=1;
2329                                                 }
2330                                         } while (choice1 != choice );
2331                                 }       
2332                         }
2333
2334                         if ( (item[choice].type==NM_TYPE_NUMBER) || (item[choice].type==NM_TYPE_SLIDER))        {
2335                                 int ov=item[choice].value;
2336                                 switch( k ) {
2337                                 case KEY_PAD4:
2338                                 case KEY_LEFT:
2339                                 case KEY_MINUS:
2340                                 case KEY_MINUS+KEY_SHIFTED:
2341                                 case KEY_PADMINUS:
2342                                         item[choice].value -= 1;
2343                                         break;
2344                                 case KEY_RIGHT:
2345                                 case KEY_PAD6:
2346                                 case KEY_EQUAL:
2347                                 case KEY_EQUAL+KEY_SHIFTED:
2348                                 case KEY_PADPLUS:
2349                                         item[choice].value++;
2350                                         break;
2351                                 case KEY_PAGEUP:
2352                                 case KEY_PAD9:
2353                                 case KEY_SPACEBAR:
2354                                         item[choice].value += 10;
2355                                         break;
2356                                 case KEY_PAGEDOWN:
2357                                 case KEY_BACKSP:
2358                                 case KEY_PAD3:
2359                                         item[choice].value -= 10;
2360                                         break;
2361                                 }
2362                                 if (ov!=item[choice].value)
2363                                         item[choice].redraw=1;
2364                         }
2365         
2366                 }
2367
2368                 WINDOS (        dd_gr_set_current_canvas(bg.menu_canvas),
2369                                 gr_set_current_canvas(bg.menu_canvas));
2370
2371         // Redraw everything...
2372         for (i=ScrollOffset; i<MaxDisplayable+ScrollOffset; i++ )
2373         {
2374         if (item[i].redraw) // warning! ugly hack below                  
2375                 {
2376                 item[i].y-=((string_height+1)*ScrollOffset);
2377                 MAC(hide_cursor());
2378                                 WIN(HideCursorW());
2379                 draw_item( &bg, &item[i], (i==choice && !all_text),TinyMode );
2380                                 item[i].redraw=0;
2381                                 MAC(if (!joydefs_calibrating) show_cursor());
2382                                 WIN(if (!MenuReordering) ShowCursorW());
2383             item[i].y+=((string_height+1)*ScrollOffset);
2384                 }   
2385          if (i==choice && (item[i].type==NM_TYPE_INPUT || (item[i].type==NM_TYPE_INPUT_MENU && item[i].group)))
2386                                 update_cursor( &item[i]);
2387                 }
2388         gr_update();
2389
2390       if (IsScrollBox)
2391         {
2392         //grd_curcanv->cv_font = NORMAL_FONT;
2393         
2394                 if (LastScrollCheck!=ScrollOffset)
2395          {
2396                 LastScrollCheck=ScrollOffset;
2397                 grd_curcanv->cv_font = SELECTED_FONT;
2398                                 
2399                 sy=item[ScrollOffset].y-((string_height+1)*ScrollOffset);
2400                 sx=item[ScrollOffset].x-(MenuHires?24:12);
2401                                 
2402           
2403                 if (ScrollOffset!=0)
2404                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, UP_ARROW_MARKER );
2405                 else
2406                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
2407
2408                 sy=item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
2409                 sx=item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
2410           
2411                 if (ScrollOffset+MaxDisplayable<nitems)
2412                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, DOWN_ARROW_MARKER );
2413                 else
2414                 nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
2415
2416         }
2417
2418         }   
2419
2420                 if ( !dont_restore && gr_palette_faded_out )    {
2421                         gr_palette_fade_in( gr_palette, 32, 0 );
2422                 }
2423         }
2424         
2425         MAC(hide_cursor());
2426         WIN(HideCursorW());
2427         
2428         // Restore everything...
2429
2430         WINDOS (        dd_gr_set_current_canvas(bg.menu_canvas),
2431                         gr_set_current_canvas(bg.menu_canvas));
2432
2433         if ( filename == NULL ) {
2434                 // Save the background under the menu...
2435                 WIN (DDGRLOCK(dd_grd_curcanv));
2436                         gr_bitmap(0, 0, bg.saved);      
2437                 WIN (DDGRUNLOCK(dd_grd_curcanv));
2438                 gr_free_bitmap(bg.saved);
2439                 d_free( bg.background );
2440         } else {
2441                 if (!dont_restore)      //info passed back from subfunction
2442                 {
2443                         WIN (DDGRLOCK(dd_grd_curcanv));
2444                         gr_bitmap(0, 0, bg.background);
2445                         WIN (DDGRUNLOCK(dd_grd_curcanv));       
2446                 }
2447                 gr_free_bitmap(bg.background);
2448         }
2449
2450         WINDOS (        dd_gr_free_sub_canvas(bg.menu_canvas),
2451                         gr_free_sub_canvas( bg.menu_canvas ) );
2452
2453         WINDOS (dd_gr_set_current_canvas(NULL), gr_set_current_canvas( NULL ));                 
2454         grd_curcanv->cv_font    = save_font;
2455         WINDOS (dd_gr_set_current_canvas(NULL), gr_set_current_canvas( save_canvas ));                  
2456         keyd_repeat = old_keyd_repeat;
2457
2458         game_flush_inputs();
2459
2460         if (time_stopped) 
2461      {
2462                 start_time();
2463                 #ifdef TACTILE
2464                         if (TactileStick)
2465                                 EnableForces();
2466                 #endif
2467           }
2468
2469         if ( sound_stopped )
2470                 digi_resume_digi_sounds();
2471
2472         WIN(mouse_set_mode(1));                         //re-enable centering mode
2473
2474         return choice;
2475         
2476 }
2477
2478
2479 int nm_messagebox1( char *title, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int nchoices, ... )
2480 {
2481         int i;
2482         char * format;
2483         va_list args;
2484         char *s;
2485         char nm_text[MESSAGEBOX_TEXT_SIZE];
2486         newmenu_item nm_message_items[5];
2487
2488         va_start(args, nchoices );
2489
2490         Assert( nchoices <= 5 );
2491
2492         for (i=0; i<nchoices; i++ )     {
2493                 s = va_arg( args, char * );
2494                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
2495         }
2496         format = va_arg( args, char * );
2497         strcpy( nm_text, "" );
2498         vsprintf(nm_text,format,args);
2499         va_end(args);
2500
2501         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE);
2502
2503         return newmenu_do( title, nm_text, nchoices, nm_message_items, subfunction );
2504 }
2505
2506 int nm_messagebox( char *title, int nchoices, ... )
2507 {
2508         int i;
2509         char * format;
2510         va_list args;
2511         char *s;
2512         char nm_text[MESSAGEBOX_TEXT_SIZE];
2513         newmenu_item nm_message_items[5];
2514
2515         va_start(args, nchoices );
2516
2517         Assert( nchoices <= 5 );
2518
2519         for (i=0; i<nchoices; i++ )     {
2520                 s = va_arg( args, char * );
2521                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
2522         }
2523         format = va_arg( args, char * );
2524         strcpy( nm_text, "" );
2525         vsprintf(nm_text,format,args);
2526         va_end(args);
2527
2528         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
2529
2530         return newmenu_do( title, nm_text, nchoices, nm_message_items, NULL );
2531 }
2532
2533
2534
2535
2536 void newmenu_file_sort( int n, char *list )
2537 {
2538         int i, j, incr;
2539         char t[14];
2540
2541         incr = n / 2;
2542         while( incr > 0 )               {
2543                 for (i=incr; i<n; i++ )         {
2544                         j = i - incr;
2545                         while (j>=0 )                   {
2546                                 if (strncmp(&list[j*14], &list[(j+incr)*14], 12) > 0)                           {
2547                                         memcpy( t, &list[j*14], FILENAME_LEN );
2548                                         memcpy( &list[j*14], &list[(j+incr)*14], FILENAME_LEN );
2549                                         memcpy( &list[(j+incr)*14], t, FILENAME_LEN );
2550                                         j -= incr;
2551                                 }
2552                                 else
2553                                         break;
2554                         }
2555                 }
2556                 incr = incr / 2;
2557         }
2558 }
2559
2560 void delete_player_saved_games(char * name)
2561 {
2562         int i;
2563         char filename[16];
2564         
2565         for (i=0;i<10; i++)     {
2566 #ifndef MACINTOSH
2567                 sprintf( filename, "%s.sg%d", name, i );
2568 #else
2569                 sprintf( filename, ":Players:%s.sg%d", name, i );
2570 #endif
2571                 unlink( filename );
2572         }
2573 }
2574
2575 #define MAX_FILES 300
2576
2577 int MakeNewPlayerFile(int allow_abort);
2578
2579 int newmenu_get_filename( char * title, char * filespec, char * filename, int allow_abort_flag )
2580 {
2581         int i;
2582         FILEFINDSTRUCT find;
2583         int NumFiles=0, key,done, citem, ocitem;
2584         char * filenames = NULL;
2585         int NumFiles_displayed = 8;
2586         int first_item = -1, ofirst_item;
2587         int old_keyd_repeat = keyd_repeat;
2588         int player_mode=0;
2589         int demo_mode=0;
2590         int demos_deleted=0;
2591         int initialized = 0;
2592         int exit_value = 0;
2593         int w_x, w_y, w_w, w_h, title_height;
2594         int box_x, box_y, box_w, box_h;
2595         bkg bg;         // background under listbox
2596 #if defined(MACINTOSH) || defined(WINDOWS)
2597         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;
2598         int mouse2_state, omouse2_state;
2599         int dblclick_flag=0;
2600    int simukey=0;
2601         int show_up_arrow=0,show_down_arrow=0;
2602 #endif
2603 WIN(int win_redraw=0);
2604
2605         w_x=w_y=w_w=w_h=title_height=0;
2606         box_x=box_y=box_w=box_h=0;
2607
2608         filenames = d_malloc( MAX_FILES * 14 );
2609         if (filenames==NULL) return 0;
2610
2611         citem = 0;
2612         keyd_repeat = 1;
2613
2614         WIN(mouse_set_mode(0));                         //disable centering mode
2615
2616         if (strstr( filespec, "*.plr" ))
2617                 player_mode = 1;
2618         else if (strstr( filespec, "*.dem" ))
2619                 demo_mode = 1;
2620
2621 ReadFileNames:
2622         done = 0;
2623         NumFiles=0;
2624         
2625 #if !defined(APPLE_DEMO)                // no new pilots for special apple oem version
2626         if (player_mode)        {
2627                 strncpy( &filenames[NumFiles*14], TXT_CREATE_NEW, FILENAME_LEN );
2628                 NumFiles++;
2629         }
2630 #endif
2631
2632         if( !FileFindFirst( filespec, &find ) ) {
2633                 do      {
2634                         if (NumFiles<MAX_FILES) {
2635                                 strncpy( &filenames[NumFiles*14], find.name, FILENAME_LEN );
2636                                 if ( player_mode )      {
2637                                         char * p;
2638                                         p = strchr(&filenames[NumFiles*14],'.');
2639                                         if (p) *p = '\0';
2640                                 }
2641                                 NumFiles++;
2642                         } else {
2643                                 break;
2644                         }
2645                 } while( !FileFindNext( &find ) );
2646                 FileFindClose();
2647         }
2648
2649         if ( (NumFiles < 1) && demos_deleted )  {
2650                 exit_value = 0;
2651                 goto ExitFileMenu;
2652         }
2653         if ( (NumFiles < 1) && demo_mode ) {
2654                 nm_messagebox( NULL, 1, TXT_OK, "%s %s\n%s", TXT_NO_DEMO_FILES, TXT_USE_F5, TXT_TO_CREATE_ONE);
2655                 exit_value = 0;
2656                 goto ExitFileMenu;
2657         }
2658
2659         #ifndef APPLE_DEMO
2660         if ( (NumFiles < 2) && player_mode ) {
2661                 citem = 0;
2662                 goto ExitFileMenuEarly;
2663         }
2664         #endif
2665
2666
2667         if ( NumFiles<1 )       {
2668                 #ifndef APPLE_DEMO
2669                         nm_messagebox( NULL, 1, "Ok", "%s\n '%s' %s", TXT_NO_FILES_MATCHING, filespec, TXT_WERE_FOUND);
2670                 #endif
2671                 exit_value = 0;
2672                 goto ExitFileMenu;
2673         }
2674
2675         if (!initialized) {     
2676 //              set_screen_mode(SCREEN_MENU);
2677                 set_popup_screen();
2678
2679         #ifdef WINDOWS
2680 RePaintNewmenuFile:
2681
2682                 dd_gr_set_current_canvas(NULL);
2683         #else
2684                 gr_set_current_canvas(NULL);
2685         #endif
2686
2687                 WIN(DDGRLOCK(dd_grd_curcanv))                                   //mwa put these here -- are these needed Samir???
2688                 {
2689                         grd_curcanv->cv_font = SUBTITLE_FONT;
2690                 }
2691                 WIN(DDGRUNLOCK(dd_grd_curcanv));
2692
2693                 w_w = 0;
2694                 w_h = 0;
2695
2696                 for (i=0; i<NumFiles; i++ ) {
2697                         int w, h, aw;
2698                         gr_get_string_size( &filenames[i*14], &w, &h, &aw );            
2699                         if ( w > w_w )
2700                                 w_w = w;
2701                 }
2702                 if ( title ) {
2703                         int w, h, aw;
2704                         gr_get_string_size( title, &w, &h, &aw );               
2705                         if ( w > w_w )
2706                                 w_w = w;
2707                         title_height = h + (grd_curfont->ft_h*2);               // add a little space at the bottom of the title
2708                 }
2709
2710                 box_w = w_w;
2711                 box_h = ((grd_curfont->ft_h + 2) * NumFiles_displayed);
2712
2713                 w_w += (grd_curfont->ft_w * 4);
2714                 w_h = title_height + box_h + (grd_curfont->ft_h * 2);           // more space at bottom
2715
2716                 if ( w_w > grd_curcanv->cv_w ) w_w = grd_curcanv->cv_w;
2717                 if ( w_h > grd_curcanv->cv_h ) w_h = grd_curcanv->cv_h;
2718         
2719                 w_x = (grd_curcanv->cv_w-w_w)/2;
2720                 w_y = (grd_curcanv->cv_h-w_h)/2;
2721         
2722                 if ( w_x < 0 ) w_x = 0;
2723                 if ( w_y < 0 ) w_y = 0;
2724
2725                 box_x = w_x + (grd_curfont->ft_w*2);                    // must be in sync with w_w!!!
2726                 box_y = w_y + title_height;
2727
2728 // save the screen behind the menu.
2729
2730                 bg.saved = NULL;
2731
2732         #if !defined(WINDOWS)
2733                 if ( (VR_offscreen_buffer->cv_w >= w_w) && (VR_offscreen_buffer->cv_h >= w_h) ) 
2734                         bg.background = &VR_offscreen_buffer->cv_bitmap;
2735                 else
2736         #endif
2737 #if defined(POLY_ACC)
2738                         bg.background = gr_create_bitmap2( w_w, w_h, grd_curcanv->cv_bitmap.bm_type, NULL );
2739 #else
2740                         bg.background = gr_create_bitmap( w_w, w_h );
2741 #endif
2742
2743                 Assert( bg.background != NULL );
2744
2745
2746                 WIN(DDGRLOCK(dd_grd_curcanv));
2747                 gr_bm_bitblt(w_w, w_h, 0, 0, w_x, w_y, &grd_curcanv->cv_bitmap, bg.background );
2748                 WIN(DDGRUNLOCK(dd_grd_curcanv));
2749
2750 #if 0
2751                 WINDOS(
2752                         dd_gr_blt_notrans(dd_grd_curcanv, 0, 0, 
2753                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh, 
2754                                 dd_VR_offscreen_buffer, 0, 0, 
2755                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
2756                         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) )
2757                 );
2758 #endif
2759
2760                 nm_draw_background( w_x,w_y,w_x+w_w-1,w_y+w_h-1 );
2761                 
2762                 WIN(DDGRLOCK(dd_grd_curcanv))
2763                 {       
2764                         gr_string( 0x8000, w_y+10, title );
2765                 }
2766                 WIN(DDGRUNLOCK(dd_grd_curcanv));
2767
2768                 WIN(DDGRRESTORE);
2769          
2770                 initialized = 1;
2771         }
2772
2773         if ( !player_mode )     {
2774                 newmenu_file_sort( NumFiles, filenames );
2775         } else {
2776                 #if defined(MACINTOSH) && defined(APPLE_DEMO)
2777                 newmenu_file_sort( NumFiles, filenames );
2778                 #else
2779                 newmenu_file_sort( NumFiles-1, &filenames[14] );                // Don't sort first one!
2780                 #endif
2781                 for ( i=0; i<NumFiles; i++ )    {
2782                         if (!stricmp(Players[Player_num].callsign, &filenames[i*14]) )  {
2783                         #if defined(WINDOWS) || defined(MACINTOSH) 
2784                                 dblclick_flag = 1;
2785                         #endif
2786                                 citem = i;
2787                         }
2788                 }
2789         }
2790         
2791 #if defined(MACINTOSH) || defined(WINDOWS)
2792         mouse_state = omouse_state = 0;
2793         mouse2_state = omouse2_state = 0;
2794         draw_close_box(w_x,w_y);
2795    #ifdef MACINTOSH
2796                 show_cursor();
2797         #else
2798                 ShowCursorW();
2799         #endif
2800 #endif
2801
2802         while(!done)    {
2803         #ifdef WINDOWS
2804                 MSG msg;
2805
2806                 DoMessageStuff(&msg);
2807
2808                 if (_RedrawScreen) {
2809                         _RedrawScreen = FALSE;
2810
2811                         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
2812                                 gr_free_bitmap(bg.background);
2813         
2814                         win_redraw = 1;         
2815                         goto RePaintNewmenuFile;
2816                 }
2817
2818                 DDGRRESTORE
2819         #endif
2820
2821                 ocitem = citem;
2822                 ofirst_item = first_item;
2823                 gr_update();
2824
2825 #if defined(MACINTOSH) || defined(WINDOWS)
2826                 omouse_state = mouse_state;
2827                 omouse2_state = mouse2_state;
2828                 mouse_state = mouse_button_state(0);
2829                 mouse2_state = mouse_button_state(1);
2830 #endif
2831
2832                 //see if redbook song needs to be restarted
2833                 songs_check_redbook_repeat();
2834
2835                 #ifdef WINDOWS
2836                 if (!mouse2_state && omouse2_state)
2837                         key = KEY_CTRLED+KEY_D;         //fake ctrl-d
2838                 else
2839                 #endif
2840                         //NOTE LINK TO ABOVE ELSE
2841                         key = key_inkey();
2842
2843         #ifdef WINDOWS
2844                 if (simukey==-1)
2845                         key=KEY_UP;
2846                 else if (simukey==1)
2847                    key=KEY_DOWN;
2848                 simukey=0;
2849         #endif
2850                         
2851                 switch(key)     {
2852                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
2853                 case KEY_PRINT_SCREEN:
2854                         MAC(hide_cursor());
2855                         save_screen_shot(0);
2856                         PA_DFX (pa_set_frontbuffer_current());
2857                         PA_DFX (pa_set_front_to_read());
2858                         
2859                         MAC(show_cursor());
2860                         MAC(key_flush());
2861                         break;
2862
2863                 case KEY_CTRLED+KEY_D:
2864                         #if defined(MACINTOSH) && defined(APPLE_DEMO)
2865                         break;
2866                         #endif
2867
2868                         if ( ((player_mode)&&(citem>0)) || ((demo_mode)&&(citem>=0)) )  {
2869                                 int x = 1;
2870                                 MAC(hide_cursor());
2871                                 #ifdef WINDOWS
2872                                 mouse_set_mode(1);                              //re-enable centering mode
2873                                 HideCursorW();
2874                                 #endif
2875                                 if (player_mode)
2876                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2877                                 else if (demo_mode)
2878                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2879                                 MAC(show_cursor());
2880                                 #ifdef WINDOWS
2881                                 mouse_set_mode(0);                              //disenable centering mode
2882                                 ShowCursorW();
2883                                 #endif
2884                                 if (x==0)       {
2885                                         char * p;
2886                                         int ret;
2887                                         char name[_MAX_PATH],dir[_MAX_DIR];
2888
2889                                         p = &filenames[(citem*14)+strlen(&filenames[citem*14])];
2890                                         if (player_mode)
2891                                                 *p = '.';
2892
2893                                         _splitpath(filespec,name,dir,NULL,NULL);
2894                                         strcat(name,dir);
2895                                         strcat(name,&filenames[citem*14]);
2896                                         
2897                                         #ifdef MACINTOSH
2898                                         {
2899                                                 int i;
2900                                                 char *p;
2901                                                 
2902                                                 if ( !strncmp(name, ".\\", 2) )
2903                                                         for (i = 0; i < strlen(name); i++)              // don't subtract 1 from strlen to get the EOS marker
2904                                                                 name[i] = name[i+1];
2905                                                 while ( (p = strchr(name, '\\')) )
2906                                                         *p = ':';
2907                                         }
2908                                         #endif
2909                                 
2910                                         ret = unlink( name );
2911                                         if (player_mode)
2912                                                 *p = 0;
2913
2914                                         if ((!ret) && player_mode)      {
2915                                                 delete_player_saved_games( &filenames[citem*14] );
2916                                         }
2917
2918                                         if (ret) {
2919                                                 if (player_mode)
2920                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2921                                                 else if (demo_mode)
2922                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2923                                         } else if (demo_mode)
2924                                                 demos_deleted = 1;
2925                                         first_item = -1;
2926                                         goto ReadFileNames;
2927                                 }
2928                         }
2929                         break;
2930                 case KEY_HOME:
2931                 case KEY_PAD7:
2932                         citem = 0;
2933                         break;
2934                 case KEY_END:
2935                 case KEY_PAD1:
2936                         citem = NumFiles-1;
2937                         break;
2938                 case KEY_UP:
2939                 case KEY_PAD8:
2940                         citem--;                        
2941                         break;
2942                 case KEY_DOWN:
2943                 case KEY_PAD2:
2944                         citem++;                        
2945                         break;
2946                 case KEY_PAGEDOWN:
2947                 case KEY_PAD3:
2948                         citem += NumFiles_displayed;
2949                         break;
2950                 case KEY_PAGEUP:
2951                 case KEY_PAD9:
2952                         citem -= NumFiles_displayed;
2953                         break;
2954                 case KEY_ESC:
2955                         if (allow_abort_flag) {
2956                                 citem = -1;
2957                                 done = 1;
2958                         }
2959                         break;
2960                 case KEY_ENTER:
2961                 case KEY_PADENTER:
2962                         done = 1;
2963                         break;
2964                         
2965                 #ifdef MACINTOSH
2966                 case KEY_COMMAND+KEY_Q: {
2967                         extern void macintosh_quit();
2968                         
2969                         if ( !(Game_mode & GM_MULTI) )
2970                                 macintosh_quit();
2971                         show_cursor();
2972                         key_flush();
2973                         break;
2974                 }
2975                 #endif
2976                 
2977                 default:        
2978                         {
2979
2980                                 int ascii = key_to_ascii(key);
2981                                 if ( ascii < 255 )      {
2982                                         int cc,cc1;
2983                                         cc=cc1=citem+1;
2984                                         if (cc1 < 0 )  cc1 = 0;
2985                                         if (cc1 >= NumFiles )  cc1 = 0;
2986                                         while(1) {
2987                                                 if ( cc < 0 ) cc = 0;
2988                                                 if ( cc >= NumFiles ) cc = 0;
2989                                                 if ( citem == cc ) break;
2990         
2991                                                 if ( toupper(filenames[cc*14]) == toupper(ascii) )      {
2992                                                         citem = cc;
2993                                                         break;
2994                                                 }
2995                                                 cc++;
2996                                         }
2997                                 }
2998                         }
2999                 }
3000                 if ( done ) break;
3001
3002
3003                 if (citem<0)
3004                         citem=0;
3005
3006                 if (citem>=NumFiles)
3007                         citem = NumFiles-1;
3008
3009                 if (citem< first_item)
3010                         first_item = citem;
3011
3012                 if (citem>=( first_item+NumFiles_displayed))
3013                 {
3014                         first_item = citem-NumFiles_displayed+1;
3015                 }
3016
3017 #ifdef WINDOWS
3018                 if (NumFiles>first_item+NumFiles_displayed)
3019                         show_down_arrow=1;
3020                 else 
3021                         show_down_arrow=0;
3022                 if (first_item>0)
3023                         show_up_arrow=1;
3024                 else    
3025                         show_up_arrow=0;
3026 #endif
3027                         
3028
3029                 if (NumFiles <= NumFiles_displayed )
3030                          first_item = 0;
3031
3032                 if (first_item>NumFiles-NumFiles_displayed)
3033                 {
3034                         first_item = NumFiles-NumFiles_displayed;
3035                 }
3036
3037                 if (first_item < 0 ) first_item = 0;
3038
3039 #if defined(MACINTOSH) || defined(WINDOWS)
3040                 WIN(Sleep(100));
3041                 if (mouse_state || mouse2_state) {
3042                         int w, h, aw;
3043
3044                         mouse_get_pos(&mx, &my);
3045                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
3046                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3047                                 x1 = box_x;
3048                                 x2 = box_x + box_w - 1;
3049                                 y1 = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
3050                                 y2 = y1+h+1;
3051                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3052                                         if (i == citem && !mouse2_state) {
3053                                                 break;
3054                                         }
3055                                         citem = i;
3056                                         dblclick_flag = 0;
3057                                         break;
3058                                 }
3059                         }
3060                 }
3061                 
3062                 if (!mouse_state && omouse_state) {
3063                         int w, h, aw;
3064
3065                         gr_get_string_size(&filenames[citem*14], &w, &h, &aw  );
3066                         mouse_get_pos(&mx, &my);
3067                         x1 = box_x;
3068                         x2 = box_x + box_w - 1;
3069                         y1 = (citem-first_item)*(grd_curfont->ft_h + 2) + box_y;
3070                         y2 = y1+h+1;
3071                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3072                                 if (dblclick_flag) done = 1;
3073                                 else dblclick_flag = 1;
3074                         }
3075                 }
3076
3077                 if ( !mouse_state && omouse_state ) {
3078                         mouse_get_pos(&mx, &my);
3079                         x1 = w_x + CLOSE_X + 2;
3080                         x2 = x1 + CLOSE_SIZE - 2;
3081                         y1 = w_y + CLOSE_Y + 2;
3082                         y2 = y1 + CLOSE_SIZE - 2;
3083                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3084                                 citem = -1;
3085                                 done = 1;
3086                         }
3087                    #ifdef WINDOWS
3088                         x1 = box_x-LHX(10);
3089                         x2 = x1 + LHX(10);
3090                         y1 = box_y;
3091                         y2 = box_y+LHY(7);
3092                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow ) 
3093                                 simukey = -1;
3094                         y1 = box_y+box_h-LHY(7);
3095                         y2 = box_y+box_h;
3096                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow) 
3097                                 simukey = 1;
3098                    #endif
3099                 }
3100
3101 #endif
3102   
3103         WIN(DDGRLOCK(dd_grd_curcanv));
3104                 gr_setcolor( BM_XRGB(2,2,2));
3105                 //gr_rect( box_x - 1, box_y-2, box_x + box_w, box_y-2 );
3106                 gr_setcolor( BM_XRGB( 0,0,0)  );
3107
3108         #ifdef WINDOWS
3109                 if (ofirst_item != first_item || win_redraw)    {
3110                         win_redraw = 0;
3111         #else
3112                 if (ofirst_item != first_item)  {
3113         #endif
3114                         MAC(hide_cursor());
3115                         WIN(HideCursorW());
3116                         gr_setcolor( BM_XRGB( 0,0,0)  );
3117                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
3118                                 int w, h, aw, y;
3119                                 y = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
3120                         
3121                                 if ( i >= NumFiles )    {
3122
3123                                         gr_setcolor( BM_XRGB(5,5,5));
3124                                         gr_rect( box_x + box_w, y-1, box_x + box_w, y + grd_curfont->ft_h + 1);
3125                                         //gr_rect( box_x, y + grd_curfont->ft_h + 2, box_x + box_w, y + grd_curfont->ft_h + 2);
3126                                         
3127                                         gr_setcolor( BM_XRGB(2,2,2));
3128                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + grd_curfont->ft_h + 2 );
3129                                         
3130                                         gr_setcolor( BM_XRGB(0,0,0));
3131                                         gr_rect( box_x, y - 1, box_x + box_w - 1, y + grd_curfont->ft_h + 1);
3132                                         
3133                                 } else {
3134                                         if ( i == citem )       
3135                                                 grd_curcanv->cv_font = SELECTED_FONT;
3136                                         else    
3137                                                 grd_curcanv->cv_font = NORMAL_FONT;
3138                                         gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3139
3140                                         gr_setcolor( BM_XRGB(5,5,5));
3141                                   //    gr_rect( box_x, y + h + 2, box_x + box_w, y + h + 2);
3142                                         gr_rect( box_x + box_w, y - 1, box_x + box_w, y + h + 1);
3143                                         
3144                                         gr_setcolor( BM_XRGB(2,2,2));
3145                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + h + 1);
3146                                         gr_setcolor( BM_XRGB(0,0,0));
3147                                                         
3148                                         gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
3149                                         gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
3150                                 }
3151                         }        
3152                         WIN(ShowCursorW());
3153                         MAC(show_cursor());
3154                 } else if ( citem != ocitem )   {
3155                         int w, h, aw, y;
3156
3157                         MAC(hide_cursor());
3158                         WIN(HideCursorW());
3159                         i = ocitem;
3160                         if ( (i>=0) && (i<NumFiles) )   {
3161                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
3162                                 if ( i == citem )       
3163                                         grd_curcanv->cv_font = SELECTED_FONT;
3164                                 else    
3165                                         grd_curcanv->cv_font = NORMAL_FONT;
3166                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3167                                 gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
3168                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
3169                         }
3170                         i = citem;
3171                         if ( (i>=0) && (i<NumFiles) )   {
3172                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
3173                                 if ( i == citem )       
3174                                         grd_curcanv->cv_font = SELECTED_FONT;
3175                                 else    
3176                                         grd_curcanv->cv_font = NORMAL_FONT;
3177                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3178                                 gr_rect( box_x, y-1, box_x + box_x - 1, y + h + 1 );
3179                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
3180                         }
3181                         WIN(ShowCursorW());
3182                         MAC(show_cursor());
3183                 }
3184
3185         #ifdef WINDOWS   
3186                         grd_curcanv->cv_font = NORMAL_FONT;
3187                         if (show_up_arrow)
3188                                 gr_string( box_x-LHX(10), box_y ,UP_ARROW_MARKER );
3189                         else
3190                         {
3191                                 No_darkening=1;
3192                                 nm_draw_background (box_x-LHX(10),box_y,box_x-2,box_y+LHY(7));
3193                                 No_darkening=0;
3194                         }
3195
3196                         if (show_down_arrow)
3197                         gr_string( box_x-LHX(10), box_y+box_h-LHY(7) ,DOWN_ARROW_MARKER );
3198                         else
3199                         {
3200                                 No_darkening=1;
3201                                 nm_draw_background (box_x-LHX(10),box_y+box_h-LHY(7),box_x-2,box_y+box_h);
3202                                 No_darkening=0;
3203                         }
3204
3205         #endif
3206
3207
3208
3209         WIN(DDGRUNLOCK(dd_grd_curcanv));
3210         }
3211
3212 ExitFileMenuEarly:
3213         MAC(hide_cursor());
3214         if ( citem > -1 )       {
3215                 strncpy( filename, (&filenames[citem*14])+((player_mode && filenames[citem*14]=='$')?1:0), FILENAME_LEN );
3216                 exit_value = 1;
3217         } else {
3218                 exit_value = 0;
3219         }                                                                                        
3220
3221 ExitFileMenu:
3222         keyd_repeat = old_keyd_repeat;
3223
3224         if ( initialized )      {
3225                         if (Newdemo_state != ND_STATE_PLAYBACK) //horrible hack to prevent restore when screen has been cleared
3226                         {
3227                         WIN (DDGRLOCK(dd_grd_curcanv));
3228                                 gr_bm_bitblt(w_w, w_h, w_x, w_y, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
3229                         WIN (DDGRUNLOCK(dd_grd_curcanv));       
3230                         }
3231                         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
3232                                 gr_free_bitmap(bg.background);
3233 #if 0
3234                 WINDOS(
3235                         dd_gr_blt_notrans(dd_VR_offscreen_buffer,
3236                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh,
3237                                 dd_grd_curcanv,
3238                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
3239                         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) )
3240                 );
3241 #endif
3242
3243                 WIN(DDGRRESTORE);
3244         }
3245
3246         if ( filenames )
3247                 d_free(filenames);
3248
3249         WIN(mouse_set_mode(1));                         //re-enable centering mode
3250         WIN(HideCursorW());
3251
3252         return exit_value;
3253
3254 }
3255
3256
3257 // Example listbox callback function...
3258 // int lb_callback( int * citem, int *nitems, char * items[], int *keypress )
3259 // {
3260 //      int i;
3261 // 
3262 //      if ( *keypress = KEY_CTRLED+KEY_D )     {
3263 //              if ( *nitems > 1 )      {
3264 //                      unlink( items[*citem] );                // Delete the file
3265 //                      for (i=*citem; i<*nitems-1; i++ )       {
3266 //                              items[i] = items[i+1];
3267 //                      }
3268 //                      *nitems = *nitems - 1;
3269 //                      d_free( items[*nitems] );
3270 //                      items[*nitems] = NULL;
3271 //                      return 1;       // redraw;
3272 //              }
3273 //                      *keypress = 0;
3274 //      }                       
3275 //      return 0;
3276 // }
3277
3278 #define LB_ITEMS_ON_SCREEN 8
3279
3280 int newmenu_listbox( char * title, int nitems, char * items[], int allow_abort_flag, int (*listbox_callback)( int * citem, int *nitems, char * items[], int *keypress ) )
3281 {
3282         return newmenu_listbox1( title, nitems, items, allow_abort_flag, 0, listbox_callback );
3283 }
3284
3285 int newmenu_listbox1( char * title, int nitems, char * items[], int allow_abort_flag, int default_item, int (*listbox_callback)( int * citem, int *nitems, char * items[], int *keypress ) )
3286 {
3287         int i;
3288         int done, ocitem,citem, ofirst_item, first_item, key, redraw;
3289         int old_keyd_repeat = keyd_repeat;
3290         int width, height, wx, wy, title_height, border_size;
3291         int total_width,total_height;
3292         bkg bg;
3293 #if defined(MACINTOSH) || defined(WINDOWS)
3294         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;  //, dblclick_flag;
3295         int close_x,close_y;
3296    int simukey=0,show_up_arrow=0,show_down_arrow=0;
3297 #endif
3298 WIN(int win_redraw=0);
3299
3300         keyd_repeat = 1;
3301
3302    PA_DFX (pa_set_frontbuffer_current());
3303         PA_DFX (pa_set_front_to_read());
3304         WIN(mouse_set_mode(0));                         //disable centering mode
3305
3306 //      set_screen_mode(SCREEN_MENU);
3307         set_popup_screen();
3308
3309 #ifdef WINDOWS
3310 RePaintNewmenuListbox:
3311  
3312         dd_gr_set_current_canvas(NULL);
3313 #else
3314         gr_set_current_canvas(NULL);
3315 #endif
3316
3317         grd_curcanv->cv_font = SUBTITLE_FONT;
3318
3319         width = 0;
3320         for (i=0; i<nitems; i++ )       {
3321                 int w, h, aw;
3322                 gr_get_string_size( items[i], &w, &h, &aw );            
3323                 if ( w > width )
3324                         width = w;
3325         }
3326         height = (grd_curfont->ft_h + 2) * LB_ITEMS_ON_SCREEN;
3327
3328         {
3329                 int w, h, aw;
3330                 gr_get_string_size( title, &w, &h, &aw );               
3331                 if ( w > width )
3332                         width = w;
3333                 title_height = h + 5;
3334         }
3335
3336         border_size = grd_curfont->ft_w;
3337    WIN (border_size=grd_curfont->ft_w*2);
3338                 
3339         width += (grd_curfont->ft_w);
3340         if ( width > grd_curcanv->cv_w - (grd_curfont->ft_w * 3) )
3341                 width = grd_curcanv->cv_w - (grd_curfont->ft_w * 3);
3342
3343         wx = (grd_curcanv->cv_bitmap.bm_w-width)/2;
3344         wy = (grd_curcanv->cv_bitmap.bm_h-(height+title_height))/2 + title_height;
3345         if ( wy < title_height )
3346                 wy = title_height;
3347
3348         total_width = width+2*border_size;
3349         total_height = height+2*border_size+title_height;
3350
3351         bg.saved = NULL;
3352
3353 #if !defined(WINDOWS)
3354         if ( (VR_offscreen_buffer->cv_w >= total_width) && (VR_offscreen_buffer->cv_h >= total_height) )
3355                 bg.background = &VR_offscreen_buffer->cv_bitmap;
3356         else
3357 #endif
3358                 //bg.background = gr_create_bitmap( width, (height + title_height) );
3359 #if defined(POLY_ACC)
3360                 bg.background = gr_create_bitmap2(total_width, total_height, grd_curcanv->cv_bitmap.bm_type, NULL);
3361 #else
3362                 bg.background = gr_create_bitmap(total_width,total_height);
3363 #endif
3364         Assert( bg.background != NULL );
3365                 
3366         WIN (DDGRLOCK(dd_grd_curcanv));
3367                 //gr_bm_bitblt(wx+width+border_size, wy+height+border_size, 0, 0, wx-border_size, wy-title_height-border_size, &grd_curcanv->cv_bitmap, bg.background );
3368                 gr_bm_bitblt(total_width,total_height, 0, 0, wx-border_size, wy-title_height-border_size, &grd_curcanv->cv_bitmap, bg.background );
3369         WIN (DDGRUNLOCK(dd_grd_curcanv));
3370
3371 #if 0
3372         WINDOS(
3373                 dd_gr_blt_notrans(dd_grd_curcanv, 0, 0, 
3374                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh, 
3375                                 dd_VR_offscreen_buffer, 0, 0, 
3376                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
3377                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) )
3378         );
3379 #endif
3380
3381         nm_draw_background( wx-border_size,wy-title_height-border_size,wx+width+border_size-1,wy+height+border_size-1 );
3382
3383         WIN(DDGRLOCK(dd_grd_curcanv));
3384                 gr_string( 0x8000, wy - title_height, title );
3385         WIN(DDGRUNLOCK(dd_grd_curcanv));        
3386
3387         WIN(DDGRRESTORE);
3388
3389         done = 0;
3390         citem = default_item;
3391         if ( citem < 0 ) citem = 0;
3392         if ( citem >= nitems ) citem = 0;
3393
3394         first_item = -1;
3395
3396 #if defined(MACINTOSH) || defined(WINDOWS)
3397         mouse_state = omouse_state = 0; //dblclick_flag = 0;
3398         close_x = wx-border_size;
3399         close_y = wy-title_height-border_size;
3400         draw_close_box(close_x,close_y);
3401         #ifdef MACINTOSH
3402                 show_cursor();
3403    #else
3404                 ShowCursorW();
3405         #endif
3406 #endif
3407
3408         while(!done)    {
3409         #ifdef WINDOWS
3410                 MSG msg;
3411
3412                 DoMessageStuff(&msg);
3413
3414                 if (_RedrawScreen) {
3415                         _RedrawScreen = FALSE;
3416
3417                         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
3418                                 gr_free_bitmap(bg.background);
3419                         win_redraw = 1;                 
3420                         goto RePaintNewmenuListbox;
3421                 }
3422
3423                 DDGRRESTORE;
3424         #endif
3425   
3426                 ocitem = citem;
3427                 ofirst_item = first_item;
3428 #if defined(MACINTOSH) || defined(WINDOWS)
3429                 omouse_state = mouse_state;
3430                 mouse_state = mouse_button_state(0);
3431 #endif
3432                 //see if redbook song needs to be restarted
3433                 songs_check_redbook_repeat();
3434
3435                 key = key_inkey();
3436
3437                 if ( listbox_callback )
3438                         redraw = (*listbox_callback)(&citem, &nitems, items, &key );
3439                 else
3440                         redraw = 0;
3441
3442         #ifdef WINDOWS
3443                 if (win_redraw) {
3444                         redraw = 1;
3445                         win_redraw = 0;
3446                 }
3447         #endif
3448
3449                 if ( key<-1 ) {
3450                         citem = key;
3451                         key = -1;
3452                         done = 1;
3453                 }
3454
3455
3456         #ifdef WINDOWS
3457                 if (simukey==-1)
3458                         key=KEY_UP;
3459                 else if (simukey==1)
3460                    key=KEY_DOWN;
3461                 simukey=0;
3462         #endif
3463                 
3464                 switch(key)     {
3465                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
3466                 case KEY_PRINT_SCREEN:          
3467                         MAC(hide_cursor());
3468                         save_screen_shot(0); 
3469                         PA_DFX (pa_set_frontbuffer_current());
3470                         PA_DFX (pa_set_front_to_read());
3471                         
3472                         MAC(show_cursor());
3473                         MAC(key_flush());
3474                         break;
3475                 case KEY_HOME:
3476                 case KEY_PAD7:
3477                         citem = 0;
3478                         break;
3479                 case KEY_END:
3480                 case KEY_PAD1:
3481                         citem = nitems-1;
3482                         break;
3483                 case KEY_UP:
3484                 case KEY_PAD8:
3485                         citem--;                        
3486                         break;
3487                 case KEY_DOWN:
3488                 case KEY_PAD2:
3489                         citem++;                        
3490                         break;
3491                 case KEY_PAGEDOWN:
3492                 case KEY_PAD3:
3493                         citem += LB_ITEMS_ON_SCREEN;
3494                         break;
3495                 case KEY_PAGEUP:
3496                 case KEY_PAD9:
3497                         citem -= LB_ITEMS_ON_SCREEN;
3498                         break;
3499                 case KEY_ESC:
3500                         if (allow_abort_flag) {
3501                                 citem = -1;
3502                                 done = 1;
3503                         }
3504                         break;
3505                 case KEY_ENTER:
3506                 case KEY_PADENTER:
3507                         done = 1;
3508                         break;
3509
3510                 #ifdef MACINTOSH
3511                 case KEY_COMMAND+KEY_Q: {
3512                         extern void macintosh_quit();
3513                         
3514                         if ( !(Game_mode & GM_MULTI) )
3515                                 macintosh_quit();
3516                         show_cursor();
3517                         key_flush();
3518                         break;
3519                 }
3520                 #endif
3521
3522                 default:        
3523                         if ( key > 0 )  {
3524                                 int ascii = key_to_ascii(key);
3525                                 if ( ascii < 255 )      {
3526                                         int cc,cc1;
3527                                         cc=cc1=citem+1;
3528                                         if (cc1 < 0 )  cc1 = 0;
3529                                         if (cc1 >= nitems )  cc1 = 0;
3530                                         while(1) {
3531                                                 if ( cc < 0 ) cc = 0;
3532                                                 if ( cc >= nitems ) cc = 0;
3533                                                 if ( citem == cc ) break;
3534         
3535                                                 if ( toupper( items[cc][0] ) == toupper(ascii) )        {
3536                                                         citem = cc;
3537                                                         break;
3538                                                 }
3539                                                 cc++;
3540                                         }
3541                                 }
3542                         }
3543                 }
3544                 if ( done ) break;
3545
3546                 if (citem<0)
3547                         citem=0;
3548
3549                 if (citem>=nitems)
3550                         citem = nitems-1;
3551
3552                 if (citem< first_item)
3553                         first_item = citem;
3554
3555                 if (citem>=( first_item+LB_ITEMS_ON_SCREEN))
3556                         first_item = citem-LB_ITEMS_ON_SCREEN+1;
3557
3558                 if (nitems <= LB_ITEMS_ON_SCREEN )
3559                          first_item = 0;
3560
3561                 if (first_item>nitems-LB_ITEMS_ON_SCREEN)
3562                         first_item = nitems-LB_ITEMS_ON_SCREEN;
3563                 if (first_item < 0 ) first_item = 0;
3564
3565 #ifdef WINDOWS
3566                 if (nitems>first_item+LB_ITEMS_ON_SCREEN)
3567                         show_down_arrow=1;
3568                 else 
3569                         show_down_arrow=0;
3570                 if (first_item>0)
3571                         show_up_arrow=1;
3572                 else    
3573                         show_up_arrow=0;
3574 #endif
3575
3576
3577 #if defined(MACINTOSH) || defined(WINDOWS)
3578                 WIN(Sleep(100));
3579                 if (mouse_state) {
3580                         int w, h, aw;
3581
3582                         mouse_get_pos(&mx, &my);
3583                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
3584                                 if (i > nitems)
3585                                         break;
3586                                 gr_get_string_size(items[i], &w, &h, &aw  );
3587                                 x1 = wx;
3588                                 x2 = wx + width;
3589                                 y1 = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3590                                 y2 = y1+h+1;
3591                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3592                                         //if (i == citem) {
3593                                         //      break;
3594                                         //}
3595                                         //dblclick_flag= 0;
3596                                         citem = i;
3597                                         done = 1;
3598                                         break;
3599                                 }
3600                         }
3601                 }
3602
3603                 //no double-click stuff for listbox
3604                 //@@if (!mouse_state && omouse_state) {
3605                 //@@    int w, h, aw;
3606                 //@@
3607                 //@@    gr_get_string_size(items[citem], &w, &h, &aw  );
3608                 //@@    mouse_get_pos(&mx, &my);
3609                 //@@    x1 = wx;
3610                 //@@    x2 = wx + width;
3611                 //@@    y1 = (citem-first_item)*(grd_curfont->ft_h+2)+wy;
3612                 //@@    y2 = y1+h+1;
3613                 //@@    if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3614                 //@@            if (dblclick_flag) done = 1;
3615                 //@@    }
3616                 //@@}
3617
3618                 //check for close box clicked
3619                 if ( !mouse_state && omouse_state ) {
3620                         mouse_get_pos(&mx, &my);
3621                         x1 = close_x + CLOSE_X + 2;
3622                         x2 = x1 + CLOSE_SIZE - 2;
3623                         y1 = close_y + CLOSE_Y + 2;
3624                         y2 = y1 + CLOSE_SIZE - 2;
3625                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3626                                 citem = -1;
3627                                 done = 1;
3628                         }
3629
3630                    #ifdef WINDOWS
3631                         x1 = wx-LHX(10);
3632                         x2 = x1 + LHX(10);
3633                         y1 = wy;
3634                         y2 = wy+LHY(7);
3635                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow) 
3636                                 simukey = -1;
3637                         y1 = total_height-LHY(7);
3638                         y2 = total_height;
3639                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow ) 
3640                                 simukey = 1;
3641                    #endif
3642
3643                         
3644                 }
3645 #endif
3646
3647                 if ( (ofirst_item != first_item) || redraw)     {
3648                         MAC(hide_cursor());
3649                         WIN(HideCursorW());
3650                         WIN(DDGRLOCK(dd_grd_curcanv));
3651
3652                         gr_setcolor( BM_XRGB( 0,0,0)  );
3653                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
3654                                 int w, h, aw, y;
3655                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3656                                 if ( i >= nitems )      {
3657                                         gr_setcolor( BM_XRGB(0,0,0));
3658                                         gr_rect( wx, y-1, wx+width-1, y+grd_curfont->ft_h + 1 );
3659                                 } else {
3660                                         if ( i == citem )       
3661                                                 grd_curcanv->cv_font = SELECTED_FONT;
3662                                         else    
3663                                                 grd_curcanv->cv_font = NORMAL_FONT;
3664                                         gr_get_string_size(items[i], &w, &h, &aw  );
3665                                         gr_rect( wx, y-1, wx+width-1, y+h+1 );
3666                                         gr_string( wx+5, y, items[i]  );
3667                                 }
3668                         }               
3669
3670                                 
3671                         // If Win95 port, draw up/down arrows on left side of menu
3672                         #ifdef WINDOWS   
3673                                 grd_curcanv->cv_font = NORMAL_FONT;
3674                         if (show_up_arrow)
3675                                 gr_string( wx-LHX(10), wy ,UP_ARROW_MARKER );
3676                         else
3677                         {
3678                                 No_darkening=1;
3679                                 nm_draw_background (wx-LHX(10),wy,wx-2,wy+LHY(7));
3680                                 No_darkening=0;
3681                         }
3682
3683                         if (show_down_arrow)
3684                         gr_string( wx-LHX(10), wy+total_height-LHY(7) ,DOWN_ARROW_MARKER );
3685                         else
3686                         {
3687                                 No_darkening=1;
3688                                 nm_draw_background (wx-LHX(10),wy+total_height-LHY(7),wx-2,wy+total_height);
3689                                 No_darkening=0;
3690                         }
3691
3692                         #endif
3693
3694
3695                         WIN(DDGRUNLOCK(dd_grd_curcanv));
3696                         WIN(ShowCursorW());
3697                         MAC(show_cursor());
3698                         gr_update();
3699                 } else if ( citem != ocitem )   {
3700                         int w, h, aw, y;
3701
3702                         MAC(hide_cursor());
3703                         WIN(HideCursorW());
3704
3705                         WIN(DDGRLOCK(dd_grd_curcanv));
3706
3707                         i = ocitem;
3708                         if ( (i>=0) && (i<nitems) )     {
3709                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3710                                 if ( i == citem )       
3711                                         grd_curcanv->cv_font = SELECTED_FONT;
3712                                 else    
3713                                         grd_curcanv->cv_font = NORMAL_FONT;
3714                                 gr_get_string_size(items[i], &w, &h, &aw  );
3715                                 gr_rect( wx, y-1, wx+width-1, y+h+1 );
3716                                 gr_string( wx+5, y, items[i]  );
3717
3718                         }
3719                         i = citem;
3720                         if ( (i>=0) && (i<nitems) )     {
3721                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3722                                 if ( i == citem )       
3723                                         grd_curcanv->cv_font = SELECTED_FONT;
3724                                 else    
3725                                         grd_curcanv->cv_font = NORMAL_FONT;
3726                                 gr_get_string_size( items[i], &w, &h, &aw  );
3727                                 gr_rect( wx, y-1, wx+width-1, y+h );
3728                                 gr_string( wx+5, y, items[i]  );
3729                         }
3730                         WIN(DDGRUNLOCK(dd_grd_curcanv));
3731
3732                         WIN(ShowCursorW());
3733                         MAC(show_cursor());
3734                         gr_update();
3735                 }
3736         }
3737         MAC(hide_cursor());
3738         WIN(HideCursorW());     
3739
3740         keyd_repeat = old_keyd_repeat;
3741
3742         WIN (DDGRLOCK(dd_grd_curcanv));
3743         gr_bm_bitblt(total_width,total_height, wx-border_size, wy-title_height-border_size, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
3744         WIN (DDGRUNLOCK(dd_grd_curcanv));       
3745
3746         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
3747                 gr_free_bitmap(bg.background);
3748
3749 #if 0
3750         WINDOS(
3751                 dd_gr_blt_notrans(dd_VR_offscreen_buffer,
3752                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh,
3753                                 dd_grd_curcanv,
3754                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
3755                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) )
3756         );
3757 #endif
3758
3759         WIN(DDGRRESTORE);
3760
3761         WIN(mouse_set_mode(1));                         //re-enable centering mode
3762
3763         return citem;
3764 }
3765
3766 int newmenu_filelist( char * title, char * filespec, char * filename )
3767 {
3768         int i, NumFiles;
3769         char * Filenames[MAX_FILES];
3770         char FilenameText[MAX_FILES][14];
3771         FILEFINDSTRUCT find;
3772
3773         NumFiles = 0;
3774         if( !FileFindFirst( filespec, &find ) ) {
3775                 do      {
3776                         if (NumFiles<MAX_FILES) {
3777                                 strncpy( FilenameText[NumFiles], find.name, FILENAME_LEN);
3778                                 Filenames[NumFiles] = FilenameText[NumFiles];
3779                                 NumFiles++;
3780                         } else {
3781                                 break;
3782                         }
3783                 } while( !FileFindNext( &find ) );
3784                 FileFindClose();
3785         }
3786
3787         i = newmenu_listbox( title, NumFiles, Filenames, 1, NULL );
3788         if ( i > -1 )   {
3789                 strcpy( filename, Filenames[i] );
3790                 return 1;
3791         } 
3792         return 0;
3793 }
3794
3795 //added on 10/14/98 by Victor Rachels to attempt a fixedwidth font messagebox
3796 int nm_messagebox_fixedfont( char *title, int nchoices, ... )
3797 {
3798         int i;
3799         char * format;
3800         va_list args;
3801         char *s;
3802         char nm_text[MESSAGEBOX_TEXT_SIZE];
3803         newmenu_item nm_message_items[5];
3804
3805         va_start(args, nchoices );
3806
3807         Assert( nchoices <= 5 );
3808
3809         for (i=0; i<nchoices; i++ )     {
3810                 s = va_arg( args, char * );
3811                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
3812         }
3813         format = va_arg( args, char * );
3814         //sprintf(        nm_text, "" ); // adb: ?
3815         vsprintf(nm_text,format,args);
3816         va_end(args);
3817
3818         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
3819
3820         return newmenu_do_fixedfont( title, nm_text, nchoices, nm_message_items, NULL, 0, NULL, -1, -1 );
3821 }
3822 //end this section addition - Victor Rachels
3823
3824 #ifdef NETWORK
3825 extern netgame_info Active_games[];
3826 extern int NumActiveNetgames;
3827
3828 void show_extra_netgame_info(int choice)
3829  {
3830         newmenu_item m[5];
3831    char mtext[5][50];
3832         int i,num=0;
3833
3834         if (choice>=NumActiveNetgames)
3835                 return;
3836         
3837    for (i=0;i<5;i++)
3838         {
3839          m[i].text=(char *)&mtext[i];
3840     m[i].type=NM_TYPE_TEXT;             
3841         }
3842
3843    sprintf (mtext[num],"Game: %s",Active_games[choice].game_name); num++;
3844    sprintf (mtext[num],"Mission: %s",Active_games[choice].mission_title); num++;
3845         sprintf (mtext[num],"Current Level: %d",Active_games[choice].levelnum); num++;
3846         sprintf (mtext[num],"Difficulty: %s",MENU_DIFFICULTY_TEXT(Active_games[choice].difficulty)); num++;
3847
3848         already_showing_info=1; 
3849         newmenu_dotiny2( NULL, "Netgame Information", num, m, NULL);
3850         already_showing_info=0; 
3851  }
3852
3853 #endif // NETWORK
3854
3855 /* Spiffy word wrap string formatting function */
3856
3857 void nm_wrap_text(char *dbuf, char *sbuf, int line_length)
3858 {
3859         int col;
3860         char *wordptr;
3861         char *tbuf;
3862
3863         tbuf = (char *)d_malloc(strlen(sbuf)+1);
3864         strcpy(tbuf, sbuf);
3865
3866         wordptr = strtok(tbuf, " ");
3867         if (!wordptr) return;
3868         col = 0;
3869         dbuf[0] = 0;
3870
3871         while (wordptr)
3872         {
3873                 col = col+strlen(wordptr)+1;
3874                 if (col >=line_length) {
3875                         col = 0;
3876                         sprintf(dbuf, "%s\n%s ", dbuf, wordptr);
3877                 }
3878                 else {
3879                         sprintf(dbuf, "%s%s ", dbuf, wordptr);
3880                 }
3881                 wordptr = strtok(NULL, " ");
3882         }
3883
3884         d_free(tbuf);
3885 }
3886                                 
3887         
3888
3889
3890