Tony Servies is a programmer/analyst with World Computer Systems in Oak Ridge, Tennessee. Presently he is working on a project to develop computer-based training programs for the U.S. Navy. His computer interests include utilities and C programming. You may contact him at Route 1, Box 143, Greenback, TN 37742.
Want to spice up your user interface with flashy windows with only a minimal amount of coding and time? With a few lines of code and Borland's Turbo C, it's possible.
Turbo C Window Interface
Two functions can be used to create text windows in Turbo C: gettext() and puttext(). Each function either gets a screen image or puts an image to the screen, respectively. The programmer supplies only the window coordinates and a character string pointer (or character array, if you will); the function does the rest. These remarkable routines do some rudimentary screen I/O quickly and cleanly.One drawback though is the inherent lag between the time you write to the window area and the moment the text is displayed. The user 'sees' any text writing that you perform. Most applications today require a window flashed on the screen intact, such as in a pull-down menu.
The code in Listing 1 allows writing to a window before it is displayed on the screen. Then when the window is flashed on the screen, it is complete.
You call puttext_write with the x,y coordinates, window size, the character string to display, the attribute for the string, and the pointer to the window buffer. The x,y coordinates start from the upper left corner with the location 0,0. The size of the window is given with the number of columns (width) and the number of rows (heighth). The string to display is simply a character string stored in standard C format; a '\0' character terminates the string. The buffer is a pointer to an area of characters that denotes the window area. The string attribute is the usual color attributes found in almost every reference manual on PCs.
How It Works
puttext_write first checks that you are not positioning the data beyond the physical bounds of the window. Of course, this routine will wrap any text past the end of a line onto the following line (unless it is the last line in the window).The routine then gets the pointer address for the last character and attribute pair in the window area, called maxbuffer in the subroutine. The offset for the proper x,y location is added to the buffer so that it points to the correct character.
While the buffer location is less than the maxbuffer pointer and the character in the string is not the end of string terminator ('\0'), the while loop updates the character and attribute of the buffer. The loop terminates only when the buffer overflows (buffer >= maxbuffer) or at the end of string (*string == '\0'). Now, just put the window on the screen and you're ready to go. I've included a quick and dirty sample program illustrating the flashy windows routine (Listing 2) .
Note how easy it is to create a window. Just use a character array of XSIZE*YSIZE*2 bytes. (You multiply the area by two because each displayed character is followed by a byte of attribute information (color, blink, etc.).)
The program then clears the window and sets all of the attributes. In this example I set all attributes to magenta characters on cyan background. Then the routine that does the actual call to puttext_write() loops through ten times. After the page is full, I put the window on the screen with the puttext() command and wait for half a second. The routine loops through nine more times until it completes the for loop. I then restore the original screen with the puttext() call for the original screen area (oldbuffer).
This routine should enable you to enhance your pull- down, pop-up, and user-entry screens. Feel free to modify the code to account for border areas, highlighted text, etc.