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

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... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.