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, especially 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 recreated 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), 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,""
GetFonts:
ON ERROR GOTO 9000
I=1
FOR x%=0 TO 127
GetIndRes! "FONT",x%,hand!
GetResInfo! hand!,id%,type$,A$
IF LEN(A$) THEN Font$(I)=A$:Font(I)=id%:I=I+1:A$="":id%=0
10 NEXT x%
ON ERROR GOTO 0
NumberofFonts=I-1
SortString! NumberofFonts,Font$(1),Font(1)
FOR J=1 TO NumberofFonts
IF Font$(J)="Geneva" THEN Check(J)=2 ELSE Check(J)=1
IF Font$(J)="Geneva" THEN g=Font(I)/128
MENU 3,J,Check(J),Font$(J)
NEXT J
REALFONT! g,9,fontstyle9
REALFONT! g,10,fontstyle10
REALFONT! g,12,fontstyle12
REALFONT! g,14,fontstyle14
REALFONT! g,18,fontstyle18
REALFONT! g,24,fontstyle24
FOR J=1 TO 6
Sizecheck(J)=1
NEXT J
Sizecheck(3)=2
GOSUB Fontsizemenu
TEXTFONT (2)
TEXTSIZE (12)
ON MENU GOSUB menuevent
MENU ON
INITCURSOR
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
Fontsizemenu:
MENU OFF
MENU 4,1,Sizecheck(1),"9 Point":SetItemStyle! 4,1,0
MENU 4,2,Sizecheck(2),"10 Point":SetItemStyle! 4,2,0
MENU 4,3,Sizecheck(3),"12 Point":SetItemStyle! 4,3,0
MENU 4,4,Sizecheck(4),"14 Point":SetItemStyle! 4,4,0
MENU 4,5,Sizecheck(5),"18 Point":SetItemStyle! 4,5,0
MENU 4,6,Sizecheck(6),"24 Point":SetItemStyle! 4,6,0
IF fontstyle9 = True THEN SetItemStyle! 4,1,8
IF fontstyle10 = True THEN SetItemStyle! 4,2,8
IF fontstyle12 = True THEN SetItemStyle! 4,3,8
IF fontstyle14 = True THEN SetItemStyle! 4,4,8
IF fontstyle18 = True THEN SetItemStyle! 4,5,8
IF fontstyle24 = True THEN SetItemStyle! 4,6,8
MENU ON
RETURN
menuevent:
Menunumber = MENU(0)
Menuitem = MENU(1)
MENU OFF:MENU
ON Menunumber GOSUB File,Editmenu,Font,Size
MENU ON
RETURN
File:
ON Menuitem GOSUB Newitem,Openitem,Closeitem,Saveitem,SaveAs,Quit
RETURN
Editmenu:
RETURN
Font:
FOR J=1 TO NumberofFonts
Check(J)=1
MENU 3,J,Check(J)
NEXT J
Check(Menuitem)=2
MENU 3,Menuitem,Check(Menuitem)
fontnum=Font(Menuitem)/128
REALFONT! fontnum,9,fontstyle9
REALFONT! fontnum,10,fontstyle10
REALFONT! fontnum,12,fontstyle12
REALFONT! fontnum,14,fontstyle14
REALFONT! fontnum,18,fontstyle18
REALFONT! fontnum,24,fontstyle24
GOSUB Fontsizemenu
CALL TEXTFONT (Font(Menuitem)/128):' Font # is id%/128
RETURN
Size:
FOR J=1 TO 6
Sizecheck(J)=1:MENU 4,J,1
NEXT J
Sizecheck(Menuitem)= 2
MENU 4,Menuitem,2
IF Menuitem=1 THEN TEXTSIZE (9)
IF Menuitem=2 THEN TEXTSIZE (10)
IF Menuitem=3 THEN TEXTSIZE (12)
IF Menuitem=4 THEN TEXTSIZE (14)
IF Menuitem=5 THEN TEXTSIZE (18)
IF Menuitem=6 THEN TEXTSIZE (24)
RETURN
Newitem:
PRINT "New Selected"
RETURN
Openitem:
PRINT "Open Selected"
RETURN
Closeitem:
PRINT "Close Selected"
RETURN
Saveitem:
PRINT "Save Selected"
RETURN
SaveAs:
PRINT "SaveAs Selected"
RETURN
Quit:
MENU RESET
END
9000 changecursor! 4
RESUME 10
BEEP:PRINT "OOPS"
REM FONT SAMPLE DEMO 2
REM MacTutor©1986
REM By Dave Kelly
REM ZBASIC configuration: Convert to Uppercase,Space Req'd, Locate
Y,X
REM Default variable type = I
WINDOW OFF
COORDINATE WINDOW
DEF MOUSE=1
DIM Font(20),Font$(20),Check(20),Pt(1),Rect(3),Sizecheck(6)
X1=50:Y1=130:X2=410:Y2=240
WINDOW 1,"Untitled",(X1,Y1)-(X2,Y2),265:REM Uses Zoom Window with
no close box
MENU 1,0,1,"File"
MENU 1,1,1,"/NNew"
MENU 1,2,1,"/OOpen"
MENU 1,3,1,"Close"
MENU 1,4,1,"Save"
MENU 1,5,1,"Save As..."
MENU 1,6,1,"/QQuit"
EDIT MENU 2
MENU 3,0,1,"Font"
"GetFonts":
I=1
CALL GETFONTNAME(0,A$)
IF LEN(A$) THEN Font$(I)=A$:Font(I)=0:I=I+1
FOR X=2 TO 127
CALL GETFONTNAME(X,A$)
IF LEN(A$) THEN Font$(I)=A$:Font(I)=X:I=I+1
NEXT X
NumberofFonts=I-1
FOR J=1 TO NumberofFonts
IF Font(J)=3 THEN Check(J)=2 ELSE Check(J)=1
MENU 3,J,Check(J),Font$(J)
NEXT J
Fontstyle9 = FN REALFONT(3,9)
Fontstyle10 = FN REALFONT(3,10)
Fontstyle12 = FN REALFONT(3,12)
Fontstyle14 = FN REALFONT(3,14)
Fontstyle18 = FN REALFONT(3,18)
Fontstyle24 = FN REALFONT(3,24)
FOR J=1 TO 6
Sizecheck(J)=1
NEXT J
Sizecheck(3)=2
GOSUB "Fontsizemenu"
TEXT 3,12
ON MENU GOSUB "menuevent"
MENU ON
"loop":
Wx=WINDOW(2):Wy=WINDOW(3)
Rect(0)=0:Rect(1)=0:Rect(2)=Wx:Rect(3)=Wy
X=MOUSE(0):Pt(0)=MOUSE(1):Pt(1)=MOUSE(2)
Result = FN PTINRECT(Pt(0),Rect(0))
IF Result=0 THEN CURSOR=0 ELSE CURSOR = 1
GOTO "loop"
"Fontsizemenu":
MENU OFF
MENU 4,0,1,"Size"
IF Fontstyle9 = 1 THEN MENU 4,1,Sizecheck(1),"<O9 Point" ELSE MENU
4,1, Sizecheck(1), "9 Point"
IF Fontstyle10 = 1 THEN MENU 4,2,Sizecheck(2),"<O10 Point" ELSE
MENU 4,2,Sizecheck(2),"10 Point"
IF Fontstyle12 = 1 THEN MENU 4,3,Sizecheck(3),"<O12 Point" ELSE
MENU 4,3,Sizecheck(3),"12 Point"
IF Fontstyle14 = 1 THEN MENU 4,4,Sizecheck(4),"<O14 Point" ELSE
MENU 4,4,Sizecheck(4),"14 Point"
IF Fontstyle18 = 1 THEN MENU 4,5,Sizecheck(5),"<O18 Point" ELSE
MENU 4,5,Sizecheck(5),"18 Point"
IF Fontstyle24 = 1 THEN MENU 4,6,Sizecheck(6),"<O24 Point" ELSE
MENU 4,6,Sizecheck(6),"24 Point"
MENU ON
RETURN
"menuevent":
Menunumber = MENU(0)
Menuitem = MENU(1)
MENU:MENU OFF
ON Menunumber GOSUB "File","Edit","Font","Size"
MENU ON
RETURN
"File":
ON Menuitem GOSUB "New","Open","Close","Save","SaveAs","Quit"
RETURN
"Edit":
RETURN
"Font":
FOR J=1 TO NumberofFonts
Check(J)=1
MENU 3,J,Check(J)
NEXT J
Check(Menuitem)=2
MENU 3,Menuitem,Check(Menuitem)
Fontstyle9 = FN REALFONT(Font(Menuitem),9)
Fontstyle10 = FN REALFONT(Font(Menuitem),10)
Fontstyle12 = FN REALFONT(Font(Menuitem),12)
Fontstyle14 = FN REALFONT(Font(Menuitem),14)
Fontstyle18 = FN REALFONT(Font(Menuitem),18)
Fontstyle24 = FN REALFONT(Font(Menuitem),24)
GOSUB "Fontsizemenu"
TEXT Font(Menuitem)
RETURN
"Size":
FOR J=1 TO 6
Sizecheck(J)=1:MENU 4,J,1
NEXT J
Sizecheck(Menuitem)= 2
MENU 4,Menuitem,2
IF Menuitem=1 THEN TEXT , 9
IF Menuitem=2 THEN TEXT ,10
IF Menuitem=3 THEN TEXT ,12
IF Menuitem=4 THEN TEXT ,14
IF Menuitem=5 THEN TEXT ,18
IF Menuitem=6 THEN TEXT ,24
RETURN
"New":
PRINT "New Selected"
RETURN
"Open":
PRINT "Open Selected"
RETURN
"Close":
PRINT "Close Selected"
RETURN
"Save":
PRINT "Save Selected"
RETURN
"SaveAs":
PRINT "SaveAs Selected"
RETURN
"Quit":
MENU RESET
END