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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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.