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