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