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