-- File: MagicViewing.mesa
-- Written by Martin Newell February 1980
-- Implements viewing part of Magic
-- Last edited (Pilot): 20-Jul-81 10:10:05

DIRECTOR
Y

CIFUtilitiesDefs: FROM "CIFUtilitiesDefs" USING [DisplayContext, Rectangle,
InitCedarGraphics, GetClipRectangle, SetClipRectangle,
SetUniformView, TrackBox, GetDisplayContext, WatchKeys],
IODefs USING [WriteLine],
Graphics: FROM "Graphics" USING [Translate],
JaMFnsDefs: FROM "JaMFnsDefs" USING [Register, PopInteger, GetReal, PushReal],
Keys: FROM "Keys" USING [KeyName];

MagicViewing: PROGRAM
IMPORTS CIFUtilitiesDefs, Graphics, IODefs, JaMFnsDefs =
BEGIN
OPEN CIFUtilitiesDefs, Graphics, IODefs, JaMFnsDefs, Keys;

--Viewing control

CallTrackBox: PROCEDURE =
BEGIN --expects <x,y, mouse(4,2,1)> (REAL)
mouseButtons: Keys.KeyName;
mouse: INTEGER ← PopInteger[];
y: REAL ← GetReal[];
x: REAL ← GetReal[];
SELECT mouse FROM
4 => mouseButtons ← Red;
2 => mouseButtons ← Yellow;
1 => mouseButtons ← Blue;
ENDCASE => RETURN;
TrackBox[x,y, mouseButtons];
END;

Expand: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
r: Rectangle ← GetClipRectangle[];
top: REAL ← GetReal[];
right: REAL ← GetReal[];
bottom: REAL ← GetReal[];
left: REAL ← GetReal[];
SetUniformView[[MIN[left,right],MIN[bottom,top],MAX[left,right],MAX[bottom,top]], r];
END;

Contract: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
r: Rectangle ← GetClipRectangle[];
top: REAL ← GetReal[];
right: REAL ← GetReal[];
bottom: REAL ← GetReal[];
left: REAL ← GetReal[];
SetUniformView[r, [MIN[left,right],MIN[bottom,top],MAX[left,right],MAX[bottom,top]]];
END;

Scroll: PROCEDURE =
BEGIN --expects <xfrom,yfrom, xto,yto> (REAL)
dc: DisplayContext ← GetDisplayContext[];
yto: REAL ← GetReal[];
xto: REAL ← GetReal[];
yfrom: REAL ← GetReal[];
xfrom: REAL ← GetReal[];
Translate[dc, xto-xfrom, yto-yfrom];
END;

CallSetClipRegion: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
rt: Rectangle;
rt.ury ← GetReal[];
rt.urx ← GetReal[];
rt.lly ← GetReal[];
rt.llx ← GetReal[];
SetClipRectangle[rt];
END;

CallGetClipRegion: PROCEDURE =
BEGIN --returns <left,bottom,right,top> (REAL)
rt: Rectangle ← GetClipRectangle[];
PushReal[rt.llx];
PushReal[rt.lly];
PushReal[rt.urx];
PushReal[rt.ury];
END;


--*** START Code ***

WriteLine["Calling InitCedarGraphics"];

InitCedarGraphics[];

WriteLine["Registering trackbox,expand,contract,scroll,watchkeys"];

--Viewing control
Register["trackbox",CallTrackBox];
Register["expand",Expand];
Register["contract",Contract];
Register["scroll",
Scroll];
R
egister["watchkeys",WatchKeys];
Register["setclipregion",
CallSetClipRegion];
Register["getclipregion",CallGetClipRegion];

WriteLine["Start code complete"];

END.