home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / FGIN131.ZIP / SOURCE.ZIP / INPUT.C < prev    next >
C/C++ Source or Header  |  1994-02-11  |  12KB  |  499 lines

  1. /****************************************************************************\
  2.  
  3. \****************************************************************************/
  4. #include "fginput.h"
  5. #include "internal.h"
  6.  
  7.  
  8. /****************************************************************************\
  9.  
  10.     INTERNAL FUNCTIONS
  11.  
  12. \****************************************************************************/
  13. static int  pollbox(const char *);
  14. static void move_right(void);
  15. static int  move_left(void);
  16. static void shift_left(void);
  17. static void shift_right(void);
  18. static void end(void);
  19. static void insert(void);
  20. static void strip_spaces(void);
  21. static void Outblanks(int);
  22. static void setcursor(CURSORTYPE);
  23.  
  24. static void showchar(int);
  25. static void draw_cursor(void);
  26.  
  27. extern void (*_print)(char*,int);
  28.  
  29.  
  30. /****************************************************************************\
  31.  
  32.     VARIABLE DECLARATIONS
  33.  
  34. \****************************************************************************/
  35. static char _TempString[MAXCHARS+1];    //Holds currently editing string. (95)max chars
  36. static char _Blank[2]=" ";
  37. static int  _StringX;
  38. static int  _FieldLen;
  39. static int  _Left;
  40. static int  _Right;
  41. static int  _Top;
  42. static int  _Bottom;
  43. static int  _InCursor=INSCURSOR;
  44.  
  45. extern int  _Defined;
  46. extern int  _VMode;
  47. extern int  _VType;
  48. extern int  _VLines;
  49.  
  50.  
  51. /****************************************************************************\
  52.  
  53.  
  54. \****************************************************************************/
  55. /****************************************************************************\
  56.  
  57.     int input_line(char *line, const char *mask)
  58.  
  59. \****************************************************************************/
  60. int input_line(char *line, const char *mask)
  61. {
  62.  
  63.     int test;
  64.  
  65.     if ((test=setup(line,mask))!=0) return(test);
  66.  
  67.     /* Display old value highlighted */
  68.     if (strlen(line)>0) {
  69.  
  70.         if (_VType==GRAPHICS) {
  71.             fg_setcolor(BackHighlightcolor);
  72.             fg_rect(_Left+3,_Left+1+strlen(line)*8,_Top+3,_Bottom-3);
  73.             fg_setcolor(TextHighlightcolor);
  74.             fg_move(_Left+3,_Bottom-3);
  75.         }
  76.         else {
  77.             fg_setattr(TextHighlightcolor,BackHighlightcolor,0);
  78.             fg_locate(_Top,_Left);
  79.         }
  80.         _print(_TempString,strlen(_TempString));
  81.  
  82.     }
  83.     else
  84.         showstring(CURSOR);
  85.  
  86.  
  87.     /* Poll box */
  88.     test=pollbox(mask);
  89.  
  90.     /* Copy new string to *line if CR pressed */
  91.     if (test!=IN_ESCAPE) {
  92.         strip_spaces();
  93.         strcpy(line,_TempString);
  94.     }
  95.     else
  96.         strcpy(_TempString,line);
  97.  
  98.     _StringX=0;
  99.     showstring(NOCURSOR);
  100.  
  101.     return(test);
  102.  
  103. }
  104.  
  105. /****************************************************************************\
  106.  
  107.     int setup(char *line, const char *mask)
  108.  
  109.             Sets up global variables and draws the input box
  110.  
  111. \****************************************************************************/
  112. int setup(char *line, const char *mask)
  113. {
  114.  
  115.     /* Check parameters */
  116.     if (!_Defined) return(IN_NOT_DEFINED);
  117.     if (_VMode==NOTSUPPORTED) return(IN_NOT_SUPPORTED);
  118.     _FieldLen=strlen(mask);
  119.     if (_FieldLen>MAXCHARS) return(IN_MASK_TOO_LONG);
  120.     if (strlen(line)>MAXCHARS) return(IN_BUFFER_TOO_LONG);
  121.  
  122.     _StringX=0;
  123.  
  124.     /* Set Text Size */
  125.     if (TextSize!=8&&TextSize!=14&&TextSize!=16) TextSize=8;
  126.     if (_VMode==COLOR16b&&TextSize==8) TextSize=14;
  127.     fg_fontsize(TextSize);
  128.  
  129.     /* Set coordinates */
  130.     if (_VType==GRAPHICS) {
  131.         _Left  =fg_getxpos();
  132.         _Right =_Left+4+_FieldLen*8;
  133.         _Bottom=fg_getypos();
  134.         _Top   =_Bottom-5-TextSize;
  135.     }
  136.     else {
  137.         fg_where(&_Top,&_Left);
  138.         _Right =_Left+_FieldLen;
  139.         _Bottom=_Top;
  140.     }
  141.  
  142.     if (_Right>fg_getmaxx()) return(IN_BOX_TOO_BIG);
  143.  
  144.     memset(_TempString,NULL,MAXCHARS+1);    //Null out _TempString;
  145.     strcpy(_TempString,line);                    //Copy current string to _TempString
  146.  
  147.     /* Draw input box */
  148.     if (_VType==GRAPHICS) {
  149.         fg_setcolor(Backcolor);
  150.         fg_rect(_Left,_Right,_Top,_Bottom);
  151.         fg_setcolor(BorderTcolor);
  152.         fg_move(_Left,_Top);
  153.         fg_draw(_Right,_Top);
  154.         fg_setcolor(BorderRcolor);
  155.         fg_draw(_Right,_Bottom);
  156.         fg_setcolor(BorderBcolor);
  157.         fg_draw(_Left,_Bottom);
  158.         fg_setcolor(BorderLcolor);
  159.         fg_draw(_Left,_Top);
  160.     }
  161.     else {
  162.         fg_setattr(0,Backcolor,0);
  163.         fg_locate(_Top,_Left);
  164.         Outblanks(_FieldLen);
  165.     }
  166.  
  167.     return(IN_SUCCESS);
  168.  
  169. }
  170.  
  171. /****************************************************************************\
  172.  
  173.     void showstring(int cursor)
  174.  
  175.             Displays string from _StringX offset to EOS
  176.  
  177. \****************************************************************************/
  178. void showstring(int cursor)
  179. {
  180.  
  181.     int   offset=_Left+3+_StringX*8;
  182.     char *ptr=_TempString+_StringX;
  183.  
  184.     if (_VType==GRAPHICS) {
  185.         fg_setcolor(Backcolor);
  186.         fg_rect(offset,_Right-1,_Top+1,_Bottom-1);
  187.  
  188.         if (cursor) draw_cursor();
  189.  
  190.         fg_setcolor(TextNormalcolor);
  191.         fg_move(offset,_Bottom-3);
  192.         _print(ptr,strlen(ptr));
  193.  
  194.         if (cursor) {
  195.             fg_setcolor(TextHighlightcolor);
  196.             fg_move(offset,_Bottom-3);
  197.             _print(ptr,1);
  198.         }
  199.     }
  200.     else {
  201.  
  202.         fg_setattr(TextNormalcolor,Backcolor,0);
  203.         fg_locate(_Top,_Left+_StringX);
  204.         Outblanks(_FieldLen-_StringX);
  205.  
  206.         fg_setcolor(TextNormalcolor);
  207.         fg_locate(_Top,_Left+_StringX);
  208.         _print(ptr,strlen(ptr));
  209.     }
  210.  
  211. }
  212.  
  213. /****************************************************************************\
  214.  
  215.     static void showchar(int highlight)
  216.  
  217.             Displays _TempString[_StringX] either highlighted or not.
  218.  
  219. \****************************************************************************/
  220. static void showchar(int highlight)
  221. {
  222.  
  223.     int fore;
  224.     int offset;
  225.  
  226.     if (highlight) { fore=TextHighlightcolor; }
  227.     else           { fore=TextNormalcolor;    }
  228.  
  229.     if (_VType==GRAPHICS) {
  230.         offset=_Left+3+_StringX*8;
  231.         fg_setcolor(Backcolor);
  232.         fg_rect(offset,offset+7,_Top+3,_Bottom-2);
  233.         if (highlight) draw_cursor();
  234.         fg_setcolor(fore);
  235.         fg_move(offset,_Bottom-3);
  236.     }
  237.     else {
  238.         fg_setattr(fore,Backcolor,0);
  239.         fg_locate(_Top,_Left+_StringX);
  240.     }
  241.     _print(_TempString+_StringX,1);
  242.  
  243. }
  244.  
  245. /****************************************************************************\
  246.  
  247.     static void draw_cursor(void)
  248.  
  249.             Draws the graphics mode cursor.
  250.  
  251. \****************************************************************************/
  252. static void draw_cursor(void)
  253. {
  254.  
  255.     fg_setcolor(Cursorcolor);
  256.     if (_InCursor==OVRCURSOR) fg_rect(_Left+3+_StringX*8,_Left+_StringX*8+8,_Top+3,_Bottom-2);
  257.     else fg_rect(_Left+3+_StringX*8,_Left+_StringX*8+8, _Bottom-3,_Bottom-2);
  258.  
  259. }
  260.  
  261. /****************************************************************************\
  262.  
  263.     static int pollbox(const char *mask)
  264.  
  265. \****************************************************************************/
  266. static int pollbox(const char *mask)
  267. {
  268.  
  269.     int ch;
  270.     _StringX=0;
  271.  
  272.     /* Read initial key press */
  273.     fg_locate(_Top,_Left+_StringX);
  274.     ch=wait_key_masked(mask[_StringX]);
  275.  
  276.     if (ch>31&&ch<256)    //Was key a non-editing key?
  277.         memset(_TempString,NULL,MAXCHARS+1);
  278.     showstring(CURSOR);
  279.  
  280.     do {
  281.  
  282.         switch(ch) {
  283.             case ESC:        return(IN_ESCAPE);
  284.             case CR:        return(IN_SUCCESS);
  285.             case BS:        if (move_left()) shift_left();                break;
  286.             case RARROW:    move_right();                                        break;
  287.             case LARROW:    move_left();                                        break;
  288.             case UARROW:    if (Polled_Mode) return(IN_UARROW);            break;
  289.             case DARROW:    if (Polled_Mode) return(IN_DARROW);            break;
  290.             case TAB:        if (Polled_Mode) return(IN_TAB);                break;
  291.             case SHIFT_TAB:if (Polled_Mode) return(IN_SHIFT_TAB);        break;
  292.             case DEL:        shift_left();                                        break;
  293.             case HOME:        showchar(0); _StringX=0; showchar(1);        break;
  294.             case END:        end();                                                break;
  295.             case INS:        insert();                                            break;
  296.             default:
  297.                 if (_InCursor==INSCURSOR) shift_right();
  298.                 _TempString[_StringX]=ch;
  299.                 move_right();
  300.                 break;
  301.         }
  302.  
  303.         fg_locate(_Top,_Left+_StringX);
  304.         ch=wait_key_masked(mask[_StringX]);
  305.  
  306.     }while(1);
  307.  
  308. }
  309.  
  310. /****************************************************************************\
  311.  
  312.     static void move_right(void)
  313.  
  314.             Moves the cursor 1 position to the right if possible.
  315.  
  316. \****************************************************************************/
  317. static void move_right(void)
  318. {
  319.  
  320.     if (_TempString[_StringX]==NULL)
  321.         _TempString[_StringX]=' ';
  322.  
  323.     if (_StringX+1>=_FieldLen) return;
  324.     showchar(0);    //Unhighlight last position
  325.     _StringX++;
  326.     showchar(1);    //Highlight new position
  327.  
  328. }
  329.  
  330. /****************************************************************************\
  331.  
  332.     static int move_left(void)
  333.  
  334.             Moves the cursor 1 position to the left if possible.
  335.  
  336. \****************************************************************************/
  337. static int move_left(void)
  338. {
  339.  
  340.     if (_StringX-1<0) return (0); //Couldn't move left
  341.  
  342.     showchar(0);    //Unhighlight last position
  343.     _StringX--;
  344.     showchar(1);   //Highlight new position
  345.     return(1);        //Could move left
  346.  
  347. }
  348.  
  349. /****************************************************************************\
  350.  
  351.     static void shift_left(void)
  352.  
  353.             Shifts the string from _StringX to the left 1 position
  354.  
  355. \****************************************************************************/
  356. static void shift_left(void)
  357. {
  358.  
  359.     register int x=_StringX;
  360.     while(_TempString[x]!=NULL) {
  361.         _TempString[x]=_TempString[x+1];
  362.         x++;
  363.     }
  364.  
  365.     memset(_TempString+x,NULL,_FieldLen-x);
  366.     showstring(CURSOR);
  367.  
  368. }
  369.  
  370. /****************************************************************************\
  371.  
  372.     static void shift_right(void)
  373.  
  374.             Shifts the string from _StringX to the right 1 position
  375.  
  376. \****************************************************************************/
  377. static void shift_right(void)
  378. {
  379.  
  380.     register int x;
  381.     for(x=_FieldLen-1; x>_StringX; x--)
  382.         _TempString[x]=_TempString[x-1];
  383.     showstring(CURSOR);
  384.  
  385. }
  386.  
  387. /****************************************************************************\
  388.  
  389.     static void end(void)
  390.  
  391.             Moves the cursor to the end of the string
  392.  
  393. \****************************************************************************/
  394. static void end(void)
  395. {
  396.  
  397.     strip_spaces();                                //Remove trailing spaces
  398.     showchar(0);                                    //Unhighlight old character
  399.     _StringX=strlen(_TempString);                //Move cursor
  400.     if (_StringX==_FieldLen)                    //Are we at the end of the box?
  401.         _StringX--;                                    //then move back one
  402.     showchar(1);                                    //Highlight new character
  403.  
  404. }
  405.  
  406. /****************************************************************************\
  407.  
  408.     static void insert(void)
  409.  
  410.             Toggles between inserting and overstriking
  411.  
  412. \****************************************************************************/
  413. static void insert(void)
  414. {
  415.  
  416.     if (_InCursor==INSCURSOR) {
  417.         _InCursor=OVRCURSOR;
  418.         if (_VType==TEXT) setcursor(CURSOR_SOLID);
  419.         else showchar(1);
  420.     }
  421.     else {
  422.         _InCursor=INSCURSOR;
  423.         if (_VType==TEXT) setcursor(CURSOR_NORMAL);
  424.         else showchar(1);
  425.     }
  426.  
  427. }
  428.  
  429. /****************************************************************************\
  430.  
  431.     static void strip_spaces(void)
  432.  
  433.             Removes trailing spaces from _TempString[]
  434.  
  435. \****************************************************************************/
  436. static void strip_spaces(void)
  437. {
  438.  
  439.     /* Strip trailing spaces */
  440.     int space=-1;
  441.     register int x=0;
  442.  
  443.     while (_TempString[x]!=NULL) {
  444.         if (_TempString[x]==' '&&space<0)                            space=x;
  445.         else if (_TempString[x]!=NULL&&_TempString[x]!=' ')    space=-1;
  446.         x++;
  447.     }
  448.     if (space>=0) _TempString[space]=NULL;
  449.  
  450. }
  451.  
  452. /****************************************************************************\
  453.  
  454.     static void Outblanks(int number)
  455.  
  456.             Outputs number of spaces to screen.
  457.  
  458. \****************************************************************************/
  459. static void Outblanks(int number)
  460. {
  461.     while (number-->0) _print(_Blank,1);
  462. }
  463.  
  464. /****************************************************************************\
  465.  
  466.     static void setcursor(CURSORTYPE type)
  467.  
  468.             Changes the text mode cursor form.
  469.  
  470. \****************************************************************************/
  471. static void setcursor(CURSORTYPE type)
  472. {
  473.  
  474.     int s,e;
  475.  
  476.     switch (type) {
  477.  
  478.         case CURSOR_NONE:
  479.             s=0x20,e=0;
  480.             break;
  481.         case CURSOR_SOLID:
  482.             if (_VLines==43)     s=0,e=8;
  483.             else              s=0,e=_VType==TEXT ? 12:7;
  484.             break;
  485.         default:
  486.             if (_VType==TEXT)    s=11,e=12;
  487.             else              s=6,e=7;
  488.             break;
  489.  
  490.     }//switch cursortype
  491.  
  492.     _CH=s;
  493.     _CL=e;
  494.     _AH=1;
  495.     asm INT 0x10;
  496.  
  497. }
  498.  
  499.