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

Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
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 »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
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

Jobs Board

Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.