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