home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / oberon / system / keplerelems.mod (.txt) < prev    next >
Oberon Text  |  1977-12-31  |  6KB  |  164 lines

  1. Syntax10.Scn.Fnt
  2. MODULE KeplerElems;    (* J. Templ, 27.09.93 *)
  3.     (* shows how to extend the Write editor by an additional element class *)
  4.     IMPORT
  5.         KeplerGraphs, KeplerFrames, KeplerPorts, TextFrames, Texts, TextPrinter,
  6.         Files, Oberon, Input, Viewers, MenuViewers, Display;
  7.     CONST
  8.         unit = TextFrames.Unit;    (* screen pixel size *)
  9.         Unit = TextPrinter.Unit;    (* printer pixel size *)
  10.         MM = 1;    (* mouse middle *)
  11.         Menu = "System.Close  System.Copy  System.Grow  KeplerElems.Update";
  12.     TYPE
  13.         Elem = POINTER TO ElemDesc;
  14.         ElemDesc = RECORD
  15.             (Texts.ElemDesc)
  16.             G: KeplerGraphs.Graph;
  17.             dx, dy, w, h, grid: INTEGER;
  18.         END;
  19.         Frame = POINTER TO FrameDesc;
  20.         FrameDesc = RECORD
  21.             (KeplerFrames.FrameDesc)
  22.             E: Elem;
  23.         END ;
  24.     PROCEDURE Draw(E: Elem; X, Y: INTEGER; VAR DF: Display.Frame);
  25.         VAR F: KeplerFrames.Frame;
  26.     BEGIN F := KeplerFrames.New(E.G); DF := F; 
  27.         F.X := X; F.Y := Y; F.W := SHORT(E.W DIV unit); F.H := SHORT(E.H DIV unit);
  28.         F.x0 := - E.dx; F.y0 := - (F.H * 4 + E.dy); F.scale := 4;
  29.         IF E.G.cons # NIL THEN E.G.Draw(F) ELSE F.DrawRect(0, 0, 99, 99, Display.white, Display.replace) END
  30.     END Draw;
  31.     PROCEDURE Print(E: Elem; X, Y: INTEGER);
  32.         VAR P: KeplerPorts.PrinterPort;
  33.     BEGIN NEW(P);
  34.         P.X := X; P.Y := Y; P.W := SHORT(E.W DIV Unit); P.H := SHORT(E.H DIV Unit);
  35.         P.x0 := - E.dx; P.y0 := - (P.H + E.dy); P.scale := 1;
  36.         E.G.Draw(P)
  37.     END Print;
  38.     PROCEDURE Copy(G: KeplerGraphs.Graph): KeplerGraphs.Graph;
  39.         VAR buf: Files.File; R: Files.Rider; o: KeplerGraphs.Object;
  40.     BEGIN buf := Files.New("");
  41.         Files.Set(R, buf, 0); KeplerGraphs.Reset;
  42.         KeplerGraphs.WriteObj(R, G);
  43.         Files.Set(R, buf, 0); KeplerGraphs.Reset;
  44.         KeplerGraphs.ReadObj(R, o);
  45.         RETURN o(KeplerGraphs.Graph)
  46.     END Copy;
  47.     PROCEDURE *FrameHandle (F: Display.Frame; VAR M: Display.FrameMsg);
  48.         VAR F1: Frame;
  49.     BEGIN
  50.         WITH F: Frame DO
  51.             WITH M: Oberon.CopyMsg DO
  52.                 NEW(F1); M.F := F1; F1^ := F^
  53.             ELSE KeplerFrames.Handle(F, M)
  54.             END
  55.         END
  56.     END FrameHandle;
  57.     PROCEDURE Edit(E: Elem);
  58.         VAR keysum, keys: SET; X, Y: INTEGER; F: Frame; V: Viewers.Viewer;
  59.     BEGIN
  60.         keysum := {MM};
  61.         REPEAT Input.Mouse(keys, X, Y); keysum := keysum + keys;
  62.             Oberon.DrawCursor(Oberon.Mouse, Oberon.Arrow, X, Y)
  63.         UNTIL keys = {};
  64.         IF keysum = {MM} THEN
  65.             Oberon.AllocateUserViewer(Oberon.Mouse.X, X, Y);
  66.             NEW(F); F.G := Copy(E.G); (*copy out*)
  67.             F.G.notify := KeplerFrames.NotifyDisplay;
  68.             F.handle := FrameHandle;
  69.             F.grid := E.grid; F.scale := 4; F.x0 := 0; F.y0 := 0; F.E := E;
  70.             V := MenuViewers.New(TextFrames.NewMenu("KeplerElem", Menu), F, TextFrames.menuH, X, Y)
  71.         END
  72.     END Edit;
  73.     PROCEDURE Load(VAR R: Files.Rider; E: Elem);
  74.         VAR o: KeplerGraphs.Object;
  75.             err: ARRAY 24 OF CHAR; version: INTEGER;
  76.     BEGIN
  77.         KeplerGraphs.Reset; KeplerGraphs.ReadObj(R, o); E.G := o(KeplerGraphs.Graph);
  78.         Files.ReadInt(R, E.dx); Files.ReadInt(R, E.dy);
  79.         Files.ReadInt(R, E.w); Files.ReadInt(R, version);
  80.         (*upward compatible version encoding*)
  81.         IF version >= 0 THEN E.h := version;
  82.         ELSIF version = -1 THEN Files.ReadInt(R, E.h); Files.ReadInt(R, E.grid)
  83.         (* ELSIF .. future versions *)
  84.         ELSE err := "version not supported"; HALT(99)
  85.         END
  86.     END Load;
  87.     PROCEDURE Store(VAR R: Files.Rider; E: Elem);
  88.     BEGIN
  89.         KeplerGraphs.Reset; KeplerGraphs.WriteObj(R, E.G);
  90.         Files.WriteInt(R, E.dx); Files.WriteInt(R, E.dy);
  91.         Files.WriteInt(R, E.w); Files.WriteInt(R, (*version*) -1);
  92.         Files.WriteInt(R, E.h); Files.WriteInt(R, E.grid);
  93.     END Store;
  94.     PROCEDURE Focus(E: Elem; focus: BOOLEAN; F: KeplerFrames.Frame);
  95.     BEGIN
  96.         IF focus THEN F.grid := E.grid ELSE F.grid := 0 END ;
  97.         F.Restore(F.X, F.Y, F.W, F.H)
  98.     END Focus;
  99.     PROCEDURE* ElemHandle (E: Texts.Elem; VAR M: Texts.ElemMsg);
  100.         VAR e: Elem;
  101.     BEGIN
  102.         WITH E: Elem DO
  103.             WITH
  104.                  M: TextFrames.DisplayMsg DO
  105.                     IF M.prepare THEN E.W := E.w * LONG(unit) DIV 4; E.H := E.h * LONG(unit) DIV 4
  106.                     ELSE Draw(E, M.X0, M.Y0, M.elemFrame)
  107.                     END
  108.                 | M: TextPrinter.PrintMsg DO
  109.                     IF M.prepare THEN E.W := E.w * LONG(Unit); E.H := E.h * LONG(Unit)
  110.                     ELSE Print(E, M.X0, M.Y0)
  111.                     END
  112.                 | M: Texts.IdentifyMsg DO
  113.                     M.mod := "KeplerElems"; M.proc := "Alloc"
  114.                 | M: Texts.FileMsg DO
  115.                     IF M.id = Texts.load THEN Load(M.r, E);
  116.                     ELSIF M.id = Texts.store THEN Store(M.r, E)
  117.                     END
  118.                 | M: Texts.CopyMsg DO
  119.                     NEW(e); Texts.CopyElem(E, e); M.e := e;
  120.                     e.dx := E.dx; e.dy := E.dy; e.w := E.w; e.h := E.h; e.grid := E.grid;
  121.                     e.G := Copy(E.G)
  122.                 | M: TextFrames.TrackMsg DO
  123.                     IF M.keys = {MM} THEN Edit(E) END
  124.                 | M: TextFrames.FocusMsg DO
  125.                     Focus(E, M.focus, M.elemFrame(KeplerFrames.Frame));
  126.             ELSE
  127.             END
  128.         END
  129.     END ElemHandle;
  130.     PROCEDURE Alloc*;
  131.         VAR e: Elem;
  132.     BEGIN NEW(e); e.handle := ElemHandle; Texts.new := e
  133.     END Alloc;
  134.     PROCEDURE Insert*;
  135.         VAR e: Elem; M: TextFrames.InsertElemMsg;
  136.     BEGIN
  137.         NEW(e); e.w := 100; e.h := 100; e.grid := 5; e.handle := ElemHandle;
  138.         NEW(e.G); M.e := e; Viewers.Broadcast(M)
  139.     END Insert;
  140.     PROCEDURE Update*;
  141.         VAR
  142.             G: KeplerGraphs.Graph;
  143.             F: Frame; E: Elem; B: KeplerPorts.BalloonPort;
  144.             R: Texts.Reader; T: Texts.Text;
  145.     BEGIN
  146.         IF Oberon.Par.vwr.dsc.next IS Frame THEN
  147.             F := Oberon.Par.vwr.dsc.next(Frame);
  148.             G := F.G; E := F.E;
  149.             G.All(0); (*deselect*);
  150.             NEW(B); KeplerPorts.InitBalloon(B);
  151.             G.Draw(B); (*get bounding box*)
  152.             E.dx := B.X; E.dy := B.Y; E.w := B.W + 4; E.h := B.H + 4;
  153.             E.G := Copy(G);    (*copy in*)
  154.             E.grid := F.grid;
  155.             T := Texts.ElemBase(E);
  156.             IF E.G.cons = NIL THEN E.dx := 0; E.dy := 0; E.w := 100; E.h := 100 END ;
  157.             IF T # NIL THEN Texts.OpenReader(R, T, 0);
  158.                 REPEAT Texts.ReadElem(R) UNTIL R.elem = E;
  159.                 T.notify(T, Texts.replace, Texts.Pos(R)-1, Texts.Pos(R))
  160.             END
  161.         END
  162.     END Update;
  163. END KeplerElems.
  164.