Thursday, November 29, 2007

Text Screen resizing

In true Turbo C, the resolution of the text console is completely under the control of the programmer. The textmode function sets the resolution, in terms of the number of text rows and columns, and sets the graphics modes appropriately. If a Turbo C program is run under Microsoft Windows, the same situation applies, in essence, because even though Windows renders the text console by means of graphical operations, the number of text rows and columns is fixed, and cannot be changed by the user.

The situation is different in ncurses -- at least, if using the X-Window system -- because the size of the text console is entirely under the control of the user. The user can resize the text console at will, and the program has little effective control over the resizing.

This presents a difficulty, because Turbo C programs have naturally been written under the suppositions that the text console size has been set by the program itself, and that the text console is not going to be unexpectedly resized during program execution. Yet, these suppositions are both rather dodgy with code that has been ported using the TurboC library.

There are several possible approaches to the problem:

  1. For TurboC versions 04/18/02 and later, the screen size is now correct if the ported program is run under xterm. TurboC physically resizes the xterm console correctly upon starting the program. While it cannot prevent the user from manually resizing the window later, it will correct any manual resizing by automatically restoring the window to its correct size. (Sadly, it is possible for a persistent user to corrupt the window by trying hard enough to resize it, but the behavior is usually pretty acceptable.) Or,
  2. If for some reason it's impractical to use xterm, or if the window manager being used resists TurboC's attempts to resize xterm , then you may need to use some method outside of TurboC (such as instructions to the user) to prevent resizing of the window; or
  3. Set the BypassResizeXterm variable to 1, and recode those portions of your program that depend on knowledge of the screen size (such as those using gotoxy and windowgotoxy(1,25) , but use gotoxy(1,LINES) instead. functions) so that they are more flexible. For example, don't use things like
In implementing approach #3, several tools are at your disposal. Firstly, the variables LINES and COLS are available for your use. These variables always contain the number of text rows and text columns, respectively, in the text console. Secondly, the TurboC library can detect (in versions 20020331 and later) if the user has resized the console. When it detects that such resizing has occurred, it calls a function called ConioResizeCallback. You can optionally provide this function yourself (though the TurboC library provides a default version of the function which does nothing), and can use this function in a variety of ways to take care of the screen resizing. Perhaps the most straightforward way to do so is to simply allow ConioResizeCallback to completely redraw the screen.

In the latter case, by the way, if the user manually resizes the text console, then your current window (as determined by the windowor textmode function) will automatically be reduced in size (if necessary) to fit the new console size. However, it will not be correspondingly increased in size of the user later enlarges the text console. To override this behavior, you'll need to provide an alternative to the ResizeTurboC function. You'll need to examine the source code for any further explanation.

No comments: