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