Screen Handler 64
Jeffrey Bailey
This useful utility adds some word processor-like features to the Commodore 64's full-screen editor for use within your own BASIC programs. It also works on the Commodore 128 in 64 mode.
Although it's often taken for granted, the Commodore 64 has one of the best full-screen editors in the business. You can easily move the cursor anywhere on the screen, type characters, and make changes wherever you like. Not all computer owners are so lucky.
As good as it is, however, there are some things that the Commodore 64's excellent screen editor can't do. If you were to draw up a wish list for a new screen editor that would be active when a BASIC program requests keyboard input, it might include these features:
- Switchable insert mode.
- Delete key that draws text into the cursor.
- Key to move the cursor to the beginning of input.
- Key to move the cursor to the last character typed.
- Key to erase text from the cursor to the end of the line.
That's quite an impressive list, but there's one more feature that's also desirable: a special key to clear out a single field. In this context, a field is simply a screen area of a certain size in which the user can type characters. For instance, let's say your BASIC program needs to request the date in the format mm/dd/yy. Each two-digit entry could be defined as a field that's two characters in length. Ideally, the user wouldn't need to type the / character between each field. As each two-digit entry was completed, the cursor would move automatically from the end of one field to the beginning of the next.
To take this concept even further, how about setting up multiple fields at different spots on the screen? If each field works as we've described, it wouldn't be necessary to press RETURN after typing data in each field. During the entry process, the user could move freely from field to field until each one is filled in. Then your program could read the entire screenful of data at once.
It may sound like a tall order, but "Screen Handler 64" makes all of this possible.
Entering The Program
Although Screen Handier 64 is written completely in machine language,
no knowledge of machine language is needed to use it. To type in Screen
Handler 64, simply use the "MLX" machine language entry program listed
elsewhere in this issue. Follow the MLX directions carefully. Here are
the addresses you need for MLX:
Starting address: C000
Ending address: C397
You can then begin entering the Screen Handler data from Program 1. When you finish entering all the data, be sure to use the MLX Save option to store at least one copy on tape or disk.
To load Screen Handler into memory, use LOAD"filename",8,1 for disk or LOAD"filename",1,1 for tape. Although Screen Handler is less than 1K in length, it uses all of the free memory from locations 49152--53247, so you shouldn't do anything to change those locations while the program is active. In addition, you should avoid changing the contents of memory locations 251--255, which Screen Handler uses as well.
Starting Screen Handler
It's quite simple to incorporate Screen Handler into your BASIC programs. After the machine language is in memory, set up a table of the fields you want, then call Screen Handler with a SYS command. Screen Handler takes care of everything else, including moving the input data into string variables. Getting started is actually a three-stage process. Let's look at each one in turn.
The first step after loading Screen Handler is to include the statement SYS 49152 in your BASIC program. This statement without any extra parameters, clears the field table that Screen Handler uses internally.
The next step is to set up each individual field, a job that's also done with a SYS. Here's the general format for the command:
SYS 49155,x,y,length,string$
Notice that this SYS statement is followed by four parameters. The first two, randy, stand for horizontal and vertical screen locations, and can be either numbers or numeric variables. The x value must be in the range 0--39, and y must be in the range 0--24.
The next parameter, length, defines the length of the field in characters. The length parameter can be any numeric value from 1--254. However, keep in mind that the field must not be so long that the screen scrolls when data is typed. If this happens, Screen Handler can't read the input correctly (because the data has moved up one or more lines).
The final parameter, string$, can be any type of string variable, from a simple string such as A$ to an array element like FI$(6). Multidimensional arrays like FI$(8,2) can also be used. Screen Handler automatically takes the information from the screen and puts it into this string variable. Note that if the input data is shorter than the field length (if the user types DOG in a ten-character field, for instance), Screen Handler fills the remainder of the string with spaces.
Once you've defined a string within a Screen Handler field, you should not redefine the string until after Screen Handler has been called (see below). This is because Screen Handler sets up pointers to BASIC's string storage space. If you suddenly redefine a string, it may move in memory, confusing the program. After Screen Handler has been called and input has been entered from the keyboard, it is safe to modify the string.
A Better Way To INPUT
Screen Handler permits you to define as many as 50 different fields. When all of the fields are set up, it's time to call Screen Handler. The statement SYS 49158 tells Screen Handler to begin receiving input from the fields you have defined.
At this point the up and down cursor keys move the cursor from field to field, not from line to line as usual. The RETURN key has the same effect as cursor down, moving you forward (down) to the next field. The left and right cursor keys work normally, but only within a field. That is, these keys move the cursor left and right inside the field as usual. But to move to another field, you must press cursor up/ down or RETURN.
Pressing CLR/HOME moves the cursor to the beginning of the current field. SHIFT--CLR/HOME erases the field and homes the cursor. The INST/DEL key works normally, but acts only on the current field. The CTRL-I key combination (hold down CTRL and press I) switches Screen Handler in and out of insert mode. The border color changes to indicate when insert mode is active.
Pressing CTRL-D deletes text by pulling it into the present cursor position. CTRL-E erases every character from the cursor to the end of the field. To move the cursor to the last character typed in a field, press CTRL-N.
That takes care of the entire wish list except for the most important part--storing all of the input data in variables. When all of the data is entered in all of the fields, press SHIFT--RETURN. Screen Handler enters the entire screen at once.
Practical Demonstration
Program 2, "Screen Handler Demo," is a simple BASIC program that illustrates how to use Screen Handler. Here's an explanation of how it works.
Line 10 loads Screen Handler from disk, and lines 20--60 create a screen display that outlines the fields visually. The DATA statement in line 70 contains x, y, and length values for defining the fields. Line 90 clears the field table and prepares Screen Handler for use. Lines 100--140 set up a simple loop to read in the values and set them up in Screen Handler's table.
Note that although an array is used in line 130, the program contains no DIM statement. If BASIC encounters an array that was not previously dimensioned, it automatically dimensions the array for 11 elements (numbered 0--10). Since Screen Handler uses some of BASIC's built-in routines, this feature is available as usual. However, it would be better programming practice to explicitly dimension the array variables--and the DIM is required if you want to use more than 11 array elements. In this case, the DIM statement must precede the SYS 49155 statement that assigns the array element to a field. In Program 2, the DIM statement could be placed anywhere before line 100.
Line 160 tells Screen Handler to start accepting data. Again, notice that the string variables must not be modified between the time that the fields are defined (line 130) and Screen Handler is called (line 160). Once the data has all been entered and you press SHIFT-RETURN to accept the fields, lines 170--210 clear the screen and print out the information. At this point in the program, it becomes safe to modify the string variables if needed.
With a little bit of practice, you can write very professional. programs in BASIC with Screen Handler's help. Experiment with programs of your own.