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