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