-- File: NutPrivateImpl.mesa
-- Contents: private access to nutList by NutImpl

-- Created by: Rick & Willie-Sue on July 8, 1982
-- Last edited by:
-- Rick on November 1, 1982 12:54 pm: check for thisV.menu=NIL in FindSpawned
-- Willie-Sue on January 21, 1983 8:56 am
-- Last Edited by: Donahue, March 22, 1983 12:17 pm

DIRECTORY
Atom USING [GetPName],
DB,
Nut,
NutPrivate,
Rope,
ViewerClasses USING[Viewer],
ViewerOps,
VirtualDesktops USING[EnumerateViewers];

NutPrivateImpl: CEDAR MONITOR
IMPORTS DB, Rope, ViewerOps, VirtualDesktops, Atom
EXPORTS NutPrivate =
BEGIN OPEN Nut;

Viewer: TYPE = ViewerClasses.Viewer;

tiptoe: PUBLIC BOOLEANFALSE; -- TRUE when want as few db accesses as possible

-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

FindViewer: PUBLIC ENTRY PROC
 [ type: Nut.NutType, domain: DB.Domain, eName: Rope.ROPE, seg: DB.Segment]
RETURNS[Viewer] =
BEGIN
entity: ROPE = Rope.Cat[ Atom.GetPName[seg], "!", DB.NameOf[domain], "!", eName ];
FOR nL: LIST OF Viewer ← GetNutList[], nL.rest UNTIL nL=NIL DO
typeProp: REF ANY = ViewerOps.FetchProp[ nL.first, $NutType ];
nutType: Nut.NutType = IF typeProp = NIL THEN displayer
      ELSE NARROW[typeProp, REF Nut.NutType]^;
IF nutType # type THEN LOOP;
IF NOT Rope.Equal[entity, NARROW[ ViewerOps.FetchProp[nL.first, $Entity]]] THEN LOOP;
IF nL.first.destroyed THEN LOOP;
RETURN[nL.first];
ENDLOOP;
RETURN[NIL];
END;

FindSpawned: PUBLIC ENTRY PROC[v: Viewer] RETURNS[Viewer] =
BEGIN
IF v = NIL THEN RETURN[NIL];
{ spawn: Viewer = NARROW[ ViewerOps.FetchProp[ v, $LastSpawned ], Viewer ];
IF spawn = NIL OR spawn.destroyed THEN RETURN[ NIL ];
IF ViewerOps.FetchProp[spawn, $Frozen] = NIL THEN RETURN[ spawn ];
RETURN[ NIL ] }
END;

SetSpawned: PUBLIC ENTRY PROC[parent, spawned: Viewer] =
BEGIN
IF parent = NIL THEN RETURN;
ViewerOps.AddProp[ parent, $LastSpawned, spawned ];
IF spawned # NIL THEN ViewerOps.AddProp[ spawned, $WhoSpawnedMe, parent ];
END;

GetNutList: INTERNAL PROC RETURNS[nL: LIST OF Viewer] =
BEGIN
enum: ViewerOps.EnumProc =
{ name: ROPE = NARROW[ViewerOps.FetchProp[v, $Entity]];
IF name # NIL THEN nL← CONS[v, nL];
RETURN[TRUE] };
 VirtualDesktops.EnumerateViewers[enum];
END;

END.