Fonts and Cursors
Volume Number: 2
Issue Number: 10
Column Tag: Basic School
On Fonts and Cursors 
By Dave Kelly, MacTutor Editorial Board
Controlling the Cursor
As I've always advocated in previous columns, whenever I write a program I try
to make it look as much as possible like a commercial program. You know, like the
ones you pay lots of money for. Hopefully, we can learn the little tricks that help to
make our programs look professional.
Have you ever wondered how to make your cursor behave the way that the cursor
does in MacWrite or other applications? For example, notice when you move the
cursor from the active region of a window and into the menu bar or scroll bars, the
cursor changes to the default (ARROW) cursor. MS Basic allows you to change the
cursor by using the INITCURSOR and SETCURSOR toolbox commands, however, MS Basic
doesn't provide a way to tell when the cursor is moved into or out of windows. The
closest thing it has is the WINDOW() or the MOUSE() functions. With the mouse
functions you can read the location of the mouse at any given time. The window
functions return the active window and the height and width of the window. What isn't
given is the location of the window in relation to the entire screen. I suppose that with
some playing around, one of you clever programmers out there could fudge your way
around it. There is an easier way.
Fortunately we can still rely on our CLR libraries for help in calling the
Macintosh ROM routines that we need. (By the way, CLR Libraries are a must if you're
using MS BASIC. Rumor has it that the Libraries will be supported by the yet to be
announced/released MS BASIC Compiler). There is one simple Quickdraw toolbox call
(via ToolLib) which makes it easy to change our cursor when it crosses outside of the
active region of our window. A demonstration of how this is accomplished is given in
the Loop routine of this month's sample programs (there are two versions, one
MSBASIC, the other ZBasic).
Change the Cursor!
loop:
Wx=WINDOW(2):Wy=WINDOW(3)
SetRect! Rect(0),0,0,Wx,Wy
x=MOUSE(0):Pt(1)=MOUSE(1):Pt(0)=MOUSE(2)
PtInRect! Pt(0),Rect(0),Result
IF Result=False THEN INITCURSOR ELSE changecursor! 1
GOTO loop
The routine can be explained as follows: First the window width Wx and window
height Wy are found with the window functions. These will be our reference. MS Basic
sets the grafport (the output path) to the active window. We want to use the boundaries
of this window to determine if the cursor should be an arrow or an insertion cursor.
Next we define a rectangle which is the same size as the current active region of the
window. The width and height returned by the window functions give the coordinates of
the active part of the window. Next we record the mouse position with the MOUSE
functions. The variable Pt() will hold the horizontal and vertical point on the screen
which the cursor points to. In the next statement we test to see if the point where the
cursor is located is inside the rectangle (the active region) or not. If it is inside then
we set the cursor to the insertion cursor (using the ChangeCursor statement from
ToolLib) or else we set the initialize the cursor as the arrow cursor.
Fig. 1 MS Basic Font Sample. Note Font Order
The concept behind this is simple yet there are probably some of you that didn't
know it could be done with MS BASIC. For ZBasic the procedure and concept is the same,
but the syntax is slightly different.
Well, so much for changing the cursor.
Word Processing in Basic?
What about other things that we've seen in the commercial programs that we
would like to mimic in BASIC? How about a text editor? With all the text processing
power that the Macintosh has, this is still a weak area when it comes to BASIC. A decent
text editor program cannot be written in MSBASIC at this time. I mean the type with
editing of text just like in MacWrite. We need more access to the toolbox. We could try
using the EDIT FIELD statement to generate a large text field to type into, but this still
doesn't give us enough control to check the text being typed in on the keyboard. When
using default text in an EDIT FIELD statement in either ZBasic or MS BASIC, the text
appears selected within the Edit field. This is not desirable in a text editor application,
e specially if text is continually added to the Edit Field as characters are typed (if
monitoring the keypresses were possible in an edit field). In MS BASIC we would have
to monitor each and every character being typed (via INKEY$) and keep track of all the
cursor locations and mouse clicks in a brute force manner. I dunno, maybe that's how
word processors like MacWrite are written. Anyway, in BASIC that method would seem
to be extremely tedious. Count MS BASIC out for that sort of processing. ZBasic
version 3.02 which will soon be released will now allow you to poll the keyboard via
events passed through the DIALOG statements. This fix was necessary to poll the
keyboard when menus or other events were active. See the Basic update section at the
end of this column to see what some of the improvements are in version 3.02. I have
not yet had a chance to test out this improvement.
Fonts We Can Do!
Here's another one for you: How does MacWrite get all the Font names from the
system file into a menu? Also how does it know what size of font is really available?
Using Toolbox calls, the answer to this is really trivial. However, for Basic
programmers, the answer is not so simple. The new libraries given in the JUNE 1986
MacTutor give us a possible way to do this. I have to admit though, the Toolbox routine
that I really wanted to use, ADDRESMENU, was not available even with CLR Libraries.
I've been told that there are other routines in the works that are soon to be released.
Both ZBasic and MS BASIC with CLR Libraries would not allow me to use the
ADDRESMENU Toolbox call. This call takes the type of resource like FONT and adds all
the names of the fonts into items in a menu from the open resource file. Obviously this
would be a quick way to build a font menu! The ZBasic manual includes ADDRESMENU on
the list of Toolbox calls, however, the ZBasic event handling doesn't know it is even
there. I wonder what the menu manager toolbox calls are good for with ZBasic anyway,
since you can't mix ZBasic menu statmements with toolbox menu statements. This
brings us to the subject of bugs
ZBasic Goofed on Events
The reason that toolbox menu calls and ZBasic menu statements cannot be mixed
is that ZBasic does a GetNextEvent at the beginning of each statement in the program.
The result of this effect is to create problems if you are trying to implement a toolbox
event loop. For one thing, it affects menu bar selection. If you select multiple menu
items in rapid succession in our ZBAsic sample, before each item can execute, you will
get a system crash. Thus menus can never be bullet proof. This bug is related to the
problem of getting events because the event loop is intimately connected with the menu
manager. Another problem is that menu selection is very slow as this month's example
shows. If you change the font and then select from the file menu quickly, the desired
font will not display because the file item was chosen before the font item finished
executing Theoretically this can't happen on the Mac, and doesn't in MS Basic, but since
ZBasic is calling GetNextEvent all the time, events can happen out of order, resulting in
strange behavior. There may be many more ZBasic bugs which turn out to be related to
this problem of how ZBasic handles events. Although the manual suggests that virtually
every toolbox call is available from ZBasic, some of them don't work or are not
implemented in the language and don't do anything when used, like ADDRESMENU. There
is no way to determine which ones work and which don't short of trying them all.
Fig. 2 ZBasic Sample. Note different font order
Fonts, Menus & ZBasic
The method for determining what fonts are available on your system when using
ZBASIC is found on page D-55 of the third edition of the ZBASIC manual. The font names
are read one at a time in a loop using GETFONTNAME and stored away. Our sample
program stores away the value of the font number and the font name in the arrays
Font$() and Font(). The application font (#1) is skipped because it is the same as the
Geneva font. (#3). Now that the font names are found, the font sizes are determined as
needed using the REALFONT toolbox statement. Each time the font menu is selected a
check mark is placed by the correct item in the font menu and the size menu is
redrawn. Actually, the size menu is re- created (from scratch) each time. Here we
have another ZBasic bug. It seems that the META characters don't work right when
re-programming a menu item. They do work fine the first time the menu is created (or
if it is re created from scratch with the new changes made). The entire menu has to be
re- created though. This is one reason the menu selection in our sample is so slow.
Changing Font and Size
In this month's sample program the items selected in the File menu (except for
Quit) will print in the program window and in the selected font and size. The text font
and size is changed in the ZBasic program with the TEXT statement. With the TEXT
statement you may change font, size, face and mode in a single statement. The manual
states that 'you may also use the text commands of quickdraw'. However, if you
substitute TEXTFONT and TEXTSIZE statements in the sample Basic program, the proper
TEXTFONT and TEXTSIZE will not print. Another ZBasic bug? Until this problem is
fixed I recommend that you always use the TEXT statement for changing fonts, sizes,
etc.
Back to MS Basic
In MSBasic reading the Font menu is just a bit harder than with ZBasic. First we
have to be sure that the system resources are available. MSBasic's HFS problems may
cause some difficulty here. Use the pathname for your System File (via System Folder)
to specify exactly where the System File is located. Next we use GETINDRES to get a
handle for each resource with the type "FONT". Then with GETRESINFO we can get the
FONT names and ID# and store them away. Note that ID# is returned and not font
numbers. Divide the font ID# by 128 to get the font number. Now that we have the font
number and name just like we did in the ZBasic program, we can use REALFONT to see
what sizes are available. The proper sizes are shown in outline mode (via
SETITEMSTYLE) in the size menu. (Note that the Size menu did not have to be redrawn
from scratch to get the menu formatting to change!).
In both sample programs a 'Style' menu could have been easily added. I'll leave
that to you. The next step is adding a means to edit text in the window. This will have to
be covered when that capability is adequately provided. Note that the MS Basic program
requires the following CLR libraries... from ToolLib: ChangeCursor, GetIndRes,
GetResInfo, SetRect, PtInRect, OpenResFile... from MathStatLib: SortString.... from
NewLibraries (see June 86 MacTutor): SetItemStyle.
BASIC UPDATE
Here is this month's report on the new BASICs recently released:
MS BASIC: Still no word on the release of the compiler, however Dave Smith was
able to get a peek at the compiler while at the Boston show last month. The compiler
will require you to create your programs via the MS Basic interpreter. The compiler is
supposed to be entirely compatible with CLR libraries. The interpreter is also being
updated(???) but the release date is still a Microsoft secret.
ZBASIC: I just received a new beta copy of version 3.02. which includes the
following additions/corrections:
• Zoom in and Zoom out can now be detected with DIALOG(8) and DIALOG(9).
• While edit fields are active the DIALOG statements will respond to SHIFT TAB
events, Mac Plus up,down,left, right and clear keys.
• Keypresses are now returned with DIALOG(16). This replaces INKEY$ during
event trapping.
• Enter or Return works for edit fields
• Add +256 to window type to disable Go-Away box, add +9 for zoom box, add
+16-23 for rounded corners on windows. Negative numbered windows for modal
dialog windows.
• HFS compatibility: The DIR command is fixed... DIR:
ROOT:FOLDER:FOLDER...:FILENAME. The EJECT command now works. FILES$ now
returns path names.
• Edit menu may now be disabled.
• New SHUTDOWN statement.
• Auto repeat SCROLL BUTTON.
• Various bug fixes too numerous to mention. More items will be fixed in the final
release version.
Be assured that Zedcor is working hard to fix bugs, etc. If you know of any,
please let them know. ZBasic still has great potential to become a significant
development product on the Macintosh.
PCMacBasic: Improvements on this have not yet been released although there
have been improvements to the packaging of the product. The manual will soon be
revised along with big improvements of the compiler to make it more HFS compatible
(the language already is HFS compatible). Toolbox documentation (a must!!) is being
worked on. By the way, if you don't like the funny block cursor appearing when quitting
the PCMacBasic programs you create, you can hide the cursor with the following
statement: LOCATE 1,1,1,1,1 . Place it somewhere before the end of the program and
the cursor will be hidden before the program terminates.
True BASIC: They have revised their pricing of their runtime package from
$500 to $150! [Thanks to Dave Kelly for this concession. -Ed.] Yes, that means to
create your own applications you have to invest about $300. You may then create
applications for your own non-commercial use. A commercial license is another $350.
If you don't like this policy, why not drop them a line. Realize however, that this
pricing is still not that much different from the pricing of the MS Basic interpreter +
compiler (although we don't know what the exact price will be). I've received copies of
the True Basic 3-dimensional Graphics library (nice package) and Sorting/Searching
and Advanced String Library. I may comment on these at a later date if I find that the
runtime package is acceptable. I have not yet seen the runtime package.
' FONT SAMPLE DEMO 1
' MacTutor©1986
' By Dave Kelly
'MS BASIC 2.10 Requires CLR Libraries
DEFINT A-z
LIBRARY "Samplelib
filerefnum%=0
openresfile! "Microsoft BASIC 2.10:System Folder:System",
filerefnum%
changecursor! 4
DIM Font(127), Font$(127), Check(127), Pt(1), Rect(3), S
Sizecheck(6)
hand!=0:id%=0:type$="FONT":A$="":x%=0
fontstyle9=0:fontstyle10=0:fontstyle12=0
fontstyle14=0:fontstyle18=0:fontstyle24=0
Result=0
X1=50:Y1=130:X2=460:Y2=240
False=0:True=NOT False
WINDOW 1,"Untitled",(X1,Y1)-(X2,Y2)
MENU 1,0,1,"File
MENU 1,1,1,"New
MENU 1,2,1,"Open
MENU 1,3,1,"Close
MENU 1,4,1,"Save
MENU 1,5,1,"Save As...
MENU 1,6,1,"Quit
MENU 3,0,1,"Font
MENU 4,0,1,"Size
MENU 5,0,0,