home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / baswiz19.zip / BASWIZ.DOC < prev    next >
Text File  |  1993-02-01  |  156KB  |  3,916 lines

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