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