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