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