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