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