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