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