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

  1. Syntax10.Scn.Fnt
  2. MODULE Kepler;    (* J. Templ, 27.09.93 *)
  3.     IMPORT SYSTEM, Oberon, Texts, Files, Printer, TextFrames, MenuViewers, Viewers,
  4.         KeplerFrames, KeplerGraphs, KeplerPorts, In;
  5.     CONST
  6.         menu = "System.Close  System.Copy  System.Grow  Kepler.Store";
  7.         W: Texts.Writer;
  8.         AttrV: MenuViewers.Viewer;
  9.         AttrT: Texts.Text;
  10.     PROCEDURE Print *;
  11.         VAR
  12.             S: Texts.Scanner;
  13.             source: KeplerGraphs.Graph;
  14.             V: Viewers.Viewer;
  15.             nofcopies: INTEGER;
  16.         PROCEDURE PrintUnit(G: KeplerGraphs.Graph; nofcopies: INTEGER);
  17.             VAR P: KeplerPorts.PrinterPort;
  18.         BEGIN NEW(P);
  19.             P.X := 0; P.Y := 0; P.W := MAX(INTEGER); P.H := 3300;
  20.             P.x0 := 0; P.y0 := 0; P.scale := 1;
  21.             G.Draw(P);
  22.             Printer.Page(nofcopies)
  23.         END PrintUnit;
  24.     BEGIN
  25.         Texts.WriteString(W, "Kepler.Print"); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
  26.         Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos); Texts.Scan(S);
  27.         IF S.class = Texts.Name THEN
  28.             Printer.Open(S.s, Oberon.User, Oberon.Password);
  29.             IF Printer.res = 0 THEN
  30.                 Texts.Scan(S); nofcopies := 1;
  31.                 IF S.class = Texts.Int THEN nofcopies := SHORT(S.i); Texts.Scan(S) END ;
  32.                 WHILE S.class = Texts.Name DO
  33.                     source := KeplerGraphs.Old(S.s);
  34.                     IF source = NIL THEN Texts.WriteString(W, " -- not found: ");
  35.                         Texts.WriteString(W, S.s); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
  36.                     ELSE PrintUnit(source, nofcopies)
  37.                     END ;
  38.                     Texts.Scan(S)
  39.                 END;
  40.                 IF (S.class = Texts.Char) & (S.c = "*") THEN
  41.                     V := Oberon.MarkedViewer();
  42.                     IF (V IS MenuViewers.Viewer) & (V.dsc.next IS KeplerFrames.Frame) THEN
  43.                         PrintUnit(V.dsc.next(KeplerFrames.Frame).G, nofcopies)
  44.                     END
  45.                 END;
  46.                 Printer.Close
  47.             ELSE
  48.                 IF Printer.res = 1 THEN Texts.WriteString(W, " no such printer")
  49.                 ELSIF Printer.res = 2 THEN Texts.WriteString(W, " no link")
  50.                 ELSIF Printer.res = 3 THEN Texts.WriteString(W, " printer not ready")
  51.                 ELSIF Printer.res = 4 THEN Texts.WriteString(W, " no permission")
  52.                 END;
  53.                 Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
  54.             END
  55.         ELSE Texts.WriteString(W, " no printer specified");
  56.             Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
  57.         END
  58.     END Print;
  59.     PROCEDURE Open*;
  60.         VAR
  61.             V: MenuViewers.Viewer;
  62.             X, Y, grid: INTEGER;
  63.             G: KeplerGraphs.Graph;
  64.             F: KeplerFrames.Frame;
  65.             name: ARRAY 32 OF CHAR;
  66.     BEGIN
  67.         In.Open; In.Name(name);
  68.         IF In.Done THEN In.Int(grid);
  69.             IF ~In.Done THEN grid := 5 END ;
  70.             Oberon.AllocateUserViewer(Oberon.Mouse.X, X, Y);
  71.             G := KeplerGraphs.Old(name);
  72.             IF G = NIL THEN NEW(G); G.seltime := -1 END ;
  73.             F := KeplerFrames.New(G);
  74.             F.grid := grid;
  75.             V := MenuViewers.New(TextFrames.NewMenu(name, menu), F, TextFrames.menuH, X, Y)
  76.         END
  77.     END Open;
  78.     PROCEDURE InitAttrV;
  79.         VAR X, Y: INTEGER;
  80.     BEGIN
  81.         Texts.Delete(AttrT, 0, AttrT.len);
  82.         IF (AttrV = NIL) OR (AttrV.state <= 0) THEN
  83.             Oberon.AllocateSystemViewer(Oberon.Mouse.X, X, Y);
  84.             AttrV := MenuViewers.New(
  85.                 TextFrames.NewMenu("Kepler", "System.Close  System.Grow  Kepler.Delete  Kepler.SendBack  Edit.Store"),
  86.                 TextFrames.NewText(AttrT, 0),
  87.                 TextFrames.menuH,
  88.                 X, Y)
  89.         END
  90.     END InitAttrV;
  91.     PROCEDURE Constellations*;
  92.         VAR c: KeplerGraphs.Constellation; mod, class: ARRAY 32 OF CHAR;
  93.             sel: KeplerGraphs.Graph; minstate: INTEGER;
  94.     BEGIN
  95.         In.Open;
  96.         In.Int(minstate);
  97.         IF ~In.Done THEN minstate := 1 END ;
  98.         KeplerFrames.GetSelection(sel);
  99.         IF sel # NIL THEN
  100.             InitAttrV;
  101.             c := sel.cons;
  102.             WHILE c # NIL DO
  103.                 IF c.State() >= minstate THEN
  104.                     Texts.WriteInt(W, SYSTEM.VAL(LONGINT, c), 10);
  105.                     Texts.WriteString(W, "  ");
  106.                     KeplerGraphs.GetType(c, mod, class);
  107.                     Texts.WriteString(W, mod);Texts.Write(W, "."); Texts.WriteString(W, class);
  108.                     Texts.WriteLn(W)
  109.                 END ;
  110.                 Texts.Append(AttrT, W.buf);
  111.                 c := c.next
  112.             END
  113.         END
  114.     END Constellations;
  115.     PROCEDURE Delete*;
  116.         VAR
  117.             S: Texts.Scanner; sel: KeplerGraphs.Graph;
  118.             F: TextFrames.Frame;
  119.             R: Texts.Reader;
  120.             ch: CHAR;
  121.     BEGIN
  122.         KeplerFrames.GetSelection(sel);
  123.         IF sel # NIL THEN
  124.             IF AttrV # NIL THEN
  125.                 F := AttrV.dsc.next(TextFrames.Frame);
  126.                 IF F.hasSel THEN
  127.                     Texts.OpenScanner(S, AttrT, F.selbeg.org); Texts.Scan(S);
  128.                     IF S.class = Texts.Int THEN
  129.                         sel.Delete(SYSTEM.VAL(KeplerGraphs.Object, S.i));
  130.                         Texts.OpenReader(R, F.text, F.selbeg.org);
  131.                         Texts.Read(R, ch);
  132.                         WHILE (ch >= " ") OR (ch = 09X) DO Texts.Read(R, ch) END ;
  133.                         Texts.Delete(F.text, F.selbeg.org, Texts.Pos(R))
  134.                     END
  135.                 END
  136.             END
  137.         END
  138.     END Delete;
  139.     PROCEDURE Backup (VAR name: ARRAY OF CHAR);
  140.         VAR res, i: INTEGER; bak: ARRAY 64 OF CHAR;
  141.     BEGIN i := 0;
  142.         WHILE name[i] # 0X DO INC(i) END ;
  143.         IF i < 60 THEN COPY(name, bak);
  144.             bak[i] := "."; bak[i+1] := "B"; bak[i+2] := "a"; bak[i+3] := "k"; bak[i+4] := 0X;
  145.             Files.Rename(name, bak, res)
  146.         END
  147.     END Backup;
  148.     PROCEDURE Store*;
  149.         VAR par: Oberon.ParList;
  150.             V: Viewers.Viewer;
  151.             T: Texts.Text;
  152.             S: Texts.Scanner;
  153.             f: Files.File;
  154.             R: Files.Rider;
  155.             beg, end, time: LONGINT;
  156.     BEGIN
  157.         par := Oberon.Par; 
  158.         IF par.frame = par.vwr.dsc THEN
  159.             V := par.vwr; Texts.OpenScanner(S, V.dsc(TextFrames.Frame).text, 0)
  160.         ELSE V := Oberon.MarkedViewer(); Texts.OpenScanner(S, par.text, par.pos)
  161.         END;
  162.         Texts.Scan(S);
  163.         IF (S.class = Texts.Char) & (S.c = "^") THEN
  164.             Oberon.GetSelection(T, beg, end, time);
  165.             IF time >= 0 THEN Texts.OpenScanner(S, T, beg); Texts.Scan(S) END
  166.         END;
  167.         IF (S.class = Texts.Name) & (V.dsc # NIL) & (V.dsc.next IS KeplerFrames.Frame) THEN
  168.             Texts.WriteString(W, "Kepler.Store ");
  169.             Texts.WriteString(W, S.s); Texts.WriteLn(W);
  170.             Texts.Append(Oberon.Log, W.buf);
  171.             f := Files.New(S.s); Files.Set(R, f, 0); KeplerGraphs.Reset;
  172.             KeplerGraphs.WriteObj(R, V.dsc.next(KeplerFrames.Frame).G);
  173.             Backup(S.s);
  174.             Files.Register(f)
  175.         END
  176.     END Store;
  177.     PROCEDURE SetGrid*;
  178.         VAR i: INTEGER; F: KeplerFrames.Frame; V: Viewers.Viewer;
  179.     BEGIN
  180.         V := Oberon.MarkedViewer();
  181.         IF V.dsc.next IS KeplerFrames.Frame THEN
  182.             F := V.dsc.next(KeplerFrames.Frame);
  183.             In.Open; In.Int(i);
  184.             IF In.Done THEN
  185.                 F.grid := i; F.Restore(F.X, F.Y, F.W, F.H)
  186.             END
  187.         END
  188.     END SetGrid;
  189.     PROCEDURE SetScale*;
  190.         VAR F: KeplerFrames.Frame; V: Viewers.Viewer;
  191.             X, Y, i: INTEGER;
  192.     BEGIN
  193.         V := Oberon.MarkedViewer();
  194.         IF V.dsc.next IS KeplerFrames.Frame THEN
  195.             F := V.dsc.next(KeplerFrames.Frame);
  196.             In.Open; In.Int(i);
  197.             IF In.Done & (i > 0) THEN
  198.                 X := Oberon.Pointer.X;
  199.                 Y := Oberon.Pointer.Y;
  200.                 F.x0 := (X - F.X) * SHORT(i) - F.Cx(X);
  201.                 F.y0 := (Y - F.Y - F.H) * SHORT(i) - F.Cy(Y);
  202.                 F.scale := i; F.Restore(F.X, F.Y, F.W, F.H)
  203.             END
  204.         END
  205.     END SetScale;
  206.     PROCEDURE Join*;
  207.         VAR G: KeplerGraphs.Graph;
  208.             f, s: KeplerGraphs.Star;
  209.             c: KeplerGraphs.Constellation;
  210.         PROCEDURE JoinCons(c: KeplerGraphs.Constellation);
  211.             VAR i: INTEGER;
  212.                 p: KeplerGraphs.Star;
  213.         BEGIN
  214.             i := 0;
  215.             WHILE i < c.nofpts DO
  216.                 p := c.p[i];
  217.                 IF p.sel & ~(p IS KeplerGraphs.Planet) & (p # f) THEN
  218.                     G.Move(p, f.x - p.x, f.y - p.y);
  219.                     c.p[i] := f; INC(f.refcnt); DEC(p.refcnt);
  220.                     IF p.refcnt = 0 THEN G.Delete(p) END
  221.                 ELSIF p IS KeplerGraphs.Planet THEN
  222.                     JoinCons(p(KeplerGraphs.Planet).c)
  223.                 END ;
  224.                 INC(i)
  225.             END
  226.         END JoinCons;
  227.     BEGIN (* Join *)
  228.         G := KeplerFrames.Focus;
  229.         IF KeplerFrames.nofpts >= 1 THEN
  230.             KeplerFrames.ConsumePoint(f);
  231.             DEC(f.refcnt);
  232.             c := G.cons;
  233.             WHILE c # NIL DO
  234.                 JoinCons(c); c := c.next
  235.             END ;
  236.             G.SendToBack(f); s := f.next;
  237.             WHILE s # NIL DO
  238.                 IF (s IS KeplerGraphs.Planet) & (s # f) THEN JoinCons(s(KeplerGraphs.Planet).c) END ;
  239.                 s := s.next
  240.             END
  241.         END
  242.     END Join;
  243.     PROCEDURE Split*;
  244.         VAR G: KeplerGraphs.Graph;
  245.             c: KeplerGraphs.Constellation;
  246.             s: KeplerGraphs.Star;
  247.         PROCEDURE SplitCons(c: KeplerGraphs.Constellation);
  248.             VAR i: INTEGER; p, q: KeplerGraphs.Star;
  249.         BEGIN
  250.             FOR i := 0 TO c.nofpts - 1 DO
  251.                 p := c.p[i];
  252.                 IF p.sel THEN (* split *)
  253.                     NEW(q); c.p[i] := q;
  254.                     q^ := p^; q.refcnt := 1;
  255.                     q.next := G.stars; G.stars := q;
  256.                     DEC(p.refcnt);
  257.                     IF (p.refcnt = 0) & ~(p IS KeplerGraphs.Planet) THEN G.Delete(p) END
  258.                 END
  259.             END
  260.         END SplitCons;
  261.     BEGIN (*Spit *)
  262.         KeplerFrames.GetSelection(G);
  263.         IF G # NIL THEN
  264.             c := G.cons;
  265.             WHILE c # NIL DO
  266.                 SplitCons(c);
  267.                 c := c.next
  268.             END ;
  269.             s := G.stars;
  270.             WHILE s # NIL DO
  271.                 IF s IS KeplerGraphs.Planet THEN SplitCons(s(KeplerGraphs.Planet).c) END ;
  272.                 s := s.next
  273.             END ;
  274.         END
  275.     END Split;
  276.     PROCEDURE SendBack*;
  277.         VAR
  278.             S: Texts.Scanner; sel: KeplerGraphs.Graph;
  279.             F: TextFrames.Frame;
  280.     BEGIN
  281.         KeplerFrames.GetSelection(sel);
  282.         IF sel # NIL THEN
  283.             IF AttrV # NIL THEN
  284.                 F := AttrV.dsc.next(TextFrames.Frame);
  285.                 IF F.hasSel THEN
  286.                     Texts.OpenScanner(S, AttrT, F.selbeg.org); Texts.Scan(S);
  287.                     IF S.class = Texts.Int THEN
  288.                         sel.SendToBack(SYSTEM.VAL(KeplerGraphs.Object, S.i));
  289.                     END
  290.                 END
  291.             END
  292.         END
  293.     END SendBack;
  294.     PROCEDURE AlignX*;
  295.         VAR G: KeplerGraphs.Graph; s, p: KeplerGraphs.Star;
  296.     BEGIN
  297.         IF KeplerFrames.nofpts > 0 THEN
  298.             KeplerFrames.GetPoint(p);
  299.             KeplerFrames.GetSelection(G);
  300.             s := G.stars;
  301.             WHILE s # NIL DO
  302.                 IF s.sel & ~(s IS KeplerGraphs.Planet) THEN G.Move(s, p.x - s.x, 0) END ;
  303.                 s := s.next
  304.             END
  305.         END
  306.     END AlignX;
  307.     PROCEDURE AlignY*;
  308.         VAR G: KeplerGraphs.Graph; s, p: KeplerGraphs.Star;
  309.     BEGIN
  310.         IF KeplerFrames.nofpts > 0 THEN
  311.             KeplerFrames.GetPoint(p);
  312.             KeplerFrames.GetSelection(G);
  313.             s := G.stars;
  314.             WHILE s # NIL DO
  315.                 IF s.sel & ~(s IS KeplerGraphs.Planet) THEN G.Move(s, 0, p.y - s.y) END ;
  316.                 s := s.next
  317.             END
  318.         END
  319.     END AlignY;
  320.     PROCEDURE AlignToGrid*;
  321.         VAR V: Viewers.Viewer; F: KeplerFrames.Frame; s: KeplerGraphs.Star; X, Y: INTEGER;
  322.     BEGIN
  323.         V := Oberon.MarkedViewer();
  324.         IF V.dsc.next IS KeplerFrames.Frame THEN
  325.             F := V.dsc.next(KeplerFrames.Frame);
  326.             IF F.grid > 0 THEN
  327.                 s := F.G.stars;
  328.                 WHILE s # NIL DO
  329.                     IF s.sel & ~(s IS KeplerGraphs.Planet) THEN
  330.                         X := F.CX(s.x); Y := F.CY(s.y);
  331.                         KeplerFrames.AlignToGrid(F, X, Y);
  332.                         F.G.Move(s, F.Cx(X) - s.x, F.Cy(Y) - s.y)
  333.                     END ;
  334.                     s := s.next
  335.                 END
  336.             END
  337.         END
  338.     END AlignToGrid;
  339.     PROCEDURE Reset*;
  340.         VAR V: Viewers.Viewer; F: KeplerFrames.Frame;
  341.     BEGIN
  342.         V := Oberon.MarkedViewer();
  343.         IF V.dsc.next IS KeplerFrames.Frame THEN F := V.dsc.next(KeplerFrames.Frame);
  344.             F.x0 := 0; F.y0 := 0; F.scale := 4;
  345.             F.Restore(F.X, F.Y, F.W, F.H)
  346.         END
  347.     END Reset;
  348.     PROCEDURE Recall*;
  349.     BEGIN KeplerGraphs.Recall;
  350.     END Recall;
  351.     PROCEDURE ScalePoints*;
  352.         VAR sel: KeplerGraphs.Graph;
  353.             p0, p1, p2, s: KeplerGraphs.Star;
  354.             cx, cy, dx, dy: REAL;
  355.     BEGIN
  356.         KeplerFrames.GetSelection(sel);
  357.         IF (sel # NIL) & (KeplerFrames.nofpts >= 3) THEN
  358.             KeplerFrames.GetPoint(p0);
  359.             KeplerFrames.GetPoint(p1);
  360.             KeplerFrames.GetPoint(p2);
  361.             IF p0.x = p1.x THEN cx := 1 ELSE cx := (p0.x - p2.x) / (p0.x - p1.x) END ;
  362.             dx := p0.x - p0.x * cx; 
  363.             IF p0.y = p1.y THEN cy := 1 ELSE cy := (p0.y - p2.y) / (p0.y - p1.y) END ;
  364.             dy := p0.y - p0.y * cy;
  365.             s := sel.stars;
  366.             WHILE s # NIL DO
  367.                 IF s.sel & ~(s IS KeplerGraphs.Planet) THEN
  368.                     sel.Move(s, SHORT(ENTIER((s.x * cx + dx) - s.x)), SHORT(ENTIER((s.y * cy + dy) - s.y)))
  369.                 END ;
  370.                 s := s.next
  371.             END
  372.         END
  373.     END ScalePoints;
  374.     PROCEDURE DumpFocus*;
  375.         VAR fp: KeplerFrames.FocusPoint;
  376.     BEGIN
  377.         Out.Int(KeplerFrames.nofpts); Out.Ln;
  378.         fp := KeplerFrames.first;
  379.         WHILE fp # NIL DO
  380.             Out.Int(fp.p.x); Out.Int(fp.p.y);
  381.             IF fp.p.sel THEN Out.WriteString("sel  ") ELSE Out.WriteString("~sel  ") END ;
  382.             Out.Ln;
  383.             fp := fp.next
  384.         END
  385.     END DumpFocus;
  386.     PROCEDURE DumpGraph*;
  387.         VAR p: KeplerGraphs.Star;
  388.     BEGIN
  389.         p := KeplerFrames.Focus.stars;
  390.         Out.WriteString("seltime = "); Out.Int(KeplerFrames.Focus.seltime); Out.Ln;
  391.         WHILE p # NIL DO
  392.             Out.Int(p.x); Out.Int(p.y);
  393.             IF p.sel THEN Out.WriteString("sel  ") ELSE Out.WriteString("~sel  ") END ;
  394.             Out.Int(p.refcnt); 
  395.             Out.Ln;
  396.             p := p.next
  397.         END
  398.     END DumpGraph;
  399. BEGIN
  400.     Texts.OpenWriter(W);
  401.     AttrT := TextFrames.Text("")
  402. END Kepler.
  403.