]> icculus.org git repositories - btb/d2x.git/blob - main/newmenu.c
separate control type into separate cvars for joy and mouse
[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,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,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 = 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, 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;
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) item[choice].value = strlen(item[choice].text);
1682                                         if (item[choice].value > 0)
1683                                                 item[choice].value--;
1684                                         item[choice].text[item[choice].value] = 0;
1685                                         item[choice].redraw = 1;        
1686                                 } else {
1687                                         ascii = key_to_ascii(k);
1688                                         if ((ascii < 255 ) && (item[choice].value < item[choice].text_len ))
1689                                         {
1690                                                 int allowed;
1691
1692                                                 if (item[choice].value==-1) {
1693                                                         item[choice].value = 0;
1694                                                 }
1695
1696                                                 allowed = char_allowed(ascii);
1697
1698                                                 if (!allowed && ascii==' ' && char_allowed('_')) {
1699                                                         ascii = '_';
1700                                                         allowed=1;
1701                                                 }
1702
1703                                                 if (allowed) {
1704                                                         item[choice].text[item[choice].value++] = ascii;
1705                                                         item[choice].text[item[choice].value] = 0;
1706                                                         item[choice].redraw=1;  
1707                                                 }
1708                                         }
1709                                 }
1710                         } else if ((item[choice].type!=NM_TYPE_INPUT) && (item[choice].type!=NM_TYPE_INPUT_MENU) ) {
1711                                 ascii = key_to_ascii(k);
1712                                 if (ascii < 255 ) {
1713                                         int choice1 = choice;
1714                                         ascii = toupper(ascii);
1715                                         do {
1716                                                 int i,ch;
1717                                                 choice1++;
1718                                                 if (choice1 >= nitems ) choice1=0;
1719                                                 for (i=0;(ch=item[choice1].text[i])!=0 && ch==' ';i++);
1720                                                 if ( ( (item[choice1].type==NM_TYPE_MENU) ||
1721                                                                  (item[choice1].type==NM_TYPE_CHECK) ||
1722                                                                  (item[choice1].type==NM_TYPE_RADIO) ||
1723                                                                  (item[choice1].type==NM_TYPE_NUMBER) ||
1724                                                                  (item[choice1].type==NM_TYPE_SLIDER) )
1725                                                                 && (ascii==toupper(ch)) )       {
1726                                                         k = 0;
1727                                                         choice = choice1;
1728                                                         if (old_choice>-1)
1729                                                                 item[old_choice].redraw=1;
1730                                                         item[choice].redraw=1;
1731                                                 }
1732                                         } while (choice1 != choice );
1733                                 }       
1734                         }
1735
1736                         if ( (item[choice].type==NM_TYPE_NUMBER) || (item[choice].type==NM_TYPE_SLIDER))        {
1737                                 int ov=item[choice].value;
1738                                 switch( k ) {
1739                                 case KEY_PAD4:
1740                                 case KEY_LEFT:
1741                                 case KEY_MINUS:
1742                                 case KEY_MINUS+KEY_SHIFTED:
1743                                 case KEY_PADMINUS:
1744                                         item[choice].value -= 1;
1745                                         break;
1746                                 case KEY_RIGHT:
1747                                 case KEY_PAD6:
1748                                 case KEY_EQUAL:
1749                                 case KEY_EQUAL+KEY_SHIFTED:
1750                                 case KEY_PADPLUS:
1751                                         item[choice].value++;
1752                                         break;
1753                                 case KEY_PAGEUP:
1754                                 case KEY_PAD9:
1755                                 case KEY_SPACEBAR:
1756                                         item[choice].value += 10;
1757                                         break;
1758                                 case KEY_PAGEDOWN:
1759                                 case KEY_BACKSP:
1760                                 case KEY_PAD3:
1761                                         item[choice].value -= 10;
1762                                         break;
1763                                 }
1764                                 if (ov!=item[choice].value)
1765                                         item[choice].redraw=1;
1766                         }
1767         
1768                 }
1769
1770                 gr_set_current_canvas(bg.menu_canvas);
1771
1772         // Redraw everything...
1773         for (i=ScrollOffset; i<MaxDisplayable+ScrollOffset; i++ )
1774         {
1775         if (item[i].redraw) // warning! ugly hack below                  
1776                 {
1777                 item[i].y-=((string_height+1)*ScrollOffset);
1778                         newmenu_hide_cursor();
1779                 draw_item( &bg, &item[i], (i==choice && !all_text),TinyMode );
1780                                 item[i].redraw=0;
1781 #ifdef NEWMENU_MOUSE
1782                                 if (!MenuReordering && !joydefs_calibrating)
1783                                         newmenu_show_cursor();
1784 #endif
1785             item[i].y+=((string_height+1)*ScrollOffset);
1786                 }   
1787          if (i==choice && (item[i].type==NM_TYPE_INPUT || (item[i].type==NM_TYPE_INPUT_MENU && item[i].group)))
1788                                 update_cursor( &item[i]);
1789                 }
1790         gr_update();
1791
1792       if (IsScrollBox)
1793         {
1794         //grd_curcanv->cv_font = NORMAL_FONT;
1795         
1796                 if (LastScrollCheck!=ScrollOffset)
1797          {
1798                 LastScrollCheck=ScrollOffset;
1799                 grd_curcanv->cv_font = SELECTED_FONT;
1800                                 
1801                 sy=item[ScrollOffset].y-((string_height+1)*ScrollOffset);
1802                 sx=item[ScrollOffset].x-(MenuHires?24:12);
1803                                 
1804           
1805                 if (ScrollOffset!=0)
1806                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, UP_ARROW_MARKER );
1807                 else
1808                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
1809
1810                 sy=item[ScrollOffset+MaxDisplayable-1].y-((string_height+1)*ScrollOffset);
1811                 sx=item[ScrollOffset+MaxDisplayable-1].x-(MenuHires?24:12);
1812           
1813                 if (ScrollOffset+MaxDisplayable<nitems)
1814                         nm_rstring( &bg, (MenuHires?20:10), sx, sy, DOWN_ARROW_MARKER );
1815                 else
1816                 nm_rstring( &bg, (MenuHires?20:10), sx, sy, "  " );
1817
1818         }
1819
1820         }   
1821
1822                 if ( !dont_restore && gr_palette_faded_out )    {
1823                         gr_palette_fade_in( gr_palette, 32, 0 );
1824                 }
1825         }
1826
1827         newmenu_hide_cursor();
1828
1829         // Restore everything...
1830
1831         gr_set_current_canvas(bg.menu_canvas);
1832
1833         if ( filename == NULL ) {
1834                 // Save the background under the menu...
1835                 gr_bitmap(0, 0, bg.saved);      
1836                 gr_free_bitmap(bg.saved);
1837                 d_free( bg.background );
1838         } else {
1839                 if (!dont_restore)      //info passed back from subfunction
1840                         gr_bitmap(0, 0, bg.background);
1841                 gr_free_bitmap(bg.background);
1842         }
1843
1844         gr_free_sub_canvas( bg.menu_canvas );
1845
1846         gr_set_current_canvas(save_canvas);
1847         grd_curcanv->cv_font    = save_font;
1848         keyd_repeat = old_keyd_repeat;
1849
1850         game_flush_inputs();
1851
1852         if (time_stopped) 
1853      {
1854                 start_time();
1855                 #ifdef TACTILE
1856                         if (TactileStick)
1857                                 EnableForces();
1858                 #endif
1859           }
1860
1861         if ( sound_stopped )
1862                 digi_resume_digi_sounds();
1863
1864         return choice;
1865         
1866 }
1867
1868
1869 int nm_messagebox1( char *title, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem), int nchoices, ... )
1870 {
1871         int i;
1872         char * format;
1873         va_list args;
1874         char *s;
1875         char nm_text[MESSAGEBOX_TEXT_SIZE];
1876         newmenu_item nm_message_items[5];
1877
1878         va_start(args, nchoices );
1879
1880         Assert( nchoices <= 5 );
1881
1882         for (i=0; i<nchoices; i++ )     {
1883                 s = va_arg( args, char * );
1884                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
1885         }
1886         format = va_arg( args, char * );
1887         strcpy( nm_text, "" );
1888         vsprintf(nm_text,format,args);
1889         va_end(args);
1890
1891         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE);
1892
1893         return newmenu_do( title, nm_text, nchoices, nm_message_items, subfunction );
1894 }
1895
1896 int nm_messagebox( char *title, int nchoices, ... )
1897 {
1898         int i;
1899         char * format;
1900         va_list args;
1901         char *s;
1902         char nm_text[MESSAGEBOX_TEXT_SIZE];
1903         newmenu_item nm_message_items[5];
1904
1905         va_start(args, nchoices );
1906
1907         Assert( nchoices <= 5 );
1908
1909         for (i=0; i<nchoices; i++ )     {
1910                 s = va_arg( args, char * );
1911                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
1912         }
1913         format = va_arg( args, char * );
1914         strcpy( nm_text, "" );
1915         vsprintf(nm_text,format,args);
1916         va_end(args);
1917
1918         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
1919
1920         return newmenu_do( title, nm_text, nchoices, nm_message_items, NULL );
1921 }
1922
1923
1924
1925
1926 void newmenu_file_sort( int n, char *list )
1927 {
1928         int i, j, incr;
1929         char t[14];
1930
1931         incr = n / 2;
1932         while( incr > 0 )               {
1933                 for (i=incr; i<n; i++ )         {
1934                         j = i - incr;
1935                         while (j>=0 )                   {
1936                                 if (strncmp(&list[j*14], &list[(j+incr)*14], 12) > 0)                           {
1937                                         memcpy( t, &list[j*14], FILENAME_LEN );
1938                                         memcpy( &list[j*14], &list[(j+incr)*14], FILENAME_LEN );
1939                                         memcpy( &list[(j+incr)*14], t, FILENAME_LEN );
1940                                         j -= incr;
1941                                 }
1942                                 else
1943                                         break;
1944                         }
1945                 }
1946                 incr = incr / 2;
1947         }
1948 }
1949
1950 void delete_player_saved_games(char * name)
1951 {
1952         int i;
1953         char filename[16];
1954
1955         for (i=0;i<10; i++)     {
1956                 sprintf( filename, PLAYER_DIR "%s.sg%d", name, i );
1957                 PHYSFS_delete(filename);
1958         }
1959 }
1960
1961 #define MAX_FILES 300
1962
1963 //FIXME: should maybe put globbing ability back?
1964 int newmenu_get_filename(char *title, char *type, char *filename, int allow_abort_flag)
1965 {
1966         int i;
1967         char **find;
1968         char **f;
1969         char *ext;
1970         int NumFiles=0, key,done, citem, ocitem;
1971         char * filenames = NULL;
1972         int NumFiles_displayed = 8;
1973         int first_item = -1, ofirst_item;
1974         int old_keyd_repeat = keyd_repeat;
1975         int player_mode=0;
1976         int demo_mode=0;
1977         int demos_deleted=0;
1978         int initialized = 0;
1979         int exit_value = 0;
1980         int w_x, w_y, w_w, w_h, title_height;
1981         int box_x, box_y, box_w, box_h;
1982         bkg bg;         // background under listbox
1983 #ifdef NEWMENU_MOUSE
1984         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;
1985         int mouse2_state, omouse2_state;
1986         int dblclick_flag=0;
1987 # ifdef WINDOWS
1988         int simukey=0;
1989         int show_up_arrow=0, show_down_arrow=0;
1990 # endif
1991 #endif
1992
1993         w_x=w_y=w_w=w_h=title_height=0;
1994         box_x=box_y=box_w=box_h=0;
1995
1996         filenames = d_malloc( MAX_FILES * 14 );
1997         if (filenames==NULL) return 0;
1998
1999         citem = 0;
2000         keyd_repeat = 1;
2001
2002         if (!stricmp(type, "plr"))
2003                 player_mode = 1;
2004         else if (!stricmp(type, "dem"))
2005                 demo_mode = 1;
2006
2007 ReadFileNames:
2008         done = 0;
2009         NumFiles=0;
2010         
2011 #if !defined(APPLE_DEMO)                // no new pilots for special apple oem version
2012         if (player_mode)        {
2013                 strncpy( &filenames[NumFiles*14], TXT_CREATE_NEW, FILENAME_LEN );
2014                 NumFiles++;
2015         }
2016 #endif
2017
2018         find = PHYSFS_enumerateFiles(demo_mode?DEMO_DIR:"");
2019         for (f = find; *f != NULL; f++)
2020         {
2021                 if (player_mode)
2022                 {
2023                         ext = strrchr(*f, '.');
2024                         if (!ext || strnicmp(ext, ".plr", 4))
2025                                 continue;
2026                 }
2027                 if (NumFiles < MAX_FILES)
2028                 {
2029                         strncpy(&filenames[NumFiles*14], *f, FILENAME_LEN);
2030                         if (player_mode)
2031                         {
2032                                 char *p;
2033
2034                                 p = strchr(&filenames[NumFiles*14], '.');
2035                                 if (p)
2036                                         *p = '\0';
2037                         }
2038                         NumFiles++;
2039                 }
2040                 else
2041                         break;
2042         }
2043
2044         PHYSFS_freeList(find);
2045
2046         if ( (NumFiles < 1) && demos_deleted )  {
2047                 exit_value = 0;
2048                 goto ExitFileMenu;
2049         }
2050         if ( (NumFiles < 1) && demo_mode ) {
2051                 nm_messagebox( NULL, 1, TXT_OK, "%s %s\n%s", TXT_NO_DEMO_FILES, TXT_USE_F5, TXT_TO_CREATE_ONE);
2052                 exit_value = 0;
2053                 goto ExitFileMenu;
2054         }
2055
2056         #ifndef APPLE_DEMO
2057         if ( (NumFiles < 2) && player_mode ) {
2058                 citem = 0;
2059                 goto ExitFileMenuEarly;
2060         }
2061         #endif
2062
2063
2064         if ( NumFiles<1 )       {
2065                 #ifndef APPLE_DEMO
2066                         nm_messagebox(NULL, 1, "Ok", "%s\n '%s' %s", TXT_NO_FILES_MATCHING, type, TXT_WERE_FOUND);
2067                 #endif
2068                 exit_value = 0;
2069                 goto ExitFileMenu;
2070         }
2071
2072         if (!initialized) {     
2073 //              set_screen_mode(SCREEN_MENU);
2074                 set_popup_screen();
2075
2076                 gr_set_current_canvas(NULL);
2077
2078                 grd_curcanv->cv_font = SUBTITLE_FONT;
2079
2080                 w_w = 0;
2081                 w_h = 0;
2082
2083                 for (i=0; i<NumFiles; i++ ) {
2084                         int w, h, aw;
2085                         gr_get_string_size( &filenames[i*14], &w, &h, &aw );            
2086                         if ( w > w_w )
2087                                 w_w = w;
2088                 }
2089                 if ( title ) {
2090                         int w, h, aw;
2091                         gr_get_string_size( title, &w, &h, &aw );               
2092                         if ( w > w_w )
2093                                 w_w = w;
2094                         title_height = h + (grd_curfont->ft_h*2);               // add a little space at the bottom of the title
2095                 }
2096
2097                 box_w = w_w;
2098                 box_h = ((grd_curfont->ft_h + 2) * NumFiles_displayed);
2099
2100                 w_w += (grd_curfont->ft_w * 4);
2101                 w_h = title_height + box_h + (grd_curfont->ft_h * 2);           // more space at bottom
2102
2103                 if ( w_w > grd_curcanv->cv_w ) w_w = grd_curcanv->cv_w;
2104                 if ( w_h > grd_curcanv->cv_h ) w_h = grd_curcanv->cv_h;
2105         
2106                 w_x = (grd_curcanv->cv_w-w_w)/2;
2107                 w_y = (grd_curcanv->cv_h-w_h)/2;
2108         
2109                 if ( w_x < 0 ) w_x = 0;
2110                 if ( w_y < 0 ) w_y = 0;
2111
2112                 box_x = w_x + (grd_curfont->ft_w*2);                    // must be in sync with w_w!!!
2113                 box_y = w_y + title_height;
2114
2115 // save the screen behind the menu.
2116
2117                 bg.saved = NULL;
2118
2119                 if ( (VR_offscreen_buffer->cv_w >= w_w) && (VR_offscreen_buffer->cv_h >= w_h) ) 
2120                         bg.background = &VR_offscreen_buffer->cv_bitmap;
2121                 else
2122                         bg.background = gr_create_bitmap( w_w, w_h );
2123
2124                 Assert( bg.background != NULL );
2125
2126
2127                 gr_bm_bitblt(w_w, w_h, 0, 0, w_x, w_y, &grd_curcanv->cv_bitmap, bg.background );
2128
2129 #if 0
2130                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) );
2131 #endif
2132
2133                 nm_draw_background( w_x,w_y,w_x+w_w-1,w_y+w_h-1 );
2134                 
2135                 gr_string( 0x8000, w_y+10, title );
2136          
2137                 initialized = 1;
2138         }
2139
2140         if ( !player_mode )     {
2141                 newmenu_file_sort( NumFiles, filenames );
2142         } else {
2143                 #if defined(MACINTOSH) && defined(APPLE_DEMO)
2144                 newmenu_file_sort( NumFiles, filenames );
2145                 #else
2146                 newmenu_file_sort( NumFiles-1, &filenames[14] );                // Don't sort first one!
2147                 #endif
2148                 for ( i=0; i<NumFiles; i++ )    {
2149                         if (!stricmp(Players[Player_num].callsign, &filenames[i*14]) )  {
2150 #ifdef NEWMENU_MOUSE
2151                                 dblclick_flag = 1;
2152 #endif
2153                                 citem = i;
2154                         }
2155                 }
2156         }
2157
2158 #ifdef NEWMENU_MOUSE
2159         mouse_state = omouse_state = 0;
2160         mouse2_state = omouse2_state = 0;
2161         //draw_close_box(w_x,w_y);
2162         newmenu_show_cursor();
2163 #endif
2164
2165         while(!done)    {
2166                 ocitem = citem;
2167                 ofirst_item = first_item;
2168                 gr_update();
2169
2170 #ifdef NEWMENU_MOUSE
2171                 omouse_state = mouse_state;
2172                 omouse2_state = mouse2_state;
2173                 mouse_state = mouse_button_state(0);
2174                 mouse2_state = mouse_button_state(1);
2175 #endif
2176
2177                 //see if redbook song needs to be restarted
2178                 songs_check_redbook_repeat();
2179
2180                 key = key_inkey();
2181
2182                 switch(key)     {
2183                 case KEY_COMMAND+KEY_SHIFTED+KEY_3:
2184                 case KEY_PRINT_SCREEN:
2185                         MAC(newmenu_hide_cursor());
2186                         save_screen_shot(0);
2187                         
2188                         MAC(newmenu_show_cursor());
2189                         MAC(key_flush());
2190                         break;
2191
2192                 case KEY_CTRLED+KEY_D:
2193                         #if defined(MACINTOSH) && defined(APPLE_DEMO)
2194                         break;
2195                         #endif
2196
2197                         if ( ((player_mode)&&(citem>0)) || ((demo_mode)&&(citem>=0)) )  {
2198                                 int x = 1;
2199                                 newmenu_hide_cursor();
2200                                 if (player_mode)
2201                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2202                                 else if (demo_mode)
2203                                         x = nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "%s %s?", TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2204                                 newmenu_show_cursor();
2205                                 if (x==0)       {
2206                                         char * p;
2207                                         int ret;
2208                                         char name[PATH_MAX];
2209
2210                                         p = &filenames[(citem*14)+strlen(&filenames[citem*14])];
2211                                         if (player_mode)
2212                                                 *p = '.';
2213
2214                                         strcpy(name, demo_mode?DEMO_DIR:"");
2215                                         strcat(name,&filenames[citem*14]);
2216                                         
2217                                         #ifdef MACINTOSH
2218                                         {
2219                                                 int i;
2220                                                 char *p;
2221                                                 
2222                                                 if ( !strncmp(name, ".\\", 2) )
2223                                                         for (i = 0; i < strlen(name); i++)              // don't subtract 1 from strlen to get the EOS marker
2224                                                                 name[i] = name[i+1];
2225                                                 while ( (p = strchr(name, '\\')) )
2226                                                         *p = ':';
2227                                         }
2228                                         #endif
2229                                 
2230                                         ret = !PHYSFS_delete(name);
2231                                         if (player_mode)
2232                                                 *p = 0;
2233
2234                                         if ((!ret) && player_mode)      {
2235                                                 delete_player_saved_games( &filenames[citem*14] );
2236                                         }
2237
2238                                         if (ret) {
2239                                                 if (player_mode)
2240                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_PILOT, &filenames[citem*14]+((player_mode && filenames[citem*14]=='$')?1:0) );
2241                                                 else if (demo_mode)
2242                                                         nm_messagebox( NULL, 1, TXT_OK, "%s %s %s", TXT_COULDNT, TXT_DELETE_DEMO, &filenames[citem*14]+((demo_mode && filenames[citem*14]=='$')?1:0) );
2243                                         } else if (demo_mode)
2244                                                 demos_deleted = 1;
2245                                         first_item = -1;
2246                                         goto ReadFileNames;
2247                                 }
2248                         }
2249                         break;
2250                 case KEY_HOME:
2251                 case KEY_PAD7:
2252                         citem = 0;
2253                         break;
2254                 case KEY_END:
2255                 case KEY_PAD1:
2256                         citem = NumFiles-1;
2257                         break;
2258                 case KEY_UP:
2259                 case KEY_PAD8:
2260                         citem--;                        
2261                         break;
2262                 case KEY_DOWN:
2263                 case KEY_PAD2:
2264                         citem++;                        
2265                         break;
2266                 case KEY_PAGEDOWN:
2267                 case KEY_PAD3:
2268                         citem += NumFiles_displayed;
2269                         break;
2270                 case KEY_PAGEUP:
2271                 case KEY_PAD9:
2272                         citem -= NumFiles_displayed;
2273                         break;
2274                 case KEY_ESC:
2275                         if (allow_abort_flag) {
2276                                 citem = -1;
2277                                 done = 1;
2278                         }
2279                         break;
2280                 case KEY_ENTER:
2281                 case KEY_PADENTER:
2282                         done = 1;
2283                         break;
2284                         
2285                 case KEY_COMMAND+KEY_Q: {
2286                         if ( !(Game_mode & GM_MULTI) )
2287                         {
2288                                 d_free(filenames);
2289                                 quit_request();
2290                         }
2291                         newmenu_show_cursor();
2292                         key_flush();
2293                         break;
2294                 }
2295                 
2296                 default:        
2297                         {
2298
2299                                 int ascii = key_to_ascii(key);
2300                                 if ( ascii < 255 )      {
2301                                         int cc,cc1;
2302                                         cc=cc1=citem+1;
2303                                         if (cc1 < 0 )  cc1 = 0;
2304                                         if (cc1 >= NumFiles )  cc1 = 0;
2305                                         while(1) {
2306                                                 if ( cc < 0 ) cc = 0;
2307                                                 if ( cc >= NumFiles ) cc = 0;
2308                                                 if ( citem == cc ) break;
2309         
2310                                                 if ( toupper(filenames[cc*14]) == toupper(ascii) )      {
2311                                                         citem = cc;
2312                                                         break;
2313                                                 }
2314                                                 cc++;
2315                                         }
2316                                 }
2317                         }
2318                 }
2319                 if ( done ) break;
2320
2321
2322                 if (citem<0)
2323                         citem=0;
2324
2325                 if (citem>=NumFiles)
2326                         citem = NumFiles-1;
2327
2328                 if (citem< first_item)
2329                         first_item = citem;
2330
2331                 if (citem>=( first_item+NumFiles_displayed))
2332                 {
2333                         first_item = citem-NumFiles_displayed+1;
2334                 }
2335
2336 #ifdef WINDOWS
2337                 if (NumFiles>first_item+NumFiles_displayed)
2338                         show_down_arrow=1;
2339                 else 
2340                         show_down_arrow=0;
2341                 if (first_item>0)
2342                         show_up_arrow=1;
2343                 else    
2344                         show_up_arrow=0;
2345 #endif
2346                         
2347
2348                 if (NumFiles <= NumFiles_displayed )
2349                          first_item = 0;
2350
2351                 if (first_item>NumFiles-NumFiles_displayed)
2352                 {
2353                         first_item = NumFiles-NumFiles_displayed;
2354                 }
2355
2356                 if (first_item < 0 ) first_item = 0;
2357
2358 #ifdef NEWMENU_MOUSE
2359                 if (mouse_state || mouse2_state) {
2360                         int w, h, aw;
2361
2362                         mouse_get_pos(&mx, &my);
2363                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
2364                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2365                                 x1 = box_x;
2366                                 x2 = box_x + box_w - 1;
2367                                 y1 = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
2368                                 y2 = y1+h+1;
2369                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2370                                         if (i == citem && !mouse2_state) {
2371                                                 break;
2372                                         }
2373                                         citem = i;
2374                                         dblclick_flag = 0;
2375                                         break;
2376                                 }
2377                         }
2378                 }
2379                 
2380                 if (!mouse_state && omouse_state) {
2381                         int w, h, aw;
2382
2383                         gr_get_string_size(&filenames[citem*14], &w, &h, &aw  );
2384                         mouse_get_pos(&mx, &my);
2385                         x1 = box_x;
2386                         x2 = box_x + box_w - 1;
2387                         y1 = (citem-first_item)*(grd_curfont->ft_h + 2) + box_y;
2388                         y2 = y1+h+1;
2389                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2390                                 if (dblclick_flag) done = 1;
2391                                 else dblclick_flag = 1;
2392                         }
2393                 }
2394
2395                 if ( !mouse_state && omouse_state ) {
2396                         mouse_get_pos(&mx, &my);
2397                         x1 = w_x + CLOSE_X + 2;
2398                         x2 = x1 + CLOSE_SIZE - 2;
2399                         y1 = w_y + CLOSE_Y + 2;
2400                         y2 = y1 + CLOSE_SIZE - 2;
2401                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2402                                 citem = -1;
2403                                 done = 1;
2404                         }
2405                    #ifdef WINDOWS
2406                         x1 = box_x-LHX(10);
2407                         x2 = x1 + LHX(10);
2408                         y1 = box_y;
2409                         y2 = box_y+LHY(7);
2410                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow ) 
2411                                 simukey = -1;
2412                         y1 = box_y+box_h-LHY(7);
2413                         y2 = box_y+box_h;
2414                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow) 
2415                                 simukey = 1;
2416                    #endif
2417                 }
2418
2419 #endif
2420   
2421                 gr_setcolor( BM_XRGB(2,2,2));
2422                 //gr_rect( box_x - 1, box_y-2, box_x + box_w, box_y-2 );
2423                 gr_setcolor( BM_XRGB( 0,0,0)  );
2424
2425                 if (ofirst_item != first_item)  {
2426                         newmenu_hide_cursor();
2427                         gr_setcolor( BM_XRGB( 0,0,0)  );
2428                         for (i=first_item; i<first_item+NumFiles_displayed; i++ )       {
2429                                 int w, h, aw, y;
2430                                 y = (i-first_item)*(grd_curfont->ft_h + 2) + box_y;
2431                         
2432                                 if ( i >= NumFiles )    {
2433
2434                                         gr_setcolor( BM_XRGB(5,5,5));
2435                                         gr_rect( box_x + box_w, y-1, box_x + box_w, y + grd_curfont->ft_h + 1);
2436                                         //gr_rect( box_x, y + grd_curfont->ft_h + 2, box_x + box_w, y + grd_curfont->ft_h + 2);
2437                                         
2438                                         gr_setcolor( BM_XRGB(2,2,2));
2439                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + grd_curfont->ft_h + 2 );
2440                                         
2441                                         gr_setcolor( BM_XRGB(0,0,0));
2442                                         gr_rect( box_x, y - 1, box_x + box_w - 1, y + grd_curfont->ft_h + 1);
2443                                         
2444                                 } else {
2445                                         if ( i == citem )       
2446                                                 grd_curcanv->cv_font = SELECTED_FONT;
2447                                         else    
2448                                                 grd_curcanv->cv_font = NORMAL_FONT;
2449                                         gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2450
2451                                         gr_setcolor( BM_XRGB(5,5,5));
2452                                   //    gr_rect( box_x, y + h + 2, box_x + box_w, y + h + 2);
2453                                         gr_rect( box_x + box_w, y - 1, box_x + box_w, y + h + 1);
2454                                         
2455                                         gr_setcolor( BM_XRGB(2,2,2));
2456                                         gr_rect( box_x - 1, y - 1, box_x - 1, y + h + 1);
2457                                         gr_setcolor( BM_XRGB(0,0,0));
2458                                                         
2459                                         gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
2460                                         gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
2461                                 }
2462                         }        
2463                         newmenu_show_cursor();
2464                 } else if ( citem != ocitem )   {
2465                         int w, h, aw, y;
2466
2467                         newmenu_hide_cursor();
2468                         i = ocitem;
2469                         if ( (i>=0) && (i<NumFiles) )   {
2470                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
2471                                 if ( i == citem )       
2472                                         grd_curcanv->cv_font = SELECTED_FONT;
2473                                 else    
2474                                         grd_curcanv->cv_font = NORMAL_FONT;
2475                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2476                                 gr_rect( box_x, y-1, box_x + box_w - 1, y + h + 1 );
2477                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
2478                         }
2479                         i = citem;
2480                         if ( (i>=0) && (i<NumFiles) )   {
2481                                 y = (i-first_item)*(grd_curfont->ft_h+2)+box_y;
2482                                 if ( i == citem )       
2483                                         grd_curcanv->cv_font = SELECTED_FONT;
2484                                 else    
2485                                         grd_curcanv->cv_font = NORMAL_FONT;
2486                                 gr_get_string_size(&filenames[i*14], &w, &h, &aw  );
2487                                 gr_rect( box_x, y-1, box_x + box_x - 1, y + h + 1 );
2488                                 gr_string( box_x + 5, y, (&filenames[i*14])+((player_mode && filenames[i*14]=='$')?1:0)  );
2489                         }
2490                         newmenu_show_cursor();
2491                 }
2492
2493         #ifdef WINDOWS   
2494                         grd_curcanv->cv_font = NORMAL_FONT;
2495                         if (show_up_arrow)
2496                                 gr_string( box_x-LHX(10), box_y ,UP_ARROW_MARKER );
2497                         else
2498                         {
2499                                 No_darkening=1;
2500                                 nm_draw_background (box_x-LHX(10),box_y,box_x-2,box_y+LHY(7));
2501                                 No_darkening=0;
2502                         }
2503
2504                         if (show_down_arrow)
2505                         gr_string( box_x-LHX(10), box_y+box_h-LHY(7) ,DOWN_ARROW_MARKER );
2506                         else
2507                         {
2508                                 No_darkening=1;
2509                                 nm_draw_background (box_x-LHX(10),box_y+box_h-LHY(7),box_x-2,box_y+box_h);
2510                                 No_darkening=0;
2511                         }
2512
2513         #endif
2514         }
2515
2516 ExitFileMenuEarly:
2517         MAC(newmenu_hide_cursor());
2518         if ( citem > -1 )       {
2519                 strncpy( filename, (&filenames[citem*14])+((player_mode && filenames[citem*14]=='$')?1:0), FILENAME_LEN );
2520                 exit_value = 1;
2521         } else {
2522                 exit_value = 0;
2523         }                                                                                        
2524
2525 ExitFileMenu:
2526         keyd_repeat = old_keyd_repeat;
2527
2528         if ( initialized )      {
2529                 if (Newdemo_state != ND_STATE_PLAYBACK) //horrible hack to prevent restore when screen has been cleared
2530                         gr_bm_bitblt(w_w, w_h, w_x, w_y, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
2531                 if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
2532                         gr_free_bitmap(bg.background);
2533 #if 0
2534                 gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) )
2535 #endif
2536         }
2537
2538         if ( filenames )
2539                 d_free(filenames);
2540
2541         return exit_value;
2542
2543 }
2544
2545
2546 // Example listbox callback function...
2547 // int lb_callback( int * citem, int *nitems, char * items[], int *keypress )
2548 // {
2549 //      int i;
2550 // 
2551 //      if ( *keypress = KEY_CTRLED+KEY_D )     {
2552 //              if ( *nitems > 1 )      {
2553 //                      PHYSFS_delete(items[*citem]);     // Delete the file
2554 //                      for (i=*citem; i<*nitems-1; i++ )       {
2555 //                              items[i] = items[i+1];
2556 //                      }
2557 //                      *nitems = *nitems - 1;
2558 //                      d_free( items[*nitems] );
2559 //                      items[*nitems] = NULL;
2560 //                      return 1;       // redraw;
2561 //              }
2562 //                      *keypress = 0;
2563 //      }                       
2564 //      return 0;
2565 // }
2566
2567 #define LB_ITEMS_ON_SCREEN 8
2568
2569 int newmenu_listbox( char * title, int nitems, char * items[], int allow_abort_flag, int (*listbox_callback)( int * citem, int *nitems, char * items[], int *keypress ) )
2570 {
2571         return newmenu_listbox1( title, nitems, items, allow_abort_flag, 0, listbox_callback );
2572 }
2573
2574 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 ) )
2575 {
2576         int i;
2577         int done, ocitem,citem, ofirst_item, first_item, key, redraw;
2578         int old_keyd_repeat = keyd_repeat;
2579         int width, height, wx, wy, title_height, border_size;
2580         int total_width,total_height;
2581         bkg bg;
2582 #ifdef NEWMENU_MOUSE
2583         int mx, my, x1, x2, y1, y2, mouse_state, omouse_state;  //, dblclick_flag;
2584         int close_x,close_y;
2585 # ifdef WINDOWS
2586    int simukey=0,show_up_arrow=0,show_down_arrow=0;
2587 # endif
2588 #endif
2589
2590         keyd_repeat = 1;
2591
2592 //      set_screen_mode(SCREEN_MENU);
2593         set_popup_screen();
2594
2595         gr_set_current_canvas(NULL);
2596
2597         grd_curcanv->cv_font = SUBTITLE_FONT;
2598
2599         width = 0;
2600         for (i=0; i<nitems; i++ )       {
2601                 int w, h, aw;
2602                 gr_get_string_size( items[i], &w, &h, &aw );            
2603                 if ( w > width )
2604                         width = w;
2605         }
2606         height = (grd_curfont->ft_h + 2) * LB_ITEMS_ON_SCREEN;
2607
2608         {
2609                 int w, h, aw;
2610                 gr_get_string_size( title, &w, &h, &aw );               
2611                 if ( w > width )
2612                         width = w;
2613                 title_height = h + 5;
2614         }
2615
2616         border_size = grd_curfont->ft_w;
2617    WIN (border_size=grd_curfont->ft_w*2);
2618                 
2619         width += (grd_curfont->ft_w);
2620         if ( width > grd_curcanv->cv_w - (grd_curfont->ft_w * 3) )
2621                 width = grd_curcanv->cv_w - (grd_curfont->ft_w * 3);
2622
2623         wx = (grd_curcanv->cv_bitmap.bm_w-width)/2;
2624         wy = (grd_curcanv->cv_bitmap.bm_h-(height+title_height))/2 + title_height;
2625         if ( wy < title_height )
2626                 wy = title_height;
2627
2628         total_width = width+2*border_size;
2629         total_height = height+2*border_size+title_height;
2630
2631         bg.saved = NULL;
2632
2633         if ( (VR_offscreen_buffer->cv_w >= total_width) && (VR_offscreen_buffer->cv_h >= total_height) )
2634                 bg.background = &VR_offscreen_buffer->cv_bitmap;
2635         else
2636                 //bg.background = gr_create_bitmap( width, (height + title_height) );
2637                 bg.background = gr_create_bitmap(total_width,total_height);
2638         Assert( bg.background != NULL );
2639                 
2640         //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 );
2641         gr_bm_bitblt(total_width,total_height, 0, 0, wx-border_size, wy-title_height-border_size, &grd_curcanv->cv_bitmap, bg.background );
2642
2643 #if 0
2644         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(grd_curcanv->cv_bitmap), &(VR_offscreen_buffer->cv_bitmap) );
2645 #endif
2646
2647         nm_draw_background( wx-border_size,wy-title_height-border_size,wx+width+border_size-1,wy+height+border_size-1 );
2648
2649         gr_string( 0x8000, wy - title_height, title );
2650
2651         done = 0;
2652         citem = default_item;
2653         if ( citem < 0 ) citem = 0;
2654         if ( citem >= nitems ) citem = 0;
2655
2656         first_item = -1;
2657
2658 #ifdef NEWMENU_MOUSE
2659         mouse_state = omouse_state = 0; //dblclick_flag = 0;
2660         close_x = wx-border_size;
2661         close_y = wy-title_height-border_size;
2662         //draw_close_box(close_x,close_y);
2663         newmenu_show_cursor();
2664 #endif
2665
2666         while(!done)    {
2667                 ocitem = citem;
2668                 ofirst_item = first_item;
2669 #ifdef NEWMENU_MOUSE
2670                 omouse_state = mouse_state;
2671                 mouse_state = mouse_button_state(0);
2672 #endif
2673                 //see if redbook song needs to be restarted
2674                 songs_check_redbook_repeat();
2675
2676                 key = key_inkey();
2677
2678                 if ( listbox_callback )
2679                         redraw = (*listbox_callback)(&citem, &nitems, items, &key );
2680                 else
2681                         redraw = 0;
2682
2683                 if ( key<-1 ) {
2684                         citem = key;
2685                         key = -1;
2686                         done = 1;
2687                 }
2688
2689
2690         #ifdef WINDOWS
2691                 if (simukey==-1)
2692                         key=KEY_UP;
2693                 else if (simukey==1)
2694                    key=KEY_DOWN;
2695                 simukey=0;
2696         #endif
2697                 
2698                 switch(key)     {
2699                 case KEY_COMMAND+KEY_SHIFTED+KEY_3:
2700                 case KEY_PRINT_SCREEN:          
2701                         MAC(newmenu_hide_cursor());
2702                         save_screen_shot(0); 
2703                         
2704                         MAC(newmenu_show_cursor());
2705                         MAC(key_flush());
2706                         break;
2707                 case KEY_HOME:
2708                 case KEY_PAD7:
2709                         citem = 0;
2710                         break;
2711                 case KEY_END:
2712                 case KEY_PAD1:
2713                         citem = nitems-1;
2714                         break;
2715                 case KEY_UP:
2716                 case KEY_PAD8:
2717                         citem--;                        
2718                         break;
2719                 case KEY_DOWN:
2720                 case KEY_PAD2:
2721                         citem++;                        
2722                         break;
2723                 case KEY_PAGEDOWN:
2724                 case KEY_PAD3:
2725                         citem += LB_ITEMS_ON_SCREEN;
2726                         break;
2727                 case KEY_PAGEUP:
2728                 case KEY_PAD9:
2729                         citem -= LB_ITEMS_ON_SCREEN;
2730                         break;
2731                 case KEY_ESC:
2732                         if (allow_abort_flag) {
2733                                 citem = -1;
2734                                 done = 1;
2735                         }
2736                         break;
2737                 case KEY_ENTER:
2738                 case KEY_PADENTER:
2739                         done = 1;
2740                         break;
2741
2742                 case KEY_COMMAND+KEY_Q: {
2743                         if ( !(Game_mode & GM_MULTI) )
2744                                 quit_request();
2745                         newmenu_show_cursor();
2746                         key_flush();
2747                         break;
2748                 }
2749
2750                 default:        
2751                         if ( key > 0 )  {
2752                                 int ascii = key_to_ascii(key);
2753                                 if ( ascii < 255 )      {
2754                                         int cc,cc1;
2755                                         cc=cc1=citem+1;
2756                                         if (cc1 < 0 )  cc1 = 0;
2757                                         if (cc1 >= nitems )  cc1 = 0;
2758                                         while(1) {
2759                                                 if ( cc < 0 ) cc = 0;
2760                                                 if ( cc >= nitems ) cc = 0;
2761                                                 if ( citem == cc ) break;
2762         
2763                                                 if ( toupper( items[cc][0] ) == toupper(ascii) )        {
2764                                                         citem = cc;
2765                                                         break;
2766                                                 }
2767                                                 cc++;
2768                                         }
2769                                 }
2770                         }
2771                 }
2772                 if ( done ) break;
2773
2774                 if (citem<0)
2775                         citem=0;
2776
2777                 if (citem>=nitems)
2778                         citem = nitems-1;
2779
2780                 if (citem< first_item)
2781                         first_item = citem;
2782
2783                 if (citem>=( first_item+LB_ITEMS_ON_SCREEN))
2784                         first_item = citem-LB_ITEMS_ON_SCREEN+1;
2785
2786                 if (nitems <= LB_ITEMS_ON_SCREEN )
2787                          first_item = 0;
2788
2789                 if (first_item>nitems-LB_ITEMS_ON_SCREEN)
2790                         first_item = nitems-LB_ITEMS_ON_SCREEN;
2791                 if (first_item < 0 ) first_item = 0;
2792
2793 #ifdef WINDOWS
2794                 if (nitems>first_item+LB_ITEMS_ON_SCREEN)
2795                         show_down_arrow=1;
2796                 else 
2797                         show_down_arrow=0;
2798                 if (first_item>0)
2799                         show_up_arrow=1;
2800                 else    
2801                         show_up_arrow=0;
2802 #endif
2803
2804
2805 #ifdef NEWMENU_MOUSE
2806                 if (mouse_state) {
2807                         int w, h, aw;
2808
2809                         mouse_get_pos(&mx, &my);
2810                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
2811                                 if (i > nitems)
2812                                         break;
2813                                 gr_get_string_size(items[i], &w, &h, &aw  );
2814                                 x1 = wx;
2815                                 x2 = wx + width;
2816                                 y1 = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2817                                 y2 = y1+h+1;
2818                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2819                                         //if (i == citem) {
2820                                         //      break;
2821                                         //}
2822                                         //dblclick_flag= 0;
2823                                         citem = i;
2824                                         done = 1;
2825                                         break;
2826                                 }
2827                         }
2828                 }
2829
2830                 //no double-click stuff for listbox
2831                 //@@if (!mouse_state && omouse_state) {
2832                 //@@    int w, h, aw;
2833                 //@@
2834                 //@@    gr_get_string_size(items[citem], &w, &h, &aw  );
2835                 //@@    mouse_get_pos(&mx, &my);
2836                 //@@    x1 = wx;
2837                 //@@    x2 = wx + width;
2838                 //@@    y1 = (citem-first_item)*(grd_curfont->ft_h+2)+wy;
2839                 //@@    y2 = y1+h+1;
2840                 //@@    if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2841                 //@@            if (dblclick_flag) done = 1;
2842                 //@@    }
2843                 //@@}
2844
2845                 //check for close box clicked
2846                 if ( !mouse_state && omouse_state ) {
2847                         mouse_get_pos(&mx, &my);
2848                         x1 = close_x + CLOSE_X + 2;
2849                         x2 = x1 + CLOSE_SIZE - 2;
2850                         y1 = close_y + CLOSE_Y + 2;
2851                         y2 = y1 + CLOSE_SIZE - 2;
2852                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
2853                                 citem = -1;
2854                                 done = 1;
2855                         }
2856
2857                    #ifdef WINDOWS
2858                         x1 = wx-LHX(10);
2859                         x2 = x1 + LHX(10);
2860                         y1 = wy;
2861                         y2 = wy+LHY(7);
2862                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_up_arrow) 
2863                                 simukey = -1;
2864                         y1 = total_height-LHY(7);
2865                         y2 = total_height;
2866                         if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) && show_down_arrow ) 
2867                                 simukey = 1;
2868                    #endif
2869
2870                         
2871                 }
2872 #endif
2873
2874                 if ( (ofirst_item != first_item) || redraw)     {
2875                         newmenu_hide_cursor();
2876
2877                         gr_setcolor( BM_XRGB( 0,0,0)  );
2878                         for (i=first_item; i<first_item+LB_ITEMS_ON_SCREEN; i++ )       {
2879                                 int w, h, aw, y;
2880                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2881                                 if ( i >= nitems )      {
2882                                         gr_setcolor( BM_XRGB(0,0,0));
2883                                         gr_rect( wx, y-1, wx+width-1, y+grd_curfont->ft_h + 1 );
2884                                 } else {
2885                                         if ( i == citem )       
2886                                                 grd_curcanv->cv_font = SELECTED_FONT;
2887                                         else    
2888                                                 grd_curcanv->cv_font = NORMAL_FONT;
2889                                         gr_get_string_size(items[i], &w, &h, &aw  );
2890                                         gr_rect( wx, y-1, wx+width-1, y+h+1 );
2891                                         gr_string( wx+5, y, items[i]  );
2892                                 }
2893                         }               
2894
2895                                 
2896                         // If Win95 port, draw up/down arrows on left side of menu
2897                         #ifdef WINDOWS   
2898                                 grd_curcanv->cv_font = NORMAL_FONT;
2899                         if (show_up_arrow)
2900                                 gr_string( wx-LHX(10), wy ,UP_ARROW_MARKER );
2901                         else
2902                         {
2903                                 No_darkening=1;
2904                                 nm_draw_background (wx-LHX(10),wy,wx-2,wy+LHY(7));
2905                                 No_darkening=0;
2906                         }
2907
2908                         if (show_down_arrow)
2909                         gr_string( wx-LHX(10), wy+total_height-LHY(7) ,DOWN_ARROW_MARKER );
2910                         else
2911                         {
2912                                 No_darkening=1;
2913                                 nm_draw_background (wx-LHX(10),wy+total_height-LHY(7),wx-2,wy+total_height);
2914                                 No_darkening=0;
2915                         }
2916
2917                         #endif
2918
2919
2920                         newmenu_show_cursor();
2921                         gr_update();
2922                 } else if ( citem != ocitem )   {
2923                         int w, h, aw, y;
2924
2925                         newmenu_hide_cursor();
2926
2927                         i = ocitem;
2928                         if ( (i>=0) && (i<nitems) )     {
2929                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2930                                 if ( i == citem )       
2931                                         grd_curcanv->cv_font = SELECTED_FONT;
2932                                 else    
2933                                         grd_curcanv->cv_font = NORMAL_FONT;
2934                                 gr_get_string_size(items[i], &w, &h, &aw  );
2935                                 gr_rect( wx, y-1, wx+width-1, y+h+1 );
2936                                 gr_string( wx+5, y, items[i]  );
2937
2938                         }
2939                         i = citem;
2940                         if ( (i>=0) && (i<nitems) )     {
2941                                 y = (i-first_item)*(grd_curfont->ft_h+2)+wy;
2942                                 if ( i == citem )       
2943                                         grd_curcanv->cv_font = SELECTED_FONT;
2944                                 else    
2945                                         grd_curcanv->cv_font = NORMAL_FONT;
2946                                 gr_get_string_size( items[i], &w, &h, &aw  );
2947                                 gr_rect( wx, y-1, wx+width-1, y+h );
2948                                 gr_string( wx+5, y, items[i]  );
2949                         }
2950
2951                         newmenu_show_cursor();
2952                         gr_update();
2953                 }
2954         }
2955         newmenu_hide_cursor();
2956
2957         keyd_repeat = old_keyd_repeat;
2958
2959         gr_bm_bitblt(total_width,total_height, wx-border_size, wy-title_height-border_size, 0, 0, bg.background, &grd_curcanv->cv_bitmap );
2960
2961         if ( bg.background != &VR_offscreen_buffer->cv_bitmap )
2962                 gr_free_bitmap(bg.background);
2963
2964 #if 0
2965         gr_bm_bitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(VR_offscreen_buffer->cv_bitmap), &(grd_curcanv->cv_bitmap) );
2966 #endif
2967
2968         return citem;
2969 }
2970
2971 #if 0
2972 int newmenu_filelist( char * title, char * filespec, char * filename )
2973 {
2974         int i, NumFiles;
2975         char * Filenames[MAX_FILES];
2976         char FilenameText[MAX_FILES][14];
2977         FILEFINDSTRUCT find;
2978
2979         NumFiles = 0;
2980         if( !FileFindFirst( filespec, &find ) ) {
2981                 do      {
2982                         if (NumFiles<MAX_FILES) {
2983                                 strncpy( FilenameText[NumFiles], find.name, FILENAME_LEN);
2984                                 Filenames[NumFiles] = FilenameText[NumFiles];
2985                                 NumFiles++;
2986                         } else {
2987                                 break;
2988                         }
2989                 } while( !FileFindNext( &find ) );
2990                 FileFindClose();
2991         }
2992
2993         i = newmenu_listbox( title, NumFiles, Filenames, 1, NULL );
2994         if ( i > -1 )   {
2995                 strcpy( filename, Filenames[i] );
2996                 return 1;
2997         } 
2998         return 0;
2999 }
3000 #endif
3001
3002 //added on 10/14/98 by Victor Rachels to attempt a fixedwidth font messagebox
3003 int nm_messagebox_fixedfont( char *title, int nchoices, ... )
3004 {
3005         int i;
3006         char * format;
3007         va_list args;
3008         char *s;
3009         char nm_text[MESSAGEBOX_TEXT_SIZE];
3010         newmenu_item nm_message_items[5];
3011
3012         va_start(args, nchoices );
3013
3014         Assert( nchoices <= 5 );
3015
3016         for (i=0; i<nchoices; i++ )     {
3017                 s = va_arg( args, char * );
3018                 nm_message_items[i].type = NM_TYPE_MENU; nm_message_items[i].text = s;
3019         }
3020         format = va_arg( args, char * );
3021         //sprintf(        nm_text, "" ); // adb: ?
3022         vsprintf(nm_text,format,args);
3023         va_end(args);
3024
3025         Assert(strlen(nm_text) < MESSAGEBOX_TEXT_SIZE );
3026
3027         return newmenu_do_fixedfont( title, nm_text, nchoices, nm_message_items, NULL, 0, NULL, -1, -1 );
3028 }
3029 //end this section addition - Victor Rachels
3030
3031 #ifdef NETWORK
3032 extern netgame_info Active_games[];
3033 extern int NumActiveNetgames;
3034
3035 void show_extra_netgame_info(int choice)
3036  {
3037         newmenu_item m[5];
3038    char mtext[5][50];
3039         int i,num=0;
3040
3041         if (choice>=NumActiveNetgames)
3042                 return;
3043         
3044    for (i=0;i<5;i++)
3045         {
3046          m[i].text=(char *)&mtext[i];
3047     m[i].type=NM_TYPE_TEXT;             
3048         }
3049
3050    sprintf (mtext[num],"Game: %s",Active_games[choice].game_name); num++;
3051    sprintf (mtext[num],"Mission: %s",Active_games[choice].mission_title); num++;
3052         sprintf (mtext[num],"Current Level: %d",Active_games[choice].levelnum); num++;
3053         sprintf (mtext[num],"Difficulty: %s",MENU_DIFFICULTY_TEXT(Active_games[choice].difficulty)); num++;
3054
3055         already_showing_info=1; 
3056         newmenu_dotiny2( NULL, "Netgame Information", num, m, NULL);
3057         already_showing_info=0; 
3058  }
3059
3060 #endif // NETWORK
3061
3062 /* Spiffy word wrap string formatting function */
3063
3064 void nm_wrap_text(char *dbuf, char *sbuf, int line_length)
3065 {
3066         int col;
3067         char *wordptr;
3068         char *tbuf;
3069
3070         tbuf = (char *)d_malloc(strlen(sbuf)+1);
3071         strcpy(tbuf, sbuf);
3072
3073         wordptr = strtok(tbuf, " ");
3074         if (!wordptr) return;
3075         col = 0;
3076         dbuf[0] = 0;
3077
3078         while (wordptr)
3079         {
3080                 col = col+strlen(wordptr)+1;
3081                 if (col >=line_length) {
3082                         col = 0;
3083                         sprintf(dbuf, "%s\n%s ", dbuf, wordptr);
3084                 }
3085                 else {
3086                         sprintf(dbuf, "%s%s ", dbuf, wordptr);
3087                 }
3088                 wordptr = strtok(NULL, " ");
3089         }
3090
3091         d_free(tbuf);
3092 }