Textinput routine I wrote in turbo C for Dos



#include < stdio.h >
#include < conio.h > 
#include < dos.h >
#include < string.h >



void double_box(int ,int ,int ,int ,int );
void single_box(int ,int ,int ,int ,int );
char *text_input_box(int ,int ,int ,int,char *,char *);
int numeric_input_box(int ,int ,int ,int ,char *);
/*
#define DEBUG
*/

#ifdef DEBUG
main()
{
  char *strn;
  window (1,1,80,25);
  textbackground(BLUE);
  clrscr();
  strn = text_input_box(12,5,34,22,"Enter Message");
  printf("%s",strn);
  getchar();
  return 0;
}

#endif


char *text_input_box(int xpos,int ypos,int max_strlen,int boxlen,char *titlestr,char *textstr)
{

  int prompt_backcolor,prompt_forecolor;
  int textstr_backcolor,textstr_forecolor;
  int textbox_backcolor,textbox_forecolor;
  int textbox_titlecolor;
  int shadow_color;
  int char_count;
  /* char textstr[100]; */
  char inchar;
  struct text_info ti;
  char scrn_buffer[4096];

   #ifdef DEBUG
    window(5,5,70,20);
    textbackground(GREEN);
    textcolor(RED);
   #endif


   gettextinfo(&ti);                /* save previous text settings */
   gettext(1,1,80,25,scrn_buffer);  /* save screen text to buffer */


   #ifdef DEBUG
   clrscr();
   cprintf("window left      %2d\r\n",ti.winleft);
   cprintf("window top       %2d\r\n",ti.wintop);
   cprintf("window right     %2d\r\n",ti.winright);
   cprintf("window bottom    %2d\r\n",ti.winbottom);
   cprintf("attribute        %2d\r\n",ti.attribute);
   cprintf("normal attribute %2d\r\n",ti.normattr);
   cprintf("current mode     %2d\r\n",ti.currmode);
   cprintf("screen height    %2d\r\n",ti.screenheight);
   cprintf("screen width     %2d\r\n",ti.screenwidth);
   cprintf("current x        %2d\r\n",ti.curx);
   cprintf("current y        %2d\r\n",ti.cury);

   getchar();

   textbackground(BLUE);
   clrscr();
  #endif

  shadow_color = DARKGRAY;
  textbox_backcolor = LIGHTGRAY;
  textbox_forecolor = BLUE;
  textbox_titlecolor = BLUE;
  prompt_backcolor = RED;
  prompt_forecolor = WHITE;
  textstr_backcolor = DARKGRAY;
  textstr_forecolor = GREEN;


		 /* setup shadow window  */
  window(xpos +2,ypos +1,xpos + boxlen +1,ypos +3);
  textbackground(shadow_color);
  clrscr();


  textbackground(textbox_backcolor);
		 /* setup window for box */
  window(xpos,ypos,xpos + boxlen,ypos+2);
  single_box(1,1,boxlen,3,textbox_forecolor);
  textcolor(textbox_titlecolor);
  gotoxy(3,1);
  cprintf("%s%s","[þ]Ä ", titlestr);

  textbackground(prompt_backcolor);        /* setup window for prompt */
  textcolor(prompt_forecolor);
  window(xpos+1,ypos+1,xpos+9,ypos+1);
  cprintf(" Enter: ");

	      /* setup window for textinput */
  window(xpos+10,ypos+1,xpos+boxlen -2,ypos+1);
  textbackground(textstr_backcolor);
  textcolor(textstr_forecolor);
  highvideo();

  clreol();
  char_count = 0;

  while((inchar = getche()))
  {

    if((inchar == 13) || (char_count == max_strlen))
    {
      textstr[char_count] = '\0';
      sound(1000);
      delay(200);
      nosound();
      break;
    }

    textstr[char_count] = inchar;
    char_count++ ;
  }



  /* reset text window and colours back to oldsettings */


  window(ti.winleft,ti.wintop,ti.winright,ti.winbottom);
  textattr(ti.attribute);
  puttext(1,1,80,25,scrn_buffer);

  #ifdef DEBUG
    clrscr();
    cprintf(" PREVIOUS WINDOW RESTORED ");
    getchar();
  #endif

  return textstr;

}



int numeric_input_box(int xpos,int ypos,int max_number,int boxlen,char *titlestr)
{

  int prompt_backcolor,prompt_forecolor;
  int textstr_backcolor,textstr_forecolor;
  int textbox_backcolor,textbox_forecolor;
  int textbox_titlecolor;
  char number_str[3];
  int message_number;
  int shadow_color;
  int char_count;

  char inchar;
  struct text_info ti;
  char scrn_buffer[4096];

   #ifdef DEBUG
    window(5,5,70,20);
    textbackground(GREEN);
    textcolor(RED);
   #endif


   gettextinfo(&ti);                /* save previous text settings */
   gettext(1,1,80,25,scrn_buffer);  /* save screen text to buffer */


   #ifdef DEBUG
   clrscr();
   cprintf("window left      %2d\r\n",ti.winleft);
   cprintf("window top       %2d\r\n",ti.wintop);
   cprintf("window right     %2d\r\n",ti.winright);
   cprintf("window bottom    %2d\r\n",ti.winbottom);
   cprintf("attribute        %2d\r\n",ti.attribute);
   cprintf("normal attribute %2d\r\n",ti.normattr);
   cprintf("current mode     %2d\r\n",ti.currmode);
   cprintf("screen height    %2d\r\n",ti.screenheight);
   cprintf("screen width     %2d\r\n",ti.screenwidth);
   cprintf("current x        %2d\r\n",ti.curx);
   cprintf("current y        %2d\r\n",ti.cury);

   getchar();

   textbackground(BLUE);
   clrscr();
  #endif

  shadow_color = DARKGRAY;
  textbox_backcolor = LIGHTGRAY;
  textbox_forecolor = BLUE;
  textbox_titlecolor = BLUE;
  prompt_backcolor = RED;
  prompt_forecolor = WHITE;
  textstr_backcolor = DARKGRAY;
  textstr_forecolor = GREEN;


		 /* setup shadow window  */
  window(xpos +2,ypos +1,xpos + boxlen +1,ypos +3);
  textbackground(shadow_color);
  clrscr();


  textbackground(textbox_backcolor);
		 /* setup window for box */
  window(xpos,ypos,xpos + boxlen,ypos+2);
  single_box(1,1,boxlen,3,textbox_forecolor);
  textcolor(textbox_titlecolor);
  gotoxy(3,1);
  cprintf("%s%s","[þ]Ä ", titlestr);

  textbackground(prompt_backcolor);        /* setup window for prompt */
  textcolor(prompt_forecolor);
  window(xpos+1,ypos+1,xpos+9,ypos+1);
  cprintf(" Enter: ");

	      /* setup window for textinput */
  window(xpos+10,ypos+1,xpos+boxlen -2,ypos+1);
  textbackground(textstr_backcolor);
  textcolor(textstr_forecolor);
  highvideo();

  clreol();
  char_count = 0;

  while((inchar = getche()))
  {

    if((inchar == 13) || (char_count == 5))
    {
      number_str[char_count] = '\0';
      sound(1000);
      delay(200);
      nosound();
      break;
    }

    if(!isdigit(inchar))
    {
      sound(1000);
      delay(200);
      nosound();
      cprintf(" invalid digit");
      delay(250);
    }

    if(isdigit(inchar))
    {
      number_str[char_count] = inchar;
      char_count++ ;
    }
    else
    {
      clrscr();
      char_count = 0;
    }


  }

  message_number = atoi(number_str);


  /* reset text window and colours back to oldsettings */


  window(ti.winleft,ti.wintop,ti.winright,ti.winbottom);
  textattr(ti.attribute);
  puttext(1,1,80,25,scrn_buffer);

  #ifdef DEBUG
    clrscr();
    cprintf(" PREVIOUS WINDOW RESTORED ");
    getchar();
  #endif

  if (message_number <  max_number)
    return message_number;

}






void double_box(int x1,int y1,int x2,int y2,int color)
{
	int n;
	int boxlen = x2-x1;
	int boxht = y2-y1;
	textcolor(color);

/*     DRAW CORNERS   É #201         #187 »
 *       FIRST
 *
 *                    È #200         #188 ¼
 */

	gotoxy(x1,y1);
	cprintf("%c",'É');
	gotoxy(x1+boxlen,y1);
	cprintf("%c",'»');
	gotoxy(x1,y2);
	cprintf("%c",'È');
	gotoxy(x1+boxlen,y2);
	cprintf("%c",'¼');

/*
 *  NOW DRAW CONNECTING LINES   #205 Í   #186  º
 *
 *
*/


     for (n=0;n<=boxlen-2;n++)
     {
       gotoxy(x1+1+n,y1);
       cprintf("%c",'Í');
       gotoxy(x1+1+n,y2);
       cprintf("%c",'Í');
     }

    for (n=0;n<=boxht-2;n++)
    {
      gotoxy(x1,y1+1+n);
      cprintf("%c",'º');
      gotoxy(x2,y1+1+n);
      cprintf("%c",'º');
    }
}



void single_box(int x1,int y1,int x2,int y2,int color)
{
	int n;
	int boxlen = x2-x1;
	int boxht = y2-y1;
	textcolor(color);

/*     DRAW CORNERS   Ú #218         #191 ¿
 *       FIRST
 *
 *                    À #192         #217 Ù
 */

	gotoxy(x1,y1);
	cprintf("%c",'Ú');
	gotoxy(x1+boxlen,y1);
	cprintf("%c",'¿');
	gotoxy(x1,y2);
	cprintf("%c",'À');
	gotoxy(x1+boxlen,y2);
	cprintf("%c",'Ù');

/*
 *  NOW DRAW CONNECTING LINES   #196 Ä   #179 ³
 *
 *
*/


     for (n=0;n<=boxlen-2;n++)
     {
       gotoxy(x1+1+n,y1);
       cprintf("%c",'Ä');
       gotoxy(x1+1+n,y2);
       cprintf("%c",'Ä');
     }

    for (n=0;n<=boxht-2;n++)
    {
      gotoxy(x1,y1+1+n);
      cprintf("%c",'³');
      gotoxy(x2,y1+1+n);
      cprintf("%c",'³');
    }
}


This code is part of my modbus message system written in turbo C email me if you found it useful or you have improved it .