CDRepetitionsImpl.mesa (part of Chipndale)
by Christian Jacobi October 27, 1983 3:36 pm
last edited by Christian Jacobi December 14, 1983 10:44 am
DIRECTORY
CD,
CDApplications USING [NewApplication, BoundingRect],
CDCallSpecific,
CDCells,
CDDirectory,
CDInline,
CDIO,
CDOrient,
CDProperties,
CDRepetitions,
TokenIO;
CDRepetitionsImpl: CEDAR PROGRAM
IMPORTS CD, CDApplications, CDCallSpecific, CDCells, CDDirectory, CDInline, CDIO, CDOrient, CDProperties, TokenIO
EXPORTS CDRepetitions
SHARES CDDirectory =
BEGIN
RepPtr: TYPE = CDRepetitions.RepPtr;
RepRec: TYPE = CDRepetitions.RepRec;
pForRepetitions: REF CD.ObjectProcs ~ CD.RegisterObjectType[$Repetitions];
RepositionElementsR: CDDirectory.RepositionElementsProc
-- PROC [me: CD.ObPtr, objToReposition: CD.ObPtr, oldSize: CD.DesignPosition, newBound: CD.DesignRect, design: CD.Design] -- =
BEGIN
rp: RepPtr ~ NARROW[me.specificRef];
IF rp.ob=objToReposition THEN {
rp.offsetOfFirst ← CDOrient.MapPosition[
itemInCell: newBound,
cellSize: oldSize,
cellInstOrient: rp.orientation,
cellInstPos: rp.offsetOfFirst];
CDDirectory.RepositionAnObject[design, me];
}
END;
ComputeBoundsR: CDDirectory.ComputeBoundsProc
-- PROC [ob: CD.ObPtr] RETURNS [CD.DesignRect] -- =
BEGIN
rp: RepPtr = NARROW[ob.specificRef];
sz: CD.DesignPosition = CDOrient.OrientedSize[rp.ob.size, rp.orientation];
toffset: CD.DesignPosition = [(rp.count-1)*rp.offset.x, (rp.count-1)*rp.offset.y];
r1: CD.DesignRect = CDInline.RectAt[rp.offsetOfFirst, sz];
r2: CD.DesignRect = CDInline.RectAt[CDInline.AddPoints[rp.offsetOfFirst, toffset], sz];
RETURN CDInline.Surround[r1, r2];
END;
AdjustItselfR: CDDirectory.AdjustItselfProc
-- PROC [objToReposition: CD.ObPtr, newBound: CD.DesignRect] -- =
BEGIN
rp: RepPtr = NARROW[objToReposition.specificRef];
rp.offsetOfFirst ← [0, 0];
IF rp.offset.x<0 THEN rp.offsetOfFirst.x← -(rp.count-1)*rp.offset.x;
IF rp.offset.y<0 THEN rp.offsetOfFirst.y← -(rp.count-1)*rp.offset.y;
END;
InternalCreateRepetition: PROC [ob: CD.ObPtr, count: NAT, offset: CD.DesignPosition, orientation: CD.Orientation] RETURNS [CD.ObPtr] =
BEGIN
repOb: CD.ObPtr = NEW[CD.ObjectDefinition];
rp: RepPtr = NEW[RepRec];
sz: CD.DesignPosition = CDOrient.OrientedSize[ob.size, orientation];
repOb.p ← pForRepetitions;
count ← MAX[1, MIN[count, 512]];
repOb.size.x ← sz.x+(count-1)*ABS[offset.x];
repOb.size.y ← sz.y+(count-1)*ABS[offset.y];
repOb.level ← ob.level;
rp.ob ← ob;
rp.offsetOfFirst ← [0, 0];
IF offset.x<0 THEN rp.offsetOfFirst.x← -(count-1)*offset.x;
IF offset.y<0 THEN rp.offsetOfFirst.y← -(count-1)*offset.y;
rp.orientation ← orientation;
rp.offset ← offset;
rp.count ← count;
repOb.specificRef ← rp;
RETURN [repOb];
END;
CreateRepetition: PUBLIC PROC [design: CD.Design, ob: CD.ObPtr, count: NAT, offset: CD.DesignPosition, orientation: CD.Orientation𡤀] RETURNS [CD.ObPtr] =
BEGIN
repOb: CD.ObPtr = InternalCreateRepetition[ob, count, offset, orientation];
[] ← CDDirectory.Include[design, repOb];
RETURN [repOb];
END;
DrawMeForRepetitions: PROC [aptr: CD.ApplicationPtr, pos: CD.DesignPosition, orient: CD.Orientation,
pr: CD.DrawRef] =
BEGIN
ENABLE UNWIND => IF pr.nesting.first=aptr THEN pr.nesting ← pr.nesting.rest;
rptr: RepPtr ← NARROW[aptr.ob.specificRef];
rel, sz: CD.DesignPosition;
o: CD.Orientation ← CDOrient.ComposeOrient[rptr.orientation, orient];
dummyApp: CD.ApplicationPtr ← NEW[CD.Application];
xi: REF INT;
dummyApp.ob ← rptr.ob;
rel ← rptr.offsetOfFirst;
sz ← CDOrient.OrientedSize[rptr.ob.size, rptr.orientation];
pr.nesting ← CONS[aptr, pr.nesting];
--hang two properties on the application; both pointing to the same int containing the
--repetition number;
--the first property has the name $CDxIndex
--the second property name is the value of the property $CDxIndexProperty
WITH CDProperties.GetPropFromApplication[aptr, $CDxIndex] SELECT FROM
intP: REF INT => xi ← intP;
ENDCASE => {
xi ← NEW[INT];
CDProperties.PutPropOnApplication[aptr, $CDxIndex, xi];
WITH CDProperties.GetPropFromApplication[aptr, $CDxIndexProperty] SELECT FROM
ixP: ATOM => {
CDProperties.PutPropOnApplication[aptr, ixP, xi];
};
ENDCASE => NULL
};
--do drawing
FOR i: INT IN [0..rptr.count) DO
p: CD.DesignPosition ← CDOrient.MapPosition[
itemInCell: CDInline.RectAt[rel, sz],
cellSize: aptr.ob.size,
cellInstOrient: orient,
cellInstPos: pos
];
IF pr.stopFlag^ THEN EXIT;
xi^ ← i;
rptr.ob.p.drawMe[dummyApp, p, o, pr];
rel ← CDInline.AddPoints[rel, rptr.offset];
ENDLOOP;
IF pr.nesting.first=aptr THEN pr.nesting ← pr.nesting.rest
END;
IncCount: CDCallSpecific.CallProc
-- PROC [design: CD.Design, aptr: CD.ApplicationPtr, x: REF] RETURNS [done: BOOL←TRUE, removeMe: BOOL�LSE, include: CD.ApplicationList←NIL, repaintMe: BOOL�LSE, repaintInclude: BOOL�LSE] -- =
BEGIN
sz: CD.DesignPosition ← aptr.ob.size;
ap: CD.ApplicationPtr ← NEW[CD.Application𡤊ptr^];
rptr: RepPtr ← NARROW[aptr.ob.specificRef];
newOb: CD.ObPtr = CreateRepetition[design, rptr.ob, rptr.count+1, rptr.offset, rptr.orientation];
IF ap.ob.size#newOb.size THEN {
relativeNewBound: CD.DesignRect ← [x1: 0, y1: 0, x2: aptr.ob.size.x, y2: aptr.ob.size.y];
IF rptr.offset.x>=0 THEN relativeNewBound.x2 ← aptr.ob.size.x+rptr.offset.x
ELSE relativeNewBound.x1 ← rptr.offset.x;
IF rptr.offset.y>=0 THEN relativeNewBound.y2 ← aptr.ob.size.y+rptr.offset.y
ELSE relativeNewBound.y1 ← rptr.offset.y;
ap.location ← CDOrient.MapPosition[
itemInCell: relativeNewBound,
cellSize: aptr.ob.size,
cellInstOrient: aptr.orientation,
cellInstPos: aptr.location];
};
ap.ob ← newOb;
include←LIST[ap];
repaintMe ← TRUE;
removeMe←TRUE;
repaintInclude←TRUE;
END;
DecCount: CDCallSpecific.CallProc
-- PROC [design: CD.Design, aptr: CD.ApplicationPtr, x: REF] RETURNS [done: BOOL←TRUE, removeMe: BOOL�LSE, include: CD.ApplicationList←NIL, repaintMe: BOOL�LSE, repaintInclude: BOOL�LSE] -- =
BEGIN
sz: CD.DesignPosition ← aptr.ob.size;
ap: CD.ApplicationPtr ← NEW[CD.Application𡤊ptr^];
rptr: RepPtr ← NARROW[aptr.ob.specificRef];
newOb: CD.ObPtr = CreateRepetition[design, rptr.ob, rptr.count-1, rptr.offset, rptr.orientation];
IF ap.ob.size#newOb.size THEN {
relativeNewBound: CD.DesignRect ← [x1: 0, y1: 0, x2: aptr.ob.size.x, y2: aptr.ob.size.y];
IF rptr.offset.x>=0 THEN relativeNewBound.x2 ← aptr.ob.size.x-rptr.offset.x
ELSE relativeNewBound.x1 ← -rptr.offset.x;
IF rptr.offset.y>=0 THEN relativeNewBound.y2 ← aptr.ob.size.y-rptr.offset.y
ELSE relativeNewBound.y1 ← -rptr.offset.y;
ap.location ← CDOrient.MapPosition[
itemInCell: relativeNewBound,
cellSize: aptr.ob.size,
cellInstOrient: aptr.orientation,
cellInstPos: aptr.location];
};
ap.ob ← newOb;
include←LIST[ap];
repaintMe ← TRUE;
removeMe←TRUE;
repaintInclude←TRUE;
END;
TransformToCell: CDCallSpecific.CallProc
-- PROC [design: CD.Design, aptr: CD.ApplicationPtr, x: REF] RETURNS [done: BOOL←TRUE, removeMe: BOOL�LSE, include: CD.ApplicationList←NIL, repaintMe: BOOL�LSE, repaintInclude: BOOL�LSE] -- =
BEGIN
rptr: RepPtr←NARROW[aptr.ob.specificRef];
cob: CD.ObPtr ← CreateRepetitionCell[design: design,
ob: rptr.ob,
count: rptr.count,
offset: rptr.offset,
orientation: rptr.orientation
];
removeMe←TRUE;
repaintMe←TRUE;
include←LIST[CDApplications.NewApplication[
cob, aptr.location, aptr.orientation, aptr.selected, aptr.properties]];
repaintInclude←TRUE;
END;
EnumerateRepeatedObjects: PROC [me: CD.ObPtr, p: CDDirectory.EnumerateObjectsProc, x: REF] =
BEGIN
rptr: RepPtr = NARROW[me.specificRef];
p[rptr.ob, x];
END;
ReadRepetition: CD.InternalReadProc --PROC [] RETURNS [ObPtr]-- =
BEGIN
ob: CD.ObPtr = CDIO.ReadObject[];
count: NAT = TokenIO.ReadInt[];
offsetx: INT = TokenIO.ReadInt[];
offsety: INT = TokenIO.ReadInt[];
orientation: CD.Orientation = CDIO.ReadOrientation[];
RETURN [ InternalCreateRepetition[
ob: ob,
count: count,
offset: [offsetx, offsety],
orientation: orientation
]];
END;
WriteRepetition: CD.InternalWriteProc -- PROC [me: ObPtr] -- =
BEGIN
specific: RepPtr = NARROW[me.specificRef];
CDIO.WriteObject[specific.ob];
TokenIO.WriteInt[specific.count];
TokenIO.WriteInt[specific.offset.x];
TokenIO.WriteInt[specific.offset.y];
CDIO.WriteOrientation[specific.orientation];
END;
CreateRepetitionCell: PUBLIC PROC [design: CD.Design, ob: CD.ObPtr, count: NAT, offset: CD.DesignPosition, orientation: CD.Orientation𡤀] RETURNS [CD.ObPtr] =
BEGIN
ap: CD.ApplicationPtr;
pos: CD.DesignPosition ← [0, 0];
co: CD.ObPtr ← CDCells.CreateEmptyCell[];
cp: CD.CellPtr ← NARROW[co.specificRef];
count ← MAX[count, 1];
IF offset.x<0 THEN {pos.x ← -offset.x*(count-1)};
IF offset.y<0 THEN {pos.y ← -offset.y*(count-1)};
FOR i: NAT IN [1..count] DO
ap ← CDApplications.NewApplication[ob: ob, location: pos, orientation: orientation];
pos ← CDInline.AddPoints[pos, offset];
cp.contents ← CONS[ap, cp.contents]
ENDLOOP;
co.size ← CDInline.SizeOfRect[CDApplications.BoundingRect[cp.contents]];
[] ← CDDirectory.Include[design, co, "-repetition-"];
RETURN [co];
END;
InitRepetitions: PROC [] =
BEGIN
rp: REF CDDirectory.DirectoryProcs ~ CDDirectory.InstallDirectoryProcs[pForRepetitions];
rp.enumerateChildObjects ← EnumerateRepeatedObjects;
rp.adjustItself ← AdjustItselfR;
rp.repositionElements ← RepositionElementsR;
rp.computeBounds ← ComputeBoundsR;
pForRepetitions.drawMe ← DrawMeForRepetitions;
pForRepetitions.internalRead ← ReadRepetition;
pForRepetitions.internalWrite ← WriteRepetition;
CDCallSpecific.Register[$TransformToCell, pForRepetitions, TransformToCell];
CDCallSpecific.Register[$IncCount, pForRepetitions, IncCount];
CDCallSpecific.Register[$DecCount, pForRepetitions, DecCount];
END;
InitRepetitions[];
END.