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