TML Pascal Tricks
 Volume Number: 6 Issue Number: 3 Column Tag: Pascal Procedures

# TML Pascal Tricks

By Paul Diegenbach, Amsterdam, the Netherlands

## XCMD - drawing in a Hypercard stack

The attractiveness of Hypercard is for a large part due to it graphical possibilities. Nice cards always mean nice graphics. The Hypertalk language allows drawing on the screen under program control. When we tear the tools menu off the menubar, we can actually see the actions as the tools flop from pen to brush to marquee and so on. Nice to watch, but oh so slow. Ever tried to draw a complicated graph in Hypercard? I tried it in an attempt to simulate the differentiation of an egg to all the different cells an embryo consists of. One of the theories implies the evaluation of some differential equations and then drawing a thousand points on the screen.

The idea was to let students be able to change the parameters in the equations and see the results in a user-friendly environment. The first step was to implement the equation solving in Hypertalk. Too slow, so an XCMD was quickly written. Drawing the results by Hypertalk: still to slow, so why not do the drawing in the XCMD as well? In a first result the drawing looks nice, but as soon as Hypercard updates the screen from its offscreen bitmap, the drawing disappears. The solution to this problem is to copy the drawing in the paste buffer (scrap) and paste it into Hypercard using the paste command from the menu. This combination works fine and quick. The skeleton for the XCMD is given below, in TML-Pascal, with a Fourier-demo in the drawing part to make it less dumb. In this demo we use parameters from Hypercard for the size of the rectangle and for the Fourier coefficients. Although not necessary, it is often handy to give the rectangle parameters. They can easily be estimated by having a temporary field of the same size and asking in the message window ‘put the size of card field tmp into msg’. The Hypercard stack with this demo implemented is on the source disk for this month.

To avoid flicker between the copy and paste, it is convenient to lock the screen before the XCMD is used. Users of other Pascals sometimes need to remove the ParamPtr parameter, that is the first parameter in every call to the glue routines:

```{1}

UNIT PictToScrp;
(******************************************************
This XCMD demodnstrates a method to plot and calculate in Pascal (perhaps
with floating point processor enabled) and then copy and paste the picture
in the card compile and link as follows (assuming Fourier is an existing
stack):

TMLPascal Fourier.p
or:  ( really fast on a mac II)
TMLPascal Fourier.p  -MC68020 -MC68881 -d Elems881=true

Link -w -t ‘STAK’ -c ‘WILD’ 
-m FOURIER -rt XCMD=1282 -sg Fourier 
Fourier.p.o 
“{Libraries}”Runtime.o 
“{Libraries}”Interface.o 
“{TMLPLibraries}”TMLPasLib.o 
“{Libraries}”HyperXLib.o 
-o “Fourier”

Hypercard call :
set the lockscreen to true
-- prevents flicker
Fourier  330,14,480,319,10,7,5,3,4
-- 330,14,480 is the area to draw into
-- 10,7,5,3,4  are the fourier factors times 10
set the lockscreen to false
******************************************************)

INTERFACE

USES MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, PasLibIntf, HyperXCMD;

PROCEDURE Fourier(ParamPtr: XCMDPtr);

IMPLEMENTATION

PROCEDURE PictScrap(ParamPtr: XCMDPtr); FORWARD;

PROCEDURE Fourier(ParamPtr: XCMDPtr);
BEGIN
PictScrap(ParamPtr);
END;

PROCEDURE PictScrap(ParamPtr: XCMDPtr);
VAR
thePoint     : Point;
hPicture     : PicHandle;
rg           : rect;
Piclength    : LONGINT;
xoffset, yoffset: integer;
str          : str255;
xLeft, bot, top, xRight: LONGINT;
one, two, three, four, five: LONGINT;
x, y         : extended;
z1, z2, z3, z4, z5: extended;

PROCEDURE MyDrawing;
{ all your drawing stuff goes here }
VAR
i            : integer;
maxim, offset: extended;

FUNCTION Sinus(x: extended): extended;
BEGIN
Sinus := sin(x) * z1 + sin(2 * x) * z2 + sin(3 * x) * z3 +
sin(4 * x)  * z4 + sin(5 * x) * z5
{ the more, the better }
END;

BEGIN
eraserect(rg);
Framerect(rg); { optional }
offset := top + (bot - top) / 2.0;
z1 := one / 10.0;
z2 := two / 10.0;
z3 := three / 10.0;
z4 := four / 10.0;
z5 := five / 10.0;
x := 0.02 {1 / 50} ;
maxim := Sinus(x);
FOR i := 2 TO 314 DO
BEGIN
IF i MOD 10 = 0 THEN
SendCardMessage(ParamPtr, ‘set cursor to busy’);
x := i / 50;
y := Sinus(x);
IF maxim < y THEN
maxim := y;
END;

x := 1 / 50;
y := Sinus(x);
maxim := 100 / maxim;
SendCardMessage(ParamPtr, ‘set cursor to busy’);
moveto(xLeft + 1, round(y * maxim + offset));
FOR i := 2 TO 314 DO
BEGIN { for two pi }
x := i / 50;
y := Sinus(x);
lineto(i + xLeft + 1, round(y * maxim + offset))
END;
SendCardMessage(ParamPtr, ‘set cursor to busy’);
penpat(ltgray);
moveto(xLeft + 1, round(offset));
lineto(xRight - 2, round(offset));
pennormal
END;

FUNCTION GetParameter(n: integer): LONGINT;
VAR
tmp          : LONGINT;
BEGIN
ZeroToPas(ParamPtr, ParamPtr^.params[n]^, str);
stringToNum(str, tmp);
GetParameter := tmp
END;

BEGIN
IF ParamPtr^.paramCount <> 9 THEN
SendCardMessage(ParamPtr, ‘put “wrong number of parameters” into
msg’)
{ I think a more convenient way of error report than by ParamPtr^.ReturnValue
}
ELSE
BEGIN
{get the drawing rectangle coordinates from hypercard }
xLeft := GetParameter(1);
top := GetParameter(2);
xRight := GetParameter(3);
bot := GetParameter(4);

{ In this example : get Fourier coëfficients from hypercard }
one := GetParameter(5);
two := GetParameter(6);
three := GetParameter(7);
four := GetParameter(8);
five := GetParameter(9);
setrect(rg, xLeft, top, xRight, bot);
xoffset := xLeft + 5;
yoffset := top + 5;
cliprect(rg);
hPicture := OpenPicture(rg);
Showpen;
MyDrawing; { here all your drawing }
closepicture;
Piclength := GethandleSize(handle(hPicture));
IF ZeroScrap + putscrap(Piclength, ‘PICT’, ptr(hPicture^)) =
NoErr THEN
ParamPtr^.ReturnValue := PasToZero(ParamPtr, ‘’)
ELSE
ParamPtr^.ReturnValue := PasToZero(ParamPtr, ‘something wrong’);
SendCardMessage(ParamPtr, ‘choose select tool’);
SendCardMessage(ParamPtr, ‘choose browse tool’);
END
END;
END.
```

## Small and Dirty “Write Always”

In the debugging of large programs we were accustomed to put a lot of ‘writeln’s’ in the source text to show what was happening. Although a workable solution on mainframes and mini’s, that’s not so easy in the Macintosh environment, there is not always a window available to write in. The following small subroutine (TML Pascal version) does the trick with some disadvantage: it overwrite all its lines on top of each other at the bottom of the screen in some kind of status line (bit like Online companion), but, sorry, the line is not automatically removed after leaving the program. Dirty isn’t it, so surely for debugging only. An ‘invalrect’ after each multifinder event and at the end of the program is sometimes a solution.

In the program source you write something like :

```--2

if dbg then DQText(‘now in procedure blahblah’)
```

(The clever one’s can write a MPW-script to put it after the first begin in every procedure or function) Dbg is used as a constant (or in a compiler directive).

The procedure (as a Pascal unit) looks like :

```{3}

UNIT DQTextProcedure;
INTERFACE
USES
MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, QDaccess;

{ Tml pascals QDaccess defines:
TYPE  QDGlobalsPtr = ^QDGlobalsRec;
QDGlobalsRec =
record
randSeed   :  LongInt;
screenBits :  BitMap;
arrow      :  Cursor;
dkGray     :  Pattern;
ltGray     :  Pattern;
gray       :  Pattern;
black      :  Pattern;
white      :  Pattern;
ThePort    :  GrafPtr;
end;
function QDGlobals: QDGlobalsPtr;external;}

PROCEDURE DQText(Message: Str255);
IMPLEMENTATION
PROCEDURE DQText(Message: Str255);
VAR
SavedPort    : GrafPtr;
LocalPort    : GrafPort;
TheRect      : Rect;
BEGIN
WITH QDGlobals^ DO
BEGIN
GetPort(SavedPort);
OpenPort(@LocalPort);
SetPort(@LocalPort);
WITH ScreenBits.Bounds DO
{ this is QDGlobals^ s ScreenBits ! }
SetRect(TheRect, Left, Bottom-20, Right, Bottom);
FillRect(TheRect, White);
MoveTo(TheRect.Left, TheRect.Top);
LineTo(TheRect.Right, TheRect.Top);
MoveTo(TheRect.Left+7, TheRect.Bottom-7);
DrawString(Message);
ClosePort(@LocalPort);
SetPort(SavedPort);
END;
END;
END.

PROGRAM tstDQtext;
{ test the dqtext procedure : }
USES MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
PaslibIntf, QDaccess, DQtext;
(*
Link  -t ‘APPL’ -c ‘????’ 
DQtext.p.o 
tstDQtext.p.o 
“{Libraries}”Runtime.o 
“{Libraries}”Interface.o 
“{TMLPLibraries}”TMLPasLib.o 
-o tstDQtext
*)
BEGIN
textbook(NIL); { TML pascal simple window }
writeln(‘ see bottom line’);
writeln(‘ click mouse button’);
PLFlush(output);
DQtext(‘before’);
REPEAT
UNTIL button;
DQtext(‘after mouse button’);
END.
```

Community Search:
MacTech Search:

Duplicate Annihilator 5.7.5 - Find and d...
Duplicate Annihilator takes on the time-consuming task of comparing the images in your iPhoto library using effective algorithms to make sure that no duplicate escapes. Duplicate Annihilator... Read more
BusyContacts 1.0.2 - Fast, efficient con...
BusyContacts is a contact manager for OS X that makes creating, finding, and managing contacts faster and more efficient. It brings to contact management the same power, flexibility, and sharing... Read more
Capture One Pro 8.2.0.82 - RAW workflow...
Capture One Pro 8 is a professional RAW converter offering you ultimate image quality with accurate colors and incredible detail from more than 300 high-end cameras -- straight out of the box. It... Read more
Backblaze 4.0.0.872 - Online backup serv...
Backblaze is an online backup service designed from the ground-up for the Mac.With unlimited storage available for \$5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more
Little Snitch gives you control over your private outgoing data. Track background activity As soon as your computer connects to the Internet, applications often have permission to send any... Read more
Monolingual 1.6.4 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. If you use your computer in only one (human) language, you... Read more
CleanApp 5.0 - Application deinstaller a...
CleanApp is an application deinstaller and archiver.... Your hard drive gets fuller day by day, but do you know why? CleanApp 5 provides you with insights how to reclaim disk space. There are... Read more
Fantastical 2.0 - Create calendar events...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
Cocktail 8.2 - General maintenance and o...
Cocktail is a general purpose utility for OS X that lets you clean, repair and optimize your Mac. It is a powerful digital toolset that helps hundreds of thousands of Mac users around the world get... Read more
Direct Mail 4.0.4 - Create and send grea...
Direct Mail is an easy-to-use, fully-featured email marketing app purpose-built for OS X. It lets you create and send great looking email campaigns. Start your newsletter by selecting from a gallery... Read more

## Latest Forum Discussions

Fast & Furious: Legacy's Creati...
N-Fusion and 505's Ember is Totally...
These are All the Apple Watch Apps and G...
The Apple Watch is less than a month from hitting store shelves, and once you get your hands on it you're probably going to want some apps and games to install. Fear not! We've compiled a list of all the Apple Watch apps and games we've been able to... | Read more »
Appy to Have Known You - Lee Hamlet Look...
Being at 148Apps these past 2 years has been an awesome experience that has taught me a great deal, and working with such a great team has been a privilege. Thank you to Rob Rich, and to both Rob LeFebvre and Jeff Scott before him, for helping me... | Read more »
Hands-On With Allstar Heroes - A Promisi...
Let’s get this out of the way quickly. Allstar Heroes looks a lot like a certain other recent action RPG release, but it turns out that while it’s not yet available here, Allstar Heroes has been around for much longer than that other title. Now that... | Read more »
Macho Man and Steve Austin Join the Rank...
WWE Immortals, by Warner Bros. Interactive Entertainment and WWE, has gotten a superstar update. You'll now have access to Macho Man Randy Savage and Steve Austin. Both characters have two different versions: Macho Man Randy Savage Renegade or Macho... | Read more »
Fearless Fantasy is Fantastic for the iF...
I actually had my first look at Fearless Fantasy last year at E3, but it was on a PC so there wasn't much for me to talk about. But now that I've been able to play with a pre-release version of the iOS build, there's quite a bit for me to talk... | Read more »
MLB Manager 2015 (Games)
MLB Manager 2015 5.0.14 Device: iOS Universal Category: Games Price: \$4.99, Version: 5.0.14 (iTunes) Description: Guide your favorite MLB franchise to glory! MLB Manager 2015, officially licensed by MLB.com and based on the award-... | Read more »
Breath of Light (Games)
Breath of Light 1.0.1421 Device: iOS Universal Category: Games Price: \$2.99, Version: 1.0.1421 (iTunes) Description: Hold a quiet moment. Breath of Light is a meditative and beautiful puzzle game with a hypnotic soundtrack by... | Read more »
WWE WrestleMania Tags into the App Store
Are You ready to rumble? The official WWE WrestleMania app, by World Wrestling Entertainment, is now available. Now you can get all your WrestleMania info in one place before anyone else. The app offers details on superstar signings, interactive... | Read more »

## Price Scanner via MacPrices.net

13-inch 2.5GHz MacBook Pro (refurbished) avai...
The Apple Store has Apple Certified Refurbished 13″ 2.5GHz MacBook Pros available for \$829, or \$270 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free: - 13″ 2.... Read more
Save up to \$80 on iPad Air 2s, NY tax only, f...
B&H Photo has iPad Air 2s on sale for \$80 off MSRP including free shipping plus NY sales tax only: - 16GB iPad Air 2 WiFi: \$469.99 \$30 off - 64GB iPad Air 2 WiFi: \$549.99 \$50 off - 128GB iPad... Read more
iMacs on sale for up to \$205 off MSRP
B&H Photo has 21″ and 27″ iMacs on sale for up to \$205 off MSRP including free shipping plus NY sales tax only: - 21″ 1.4GHz iMac: \$1019 \$80 off - 21″ 2.7GHz iMac: \$1189 \$110 off - 21″ 2.9GHz... Read more
Färbe Technik Offers iPhone Battery Charge LI...
Färbe Technik, which manufactures and markets of mobile accessories for Apple, Blackberry and Samsung mobile devices, is offering tips on how to keep your iPhone charged while in the field: •... Read more
Electronic Recyclers International CEO Urges...
Citing a recent story on CNBC about concerns some security professionals have about the forthcoming Apple Watch, John Shegerian, Chairman and CEO of Electronic Recyclers International (ERI), the... Read more
Save up to \$380 with Apple refurbished iMacs
The Apple Store has Apple Certified Refurbished iMacs available for up to \$380 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free: - 27″ 3.5GHz 5K iMac – \$2119 \$... Read more
Mac minis on sale for up to \$75 off, starting...
MacMall has Mac minis on sale for up to \$75 off MSRP including free shipping. Their prices are the lowest available for these models from any reseller: - 1.4GHz Mac mini: \$459.99 \$40 off - 2.6GHz Mac... Read more
College Student Deals: Additional \$50 off Mac...
Take an additional \$50 off all MacBooks and iMacs at Best Buy Online with their College Students Deals Savings, valid through April 11, 2015. Anyone with a valid .EDU email address can take advantage... Read more
Mac Pros on sale for up to \$260 off MSRP
B&H Photo has Mac Pros on sale for up to \$260 off MSRP. Shipping is free, and B&H charges sales tax in NY only: - 3.7GHz 4-core Mac Pro: \$2799, \$200 off MSRP - 3.5GHz 6-core Mac Pro: \$3719.99... Read more
13-inch 2.5GHz MacBook Pro on sale for \$100 o...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for \$999 including free shipping plus NY sales tax only. Their price is \$100 off MSRP. Read more

## Jobs Board

DevOps Software Engineer - *Apple* Pay, iOS...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Sr. Technical Services Consultant, *Apple*...
**Job Summary** Apple Professional Services (APS) has an opening for a senior technical position that contributes to Apple 's efforts for strategic and transactional Read more
Lead *Apple* Solutions Consultant - Retail...
**Job Summary** Job Summary The Lead ASC is an Apple employee who serves as the Apple business manager and influencer in a hyper-business critical Reseller's store Read more
*Apple* Pay - Site Reliability Engineer - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more