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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.