C Programming Tutorial

 
 
 
 

Image

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

/* ..ASCII and Scan Codes of Keys.. */

# define ESC 27
# define ENTER 13
# define RIGHT 77
# define LEFT 75
# define HOME 71
# define END 79
# define INSERT 82
# define DEL 83

/*-------------------------------------
   ..Checks the status of Insert Key..
   ..Returns..
   ..1 if ON..
   ..0 if OFF..
--------------------------------------*/
int ins_state ( void )
{
  unsigned char far *stat =(char far*)0x417 ;

  char status ;

  status = *stat ;

  if ( ( status & 128 ) == 128 )
     return 1;
  else
     return 0;
}

/*----------------------------------
    ..Changes the shape of cursor..
------------------------------------*/
void cursor_shape ( int y , int x )
{
    union REGS i, o ;
    i.h.ah = 1 ;
    i.h.ch = y ;
    i.h.cl = x ;
    int86 ( 16, &i, &o ) ;
}

/*-----------------------------------------------
    ..Restores the screen to the orginal state..
-------------------------------------------------*/
void restore_ui(void)
{
  int i;
  char far *vidmem=(char far*)0xB8000000;

  window(1,1,80,25);
  clrscr();

  for (i=1;i<4000;i+=2)
      *(vidmem+i)=7;

  _setcursortype(_NORMALCURSOR);
}

/*-------------------------------------------
    ..Function for drawing horizontal line..
---------------------------------------------*/
void horiz_draw(int start_x,int end_x,int y,char symbol)
{
 int i,len;
 len=end_x-start_x;

 for (i=1;i<=len+1;++i,++start_x)
 {
   gotoxy(start_x,y);
   cprintf(\"%c\",symbol);
 }
}

/*
-----------------------------------
  ..The important fuction..
  ..Just include in your programs..
-----------------------------------

+------------------------- Available Keys -----------------------------------+
|    ~ Right Arrow --> For moving the cursor to the right                 |
|    ~ Left  Arrow --> For moving the cursor to the left                  |
|    ~ DEL         --> For deleting characters                            |
|    ~ Backspace   --> For deleting characters                            |
|    ~ Insert      --> For inserting charcters                            |
|    ~ Home           --> For going to the beginning of the text             |
|    ~ End         --> For going to the end of the text                   |
|    ~ ESC           --> For clearing the textbox                 |
+----------------------------------------------------------------------------+

*/
void txtbox ( int length )
{
   char str[80];  /*
              ..Array into which characters are being stored,
               the length of the string is limited to 80..

             ..Just change the length to meet your needs..

             ..You can even use dynamic memory allocation, if you
               are poor in memory..
          */

   char key;

   int startx,    /* ..For storing the starting position.. */
       x,
       y,
       i,
       j,
       currentpos,
       lastpos = 0,
       len;      /* ..Length of the string.. */

   startx = wherex();
   y = wherey();

/*--------------------------------------------------------------------
      ..I have used the colors which I like, you are free to use your
      own colors..
----------------------------------------------------------------------*/

   textcolor ( WHITE );
   textbackground ( BLUE );

   /* ..Just for drawing the cute blue box.. */
   horiz_draw ( startx , startx + length , y , \' \');

   gotoxy ( startx , y );
   str[0] = \'\\0\';

   while ( 1 )
   {
      if (  ins_state() == 1 ) /* ..If the Insert is ON change the cursor.. */
      cursor_shape ( 4 , 8 );
      else
      _setcursortype( _NORMALCURSOR );

      key = getch();
      x = wherex();
      len = strlen ( str );

      if ( key == ENTER )
     break;

      if ( key == ESC ) /* ..For clearing the characters in the textbox.. */
      {
     textbackground ( BLUE );
     horiz_draw ( startx , startx + length , y , \' \');
     gotoxy ( startx , y );
     str[0] = \'\\0\';
     lastpos = 0;
     continue;
      }

      /* ..For storing the typed characters into the array.. */
      if ( key > 31 && key < 127 && x < ( startx + length ) )
      {
     if ( len == length && ins_state() == 1 )
     {
         printf ( \"\\a\" );
         continue;
     }

     if (  ins_state() == 1 )  /* ..Insert.. */
     {
        currentpos = x - startx;
        str[len + 1] = \'\\0\';

        for ( i = len ; i >= currentpos ; i-- )
        str[i] = str[i - 1];

        str[currentpos] = key;
        len = strlen ( str );
        gotoxy ( startx , y );
        horiz_draw ( startx , startx + len , y , \' \');
        gotoxy ( startx , y );

        for ( i = 0 ; i < len ; i++ )
           putch ( str[i] );

        gotoxy ( x , y );
     }
     else
     {
       i = x - startx;
       putch ( key );

       str[i] = key;

       if ( i >= lastpos )
       {
          lastpos = i;
          str[++i] = \'\\0\';
       }
     }
      }

      if ( key == 8 && x <= startx + len && x != startx ) /* ..Backspace.. */
      {
     x = wherex();
     currentpos = x - startx;

     for ( i = currentpos - 1 ; str[i] != \'\\0\' ; i++ )
         str[i] = str[i + 1];

     gotoxy ( startx , y );
     horiz_draw ( startx , startx + len , y , \' \');
     gotoxy ( startx , y );

     for ( i = 0 ; i < len ; i++ )
         putch ( str[i] );

     gotoxy ( --x , y );
     continue;
      }

      if ( key == 0 )   /* ..If the keys are Scan Keys.. */
      {
     key = getch();

     if ( key == END )
     {
         gotoxy ( startx + len , y );
         continue;
     }

     if ( key == HOME )
     {
         gotoxy ( startx , y );
         continue;
     }

     if ( key == LEFT && x > startx )
     {
         gotoxy ( --x , y );
         continue;
     }

     if ( key == RIGHT && x < startx + len )
     {
         gotoxy ( ++x , y );
         continue;
     }

     x = wherex();

     if ( key == DEL &&  x != startx + len )
     {
        x = wherex();
        currentpos = x - startx;

        for ( i = currentpos ; str[i] != \'\\0\' ; i++ )
           str[i] = str[i + 1];

        gotoxy ( startx , y );
        horiz_draw ( startx , startx + len , y , \' \');
        gotoxy ( startx , y );

        for ( i = 0 ; i < len ; i++ )
           putch ( str[i] );

        gotoxy ( x , y );
        continue;
     }
      }
   }  /* ..End of while loop.. */

   i = strlen ( str );

   /* ..Truncates the useless spaces found at the end of string.. */
   for ( j = i - 1 ; j >= 0 ; --j )
   {
      if ( str[j] != \' \' )
      {
     str[++j] = \'\\0\';
     break;
      }
   }

   printf (\"\\n\\nThe string you entered : %s\",str);
   getch();
}

/*--------------------------------------------------
   ..Written just to demonstrate textbox function..
   ..Really shitting..
----------------------------------------------------*/
void main()
{
  clrscr();
  gotoxy ( 32 , 1 );
  printf ( \"Text Box Demo\" );
  printf ( \"\\n\\nEnter any text :  \" );
  txtbox ( 60 );   /* ..Invoke the function.. */
  fflush ( stdin );
  restore_ui();
}

 
 
Didn't find what you were looking for? Find more on Program to display textbox works like the normal textbox found in Windows