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

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 »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »
Play Together teams up with Sanrio to br...
I was quite surprised to learn that the massive social network game Play Together had never collaborated with the globally popular Sanrio IP, it seems like the perfect team. Well, this glaring omission has now been rectified, as that instantly... | Read more »
Dark and Darker Mobile gets a new teaser...
Bluehole Studio and KRAFTON have released a new teaser trailer for their upcoming loot extravaganza Dark and Darker Mobile. Alongside this look into the underside of treasure hunting, we have received a few pieces of information about gameplay... | Read more »

Price Scanner via MacPrices.net

14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more
B&H has 16-inch MacBook Pros on sale for...
Apple 16″ MacBook Pros with M3 Pro and M3 Max CPUs are in stock and on sale today for $200-$300 off MSRP at B&H Photo. Their prices are among the lowest currently available for these models. B... Read more
Updated Mac Desktop Price Trackers
Our Apple award-winning Mac desktop price trackers are the best place to look for the lowest prices and latest sales on all the latest computers. Scan our price trackers for the latest information on... Read more
9th-generation iPads on sale for $80 off MSRP...
Best Buy has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80 off MSRP on their online store for a limited time. Prices start at only $249. Sale prices for online orders only, in-store prices... Read more
15-inch M3 MacBook Airs on sale for $100 off...
Best Buy has Apple 15″ MacBook Airs with M3 CPUs on sale for $100 off MSRP on their online store. Prices valid for online orders only, in-store prices may vary. Order online and choose free shipping... Read more

Jobs Board

Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Mar 22, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Retail Assistant Manager- *Apple* Blossom Ma...
Retail Assistant Manager- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 04225 Job Area: Store: Management Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sonographer - *Apple* Hill Imaging Center -...
Sonographer - Apple Hill Imaging Center - Evenings Location: York Hospital, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now See Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.