]> icculus.org git repositories - btb/d2x.git/blob - main/newmenu.c
replaced joy/mouse sensitivity slider, increased range to 16, and added d2x keys...
[btb/d2x.git] / main / newmenu.c
1 /* $Id: newmenu.c,v 1.18 2003-02-28 09:56:10 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 #ifdef OGL
735                 gr_palette_load(gr_palette);
736 #endif
737                 remap_fonts_and_menus(1);
738
739         }
740
741 WIN(DDGRLOCK(dd_grd_curcanv));
742 #if defined(POLY_ACC)
743     pa_save_clut();
744     pa_update_clut(gr_palette, 0, 256, 0);
745 #endif
746         show_fullscr(bmp);
747 #if defined(POLY_ACC)
748     pa_restore_clut();
749 #endif
750 WIN(DDGRUNLOCK(dd_grd_curcanv));
751
752         gr_free_bitmap(bmp);
753
754         strcpy(last_palette_loaded,"");         //force palette load next time
755
756 }
757
758 #define MENU_BACKGROUND_BITMAP_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
759 #define MENU_BACKGROUND_BITMAP_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
760
761 #define MENU_BACKGROUND_BITMAP (MenuHires?MENU_BACKGROUND_BITMAP_HIRES:MENU_BACKGROUND_BITMAP_LORES)
762
763 int Background_hires;
764 int No_darkening=0;
765
766 void nm_draw_background(int x1, int y1, int x2, int y2 )
767 {
768         int w,h;
769
770         if (Newmenu_first_time || MenuHires!=Background_hires)  {
771                 int pcx_error;
772
773                 if (Newmenu_first_time) {
774                         atexit( newmenu_close );
775                         Newmenu_first_time = 0;
776                         nm_background_save.bm_data=NULL;                
777                 }
778                 else {
779                         if (nm_background_save.bm_data)
780                                 d_free(nm_background_save.bm_data);
781                         if (nm_background.bm_data)
782                                 d_free(nm_background.bm_data);
783                 }
784
785                 pcx_error = pcx_read_bitmap(MENU_BACKGROUND_BITMAP,&nm_background_save,BM_LINEAR,background_palette);
786                 Assert(pcx_error == PCX_ERROR_NONE);
787
788                 nm_background = nm_background_save;
789                 nm_background.bm_data=NULL;             
790                 nm_remap_background();
791
792                 Background_hires = MenuHires;
793         }
794
795         if ( x1 < 0 ) x1 = 0;
796         if ( y1 < 0 ) y1 = 0;
797
798         w = x2-x1+1;
799         h = y2-y1+1;
800
801         //if ( w > nm_background.bm_w ) w = nm_background.bm_w;
802         //if ( h > nm_background.bm_h ) h = nm_background.bm_h;
803         
804         x2 = x1 + w - 1;
805         y2 = y1 + h - 1;
806
807         {
808                 grs_bitmap *tmp = gr_create_bitmap(w, h);
809
810                 gr_bitmap_scale_to(&nm_background, tmp);
811
812                 WIN(DDGRLOCK(dd_grd_curcanv));
813                 if (No_darkening)
814                         gr_bm_bitblt(w, h, x1, y1, LHX(10), LHY(10), tmp, &(grd_curcanv->cv_bitmap) );
815                 else
816                         gr_bm_bitblt(w, h, x1, y1, 0, 0, tmp, &(grd_curcanv->cv_bitmap) );
817                 gr_free_bitmap(tmp);
818         }
819
820         if (!No_darkening) {
821                 Gr_scanline_darkening_level = 2*7;
822
823                 gr_setcolor( BM_XRGB(0,0,0) );
824                 gr_urect( x2-5, y1+5, x2-5, y2-5 );
825                 gr_urect( x2-4, y1+4, x2-4, y2-5 );
826                 gr_urect( x2-3, y1+3, x2-3, y2-5 );
827                 gr_urect( x2-2, y1+2, x2-2, y2-5 );
828                 gr_urect( x2-1, y1+1, x2-1, y2-5 );
829                 gr_urect( x2+0, y1+0, x2-0, y2-5 );
830
831                 gr_urect( x1+5, y2-5, x2, y2-5 );
832                 gr_urect( x1+4, y2-4, x2, y2-4 );
833                 gr_urect( x1+3, y2-3, x2, y2-3 );
834                 gr_urect( x1+2, y2-2, x2, y2-2 );
835                 gr_urect( x1+1, y2-1, x2, y2-1 );
836                 gr_urect( x1+0, y2, x2, y2-0 );
837         }
838         WIN(DDGRUNLOCK(dd_grd_curcanv));
839
840         Gr_scanline_darkening_level = GR_FADE_LEVELS;
841 }
842
843 void nm_restore_background( int x, int y, int w, int h )
844 {
845         int x1, x2, y1, y2;
846
847         x1 = x; x2 = x+w-1;
848         y1 = y; y2 = y+h-1;
849
850         if ( x1 < 0 ) x1 = 0;
851         if ( y1 < 0 ) y1 = 0;
852
853         if ( x2 >= nm_background.bm_w ) x2=nm_background.bm_w-1;
854         if ( y2 >= nm_background.bm_h ) y2=nm_background.bm_h-1;
855
856         w = x2 - x1 + 1;
857         h = y2 - y1 + 1;
858
859         WIN(DDGRLOCK(dd_grd_curcanv));
860                 gr_bm_bitblt(w, h, x1, y1, x1, y1, &nm_background, &(grd_curcanv->cv_bitmap) );
861         WIN(DDGRUNLOCK(dd_grd_curcanv));
862 }
863
864 // Draw a left justfied string
865 void nm_string( bkg * b, int w1,int x, int y, char * s)
866 {
867         int w,h,aw,tx=0,t=0,i;
868         char *p,*s1,*s2,measure[2];
869         int XTabs[]={15,87,124,162,228,253};
870    
871         p=s1=NULL;
872         s2 = d_strdup(s);
873
874         for (i=0;i<6;i++) {
875                 XTabs[i]=(LHX(XTabs[i]));
876                 XTabs[i]+=x;
877         }
878  
879         measure[1]=0;
880
881         if (!SurfingNet) {
882                 p = strchr( s2, '\t' );
883                 if (p && (w1>0) ) {
884                         *p = '\0';
885                         s1 = p+1;
886                 }
887         }
888
889         gr_get_string_size(s2, &w, &h, &aw  );
890
891         if (w1 > 0)
892                 w = w1;
893
894         // CHANGED
895         gr_bm_bitblt(b->background->bm_w-15, h+2, 5, y-1, 5, y-1, b->background, &(grd_curcanv->cv_bitmap) );
896         //gr_bm_bitblt(w, h, x, y, x, y, b->background, &(grd_curcanv->cv_bitmap) );
897
898         if (SurfingNet) {
899                 for (i=0;i<strlen(s2);i++) {
900                         if (s2[i]=='\t' && SurfingNet) {
901                                 x=XTabs[t];
902                                 t++;
903                                 continue;
904                         }
905                         measure[0]=s2[i];
906                         gr_get_string_size(measure,&tx,&h,&aw);
907                         gr_string(x,y,measure);
908                         x+=tx;
909                 }
910         }
911         else
912                 gr_string (x,y,s2);
913          
914         if (!SurfingNet && p && (w1>0) ) {
915                 gr_get_string_size(s1, &w, &h, &aw  );
916
917                 gr_string( x+w1-w, y, s1 );
918
919                 *p = '\t';
920         }
921         d_free(s2);
922 }
923
924 // Draw a slider and it's string
925 void nm_string_slider( bkg * b, int w1,int x, int y, char * s )
926 {
927         int w,h,aw;
928         char *p,*s1;
929
930         s1=NULL;
931
932         p = strchr( s, '\t' );
933         if (p)  {
934                 *p = '\0';
935                 s1 = p+1;
936         }
937
938         gr_get_string_size(s, &w, &h, &aw  );
939         // CHANGED
940
941                 gr_bm_bitblt(b->background->bm_w-15, h, 5, y, 5, y, b->background, &(grd_curcanv->cv_bitmap) );
942                 //gr_bm_bitblt(w, h, x, y, x, y, b->background, &(grd_curcanv->cv_bitmap) );
943
944                 gr_string( x, y, s );
945
946                 if (p)  {
947                         gr_get_string_size(s1, &w, &h, &aw  );
948
949                         // CHANGED
950                         gr_bm_bitblt(w, 1, x+w1-w, y, x+w1-w, y, b->background, &(grd_curcanv->cv_bitmap) );
951                         // CHANGED
952                         gr_bm_bitblt(w, 1, x+w1-w, y+h-1, x+w1-w, y, b->background, &(grd_curcanv->cv_bitmap) );
953
954                         gr_string( x+w1-w, y, s1 );
955
956                         *p = '\t';
957                 }
958 }
959
960
961 // Draw a left justfied string with black background.
962 void nm_string_black( bkg * b, int w1,int x, int y, char * s )
963 {
964         int w,h,aw;
965         gr_get_string_size(s, &w, &h, &aw  );
966         b = b;                                  
967         if (w1 == 0) w1 = w;
968
969         WIN(DDGRLOCK(dd_grd_curcanv));
970                 gr_setcolor( BM_XRGB(2,2,2) );
971                 gr_rect( x-1, y-1, x-1, y+h-1 );
972                 gr_rect( x-1, y-1, x+w1-1, y-1 );
973
974          
975                 gr_setcolor( BM_XRGB(5,5,5) );
976                 gr_rect( x, y+h, x+w1, y+h);
977                 gr_rect( x+w1, y-1, x+w1, y+h );
978      
979                 gr_setcolor( BM_XRGB(0,0,0) );
980                 gr_rect( x, y, x+w1-1, y+h-1 );
981         
982                 gr_string( x+1, y+1, s );
983         WIN(DDGRUNLOCK(dd_grd_curcanv));
984 }
985
986
987 // Draw a right justfied string
988 void nm_rstring( bkg * b,int w1,int x, int y, char * s )
989 {
990         int w,h,aw;
991         gr_get_string_size(s, &w, &h, &aw  );
992         x -= 3;
993
994         if (w1 == 0) w1 = w;
995
996         //mprintf( 0, "Width = %d, string='%s'\n", w, s );
997
998         // CHANGED
999         WIN(DDGRLOCK(dd_grd_curcanv));
1000                 gr_bm_bitblt(w1, h, x-w1, y, x-w1, y, b->background, &(grd_curcanv->cv_bitmap) );
1001                 gr_string( x-w, y, s );
1002         WIN(DDGRUNLOCK(dd_grd_curcanv));
1003 }
1004
1005 #include "timer.h"
1006
1007 //for text items, constantly redraw cursor (to achieve flash)
1008 void update_cursor( newmenu_item *item)
1009 {
1010         int w,h,aw;
1011         fix time = timer_get_approx_seconds();
1012         int x,y;
1013         char * text = item->text;
1014
1015         Assert(item->type==NM_TYPE_INPUT_MENU || item->type==NM_TYPE_INPUT);
1016
1017         while( *text )  {
1018                 gr_get_string_size(text, &w, &h, &aw  );
1019                 if ( w > item->w-10 )
1020                         text++;
1021                 else
1022                         break;
1023         }
1024         if (*text==0) 
1025                 w = 0;
1026         x = item->x+w; y = item->y;
1027
1028 WIN(DDGRLOCK(dd_grd_curcanv));
1029         if (time & 0x8000)
1030                 gr_string( x, y, CURSOR_STRING );
1031         else {
1032                 gr_setcolor( BM_XRGB(0,0,0) );
1033                 gr_rect( x, y, x+grd_curcanv->cv_font->ft_w-1, y+grd_curcanv->cv_font->ft_h-1 );
1034         }
1035 WIN(DDGRUNLOCK(dd_grd_curcanv));
1036 }
1037
1038 void nm_string_inputbox( bkg *b, int w, int x, int y, char * text, int current )
1039 {
1040         int w1,h1,aw;
1041
1042         while( *text )  {
1043                 gr_get_string_size(text, &w1, &h1, &aw  );
1044                 if ( w1 > w-10 )
1045                         text++;
1046                 else
1047                         break;
1048         }
1049         if ( *text == 0 )
1050                 w1 = 0;
1051
1052    nm_string_black( b, w, x, y, text );
1053                 
1054         if ( current )  {
1055                 gr_string( x+w1+1, y, CURSOR_STRING );
1056         }
1057 }
1058
1059 void draw_item( bkg * b, newmenu_item *item, int is_current,int tiny )
1060 {
1061        if (tiny)
1062         {
1063          if (is_current)
1064           gr_set_fontcolor(gr_find_closest_color_current(57,49,20),-1);
1065          else
1066           gr_set_fontcolor(gr_find_closest_color_current(29,29,47),-1);
1067
1068          if (item->text[0]=='\t')
1069           gr_set_fontcolor (gr_find_closest_color_current(63,63,63),-1);
1070         }
1071        else
1072         {
1073          if (is_current)
1074           grd_curcanv->cv_font = SELECTED_FONT;
1075          else
1076           grd_curcanv->cv_font = NORMAL_FONT;
1077
1078                 #ifdef WINDOWS
1079                         if (is_current && item->type == NM_TYPE_TEXT) 
1080                                 grd_curcanv->cv_font = NORMAL_FONT;
1081                 #endif
1082         }
1083
1084 WIN(DDGRLOCK(dd_grd_curcanv));  
1085         switch( item->type )    {
1086         case NM_TYPE_TEXT:
1087       // grd_curcanv->cv_font=TEXT_FONT;
1088                 // fall through on purpose
1089
1090         case NM_TYPE_MENU:
1091                 nm_string( b, item->w, item->x, item->y, item->text );
1092                 break;
1093         case NM_TYPE_SLIDER:    {
1094                 int j;
1095                 if (item->value < item->min_value) item->value=item->min_value;
1096                 if (item->value > item->max_value) item->value=item->max_value;
1097                 sprintf( item->saved_text, "%s\t%s", item->text, SLIDER_LEFT );
1098                 for (j=0; j<(item->max_value-item->min_value+1); j++ )  {
1099                         sprintf( item->saved_text, "%s%s", item->saved_text,SLIDER_MIDDLE );
1100                 }
1101                 sprintf( item->saved_text, "%s%s", item->saved_text,SLIDER_RIGHT );
1102                 
1103                 item->saved_text[item->value+1+strlen(item->text)+1] = SLIDER_MARKER[0];
1104                 
1105                 nm_string_slider( b, item->w, item->x, item->y, item->saved_text );
1106                 }
1107                 break;
1108         case NM_TYPE_INPUT_MENU:
1109                 if ( item->group==0 )           {
1110                         nm_string( b, item->w, item->x, item->y, item->text );
1111                 } else {
1112                         nm_string_inputbox( b, item->w, item->x, item->y, item->text, is_current );
1113                 }
1114                 break;
1115         case NM_TYPE_INPUT:
1116                 nm_string_inputbox( b, item->w, item->x, item->y, item->text, is_current );
1117                 break;
1118         case NM_TYPE_CHECK:
1119                 nm_string( b, item->w, item->x, item->y, item->text );
1120                 if (item->value)
1121                         nm_rstring( b,item->right_offset,item->x, item->y, CHECKED_CHECK_BOX );
1122                 else                                                                                                              
1123                         nm_rstring( b,item->right_offset,item->x, item->y, NORMAL_CHECK_BOX );
1124                 break;
1125         case NM_TYPE_RADIO:
1126                 nm_string( b, item->w, item->x, item->y, item->text );
1127                 if (item->value)
1128                         nm_rstring( b,item->right_offset, item->x, item->y, CHECKED_RADIO_BOX );
1129                 else
1130                         nm_rstring( b,item->right_offset, item->x, item->y, NORMAL_RADIO_BOX );
1131                 break;
1132         case NM_TYPE_NUMBER:    {
1133                 char text[10];
1134                 if (item->value < item->min_value) item->value=item->min_value;
1135                 if (item->value > item->max_value) item->value=item->max_value;
1136                 nm_string( b, item->w, item->x, item->y, item->text );
1137                 sprintf( text, "%d", item->value );
1138                 nm_rstring( b,item->right_offset,item->x, item->y, text );
1139                 }
1140                 break;
1141         }
1142 WIN(DDGRUNLOCK(dd_grd_curcanv));
1143
1144 }
1145
1146 char *Newmenu_allowed_chars=NULL;
1147
1148 //returns true if char is allowed
1149 int char_allowed(char c)
1150 {
1151         char *p = Newmenu_allowed_chars;
1152
1153         if (!p)
1154                 return 1;
1155
1156         while (*p) {
1157                 Assert(p[1]);
1158
1159                 if (c>=p[0] && c<=p[1])
1160                         return 1;
1161
1162                 p += 2;
1163         }
1164
1165         return 0;
1166 }
1167
1168 void strip_end_whitespace( char * text )
1169 {
1170         int i,l;
1171         l = strlen( text );
1172         for (i=l-1; i>=0; i-- ) {
1173                 if ( isspace(text[i]) )
1174                         text[i] = 0;
1175                 else
1176                         return;
1177         }
1178 }
1179
1180 int newmenu_do( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
1181 {
1182         return newmenu_do3( title, subtitle, nitems, item, subfunction, 0, NULL, -1, -1 );
1183 }
1184 int newmenu_dotiny( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
1185 {
1186         return newmenu_do4( title, subtitle, nitems, item, subfunction, 0, NULL, LHX(310), -1, 1 );
1187 }
1188
1189
1190 int newmenu_dotiny2( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
1191 {
1192         return newmenu_do4( title, subtitle, nitems, item, subfunction, 0, NULL, -1, -1, 1 );
1193 }
1194
1195
1196 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 )
1197 {
1198         return newmenu_do3( title, subtitle, nitems, item, subfunction, citem, NULL, -1, -1 );
1199 }
1200
1201
1202 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 )
1203 {
1204         return newmenu_do3( title, subtitle, nitems, item, subfunction, citem, filename, -1, -1 );
1205 }
1206 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 )
1207  {
1208   return newmenu_do4( title, subtitle, nitems, item, subfunction, citem, filename, width, height,0 );
1209  }
1210
1211 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){
1212         set_screen_mode(SCREEN_MENU);//hafta set the screen mode before calling or fonts might get changed/freed up if screen res changes
1213 //      return newmenu_do3_real( title, subtitle, nitems, item, subfunction, citem, filename, width,height, GAME_FONT, GAME_FONT, GAME_FONT, GAME_FONT);
1214         return newmenu_do4( title, subtitle, nitems, item, subfunction, citem, filename, width,height, 0);
1215 }
1216
1217 //returns 1 if a control device button has been pressed
1218 int check_button_press()
1219 {
1220         int i;
1221
1222         switch (Config_control_type) {
1223         case    CONTROL_JOYSTICK:
1224         case    CONTROL_FLIGHTSTICK_PRO:
1225         case    CONTROL_THRUSTMASTER_FCS:
1226         case    CONTROL_GRAVIS_GAMEPAD:
1227                 for (i=0; i<4; i++ )    
1228                         if (joy_get_button_down_cnt(i)>0) return 1;
1229                 break;
1230         case    CONTROL_MOUSE:
1231         case    CONTROL_CYBERMAN:
1232         #ifndef MACINTOSH                       // don't allow mouse to continue from menu
1233                 for (i=0; i<3; i++ )    
1234                         if (mouse_button_down_count(i)>0) return 1;
1235                 break;
1236         #endif
1237         case    CONTROL_WINJOYSTICK:
1238         #ifdef WINDOWS  
1239                 for (i=0; i<4; i++ )    
1240                         if (joy_get_button_down_cnt(i)>0) return 1;
1241         #endif  
1242                 break;
1243         case    CONTROL_NONE:           //keyboard only
1244                 #ifdef APPLE_DEMO
1245                         if (key_checkch())      return 1;                       
1246                 #endif
1247
1248                 break;
1249         default:
1250                 Error("Bad control type (Config_control_type):%i",Config_control_type);
1251         }
1252
1253         return 0;
1254 }
1255
1256 extern int network_request_player_names(int);
1257 extern int RestoringMenu;
1258
1259 #if defined(WINDOWS) || defined(MACINTOSH)
1260 ubyte Hack_DblClick_MenuMode=0;
1261 #endif
1262
1263 MAC(extern ubyte joydefs_calibrating;)
1264
1265 #define CLOSE_X         15
1266 #define CLOSE_Y         15
1267 #define CLOSE_SIZE      10
1268
1269 void draw_close_box(int x,int y)
1270 {
1271         WIN (DDGRLOCK(dd_grd_curcanv));
1272         gr_setcolor( BM_XRGB(0, 0, 0) );
1273         gr_rect(x + CLOSE_X, y + CLOSE_Y, x + CLOSE_X + CLOSE_SIZE, y + CLOSE_Y + CLOSE_SIZE);
1274         gr_setcolor( BM_XRGB(21, 21, 21) );
1275         gr_rect( x + CLOSE_X + 2, y + CLOSE_Y + 2, x + CLOSE_X + CLOSE_SIZE - 2, y + CLOSE_Y + CLOSE_SIZE - 2 );
1276         WIN (DDGRUNLOCK(dd_grd_curcanv));
1277 }
1278
1279 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 )
1280 {
1281         int old_keyd_repeat, done;
1282         int  choice,old_choice,i,j,x,y,w,h,aw, tw, th, twidth,fm,right_offset;
1283         int k, nmenus, nothers,ScrollOffset=0,LastScrollCheck=-1,MaxDisplayable,sx,sy;
1284         grs_font * save_font;
1285         int string_width, string_height, average_width;
1286         int ty;
1287         bkg bg;
1288         int all_text=0;         //set true if all text items
1289         int sound_stopped=0,time_stopped=0;
1290    int TopChoice,IsScrollBox=0;   // Is this a scrolling box? Set to false at init
1291    char *Temp,TempVal;
1292         int dont_restore=0;
1293    int MaxOnMenu=MAXDISPLAYABLEITEMS;
1294         WINDOS(dd_grs_canvas *save_canvas, grs_canvas *save_canvas );   
1295 #if defined(MACINTOSH) || defined(WINDOWS)
1296         int mouse_state, omouse_state, dblclick_flag=0;
1297         int mx=0, my=0, x1, x2, y1, y2;
1298         int close_box=0;
1299 #endif
1300 #ifdef MACINTOSH
1301         EventRecord event;              // looking for disk inserted events for CD mounts
1302 #endif
1303
1304         PA_DFX (pa_set_frontbuffer_current());
1305         PA_DFX (pa_set_front_to_read());
1306
1307         WIN(if (!_AppActive) return -1);                // Don't draw message if minimized!
1308         WIN(HideCursorW());
1309         MAC(hide_cursor();)
1310
1311         if (nitems < 1 )
1312     {
1313                 return -1;
1314     } 
1315
1316         WIN(mouse_set_mode(0));         //disable centering mode
1317
1318         MaxDisplayable=nitems;
1319
1320 #ifdef OGL
1321         set_screen_mode(SCREEN_MENU);
1322 #endif
1323         set_popup_screen();
1324
1325         if ( Function_mode == FMODE_GAME && !(Game_mode & GM_MULTI)) {
1326                 digi_pause_digi_sounds();
1327                 sound_stopped = 1;
1328         }
1329
1330         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
1331         {
1332                 time_stopped = 1;
1333                 stop_time();
1334                 #ifdef TACTILE 
1335                   if (TactileStick)     
1336                           DisableForces();      
1337                 #endif
1338         }
1339
1340 #ifdef WINDOWS
1341 RePaintNewmenu4:
1342 #endif
1343         WINDOS( save_canvas = dd_grd_curcanv, save_canvas = grd_curcanv );
1344
1345         WINDOS( dd_gr_set_current_canvas(NULL), gr_set_current_canvas(NULL) );
1346
1347         save_font = grd_curcanv->cv_font;
1348
1349         tw = th = 0;
1350
1351         if ( title )    {
1352                 grd_curcanv->cv_font = TITLE_FONT;
1353                 gr_get_string_size(title,&string_width,&string_height,&average_width );
1354                 tw = string_width;
1355                 th = string_height;
1356         }
1357         if ( subtitle ) {
1358                 grd_curcanv->cv_font = SUBTITLE_FONT;
1359                 gr_get_string_size(subtitle,&string_width,&string_height,&average_width );
1360                 if (string_width > tw )
1361                         tw = string_width;
1362                 th += string_height;
1363         }
1364
1365         th += LHY(8);           //put some space between titles & body
1366
1367         if (TinyMode)
1368         grd_curcanv->cv_font = SMALL_FONT;
1369         else 
1370         grd_curcanv->cv_font = NORMAL_FONT;
1371
1372         w = aw = 0;
1373         h = th;
1374         nmenus = nothers = 0;
1375
1376         // Find menu height & width (store in w,h)
1377         for (i=0; i<nitems; i++ )       {
1378                 item[i].redraw=1;
1379                 item[i].y = h;
1380                 gr_get_string_size(item[i].text,&string_width,&string_height,&average_width );
1381                 item[i].right_offset = 0;
1382                 
1383                 if (SurfingNet)
1384                         string_height+=LHY(3);
1385
1386                 item[i].saved_text[0] = '\0';
1387
1388                 if ( item[i].type == NM_TYPE_SLIDER )   {
1389                         int w1,h1,aw1;
1390                         nothers++;
1391                         sprintf( item[i].saved_text, "%s", SLIDER_LEFT );
1392                         for (j=0; j<(item[i].max_value-item[i].min_value+1); j++ )      {
1393                                 sprintf( item[i].saved_text, "%s%s", item[i].saved_text,SLIDER_MIDDLE );
1394                         }
1395                         sprintf( item[i].saved_text, "%s%s", item[i].saved_text,SLIDER_RIGHT );
1396                         gr_get_string_size(item[i].saved_text,&w1,&h1,&aw1 );
1397                         string_width += w1 + aw;
1398                 }
1399
1400                 if ( item[i].type == NM_TYPE_MENU )     {
1401                         nmenus++;
1402                 }
1403
1404                 if ( item[i].type == NM_TYPE_CHECK )    {
1405                         int w1,h1,aw1;
1406                         nothers++;
1407                         gr_get_string_size(NORMAL_CHECK_BOX, &w1, &h1, &aw1  );
1408                         item[i].right_offset = w1;
1409                         gr_get_string_size(CHECKED_CHECK_BOX, &w1, &h1, &aw1  );
1410                         if (w1 > item[i].right_offset)
1411                                 item[i].right_offset = w1;
1412                 }
1413                 
1414                 if (item[i].type == NM_TYPE_RADIO ) {
1415                         int w1,h1,aw1;
1416                         nothers++;
1417                         gr_get_string_size(NORMAL_RADIO_BOX, &w1, &h1, &aw1  );
1418                         item[i].right_offset = w1;
1419                         gr_get_string_size(CHECKED_RADIO_BOX, &w1, &h1, &aw1  );
1420                         if (w1 > item[i].right_offset)
1421                                 item[i].right_offset = w1;
1422                 }
1423
1424                 if  (item[i].type==NM_TYPE_NUMBER )     {
1425                         int w1,h1,aw1;
1426                         char test_text[20];
1427                         nothers++;
1428                         sprintf( test_text, "%d", item[i].max_value );
1429                         gr_get_string_size( test_text, &w1, &h1, &aw1 );
1430                         item[i].right_offset = w1;
1431                         sprintf( test_text, "%d", item[i].min_value );
1432                         gr_get_string_size( test_text, &w1, &h1, &aw1 );
1433                         if ( w1 > item[i].right_offset)
1434                                 item[i].right_offset = w1;
1435                 }
1436
1437                 if ( item[i].type == NM_TYPE_INPUT )    {
1438                         Assert( strlen(item[i].text) < NM_MAX_TEXT_LEN );
1439                         strcpy(item[i].saved_text, item[i].text );
1440                         nothers++;
1441                         string_width = item[i].text_len*grd_curcanv->cv_font->ft_w+((MenuHires?3:1)*item[i].text_len);
1442                         if ( string_width > MAX_TEXT_WIDTH ) 
1443                                 string_width = MAX_TEXT_WIDTH;
1444                         item[i].value = -1;
1445                 }
1446
1447                 if ( item[i].type == NM_TYPE_INPUT_MENU )       {
1448                         Assert( strlen(item[i].text) < NM_MAX_TEXT_LEN );
1449                         strcpy(item[i].saved_text, item[i].text );
1450                         nmenus++;
1451                         string_width = item[i].text_len*grd_curcanv->cv_font->ft_w+((MenuHires?3:1)*item[i].text_len);
1452                         item[i].value = -1;
1453                         item[i].group = 0;
1454                 }
1455
1456                 item[i].w = string_width;
1457                 item[i].h = string_height;
1458
1459                 if ( string_width > w )
1460                         w = string_width;               // Save maximum width
1461                 if ( average_width > aw )
1462                         aw = average_width;
1463                 h += string_height+1;           // Find the height of all strings
1464         }
1465
1466    // Big hack for allowing the netgame options menu to spill over
1467
1468    MaxOnMenu=MAXDISPLAYABLEITEMS;
1469    if (ExtGameStatus==GAMESTAT_NETGAME_OPTIONS || ExtGameStatus==GAMESTAT_MORE_NETGAME_OPTIONS)
1470                 MaxOnMenu++;
1471
1472    if (!TinyMode && (h>((MaxOnMenu+1)*(string_height+1))+(LHY(8))))
1473     {
1474      IsScrollBox=1;
1475      h=(MaxOnMenu*(string_height+1)+LHY(8));
1476      MaxDisplayable=MaxOnMenu;
1477      mprintf ((0,"Hey, this is a scroll box!\n"));
1478     }
1479    else
1480     IsScrollBox=0;
1481
1482         right_offset=0;
1483
1484         if ( width > -1 )
1485                 w = width;
1486
1487         if ( height > -1 )
1488                 h = height;
1489
1490         for (i=0; i<nitems; i++ )       {
1491                 item[i].w = w;
1492                 if (item[i].right_offset > right_offset )
1493                         right_offset = item[i].right_offset;
1494         }
1495         if (right_offset > 0 )
1496                 right_offset += 3;
1497
1498         //gr_get_string_size("",&string_width,&string_height,&average_width );
1499
1500         w += right_offset;
1501
1502
1503         twidth = 0;
1504         if ( tw > w )   {
1505                 twidth = ( tw - w )/2;
1506                 w = tw;
1507         }
1508
1509    if (RestoringMenu)
1510          { right_offset=0; twidth=0;}
1511
1512         mprintf(( 0, "Right offset = %d\n", right_offset ));
1513
1514
1515         // Find min point of menu border
1516 //      x = (grd_curscreen->sc_w-w)/2;
1517 //      y = (grd_curscreen->sc_h-h)/2;
1518
1519         w += MenuHires?60:30;
1520         h += MenuHires?60:30;
1521
1522         if ( w > grd_curcanv->cv_bitmap.bm_w ) w = grd_curcanv->cv_bitmap.bm_w;
1523         if ( h > grd_curcanv->cv_bitmap.bm_h ) h = grd_curcanv->cv_bitmap.bm_h;
1524
1525         x = (grd_curcanv->cv_bitmap.bm_w-w)/2;
1526         y = (grd_curcanv->cv_bitmap.bm_h-h)/2;
1527
1528         if ( x < 0 ) x = 0;
1529         if ( y < 0 ) y = 0;
1530
1531         if ( filename != NULL ) {
1532                 nm_draw_background1( filename );
1533                 gr_palette_load(gr_palette);
1534         }
1535
1536 // Save the background of the display
1537 //              Win95 must refer to the screen as a dd_grs_canvas, so...
1538         WINDOS (        bg.menu_canvas = dd_gr_create_sub_canvas( dd_grd_screencanv, x, y, w, h ),
1539                         bg.menu_canvas = gr_create_sub_canvas( &grd_curscreen->sc_canvas, x, y, w, h )
1540         );
1541         WINDOS (        dd_gr_set_current_canvas( bg.menu_canvas ), 
1542                         gr_set_current_canvas(bg.menu_canvas)   );
1543
1544         if ( filename == NULL ) {
1545                 // Save the background under the menu...
1546                 #ifdef TACTILE
1547                         if (TactileStick)
1548                                 DisableForces();
1549                 #endif
1550                 
1551 #if defined(POLY_ACC)
1552                 bg.saved = gr_create_bitmap2( w, h, grd_curcanv->cv_bitmap.bm_type, NULL );
1553 #else
1554                 bg.saved = gr_create_bitmap( w, h );
1555 #endif
1556                 Assert( bg.saved != NULL );
1557
1558                 WIN (DDGRLOCK(dd_grd_curcanv));
1559                         gr_bm_bitblt(w, h, 0, 0, 0, 0, &grd_curcanv->cv_bitmap, bg.saved );
1560                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1561
1562                 WINDOS (        dd_gr_set_current_canvas(NULL), 
1563                                         gr_set_current_canvas( NULL ) 
1564                 );
1565
1566                 nm_draw_background(x,y,x+w-1,y+h-1);
1567
1568                 WINDOS (        dd_gr_set_current_canvas(bg.menu_canvas),
1569                                         gr_set_current_canvas( bg.menu_canvas )
1570                 );
1571
1572                 bg.background = gr_create_sub_bitmap(&nm_background,0,0,w,h);
1573
1574         } else {
1575                 bg.saved = NULL;
1576 #if defined(POLY_ACC)
1577                 bg.background = gr_create_bitmap2( w, h, grd_curcanv->cv_bitmap.bm_type, NULL );
1578 #else
1579                 bg.background = gr_create_bitmap( w, h );
1580 #endif
1581                 Assert( bg.background != NULL );
1582                 
1583                 WIN (DDGRLOCK(dd_grd_curcanv));
1584                         gr_bm_bitblt(w, h, 0, 0, 0, 0, &grd_curcanv->cv_bitmap, bg.background );
1585                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1586         }
1587
1588 // ty = 15 + (yborder/4);
1589
1590         ty = MenuHires?30:15;
1591
1592         if ( title )    {
1593                 grd_curcanv->cv_font = TITLE_FONT;
1594                 gr_set_fontcolor( GR_GETCOLOR(31,31,31), -1 );
1595                 gr_get_string_size(title,&string_width,&string_height,&average_width );
1596                 tw = string_width;
1597                 th = string_height;
1598                 WIN (DDGRLOCK(dd_grd_curcanv));
1599                         gr_printf( 0x8000, ty, title );
1600                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1601                 ty += th;
1602         }
1603
1604         if ( subtitle ) {
1605                 grd_curcanv->cv_font = SUBTITLE_FONT;
1606                 gr_set_fontcolor( GR_GETCOLOR(21,21,21), -1 );
1607                 gr_get_string_size(subtitle,&string_width,&string_height,&average_width );
1608                 tw = string_width;
1609                 th = string_height;
1610                 WIN (DDGRLOCK(dd_grd_curcanv));
1611                         gr_printf( 0x8000, ty, subtitle );
1612                 WIN (DDGRUNLOCK(dd_grd_curcanv));
1613                 ty += th;
1614         }
1615
1616         if (TinyMode)
1617         grd_curcanv->cv_font = SMALL_FONT;
1618         else 
1619         grd_curcanv->cv_font = NORMAL_FONT;
1620         
1621         // Update all item's x & y values.
1622         for (i=0; i<nitems; i++ )       {
1623                 item[i].x = (MenuHires?30:15) + twidth + right_offset;
1624                 item[i].y += (MenuHires?30:15);
1625                 if ( item[i].type==NM_TYPE_RADIO )      {
1626                         fm = -1;        // find first marked one
1627                         for ( j=0; j<nitems; j++ )      {
1628                                 if ( item[j].type==NM_TYPE_RADIO && item[j].group==item[i].group )      {
1629                                         if (fm==-1 && item[j].value)
1630                                                 fm = j;
1631                                         item[j].value = 0;
1632                                 }
1633                         }
1634                         if ( fm>=0 )    
1635                                 item[fm].value=1;
1636                         else
1637                                 item[i].value=1;
1638                 }
1639         }
1640
1641         old_keyd_repeat = keyd_repeat;
1642         keyd_repeat = 1;
1643
1644         if (citem==-1)  {
1645                 choice = -1;
1646         } else {
1647                 if (citem < 0 ) citem = 0;
1648                 if (citem > nitems-1 ) citem = nitems-1;
1649                 choice = citem;
1650         
1651         #if defined(WINDOWS) || defined(MACINTOSH) 
1652                 dblclick_flag = 1;
1653         #endif
1654
1655                 while ( item[choice].type==NM_TYPE_TEXT )       {
1656                         choice++;
1657                         if (choice >= nitems ) {
1658                                 choice=0; 
1659                         }
1660                         if (choice == citem ) {
1661                                 choice=0; 
1662                                 all_text=1;
1663                                 break; 
1664                         }
1665                 }
1666         } 
1667         done = 0;
1668    TopChoice=choice;
1669
1670         gr_update();
1671         // Clear mouse, joystick to clear button presses.
1672         game_flush_inputs();
1673
1674 #if defined(WINDOWS) || defined(MACINTOSH)
1675         mouse_state = omouse_state = 0;
1676         if (filename == NULL && !MenuReordering) {
1677                 draw_close_box(0,0);
1678                 close_box = 1;
1679         }
1680 #endif
1681
1682 #ifdef WINDOWS
1683         if (!MenuReordering) {
1684                 ShowCursorW();
1685                 SetCursor (LoadCursor(NULL,IDC_ARROW));
1686         }
1687 #endif
1688
1689 #ifdef MACINTOSH  
1690         if (!joydefs_calibrating)
1691                 show_cursor();
1692 #endif
1693
1694    mprintf ((0,"Set to true!\n"));
1695
1696         while(!done)    {
1697         #ifdef WINDOWS
1698                 MSG msg;
1699
1700                 DoMessageStuff(&msg);
1701
1702                 if (_RedrawScreen) {
1703                         _RedrawScreen = FALSE;
1704                 
1705                         if (!filename) {
1706                                 gr_free_bitmap(bg.saved);
1707                                 d_free( bg.background );
1708                         }
1709                         else    
1710                                 gr_free_bitmap(bg.background);
1711
1712                         dd_gr_free_sub_canvas( bg.menu_canvas );
1713                         grd_curcanv->cv_font = save_font;
1714                         dd_grd_curcanv = save_canvas;
1715
1716                         goto RePaintNewmenu4;
1717                 }
1718
1719                 DDGRRESTORE;
1720
1721         #endif
1722
1723         
1724 #ifdef MACINTOSH
1725                 omouse_state = mouse_state;     
1726                 mouse_state = mouse_button_state(0);
1727                 if (!joydefs_calibrating)
1728                         show_cursor();          // possibly hidden
1729 #endif
1730
1731 #ifdef WINDOWS
1732                 omouse_state = mouse_state;     
1733                 if (!MenuReordering)
1734                         mouse_state = mouse_button_state(0);
1735 //@@      mprintf ((0,"mouse state:%d\n",mouse_state));
1736 #endif
1737
1738                 //see if redbook song needs to be restarted
1739                 songs_check_redbook_repeat();
1740
1741                 //network_listen();
1742
1743                 k = key_inkey();
1744
1745         if (subfunction)
1746         (*subfunction)(nitems,item,&k,choice);
1747
1748 #ifdef NETWORK
1749                 if (!time_stopped)      {
1750                         // Save current menu box
1751                         if (multi_menu_poll() == -1)
1752                                 k = -2;
1753                 }
1754 #endif
1755
1756                 if ( k<-1 ) {
1757                         dont_restore = (k == -3);               //-3 means don't restore
1758                         choice = k;
1759                         k = -1;
1760                         done = 1;
1761                 }
1762 #ifndef WINDOWS
1763                 if (check_button_press())
1764                         done = 1;
1765 #endif
1766
1767 //              if ( (nmenus<2) && (k>0) && (nothers==0) )
1768 //                      done=1;
1769
1770                 old_choice = choice;
1771         
1772                 switch( k )     {
1773
1774 #ifdef NETWORK
1775                 case KEY_I:
1776                  if (SurfingNet && !already_showing_info)
1777                    {
1778                          show_extra_netgame_info(choice-2);
1779                    }
1780                  if (SurfingNet && already_showing_info)
1781                         {
1782                          done=1;
1783                          choice=-1;
1784                         }
1785                  break;
1786                 case KEY_U:
1787                  if (SurfingNet && !already_showing_info)
1788                    {
1789                          network_request_player_names(choice-2);
1790                    }
1791                  if (SurfingNet && already_showing_info)
1792                         {
1793                          done=1;
1794                          choice=-1;
1795                         }
1796                  break;
1797 #endif
1798                 case KEY_PAUSE:
1799                  if (Pauseable_menu)
1800                    {    
1801                          Pauseable_menu=0;
1802                          done=1;
1803                          choice=-1;
1804                    }
1805                  break;
1806                 case KEY_TAB + KEY_SHIFTED:
1807                 case KEY_UP:
1808                 case KEY_PAD8:
1809                         if (all_text) break;
1810                         do {
1811                                 choice--;
1812
1813                 if (IsScrollBox)
1814                 {
1815                         LastScrollCheck=-1;
1816                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1817                                    
1818                 if (choice<TopChoice)
1819                         { choice=TopChoice; break; }
1820
1821                 if (choice<ScrollOffset)
1822                {
1823                         for (i=0;i<nitems;i++)
1824                                 item[i].redraw=1;
1825                      ScrollOffset--;
1826                      mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1827                }
1828                 }
1829                 else
1830                 {
1831                         if (choice >= nitems ) choice=0;
1832                 if (choice < 0 ) choice=nitems-1;
1833                 }
1834                         } while ( item[choice].type==NM_TYPE_TEXT );
1835                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1836                                 item[choice].value = -1;
1837                         if ((old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))     {
1838                                 item[old_choice].group=0;
1839                                 strcpy(item[old_choice].text, item[old_choice].saved_text );
1840                                 item[old_choice].value = -1;
1841                         }
1842                         if (old_choice>-1) 
1843                                 item[old_choice].redraw = 1;
1844                         item[choice].redraw=1;
1845                         break;
1846                 case KEY_TAB:
1847                 case KEY_DOWN:
1848                 case KEY_PAD2:
1849         // ((0,"Pressing down! IsScrollBox=%d",IsScrollBox));
1850                 if (all_text) break;
1851                         do {
1852                                 choice++;
1853
1854                         if (IsScrollBox)
1855                 {
1856                 LastScrollCheck=-1;
1857                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1858                                    
1859                 if (choice==nitems)
1860                 { choice--; break; }
1861
1862                 if (choice>=MaxOnMenu+ScrollOffset)
1863                 {
1864                 for (i=0;i<nitems;i++)
1865                                 item[i].redraw=1;
1866                   ScrollOffset++;
1867                   mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1868                 }
1869                 }
1870             else
1871             {
1872                         if (choice < 0 ) choice=nitems-1;
1873                         if (choice >= nitems ) choice=0;
1874                 }
1875
1876                         } while ( item[choice].type==NM_TYPE_TEXT );
1877                                                       
1878                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1879                                 item[choice].value = -1;
1880                         if ( (old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))    {
1881                                 item[old_choice].group=0;
1882                                 strcpy(item[old_choice].text, item[old_choice].saved_text );    
1883                                 item[old_choice].value = -1;
1884                         }
1885                         if (old_choice>-1)
1886                                 item[old_choice].redraw=1;
1887                         item[choice].redraw=1;
1888                         break;
1889                 case KEY_SPACEBAR:
1890                         if ( choice > -1 )      {
1891                                 switch( item[choice].type )     {
1892                                 case NM_TYPE_MENU:
1893                                 case NM_TYPE_INPUT:
1894                                 case NM_TYPE_INPUT_MENU:
1895                                         break;
1896                                 case NM_TYPE_CHECK:
1897                                         if ( item[choice].value )
1898                                                 item[choice].value = 0;
1899                                         else
1900                                                 item[choice].value = 1;
1901                                         mprintf ((0,"ISB=%d MDI=%d SO=%d choice=%d\n",IsScrollBox,MAXDISPLAYABLEITEMS,ScrollOffset,choice));
1902                                         if (IsScrollBox)
1903                                          {
1904                                                 if (choice==(MaxOnMenu+ScrollOffset-1) || choice==ScrollOffset)
1905                                                  {
1906                                                    mprintf ((0,"Special redraw!\n"));
1907                                                         LastScrollCheck=-1;                                     
1908                                                  }
1909                                          }
1910                                 
1911                                         item[choice].redraw=1;
1912                                         break;
1913                                 case NM_TYPE_RADIO:
1914                                         for (i=0; i<nitems; i++ )       {
1915                                                 if ((i!=choice) && (item[i].type==NM_TYPE_RADIO) && (item[i].group==item[choice].group) && (item[i].value) )    {
1916                                                         item[i].value = 0;
1917                                                         item[i].redraw = 1;
1918                                                 }
1919                                         }
1920                                         item[choice].value = 1;
1921                                         item[choice].redraw = 1;
1922                                         break;
1923                                 }       
1924                         }
1925                         break;
1926
1927                 case KEY_SHIFTED+KEY_UP:
1928                  if (MenuReordering && choice!=TopChoice)
1929                   {
1930                    Temp=item[choice].text;
1931                    TempVal=item[choice].value;
1932                    item[choice].text=item[choice-1].text;
1933                    item[choice].value=item[choice-1].value;
1934                    item[choice-1].text=Temp;
1935                    item[choice-1].value=TempVal;
1936                    item[choice].redraw=1;
1937                    item[choice-1].redraw=1;
1938                    choice--;
1939                   }
1940                  break;
1941                 case KEY_SHIFTED+KEY_DOWN:
1942                  if (MenuReordering && choice!=(nitems-1))
1943                   {
1944                    Temp=item[choice].text;
1945                    TempVal=item[choice].value;
1946                    item[choice].text=item[choice+1].text;
1947                    item[choice].value=item[choice+1].value;
1948                    item[choice+1].text=Temp;
1949                    item[choice+1].value=TempVal;
1950                    item[choice].redraw=1;
1951                    item[choice+1].redraw=1;
1952                    choice++;
1953                   }
1954                  break;
1955                 
1956                 case KEY_ENTER:
1957                 case KEY_PADENTER:
1958                         if ( (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==0)) {
1959                                 item[choice].group = 1;
1960                                 item[choice].redraw = 1;
1961                                 if ( !strnicmp( item[choice].saved_text, TXT_EMPTY, strlen(TXT_EMPTY) ) )       {
1962                                         item[choice].text[0] = 0;
1963                                         item[choice].value = -1;
1964                                 } else {        
1965                                         strip_end_whitespace(item[choice].text);
1966                                 }
1967                         } else
1968                                 done = 1;
1969                         break;
1970
1971                 case KEY_ESC:
1972                         if ( (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==1)) {
1973                                 item[choice].group=0;
1974                                 strcpy(item[choice].text, item[choice].saved_text );    
1975                                 item[choice].redraw=1;
1976                                 item[choice].value = -1;
1977                         } else {
1978                                 done = 1;
1979                                 choice = -1;
1980                         }
1981                         break;
1982
1983                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
1984                 case KEY_PRINT_SCREEN:
1985                         MAC(hide_cursor());
1986                         save_screen_shot(0);
1987                         PA_DFX (pa_set_frontbuffer_current());
1988                         PA_DFX (pa_set_front_to_read());
1989                         for (i=0;i<nitems;i++)
1990                                 item[i].redraw=1;
1991                         
1992                         MAC(show_cursor());
1993                         MAC(key_flush());
1994                         break;
1995
1996                 #ifdef MACINTOSH
1997
1998                 case KEY_COMMAND+KEY_RIGHT:
1999                         songs_goto_next_song();
2000                         break;
2001                 case KEY_COMMAND+KEY_LEFT:
2002                         songs_goto_prev_song();
2003                         break;
2004                 case KEY_COMMAND+KEY_UP:
2005                         songs_play_level_song(1);
2006                         break;
2007                 case KEY_COMMAND+KEY_DOWN:
2008                         songs_stop_redbook();
2009                         break;
2010
2011                 case KEY_COMMAND+KEY_M:
2012                         k = -1;
2013                         #if !defined(SHAREWARE) || defined(APPLE_DEMO)
2014                         if ( (Game_mode & GM_MULTI) )           // don't process in multiplayer games
2015                                 break;
2016
2017                         key_close();            // no processing of keys with keyboard handler.. jeez                           
2018                         stop_time();
2019                         hide_cursor();
2020                         show_boxed_message ("Mounting CD\nESC to quit");        
2021                         RBAMountDisk();         // OS has totaly control of the CD.
2022                         if (Function_mode == FMODE_MENU)
2023                                 songs_play_song(SONG_TITLE,1);
2024                         else if (Function_mode == FMODE_GAME)
2025                                 songs_play_level_song( Current_level_num );
2026                         clear_boxed_message();
2027                         show_cursor();
2028                         key_init();
2029                         key_flush();
2030                         start_time();
2031                         #endif
2032                         
2033                         break;
2034
2035                 case KEY_COMMAND+KEY_E:
2036                         songs_stop_redbook();
2037                         RBAEjectDisk();
2038                         k = -1;         // force key not to register
2039                         break;
2040                         
2041                 case KEY_COMMAND+KEY_Q: {
2042                         extern void macintosh_quit();
2043                         
2044                         if ( !(Game_mode & GM_MULTI) )
2045                                 macintosh_quit();
2046                         if (!joydefs_calibrating)
2047                                 show_cursor();
2048                         k = -1;         // force key not to register
2049                         break;
2050                 }
2051                 #endif
2052
2053                 #ifndef NDEBUG
2054                 case KEY_BACKSP:        
2055                         if ( (choice>-1) && (item[choice].type!=NM_TYPE_INPUT)&&(item[choice].type!=NM_TYPE_INPUT_MENU))
2056                                 Int3(); 
2057                         break;
2058                 #endif
2059
2060                 }
2061
2062 #if defined(MACINTOSH) || defined(WINDOWS) // for mouse selection of menu's etc.
2063                 WIN(Sleep(100));
2064                 if ( !done && mouse_state && !omouse_state && !all_text ) {
2065                         mouse_get_pos(&mx, &my);
2066                         for (i=0; i<nitems; i++ )       {
2067                                 x1 = grd_curcanv->cv_bitmap.bm_x + item[i].x - item[i].right_offset - 6;
2068                                 x2 = x1 + item[i].w;
2069                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[i].y;
2070                                 y2 = y1 + item[i].h;
2071                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
2072                                         if (i+ScrollOffset != choice) {
2073                                                 if(Hack_DblClick_MenuMode) dblclick_flag = 0; 
2074                                         }
2075                                         
2076                                         choice = i + ScrollOffset;
2077
2078                                         switch( item[choice].type )     {
2079                                         case NM_TYPE_CHECK:
2080                                                 if ( item[choice].value )
2081                                                         item[choice].value = 0;
2082                                                 else
2083                                                         item[choice].value = 1;
2084                                                 item[choice].redraw=1;
2085
2086                                                 if (IsScrollBox)
2087                                                         LastScrollCheck=-1;
2088 #if 0
2089                                                 if (IsScrollBox)
2090                                                  {
2091                                                         if (choice==(MaxOnMenu+ScrollOffset-1) || choice==ScrollOffset)
2092                                                          {
2093                                                            mprintf ((0,"Special redraw!\n"));
2094                                                                 LastScrollCheck=-1;                                     
2095                                                          }
2096                                                  }
2097 #endif
2098                                                 break;
2099                                         case NM_TYPE_RADIO:
2100                                                 for (i=0; i<nitems; i++ )       {
2101                                                         if ((i!=choice) && (item[i].type==NM_TYPE_RADIO) && (item[i].group==item[choice].group) && (item[i].value) )    {
2102                                                                 item[i].value = 0;
2103                                                                 item[i].redraw = 1;
2104                                                         }
2105                                                 }
2106                                                 item[choice].value = 1;
2107                                                 item[choice].redraw = 1;
2108                                                 break;
2109                                         }
2110                                         item[old_choice].redraw=1;
2111                                         break;
2112                                 }
2113                         }
2114                 }
2115
2116                 if (mouse_state && all_text)
2117                         done = 1;
2118                 
2119                 if ( !done && mouse_state && !all_text ) {
2120                         mouse_get_pos(&mx, &my);
2121                         
2122                         // check possible scrollbar stuff first
2123                         if (IsScrollBox) {
2124                                 int arrow_width, arrow_height, aw;
2125                                 
2126                                 if (ScrollOffset != 0) {
2127                                         gr_get_string_size(UP_ARROW_MARKER, &arrow_width, &arrow_height, &aw);
2128                                         x2 = grd_curcanv->cv_bitmap.bm_x + item[ScrollOffset].x-(MenuHires?24:12);
2129                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[ScrollOffset].y-((string_height+1)*ScrollOffset);
2130                                         x1 = x1 - arrow_width;
2131                                         y2 = y1 + arrow_height;
2132                                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2133                                                 choice--;
2134                                         LastScrollCheck=-1;
2135                                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
2136                                                    
2137                                 if (choice<ScrollOffset)
2138                                {
2139                                         for (i=0;i<nitems;i++)
2140                                                 item[i].redraw=1;
2141                                      ScrollOffset--;
2142                                      mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
2143                                }
2144                                         }
2145                                 }
2146                                 if (ScrollOffset+MaxDisplayable<nitems) {
2147                                         gr_get_string_size(DOWN_ARROW_MARKER, &arrow_width, &arrow_height, &aw);
2148                                         x2 = grd_curcanv->cv_bitmap.bm_x + item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
2149                                         y1 = grd_curcanv->cv_bitmap.bm_y + item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
2150                                         x1 = x1 - arrow_width;
2151                                         y2 = y1 + arrow_height;
2152                                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2153                                                 choice++;
2154                                 LastScrollCheck=-1;
2155                                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
2156                                                    
2157                                 if (choice>=MaxOnMenu+ScrollOffset)
2158                                 {
2159                                 for (i=0;i<nitems;i++)
2160                                                 item[i].redraw=1;
2161                                   ScrollOffset++;
2162                                   mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
2163                                 }
2164                                         }
2165                                 }
2166                         }
2167                         
2168                         for (i=0; i<nitems; i++ )       {
2169                                 x1 = grd_curcanv->cv_bitmap.bm_x + item[i].x - item[i].right_offset - 6;
2170                                 x2 = x1 + item[i].w;
2171                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[i].y;
2172                                 y2 = y1 + item[i].h;
2173                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && (item[i].type != NM_TYPE_TEXT) ) {
2174                                         if (i+ScrollOffset != choice) {
2175                                                 if(Hack_DblClick_MenuMode) dblclick_flag = 0; 
2176                                         }
2177
2178                                         choice = i + ScrollOffset;
2179
2180                                         if ( item[choice].type == NM_TYPE_SLIDER ) {
2181                                                 char slider_text[NM_MAX_TEXT_LEN+1], *p, *s1;
2182                                                 int slider_width, height, aw, sleft_width, sright_width, smiddle_width;
2183                                                 
2184                                                 strcpy(slider_text, item[choice].saved_text);
2185                                                 p = strchr(slider_text, '\t');
2186                                                 if (p) {
2187                                                         *p = '\0';
2188                                                         s1 = p+1;
2189                                                 }
2190                                                 if (p) {
2191                                                         gr_get_string_size(s1, &slider_width, &height, &aw);
2192                                                         gr_get_string_size(SLIDER_LEFT, &sleft_width, &height, &aw);
2193                                                         gr_get_string_size(SLIDER_RIGHT, &sright_width, &height, &aw);
2194                                                         gr_get_string_size(SLIDER_MIDDLE, &smiddle_width, &height, &aw);
2195
2196                                                         x1 = grd_curcanv->cv_bitmap.bm_x + item[choice].x + item[choice].w - slider_width;
2197                                                         x2 = x1 + slider_width + sright_width;
2198                                                         if ( (mx > x1) && (mx < (x1 + sleft_width)) && (item[choice].value != item[choice].min_value) ) {
2199                                                                 item[choice].value = item[choice].min_value;
2200                                                                 item[choice].redraw = 2;
2201                                                         } else if ( (mx < x2) && (mx > (x2 - sright_width)) && (item[choice].value != item[choice].max_value) ) {
2202                                                                 item[choice].value = item[choice].max_value;
2203                                                                 item[choice].redraw = 2;
2204                                                         } else if ( (mx > (x1 + sleft_width)) && (mx < (x2 - sright_width)) ) {
2205                                                                 int num_values, value_width, new_value;
2206                                                                 
2207                                                                 num_values = item[choice].max_value - item[choice].min_value + 1;
2208                                                                 value_width = (slider_width - sleft_width - sright_width) / num_values;
2209                                                                 new_value = (mx - x1 - sleft_width) / value_width;
2210                                                                 if ( item[choice].value != new_value ) {
2211                                                                         item[choice].value = new_value;
2212                                                                         item[choice].redraw = 2;
2213                                                                 }
2214                                                         }
2215                                                         *p = '\t';
2216                                                 }
2217                                         }
2218                                         if (choice == old_choice)
2219                                                 break;
2220                                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
2221                                                 item[choice].value = -1;
2222                                         if ((old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))     {
2223                                                 item[old_choice].group=0;
2224                                                 strcpy(item[old_choice].text, item[old_choice].saved_text );
2225                                                 item[old_choice].value = -1;
2226                                         }
2227                                         if (old_choice>-1) 
2228                                                 item[old_choice].redraw = 1;
2229                                         item[choice].redraw=1;
2230                                         break;
2231                                 }
2232                         }
2233                 }
2234                 
2235                 if ( !done && !mouse_state && omouse_state && !all_text && (choice != -1) && (item[choice].type == NM_TYPE_MENU) ) {
2236                         mouse_get_pos(&mx, &my);
2237                         x1 = grd_curcanv->cv_bitmap.bm_x + item[choice].x;
2238                         x2 = x1 + item[choice].w;
2239                         y1 = grd_curcanv->cv_bitmap.bm_y + item[choice].y;
2240                         y2 = y1 + item[choice].h;
2241                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
2242                                 if (Hack_DblClick_MenuMode) {
2243                                         if (dblclick_flag) done = 1;
2244                                         else dblclick_flag = 1;
2245                                 }
2246                                 else done = 1;
2247                         }
2248                 }
2249                 
2250                 if ( !done && !mouse_state && omouse_state && (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==0))        {
2251                         item[choice].group = 1;
2252                         item[choice].redraw = 1;
2253                         if ( !strnicmp( item[choice].saved_text, TXT_EMPTY, strlen(TXT_EMPTY) ) )       {
2254                                 item[choice].text[0] = 0;
2255                                 item[choice].value = -1;
2256                         } else {        
2257                                 strip_end_whitespace(item[choice].text);
2258                         }
2259                 }
2260                 
2261                 if ( !done && !mouse_state && omouse_state && close_box ) {
2262                         mouse_get_pos(&mx, &my);
2263                         x1 = grd_curcanv->cv_bitmap.bm_x + CLOSE_X;
2264                         x2 = x1 + CLOSE_SIZE;
2265                         y1 = grd_curcanv->cv_bitmap.bm_y + CLOSE_Y;
2266                         y2 = y1 + CLOSE_SIZE;
2267                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2268                                 choice = -1;
2269                                 done = 1;
2270                         }
2271                 }
2272
2273 //       HACK! Don't redraw loadgame preview
2274                 if (RestoringMenu) item[0].redraw = 0;
2275 #endif          // ifdef MACINTOSH
2276
2277                 if ( choice > -1 )      {
2278                         int ascii;
2279
2280                         if ( ((item[choice].type==NM_TYPE_INPUT)||((item[choice].type==NM_TYPE_INPUT_MENU)&&(item[choice].group==1)) )&& (old_choice==choice) ) {
2281                                 if ( k==KEY_LEFT || k==KEY_BACKSP || k==KEY_PAD4 )      {
2282                                         if (item[choice].value==-1) item[choice].value = strlen(item[choice].text);
2283                                         if (item[choice].value > 0)
2284                                                 item[choice].value--;
2285                                         item[choice].text[item[choice].value] = 0;
2286                                         item[choice].redraw = 1;        
2287                                 } else {
2288                                         ascii = key_to_ascii(k);
2289                                         if ((ascii < 255 ) && (item[choice].value < item[choice].text_len ))
2290                                         {
2291                                                 int allowed;
2292
2293                                                 if (item[choice].value==-1) {
2294                                                         item[choice].value = 0;
2295                                                 }
2296
2297                                                 allowed = char_allowed(ascii);
2298
2299                                                 if (!allowed && ascii==' ' && char_allowed('_')) {
2300                                                         ascii = '_';
2301                                                         allowed=1;
2302                                                 }
2303
2304                                                 if (allowed) {
2305                                                         item[choice].text[item[choice].value++] = ascii;
2306                                                         item[choice].text[item[choice].value] = 0;
2307                                                         item[choice].redraw=1;  
2308                                                 }
2309                                         }
2310                                 }
2311                         } else if ((item[choice].type!=NM_TYPE_INPUT) && (item[choice].type!=NM_TYPE_INPUT_MENU) ) {
2312                                 ascii = key_to_ascii(k);
2313                                 if (ascii < 255 ) {
2314                                         int choice1 = choice;
2315                                         ascii = toupper(ascii);
2316                                         do {
2317                                                 int i,ch;
2318                                                 choice1++;
2319                                                 if (choice1 >= nitems ) choice1=0;
2320                                                 for (i=0;(ch=item[choice1].text[i])!=0 && ch==' ';i++);
2321                                                 if ( ( (item[choice1].type==NM_TYPE_MENU) ||
2322                                                                  (item[choice1].type==NM_TYPE_CHECK) ||
2323                                                                  (item[choice1].type==NM_TYPE_RADIO) ||
2324                                                                  (item[choice1].type==NM_TYPE_NUMBER) ||
2325                                                                  (item[choice1].type==NM_TYPE_SLIDER) )
2326                                                                 && (ascii==toupper(ch)) )       {
2327                                                         k = 0;
2328                                                         choice = choice1;
2329                                                         if (old_choice>-1)
2330                                                                 item[old_choice].redraw=1;
2331                                                         item[choice].redraw=1;
2332                                                 }
2333                                         } while (choice1 != choice );
2334                                 }       
2335                         }
2336
2337                         if ( (item[choice].type==NM_TYPE_NUMBER) || (item[choice].type==NM_TYPE_SLIDER))        {
2338                                 int ov=item[choice].value;
2339                                 switch( k ) {
2340                                 case KEY_PAD4:
2341                                 case KEY_LEFT:
2342                                 case KEY_MINUS:
2343                                 case KEY_MINUS+KEY_SHIFTED:
2344                                 case KEY_PADMINUS:
2345                                         item[choice].value -= 1;
2346                                         break;
2347                                 case KEY_RIGHT:
2348                                 case KEY_PAD6:
2349                                 case KEY_EQUAL:
2350                                 case KEY_EQUAL+KEY_SHIFTED:
2351                                 case KEY_PADPLUS:
2352                                         item[choice].value++;
2353                                         break;
2354                                 case KEY_PAGEUP:
2355                                 case KEY_PAD9:
2356                                 case KEY_SPACEBAR:
2357                                         item[choice].value += 10;
2358                                         break;
2359                                 case KEY_PAGEDOWN:
2360                                 case KEY_BACKSP:
2361                                 case KEY_PAD3:
2362                                         item[choice].value -= 10;
2363                                         break;
2364                                 }
2365                                 if (ov!=item[choice].value)
2366                                         item[choice].redraw=1;
2367                         }
2368         
2369                 }
2370
2371                 WINDOS (        dd_gr_set_current_canvas(bg.menu_canvas),
2372                                 gr_set_current_canvas(bg.menu_canvas));
2373
2374         // Redraw everything...
2375         for (i=ScrollOffset; i<MaxDisplayable+ScrollOffset; i++ )
2376         {
2377         if (item[i].redraw) // warning! ugly hack below                  
2378                 {
2379                 item[i].y-=((string_height+1)*ScrollOffset);
2380                 MAC(hide_cursor());
2381                                 WIN(HideCursorW());
2382                 draw_item( &bg, &item[i], (i==choice && !all_text),TinyMode );
2383                                 item[i].redraw=0;
2384                                 MAC(if (!joydefs_calibrating) show_cursor());
2385                                 WIN(if (!MenuReordering) ShowCursorW());
2386             item[i].y+=((string_height+1)*ScrollOffset);
2387                 }   
2388          if (i==choice && (item[i].type==NM_TYPE_INPUT || (item[i].type==NM_TYPE_INPUT_MENU && item[i].group)))
2389                                 update_cursor( &item[i]);
2390                 }
2391         gr_update();
2392
2393       if (IsScrollBox)
2394         {
2395         //grd_curcanv->cv_font = NORMAL_FONT;
2396         
2397                 if (LastScrollCheck!=ScrollOffset)
2398          {
2399                 LastScrollCheck=ScrollOffset;
2400                 grd_curcanv->cv_font = SELECTED_FONT;
2401                                 
2402                 sy=item[ScrollOffset].y-((string_height+1)*ScrollOffset);
2403                 sx=item[ScrollOffset].x-(MenuHires?24:12);
2404                                 
2405           
2406                 if (ScrollOffset!=0)
2407                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, UP_ARROW_MARKER );
2408                 else
2409                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
2410
2411                 sy=item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
2412                 sx=item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
2413           
2414                 if (ScrollOffset+MaxDisplayable<nitems)
2415                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, DOWN_ARROW_MARKER );
2416                 else
2417                 nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
2418
2419         }
2420
2421         }   
2422
2423                 if ( !dont_restore && gr_palette_faded_out )    {
2424                         gr_palette_fade_in( gr_palette, 32, 0 );
2425                 }
2426         }
2427         
2428         MAC(hide_cursor());
2429         WIN(HideCursorW());
2430         
2431         // Restore everything...
2432
2433         WINDOS (        dd_gr_set_current_canvas(bg.menu_canvas),
2434                         gr_set_current_canvas(bg.menu_canvas));
2435
2436         if ( filename == NULL ) {
2437                 // Save the background under the menu...
2438                 WIN (DDGRLOCK(dd_grd_curcanv));
2439                         gr_bitmap(0, 0, bg.saved);      
2440                 WIN (DDGRUNLOCK(dd_grd_curcanv));
2441                 gr_free_bitmap(bg.saved);
2442                 d_free( bg.background );
2443         } else {
2444                 if (!dont_restore)      //info passed back from subfunction
2445                 {
2446                         WIN (DDGRLOCK(dd_grd_curcanv));
2447                         gr_bitmap(0, 0, bg.background);
2448                         WIN (DDGRUNLOCK(dd_grd_curcanv));       
2449                 }
2450                 gr_free_bitmap(bg.background);
2451         }
2452
2453         WINDOS (        dd_gr_free_sub_canvas(bg.menu_canvas),
2454                         gr_free_sub_canvas( bg.menu_canvas ) );
2455
2456         WINDOS (dd_gr_set_current_canvas(NULL), gr_set_current_canvas( NULL ));                 
2457         grd_curcanv->cv_font    = save_font;
2458         WINDOS (dd_gr_set_current_canvas(NULL), gr_set_current_canvas( save_canvas ));                  
2459         keyd_repeat = old_keyd_repeat;
2460
2461         game_flush_inputs();
2462
2463         if (time_stopped) 
2464      {
2465                 start_time();
2466                 #ifdef TACTILE
2467                         if (TactileStick)
2468                                 EnableForces();
2469                 #endif
2470           }
2471
2472         if ( sound_stopped )
2473                 digi_resume_digi_sounds();
2474
2475         WIN(mouse_set_mode(1));                         //re-enable centering mode
2476
2477         return choice;
2478         
2479 }
2480
2481
2482 int nm_messagebox1( char *title, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int nchoices, ... )
2483 {
2484         int i;
2485         char * format;
2486         va_list args;
2487         char *s;
2488         char nm_text[MESSAGEBOX_TEXT_SIZE];
2489         newmenu_item nm_message_items[5];
2490
2491         va_start(args, nchoices );
2492
2493         Assert( nchoices <= 5 );
2494
2495         for (i=0; i<nchoices; i++ )     {
2496                 s = va_arg( args, char * );
2497                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
2498         }
2499         format = va_arg( args, char * );
2500         strcpy( nm_text, "" );
2501         vsprintf(nm_text,format,args);
2502         va_end(args);
2503
2504         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE);
2505
2506         return newmenu_do( title, nm_text, nchoices, nm_message_items, subfunction );
2507 }
2508
2509 int nm_messagebox( char *title, int nchoices, ... )
2510 {
2511         int i;
2512         char * format;
2513         va_list args;
2514         char *s;
2515         char nm_text[MESSAGEBOX_TEXT_SIZE];
2516         newmenu_item nm_message_items[5];
2517
2518         va_start(args, nchoices );
2519
2520         Assert( nchoices <= 5 );
2521
2522         for (i=0; i<nchoices; i++ )     {
2523                 s = va_arg( args, char * );
2524                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
2525         }
2526         format = va_arg( args, char * );
2527         strcpy( nm_text, "" );
2528         vsprintf(nm_text,format,args);
2529         va_end(args);
2530
2531         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
2532
2533         return newmenu_do( title, nm_text, nchoices, nm_message_items, NULL );
2534 }
2535
2536
2537
2538
2539 void newmenu_file_sort( int n, char *list )
2540 {
2541         int i, j, incr;
2542         char t[14];
2543
2544         incr = n / 2;
2545         while( incr > 0 )               {
2546                 for (i=incr; i<n; i++ )         {
2547                         j = i - incr;
2548                         while (j>=0 )                   {
2549                                 if (strncmp(&list[j*14], &list[(j+incr)*14], 12) > 0)                           {
2550                                         memcpy( t, &list[j*14], FILENAME_LEN );
2551                                         memcpy( &list[j*14], &list[(j+incr)*14], FILENAME_LEN );
2552                                         memcpy( &list[(j+incr)*14], t, FILENAME_LEN );
2553                                         j -= incr;
2554                                 }
2555                                 else
2556                                         break;
2557                         }
2558                 }
2559                 incr = incr / 2;
2560         }
2561 }
2562
2563 void delete_player_saved_games(char * name)
2564 {
2565         int i;
2566         char filename[16];
2567         
2568         for (i=0;i<10; i++)     {
2569 #ifndef MACINTOSH
2570                 sprintf( filename, "%s.sg%d", name, i );
2571 #else
2572                 sprintf( filename, ":Players:%s.sg%d", name, i );
2573 #endif
2574                 unlink( filename );
2575         }
2576 }
2577
2578 #define MAX_FILES 300
2579
2580 int MakeNewPlayerFile(int allow_abort);
2581
2582 int newmenu_get_filename( char * title, char * filespec, char * filename, int allow_abort_flag )
2583 {
2584         int i;
2585         FILEFINDSTRUCT find;
2586         int NumFiles=0, key,done, citem, ocitem;
2587         char * filenames = NULL;
2588         int NumFiles_displayed = 8;
2589         int first_item = -1, ofirst_item;
2590         int old_keyd_repeat = keyd_repeat;
2591         int player_mode=0;
2592         int demo_mode=0;
2593         int demos_deleted=0;
2594         int initialized = 0;
2595         int exit_value = 0;
2596         int w_x, w_y, w_w, w_h, title_height;
2597         int box_x, box_y, box_w, box_h;
2598         bkg bg;         // background under listbox
2599 #if defined(MACINTOSH) || defined(WINDOWS)
2600         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;
2601         int mouse2_state, omouse2_state;
2602         int dblclick_flag=0;
2603    int simukey=0;
2604         int show_up_arrow=0,show_down_arrow=0;
2605 #endif
2606 WIN(int win_redraw=0);
2607
2608         w_x=w_y=w_w=w_h=title_height=0;
2609         box_x=box_y=box_w=box_h=0;
2610
2611         filenames = d_malloc( MAX_FILES * 14 );
2612         if (filenames==NULL) return 0;
2613
2614         citem = 0;
2615         keyd_repeat = 1;
2616
2617         WIN(mouse_set_mode(0));                         //disable centering mode
2618
2619         if (strstr( filespec, "*.plr" ))
2620                 player_mode = 1;
2621         else if (strstr( filespec, "*.dem" ))
2622                 demo_mode = 1;
2623
2624 ReadFileNames:
2625         done = 0;
2626         NumFiles=0;
2627         
2628 #if !defined(APPLE_DEMO)                // no new pilots for special apple oem version
2629         if (player_mode)        {
2630                 strncpy( &filenames[NumFiles*14], TXT_CREATE_NEW, FILENAME_LEN );
2631                 NumFiles++;
2632         }
2633 #endif
2634
2635         if( !FileFindFirst( filespec, &find ) ) {
2636                 do      {
2637                         if (NumFiles<MAX_FILES) {
2638                                 strncpy( &filenames[NumFiles*14], find.name, FILENAME_LEN );
2639                                 if ( player_mode )      {
2640                                         char * p;
2641                                         p = strchr(&filenames[NumFiles*14],'.');
2642                                         if (p) *p = '\0';
2643                                 }
2644                                 NumFiles++;
2645                         } else {
2646                                 break;
2647                         }
2648                 } while( !FileFindNext( &find ) );
2649                 FileFindClose();
2650         }
2651
2652         if ( (NumFiles < 1) && demos_deleted )  {
2653                 exit_value = 0;
2654                 goto ExitFileMenu;
2655         }
2656         if ( (NumFiles < 1) && demo_mode ) {
2657                 nm_messagebox( NULL, 1, TXT_OK, "%s %s\n%s", TXT_NO_DEMO_FILES, TXT_USE_F5, TXT_TO_CREATE_ONE);
2658                 exit_value = 0;
2659                 goto ExitFileMenu;
2660         }
2661
2662         #ifndef APPLE_DEMO
2663         if ( (NumFiles < 2) && player_mode ) {
2664                 citem = 0;
2665                 goto ExitFileMenuEarly;
2666         }
2667         #endif
2668
2669
2670         if ( NumFiles<1 )       {
2671                 #ifndef APPLE_DEMO
2672                         nm_messagebox( NULL, 1, "Ok", "%s\n '%s' %s", TXT_NO_FILES_MATCHING, filespec, TXT_WERE_FOUND);
2673                 #endif
2674                 exit_value = 0;
2675                 goto ExitFileMenu;
2676         }
2677
2678         if (!initialized) {     
2679 //              set_screen_mode(SCREEN_MENU);
2680                 set_popup_screen();
2681
2682         #ifdef WINDOWS
2683 RePaintNewmenuFile:
2684
2685                 dd_gr_set_current_canvas(NULL);
2686         #else
2687                 gr_set_current_canvas(NULL);
2688         #endif
2689
2690                 WIN(DDGRLOCK(dd_grd_curcanv))                                   //mwa put these here -- are these needed Samir???
2691                 {
2692                         grd_curcanv->cv_font = SUBTITLE_FONT;
2693                 }
2694                 WIN(DDGRUNLOCK(dd_grd_curcanv));
2695
2696                 w_w = 0;
2697                 w_h = 0;
2698
2699                 for (i=0; i<NumFiles; i++ ) {
2700                         int w, h, aw;
2701                         gr_get_string_size( &filenames[i*14], &w, &h, &aw );            
2702                         if ( w > w_w )
2703                                 w_w = w;
2704                 }
2705                 if ( title ) {
2706                         int w, h, aw;
2707                         gr_get_string_size( title, &w, &h, &aw );               
2708                         if ( w > w_w )
2709                                 w_w = w;
2710                         title_height = h + (grd_curfont->ft_h*2);               // add a little space at the bottom of the title
2711                 }
2712
2713                 box_w = w_w;
2714                 box_h = ((grd_curfont->ft_h + 2) * NumFiles_displayed);
2715
2716                 w_w += (grd_curfont->ft_w * 4);
2717                 w_h = title_height + box_h + (grd_curfont->ft_h * 2);           // more space at bottom
2718
2719                 if ( w_w > grd_curcanv->cv_w ) w_w = grd_curcanv->cv_w;
2720                 if ( w_h > grd_curcanv->cv_h ) w_h = grd_curcanv->cv_h;
2721         
2722                 w_x = (grd_curcanv->cv_w-w_w)/2;
2723                 w_y = (grd_curcanv->cv_h-w_h)/2;
2724         
2725                 if ( w_x < 0 ) w_x = 0;
2726                 if ( w_y < 0 ) w_y = 0;
2727
2728                 box_x = w_x + (grd_curfont->ft_w*2);                    // must be in sync with w_w!!!
2729                 box_y = w_y + title_height;
2730
2731 // save the screen behind the menu.
2732
2733                 bg.saved = NULL;
2734
2735         #if !defined(WINDOWS)
2736                 if ( (VR_offscreen_buffer->cv_w >= w_w) && (VR_offscreen_buffer->cv_h >= w_h) ) 
2737                         bg.background = &VR_offscreen_buffer->cv_bitmap;
2738                 else
2739         #endif
2740 #if defined(POLY_ACC)
2741                         bg.background = gr_create_bitmap2( w_w, w_h, grd_curcanv->cv_bitmap.bm_type, NULL );
2742 #else
2743                         bg.background = gr_create_bitmap( w_w, w_h );
2744 #endif
2745
2746                 Assert( bg.background != NULL );
2747
2748
2749                 WIN(DDGRLOCK(dd_grd_curcanv));
2750                 gr_bm_bitblt(w_w, w_h, 0, 0, w_x, w_y, &grd_curcanv->cv_bitmap, bg.background );
2751                 WIN(DDGRUNLOCK(dd_grd_curcanv));
2752
2753 #if 0
2754                 WINDOS(
2755                         dd_gr_blt_notrans(dd_grd_curcanv, 0, 0, 
2756                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh, 
2757                                 dd_VR_offscreen_buffer, 0, 0, 
2758                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
2759                         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) )
2760                 );
2761 #endif
2762
2763                 nm_draw_background( w_x,w_y,w_x+w_w-1,w_y+w_h-1 );
2764                 
2765                 WIN(DDGRLOCK(dd_grd_curcanv))
2766                 {       
2767                         gr_string( 0x8000, w_y+10, title );
2768                 }
2769                 WIN(DDGRUNLOCK(dd_grd_curcanv));
2770
2771                 WIN(DDGRRESTORE);
2772          
2773                 initialized = 1;
2774         }
2775
2776         if ( !player_mode )     {
2777                 newmenu_file_sort( NumFiles, filenames );
2778         } else {
2779                 #if defined(MACINTOSH) && defined(APPLE_DEMO)
2780                 newmenu_file_sort( NumFiles, filenames );
2781                 #else
2782                 newmenu_file_sort( NumFiles-1, &filenames[14] );                // Don't sort first one!
2783                 #endif
2784                 for ( i=0; i<NumFiles; i++ )    {
2785                         if (!stricmp(Players[Player_num].callsign, &filenames[i*14]) )  {
2786                         #if defined(WINDOWS) || defined(MACINTOSH) 
2787                                 dblclick_flag = 1;
2788                         #endif
2789                                 citem = i;
2790                         }
2791                 }
2792         }
2793         
2794 #if defined(MACINTOSH) || defined(WINDOWS)
2795         mouse_state = omouse_state = 0;
2796         mouse2_state = omouse2_state = 0;
2797         draw_close_box(w_x,w_y);
2798    #ifdef MACINTOSH
2799                 show_cursor();
2800         #else
2801                 ShowCursorW();
2802         #endif
2803 #endif
2804
2805         while(!done)    {
2806         #ifdef WINDOWS
2807                 MSG msg;
2808
2809                 DoMessageStuff(&msg);
2810
2811                 if (_RedrawScreen) {
2812                         _RedrawScreen = FALSE;
2813
2814                         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
2815                                 gr_free_bitmap(bg.background);
2816         
2817                         win_redraw = 1;         
2818                         goto RePaintNewmenuFile;
2819                 }
2820
2821                 DDGRRESTORE
2822         #endif
2823
2824                 ocitem = citem;
2825                 ofirst_item = first_item;
2826                 gr_update();
2827
2828 #if defined(MACINTOSH) || defined(WINDOWS)
2829                 omouse_state = mouse_state;
2830                 omouse2_state = mouse2_state;
2831                 mouse_state = mouse_button_state(0);
2832                 mouse2_state = mouse_button_state(1);
2833 #endif
2834
2835                 //see if redbook song needs to be restarted
2836                 songs_check_redbook_repeat();
2837
2838                 #ifdef WINDOWS
2839                 if (!mouse2_state && omouse2_state)
2840                         key = KEY_CTRLED+KEY_D;         //fake ctrl-d
2841                 else
2842                 #endif
2843                         //NOTE LINK TO ABOVE ELSE
2844                         key = key_inkey();
2845
2846         #ifdef WINDOWS
2847                 if (simukey==-1)
2848                         key=KEY_UP;
2849                 else if (simukey==1)
2850                    key=KEY_DOWN;
2851                 simukey=0;
2852         #endif
2853                         
2854                 switch(key)     {
2855                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
2856                 case KEY_PRINT_SCREEN:
2857                         MAC(hide_cursor());
2858                         save_screen_shot(0);
2859                         PA_DFX (pa_set_frontbuffer_current());
2860                         PA_DFX (pa_set_front_to_read());
2861                         
2862                         MAC(show_cursor());
2863                         MAC(key_flush());
2864                         break;
2865
2866                 case KEY_CTRLED+KEY_D:
2867                         #if defined(MACINTOSH) && defined(APPLE_DEMO)
2868                         break;
2869                         #endif
2870
2871                         if ( ((player_mode)&&(citem>0)) || ((demo_mode)&&(citem>=0)) )  {
2872                                 int x = 1;
2873                                 MAC(hide_cursor());
2874                                 #ifdef WINDOWS
2875                                 mouse_set_mode(1);                              //re-enable centering mode
2876                                 HideCursorW();
2877                                 #endif
2878                                 if (player_mode)
2879                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2880                                 else if (demo_mode)
2881                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2882                                 MAC(show_cursor());
2883                                 #ifdef WINDOWS
2884                                 mouse_set_mode(0);                              //disenable centering mode
2885                                 ShowCursorW();
2886                                 #endif
2887                                 if (x==0)       {
2888                                         char * p;
2889                                         int ret;
2890                                         char name[_MAX_PATH],dir[_MAX_DIR];
2891
2892                                         p = &filenames[(citem*14)+strlen(&filenames[citem*14])];
2893                                         if (player_mode)
2894                                                 *p = '.';
2895
2896                                         _splitpath(filespec,name,dir,NULL,NULL);
2897                                         strcat(name,dir);
2898                                         strcat(name,&filenames[citem*14]);
2899                                         
2900                                         #ifdef MACINTOSH
2901                                         {
2902                                                 int i;
2903                                                 char *p;
2904                                                 
2905                                                 if ( !strncmp(name, ".\\", 2) )
2906                                                         for (i = 0; i < strlen(name); i++)              // don't subtract 1 from strlen to get the EOS marker
2907                                                                 name[i] = name[i+1];
2908                                                 while ( (p = strchr(name, '\\')) )
2909                                                         *p = ':';
2910                                         }
2911                                         #endif
2912                                 
2913                                         ret = unlink( name );
2914                                         if (player_mode)
2915                                                 *p = 0;
2916
2917                                         if ((!ret) && player_mode)      {
2918                                                 delete_player_saved_games( &filenames[citem*14] );
2919                                         }
2920
2921                                         if (ret) {
2922                                                 if (player_mode)
2923                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2924                                                 else if (demo_mode)
2925                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2926                                         } else if (demo_mode)
2927                                                 demos_deleted = 1;
2928                                         first_item = -1;
2929                                         goto ReadFileNames;
2930                                 }
2931                         }
2932                         break;
2933                 case KEY_HOME:
2934                 case KEY_PAD7:
2935                         citem = 0;
2936                         break;
2937                 case KEY_END:
2938                 case KEY_PAD1:
2939                         citem = NumFiles-1;
2940                         break;
2941                 case KEY_UP:
2942                 case KEY_PAD8:
2943                         citem--;                        
2944                         break;
2945                 case KEY_DOWN:
2946                 case KEY_PAD2:
2947                         citem++;                        
2948                         break;
2949                 case KEY_PAGEDOWN:
2950                 case KEY_PAD3:
2951                         citem += NumFiles_displayed;
2952                         break;
2953                 case KEY_PAGEUP:
2954                 case KEY_PAD9:
2955                         citem -= NumFiles_displayed;
2956                         break;
2957                 case KEY_ESC:
2958                         if (allow_abort_flag) {
2959                                 citem = -1;
2960                                 done = 1;
2961                         }
2962                         break;
2963                 case KEY_ENTER:
2964                 case KEY_PADENTER:
2965                         done = 1;
2966                         break;
2967                         
2968                 #ifdef MACINTOSH
2969                 case KEY_COMMAND+KEY_Q: {
2970                         extern void macintosh_quit();
2971                         
2972                         if ( !(Game_mode & GM_MULTI) )
2973                                 macintosh_quit();
2974                         show_cursor();
2975                         key_flush();
2976                         break;
2977                 }
2978                 #endif
2979                 
2980                 default:        
2981                         {
2982
2983                                 int ascii = key_to_ascii(key);
2984                                 if ( ascii < 255 )      {
2985                                         int cc,cc1;
2986                                         cc=cc1=citem+1;
2987                                         if (cc1 < 0 )  cc1 = 0;
2988                                         if (cc1 >= NumFiles )  cc1 = 0;
2989                                         while(1) {
2990                                                 if ( cc < 0 ) cc = 0;
2991                                                 if ( cc >= NumFiles ) cc = 0;
2992                                                 if ( citem == cc ) break;
2993         
2994                                                 if ( toupper(filenames[cc*14]) == toupper(ascii) )      {
2995                                                         citem = cc;
2996                                                         break;
2997                                                 }
2998                                                 cc++;
2999                                         }
3000                                 }
3001                         }
3002                 }
3003                 if ( done ) break;
3004
3005
3006                 if (citem<0)
3007                         citem=0;
3008
3009                 if (citem>=NumFiles)
3010                         citem = NumFiles-1;
3011
3012                 if (citem< first_item)
3013                         first_item = citem;
3014
3015                 if (citem>=( first_item+NumFiles_displayed))
3016                 {
3017                         first_item = citem-NumFiles_displayed+1;
3018                 }
3019
3020 #ifdef WINDOWS
3021                 if (NumFiles>first_item+NumFiles_displayed)
3022                         show_down_arrow=1;
3023                 else 
3024                         show_down_arrow=0;
3025                 if (first_item>0)
3026                         show_up_arrow=1;
3027                 else    
3028                         show_up_arrow=0;
3029 #endif
3030                         
3031
3032                 if (NumFiles <= NumFiles_displayed )
3033                          first_item = 0;
3034
3035                 if (first_item>NumFiles-NumFiles_displayed)
3036                 {
3037                         first_item = NumFiles-NumFiles_displayed;
3038                 }
3039
3040                 if (first_item < 0 ) first_item = 0;
3041
3042 #if defined(MACINTOSH) || defined(WINDOWS)
3043                 WIN(Sleep(100));
3044                 if (mouse_state || mouse2_state) {
3045                         int w, h, aw;
3046
3047                         mouse_get_pos(&mx, &my);
3048                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
3049                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3050                                 x1 = box_x;
3051                                 x2 = box_x + box_w - 1;
3052                                 y1 = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
3053                                 y2 = y1+h+1;
3054                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3055                                         if (i == citem && !mouse2_state) {
3056                                                 break;
3057                                         }
3058                                         citem = i;
3059                                         dblclick_flag = 0;
3060                                         break;
3061                                 }
3062                         }
3063                 }
3064                 
3065                 if (!mouse_state && omouse_state) {
3066                         int w, h, aw;
3067
3068                         gr_get_string_size(&filenames[citem*14], &w, &h, &aw  );
3069                         mouse_get_pos(&mx, &my);
3070                         x1 = box_x;
3071                         x2 = box_x + box_w - 1;
3072                         y1 = (citem-first_item)*(grd_curfont->ft_h + 2) + box_y;
3073                         y2 = y1+h+1;
3074                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3075                                 if (dblclick_flag) done = 1;
3076                                 else dblclick_flag = 1;
3077                         }
3078                 }
3079
3080                 if ( !mouse_state && omouse_state ) {
3081                         mouse_get_pos(&mx, &my);
3082                         x1 = w_x + CLOSE_X + 2;
3083                         x2 = x1 + CLOSE_SIZE - 2;
3084                         y1 = w_y + CLOSE_Y + 2;
3085                         y2 = y1 + CLOSE_SIZE - 2;
3086                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3087                                 citem = -1;
3088                                 done = 1;
3089                         }
3090                    #ifdef WINDOWS
3091                         x1 = box_x-LHX(10);
3092                         x2 = x1 + LHX(10);
3093                         y1 = box_y;
3094                         y2 = box_y+LHY(7);
3095                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow ) 
3096                                 simukey = -1;
3097                         y1 = box_y+box_h-LHY(7);
3098                         y2 = box_y+box_h;
3099                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow) 
3100                                 simukey = 1;
3101                    #endif
3102                 }
3103
3104 #endif
3105   
3106         WIN(DDGRLOCK(dd_grd_curcanv));
3107                 gr_setcolor( BM_XRGB(2,2,2));
3108                 //gr_rect( box_x - 1, box_y-2, box_x + box_w, box_y-2 );
3109                 gr_setcolor( BM_XRGB( 0,0,0)  );
3110
3111         #ifdef WINDOWS
3112                 if (ofirst_item != first_item || win_redraw)    {
3113                         win_redraw = 0;
3114         #else
3115                 if (ofirst_item != first_item)  {
3116         #endif
3117                         MAC(hide_cursor());
3118                         WIN(HideCursorW());
3119                         gr_setcolor( BM_XRGB( 0,0,0)  );
3120                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
3121                                 int w, h, aw, y;
3122                                 y = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
3123                         
3124                                 if ( i >= NumFiles )    {
3125
3126                                         gr_setcolor( BM_XRGB(5,5,5));
3127                                         gr_rect( box_x + box_w, y-1, box_x + box_w, y + grd_curfont->ft_h + 1);
3128                                         //gr_rect( box_x, y + grd_curfont->ft_h + 2, box_x + box_w, y + grd_curfont->ft_h + 2);
3129                                         
3130                                         gr_setcolor( BM_XRGB(2,2,2));
3131                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + grd_curfont->ft_h + 2 );
3132                                         
3133                                         gr_setcolor( BM_XRGB(0,0,0));
3134                                         gr_rect( box_x, y - 1, box_x + box_w - 1, y + grd_curfont->ft_h + 1);
3135                                         
3136                                 } else {
3137                                         if ( i == citem )       
3138                                                 grd_curcanv->cv_font = SELECTED_FONT;
3139                                         else    
3140                                                 grd_curcanv->cv_font = NORMAL_FONT;
3141                                         gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3142
3143                                         gr_setcolor( BM_XRGB(5,5,5));
3144                                   //    gr_rect( box_x, y + h + 2, box_x + box_w, y + h + 2);
3145                                         gr_rect( box_x + box_w, y - 1, box_x + box_w, y + h + 1);
3146                                         
3147                                         gr_setcolor( BM_XRGB(2,2,2));
3148                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + h + 1);
3149                                         gr_setcolor( BM_XRGB(0,0,0));
3150                                                         
3151                                         gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
3152                                         gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
3153                                 }
3154                         }        
3155                         WIN(ShowCursorW());
3156                         MAC(show_cursor());
3157                 } else if ( citem != ocitem )   {
3158                         int w, h, aw, y;
3159
3160                         MAC(hide_cursor());
3161                         WIN(HideCursorW());
3162                         i = ocitem;
3163                         if ( (i>=0) && (i<NumFiles) )   {
3164                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
3165                                 if ( i == citem )       
3166                                         grd_curcanv->cv_font = SELECTED_FONT;
3167                                 else    
3168                                         grd_curcanv->cv_font = NORMAL_FONT;
3169                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3170                                 gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
3171                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
3172                         }
3173                         i = citem;
3174                         if ( (i>=0) && (i<NumFiles) )   {
3175                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
3176                                 if ( i == citem )       
3177                                         grd_curcanv->cv_font = SELECTED_FONT;
3178                                 else    
3179                                         grd_curcanv->cv_font = NORMAL_FONT;
3180                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
3181                                 gr_rect( box_x, y-1, box_x + box_x - 1, y + h + 1 );
3182                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
3183                         }
3184                         WIN(ShowCursorW());
3185                         MAC(show_cursor());
3186                 }
3187
3188         #ifdef WINDOWS   
3189                         grd_curcanv->cv_font = NORMAL_FONT;
3190                         if (show_up_arrow)
3191                                 gr_string( box_x-LHX(10), box_y ,UP_ARROW_MARKER );
3192                         else
3193                         {
3194                                 No_darkening=1;
3195                                 nm_draw_background (box_x-LHX(10),box_y,box_x-2,box_y+LHY(7));
3196                                 No_darkening=0;
3197                         }
3198
3199                         if (show_down_arrow)
3200                         gr_string( box_x-LHX(10), box_y+box_h-LHY(7) ,DOWN_ARROW_MARKER );
3201                         else
3202                         {
3203                                 No_darkening=1;
3204                                 nm_draw_background (box_x-LHX(10),box_y+box_h-LHY(7),box_x-2,box_y+box_h);
3205                                 No_darkening=0;
3206                         }
3207
3208         #endif
3209
3210
3211
3212         WIN(DDGRUNLOCK(dd_grd_curcanv));
3213         }
3214
3215 ExitFileMenuEarly:
3216         MAC(hide_cursor());
3217         if ( citem > -1 )       {
3218                 strncpy( filename, (&filenames[citem*14])+((player_mode && filenames[citem*14]=='$')?1:0), FILENAME_LEN );
3219                 exit_value = 1;
3220         } else {
3221                 exit_value = 0;
3222         }                                                                                        
3223
3224 ExitFileMenu:
3225         keyd_repeat = old_keyd_repeat;
3226
3227         if ( initialized )      {
3228                         if (Newdemo_state != ND_STATE_PLAYBACK) //horrible hack to prevent restore when screen has been cleared
3229                         {
3230                         WIN (DDGRLOCK(dd_grd_curcanv));
3231                                 gr_bm_bitblt(w_w, w_h, w_x, w_y, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
3232                         WIN (DDGRUNLOCK(dd_grd_curcanv));       
3233                         }
3234                         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
3235                                 gr_free_bitmap(bg.background);
3236 #if 0
3237                 WINDOS(
3238                         dd_gr_blt_notrans(dd_VR_offscreen_buffer,
3239                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh,
3240                                 dd_grd_curcanv,
3241                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
3242                         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) )
3243                 );
3244 #endif
3245
3246                 WIN(DDGRRESTORE);
3247         }
3248
3249         if ( filenames )
3250                 d_free(filenames);
3251
3252         WIN(mouse_set_mode(1));                         //re-enable centering mode
3253         WIN(HideCursorW());
3254
3255         return exit_value;
3256
3257 }
3258
3259
3260 // Example listbox callback function...
3261 // int lb_callback( int * citem, int *nitems, char * items[], int *keypress )
3262 // {
3263 //      int i;
3264 // 
3265 //      if ( *keypress = KEY_CTRLED+KEY_D )     {
3266 //              if ( *nitems > 1 )      {
3267 //                      unlink( items[*citem] );                // Delete the file
3268 //                      for (i=*citem; i<*nitems-1; i++ )       {
3269 //                              items[i] = items[i+1];
3270 //                      }
3271 //                      *nitems = *nitems - 1;
3272 //                      d_free( items[*nitems] );
3273 //                      items[*nitems] = NULL;
3274 //                      return 1;       // redraw;
3275 //              }
3276 //                      *keypress = 0;
3277 //      }                       
3278 //      return 0;
3279 // }
3280
3281 #define LB_ITEMS_ON_SCREEN 8
3282
3283 int newmenu_listbox( char * title, int nitems, char * items[], int allow_abort_flag, int (*listbox_callback)( int * citem, int *nitems, char * items[], int *keypress ) )
3284 {
3285         return newmenu_listbox1( title, nitems, items, allow_abort_flag, 0, listbox_callback );
3286 }
3287
3288 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 ) )
3289 {
3290         int i;
3291         int done, ocitem,citem, ofirst_item, first_item, key, redraw;
3292         int old_keyd_repeat = keyd_repeat;
3293         int width, height, wx, wy, title_height, border_size;
3294         int total_width,total_height;
3295         bkg bg;
3296 #if defined(MACINTOSH) || defined(WINDOWS)
3297         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;  //, dblclick_flag;
3298         int close_x,close_y;
3299    int simukey=0,show_up_arrow=0,show_down_arrow=0;
3300 #endif
3301 WIN(int win_redraw=0);
3302
3303         keyd_repeat = 1;
3304
3305    PA_DFX (pa_set_frontbuffer_current());
3306         PA_DFX (pa_set_front_to_read());
3307         WIN(mouse_set_mode(0));                         //disable centering mode
3308
3309 //      set_screen_mode(SCREEN_MENU);
3310         set_popup_screen();
3311
3312 #ifdef WINDOWS
3313 RePaintNewmenuListbox:
3314  
3315         dd_gr_set_current_canvas(NULL);
3316 #else
3317         gr_set_current_canvas(NULL);
3318 #endif
3319
3320         grd_curcanv->cv_font = SUBTITLE_FONT;
3321
3322         width = 0;
3323         for (i=0; i<nitems; i++ )       {
3324                 int w, h, aw;
3325                 gr_get_string_size( items[i], &w, &h, &aw );            
3326                 if ( w > width )
3327                         width = w;
3328         }
3329         height = (grd_curfont->ft_h + 2) * LB_ITEMS_ON_SCREEN;
3330
3331         {
3332                 int w, h, aw;
3333                 gr_get_string_size( title, &w, &h, &aw );               
3334                 if ( w > width )
3335                         width = w;
3336                 title_height = h + 5;
3337         }
3338
3339         border_size = grd_curfont->ft_w;
3340    WIN (border_size=grd_curfont->ft_w*2);
3341                 
3342         width += (grd_curfont->ft_w);
3343         if ( width > grd_curcanv->cv_w - (grd_curfont->ft_w * 3) )
3344                 width = grd_curcanv->cv_w - (grd_curfont->ft_w * 3);
3345
3346         wx = (grd_curcanv->cv_bitmap.bm_w-width)/2;
3347         wy = (grd_curcanv->cv_bitmap.bm_h-(height+title_height))/2 + title_height;
3348         if ( wy < title_height )
3349                 wy = title_height;
3350
3351         total_width = width+2*border_size;
3352         total_height = height+2*border_size+title_height;
3353
3354         bg.saved = NULL;
3355
3356 #if !defined(WINDOWS)
3357         if ( (VR_offscreen_buffer->cv_w >= total_width) && (VR_offscreen_buffer->cv_h >= total_height) )
3358                 bg.background = &VR_offscreen_buffer->cv_bitmap;
3359         else
3360 #endif
3361                 //bg.background = gr_create_bitmap( width, (height + title_height) );
3362 #if defined(POLY_ACC)
3363                 bg.background = gr_create_bitmap2(total_width, total_height, grd_curcanv->cv_bitmap.bm_type, NULL);
3364 #else
3365                 bg.background = gr_create_bitmap(total_width,total_height);
3366 #endif
3367         Assert( bg.background != NULL );
3368                 
3369         WIN (DDGRLOCK(dd_grd_curcanv));
3370                 //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 );
3371                 gr_bm_bitblt(total_width,total_height, 0, 0, wx-border_size, wy-title_height-border_size, &grd_curcanv->cv_bitmap, bg.background );
3372         WIN (DDGRUNLOCK(dd_grd_curcanv));
3373
3374 #if 0
3375         WINDOS(
3376                 dd_gr_blt_notrans(dd_grd_curcanv, 0, 0, 
3377                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh, 
3378                                 dd_VR_offscreen_buffer, 0, 0, 
3379                                 _DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
3380                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) )
3381         );
3382 #endif
3383
3384         nm_draw_background( wx-border_size,wy-title_height-border_size,wx+width+border_size-1,wy+height+border_size-1 );
3385
3386         WIN(DDGRLOCK(dd_grd_curcanv));
3387                 gr_string( 0x8000, wy - title_height, title );
3388         WIN(DDGRUNLOCK(dd_grd_curcanv));        
3389
3390         WIN(DDGRRESTORE);
3391
3392         done = 0;
3393         citem = default_item;
3394         if ( citem < 0 ) citem = 0;
3395         if ( citem >= nitems ) citem = 0;
3396
3397         first_item = -1;
3398
3399 #if defined(MACINTOSH) || defined(WINDOWS)
3400         mouse_state = omouse_state = 0; //dblclick_flag = 0;
3401         close_x = wx-border_size;
3402         close_y = wy-title_height-border_size;
3403         draw_close_box(close_x,close_y);
3404         #ifdef MACINTOSH
3405                 show_cursor();
3406    #else
3407                 ShowCursorW();
3408         #endif
3409 #endif
3410
3411         while(!done)    {
3412         #ifdef WINDOWS
3413                 MSG msg;
3414
3415                 DoMessageStuff(&msg);
3416
3417                 if (_RedrawScreen) {
3418                         _RedrawScreen = FALSE;
3419
3420                         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
3421                                 gr_free_bitmap(bg.background);
3422                         win_redraw = 1;                 
3423                         goto RePaintNewmenuListbox;
3424                 }
3425
3426                 DDGRRESTORE;
3427         #endif
3428   
3429                 ocitem = citem;
3430                 ofirst_item = first_item;
3431 #if defined(MACINTOSH) || defined(WINDOWS)
3432                 omouse_state = mouse_state;
3433                 mouse_state = mouse_button_state(0);
3434 #endif
3435                 //see if redbook song needs to be restarted
3436                 songs_check_redbook_repeat();
3437
3438                 key = key_inkey();
3439
3440                 if ( listbox_callback )
3441                         redraw = (*listbox_callback)(&citem, &nitems, items, &key );
3442                 else
3443                         redraw = 0;
3444
3445         #ifdef WINDOWS
3446                 if (win_redraw) {
3447                         redraw = 1;
3448                         win_redraw = 0;
3449                 }
3450         #endif
3451
3452                 if ( key<-1 ) {
3453                         citem = key;
3454                         key = -1;
3455                         done = 1;
3456                 }
3457
3458
3459         #ifdef WINDOWS
3460                 if (simukey==-1)
3461                         key=KEY_UP;
3462                 else if (simukey==1)
3463                    key=KEY_DOWN;
3464                 simukey=0;
3465         #endif
3466                 
3467                 switch(key)     {
3468                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
3469                 case KEY_PRINT_SCREEN:          
3470                         MAC(hide_cursor());
3471                         save_screen_shot(0); 
3472                         PA_DFX (pa_set_frontbuffer_current());
3473                         PA_DFX (pa_set_front_to_read());
3474                         
3475                         MAC(show_cursor());
3476                         MAC(key_flush());
3477                         break;
3478                 case KEY_HOME:
3479                 case KEY_PAD7:
3480                         citem = 0;
3481                         break;
3482                 case KEY_END:
3483                 case KEY_PAD1:
3484                         citem = nitems-1;
3485                         break;
3486                 case KEY_UP:
3487                 case KEY_PAD8:
3488                         citem--;                        
3489                         break;
3490                 case KEY_DOWN:
3491                 case KEY_PAD2:
3492                         citem++;                        
3493                         break;
3494                 case KEY_PAGEDOWN:
3495                 case KEY_PAD3:
3496                         citem += LB_ITEMS_ON_SCREEN;
3497                         break;
3498                 case KEY_PAGEUP:
3499                 case KEY_PAD9:
3500                         citem -= LB_ITEMS_ON_SCREEN;
3501                         break;
3502                 case KEY_ESC:
3503                         if (allow_abort_flag) {
3504                                 citem = -1;
3505                                 done = 1;
3506                         }
3507                         break;
3508                 case KEY_ENTER:
3509                 case KEY_PADENTER:
3510                         done = 1;
3511                         break;
3512
3513                 #ifdef MACINTOSH
3514                 case KEY_COMMAND+KEY_Q: {
3515                         extern void macintosh_quit();
3516                         
3517                         if ( !(Game_mode & GM_MULTI) )
3518                                 macintosh_quit();
3519                         show_cursor();
3520                         key_flush();
3521                         break;
3522                 }
3523                 #endif
3524
3525                 default:        
3526                         if ( key > 0 )  {
3527                                 int ascii = key_to_ascii(key);
3528                                 if ( ascii < 255 )      {
3529                                         int cc,cc1;
3530                                         cc=cc1=citem+1;
3531                                         if (cc1 < 0 )  cc1 = 0;
3532                                         if (cc1 >= nitems )  cc1 = 0;
3533                                         while(1) {
3534                                                 if ( cc < 0 ) cc = 0;
3535                                                 if ( cc >= nitems ) cc = 0;
3536                                                 if ( citem == cc ) break;
3537         
3538                                                 if ( toupper( items[cc][0] ) == toupper(ascii) )        {
3539                                                         citem = cc;
3540                                                         break;
3541                                                 }
3542                                                 cc++;
3543                                         }
3544                                 }
3545                         }
3546                 }
3547                 if ( done ) break;
3548
3549                 if (citem<0)
3550                         citem=0;
3551
3552                 if (citem>=nitems)
3553                         citem = nitems-1;
3554
3555                 if (citem< first_item)
3556                         first_item = citem;
3557
3558                 if (citem>=( first_item+LB_ITEMS_ON_SCREEN))
3559                         first_item = citem-LB_ITEMS_ON_SCREEN+1;
3560
3561                 if (nitems <= LB_ITEMS_ON_SCREEN )
3562                          first_item = 0;
3563
3564                 if (first_item>nitems-LB_ITEMS_ON_SCREEN)
3565                         first_item = nitems-LB_ITEMS_ON_SCREEN;
3566                 if (first_item < 0 ) first_item = 0;
3567
3568 #ifdef WINDOWS
3569                 if (nitems>first_item+LB_ITEMS_ON_SCREEN)
3570                         show_down_arrow=1;
3571                 else 
3572                         show_down_arrow=0;
3573                 if (first_item>0)
3574                         show_up_arrow=1;
3575                 else    
3576                         show_up_arrow=0;
3577 #endif
3578
3579
3580 #if defined(MACINTOSH) || defined(WINDOWS)
3581                 WIN(Sleep(100));
3582                 if (mouse_state) {
3583                         int w, h, aw;
3584
3585                         mouse_get_pos(&mx, &my);
3586                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
3587                                 if (i > nitems)
3588                                         break;
3589                                 gr_get_string_size(items[i], &w, &h, &aw  );
3590                                 x1 = wx;
3591                                 x2 = wx + width;
3592                                 y1 = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3593                                 y2 = y1+h+1;
3594                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3595                                         //if (i == citem) {
3596                                         //      break;
3597                                         //}
3598                                         //dblclick_flag= 0;
3599                                         citem = i;
3600                                         done = 1;
3601                                         break;
3602                                 }
3603                         }
3604                 }
3605
3606                 //no double-click stuff for listbox
3607                 //@@if (!mouse_state && omouse_state) {
3608                 //@@    int w, h, aw;
3609                 //@@
3610                 //@@    gr_get_string_size(items[citem], &w, &h, &aw  );
3611                 //@@    mouse_get_pos(&mx, &my);
3612                 //@@    x1 = wx;
3613                 //@@    x2 = wx + width;
3614                 //@@    y1 = (citem-first_item)*(grd_curfont->ft_h+2)+wy;
3615                 //@@    y2 = y1+h+1;
3616                 //@@    if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3617                 //@@            if (dblclick_flag) done = 1;
3618                 //@@    }
3619                 //@@}
3620
3621                 //check for close box clicked
3622                 if ( !mouse_state && omouse_state ) {
3623                         mouse_get_pos(&mx, &my);
3624                         x1 = close_x + CLOSE_X + 2;
3625                         x2 = x1 + CLOSE_SIZE - 2;
3626                         y1 = close_y + CLOSE_Y + 2;
3627                         y2 = y1 + CLOSE_SIZE - 2;
3628                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
3629                                 citem = -1;
3630                                 done = 1;
3631                         }
3632
3633                    #ifdef WINDOWS
3634                         x1 = wx-LHX(10);
3635                         x2 = x1 + LHX(10);
3636                         y1 = wy;
3637                         y2 = wy+LHY(7);
3638                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow) 
3639                                 simukey = -1;
3640                         y1 = total_height-LHY(7);
3641                         y2 = total_height;
3642                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow ) 
3643                                 simukey = 1;
3644                    #endif
3645
3646                         
3647                 }
3648 #endif
3649
3650                 if ( (ofirst_item != first_item) || redraw)     {
3651                         MAC(hide_cursor());
3652                         WIN(HideCursorW());
3653                         WIN(DDGRLOCK(dd_grd_curcanv));
3654
3655                         gr_setcolor( BM_XRGB( 0,0,0)  );
3656                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
3657                                 int w, h, aw, y;
3658                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3659                                 if ( i >= nitems )      {
3660                                         gr_setcolor( BM_XRGB(0,0,0));
3661                                         gr_rect( wx, y-1, wx+width-1, y+grd_curfont->ft_h + 1 );
3662                                 } else {
3663                                         if ( i == citem )       
3664                                                 grd_curcanv->cv_font = SELECTED_FONT;
3665                                         else    
3666                                                 grd_curcanv->cv_font = NORMAL_FONT;
3667                                         gr_get_string_size(items[i], &w, &h, &aw  );
3668                                         gr_rect( wx, y-1, wx+width-1, y+h+1 );
3669                                         gr_string( wx+5, y, items[i]  );
3670                                 }
3671                         }               
3672
3673                                 
3674                         // If Win95 port, draw up/down arrows on left side of menu
3675                         #ifdef WINDOWS   
3676                                 grd_curcanv->cv_font = NORMAL_FONT;
3677                         if (show_up_arrow)
3678                                 gr_string( wx-LHX(10), wy ,UP_ARROW_MARKER );
3679                         else
3680                         {
3681                                 No_darkening=1;
3682                                 nm_draw_background (wx-LHX(10),wy,wx-2,wy+LHY(7));
3683                                 No_darkening=0;
3684                         }
3685
3686                         if (show_down_arrow)
3687                         gr_string( wx-LHX(10), wy+total_height-LHY(7) ,DOWN_ARROW_MARKER );
3688                         else
3689                         {
3690                                 No_darkening=1;
3691                                 nm_draw_background (wx-LHX(10),wy+total_height-LHY(7),wx-2,wy+total_height);
3692                                 No_darkening=0;
3693                         }
3694
3695                         #endif
3696
3697
3698                         WIN(DDGRUNLOCK(dd_grd_curcanv));
3699                         WIN(ShowCursorW());
3700                         MAC(show_cursor());
3701                         gr_update();
3702                 } else if ( citem != ocitem )   {
3703                         int w, h, aw, y;
3704
3705                         MAC(hide_cursor());
3706                         WIN(HideCursorW());
3707
3708                         WIN(DDGRLOCK(dd_grd_curcanv));
3709
3710                         i = ocitem;
3711                         if ( (i>=0) && (i<nitems) )     {
3712                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3713                                 if ( i == citem )       
3714                                         grd_curcanv->cv_font = SELECTED_FONT;
3715                                 else    
3716                                         grd_curcanv->cv_font = NORMAL_FONT;
3717                                 gr_get_string_size(items[i], &w, &h, &aw  );
3718                                 gr_rect( wx, y-1, wx+width-1, y+h+1 );
3719                                 gr_string( wx+5, y, items[i]  );
3720
3721                         }
3722                         i = citem;
3723                         if ( (i>=0) && (i<nitems) )     {
3724                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
3725                                 if ( i == citem )       
3726                                         grd_curcanv->cv_font = SELECTED_FONT;
3727                                 else    
3728                                         grd_curcanv->cv_font = NORMAL_FONT;
3729                                 gr_get_string_size( items[i], &w, &h, &aw  );
3730                                 gr_rect( wx, y-1, wx+width-1, y+h );
3731                                 gr_string( wx+5, y, items[i]  );
3732                         }
3733                         WIN(DDGRUNLOCK(dd_grd_curcanv));
3734
3735                         WIN(ShowCursorW());
3736                         MAC(show_cursor());
3737                         gr_update();
3738                 }
3739         }
3740         MAC(hide_cursor());
3741         WIN(HideCursorW());     
3742
3743         keyd_repeat = old_keyd_repeat;
3744
3745         WIN (DDGRLOCK(dd_grd_curcanv));
3746         gr_bm_bitblt(total_width,total_height, wx-border_size, wy-title_height-border_size, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
3747         WIN (DDGRUNLOCK(dd_grd_curcanv));       
3748
3749         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
3750                 gr_free_bitmap(bg.background);
3751
3752 #if 0
3753         WINDOS(
3754                 dd_gr_blt_notrans(dd_VR_offscreen_buffer,
3755                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh,
3756                                 dd_grd_curcanv,
3757                                 0,0,_DDModeList[W95DisplayMode].rw, _DDModeList[W95DisplayMode].rh),
3758                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) )
3759         );
3760 #endif
3761
3762         WIN(DDGRRESTORE);
3763
3764         WIN(mouse_set_mode(1));                         //re-enable centering mode
3765
3766         return citem;
3767 }
3768
3769 int newmenu_filelist( char * title, char * filespec, char * filename )
3770 {
3771         int i, NumFiles;
3772         char * Filenames[MAX_FILES];
3773         char FilenameText[MAX_FILES][14];
3774         FILEFINDSTRUCT find;
3775
3776         NumFiles = 0;
3777         if( !FileFindFirst( filespec, &find ) ) {
3778                 do      {
3779                         if (NumFiles<MAX_FILES) {
3780                                 strncpy( FilenameText[NumFiles], find.name, FILENAME_LEN);
3781                                 Filenames[NumFiles] = FilenameText[NumFiles];
3782                                 NumFiles++;
3783                         } else {
3784                                 break;
3785                         }
3786                 } while( !FileFindNext( &find ) );
3787                 FileFindClose();
3788         }
3789
3790         i = newmenu_listbox( title, NumFiles, Filenames, 1, NULL );
3791         if ( i > -1 )   {
3792                 strcpy( filename, Filenames[i] );
3793                 return 1;
3794         } 
3795         return 0;
3796 }
3797
3798 //added on 10/14/98 by Victor Rachels to attempt a fixedwidth font messagebox
3799 int nm_messagebox_fixedfont( char *title, int nchoices, ... )
3800 {
3801         int i;
3802         char * format;
3803         va_list args;
3804         char *s;
3805         char nm_text[MESSAGEBOX_TEXT_SIZE];
3806         newmenu_item nm_message_items[5];
3807
3808         va_start(args, nchoices );
3809
3810         Assert( nchoices <= 5 );
3811
3812         for (i=0; i<nchoices; i++ )     {
3813                 s = va_arg( args, char * );
3814                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
3815         }
3816         format = va_arg( args, char * );
3817         //sprintf(        nm_text, "" ); // adb: ?
3818         vsprintf(nm_text,format,args);
3819         va_end(args);
3820
3821         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
3822
3823         return newmenu_do_fixedfont( title, nm_text, nchoices, nm_message_items, NULL, 0, NULL, -1, -1 );
3824 }
3825 //end this section addition - Victor Rachels
3826
3827 #ifdef NETWORK
3828 extern netgame_info Active_games[];
3829 extern int NumActiveNetgames;
3830
3831 void show_extra_netgame_info(int choice)
3832  {
3833         newmenu_item m[5];
3834    char mtext[5][50];
3835         int i,num=0;
3836
3837         if (choice>=NumActiveNetgames)
3838                 return;
3839         
3840    for (i=0;i<5;i++)
3841         {
3842          m[i].text=(char *)&mtext[i];
3843     m[i].type=NM_TYPE_TEXT;             
3844         }
3845
3846    sprintf (mtext[num],"Game: %s",Active_games[choice].game_name); num++;
3847    sprintf (mtext[num],"Mission: %s",Active_games[choice].mission_title); num++;
3848         sprintf (mtext[num],"Current Level: %d",Active_games[choice].levelnum); num++;
3849         sprintf (mtext[num],"Difficulty: %s",MENU_DIFFICULTY_TEXT(Active_games[choice].difficulty)); num++;
3850
3851         already_showing_info=1; 
3852         newmenu_dotiny2( NULL, "Netgame Information", num, m, NULL);
3853         already_showing_info=0; 
3854  }
3855
3856 #endif // NETWORK
3857
3858 /* Spiffy word wrap string formatting function */
3859
3860 void nm_wrap_text(char *dbuf, char *sbuf, int line_length)
3861 {
3862         int col;
3863         char *wordptr;
3864         char *tbuf;
3865
3866         tbuf = (char *)d_malloc(strlen(sbuf)+1);
3867         strcpy(tbuf, sbuf);
3868
3869         wordptr = strtok(tbuf, " ");
3870         if (!wordptr) return;
3871         col = 0;
3872         dbuf[0] = 0;
3873
3874         while (wordptr)
3875         {
3876                 col = col+strlen(wordptr)+1;
3877                 if (col >=line_length) {
3878                         col = 0;
3879                         sprintf(dbuf, "%s\n%s ", dbuf, wordptr);
3880                 }
3881                 else {
3882                         sprintf(dbuf, "%s%s ", dbuf, wordptr);
3883                 }
3884                 wordptr = strtok(NULL, " ");
3885         }
3886
3887         d_free(tbuf);
3888 }
3889                                 
3890         
3891
3892
3893