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