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