mentor/derived/WheelerWhyDecompressWorksObliqView.m3


 Copyright (C) 1995, Digital Equipment Corporation.       
 All rights reserved.                                     
 See the file COPYRIGHT for a full description.           
                                                          
 Last modified on Fri Sep  8 15:48:12 PDT 1995 by najork  
      modified on Thu Feb  9 08:54:15 PST 1995 by kalsow  
 Last modified on Fri Dec  9 15:00:46 PST 1994 by mhb     
      modified on Sat Jun  4 16:24:49 1994 by heydon      
      modified on Tue Feb 16 16:31:40 PST 1993 by johnh   

********************************************************************

      *  NOTE: This file is generated automatically from the event
      *        definition file Wheeler.evt.
      ********************************************************************


<* PRAGMA LL *>

MODULE WheelerWhyDecompressWorksObliqView;

<*NOWARN*> IMPORT TextConv, ObLibM3, ObLibUI, SynWr, Obliq;
<*NOWARN*> IMPORT ObliqParser, Rd, Filter, WheelerViewClass, Fmt;
<*NOWARN*> IMPORT ObLibAnim, ZFmt, ZeusPanel, ObValue, TextWr;
<*NOWARN*> IMPORT View, VBT, Thread, MiscFmt, TextRd, Rsrc;

CONST
  ViewName =  "WhyDecompressWorks.obl";

TYPE
  T = WheelerViewClass.T BRANDED OBJECT
        object  : Obliq.Val;
        env     : Obliq.Env;
        wr      : TextWr.T;
        swr     : SynWr.T;
        parser  : ObliqParser.T;
      OVERRIDES
        <* LL.sup < VBT.mu *>
        startrun := Startrun;
        <* LL.sup < VBT.mu *>
        oeStartPermute := StartPermute;
        oeNextRotation := NextRotation;
        oeRotationsSorted := RotationsSorted;
        oePermuteDone := PermuteDone;
        oeStartEncode := StartEncode;
        oeEncodeNextChar := EncodeNextChar;
        oeEncodeDistinctCount := EncodeDistinctCount;
        oeEncodeFoundCode := EncodeFoundCode;
        oeEncodeDone := EncodeDone;
        oeInitDecode := InitDecode;
        oeStartDecode := StartDecode;
        oeDecodeNextCode := DecodeNextCode;
        oeDecodeDistinctCount := DecodeDistinctCount;
        oeDecodeFoundChar := DecodeFoundChar;
        oeDecodeDone := DecodeDone;
        oeStartReconstruct := StartReconstruct;
        oeFirstChars := FirstChars;
        oeConsiderChar := ConsiderChar;
        oeEqualChars := EqualChars;
        oeFinishCharRun := FinishCharRun;
        oeStartResult := StartResult;
        oeResultNextChar := ResultNextChar;
        oeEndResult := EndResult;
        oeReveal := Reveal;
        <* LL.sup = VBT.mu *>
      END;
OUTPUT and UPDATE event handling methods:

PROCEDURE StartPermute (view: T;  string, alphabet: TEXT) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "StartPermute") THEN
      Invoke (view, "StartPermute", ""
      & TextConv.Encode(string)
      & ","
      & TextConv.Encode(alphabet)
      )
    END
  END StartPermute;
PROCEDURE NextRotation (view: T;  i: INTEGER; string: TEXT) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "NextRotation") THEN
      Invoke (view, "NextRotation", ""
      & Fmt.Int(i)
      & ","
      & TextConv.Encode(string)
      )
    END
  END NextRotation;
PROCEDURE RotationsSorted (view: T;  rotations: MiscFmt.RefTextArray; rowIndex: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "RotationsSorted") THEN
      Invoke (view, "RotationsSorted", ""
      & MiscFmt.TextArray(rotations)
      & ","
      & Fmt.Int(rowIndex)
      )
    END
  END RotationsSorted;
PROCEDURE PermuteDone (view: T;  lastchars: TEXT; rowIndex: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "PermuteDone") THEN
      Invoke (view, "PermuteDone", ""
      & TextConv.Encode(lastchars)
      & ","
      & Fmt.Int(rowIndex)
      )
    END
  END PermuteDone;
PROCEDURE StartEncode (view: T;  alphabet: TEXT) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "StartEncode") THEN
      Invoke (view, "StartEncode", ""
      & TextConv.Encode(alphabet)
      )
    END
  END StartEncode;
PROCEDURE EncodeNextChar (view: T;  i: INTEGER; c: CHAR) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "EncodeNextChar") THEN
      Invoke (view, "EncodeNextChar", ""
      & Fmt.Int(i)
      & ","
      & MiscFmt.Char(c)
      )
    END
  END EncodeNextChar;
PROCEDURE EncodeDistinctCount (view: T;  i, k, n: INTEGER; c: CHAR) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "EncodeDistinctCount") THEN
      Invoke (view, "EncodeDistinctCount", ""
      & Fmt.Int(i)
      & ","
      & Fmt.Int(k)
      & ","
      & Fmt.Int(n)
      & ","
      & MiscFmt.Char(c)
      )
    END
  END EncodeDistinctCount;
PROCEDURE EncodeFoundCode (view: T;  i, k, code: INTEGER; c: CHAR) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "EncodeFoundCode") THEN
      Invoke (view, "EncodeFoundCode", ""
      & Fmt.Int(i)
      & ","
      & Fmt.Int(k)
      & ","
      & Fmt.Int(code)
      & ","
      & MiscFmt.Char(c)
      )
    END
  END EncodeFoundCode;
PROCEDURE EncodeDone (view: T;  alphabet: TEXT; codes: MiscFmt.RefIntArray; rowIndex: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "EncodeDone") THEN
      Invoke (view, "EncodeDone", ""
      & TextConv.Encode(alphabet)
      & ","
      & MiscFmt.IntArray(codes)
      & ","
      & Fmt.Int(rowIndex)
      )
    END
  END EncodeDone;
PROCEDURE InitDecode (view: T;  alphabet: TEXT; codes: MiscFmt.RefIntArray; rowIndex: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "InitDecode") THEN
      Invoke (view, "InitDecode", ""
      & TextConv.Encode(alphabet)
      & ","
      & MiscFmt.IntArray(codes)
      & ","
      & Fmt.Int(rowIndex)
      )
    END
  END InitDecode;
PROCEDURE StartDecode (view: T;  ) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "StartDecode") THEN
      Invoke (view, "StartDecode", ""
      )
    END
  END StartDecode;
PROCEDURE DecodeNextCode (view: T;  i: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "DecodeNextCode") THEN
      Invoke (view, "DecodeNextCode", ""
      & Fmt.Int(i)
      )
    END
  END DecodeNextCode;
PROCEDURE DecodeDistinctCount (view: T;  i, k, n: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "DecodeDistinctCount") THEN
      Invoke (view, "DecodeDistinctCount", ""
      & Fmt.Int(i)
      & ","
      & Fmt.Int(k)
      & ","
      & Fmt.Int(n)
      )
    END
  END DecodeDistinctCount;
PROCEDURE DecodeFoundChar (view: T;  i, k: INTEGER; c: CHAR) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "DecodeFoundChar") THEN
      Invoke (view, "DecodeFoundChar", ""
      & Fmt.Int(i)
      & ","
      & Fmt.Int(k)
      & ","
      & MiscFmt.Char(c)
      )
    END
  END DecodeFoundChar;
PROCEDURE DecodeDone (view: T;  lastchars: TEXT; rowIndex: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "DecodeDone") THEN
      Invoke (view, "DecodeDone", ""
      & TextConv.Encode(lastchars)
      & ","
      & Fmt.Int(rowIndex)
      )
    END
  END DecodeDone;
PROCEDURE StartReconstruct (view: T;  lastchars: TEXT; rowIndex: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "StartReconstruct") THEN
      Invoke (view, "StartReconstruct", ""
      & TextConv.Encode(lastchars)
      & ","
      & Fmt.Int(rowIndex)
      )
    END
  END StartReconstruct;
PROCEDURE FirstChars (view: T;  t: TEXT) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "FirstChars") THEN
      Invoke (view, "FirstChars", ""
      & TextConv.Encode(t)
      )
    END
  END FirstChars;
PROCEDURE ConsiderChar (view: T;  i: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "ConsiderChar") THEN
      Invoke (view, "ConsiderChar", ""
      & Fmt.Int(i)
      )
    END
  END ConsiderChar;
PROCEDURE EqualChars (view: T;  i, j: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "EqualChars") THEN
      Invoke (view, "EqualChars", ""
      & Fmt.Int(i)
      & ","
      & Fmt.Int(j)
      )
    END
  END EqualChars;
PROCEDURE FinishCharRun (view: T;  ) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "FinishCharRun") THEN
      Invoke (view, "FinishCharRun", ""
      )
    END
  END FinishCharRun;
PROCEDURE StartResult (view: T;  ) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "StartResult") THEN
      Invoke (view, "StartResult", ""
      )
    END
  END StartResult;
PROCEDURE ResultNextChar (view: T;  pos, k: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "ResultNextChar") THEN
      Invoke (view, "ResultNextChar", ""
      & Fmt.Int(pos)
      & ","
      & Fmt.Int(k)
      )
    END
  END ResultNextChar;
PROCEDURE EndResult (view: T;  ) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "EndResult") THEN
      Invoke (view, "EndResult", ""
      )
    END
  END EndResult;
PROCEDURE Reveal (view: T;  i: INTEGER) =
  <* LL.sup < VBT.mu *>
  BEGIN
    IF FieldDefined(view.object, "Reveal") THEN
      Invoke (view, "Reveal", ""
      & Fmt.Int(i)
      )
    END
  END Reveal;

PROCEDURE RegisterView () =
  BEGIN
    ZeusPanel.RegisterView(New, "WhyDecompressWorks.obl", "Wheeler")
  END RegisterView;

PROCEDURE New (): View.T =
  BEGIN
    RETURN NEW(T).init(NIL)
  END New;

CONST
  ObliqStackSizeMultiplier = 8;

TYPE
  Closure = Thread.SizedClosure OBJECT
              view: T;
            OVERRIDES
              apply := ForkedStartrun;
            END;

PROCEDURE Startrun (view: T) =
  <* LL.sup < VBT.mu *>
  BEGIN
    EVAL
      Thread.Join(
        Thread.Fork(
          NEW(Closure, view := view,
              stackSize := ObliqStackSizeMultiplier * Thread.GetDefaultStackSize())));
  END Startrun;

PROCEDURE ForkedStartrun (cl: Closure): REFANY =
  VAR rd: Rd.T; view := cl.view;
  BEGIN
    IF view.parser = NIL THEN
      view.wr := TextWr.New();
      view.swr := SynWr.New(view.wr);
      view.parser := ObliqParser.New(view.swr);
    END;
    view.object := NIL;
    TRY
      rd := Rsrc.Open(ViewName, ZeusPanel.GetPath());
      view.env := ParseRd(view.parser, ViewName, rd);
      WITH obj = Obliq.Lookup("view", view.env) DO
        IF NOT ISTYPE(obj, ObValue.ValObj) THEN
          ZeusPanel.ReportError(
            "not an Obliq object in '" & ViewName & "'")
        ELSIF FieldDefined (obj, "graphvbt") THEN
          WITH graphvbt =
            NARROW(Obliq.ObjectSelect(obj, "graphvbt"),
                   ObLibAnim.ValGraph).vbt DO
            LOCK VBT.mu DO
              EVAL Filter.Replace(view, graphvbt)
            END
          END;
          view.object := obj;
        ELSIF FieldDefined (obj, "rectsvbt") THEN
          WITH rectsvbt =
            NARROW(Obliq.ObjectSelect(obj, "rectsvbt"),
                   ObLibAnim.ValRects).vbt DO
            LOCK VBT.mu DO
              EVAL Filter.Replace(view, rectsvbt)
            END
          END;
          view.object := obj;
        ELSIF FieldDefined (obj, "formsvbt") THEN
          WITH formsvbt =
            NARROW(Obliq.ObjectSelect(obj, "formsvbt"),
                   ObLibUI.ValForm).vbt DO
            LOCK VBT.mu DO
              EVAL Filter.Replace(view, formsvbt)
            END
          END;
          view.object := obj;
        ELSE
          ZeusPanel.ReportError(
            "cannot find 'graphvbt', 'rectsvbt', or 'formsvbt' in '" & ViewName & "'")
        END
      END
    EXCEPT
    | Rsrc.NotFound =>
        ZeusPanel.ReportError("cannot find '" & ViewName & "'")
    | ObValue.Error (packet) => OblError(view, packet)
    | ObValue.Exception (packet) => OblException(view, packet)
    END;
    RETURN NIL;
  END ForkedStartrun;

PROCEDURE ParseRd (p: ObliqParser.T; name: TEXT; rd: Rd.T):
  Obliq.Env RAISES {ObValue.Error, ObValue.Exception} =
  VAR env := Obliq.EmptyEnv();
  BEGIN
    ObliqParser.ReadFrom(p, name, rd, TRUE);
    TRY
      LOOP
        EVAL ObliqParser.EvalPhrase(p, ObliqParser.ParsePhrase(p), env)
      END
    EXCEPT
      ObliqParser.Eof => (* clean exit of loop *)
    END;
    RETURN env
  END ParseRd;

PROCEDURE Invoke (view: T; event, args: TEXT) =
  VAR
    exp    := "view." & event & "(" & args & ");";
    name   := "Zeus Event <" & event & ">";
  BEGIN
    ObliqParser.ReadFrom (view.parser, name, TextRd.New(exp), FALSE);
    TRY
      EVAL Obliq.EvalTerm(ObliqParser.ParseTerm(view.parser), view.env)
    EXCEPT
    | ObliqParser.Eof => <* ASSERT FALSE *>
    | ObValue.Error (packet) => OblError(view, packet)
    | ObValue.Exception (packet) => OblException(view, packet)
    END
  END Invoke;

PROCEDURE FieldDefined (object: Obliq.Val; event: TEXT): BOOLEAN =
  BEGIN
    TRY
      RETURN object # NIL AND Obliq.ObjectHas(object, event)
    EXCEPT
    | ObValue.Error =>
    | ObValue.Exception =>
    END;
    RETURN FALSE
  END FieldDefined;

PROCEDURE OblError (view: T; packet: ObValue.ErrorPacket) =
  BEGIN
    Obliq.ReportError(view.swr, packet);
    ZeusPanel.ReportError(
      "Obliq error: " & TextWr.ToText(view.wr))
  END OblError;

PROCEDURE OblException (view: T; packet: ObValue.ExceptionPacket) =
  BEGIN
    Obliq.ReportException(view.swr, packet);
    ZeusPanel.ReportError(
      "Obliq exception: " & TextWr.ToText(view.wr))
  END OblException;

BEGIN
  SynWr.Setup();
  ObliqParser.PackageSetup();
  ObLibM3.PackageSetup();
  ObLibUI.PackageSetup();
  ObLibAnim.PackageSetup();
  RegisterView ();
END WheelerWhyDecompressWorksObliqView.

interface View is in: