]> icculus.org git repositories - btb/d2x.git/blob - main/newmenu.c
move PhysicsFS initialisation, search path setup and argument reading to physfsx.h
[btb/d2x.git] / main / newmenu.c
1 /* $Id: newmenu.c,v 1.33 2006-02-24 06:19:20 chris 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  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #if !defined(_MSC_VER) && !defined(macintosh)
31 #include <unistd.h>
32 #endif
33 #include <limits.h>
34
35 #include <physfs.h>
36
37 #include "error.h"
38 #include "pstypes.h"
39 #include "gr.h"
40 #include "mono.h"
41 #include "songs.h"
42 #include "key.h"
43 #include "palette.h"
44 #include "game.h"
45 #include "text.h"
46 #include "menu.h"
47 #include "newmenu.h"
48 #include "gamefont.h"
49 #include "gamepal.h"
50 #ifdef NETWORK
51 #include "network.h"
52 #endif
53 #include "iff.h"
54 #include "pcx.h"
55 #include "u_mem.h"
56 #include "mouse.h"
57 #include "joy.h"
58 #include "digi.h"
59
60 #include "multi.h"
61 #include "endlevel.h"
62 #include "screens.h"
63 #include "config.h"
64 #include "player.h"
65 #include "newdemo.h"
66 #include "kconfig.h"
67 #include "strutil.h"
68
69 #ifdef MACINTOSH
70 #include <Events.h>
71 #endif
72
73 #if defined (TACTILE)
74  #include "tactile.h"
75 #endif
76
77 #define MAXDISPLAYABLEITEMS 15
78
79 #define LHX(x)      ((x)*(MenuHires?2:1))
80 #define LHY(y)      ((y)*(MenuHires?2.4:1))
81
82 #define TITLE_FONT      HUGE_FONT
83 #define NORMAL_FONT     MEDIUM1_FONT    //normal, non-highlighted item
84 #define SELECTED_FONT   MEDIUM2_FONT    //highlighted item
85 #define SUBTITLE_FONT   MEDIUM3_FONT
86
87 #define NORMAL_CHECK_BOX    "\81"
88 #define CHECKED_CHECK_BOX   "\82"
89
90 #define NORMAL_RADIO_BOX    "\7f"
91 #define CHECKED_RADIO_BOX   "\80"
92 #define CURSOR_STRING       "_"
93 #define SLIDER_LEFT         "\83"  // 131
94 #define SLIDER_RIGHT        "\84"  // 132
95 #define SLIDER_MIDDLE       "\85"  // 133
96 #define SLIDER_MARKER       "\86"  // 134
97 #define UP_ARROW_MARKER     "\87"  // 135
98 #define DOWN_ARROW_MARKER   "\88"  // 136
99
100 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 );
101 void show_extra_netgame_info(int choice);
102
103
104 int Newmenu_first_time = 1;
105 //--unused-- int Newmenu_fade_in = 1;
106
107 typedef struct bkg {
108         grs_canvas * menu_canvas;
109         grs_bitmap * saved;                     // The background under the menu.
110         grs_bitmap * background;
111 } bkg;
112
113 grs_bitmap nm_background,nm_background_save;
114
115 #define MESSAGEBOX_TEXT_SIZE 2176   // How many characters in messagebox (changed form 300 (fixes crash from show_game_score and friends) - 2000/01/18 Matt Mueller)
116 #define MAX_TEXT_WIDTH  200                             // How many pixels wide a input box can be
117
118 ubyte MenuReordering=0;
119 ubyte SurfingNet=0;
120 char Pauseable_menu=0;
121 char already_showing_info=0;
122
123
124 void newmenu_close()    {
125         if ( nm_background.bm_data )
126                 d_free(nm_background.bm_data);
127
128         if ( nm_background_save.bm_data )
129                 d_free(nm_background_save.bm_data);
130         Newmenu_first_time = 1;
131 }
132
133 ubyte background_palette[768];
134
135 //should be called whenever the palette changes
136 void nm_remap_background()
137 {
138         if (!Newmenu_first_time) {
139                 if (!nm_background.bm_data)
140                         nm_background.bm_data = d_malloc(nm_background.bm_w * nm_background.bm_h);
141
142                 memcpy(nm_background.bm_data,nm_background_save.bm_data,nm_background.bm_w * nm_background.bm_h);
143
144                 gr_remap_bitmap_good( &nm_background, background_palette, -1, -1 );
145         }
146 }
147
148 #include <math.h>
149
150 extern char last_palette_loaded[];
151
152 void nm_draw_background1(char * filename)
153 {
154         int pcx_error;
155         grs_bitmap *bmp;
156         ubyte pal[256*3];
157         int width, height;
158
159         //@@//I think this only gets called to fill the whole screen
160         //@@Assert(grd_curcanv->cv_bitmap.bm_w == 320);
161         //@@Assert(grd_curcanv->cv_bitmap.bm_h == 200);
162
163         pcx_error = pcx_get_dimensions(filename, &width, &height);
164         if (pcx_error != PCX_ERROR_NONE)
165                 Error("Could not open pcx file <%s>\n", filename);
166
167         bmp = gr_create_bitmap(width, height);
168
169         pcx_error = pcx_read_bitmap(filename,bmp,bmp->bm_type,pal);
170         Assert(pcx_error == PCX_ERROR_NONE);
171
172         //@@gr_remap_bitmap_good( bmp, pal, -1, -1 );
173
174
175         {       //remap stuff. this code is kindof a hack
176
177                 //now, before we bring up the menu, we need to
178                 //do some stuff to make sure the palette is ok.  First, we need to
179                 //get our current palette into the 2d's array, so the remapping will
180                 //work.  Second, we need to remap the fonts.  Third, we need to fill
181                 //in part of the fade tables so the darkening of the menu edges works
182
183                 gr_copy_palette(gr_palette, pal, sizeof(gr_palette));
184 #ifdef OGL
185                 gr_palette_load(gr_palette);
186 #endif
187                 remap_fonts_and_menus(1);
188
189         }
190
191         show_fullscr(bmp);
192
193         gr_free_bitmap(bmp);
194
195         strcpy(last_palette_loaded,"");         //force palette load next time
196
197 }
198
199 #define MENU_BACKGROUND_BITMAP_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
200 #define MENU_BACKGROUND_BITMAP_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
201
202 #define MENU_BACKGROUND_BITMAP (MenuHires?MENU_BACKGROUND_BITMAP_HIRES:MENU_BACKGROUND_BITMAP_LORES)
203
204 int Background_hires;
205 int No_darkening=0;
206
207 void nm_draw_background(int x1, int y1, int x2, int y2 )
208 {
209         int w,h;
210
211         if (Newmenu_first_time || MenuHires!=Background_hires)  {
212                 int pcx_error;
213
214                 if (Newmenu_first_time) {
215                         atexit( newmenu_close );
216                         Newmenu_first_time = 0;
217                         nm_background_save.bm_data=NULL;                
218                 }
219                 else {
220                         if (nm_background_save.bm_data)
221                                 d_free(nm_background_save.bm_data);
222                         if (nm_background.bm_data)
223                                 d_free(nm_background.bm_data);
224                 }
225
226                 pcx_error = pcx_read_bitmap(MENU_BACKGROUND_BITMAP,&nm_background_save,BM_LINEAR,background_palette);
227                 Assert(pcx_error == PCX_ERROR_NONE);
228
229                 nm_background = nm_background_save;
230                 nm_background.bm_data=NULL;             
231                 nm_remap_background();
232
233                 Background_hires = MenuHires;
234         }
235
236         if ( x1 < 0 ) x1 = 0;
237         if ( y1 < 0 ) y1 = 0;
238
239         w = x2-x1+1;
240         h = y2-y1+1;
241
242         //if ( w > nm_background.bm_w ) w = nm_background.bm_w;
243         //if ( h > nm_background.bm_h ) h = nm_background.bm_h;
244         
245         x2 = x1 + w - 1;
246         y2 = y1 + h - 1;
247
248 #if 0
249         {
250                 grs_bitmap *tmp = gr_create_bitmap(w, h);
251
252                 gr_bitmap_scale_to(&nm_background, tmp);
253
254                 if (No_darkening)
255                         gr_bm_bitblt(w, h, x1, y1, LHX(10), LHY(10), tmp, &(grd_curcanv->cv_bitmap) );
256                 else
257                         gr_bm_bitblt(w, h, x1, y1, 0, 0, tmp, &(grd_curcanv->cv_bitmap) );
258                 gr_free_bitmap(tmp);
259         }
260 #else
261         if (No_darkening)
262                 gr_bm_bitblt(w, h, x1, y1, LHX(10), LHY(10), &nm_background, &(grd_curcanv->cv_bitmap) );
263         else
264                 gr_bm_bitblt(w, h, x1, y1, 0, 0, &nm_background, &(grd_curcanv->cv_bitmap) );
265 #endif
266
267         if (!No_darkening) {
268                 Gr_scanline_darkening_level = 2*7;
269
270                 gr_setcolor( BM_XRGB(0,0,0) );
271                 gr_urect( x2-5, y1+5, x2-5, y2-5 );
272                 gr_urect( x2-4, y1+4, x2-4, y2-5 );
273                 gr_urect( x2-3, y1+3, x2-3, y2-5 );
274                 gr_urect( x2-2, y1+2, x2-2, y2-5 );
275                 gr_urect( x2-1, y1+1, x2-1, y2-5 );
276                 gr_urect( x2+0, y1+0, x2-0, y2-5 );
277
278                 gr_urect( x1+5, y2-5, x2, y2-5 );
279                 gr_urect( x1+4, y2-4, x2, y2-4 );
280                 gr_urect( x1+3, y2-3, x2, y2-3 );
281                 gr_urect( x1+2, y2-2, x2, y2-2 );
282                 gr_urect( x1+1, y2-1, x2, y2-1 );
283                 gr_urect( x1+0, y2, x2, y2-0 );
284         }
285
286         Gr_scanline_darkening_level = GR_FADE_LEVELS;
287 }
288
289 void nm_restore_background( int x, int y, int w, int h )
290 {
291         int x1, x2, y1, y2;
292
293         x1 = x; x2 = x+w-1;
294         y1 = y; y2 = y+h-1;
295
296         if ( x1 < 0 ) x1 = 0;
297         if ( y1 < 0 ) y1 = 0;
298
299         if ( x2 >= nm_background.bm_w ) x2=nm_background.bm_w-1;
300         if ( y2 >= nm_background.bm_h ) y2=nm_background.bm_h-1;
301
302         w = x2 - x1 + 1;
303         h = y2 - y1 + 1;
304
305         gr_bm_bitblt(w, h, x1, y1, x1, y1, &nm_background, &(grd_curcanv->cv_bitmap) );
306 }
307
308 // Draw a left justfied string
309 void nm_string( bkg * b, int w1,int x, int y, char * s)
310 {
311         int w,h,aw,tx=0,t=0,i;
312         char *p,*s1,*s2,measure[2];
313         int XTabs[]={15,87,124,162,228,253};
314    
315         p=s1=NULL;
316         s2 = d_strdup(s);
317
318         for (i=0;i<6;i++) {
319                 XTabs[i]=(LHX(XTabs[i]));
320                 XTabs[i]+=x;
321         }
322  
323         measure[1]=0;
324
325         if (!SurfingNet) {
326                 p = strchr( s2, '\t' );
327                 if (p && (w1>0) ) {
328                         *p = '\0';
329                         s1 = p+1;
330                 }
331         }
332
333         gr_get_string_size(s2, &w, &h, &aw  );
334
335         if (w1 > 0)
336                 w = w1;
337
338         // CHANGED
339         gr_bm_bitblt(b->background->bm_w-15, h+2, 5, y-1, 5, y-1, b->background, &(grd_curcanv->cv_bitmap) );
340         //gr_bm_bitblt(w, h, x, y, x, y, b->background, &(grd_curcanv->cv_bitmap) );
341
342         if (SurfingNet) {
343                 for (i=0;i<strlen(s2);i++) {
344                         if (s2[i]=='\t' && SurfingNet) {
345                                 x=XTabs[t];
346                                 t++;
347                                 continue;
348                         }
349                         measure[0]=s2[i];
350                         gr_get_string_size(measure,&tx,&h,&aw);
351                         gr_string(x,y,measure);
352                         x+=tx;
353                 }
354         }
355         else
356                 gr_string (x,y,s2);
357          
358         if (!SurfingNet && p && (w1>0) ) {
359                 gr_get_string_size(s1, &w, &h, &aw  );
360
361                 gr_string( x+w1-w, y, s1 );
362
363                 *p = '\t';
364         }
365         d_free(s2);
366 }
367
368 // Draw a slider and it's string
369 void nm_string_slider( bkg * b, int w1,int x, int y, char * s )
370 {
371         int w,h,aw;
372         char *p,*s1;
373
374         s1=NULL;
375
376         p = strchr( s, '\t' );
377         if (p)  {
378                 *p = '\0';
379                 s1 = p+1;
380         }
381
382         gr_get_string_size(s, &w, &h, &aw  );
383         // CHANGED
384
385                 gr_bm_bitblt(b->background->bm_w-15, h, 5, y, 5, y, b->background, &(grd_curcanv->cv_bitmap) );
386                 //gr_bm_bitblt(w, h, x, y, x, y, b->background, &(grd_curcanv->cv_bitmap) );
387
388                 gr_string( x, y, s );
389
390                 if (p)  {
391                         gr_get_string_size(s1, &w, &h, &aw  );
392
393                         // CHANGED
394                         gr_bm_bitblt(w, 1, x+w1-w, y, x+w1-w, y, b->background, &(grd_curcanv->cv_bitmap) );
395                         // CHANGED
396                         gr_bm_bitblt(w, 1, x+w1-w, y+h-1, x+w1-w, y, b->background, &(grd_curcanv->cv_bitmap) );
397
398                         gr_string( x+w1-w, y, s1 );
399
400                         *p = '\t';
401                 }
402 }
403
404
405 // Draw a left justfied string with black background.
406 void nm_string_black( bkg * b, int w1,int x, int y, char * s )
407 {
408         int w,h,aw;
409         gr_get_string_size(s, &w, &h, &aw  );
410         b = b;                                  
411         if (w1 == 0) w1 = w;
412
413                 gr_setcolor( BM_XRGB(2,2,2) );
414                 gr_rect( x-1, y-1, x-1, y+h-1 );
415                 gr_rect( x-1, y-1, x+w1-1, y-1 );
416
417          
418                 gr_setcolor( BM_XRGB(5,5,5) );
419                 gr_rect( x, y+h, x+w1, y+h);
420                 gr_rect( x+w1, y-1, x+w1, y+h );
421      
422                 gr_setcolor( BM_XRGB(0,0,0) );
423                 gr_rect( x, y, x+w1-1, y+h-1 );
424         
425                 gr_string( x+1, y+1, s );
426 }
427
428
429 // Draw a right justfied string
430 void nm_rstring( bkg * b,int w1,int x, int y, char * s )
431 {
432         int w,h,aw;
433         gr_get_string_size(s, &w, &h, &aw  );
434         x -= 3;
435
436         if (w1 == 0) w1 = w;
437
438         //mprintf( 0, "Width = %d, string='%s'\n", w, s );
439
440         // CHANGED
441         gr_bm_bitblt(w1, h, x-w1, y, x-w1, y, b->background, &(grd_curcanv->cv_bitmap) );
442         gr_string( x-w, y, s );
443 }
444
445 #include "timer.h"
446
447 //for text items, constantly redraw cursor (to achieve flash)
448 void update_cursor( newmenu_item *item)
449 {
450         int w,h,aw;
451         fix time = timer_get_approx_seconds();
452         int x,y;
453         char * text = item->text;
454
455         Assert(item->type==NM_TYPE_INPUT_MENU || item->type==NM_TYPE_INPUT);
456
457         while( *text )  {
458                 gr_get_string_size(text, &w, &h, &aw  );
459                 if ( w > item->w-10 )
460                         text++;
461                 else
462                         break;
463         }
464         if (*text==0) 
465                 w = 0;
466         x = item->x+w; y = item->y;
467
468         if (time & 0x8000)
469                 gr_string( x, y, CURSOR_STRING );
470         else {
471                 gr_setcolor( BM_XRGB(0,0,0) );
472                 gr_rect( x, y, x+grd_curcanv->cv_font->ft_w-1, y+grd_curcanv->cv_font->ft_h-1 );
473         }
474 }
475
476 void nm_string_inputbox( bkg *b, int w, int x, int y, char * text, int current )
477 {
478         int w1,h1,aw;
479
480         while( *text )  {
481                 gr_get_string_size(text, &w1, &h1, &aw  );
482                 if ( w1 > w-10 )
483                         text++;
484                 else
485                         break;
486         }
487         if ( *text == 0 )
488                 w1 = 0;
489
490    nm_string_black( b, w, x, y, text );
491                 
492         if ( current )  {
493                 gr_string( x+w1+1, y, CURSOR_STRING );
494         }
495 }
496
497 void draw_item( bkg * b, newmenu_item *item, int is_current,int tiny )
498 {
499        if (tiny)
500         {
501          if (is_current)
502           gr_set_fontcolor(gr_find_closest_color_current(57,49,20),-1);
503          else
504           gr_set_fontcolor(gr_find_closest_color_current(29,29,47),-1);
505
506          if (item->text[0]=='\t')
507           gr_set_fontcolor (gr_find_closest_color_current(63,63,63),-1);
508         }
509        else
510         {
511          if (is_current)
512           grd_curcanv->cv_font = SELECTED_FONT;
513          else
514           grd_curcanv->cv_font = NORMAL_FONT;
515
516                 #ifdef WINDOWS
517                         if (is_current && item->type == NM_TYPE_TEXT) 
518                                 grd_curcanv->cv_font = NORMAL_FONT;
519                 #endif
520         }
521
522         switch( item->type )    {
523         case NM_TYPE_TEXT:
524       // grd_curcanv->cv_font=TEXT_FONT;
525                 // fall through on purpose
526
527         case NM_TYPE_MENU:
528                 nm_string( b, item->w, item->x, item->y, item->text );
529                 break;
530         case NM_TYPE_SLIDER:    {
531                 int j;
532                 if (item->value < item->min_value) item->value=item->min_value;
533                 if (item->value > item->max_value) item->value=item->max_value;
534                 sprintf( item->saved_text, "%s\t%s", item->text, SLIDER_LEFT );
535                 for (j=0; j<(item->max_value-item->min_value+1); j++ )  {
536                         sprintf( item->saved_text, "%s%s", item->saved_text,SLIDER_MIDDLE );
537                 }
538                 sprintf( item->saved_text, "%s%s", item->saved_text,SLIDER_RIGHT );
539                 
540                 item->saved_text[item->value+1+strlen(item->text)+1] = SLIDER_MARKER[0];
541                 
542                 nm_string_slider( b, item->w, item->x, item->y, item->saved_text );
543                 }
544                 break;
545         case NM_TYPE_INPUT_MENU:
546                 if ( item->group==0 )           {
547                         nm_string( b, item->w, item->x, item->y, item->text );
548                 } else {
549                         nm_string_inputbox( b, item->w, item->x, item->y, item->text, is_current );
550                 }
551                 break;
552         case NM_TYPE_INPUT:
553                 nm_string_inputbox( b, item->w, item->x, item->y, item->text, is_current );
554                 break;
555         case NM_TYPE_CHECK:
556                 nm_string( b, item->w, item->x, item->y, item->text );
557                 if (item->value)
558                         nm_rstring( b,item->right_offset,item->x, item->y, CHECKED_CHECK_BOX );
559                 else                                                                                                              
560                         nm_rstring( b,item->right_offset,item->x, item->y, NORMAL_CHECK_BOX );
561                 break;
562         case NM_TYPE_RADIO:
563                 nm_string( b, item->w, item->x, item->y, item->text );
564                 if (item->value)
565                         nm_rstring( b,item->right_offset, item->x, item->y, CHECKED_RADIO_BOX );
566                 else
567                         nm_rstring( b,item->right_offset, item->x, item->y, NORMAL_RADIO_BOX );
568                 break;
569         case NM_TYPE_NUMBER:    {
570                 char text[10];
571                 if (item->value < item->min_value) item->value=item->min_value;
572                 if (item->value > item->max_value) item->value=item->max_value;
573                 nm_string( b, item->w, item->x, item->y, item->text );
574                 sprintf( text, "%d", item->value );
575                 nm_rstring( b,item->right_offset,item->x, item->y, text );
576                 }
577                 break;
578         }
579 }
580
581 char *Newmenu_allowed_chars=NULL;
582
583 //returns true if char is allowed
584 int char_allowed(char c)
585 {
586         char *p = Newmenu_allowed_chars;
587
588         if (!p)
589                 return 1;
590
591         while (*p) {
592                 Assert(p[1]);
593
594                 if (c>=p[0] && c<=p[1])
595                         return 1;
596
597                 p += 2;
598         }
599
600         return 0;
601 }
602
603 void strip_end_whitespace( char * text )
604 {
605         int i,l;
606         l = strlen( text );
607         for (i=l-1; i>=0; i-- ) {
608                 if ( isspace(text[i]) )
609                         text[i] = 0;
610                 else
611                         return;
612         }
613 }
614
615 int newmenu_do( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
616 {
617         return newmenu_do3( title, subtitle, nitems, item, subfunction, 0, NULL, -1, -1 );
618 }
619 int newmenu_dotiny( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
620 {
621         return newmenu_do4( title, subtitle, nitems, item, subfunction, 0, NULL, LHX(310), -1, 1 );
622 }
623
624
625 int newmenu_dotiny2( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) )
626 {
627         return newmenu_do4( title, subtitle, nitems, item, subfunction, 0, NULL, -1, -1, 1 );
628 }
629
630
631 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 )
632 {
633         return newmenu_do3( title, subtitle, nitems, item, subfunction, citem, NULL, -1, -1 );
634 }
635
636
637 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 )
638 {
639         return newmenu_do3( title, subtitle, nitems, item, subfunction, citem, filename, -1, -1 );
640 }
641 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 )
642  {
643   return newmenu_do4( title, subtitle, nitems, item, subfunction, citem, filename, width, height,0 );
644  }
645
646 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){
647         set_screen_mode(SCREEN_MENU);//hafta set the screen mode before calling or fonts might get changed/freed up if screen res changes
648 //      return newmenu_do3_real( title, subtitle, nitems, item, subfunction, citem, filename, width,height, GAME_FONT, GAME_FONT, GAME_FONT, GAME_FONT);
649         return newmenu_do4( title, subtitle, nitems, item, subfunction, citem, filename, width,height, 0);
650 }
651
652 //returns 1 if a control device button has been pressed
653 int check_button_press()
654 {
655         int i;
656
657         switch (Config_control_type) {
658         case    CONTROL_JOYSTICK:
659         case    CONTROL_FLIGHTSTICK_PRO:
660         case    CONTROL_THRUSTMASTER_FCS:
661         case    CONTROL_GRAVIS_GAMEPAD:
662                 for (i=0; i<4; i++ )    
663                         if (joy_get_button_down_cnt(i)>0) return 1;
664                 break;
665         case    CONTROL_MOUSE:
666         case    CONTROL_CYBERMAN:
667 #ifndef NEWMENU_MOUSE   // don't allow mouse to continue from menu
668                 for (i=0; i<3; i++ )    
669                         if (mouse_button_down_count(i)>0) return 1;
670                 break;
671 #endif
672         case    CONTROL_WINJOYSTICK:
673         #ifdef WINDOWS  
674                 for (i=0; i<4; i++ )    
675                         if (joy_get_button_down_cnt(i)>0) return 1;
676         #endif  
677                 break;
678         case    CONTROL_NONE:           //keyboard only
679                 #ifdef APPLE_DEMO
680                         if (key_checkch())      return 1;                       
681                 #endif
682
683                 break;
684         default:
685                 Error("Bad control type (Config_control_type):%i",Config_control_type);
686         }
687
688         return 0;
689 }
690
691 extern int network_request_player_names(int);
692 extern int RestoringMenu;
693
694 #ifdef NEWMENU_MOUSE
695 ubyte Hack_DblClick_MenuMode=0;
696 #endif
697
698 #ifdef MACINTOSH
699 extern ubyte joydefs_calibrating;
700 #else
701 # define joydefs_calibrating 0
702 #endif
703
704 #define CLOSE_X     (MenuHires?15:7)
705 #define CLOSE_Y     (MenuHires?15:7)
706 #define CLOSE_SIZE  (MenuHires?10:5)
707
708 void draw_close_box(int x,int y)
709 {
710         gr_setcolor( BM_XRGB(0, 0, 0) );
711         gr_rect(x + CLOSE_X, y + CLOSE_Y, x + CLOSE_X + CLOSE_SIZE, y + CLOSE_Y + CLOSE_SIZE);
712         gr_setcolor( BM_XRGB(21, 21, 21) );
713         gr_rect(x + CLOSE_X + LHX(1), y + CLOSE_Y + LHX(1), x + CLOSE_X + CLOSE_SIZE - LHX(1), y + CLOSE_Y + CLOSE_SIZE - LHX(1));
714 }
715
716 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 )
717 {
718         int old_keyd_repeat, done;
719         int  choice,old_choice,i,j,x,y,w,h,aw, tw, th, twidth,fm,right_offset;
720         int k, nmenus, nothers,ScrollOffset=0,LastScrollCheck=-1,MaxDisplayable,sx,sy;
721         grs_font * save_font;
722         int string_width, string_height, average_width;
723         int ty;
724         bkg bg;
725         int all_text=0;         //set true if all text items
726         int sound_stopped=0,time_stopped=0;
727    int TopChoice,IsScrollBox=0;   // Is this a scrolling box? Set to false at init
728    char *Temp,TempVal;
729         int dont_restore=0;
730    int MaxOnMenu=MAXDISPLAYABLEITEMS;
731         grs_canvas *save_canvas;        
732 #ifdef NEWMENU_MOUSE
733         int mouse_state, omouse_state, dblclick_flag=0;
734         int mx=0, my=0, x1 = 0, x2, y1, y2;
735         int close_box=0;
736 #endif
737 #ifdef MACINTOSH
738         EventRecord event;              // looking for disk inserted events for CD mounts
739 #endif
740
741         WIN(if (!_AppActive) return -1);                // Don't draw message if minimized!
742         newmenu_hide_cursor();
743
744         if (nitems < 1 )
745     {
746                 return -1;
747     } 
748
749         MaxDisplayable=nitems;
750
751         //set_screen_mode(SCREEN_MENU);
752         set_popup_screen();
753
754         if ( Function_mode == FMODE_GAME && !(Game_mode & GM_MULTI)) {
755                 digi_pause_digi_sounds();
756                 sound_stopped = 1;
757         }
758
759         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
760         {
761                 time_stopped = 1;
762                 stop_time();
763                 #ifdef TACTILE 
764                   if (TactileStick)     
765                           DisableForces();      
766                 #endif
767         }
768
769         save_canvas = grd_curcanv;
770
771         gr_set_current_canvas(NULL);
772
773         save_font = grd_curcanv->cv_font;
774
775         tw = th = 0;
776
777         if ( title )    {
778                 grd_curcanv->cv_font = TITLE_FONT;
779                 gr_get_string_size(title,&string_width,&string_height,&average_width );
780                 tw = string_width;
781                 th = string_height;
782         }
783         if ( subtitle ) {
784                 grd_curcanv->cv_font = SUBTITLE_FONT;
785                 gr_get_string_size(subtitle,&string_width,&string_height,&average_width );
786                 if (string_width > tw )
787                         tw = string_width;
788                 th += string_height;
789         }
790
791         th += LHY(8);           //put some space between titles & body
792
793         if (TinyMode)
794         grd_curcanv->cv_font = SMALL_FONT;
795         else 
796         grd_curcanv->cv_font = NORMAL_FONT;
797
798         w = aw = 0;
799         h = th;
800         nmenus = nothers = 0;
801
802         // Find menu height & width (store in w,h)
803         for (i=0; i<nitems; i++ )       {
804                 item[i].redraw=1;
805                 item[i].y = h;
806                 gr_get_string_size(item[i].text,&string_width,&string_height,&average_width );
807                 item[i].right_offset = 0;
808                 
809                 if (SurfingNet)
810                         string_height+=LHY(3);
811
812                 item[i].saved_text[0] = '\0';
813
814                 if ( item[i].type == NM_TYPE_SLIDER )   {
815                         int w1,h1,aw1;
816                         nothers++;
817                         sprintf( item[i].saved_text, "%s", SLIDER_LEFT );
818                         for (j=0; j<(item[i].max_value-item[i].min_value+1); j++ )      {
819                                 sprintf( item[i].saved_text, "%s%s", item[i].saved_text,SLIDER_MIDDLE );
820                         }
821                         sprintf( item[i].saved_text, "%s%s", item[i].saved_text,SLIDER_RIGHT );
822                         gr_get_string_size(item[i].saved_text,&w1,&h1,&aw1 );
823                         string_width += w1 + aw;
824                 }
825
826                 if ( item[i].type == NM_TYPE_MENU )     {
827                         nmenus++;
828                 }
829
830                 if ( item[i].type == NM_TYPE_CHECK )    {
831                         int w1,h1,aw1;
832                         nothers++;
833                         gr_get_string_size(NORMAL_CHECK_BOX, &w1, &h1, &aw1  );
834                         item[i].right_offset = w1;
835                         gr_get_string_size(CHECKED_CHECK_BOX, &w1, &h1, &aw1  );
836                         if (w1 > item[i].right_offset)
837                                 item[i].right_offset = w1;
838                 }
839                 
840                 if (item[i].type == NM_TYPE_RADIO ) {
841                         int w1,h1,aw1;
842                         nothers++;
843                         gr_get_string_size(NORMAL_RADIO_BOX, &w1, &h1, &aw1  );
844                         item[i].right_offset = w1;
845                         gr_get_string_size(CHECKED_RADIO_BOX, &w1, &h1, &aw1  );
846                         if (w1 > item[i].right_offset)
847                                 item[i].right_offset = w1;
848                 }
849
850                 if  (item[i].type==NM_TYPE_NUMBER )     {
851                         int w1,h1,aw1;
852                         char test_text[20];
853                         nothers++;
854                         sprintf( test_text, "%d", item[i].max_value );
855                         gr_get_string_size( test_text, &w1, &h1, &aw1 );
856                         item[i].right_offset = w1;
857                         sprintf( test_text, "%d", item[i].min_value );
858                         gr_get_string_size( test_text, &w1, &h1, &aw1 );
859                         if ( w1 > item[i].right_offset)
860                                 item[i].right_offset = w1;
861                 }
862
863                 if ( item[i].type == NM_TYPE_INPUT )    {
864                         Assert( strlen(item[i].text) < NM_MAX_TEXT_LEN );
865                         strcpy(item[i].saved_text, item[i].text );
866                         nothers++;
867                         string_width = item[i].text_len*grd_curcanv->cv_font->ft_w+((MenuHires?3:1)*item[i].text_len);
868                         if ( string_width > MAX_TEXT_WIDTH ) 
869                                 string_width = MAX_TEXT_WIDTH;
870                         item[i].value = -1;
871                 }
872
873                 if ( item[i].type == NM_TYPE_INPUT_MENU )       {
874                         Assert( strlen(item[i].text) < NM_MAX_TEXT_LEN );
875                         strcpy(item[i].saved_text, item[i].text );
876                         nmenus++;
877                         string_width = item[i].text_len*grd_curcanv->cv_font->ft_w+((MenuHires?3:1)*item[i].text_len);
878                         item[i].value = -1;
879                         item[i].group = 0;
880                 }
881
882                 item[i].w = string_width;
883                 item[i].h = string_height;
884
885                 if ( string_width > w )
886                         w = string_width;               // Save maximum width
887                 if ( average_width > aw )
888                         aw = average_width;
889                 h += string_height+1;           // Find the height of all strings
890         }
891
892    // Big hack for allowing the netgame options menu to spill over
893
894    MaxOnMenu=MAXDISPLAYABLEITEMS;
895    if (ExtGameStatus==GAMESTAT_NETGAME_OPTIONS || ExtGameStatus==GAMESTAT_MORE_NETGAME_OPTIONS)
896                 MaxOnMenu++;
897
898    if (!TinyMode && (h>((MaxOnMenu+1)*(string_height+1))+(LHY(8))))
899     {
900      IsScrollBox=1;
901      h=(MaxOnMenu*(string_height+1)+LHY(8));
902      MaxDisplayable=MaxOnMenu;
903      mprintf ((0,"Hey, this is a scroll box!\n"));
904     }
905    else
906     IsScrollBox=0;
907
908         right_offset=0;
909
910         if ( width > -1 )
911                 w = width;
912
913         if ( height > -1 )
914                 h = height;
915
916         for (i=0; i<nitems; i++ )       {
917                 item[i].w = w;
918                 if (item[i].right_offset > right_offset )
919                         right_offset = item[i].right_offset;
920         }
921         if (right_offset > 0 )
922                 right_offset += 3;
923
924         //gr_get_string_size("",&string_width,&string_height,&average_width );
925
926         w += right_offset;
927
928
929         twidth = 0;
930         if ( tw > w )   {
931                 twidth = ( tw - w )/2;
932                 w = tw;
933         }
934
935    if (RestoringMenu)
936          { right_offset=0; twidth=0;}
937
938         mprintf(( 0, "Right offset = %d\n", right_offset ));
939
940
941         // Find min point of menu border
942 //      x = (grd_curscreen->sc_w-w)/2;
943 //      y = (grd_curscreen->sc_h-h)/2;
944
945         w += MenuHires?60:30;
946         h += MenuHires?60:30;
947
948         if ( w > grd_curcanv->cv_bitmap.bm_w ) w = grd_curcanv->cv_bitmap.bm_w;
949         if ( h > grd_curcanv->cv_bitmap.bm_h ) h = grd_curcanv->cv_bitmap.bm_h;
950
951         x = (grd_curcanv->cv_bitmap.bm_w-w)/2;
952         y = (grd_curcanv->cv_bitmap.bm_h-h)/2;
953
954         if ( x < 0 ) x = 0;
955         if ( y < 0 ) y = 0;
956
957         if ( filename != NULL ) {
958                 nm_draw_background1( filename );
959                 gr_palette_load(gr_palette);
960         }
961
962 // Save the background of the display
963         bg.menu_canvas = gr_create_sub_canvas( &grd_curscreen->sc_canvas, x, y, w, h );
964         gr_set_current_canvas(bg.menu_canvas);
965
966         if ( filename == NULL ) {
967                 // Save the background under the menu...
968                 #ifdef TACTILE
969                         if (TactileStick)
970                                 DisableForces();
971                 #endif
972                 
973                 bg.saved = gr_create_bitmap( w, h );
974                 Assert( bg.saved != NULL );
975
976                 gr_bm_bitblt(w, h, 0, 0, 0, 0, &grd_curcanv->cv_bitmap, bg.saved );
977
978                 gr_set_current_canvas( NULL );
979
980                 nm_draw_background(x,y,x+w-1,y+h-1);
981
982                 gr_set_current_canvas( bg.menu_canvas );
983
984                 bg.background = gr_create_sub_bitmap(&nm_background,0,0,w,h);
985
986         } else {
987                 bg.saved = NULL;
988                 bg.background = gr_create_bitmap( w, h );
989                 Assert( bg.background != NULL );
990
991                 gr_bm_bitblt(w, h, 0, 0, 0, 0, &grd_curcanv->cv_bitmap, bg.background );
992         }
993
994 // ty = 15 + (yborder/4);
995
996         ty = MenuHires?30:15;
997
998         if ( title )    {
999                 grd_curcanv->cv_font = TITLE_FONT;
1000                 gr_set_fontcolor( GR_GETCOLOR(31,31,31), -1 );
1001                 gr_get_string_size(title,&string_width,&string_height,&average_width );
1002                 tw = string_width;
1003                 th = string_height;
1004                 gr_printf( 0x8000, ty, title );
1005                 ty += th;
1006         }
1007
1008         if ( subtitle ) {
1009                 grd_curcanv->cv_font = SUBTITLE_FONT;
1010                 gr_set_fontcolor( GR_GETCOLOR(21,21,21), -1 );
1011                 gr_get_string_size(subtitle,&string_width,&string_height,&average_width );
1012                 tw = string_width;
1013                 th = string_height;
1014                 gr_printf( 0x8000, ty, subtitle );
1015                 ty += th;
1016         }
1017
1018         if (TinyMode)
1019         grd_curcanv->cv_font = SMALL_FONT;
1020         else 
1021         grd_curcanv->cv_font = NORMAL_FONT;
1022         
1023         // Update all item's x & y values.
1024         for (i=0; i<nitems; i++ )       {
1025                 item[i].x = (MenuHires?30:15) + twidth + right_offset;
1026                 item[i].y += (MenuHires?30:15);
1027                 if ( item[i].type==NM_TYPE_RADIO )      {
1028                         fm = -1;        // find first marked one
1029                         for ( j=0; j<nitems; j++ )      {
1030                                 if ( item[j].type==NM_TYPE_RADIO && item[j].group==item[i].group )      {
1031                                         if (fm==-1 && item[j].value)
1032                                                 fm = j;
1033                                         item[j].value = 0;
1034                                 }
1035                         }
1036                         if ( fm>=0 )    
1037                                 item[fm].value=1;
1038                         else
1039                                 item[i].value=1;
1040                 }
1041         }
1042
1043         old_keyd_repeat = keyd_repeat;
1044         keyd_repeat = 1;
1045
1046         if (citem==-1)  {
1047                 choice = -1;
1048         } else {
1049                 if (citem < 0 ) citem = 0;
1050                 if (citem > nitems-1 ) citem = nitems-1;
1051                 choice = citem;
1052
1053 #ifdef NEWMENU_MOUSE
1054                 dblclick_flag = 1;
1055 #endif
1056
1057                 while ( item[choice].type==NM_TYPE_TEXT )       {
1058                         choice++;
1059                         if (choice >= nitems ) {
1060                                 choice=0; 
1061                         }
1062                         if (choice == citem ) {
1063                                 choice=0; 
1064                                 all_text=1;
1065                                 break; 
1066                         }
1067                 }
1068         } 
1069         done = 0;
1070    TopChoice=choice;
1071
1072         gr_update();
1073         // Clear mouse, joystick to clear button presses.
1074         game_flush_inputs();
1075
1076 #ifdef NEWMENU_MOUSE
1077         mouse_state = omouse_state = 0;
1078         if (filename == NULL && !MenuReordering) {
1079                 draw_close_box(0,0);
1080                 close_box = 1;
1081         }
1082
1083         if (!MenuReordering && !joydefs_calibrating)
1084         {
1085                 newmenu_show_cursor();
1086 # ifdef WINDOWS
1087                 SetCursor(LoadCursor(NULL,IDC_ARROW));
1088 # endif
1089         }
1090 #endif
1091
1092    mprintf ((0,"Set to true!\n"));
1093
1094         while(!done)    {
1095 #ifdef NEWMENU_MOUSE
1096                 if (!joydefs_calibrating)
1097                         newmenu_show_cursor();      // possibly hidden
1098                 omouse_state = mouse_state;
1099                 if (!MenuReordering)
1100                         mouse_state = mouse_button_state(0);
1101 //@@      mprintf ((0,"mouse state:%d\n",mouse_state));
1102 #endif
1103
1104                 //see if redbook song needs to be restarted
1105                 songs_check_redbook_repeat();
1106
1107                 //network_listen();
1108
1109                 k = key_inkey();
1110
1111         if (subfunction)
1112         (*subfunction)(nitems,item,&k,choice);
1113
1114 #ifdef NETWORK
1115                 if (!time_stopped)      {
1116                         // Save current menu box
1117                         if (multi_menu_poll() == -1)
1118                                 k = -2;
1119                 }
1120 #endif
1121
1122                 if ( k<-1 ) {
1123                         dont_restore = (k == -3);               //-3 means don't restore
1124                         choice = k;
1125                         k = -1;
1126                         done = 1;
1127                 }
1128                 if (check_button_press())
1129                         done = 1;
1130
1131 //              if ( (nmenus<2) && (k>0) && (nothers==0) )
1132 //                      done=1;
1133
1134                 old_choice = choice;
1135         
1136                 switch( k )     {
1137
1138 #ifdef NETWORK
1139                 case KEY_I:
1140                  if (SurfingNet && !already_showing_info)
1141                    {
1142                          show_extra_netgame_info(choice-2);
1143                    }
1144                  if (SurfingNet && already_showing_info)
1145                         {
1146                          done=1;
1147                          choice=-1;
1148                         }
1149                  break;
1150                 case KEY_U:
1151                  if (SurfingNet && !already_showing_info)
1152                    {
1153                          network_request_player_names(choice-2);
1154                    }
1155                  if (SurfingNet && already_showing_info)
1156                         {
1157                          done=1;
1158                          choice=-1;
1159                         }
1160                  break;
1161 #endif
1162                 case KEY_PAUSE:
1163                  if (Pauseable_menu)
1164                    {    
1165                          Pauseable_menu=0;
1166                          done=1;
1167                          choice=-1;
1168                    }
1169                  break;
1170                 case KEY_TAB + KEY_SHIFTED:
1171                 case KEY_UP:
1172                 case KEY_PAD8:
1173                         if (all_text) break;
1174                         do {
1175                                 choice--;
1176
1177                 if (IsScrollBox)
1178                 {
1179                         LastScrollCheck=-1;
1180                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1181                                    
1182                 if (choice<TopChoice)
1183                         { choice=TopChoice; break; }
1184
1185                 if (choice<ScrollOffset)
1186                {
1187                         for (i=0;i<nitems;i++)
1188                                 item[i].redraw=1;
1189                      ScrollOffset--;
1190                      mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1191                }
1192                 }
1193                 else
1194                 {
1195                         if (choice >= nitems ) choice=0;
1196                 if (choice < 0 ) choice=nitems-1;
1197                 }
1198                         } while ( item[choice].type==NM_TYPE_TEXT );
1199                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1200                                 item[choice].value = -1;
1201                         if ((old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))     {
1202                                 item[old_choice].group=0;
1203                                 strcpy(item[old_choice].text, item[old_choice].saved_text );
1204                                 item[old_choice].value = -1;
1205                         }
1206                         if (old_choice>-1) 
1207                                 item[old_choice].redraw = 1;
1208                         item[choice].redraw=1;
1209                         break;
1210                 case KEY_TAB:
1211                 case KEY_DOWN:
1212                 case KEY_PAD2:
1213         // ((0,"Pressing down! IsScrollBox=%d",IsScrollBox));
1214                 if (all_text) break;
1215                         do {
1216                                 choice++;
1217
1218                         if (IsScrollBox)
1219                 {
1220                 LastScrollCheck=-1;
1221                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1222                                    
1223                 if (choice==nitems)
1224                 { choice--; break; }
1225
1226                 if (choice>=MaxOnMenu+ScrollOffset)
1227                 {
1228                 for (i=0;i<nitems;i++)
1229                                 item[i].redraw=1;
1230                   ScrollOffset++;
1231                   mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1232                 }
1233                 }
1234             else
1235             {
1236                         if (choice < 0 ) choice=nitems-1;
1237                         if (choice >= nitems ) choice=0;
1238                 }
1239
1240                         } while ( item[choice].type==NM_TYPE_TEXT );
1241                                                       
1242                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1243                                 item[choice].value = -1;
1244                         if ( (old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))    {
1245                                 item[old_choice].group=0;
1246                                 strcpy(item[old_choice].text, item[old_choice].saved_text );    
1247                                 item[old_choice].value = -1;
1248                         }
1249                         if (old_choice>-1)
1250                                 item[old_choice].redraw=1;
1251                         item[choice].redraw=1;
1252                         break;
1253                 case KEY_SPACEBAR:
1254                         if ( choice > -1 )      {
1255                                 switch( item[choice].type )     {
1256                                 case NM_TYPE_MENU:
1257                                 case NM_TYPE_INPUT:
1258                                 case NM_TYPE_INPUT_MENU:
1259                                         break;
1260                                 case NM_TYPE_CHECK:
1261                                         if ( item[choice].value )
1262                                                 item[choice].value = 0;
1263                                         else
1264                                                 item[choice].value = 1;
1265                                         mprintf ((0,"ISB=%d MDI=%d SO=%d choice=%d\n",IsScrollBox,MAXDISPLAYABLEITEMS,ScrollOffset,choice));
1266                                         if (IsScrollBox)
1267                                          {
1268                                                 if (choice==(MaxOnMenu+ScrollOffset-1) || choice==ScrollOffset)
1269                                                  {
1270                                                    mprintf ((0,"Special redraw!\n"));
1271                                                         LastScrollCheck=-1;                                     
1272                                                  }
1273                                          }
1274                                 
1275                                         item[choice].redraw=1;
1276                                         break;
1277                                 case NM_TYPE_RADIO:
1278                                         for (i=0; i<nitems; i++ )       {
1279                                                 if ((i!=choice) && (item[i].type==NM_TYPE_RADIO) && (item[i].group==item[choice].group) && (item[i].value) )    {
1280                                                         item[i].value = 0;
1281                                                         item[i].redraw = 1;
1282                                                 }
1283                                         }
1284                                         item[choice].value = 1;
1285                                         item[choice].redraw = 1;
1286                                         break;
1287                                 }       
1288                         }
1289                         break;
1290
1291                 case KEY_SHIFTED+KEY_UP:
1292                  if (MenuReordering && choice!=TopChoice)
1293                   {
1294                    Temp=item[choice].text;
1295                    TempVal=item[choice].value;
1296                    item[choice].text=item[choice-1].text;
1297                    item[choice].value=item[choice-1].value;
1298                    item[choice-1].text=Temp;
1299                    item[choice-1].value=TempVal;
1300                    item[choice].redraw=1;
1301                    item[choice-1].redraw=1;
1302                    choice--;
1303                   }
1304                  break;
1305                 case KEY_SHIFTED+KEY_DOWN:
1306                  if (MenuReordering && choice!=(nitems-1))
1307                   {
1308                    Temp=item[choice].text;
1309                    TempVal=item[choice].value;
1310                    item[choice].text=item[choice+1].text;
1311                    item[choice].value=item[choice+1].value;
1312                    item[choice+1].text=Temp;
1313                    item[choice+1].value=TempVal;
1314                    item[choice].redraw=1;
1315                    item[choice+1].redraw=1;
1316                    choice++;
1317                   }
1318                  break;
1319                 
1320                 case KEY_ENTER:
1321                 case KEY_PADENTER:
1322                         if ( (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==0)) {
1323                                 item[choice].group = 1;
1324                                 item[choice].redraw = 1;
1325                                 if ( !strnicmp( item[choice].saved_text, TXT_EMPTY, strlen(TXT_EMPTY) ) )       {
1326                                         item[choice].text[0] = 0;
1327                                         item[choice].value = -1;
1328                                 } else {        
1329                                         strip_end_whitespace(item[choice].text);
1330                                 }
1331                         } else
1332                                 done = 1;
1333                         break;
1334
1335                 case KEY_ESC:
1336                         if ( (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==1)) {
1337                                 item[choice].group=0;
1338                                 strcpy(item[choice].text, item[choice].saved_text );    
1339                                 item[choice].redraw=1;
1340                                 item[choice].value = -1;
1341                         } else {
1342                                 done = 1;
1343                                 choice = -1;
1344                         }
1345                         break;
1346
1347                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
1348                 case KEY_PRINT_SCREEN:
1349                         MAC(newmenu_hide_cursor());
1350                         save_screen_shot(0);
1351                         for (i=0;i<nitems;i++)
1352                                 item[i].redraw=1;
1353                         
1354                         MAC(newmenu_show_cursor());
1355                         MAC(key_flush());
1356                         break;
1357
1358                 #ifdef MACINTOSH
1359
1360                 case KEY_COMMAND+KEY_RIGHT:
1361                         songs_goto_next_song();
1362                         break;
1363                 case KEY_COMMAND+KEY_LEFT:
1364                         songs_goto_prev_song();
1365                         break;
1366                 case KEY_COMMAND+KEY_UP:
1367                         songs_play_level_song(1);
1368                         break;
1369                 case KEY_COMMAND+KEY_DOWN:
1370                         songs_stop_redbook();
1371                         break;
1372
1373                 case KEY_COMMAND+KEY_M:
1374                         k = -1;
1375                         #if !defined(SHAREWARE) || defined(APPLE_DEMO)
1376                         if ( (Game_mode & GM_MULTI) )           // don't process in multiplayer games
1377                                 break;
1378
1379                         key_close();            // no processing of keys with keyboard handler.. jeez                           
1380                         stop_time();
1381                         newmenu_hide_cursor();
1382                         show_boxed_message ("Mounting CD\nESC to quit");        
1383                         RBAMountDisk();         // OS has totaly control of the CD.
1384                         if (Function_mode == FMODE_MENU)
1385                                 songs_play_song(SONG_TITLE,1);
1386                         else if (Function_mode == FMODE_GAME)
1387                                 songs_play_level_song( Current_level_num );
1388                         clear_boxed_message();
1389                         newmenu_show_cursor();
1390                         key_init();
1391                         key_flush();
1392                         start_time();
1393                         #endif
1394                         
1395                         break;
1396
1397                 case KEY_COMMAND+KEY_E:
1398                         songs_stop_redbook();
1399                         RBAEjectDisk();
1400                         k = -1;         // force key not to register
1401                         break;
1402                         
1403                 case KEY_COMMAND+KEY_Q: {
1404                         extern void macintosh_quit();
1405                         
1406                         if ( !(Game_mode & GM_MULTI) )
1407                                 macintosh_quit();
1408                         if (!joydefs_calibrating)
1409                                 newmenu_show_cursor();
1410                         k = -1;         // force key not to register
1411                         break;
1412                 }
1413                 #endif
1414
1415                 #ifndef NDEBUG
1416                 case KEY_BACKSP:        
1417                         if ( (choice>-1) && (item[choice].type!=NM_TYPE_INPUT)&&(item[choice].type!=NM_TYPE_INPUT_MENU))
1418                                 Int3(); 
1419                         break;
1420                 #endif
1421
1422                 }
1423
1424 #ifdef NEWMENU_MOUSE // for mouse selection of menu's etc.
1425                 if ( !done && mouse_state && !omouse_state && !all_text ) {
1426                         mouse_get_pos(&mx, &my);
1427                         for (i=0; i<nitems; i++ )       {
1428                                 x1 = grd_curcanv->cv_bitmap.bm_x + item[i].x - item[i].right_offset - 6;
1429                                 x2 = x1 + item[i].w;
1430                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[i].y;
1431                                 y2 = y1 + item[i].h;
1432                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
1433                                         if (i+ScrollOffset != choice) {
1434                                                 if(Hack_DblClick_MenuMode) dblclick_flag = 0; 
1435                                         }
1436                                         
1437                                         choice = i + ScrollOffset;
1438
1439                                         switch( item[choice].type )     {
1440                                         case NM_TYPE_CHECK:
1441                                                 if ( item[choice].value )
1442                                                         item[choice].value = 0;
1443                                                 else
1444                                                         item[choice].value = 1;
1445                                                 item[choice].redraw=1;
1446
1447                                                 if (IsScrollBox)
1448                                                         LastScrollCheck=-1;
1449 #if 0
1450                                                 if (IsScrollBox)
1451                                                  {
1452                                                         if (choice==(MaxOnMenu+ScrollOffset-1) || choice==ScrollOffset)
1453                                                          {
1454                                                            mprintf ((0,"Special redraw!\n"));
1455                                                                 LastScrollCheck=-1;                                     
1456                                                          }
1457                                                  }
1458 #endif
1459                                                 break;
1460                                         case NM_TYPE_RADIO:
1461                                                 for (i=0; i<nitems; i++ )       {
1462                                                         if ((i!=choice) && (item[i].type==NM_TYPE_RADIO) && (item[i].group==item[choice].group) && (item[i].value) )    {
1463                                                                 item[i].value = 0;
1464                                                                 item[i].redraw = 1;
1465                                                         }
1466                                                 }
1467                                                 item[choice].value = 1;
1468                                                 item[choice].redraw = 1;
1469                                                 break;
1470                                         }
1471                                         item[old_choice].redraw=1;
1472                                         break;
1473                                 }
1474                         }
1475                 }
1476
1477                 if (mouse_state && all_text)
1478                         done = 1;
1479                 
1480                 if ( !done && mouse_state && !all_text ) {
1481                         mouse_get_pos(&mx, &my);
1482                         
1483                         // check possible scrollbar stuff first
1484                         if (IsScrollBox) {
1485                                 int arrow_width, arrow_height, aw;
1486                                 
1487                                 if (ScrollOffset != 0) {
1488                                         gr_get_string_size(UP_ARROW_MARKER, &arrow_width, &arrow_height, &aw);
1489                                         x2 = grd_curcanv->cv_bitmap.bm_x + item[ScrollOffset].x-(MenuHires?24:12);
1490                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[ScrollOffset].y-((string_height+1)*ScrollOffset);
1491                                         x1 = x1 - arrow_width;
1492                                         y2 = y1 + arrow_height;
1493                                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
1494                                                 choice--;
1495                                         LastScrollCheck=-1;
1496                                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1497                                                    
1498                                 if (choice<ScrollOffset)
1499                                {
1500                                         for (i=0;i<nitems;i++)
1501                                                 item[i].redraw=1;
1502                                      ScrollOffset--;
1503                                      mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1504                                }
1505                                         }
1506                                 }
1507                                 if (ScrollOffset+MaxDisplayable<nitems) {
1508                                         gr_get_string_size(DOWN_ARROW_MARKER, &arrow_width, &arrow_height, &aw);
1509                                         x2 = grd_curcanv->cv_bitmap.bm_x + item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
1510                                         y1 = grd_curcanv->cv_bitmap.bm_y + item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
1511                                         x1 = x1 - arrow_width;
1512                                         y2 = y1 + arrow_height;
1513                                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
1514                                                 choice++;
1515                                 LastScrollCheck=-1;
1516                                 mprintf ((0,"Scrolling! Choice=%d\n",choice));
1517                                                    
1518                                 if (choice>=MaxOnMenu+ScrollOffset)
1519                                 {
1520                                 for (i=0;i<nitems;i++)
1521                                                 item[i].redraw=1;
1522                                   ScrollOffset++;
1523                                   mprintf ((0,"ScrollOffset=%d\n",ScrollOffset));
1524                                 }
1525                                         }
1526                                 }
1527                         }
1528                         
1529                         for (i=0; i<nitems; i++ )       {
1530                                 x1 = grd_curcanv->cv_bitmap.bm_x + item[i].x - item[i].right_offset - 6;
1531                                 x2 = x1 + item[i].w;
1532                                 y1 = grd_curcanv->cv_bitmap.bm_y + item[i].y;
1533                                 y2 = y1 + item[i].h;
1534                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && (item[i].type != NM_TYPE_TEXT) ) {
1535                                         if (i+ScrollOffset != choice) {
1536                                                 if(Hack_DblClick_MenuMode) dblclick_flag = 0; 
1537                                         }
1538
1539                                         choice = i + ScrollOffset;
1540
1541                                         if ( item[choice].type == NM_TYPE_SLIDER ) {
1542                                                 char slider_text[NM_MAX_TEXT_LEN+1], *p, *s1;
1543                                                 int slider_width, height, aw, sleft_width, sright_width, smiddle_width;
1544                                                 
1545                                                 strcpy(slider_text, item[choice].saved_text);
1546                                                 p = strchr(slider_text, '\t');
1547                                                 if (p) {
1548                                                         *p = '\0';
1549                                                         s1 = p+1;
1550                                                 }
1551                                                 if (p) {
1552                                                         gr_get_string_size(s1, &slider_width, &height, &aw);
1553                                                         gr_get_string_size(SLIDER_LEFT, &sleft_width, &height, &aw);
1554                                                         gr_get_string_size(SLIDER_RIGHT, &sright_width, &height, &aw);
1555                                                         gr_get_string_size(SLIDER_MIDDLE, &smiddle_width, &height, &aw);
1556
1557                                                         x1 = grd_curcanv->cv_bitmap.bm_x + item[choice].x + item[choice].w - slider_width;
1558                                                         x2 = x1 + slider_width + sright_width;
1559                                                         if ( (mx > x1) && (mx < (x1 + sleft_width)) && (item[choice].value != item[choice].min_value) ) {
1560                                                                 item[choice].value = item[choice].min_value;
1561                                                                 item[choice].redraw = 2;
1562                                                         } else if ( (mx < x2) && (mx > (x2 - sright_width)) && (item[choice].value != item[choice].max_value) ) {
1563                                                                 item[choice].value = item[choice].max_value;
1564                                                                 item[choice].redraw = 2;
1565                                                         } else if ( (mx > (x1 + sleft_width)) && (mx < (x2 - sright_width)) ) {
1566                                                                 int num_values, value_width, new_value;
1567                                                                 
1568                                                                 num_values = item[choice].max_value - item[choice].min_value + 1;
1569                                                                 value_width = (slider_width - sleft_width - sright_width) / num_values;
1570                                                                 new_value = (mx - x1 - sleft_width) / value_width;
1571                                                                 if ( item[choice].value != new_value ) {
1572                                                                         item[choice].value = new_value;
1573                                                                         item[choice].redraw = 2;
1574                                                                 }
1575                                                         }
1576                                                         *p = '\t';
1577                                                 }
1578                                         }
1579                                         if (choice == old_choice)
1580                                                 break;
1581                                         if ((item[choice].type==NM_TYPE_INPUT) && (choice!=old_choice)) 
1582                                                 item[choice].value = -1;
1583                                         if ((old_choice>-1) && (item[old_choice].type==NM_TYPE_INPUT_MENU) && (old_choice!=choice))     {
1584                                                 item[old_choice].group=0;
1585                                                 strcpy(item[old_choice].text, item[old_choice].saved_text );
1586                                                 item[old_choice].value = -1;
1587                                         }
1588                                         if (old_choice>-1) 
1589                                                 item[old_choice].redraw = 1;
1590                                         item[choice].redraw=1;
1591                                         break;
1592                                 }
1593                         }
1594                 }
1595                 
1596                 if ( !done && !mouse_state && omouse_state && !all_text && (choice != -1) && (item[choice].type == NM_TYPE_MENU) ) {
1597                         mouse_get_pos(&mx, &my);
1598                         x1 = grd_curcanv->cv_bitmap.bm_x + item[choice].x;
1599                         x2 = x1 + item[choice].w;
1600                         y1 = grd_curcanv->cv_bitmap.bm_y + item[choice].y;
1601                         y2 = y1 + item[choice].h;
1602                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
1603                                 if (Hack_DblClick_MenuMode) {
1604                                         if (dblclick_flag) done = 1;
1605                                         else dblclick_flag = 1;
1606                                 }
1607                                 else done = 1;
1608                         }
1609                 }
1610                 
1611                 if ( !done && !mouse_state && omouse_state && (choice>-1) && (item[choice].type==NM_TYPE_INPUT_MENU) && (item[choice].group==0))        {
1612                         item[choice].group = 1;
1613                         item[choice].redraw = 1;
1614                         if ( !strnicmp( item[choice].saved_text, TXT_EMPTY, strlen(TXT_EMPTY) ) )       {
1615                                 item[choice].text[0] = 0;
1616                                 item[choice].value = -1;
1617                         } else {        
1618                                 strip_end_whitespace(item[choice].text);
1619                         }
1620                 }
1621                 
1622                 if ( !done && !mouse_state && omouse_state && close_box ) {
1623                         mouse_get_pos(&mx, &my);
1624                         x1 = grd_curcanv->cv_bitmap.bm_x + CLOSE_X;
1625                         x2 = x1 + CLOSE_SIZE;
1626                         y1 = grd_curcanv->cv_bitmap.bm_y + CLOSE_Y;
1627                         y2 = y1 + CLOSE_SIZE;
1628                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
1629                                 choice = -1;
1630                                 done = 1;
1631                         }
1632                 }
1633
1634 //       HACK! Don't redraw loadgame preview
1635                 if (RestoringMenu) item[0].redraw = 0;
1636 #endif // NEWMENU_MOUSE
1637
1638                 if ( choice > -1 )      {
1639                         int ascii;
1640
1641                         if ( ((item[choice].type==NM_TYPE_INPUT)||((item[choice].type==NM_TYPE_INPUT_MENU)&&(item[choice].group==1)) )&& (old_choice==choice) ) {
1642                                 if ( k==KEY_LEFT || k==KEY_BACKSP || k==KEY_PAD4 )      {
1643                                         if (item[choice].value==-1) item[choice].value = strlen(item[choice].text);
1644                                         if (item[choice].value > 0)
1645                                                 item[choice].value--;
1646                                         item[choice].text[item[choice].value] = 0;
1647                                         item[choice].redraw = 1;        
1648                                 } else {
1649                                         ascii = key_to_ascii(k);
1650                                         if ((ascii < 255 ) && (item[choice].value < item[choice].text_len ))
1651                                         {
1652                                                 int allowed;
1653
1654                                                 if (item[choice].value==-1) {
1655                                                         item[choice].value = 0;
1656                                                 }
1657
1658                                                 allowed = char_allowed(ascii);
1659
1660                                                 if (!allowed && ascii==' ' && char_allowed('_')) {
1661                                                         ascii = '_';
1662                                                         allowed=1;
1663                                                 }
1664
1665                                                 if (allowed) {
1666                                                         item[choice].text[item[choice].value++] = ascii;
1667                                                         item[choice].text[item[choice].value] = 0;
1668                                                         item[choice].redraw=1;  
1669                                                 }
1670                                         }
1671                                 }
1672                         } else if ((item[choice].type!=NM_TYPE_INPUT) && (item[choice].type!=NM_TYPE_INPUT_MENU) ) {
1673                                 ascii = key_to_ascii(k);
1674                                 if (ascii < 255 ) {
1675                                         int choice1 = choice;
1676                                         ascii = toupper(ascii);
1677                                         do {
1678                                                 int i,ch;
1679                                                 choice1++;
1680                                                 if (choice1 >= nitems ) choice1=0;
1681                                                 for (i=0;(ch=item[choice1].text[i])!=0 && ch==' ';i++);
1682                                                 if ( ( (item[choice1].type==NM_TYPE_MENU) ||
1683                                                                  (item[choice1].type==NM_TYPE_CHECK) ||
1684                                                                  (item[choice1].type==NM_TYPE_RADIO) ||
1685                                                                  (item[choice1].type==NM_TYPE_NUMBER) ||
1686                                                                  (item[choice1].type==NM_TYPE_SLIDER) )
1687                                                                 && (ascii==toupper(ch)) )       {
1688                                                         k = 0;
1689                                                         choice = choice1;
1690                                                         if (old_choice>-1)
1691                                                                 item[old_choice].redraw=1;
1692                                                         item[choice].redraw=1;
1693                                                 }
1694                                         } while (choice1 != choice );
1695                                 }       
1696                         }
1697
1698                         if ( (item[choice].type==NM_TYPE_NUMBER) || (item[choice].type==NM_TYPE_SLIDER))        {
1699                                 int ov=item[choice].value;
1700                                 switch( k ) {
1701                                 case KEY_PAD4:
1702                                 case KEY_LEFT:
1703                                 case KEY_MINUS:
1704                                 case KEY_MINUS+KEY_SHIFTED:
1705                                 case KEY_PADMINUS:
1706                                         item[choice].value -= 1;
1707                                         break;
1708                                 case KEY_RIGHT:
1709                                 case KEY_PAD6:
1710                                 case KEY_EQUAL:
1711                                 case KEY_EQUAL+KEY_SHIFTED:
1712                                 case KEY_PADPLUS:
1713                                         item[choice].value++;
1714                                         break;
1715                                 case KEY_PAGEUP:
1716                                 case KEY_PAD9:
1717                                 case KEY_SPACEBAR:
1718                                         item[choice].value += 10;
1719                                         break;
1720                                 case KEY_PAGEDOWN:
1721                                 case KEY_BACKSP:
1722                                 case KEY_PAD3:
1723                                         item[choice].value -= 10;
1724                                         break;
1725                                 }
1726                                 if (ov!=item[choice].value)
1727                                         item[choice].redraw=1;
1728                         }
1729         
1730                 }
1731
1732                 gr_set_current_canvas(bg.menu_canvas);
1733
1734         // Redraw everything...
1735         for (i=ScrollOffset; i<MaxDisplayable+ScrollOffset; i++ )
1736         {
1737         if (item[i].redraw) // warning! ugly hack below                  
1738                 {
1739                 item[i].y-=((string_height+1)*ScrollOffset);
1740                         newmenu_hide_cursor();
1741                 draw_item( &bg, &item[i], (i==choice && !all_text),TinyMode );
1742                                 item[i].redraw=0;
1743 #ifdef NEWMENU_MOUSE
1744                                 if (!MenuReordering && !joydefs_calibrating)
1745                                         newmenu_show_cursor();
1746 #endif
1747             item[i].y+=((string_height+1)*ScrollOffset);
1748                 }   
1749          if (i==choice && (item[i].type==NM_TYPE_INPUT || (item[i].type==NM_TYPE_INPUT_MENU && item[i].group)))
1750                                 update_cursor( &item[i]);
1751                 }
1752         gr_update();
1753
1754       if (IsScrollBox)
1755         {
1756         //grd_curcanv->cv_font = NORMAL_FONT;
1757         
1758                 if (LastScrollCheck!=ScrollOffset)
1759          {
1760                 LastScrollCheck=ScrollOffset;
1761                 grd_curcanv->cv_font = SELECTED_FONT;
1762                                 
1763                 sy=item[ScrollOffset].y-((string_height+1)*ScrollOffset);
1764                 sx=item[ScrollOffset].x-(MenuHires?24:12);
1765                                 
1766           
1767                 if (ScrollOffset!=0)
1768                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, UP_ARROW_MARKER );
1769                 else
1770                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
1771
1772                 sy=item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
1773                 sx=item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
1774           
1775                 if (ScrollOffset+MaxDisplayable<nitems)
1776                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, DOWN_ARROW_MARKER );
1777                 else
1778                 nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
1779
1780         }
1781
1782         }   
1783
1784                 if ( !dont_restore && gr_palette_faded_out )    {
1785                         gr_palette_fade_in( gr_palette, 32, 0 );
1786                 }
1787         }
1788
1789         newmenu_hide_cursor();
1790
1791         // Restore everything...
1792
1793         gr_set_current_canvas(bg.menu_canvas);
1794
1795         if ( filename == NULL ) {
1796                 // Save the background under the menu...
1797                 gr_bitmap(0, 0, bg.saved);      
1798                 gr_free_bitmap(bg.saved);
1799                 d_free( bg.background );
1800         } else {
1801                 if (!dont_restore)      //info passed back from subfunction
1802                         gr_bitmap(0, 0, bg.background);
1803                 gr_free_bitmap(bg.background);
1804         }
1805
1806         gr_free_sub_canvas( bg.menu_canvas );
1807
1808         gr_set_current_canvas(save_canvas);
1809         grd_curcanv->cv_font    = save_font;
1810         keyd_repeat = old_keyd_repeat;
1811
1812         game_flush_inputs();
1813
1814         if (time_stopped) 
1815      {
1816                 start_time();
1817                 #ifdef TACTILE
1818                         if (TactileStick)
1819                                 EnableForces();
1820                 #endif
1821           }
1822
1823         if ( sound_stopped )
1824                 digi_resume_digi_sounds();
1825
1826         return choice;
1827         
1828 }
1829
1830
1831 int nm_messagebox1( char *title, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int nchoices, ... )
1832 {
1833         int i;
1834         char * format;
1835         va_list args;
1836         char *s;
1837         char nm_text[MESSAGEBOX_TEXT_SIZE];
1838         newmenu_item nm_message_items[5];
1839
1840         va_start(args, nchoices );
1841
1842         Assert( nchoices <= 5 );
1843
1844         for (i=0; i<nchoices; i++ )     {
1845                 s = va_arg( args, char * );
1846                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
1847         }
1848         format = va_arg( args, char * );
1849         strcpy( nm_text, "" );
1850         vsprintf(nm_text,format,args);
1851         va_end(args);
1852
1853         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE);
1854
1855         return newmenu_do( title, nm_text, nchoices, nm_message_items, subfunction );
1856 }
1857
1858 int nm_messagebox( char *title, int nchoices, ... )
1859 {
1860         int i;
1861         char * format;
1862         va_list args;
1863         char *s;
1864         char nm_text[MESSAGEBOX_TEXT_SIZE];
1865         newmenu_item nm_message_items[5];
1866
1867         va_start(args, nchoices );
1868
1869         Assert( nchoices <= 5 );
1870
1871         for (i=0; i<nchoices; i++ )     {
1872                 s = va_arg( args, char * );
1873                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
1874         }
1875         format = va_arg( args, char * );
1876         strcpy( nm_text, "" );
1877         vsprintf(nm_text,format,args);
1878         va_end(args);
1879
1880         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
1881
1882         return newmenu_do( title, nm_text, nchoices, nm_message_items, NULL );
1883 }
1884
1885
1886
1887
1888 void newmenu_file_sort( int n, char *list )
1889 {
1890         int i, j, incr;
1891         char t[14];
1892
1893         incr = n / 2;
1894         while( incr > 0 )               {
1895                 for (i=incr; i<n; i++ )         {
1896                         j = i - incr;
1897                         while (j>=0 )                   {
1898                                 if (strncmp(&list[j*14], &list[(j+incr)*14], 12) > 0)                           {
1899                                         memcpy( t, &list[j*14], FILENAME_LEN );
1900                                         memcpy( &list[j*14], &list[(j+incr)*14], FILENAME_LEN );
1901                                         memcpy( &list[(j+incr)*14], t, FILENAME_LEN );
1902                                         j -= incr;
1903                                 }
1904                                 else
1905                                         break;
1906                         }
1907                 }
1908                 incr = incr / 2;
1909         }
1910 }
1911
1912 void delete_player_saved_games(char * name)
1913 {
1914         int i;
1915         char filename[16];
1916
1917         for (i=0;i<10; i++)     {
1918 #ifndef MACINTOSH
1919                 sprintf( filename, "%s.sg%d", name, i );
1920 #else
1921                 sprintf(filename, "Players/%s.sg%d", name, i);
1922 #endif
1923                 PHYSFS_delete(filename);
1924         }
1925 }
1926
1927 #define MAX_FILES 300
1928
1929 //FIXME: should maybe put globbing ability back?
1930 int newmenu_get_filename(char *title, char *type, char *filename, int allow_abort_flag)
1931 {
1932         int i;
1933         char **find;
1934         char **f;
1935         char *ext;
1936         int NumFiles=0, key,done, citem, ocitem;
1937         char * filenames = NULL;
1938         int NumFiles_displayed = 8;
1939         int first_item = -1, ofirst_item;
1940         int old_keyd_repeat = keyd_repeat;
1941         int player_mode=0;
1942         int demo_mode=0;
1943         int demos_deleted=0;
1944         int initialized = 0;
1945         int exit_value = 0;
1946         int w_x, w_y, w_w, w_h, title_height;
1947         int box_x, box_y, box_w, box_h;
1948         bkg bg;         // background under listbox
1949 #ifdef NEWMENU_MOUSE
1950         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;
1951         int mouse2_state, omouse2_state;
1952         int dblclick_flag=0;
1953 # ifdef WINDOWS
1954         int simukey=0;
1955         int show_up_arrow=0, show_down_arrow=0;
1956 # endif
1957 #endif
1958
1959         w_x=w_y=w_w=w_h=title_height=0;
1960         box_x=box_y=box_w=box_h=0;
1961
1962         filenames = d_malloc( MAX_FILES * 14 );
1963         if (filenames==NULL) return 0;
1964
1965         citem = 0;
1966         keyd_repeat = 1;
1967
1968         if (!stricmp(type, "plr"))
1969                 player_mode = 1;
1970         else if (!stricmp(type, "dem"))
1971                 demo_mode = 1;
1972
1973 ReadFileNames:
1974         done = 0;
1975         NumFiles=0;
1976         
1977 #if !defined(APPLE_DEMO)                // no new pilots for special apple oem version
1978         if (player_mode)        {
1979                 strncpy( &filenames[NumFiles*14], TXT_CREATE_NEW, FILENAME_LEN );
1980                 NumFiles++;
1981         }
1982 #endif
1983
1984         find = PHYSFS_enumerateFiles(demo_mode?DEMO_DIR:"");
1985         for (f = find; *f != NULL; f++)
1986         {
1987                 if (player_mode)
1988                 {
1989                         ext = strrchr(*f, '.');
1990                         if (!ext || strnicmp(ext, ".plr", 4))
1991                                 continue;
1992                 }
1993                 if (NumFiles < MAX_FILES)
1994                 {
1995                         strncpy(&filenames[NumFiles*14], *f, FILENAME_LEN);
1996                         if (player_mode)
1997                         {
1998                                 char *p;
1999
2000                                 p = strchr(&filenames[NumFiles*14], '.');
2001                                 if (p)
2002                                         *p = '\0';
2003                         }
2004                         NumFiles++;
2005                 }
2006                 else
2007                         break;
2008         }
2009
2010         PHYSFS_freeList(find);
2011
2012         if ( (NumFiles < 1) && demos_deleted )  {
2013                 exit_value = 0;
2014                 goto ExitFileMenu;
2015         }
2016         if ( (NumFiles < 1) && demo_mode ) {
2017                 nm_messagebox( NULL, 1, TXT_OK, "%s %s\n%s", TXT_NO_DEMO_FILES, TXT_USE_F5, TXT_TO_CREATE_ONE);
2018                 exit_value = 0;
2019                 goto ExitFileMenu;
2020         }
2021
2022         #ifndef APPLE_DEMO
2023         if ( (NumFiles < 2) && player_mode ) {
2024                 citem = 0;
2025                 goto ExitFileMenuEarly;
2026         }
2027         #endif
2028
2029
2030         if ( NumFiles<1 )       {
2031                 #ifndef APPLE_DEMO
2032                         nm_messagebox(NULL, 1, "Ok", "%s\n '%s' %s", TXT_NO_FILES_MATCHING, type, TXT_WERE_FOUND);
2033                 #endif
2034                 exit_value = 0;
2035                 goto ExitFileMenu;
2036         }
2037
2038         if (!initialized) {     
2039 //              set_screen_mode(SCREEN_MENU);
2040                 set_popup_screen();
2041
2042                 gr_set_current_canvas(NULL);
2043
2044                 grd_curcanv->cv_font = SUBTITLE_FONT;
2045
2046                 w_w = 0;
2047                 w_h = 0;
2048
2049                 for (i=0; i<NumFiles; i++ ) {
2050                         int w, h, aw;
2051                         gr_get_string_size( &filenames[i*14], &w, &h, &aw );            
2052                         if ( w > w_w )
2053                                 w_w = w;
2054                 }
2055                 if ( title ) {
2056                         int w, h, aw;
2057                         gr_get_string_size( title, &w, &h, &aw );               
2058                         if ( w > w_w )
2059                                 w_w = w;
2060                         title_height = h + (grd_curfont->ft_h*2);               // add a little space at the bottom of the title
2061                 }
2062
2063                 box_w = w_w;
2064                 box_h = ((grd_curfont->ft_h + 2) * NumFiles_displayed);
2065
2066                 w_w += (grd_curfont->ft_w * 4);
2067                 w_h = title_height + box_h + (grd_curfont->ft_h * 2);           // more space at bottom
2068
2069                 if ( w_w > grd_curcanv->cv_w ) w_w = grd_curcanv->cv_w;
2070                 if ( w_h > grd_curcanv->cv_h ) w_h = grd_curcanv->cv_h;
2071         
2072                 w_x = (grd_curcanv->cv_w-w_w)/2;
2073                 w_y = (grd_curcanv->cv_h-w_h)/2;
2074         
2075                 if ( w_x < 0 ) w_x = 0;
2076                 if ( w_y < 0 ) w_y = 0;
2077
2078                 box_x = w_x + (grd_curfont->ft_w*2);                    // must be in sync with w_w!!!
2079                 box_y = w_y + title_height;
2080
2081 // save the screen behind the menu.
2082
2083                 bg.saved = NULL;
2084
2085                 if ( (VR_offscreen_buffer->cv_w >= w_w) && (VR_offscreen_buffer->cv_h >= w_h) ) 
2086                         bg.background = &VR_offscreen_buffer->cv_bitmap;
2087                 else
2088                         bg.background = gr_create_bitmap( w_w, w_h );
2089
2090                 Assert( bg.background != NULL );
2091
2092
2093                 gr_bm_bitblt(w_w, w_h, 0, 0, w_x, w_y, &grd_curcanv->cv_bitmap, bg.background );
2094
2095 #if 0
2096                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) );
2097 #endif
2098
2099                 nm_draw_background( w_x,w_y,w_x+w_w-1,w_y+w_h-1 );
2100                 
2101                 gr_string( 0x8000, w_y+10, title );
2102          
2103                 initialized = 1;
2104         }
2105
2106         if ( !player_mode )     {
2107                 newmenu_file_sort( NumFiles, filenames );
2108         } else {
2109                 #if defined(MACINTOSH) && defined(APPLE_DEMO)
2110                 newmenu_file_sort( NumFiles, filenames );
2111                 #else
2112                 newmenu_file_sort( NumFiles-1, &filenames[14] );                // Don't sort first one!
2113                 #endif
2114                 for ( i=0; i<NumFiles; i++ )    {
2115                         if (!stricmp(Players[Player_num].callsign, &filenames[i*14]) )  {
2116 #ifdef NEWMENU_MOUSE
2117                                 dblclick_flag = 1;
2118 #endif
2119                                 citem = i;
2120                         }
2121                 }
2122         }
2123
2124 #ifdef NEWMENU_MOUSE
2125         mouse_state = omouse_state = 0;
2126         mouse2_state = omouse2_state = 0;
2127         draw_close_box(w_x,w_y);
2128         newmenu_show_cursor();
2129 #endif
2130
2131         while(!done)    {
2132                 ocitem = citem;
2133                 ofirst_item = first_item;
2134                 gr_update();
2135
2136 #ifdef NEWMENU_MOUSE
2137                 omouse_state = mouse_state;
2138                 omouse2_state = mouse2_state;
2139                 mouse_state = mouse_button_state(0);
2140                 mouse2_state = mouse_button_state(1);
2141 #endif
2142
2143                 //see if redbook song needs to be restarted
2144                 songs_check_redbook_repeat();
2145
2146                 key = key_inkey();
2147
2148                 switch(key)     {
2149                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
2150                 case KEY_PRINT_SCREEN:
2151                         MAC(newmenu_hide_cursor());
2152                         save_screen_shot(0);
2153                         
2154                         MAC(newmenu_show_cursor());
2155                         MAC(key_flush());
2156                         break;
2157
2158                 case KEY_CTRLED+KEY_D:
2159                         #if defined(MACINTOSH) && defined(APPLE_DEMO)
2160                         break;
2161                         #endif
2162
2163                         if ( ((player_mode)&&(citem>0)) || ((demo_mode)&&(citem>=0)) )  {
2164                                 int x = 1;
2165                                 newmenu_hide_cursor();
2166                                 if (player_mode)
2167                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2168                                 else if (demo_mode)
2169                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2170                                 newmenu_show_cursor();
2171                                 if (x==0)       {
2172                                         char * p;
2173                                         int ret;
2174                                         char name[PATH_MAX];
2175
2176                                         p = &filenames[(citem*14)+strlen(&filenames[citem*14])];
2177                                         if (player_mode)
2178                                                 *p = '.';
2179
2180                                         strcpy(name, demo_mode?DEMO_DIR:"");
2181                                         strcat(name,&filenames[citem*14]);
2182                                         
2183                                         #ifdef MACINTOSH
2184                                         {
2185                                                 int i;
2186                                                 char *p;
2187                                                 
2188                                                 if ( !strncmp(name, ".\\", 2) )
2189                                                         for (i = 0; i < strlen(name); i++)              // don't subtract 1 from strlen to get the EOS marker
2190                                                                 name[i] = name[i+1];
2191                                                 while ( (p = strchr(name, '\\')) )
2192                                                         *p = ':';
2193                                         }
2194                                         #endif
2195                                 
2196                                         ret = !PHYSFS_delete(name);
2197                                         if (player_mode)
2198                                                 *p = 0;
2199
2200                                         if ((!ret) && player_mode)      {
2201                                                 delete_player_saved_games( &filenames[citem*14] );
2202                                         }
2203
2204                                         if (ret) {
2205                                                 if (player_mode)
2206                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2207                                                 else if (demo_mode)
2208                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2209                                         } else if (demo_mode)
2210                                                 demos_deleted = 1;
2211                                         first_item = -1;
2212                                         goto ReadFileNames;
2213                                 }
2214                         }
2215                         break;
2216                 case KEY_HOME:
2217                 case KEY_PAD7:
2218                         citem = 0;
2219                         break;
2220                 case KEY_END:
2221                 case KEY_PAD1:
2222                         citem = NumFiles-1;
2223                         break;
2224                 case KEY_UP:
2225                 case KEY_PAD8:
2226                         citem--;                        
2227                         break;
2228                 case KEY_DOWN:
2229                 case KEY_PAD2:
2230                         citem++;                        
2231                         break;
2232                 case KEY_PAGEDOWN:
2233                 case KEY_PAD3:
2234                         citem += NumFiles_displayed;
2235                         break;
2236                 case KEY_PAGEUP:
2237                 case KEY_PAD9:
2238                         citem -= NumFiles_displayed;
2239                         break;
2240                 case KEY_ESC:
2241                         if (allow_abort_flag) {
2242                                 citem = -1;
2243                                 done = 1;
2244                         }
2245                         break;
2246                 case KEY_ENTER:
2247                 case KEY_PADENTER:
2248                         done = 1;
2249                         break;
2250                         
2251                 #ifdef MACINTOSH
2252                 case KEY_COMMAND+KEY_Q: {
2253                         extern void macintosh_quit();
2254                         
2255                         if ( !(Game_mode & GM_MULTI) )
2256                                 macintosh_quit();
2257                         newmenu_show_cursor();
2258                         key_flush();
2259                         break;
2260                 }
2261                 #endif
2262                 
2263                 default:        
2264                         {
2265
2266                                 int ascii = key_to_ascii(key);
2267                                 if ( ascii < 255 )      {
2268                                         int cc,cc1;
2269                                         cc=cc1=citem+1;
2270                                         if (cc1 < 0 )  cc1 = 0;
2271                                         if (cc1 >= NumFiles )  cc1 = 0;
2272                                         while(1) {
2273                                                 if ( cc < 0 ) cc = 0;
2274                                                 if ( cc >= NumFiles ) cc = 0;
2275                                                 if ( citem == cc ) break;
2276         
2277                                                 if ( toupper(filenames[cc*14]) == toupper(ascii) )      {
2278                                                         citem = cc;
2279                                                         break;
2280                                                 }
2281                                                 cc++;
2282                                         }
2283                                 }
2284                         }
2285                 }
2286                 if ( done ) break;
2287
2288
2289                 if (citem<0)
2290                         citem=0;
2291
2292                 if (citem>=NumFiles)
2293                         citem = NumFiles-1;
2294
2295                 if (citem< first_item)
2296                         first_item = citem;
2297
2298                 if (citem>=( first_item+NumFiles_displayed))
2299                 {
2300                         first_item = citem-NumFiles_displayed+1;
2301                 }
2302
2303 #ifdef WINDOWS
2304                 if (NumFiles>first_item+NumFiles_displayed)
2305                         show_down_arrow=1;
2306                 else 
2307                         show_down_arrow=0;
2308                 if (first_item>0)
2309                         show_up_arrow=1;
2310                 else    
2311                         show_up_arrow=0;
2312 #endif
2313                         
2314
2315                 if (NumFiles <= NumFiles_displayed )
2316                          first_item = 0;
2317
2318                 if (first_item>NumFiles-NumFiles_displayed)
2319                 {
2320                         first_item = NumFiles-NumFiles_displayed;
2321                 }
2322
2323                 if (first_item < 0 ) first_item = 0;
2324
2325 #ifdef NEWMENU_MOUSE
2326                 if (mouse_state || mouse2_state) {
2327                         int w, h, aw;
2328
2329                         mouse_get_pos(&mx, &my);
2330                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
2331                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2332                                 x1 = box_x;
2333                                 x2 = box_x + box_w - 1;
2334                                 y1 = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
2335                                 y2 = y1+h+1;
2336                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2337                                         if (i == citem && !mouse2_state) {
2338                                                 break;
2339                                         }
2340                                         citem = i;
2341                                         dblclick_flag = 0;
2342                                         break;
2343                                 }
2344                         }
2345                 }
2346                 
2347                 if (!mouse_state && omouse_state) {
2348                         int w, h, aw;
2349
2350                         gr_get_string_size(&filenames[citem*14], &w, &h, &aw  );
2351                         mouse_get_pos(&mx, &my);
2352                         x1 = box_x;
2353                         x2 = box_x + box_w - 1;
2354                         y1 = (citem-first_item)*(grd_curfont->ft_h + 2) + box_y;
2355                         y2 = y1+h+1;
2356                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2357                                 if (dblclick_flag) done = 1;
2358                                 else dblclick_flag = 1;
2359                         }
2360                 }
2361
2362                 if ( !mouse_state && omouse_state ) {
2363                         mouse_get_pos(&mx, &my);
2364                         x1 = w_x + CLOSE_X + 2;
2365                         x2 = x1 + CLOSE_SIZE - 2;
2366                         y1 = w_y + CLOSE_Y + 2;
2367                         y2 = y1 + CLOSE_SIZE - 2;
2368                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2369                                 citem = -1;
2370                                 done = 1;
2371                         }
2372                    #ifdef WINDOWS
2373                         x1 = box_x-LHX(10);
2374                         x2 = x1 + LHX(10);
2375                         y1 = box_y;
2376                         y2 = box_y+LHY(7);
2377                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow ) 
2378                                 simukey = -1;
2379                         y1 = box_y+box_h-LHY(7);
2380                         y2 = box_y+box_h;
2381                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow) 
2382                                 simukey = 1;
2383                    #endif
2384                 }
2385
2386 #endif
2387   
2388                 gr_setcolor( BM_XRGB(2,2,2));
2389                 //gr_rect( box_x - 1, box_y-2, box_x + box_w, box_y-2 );
2390                 gr_setcolor( BM_XRGB( 0,0,0)  );
2391
2392                 if (ofirst_item != first_item)  {
2393                         newmenu_hide_cursor();
2394                         gr_setcolor( BM_XRGB( 0,0,0)  );
2395                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
2396                                 int w, h, aw, y;
2397                                 y = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
2398                         
2399                                 if ( i >= NumFiles )    {
2400
2401                                         gr_setcolor( BM_XRGB(5,5,5));
2402                                         gr_rect( box_x + box_w, y-1, box_x + box_w, y + grd_curfont->ft_h + 1);
2403                                         //gr_rect( box_x, y + grd_curfont->ft_h + 2, box_x + box_w, y + grd_curfont->ft_h + 2);
2404                                         
2405                                         gr_setcolor( BM_XRGB(2,2,2));
2406                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + grd_curfont->ft_h + 2 );
2407                                         
2408                                         gr_setcolor( BM_XRGB(0,0,0));
2409                                         gr_rect( box_x, y - 1, box_x + box_w - 1, y + grd_curfont->ft_h + 1);
2410                                         
2411                                 } else {
2412                                         if ( i == citem )       
2413                                                 grd_curcanv->cv_font = SELECTED_FONT;
2414                                         else    
2415                                                 grd_curcanv->cv_font = NORMAL_FONT;
2416                                         gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2417
2418                                         gr_setcolor( BM_XRGB(5,5,5));
2419                                   //    gr_rect( box_x, y + h + 2, box_x + box_w, y + h + 2);
2420                                         gr_rect( box_x + box_w, y - 1, box_x + box_w, y + h + 1);
2421                                         
2422                                         gr_setcolor( BM_XRGB(2,2,2));
2423                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + h + 1);
2424                                         gr_setcolor( BM_XRGB(0,0,0));
2425                                                         
2426                                         gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
2427                                         gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
2428                                 }
2429                         }        
2430                         newmenu_show_cursor();
2431                 } else if ( citem != ocitem )   {
2432                         int w, h, aw, y;
2433
2434                         newmenu_hide_cursor();
2435                         i = ocitem;
2436                         if ( (i>=0) && (i<NumFiles) )   {
2437                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
2438                                 if ( i == citem )       
2439                                         grd_curcanv->cv_font = SELECTED_FONT;
2440                                 else    
2441                                         grd_curcanv->cv_font = NORMAL_FONT;
2442                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2443                                 gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
2444                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
2445                         }
2446                         i = citem;
2447                         if ( (i>=0) && (i<NumFiles) )   {
2448                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
2449                                 if ( i == citem )       
2450                                         grd_curcanv->cv_font = SELECTED_FONT;
2451                                 else    
2452                                         grd_curcanv->cv_font = NORMAL_FONT;
2453                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2454                                 gr_rect( box_x, y-1, box_x + box_x - 1, y + h + 1 );
2455                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
2456                         }
2457                         newmenu_show_cursor();
2458                 }
2459
2460         #ifdef WINDOWS   
2461                         grd_curcanv->cv_font = NORMAL_FONT;
2462                         if (show_up_arrow)
2463                                 gr_string( box_x-LHX(10), box_y ,UP_ARROW_MARKER );
2464                         else
2465                         {
2466                                 No_darkening=1;
2467                                 nm_draw_background (box_x-LHX(10),box_y,box_x-2,box_y+LHY(7));
2468                                 No_darkening=0;
2469                         }
2470
2471                         if (show_down_arrow)
2472                         gr_string( box_x-LHX(10), box_y+box_h-LHY(7) ,DOWN_ARROW_MARKER );
2473                         else
2474                         {
2475                                 No_darkening=1;
2476                                 nm_draw_background (box_x-LHX(10),box_y+box_h-LHY(7),box_x-2,box_y+box_h);
2477                                 No_darkening=0;
2478                         }
2479
2480         #endif
2481         }
2482
2483 ExitFileMenuEarly:
2484         MAC(newmenu_hide_cursor());
2485         if ( citem > -1 )       {
2486                 strncpy( filename, (&filenames[citem*14])+((player_mode && filenames[citem*14]=='$')?1:0), FILENAME_LEN );
2487                 exit_value = 1;
2488         } else {
2489                 exit_value = 0;
2490         }                                                                                        
2491
2492 ExitFileMenu:
2493         keyd_repeat = old_keyd_repeat;
2494
2495         if ( initialized )      {
2496                 if (Newdemo_state != ND_STATE_PLAYBACK) //horrible hack to prevent restore when screen has been cleared
2497                         gr_bm_bitblt(w_w, w_h, w_x, w_y, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
2498                 if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
2499                         gr_free_bitmap(bg.background);
2500 #if 0
2501                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) )
2502 #endif
2503         }
2504
2505         if ( filenames )
2506                 d_free(filenames);
2507
2508         return exit_value;
2509
2510 }
2511
2512
2513 // Example listbox callback function...
2514 // int lb_callback( int * citem, int *nitems, char * items[], int *keypress )
2515 // {
2516 //      int i;
2517 // 
2518 //      if ( *keypress = KEY_CTRLED+KEY_D )     {
2519 //              if ( *nitems > 1 )      {
2520 //                      PHYSFS_delete(items[*citem]);     // Delete the file
2521 //                      for (i=*citem; i<*nitems-1; i++ )       {
2522 //                              items[i] = items[i+1];
2523 //                      }
2524 //                      *nitems = *nitems - 1;
2525 //                      d_free( items[*nitems] );
2526 //                      items[*nitems] = NULL;
2527 //                      return 1;       // redraw;
2528 //              }
2529 //                      *keypress = 0;
2530 //      }                       
2531 //      return 0;
2532 // }
2533
2534 #define LB_ITEMS_ON_SCREEN 8
2535
2536 int newmenu_listbox( char * title, int nitems, char * items[], int allow_abort_flag, int (*listbox_callback)( int * citem, int *nitems, char * items[], int *keypress ) )
2537 {
2538         return newmenu_listbox1( title, nitems, items, allow_abort_flag, 0, listbox_callback );
2539 }
2540
2541 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 ) )
2542 {
2543         int i;
2544         int done, ocitem,citem, ofirst_item, first_item, key, redraw;
2545         int old_keyd_repeat = keyd_repeat;
2546         int width, height, wx, wy, title_height, border_size;
2547         int total_width,total_height;
2548         bkg bg;
2549 #ifdef NEWMENU_MOUSE
2550         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;  //, dblclick_flag;
2551         int close_x,close_y;
2552 # ifdef WINDOWS
2553    int simukey=0,show_up_arrow=0,show_down_arrow=0;
2554 # endif
2555 #endif
2556
2557         keyd_repeat = 1;
2558
2559 //      set_screen_mode(SCREEN_MENU);
2560         set_popup_screen();
2561
2562         gr_set_current_canvas(NULL);
2563
2564         grd_curcanv->cv_font = SUBTITLE_FONT;
2565
2566         width = 0;
2567         for (i=0; i<nitems; i++ )       {
2568                 int w, h, aw;
2569                 gr_get_string_size( items[i], &w, &h, &aw );            
2570                 if ( w > width )
2571                         width = w;
2572         }
2573         height = (grd_curfont->ft_h + 2) * LB_ITEMS_ON_SCREEN;
2574
2575         {
2576                 int w, h, aw;
2577                 gr_get_string_size( title, &w, &h, &aw );               
2578                 if ( w > width )
2579                         width = w;
2580                 title_height = h + 5;
2581         }
2582
2583         border_size = grd_curfont->ft_w;
2584    WIN (border_size=grd_curfont->ft_w*2);
2585                 
2586         width += (grd_curfont->ft_w);
2587         if ( width > grd_curcanv->cv_w - (grd_curfont->ft_w * 3) )
2588                 width = grd_curcanv->cv_w - (grd_curfont->ft_w * 3);
2589
2590         wx = (grd_curcanv->cv_bitmap.bm_w-width)/2;
2591         wy = (grd_curcanv->cv_bitmap.bm_h-(height+title_height))/2 + title_height;
2592         if ( wy < title_height )
2593                 wy = title_height;
2594
2595         total_width = width+2*border_size;
2596         total_height = height+2*border_size+title_height;
2597
2598         bg.saved = NULL;
2599
2600         if ( (VR_offscreen_buffer->cv_w >= total_width) && (VR_offscreen_buffer->cv_h >= total_height) )
2601                 bg.background = &VR_offscreen_buffer->cv_bitmap;
2602         else
2603                 //bg.background = gr_create_bitmap( width, (height + title_height) );
2604                 bg.background = gr_create_bitmap(total_width,total_height);
2605         Assert( bg.background != NULL );
2606                 
2607         //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 );
2608         gr_bm_bitblt(total_width,total_height, 0, 0, wx-border_size, wy-title_height-border_size, &grd_curcanv->cv_bitmap, bg.background );
2609
2610 #if 0
2611         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) );
2612 #endif
2613
2614         nm_draw_background( wx-border_size,wy-title_height-border_size,wx+width+border_size-1,wy+height+border_size-1 );
2615
2616         gr_string( 0x8000, wy - title_height, title );
2617
2618         done = 0;
2619         citem = default_item;
2620         if ( citem < 0 ) citem = 0;
2621         if ( citem >= nitems ) citem = 0;
2622
2623         first_item = -1;
2624
2625 #ifdef NEWMENU_MOUSE
2626         mouse_state = omouse_state = 0; //dblclick_flag = 0;
2627         close_x = wx-border_size;
2628         close_y = wy-title_height-border_size;
2629         draw_close_box(close_x,close_y);
2630         newmenu_show_cursor();
2631 #endif
2632
2633         while(!done)    {
2634                 ocitem = citem;
2635                 ofirst_item = first_item;
2636 #ifdef NEWMENU_MOUSE
2637                 omouse_state = mouse_state;
2638                 mouse_state = mouse_button_state(0);
2639 #endif
2640                 //see if redbook song needs to be restarted
2641                 songs_check_redbook_repeat();
2642
2643                 key = key_inkey();
2644
2645                 if ( listbox_callback )
2646                         redraw = (*listbox_callback)(&citem, &nitems, items, &key );
2647                 else
2648                         redraw = 0;
2649
2650                 if ( key<-1 ) {
2651                         citem = key;
2652                         key = -1;
2653                         done = 1;
2654                 }
2655
2656
2657         #ifdef WINDOWS
2658                 if (simukey==-1)
2659                         key=KEY_UP;
2660                 else if (simukey==1)
2661                    key=KEY_DOWN;
2662                 simukey=0;
2663         #endif
2664                 
2665                 switch(key)     {
2666                 MAC(case KEY_COMMAND+KEY_SHIFTED+KEY_3:)
2667                 case KEY_PRINT_SCREEN:          
2668                         MAC(newmenu_hide_cursor());
2669                         save_screen_shot(0); 
2670                         
2671                         MAC(newmenu_show_cursor());
2672                         MAC(key_flush());
2673                         break;
2674                 case KEY_HOME:
2675                 case KEY_PAD7:
2676                         citem = 0;
2677                         break;
2678                 case KEY_END:
2679                 case KEY_PAD1:
2680                         citem = nitems-1;
2681                         break;
2682                 case KEY_UP:
2683                 case KEY_PAD8:
2684                         citem--;                        
2685                         break;
2686                 case KEY_DOWN:
2687                 case KEY_PAD2:
2688                         citem++;                        
2689                         break;
2690                 case KEY_PAGEDOWN:
2691                 case KEY_PAD3:
2692                         citem += LB_ITEMS_ON_SCREEN;
2693                         break;
2694                 case KEY_PAGEUP:
2695                 case KEY_PAD9:
2696                         citem -= LB_ITEMS_ON_SCREEN;
2697                         break;
2698                 case KEY_ESC:
2699                         if (allow_abort_flag) {
2700                                 citem = -1;
2701                                 done = 1;
2702                         }
2703                         break;
2704                 case KEY_ENTER:
2705                 case KEY_PADENTER:
2706                         done = 1;
2707                         break;
2708
2709                 #ifdef MACINTOSH
2710                 case KEY_COMMAND+KEY_Q: {
2711                         extern void macintosh_quit();
2712                         
2713                         if ( !(Game_mode & GM_MULTI) )
2714                                 macintosh_quit();
2715                         newmenu_show_cursor();
2716                         key_flush();
2717                         break;
2718                 }
2719                 #endif
2720
2721                 default:        
2722                         if ( key > 0 )  {
2723                                 int ascii = key_to_ascii(key);
2724                                 if ( ascii < 255 )      {
2725                                         int cc,cc1;
2726                                         cc=cc1=citem+1;
2727                                         if (cc1 < 0 )  cc1 = 0;
2728                                         if (cc1 >= nitems )  cc1 = 0;
2729                                         while(1) {
2730                                                 if ( cc < 0 ) cc = 0;
2731                                                 if ( cc >= nitems ) cc = 0;
2732                                                 if ( citem == cc ) break;
2733         
2734                                                 if ( toupper( items[cc][0] ) == toupper(ascii) )        {
2735                                                         citem = cc;
2736                                                         break;
2737                                                 }
2738                                                 cc++;
2739                                         }
2740                                 }
2741                         }
2742                 }
2743                 if ( done ) break;
2744
2745                 if (citem<0)
2746                         citem=0;
2747
2748                 if (citem>=nitems)
2749                         citem = nitems-1;
2750
2751                 if (citem< first_item)
2752                         first_item = citem;
2753
2754                 if (citem>=( first_item+LB_ITEMS_ON_SCREEN))
2755                         first_item = citem-LB_ITEMS_ON_SCREEN+1;
2756
2757                 if (nitems <= LB_ITEMS_ON_SCREEN )
2758                          first_item = 0;
2759
2760                 if (first_item>nitems-LB_ITEMS_ON_SCREEN)
2761                         first_item = nitems-LB_ITEMS_ON_SCREEN;
2762                 if (first_item < 0 ) first_item = 0;
2763
2764 #ifdef WINDOWS
2765                 if (nitems>first_item+LB_ITEMS_ON_SCREEN)
2766                         show_down_arrow=1;
2767                 else 
2768                         show_down_arrow=0;
2769                 if (first_item>0)
2770                         show_up_arrow=1;
2771                 else    
2772                         show_up_arrow=0;
2773 #endif
2774
2775
2776 #ifdef NEWMENU_MOUSE
2777                 if (mouse_state) {
2778                         int w, h, aw;
2779
2780                         mouse_get_pos(&mx, &my);
2781                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
2782                                 if (i > nitems)
2783                                         break;
2784                                 gr_get_string_size(items[i], &w, &h, &aw  );
2785                                 x1 = wx;
2786                                 x2 = wx + width;
2787                                 y1 = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2788                                 y2 = y1+h+1;
2789                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2790                                         //if (i == citem) {
2791                                         //      break;
2792                                         //}
2793                                         //dblclick_flag= 0;
2794                                         citem = i;
2795                                         done = 1;
2796                                         break;
2797                                 }
2798                         }
2799                 }
2800
2801                 //no double-click stuff for listbox
2802                 //@@if (!mouse_state && omouse_state) {
2803                 //@@    int w, h, aw;
2804                 //@@
2805                 //@@    gr_get_string_size(items[citem], &w, &h, &aw  );
2806                 //@@    mouse_get_pos(&mx, &my);
2807                 //@@    x1 = wx;
2808                 //@@    x2 = wx + width;
2809                 //@@    y1 = (citem-first_item)*(grd_curfont->ft_h+2)+wy;
2810                 //@@    y2 = y1+h+1;
2811                 //@@    if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2812                 //@@            if (dblclick_flag) done = 1;
2813                 //@@    }
2814                 //@@}
2815
2816                 //check for close box clicked
2817                 if ( !mouse_state && omouse_state ) {
2818                         mouse_get_pos(&mx, &my);
2819                         x1 = close_x + CLOSE_X + 2;
2820                         x2 = x1 + CLOSE_SIZE - 2;
2821                         y1 = close_y + CLOSE_Y + 2;
2822                         y2 = y1 + CLOSE_SIZE - 2;
2823                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2824                                 citem = -1;
2825                                 done = 1;
2826                         }
2827
2828                    #ifdef WINDOWS
2829                         x1 = wx-LHX(10);
2830                         x2 = x1 + LHX(10);
2831                         y1 = wy;
2832                         y2 = wy+LHY(7);
2833                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow) 
2834                                 simukey = -1;
2835                         y1 = total_height-LHY(7);
2836                         y2 = total_height;
2837                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow ) 
2838                                 simukey = 1;
2839                    #endif
2840
2841                         
2842                 }
2843 #endif
2844
2845                 if ( (ofirst_item != first_item) || redraw)     {
2846                         newmenu_hide_cursor();
2847
2848                         gr_setcolor( BM_XRGB( 0,0,0)  );
2849                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
2850                                 int w, h, aw, y;
2851                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2852                                 if ( i >= nitems )      {
2853                                         gr_setcolor( BM_XRGB(0,0,0));
2854                                         gr_rect( wx, y-1, wx+width-1, y+grd_curfont->ft_h + 1 );
2855                                 } else {
2856                                         if ( i == citem )       
2857                                                 grd_curcanv->cv_font = SELECTED_FONT;
2858                                         else    
2859                                                 grd_curcanv->cv_font = NORMAL_FONT;
2860                                         gr_get_string_size(items[i], &w, &h, &aw  );
2861                                         gr_rect( wx, y-1, wx+width-1, y+h+1 );
2862                                         gr_string( wx+5, y, items[i]  );
2863                                 }
2864                         }               
2865
2866                                 
2867                         // If Win95 port, draw up/down arrows on left side of menu
2868                         #ifdef WINDOWS   
2869                                 grd_curcanv->cv_font = NORMAL_FONT;
2870                         if (show_up_arrow)
2871                                 gr_string( wx-LHX(10), wy ,UP_ARROW_MARKER );
2872                         else
2873                         {
2874                                 No_darkening=1;
2875                                 nm_draw_background (wx-LHX(10),wy,wx-2,wy+LHY(7));
2876                                 No_darkening=0;
2877                         }
2878
2879                         if (show_down_arrow)
2880                         gr_string( wx-LHX(10), wy+total_height-LHY(7) ,DOWN_ARROW_MARKER );
2881                         else
2882                         {
2883                                 No_darkening=1;
2884                                 nm_draw_background (wx-LHX(10),wy+total_height-LHY(7),wx-2,wy+total_height);
2885                                 No_darkening=0;
2886                         }
2887
2888                         #endif
2889
2890
2891                         newmenu_show_cursor();
2892                         gr_update();
2893                 } else if ( citem != ocitem )   {
2894                         int w, h, aw, y;
2895
2896                         newmenu_hide_cursor();
2897
2898                         i = ocitem;
2899                         if ( (i>=0) && (i<nitems) )     {
2900                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2901                                 if ( i == citem )       
2902                                         grd_curcanv->cv_font = SELECTED_FONT;
2903                                 else    
2904                                         grd_curcanv->cv_font = NORMAL_FONT;
2905                                 gr_get_string_size(items[i], &w, &h, &aw  );
2906                                 gr_rect( wx, y-1, wx+width-1, y+h+1 );
2907                                 gr_string( wx+5, y, items[i]  );
2908
2909                         }
2910                         i = citem;
2911                         if ( (i>=0) && (i<nitems) )     {
2912                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2913                                 if ( i == citem )       
2914                                         grd_curcanv->cv_font = SELECTED_FONT;
2915                                 else    
2916                                         grd_curcanv->cv_font = NORMAL_FONT;
2917                                 gr_get_string_size( items[i], &w, &h, &aw  );
2918                                 gr_rect( wx, y-1, wx+width-1, y+h );
2919                                 gr_string( wx+5, y, items[i]  );
2920                         }
2921
2922                         newmenu_show_cursor();
2923                         gr_update();
2924                 }
2925         }
2926         newmenu_hide_cursor();
2927
2928         keyd_repeat = old_keyd_repeat;
2929
2930         gr_bm_bitblt(total_width,total_height, wx-border_size, wy-title_height-border_size, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
2931
2932         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
2933                 gr_free_bitmap(bg.background);
2934
2935 #if 0
2936         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) );
2937 #endif
2938
2939         return citem;
2940 }
2941
2942 #if 0
2943 int newmenu_filelist( char * title, char * filespec, char * filename )
2944 {
2945         int i, NumFiles;
2946         char * Filenames[MAX_FILES];
2947         char FilenameText[MAX_FILES][14];
2948         FILEFINDSTRUCT find;
2949
2950         NumFiles = 0;
2951         if( !FileFindFirst( filespec, &find ) ) {
2952                 do      {
2953                         if (NumFiles<MAX_FILES) {
2954                                 strncpy( FilenameText[NumFiles], find.name, FILENAME_LEN);
2955                                 Filenames[NumFiles] = FilenameText[NumFiles];
2956                                 NumFiles++;
2957                         } else {
2958                                 break;
2959                         }
2960                 } while( !FileFindNext( &find ) );
2961                 FileFindClose();
2962         }
2963
2964         i = newmenu_listbox( title, NumFiles, Filenames, 1, NULL );
2965         if ( i > -1 )   {
2966                 strcpy( filename, Filenames[i] );
2967                 return 1;
2968         } 
2969         return 0;
2970 }
2971 #endif
2972
2973 //added on 10/14/98 by Victor Rachels to attempt a fixedwidth font messagebox
2974 int nm_messagebox_fixedfont( char *title, int nchoices, ... )
2975 {
2976         int i;
2977         char * format;
2978         va_list args;
2979         char *s;
2980         char nm_text[MESSAGEBOX_TEXT_SIZE];
2981         newmenu_item nm_message_items[5];
2982
2983         va_start(args, nchoices );
2984
2985         Assert( nchoices <= 5 );
2986
2987         for (i=0; i<nchoices; i++ )     {
2988                 s = va_arg( args, char * );
2989                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
2990         }
2991         format = va_arg( args, char * );
2992         //sprintf(        nm_text, "" ); // adb: ?
2993         vsprintf(nm_text,format,args);
2994         va_end(args);
2995
2996         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
2997
2998         return newmenu_do_fixedfont( title, nm_text, nchoices, nm_message_items, NULL, 0, NULL, -1, -1 );
2999 }
3000 //end this section addition - Victor Rachels
3001
3002 #ifdef NETWORK
3003 extern netgame_info Active_games[];
3004 extern int NumActiveNetgames;
3005
3006 void show_extra_netgame_info(int choice)
3007  {
3008         newmenu_item m[5];
3009    char mtext[5][50];
3010         int i,num=0;
3011
3012         if (choice>=NumActiveNetgames)
3013                 return;
3014         
3015    for (i=0;i<5;i++)
3016         {
3017          m[i].text=(char *)&mtext[i];
3018     m[i].type=NM_TYPE_TEXT;             
3019         }
3020
3021    sprintf (mtext[num],"Game: %s",Active_games[choice].game_name); num++;
3022    sprintf (mtext[num],"Mission: %s",Active_games[choice].mission_title); num++;
3023         sprintf (mtext[num],"Current Level: %d",Active_games[choice].levelnum); num++;
3024         sprintf (mtext[num],"Difficulty: %s",MENU_DIFFICULTY_TEXT(Active_games[choice].difficulty)); num++;
3025
3026         already_showing_info=1; 
3027         newmenu_dotiny2( NULL, "Netgame Information", num, m, NULL);
3028         already_showing_info=0; 
3029  }
3030
3031 #endif // NETWORK
3032
3033 /* Spiffy word wrap string formatting function */
3034
3035 void nm_wrap_text(char *dbuf, char *sbuf, int line_length)
3036 {
3037         int col;
3038         char *wordptr;
3039         char *tbuf;
3040
3041         tbuf = (char *)d_malloc(strlen(sbuf)+1);
3042         strcpy(tbuf, sbuf);
3043
3044         wordptr = strtok(tbuf, " ");
3045         if (!wordptr) return;
3046         col = 0;
3047         dbuf[0] = 0;
3048
3049         while (wordptr)
3050         {
3051                 col = col+strlen(wordptr)+1;
3052                 if (col >=line_length) {
3053                         col = 0;
3054                         sprintf(dbuf, "%s\n%s ", dbuf, wordptr);
3055                 }
3056                 else {
3057                         sprintf(dbuf, "%s%s ", dbuf, wordptr);
3058                 }
3059                 wordptr = strtok(NULL, " ");
3060         }
3061
3062         d_free(tbuf);
3063 }