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