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