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