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