Window Defining: A Program, Part II

Author(s): David McCall
Publication: Update Magazine
Pub Details: Update October 1989
Date: October 1989
Pages: 45-49

In the last article, we wrote the assembly language routine which will become part of our window defining program. In this article we will write and discuss the different functions that will be used. SuperBASIC gives us the ability to write very versatile, many variable functions which can do more than merely return a number. Next time the procedures will be discussed. Once completed, the program will provide all the information discussed below and more in a user friendly pop up window. The window being defined will be able to be positioned and sized on the screen without overwriting anything, such as other windows or text. Every attribute about a window will be defined and a sample window displayed on the screen.

The following SuperBASIC programs, functions and procedures have been written in a very structured style. This is very helpful when trying to follow the logic. Different techniques are used to demonstrate the power and adaptability of SuperBASIC. From looking at the listing, the use of indentation is quite prominent. This is another helpful technique when trying to read long programs because the nested control is very clearly marked by the appropriate indentation.

The description begins with the simple sign function, sgn%. It will return the sign of a number: 1 if positive; -1 if negative; and 0 if zero. As indicated by the percentage symbol affixed to the end of the name, the function is of integer type. The logic will be a one lined IF-THEN-ELSE statement, lines 2000 to 2020. First the number needs to be tested against zero and a zero returned if so. Otherwise, the sign which is the absolute value of the number divided by itself is returned.

Notice that more than one RETurn was used. It is important to remember that once a RETurn has been encountered execution leaves the function or procedure at that point. Also, the name of the function is included in the END DEFine statement making it and other functions easier to identify.

The minimum of two numbers is another function which is useful. The logic is given in lines 2280 to 2340. The function name is Min and takes two parameters, mn1 and mn2. As indicated by the function and parameters names, it is and takes real or floating point numbers. The long form of the IF-THEN-ELSE statement is used this time. Note that this function can be used to determine the minimum of more than two numbers. For example, the minimum, x, of three numbers, mn1, mn2, mn3, can be determined by the following statement:

x = Min( mn1, Min( mn2, mn3))

All functions and procedures can be called recursively, like above, or within the function or procedure itself.

The next group of functions are all real valued but take no parameters. Instead global variables are used to pass parameters, for several reasons. First, the variables are constantly being changed and second, the functions never require different parameters. Additionally, it shows another alternative for the use of functions. They, max_xp, max_yp, max_xpix, max_ypix, max_xc, and max_yx, determine the maximum x and y point, pixel, and character positions for a window, respectively. Keep in mind that a pixel is the smallest screen element the computer can access, while a point is a position mapped onto these pixels. To clarify this concept, remember that the absolute maximum number of pixels across the screen is 512 while the number of points can be any value depending upon the scaling.

The global variables used are:

    scl = the SCALE of the window
  xg,yg = the graphics origin defined by the SCALE command
  scr_x = the x dimension of the window
  scr_y = the y dimension of the window
  brder = the BORDER width
cxi,cyi = the number of pixels a character occupies in the x
          and y directions

The simplest functions is max_yp, lines 2040 to 2060. The number of points in the y direction, scl, is defined with the SCALE command. The maximum y position is, therefore, the y origin value, yg, added to scl.

The most difficult to understand function is max_xp, lines 2080 to 2100. It is a function of many variables xg, scl, brder, scr_x, and scr_y, and the computation is complex. The SCALE command scales the window by making the x and y directions appear to have the same interval between points. There are 512 pixels in the x direction and 256 pixels in the y direction. Plus, the actual screen image on your monitor has a ratio of about 802 to 503 (this was determined experimentally to give the best answer in the function). The number of pixels that are accessible in the window are not scr_x and scr_y if a border is defined. They are (**scr_y – 2 * brder**) and (**scr_x – 4 * brder**) — the BORDER width is doubled in the y-direction and quadrupled in the x-direction.

To calculate the maximum point position in the x-direction, the maximum length of the window in the y-direction must be mapped into length as measured in the x direction. This is done by multiplying scl by (503/802). This result must then be multiplied by the ratio of pixels in the different direction, (**scr_x – 4*brder**)/(**scr_y – 2*brder**). Finally, it must be added to the x value of the graphics origin, xg.

The expression for this calculation given in line 2090 has alternating division and multiplication to maintain as many significant digits as possible. Whenever a calculation requires many multiplications and divisions, it should always be arranged such that the two operations alternate with division coming first. Otherwise more significant digits can be lost by the rounding error in the computer.

As discussed above, the number of accessible pixels in the defined window is (**scr_y – 2 * brder**) and (**scr_x – 4 * brder**). To obtain the values for max_xpix and max_ypix, one must be subtracted from both, see lines 2120 to 2180. This makes the pixel origin (0,0) instead of (1,1).

The last window characteristic functions, max_xc and max_yc, are relatively simple. These determine the maximum number of characters that can be printed in the window with the specified character size. This calculation is the integer portion of the maximum pixels divided by the number of pixels the character takes up. For the x and y directions, the equations are INT((**scr_x -4*brder -1**)/ cxi) and INT((**scr_y -2*brder -1**)/ cyi), respectively. Please refer to lines 2200 to 2260.

Finally, the remaining functions are used in conjunction to replace the standard INPUT. In their present state, they allow only integer values with a specified number of digits and maximum value. The values inputted with these routines are constantly right justified as they are typed in and if nothing is entered, a default value is returned. Also, if positive numbers are only desired, they will ensure this.

The function get_num% calls the main function routine, input%, which handles all justification and keyboard reading. Get_num% performs the limit and sign testing and returns the proper value according to the restrictions given. The first line, 2380, calls input% and sets the return value, old%, to a default value if nothing has been entered. The next line, 2390, checks to see if the value is greater than the maximum value, max%. If greater, it sets the return value to the maximum value. The following line, 2400, checks the positive value only flag, positive%, and if set, makes sure the value is positive. Finally, line 2410 returns the value.

The last function, input%, lines 2440 to 3420, will not be discussed line by line as the previous ones were because of its length. However, each major section will be discussed and explained. The first five lines initialize the variables, set ink color, and define the local variables. The rest of the function is a REPeat loop with a large SELect ON statement nested inside. Basically the function constantly checks the keyboard with an INKEY$ function and the SELect ON statement performs the necessary tasks depending upon the key entered. The number being entered is kept in a string stg$. The variable pos% keeps track of the string position the cursor is presently in, length% holds the number of digits typed, and max% is the maximum number of digits allowed.

The first few lines of the REPeat loop, 2520 to 2560, print the cursor and retrieve the next key. The SELect statement processes the key typed. If the “ESC” key is typed, the program is stopped, lines 2580 to 2590. If “-” or “_” is typed a negative sign is inserted at the far left position, 2600 to 2610. Similarly, if “=” or “+” is typed, a space is put in the far left position. If a number is typed, it is inserted at the present cursor location and the number of digits, length%, is increased, 2640 to 2780. As you can see, a double nested IF-THEN-ELSE statement is used. This is necessary because of the different cursor positions, far left, far right, in the middle, that are possible and the string functions in SuperBASIC require different handling in each case.

Lines 2790 to 2850 move the cursor about the already typed numbers and it is not allowed to move across any untyped space. Right deletion is handled with lines 2860 to 3050. Again there are two nested IF-THEN-ELSE structures taking care of the different possibilities that could arise. The outer IF statement checks to see where the cursor is in the string. If it is at the first position, removing a character is easier. However if there has only been one character typed in the string can be reset to its initial state otherwise the rest of the characters need to be shifted.

When the cursor is not in the first position, the string needs to be cut in two and put back together without the deleted character. The left% and right% variables are used to find the position just left of the cursor and just right of the cursor. The string is then reconstructed after the left position is checked to make sure it is still within the string.

Left deletion is performed by lines 3060 to 3260 and is analogous to the right delete except it does not remove the character the cursor is on like the regular INPUT. The right delete was chosen to perform this action because the routine keeps numbers right justified.

The number is entered whenever “Return”, “Up Arrow”, or “Down Arrow” is pressed, lines 3270 to 3300. The last ON clause repeats the loop again if any other key is typed. Immediately following the END SELect, lines 3340 to 3350, are the lines which update the string on the screen. Finally, lines 3370 to 3410 return the value entered or -9999, which indicates nothing has been typed in.

Half of the larger window defining program has been covered with this second article. You will notice that the line numbering started at 2000. Each article has been constructed to ensure no line numbering conflicts when they are all merged together at the end of this series. The complete program will be available on an “Issue Disk” also.

LISTING A

2000 DEFine FuNction sgn%(t%)
2010 IF t%=0 THEN RETurn 0:ELSE RETurn ABS(t%)/t%
2020 END DEFine sgn%
2030 :
2040 DEFine FuNction max_yp
2050 RETurn scl + yg
2060 END DEFine max_yp
2070 :
2080 DEFine FuNction max_xp
2090 RETurn scl/802*503/(scr_y-2*brder-1)*(scr_x-4*brder-1)+xg
2100 END DEFine max_xp
2110 :
2120 DEFine FuNction max_xpix
2130 RETurn (scr_x-4*brder-1)
2140 END DEFine max_pix
2150 :
2160 DEFine FuNction max_ypix
2170 RETurn (scr_y-2*brder-1)
2180 END DEFine max_ypix
2190 :
2200 DEFine FuNction max_xc
2210 RETurn INT((scr_y-2*brder-1)/cyi)
2220 END DEFine max_xc
2230 :
2240 DEFine FuNction max_yc
2250 RETurn INT((scr_x-4*brder-1)/cxi)
2260 END DEFine max_yc
2270 :
2280 DEFine FuNction Min(mn1,mn2)
2290 IF mn1<mn2 THEN
2300 RETurn mn1
2310 ELSE
2320 RETurn mn2
2330 END IF
2340 END DEFine Min
2350 :
2360 DEFine FuNction get_num%(win%,x,y,len%,old%,max%,positve%)
2370 LOCal t%
2380 t%=input%(win%,x,y,len%):IF t%<>-9999 THEN old%=t%
2390 IF ABS(old%)>max% THEN old%=sgn%(old%)*max%
2400 IF positve% THEN old%=ABS(old%)
2410 RETurn old%
2420 END DEFine get_num%
2430 :
2440 DEFine FuNction input% (win%,x,y,max%)
2450 LOCal loop%,k$(1),k,s$(1),stg$(max%),pos%,right%,left%,length%
2460 INK#win%,7
2470 s$(1)=' '
2480 stg$=FILL$('_',max%)
2490 pos%=1
2500 length%=0
2510 REPeat loop%
2520 OVER#win%, -1
2530 AT #win%,x,y+max%-pos%:PRINT #win%,'_'
2540 OVER #win%,0
2550 k$(1)=INKEY$(#3)
2560 k=CODE(k$(1))
2570 SELect ON k
2580 ON k=27
2590 STOP
2600 ON k=45,95
2610 s$(1)='-'
2620 ON k=43,61
2630 s$(1)=' '
2640 ON k=48 TO 57
2650 IF pos%=1 THEN
2660 IF max%>1 THEN
2670 stg$(1 TO max%)=stg$(2 TO max%)&k$(1)
2680 ELSE
2690 stg$(1)=k$(1)
2700 END IF
2710 ELSE
2720 IF pos%<max% THEN
2730 stg$(1 TO max%)=stg$(2 TO max%-pos%+1)&k$(1)&
stg$(max%-pos%+2 TO max%)
2740 ELSE
2750 stg$(1 TO max%)=k$(1)&stg$(2 TO max%)
2760 END IF
2770 END IF
2780 length%=(length%+1):IF length%>max% THEN length%=max%
2790 ON k=200
2800 pos% = pos% -1
2810 IF pos%<=0 THEN pos% = 1
2820 ON k=192
2830 pos% = pos% +1
2840 IF pos%>=length% THEN pos% = length%
2850 IF pos%=0 THEN pos%=1
2860 ON k=202
2870 IF pos% <> 1 THEN
2880 left% = max%-pos%
2890 right% = max%-pos%+2
2900 IF left% < 1 THEN
2910 stg$(1 TO max%)='_'&stg$(2 TO max%)
2920 ELSE
2930 stg$(1 TO max%)='_'&stg$(1 TO left%)&stg$(right% TO max%)
2940 END IF
2950 length%=length%-1:IF length%<0 THEN length%=0
2960 ELSE
2970 IF max%=1 THEN
2980 stg$(1 TO max%)='_'
2990 ELSE
3000 stg$(1 TO max%)='_'&stg$(1 TO max%-1)
3010 END IF
3020 length%=length%-1:IF length%<0 THEN length%=0
3030 END IF
3040 pos%=pos%-1
3050 IF pos%<=0 THEN pos%=1
3060 ON k=194
3070 IF pos% = 1 THEN
3080 IF max%<=2 THEN
3090 IF max%=2 THEN
3100 stg$(1 TO max%)='_'&stg$(max%)
3110 length%=length%-1:IF length%<1 THEN length%=1
3120 END IF
3130 ELSE
3140 stg$(1 TO max%)='_'&stg$(1 TO max%-2)&stg$(max%)
3150 length%=length%-1:IF length%<1 THEN length%=1
3160 END IF
3170 ELSE
3180 left% = max%-pos%-1
3190 right%=max%-pos%+1
3200 IF left%<1 THEN
3210 stg$(1 TO max%)='_'&stg$(right% TO max%)
3220 ELSE
3230 stg$(1 TO max%)='_'&stg$(1 TO left%)&stg$(right% TO max%)
3240 END IF
3250 length%=length%-1:IF length%<0 THEN length%=0
3260 END IF
3270 ON k=10,208,216
3280 AT#win%,x,y-1
3290 PRINT #win%,s$(1)&stg$
3300 EXIT loop%
3310 ON k= REMAINDER
3320 NEXT loop%
3330 END SELect
3340 AT#win%,x,y-1
3350 PRINT#win%,s$(1);stg$
3360 END REPeat loop%
3370 IF length%=0 THEN
3380 RETurn -9999
3390 ELSE
3400 RETurn s$(1)&stg$(max%-length%+1 TO max%)
3410 END IF
3420 END DEFine input%

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.