home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / bas / hanlin3 / baswiz30 / baswiz.doc < prev    next >
Text File  |  1994-11-04  |  155KB  |  3,887 lines

  1.      BasWiz  Copyright (c) 1990-1994  Thomas G. Hanlin III
  2.      =---------------------------------------------------=
  3.             The BASIC Wizard's Library, version 3.0
  4.  
  5.  
  6.  
  7. Use of LibWiz is strongly recommended for creating the BasWiz
  8. library, due to the number of routines involved.
  9.  
  10. This is BasWiz, a general-purpose library with hundreds of
  11. routines for use with Microsoft BASIC compilers: QuickBasic,
  12. Bascom/PDS, and Visual BASIC for DOS.
  13.  
  14. The BasWiz collection is copyrighted. It may be distributed only
  15. under the following conditions:
  16.  
  17.    All BasWiz files must be distributed together as a unit in
  18.    unmodified form. No files may be left out or added.
  19.  
  20. YOU USE THIS LIBRARY AT YOUR OWN RISK. I have tested it on my
  21. own computer, but I will not assume responsibility for any
  22. problems which BasWiz may cause you.
  23.  
  24. It is expected that if you find BasWiz useful, you will register
  25. your copy. You may not use BasWiz routines in programs intended
  26. for distribution unless you have registered. See the ORDER.FRM
  27. file for registration details.
  28.  
  29.  
  30.  
  31. To create a BasWiz library using LibWiz, you will need a
  32. complete set of .OBJ files for all BasWiz routines. Create a
  33. fresh subdirectory and extract the BASIC source code from the
  34. BW$BAS archive. Compile them using a DOS command like so:
  35.  
  36.    FOR %x IN (*.BAS) DO BC %x /o;
  37.  
  38. Add the /FS switch (before the /o) if you wish to use far
  39. strings with the PDS compiler. This is required for using BasWiz
  40. in the QBX editor/environment.
  41.  
  42. Extract the .OBJ files from BW$MAIN.LIB into the same location,
  43. using the UNLIB utility that comes with LibWiz.
  44.  
  45. Now you must extract the string routines by using UNLIB on the
  46. appropriate string library: BW$NEAR.LIB for near strings or
  47. BW$FAR for far strings. For QuickBasic, use near strings. For
  48. Visual Basic, use far strings. For PDS, choose one or the other
  49. (use far strings if you use the QBX editor/environment).
  50.  
  51. After these three steps-- compiling the BASIC files with your
  52. compiler, extracting the main .OBJ files, and extracting the
  53. appropriate string .OBJ files-- you have a complete set of .OBJs
  54. for BasWiz. Use LibWiz to create a custom subset of BasWiz
  55. that's tailored to your needs.
  56.  
  57.                       Table of Contents                 page 2
  58.  
  59.  
  60.  
  61.  Overview and Legal Info ................................... 1
  62.  
  63.  BCD Math .................................................. 3
  64.  
  65.  Expression Evaluator ...................................... 7
  66.  
  67.  Extensions to BASIC's math ................................ 8
  68.  
  69.  Far Strings .............................................. 11
  70.  
  71.  File Handling ............................................ 13
  72.  
  73.  Fractions ................................................ 21
  74.  
  75.  Graphics
  76.     General Routines ...................................... 22
  77.     VESA Info Routines .................................... 32
  78.     Text-mode Routines .................................... 34
  79.     Dual Monitor Routines ................................. 35
  80.     Printer Routines ...................................... 36
  81.     A Little Geometry ..................................... 37
  82.     Equations, Etc ........................................ 41
  83.  
  84.  Memory Management and Pointers ........................... 44
  85.  
  86.  Quick Time (millisecond timer) ........................... 48
  87.  
  88.  Telecommunications ....................................... 50
  89.  
  90.  Virtual Windowing System ................................. 56
  91.  
  92.  Other Routines ........................................... 70
  93.  
  94.  Miscellaneous Notes ...................................... 71
  95.  
  96.  Error Codes .............................................. 74
  97.  
  98.  Troubleshooting .......................................... 76
  99.  
  100.  History & Philosophy ..................................... 79
  101.  
  102.  Using BasWiz with P.D.Q. or QBTiny ....................... 81
  103.  
  104.  Credits .................................................. 82
  105.  
  106.                           BCD Math                      page 3
  107.  
  108.  
  109.  
  110. Some of you may not have heard of BCD math, or at least not have
  111. more than a passing acquaintance with the subject. BCD (short
  112. for Binary-Coded Decimal) is a way of encoding numbers. It
  113. differs from the normal method of handling numbers in several
  114. respects. On the down side, BCD math is much slower than normal
  115. math and the numbers take up more memory. However, the benefits
  116. may far outweigh these disadvantages, depending on your
  117. application: BCD math is absolutely precise within your desired
  118. specifications, and you can make a BCD number as large as you
  119. need. If your applications don't require great range or
  120. precision out of numbers, normal BASIC math is probably the best
  121. choice. For scientific applications, accounting, engineering and
  122. other demanding tasks, though, BCD may be just the thing you
  123. need.
  124.  
  125. The BCD math routines provided by BasWiz allow numbers of up to
  126. 255 digits long (the sign counts as a digit, but the decimal
  127. point doesn't). You may set the decimal point to any position
  128. you like, as long as there is at least one digit position to the
  129. left of the decimal.
  130.  
  131. Since QuickBasic doesn't support BCD numbers directly, we store
  132. the BCD numbers in strings. The results are not in text format
  133. and won't mean much if displayed. A conversion routine allows
  134. you to change a BCD number to a text string in any of a variety
  135. of formats.
  136.  
  137. Note that the BCD math handler doesn't yet track
  138. overflow/underflow error conditions. If you anticipate that this
  139. may be a problem, it would be a good idea to screen your input
  140. or to make the BCD range large enough to avoid these errors.
  141.  
  142. Let's start off by examining the routine which allows you to set
  143. the BCD range:
  144.  
  145.    BCDSetSize LeftDigits%, RightDigits%
  146.  
  147. The parameters specify the maximum number of digits to the left
  148. and to the right of the decimal point. There must be at least
  149. one digit on the left, and the total number of digits must be
  150. less than 255. The BCD strings will have a length that's one
  151. larger than the total number of digits, to account for the sign
  152. of the number. The decimal point is implicit and doesn't take up
  153. any extra space.
  154.  
  155. It is assumed that you will only use one size of BCD number in
  156. your program-- there are no provisions for handling mixed-length
  157. BCD numbers. Of course, you could manage that yourself with a
  158. little extra work, if it seems like a useful capability. If you
  159. don't use BCDSetSize, the default size of the BCD numbers will
  160. be 32 (20 to the left, 11 to the right, 1 for the sign).
  161.  
  162.                           BCD Math                      page 4
  163.  
  164.  
  165.  
  166. You can get the current size settings as well:
  167.  
  168.    BCDGetSize LeftDigits%, RightDigits%
  169.  
  170. Of course, before doing any BCD calculations, you must have some
  171. BCD numbers! The BCDSet routine takes a number in text string
  172. form and converts it to BCD:
  173.  
  174.    TextSt$ = "1234567890.50"
  175.    Nr$ = BCDSet$(TextSt$)
  176.  
  177. If your numbers are stored as actual numbers, you can convert
  178. them to a text string with BASIC's STR$ function, then to BCD.
  179. Leading spaces are ignored:
  180.  
  181.    Nr$ = BCDSet$(STR$(AnyNum#))
  182.  
  183. BCD numbers can also be converted back to text strings, of
  184. course. You may specify how many digits to the right of the
  185. decimal to keep (the number will be truncated, not rounded). If
  186. the RightDigits% is positive, trailing zeros will be kept; if
  187. negative, trailing zeros will be removed. There are also various
  188. formatting options which may be used. Here's how it works:
  189.  
  190.    TextSt$ = BCDFormat$(Nr$, HowToFormat%, RightDigits%)
  191.  
  192. The HowToFormat% value may be any combination of the following
  193. (just add the numbers of the desired formats together):
  194.  
  195.    0   plain number
  196.    1   use commas to separate thousands, etc
  197.    2   start number with a dollar sign
  198.    4   put the sign on the right side of the number
  199.    8   use a plus sign if the number is not negative
  200.  
  201.                           BCD Math                      page 5
  202.  
  203.  
  204.  
  205. The BCD math functions are pretty much self-explanatory, so I'll
  206. keep the descriptions brief. Here are the single-parameter
  207. functions:
  208.  
  209.    Result$ = BCDAbs$(Nr$)       ' absolute value
  210.    Result$ = BCDCos$(Nr$)       ' cosine function
  211.    Result$ = BCDCot$(Nr$)       ' cotangent function
  212.    Result$ = BCDCsc$(Nr$)       ' cosecant function
  213.    Result$ = BCDDeg2Rad$(Nr$)   ' convert degrees to radians
  214.    e$ = BCDe$                   ' the constant "e"
  215.    Result$ = BCDFact$(N%)       ' factorial
  216.    Result$ = BCDFrac$(Nr$)      ' return the fractional part
  217.    Result$ = BCDInt$(Nr$)       ' return the integer part
  218.    Result$ = BCDNeg$(Nr$)       ' negate a number
  219.    pi$ = BCDpi$                 ' the constant "pi"
  220.    Result$ = BCDRad2Deg$(Nr$)   ' convert radians to degrees
  221.    Result$ = BCDSec$(Nr$)       ' secant function
  222.    Result% = BCDSgn%(Nr$)       ' signum function
  223.    Result$ = BCDSin$(Nr$)       ' sine function
  224.    Result$ = BCDSqr$(Nr$)       ' square root
  225.    Result$ = BCDTan$(Nr$)       ' tangent function
  226.  
  227. Notes on the single-parameter functions:
  228.  
  229.   The signum function returns an integer based on the sign of
  230.   the BCD number:
  231.  
  232.      -1   if the BCD number is negative
  233.       0   if the BCD number is zero
  234.       1   if the BCD number is positive
  235.  
  236.   BCDpi$ is accurate to the maximum level afforded by the BCD
  237.   functions. BCDe$ is accurate to as many as 115 decimal places.
  238.   The actual accuracy, of course, depends on the size of BCD
  239.   numbers you've chosen.
  240.  
  241.   The trigonometric functions (cos, sin, tan, sec, csc, cot)
  242.   expect angles in radians. BCDDeg2Rad and BCDRad2Deg will allow
  243.   you to convert back and forth between radians and degrees.
  244.  
  245.                           BCD Math                      page 6
  246.  
  247.  
  248.  
  249. Here is a list of the two-parameter functions:
  250.  
  251.  
  252.    Result$ = BCDAdd$(Nr1$, Nr2$)      ' Nr1 + Nr2
  253.  
  254.    Result$ = BCDSub$(Nr1$, Nr2$)      ' Nr1 - Nr2
  255.  
  256.    Result$ = BCDMul$(Nr1$, Nr2$)      ' Nr1 * Nr2
  257.  
  258.    Result$ = BCDDiv$(Nr1$, Nr2$)      ' Nr1 / Nr2
  259.  
  260.    Result$ = BCDPower$(Nr$, Power%)   ' Nr ^ Power
  261.  
  262.    Result% = BCDCompare%(Nr1$, Nr2$)  ' compare two numbers
  263.  
  264. The comparison function returns an integer which reflects how
  265. the two numbers compare to each other:
  266.  
  267.    -1   Nr1 < Nr2
  268.     0   Nr1 = Nr2
  269.     1   Nr1 > Nr2
  270.  
  271.                     Expression Evaluator                page 7
  272.  
  273.  
  274.  
  275. The expression evaluator allows you to find the result of an
  276. expression contained in a string. Normal algebraic precedence is
  277. used, e.g. 4+3*5 evaluates to 19. The usual numeric operators
  278. (*, /, +, -, ^) are supported (multiply, divide, add, subtract,
  279. and raise to a power). Use of negative numbers is just fine, of
  280. course. Parentheses for overriding the default order of
  281. operations are also supported.
  282.  
  283. You may use either double asterisk ("**") or caret ("^") symbols
  284. to indicate exponentiation.
  285.  
  286. The constant PI is recognized, as are the following functions:
  287.    ABS    absolute value        INT    integer
  288.    ACOS   inverse cosine        LOG    natural log
  289.    ASIN   inverse sine          SIN    sine
  290.    ATAN   inverse tangent       SQR    square root
  291.    COS    cosine                TAN    tangent
  292.    FRAC   fraction
  293.  
  294. Functions should not be nested.
  295.  
  296. Trig functions expect angles in radians.
  297.  
  298. To evaluate an expression, you pass it to the evaluator as a
  299. string. You will get back either an error code or a single-
  300. precision result. Try this example to see how the expression
  301. evaluator works:
  302.  
  303.    REM $INCLUDE: 'BASWIZ.BI'
  304.    DO
  305.       INPUT "Expression? "; Expr$
  306.       IF LEN(Expr$) THEN
  307.          Evaluate Expr$, Result!, ErrCode%
  308.          IF ErrCode% THEN
  309.             PRINT "Invalid expression.  Error = "; ErrCode%
  310.          ELSE
  311.             PRINT "Result: "; Result!
  312.          END IF
  313.       END IF
  314.    LOOP WHILE LEN(Expr$)
  315.    END
  316.  
  317. An expression evaluator adds convenience to any program that
  318. needs to accept numbers. Why make someone reach for a calculator
  319. when number crunching is what a computer does best?
  320.  
  321.                  Extensions to BASIC's math             page 8
  322.  
  323.  
  324.  
  325. For the most part, the math routines in this library is designed
  326. to provide alternatives to the math routines that are built into
  327. BASIC. Still, BASIC's own math support is quite adequate for
  328. many purposes, so there's no sense in ignoring it. Here are some
  329. functions which improve on BASIC's math. I'll list the single-
  330. precision functions on this page, and double-precision on the
  331. next.
  332.  
  333.    Result! = ArcCosHS!(Nr!)    ' inverse hyperbolic cosine
  334.    Result! = ArcCosS!(Nr!)     ' inverse cosine (1 >= Nr >= -1)
  335.    Result! = ArcCotS!(Nr!)     ' inverse cotangent
  336.    Result! = ArcCotSH!(Nr!)    ' inverse hyperbolic cotangent
  337.    Result! = ArcCscHS!(Nr!)    ' inverse hyperbolic cosecant
  338.    Result! = ArcCscS!(Nr!)     ' inverse cosecant
  339.    Result! = ArcSecHS!(Nr!)    ' inverse hyperbolic secant
  340.    Result! = ArcSecS!(Nr!)     ' inverse secant
  341.    Result! = ArcSinHS!(Nr!)    ' inverse hyperbolic sine
  342.    Result! = ArcSinS!(Nr!)     ' inverse sine (1 >= Nr >= -1)
  343.    Result! = ArcTanHS!(Nr!)    ' inverse hyperbolic tangent
  344.    Result! = Cent2Fahr!(Nr!)   ' centigrade to Fahrenheit
  345.    Result! = CosHS!(Nr!)       ' hyperbolic cosine
  346.    Result! = CotHS!(Nr!)       ' hyperbolic cotangent
  347.    Result! = CotS!(Nr!)        ' cotangent
  348.    Result! = CscHS!(Nr!)       ' hyperbolic cosecant
  349.    Result! = CscS!(Nr!)        ' cosecant
  350.    Result! = Deg2RadS!(Nr!)    ' convert degrees to radians
  351.    e! = eS!                    ' the constant "e"
  352.    Result! = ErfS!(Nr!)        ' error function
  353.    Result! = FactS!(Nr%)       ' factorial
  354.    Result! = Fahr2Cent!(Nr!)   ' Fahrenheit to centigrade
  355.    Result! = Kg2Pound!(Nr!)    ' convert kilograms to pounds
  356.    Pi! = PiS!                  ' the constant "pi"
  357.    Result! = Pound2Kg!(Nr!)    ' convert pounds to kilograms
  358.    Result! = Rad2DegS!(Nr!)    ' convert radians to degrees
  359.    Result! = SecHS!(Nr!)       ' hyperbolic secant
  360.    Result! = SecS!(Nr!)        ' secant
  361.    Result! = SinHS!(Nr!)       ' hyperbolic sine
  362.    Result! = TanHS!(Nr!)       ' hyperbolic tangent
  363.  
  364.                  Extensions to BASIC's math             page 9
  365.  
  366.  
  367.  
  368.    Result# = ArcCosD#(Nr#)     ' inverse cosine (1 >= Nr >= -1)
  369.    Result# = ArcCosHD#(Nr#)    ' inverse hyperbolic cosine
  370.    Result# = ArcCotD#(Nr#)     ' inverse cotangent
  371.    Result# = ArcCotHD#(Nr#)    ' inverse hyperbolic cotangent
  372.    Result# = ArcCscD#(Nr#)     ' inverse cosecant
  373.    Result# = ArcCscHD#(Nr#)    ' inverse hyperbolic cosecant
  374.    Result# = ArcSecD#(Nr#)     ' inverse secant
  375.    Result# = ArcSecHD#(Nr#)    ' inverse hyperbolic secant
  376.    Result# = ArcSinD#(Nr#)     ' inverse sine (1 >= Nr >= -1)
  377.    Result# = ArcSinHD#(Nr#)    ' inverse hyperbolic sine
  378.    Result# = ArcTanHD#(Nr#)    ' inverse hyperbolic tangent
  379.    Result# = CosHD#(Nr#)       ' hyperbolic cosine
  380.    Result# = CotD#(Nr#)        ' cotangent
  381.    Result# = CotHD#(Nr#)       ' hyperbolic cotangent
  382.    Result# = CscD#(Nr#)        ' cosecant
  383.    Result# = CscHD#(Nr#)       ' hyperbolic cosecant
  384.    Result# = Deg2RadD#(Nr#)    ' convert degrees to radians
  385.    e# = eD#                    ' the constant "e"
  386.    Result# = ErfD#(Nr#)        ' error function
  387.    Result# = FactD#(Nr%)       ' factorial
  388.    Pi# = PiD#                  ' the constant "pi"
  389.    Result# = Rad2DegD#(Nr#)    ' convert radians to degrees
  390.    Result# = SecD#(Nr#)        ' secant
  391.    Result# = SecHD#(Nr#)       ' hyperbolic secant
  392.    Result# = SinHD#(Nr#)       ' hyperbolic sine
  393.    Result# = TanHD#(Nr#)       ' hyperbolic tangent
  394.  
  395.                  Extensions to BASIC's math            page 10
  396.  
  397.  
  398.  
  399.    Result% = GCDI%(Nr1%, Nr2%)  ' greatest common denominator
  400.    Result% = Power2I%(Nr%)      ' raise 2 to a specified power
  401.    Result% = ROLI%(Nr%, Count%) ' bit-rotate left
  402.    Result% = RORI%(Nr%, Count%) ' bit-rotate right
  403.    Result% = SHLI%(Nr%, Count%) ' bit-shift left
  404.    Result% = SHRI%(Nr%, Count%) ' bit-shift right
  405.    Result% = VALI%(St$)         ' integer VAL function
  406.  
  407.    Result& = GCDL&(Nr1&, Nr2&)  ' greatest common denominator
  408.    Result& = Power2L&(Nr%)      ' raise 2 to a specified power
  409.  
  410.  
  411.  
  412. Like BASIC's trig functions, these trig functions expect the
  413. angle to be in radians. Conversion functions are provided in
  414. case you prefer degrees.
  415.  
  416. Note that there is no ArcTanS! or ArcTanD# function for the
  417. simple reason that BASIC supplies an ATN function.
  418.  
  419. Constants are expressed to the maximum precision available.
  420.  
  421. The Power2I% and Power2L& functions are vastly quicker than the
  422. equivalent BASIC formulas. If powers of two are useful to you,
  423. try these functions!
  424.  
  425.  
  426.  
  427. If you are not familiar with variable postfix symbols, here's a
  428. brief summary:
  429.  
  430.   Symbol   Meaning             Range (very approximate)
  431.   ------   --------            ------------------------
  432.     %      integer             +- 32767
  433.     &      long integer        +- 2 * 10^9
  434.     !      single precision    +- 1 * 10^38   (7-digit prec.)
  435.     #      double precision    +- 1 * 10^308  (15-digit prec.)
  436.     $      string              [0 to 32767 characters]
  437.  
  438. See your BASIC manual or QuickBasic's online help for further
  439. details.
  440.  
  441.                          Far Strings                   page 11
  442.  
  443.  
  444.  
  445. One of the best things about BASIC is its support for
  446. variable-length strings. Few other languages support such
  447. dynamically-allocated strings and they're a terrifically
  448. efficient way of using memory. At least, they would be, except
  449. for one minor limitation... in every version of QuickBasic and
  450. BASCOM (except for the new and expensive BASCOM 7.0
  451. "Professional Development System"), string space is limited to a
  452. mere 50K-60K bytes. As if this weren't trouble enough, this
  453. space is also shared with a number of other things. Running out
  454. of string space is a common and painful problem.
  455.  
  456. Anyway, it used to be. The BasWiz library comes with an
  457. assortment of routines and functions which allow you to keep
  458. variable-length strings outside of BASIC's tiny string area.
  459. Currently, you may have up to 65,535 far strings of up to 255
  460. characters each, subject to available memory. Either normal
  461. system memory or expanded memory may be used.
  462.  
  463. Using far strings works almost the same way as using normal
  464. strings. Rather than referring to a far string with a string
  465. variable name, however, you refer to it with an integer variable
  466. called a "handle". To create a new far string, you use a handle
  467. of zero. A new handle will be returned to you which will
  468. identify that string for future reference.
  469.  
  470. Before you use any far strings, you must initialize the far
  471. string handler. When you are done using far strings, you must
  472. terminate the far string handler. Normally, each of these
  473. actions will take place only once in your program: you
  474. initialize at the beginning and terminate at the end.
  475.  
  476. NOTE: The BasWiz far string handler does not support PDS or
  477. VB/DOS far strings! If you are using Microsoft far strings, you
  478. can't use BasWiz far strings.
  479.  
  480.                          Far Strings                   page 12
  481.  
  482.  
  483.  
  484. A working example of far string use is provided in FDEMO.BAS.
  485. Somewhat simplified code is given below as an overview.
  486.  
  487.    REM $INCLUDE: 'BASWIZ.BI'
  488.    DIM Text%(1 TO 5000)           ' array for string handles
  489.    FSInit 0                       ' init far string handler
  490.    TextLines = 0
  491.    OPEN "ANYFILE.TXT" FOR INPUT AS #1
  492.    DO UNTIL EOF(1)
  493.       LINE INPUT#1, TextRow$
  494.       Handle% = 0                 ' zero to create new string
  495.       FSSet Handle%, TextRow$     ' set far string
  496.       TextLines% = TextLines% + 1
  497.       Text(TextLines%) = Handle%  ' save far string handle
  498.    LOOP
  499.    CLOSE
  500.    FOR Row% = 1 TO TextLines%
  501.       PRINT FSGet$(Text%(Row%))   ' display a far string
  502.    NEXT
  503.    FSDone                         ' close far string handler
  504.    END
  505.  
  506. If you wanted to change an existing far string, you would
  507. specify its existing handle for FSSet. The handle of zero is
  508. used only to create new far strings, rather in the manner of
  509. using a new variable for the first time.
  510.  
  511. Note the 0 after the FSInit call. That specifies that main
  512. system memory is to be used. If you would prefer to use EMS, use
  513. a 1. If you specify EMS and none is available, BasWiz will fall
  514. back to conventional memory.
  515.  
  516.                         File Handling                  page 13
  517.  
  518.  
  519.  
  520. The file handling capabilities of BASIC were improved quite a
  521. bit as of QuickBasic 4.0. A binary mode was added and it became
  522. possible to use structured (TYPE) variables instead of the
  523. awkward FIELD-based random access handling. Even today, however,
  524. BASIC file handling is inefficient for many tasks. It requires
  525. error trapping to avoid problems like open floppy drive doors
  526. and cannot transfer information in large quantities at a time.
  527.  
  528. The BasWiz routines provide additional flexibility and power.
  529. They allow you to access files at as low or high a level as you
  530. wish. Here are some of the features of BasWiz file handling:
  531.  
  532.   - File sharing is automatically used if the DOS version is
  533.     high enough, (DOS 3.0 or later) providing effortless
  534.     network compatibility.
  535.   - Critical errors, like other errors, are detected at any
  536.     point you find convenient via a single function call.
  537.   - Optional input buffers speed up reading from files.
  538.   - Up to 32K of data may be read or written at one time.
  539.   - Files can be flushed to disk to avoid loss due to power
  540.     outages, etc.
  541.  
  542. Files are not considered to be strongly moded by BasWiz,
  543. although there are a few limitations on how you can deal with
  544. text files as opposed to other kinds of files. Reads and writes
  545. normally take place sequentially, like the INPUT and OUTPUT
  546. modes allowed by BASIC. However, you can also do random access
  547. by moving the file pointer to anywhere in the file, just as with
  548. the RANDOM and BINARY modes allowed by BASIC. These routines
  549. place no arbitrary limitations on the programmer.
  550.  
  551. As with BASIC, files are referred to by a number after they are
  552. opened for access. Unlike BASIC, the number is returned to you
  553. when the file is successfully opened, rather than being
  554. specified by you when you open the file. This means that you
  555. never have to worry about a file number already being in use.
  556. We'll refer to the file number as a "file handle" from now on.
  557.  
  558.                         File Handling                  page 14
  559.  
  560.  
  561.  
  562. Before doing anything else, you must initialize the file
  563. handling routines. This is typically done only once, at the
  564. beginning of your program. The FInit routine needs to know the
  565. number of files you want to deal with. This can be up to 15
  566. files, or possibly up to 50 if you are using DOS 3.3 or higher.
  567.  
  568.    FInit MaxFiles%, ErrCode%
  569.  
  570. A file is opened for access like so:
  571.  
  572.    FOpen File$, FMode$, BufferLen%, Handle%, ErrCode%
  573.  
  574. You pass the File$, FMode$, and BufferLen%. The Handle% and
  575. ErrCode% are returned to you. The BufferLen% valueis the length
  576. of the buffer desired for input. This must be zero if you want
  577. to write to the file. The filename is passed in File$, naturally
  578. enough. There is a choice of various modes for FMode$ and these
  579. can be combined to some extent:
  580.  
  581.    A   Append to file      used to add to an existing file
  582.    C   Create file         creates a new file
  583.    R   Read access         allows reading (input) from a file
  584.    T   Text mode file      allows text-mode input from a file
  585.    W   Write access        allows writing (output) to a file
  586.  
  587. For the most part, the combinations are self-explanatory. For
  588. instance, it would be reasonable to open a file for read and
  589. write, for create and write, for append and write, or for read
  590. and text. Text files always require a buffer. If you request
  591. text access without specifying a buffer, a buffer of 512 bytes
  592. will be provided for you. If you request "create" access without
  593. additional parameters, the file will be opened for write by
  594. default.
  595.  
  596. You may not use a buffer if you want to write to a file. This
  597. includes text files, which always use a buffer, as well as
  598. binary files. However, writing may be done to a text-type file
  599. if the file was not opened in text mode. We'll see how that
  600. works presently.
  601.  
  602. When you are done using a particular file, you can close it,
  603. just as in ordinary BASIC:
  604.  
  605.    FClose Handle%
  606.  
  607. Before your program ends, you should terminate the file handler.
  608. This will close any open files as well as concluding use of the
  609. file routines:
  610.  
  611.    FDone
  612.  
  613.                         File Handling                  page 15
  614.  
  615.  
  616.  
  617. That covers the basic set-up routines: initialize, open, close,
  618. and terminate. Of more interest are the routines which actually
  619. deal with the file itself. These provide assorted read/write
  620. services, the ability to get or set the file read/write pointer,
  621. size, time, and date, and the ability to get or set the error
  622. code for a specific file, among other things. Let's take a look
  623. at the error handler first.
  624.  
  625. The FInit and FOpen routines return an error code directly,
  626. since you need to know immediately if these have failed. The
  627. other file routines do not return a direct error code, however.
  628. In order to discover whether an error has occurred, you use the
  629. FGetError% function. This will return an error of zero if there
  630. was no error, or a specific error code (listed at the end of
  631. this manual) if some problem occurred. The error code will
  632. remain the same until you reset it using FError. The FError
  633. service also allows you to test your error handler by forcing
  634. specific error codes even when everything is fine.
  635.  
  636.    PRINT "Error code: "; FGetError%(Handle%)
  637.    FError Handle%, 0         ' clear the error code
  638.  
  639. It is recommended that you check for errors after any file
  640. routine is used if there is a chance that your program will be
  641. executed on a floppy disk. These are particularly prone to user
  642. errors (like leaving the drive door open) or running out of
  643. space. If your program will only run on a hard drive, you may
  644. not need to check as frequently. It's your choice. Note that the
  645. error code is not cleared automatically-- use FError to reset
  646. the error code to zero if you determine that it wasn't a serious
  647. error.
  648.  
  649. Down to the nitty-gritty... we've seen how to open and close a
  650. file, how to check operations for errors, and so forth. So how
  651. do we actually manipulate the file? There are assorted
  652. alternatives, depending on how you want to deal with the file:
  653. text reads, text writes, byte-oriented reads and writes, and
  654. block reads and writes, not to mention handling the time, date,
  655. size, and read/write pointer. We'll start off with the routines
  656. which read from a file.
  657.  
  658. If you opened the file for text access, you must want to read
  659. the file a line at a time. Each line is assumed to be less than
  660. 256 characters and delimited by a carriage return and linefeed
  661. (<CR><LF>, or ^M^J, in normal notation). In that case, you
  662. should use the FReadLn$ function:
  663.  
  664.    St$ = FReadLn$(Handle%)
  665.  
  666.                         File Handling                  page 16
  667.  
  668.  
  669.  
  670. A simple program to display a text file directly on the screen
  671. might look something like this in BASIC:
  672.  
  673.    OPEN COMMAND$ FOR INPUT AS #1
  674.    WHILE NOT EOF(1)
  675.       LINE INPUT#1, St$
  676.       PRINT St$
  677.    WEND
  678.    CLOSE #1
  679.  
  680. The same program using BasWiz would look something like this:
  681.  
  682.    REM $INCLUDE: 'BASWIZ.BI'
  683.    FInit 5, ErrCode%
  684.    FOpen COMMAND$, "RT", 0, Handle%, ErrCode%
  685.    WHILE NOT FEOF%(Handle%)
  686.       PRINT FReadLn$(Handle%)
  687.    WEND
  688.    FDone
  689.  
  690. In either case, we're accepting a command-line parameter which
  691. specifies the name of the file. In the BasWiz example, note the
  692. use of the FEOF% function, which tells whether we've gone past
  693. the end of the file. This works like the EOF function in BASIC.
  694.  
  695. There are two ways of reading from binary files. You can get the
  696. results as a string of a specified (maximum) length:
  697.  
  698.    St$ = FRead$(Handle%, Bytes%)
  699.  
  700. In plain BASIC, the same thing might be expressed this way:
  701.  
  702.    St$ = INPUT$(Bytes%, FileNumber%)
  703.  
  704. The other way of reading from a binary file has no equivalent in
  705. BASIC. It allows you to read in up to 32K bytes at a time,
  706. directly into an array or TYPEd variable. You can read the
  707. information into anything that doesn't contain normal strings
  708. (the fixed-length string type can be used, though):
  709.  
  710.    Segm% = VARSEG(Array(0))
  711.    Offs% = VARPTR(Array(0))
  712.    FBlockRead Handle%, Segm%, Offs%, Bytes%
  713.  
  714. That would read the specified number of bytes into Array(),
  715. starting at array element zero.
  716.  
  717.                         File Handling                  page 17
  718.  
  719.  
  720.  
  721. You can use any data type, whether single variable or array, as
  722. long as it is not a variable length string. In other words, Vbl$
  723. and Vbl$(0) would not work. If you want to use a string with the
  724. block read, it must be a fixed-length string. For example:
  725.  
  726.    DIM Vbl AS STRING * 1024
  727.    Segm% = VARSEG(Vbl)
  728.    Offs% = VARPTR(Vbl)
  729.    FBlockRead Handle%, Segm%, Offs%, Bytes%
  730.  
  731. It's a good idea to calculate the Segment and Offset values each
  732. time. These tell FBlockRead where to store the information it
  733. reads. BASIC may move the variables around in memory, so VARSEG
  734. and VARPTR should be used just before FBlockRead to ensure that
  735. they return current information.
  736.  
  737. The file output commands are similar. File output can only be
  738. done if there is no input buffer. This means that you can't use
  739. file output if the file was opened in text mode, either, since
  740. text mode always requires an input buffer. It is possible to do
  741. text output on a file that was opened in binary mode, however.
  742. The limitation just means that you can't open a file for both
  743. reading and writing if you use a buffer (or text mode).
  744.  
  745. To output (write) a string to a file, use this:
  746.  
  747.    FWrite Handle%, St$
  748.  
  749. This is like the plain BASIC statement:
  750.  
  751.    PRINT #FileNumber%, St$;
  752.  
  753. If you would like the string to be terminated by a carriage
  754. return and linefeed, use this instead:
  755.  
  756.    FWriteLn Handle%, St$
  757.  
  758. This is like the plain BASIC statement:
  759.  
  760.    PRINT #FileNumber%, St$
  761.  
  762. In BASIC, the difference between the two writes is controlled by
  763. whether you put a semicolon at the end. With BasWiz, different
  764. routines are used instead. FWrite is like PRINT with a semicolon
  765. and FWriteLn is like PRINT without a semicolon.
  766.  
  767.                         File Handling                  page 18
  768.  
  769.  
  770.  
  771. As well as simple string output, you can also output TYPEd
  772. variables and even entire arrays. This type of output has no
  773. corresponding BASIC instruction, although it's somewhat similar
  774. to the file PUT statement. Up to 32K can be output at a time:
  775.  
  776.    Segm% = VARSEG(Array(0))
  777.    Offs% = VARPTR(Array(0))
  778.    FBlockWrite Handle%, Segm%, Offs%, Bytes%
  779.  
  780. If you haven't already read the section on FBlockRead, go back a
  781. page and review it. The same comments apply for FBlockRead: it
  782. can handle fixed-length strings but not old-style strings, and
  783. VARSEG/VARPTR should immediately precede the block I/O, among
  784. other things.
  785.  
  786. Normally, reads and writes take place sequentially. If you want
  787. to move to a specific spot in the file, though, that's easy. You
  788. can do it in text mode or binary mode, whether or not you have a
  789. buffer, giving you additional flexibility over the usual BASIC
  790. file handling. Set the location for the next read or write, so:
  791.  
  792.    FLocate Handle%, Position&
  793.  
  794. The Position& specified will be where the next read or write
  795. takes place. It starts at one and (since it's specified as a
  796. LONG integer) can go up to however many bytes are in the file.
  797. If you want a record position rather than a byte position, you
  798. can do that too. Just convert the record number to a byte
  799. number, like so:
  800.  
  801.    Position& = (RecordNumber& - 1&) * RecordLength& + 1&
  802.  
  803. If you do not want to maintain RecordNumber and RecordLength as
  804. LONG integers, convert them to such by using the CLNG() function
  805. on them before doing the calculation. Otherwise you may get an
  806. overflow error in the calculation, since QuickBasic will assume
  807. that the result will be an integer.
  808.  
  809. You can get the current position of the file read/write pointer
  810. too:
  811.  
  812.    Position& = FGetLocate&(Handle%)
  813.  
  814. Let's see... we've examined initialization and termination,
  815. opening and closing, reading and writing, and manipulating the
  816. file read/write pointer. What else could there be? Well, how
  817. about checking the size of a file and getting or setting the
  818. file time and date? Why, sure! The "get" routines are pretty
  819. well self-explanatory:
  820.  
  821.    FileSize& = FGetSize&(Handle%)
  822.    FileTime$ = FGetTime$(Handle%)
  823.    FileDate$ = FGetDate$(Handle%)
  824.  
  825.                         File Handling                  page 19
  826.  
  827.  
  828.  
  829. Setting the time and date is equally easy. This should be done
  830. just before you close the file with FClose or FDone. You may use
  831. any date and time delimiters you choose. If a field is left
  832. blank, the appropriate value from the current time or date will
  833. be used. Years may be specified in four-digit or two-digit
  834. format. Two-digit years will be assumed to be in the 20th
  835. century ("90" == "1990"). Careful there! Your program should
  836. allow four-digit dates to be used or disaster will strike when
  837. the year 2000 rolls around. The 21st century is closer than you
  838. think!
  839.  
  840.    FTime Handle%, FileTime$
  841.    FDate Handle%, FileDate$
  842.  
  843. There's just one more file routine. It allows you to "flush" a
  844. file to disk. This insures that the file has been properly
  845. updated to the current point, so nothing will be lost if there
  846. is a power outage or similar problem. If you do not use the
  847. "flush" routine, data may be lost if the program terminates
  848. unexpectedly. Note that use of FFlush requires that a free file
  849. handle be available, before DOS 4.0.
  850.  
  851.    FFlush Handle%
  852.  
  853. That's it for the BasWiz file handler. As a quick review, let's
  854. run through the available routines, then try a couple of example
  855. programs. You might also wish to examine the WDEMO.BAS program,
  856. which also makes use of the file routines.
  857.  
  858. FInit         initialize the file handler
  859. FDone         end the file handler and close any open files
  860.  
  861. FOpen         open a file for access (like OPEN)
  862. FClose        close a file (like CLOSE)
  863.  
  864. FRead$        read a string from a binary file (like INPUT$)
  865. FReadLn$      read a string from a text file (like LINE INPUT)
  866. FBlockRead    read an item from a binary file
  867.  
  868. FWrite        write a string to a binary file
  869. FWriteLn      write a string with a <CR><LF> to a binary file
  870. FBlockWrite   write an item to a binary file
  871.  
  872. FLocate       set the read/write pointer to a given position
  873. FTime         set the time stamp
  874. FDate         set the date stamp
  875. FError        set the error code
  876.  
  877. FGetLocate&   get the read/write pointer
  878. FGetTime$     get the time stamp
  879. FGetDate$     get the date stamp
  880. FGetError     get the error code
  881.  
  882. FFlush        flush to disk (makes sure file is updated)
  883. FGetSize&     get size
  884. FEOF          see if the end of the file has been reached
  885.  
  886.                         File Handling                  page 20
  887.  
  888.  
  889.  
  890. So much for theory. Let's try something practical. A common
  891. problem is copying one file to another. We'll limit this to text
  892. files, so we can do it in both plain BASIC and with BasWiz.
  893. Although BasWiz can handle any type of file readily, BASIC has
  894. problems in efficiently handling variable-length binary files.
  895. So, we'll do this first in BASIC, then BasWiz, for text files.
  896.  
  897. In BASIC, a text-file copying program might look like this:
  898.  
  899.    INPUT "File to copy"; FromFile$
  900.    INPUT "Copy file to"; ToFile$
  901.    OPEN FromFile$ FOR INPUT AS #1
  902.    OPEN ToFile$ FOR OUTPUT AS #2
  903.    WHILE NOT EOF(1)
  904.       LINE INPUT#1, St$
  905.       PRINT#2, St$
  906.    WEND
  907.    CLOSE
  908.  
  909. With BasWiz, the same program would look more like this:
  910.  
  911.    REM $INCLUDE: 'BASWIZ.BI'
  912.    INPUT "File to copy"; FromFile$
  913.    INPUT "Copy file to"; ToFile$
  914.    FInit 15, ErrCode%
  915.    FOpen FromFile$, "RT", 1024, FromHandle%, ErrCode%
  916.    FOpen ToFile$, "CW", 0, ToHandle%, ErrCode%
  917.    FileTime$ = FGetTime$(FromHandle%)
  918.    FileDate$ = FGetDate$(FromHandle%)
  919.    WHILE NOT FEOF%(FromHandle%)
  920.       WriteLn ToHandle%, ReadLn$(FromHandle%)
  921.    WEND
  922.    FTime ToHandle%, FileTime$
  923.    FDate ToHandle%, FileDate$
  924.    FDone
  925.  
  926. You might have noticed that the BasWiz version of the program is
  927. a bit longer than the plain BASIC version. It has a number of
  928. advantages, however. It's faster, produces smaller code under
  929. ordinary circumstances, and preserves the date and time of the
  930. original file in the copied file. Unlike some of the BASIC
  931. compilers, the BasWiz routines do not automatically add a ^Z to
  932. the end of text files, so the BasWiz example will not alter the
  933. original file.
  934.  
  935.                            Fractions                   page 21
  936.  
  937.  
  938.  
  939. Using BCD allows you to represent numbers with excellent
  940. precision, but at a fairly large cost in speed. Another way to
  941. represent numbers with good precision is to use fractions.
  942. Fractions can represent numbers far more accurately than BCD,
  943. but can be handled much more quickly. There are limitations, of
  944. course, but by now you've guessed that's always true!
  945.  
  946. Each fraction is represented by BasWiz as an 8-byte string. The
  947. numerator (top part of the fraction) may be anywhere from
  948. -999,999,999 to 999,999,999. The denominator (the bottom part)
  949. may be from 1 to 999,999,999. This allows handling a fairly wide
  950. range of numbers exactly.
  951.  
  952. Fractions can be converted to or from numeric text strings in
  953. any of three formats: real number (e.g., "1.5"), plain fraction
  954. (e.g., "3/2"), or whole number and fraction (e.g., "1 1/2").
  955. Internally, the numbers are stored as a plain fraction, reduced
  956. to the smallest fraction possible which means the same thing
  957. (for instance, "5/10" will be reduced to "1/2").
  958.  
  959. To convert a numeric text string into a fraction, do this:
  960.  
  961.    Nr$ = FracSet$(NumSt$)
  962.  
  963. To convert a fraction into a numeric text string, try this:
  964.  
  965.    NumSt$ = FracFormat$(Nr$, HowToFormat%)
  966.  
  967. The formatting options are:
  968.  
  969.    0   convert to plain fraction
  970.    1   convert to whole number and fraction
  971.    2   convert to decimal number
  972.  
  973. Here is a list of the other functions available:
  974.  
  975.    Result$ = FracAbs$(Nr$)            ' absolute value
  976.    Result$ = FracAdd$(Nr1$, Nr2$)     ' Nr1 + Nr2
  977.    Result% = FracCompare%(Nr1$, Nr2$) ' compare two fractions
  978.    Result$ = FracDiv$(Nr1$, Nr2$)     ' Nr1 / Nr2
  979.    Result$ = FracMul$(Nr1$, Nr2$)     ' Nr1 * Nr2
  980.    Result$ = FracNeg$(Nr$)            ' - Nr
  981.    Result% = FracSgn%(Nr$)            ' signum function
  982.    Result$ = FracSub$(Nr1$, Nr2$)     ' Nr1 - Nr2
  983.  
  984. Fractions are automatically reduced to allow the greatest
  985. possible range. Note that little range-checking is done at this
  986. point, so you may wish to screen any input to keep it
  987. reasonable.
  988.  
  989.    Result     FracSgn      FracCompare
  990.      -1       negative #    1st < 2nd
  991.       0       # is zero     1st = 2nd
  992.       1       positive #    1st > 2nd
  993.  
  994.                  Graphics: General Routines            page 22
  995.  
  996.  
  997.  
  998. These routines are designed to work with specific graphics
  999. modes, so your program will only include those routines which
  1000. apply to the modes you use. These modes are supported:
  1001.  
  1002.  SCREEN   Card     Graph. Res   Colors   Text Res.       Notes
  1003.  ======   ====     ==========   ======   =============   =====
  1004.     0      any       varies       16     varies            *0
  1005.     1      CGA     320 x 200       4     40 x 25
  1006.     2      CGA     640 x 200       2     80 x 25
  1007.     3      HGA     720 x 348       2     90 x 43           *1
  1008.     7      EGA     320 x 200      16     40 x 25
  1009.     8      EGA     640 x 200      16     80 x 25
  1010.     9      EGA     640 x 350      16     80 x 25/43
  1011.    10      EGA     640 x 350       4     80 x 25/43       mono
  1012.    11      VGA     640 x 480       2     80 x 30/60
  1013.    12      VGA     640 x 480      16     80 x 30/60
  1014.    13      VGA     320 x 200     256     40 x 25
  1015.  --------------------------------------------------------------
  1016.    GV     VESA      <varies>   <varies>  <varies>          *7
  1017.    N0      VGA     360 x 480     256     45 x 30           *2
  1018.    N1      VGA     320 x 400     256     40 x 25           *2
  1019.    N2   <printer>  480 x 640       2     60 x 80/45/40     *3
  1020.    N4      any      80 x  50       2      6 x 10           *4
  1021.    N5     SVGA    <user spec>    256     <varies>          *5
  1022.    N6      MDA      80 x 25      ---     25 x 80           *6
  1023.  
  1024. The number of rows of text available depends on the font size:
  1025. 8 x 8, 8 x 14, or 8 x 16.
  1026.  
  1027. *0  This is actually for text mode, not graphics mode.
  1028.  
  1029. *1  The BasWiz Hercules routines don't need Microsoft's QBHERC
  1030.     TSR to be loaded. This may confuse the BASIC editor. In that
  1031.     case, use BC.EXE to compile the program directly.
  1032.  
  1033. *2  This non-standard VGA mode works on most ordinary VGAs.
  1034.  
  1035. *3  This works with Epson-compatible dot matrix printers and
  1036.     HP-compatible laser printers. The results may be previewed
  1037.     on a VGA. See "Printer Routines".
  1038.  
  1039. *4  This actually provides graphics in text mode for any display
  1040.     adapter. 80x25 text remains available via PRINT.
  1041.  
  1042. *5  This mode provides support for high-resolution 256-color
  1043.     on SuperVGAs based on the popular Tseng ET4000 chips.
  1044.  
  1045. *6  This mode provides support for the monochrome monitor of a
  1046.     dual-monitor system. It works when the mono display is the
  1047.     (theoretically) "inactive" display.
  1048.  
  1049. *7  This mode provides support for the VESA graphics standard,
  1050.     which is common on SuperVGAs. See "VESA Info Routines".
  1051.  
  1052.                  Graphics: General Routines            page 23
  1053.  
  1054.  
  1055.  
  1056. Compatibility: An EGA can display CGA modes. A VGA can display
  1057. EGA and CGA modes. An MCGA can display CGA modes and two VGA
  1058. modes: SCREEN 11 and SCREEN 13. See "Miscellaneous Notes" for
  1059. additional information.
  1060.  
  1061. The routine for a specific mode is indicated by a prefix of "G",
  1062. followed by the mode number, and then the routine name. For
  1063. example, if you wished to plot a point in SCREEN 2 mode, you
  1064. would use:
  1065.  
  1066.    G2Plot X%, Y%
  1067.  
  1068. Many of these routines correspond with existing BASIC
  1069. instructions. However, they are smaller and usually faster by
  1070. 22% - 64%. See "Miscellaneous Notes" for notes on the
  1071. differences between BASIC and the BasWiz routines.
  1072.  
  1073. The smaller size may not be noticeable if you use the SCREEN
  1074. statement, since that causes BASIC to link in some of its own
  1075. graphics routines. If you intend to use only BasWiz routines for
  1076. graphics, you can avoid that by using the G#Mode command instead
  1077. of SCREEN:
  1078.  
  1079.    G#Mode Graphics%         ' 0 for SCREEN 0, else SCREEN #
  1080.  
  1081. If you're using the mode N5 routines, you'll need to initialize
  1082. them before setting the mode. This is done by specifying the
  1083. BIOS mode number and the screen resolution:
  1084.  
  1085.    GN5Init BIOSMode%, PixelsWide%, PixelsHigh%
  1086.  
  1087. The mode GV routines need to be initialized before you set the
  1088. mode and shut down before your program terminates. Also, you
  1089. specify the actual VESA mode number when setting the mode.
  1090.  
  1091.    GGVInit              ' before setting the mode
  1092.    GGVMode VESAmode%    ' to set a VESA mode
  1093.    GGVDone              ' when done using VESA modes
  1094.  
  1095. One difference between BASIC and BasWiz is that, instead of each
  1096. "draw" command requiring a color parameter as in BASIC, the
  1097. BasWiz library provides a separate color command:
  1098.  
  1099.    G#Color Foreground%, Background%
  1100.  
  1101. The "foreground" color is used by all graphics routines. The
  1102. background color is used by the G#Cls routine. Both foreground
  1103. and background colors are used in the G#Write and G#WriteLn
  1104. routines.
  1105.  
  1106.                  Graphics: General Routines            page 24
  1107.  
  1108.  
  1109.  
  1110. Here is a list of the corresponding routines, first BASIC, then
  1111. BasWiz (replace the "#" with the appropriate mode number):
  1112.  
  1113.    ' get the color of a specified point
  1114.    colour% = POINT(x%, y%)
  1115.    colour% = G#GetPel(x%, y%)
  1116.  
  1117.    ' set the color of a specified point
  1118.    PSET (x%, y%), colour%
  1119.    G#Color colour%, backgnd% : G#Plot x%, y%
  1120.  
  1121.    ' draw a line of a specified color
  1122.    LINE (x1%, y1%) - (x2%, y2%), colour%
  1123.    G#Color colour%, backgnd% : G#Line x1%, y1%, x2%, y2%
  1124.  
  1125.    ' draw a box frame of a specified color
  1126.    LINE (x1%, y1%) - (x2%, y2%), colour%, B
  1127.    G#Color colour%, backgnd% : G#Box x1%, y1%, x2%, y2%, 0
  1128.  
  1129.    ' draw a box of a specified color and fill it in
  1130.    LINE (x1%, y1%) - (x2%, y2%), colour%, BF
  1131.    G#Color colour%, backgnd% : G#Box x1%, y1%, x2%, y2%, 1
  1132.  
  1133.    ' clear the screen and home the cursor
  1134.    CLS
  1135.    G#Cls
  1136.  
  1137.    ' get the current cursor position
  1138.    Row% = CSRLIN: Column% = POS(0)
  1139.    G#GetLocate Row%, Column%
  1140.  
  1141.    ' set the current cursor position
  1142.    LOCATE Row%, Column%
  1143.    G#Locate Row%, Column%
  1144.  
  1145.    ' display a string without a carriage return and linefeed
  1146.    PRINT St$;
  1147.    G#Write St$
  1148.  
  1149.    ' display a string with a carriage return and linefeed
  1150.    PRINT St$
  1151.    G#WriteLn St$
  1152.  
  1153. Note that BasWiz, unlike BASIC, allows both foreground and
  1154. background colors for text in graphics mode. It also displays
  1155. text substantially faster than BASIC. See the "Miscellaneous
  1156. Notes" section for information on other differences in text
  1157. printing.
  1158.  
  1159.                  Graphics: General Routines            page 25
  1160.  
  1161.  
  1162.  
  1163. If you need to print a number rather than a string, just use the
  1164. BASIC function STR$ to convert it. If you don't want a leading
  1165. space, use this approach:
  1166.  
  1167.    St$ = LTRIM$(STR$(Number))
  1168.  
  1169. The BasWiz library has other routines which have no BASIC
  1170. equivalent. One allows you to get the current colors:
  1171.  
  1172.    G#GetColor Foreground%, Background%
  1173.  
  1174. Sometimes the normal text services seem unduly limited. Text is
  1175. displayed only at specific character positions, so it may not
  1176. align properly with a graph, for instance. Text is also of only
  1177. one specific size. These are limitations which make the normal
  1178. text routines very fast, but for times when you need something a
  1179. little bit more fancy, try:
  1180.  
  1181.    G#Banner St$, X%, Y%, Xmul%, Ymul%
  1182.  
  1183. You may display the string starting at any graphics position.
  1184. The Xmul% and Ymul% values are multipliers, specifying how many
  1185. times larger than normal each character should be. Use Xmul% = 1
  1186. and Ymul% = 1 for normal-sized characters. What "normal" means
  1187. depends on size of the font in use.
  1188.  
  1189. Since G#Banner "draws" the text onto the screen, it is a bit
  1190. slower than the normal text services. It also uses only the
  1191. foreground color, so the letters go right on top of anything
  1192. that was previously there. Use G#Box to clear the area
  1193. beforehand if this is a problem for you.
  1194.  
  1195. The G#Banner routine supports several fonts. The larger fonts
  1196. provide a more precise character set but leave you with less
  1197. room on the screen. You may choose from these fonts:
  1198.  
  1199.    Font Number     Font Size (width x height)
  1200.         0            8 x 8    --- default
  1201.         1            8 x 14
  1202.         2            8 x 16
  1203.  
  1204. Select a font like so:
  1205.  
  1206.    BFont FontNr%
  1207.  
  1208. If you want to find out what the current font is, can do:
  1209.  
  1210.    FontNr% = GetBFont
  1211.  
  1212. Besides looking more elegant, the larger fonts are easier to
  1213. read. They will also suffer less from being increased in size,
  1214. although some deterioration is inevitable when magnifying these
  1215. kinds of fonts.
  1216.  
  1217.                  Graphics: General Routines            page 26
  1218.  
  1219.  
  1220.  
  1221. The G#Banner routines accept CHR$(0) - CHR$(127). No control
  1222. code interpretation is done. All codes are displayed directly to
  1223. the screen.
  1224.  
  1225. Circles and ellipses can be drawn with the Ellipse routine. This
  1226. is similar to the BASIC CIRCLE statement. You specify the center
  1227. of the ellipse (X,Y), plus the X and Y radius values:
  1228.  
  1229.    G#Ellipse CenterX%, CenterY%, XRadius%, YRadius%
  1230.  
  1231. A circle is an ellipse with a constant radius. So, to draw a
  1232. circle, just set both radius values to the same value.
  1233.  
  1234. As well as the usual points, lines, and ellipses, BasWiz also
  1235. allows you to draw polygons: triangles, squares, pentagons,
  1236. hexagons, all the way up to full circles!
  1237.  
  1238.    G#Polygon X%, Y%, Radius%, Vertices%, Angle!
  1239.  
  1240. The X% and Y% values represent the coordinates of the center of
  1241. the polygon. The Radius% is the radius of the polygon (as if you
  1242. were fitting it into a circle). Vertices% is the number of
  1243. angles (also the number of sides) for the polygon to have.
  1244. Angle! specifies the rotation of the polygon, and is specified
  1245. in radians. See "A Little Geometry" for more information.
  1246.  
  1247. Another routine is designed to manipulate a GET/PUT image. Given
  1248. an image in array Original%() and a blank array of the same
  1249. dimensions called Flipped%(), this routine copies the original
  1250. image to the new array as a mirror image about the horizontal
  1251. axis. This is the same as the image you'd see if you turned your
  1252. monitor upside-down: the resulting image is upside-down and
  1253. backwards.
  1254.  
  1255.    G#MirrorH Original%(), Flipped%()
  1256.  
  1257. Don't forget to make the Flipped%() array the same DIM size as
  1258. the original, or the picture will overflow into main memory,
  1259. probably causing disaster!
  1260.  
  1261. Note that G#MirrorH will only work properly on images with byte
  1262. alignment. This means that the width of the image must be evenly
  1263. divisible by four if SCREEN 1 is used, or evenly divisible by
  1264. eight if SCREEN 2 is used. EGA modes are not yet supported for
  1265. this routine.
  1266.  
  1267. There are more routines that work only with SCREEN 2. One allows
  1268. you to load a MacPaint-type image ("ReadMac" or .MAC files) into
  1269. an array which can then be PUT onto the screen:
  1270.  
  1271.    G2LoadMAC FileName$, Image%(), StartRow%
  1272.  
  1273.                  Graphics: General Routines            page 27
  1274.  
  1275.  
  1276.  
  1277. Note that a full .MAC picture is 576x720, which won't fit on the
  1278. screen, so the image will be truncated to 576x200. You may
  1279. specify a starting row within the .MAC image, StartRow%, which
  1280. may be 0-521, allowing the entire picture to be loaded in
  1281. several parts.
  1282.  
  1283. The Image%() must be dimensioned with 7202 elements:
  1284.  
  1285.    DIM Array(1 TO 7202) AS INTEGER
  1286.  
  1287. If you don't give an extension in the FileName$, an extension of
  1288. ".MAC" will be used. There is no checking to see if the file
  1289. actually exists, so you may wish to do this beforehand.
  1290.  
  1291. There is no way of knowing whether a .MAC picture is supposed to
  1292. be black on white or white on black. If the image doesn't look
  1293. right when you PUT using PSET, you can switch it around by using
  1294. PUT with PRESET instead.
  1295.  
  1296. PC PaintBrush (.PCX) pictures can also be loaded. These images
  1297. can be of various sizes, so you need to dimension a dynamic
  1298. array for them:
  1299.  
  1300.    REM $DYNAMIC
  1301.    DIM Image(1 TO 2) AS INTEGER
  1302.  
  1303. The array will be set to the correct size by the loader. It goes
  1304. like this:
  1305.  
  1306.    G2LoadPCX FileName$, Image%(), ErrCode%
  1307.  
  1308. If you don't give an extension in the FileName$, an extension of
  1309. ".PCX" will be used. You may wish to check to see if the file
  1310. exists beforehand. Possible errors are as follows:
  1311.  
  1312.    -1   File is not in PCX format
  1313.     1   Image is too large for this screen mode
  1314.     2   Image won't work in this screen mode (too many planes)
  1315.  
  1316. Two new routines are replacements for the GET and PUT image
  1317. statements in BASIC. They are on the slow side, but if you don't
  1318. intend to use them for animation, they will serve to save some
  1319. memory. There are also GN5Get and GN5Put routines for use with
  1320. 256-color SuperVGA modes.
  1321.  
  1322.    REM $DYNAMIC
  1323.    DIM Image(1 TO 2) AS INTEGER
  1324.    G2Get X1%, Y1%, X2%, Y2%, Image()
  1325.  
  1326. Note the DIMensioning of a dynamic array. The G2Get routine will
  1327. set the array to the appropriate size to hold the image.
  1328.  
  1329.                  Graphics: General Routines            page 28
  1330.  
  1331.  
  1332.  
  1333. The PUT replacement assumes that you intend to PSET the image.
  1334. It doesn't allow for other display modes yet:
  1335.  
  1336.    G2Put X%, Y%, Image()
  1337.  
  1338. See "Miscellaneous Notes" for more information on using GET/PUT
  1339. images. Note that SCREEN 13 is also supported, via G13Get and
  1340. G13Put.
  1341.  
  1342. Windows 256-color bitmaps may displayed in, or written from, any
  1343. of the VGA or SVGA modes which support 256 colors. This includes
  1344. modes 13, GV, N0, N1, and N5. Since BasWiz file handling is
  1345. employed, you must be sure to initialize the file handler with
  1346. FInit before using these routines and close the file handler
  1347. with FDone before your program terminates. The graphics mode
  1348. must be set beforehand. When reading a BMP, the palette will be
  1349. initialized according to the settings in the .BMP file.
  1350.  
  1351.    G#ShowBMP FileName$, X%, Y%, ErrCode%
  1352.  
  1353.    G#MakeBMP FileName$, X1%, Y1%, X2%, Y2%, ErrCode%
  1354.  
  1355. A bitmap can only be displayed if the screen resolution is
  1356. sufficient to hold the entire image. You can read the .BMP
  1357. information using the following routine:
  1358.  
  1359.    GetInfoBMP FileName$, Wide%, High%, Colors%, ErrCode%
  1360.  
  1361. A positive error code indicates a DOS error code, which is a
  1362. problem in reading the file. Negative error codes are one of the
  1363. following:
  1364.  
  1365.    -1   not a valid .BMP file
  1366.    -2   color format not supported
  1367.    -3   compression type not supported
  1368.    -4   incorrect file size
  1369.    -5   unreasonable image size
  1370.    -6   invalid (X,Y) origin specified for G#ShowBMP
  1371.  
  1372.                  Graphics: General Routines            page 29
  1373.  
  1374.  
  1375.  
  1376. The COLOR statement in SCREEN 1 is anomalous. It doesn't really
  1377. control color at all, which is why QuickBasic proper doesn't
  1378. support colored text in this (or any graphics) mode. Instead, it
  1379. is used for controlling the background/border color and palette.
  1380. Since BasWiz -does- support a true G1COLOR routine, there are
  1381. different routines which allow you to change the palette and
  1382. border colors. To change the background (and border) color, use:
  1383.  
  1384.    G1Border Colour%
  1385.  
  1386. There are two palette routines. Why two? Well, QuickBasic
  1387. supports two CGA palettes. One of the routines works like
  1388. QuickBasic and can be used on any CGA, EGA or VGA display (as
  1389. long as it's in CGA mode). The other routine gives you a wider
  1390. choice of palettes, but will only work on true CGAs (and some
  1391. EGA or VGA systems that have been "locked" into CGA mode).
  1392.  
  1393. Here's the QuickBasic-style two-palette routine for any
  1394. CGA/EGA/VGA:
  1395.  
  1396.    G1PaletteA PaletteNr%
  1397.  
  1398. The PaletteNr% may be as follows:
  1399.  
  1400.    0     (bright) Green, Red, Yellow
  1401.    1     Cyan, Violet, White
  1402.  
  1403.  
  1404. The more flexible six-palette routine (for CGA only) works like
  1405. this:
  1406.  
  1407.    G1PaletteB PaletteNr%
  1408.  
  1409. Palettes are as follows:
  1410.  
  1411.    0  Green, Red, Brown        4  (bright) Green, Red, Yellow
  1412.    1  Cyan, Violet, White      5  (bright) Cyan, Violet, White
  1413.    2  Cyan, Red, White         6  (bright) Cyan, Red, White
  1414.  
  1415.                  Graphics: General Routines            page 30
  1416.  
  1417.  
  1418.  
  1419. The EGA has a number of features which work in all its modes, so
  1420. rather than giving them screen mode prefixes, they are simply
  1421. named with an "E". These routines allow you to get or set the
  1422. palette, get or set the border color, and determine whether the
  1423. higher background colors should be displayed as bright colors or
  1424. as blinking.
  1425.  
  1426. To get a palette color value, use:
  1427.  
  1428.    Colour% = EGetPalette(ColorNumber%)
  1429.  
  1430. To set the color value, use:
  1431.  
  1432.    EPalette ColorNumber%, Colour%
  1433.  
  1434. To get the border color:
  1435.  
  1436.    Colour% = EGetBorder%
  1437.  
  1438. You can probably guess how to set the border color:
  1439.  
  1440.    EBorder Colour%
  1441.  
  1442. Finally, the blink vs. intensity. Actually, this is designed for
  1443. text mode; I'm not sure whether it has any function in graphics
  1444. modes. The text-mode default is for blinking to be turned on.
  1445. With BASIC, you add 16 to the foreground color to make it blink.
  1446. That's a little weird, since the "blink" attribute is actually a
  1447. part of the background color, but that's how BASIC views it. You
  1448. can tell the EGA to turn off blinking, in which case adding 16
  1449. to the foreground color makes the background color intense. This
  1450. doubles the number of available background colors.
  1451.  
  1452.    EBlink Blink%
  1453.  
  1454. Use -1 for blinking (default), or 0 to turn off blinking.
  1455.  
  1456. Like the EGA, the VGA has a number of features which work in all
  1457. its modes. Again, rather than giving them screen mode prefixes,
  1458. we simply name them with a "V". The current routines allow you
  1459. to get or set the palette colors.
  1460.  
  1461. To get a palette color value, use:
  1462.  
  1463.    VGetPalette ColorNumber%, Red%, Green%, Blue%
  1464.  
  1465. To set the color value, use:
  1466.  
  1467.    VPalette ColorNumber%, Red%, Green%, Blue%
  1468.  
  1469.                  Graphics: General Routines            page 31
  1470.  
  1471.  
  1472.  
  1473. As you've probably noticed, this doesn't work the same way as
  1474. the QuickBasic PALETTE statement. Rather than using a formula to
  1475. calculate a single LONG color value, like QuickBasic, the BasWiz
  1476. library allows you to specify the color in a more meaningful
  1477. way. The Red%, Green%, and Blue% parameters each hold an
  1478. intensity value (0-63). By mixing these three, you can get an
  1479. immense variety of shades-- over 250,000 combinations in all.
  1480.  
  1481. If you need to keep track of the intensities in your program,
  1482. I'd suggest the following TYPE definition:
  1483.  
  1484.    TYPE VGAcolor
  1485.       Red AS INTEGER
  1486.       Green AS INTEGER
  1487.       Blue AS INTEGER
  1488.    END TYPE
  1489.  
  1490. If space is more important than speed, you can compress that to
  1491. half the size by using STRING * 1 instead of INTEGER. In that
  1492. case, you will need to use the CHR$ and ASC functions to convert
  1493. between string and integer values.
  1494.  
  1495.                      VESA Info Routines                page 32
  1496.  
  1497.  
  1498.  
  1499. As IBM's influence decreased in the microcomputer world, there
  1500. came to be a great deal of chaos associated with new graphics
  1501. standards-- or, more precisely, the lack thereof. With each
  1502. manufacturer merrily creating a new SVGA interface, it became
  1503. very difficult to find software which actually supported the
  1504. particular SuperVGA you purchased. The exciting capabilities of
  1505. the new adapters, often as not, turned out to be worthless
  1506. advertising promises. Eventually, the VESA graphics standard was
  1507. created in order to help resolve this problem.
  1508.  
  1509. Most SuperVGAs today offer VESA support, either built into the
  1510. adapter's ROM BIOS, or as an optional TSR or driver. BasWiz
  1511. allows you to see if VESA support is available, and if so, what
  1512. video modes may be used. The best approach to using VESA is to
  1513. offer the user a choice of the modes that VESA reports as
  1514. available, since the monitor may well not support all of the
  1515. modes that the adapter is capable of handling.
  1516.  
  1517. Quite honestly, VESA is not much of a standard. It offers the
  1518. barest minimum needed to access the capabilities of a display,
  1519. and not always even that. The standard allows the manufacturer
  1520. to leave out normal BIOS support for extended video modes,
  1521. though it does recommend that they be supported. BasWiz expects
  1522. a bit more than such sketchy minimalist compliance with VESA.
  1523. Most importantly, BasWiz must be able to access the display
  1524. through the normal BIOS routines, in conjunction with
  1525. appropriate VESA mode handling. If you believe your SVGA
  1526. provides VESA support, but the routines to get mode information
  1527. do not return any valid modes, perhaps your implementation of
  1528. VESA lacks this key feature. Check with the manufacturer. Among
  1529. the things VESA doesn't support, by the way, is modes with more
  1530. than 256 colors-- so BasWiz can't either. Sorry.
  1531.  
  1532. One thing the VESA standard does provide is a great deal of
  1533. information about the available video modes-- their mode
  1534. numbers, graphics and text resolutions, number of colors, and so
  1535. forth. You may access this information through BasWiz whether or
  1536. not you intend to actually use VESA graphics in your program--
  1537. the routines do not need GGVInit to be initialized. Note that
  1538. VESA supports both extended graphics and text modes. The BasWiz
  1539. VESA routines currently support only VESA graphics, which is
  1540. referred to as mode GV. VESA text modes, when implemented, will
  1541. become mode TV. As the VESA information routines do not
  1542. specifically refer to either a text or graphics mode, there is
  1543. no mode number; instead, the routines simply have a prefix of
  1544. VESA.
  1545.  
  1546. The key routine for all of this allows you to see whether VESA
  1547. support is available, and which version of VESA:
  1548.  
  1549.    VesaVersion MajorV%, MinorV%
  1550.  
  1551. The MajorV% value is the major version number, and MinorV% the
  1552. minor version number. If VESA support is not available, both of
  1553. these numbers will be zero.
  1554.  
  1555.                      VESA Info Routines                page 33
  1556.  
  1557.  
  1558.  
  1559. Since VESA is intended to support a wide variety of video modes,
  1560. the mode numbers and specifications are not built into the
  1561. standard as such. Instead, they are part of the VESA driver
  1562. which supports your particular video card. You can ask the
  1563. driver which modes are available and what they are like. BasWiz
  1564. treats this in a way similar to the way DOS lets you seek for
  1565. files: with a "find first" request to initialize the routines
  1566. and search for the first item, followed by any number of "find
  1567. next" requests to find subsequent items.
  1568.  
  1569. The "find first" and "find next" routines return a mode number.
  1570. If the mode number is -1 (negative one), you have reached the
  1571. end of the mode list. Otherwise, it's a video mode number, and
  1572. you can get additional information about the mode with an
  1573. assortment of other functions. This is pretty straightforward,
  1574. so I won't go into detail here. See VESAINFO.BAS for an example
  1575. program which uses all of these functions to tell you about all
  1576. available VESA modes on your display.
  1577.  
  1578.    VMode% = VesaFindFirst%        ' find first mode
  1579.    VMode% = VesaFindNext%         ' find subsequent mode
  1580.  
  1581. These provide results in pixels for graphics modes, or in
  1582. characters for text modes. Note that BasWiz does not yet support
  1583. use of VESA text modes.
  1584.  
  1585.    XSize% = VesaScrWidth%         ' screen width
  1586.    YSize% = VesaScrHeight%        ' screen height
  1587.  
  1588. These describe the size of the character matrix in pixels (use
  1589. for figuring text rows & cols in graphics modes). Note that
  1590. BasWiz requires VesaChrWidth% = 8 for printing, which is the
  1591. usual setting in graphics modes.
  1592.  
  1593.    ChWidth% = VesaChrWidth%       ' character width
  1594.    ChHeight% = VesaChrHeight%     ' character height
  1595.  
  1596. BasWiz supports no more than 256 colors, as neither VESA nor the
  1597. standard BIOS functions were designed for more. If someone can
  1598. send me info on how to handle more colors, I'll see what I can
  1599. do.
  1600.  
  1601.    Colors& = VesaColors&          ' number of colors
  1602.  
  1603. These two return boolean values: -1 if true, 0 if false.
  1604.  
  1605.    Mono% = VesaIsMono%            ' if mode is monochrome
  1606.    Text% = VesaIsText%            ' if it's a text mode
  1607.  
  1608. Scrolling in VESA modes is extremely slow. Avoid if possible.
  1609.  
  1610.                 Graphics: Text-mode Routines           page 34
  1611.  
  1612.  
  1613.  
  1614. It may seem odd to lump text-mode handling in with graphics
  1615. mode. It seemed like the most logical approach, however. There
  1616. is certainly some value in having graphics-type capabilities for
  1617. text mode. The ability to draw lines and boxes, use banner-style
  1618. text, and so forth can be handy. So, for the folks who don't
  1619. need all the power of the virtual windowing system, I've added
  1620. text-mode support into the "graphics" routines.
  1621.  
  1622. There are some quirks to these routines, since text mode doesn't
  1623. work the same way as graphics mode. For one thing, each "pixel"
  1624. is actually an entire character. The default pixel is a solid
  1625. block character, CHR$(219). You can change this, however:
  1626.  
  1627.    G0SetBlock Ch%      ' set ASCII code (use ASC(Ch$))
  1628.    Ch% = G0GetBlock%   ' get ASCII code
  1629.  
  1630. Since a pixel consists of a character with both foreground and
  1631. background colors, the "get pixel" routine has been altered to
  1632. accordingly:
  1633.  
  1634.    G0GetPel X%, Y%, Ch%, Fore%, Back%
  1635.  
  1636. Finally, let's consider the "set mode" command. If you pass it a
  1637. zero, the current mode will be used as-is. This is useful in
  1638. case you've already set up a desired mode.
  1639.  
  1640. Any other mode number will be assumed to be a BIOS video mode
  1641. which should be set. If you feel like initializing the screen
  1642. mode for some reason, it may be useful to know that 3 is the
  1643. normal color mode (for CGA, EGA, VGA, etc) and 7 is the normal
  1644. mono mode (for MDA and Hercules). These provide 80x25 text. If
  1645. you wish to take advantage of 43-row EGA or 50-row VGA text
  1646. modes, you must set them up in advance (using the BASIC
  1647. statements SCREEN and WIDTH) before calling G0Mode with a zero.
  1648.  
  1649. If you have a SuperVGA or other adapter which supports unusual
  1650. text modes, you can use the mode command to switch to the
  1651. appropriate mode. On my Boca SuperVGA, for example, mode &H26
  1652. provides 80x60 text, and mode &H22 provides 132x44. The G0
  1653. routines are designed to support any text resolution up to
  1654. 255x255, provided that the video BIOS properly updates the
  1655. appropriate memory locations when a mode set is done. This
  1656. should be true for any special EGA or VGA-based text modes.
  1657.  
  1658.    G0Mode ModeNr%
  1659.  
  1660.               Graphics: Dual Monitor Routines          page 35
  1661.  
  1662.  
  1663.  
  1664. The N6 mode support dual monitors. To use this mode, you must
  1665. make the color monitor the "active" display, so it can be
  1666. handled with the usual BASIC or BasWiz display routines. The N6
  1667. routines are designed to work with a monochrome monitor only
  1668. when it is the (supposedly) "inactive" display in a dual monitor
  1669. system. The normal BASIC, BIOS, and DOS routines are designed
  1670. with the idea that only one monitor is active at any given time,
  1671. so we let them think what they like, and bypass 'em with a bit
  1672. of fancy footwork.
  1673.  
  1674. These routines are designed for monochrome adapters. Either a
  1675. plain MDA or a Hercules mono graphics adapter will do.
  1676.  
  1677. The notes on SetBlock and GetPel from the explanation of mode 0
  1678. on the previous page apply. In this case, of course, they're
  1679. GN6SetBlock and GN6GetPel, but the functionality is the same.
  1680.  
  1681. In addition, there are two new routines which allow you to get
  1682. and set the cursor size. The cursor size is defined in scan
  1683. lines, which may range from 0 (invisible) to 11 (large block):
  1684.  
  1685.    GN6CursorSize ScanLines%
  1686.    ScanLines% = GN6GetCursorSize%
  1687.  
  1688.                  Graphics: Printer Routines            page 36
  1689.  
  1690.  
  1691.  
  1692. The BasWiz printer routines allow you to work with a printer
  1693. using the same convenient methods you'd use on a screen. The
  1694. image is created with the usual G# routines (using mode N2), but
  1695. the results are kept in a buffer in memory (about 37K bytes)
  1696. rather than being displayed directly. The image can be previewed
  1697. on a VGA or printed out at your convenience, to any printer or
  1698. even a file. The results will take up a single printer page,
  1699. assuming the usual 8.5" x 11" paper is used.
  1700.  
  1701. Printing a finished page works like this:
  1702.  
  1703.    GN2Print Device$     ' for Epson-type dot matrix printers
  1704.    GN2PrintL Device$    ' for HP-type laser printers
  1705.  
  1706. The Device$ variable should be set to the name of the device:
  1707.  
  1708.    LPT1    parallel printer on port 1   (PRN also works)
  1709.    LPT2    parallel printer on port 2
  1710.    LPT3    parallel printer on port 3
  1711.    COM1    serial printer on port 1     (AUX also works)
  1712.    COM2    serial printer on port 2
  1713.  
  1714. Instead of using a device name, you can also use a file name, to
  1715. store the results for later printing. Output is done using BASIC
  1716. file handling, so it would be a good idea to provide an ON ERROR
  1717. GOTO trap in case of problems. The FREEFILE function is used, so
  1718. you don't have to worry about conflicts with any file numbers
  1719. which may be in use by your program.
  1720.  
  1721. Getting a page layout just right can consume a lot of paper.
  1722. Fortunately, there's a "preview" routine that allows you to
  1723. display the results on a VGA. The display will be sideways,
  1724. allowing the whole page to be seen at once. This will exactly
  1725. match the printed output in N2 mode. Here's how it works:
  1726.  
  1727.    G11Mode 1               ' set SCREEN 11 (VGA 640x480 x2)
  1728.    GN2Display              ' display the page
  1729.    DO                      ' wait for a key to be pressed
  1730.    LOOP UNTIL LEN(INKEY$)  '
  1731.    G11Mode 0               ' set SCREEN 0 (text mode)
  1732.  
  1733. The GN2Write and GN2WriteLn printer routines are unlike the
  1734. display versions of the same routines in that they don't scroll.
  1735. These routines only handle one page at a time.
  1736.  
  1737. Before using GN2Write or GN2WriteLn routines, you must choose a
  1738. font with GN2Font. These are the same fonts as used with
  1739. G#Banner:
  1740.  
  1741.     0     8 x 8        80 text rows
  1742.     1     8 x 14       45 text rows
  1743.     2     8 x 16       40 text rows
  1744.  
  1745. The current font can be retrieved with GN2GetFont%. The result
  1746. will be meaningless if the font was never set with GN2Font.
  1747.  
  1748.                  Graphics: A Little Geometry           page 37
  1749.  
  1750.  
  1751.  
  1752. The increasing capabilities of computer graphics systems has
  1753. left many of us in the dust. It's great to be able to run
  1754. dazzling applications or to doodle with a "paint" program, but
  1755. many of us find it difficult to design appealing images of our
  1756. own. Becoming an artist is perhaps a bit more than most of us
  1757. are willing to take on! It is important to remember, however,
  1758. that computers are wonderful number-crunchers. With a little
  1759. application of plane geometry, you can have the computer take on
  1760. much of the work for you-- and after all, isn't that why we have
  1761. computers in the first place?
  1762.  
  1763. A complete review of plane geometry is a bit beyond the scope of
  1764. this text. However, I'm going to run through some of the things
  1765. I think you'll find most useful. I'd also like to suggest that
  1766. you might dig out your old textbooks or rummage through your
  1767. local used book store. It may have seemed like a dry subject at
  1768. the time, but when you can watch the results growing on your
  1769. computer screen, you will have a much better idea of how
  1770. geometry can be useful to you-- and it can be surprisingly fun,
  1771. too!
  1772.  
  1773. In geometry talk, a "point" doesn't have any actual size. In our
  1774. case, we want to apply geometry to physical reality, namely the
  1775. computer screen. As far as we're concerned, a "point" will be an
  1776. individual graphics dot, also called a "pel" or "pixel" (for
  1777. "picture element"). We can safely dispense with such formalities
  1778. for our applications, for the most part.
  1779.  
  1780. The most important thing about a point is that it has a
  1781. location! Ok, that may not seem staggering, but it happens that
  1782. there are a number of ways of specifying that location. The most
  1783. common method is called the Cartesian coordinate system. It is
  1784. based on a pair of numbers: X, which represents the distance
  1785. along a horizontal line, and Y, which represents the distance
  1786. along a vertical line. Consider the CGA in SCREEN 2, for
  1787. instance. It has a coordinate system where X can be 0 - 639 and
  1788. Y can be 0 - 199. The points are mapped on kind of an invisible
  1789. grid.
  1790.  
  1791. The Cartesian coordinate system makes it easy to visualize how a
  1792. given point relates to other points on the same plane (or
  1793. screen). It is particularly useful for drawing lines. Horizontal
  1794. and vertical lines become a cinch: just change the X value to
  1795. draw horizontally, or the Y value to draw vertically. Squares
  1796. and rectangles (or boxes) can be formed by a combination of such
  1797. lines. You can define an area of the screen in terms of an
  1798. imaginary box (as GET and PUT do) with nice, clean boundaries.
  1799. When we get to diagonal lines, it's a bit more of a nuisance,
  1800. but still easy enough with the proper formula. That means we can
  1801. do triangles too. Curves are worse... when it comes to even a
  1802. simple circle or ellipse, the calculations start to get on the
  1803. messy side. For things like that, though, there is an
  1804. alternative.
  1805.  
  1806.                  Graphics: A Little Geometry           page 38
  1807.  
  1808.  
  1809.  
  1810. Another way of describing the location of a point is by Polar
  1811. coordinates. In Cartesian coordinates, the location is specified
  1812. by its horizontal and vertical distances from the "origin" or
  1813. reference point, (0,0). In Polar coordinates, the location is
  1814. specified by its distance and angle from the origin. Think of it
  1815. as following a map: Cartesian coordinates tell you how many
  1816. blocks down and how many blocks over the point is, whereas Polar
  1817. coordinates tell you in which direction the point is and how far
  1818. away it is "as the crow flies".
  1819.  
  1820. The Polar coordinate system is great for describing many kinds
  1821. of curves, much better than Cartesian. For example, a circle is
  1822. defined as all of the points at a given (fixed) distance from a
  1823. center point. Polar coordinates include both a distance and an
  1824. angle, and we've already got the distance, so all we need to do
  1825. is plot points at all of the angles on a circle. Technically,
  1826. there is an infinite number of angles, but since our points
  1827. don't follow the mathematical definition (they have a size), we
  1828. don't have to worry about that.
  1829.  
  1830. Let me digress for a moment to talk about angles. In BASIC,
  1831. angles are specified in "radians". People more often use
  1832. "degrees". Fortunately, it isn't hard to convert from one to the
  1833. other. Both may be visualized on a circle. In radians, the sum
  1834. of the angles in a circle is twice pi. In degrees, the sum of
  1835. the angles is 360. That's something like this:
  1836.  
  1837.  
  1838.                  90 deg, 1/2 * pi rad
  1839.                        /---|---\
  1840.                       /    |    \
  1841.                      /     |     \
  1842.        180 degrees   |___  .  ___|    0 deg, 0 rad; or...
  1843.        pi radians    |           |  360 deg, 2 * pi rad
  1844.                      \     |     /
  1845.                       \    |    /
  1846.                        \---|---/
  1847.                  270 deg, 3/2 * pi rad
  1848.  
  1849.  
  1850. Ok, so that's a grotesquely ugly circle! Hopefully it shows the
  1851. important thing, though. Angles start at zero on the extreme
  1852. right and get larger as they work around counter-clockwise. The
  1853. places marked on the "circle" are places where lines drawn
  1854. horizontally and vertically through the center intersect the
  1855. outside of the circle. These serve as a useful reference point,
  1856. especially in that they help show how the angles can be
  1857. construed from a Cartesian viewpoint.
  1858.  
  1859. So much for angles. I'll go into conversion formulae, the value
  1860. of pi, and other good junk a bit later on. Right now, let's get
  1861. back to our discussion of Polar coordinates.
  1862.  
  1863.                  Graphics: A Little Geometry           page 39
  1864.  
  1865.  
  1866.  
  1867. I've explained how the Polar system makes it easy to draw a
  1868. circle. Since you can vary the range of angles, it's equally
  1869. simple to draw an arc. If you wanted to make a pie chart, you
  1870. might want to join the ends of the arcs to the center of the
  1871. circle, in which case you'd keep the angle constant (at the ends
  1872. of the arc) and plot by changing the distance from zero to the
  1873. radius. Circles are also handy for drawing equilateral
  1874. polygons... you know, shapes with sides of equal length:
  1875. triangle, square, pentagon, hexagon, etc. In this case, the best
  1876. features of the Cartesian and Polar systems can be joined to
  1877. accomplish something that would be difficult in either alone.
  1878.  
  1879. The starting point for these polygons is the circle. Imagine
  1880. that the polygon is inside a circle, with the vertices (pointy
  1881. ends, that is, wherever the sides meet) touching the edge of the
  1882. circle. These are equilateral polygons, so all of the sides and
  1883. angles are the same size. Each of the vertices touches the
  1884. circle, and each does it at exactly the same distance from each
  1885. other along the arc of the circle. All of this detail isn't
  1886. precisely necessary, but I hope it makes the reasoning a bit
  1887. more clear!
  1888.  
  1889. The circle can be considered as being divided by the polygon
  1890. into a number of arcs that corresponds to the number of vertices
  1891. (and sides) the polygon has. Think of a triangle inside a
  1892. circle, with the tips all touching the circle. If you ignore the
  1893. area inside the triangle, you will see that the circle is
  1894. divided into three equal arcs. The same property is true of any
  1895. equilateral polygon. As a matter of fact, as the number of
  1896. vertices goes up, the circle is partitioned into more, but
  1897. smaller, arcs... so that a polygon with a large enough number of
  1898. vertices is effectively a circle itself!
  1899.  
  1900. Anyway, the important thing is the equal partitioning. We know
  1901. how many angles, be they degrees or radians, are in a circle. To
  1902. get the points of a polygon, then... well, we already know the
  1903. "distance" part: that's the same as the radius. The angles can
  1904. be calculated by dividing the angles in the whole circle by the
  1905. number of vertices in the desired polygon. Trying that case with
  1906. the triangle, assuming a radius of 20 (why not), and measuring
  1907. in degrees, that would give us the Polar points (20, 0), (20,
  1908. 120), (20, 240). To make this a triangle, we need to connect the
  1909. points using lines, which is easy in Cartesian coordinates.
  1910. Since the computer likes Cartesian anyway, we just convert the
  1911. Polar coordinates to Cartesian, draw the lines, and viola!
  1912.  
  1913.                  Graphics: A Little Geometry           page 40
  1914.  
  1915.  
  1916.  
  1917. That's essentially the method used by the G#Polygon routines.
  1918. It's very simple in practice, but I haven't seen it elsewhere...
  1919. probably because people forget about the Polar coordinate
  1920. system, which is what makes it all come together. Polar
  1921. coordinates also have simple equations for figures that look
  1922. like daisies, hearts, and other unusual things. See "Equations,
  1923. Etc" and ROSES.BAS for more information.
  1924.  
  1925. On a side note, the Cartesian system isn't used by all
  1926. computers, although it's the most common. Cartesian coordinates
  1927. are the standard for what is called "raster" displays. The Polar
  1928. coordinate system is used on "vector" displays. One example of a
  1929. vector display that you may have seen is the old Asteroids video
  1930. arcade game. Vector displays tend to be used for drawing
  1931. "framework" pictures where the image must be very sharp (unlike
  1932. in raster images, the diagonal lines aren't jagged, since
  1933. there's no raster "grid").
  1934.  
  1935. In this section, I'm going to list a number of equations and so
  1936. forth. Some of them will be useful to you in experimenting with
  1937. Polar coordinates. Some of them provide formulae for things that
  1938. are already in BasWiz, but which you might like to understand
  1939. better. Some of them are just for the heck of it... note that
  1940. not all of this information may be complete enough for you to
  1941. just use without understanding it.
  1942.  
  1943. One problem is... if you try to draw a circle, for instance, it
  1944. will come out looking squashed in most SCREEN modes. Remember we
  1945. said our points, unlike mathematical points, have a size? In
  1946. most graphics modes, the points are effectively wider than they
  1947. are high, so a real circle looks like an ellipse.
  1948.  
  1949. Another problem is that these equations are based on an origin
  1950. of (0,0) which is assumed to be at the center of the plane. In
  1951. our case, (0,0) is at the upper right edge, which also makes the
  1952. Y axis (vertical values) effectively upside-down. This isn't
  1953. necessarily a problem, but sometimes it is! Adding appropriate
  1954. offsets to the plotted X and Y coordinates often fixes it. In
  1955. the case of Y, you may need to subtract the value from the
  1956. maximum Y value to make it appear right-side-up.
  1957.  
  1958. The displayed form of these equations may contain "holes",
  1959. usually again because the points have a size, and/or since we
  1960. try to use integer math to speed things up. If the screen had
  1961. infinite resolution, this would not be a problem... meanwhile
  1962. (!), getting around such problems takes fiddlin'.
  1963.  
  1964.                   Graphics: Equations, Etc             page 41
  1965.  
  1966.  
  1967.  
  1968. There are other problems, mostly due to forcing these
  1969. simplified-universe theoretical equations into practical use.
  1970. It's a lot easier to shoehorn in these simple equations than to
  1971. use more accurate mathematical descriptions, though... a -lot-
  1972. easier. So a few minor quirks can be ignored!
  1973.  
  1974. With those disclaimers, here's the scoop on some handy
  1975. equations.
  1976.  
  1977.    Polar coordinates may be expressed as (R, A), where R is
  1978.    radius or distance from the origin, and A is the angle.
  1979.  
  1980.    Cartesian coordinates may be expressed as (X, Y), where X is
  1981.    the distance along the horizontal axis and Y is the distance
  1982.    along the vertical axis.
  1983.  
  1984.    Polar coordinates can be converted to Cartesian coordinates
  1985.    like so:
  1986.       X = R * COS(A): Y = R * SIN(A)
  1987.  
  1988.    Angles may be expressed in radians or degrees. BASIC prefers
  1989.    radians. Radians are based on PI, with 2 * PI radians in a
  1990.    circle. There are 360 degrees in a circle. Angles increase
  1991.    counter-clockwise from a 3:00 clock position, which is the
  1992.    starting (zero) angle. Angles can wrap around: 720 degrees is
  1993.    the same as 360 degrees or 0 degrees, just as 3:00 am is at
  1994.    the same clock position as 3:00 pm.
  1995.  
  1996.    Angles may be converted between degrees and radians, so:
  1997.       radians = degrees * PI / 180
  1998.       degrees = radians * 180 / PI
  1999.  
  2000.    The value PI is approximately 3.14159265358979. For most
  2001.    graphics purposes, a simple 3.141593 should do quite nicely.
  2002.    The true value of PI is an irrational number (the decimal
  2003.    part repeats forever, as near as anyone can tell). It has
  2004.    been calculated out to millions of decimal points by people
  2005.    with a scientific bent (and/or nothing better to do)!
  2006.  
  2007.                   Graphics: Equations, Etc             page 42
  2008.  
  2009.  
  2010.  
  2011. Line Drawing:
  2012.  
  2013.    One of the convenient ways of expressing the formula of a
  2014.    line (Cartesian coordinates) is:
  2015.       Y = M * X + B
  2016.  
  2017.    Given the starting and ending points for the line, M (the
  2018.    slope, essentially meaning the gradient, of the line) can be
  2019.    determined by:
  2020.       M = (Y2 - Y1) / (X2 - X1)
  2021.  
  2022.    The B value is called the Y-intercept, and indicates where
  2023.    the line intersects with the Y-axis. Given the ingredients
  2024.    above, you can calculate that as:
  2025.       B = Y1 - M * X1
  2026.  
  2027.    With this much figured out, you can use the original formula
  2028.    to calculate the appropriate Y values, given a FOR X = X1 TO
  2029.    X2 sort of arrangement. If the slope is steep, however, this
  2030.    will result in holes in the line. In that case, it will be
  2031.    smoother to recalculate the formula in terms of the X value
  2032.    and run along FOR Y = Y1 TO Y2... in that case, restate it
  2033.    as:
  2034.       X = (Y - B) / M
  2035.  
  2036.    Keep an eye on conditions where X1 = X2 or Y1 = Y2! In those
  2037.    cases, you've got a vertical or horizontal line. Implement
  2038.    those cases by simple loops to improve speed and to avoid
  2039.    dividing by zero.
  2040.  
  2041.  
  2042.  
  2043. Circle Drawing:
  2044.  
  2045.    The Cartesian formula gets messy, especially due to certain
  2046.    aspects of the display that are not accounted for (mainly
  2047.    that pixels, unlike theoretical points, have a size and shape
  2048.    which is usually rectangular). The Polar formula is trivial,
  2049.    though. The radius should be specified to the circle routine,
  2050.    along with the center point. Do a FOR ANGLE! = 0 TO 2 * PI!
  2051.    STEP 0.5, converting the resulting (Radius, Angle)
  2052.    coordinates to Cartesian, then adding the center (X,Y) as an
  2053.    offset to the result. The appropriate STEP value for the loop
  2054.    may be determined by trial and error. Smaller values make
  2055.    better circles but take more time. Larger values may leave
  2056.    "holes" in the circle.
  2057.  
  2058.                   Graphics: Equations, Etc             page 43
  2059.  
  2060.  
  2061.  
  2062. Spiral Drawing:
  2063.  
  2064.    If you use Polar coordinates, this is easy. Just treat it
  2065.    like a circle, but decrease the radius as you go along to
  2066.    spiral in... or increase the radius as you go along if you
  2067.    prefer to spiral out.
  2068.  
  2069.  
  2070.  
  2071. Polygon Drawing:
  2072.  
  2073.    I've already discussed that, so I'll leave it as an
  2074.    exercise-- or, just see the BasWiz source code! The polygon
  2075.    routines are primarily written in BASIC.
  2076.  
  2077.  
  2078.  
  2079. Flower Drawing:
  2080.  
  2081.    This sort of thing would be rather difficult to do using
  2082.    strictly Cartesian methods, but with Polar coordinates, no
  2083.    problem. Here we calculate the radius based on the angle,
  2084.    using something like:
  2085.  
  2086.       FOR Angle! = 0 TO PI! * 2 STEP .01
  2087.  
  2088.    (a low STEP value is a good idea). The radius is calculated
  2089.    like so:
  2090.  
  2091.       Radius! = TotalRadius! * COS(Petals! * Angle!)
  2092.  
  2093.    The Petals! value specifies how many petals the flower should
  2094.    have. If it is odd, the exact number of petals will be
  2095.    generated; if even, twice that number will be generated.
  2096.  
  2097.    These figures are technically called "roses", although they
  2098.    more resemble daisies. Try the ROSES.BAS program to see how
  2099.    they look.
  2100.  
  2101.  
  2102.  
  2103. Other Drawing:
  2104.  
  2105.    Experiment! There are all sorts of interesting things you can
  2106.    do with the Polar coordinate system in particular. Dig up
  2107.    those old Geometry texts or see if your Calculus books review
  2108.    it. If you've kept well away from math, try your local
  2109.    library or used book store.
  2110.  
  2111.                Memory Management and Pointers          page 44
  2112.  
  2113.  
  2114.  
  2115. On the whole, BASIC is easily a match for any other language, as
  2116. far as general-purpose programming goes. There is one major
  2117. lack, however-- a set of valuable features that is supported by
  2118. most other languages, but was inexplicably left out of BASIC.
  2119. Perhaps Microsoft felt it was too advanced and dangerous for a
  2120. so-called "beginner's" language. In truth, using pointers and
  2121. memory management takes a little understanding of what you're
  2122. doing-- the compiler can't protect you from all of your
  2123. mistakes. However, they can be extraordinarily useful for many
  2124. things, so I have added these capabilities to BasWiz.
  2125.  
  2126. A "pointer" is essentially just the address of an item. It is
  2127. useful in two respects: it allows you to pass just the pointer,
  2128. rather than the whole item (be it a TYPEd variable, normal
  2129. variable, entire array, or whatever) to a subprogram. This is
  2130. faster and more memory-efficient than the alternatives.
  2131. Secondly, a pointer combined with memory management allows you
  2132. to allocate and deallocate memory "on the fly", in just the
  2133. amount you need. You don't have to worry about DIMensioning an
  2134. array too large or too small, or even with how large each
  2135. element of the array should be, for example. You can determine
  2136. that when your program -runs-, rather than at compile time, and
  2137. set up your data structures accordingly. You can also create a
  2138. large variety of data structures, such as trees and linked
  2139. lists, which would be difficult and cumbersome to emulate using
  2140. BASIC alone.
  2141.  
  2142. The BasWiz memory/pointer routines allow you to allocate and
  2143. deallocate memory; fill, copy or move a block of memory; get or
  2144. put a single character according to a pointer; and convert back
  2145. and forth between a segment/offset address and a pointer.
  2146.  
  2147. Pointers are kept in LONG integers, using an absolute memory
  2148. addressing scheme. This means that you can manipulate pointers
  2149. just like any ordinary LONG integer, e.g. to move to the next
  2150. memory address, just add one. Since you can convert from a
  2151. segment/offset address to a pointer and you can copy information
  2152. from one pointer to another, you can move information back and
  2153. forth between allocated memory and a TYPEd variable, numeric
  2154. variable, or array. You can even do things like set a pointer to
  2155. screen memory and transfer the screen into a variable or vice
  2156. versa! Or implement your own "far string" routines, hierarchical
  2157. evaluations, or any number of other things. Pointers are
  2158. incredibly powerful!
  2159.  
  2160.                Memory Management and Pointers          page 45
  2161.  
  2162.  
  2163.  
  2164. Note that there are different ways of representing the same
  2165. segment/offset address, but only one absolute pointer
  2166. representation. If you need to compare two addresses, using
  2167. pointers is terrific. However, it's good to keep in mind that an
  2168. segment/offset address may -appear- to change if you convert it
  2169. to a pointer and then back to a segment/offset address. When you
  2170. convert from a pointer to a segment and offset, the segment will
  2171. be maximized and the offset will be minimized. So, for example,
  2172. 0040:001C will turn into 0041:000C.
  2173.  
  2174. Although the byte count for these routines is handled through a
  2175. LONG integer, the routines handle a maximum of 65,520 bytes at a
  2176. time. In other words, a pointer can only access a bit less than
  2177. 64K at a time. If I get enough requests to extend this range, I
  2178. will do so. Meantime, that's the limit!
  2179.  
  2180. There are two routines which take care of memory management.
  2181. These allow you to allocate or deallocate memory. Note that if
  2182. you allocate too much memory, QuickBasic won't have any memory
  2183. to work with! Use the BASIC function "SETMEM" to see how much
  2184. memory is available before going hog-wild.
  2185.  
  2186. You can allocate memory like so:
  2187.  
  2188.    MAllocate Bytes&, Ptr&, ErrCode%
  2189.  
  2190. If there isn't enough memory available, an error code will be
  2191. returned. Otherwise, Ptr& will point to the allocated memory.
  2192. Memory is allocated in chunks of 16 bytes, so there may be some
  2193. memory wasted if you choose a number of bytes that isn't evenly
  2194. divisible by 16.
  2195.  
  2196. When you are finished with that memory, you can free it up by
  2197. deallocation:
  2198.  
  2199.    MDeallocate Ptr&, ErrCode%
  2200.  
  2201. An error code will be returned if Ptr& doesn't point to
  2202. previously allocated memory.
  2203.  
  2204. In the best of all possible worlds, there would be a third
  2205. routine which would allow you to reallocate or resize a block of
  2206. memory. However, due to certain peculiarities of QuickBasic, I
  2207. was unable to implement that. You can simulate such a thing by
  2208. allocating a new area of memory of the desired size, moving an
  2209. appropriate amount of information from the old block to the new,
  2210. and finally deallocating the old block.
  2211.  
  2212.                Memory Management and Pointers          page 46
  2213.  
  2214.  
  2215.  
  2216. Once you've allocated memory, you can move any sort of
  2217. information in or out of it except normal strings-- fixed-length
  2218. strings, TYPEd values, arrays, or numeric values. To do that,
  2219. you use BASIC's VARSEG and VARPTR functions on the variable.
  2220. Convert the resulting segment/offset address to a pointer:
  2221.  
  2222.    TSeg% = VARSEG(Variable)
  2223.    TOfs% = VARPTR(Variable)
  2224.    VariablePtr& = MJoinPtr&(TSeg%, TOfs%)
  2225.  
  2226. Moving the information from one pointer to another is like so:
  2227.  
  2228.    MMove FromPtr&, ToPtr&, Bytes&
  2229.  
  2230. For STRING or TYPEd values, you can get the number of bytes via
  2231. the LEN function. For numeric values, the following applies:
  2232.  
  2233.    Type       Bytes per value
  2234.    =======    ===============
  2235.    INTEGER           2
  2236.    LONG              4
  2237.    SINGLE            4
  2238.    DOUBLE            8
  2239.  
  2240. The "memory move" (MMove) routine is good for more than just
  2241. transferring information between a variable and allocated
  2242. memory, of course. Pointers can refer to any part of memory. For
  2243. instance, CGA display memory starts at segment &HB800, offset 0,
  2244. and goes on for 4000 bytes in text mode. That gives a pointer of
  2245. &HB8000. You can transfer from the screen to a variable or vice
  2246. versa. For that matter, you can scroll the screen up, down,
  2247. left, or right by using the appropriate pointers. Add two to the
  2248. pointer to move it to the next character or 160 to move it to
  2249. the next row. Pointers have all kinds of applications! You don't
  2250. need to worry about overlapping memory-- if the two pointers,
  2251. combined with the bytes to move, overlap at some point, why, the
  2252. MMove routine takes care of that for you. It avoids pointer
  2253. conflicts. MMove is a very efficient memory copying routine.
  2254.  
  2255. Suppose you've got a pointer and would like to convert it back
  2256. to the segment/offset address that BASIC understands. That's no
  2257. problem:
  2258.  
  2259.    MSplitPtr Ptr&, TSeg%, TOfs%
  2260.  
  2261.                Memory Management and Pointers          page 47
  2262.  
  2263.  
  2264.  
  2265. You might also want to fill an area of memory with a specified
  2266. byte value, perhaps making freshly-allocated memory zeroes, for
  2267. example:
  2268.  
  2269.    MFill Ptr&, Value%, Bytes&
  2270.  
  2271. Finally, there may be occasions when you might want to transfer
  2272. a single character. Rather than going through putting the
  2273. character into a STRING*1, getting the VARSEG/VARPTR, and using
  2274. MJoinPtr&, there is a simpler way:
  2275.  
  2276.    MPutChr Ptr&, Ch$
  2277.    Ch$ = MGetChr$(Ptr&)
  2278.  
  2279. Hopefully, this will give you some ideas to start with. There
  2280. are many, many possible uses for such capabilities. Pointers and
  2281. memory management used to be the only real way in which BASIC
  2282. could be considered inferior to other popular languages-- that
  2283. is no more!
  2284.  
  2285. NOTE:
  2286.    QuickBasic may move its arrays around in memory! Don't expect
  2287.    the address of an array to remain constant while your program
  2288.    is running. Be sure to get the VARSEG/VARPTR for arrays any
  2289.    time you're not sure they're in the same location. Among the
  2290.    things which can cause arrays to move are use of DIM, REDIM,
  2291.    or ERASE, and SHELL. I'm not sure if anything else may cause
  2292.    the arrays to move, so be cautious!
  2293.  
  2294.                           Quick Timer                  page 48
  2295.  
  2296.  
  2297.  
  2298. On occasion, it's convenient to be able to time something
  2299. precisely, or delay for a very small interval. The standard PC
  2300. timer runs at about 18.2 ticks per second, which is enough for
  2301. some purposes. For other tasks, it can be like timing a race
  2302. with an hourglass. Fortunately, we can do a lot better.
  2303.  
  2304. The Quick Timer unit provides four countdown timers which work
  2305. with millisecond resolution. This is not compatible with BASIC
  2306. programs that use BEEP, PLAY, or SOUND statements, since BASIC
  2307. reprograms the system timer to handle sound effects. A set of
  2308. replacement sound routines provides similar capabilities,
  2309. though, so this will not present a problem unless you need music
  2310. to be played in the background. The current BasWiz sound handler
  2311. only plays in the foreground.
  2312.  
  2313. Before using any of the Quick Timer routines, you must install
  2314. and initialize the fast timer, like so:
  2315.  
  2316.    QTInit
  2317.  
  2318. It is *IMPORTANT* to shut down the fast timer before your
  2319. program exits! Otherwise, the computer will certainly crash in
  2320. short order. The shutdown may be done thusly:
  2321.  
  2322.    QTDone
  2323.  
  2324. Once the fast timer is installed, there are four countdown
  2325. channels available. You can set any of the channels to a given
  2326. value (0-32767), and it will count down to zero at 1000 ticks
  2327. per second. Channel 0 is used internally by several of the Quick
  2328. Timer routines, but channels 1-3 are entirely free for your use.
  2329. The "countdown timer" approach allows you to maintain timing in
  2330. the background while your program does something else.
  2331.  
  2332.    SetTimer Channel%, MilliSeconds%
  2333.    MilliSeconds% = GetTimer%(Channel%)
  2334.  
  2335. If your aim is to time something, set a channel to 32767 when it
  2336. starts, and get the channel value when it ends. Subtract the
  2337. final channel value from 32767 to get the number of milliseconds
  2338. taken by the event. You will not be able to time events that
  2339. last for more than some 32 seconds, since that is the maximum
  2340. delay the timer can handle.
  2341.  
  2342. If you just want to delay for a bit, without doing anything
  2343. else, it will be more convenient to use the QTDelay routine.
  2344.  
  2345.    QTDelay MilliSeconds%
  2346.  
  2347. Note that QTDelay uses channel 0. It simply sets the channel to
  2348. the desired delay and waits until the channel has counted down
  2349. to zero.
  2350.  
  2351.                           Quick Timer                  page 49
  2352.  
  2353.  
  2354.  
  2355. Since our Quick Timer reprograms the same timer than BASIC wants
  2356. to reprogram for sound handling, it is important to avoid the
  2357. use of any BASIC sound statements: BEEP, PLAY, or SOUND. BasWiz
  2358. provides replacements for these statements. Although the BasWiz
  2359. routines don't support background music processing, they can be
  2360. much smaller than the originals, especially if your program
  2361. doesn't use floating point math (which SOUND and PLAY normally
  2362. bring in, whether the rest of the program needs it or not).
  2363.  
  2364. All of the replacement sound routines make use of countdown
  2365. channel 0. You should generally avoid use of channel 0 by your
  2366. program to avoid possible conflicts.
  2367.  
  2368. The BEEP statement can be simply replaced:
  2369.  
  2370.    QTBeep
  2371.  
  2372. The SOUND statement takes a little more work to replace. Whereas
  2373. SOUND uses a sound time based on 18.2 ticks per second, QTSound
  2374. is based on milliseconds. When converting from SOUND to QTSound,
  2375. you must multiply the time by 55 to compensate.
  2376.  
  2377.    QTSound Frequency%, MilliSeconds%
  2378.  
  2379. The PLAY statement works just like you'd expect, although "MB"
  2380. (play music in the background) is ignored, since that capability
  2381. is not supported. Note that QTPlay will also ignore any errors
  2382. in the music command string-- this can be handy for interpreting
  2383. "ANSI" music, which may contain garbage. See the ANSI.BAS file,
  2384. which contains a "BBS ANSI" interpreter.
  2385.  
  2386.    QTPlay MusicCommand$
  2387.  
  2388. After playing a sequence of music commands, such as a song, you
  2389. may wish to reset to the original default music settings. This
  2390. is done like so:
  2391.  
  2392.    QTInitPlay
  2393.  
  2394. Remember, these BasWiz music routines are based on the Quick
  2395. Timer. You must install the Quick Timer with QTInit before they
  2396. will function. Don't forget to shut down with QTDone when you're
  2397. finished!
  2398.  
  2399.                       Telecommunications               page 50
  2400.  
  2401.  
  2402.  
  2403. BASIC is unusual among languages in that it comes complete with
  2404. built-in telecommunications support. Unfortunately, that support
  2405. is somewhat crude. Amongst other problems, it turns off the DTR
  2406. when the program SHELLs or ends, making it difficult to write
  2407. doors for BBSes or good terminal programs. It also requires use
  2408. of the /E switch for error trapping, since it generates errors
  2409. when line noise is encountered, and doesn't provide much
  2410. control. It doesn't even support COM3 and COM4, which have been
  2411. available for years.
  2412.  
  2413. BasWiz rectifies these troubles. It allows comprehensive control
  2414. over communications, includes COM3 and COM4, and doesn't require
  2415. error trapping. It won't fiddle with the DTR unless you tell it
  2416. to do so. The one limitation is that you may use only a single
  2417. comm port at a time.
  2418.  
  2419. Before you can use communications, you must initialize the
  2420. communications handler. If you didn't have BasWiz, you would
  2421. probably use something like:
  2422.  
  2423.    OPEN "COM1:2400,N,8,1,RS,CS,DS" AS #1
  2424.  
  2425. With BasWiz, you do not have to set the speed, parity, and so
  2426. forth. Communications will proceed with whatever the current
  2427. settings are, unless you choose to specify your own settings.
  2428. When you initialize the comm handler, you specify only the port
  2429. number (1-4) and the size of the input and output buffers
  2430. (1-32,767 bytes):
  2431.  
  2432.    TCInit Port, InSize, OutSize, ErrCode
  2433.  
  2434. The size you choose for the buffers should be guided by how your
  2435. program will use communications. Generally, a small output
  2436. buffer of 128 bytes will be quite adequate. You may wish to
  2437. expand it up to 1,500 bytes or so if you expect to write file
  2438. transfer protocols. For the input buffer, you will want perhaps
  2439. 512 bytes for normal use. For file transfer protocols, perhaps
  2440. 1,500 bytes would be better. If a high baud rate is used, or for
  2441. some other reason you might not be emptying the buffer
  2442. frequently, you may wish to expand the input buffer size to
  2443. 4,000 bytes or more.
  2444.  
  2445. When you are done with the telecomm routines, you must shut them
  2446. down. In BASIC, this would look something like:
  2447.  
  2448.    CLOSE #1
  2449.  
  2450. With the BasWiz routines, though, you would use this instead:
  2451.  
  2452.    TCDone
  2453.  
  2454.                      Telecommunications                page 51
  2455.  
  2456.  
  2457.  
  2458. The BasWiz "TCDone" does not drop the DTR, unlike BASIC's
  2459. "CLOSE". This means that the modem will not automatically be
  2460. told to hang up. With BasWiz, you have complete control over the
  2461. DTR with the TCDTR routine. Use a value of zero to drop the DTR
  2462. or nonzero to raise the DTR:
  2463.  
  2464.    TCDTR DTRstate
  2465.  
  2466. You may set the speed of the comm port to any baud rate from
  2467. 1-65,535. If you will be dealing with comm programs that were
  2468. not written using BasWiz, you may wish to restrict that to the
  2469. more common rates: 300, 1200, 2400, 4800, 9600, 19200, 38400,
  2470. and 57600.
  2471.  
  2472.    TCSpeed Baud&
  2473.  
  2474. The parity, word length, and stop bits can also be specified.
  2475. You may use 1-2 stop bits, 6-8 bit words, and parity settings of
  2476. None, Even, Odd, Mark, or Space. Nearly all BBSes use settings
  2477. of None, 8 bit words, and 1 stop bit, although you will
  2478. sometimes see Even, 7 bit words, and 1 stop bit. The other
  2479. capabilities are provided for dealing with mainframes and other
  2480. systems which may require unusual communications parameters.
  2481.  
  2482. When specifying parity, only the first character in the string
  2483. is used, and uppercase/lowercase distinctions are ignored. Thus,
  2484. using either "none" or "N" would specify that no parity is to be
  2485. used.
  2486.  
  2487.    TCParms Parity$, WordLength, StopBits
  2488.  
  2489. If your program needs to be aware of when a carrier is present,
  2490. it can check the carrier detect signal from the modem with the
  2491. TCCarrier function. This function returns zero if no carrier is
  2492. present:
  2493.  
  2494.    IF TCCarrier THEN
  2495.       PRINT "Carrier detected"
  2496.    ELSE
  2497.       PRINT "No carrier"
  2498.    END IF
  2499.  
  2500.                      Telecommunications                page 52
  2501.  
  2502.  
  2503.  
  2504. Suppose, though, that you need to know immediately when someone
  2505. has dropped the carrier? It wouldn't be too convenient to have
  2506. to spot TCCarrier functions all over your program! In that case,
  2507. try the "ON TIMER" facility provided by BASIC for keeping an eye
  2508. on things. It will enable you to check the carrier at specified
  2509. intervals and act accordingly. Here's a brief framework for
  2510. writing such code:
  2511.  
  2512.    ON TIMER(30) GOSUB CarrierCheck
  2513.    TIMER ON
  2514.    ' ...your program goes here...
  2515. CarrierCheck:
  2516.    IF TCCarrier THEN     ' if the carrier is present...
  2517.       RETURN             ' ...simply resume where we left off
  2518.    ELSE                  ' otherwise...
  2519.       RETURN Restart     ' ...return to the "Restart" label
  2520.    END IF
  2521.  
  2522. To get a character from the comm port, use the TCInkey$
  2523. function:
  2524.  
  2525.    ch$ = TCInkey$
  2526.  
  2527. To send a string to the comm port, use TCWrite:
  2528.  
  2529.    TCWrite St$
  2530.  
  2531. If you are dealing strictly with text, you may want to have a
  2532. carriage return and a linefeed added to the end of the string.
  2533. No problem:
  2534.  
  2535.    TCWriteLn St$
  2536.  
  2537. Note that the length of the output buffer affects how the
  2538. TCWrite and TCWriteLn routines work. They don't actually send
  2539. string directly to the comm port. Instead, they put the string
  2540. into the output buffer, and it gets sent to the comm port
  2541. whenever the comm port is ready. If there is not enough room in
  2542. the output buffer for the whole string, the TCWrite/TCWriteLn
  2543. routines are forced to wait until enough space has been cleared
  2544. for the string. This can delay your program. You can often avoid
  2545. this delay simply by making the output buffer larger.
  2546.  
  2547. If you'd like to know how many bytes are waiting in the input
  2548. buffer or output buffer, there are functions which will tell
  2549. you:
  2550.  
  2551.    PRINT "Bytes in input buffer:"; TCInStat
  2552.    PRINT "Bytes in output buffer:"; TCOutStat
  2553.  
  2554.                      Telecommunications                page 53
  2555.  
  2556.  
  2557.  
  2558. If you would like to clear the buffers for some reason, you can
  2559. do that too. The following routines clear the buffers,
  2560. discarding anything which was waiting in them:
  2561.  
  2562.    TCFlushIn
  2563.    TCFlushOut
  2564.  
  2565. Finally, there is a routine which allows you to handle ANSI
  2566. codes in a window. Besides the IBM semi-ANSI display code
  2567. subset, mock-ANSI music is allowed. This routine is designed as
  2568. a subroutine that you can access via GOSUB, since there are a
  2569. number of variables that the routine needs to maintain that
  2570. would be a nuisance to pass as parameters, and QuickBasic
  2571. unfortunately can't handle SUBs in $INCLUDE files (so SHARED
  2572. won't work). To use it, either include ANSI.BAS directly in your
  2573. code, or use:
  2574.  
  2575.    REM $INCLUDE: 'ANSI.BAS'
  2576.  
  2577. Set St$ to the string to process, set Win% to the handle of the
  2578. window to which to display, and set Music% to zero if you don't
  2579. want sounds or -1 if you do want sounds. Then:
  2580.  
  2581.    GOSUB ANSIprint
  2582.  
  2583. Note that the virtual screen tied to the window must be at least
  2584. an 80 column by 25 row screen, since ANSI expects that size. You
  2585. are also advised to have an ON ERROR trap if you use ANSIprint
  2586. with Music% = -1, just in case a "bad" music sequence slips
  2587. through and makes BASIC unhappy. Check for ERR = 5 (Illegal
  2588. Function Call). I may add a music handler later to avoid this.
  2589.  
  2590. To get some idea of how these routines all tie together in
  2591. practice, see the TERM.BAS example program. It provides a simple
  2592. "dumb terminal" program to demonstrate the BasWiz comm handler.
  2593. Various command-line switches are allowed:
  2594.  
  2595.    /43     use 43-line mode (EGA and VGA only)
  2596.    /COM2   use COM2
  2597.    /COM3   use COM3
  2598.    /COM4   use COM4
  2599.    /300    use 300 bps
  2600.    /1200   use 1200 bps
  2601.    /9600   use 9600 bps
  2602.    /14400  use 14400 bps
  2603.    /38400  use 38400 bps
  2604.    /57600  use 57600 bps
  2605.    /QUIET  ignore "ANSI" music
  2606.  
  2607. By default, the TERM.BAS program will use COM1 at 2400 baud with
  2608. no parity, 8 bit words and 1 stop bit. You can exit the program
  2609. by pressing Alt-X.
  2610.  
  2611.                      Telecommunications                page 54
  2612.  
  2613.  
  2614.  
  2615. If you're using a fast modem (9600 bps or greater), you should
  2616. turn on hardware flow control for reliable communications:
  2617.  
  2618.    TCFlowCtl -1
  2619.  
  2620. The Xmodem file transfer protocol is currently supported for
  2621. sending files only. It automatically handles any of the usual
  2622. variants on the Xmodem protocol: 128-byte or 1024-byte blocks,
  2623. plus checksum or CRC error detection. In other words, it is
  2624. compatible with Xmodem (checksum), Xmodem CRC, and Xmodem-1K
  2625. (single-file Ymodem-like variant).
  2626.  
  2627. There are only two routines which must be used to transfer a
  2628. file. The first is called once to initialize the transfer. The
  2629. second is called repeatedly until the transfer is finished or
  2630. aborted. Complete status information is returned by both
  2631. routines. You can ignore most of this information or display it
  2632. any way you please.
  2633.  
  2634. The initialization routine looks like this:
  2635.  
  2636.    StartXmodemSend Handle, Protocol$, Baud$, MaxRec, Record,
  2637.       EstTime$, ErrCode
  2638.  
  2639. Only the first three parameters are passed to the routine. These
  2640. are the Handle of the file that you wish to send (use FOpen to
  2641. get the handle) and the Protocol$ that you wish to use ("Xmodem"
  2642. or "Xmodem-1K"), and the current Baud$. On return, you will get
  2643. an ErrCode if the other computer did not respond, or MaxRec (the
  2644. number of blocks to be sent), Record (the current block number),
  2645. and EstTime$ (an estimate of the time required to complete the
  2646. transfer. The Protocol$ will have "CHK" or "CRC" added to it to
  2647. indicate whether checksum or CRC error detection is being used,
  2648. depending on which the receiver requested.
  2649.  
  2650. The secondary routine looks like this:
  2651.  
  2652.    XmodemSend Handle, Protocol$, MaxRec, Record, ErrCount,
  2653.       ErrCode
  2654.  
  2655. The ErrCode may be zero (no error), greater than zero (error
  2656. reading file), or less than zero (file transfer error,
  2657. completion or abort). See the appendix on Error Codes for
  2658. specific details. The TERM.BAS example program shows how these
  2659. routines work together in practice.
  2660.  
  2661. The file accessed by the Xmodem routine will remain open.
  2662. Remember to close it when the transfer is done (for whatever
  2663. reason), using the FClose routine.
  2664.  
  2665.                      Telecommunications                page 55
  2666.  
  2667.  
  2668.  
  2669. A few notes on the ins and outs of telecommunications...
  2670.  
  2671. The DTR signal is frequently used to control the modem. When the
  2672. DTR is "raised" or "high", the modem knows that we're ready to
  2673. do something. When the DTR is "dropped" or "low", the modem
  2674. knows that we're not going to do anything. In most cases, this
  2675. tells it to hang up or disconnect the phone line. Some modems
  2676. may be set to ignore the DTR, in which case it will not
  2677. disconnect when the DTR is dropped. Usually this can be fixed by
  2678. changing a switch on the modem. On some modems, a short software
  2679. command may suffice.
  2680.  
  2681. The DTR is generally the best way to disconnect. The Hayes "ATH"
  2682. command is supposed to hang up, but it doesn't work very well.
  2683.  
  2684. The BasWiz comm handler makes sure the DTR is raised when TCInit
  2685. is used. It does not automatically drop the DTR when TCDone is
  2686. used, so you can keep the line connected in case another program
  2687. wants to use it. If this is not suitable, just use TCDTR to drop
  2688. the DTR. Your program must always use TCDone before it exits,
  2689. but it need only drop the DTR if you want it that way.
  2690.  
  2691. If you want to execute another program via SHELL, it is ok to
  2692. leave communications running as long as control will return to
  2693. your program when the SHELLed program is done. In that case, the
  2694. input buffer will continue to receive characters from the comm
  2695. port unless the SHELLed program provides its own comm support.
  2696. The output buffer will likewise continue to transmit characters
  2697. unless overruled.
  2698.  
  2699.                 The Virtual Windowing System           page 56
  2700.  
  2701.  
  2702.  
  2703. The virtual windowing system offers pop-up and collapsing
  2704. windows, yes... but that is just a small fraction of what it
  2705. provides. When you create a window, the part that you see on the
  2706. screen may be only a view port on a much larger window, called a
  2707. virtual screen. You can make virtual screens of up to 255 rows
  2708. long or 255 columns wide. The only limitation is that any single
  2709. virtual screen must take up less than 65,520 bytes. Each virtual
  2710. screen is treated much like the normal screen display, with
  2711. simple replacements for the standard PRINT, LOCATE, and COLOR
  2712. commands. Many other commands are provided for additional
  2713. flexibility. The window on the virtual screen may be moved,
  2714. resized, or requested to display a different portion of the
  2715. virtual screen. If you like, you may choose to display a frame
  2716. and/or title around a window. When you open a new window, any
  2717. windows under it are still there and can still be updated--
  2718. nothing is ever destroyed unless you want it that way! With the
  2719. virtual windowing system, you get a tremendous amount of control
  2720. for a very little bit of work.
  2721.  
  2722. The current version of the virtual windowing system only allows
  2723. text mode screens to be used. All standard text modes are
  2724. supported, however. This includes 25x40 CGA screens, the
  2725. standard 25x80 screen, and longer screens such as the 43x80 EGA
  2726. screen. The virtual windowing system is designed for computers
  2727. that offer hardware-level compatibility with the IBM PC, which
  2728. includes almost all MS-DOS/PC-DOS computers in use today.
  2729.  
  2730.  
  2731. Terminology:
  2732. -----------
  2733.  
  2734. DISPLAY
  2735.    The actual screen.
  2736.  
  2737. SHADOW SCREEN
  2738.    This is a screen kept in memory which reflects any changes
  2739.    you make to windows. Rather than making changes directly on
  2740.    the actual screen, the virtual windowing system works with a
  2741.    "shadow screen" for increased speed and flexibility. You
  2742.    specify when to update the display from the shadow screen.
  2743.    This makes changes appear very smoothly.
  2744.  
  2745. VIRTUAL SCREEN
  2746.    This is a screen kept in memory which can be treated much
  2747.    like the actual screen. You may choose to make a virtual
  2748.    screen any reasonable size. Every virtual screen will have a
  2749.    corresponding window.
  2750.  
  2751. WINDOW
  2752.    This is the part of a virtual screen which is actually
  2753.    displayed. You might think of a window as a "view port" on a
  2754.    virtual screen. A window may be smaller than its virtual
  2755.    screen or the same size. It may have a frame or a title and
  2756.    can be moved or resized.
  2757.  
  2758.                 The Virtual Windowing System           page 57
  2759.  
  2760.  
  2761.  
  2762. Frankly, the virtual windowing system is one of those things
  2763. that's more difficult to explain than to use. It's very easy to
  2764. use, as a matter of fact, but the basic concepts will need a
  2765. little explanation. Rather than launching into a tedious and
  2766. long-winded description of the system, I'm going to take a more
  2767. tutorial approach, giving examples and explaining as I go along.
  2768. Take a look at the WDEMO.BAS program for examples.
  2769.  
  2770. Let's begin with the simplest possible scenario, where only a
  2771. background window is created. This looks just like a normal
  2772. screen.
  2773.  
  2774.    REM $INCLUDE: 'BASWIZ.BI'
  2775.    DEFINT A-Z
  2776.    Rows = 25: Columns = 80         ' define display size
  2777.    WInit Rows, Columns, ErrCode    ' initialize window system
  2778.    IF ErrCode THEN                 ' stop if we couldn't...
  2779.       PRINT "Insufficient memory"
  2780.       END
  2781.    END IF
  2782.    Handle = 0                      ' use background handle
  2783.    WWriteLn Handle, "This is going on the background window."
  2784.    WWriteLn Handle, "Right now, that's the full screen."
  2785.    WUpdate                         ' update the display
  2786.    WDone                           ' terminate window system
  2787.  
  2788. What we just did was to display two lines on the screen--
  2789. nothing at all fancy, but it gives you the general idea of how
  2790. things work. Let's take a closer look:
  2791.  
  2792.   - We INCLUDE the BASWIZ.BI definition file to let BASIC know
  2793.     that we'll be using the BasWiz routines.
  2794.  
  2795.   - We define the size of the display using the integer
  2796.     variables Rows and Columns (you can use any variable names
  2797.     you want). If you have an EGA display and had previously
  2798.     used WIDTH ,43 to go into 43x80 mode, you'd use "Rows = 43"
  2799.     here, for example.
  2800.  
  2801.   - We initialize the windowing system with WInit, telling it
  2802.     how large the display is. It returns an error code if it is
  2803.     unable to initialize.
  2804.  
  2805.   - We define the Handle of the window that we want to use. The
  2806.     "background window" is always available as handle zero, so
  2807.     we choose "Handle = 0".
  2808.  
  2809.   - We print two strings to the background window with WWriteLn,
  2810.     which is like a PRINT without a semicolon on the end (it
  2811.     moves to the next line).
  2812.  
  2813.   - At this point, only the shadow screen has been updated.
  2814.     We're ready to display the information, so we use WUpdate to
  2815.     update the actual screen.
  2816.  
  2817.   - We're all done with the program, so we end with WDone.
  2818.  
  2819.                 The Virtual Windowing System           page 58
  2820.  
  2821.  
  2822.  
  2823. See, there's nothing to it! We initialize the screen, print to
  2824. it or whatever else we need to do, tell the windowing system to
  2825. update the display, and when the program is done, we close up
  2826. shop.
  2827.  
  2828. The background screen is always available. It might help to
  2829. think of it as a virtual screen that's the size of the display.
  2830. The window on this virtual screen is exactly the same size, so
  2831. the entire virtual screen is displayed. As with other virtual
  2832. screens, you can print to it without disturbing anything else.
  2833. That means you can treat the background screen the same way
  2834. regardless of whether it has other windows on top of it-- the
  2835. other windows just "cover" the background information, which
  2836. will still be there.
  2837.  
  2838. This leads us to the topic of creating windows. Both a virtual
  2839. screen and a window are created simultaneously-- remember, a
  2840. window is just a view port on a virtual screen. The window can
  2841. be the same size as the virtual screen, in which case the entire
  2842. virtual screen is visible (as with the background window) or it
  2843. can be smaller than the virtual screen, in which case just a
  2844. portion of the virtual screen will be visible at any one time.
  2845.  
  2846. A window is created like so:
  2847.  
  2848.    ' This is a partial program and can be inserted in the
  2849.    ' original example after the second WWriteLn statement...
  2850.    VRows = 43: VColumns = 80      ' define virtual screen size
  2851.    ' create the window
  2852.    WOpen VRows, VColumns, 1, 1, Rows, Columns, Handle, ErrCode
  2853.    IF ErrCode THEN                ' error if we couldn't...
  2854.       PRINT "Insufficient memory available"
  2855.       WDone                       ' (or use an error handler)
  2856.       END
  2857.    END IF
  2858.  
  2859. What we have done here is to create a virtual screen of 43 rows
  2860. by 80 columns. The window will be the size of the display, so if
  2861. you are in the normal 25x80 mode, only the first 25 rows of the
  2862. virtual screen will be visible. If you have an EGA or VGA in
  2863. 43x80 mode, though, the entire virtual screen will be visible!
  2864. So, this window lets you treat a screen the same way regardless
  2865. of the display size.
  2866.  
  2867. The Handle returned is used any time you want to print to this
  2868. new window or otherwise deal with it. If you are using many
  2869. windows, you might want to keep an array of handles, to make it
  2870. easier to keep track of which is which.
  2871.  
  2872.                 The Virtual Windowing System           page 59
  2873.  
  2874.  
  2875.  
  2876. By default, a virtual screen is created with the following
  2877. attributes:
  2878.  
  2879.   - The cursor is at (1,1), the upper left corner of the
  2880.     virtual screen.
  2881.   - The cursor size is 0 (invisible).
  2882.   - The text color is 7,0 (white foreground on a black
  2883.     background).
  2884.   - There is no title or frame.
  2885.   - The window starts at (1,1) in the virtual screen, which
  2886.     displays the area starting at the upper left corner of
  2887.     the virtual screen.
  2888.  
  2889. When you create a new window, it becomes the "top" window, and
  2890. will be displayed on top of any other windows that are in the
  2891. same part of the screen. Remember, you can print to a window or
  2892. otherwise deal with it, even if it's only partially visible or
  2893. entirely covered by other windows.
  2894.  
  2895. Don't forget WUpdate! None of your changes are actually
  2896. displayed until WUpdate is used. You can make as many changes as
  2897. you like before calling WUpdate, which will display the results
  2898. smoothly and at lightning speed.
  2899.  
  2900. We've created a window which is exactly the size of the display,
  2901. but which might well be smaller than its virtual screen. Let's
  2902. assume that the normal 25x80 display is being used, in which
  2903. case our virtual screen (43x80) is larger than the window. We
  2904. can still print to the virtual screen normally, but if we print
  2905. below line 25, the results won't be displayed. What a
  2906. predicament! How do we fix this?
  2907.  
  2908. The window is allowed to start at any given location in the
  2909. virtual screen, so if we want to see a different portion of the
  2910. virtual screen, all we have to do is tell the window to start
  2911. somewhere else. When the window is created, it starts at the
  2912. beginning of the virtual screen, coordinate (1,1). The WView
  2913. routine allows us to change this.
  2914.  
  2915. In our example, we're displaying a 43x80 virtual screen in a
  2916. 25x80 window. To begin with, then, rows 1-25 of the virtual
  2917. screen are visible. To make rows 2-26 of the virtual screen
  2918. visible, we simply do this:
  2919.  
  2920.    WView Handle, 2, 1
  2921.  
  2922. That tells the window to start at row 2, column 1 in the virtual
  2923. screen. Sounds easy enough, doesn't it? Well, if not, don't
  2924. despair. Play with it a little until you get the hang of it.
  2925.  
  2926.                 The Virtual Windowing System           page 60
  2927.  
  2928.  
  2929.  
  2930. You've noticed that the window doesn't need to be the same size
  2931. as the virtual screen. Suppose we don't want it the same size as
  2932. the display, either... suppose we want it in a nice box, sitting
  2933. out of the way in a corner of the display? Well, we could have
  2934. created it that way to begin with when we used WOpen. Since
  2935. we've already created it, though, let's take a look at the
  2936. routines to change the size of a window and to move it
  2937. elsewhere. The window can be made as small as 1x1 or as large as
  2938. its virtual screen, and it can be moved anywhere on the display
  2939. you want it.
  2940.  
  2941. Let's make the window a convenient 10 rows by 20 columns:
  2942.  
  2943.    WSize Handle, 10, 20
  2944.  
  2945. And move it into the lower right corner of the display:
  2946.  
  2947.    WPlace Handle, 12, 55
  2948.  
  2949. Don't forget to call WUpdate or the changes won't be visible!
  2950. Note also that we didn't really lose any text. The virtual
  2951. screen, which holds all the text, is still there. We've just
  2952. changed the size and position of the window, which is the part
  2953. of the virtual screen that we see, so less of the text (if there
  2954. is any!) is visible. If we made the window larger again, the
  2955. text in the window would expand accordingly.
  2956.  
  2957. If you were paying close attention, you noticed that we didn't
  2958. place the resized window flush against the corner of the
  2959. display. We left a little bit of room so we can add a frame and
  2960. a title. Let's proceed to do just that.
  2961.  
  2962. Window frames are displayed around the outside of a window and
  2963. will not be displayed unless there is room to do so. We have
  2964. four different types of standard frames available:
  2965.  
  2966.    0   (no frame)
  2967.    1   single lines
  2968.    2   double lines
  2969.    3   single horizontal lines, double vertical lines
  2970.    4   single vertical lines, double horizontal lines
  2971.  
  2972. We must also choose the colors for the frame. It usually looks
  2973. best if the background color is the same background color as
  2974. used by the virtual screen. Let's go ahead and create a
  2975. double-line frame in bright white on black:
  2976.  
  2977.    FType = 2: Fore = 15: Back = 0
  2978.    WFrame Handle, FType, Fore, Back
  2979.  
  2980.                 The Virtual Windowing System           page 61
  2981.  
  2982.  
  2983.  
  2984. If you'd rather not use the default frame types, there's ample
  2985. room to get creative! Frames 5-9 can be defined any way you
  2986. please. They are null by default. To create a new frame type,
  2987. you must specify the eight characters needed to make the frame:
  2988. upper left corner, upper middle columns, upper right corner,
  2989. left middle rows, right middle rows, lower left corner, lower
  2990. middle columns, and lower right corner.
  2991.  
  2992.    +----------------------------------------+
  2993.    |  Want a plain text frame like this?    |
  2994.    |  Use the definition string "+-+||+-+"  |
  2995.    +----------------------------------------+
  2996.  
  2997. The above window frame would be defined something like this:
  2998.  
  2999.    Frame = 5
  3000.    FrameInfo$ = "+-+||+-+"
  3001.    WUserFrame Frame, FrameInfo$
  3002.  
  3003. Of course, you can choose any values you like. As always, the
  3004. names of the variables can be anything, as long as you name them
  3005. consistently within your program. You can even use constants if
  3006. you prefer:
  3007.  
  3008.    WUserFrame 5, "+-+||+-+"
  3009.  
  3010. If you use a frame, you can also have a "shadow", which provides
  3011. a sort of 3-D effect. The shadow can be made up of any character
  3012. you choose, or it can be entirely transparent, in which case
  3013. anything under the shadow will change to the shadow colors. This
  3014. latter effect can be quite nice. I've found that it works best
  3015. for me when I use a dim foreground color with a black
  3016. background-- a foreground color of 8 produces wonderful effects
  3017. on machines that support it (it's "bright black", or dark gray;
  3018. some displays will show it as entirely black, though, so it may
  3019. not always work the way you want). For a transparent shadow,
  3020. select CHR$(255) as the shadow character. You can turn the
  3021. shadow off with either a null string or CHR$(0).
  3022.  
  3023.    Shadow$ = CHR$(255)                  ' transparent shadow
  3024.    Fore = 8: Back = 0                   ' dark gray on black
  3025.    WShadow Handle, Shadow$, Fore, Back
  3026.  
  3027. A shadow will only appear if there is also a frame, and if there
  3028. is enough space for it on the screen. Currently, there is only
  3029. one type of shadow, which appears on the right and bottom sides
  3030. of the frame. It effectively makes the frame wider and longer by
  3031. one character.
  3032.  
  3033.                 The Virtual Windowing System           page 62
  3034.  
  3035.  
  3036.  
  3037. We can have a title regardless of whether a frame is present or
  3038. not. Like the frame, the title is displayed only if there is
  3039. enough room for it. If the window is too small to accommodate
  3040. the full title, only the part of the title that fits will be
  3041. displayed. The maximum length of a title is 70 characters.
  3042. Titles have their own colors.
  3043.  
  3044.    Title$ = "Wonderful Window!"
  3045.    Fore = 0: Back = 7
  3046.    WTitle Handle, Title$, Fore, Back
  3047.  
  3048. To get rid of a title, just use a null title string, for
  3049. example:
  3050.  
  3051.    Title$ = ""
  3052.  
  3053. It may be convenient to set up a window that isn't always
  3054. visible-- say, for a help window, perhaps. The window could be
  3055. set up in advance, then shown whenever requested using just one
  3056. statement:
  3057.  
  3058.    WHide Handle, Hide
  3059.  
  3060. You can make a window invisible by using any nonzero value for
  3061. Hide, or make it reappear by setting Hide to zero. As always,
  3062. the change will only take effect after WUpdate is used.
  3063.  
  3064. When WWrite or WWriteLn gets to the end of a virtual screen,
  3065. they normally scroll the "screen" up to make room for more text.
  3066. This is usually what you want, of course, but there are
  3067. occasions when it can be a nuisance. The automatic scrolling can
  3068. be turned off or restored like so:
  3069.  
  3070.    WScroll Handle, AutoScroll
  3071.  
  3072. There are only a few more ways of dealing with windows
  3073. themselves. After that, I'll explain the different things you
  3074. can do with text in windows and how to get information about a
  3075. specific window or virtual screen.
  3076.  
  3077. If you have a lot of windows, one window may be on top of
  3078. another, obscuring part or all of the window(s) below. In order
  3079. to make sure a window is visible, all you need to do is to put
  3080. it on top, right? Hey, is this easy or what?!
  3081.  
  3082.    WTop Handle
  3083.  
  3084. You may also need to "unhide" the window if you used WHide on it
  3085. previously.
  3086.  
  3087.                 The Virtual Windowing System           page 63
  3088.  
  3089.  
  3090.  
  3091. Note that the background window will always be the background
  3092. window. You can't put handle zero, the background window, on
  3093. top. What? You say you need to do that?! Well, that's one of the
  3094. ways you can use the WCopy routine. WCopy copies one virtual
  3095. screen to another one of the same size:
  3096.  
  3097.    WCopy FromHandle, ToHandle
  3098.  
  3099. You can copy the background window (or any other window) to
  3100. another window. The new window can be put on top, resized,
  3101. moved, or otherwise spindled and mutilated. The WDEMO program
  3102. uses this trick.
  3103.  
  3104. We've been through how to open windows, print to them, resize
  3105. them and move them around, among other things. We've seen how to
  3106. put a frame and a title on a window and pop it onto the display.
  3107. If you're a fan of flashy displays, though, you'd probably like
  3108. to be able to make a window "explode" onto the screen or
  3109. "collapse" off. It's the little details like that which make a
  3110. program visually exciting and professional-looking. I wouldn't
  3111. disappoint you by leaving something fun like that out!
  3112.  
  3113. Since we're using a virtual windowing system rather than just a
  3114. plain ol' ordinary window handler, there's an extra benefit.
  3115. When a window explodes or collapses, it does so complete with
  3116. its title, frame, shadow, and even its text. This adds rather
  3117. nicely to the effect.
  3118.  
  3119. To "explode" a window, we just set up all its parameters the way
  3120. we normally would-- open the window, add a title or frame if we
  3121. like, print any text that we want displayed, and set the screen
  3122. position. Then we use WExplode to zoom the window from a tiny
  3123. box up to its full size:
  3124.  
  3125.    WExplode Handle
  3126.  
  3127. The "collapse" routine works similarly. It should be used only
  3128. when you are through with a window, because it closes the window
  3129. when it's done. The window is collapsed from its full size down
  3130. to a tiny box, then eliminated entirely:
  3131.  
  3132.    WCollapse Handle
  3133.  
  3134. Note that WExplode and WCollapse automatically use WUpdate to
  3135. update the display. You do not need to use WUpdate yourself and
  3136. you should make sure that the screen is the way you want it
  3137. displayed before you call either routine.
  3138.  
  3139.                 The Virtual Windowing System           page 64
  3140.  
  3141.  
  3142.  
  3143. The WCollapse and WExplode routines were written in BASIC, so
  3144. you can customize them just the way you want them.
  3145.  
  3146. That's it for the windows. We've been through all the "tricky
  3147. stuff". There are a number of useful things you can do with a
  3148. virtual screen, though, besides printing to it with WWriteLn.
  3149. Let's take a look at what we can do.
  3150.  
  3151. WWriteLn is fine if you want to use a "PRINT St$" sort of
  3152. operation. Suppose you don't want to move to a new line
  3153. afterward, though? In BASIC, you'd use something like "PRINT
  3154. St$;" (with a semicolon). With the virtual windowing system, you
  3155. use WWrite, which is called just like WWriteLn:
  3156.  
  3157.    WWrite Handle, St$
  3158.  
  3159. There are also routines that work like CLS, COLOR and LOCATE:
  3160.  
  3161.    WClear Handle
  3162.    WColor Handle, Fore, Back
  3163.    WLocate Handle, Row, Column
  3164.  
  3165. The WClear routine is not quite like CLS in that it does not
  3166. alter the cursor position. If you want the cursor "homed", use
  3167. WLocate.
  3168.  
  3169. Note that the coordinates for WLocate are based on the virtual
  3170. screen, not the window. If you move the cursor to a location
  3171. outside the view port provided by the window, it will disappear.
  3172. Speaking of disappearing cursors, you might have noticed that
  3173. our WLocate doesn't mimic LOCATE exactly: it doesn't provide for
  3174. controlling the cursor size. Don't panic! There's another
  3175. routine available for that:
  3176.  
  3177.    WCursor Handle, CSize
  3178.  
  3179. The CSize value may range from zero (in which case the cursor
  3180. will be invisible) to the maximum size allowed by your display
  3181. adapter. This will always be at least eight.
  3182.  
  3183. Now, since each virtual screen is treated much like the full
  3184. display, you may be wondering what happens if the cursor is "on"
  3185. in more than one window. Does that mean multiple cursors are
  3186. displayed? Well, no. That would get a little confusing! Only the
  3187. cursor for the top window is displayed. If you put a different
  3188. window on top, the cursor for that window will be activated and
  3189. the cursor for the old top window will disappear. The virtual
  3190. windowing system remembers the cursor information for each
  3191. window, but it only actually displays the cursor for the window
  3192. that's on top.
  3193.  
  3194.                 The Virtual Windowing System           page 65
  3195.  
  3196.  
  3197.  
  3198.  
  3199. In addition to the usual screen handling, the windowing system
  3200. provides a number of new capabilities which you may find very
  3201. handy. These include routines to insert and delete both
  3202. characters and rows, which is done at the current cursor
  3203. position within a selected virtual screen:
  3204.  
  3205.    WDelChr Handle
  3206.    WDelLine Handle
  3207.    WInsChr Handle
  3208.    WInsLine Handle
  3209.  
  3210. These routines can also be used for scrolling. Remember, the
  3211. display isn't updated until you use WUpdate, and then it's
  3212. updated all at once. You can use any of the routines multiple
  3213. times and the display will still be updated perfectly smoothly--
  3214. all the real work goes on behind the scenes!
  3215.  
  3216. Normally, the windowing system interprets control codes
  3217. according to the ASCII standard-- CHR$(7) beeps, CHR$(8) is a
  3218. backspace, and so forth. Sometimes you may want to print the
  3219. corresponding IBM graphics character instead, though... or maybe
  3220. you just don't use control codes and want a little more speed
  3221. out of the windowing system. You can turn control code handling
  3222. on or off for any individual window:
  3223.  
  3224.    WControl Handle, DoControl
  3225.  
  3226. When you are done with a virtual screen and no longer need it,
  3227. you can dispose of it like so:
  3228.  
  3229.    WClose Handle
  3230.  
  3231. All of the information that can be "set" can also be retrieved.
  3232. That's useful in general, of course, but it's also a great
  3233. feature for writing portable subprograms. You can create
  3234. subprograms that will work with any virtual screen, since it can
  3235. retrieve any information it needs to know about the virtual
  3236. screen or its window. That's power!
  3237.  
  3238.                 The Virtual Windowing System           page 66
  3239.  
  3240.  
  3241.  
  3242. Here is a list of the available window information routines:
  3243.  
  3244.    WGetColor Handle, Fore, Back
  3245.    ' gets the current foreground and background colors
  3246.  
  3247.    WGetControl Handle, DoControl
  3248.    ' gets whether control codes are interpreted
  3249.  
  3250.    WGetCursor Handle, CSize
  3251.    ' gets the cursor size
  3252.  
  3253.    WGetFrame Handle, Frame, Fore, Back
  3254.    ' gets the frame type and frame colors
  3255.  
  3256.    WGetLocate Handle, Row, Column
  3257.    ' gets the cursor position
  3258.  
  3259.    WGetPlace Handle, Row, Column
  3260.    ' gets the starting position of a window on the display
  3261.  
  3262.    WGetScroll Handle, AutoScroll
  3263.    ' gets the status of auto-scroll
  3264.    ' (scrolling at the end of a virtual screen)
  3265.  
  3266.    Shadow$ = SPACE$(1)
  3267.    WGetShadow Handle, Shadow$, Fore, Back
  3268.    ' gets the shadow character (CHR$(0) if there's no
  3269.    ' shadow) and colors
  3270.  
  3271.    WGetSize Handle, Rows, Columns
  3272.    ' gets the size of a window
  3273.  
  3274.    Title$ = SPACE$(70)
  3275.    WGetTitle Handle, Title$, TLen, Fore, Back
  3276.    Title$ = LEFT$(Title$, TLen)
  3277.    ' gets the title string (null if there's no title) and
  3278.    ' title colors
  3279.  
  3280.    WGetTop Handle
  3281.    ' gets the handle of the top window
  3282.  
  3283.    FrameInfo$ = SPACE$(8)
  3284.    WGetUFrame$ Frame, FrameInfo$
  3285.    ' gets the specification for a given user-defined frame
  3286.  
  3287.    WGetView Handle, Row, Column
  3288.    ' gets the starting position of a window within a
  3289.    ' virtual screen
  3290.  
  3291.    WGetVSize Handle, Rows, Columns
  3292.    ' gets the size of a virtual screen
  3293.  
  3294.    WHidden Handle, Hidden
  3295.    ' tells you whether a window is visible
  3296.  
  3297.                 The Virtual Windowing System           page 67
  3298.  
  3299.  
  3300.  
  3301. As well as displaying information in a window, you will
  3302. frequently want to allow for getting input from the user. Of
  3303. course, INKEY$ will still work fine, but that's not an effective
  3304. way of handling more than single characters. The virtual window
  3305. system includes a flexible string input routine which is a lot
  3306. more powerful:
  3307.  
  3308.    WInput Handle, Valid$, ExitCode$, ExtExitCode$,
  3309.       MaxLength, St$, ExitKey$
  3310.  
  3311. The Valid$ variable allows you to specify a list of characters
  3312. which may be entered. If you use a null string (""), any
  3313. character will be accepted.
  3314.  
  3315. ExitCode$ specifies the normal keys that can be used to exit
  3316. input. You'll probably want to use a carriage return, CHR$(13),
  3317. for this most of the time. You can also specify exit on extended
  3318. key codes like arrow keys and function keys via ExtExitCode$.
  3319.  
  3320. MaxLength is the maximum length of the string you want. Use zero
  3321. to get the longest possible string. The length may go up to the
  3322. width of the virtual screen, minus one character. The window
  3323. will be scrolled sideways as needed to accommodate the full
  3324. length of the string.
  3325.  
  3326. The St$ variable is used to return the entered string, but you
  3327. can also use it to pass a default string to the routine.
  3328.  
  3329. ExitKey$ returns the key that was used to exit input.
  3330.  
  3331. A fairly strong set of editing capabilities is available through
  3332. WInput. The editing keys can be overridden by ExitCode$ or
  3333. ExtExitCode$, but by default they include support for both the
  3334. cursor keypad and WordStar:
  3335.  
  3336.    Control-S   LeftArrow    move left once
  3337.    Control-D   RightArrow   move right once
  3338.    Control-V   Ins          insert <--> overstrike modes
  3339.    Control-G   Del          delete current character
  3340.    Control-H   Backspace    destructive backspace
  3341.                Home         move to the start of input
  3342.                End          move to the end of input
  3343.  
  3344.                 The Virtual Windowing System           page 68
  3345.  
  3346.  
  3347.  
  3348. Pop-up menus have become very popular in recent years.
  3349. Fortunately, they are a natural application for virtual windows!
  3350. BasWiz provides a pop-up menuing routine which allows you to
  3351. have as many as 255 choices-- the window will be scrolled
  3352. automatically to accommodate your "pick list", with a highlight
  3353. bar indicating the current selection.
  3354.  
  3355. The pop-up menu routine uses a window which you've already set
  3356. up, so you can use any of the normal window options-- frames,
  3357. titles, shadows, etc. You must provide a virtual screen large
  3358. enough to hold your entire pick list; the window itself can be
  3359. any size at all.
  3360.  
  3361. The pick list is passed to WMenuPopUp through a string array.
  3362. You can dimension this array in any range that suits you. The
  3363. returned selection will be the relative position in the array (1
  3364. for the first item, etc); if the menu was aborted, 0 will be
  3365. returned instead.
  3366.  
  3367. The current window colors will be used for the "normal" colors.
  3368. You specify the desired highlight colors when calling the pop-up
  3369. menu routine.
  3370.  
  3371.    Result = WMenuPopUp(Handle, PickList$(), HiFore, HiBack)
  3372.  
  3373. The mouse is not supported, since BasWiz does not yet have mouse
  3374. routines. However, scrolling can be accomplished with any of the
  3375. more common methods: up and down arrows, WordStar-type Control-E
  3376. and Control-X, or Lotus-type tab and backtab. The ESCape key can
  3377. be used to abort without choosing an option.
  3378.  
  3379. On exit, the menu window will remain in its final position, in
  3380. case you wish to pop up a related window next to it or something
  3381. similar. Since it's just an ordinary window, you can use WClose
  3382. or WCollapse if you prefer to get rid of it.
  3383.  
  3384. The WMenuPopUp routine was written in BASIC, so you will find it
  3385. easy to modify to your tastes. It was written with extra
  3386. emphasis on comments and clarity, since I know many people will
  3387. want to customize this routine!
  3388.  
  3389.                 The Virtual Windowing System           page 69
  3390.  
  3391.  
  3392.  
  3393. There are two more routines which allow the virtual windowing
  3394. system to work on a wide variety of displays: WFixColor and
  3395. WSnow.
  3396.  
  3397. Chances are, as a software developer you have a color display.
  3398. However, there are many people out there who have monochrome
  3399. displays, whether due to preference, a low budget, or use of
  3400. notebook-style computers with mono LCD or plasma screens.
  3401. WFixColor allows you to develop your programs in color while
  3402. still supporting monochrome systems. It tells the virtual
  3403. windowing system whether to keep the colors as specified or to
  3404. translate them to their monochrome equivalents:
  3405.  
  3406.    WFixColor Convert%
  3407.  
  3408. Set Convert% to zero if you want true color (default), or to any
  3409. other value if you want the colors to be translated to
  3410. monochrome. In the latter case, the translation will be done
  3411. based on the relative brightness of the foreground and
  3412. background colors. The result is guaranteed to be readable on a
  3413. monochrome system if it's readable on a color system. You should
  3414. check the results on your system to make sure that such things
  3415. as highlight bars still appear highlighted, however.
  3416.  
  3417. In the case of some of the older or less carefully designed CGA
  3418. cards, the high-speed displays of the virtual windowing system
  3419. can cause the display to flicker annoyingly. You can get rid of
  3420. the flicker at the expense of slowing the display:
  3421.  
  3422.    WSnow Remove%
  3423.  
  3424. Set Remove% to zero if there is no problem with "snow" or
  3425. flickering (default), or to any other value if you need "snow
  3426. removal". Using snow removal will slow down the display
  3427. substantially, which may be a problem if you update (WUpdate) it
  3428. frequently.
  3429.  
  3430. Note that you can't detect either of these cases automatically
  3431. with perfect reliability. Not all CGA cards have flicker
  3432. problems. Also, mono displays may be attached to CGA cards and
  3433. the computer won't know the difference. A VGA with a "paper
  3434. white" monitor may well think it has color, and will mostly act
  3435. like it, but some "color" combinations can be very difficult to
  3436. read. While you can self-configure the program to some extent
  3437. using the GetDisplay routine (see Other Routines), you should
  3438. also provide command-line switches so that the user can override
  3439. your settings. Microsoft generally uses "/B" to denote a
  3440. monochrome ("black and white") display, so you may want to
  3441. follow that as a standard.
  3442.  
  3443. Finally, by popular request, there is a routine which returns
  3444. the segment and offset of a virtual screen. This lets you do
  3445. things with a virtual screen that are not directly supported by
  3446. BasWiz. Virtual screens are laid out like normal text screens.
  3447.  
  3448.    WGetAddress Handle, WSeg, WOfs
  3449.  
  3450.                        Other Routines                  page 70
  3451.  
  3452.  
  3453.  
  3454. There are a number of routines for which I couldn't find a
  3455. specific category.
  3456.  
  3457. To see how much expanded memory is available, use the GetEMS
  3458. function. It'll return zero if there is no expanded memory
  3459. installed:
  3460.  
  3461.    PRINT "Kbytes of expanded memory:"; GetEMS
  3462.  
  3463. The GetDisplay routine tells what kind of display adapter is
  3464. active and whether it's hooked up to a color monitor. The only
  3465. time it can't detect the monitor type is on CGA setups (it
  3466. assumes "color"). It's a good idea to allow a "/B" switch for
  3467. your program so the user can specify if a monochrome monitor is
  3468. attached to a CGA.
  3469.  
  3470.    GetDisplay Adapter, Mono
  3471.    IF Mono THEN
  3472.       PRINT "Monochrome monitor"
  3473.    ELSE
  3474.       PRINT "Color monitor"
  3475.    END IF
  3476.    SELECT CASE Adapter
  3477.       CASE 1: PRINT "MDA"
  3478.       CASE 2: PRINT "Hercules"
  3479.       CASE 3: PRINT "CGA"
  3480.       CASE 4: PRINT "EGA"
  3481.       CASE 5: PRINT "MCGA"
  3482.       CASE 6: PRINT "VGA"
  3483.    END SELECT
  3484.  
  3485. The ScreenSize routine returns the number of rows and columns on
  3486. the display (text modes only):
  3487.  
  3488.    ScreenSize Rows%, Columns%
  3489.  
  3490.                      Miscellaneous Notes               page 71
  3491.  
  3492.  
  3493.  
  3494. The virtual windowing system allows up to 16 windows to be open
  3495. at a time, including the background window, which is opened
  3496. automatically. This is subject to available memory, of course.
  3497.  
  3498. The far string handler allows up to 65,535 strings of up to 255
  3499. characters each, subject to available memory. When the handler
  3500. needs additional memory for string storage, it allocates more in
  3501. blocks of 16 Kbytes. If that much memory is not available, an
  3502. "out of memory" error will be generated (BASIC error number 7).
  3503. You can check the size of the available memory pool using the
  3504. SETMEM function provided by QuickBasic.
  3505.  
  3506. The communications handler only allows one comm port to be used
  3507. at a time.
  3508.  
  3509. The file handler does not allow you to combine Write mode with
  3510. Text mode or input buffering.
  3511.  
  3512. A certain lack of speed is inherent in BCD math, especially if
  3513. you require high precision. The division, root, and trig
  3514. routines in particular are quite slow.
  3515.  
  3516. The fraction routines are much faster, but they have a much
  3517. smaller range. I'll have to do some experimenting on that. It
  3518. may prove practical to use a subset of the BCD routines to
  3519. provide an extended range for fractions without an unreasonable
  3520. loss in speed.
  3521.  
  3522. All routines are designed to be as bomb-proof as possible. If
  3523. you pass an invalid value to a routine which does not return an
  3524. error code, it will simply ignore the value.
  3525.  
  3526. The EGA graphics routines are designed for use with EGAs having
  3527. at least 256K RAM on board. They will not operate properly on
  3528. old 64K EGA systems.
  3529.  
  3530. Image loading (.MAC and .PCX) is quite slow. The bulk of the
  3531. code is in BASIC at this point, to make it easier for me to
  3532. extend the routines to cover other graphics modes. They will be
  3533. translated to assembly later.
  3534.  
  3535. The G#Write and G#WriteLn services support three different
  3536. fonts: 8x8, 8x14, and 8x16. The default font is always 8x8,
  3537. providing the highest possible text density. QuickBasic, on the
  3538. other hand, allows only one font with a text density of as close
  3539. to 80x25 as possible.
  3540.  
  3541.                      Miscellaneous Notes               page 72
  3542.  
  3543.  
  3544.  
  3545. The G#Write and G#WriteLn services interpret ASCII control
  3546. characters, i.e. CHR$(0) - CHR$(31), according to the more
  3547. standard handling used by DOS rather than the esoteric
  3548. interpretation offered by QuickBasic. This is not exactly a
  3549. limitation, but it could conceivably cause confusion if your
  3550. program happens to use these characters. The ASCII
  3551. interpretation works as follows:
  3552.  
  3553.     Code       Meaning
  3554.     ====       =======
  3555.       7        Bell       (sound a beep through the speaker)
  3556.       8        Backspace  (eliminate the previous character)
  3557.       9        Tab        (based on 8-character tab fields)
  3558.      10        LineFeed   (move down one line, same column)
  3559.      12        FormFeed   (clear the screen)
  3560.      13        Return     (move to the start of the row)
  3561.  
  3562. G#MirrorH will only work properly on images with byte alignment!
  3563. This means that the width of the image must be evenly divisible
  3564. by four if SCREEN 1 is used, or evenly divisible by eight if
  3565. SCREEN 2 is used.
  3566.  
  3567. The graphics routines provide little error checking and will not
  3568. do clipping (which ignores points outside the range of the
  3569. graphics mode). If you specify coordinates which don't exist,
  3570. the results will be unusual at best. Try to keep those values
  3571. within the proper range!
  3572.  
  3573. A very few of the graphics routines are slower than their
  3574. counterparts in QuickBasic. These are mostly drawing diagonal
  3575. lines and filling boxes. The GET/PUT replacements are quite
  3576. slow as well.
  3577.  
  3578. If you use PRINT in conjunction with GN4Write or GN4WriteLn, be
  3579. sure to save the cursor position before the PRINT and restore it
  3580. afterwards. BASIC and BasWiz share the same cursor position, but
  3581. each interprets it to mean something different.
  3582.  
  3583. The GN0 (360x480x256) and GN1 (320x400x256) routines use
  3584. nonstandard VGA modes. The GN1 routines should work on just
  3585. about any VGA, however. The GN0 routines will work on many VGAs,
  3586. but are somewhat less likely to work than the GN1 routines due
  3587. to the techniques involved.
  3588.  
  3589.                      Miscellaneous Notes               page 73
  3590.  
  3591.  
  3592.  
  3593. The GN0Write, GN0WriteLn, GN1Write and GN1WriteLn routines are
  3594. somewhat slow in general and quite slow when it comes to
  3595. scrolling the screen.
  3596.  
  3597. The G1Border routine is normally used to select the background
  3598. (and border) color for SCREEN 1 mode. It can also be used in
  3599. SCREEN 2 mode, where it will change the foreground color
  3600. instead. Note that this may produce peculiar results if an EGA
  3601. or VGA is used and it isn't locked into "CGA" mode, so be
  3602. careful if your program may run on systems with displays other
  3603. than true CGAs.
  3604.  
  3605. Note that you can GET an image in SCREEN 1 and PUT it in SCREEN
  3606. 2! It'll be shaded instead of in colors. This is a side-effect
  3607. of the CGA display format.
  3608.  
  3609. The first two elements of a GET/PUT array (assuming it's an
  3610. integer array) tells you the size of the image. The first
  3611. element is the width and the second is the height, in pixels.
  3612. Actually, that's not quite true. Divide the first element by 2
  3613. for the width if the image is for SCREEN 1, or by 8 if for
  3614. SCREEN 13.
  3615.  
  3616.                          Error Codes                   page 74
  3617.  
  3618.  
  3619.  
  3620. The expression evaluator returns the following error codes:
  3621.  
  3622.    0    No error, everything went fine
  3623.    2    A number was expected but not found
  3624.    4    Unbalanced parentheses
  3625.    8    The expression string had a length of zero
  3626.    9    The expression included an attempt to divide by zero
  3627.  
  3628.  
  3629.  
  3630. The far string handler does not return error codes. If an
  3631. invalid string handle is specified for FSSet, it will be
  3632. ignored; if for FSGet, a null string will be returned. If you
  3633. run out of memory for far strings, an "out of memory" error will
  3634. be generated (BASIC error #7). You can prevent this by checking
  3635. available memory beforehand with the SETMEM function provided by
  3636. QuickBasic. Far string space is allocated as needed in blocks of
  3637. just over 16 Kbytes, or 16,400 bytes to be exact.
  3638.  
  3639.  
  3640.  
  3641. The telecommunications handler returns the following error codes
  3642. for TCInit:
  3643.  
  3644.    0    No error, everything A-Ok
  3645.    1    The comm handler is already installed
  3646.    2    Invalid comm port specified
  3647.    3    Not enough memory available for input/output buffers
  3648.  
  3649.  
  3650.  
  3651. The telecommunications handler returns these error codes for
  3652. Xmodem Send:
  3653.  
  3654.  -13    FATAL   : Unsupported transfer protocol
  3655.  -12    FATAL   : Excessive errors
  3656.  -11    FATAL   : Keyboard <ESC> or receiver requested CANcel
  3657.   -5    WARNING : Checksum or CRC error
  3658.   -1    WARNING : Time-out error (receiver didn't respond)
  3659.    0    DONE    : No error, transfer completed ok
  3660.   >0    ERROR   : File problem (see file error codes)
  3661.  
  3662.                          Error Codes                   page 75
  3663.  
  3664.  
  3665.  
  3666. The file services return the following error codes: (The
  3667. asterisk "*" is used to identify "critical errors")
  3668.  
  3669.    0    No error
  3670.    1    Invalid function number (usually invalid parameter)
  3671.    2    File not found
  3672.    3    Path not found
  3673.    4    Too many open files
  3674.    5    Access denied (probably "write to read-only file")
  3675.    6    Invalid file handle
  3676.    7    Memory control blocks destroyed
  3677.    8    Insufficient memory (usually RAM, sometimes disk)
  3678.    9    Incorrect memory pointer specified
  3679.   15    Invalid drive specified
  3680. * 19    Tried to write on a write-protected disk
  3681. * 21    Drive not ready
  3682. * 23    Disk data error
  3683. * 25    Disk seek error
  3684. * 26    Unknown media type
  3685. * 27    Sector not found
  3686. * 28    Printer out of paper
  3687. * 29    Write fault
  3688. * 30    Read fault
  3689. * 31    General failure
  3690. * 32    Sharing violation
  3691. * 33    Lock violation
  3692. * 34    Invalid disk change
  3693.   36    Sharing buffer overflow
  3694.  
  3695.  
  3696.  
  3697. A "critical error" is one that would normally give you the
  3698. dreaded prompt:
  3699.  
  3700.    A>bort, R>etry, I>gnore, F>ail?
  3701.  
  3702. Such errors generally require some action on the part of the
  3703. user. For instance, they may need to close a floppy drive door
  3704. or replace the paper in a printer. If a critical error occurs on
  3705. a hard drive, it may indicate a problem in the drive hardware or
  3706. software setup. In that case, the problem may possibly be
  3707. cleared up by "CHKDSK /F", which should be executed directly
  3708. from the DOS command line (do not execute this by SHELL).
  3709.  
  3710.                        Troubleshooting                 page 76
  3711.  
  3712.  
  3713.  
  3714. Problem:
  3715.    QB says "subprogram not defined".
  3716.  
  3717. Solution:
  3718.    The definition file was not included. Your program must
  3719.    contain the line:
  3720.       REM $INCLUDE: 'BASWIZ.BI'
  3721.    before any executable code in your program. You should also
  3722.    start QuickBasic with
  3723.       QB /L BASWIZ
  3724.    so it knows to use the BasWiz library.
  3725.  
  3726.  
  3727. Problem:
  3728.    LINK says "unresolved external reference".
  3729.  
  3730. Solution:
  3731.    Did you specify BasWiz as the library when you used LINK? You
  3732.    should! The BASWIZ.LIB file must be in the current directory
  3733.    or along a path specified by the LIB environment variable
  3734.    (like PATH, but for LIB files).
  3735.  
  3736.  
  3737. Problem:
  3738.    The virtual windowing system doesn't display anything.
  3739.  
  3740. Solution:
  3741.    Perhaps you left out the WUpdate routine? If so, the shadow
  3742.    screen is not reflected to the actual screen and nothing will
  3743.    appear. The screen also needs to be in text mode (either no
  3744.    SCREEN statement or SCREEN 0). Finally, only the default
  3745.    "page zero" is supported on color monitors.
  3746.  
  3747.  
  3748. Problem:
  3749.    The virtual windowing system causes the display to flicker on
  3750.    CGAs.
  3751.  
  3752. Solution:
  3753.    Use the WSnow routine to get rid of it. Unfortunately, this
  3754.    will slow the display down severely. You might want to
  3755.    upgrade your display card!
  3756.  
  3757.                        Troubleshooting                 page 77
  3758.  
  3759.  
  3760.  
  3761. Problem:
  3762.    QuickBasic doesn't get along with the Hercules display
  3763.    routines.
  3764.  
  3765. Solution:
  3766.    Are you using an adapter which mimics Hercules mode along
  3767.    with EGA or VGA mode? QuickBasic doesn't like that, since it
  3768.    thinks you'll be using EGA or VGA mode. Use the stand-alone
  3769.    compiler (BC.EXE) instead of the environment (QB.EXE) and you
  3770.    should be fine. You might also consider getting a separate
  3771.    Herc adapter and monochrome monitor. It's possible to combine
  3772.    a Hercules monochrome adapter with a CGA, EGA or VGA. This
  3773.    does, however, slow down 16-bit VGAs.
  3774.  
  3775.  
  3776. Problem:
  3777.    QB says "out of memory" (or "range out of bounds" on a DIM or
  3778.    REDIM).
  3779.  
  3780. Solution:
  3781.    If you're using the memory management/pointer routines,
  3782.    you've probably allocated too much memory! You need to leave
  3783.    some for QuickBasic. Use the SETMEM function provided by
  3784.    BASIC to determine how much memory is available before
  3785.    allocating memory. The amount needed by QuickBasic will
  3786.    depend on your program. The primary memory-eaters are arrays
  3787.    and recursive subprograms or functions.
  3788.  
  3789.    Many of the BasWiz routines need to allocate memory,
  3790.    including the virtual window manager, telecommunications
  3791.    handler, and memory management system. Besides checking with
  3792.    SETMEM to make sure there's memory to spare, don't forget to
  3793.    check the error codes returned by these routines to make sure
  3794.    they're working properly!
  3795.  
  3796.  
  3797. Problem:
  3798.    The cursor acts funny (appears when it shouldn't or vice
  3799.    versa).
  3800.  
  3801. Solution:
  3802.    Try locking your EGA or VGA into a specific video mode using
  3803.    the utility provided with your display adapter. Cursor
  3804.    problems are usually related either to "auto mode detection"
  3805.    or older EGAs.
  3806.  
  3807.                        Troubleshooting                 page 78
  3808.  
  3809.  
  3810.  
  3811. Problem:
  3812.    The BCD trig functions return weird results.
  3813.  
  3814. Solution:
  3815.    Make sure you've made room in your BCD size definition for
  3816.    some digits to the left of the decimal as well as to the
  3817.    right! Calculations with large numbers are needed to return
  3818.    trig functions with high accuracy.
  3819.  
  3820.  
  3821. Problem:
  3822.    The G#MirrorH routine is -almost- working right, but the
  3823.    results are truncated or wrapped to one side.
  3824.  
  3825. Solution:
  3826.    Make your GET image a tad wider. The number of pixels wide
  3827.    must be evenly divisible by four in SCREEN 1, or by eight in
  3828.    SCREEN 2.
  3829.  
  3830.              Using BasWiz with P.D.Q. or QBTiny        page 79
  3831.  
  3832.  
  3833.  
  3834. Most of the BasWiz routines will work with current versions of
  3835. Crescent's P.D.Q. or my QBTiny library without modification. The
  3836. major exceptions are the expression evaluator, the BCD and
  3837. fraction math routines, and the polygon-generating graphics
  3838. routines, due to their use of floating point math.
  3839.  
  3840. Older versions of the P.D.Q. library do not support the SETMEM
  3841. function, which is required by many BasWiz routines. If your
  3842. version of P.D.Q. is before v2.10, contact Crescent for details
  3843. on how to upgrade to the latest version.
  3844.  
  3845. Note that, with some older versions of P.D.Q., it is important
  3846. to list PDQ.LIB as the last library on the command line when
  3847. LINKing. This bug has also been resolved in current versions.
  3848.  
  3849. Some older versions of P.D.Q. do not support dynamic string
  3850. functions. In that case, you will have to add the STATIC keyword
  3851. to all BasWiz BASIC string functions and recompile them.
  3852.  
  3853. QBTiny does not support dynamic arrays. You will be unable to
  3854. use any routines which require dynamic arrays with QBTiny.
  3855.  
  3856.                            Credits                     page 80
  3857.  
  3858.  
  3859.  
  3860. For some of the reference works I have used in writing BasWiz,
  3861. see the BIBLIO.TXT file.
  3862.  
  3863. Crescent Software provided me with a copy of P.D.Q. so I could
  3864. test for any compatibility problems between it and BasWiz.
  3865.  
  3866. The inverse hyperbolic trig functions are based on a set of
  3867. BASIC routines by Kerry Mitchell.
  3868.  
  3869. The 360x480 256-color VGA mode was made possible by John
  3870. Bridges' VGAKIT library for C. Two of the most vital low-level
  3871. routines are based directly on code from VGAKIT. If you use C,
  3872. check your local BBS for this excellent library. Last I looked,
  3873. VGAKIT50.ZIP was the current version.
  3874.  
  3875. The 320x400 VGA mode was made possible by Michael Abrash's
  3876. graphics articles in Programmer's Journal. Since the sad demise
  3877. of P.J., Mr. Abrash's articles can be found in another excellent
  3878. tech magazine, Dr. Dobb's Journal.
  3879.  
  3880. Definicon Corp very kindly released a public-domain program
  3881. called SAMPLE.C which shows how to access the 64k banks used by
  3882. extended VGA 256-color modes. This was the key to the GN5xxx
  3883. routines (the so-called "tech ref" section of my Boca SuperVGA
  3884. manual referred me to IBM's VGA docs, which would be utterly
  3885. useless in accessing these modes).
  3886.  
  3887.