






  PRODUCT  :  Borland C++                           NUMBER  :  1379
  VERSION  :  All
       OS  :  DOS
     DATE  :  May 16, 1993                             PAGE  :  1/6

    TITLE  :  Getting input while in graphics mode.




  /* This program demonstrates how to get input from the user in
     graphics mode, echoed in the current colors and font size and
     font style.

     Functions:

     newLine()     : advances the (graphic) text position to the
                     next line.

     getGrChar()   : returns a character, taking care of cursor
     getGrString() : echoes graphically the user input and stores
                     it in a buffer.

     doCursor()    : a helper function for getGrString, to handle
                     the cursor.

     main(): the use of getGrString is demonstrated for string and
             numeric input.

  NOTE:  Although it is believed that this software is fully
  functional as described in the comments, no guarantees are made,
  express or implied.
  */

  #define ON 1
  #define OFF 0

  #include <stdio.h>
  #include <stdlib.h>
  #include <conio.h>
  #include <graphics.h>

  void doCursor(int);
  void newLine();
  void getGrString(char *);

  int main(void)
  {
     char nameString[80],ageString[80];
     int age;

     /* request auto detection */
     int gdriver = DETECT, gmode, errorcode;













  PRODUCT  :  Borland C++                           NUMBER  :  1379
  VERSION  :  All
       OS  :  DOS
     DATE  :  May 16, 1993                             PAGE  :  2/6

    TITLE  :  Getting input while in graphics mode.




     /* initialize graphics and local variables */
     initgraph(&gdriver, &gmode, "");
     /* read result of initialization */
     errorcode = graphresult();
     if (errorcode != grOk)  /* an error occurred */
     {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        return(1); /* terminate with an error code */
     }

     /* use some weird colors to show that getGrString() handles
        foreground and background colors successfully. */
     setbkcolor(BLUE);
     /* left-to-right gothic font, user-sizeable */
     /* change this as you like, except getGrString assumes
        left-to-right text direction! (For this example, make sure
         GOTHIC.CHR is in the current directory) */
     settextstyle(GOTHIC_FONT,HORIZ_DIR,0);
     /* get a reasonable screen position */
     moveto(0,0);
     outtext("Your name? ");
     getGrString(nameString);
     newLine();
     /* just to demonstrate that you can get numeric input from a
        string! */
     outtext("Your age please? ");
     getGrString(ageString);
     /* note: if atoi() returns 0, the string may not have been a
        valid number! A real program should check for this. */
     age=atoi(ageString);
     newLine();
     outtext("Name: ");
     outtext(nameString);
     /* increment age to work with it as a number */
     ++age;
     /* make it a string again */
     sprintf(ageString,"%d",age);
     newLine();
     outtext("Next year, you will be ");
     outtext(ageString);
     newLine();













  PRODUCT  :  Borland C++                           NUMBER  :  1379
  VERSION  :  All
       OS  :  DOS
     DATE  :  May 16, 1993                             PAGE  :  3/6

    TITLE  :  Getting input while in graphics mode.




     outtext("Press key to exit! ");
     getch();
     closegraph();
     return 0;
  }

  /* newLine: primitive yet serviceable routine for a new text line
              in graphics mode
  */

  void newLine()
  {
      moveto(0,gety()+textheight("A"));
  }

  /* doCursor: draw or undraw the cursor, depending on whether the
     parameter is non-zero (ON) or zero (OFF)
  */

  void doCursor(int on)
  {
      int curX,oldColor;
      /* we'll use an underbar as a cursor */
      static char uBarStr[2] = { '_',0 };
      /* if the cursor goes OFF, erase it by drawing in bkground
          color */
      if (!on)
      {
          oldColor=getcolor();
          setcolor(getbkcolor());
      }
      /* save horizontal position before drawing cursor */
      curX=getx();
      outtext(uBarStr);
      moveto(curX,gety());
      /* if we changed the color to erase cursor, change it back */
      if (!on) setcolor(oldColor);
  }

  /* getGrChar gets a character and takes care of the cursor,
     but does not echo it as a graphics character or otherwise */

  int getGrChar()













  PRODUCT  :  Borland C++                           NUMBER  :  1379
  VERSION  :  All
       OS  :  DOS
     DATE  :  May 16, 1993                             PAGE  :  4/6

    TITLE  :  Getting input while in graphics mode.




  {
      char charRet;
      /* turn on the cursor */
      doCursor(ON);
      /* get single character, no echo */
      charRet=getch();
      /* turn off the cursor before we write out a new character */
      doCursor(OFF);
      return(charRet);
  }


  /* getGrString: takes a parameter of an input buffer,
                  echoes characters typed, and fills input buffer.
                  Function returns upon <ENTER> or <LINEFEED>.
                  Function responds appropriately to backspace.
                  No provision is made to guard against overflow of
                  the buffer or going over the right screen border.
  */

  void getGrString(char *inputString)
  {
     /* stringIndex is the current place in the string, so that we
         may build it as we go along getting input characters */
     int stringIndex=0;
     /* xVal will store the screen position for each char as we go
        along, so that we can erase and move the cursor
        successfully during backspacing */
     int xVal[255];
     /* inputChar: the character typed;  outString: the string
        version of that  character */
     char inputChar,outString[2];
     /* oldColor saves the previous color value, to restore after
        erasing */
     int oldColor;
     /* outString is just one char + a null-terminator */
     outString[1]=0;
     /* screen starting position for input char string */
     xVal[0]=getx();
     inputChar=getGrChar();
     /* end getting characters on ENTER or LF */
     while ( !(inputChar==13 || inputChar==10) )
     {













  PRODUCT  :  Borland C++                           NUMBER  :  1379
  VERSION  :  All
       OS  :  DOS
     DATE  :  May 16, 1993                             PAGE  :  5/6

    TITLE  :  Getting input while in graphics mode.




        /* avoid dealing with all special keys that are prefaced
           with 0 */
        if (inputChar==0) getch();
        else
        {
            if (inputChar==8) { /* backspace */
                /* save old character color */
                oldColor=getcolor();
                /* back up in the string */
                --stringIndex;
                /* don't allow backing up to before beginning of
                   string! */
                if (stringIndex<0) stringIndex=0;
                /* move to (old horz position, current vert
                   position) */
                moveto(xVal[stringIndex],gety());
                /* erasing consists of rewriting the old character
                   in the background color */
                setcolor(getbkcolor());
                outString[0]=inputString[stringIndex];
                outtext(outString);
                /* correct the current screen position since it
                   will have advanced after writing outString */
                moveto(xVal[stringIndex],gety());
                /* restore the text color we had */
                setcolor(oldColor);
            }
            else
            {
                /* put a character into the string and draw it on
                    screen */
                /* stuff the input into the string */
                inputString[stringIndex]=inputChar;
                /* draw the character on screen, as a string (since
                   that's what outtext() needs) */
                outString[0]=inputChar;
                outtext(outString);
                /* proceed to next char in the string */
                ++stringIndex;
                /* save horz position for possible backspacing
                   later */
                xVal[stringIndex]=getx();
            }













  PRODUCT  :  Borland C++                           NUMBER  :  1379
  VERSION  :  All
       OS  :  DOS
     DATE  :  May 16, 1993                             PAGE  :  6/6

    TITLE  :  Getting input while in graphics mode.




         }
         inputChar=getGrChar();
     };
     /* null-terminate input string before returning */
     inputString[stringIndex]=0;
  }

  +
  DISCLAIMER: You have the right to use this technical information
  subject to the terms of the No-Nonsense License Statement that
  you received with the Borland product to which this information
  pertains.





































