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