-- File: SVViewerToolImpl.mesa
-- Last edited by Bier on December 18, 1982 1:46 am
-- Author: Eric Bier on March 22, 1983 2:29 pm
-- My very own container-type class for solid viewers

DIRECTORY
 Icons,
 SV2d,
 SVEditUser,
 SVViewerTool,
 ViewerClasses,
 ViewerOps;

SVViewerToolImpl: PROGRAM

IMPORTS Icons, SVEditUser, ViewerOps
SHARES ViewerOps =

BEGIN OPEN ViewerClasses;

Point2d: TYPE = SV2d.Point2d;

ViewerToolData: TYPE = REF ViewerToolDataObj;
ViewerToolDataObj: TYPE = SVViewerTool.ViewerToolDataObj;

SolidViewerToolPaint: PRIVATE PaintProc = TRUSTED BEGIN
 viewerToolData: ViewerToolData ← NARROW[self.data];
 v: Viewer;
 update: BOOLFALSE;
 hdiff, wdiff: INTEGER;
FOR l: LIST OF Viewer ← viewerToolData.xBounded, l.rest UNTIL l=NIL DO
  v ← l.first;
  IF v.destroyed THEN {update ← TRUE; LOOP};
  wdiff ← v.ww-v.cw;
  v.ww ← MAX[v.parent.cw-v.wx-1, 5];
  v.cw ← v.ww-wdiff;
ENDLOOP;
FOR l: LIST OF Viewer ← viewerToolData.yBounded, l.rest UNTIL l=NIL DO
  v ← l.first;
  IF v.destroyed THEN {update ← TRUE; LOOP};
  hdiff ← v.wh-v.ch;
  v.wh ← MAX[v.parent.ch-v.wy-1, 5];
  v.ch ← v.wh-hdiff;
ENDLOOP;
IF viewerToolData.xBounded#NIL OR viewerToolData.yBounded#NIL
  THEN ViewerOps.ResetPaintCache[self, FALSE];
IF update THEN BEGIN
  oldx: LIST OF Viewer ← viewerToolData.xBounded;
  oldy: LIST OF Viewer ← viewerToolData.yBounded;
  viewerToolData.xBounded ← viewerToolData.yBounded ← NIL;
  FOR l: LIST OF Viewer ← oldx, l.rest UNTIL l=NIL DO
   IF ~l.first.destroyed
     THEN viewerToolData.xBounded ← CONS[l.first, viewerToolData.xBounded];
  ENDLOOP;
  FOR l: LIST OF Viewer ← oldy, l.rest UNTIL l=NIL DO
   IF ~l.first.destroyed
     THEN viewerToolData.yBounded ← CONS[l.first, viewerToolData.yBounded];
  ENDLOOP;
  END;
END;

SolidViewerToolDestroy: PRIVATE DestroyProc = TRUSTED {
 SVEditUser.NotifyDestroy[self];
 };

SolidViewerToolSet: PRIVATE SetProc = TRUSTED BEGIN
 viewerToolData: ViewerToolData ← NARROW[self.data];
 child: Viewer = NARROW[data];
IF child.parent#self THEN ERROR;
IF op=$YBound THEN viewerToolData.yBounded ← CONS[child, viewerToolData.yBounded]
ELSE IF op=$XBound THEN viewerToolData.xBounded ← CONS[child, viewerToolData.xBounded]
ELSE ERROR;
END;

SolidViewerToolInit: PRIVATE InitProc = CHECKED BEGIN
END;

solidViewerToolClass: ViewerClasses.ViewerClass ← NEW[ViewerClasses.ViewerClassRec ← [
 init: SolidViewerToolInit,
 paint: SolidViewerToolPaint,
 destroy: SolidViewerToolDestroy,
 set: SolidViewerToolSet,
 coordSys: top,
 icon: Icons.NewIconFromFile["SolidViews.icons", 0]
 ]];

ViewerOps.RegisterViewerClass[$SolidViewerTool, solidViewerToolClass]; -- plug in to Viewers

END.