TweetFollow Us on Twitter

Resources/ ZBAS 4.0
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
 

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.