|
Volume Number: | 3 | |
Issue Number: | 8 | |
Column Tag: | Basic School |
Using Resources with ZBASIC 4.0
By Dave Kelly, MacTutor Editorial Board
This is the anniversary of Basic Wars which began a year ago. This time there is a real winner! After a lot of struggle it appears that ZBasic™ has now come out on top. Version 4.0 is now available. If nothing has broken between what I’ve seen and the final release, ZBASIC ™ should be 99% free of problems (famous last words). The program at the end of this months column was written using version 3.85 beta which is supposed to be like version 4.0 except without the new editor. Even without the editor, I am extremely excited about ZBasic™ now (NO MORE BOMBS!!!).
Enhancements to ZBASIC ™ before version 4.0 are:
• NEW MANUAL: 206 pages dedicated to just the Macintosh, including many many examples.
• TOOLBOX supported: documentation is now provided to use toolbox commands at will.
• NEW COMMANDS such as TEHANDLE, SELECT CASE, GET WINDOW, HANDSHAKE, SHUTDOWN, FLUSHEVENTS.
Ok, enough of that. The real reason we are here is to discover how to program in Basic. If you have been watching this column over the past several months you will see that there have been things I’ve wanted to do with Basic that was not supported by any version of Basic. In addition, we have received many letters from frustrated readers trying to use Basic to do things that should have been easy but not supported. I’ll attempt to patch the holes in the past columns by showing you how in the ‘new’ ZBASIC .
Michael Crichton Reads MacTutor
One letter I received recently was from a fellow author, Michael Crichton. For those of you that don’t know him, he was author of the book, “The Andromeda Strain” and has just released a new book, “SPHERE” (How’s that for a plug for your new book, Michael?). Anyway, in his letter, he indicated that he wants “to manipulate text strings as a resource in a compiled ZBasic program”. The basic theme of this month’s column is “Using Resources with ZBasic”.
The idea that I will show is to use a string resource of type ‘STR ‘ to store data that is to be used by the application the next time that it is run. In this case, I will store the current font and size in the string resource. The other half of this column finishes up what got started in the past several columns. If you remember, I attempted to read the font names from the system file and install them into a Font menu. With PCMacBasic it was determined that even though you could use the ADDRESMENU toolbox call and create the menu, PCMacBasic would not let you access any new menus. UGH! Then there was MS Basic which let you read the font resources through library calls (though a roundabout way), but would only let you have 20 items in a menu. Since many of you have hard disks and want more than 20 fonts, that limited your programs to those fonts that you could fit. Even previous versions of ZBasic had problems, but since it works now and I can’t remember exactly the problem (probably a bomb!) we won’t discuss it.
Fig. 1 A Font and Size menu that really works in Basic!
Font Menu That Works
To use ADDRESMENU to load the font resource names into a menu I use the function GETMHANDLE ( line 39) to get a handle to the menu I want to add items to. In this case, I have already set up a ‘Font’ menu with a previous menu statement ( line 30). (Sorry, you’ll have to count lines yourself). The FontMenuHandle& variable now contains the handle to the font menu. FontMenuHandle& is used in the call to ADDRESMENU ( line 40). The resource type for the ADDRESMENU call is represented by CVI(“FONT”) as shown by looking on page E-201 of the new ZBasic manual in the ‘Alphabetical Listing of Toolbox Terms’. There is no more confusion on what kind of parameter needs to be used in a toolbox call; just look up the terms if you need help. OK, now the ‘Font’ menu is set up.
Finding the application
Next the program sets up the string resource that will be used to store the font and size. In my first attempt, I tried using the global memory location CurApRefNum found on page E-198 to get a RefNum to use in resource statements to indicate the resource file that I wanted to use (in this case, I wanted to use the application itself). BUT ZBasic opens up the printer driver automatically for you when the application is launched and CurApRefNum turns out to be the reference number for the printer driver and not the application. The program worked fine under those conditions, but the string was being saved in the printer driver resource and not in the ZBasic application. If you recall, I complained about the printer driver opening up several months back when we discussed ZBasic printing. This could have led to a real problem except that I found that the global memory location CurApName still contained the name of the application. WHEW! If not for this, ZBasic would be branded as not being able to find its own application (in case somebody changes the name or location of the application). Lines 42-47 get the application name from CurApName.
Next the application resource file is opened with OPENRESFILE and the file reference number is returned. Lines 51-57 were added for error handling just in case something goes wrong although these lines will probably never have to be used. Lines 60-67 setup the string (‘STR ’) resource. If the string resource handle strHnd& is zero then the resource does not exist and is created with ID#=1000. Otherwise the handle strHnd& will refer to the string stored previously (the last time the application was run). This string resource must now be converted to a ZBasic string variable and converted into font and size information (Lines 69-72). The function I call ReturnString$ is defined in lines 14-20 and reads the string referred to by the handle strHnd& into the string variable returned by the function.
Menu Items
The next several lines determine which menu item matches the set up that has just been read in and checks the appropriate item. The items were searched by using the GETITEM toolbox call. Then in line 91 the font number which matches the font name is read using GETFNUM and the font and size can now be set with ZBasic’s TEXT statement. A NOTE HERE: I tried CALL TEXTFONT and CALL TEXTSIZE to change the font and size information but these commands did not work when BREAK ON was being used. By removing BREAK ON everything seemed ok. This apparent bug could give somebody a few headaches someday. I recommend that you use the TEXT statement because it works better and sets the font, size, face, and mode in one statement. This still doesn’t excuse Zedcor from eventually fixing this bug.
Dialog Statement Fixed
The remainder of the program handles the menu events. There are a few more comments about event processing. Several months back, I showed how to use ZBasic DIALOG statements (not used in this month’s program). At that time, ZBasic did not quite function properly. There are a few corrections that should be made to the dialog example program published in the January 1987 MacTutor. The new improved program is printed in the new ZBasic manual on page E-68 as Example 1 for the DIALOG statement. The old program will bomb with version 4.0. The reason is that the DIALOG ON/OFF statements must each have an ON DIALOG statement to tell the compiler where the DIALOG events will be handled. What counts is that it now works wonderfully!! IF you are having problems with implementing DIALOG statements then be sure hat the DIALOG ON/OFF statement includes the portion of the program that you wish to trap dialog events and that there is a ON DIALOG statement to go with it.
As you can see, the main event loop (lines 97-102) is the only part of the program that will allow events to control the program. During the remainder of the program, events will be held in queue until the program returns to the main loop. Event trapping in ZBasic works differently than in MSBasic because of the way it is compiled. I (almost) like to think of the DIALOG ON/OFF, MENU ON/OFF, BREAK ON/OFF and other event statements to be compiler directives rather than Basic commands because they actually instruct the compiler as to which parts of the code the event trapping will occur.
SELECT CASE is a welcome improvement when it comes to handling menu events. The code is really easier to write (and to read later).
That about wraps it up for this time. Have FUN!!
REM ZBASIC RESOURCE EXAMPLE REM ©MacTutor 1987 REM By Dave Kelly REM IMPORTANT: COMPILE THIS PROGRAM AS REM AN APPLICATION BEFORE RUNNING!! REM Resources are used which need to be REM written to application’s own resource file WINDOW OFF:REM Turn off default window BREAK ON :REM This line may be deleted after program debugged. DEFSTR LONG:DEF MOUSE=-1:COORDINATE WINDOW REM convert string resource to a string, given handle. LONG FN ReturnString$(SHndl&) FByte%=PEEK(PEEK LONG(SHndl&)) String$=”” FOR i%=1 TO FByte% String$=String$+CHR$(PEEK(PEEK LONG(SHndl&)+i%)) NEXT i% END FN= String$ WINDOW #1,””,(50,100)-(450,300),4 : REM Main window. False=0:True=NOT(False) OldFont=0:OldSize=12 REM Setup menus APPLE MENU “About this application ” MENU 1,0,1,”File” MENU 1,1,1,”Quit” EDIT MENU 2 MENU 3,0,1,”Font” MENU 4,0,1,”Size” MENU 4,1,1,”9 Point” MENU 4,2,1,”10 Point” MENU 4,3,1,”12 Point” MENU 4,4,1,”14 Point” MENU 4,5,1,”18 Point” MENU 4,6,1,”24 Point” SizeMenuHandle&=FN GETMHANDLE(4) FontMenuHandle&=FN GETMHANDLE(3) CALL ADDRESMENU(FontMenuHandle&,CVI(“FONT”)) REM Find out what this application is named CurApName=&H910 CurApName$=”” FOR I=1 TO PEEK(CurApName) CurApName$=CurApName$+CHR$(PEEK(CurApName+I)) NEXT I REM Open application resource file Refnum=FN OPENRESFILE(CurApName$) Errnum=FN RESERROR LONG IF Errnum<>0 BEEP PRINT “ERROR # “;Errnum PRINT”Problem with Application Resource File!” FOR I=1 TO 1000:NEXT I:END END IF StrHnd&=FN GETRESOURCE(CVI(“STR “),1000) REM Setting up string resource to save default font and size LONG IF StrHnd&=0 Str$=”Chicago.12" StrHnd&=FN NEWSTRING(Str$) CALL ADDRESOURCE(StrHnd&,CVI(“STR “),1000,””) XELSE StrHnd&=FN GETSTRING(1000) END IF REM Convert default font and size default$=FN ReturnString$(StrHnd&) defaultfont$=MID$(default$,1,INSTR(1,default$,”.”)-1) Fsize=VAL(MID$(default$,INSTR(1,default$,”.”)+1)) REM Check default font in Font menu fontcnt%=FN COUNTMITEMS(FontMenuHandle&) FOR i=1 TO fontcnt% CALL GETITEM(FontMenuHandle&,i,item$) IF item$=defaultfont$ THEN OldFont=i:i=fontcnt%+1 NEXT i CALL CHECKITEM(FontMenuHandle&,OldFont,True) REM Check default size in Size menu sizecnt%=FN COUNTMITEMS(FontMenuHandle&) FOR i=1 TO sizecnt% CALL GETITEM(SizeMenuHandle&,i,item$) sname$=MID$(STR$(Fsize),2)+” Point” IF item$=sname$ THEN OldSize=i:i=sizecnt%+1 NEXT i CALL CHECKITEM(SizeMenuHandle&,OldSize,True) CALL GETFNUM(defaultfont$,Fnum%) TEXT Fnum%,Fsize,0,0 GOSUB “Display” ON MENU GOSUB “MenuEvent” FLUSHEVENTS:MENU ON REM Loop here until something happens “Loop” DO UNTIL 0 MENU STOP END “MenuEvent” Menunumber=MENU(0):Menuitem=MENU(1) MENU SELECT Menunumber CASE 1 GOSUB “File” CASE 2 GOSUB “Edit” CASE 3 GOSUB”Font” CASE 4 GOSUB”Size” CASE 255 IF Menuitem=1 THEN GOSUB “About” GOSUB “Display” CASE ELSE END SELECT RETURN “About” :REM About menu WINDOW #2,””,(75,100)-(400,250),-2 TEXT 2,14,1,0 PRINT PRINT SPACE$(11);”ZBasic™ Resource Sample” PRINT TEXT 2,12,0,0 PRINT SPACE$(15);”©MacTutor, 1987 by Dave Kelly” PRINT PRINT SPACE$(12);”ZBasic™ Version 4.0 - IT WORKS!!!!” PRINT MOUSE ON DO UNTIL MOUSE(0)<>0 MOUSE OFF WINDOW CLOSE #2 GOSUB “Display” RETURN “File” CALL UPDATERESFILE(RefNum) END “Edit” : REM Edit menu - used for DA’s only RETURN “Font” : REM Font menu CALL CHECKITEM(FontMenuHandle&,OldFont,False) CALL GETITEM(FontMenuHandle&,Menuitem,FontName$) CALL CHECKITEM(FontMenuHandle&,Menuitem,True) CALL GETITEM(FontMenuHandle&,Menuitem,defaultfont$) CALL GETFNUM(defaultfont$,Fnum%) IF Menuitem<>OldFont GOSUB “SetResString” OldFont=Menuitem GOSUB “Display” RETURN “Size” : REM Size menu CALL CHECKITEM(SizeMenuHandle&,OldSize,False) CALL CHECKITEM(SizeMenuHandle&,Menuitem,True) SELECT Menuitem CASE 1 Fsize=9 CASE 2 Fsize=10 CASE 3 Fsize=12 CASE 4 Fsize=14 CASE 5 Fsize=18 CASE 6 Fsize=24 END SELECT IF Menuitem<>OldSize GOSUB “SetResString” OldSize=Menuitem GOSUB “Display” RETURN REM Set string to be saved in string resource “SetResString” default$=defaultfont$+”.”+MID$(STR$(Fsize),2) CALL SETSTRING(StrHnd&,default$) CALL CHANGEDRESOURCE(StrHnd&) RETURN “Display” :REM Main screen display TEXT Fnum%,Fsize,0,0 CLS PRINT “FONT:”;defaultfont$ PRINT “SIZE:”;Fsize PRINT TEXT ,,1,0 MacTutor$=”Read MacTutor™ !!” REM Center MacTutor string Pxls%=FN STRINGWIDTH(MacTutor$) Windwidth%=WINDOW(6) CALL MOVE((Windwidth%-Pxls%)/2,0) PRINT MacTutor$ RETURN
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine