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