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