MODULE ObxViews12;

   project   = "BlackBox"
   organization   = ""
   contributors   = "Oberon microsystems"
   version   = "System/Rsrc/About"
   copyright   = "System/Rsrc/About"
   license   = "Docu/BB-License"
   purpose   = "Same as ObxViews11, but uses a separate model for the string"
   changes   = ""
   issues   = ""


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

   CONST d = 20 * Ports.point;


      Model = POINTER TO RECORD (Models.Model)
         i: INTEGER;         (* position of next free slot in string *)
         s: ARRAY 256 OF CHAR   (* string *)
      View = POINTER TO RECORD (Views.View)

         model: Model
   (* Model *)

   PROCEDURE (m: Model) Internalize (VAR rd: Stores.Reader);

      VAR version: INTEGER;
      rd.ReadVersion(0, 0, version);
      IF ~rd.cancelled THEN
         rd.ReadInt(m.i); rd.ReadString(m.s)
   END Internalize;
   PROCEDURE (m: Model) Externalize (VAR wr: Stores.Writer);

      wr.WriteInt(m.i); wr.WriteString(m.s)
   END Externalize;
   PROCEDURE (m: Model) CopyFrom (source: Stores.Store);

      WITH source: Model DO
         m.i := source.i; m.s := source.s
   END CopyFrom;
   (* View *)

   PROCEDURE (v: View) Internalize (VAR rd: Stores.Reader);

      VAR version: INTEGER; st: Stores.Store;
      rd.ReadVersion(0, 0, 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);

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

      f.DrawString(d, d,, v.model.s, Fonts.dir.Default())
   END Restore;
   PROCEDURE (v: View) ThisModel (): Models.Model;

      RETURN v.model
   END ThisModel;
   PROCEDURE (v: View) HandleModelMsg (VAR msg: Models.Message);

      Views.Update(v, Views.keepFrames)   (* restore v in any frame that displays it *)
   END HandleModelMsg;
   PROCEDURE (v: View) HandleCtrlMsg (f: Views.Frame;

                              VAR msg: Controllers.Message;
                              VAR focus: Views.View);
      VAR m: Model; umsg: Models.UpdateMsg;
      WITH msg: Controllers.EditMsg DO
         IF msg.op = Controllers.pasteChar THEN
            m := v.model;
            m.s[m.i] := msg.char; INC(m.i);
            m.s[m.i] := 0X;   (* append character to string *)
            Views.SetDirty(v);   (* mark view's document as dirty *)
            Models.Broadcast(m, umsg)   (* update all views on this model *)
      ELSE                  (* ignore other messages *)
   END HandleCtrlMsg;
   PROCEDURE (v:View) HandlePropMsg (VAR msg: Properties.Message);

      WITH msg: Properties.SizePref DO
         IF (msg.w = Views.undefined) OR (msg.h = Views.undefined) THEN
            msg.w := 10 * d; msg.h := 2 * d
      | msg: Properties.FocusPref DO
         msg.setFocus := TRUE
   END HandlePropMsg;
   PROCEDURE Deposit*;

      VAR v: View; m: Model;
      NEW(m); m.i := 0; m.s := "";
      NEW(v); v.model := m; Stores.Join(v, m);
   END Deposit;
END ObxViews12.