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