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