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