home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / REALDOS.C < prev    next >
C/C++ Source or Header  |  1991-07-23  |  52KB  |  1,670 lines

  1. /*
  2.     Miscellaneous C routines used only in DOS Fractint.
  3. */
  4.  
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <sys\types.h>
  9. #include <sys\stat.h>
  10. #include <fcntl.h>
  11. #include <math.h>
  12. #include "fractint.h"
  13. #include "fractype.h"
  14. #include "helpdefs.h"
  15.  
  16. /* routines in this module    */
  17.  
  18. int  stopmsg(int,unsigned char far *);
  19. int  texttempmsg(char *msgparm);
  20. int  showtempmsg(char *msgparm);
  21. void cleartempmsg(void);
  22. void blankrows(int row,int rows,int attr);
  23. void helptitle(void);
  24. int  putstringcenter(int row, int col, int width, int attr, char far *msg);
  25. void stackscreen(void);
  26. void unstackscreen(void);
  27. void discardscreen(void);
  28. int  fullscreen_choice(
  29.          int options, char *hdg, char *hdg2, char *instr, int numchoices,
  30.          char **choices, int *attributes, int boxwidth, int boxdepth,
  31.          int colwidth, int current, void (*formatitem)(),
  32.          char *speedstring, int (*speedprompt)(), int (*checkkey)());
  33. int  strncasecmp(char *s,char *t,int ct);
  34. int  main_menu(int fullmenu);
  35. int  input_field(int options, int attr, char *fld, int len, int row, int col, int (*checkkey)(int) );
  36. int  field_prompt(int options, char *hdg, char *instr, char *fld, int len, int (*checkkey)() );
  37. int  thinking(int options, char *msg);
  38. void clear_screen(void);
  39. int  savegraphics(void);
  40. void restoregraphics(void),discardgraphics(void);
  41. int load_fractint_cfg(int options);
  42. void bad_fractint_cfg_msg(void);
  43. void load_videotable(int options);
  44. int check_vidmode_key(int option,int k);
  45. int check_vidmode_keyname(char *kname);
  46. void vidmode_keyname(int k,char *buf);
  47.  
  48. static int menu_checkkey(int curkey,int choice);
  49.  
  50. int release=1611; /* this has 2 implied decimals; increment it every synch */
  51.  
  52. /* fullscreen_choice options */
  53. #define CHOICERETURNKEY 1
  54. #define CHOICEMENU    2
  55. #define CHOICEHELP    4
  56.  
  57. extern int  xdots, ydots, sxdots, sydots, sxoffs, syoffs;
  58. extern int  colors;
  59. extern int  dotmode;
  60. extern int  oktoprint;
  61. extern char far *findfont(int);
  62. extern int  textrow, textcol, textrbase, textcbase;
  63. extern int  debugflag;
  64. extern int  fractype;
  65. extern int  calc_status;
  66. extern double param[];
  67. extern int  tabmode;
  68. extern int  color_dark,color_medium,color_bright;
  69. extern int  lookatmouse;
  70. extern int  gotrealdac;
  71. extern int  reallyega;
  72. extern int  extraseg;
  73. extern int  active_system;
  74. extern int  first_init;
  75.  
  76.  
  77. /* int stopmsg(flags,message) displays message and waits for a key:
  78.      message should be a max of 9 lines with \n's separating them;
  79.        no leading or trailing \n's in message;
  80.        no line longer than 76 chars for best appearance;
  81.      flag options:
  82.        &1 if already in text display mode, stackscreen is not called
  83.       and message is displayed at (12,0) instead of (4,0)
  84.        &2 if continue/cancel indication is to be returned;
  85.       when not set, "Any key to continue..." is displayed
  86.       when set, "Escape to cancel, any other key to continue..."
  87.       -1 is returned for cancel, 0 for continue
  88.        &4 set to suppress buzzer
  89.        &8 for Fractint for Windows & parser - use a fixed pitch font
  90.       &16 for info only message (green box instead of red in DOS vsn)
  91.    */
  92. int stopmsg (int flags, unsigned char far *msg)
  93. {
  94.    int ret,toprow,color,savelookatmouse;
  95.    if (active_system == 0 /* DOS */
  96.      && first_init) {      /* & cmdfiles hasn't finished 1st try */
  97.       setvideotext();
  98.       buzzer(2);
  99.       putstring(0,0,15,"*** Error during startup:");
  100.       putstring(2,0,15,msg);
  101.       movecursor(8,0);
  102.       exit(1);
  103.       }
  104.    ret = 0;
  105.    savelookatmouse = lookatmouse;
  106.    lookatmouse = -13;
  107.    if ((flags & 1))
  108.       blankrows(toprow=12,10,7);
  109.    else {
  110.       stackscreen();
  111.       toprow = 4;
  112.       movecursor(4,0);
  113.       }
  114.    textcbase = 2; /* left margin is 2 */
  115.    putstring(toprow,0,7,msg);
  116.    if (flags & 2)
  117.       putstring(textrow+2,0,7,"Escape to cancel, any other key to continue...");
  118.    else
  119.       putstring(textrow+2,0,7,"Any key to continue...");
  120.    textcbase = 0; /* back to full line */
  121.    color = (flags & 16) ? C_STOP_INFO : C_STOP_ERR;
  122.    setattr(toprow,0,color,(textrow+1-toprow)*80);
  123.    movecursor(25,80);    /* cursor off */
  124.    if ((flags & 4) == 0)
  125.       buzzer((flags & 16) ? 0 : 2);
  126.    while (keypressed()) /* flush any keyahead */
  127.       getakey();
  128.    if (getakeynohelp() == ESC)
  129.       ret = -1;
  130.    if ((flags & 1))
  131.       blankrows(toprow,10,7);
  132.    else
  133.       unstackscreen();
  134.    lookatmouse = savelookatmouse;
  135.    return ret;
  136. }
  137.  
  138.  
  139. static char far *temptextsave = NULL;
  140. static int  textxdots,textydots;
  141.  
  142. /* texttempmsg(msg) displays a text message of up to 40 characters, waits
  143.       for a key press, restores the prior display, and returns (without
  144.       eating the key).
  145.       It works in almost any video mode - does nothing in some very odd cases
  146.       (HCGA hi-res with old bios), or when there isn't 10k of temp mem free. */
  147. int texttempmsg(char *msgparm)
  148. {
  149.    if (showtempmsg(msgparm))
  150.       return(-1);
  151.    while (!keypressed()) { } /* wait for a keystroke but don't eat it */
  152.    cleartempmsg();
  153.    return(0);
  154. }
  155.  
  156. int showtempmsg(char *msgparm)
  157. {
  158.    extern int color_dark,color_medium;
  159.    unsigned char msg[41],buffer[640];
  160.    char far *fartmp;
  161.    char far *fontptr;
  162.    unsigned char *bufptr;
  163.    int i,j,k,xrepeat,yrepeat,fontchar,charnum;
  164.    int save_sxoffs,save_syoffs;
  165.    strncpy(msg,msgparm,40);
  166.    msg[40] = 0; /* ensure max message len of 40 chars */
  167.    if (dotmode == 11) { /* disk video, screen in text mode, easy */
  168.       dvid_status(0,msg);
  169.       return(0);
  170.       }
  171.    if ((fontptr = findfont(0)) == NULL) { /* old bios, no font table? */
  172.       if (oktoprint == 0           /* can't printf */
  173.     || sxdots > 640 || sydots > 200) /* not willing to trust char cell size */
  174.      return(-1); /* sorry, message not displayed */
  175.       textydots = 8;
  176.       textxdots = sxdots;
  177.       }
  178.    else {
  179.       xrepeat = (sxdots >= 640) ? 2 : 1;
  180.       yrepeat = (sydots >= 300) ? 2 : 1;
  181.       textxdots = strlen(msg) * xrepeat * 8;
  182.       textydots = yrepeat * 8;
  183.       }
  184.    /* worst case needs 10k */
  185.    if ((temptextsave = farmemalloc((long)textxdots * textydots)) == NULL)
  186.       return(-1); /* sorry, message not displayed */
  187.    save_sxoffs = sxoffs;
  188.    save_syoffs = syoffs;
  189.    sxoffs = syoffs = 0;
  190.    fartmp = temptextsave;
  191.    for (i = 0; i < textydots; ++i) {
  192.       get_line(i,0,textxdots-1,buffer);
  193.       for (j = 0; j < textxdots; ++j) /* copy it out to far memory */
  194.      *(fartmp++) = buffer[j];
  195.       }
  196.    if (fontptr == NULL) { /* bios must do it for us */
  197.       home();
  198.       printf(msg);
  199.       }
  200.    else { /* generate the characters */
  201.       find_special_colors(); /* get color_dark & color_medium set */
  202.       for (i = 0; i < 8; ++i) {
  203.      memset(buffer,color_dark,640);
  204.      bufptr = buffer;
  205.      charnum = -1;
  206.      while (msg[++charnum] != 0) {
  207.         fontchar = *(fontptr + msg[charnum]*8 + i);
  208.         for (j = 0; j < 8; ++j) {
  209.            for (k = 0; k < xrepeat; ++k) {
  210.           if ((fontchar & 0x80) != 0)
  211.              *bufptr = color_medium;
  212.           ++bufptr;
  213.           }
  214.            fontchar <<= 1;
  215.            }
  216.         }
  217.      for (j = 0; j < yrepeat; ++j)
  218.         put_line(i*yrepeat+j,0,textxdots-1,buffer);
  219.      }
  220.       }
  221.    sxoffs = save_sxoffs;
  222.    syoffs = save_syoffs;
  223.    return(0);
  224. }
  225.  
  226. void cleartempmsg()
  227. {
  228.    unsigned char buffer[640];
  229.    char far *fartmp;
  230.    int i,j;
  231.    int save_sxoffs,save_syoffs;
  232.    if (dotmode == 11) /* disk video, easy */
  233.       dvid_status(0,"");
  234.    else if (temptextsave != NULL) {
  235.       save_sxoffs = sxoffs;
  236.       save_syoffs = syoffs;
  237.       sxoffs = syoffs = 0;
  238.       fartmp = temptextsave;
  239.       for (i = 0; i < textydots; ++i) {
  240.      for (j = 0; j < textxdots; ++j) /* copy back from far memory */
  241.         buffer[j] = *(fartmp++);
  242.      put_line(i,0,textxdots-1,buffer);
  243.      }
  244.       farmemfree(temptextsave);
  245.       temptextsave = NULL;
  246.       sxoffs = save_sxoffs;
  247.       syoffs = save_syoffs;
  248.       }
  249. }
  250.  
  251.  
  252. void blankrows(int row,int rows,int attr)
  253. {
  254.    char buf[81];
  255.    memset(buf,' ',80);
  256.    buf[80] = 0;
  257.    while (--rows >= 0)
  258.       putstring(row++,0,attr,buf);
  259.    }
  260.  
  261.  
  262. void helptitle()
  263. {
  264.    char msg[80],buf[10];
  265.    setclear(); /* clear the screen */
  266. #ifdef WAITE
  267.    sprintf(msg,"Special FRACTINT  Version %d.%01d",release/100,(release%100)/10);
  268. #else
  269.    sprintf(msg,"FRACTINT  Version %d.%01d",release/100,(release%100)/10);
  270. #endif
  271.    if (release%10) {
  272.       sprintf(buf,"%01d",release%10);
  273.       strcat(msg,buf);
  274.       }
  275. #ifdef WAITE /* realdos.c */
  276.    strcat(msg," for the Waite Group's Fractal Creations");
  277. #endif /* WAITE - realdos.c */
  278.    putstringcenter(0,0,80,C_TITLE,msg);
  279. #ifdef WAITE
  280.    return;
  281. #endif
  282. /* uncomment next for production executable: */
  283. /* return; */
  284.    if (debugflag == 3002) return;
  285.    putstring(0,2,C_TITLE_DEV,"Development Version");
  286. /* replace above by next after creating production release, for release source */
  287. /* putstring(0,3,C_TITLE_DEV, "Customized Version"); */
  288.    putstring(0,53,C_TITLE_DEV,"Not for Public Release");
  289. }
  290.  
  291.  
  292. int putstringcenter(int row, int col, int width, int attr, char far *msg)
  293. {
  294.    char buf[81];
  295.    int i,j,k;
  296.    i = 0;
  297.    while (msg[i]) ++i; /* strlen for a far */
  298.    if (i == 0) return(-1);
  299.    j = (width - i) / 2;
  300.    j -= (width + 10 - i) / 20; /* when wide a bit left of center looks better */
  301.    memset(buf,' ',width);
  302.    buf[width] = 0;
  303.    i = 0;
  304.    k = j;
  305.    while (msg[i]) buf[k++] = msg[i++]; /* strcpy for a far */
  306.    putstring(row,col,attr,buf);
  307.    return j;
  308. }
  309.  
  310.  
  311. static int screenctr=-1;
  312. #define MAXSCREENS 3
  313. static unsigned char far *savescreen[MAXSCREENS];
  314. static int saverc[MAXSCREENS+1];
  315. static FILE *savescf=NULL;
  316. static char scsvfile[]="fractscr.tmp";
  317. extern int text_type;
  318. extern int textaddr;
  319.  
  320. void stackscreen()
  321. {
  322.    unsigned char far *vidmem;
  323.    int savebytes;
  324.    int i;
  325.    unsigned char far *ptr;
  326.    char buf[100];
  327.    saverc[screenctr+1] = textrow*80 + textcol;
  328.    if (++screenctr) { /* already have some stacked */
  329.      static char far msg[]={"stackscreen overflow"};
  330.       if ((i = screenctr - 1) >= MAXSCREENS) { /* bug, missing unstack? */
  331.      stopmsg(1,msg);
  332.      exit(1);
  333.      }
  334.       vidmem = MK_FP(textaddr,0);
  335.       savebytes = (text_type == 0) ? 4000 : 16384;
  336.       if ((ptr = savescreen[i] = farmemalloc((long)savebytes)))
  337.      far_memcpy(ptr,vidmem,savebytes);
  338.       else {
  339.      if (savescf == NULL) { /* create file just once */
  340.         if ((savescf = fopen(scsvfile,"wb")) == NULL)
  341.            goto fileproblem;
  342.         if (fwrite(buf,MAXSCREENS,16384,savescf) != 16384)
  343.            goto fileproblem;
  344.         fclose(savescf);
  345.         if ((savescf = fopen(scsvfile,"r+b")) == NULL) {
  346.         static char far msg[]={"insufficient memory, aborting"};
  347. fileproblem:   stopmsg(1,msg);
  348.            exit(1);
  349.            }
  350.         }
  351.      fseek(savescf,(long)(savebytes*i),SEEK_SET);
  352.      while (--savebytes >= 0)
  353.         putc(*(vidmem++),savescf);
  354.      }
  355.       setclear();
  356.       }
  357.    else
  358.       setfortext();
  359. }
  360.  
  361. void unstackscreen()
  362. {
  363.    char far *vidmem;
  364.    int savebytes;
  365.    unsigned char far *ptr;
  366.    textrow = saverc[screenctr] / 80;
  367.    textcol = saverc[screenctr] % 80;
  368.    if (--screenctr >= 0) { /* unstack */
  369.       vidmem = MK_FP(textaddr,0);
  370.       savebytes = (text_type == 0) ? 4000 : 16384;
  371.       if ((ptr = savescreen[screenctr])) {
  372.      far_memcpy(vidmem,ptr,savebytes);
  373.      farmemfree(ptr);
  374.      }
  375.       else {
  376.      fseek(savescf,(long)(savebytes*screenctr),SEEK_SET);
  377.      while (--savebytes >= 0)
  378.         *(vidmem++) = getc(savescf);
  379.      }
  380.       }
  381.    else
  382.       setforgraphics();
  383.    movecursor(-1,-1);
  384. }
  385.  
  386. void discardscreen()
  387. {
  388.    if (--screenctr >= 0) { /* unstack */
  389.       if (savescreen[screenctr])
  390.      farmemfree(savescreen[screenctr]);
  391.       }
  392.    else
  393.       discardgraphics();
  394. }
  395.  
  396.  
  397. /* --------------------------------------------------------------------- */
  398.  
  399. char speed_prompt[]="Speed key string";
  400.  
  401. int fullscreen_choice(
  402.     int options,         /* &2 use menu coloring scheme           */
  403.                  /* &4 include F1 for help in instructions */
  404.                  /* &8 add caller's instr after normal set */
  405.     char *hdg,         /* heading info, \n delimited           */
  406.     char *hdg2,         /* column heading or NULL               */
  407.     char *instr,         /* instructions, \n delimited, or NULL    */
  408.     int numchoices,      /* How many choices in list           */
  409.     char **choices,      /* array of choice strings            */
  410.     int *attributes,     /* &3: 0 normal color, 1,3 highlight      */
  411.                  /* &256 marks a dummy entry           */
  412.     int boxwidth,         /* box width, 0 for calc (in items)       */
  413.     int boxdepth,         /* box depth, 0 for calc, 99 for max      */
  414.     int colwidth,         /* data width of a column, 0 for calc     */
  415.     int current,         /* start with this item               */
  416.     void (*formatitem)(),/* routine to display an item or NULL     */
  417.     char *speedstring,   /* returned speed key value, or NULL >[30]*/
  418.     int (*speedprompt)(),/* routine to display prompt or NULL      */
  419.     int (*checkkey)()    /* routine to check keystroke or NULL     */
  420. )
  421.    /* return is: n>=0 for choice n selected,
  422.          -1 for escape
  423.           k for checkkey routine return value k (if not 0 nor -1)
  424.       speedstring[0] != 0 on return if string is present
  425.       */
  426. {
  427. static char far choiceinstr1a[]="Use the cursor keys to highlight your selection";
  428. static char far choiceinstr1b[]="Use the cursor keys or type a value to make a selection";
  429. static char far choiceinstr2a[]="Press ENTER for highlighted choice, or ESCAPE to back out";
  430. static char far choiceinstr2b[]="Press ENTER for highlighted choice, ESCAPE to back out, or F1 for help";
  431. static char far choiceinstr2c[]="Press ENTER for highlighted choice, or F1 for help";
  432.  
  433.    int titlelines,titlewidth;
  434.    int reqdrows;
  435.    int topleftrow,topleftcol;
  436.    int topleftchoice;
  437.    int speedrow;  /* speed key prompt */
  438.    int boxitems;  /* boxwidth*boxdepth */
  439.    int curkey,increment,rev_increment;
  440.    int redisplay;
  441.    int i,j,k;
  442.    char *charptr;
  443.    char buf[81];
  444.    int speed_match = 0;
  445.    char curitem[81];
  446.    char *itemptr;
  447.    int ret,savelookatmouse;
  448.  
  449.    savelookatmouse = lookatmouse;
  450.    lookatmouse = 0;
  451.    ret = -1;
  452.    if (speedstring
  453.      && (i = strlen(speedstring)) > 0) { /* preset current to passed string */
  454.       current = 0;
  455.       while (current < numchoices
  456.     && (k = strncasecmp(speedstring,choices[current],i)) > 0)
  457.      ++current;
  458.       if (k < 0 && current > 0)  /* oops - overshot */
  459.      --current;
  460.       if (current >= numchoices) /* bumped end of list */
  461.      current = numchoices - 1;
  462.       }
  463.  
  464.    while (1) {
  465.       if (current >= numchoices)  /* no real choice in the list? */
  466.      goto fs_choice_end;
  467.       if ((attributes[current] & 256) == 0)
  468.      break;
  469.       ++current;          /* scan for a real choice */
  470.       }
  471.  
  472.    titlelines = titlewidth = 0;
  473.    if (hdg) {
  474.       charptr = hdg;          /* count title lines, find widest */
  475.       i = 0;
  476.       titlelines = 1;
  477.       while (*charptr) {
  478.      if (*(charptr++) == '\n') {
  479.         ++titlelines;
  480.         i = -1;
  481.         }
  482.      if (++i > titlewidth)
  483.         titlewidth = i;
  484.      }
  485.       }
  486.  
  487.    if (colwidth == 0)          /* find widest column */
  488.       for (i = 0; i < numchoices; ++i)
  489.      if (strlen(choices[i]) > colwidth)
  490.         colwidth = strlen(choices[i]);
  491.  
  492.    /* title(1), blank(1), hdg(n), blank(1), body(n), blank(1), instr(?) */
  493.    reqdrows = 3;          /* calc rows available */
  494.    if (hdg)
  495.       reqdrows += titlelines + 1;
  496.    if (instr) {           /* count instructions lines */
  497.       charptr = instr;
  498.       ++reqdrows;
  499.       while (*charptr)
  500.      if (*(charptr++) == '\n')
  501.         ++reqdrows;
  502.       if ((options & 8))      /* show std instr too */
  503.      reqdrows += 2;
  504.       }
  505.    else
  506.       reqdrows += 2;          /* standard instructions */
  507.    if (speedstring) ++reqdrows;   /* a row for speedkey prompt */
  508.    if (boxdepth > (i = 25 - reqdrows)) /* limit the depth to max */
  509.       boxdepth = i;
  510.    if (boxwidth == 0) {       /* pick box width and depth */
  511.       if (numchoices <= i - 2) {  /* single column is 1st choice if we can */
  512.      boxdepth = numchoices;
  513.      boxwidth = 1;
  514.      }
  515.       else {              /* sort-of-wide is 2nd choice */
  516.      boxwidth = 60 / (colwidth + 1);
  517.      if (boxwidth == 0
  518.        || (boxdepth = (numchoices+boxwidth-1)/boxwidth) > i - 2) {
  519.         boxwidth = 80 / (colwidth + 1); /* last gasp, full width */
  520.         if ((boxdepth = (numchoices+boxwidth-1)/boxwidth) > i)
  521.            boxdepth = i;
  522.         }
  523.      }
  524.       }
  525.    if ((i = 77 / boxwidth - colwidth) > 3) /* spaces to add @ left each choice */
  526.       i = 3;
  527.    j = boxwidth * (colwidth += i) + i;       /* overall width of box */
  528.    if (j < titlewidth+2)
  529.       j = titlewidth + 2;
  530.    if (j > 80)
  531.       j = 80;
  532.    if (j <= 70 && boxwidth == 2) {       /* special case makes menus nicer */
  533.       ++j;
  534.       ++colwidth;
  535.       }
  536.    k = (80 - j) / 2;               /* center the box */
  537.    k -= (90 - j) / 20;
  538.    topleftcol = k + i;               /* column of topleft choice */
  539.    i = (25 - reqdrows - boxdepth) / 2;
  540.    i -= i / 4;                   /* higher is better if lots extra */
  541.    topleftrow = 3 + titlelines + i;       /* row of topleft choice */
  542.  
  543.    /* now set up the overall display */
  544.    helptitle();                /* clear, display title line */
  545.    setattr(1,0,C_PROMPT_BKGRD,24*80);       /* init rest to background */
  546.    for (i = topleftrow-1-titlelines; i < topleftrow+boxdepth+1; ++i)
  547.       setattr(i,k,C_PROMPT_LO,j);       /* draw empty box */
  548.    if (hdg) {
  549.       textcbase = (80 - titlewidth) / 2;   /* set left margin for putstring */
  550.       textcbase -= (90 - titlewidth) / 20; /* put heading into box */
  551.       putstring(topleftrow-titlelines-1,0,C_PROMPT_HI,hdg);
  552.       textcbase = 0;
  553.       }
  554.    if (hdg2)                   /* display 2nd heading */
  555.       putstring(topleftrow-1,topleftcol,C_PROMPT_MED,hdg2);
  556.    i = topleftrow + boxdepth + 1;
  557.    if (instr == NULL || (options & 8)) {   /* display default instructions */
  558.       if (i < 20) ++i;
  559.       if (speedstring) {
  560.      speedrow = i;
  561.      *speedstring = 0;
  562.      if (++i < 22) ++i;
  563.      }
  564.       putstringcenter(i++,0,80,C_PROMPT_BKGRD,
  565.         (speedstring) ? choiceinstr1b : choiceinstr1a);
  566.       putstringcenter(i++,0,80,C_PROMPT_BKGRD,
  567.         (options&CHOICEMENU) ? choiceinstr2c
  568.         : ((options&CHOICEHELP) ? choiceinstr2b : choiceinstr2a));
  569.       }
  570.    if (instr) {                /* display caller's instructions */
  571.       charptr = instr;
  572.       j = -1;
  573.       while ((buf[++j] = *(charptr++)))
  574.      if (buf[j] == '\n') {
  575.         buf[j] = 0;
  576.         putstringcenter(i++,0,80,C_PROMPT_BKGRD,buf);
  577.         j = -1;
  578.         }
  579.       putstringcenter(i,0,80,C_PROMPT_BKGRD,buf);
  580.       }
  581.  
  582.    boxitems = boxwidth * boxdepth;
  583.    topleftchoice = 0;               /* pick topleft for init display */
  584.    while (current - topleftchoice >= boxitems
  585.      || (current - topleftchoice > boxitems/2
  586.      && topleftchoice + boxitems < numchoices))
  587.       topleftchoice += boxwidth;
  588.    redisplay = 1;
  589.  
  590.    while (1) { /* main loop */
  591.  
  592.       if (redisplay) {                 /* display the current choices */
  593.      if ((options & CHOICEMENU) == 0) {
  594.         memset(buf,' ',80);
  595.         buf[boxwidth*colwidth] = 0;
  596.         for (i = (hdg2) ? 0 : -1; i <= boxdepth; ++i)  /* blank the box */
  597.            putstring(topleftrow+i,topleftcol,C_PROMPT_LO,buf);
  598.         }
  599.      for (i = 0; i+topleftchoice < numchoices && i < boxitems; ++i) {
  600.         /* display the choices */
  601.         if ((k = attributes[j = i+topleftchoice] & 3) == 1)
  602.            k = C_PROMPT_LO;
  603.         else if (k == 3)
  604.            k = C_PROMPT_HI;
  605.         else
  606.            k = C_PROMPT_MED;
  607.         if (formatitem)
  608.            (*formatitem)(j,charptr=buf);
  609.         else
  610.            charptr = choices[j];
  611.         putstring(topleftrow+i/boxwidth,topleftcol+(i%boxwidth)*colwidth,
  612.               k,charptr);
  613.         }
  614.      /***
  615.      ... format differs for summary/detail, whups, force box width to
  616.      ...  be 72 when detail toggle available?  (2 grey margin each
  617.      ...  side, 1 blue margin each side)
  618.      ***/
  619.      if (topleftchoice > 0 && hdg2 == NULL)
  620.         putstring(topleftrow-1,topleftcol,C_PROMPT_LO,"(more)");
  621.      if (topleftchoice + boxitems < numchoices)
  622.         putstring(topleftrow+boxdepth,topleftcol,C_PROMPT_LO,"(more)");
  623.      redisplay = 0;
  624.      }
  625.  
  626.       i = current - topleftchoice;         /* highlight the current choice */
  627.       if (formatitem)
  628.      (*formatitem)(current,itemptr=curitem);
  629.       else
  630.      itemptr = choices[current];
  631.       putstring(topleftrow+i/boxwidth,topleftcol+(i%boxwidth)*colwidth,
  632.         C_CHOICE_CURRENT,itemptr);
  633.  
  634.       if (speedstring) {             /* show speedstring if any */
  635.      memset(buf,' ',80);
  636.      buf[80] = 0;
  637.      putstring(speedrow,0,C_PROMPT_BKGRD,buf);
  638.      if (*speedstring) {             /* got a speedstring on the go */
  639.         putstring(speedrow,15,C_CHOICE_SP_INSTR," ");
  640.         if (speedprompt)
  641.            j = speedprompt(speedrow,16,C_CHOICE_SP_INSTR,speedstring,speed_match);
  642.         else {
  643.            putstring(speedrow,16,C_CHOICE_SP_INSTR,speed_prompt);
  644.            j = strlen(speed_prompt);
  645.            }
  646.         strcpy(buf,speedstring);
  647.         i = strlen(buf);
  648.         while (i < 30)
  649.            buf[i++] = ' ';
  650.         buf[i] = 0;
  651.         putstring(speedrow,16+j,C_CHOICE_SP_INSTR," ");
  652.         putstring(speedrow,17+j,C_CHOICE_SP_KEYIN,buf);
  653.         movecursor(speedrow,17+j+strlen(speedstring));
  654.         }
  655.      else
  656.         movecursor(25,80);
  657.      }
  658.       else
  659.      movecursor(25,80);
  660.  
  661.       while (!keypressed()) { } /* enables help */
  662.       curkey = getakey();
  663.  
  664.       i = current - topleftchoice;         /* unhighlight current choice */
  665.       if ((k = attributes[current] & 3) == 1)
  666.      k = C_PROMPT_LO;
  667.       else if (k == 3)
  668.      k = C_PROMPT_HI;
  669.       else
  670.      k = C_PROMPT_MED;
  671.       putstring(topleftrow+i/boxwidth,topleftcol+(i%boxwidth)*colwidth,
  672.         k,itemptr);
  673.  
  674.       increment = 0;
  675.       switch (curkey) {              /* deal with input key */
  676.      case ENTER:
  677.      case ENTER_2:
  678.         ret = current;
  679.         goto fs_choice_end;
  680.      case ESC:
  681.         goto fs_choice_end;
  682.      case DOWN_ARROW:
  683.      case DOWN_ARROW_2:
  684.         rev_increment = 0 - (increment = boxwidth);
  685.         break;
  686.      case UP_ARROW:
  687.      case UP_ARROW_2:
  688.         increment = 0 - (rev_increment = boxwidth);
  689.         break;
  690.      case RIGHT_ARROW:
  691.      case RIGHT_ARROW_2:
  692.         if (boxwidth == 1) break;
  693.         increment = 1; rev_increment = -1;
  694.         break;
  695.      case LEFT_ARROW:
  696.      case LEFT_ARROW_2:
  697.         if (boxwidth == 1) break;
  698.         increment = -1; rev_increment = 1;
  699.         break;
  700.      case PAGE_UP:
  701.         if (numchoices > boxitems) {
  702.            topleftchoice -= boxitems;
  703.            increment = -boxitems;
  704.            rev_increment = boxwidth;
  705.            redisplay = 1;
  706.            }
  707.         break;
  708.      case PAGE_DOWN:
  709.         if (numchoices > boxitems) {
  710.            topleftchoice += boxitems;
  711.            increment = boxitems;
  712.            rev_increment = -boxwidth;
  713.            redisplay = 1;
  714.            }
  715.         break;
  716.      case CTL_HOME:
  717.         current = -1;
  718.         increment = rev_increment = 1;
  719.         break;
  720.      case CTL_END:
  721.         current = numchoices;
  722.         increment = rev_increment = -1;
  723.         break;
  724.      default:
  725.         if (checkkey) {
  726.            if ((ret = (*checkkey)(curkey,current)) < -1 || ret > 0)
  727.           goto fs_choice_end;
  728.            if (ret == -1)
  729.           redisplay = -1;
  730.            }
  731.         ret = -1;
  732.         if (speedstring) {
  733.            i = strlen(speedstring);
  734.            if (curkey == 8 && i > 0) /* backspace */
  735.           speedstring[--i] = 0;
  736.            if (33 <= curkey && curkey <= 126 && i < 30) {
  737.           curkey = tolower(curkey);
  738.           speedstring[i] = curkey;
  739.           speedstring[++i] = 0;
  740.           }
  741.            if (i > 0) {         /* locate matching type */
  742.           current = 0;
  743.           while (current < numchoices
  744.             && (speed_match = strncasecmp(speedstring,choices[current],i)) > 0)
  745.              ++current;
  746.           if (speed_match < 0 && current > 0)  /* oops - overshot */
  747.              --current;
  748.           if (current >= numchoices) /* bumped end of list */
  749.              current = numchoices - 1;
  750.           }
  751.            }
  752.         break;
  753.      }
  754.  
  755.       if (increment) {            /* apply cursor movement */
  756.      current += increment;
  757.      if (speedstring)        /* zap speedstring */
  758.         speedstring[0] = 0;
  759.      }
  760.       while (1) {            /* adjust to a non-comment choice */
  761.      if (current < 0 || current >= numchoices)
  762.          increment = rev_increment;
  763.      else if ((attributes[current] & 256) == 0)
  764.          break;
  765.      current += increment;
  766.      }
  767.       if (topleftchoice > numchoices - boxitems)
  768.      topleftchoice = ((numchoices+boxwidth-1)/boxwidth)*boxwidth - boxitems;
  769.       if (topleftchoice < 0)
  770.      topleftchoice = 0;
  771.       while (current < topleftchoice) {
  772.      topleftchoice -= boxwidth;
  773.      redisplay = 1;
  774.      }
  775.       while (current >= topleftchoice + boxitems) {
  776.      topleftchoice += boxwidth;
  777.      redisplay = 1;
  778.      }
  779.       }
  780.  
  781. fs_choice_end:
  782.    lookatmouse = savelookatmouse;
  783.    return(ret);
  784.  
  785. }
  786.  
  787.  
  788. /* case independent version of strncmp */
  789. int strncasecmp(char *s,char *t,int ct)
  790. {
  791.    for(; (tolower(*s) == tolower(*t)) && --ct ; s++,t++)
  792.       if(*s == '\0')
  793.      return(0);
  794.    return(tolower(*s) - tolower(*t));
  795. }
  796.  
  797.  
  798. static int menutype;
  799. #define MENU_HDG 3
  800. #define MENU_ITEM 1
  801.  
  802. int main_menu(int fullmenu)
  803. {
  804.    char *choices[44]; /* 2 columns * 22 rows */
  805.    int attributes[44];
  806.    int choicekey[44];
  807.    int i;
  808.    int nextleft,nextright;
  809.    int oldtabmode,oldhelpmode;
  810.    oldtabmode = tabmode;
  811.    oldhelpmode = helpmode;
  812. top:
  813.    menutype = fullmenu;
  814.    tabmode = 0;
  815.    for (i = 0; i < 44; ++i) {
  816.       attributes[i] = 256;
  817.       choices[i] = "";
  818.       choicekey[i] = -1;
  819.       }
  820.    nextleft = -2;
  821.    nextright = -1;
  822.  
  823.    if (fullmenu) {
  824.       choices[nextleft+=2] = "      CURRENT IMAGE";
  825.       attributes[nextleft] = 256+MENU_HDG;
  826.       choicekey[nextleft+=2] = 13; /* enter */
  827.       attributes[nextleft] = MENU_ITEM;
  828.       if (calc_status == 2)
  829.      choices[nextleft] = "continue calculation";
  830.       else
  831.      choices[nextleft] = "return to image";
  832.       choicekey[nextleft+=2] = 9; /* tab */
  833.       attributes[nextleft] = MENU_ITEM;
  834.       choices[nextleft] = "info about image      <tab>";
  835.       choicekey[nextleft+=2] = -10;
  836.       attributes[nextleft] = MENU_ITEM;
  837.       choices[nextleft] = "zoom box functions...";
  838.       nextleft+=2;
  839.       }
  840.    choices[nextleft+=2] = "      NEW IMAGE";
  841.    attributes[nextleft] = 256+MENU_HDG;
  842.    choicekey[nextleft+=2] = DELETE;
  843.    attributes[nextleft] = MENU_ITEM;
  844.    choices[nextleft] = "select video mode...  <del>";
  845.    choicekey[nextleft+=2] = 't';
  846.    attributes[nextleft] = MENU_ITEM;
  847.    choices[nextleft] = "select fractal type    <t>";
  848.    if (fullmenu) {
  849.       if ((curfractalspecific->tojulia != NOFRACTAL
  850.       && param[0] == 0.0 && param[1] == 0.0)
  851.       || curfractalspecific->tomandel != NOFRACTAL) {
  852.      choicekey[nextleft+=2] = ' ';
  853.      attributes[nextleft] = MENU_ITEM;
  854.      choices[nextleft] = "toggle to/from julia <space>";
  855.      }
  856.       choicekey[nextleft+=2] = '\\';
  857.       attributes[nextleft] = MENU_ITEM;
  858.       choices[nextleft] = "return to prior image  <\\>";
  859.       }
  860.    nextleft += 2;
  861.    choices[nextleft+=2] = "      OPTIONS";
  862.    attributes[nextleft] = 256+MENU_HDG;
  863.    choicekey[nextleft+=2] = 'x';
  864.    attributes[nextleft] = MENU_ITEM;
  865.    choices[nextleft] = "basic options...       <x>";
  866.    choicekey[nextleft+=2] = 'y';
  867.    attributes[nextleft] = MENU_ITEM;
  868.    choices[nextleft] = "extended doodads...    <y>";
  869.    choicekey[nextleft+=2] = 'z';
  870.    attributes[nextleft] = MENU_ITEM;
  871.    choices[nextleft] = "type-specific parms... <z>";
  872.    choicekey[nextleft+=2] = 'v';
  873.    attributes[nextleft] = MENU_ITEM;
  874.    choices[nextleft] = "view window options... <v>";
  875.    choicekey[nextleft+=2] = 'i';
  876.    attributes[nextleft] = MENU_ITEM;
  877.    choices[nextleft] = "fractal 3D parms...    <i>";
  878.  
  879.    choices[nextright+=2] = "        FILE";
  880.    attributes[nextright] = 256+MENU_HDG;
  881.    choicekey[nextright+=2] = '@';
  882.    attributes[nextright] = MENU_ITEM;
  883.    choices[nextright] = "run saved command set... <@>";
  884.    if (fullmenu) {
  885.       choicekey[nextright+=2] = 's';
  886.       attributes[nextright] = MENU_ITEM;
  887.       choices[nextright] = "save image to file       <s>";
  888.       }
  889.    choicekey[nextright+=2] = 'r';
  890.    attributes[nextright] = MENU_ITEM;
  891.    choices[nextright] = "load image from file...  <r>";
  892.    choicekey[nextright+=2] = '3';
  893.    attributes[nextright] = MENU_ITEM;
  894.    choices[nextright] = "3d transform from file...<3>";
  895.    if (fullmenu) {
  896.       choicekey[nextright+=2] = 'o';
  897.       attributes[nextright] = MENU_ITEM;
  898.       choices[nextright] = "3d overlay from file...  <o>";
  899.       choicekey[nextright+=2] = 'b';
  900.       attributes[nextright] = MENU_ITEM;
  901.       choices[nextright] = "save current parameters..<b>";
  902.       choicekey[nextright+=2] = 'p';
  903.       attributes[nextright] = MENU_ITEM;
  904.       choices[nextright] = "print image              <p>";
  905.       }
  906.    choicekey[nextright+=2] = 'd';
  907.    attributes[nextright] = MENU_ITEM;
  908.    choices[nextright] = "shell to dos             <d>";
  909.    choicekey[nextright+=2] = ESC;
  910.    attributes[nextright] = MENU_ITEM;
  911.    choices[nextright] = "quit Fractint           <esc>";
  912.    choicekey[nextright+=2] = 1082;
  913.    attributes[nextright] = MENU_ITEM;
  914.    choices[nextright] = "restart Fractint        <ins>";
  915.    if (fullmenu && gotrealdac && colors >= 16) {
  916.       nextright += 2;
  917.       choices[nextright+=2] = "       COLORS";
  918.       attributes[nextright] = 256+MENU_HDG;
  919.       /*** choicekey[nextright+=2] = -12;
  920.         attributes[nextright] = MENU_ITEM;
  921.         choices[nextright] = "color cycling menu...";
  922.         choicekey[nextright+=2] = -13;
  923.         attributes[nextright] = MENU_ITEM;
  924.         choices[nextright] = "palette editing menu..."; ***/
  925.       choicekey[nextright+=2] = 'c';
  926.       attributes[nextright] = MENU_ITEM;
  927.       choices[nextright] = "color cycling mode       <c>";
  928.       choicekey[nextright+=2] = '+';
  929.       attributes[nextright] = MENU_ITEM;
  930.       choices[nextright] = "rotate palette      <+>, <->";
  931.       if (colors > 16) {
  932.      if (!reallyega) {
  933.         choicekey[nextright+=2] = 'e';
  934.         attributes[nextright] = MENU_ITEM;
  935.         choices[nextright] = "palette editing mode     <e>";
  936.         }
  937.      choicekey[nextright+=2] = 'a';
  938.      attributes[nextright] = MENU_ITEM;
  939.      choices[nextright] = "make starfield           <a>";
  940.      }
  941.       }
  942.  
  943.    i = (keypressed()) ? getakey() : 0;
  944.    if (menu_checkkey(i,0) == 0) {
  945.       helpmode = HELPMAIN;       /* switch help modes */
  946.       if ((nextleft += 2) < nextright)
  947.      nextleft = nextright + 1;
  948.       i = fullscreen_choice(CHOICEMENU,"          MAIN MENU\n"
  949.                        "(<Esc> returns here from image)",
  950.       NULL,NULL,nextleft,choices,attributes,
  951.       2,nextleft/2,29,0,NULL,NULL,NULL,menu_checkkey);
  952.       if (i == -1)     /* escape */
  953.      i = ESC;
  954.       else if (i < 0)
  955.      i = 0 - i;
  956.       else {              /* user selected a choice */
  957.      i = choicekey[i];
  958.      switch (i) {          /* check for special cases */
  959.         case -10:          /* zoombox functions */
  960.            helpmode = HELPZOOM;
  961.            help(0);
  962.            i = 0;
  963.            break;
  964.         }
  965.      }
  966.       }
  967.    if (i == ESC) {           /* escape from menu exits Fractint */
  968.       static char far s[] = "Exit from Fractint (y/n)? y";
  969.       helptitle();
  970.       setattr(1,0,C_GENERAL_MED,24*80);
  971.       for (i = 9; i <= 11; ++i)
  972.      setattr(i,18,C_GENERAL_INPUT,40);
  973.       putstringcenter(10,18,40,C_GENERAL_INPUT,s);
  974.       movecursor(25,80);
  975.       while ((i = getakey()) != 'y' && i != 'Y' && i != 13) {
  976.      if (i == 'n' || i == 'N')
  977.         goto top;
  978.      }
  979.       goodbye();
  980.       }
  981.    if (i == TAB) {
  982.       tab_display();
  983.       i = 0;
  984.       }
  985.    if (i == ENTER || i == ENTER_2)
  986.       i = 0;             /* don't trigger new calc */
  987.    tabmode = oldtabmode;
  988.    helpmode = oldhelpmode;
  989.    return(i);
  990. }
  991.  
  992. static int menu_checkkey(int curkey,int choice)
  993. {
  994.    int testkey;
  995.    testkey = (curkey>='A' && curkey<='Z') ? curkey+('a'-'A') : curkey;
  996.    if (strchr("@txyzvir3d",testkey) || testkey == INSERT
  997.      || testkey == ESC || testkey == DELETE)
  998.       return(0-testkey);
  999.    if (menutype) {
  1000.       if (strchr("\\sobp",testkey) || testkey == 9)
  1001.      return(0-testkey);
  1002.       if (testkey == ' ')
  1003.      if ((curfractalspecific->tojulia != NOFRACTAL
  1004.           && param[0] == 0.0 && param[1] == 0.0)
  1005.        || curfractalspecific->tomandel != NOFRACTAL)
  1006.      return(0-testkey);
  1007.       if (gotrealdac && colors >= 16) {
  1008.      if (strchr("c+-",testkey))
  1009.         return(0-testkey);
  1010.      if (colors > 16
  1011.        && (testkey == 'a' || (!reallyega && testkey == 'e')))
  1012.         return(0-testkey);
  1013.      }
  1014.       }
  1015.    if (check_vidmode_key(0,testkey) >= 0)
  1016.       return(0-testkey);
  1017.    return(0);
  1018. }
  1019.  
  1020.  
  1021. int input_field(
  1022.     int options,          /* &1 numeric, &2 integer, &4 double */
  1023.     int attr,          /* display attribute */
  1024.     char *fld,          /* the field itself */
  1025.     int len,          /* field length (declare as 1 larger for \0) */
  1026.     int row,          /* display row */
  1027.     int col,          /* display column */
  1028.     int (*checkkey)(int)  /* routine to check non data keys, or NULL */
  1029.     )
  1030. {
  1031.    char savefld[81];
  1032.    char buf[81];
  1033.    int insert, started, offset, curkey, display;
  1034.    int i, j;
  1035.    int ret,savelookatmouse;
  1036.    savelookatmouse = lookatmouse;
  1037.    lookatmouse = 0;
  1038.    ret = -1;
  1039.    strcpy(savefld,fld);
  1040.    insert = started = offset = 0;
  1041.    display = 1;
  1042.    while (1) {
  1043.       strcpy(buf,fld);
  1044.       i = strlen(buf);
  1045.       while (i < len)
  1046.      buf[i++] = ' ';
  1047.       buf[len] = 0;
  1048.       if (display) {                    /* display current value */
  1049.      putstring(row,col,attr,buf);
  1050.      display = 0;
  1051.      }
  1052.       curkey = keycursor(row+insert,col+offset);  /* get a keystroke */
  1053.       switch (curkey) {
  1054.      case ENTER:
  1055.      case ENTER_2:
  1056.         ret = 0;
  1057.         goto inpfld_end;
  1058.      case ESC:
  1059.         goto inpfld_end;
  1060.      case RIGHT_ARROW:
  1061.      case RIGHT_ARROW_2:
  1062.         if (offset < len) ++offset;
  1063.         started = 1;
  1064.         break;
  1065.      case LEFT_ARROW:
  1066.      case LEFT_ARROW_2:
  1067.         if (offset > 0) --offset;
  1068.         started = 1;
  1069.         break;
  1070.      case HOME:
  1071.         offset = 0;
  1072.         started = 1;
  1073.         break;
  1074.      case END:
  1075.         offset = strlen(fld);
  1076.         started = 1;
  1077.         break;
  1078.      case 8:
  1079.      case 127:                /* backspace */
  1080.         if (offset > 0) {
  1081.            j = strlen(fld);
  1082.            for (i = offset-1; i < j; ++i)
  1083.           fld[i] = fld[i+1];
  1084.            --offset;
  1085.            }
  1086.         started = display = 1;
  1087.         break;
  1088.      case DELETE:                /* delete */
  1089.         j = strlen(fld);
  1090.         for (i = offset; i < j; ++i)
  1091.            fld[i] = fld[i+1];
  1092.         started = display = 1;
  1093.         break;
  1094.      case 1082:                /* insert */
  1095.         insert ^= 0x8000;
  1096.         started = 1;
  1097.         break;
  1098.      case F5:
  1099.         strcpy(fld,savefld);
  1100.         insert = started = offset = 0;
  1101.         display = 1;
  1102.         break;
  1103.      default:
  1104.         if (curkey < 32 || curkey >= 127) {
  1105.            if (checkkey && (ret = (*checkkey)(curkey)))
  1106.           goto inpfld_end;
  1107.            break;                     /* non alphanum char */
  1108.            }
  1109.         if (offset >= len) break;             /* at end of field */
  1110.         if (insert && started && strlen(fld) >= len)
  1111.            break;                     /* insert & full */
  1112.         if ((options & 1)
  1113.           && (curkey < '0' || curkey > '9')
  1114.           && curkey != '+' && curkey != '-') {
  1115.            if ((options & 2))
  1116.           break;
  1117.            /* allow scientific notation, and specials "e" and "p" */
  1118.            if ( ((curkey != 'e' && curkey != 'E') || offset >= 18)
  1119.          && ((curkey != 'p' && curkey != 'P') || offset != 0 )
  1120.          && curkey != '.')
  1121.           break;
  1122.            }
  1123.         if (started == 0) /* first char is data, zap field */
  1124.            fld[0] = 0;
  1125.         if (insert) {
  1126.            j = strlen(fld);
  1127.            while (j >= offset) {
  1128.           fld[j+1] = fld[j];
  1129.           --j;
  1130.           }
  1131.            }
  1132.         if (offset >= strlen(fld))
  1133.            fld[offset+1] = 0;
  1134.         fld[offset++] = curkey;
  1135.         /* if "e" or "p" in first col make number e or pi */
  1136.         if ((options & 3) == 1) { /* floating point */
  1137.            double tmpd;
  1138.            int specialv;
  1139.            char tmpfld[30];
  1140.            specialv = 0;
  1141.            if (*fld == 'e' || *fld == 'E') {
  1142.           tmpd = exp(1.0);
  1143.           specialv = 1;
  1144.           }
  1145.            if (*fld == 'p' || *fld == 'P') {
  1146.           tmpd = atan(1.0) * 4;
  1147.           specialv = 1;
  1148.           }
  1149.            if (specialv) {
  1150.           if ((options & 4) == 0)
  1151.              roundfloatd(&tmpd);
  1152.           sprintf(tmpfld,"%.15g",tmpd);
  1153.           tmpfld[len-1] = 0; /* safety, field should be long enough */
  1154.           strcpy(fld,tmpfld);
  1155.           offset = 0;
  1156.           }
  1157.            }
  1158.         started = display = 1;
  1159.      }
  1160.       }
  1161. inpfld_end:
  1162.    lookatmouse = savelookatmouse;
  1163.    return(ret);
  1164. }
  1165.  
  1166. int field_prompt(
  1167.     int options,        /* &1 numeric value, &2 integer */
  1168.     char *hdg,        /* heading, \n delimited lines */
  1169.     char *instr,        /* additional instructions or NULL */
  1170.     char *fld,        /* the field itself */
  1171.     int len,        /* field length (declare as 1 larger for \0) */
  1172.     int (*checkkey)()   /* routine to check non data keys, or NULL */
  1173.     )
  1174. {
  1175.    char *charptr;
  1176.    int boxwidth,titlelines,titlecol,titlerow;
  1177.    int promptcol;
  1178.    int i,j;
  1179.    char buf[81];
  1180.    helptitle();               /* clear screen, display title */
  1181.    setattr(1,0,C_PROMPT_BKGRD,24*80);      /* init rest to background */
  1182.    charptr = hdg;              /* count title lines, find widest */
  1183.    i = boxwidth = 0;
  1184.    titlelines = 1;
  1185.    while (*charptr) {
  1186.       if (*(charptr++) == '\n') {
  1187.      ++titlelines;
  1188.      i = -1;
  1189.      }
  1190.       if (++i > boxwidth)
  1191.      boxwidth = i;
  1192.       }
  1193.    if (len > boxwidth)
  1194.       boxwidth = len;
  1195.    i = titlelines + 4;              /* total rows in box */
  1196.    titlerow = (25 - i) / 2;          /* top row of it all when centered */
  1197.    titlerow -= titlerow / 4;          /* higher is better if lots extra */
  1198.    titlecol = (80 - boxwidth) / 2;      /* center the box */
  1199.    titlecol -= (90 - boxwidth) / 20;
  1200.    promptcol = titlecol - (boxwidth-len)/2;
  1201.    j = titlecol;              /* add margin at each side of box */
  1202.    if ((i = (82-boxwidth)/4) > 3)
  1203.       i = 3;
  1204.    j -= i;
  1205.    boxwidth += i * 2;
  1206.    for (i = -1; i < titlelines+3; ++i)      /* draw empty box */
  1207.       setattr(titlerow+i,j,C_PROMPT_LO,boxwidth);
  1208.    textcbase = titlecol;          /* set left margin for putstring */
  1209.    putstring(titlerow,0,C_PROMPT_HI,hdg); /* display heading */
  1210.    textcbase = 0;
  1211.    i = titlerow + titlelines + 4;
  1212.    if (instr) {               /* display caller's instructions */
  1213.       charptr = instr;
  1214.       j = -1;
  1215.       while ((buf[++j] = *(charptr++)))
  1216.      if (buf[j] == '\n') {
  1217.         buf[j] = 0;
  1218.         putstringcenter(i++,0,80,C_PROMPT_BKGRD,buf);
  1219.         j = -1;
  1220.         }
  1221.       putstringcenter(i,0,80,C_PROMPT_BKGRD,buf);
  1222.       }
  1223.    else                   /* default instructions */
  1224.       putstringcenter(i,0,80,C_PROMPT_BKGRD,
  1225.           "Press ENTER when finished (or ESCAPE to back out)");
  1226.    return(input_field(options,C_PROMPT_INPUT,fld,len,
  1227.               titlerow+titlelines+1,promptcol,checkkey));
  1228. }
  1229.  
  1230.  
  1231. /* thinking(1,message):
  1232.       if thinking message not yet on display, it is displayed;
  1233.       otherwise the wheel is updated
  1234.       returns 0 to keep going, -1 if keystroke pending
  1235.    thinking(0,NULL):
  1236.       call this when thinking phase is done
  1237.    */
  1238.  
  1239. int thinking(int options,char *msg)
  1240. {
  1241.    static int thinkstate = -1;
  1242.    static char *wheel[] = {"-","\\","|","/"};
  1243.    static int thinkcol;
  1244.    char buf[81];
  1245.    if (options == 0) {
  1246.       if (thinkstate >= 0) {
  1247.      thinkstate = -1;
  1248.      unstackscreen();
  1249.      }
  1250.       return(0);
  1251.       }
  1252.    if (thinkstate < 0) {
  1253.       stackscreen();
  1254.       thinkstate = 0;
  1255.       helptitle();
  1256.       strcpy(buf,"  ");
  1257.       strcat(buf,msg);
  1258.       strcat(buf,"    ");
  1259.       putstring(4,10,C_GENERAL_HI,buf);
  1260.       thinkcol = textcol - 3;
  1261.       }
  1262.    putstring(4,thinkcol,C_GENERAL_HI,wheel[thinkstate]);
  1263.    movecursor(25,80); /* turn off cursor */
  1264.    thinkstate = (thinkstate + 1) & 3;
  1265.    return (keypressed());
  1266. }
  1267.  
  1268.  
  1269. void clear_screen(void)  /* a stub for a windows only subroutine */
  1270. {
  1271. }
  1272.  
  1273.  
  1274. /* savegraphics/restoregraphics: video.asm subroutines */
  1275.  
  1276. static unsigned char far *swapsavebuf;
  1277. static unsigned int memhandle;
  1278. unsigned long swaptotlen;
  1279. unsigned long swapoffset;
  1280. unsigned char far *swapvidbuf;
  1281. int swaplength;
  1282. static int swaptype = -1;
  1283. static int swapblklen; /* must be a power of 2 */
  1284. extern void (*swapsetup)(void),swapnormread(void);
  1285. void movewords(int,unsigned char far*,unsigned char far*);
  1286. extern unsigned char suffix[4096];
  1287.  
  1288. extern    unsigned int emmallocate(unsigned int),xmmallocate(unsigned int);
  1289. extern    void   emmdeallocate(unsigned int),xmmdeallocate(unsigned int);
  1290. extern    void   emmgetpage(unsigned int, unsigned int);
  1291. extern    unsigned char far *emmquery(void);
  1292. extern    unsigned int *xmmquery(void);
  1293.  
  1294. int savegraphics()
  1295. {
  1296.    extern int made_dsktemp;
  1297.    int i;
  1298.    struct {
  1299.       unsigned long   len;
  1300.       unsigned int    sourcehandle;
  1301.       unsigned long   sourceptr;
  1302.       unsigned int    desthandle;
  1303.       unsigned long   destptr;
  1304.       } xmmparms;
  1305.    discardgraphics(); /* if any emm/xmm in use from prior call, release it */
  1306.    swaptotlen = (long)sxdots * sydots;
  1307.    i = colors;
  1308.    while (i <= 16) {
  1309.       swaptotlen >>= 1;
  1310.       i = i * i;
  1311.       }
  1312.    swapoffset = 0;
  1313.    if (debugflag != 420 && debugflag != 422 /* 422=xmm test, 420=disk test */
  1314.      && (swapsavebuf = emmquery()) != NULL
  1315.      && (memhandle = emmallocate((unsigned int)((swaptotlen + 16383) >> 14)))
  1316.      != 0) {
  1317.       swaptype = 0; /* use expanded memory */
  1318.       swapblklen = 16384;
  1319.       }
  1320.    else if (debugflag != 420
  1321.      && xmmquery() !=0
  1322.      && (memhandle = xmmallocate((unsigned int)((swaptotlen + 1023) >> 10)))
  1323.      != 0) {
  1324.       swaptype = 1; /* use extended memory */
  1325.       swapblklen = 16384;
  1326.       }
  1327.    else {
  1328.       swaptype = 2; /* use disk */
  1329.       swapblklen = 4096;
  1330.  
  1331.    /* MCP 7-7-91, If 'memhandle' is an 'unsigned int', how is it ever going
  1332.       to be equal to -1?
  1333.  
  1334.       if ((memhandle = open("FRACTINT.DSK",O_CREAT|O_WRONLY|O_BINARY,S_IWRITE))
  1335.      == -1) {
  1336.    */
  1337.       if ((memhandle = open("FRACTINT.DSK",O_CREAT|O_WRONLY|O_BINARY,S_IWRITE))
  1338.      == 0xffff) {
  1339.  
  1340.  
  1341. dskfile_error:
  1342.      setvideotext(); /* text mode */
  1343.      setclear();
  1344.      printf("!!! error in temp file FRACTINT.DSK (disk full?) - aborted\n\n");
  1345.      exit(1);
  1346.      }
  1347.       made_dsktemp = 1;
  1348.       }
  1349.    while (swapoffset < swaptotlen) {
  1350.       swaplength = swapblklen;
  1351.       if ((swapoffset & (swapblklen-1)) != 0)
  1352.      swaplength = swapblklen - (swapoffset & (swapblklen-1));
  1353.       if (swaplength > swaptotlen - swapoffset)
  1354.      swaplength = swaptotlen - swapoffset;
  1355.       (*swapsetup)(); /* swapoffset,swaplength -> sets swapvidbuf,swaplength */
  1356.       switch(swaptype) {
  1357.      case 0:
  1358.         emmgetpage((unsigned int)(swapoffset>>14),memhandle);
  1359.         movewords(swaplength>>1,swapvidbuf,
  1360.               swapsavebuf+(swapoffset&(swapblklen-1)));
  1361.         break;
  1362.      case 1:
  1363.         xmmparms.len = swaplength;
  1364.         xmmparms.sourcehandle = 0; /* Source is conventional memory */
  1365.         xmmparms.sourceptr = (unsigned long)swapvidbuf;
  1366.         xmmparms.desthandle = memhandle;
  1367.         xmmparms.destptr = swapoffset;
  1368.         xmmmoveextended(&xmmparms);
  1369.         break;
  1370.      default:
  1371.         movewords(swaplength>>1,swapvidbuf,(unsigned char far *)suffix);
  1372.         if (write(memhandle,suffix,swaplength) == -1)
  1373.            goto dskfile_error;
  1374.      }
  1375.       swapoffset += swaplength;
  1376.       }
  1377.    if (swaptype == 2)
  1378.       close(memhandle);
  1379.    return 0;
  1380. }
  1381.  
  1382. void restoregraphics()
  1383. {
  1384.    struct {
  1385.       unsigned long   len;
  1386.       unsigned int    sourcehandle;
  1387.       unsigned long   sourceptr;
  1388.       unsigned int    desthandle;
  1389.       unsigned long   destptr;
  1390.       } xmmparms;
  1391.    swapoffset = 0;
  1392.    if (swaptype == 2)
  1393.       memhandle = open("FRACTINT.DSK",O_RDONLY|O_BINARY,S_IREAD);
  1394.    swapvidbuf = MK_FP(extraseg+0x1000,0); /* for swapnormwrite case */
  1395.    while (swapoffset < swaptotlen) {
  1396.       swaplength = swapblklen;
  1397.       if ((swapoffset & (swapblklen-1)) != 0)
  1398.      swaplength = swapblklen - (swapoffset & (swapblklen-1));
  1399.       if (swaplength > swaptotlen - swapoffset)
  1400.      swaplength = swaptotlen - swapoffset;
  1401.       if (swapsetup != swapnormread)
  1402.      (*swapsetup)(); /* swapoffset,swaplength -> sets swapvidbuf,swaplength */
  1403.       switch(swaptype) {
  1404.      case 0:
  1405.         emmgetpage((unsigned int)(swapoffset>>14),memhandle);
  1406.         movewords(swaplength>>1,swapsavebuf+(swapoffset&(swapblklen-1)),
  1407.               swapvidbuf);
  1408.         break;
  1409.      case 1:
  1410.         xmmparms.len = swaplength;
  1411.         xmmparms.sourcehandle = memhandle;
  1412.         xmmparms.sourceptr = swapoffset;
  1413.         xmmparms.desthandle = 0; /* conventional memory */
  1414.         xmmparms.destptr = (unsigned long)swapvidbuf;
  1415.         xmmmoveextended(&xmmparms);
  1416.         break;
  1417.      default:
  1418.         read(memhandle,suffix,swaplength);
  1419.         movewords(swaplength>>1,(unsigned char far *)suffix,swapvidbuf);
  1420.      }
  1421.       if (swapsetup == swapnormread)
  1422.      swapnormwrite();
  1423.       swapoffset += swaplength;
  1424.       }
  1425.    if (swaptype == 2)
  1426.       close(memhandle);
  1427.    discardgraphics();
  1428. }
  1429.  
  1430. void discardgraphics() /* release expanded/extended memory if any in use */
  1431. {
  1432.    switch(swaptype) {
  1433.       case 0:
  1434.      emmdeallocate(memhandle);
  1435.      break;
  1436.       case 1:
  1437.      xmmdeallocate(memhandle);
  1438.       }
  1439.    swaptype = -1;
  1440.    }
  1441.  
  1442.  
  1443. extern int extraseg;
  1444. extern int badconfig;
  1445. extern struct videoinfo far videotable[];
  1446. struct videoinfo far *vidtbl;  /* temporarily loaded fractint.cfg info */
  1447. int vidtbllen;               /* number of entries in above           */
  1448.  
  1449. int load_fractint_cfg(int options)
  1450. {
  1451.    /* Reads fractint.cfg, loading videoinfo entries into extraseg. */
  1452.    /* Sets vidtbl pointing to the loaded table, and returns the    */
  1453.    /* number of entries (also sets vidtbllen to this).           */
  1454.    /* Past vidtbl, cfglinenums are stored for update_fractint_cfg. */
  1455.    /* If fractint.cfg is not found or invalid, issues a message    */
  1456.    /* (first time the problem occurs only, and only if options is  */
  1457.    /* zero) and uses the hard-coded table.               */
  1458.  
  1459.    FILE *cfgfile;
  1460.    struct videoinfo far *vident;
  1461.    int far *cfglinenums;
  1462.    int linenum;
  1463.    int i, j, keynum, ax, bx, cx, dx, dotmode, xdots, ydots, colors;
  1464.    int commas[10];
  1465.    int textsafe2;
  1466.    char tempstring[150];
  1467.  
  1468.    vidtbl = MK_FP(extraseg,0);
  1469.    cfglinenums = (int far *)(&vidtbl[MAXVIDEOMODES]);
  1470.  
  1471.    if (badconfig)  /* fractint.cfg already known to be missing or bad */
  1472.       goto use_resident_table;
  1473.  
  1474.    findpath("fractint.cfg",tempstring);
  1475.    if (tempstring[0] == 0                 /* can't find the file */
  1476.      || (cfgfile = fopen(tempstring,"r")) == NULL)   /* can't open it */
  1477.       goto bad_fractint_cfg;
  1478.  
  1479.    vidtbllen = 0;
  1480.    linenum = 0;
  1481.    vident = vidtbl;
  1482.    while (vidtbllen < MAXVIDEOMODES
  1483.      && fgets(tempstring, 120, cfgfile)) {
  1484.       ++linenum;
  1485.       if (tempstring[0] == ';') continue;   /* comment line */
  1486.       tempstring[120] = 0;
  1487.       tempstring[strlen(tempstring)-1] = 0; /* zap trailing \n */
  1488.       memset(commas,0,20);
  1489.       i = j = -1;
  1490.       while (1) {
  1491.      if (tempstring[++i] < ' ') {
  1492.         if (tempstring[i] == 0) break;
  1493.         tempstring[i] = ' '; /* convert tab (or whatever) to blank */
  1494.         }
  1495.      else if (tempstring[i] == ',' && ++j < 10) {
  1496.         commas[j] = i + 1;     /* remember start of next field */
  1497.         tempstring[i] = 0;     /* make field a separate string */
  1498.         }
  1499.      }
  1500.       keynum = check_vidmode_keyname(tempstring);
  1501.       sscanf(&tempstring[commas[1]],"%x",&ax);
  1502.       sscanf(&tempstring[commas[2]],"%x",&bx);
  1503.       sscanf(&tempstring[commas[3]],"%x",&cx);
  1504.       sscanf(&tempstring[commas[4]],"%x",&dx);
  1505.       dotmode      = atoi(&tempstring[commas[5]]);
  1506.       xdots      = atoi(&tempstring[commas[6]]);
  1507.       ydots      = atoi(&tempstring[commas[7]]);
  1508.       colors      = atoi(&tempstring[commas[8]]);
  1509.       textsafe2   = dotmode / 100;
  1510.       dotmode     %= 100;
  1511.       if (j != 9 ||
  1512.         keynum < 0 ||
  1513.         dotmode < 0 || dotmode > 30 ||
  1514.         textsafe2 < 0 || textsafe2 > 4 ||
  1515.         xdots < 160 || xdots > 2048 ||
  1516.         ydots < 160 || ydots > 2048 ||
  1517.         (colors != 0 && colors != 2 && colors != 4 && colors != 16 &&
  1518.          colors != 256)
  1519.        )
  1520.      goto bad_fractint_cfg;
  1521.       cfglinenums[vidtbllen] = linenum; /* for update_fractint_cfg */
  1522.       far_memcpy(vident->name,     (char far *)&tempstring[commas[0]],25);
  1523.       far_memcpy(vident->comment,(char far *)&tempstring[commas[9]],25);
  1524.       vident->name[25] = vident->comment[25] = 0;
  1525.       vident->keynum      = keynum;
  1526.       vident->videomodeax = ax;
  1527.       vident->videomodebx = bx;
  1528.       vident->videomodecx = cx;
  1529.       vident->videomodedx = dx;
  1530.       vident->dotmode      = textsafe2 * 100 + dotmode;
  1531.       vident->xdots      = xdots;
  1532.       vident->ydots      = ydots;
  1533.       vident->colors      = colors;
  1534.       ++vident;
  1535.       ++vidtbllen;
  1536.       }
  1537.    fclose(cfgfile);
  1538.    return (vidtbllen);
  1539.  
  1540. bad_fractint_cfg:
  1541.    badconfig = -1; /* bad, no message issued yet */
  1542.    if (options == 0)
  1543.       bad_fractint_cfg_msg();
  1544.  
  1545. use_resident_table:
  1546.    vidtbllen = 0;
  1547.    vident = vidtbl;
  1548.    for (i = 0; i < 40; ++i) {
  1549.       if (videotable[i].xdots) {
  1550.      far_memcpy((char far *)vident,(char far *)&videotable[i],
  1551.             sizeof(*vident));
  1552.      ++vident;
  1553.      ++vidtbllen;
  1554.      }
  1555.       }
  1556.    return (vidtbllen);
  1557.  
  1558. }
  1559.  
  1560. void bad_fractint_cfg_msg()
  1561. {
  1562. static char far badcfgmsg[]={"\
  1563. File FRACTINT.CFG is missing or invalid.\n\
  1564. See Hardware Support and Video Modes in the full documentation for help.\n\
  1565. I will continue with only the built-in video modes available."};
  1566.    stopmsg(0,badcfgmsg);
  1567.    badconfig = 1; /* bad, message issued */
  1568. }
  1569.  
  1570. void load_videotable(int options)
  1571. {
  1572.    /* Loads fractint.cfg and copies the video modes which are */
  1573.    /* assigned to function keys into videotable.          */
  1574.    int keyents,i;
  1575.    load_fractint_cfg(options); /* load fractint.cfg to extraseg */
  1576.    keyents = 0;
  1577.    far_memset((char far *)videotable,0,sizeof(*vidtbl)*MAXVIDEOTABLE);
  1578.    for (i = 0; i < vidtbllen; ++i) {
  1579.       if (vidtbl[i].keynum > 0) {
  1580.      far_memcpy((char far *)&videotable[keyents],(char far *)&vidtbl[i],
  1581.             sizeof(*vidtbl));
  1582.      if (++keyents >= MAXVIDEOTABLE)
  1583.         break;
  1584.      }
  1585.       }
  1586. }
  1587.  
  1588. int check_vidmode_key(int option,int k)
  1589. {
  1590.    int i;
  1591.    /* returns videotable entry number if the passed keystroke is a  */
  1592.    /* function key currently assigned to a video mode, -1 otherwise */
  1593.    if (k == 1400)           /* special value from select_vid_mode  */
  1594.       return(MAXVIDEOTABLE-1); /* for last entry with no key assigned */
  1595.    if (k != 0)
  1596.       if (option == 0) { /* check resident video mode table */
  1597.      for (i = 0; i < MAXVIDEOTABLE; ++i) {
  1598.         if (videotable[i].keynum == k)
  1599.            return(i);
  1600.         }
  1601.      }
  1602.       else { /* check full vidtbl */
  1603.      for (i = 0; i < vidtbllen; ++i) {
  1604.         if (vidtbl[i].keynum == k)
  1605.            return(i);
  1606.         }
  1607.      }
  1608.    return(-1);
  1609. }
  1610.  
  1611. int check_vidmode_keyname(char *kname)
  1612. {
  1613.    /* returns key number for the passed keyname, 0 if not a keyname */
  1614.    int i,keyset;
  1615.    keyset = 1058;
  1616.    if (*kname == 'S' || *kname == 's') {
  1617.       keyset = 1083;
  1618.       ++kname;
  1619.       }
  1620.    else if (*kname == 'C' || *kname == 'c') {
  1621.       keyset = 1093;
  1622.       ++kname;
  1623.       }
  1624.    else if (*kname == 'A' || *kname == 'a') {
  1625.       keyset = 1103;
  1626.       ++kname;
  1627.       }
  1628.    if (*kname != 'F' && *kname != 'f')
  1629.       return(0);
  1630.    if (*++kname < '1' || *kname > '9')
  1631.       return(0);
  1632.    i = *kname - '0';
  1633.    if (*++kname != 0 && *kname != ' ') {
  1634.       if (*kname != '0' || i != 1)
  1635.      return(0);
  1636.       i = 10;
  1637.       ++kname;
  1638.       }
  1639.    while (*kname)
  1640.       if (*(kname++) != ' ')
  1641.      return(0);
  1642.    if ((i += keyset) < 2)
  1643.       i = 0;
  1644.    return(i);
  1645. }
  1646.  
  1647. void vidmode_keyname(int k,char *buf)
  1648. {
  1649.    /* set buffer to name of passed key number */
  1650.    *buf = 0;
  1651.    if (k > 0) {
  1652.       if (k > 1103) {
  1653.      *(buf++) = 'A';
  1654.      k -= 1103;
  1655.      }
  1656.       else if (k > 1093) {
  1657.      *(buf++) = 'C';
  1658.      k -= 1093;
  1659.      }
  1660.       else if (k > 1083) {
  1661.      *(buf++) = 'S';
  1662.      k -= 1083;
  1663.      }
  1664.       else
  1665.      k -= 1058;
  1666.       sprintf(buf,"F%d",k);
  1667.       }
  1668. }
  1669.  
  1670.