TweetFollow Us on Twitter

Scroll Win in BASIC
Volume Number:2
Issue Number:1
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:

Range Description

-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.

'Scroll Demo
'By Dave Kelly & Clear Lake Research
'©MACTUTOR 1985
'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.

LIBRARY"ToolLib"
LIBRARY"MathStatLib"
OPTION BASE 1
DIM TextRect%(4),ScrollRect%(4),text$(15)
DIM Pt%(2),Index%(15),Temp$(15)
Initialize:
    ScrollBar=0
    Top%=0
    inRect%=0
    LineNum%=0
    WINDOW 1,,(60,40)-(250,330),2
    WINDOW 2,,(280,100)-(500,200),2
    TEXTFONT(0)
    LOCATE 2,9:PRINT "Professor Mac's"
    LOCATE ,9:PRINT "Scroll Bar Demo"
    TEXTFONT(3):TEXTSIZE(9)
    LOCATE ,2:PRINT "Select your favorite World Chess Champion."
    TEXTSIZE(12):TEXTFONT(0)
    WINDOW 1
    SetRect TextRect%(1),20,30,151,250
    SetRect ScrollRect%(1),151,30,167,250
    FOR i= 1 TO 15
        Index%(i)=i
    NEXT i
    BUTTON 1,1,"OK",(55,260)-(125,280)
    FOR i= 1 TO 15
        READ text$(i)
    NEXT i
    MENU 1,0,1,"Order"
    MENU 1,1,1,"Alphabetical"
    MENU 1,2,1,"Randomized"
    MENU 1,3,1,"Quit"
' clear rest of menu bar
    FOR i= 2 TO 5
        MENU i,0,1,""
    NEXT i

CreateScroll:
    LINE (20,30)-(151,249),,b
    NewScroll ScrollBar,ScrollRect%(1),1,1,10,1
    ' visible with values ranging from 1-10 startring at 1
    ' these parameters are reset by a subsequent 
    ' ScrollText statement

Loop:
ScrollText ScrollBar,TextRect%(1),text$(1),Top%,15,LineNum%
    IF MOUSE(0)<>0 THEN GOSUB MouseDown
    d%=DIALOG(0)
    IF d%=1 THEN GOSUB Buttonselected
    IF d%= 5 THEN GOSUB Update
    IF MENU(0)<> 0 THEN GOSUB MenuEvent
GOTO Loop

Buttonselected:
    WINDOW OUTPUT 2
    CLS
    TEXTFONT (0)
    LOCATE 2,4
    PRINT "Last item selected was"
    LOCATE 4,10
    IF LineNum%=0 THEN PRINT "Nothing" ELSE PRINT text$(LineNum%)
    TEXTFONT(1)
    WINDOW OUTPUT 1
    LineNum%=0:GOSUB Update
RETURN

Finish:
    DisposeScroll ScrollBar
    BUTTON CLOSE 1
    WINDOW CLOSE 2
    WINDOW 1,"Scroll Demo",(2,40)-(510,290),1
    CLS
END

Update:
Top%=0 
'That's all that's required since ScrollText will redraw the text.
    LINE (20,30)-(151,249),,b
RETURN

MouseDown:
    SetPt Pt%(1),MOUSE(1),MOUSE(2)
    PtInRect Pt%(1),TextRect%(1),inRect% 
 ' 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%
    LineNum%=(Pt%(1)-TextRect%(1))\20+Top%
      ' Make ScrollText redraw text with LineNum% selected
    Top%=0
ScrollText ScrollBar,TextRect%(1),text$(1),Top%,15,LineNum%
    
MouseOver:
    WHILE MOUSE(0)<>0:WEND 
'wait for mouse button to come up
RETURN

MenuEvent:
    ChangeCursor 4  'make cursor the watch
    ON MENU(1) GOSUB  SortIt,ScrambleIt,Finish
    MENU
    Top%=0    'so it will be redrawn
    INITCURSOR
RETURN

SortIt:
    SortString 15,text$(1)
    LineNum%=0
RETURN

ScrambleIt:
    Shuffle 15,Index%(1) 'scramble the indexes
    LineNum%=0
    FOR i= 1 TO 15
        Temp$(i)=text$(i)
    NEXT i
    FOR i= 1 TO 15
        text$(i)=Temp$(Index%(i))
    NEXT i
RETURN
DATA Morphy,Anderssen,Steinitz,Lasker,Capablanca
DATA Alekhine,Euwe,Botvinnik,Smyslov,Tal,Petrosian
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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.