Scroll Win in BASIC
|Column Tag:||Basic School
Scroll that Window in BASIC!
By Dave Kelly, General Dynamics, MacTutor Editorial Board
One of the problems of using BASIC as a programming language is the inability to access the Macintosh ROM routines. Many of the toolbox and graphics functions are built into MSBASIC (2.0 or later) such as MENU, WINDOW, EDIT FIELD, BUTTON which we have covered in prior issues of MacTutor. This month we will discover the use of some specialized LIBRARY routines which make use of Macintosh ROM routines which before this time were only accessible to other programming languages (C, Forth, 68000 Assy., and others).
Fig. 1 Basic Scrolls Windows!
The routines which are featured in this month's column come from Clear Lake Research of Houston, Texas. It should be noted that the program and discussion which follows will not work without the Clear Lake Research Libraries. Also the MacTutor source disk will contain only the listing published here and will not include any of the CLR routines used by this program listing, since they are obviously a commercial product for sale. The CLR Libraries are required to run the program. [We encourage you to obtain these routines (see ad for ordering info or watch the MacTutor mail order store for this product), since they greatly enhance the power of Microsoft Basic, which is sadly missing a majority of the toolbox capability. -Ed.]
First a few comments about library routines: In November we discussed the process of installing BASIC Libraries. Please refer to that issue for information about installing libraries. It has come to my attention that the ID numbers for some desk accessories may conflict with the resource ID numbers for libraries even though resource ID numbers of different types may have the same ID according to Inside Macintosh. The range of ID numbers should be as follows:
-32767 through -16385 Reserved; do not use
-16384 through -1 Used for system resources owned by other system resources (see IM)
0 through 127 Used for other system resources
128 through 32767 Available for your use in whatever way you wish
Resource ID numbers for desk accessories should be between 12 and 31 inclusive. Therefore, it is advisable that the ID numbers for libraries be assigned to numbers 128 through 32767. In addition, the CLR Libraries have been assigned resource ID numbers of 10000 thru about 12000 with a few exceptions. You can be sure not to have ID numbers that don't conflict with the CLR Libraries if you use ID 20000 through 32767 for your own libraries. This applies to the _Eject routine that appeared in the November issue. I recommend that the resource ID number for the libraries be changed as explained above. The _Eject routine works fine in most cases, but there is a chance that the ID number will conflict with something else.
Please be aware of how to define variables for use in your libraries. For some reason Integer variables used in Libraries routines should be defined with a % (following the variable name) instead of using the DEFINT statement. I tried the statement, DEFINT a-z to define integers throughout the entire program and when a library routine is executed an error results. You may use DEFINT as long as the name of the variables used appear later in alphabetical order than the variables defined in the DEFINT statement. I saw no difference between BASIC 2.0 and BASIC 2.1 as far as this problem is concerned.
The demo program demonstrates the use of the CLR Libraries for implementing scroll bars within your BASIC programs. The standard Macintosh scroll bar is a predefined type of dial accessed through the Control Manager. The CLR Libraries manual (for CLR ToolLib) describes the functions which are provided for using the scroll bar to scroll text stored in an array of string variables. In summary they are:
CLR Routine Purpose
ActiveScroll: Scroll bar is made active
DisposeScroll: Scroll bar is disposed and memory is released
HideScroll: Scroll bar is made invisible
InactiveScroll: Scroll bar is made inactive
MoveScroll: Moves a scroll bar
NewScroll: Create a new scroll bar
ScrollText: Scroll text in an array of string variables
GetScrollValue: Get value of current scroll box.
SetScrollValue: Set value of current scroll box.
ShowScroll: Make scroll bar visible
SizeScroll: Set the size of the scroll bar.
The first thing to do in creating your own scroll bars is to determine what you want to scroll. Set up an array of string variables ( in the program the string is named text$ ). Next, decide size and location of the rectangle where the scrolling should take place. The array TextRect% stores the rectangle location of the area where the text will scroll. The array ScrollText% stores the rectangle indicating the size and location of the scroll bar itself. The scroll bar may be resized with the SizeScroll command if necessary. A handy routine, SetRect, is used to set up the rectangles. This saves you from having to set each variable of the rectangle array one at a time. You simply type SetRect statement, the rectangle array to define and then the (X1,Y1)-(X2-Y2) coordinates. Example:
SetRect ScrollRect%(1), 151,30,167,250
where X1=151,Y1=30, X2=167, Y2=250. Be sure to initialize any variables to be used in a library routine by setting the variable equal to zero or some other number. This reserves the variable space so that the library routine can find it when it is used by the routine. Also don't forget to define all of your array variables with DIM statements.
Scroll bars must be initially defined using the statement NewScroll. NewScroll creates a scroll bar and returns a handle. Many of the CLR Libraries use handles to point to a location in memory. In this case, the scroll bar handle points to the scroll bar to be used by the statements that follow. Don't try to print or restore a handle as the handle variable is not in a standard MSBASIC format. In the NewScroll statement you tell the routine what variable name you want to call the handle, in the program the handle is ScrollBar. The handle name is followed by the scroll rectangle array, then by a parameter indicating if the scrollbar will be visible (1=visible, 0=invisible). You may later hide or show the scroll bar with the HideScroll or ShowScroll statements. The next parameters in the NewScroll statement is the minimum value for the scroll bar indicator followed by the maximum value for the scroll bar indicator and then followed by the initial starting value of the scrollbar. Just think of the scroll box as a pointer in which the one end of the scroll bar is the minimum value and the other end is the maximum value.
The ScrollText statement is used to set the position of the scroll box. The Loop: routine in the program loops through the ScrollText statement with different values of the LineNum% and Top% (see program). Top% is used as an index of the line to appear at the top of the scrolled rectangle. If Top%=0 then the scroll bar will be redrawn. The value of LineNum% indicates the line of text which should be highlighted. When Top%=0 the scroll bar and text is redrawn and the indicated line will be highlighted. LineNum%=0 when no text is to be highlighted.
The MouseDown: routine determines if the mouse has been pressed inside the text rectangle. If it is then the line number, LineNum%, is calculated. The routine SetPt sets the Pt%(1) = MOUSE(2) and Pt%(2) = MOUSE(1). PtInRect determines if the point Pt% is in the text rectangle. The remainder of the MouseDown routine calculates the linenumber to be highlighted.
In your own program, it is the program's responsibility to determine which line of text was clicked. The Scroll Demo program will select the line of text pointed to when the cursor is selected. More possibilities could be worked out by determining when the mouse is dragged across the text rectangle. Also you should be aware of the size and style of the font being displayed in the text rectangle. The number of pixels ScrollText spaces down between lines of text is equal to the existing font's height+descent+4. The height and descent can be obtained with the GetFontInfo statement (another CLR routine). Text is always printed within the rectangle passed to the ScrollText routine. If you wish to have a border around the text rectangle, you will have to draw it yourself. The BASIC statement LINE is used in the demo program to draw a border around the text. If the window containing the scroll bar needs refreshing, the text and scroll bar will automatically be refreshed when the ScrollText statement is issued (with Top%=0 and ScrollText is issued, the text is redrawn). The border (if any) will not be automatically drawn. You should refresh the border as you would any other window needing refreshing.
To close out the scroll bar, use DisposeScroll. If your program terminates prematurely because of syntax error or by typing command-".", then you should type DisposeScroll ScrollBar, where ScrollBar is the handle to the ScrollBar. If you forget to dispose the scrollbar and forget the scrollbar handle the scrollbar will not go away without quitting BASIC. Also the memory required for the scrollbar will still be reserved and cannot be reclaimed till the scrollbar is disposed of.
Some other routines used in the program:
SortString: Sorts a string array alphabetically
Shuffle: Randomizes a string array.
ChangeCursor: Set the cursor to a cursor in the system resource file.
The usefullness of these routines and other CLR routines will be left to future articles.
'By Dave Kelly & Clear Lake Research
'REQUIRES CLR LIBRARIES
' This program illustrates the use of the scroll bar
' as well as a few other statements from
' CLR ToolLib and CLR MathStatLib.
' The names of 15 famous chessplayers are presented
' on the screen and can be scrolled with the scroll bar.
' The arrows as well as the scroll box are functional.
' A selected name will be highlighted.
' The order of the names can be alphabetized or randomized.
' When the "OK button" is pressed the selected name is printed.
OPTION BASE 1
LOCATE 2,9:PRINT "Professor Mac's"
LOCATE ,9:PRINT "Scroll Bar Demo"
LOCATE ,2:PRINT "Select your favorite World Chess Champion."
FOR i= 1 TO 15
FOR i= 1 TO 15
' clear rest of menu bar
FOR i= 2 TO 5
' visible with values ranging from 1-10 startring at 1
' these parameters are reset by a subsequent
' ScrollText statement
IF MOUSE(0)<>0 THEN GOSUB MouseDown
IF d%=1 THEN GOSUB Buttonselected
IF d%= 5 THEN GOSUB Update
IF MENU(0)<> 0 THEN GOSUB MenuEvent
WINDOW OUTPUT 2
PRINT "Last item selected was"
IF LineNum%=0 THEN PRINT "Nothing" ELSE PRINT text$(LineNum%)
WINDOW OUTPUT 1
BUTTON CLOSE 1
WINDOW CLOSE 2
WINDOW 1,"Scroll Demo",(2,40)-(510,290),1
'That's all that's required since ScrollText will redraw the text.
' Is mousedown in TextRect?
IF NOT inRect% THEN MouseOver
' IF 0<= Pt%(1) - TextRect%(1) <= 19 then LineNum%=Top%
' IF 20<= Pt%(1) - TextRect%(1) <= 39 then
' LineNum%=Top%+1 etc.
' The following is a fast method of calculating LineNum%
' Make ScrollText redraw text with LineNum% selected
'wait for mouse button to come up
ChangeCursor 4 'make cursor the watch
ON MENU(1) GOSUB SortIt,ScrambleIt,Finish
Top%=0 'so it will be redrawn
Shuffle 15,Index%(1) 'scramble the indexes
FOR i= 1 TO 15
FOR i= 1 TO 15
DATA Spassky, Fischer,Karpov,Kasparov
Questions and Answers
Thank you to those who have sent in suggestions or questions that they may have concerning this column. MacTutor will continue to try to provide the information that you are hungry for. In response to letters received, here are the following questions/suggestions and answers:
Q. How do you tell your program to quit to the finder, instead of BASIC?
A. The command SYSTEM terminates the program and goes to the finder.
Q. A call to a sort routine is badly needed.
A. A quicksort program was printed in the Sept. MacTutor (Ask Prof. Mac column). CLR MathStat Library has various sort routines available. [Basic Smoke is also marketing both bubble and shell sort routines, string sorts with tags in assembly. Contact Jim Shores, 139 Alpine Dr., Closter, NJ 07624. Price is just $4 for each sort routine (these are in assembly for Basic). -Ed. ]
Q. Calls to clear parts of the screen would be helpful.
A. One way to clear a part of the screen is to use the GET and PUT statement to clear a section of the screen. Use PUT to read in a blank section of the desired size, then use GET to write it back whenever it is needed. Another way might be to open a window over the section to clear and then close it again without refreshing the screen.
Q. How can the defaults for the TAB function be changed while editing? As it is, one might as well use the space bar.
A. I don't know of any way to change the TAB fields.
Please continue to send in your suggestions or questions.