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

Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »

Price Scanner via MacPrices.net

New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.