MODULE ObxLines;

   project   = "BlackBox"
   organization   = ""
   contributors   = "Oberon microsystems"
   version   = "System/Rsrc/About"
   copyright   = "System/Rsrc/About"
   license   = "Docu/BB-License"
   changes   = ""
   issues   = ""


   IMPORT Stores, Ports, Models, Views, Controllers, Properties;

   CONST minVersion = 0; maxVersion = 0;


         next: Line;
         x0, y0, x1, y1: INTEGER
      Model = POINTER TO RECORD (Models.Model)

         lines: Line
      View = POINTER TO RECORD (Views.View)

         color: Ports.Color;
         model: Model
      UpdateMsg = RECORD (Models.UpdateMsg)

         l, t, r, b: INTEGER
      LineOp = POINTER TO RECORD (Stores.Operation)

         model: Model;
         line: Line
      ColorOp = POINTER TO RECORD (Stores.Operation)

         view: View;
         color: Ports.Color
   PROCEDURE GetBox (x0, y0, x1, y1: INTEGER; VAR l, t, r, b: INTEGER);

      IF x0 > x1 THEN l := x1; r := x0 ELSE l := x0; r := x1 END;
      IF y0 > y1 THEN t := y1; b := y0 ELSE t := y0; b := y1 END;
      INC(r, Ports.point); INC(b, Ports.point)
   END GetBox;
   PROCEDURE (op: LineOp) Do;

      VAR l: Line; msg: UpdateMsg;
      l := op.line;
      IF l # op.model.lines THEN   (* insert op.line *)
         ASSERT(l # NIL, 100); ASSERT( = op.model.lines, 101);
         op.model.lines := l
      ELSE   (* delete op.line *)
         ASSERT(l = op.model.lines, 102);
         op.model.lines :=
      GetBox(l.x0, l.y0, l.x1, l.y1, msg.l, msg.t, msg.r, msg.b); Models.Broadcast(op.model, msg)
   END Do;
   PROCEDURE (m: Model) Internalize (VAR rd: Stores.Reader);

      VAR version: INTEGER; x0: INTEGER; p: Line;
      rd.ReadVersion(minVersion, maxVersion, version);
      IF ~rd.cancelled THEN
         rd.ReadInt(x0); m.lines := NIL;
         WHILE x0 # MIN(INTEGER) DO
            NEW(p); := m.lines; m.lines := p;
            p.x0 := x0; rd.ReadInt(p.y0); rd.ReadInt(p.x1); rd.ReadInt(p.y1);
   END Internalize;
   PROCEDURE (m: Model) Externalize (VAR wr: Stores.Writer);

      VAR p: Line;
      p := m.lines;
      WHILE p # NIL DO
         wr.WriteInt(p.x0); wr.WriteInt(p.y0); wr.WriteInt(p.x1); wr.WriteInt(p.y1);
         p :=
   END Externalize;
   PROCEDURE (m: Model) CopyFrom (source: Stores.Store);

      m.lines := source(Model).lines   (* lines are immutable and thus can be shared *)
   END CopyFrom;
   PROCEDURE (m: Model) Insert (x0, y0, x1, y1: INTEGER), NEW;

      VAR op: LineOp; p: Line;
      NEW(op); op.model := m;
      NEW(p); := m.lines; op.line := p;
      p.x0 := x0; p.y0 := y0; p.x1 := x1; p.y1 := y1;
      Models.Do(m, "Insert Line", op)
   END Insert;
   PROCEDURE (op: ColorOp) Do;

      VAR color: Ports.Color;
      color := op.view.color;   (* save old state *)
      op.view.color := op.color;   (* set new state *)
      Views.Update(op.view, Views.keepFrames);   (* restore everything *)
      op.color := color   (* old state becomes new state for undo *)
   END Do;
   PROCEDURE (v: View) Internalize (VAR rd: Stores.Reader);

      VAR version: INTEGER; st: Stores.Store;
      rd.ReadVersion(minVersion, maxVersion, version);
      IF ~rd.cancelled THEN
         v.model := st(Model)
   END Internalize;
   PROCEDURE (v: View) Externalize (VAR wr: Stores.Writer);

   END Externalize;
   PROCEDURE (v: View) CopyFromModelView (source: Views.View; model: Models.Model);

      ASSERT(model IS Model, 20);
      WITH source: View DO
         v.model := model(Model);
         v.color := source.color
   END CopyFromModelView;
   PROCEDURE (v: View) ThisModel (): Models.Model;

      RETURN v.model
   END ThisModel;
   PROCEDURE (v: View) Restore (f: Views.Frame; l, t, r, b: INTEGER);

      VAR p: Line;
      p := v.model.lines;
      WHILE p # NIL DO
         f.DrawLine(p.x0, p.y0, p.x1, p.y1,, v.color);
         p :=
   END Restore;
   PROCEDURE (v: View) HandleModelMsg (VAR msg: Models.Message);

      WITH msg: UpdateMsg DO
         Views.UpdateIn(v, msg.l, msg.t, msg.r, msg.b, Views.keepFrames)
   END HandleModelMsg;
   PROCEDURE (v: View) SetColor (color: Ports.Color), NEW;

      VAR op: ColorOp;
      NEW(op); op.view := v; op.color := color; Views.Do(v, "Set Color", op)
   END SetColor;
   PROCEDURE (v: View) HandleCtrlMsg (f: Views.Frame; VAR msg: Controllers.Message;

                                                VAR focus: Views.View);
      VAR x0, y0, x1, y1, x, y, res, l, t, r, b: INTEGER; modifiers: SET; isDown: BOOLEAN;
      WITH msg: Controllers.TrackMsg DO
         x0 := msg.x; y0 := msg.y; x1 := x0; y1 := y0;
         f.SaveRect(f.l, f.t, f.r, f.b, res);   (* operation was successful if res = 0 *)
         IF res = 0 THEN f.DrawLine(x0, y0, x1, y1, Ports.point, v.color) END;
            f.Input(x, y, modifiers, isDown);
            IF (x # x1) OR (y # y1) THEN
               GetBox(x0, y0, x1, y1, l, t, r, b); f.RestoreRect(l, t, r, b, Ports.keepBuffer);
               x1 := x; y1 := y;
               IF res = 0 THEN f.DrawLine(x0, y0, x1, y1, Ports.point, v.color) END
         UNTIL ~isDown;
         GetBox(x0, y0, x1, y1, l, t, r, b); f.RestoreRect(l, t, r, b, Ports.disposeBuffer);
         v.model.Insert(x0, y0, x1, y1)
      | msg: Controllers.EditMsg DO
         IF msg.op = Controllers.pasteChar THEN
            CASE msg.char OF
            | "B": v.SetColor(
            | "r": v.SetColor(
            | "g": v.SetColor(
            | "b": v.SetColor(
   END HandleCtrlMsg;
   PROCEDURE (v: View) HandlePropMsg (VAR msg: Properties.Message);

      WITH msg: Properties.FocusPref DO
         msg.setFocus := TRUE
   END HandlePropMsg;
   PROCEDURE Deposit*;

      VAR m: Model; v: View;
      NEW(v); v.model := m; Stores.Join(v, m);
   END Deposit;
END ObxLines.