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

  1. Syntax24b.Scn.Fnt
  2. ParcElems
  3. Alloc
  4. Syntax10.Scn.Fnt
  5. Syntax10i.Scn.Fnt
  6. Syntax10b.Scn.Fnt
  7. Syntax8.Scn.Fnt
  8. Syntax12.Scn.Fnt
  9. (* Amiga NonFPU *) 
  10. MODULE TextFrames;    (** CAS/MH/HM 20.4.94/NW 12.11.94 **)    (*<< Linz Cursor Up/Down*) 
  11.     IMPORT Modules, Input, Display, Files, Fonts, Viewers, Oberon, MenuViewers, Texts, AmigaMath;
  12.     CONST
  13.         (** update message IDs **)
  14.             replace* = 0; insert* = 1; delete* = 2;
  15.         (** units **)
  16.             mm* = 36000;  Unit* = 10000;
  17.         (** parc options **)
  18.             gridAdj* = 0;  leftAdj* = 1;  rightAdj* = 2;  pageBreak* = 3;    twoColumns* = 4;
  19.         (** maximum number of TAB stops in Parc **)
  20.             MaxTabs* = 32;
  21.         AdjMask = {leftAdj, rightAdj};
  22.         TAB = 9X; LF = 0AX; CR = 0DX; DEL = 7FX; BRK = 0ACX; ShiftBRK = 0ADX; CRSL = 0C4X; CRSR = 0C3X;
  23.         CRSU = 0C1X; CRSD = 0C2X;    (*<<*) 
  24.         AdjustSpan = 30; MinTabWidth = 3 * mm; StdTabWidth = 4 * mm;
  25.         rightKey = 0; middleKey = 1; leftKey = 2; cancel = {rightKey, middleKey, leftKey};
  26.     TYPE
  27.         Parc* = POINTER TO ParcDesc;
  28.         ParcDesc* = RECORD (Texts.ElemDesc)
  29.             left*: LONGINT;    (** distance from (F.X + F.left); in units **)
  30.             first*: LONGINT;    (** first line indentation from P.left; in units **)
  31.             width*: LONGINT;    (** parc width; in units **)
  32.             lead*: LONGINT;    (** distance to previous line; in units **)
  33.             lsp*: LONGINT;    (** line spacing of text after P; in units **)
  34.             dsr*: LONGINT;    (** descender of text after P; in units **)
  35.             opts*: SET;
  36.             nofTabs*: INTEGER;
  37.             tab*: ARRAY MaxTabs OF LONGINT    (** in units **)
  38.         END;
  39.         TextLine = POINTER TO TextLineDesc;
  40.         Location* = RECORD
  41.             org*, pos*: LONGINT;
  42.             x*, y*, dx*, dy*: INTEGER;
  43.             line: TextLine
  44.         END;
  45.         TextLineDesc = RECORD
  46.             next: TextLine;
  47.             eot: BOOLEAN;                (* contains end of text *)    
  48.             indent: LONGINT;                (* line indentation in units *)
  49.             w, h, dsr: INTEGER;            (* bounding box clipped to frame (w including indent) *)
  50.             w0, nob: INTEGER;            (* unclipped width (including indent), number of contained blanks: nob > 0 if text line wraps around *)
  51.             org, len, span: LONGINT;    (* len ... characters w/o; span ... w/ trailing CR or white space, if any *)
  52.             P: Parc;                            (* last parc before this text line *)
  53.             pbeg: LONGINT                (* position of P *)
  54.         END;
  55.         Frame* = POINTER TO FrameDesc;
  56.         FrameDesc* = RECORD (Display.FrameDesc)
  57.             text*: Texts.Text;
  58.             org*: LONGINT;
  59.             col*, left*, right*, top*, bot*: INTEGER;
  60.             markH*: INTEGER;    (** position of tick mark in scroll bar (< 0 => no tick mark) **)
  61.             barW*: INTEGER;    (** scroll bar width **)
  62.             time*: LONGINT;    (** selection time **)
  63.             hasCar*, hasSel*, showsParcs*: BOOLEAN;    (** caret/selection present; parcs visible **)
  64.             carloc*, selbeg*, selend*: Location;
  65.             focus*: Display.Frame;    (** frame of nested element if this element contains the focus **)
  66.             trailer: TextLine    (* ring with trailer and header *)
  67.         END;
  68.         DisplayMsg* = RECORD (Texts.ElemMsg)
  69.             prepare*: BOOLEAN;
  70.             fnt*: Fonts.Font;
  71.             col*: SHORTINT;
  72.             pos*: LONGINT;    (** position in host text **)
  73.             frame*: Display.Frame;    (** ~prepare => host frame **)
  74.             X0*, Y0*: INTEGER;    (** ~prepare => receiver origin in screen space **)
  75.             indent*: LONGINT;    (** prepare => width already consumed in line, in units **)
  76.             elemFrame*: Display.Frame    (** optional return parameter **)
  77.         END;
  78.         TrackMsg* = RECORD (Texts.ElemMsg)
  79.             X*, Y*: INTEGER;
  80.             keys*: SET;
  81.             fnt*: Fonts.Font;
  82.             col*: SHORTINT;
  83.             pos*: LONGINT;    (** position in host text **)
  84.             frame*: Display.Frame;    (** host frame **)
  85.             X0*, Y0*: INTEGER    (** receiver origin in screen space **)
  86.         END;
  87.         FocusMsg* = RECORD (Texts.ElemMsg)
  88.             focus*: BOOLEAN;    (** whether to focus or to defocus **)
  89.             elemFrame*: Display.Frame;    (** focus/defocus target **)
  90.             frame*: Display.Frame    (** host frame **)
  91.         END;
  92.         NotifyMsg* = RECORD (Display.FrameMsg)
  93.             frame*: Display.Frame    (** host frame **)
  94.         END;
  95.         UpdateMsg* = RECORD (Display.FrameMsg)
  96.             id*: INTEGER;
  97.             text*: Texts.Text;
  98.             beg*, end*: LONGINT
  99.         END;
  100.         InsertElemMsg* = RECORD (Display.FrameMsg)
  101.             e*: Texts.Elem
  102.         END;
  103.         SelectMsg = RECORD (Display.FrameMsg)
  104.             text: Texts.Text;
  105.             beg, end: LONGINT;
  106.             time: LONGINT
  107.         END;
  108.         menuH*, barW*, left*, right*, top*, bot*: INTEGER;
  109.         defParc*: Parc;
  110.         (*shared globals => get rid off in a later version?*)
  111.         W, W0: Texts.Writer;
  112.         B: Texts.Buffer;
  113.         P: Parc;
  114.         pbeg: LONGINT;    (*inv T[pbeg] = P*)
  115.         R: Texts.Reader;
  116.         nextCh: CHAR;    (*inv Base(R) = T => T[Pos(R)-1] = nextCh]*)
  117.         par: Oberon.ParList;
  118.         neutralize: Oberon.ControlMsg;
  119.     PROCEDURE Min (x, y: INTEGER): INTEGER;
  120.     BEGIN IF x < y THEN RETURN x ELSE RETURN y END
  121.     END Min;
  122.     PROCEDURE Max (x, y: INTEGER): INTEGER;
  123.     BEGIN IF x > y THEN RETURN x ELSE RETURN y END
  124.     END Max;
  125.     PROCEDURE MarkMenu (F: Frame);
  126.         VAR R: Texts.Reader; V: Viewers.Viewer; T: Texts.Text; ch: CHAR;
  127.     BEGIN V := Viewers.This(F.X, F.Y);
  128.         IF (V IS MenuViewers.Viewer) & (V.dsc IS Frame) & (F # V.dsc) THEN
  129.             T := V.dsc(Frame).text;
  130.             IF T.len > 0 THEN Texts.OpenReader(R, T, T.len - 1); Texts.Read(R, ch) ELSE ch := 0X END;
  131.             IF ch # "!" THEN Texts.Write(W0, "!"); Texts.Append(T, W0.buf) END
  132.         END
  133.     END MarkMenu;
  134.     (* Element Subframes *)
  135.     PROCEDURE InvertBorder (F: Display.Frame);
  136.     BEGIN
  137.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y-1, F.W+2, 1, Display.invert);
  138.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y+F.H, F.W+2, 1, Display.invert);
  139.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y, 1, F.H, Display.invert);
  140.         Display.ReplPattern(Display.white, Display.grey1, F.X+F.W, F.Y, 1, F.H, Display.invert)
  141.     END InvertBorder;
  142.     PROCEDURE InvalSubFrames (F: Frame; x, y, w, h: INTEGER);    (* removes and suspends all subframes partly in (x, y, w, h) *)
  143.         VAR p, f: Display.Frame; msg: MenuViewers.ModifyMsg;
  144.     BEGIN
  145.         IF (w > 0) & (h > 0) THEN f := F.dsc;
  146.             IF f # NIL THEN p := f; f := p.next END;
  147.             WHILE f # NIL DO
  148.                 IF (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN p.next := f.next;
  149.                     msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  150.                     f.handle(f, msg)
  151.                 ELSE p := f
  152.                 END;
  153.                 f := p.next
  154.             END;
  155.             f := F.dsc;
  156.             IF (f # NIL) & (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN F.dsc := F.dsc.next;
  157.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  158.                 f.handle(f, msg)
  159.             END
  160.         END
  161.     END InvalSubFrames;
  162.     PROCEDURE ShiftSubFrames (F: Frame; oldY, newY, h: INTEGER);    (* shift (F.X, oldY, F.W, h) to (F.X, newY, F.W, h) *)
  163.         VAR f: Display.Frame; msg: MenuViewers.ModifyMsg;
  164.     BEGIN
  165.         IF oldY > newY THEN InvalSubFrames(F, F.X, newY, F.W, oldY - newY)
  166.         ELSE InvalSubFrames(F, F.X, oldY + h, F.W, newY - oldY)
  167.         END;
  168.         f := F.dsc;
  169.         WHILE f # NIL DO
  170.             IF (f.Y < oldY + h) & (f.Y + f.H > oldY) THEN INC(f.Y, newY - oldY);
  171.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := f.H;
  172.                 f.handle(f, msg)
  173.             END;
  174.             f := f.next
  175.         END
  176.     END ShiftSubFrames;
  177.     PROCEDURE NotifySubFrames (F: Frame; VAR msg: Display.FrameMsg);
  178.         VAR p, f: Display.Frame;
  179.     BEGIN f := F.dsc;
  180.         IF msg IS NotifyMsg THEN msg(NotifyMsg).frame := F END;
  181.         WHILE f # NIL DO p := f; f := f.next; p.handle(p, msg) END
  182.     END NotifySubFrames;
  183.     (* Display Primitives *)
  184.     PROCEDURE DrawCursor (x, y: INTEGER);
  185.     BEGIN Oberon.DrawCursor(Oberon.Mouse, Oberon.Arrow, x, y)
  186.     END DrawCursor;
  187.     PROCEDURE TrackMouse (VAR x, y: INTEGER; VAR keys, keysum: SET);
  188.     BEGIN Input.Mouse(keys, x, y); keysum := keysum + keys; DrawCursor(x, y)
  189.     END TrackMouse;
  190.     PROCEDURE EraseRect (F: Frame; x, y, w, h: INTEGER);
  191.     BEGIN Display.ReplConst(F.col, x, y, w, h, Display.replace); InvalSubFrames(F, x, y, w, h)
  192.     END EraseRect;
  193.     PROCEDURE Erase (F: Frame; x, y, w, h: INTEGER);    (*RemoveMarks optimization*)
  194.     BEGIN
  195.         IF h > 0 THEN Oberon.RemoveMarks(x, y, w, h); EraseRect(F, x, y, w, h) END
  196.     END Erase;
  197.     PROCEDURE Shift (F: Frame; oldY, newY, h: INTEGER);    (*RemoveMarks optimization*)
  198.     BEGIN
  199.         IF (oldY # newY) & (h > 0) THEN
  200.             Oberon.RemoveMarks(F.X + F.left, Min(oldY, newY), F.W - F.left, Max(oldY, newY) + h);
  201.             Display.CopyBlock(F.X + F.left, oldY, F.W - F.left, h, F.X + F.left, newY, Display.replace);
  202.             ShiftSubFrames(F, oldY, newY, h)
  203.         END
  204.     END Shift;
  205.     PROCEDURE InvertCaret (F: Frame);
  206.         VAR loc: Location; bot: INTEGER;
  207.     BEGIN loc := F.carloc; bot := loc.y + loc.line.dsr - 6;
  208.         Display.CopyPatternC(F, Display.white, Display.hook, loc.x, bot, Display.invert)
  209.     END InvertCaret;
  210.     PROCEDURE InvertRect (F: Frame; x, y, w, h: INTEGER);    (*clips to right and bottom frame margin*)
  211.     BEGIN
  212.         IF x + w > F.X + F.W - F.right THEN w := F.X + F.W - F.right - x END;
  213.         IF y >= F.Y + F.bot THEN Display.ReplConst(Display.white, x, y, w, h, Display.invert) END
  214.     END InvertRect;
  215.     PROCEDURE InvertSelection (F: Frame; beg, end: Location);
  216.         VAR t: TextLine; ex, rx, w, py: INTEGER;
  217.     BEGIN
  218.         rx := F.X + F.W - F.right; t := end.line;
  219.         IF t.eot OR (end.pos <= t.org + t.len) THEN ex := end.x ELSE ex := rx END;
  220.         IF beg.line = end.line THEN InvertRect(F, beg.x, beg.y, ex - beg.x, beg.line.h)
  221.         ELSE t := beg.line; py := beg.y; w := F.W - F.left - F.right;
  222.             InvertRect(F, beg.x, py, rx - beg.x, t.h); t := t.next; DEC(py, t.h);
  223.             WHILE t # end.line DO InvertRect(F, F.X + F.left, py, w, t.h); t := t.next; DEC(py, t.h) END;
  224.             InvertRect(F, F.X + F.left, py, ex - (F.X + F.left), t.h)
  225.         END
  226.     END InvertSelection;
  227.     PROCEDURE CoordToPos (F: Frame; mh: INTEGER): LONGINT;
  228.         VAR h: INTEGER; Dum1, Dum2: REAL;
  229.     BEGIN h := F.H - 1;
  230.         IF h > 0 THEN
  231.             AmigaMath.IntToReal(F.text.len, Dum1);
  232.             AmigaMath.IntToReal(h, Dum2);
  233.             AmigaMath.Div(Dum1, Dum2, Dum1);
  234.             AmigaMath.IntToReal(h - mh, Dum2);
  235.             AmigaMath.Mul(Dum1, Dum2, Dum1);
  236.             RETURN AmigaMath.Entier(Dum1);
  237.         ELSE RETURN 0 END
  238. (*        IF h > 0 THEN RETURN ENTIER(F.text.len / h * (h - mh)) ELSE RETURN 0 END*)
  239.     END CoordToPos;
  240.     PROCEDURE ShowBar (F: Frame; botH, topH: INTEGER);
  241.     BEGIN
  242.         IF (F.left > F.barW) & (F.barW > 0) THEN
  243.             Display.ReplConst(Display.white, F.X + F.barW - 1, F.Y + botH, 1, topH - botH, Display.replace)
  244.         END
  245.     END ShowBar;
  246.     PROCEDURE Tick (F: Frame);
  247.     BEGIN
  248.         IF (0 <= F.markH) & (F.markH < F.H) & (F.left > F.barW) & (F.barW > 6) & (F.H > 2) THEN
  249.             Display.ReplConst(Display.white, F.X + 1, F.Y + F.markH, F.barW - 6, 2, Display.invert)
  250.         END
  251.     END Tick;
  252.     PROCEDURE ShowTick (F: Frame);    (* removes global marks as needed *)
  253.         VAR h, mh: INTEGER; len: LONGINT; Dum1, Dum2: REAL;
  254.     BEGIN
  255.         h := F.H - 2; len := F.text.len;
  256.         IF len > 0 THEN 
  257.             AmigaMath.IntToReal(F.org, Dum1);
  258.             AmigaMath.IntToReal(len, Dum2);
  259.             AmigaMath.Div(Dum1, Dum2, Dum1);
  260.             AmigaMath.IntToReal(h, Dum2);
  261.             AmigaMath.Mul(Dum1, Dum2, Dum1);
  262.             AmigaMath.Sub(Dum2, Dum1, Dum1);
  263.             mh := SHORT(AmigaMath.Entier(Dum1))
  264. (*            mh := SHORT(ENTIER(h - F.org / len * h))*)
  265.         ELSE mh := h END;
  266.         IF F.markH # mh THEN Oberon.RemoveMarks(F.X, F.Y, F.barW, F.H);
  267.             Tick(F); F.markH := mh; Tick(F)
  268.         END
  269.     END ShowTick;
  270.     PROCEDURE Mark* (F: Frame; mark: INTEGER);
  271.     BEGIN
  272.         Erase(F, F.X, F.Y, F.barW - 1, F.H); F.markH := -1;
  273.         IF (mark < 0) & (F.H >= 16) THEN
  274.             Display.CopyPattern(Display.white, Display.downArrow, F.X, F.Y, Display.invert)
  275.         ELSIF mark > 0 THEN
  276.             ShowTick(F)
  277.         END
  278.     END Mark;
  279.     (** Parcs **)
  280.     PROCEDURE ParcBefore* (T: Texts.Text; pos: LONGINT; VAR P: Parc; VAR beg: LONGINT);
  281.         VAR R: Texts.Reader;
  282.     BEGIN Texts.OpenReader(R, T, pos + 1);
  283.         REPEAT Texts.ReadPrevElem(R) UNTIL R.eot OR (R.elem IS Parc);
  284.         IF R.eot THEN P := defParc; beg := -1 ELSE P := R.elem(Parc); beg := Texts.Pos(R) END
  285.     END ParcBefore;
  286.     PROCEDURE InitDefParc;
  287.     BEGIN
  288.         IF Modules.ThisMod("ParcElems") = NIL THEN HALT(99) END
  289.         (* side effect: body of ParcElems initialises defParc *)
  290.     END InitDefParc;
  291.     (* Screen Metrics *)
  292.     PROCEDURE Tab (dw: INTEGER; VAR dx: INTEGER);    (*P set*)
  293.         (* dw = line width from left margin to caret (in pixels); dx = distance from caret to next tab stop (in pixels) *)
  294.         VAR i, n: INTEGER; w: LONGINT;
  295.     BEGIN
  296.         i := 0; n := P.nofTabs; w := LONG(dw) * Unit + MinTabWidth;
  297.         IF dw < 0 THEN dx := -dw
  298.         ELSE
  299.             WHILE (i < n) & (P.tab[i] < w) DO INC(i) END;
  300.             IF i < n THEN dx := SHORT((P.tab[i] - LONG(dw) * Unit) DIV Unit)
  301.             ELSE dx := StdTabWidth DIV Unit
  302.             END
  303.         END
  304.     END Tab;
  305.     PROCEDURE MeasureSpecial (dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  306.         (* returns metrics of nextCh (nextCh <= " "); sends prepare message to elements; P, R, nextCh set *)
  307.         VAR e: Texts.Elem; pat: Display.Pattern; msg: DisplayMsg;
  308.     BEGIN
  309.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  310.             x := 0; y := 0; w := dx; h := 0
  311.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  312.         ELSIF R.elem # NIL THEN e := R.elem;
  313.             msg.prepare := TRUE; msg.indent := LONG(dw) * Unit;
  314.             msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R)-1;
  315.             msg.Y0 := -SHORT(P.dsr DIV Unit);    (*<<< 18-Nov-91*)
  316.             e.handle(e, msg);
  317.             w := SHORT(e.W DIV Unit);
  318.             dx := w; x := 0; y := msg.Y0; h := SHORT(e.H DIV Unit)    (*<<< 18-Nov-91*)
  319.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  320.         END
  321.     END MeasureSpecial;
  322.     PROCEDURE GetSpecial (F: Frame; VAR n: INTEGER; cn, ddx, dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  323.         (* returns metrics of nextCh (nextCh <= " "); no prepare message to elements; extends blanks for block adjust *)
  324.         (* cn ... add 1 pixel to first cn blanks (block adjust); ddx ... add ddx pixels to every blank (block adjust) *)
  325.         (*P, R, nextCh set*)
  326.         VAR e: Texts.Elem; pat: Display.Pattern;
  327.     BEGIN
  328.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  329.             x := 0; y := 0; INC(dx, ddx); INC(n); IF n <= cn THEN INC(dx) END;    (*space correction for block adjustment*)
  330.             w := dx; h := 0
  331.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  332.         ELSIF R.elem # NIL THEN e := R.elem;
  333.             IF (e IS Parc) & (P.W = 9999 * Unit) THEN (* P gets this value in prepare message *)
  334.                 w := Min(SHORT((P.width + P.left) DIV Unit), F.W - F.right - F.left);
  335.                 e.W := LONG(w) * Unit
  336.             ELSE w := SHORT(e.W DIV Unit)
  337.             END;
  338.             dx := w; x := 0; y := -SHORT(P.dsr DIV Unit); h := SHORT(e.H DIV Unit)
  339.         ELSIF ~R.eot THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  340.         ELSE dx := 0; x := 0; y := 0; w := 0; h := 0
  341.         END
  342.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  343.         END
  344.     END GetSpecial;
  345.     PROCEDURE NextLine (T: Texts.Text; VAR org: LONGINT);    (*R, nextCh set; org = Texts.Pos(R)-1*)
  346.         VAR pat: Display.Pattern; pos, bk, d: LONGINT; width, tw, dx, x, y, w, h: INTEGER;
  347.             R1: Texts.Reader; peekCh: CHAR; indent: INTEGER;
  348.     BEGIN
  349.         tw := 0; dx := 0; w := 0; bk := -999;    (* bk = pos of last seperator *)
  350.         pos := org; ParcBefore(T, pos, P, pbeg); width := SHORT(P.width DIV Unit);
  351.         indent := 0;
  352.         IF org > 0 THEN Texts.OpenReader(R1, T, org - 1); Texts.Read(R1, peekCh);
  353.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN indent := SHORT(P.first DIV Unit) END;
  354.         END;
  355.         INC(tw, indent);
  356.         LOOP INC(pos);    (*inv pos = Texts.Pos(R), ~R.eof => nextCh = text[pos-1]*)
  357.             IF R.eot OR (nextCh = CR) THEN EXIT END;
  358.             INC(tw, dx);
  359.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  360.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  361.             END;
  362.             IF tw + x + w > width THEN d := pos - bk;
  363.                 IF (d < AdjustSpan) & (nextCh > " ") THEN pos := bk
  364.                 ELSIF ((nextCh > " ") OR (nextCh = Texts.ElemChar)) & (pos > org + 1) THEN DEC(pos)
  365.                 END;
  366.                 Texts.OpenReader(R, T, pos); Texts.Read(R, nextCh);
  367.                 EXIT
  368.             END;
  369.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN bk := pos END;
  370.             Texts.Read(R, nextCh)
  371.         END;
  372.         org := pos
  373.     END NextLine;
  374.     PROCEDURE BegOfLine (T: Texts.Text; VAR pos: LONGINT; adjust: BOOLEAN);
  375.         (* returns origin of line containing pos *)
  376.         VAR p, org: LONGINT;
  377.     BEGIN
  378.         IF pos <= 0 THEN pos := 0
  379.         ELSE
  380.             IF pos <= T.len THEN org := pos ELSE org := T.len END;
  381.             LOOP    (*search backwards for CR*)
  382.                 IF org = 0 THEN EXIT END;
  383.                 Texts.OpenReader(R, T, org - 1); Texts.Read(R, nextCh);
  384.                 IF nextCh = CR THEN EXIT END;
  385.                 DEC(org)
  386.             END;
  387.             IF adjust THEN    (*search forward for actual line origin*)
  388.                 Texts.OpenReader(R, T, org); Texts.Read(R, nextCh); p := org;
  389.                 REPEAT org := p; NextLine(T, p) UNTIL (p > pos) OR R.eot
  390.             END;
  391.             pos := org
  392.         END
  393.     END BegOfLine;
  394.     PROCEDURE AdjustMetrics (F: Frame; t: TextLine; VAR pw, tw, ddx, cn: INTEGER);    (*t.org set*)
  395.         (* pw ... x-coord of first char in line (in pixels); tw ... width of text line; ddx, cn ... see GetSpecial *)
  396.     BEGIN
  397.         P := t.P; pbeg := t.pbeg;
  398.         pw := F.left; tw := t.w; ddx := 0; cn := 0;
  399.         IF t.pbeg # t.org THEN
  400.             INC(pw, SHORT((P.left + t.indent) DIV Unit));
  401.             IF leftAdj IN P.opts THEN
  402.                 IF (rightAdj IN P.opts) & (t.nob > 0) THEN
  403.                     tw := SHORT(P.width DIV Unit); ddx := (tw - t.w0) DIV t.nob; cn := (tw - t.w0) MOD t.nob
  404.                 END
  405.             ELSIF rightAdj IN P.opts THEN INC(pw, SHORT(P.width DIV Unit) - t.w0)
  406.             ELSE (*center*) INC(pw, (SHORT(P.width DIV Unit) - t.w0) DIV 2)
  407.             END;
  408.             DEC(tw, SHORT(t.indent DIV Unit));
  409.         END
  410.     END AdjustMetrics;
  411.     (* Screen Placement *)
  412.     PROCEDURE DrawSpecial (F: Frame; px, py, x, y: INTEGER);    (*R, nextCh set*)
  413.         VAR e: Texts.Elem; pat: Display.Pattern; dx, w, h: INTEGER; msg: DisplayMsg;
  414.     BEGIN
  415.         IF (nextCh = TAB) OR (nextCh = CR) THEN (*skip*)
  416.         ELSIF R.elem # NIL THEN e := R.elem;
  417.             IF ~(e IS Parc) OR F.showsParcs THEN
  418.                 msg.prepare := FALSE; msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R) - 1;
  419.                 msg.frame := F; msg.X0 := px + x; msg.Y0 := py + y; msg.elemFrame := NIL;
  420.                 e.handle(e, msg);
  421.                 IF msg.elemFrame # NIL THEN msg.elemFrame.next := F.dsc; F.dsc := msg.elemFrame END;
  422.             ELSIF pageBreak IN e(Parc).opts THEN (*(e IS Parc) & ~F.showsParcs*)
  423.                 Display.ReplPattern(Display.white, Display.grey1, px + x, py, SHORT(e.W DIV Unit), 1, Display.replace)
  424.             END
  425.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  426.             Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  427.         END;
  428.     END DrawSpecial;
  429.     PROCEDURE ShowLine (F: Frame; t: TextLine; left, right, py: INTEGER);
  430.         VAR pat: Display.Pattern; i: LONGINT; n, cn, lm, px, pw, tw, ddx, dx, x, y, w, h: INTEGER;
  431.     BEGIN
  432.         (* lm ... left parc margin in screen coord; pw ...  x of first char in frame coord *)
  433.         Texts.OpenReader(R, F.text, t.org); AdjustMetrics(F, t, pw, tw, ddx, cn);
  434.         lm := F.X + F.left + SHORT(P.left DIV Unit); px := F.X + pw; INC(py, t.dsr); i := 0; n := 0;
  435.         WHILE i < t.len DO Texts.Read(R, nextCh);
  436.             IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, px - lm, dx, x, y, w, h)
  437.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  438.             END;
  439.             INC(y, R.fnt.height * R.voff DIV 64);
  440.             IF px + x + w <= right THEN
  441.                 IF px + x >= left THEN
  442.                     IF nextCh <= " " THEN DrawSpecial(F, px, py, x, y)
  443.                     ELSE Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  444.                     END
  445.                 END;
  446.                 INC(px, dx); INC(i)
  447.             ELSE i := t.len
  448.             END
  449.         END
  450.     END ShowLine;
  451.     PROCEDURE ShowLines (F: Frame; botH, topH: INTEGER; erase: BOOLEAN);
  452.         VAR t: TextLine; ph: INTEGER;
  453.     BEGIN
  454.         t := F.trailer.next; ph := F.H - F.top;
  455.         WHILE (t # F.trailer) & (ph - t.h >= topH) DO DEC(ph, t.h); t := t.next END;
  456.         WHILE (t # F.trailer) & (ph - t.h >= botH) DO DEC(ph, t.h);
  457.             IF erase THEN Erase(F, F.X + F.left, F.Y + ph, F.W - F.right - F.left, t.h) END;
  458.             ShowLine(F, t, F.X + F.left, F.X + F.W - F.right, F.Y + ph); t := t.next
  459.         END
  460.     END ShowLines;
  461.     (* Screen Casting *)
  462.     PROCEDURE MeasureLine (F: Frame; maxW: INTEGER; t: TextLine);    (* R, nextCh set *)
  463.         VAR pat: Display.Pattern; len, bklen, d: LONGINT; eol: BOOLEAN;
  464.             nob, bknob, width, minY, bkminY, maxY, bkmaxY, tw, bktw, lsp, dsr, dx, x, y, w, h: INTEGER;
  465.             R1: Texts.Reader; peekCh: CHAR;
  466.             (* bk* ... backup for last blank *)
  467.     BEGIN
  468.         len := 0; nob := 0; bklen := -999; tw := 0; dx := 0; minY := 0; maxY := 0;
  469.         ParcBefore(F.text, t.org, P, pbeg);
  470.         lsp := SHORT(P.lsp DIV Unit); dsr := SHORT(P.dsr DIV Unit); width := SHORT(P.width DIV Unit);
  471.         t.indent := 0;
  472.         IF t.org > 0 THEN Texts.OpenReader(R1, F.text, t.org - 1); Texts.Read(R1, peekCh);
  473.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN t.indent := P.first END;
  474.         END;
  475.         INC(tw, SHORT(t.indent DIV Unit));
  476.         LOOP
  477.             IF R.eot OR (nextCh = CR) THEN nob := 0; eol := ~R.eot; EXIT END;
  478.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  479.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  480.             END;
  481.             IF tw + x + w > width THEN d := len - bklen;
  482.                 IF (d < AdjustSpan) & (nextCh > " ") THEN eol := TRUE;
  483.                     Texts.OpenReader(R, F.text, Texts.Pos(R) - d);
  484.                     nob := bknob; len := bklen; tw := bktw; minY := bkminY; maxY := bkmaxY
  485.                 ELSIF len = 0 THEN    (* force at least one character on each line *)
  486.                     INC(len); INC(y, R.fnt.height * R.voff DIV 64); minY := Min(minY, y); maxY := Max(maxY, y + h);
  487.                     Texts.Read(R, nextCh); eol := FALSE; tw := maxW
  488.                 ELSE eol := (nextCh <= " ") & (nextCh # Texts.ElemChar)
  489.                 END;
  490.                 EXIT
  491.             END;
  492.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN
  493.                 bknob := nob; bklen := len; bktw := tw; bkminY := minY; bkmaxY := maxY;
  494.                 IF nextCh = " " THEN INC(nob) END
  495.             END;
  496.             INC(len); INC(tw, dx); INC(y, R.fnt.height * R.voff DIV 64);
  497.             IF y < minY THEN minY := y END;
  498.             IF y + h > maxY THEN maxY := y + h END;
  499.             Texts.Read(R, nextCh)
  500.         END;
  501.         IF ~F.showsParcs & (pbeg = t.org) THEN dsr := 0; t.h := SHORT(P.lead DIV Unit) + 1
  502.         ELSIF gridAdj IN P.opts THEN
  503.             WHILE dsr < -minY DO INC(dsr, lsp) END;
  504.             t.h := Max(lsp, dsr + maxY); INC(t.h, (-t.h) MOD lsp)
  505.         ELSE dsr := Max(dsr, -minY); t.h := Max(lsp, dsr + maxY)
  506.         END;
  507.         t.len := len; t.w0 := tw; t.w := Min(tw, maxW); t.dsr := dsr; t.nob := nob; t.eot := R.eot; t.P := P; t.pbeg := pbeg;
  508.         IF eol THEN Texts.Read(R, nextCh); t.span := len + 1 ELSE t.span := len END
  509.     END MeasureLine;
  510.     PROCEDURE MeasureLines (F: Frame; org: LONGINT; VAR trailer: TextLine);
  511.         VAR s, t: TextLine; ph: INTEGER;
  512.     BEGIN
  513.         NEW(trailer); s := trailer;
  514.         Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); ph := F.H - F.top;
  515.         LOOP NEW(t); t.org := org; MeasureLine(F, F.W - F.left - F.right, t);
  516.             IF ph - t.h < F.bot THEN EXIT END;
  517.             s.next := t; s := t; INC(org, s.span); DEC(ph, s.h);
  518.             IF R.eot THEN EXIT END
  519.         END;
  520.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; (* start of first invisible line *) trailer.len := 0; trailer.w := 0;
  521.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P (* P set by MeasureLine *) ; trailer.pbeg := pbeg
  522.     END MeasureLines;
  523.     (** Locators **)
  524.     PROCEDURE LocateLineTop (F: Frame; trailer: TextLine; org: LONGINT; VAR loc: Location);
  525.         VAR t: TextLine; ph: INTEGER;
  526.     BEGIN
  527.         ph := F.H - F.top; t := trailer.next;
  528.         WHILE (t # trailer) & (t.org # org) DO DEC(ph, t.h); t := t.next END;
  529.         loc.org := org; loc.line := t; loc.y := F.Y + ph
  530.     END LocateLineTop;
  531.     PROCEDURE Width (F: Frame; t: TextLine; pos: LONGINT; VAR pw, dx, dy: INTEGER);
  532.         VAR pat: Display.Pattern; i: LONGINT; n, mw, lm, tw, ddx, cn, x, y, w, h: INTEGER;
  533.     BEGIN
  534.         AdjustMetrics(F, t, pw, tw, ddx, cn); dy := 0; lm := F.left + SHORT(P.left DIV Unit);
  535.         IF t # F.trailer THEN Texts.OpenReader(R, F.text, t.org); Texts.Read(R, nextCh);
  536.             i := 0; n := 0; DEC(pos, t.org); dx := 0; mw := F.W - F.right;
  537.             WHILE ~R.eot & (i < t.len) & (i <= pos) & (pw + dx <= mw) DO
  538.                 (* i ... pos of nextCh; dx ... width of char before nextCh; pw ... line width up to pos (or up to right margin) *)
  539.                 INC(i); INC(pw, dx);
  540.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, x, y, w, h)
  541.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  542.                 END;
  543.                 dy := R.fnt.height * R.voff DIV 64;
  544.                 Texts.Read(R, nextCh)
  545.             END;
  546.             IF (i <= pos) & (pw + dx <= mw) THEN INC(i); INC(pw, dx) END
  547.         ELSE dx := 4
  548.         END
  549.     END Width;
  550.     PROCEDURE LocatePos* (F: Frame; pos: LONGINT; VAR loc: Location);    (* loc.dx = dx of char at pos *)
  551.         VAR t: TextLine; pw, dx, dy: INTEGER;
  552.     BEGIN
  553.         IF pos < F.org THEN pos := F.org; t := F.trailer.next
  554.         ELSIF pos < F.trailer.org THEN t := F.trailer;
  555.             WHILE (t.next # F.trailer) & (t.next.org <= pos) DO t := t.next END
  556.         ELSE pos := F.trailer.org; t := F.trailer.next;
  557.             WHILE ~t.eot DO t := t.next END
  558.         END;
  559.         Width(F, t, pos, pw, dx, dy); LocateLineTop(F, F.trailer, t.org, loc); DEC(loc.y, loc.line.h);
  560.         loc.org := t.org; loc.pos := pos; loc.x := F.X + pw; loc.dx := dx; loc.dy := dy; loc.line := t
  561.     END LocatePos;
  562.     PROCEDURE LocateLine* (F: Frame; y: INTEGER; VAR loc: Location);
  563.         (* loc.x = line start; loc.y = line bottom; loc.dx = line width *)
  564.         VAR t: TextLine; h, ph, pw, tw, ddx, cn: INTEGER;
  565.     BEGIN
  566.         t := F.trailer.next; h := y - F.Y; ph := F.H - F.top - t.h;
  567.         WHILE ~t.eot & (ph - t.next.h >= F.bot) & (ph > h) DO t := t.next; DEC(ph, t.h) END;
  568.         AdjustMetrics(F, t, pw, tw, ddx, cn);
  569.         IF pw >= F.W - F.right THEN pw := F.W - F.right - 4 END;
  570.         loc.org := t.org; loc.pos := loc.org; loc.x := F.X + pw; loc.y := F.Y + ph; loc.dx := tw; loc.dy := 0; loc.line := t
  571.     END LocateLine;
  572.     PROCEDURE LocateChar* (F: Frame; x, y: INTEGER; VAR loc: Location);
  573.         VAR t: TextLine; pat: Display.Pattern; i: LONGINT; n, w, lm, pw, tw, ddx, cn, dx, xc, yc, wc, hc: INTEGER;
  574.     BEGIN
  575.         LocateLine(F, y, loc); t := loc.line; w := x - F.X; AdjustMetrics(F, t, pw, tw, ddx, cn);
  576.         lm := F.left + SHORT(P.left DIV Unit);
  577.         IF (t # F.trailer) & (w > pw) THEN Texts.OpenReader(R, F.text, t.org);
  578.             i := 0; n := 0; dx := 0; nextCh := 0X;
  579.             WHILE (i < t.len) & (pw + dx < w) DO
  580.                 (* i = pos after nextCh; dx = width of nextCh; pw = line width without nextCh *)
  581.                 Texts.Read(R, nextCh); INC(i); INC(pw, dx);
  582.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, xc, yc, wc, hc)
  583.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  584.                 END
  585.             END;
  586.             IF pw + dx < w THEN INC(i); INC(pw, dx); R.elem := NIL END;
  587.             INC(loc.pos, i - 1); loc.x := F.X + pw;
  588.             IF i < t.len THEN loc.dx := dx; loc.dy := R.fnt.height * R.voff DIV 64 ELSE loc.dx := 4 END
  589.         ELSE loc.dx := 4; R.elem := NIL
  590.         END
  591.     END LocateChar;
  592.     PROCEDURE LocateWord* (F: Frame; x, y: INTEGER; VAR loc: Location);
  593.         VAR t: TextLine; pos, i: LONGINT; px, rx: INTEGER; pat: Display.Pattern; dx, xc, yc, wc, hc: INTEGER;
  594.     BEGIN
  595.         LocateChar(F, x, y, loc); pos := loc.pos + 1;
  596.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  597.         UNTIL (pos < loc.org) OR (nextCh > " ");
  598.         INC(pos);
  599.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  600.         UNTIL (pos < loc.org) OR (nextCh <= " ");
  601.         LocatePos(F, pos + 1, loc); t := loc.line; i := loc.pos - loc.org;
  602.         IF i < t.len THEN px := loc.x; rx := F.X + F.W - F.right;
  603.             Texts.OpenReader(R, F.text, loc.pos); dx := 0; wc := 0; nextCh := "x";
  604.             WHILE (i < t.len) & (nextCh > " ") & (px + dx < rx) DO
  605.                 Texts.Read(R, nextCh); INC(i); INC(px, dx);
  606.                 Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  607.             END;
  608.             IF (nextCh > " ") & (px + dx < rx) THEN INC(i); INC(px, dx) END;
  609.             loc.dx := px - loc.x
  610.         ELSE loc.dx := 0
  611.         END
  612.     END LocateWord;
  613.     PROCEDURE Pos* (F: Frame; x, y: INTEGER): LONGINT;
  614.         VAR loc: Location;
  615.     BEGIN LocateChar(F, x, y, loc); RETURN loc.pos
  616.     END Pos;
  617.     PROCEDURE ThisSubFrame (F: Frame; x, y: INTEGER): Display.Frame;
  618.         VAR f: Display.Frame;
  619.     BEGIN f := F.dsc;
  620.         WHILE (f # NIL) & ((x < f.X) OR (x >= f.X + f.W) OR (y < f.Y) OR (y >= f.Y + f.H)) DO f := f.next END;
  621.         RETURN f
  622.     END ThisSubFrame;
  623.     (** Caret & Selection **)
  624.     PROCEDURE PassSubFocus (F: Frame; f: Display.Frame);
  625.         (* pass focus from F.focus to f (f is also an element frame in F) *)
  626.         VAR loc: Location; f1: Display.Frame; ctrl: Oberon.ControlMsg; focus: FocusMsg;
  627.     BEGIN
  628.         IF F.focus # NIL THEN f1 := F.focus;
  629.             ctrl.id := Oberon.defocus; f1.handle(f1, ctrl);
  630.             LocateChar(F, f1.X + 1, f1.Y + 1, loc);
  631.             InvertBorder(f1); F.focus := NIL;
  632.             IF R.elem # NIL THEN
  633.                 focus.focus := FALSE; focus.elemFrame := f1; focus.frame := F; R.elem.handle(R.elem, focus)
  634.             END
  635.         END;
  636.         IF f # NIL THEN
  637.             LocateChar(F, f.X + 1, f.Y + 1, loc);    (* side effect: set R to element *)
  638.             focus.focus := TRUE; focus.elemFrame := f; focus.frame := F; R.elem.handle(R.elem, focus);
  639.             InvertBorder(f)
  640.         END;
  641.         F.focus := f
  642.     END PassSubFocus;
  643.     PROCEDURE RemoveSelection* (F: Frame);
  644.     BEGIN
  645.         IF F.hasSel THEN InvertSelection(F, F.selbeg, F.selend); F.hasSel := FALSE END
  646.     END RemoveSelection;
  647.     PROCEDURE SetSelection* (F: Frame; beg, end: LONGINT);    (** forces range to visible bounds **)
  648.         VAR loc: Location;
  649.     BEGIN
  650.         IF end > F.text.len THEN end := F.text.len END;
  651.         IF end > beg THEN
  652.             IF F.hasSel & (F.selbeg.pos = beg) THEN
  653.                 IF (F.selend.pos < end) & (F.selend.pos < F.trailer.org) THEN
  654.                     LocatePos(F, F.selend.pos, loc); LocatePos(F, end, F.selend); InvertSelection(F, loc, F.selend)
  655.                 ELSIF end < F.selend.pos THEN
  656.                     LocatePos(F, end, loc); InvertSelection(F, loc, F.selend); LocatePos(F, end, F.selend)
  657.                 END
  658.             ELSE RemoveSelection(F); PassSubFocus(F, NIL);
  659.                 LocatePos(F, beg, F.selbeg); LocatePos(F, end, F.selend); InvertSelection(F, F.selbeg, F.selend)
  660.             END;
  661.             F.hasSel := TRUE; F.time := Oberon.Time()
  662.         END
  663.     END SetSelection;
  664.     PROCEDURE RemoveCaret* (F: Frame);
  665.         VAR msg: Oberon.ControlMsg;
  666.     BEGIN
  667.         IF F.focus # NIL THEN msg.id := Oberon.defocus; F.focus.handle(F.focus, msg) END;
  668.         IF F.hasCar THEN InvertCaret(F); F.hasCar := FALSE END
  669.     END RemoveCaret;
  670.     PROCEDURE SetCaret* (F: Frame; pos: LONGINT);    (** only done if within visible bounds **)
  671.     BEGIN
  672.         IF ~F.hasCar OR (F.carloc.pos # pos) THEN RemoveCaret(F); PassSubFocus(F, NIL);
  673.             LocatePos(F, pos, F.carloc);
  674.             IF F.carloc.x <= F.X + F.W - F.right THEN InvertCaret(F); F.hasCar := TRUE END
  675.         END
  676.     END SetCaret;
  677.     (** Display Range **)
  678.     PROCEDURE Complete (F: Frame; trailer: TextLine; s: TextLine; org: LONGINT; ph: INTEGER);
  679.         VAR u: TextLine;
  680.     BEGIN
  681.         IF ph > F.bot THEN    (*try to add new lines to the bottom*)
  682.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);
  683.             LOOP
  684.                 IF R.eot THEN EXIT END;
  685.                 NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  686.                 IF ph - u.h < F.bot THEN EXIT END;
  687.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span)
  688.             END
  689.         END;
  690.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; trailer.len := 0; trailer.w := 0;
  691.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P; trailer.pbeg := pbeg
  692.     END Complete;
  693.     PROCEDURE ShowFrom (F: Frame; pos: LONGINT);    (* removes global marks as needed and neutralizes F *)
  694.         VAR new, s: TextLine; beg, end: Location; org: LONGINT; ph, y0, dy: INTEGER;
  695.     BEGIN
  696.         F.handle(F, neutralize);
  697.         IF (F.trailer # NIL) & (F.org < pos) & (pos < F.trailer.org) THEN    (* shift up and extend to the bottom *)
  698.             LocateLineTop(F, F.trailer, pos, beg); LocateLineTop(F, F.trailer, F.trailer.org, end);
  699.             dy := (F.Y + F.H - F.top) - beg.y; Shift(F, end.y, end.y + dy, beg.y - end.y);
  700.             Erase(F, F.X + F.left, end.y, F.W - F.left, dy);
  701.             s := F.trailer.next; WHILE s.org # pos DO s := s.next END;
  702.             F.trailer.next := s; org := s.org + s.span; ph := F.H - F.top - s.h;
  703.             WHILE s.next # F.trailer DO s := s.next; org := org + s.span; ph := ph - s.h END;
  704.             Complete(F, F.trailer, s, org, ph); F.org := pos; ShowLines(F, F.bot, end.y + dy - F.Y, FALSE)
  705.         ELSIF (F.trailer = NIL) OR (pos # F.org) THEN
  706.             MeasureLines(F, pos, new);
  707.             IF (F.trailer # NIL) & (pos < F.org) & (F.org <= new.org) THEN    (* shift down and extend to the top *)
  708.                 LocateLineTop(F, new, F.org, beg); LocateLineTop(F, new, new.org, end);
  709.                 y0 := F.Y + F.H - F.top; Shift(F, y0 - (beg.y - end.y), end.y, beg.y - end.y);
  710.                 Erase(F, F.X + F.left, beg.y, F.W - F.left, y0 - beg.y);
  711.                 Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, end.y - (F.Y + F.bot));
  712.                 F.org := pos; F.trailer := new; ShowLines(F, beg.y - F.Y, F.H - F.top, FALSE)
  713.             ELSE    (* full redisplay *)
  714.                 IF F.trailer = NIL THEN Erase(F, F.X, F.Y, F.W, F.H); ShowBar(F, 0, F.H); F.markH := -1
  715.                 ELSE Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, F.H - F.bot - F.top)
  716.                 END;
  717.                 F.org := pos; F.trailer := new; ShowLines(F, F.bot, F.H - F.top, FALSE)
  718.             END
  719.         END;
  720.         ShowTick(F)
  721.     END ShowFrom;
  722.     PROCEDURE Show* (F: Frame; pos: LONGINT);    (** removes global marks as needed and neutralizes F **)
  723.     BEGIN BegOfLine(F.text, pos, TRUE); ShowFrom(F, pos)
  724.     END Show;
  725.     PROCEDURE Resize (F: Frame; x, y, w, h: INTEGER);
  726.         VAR oldY, oldH, dh, ph: INTEGER; t: TextLine;
  727.     BEGIN
  728.         IF (w = 0) OR (h = 0) THEN InvalSubFrames(F, F.X, F.Y, F.W, F.H);
  729.             F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL
  730.         ELSIF (F.trailer # NIL) & (x = F.X) & (w = F.W) THEN
  731.             oldY := F.Y; oldH := F.H; Tick(F); F.markH := -1; F.Y := y; F.H := h;
  732.             IF h > oldH THEN dh := h - oldH;    (* extend *)
  733.                 IF y + h # oldY + oldH THEN
  734.                     Display.CopyBlock(x, oldY, w, oldH, x, y + dh, Display.replace);
  735.                     ShiftSubFrames(F, oldY, y + dh, oldH)
  736.                 END;
  737.                 EraseRect(F, x, y, w, dh); ShowBar(F, 0, dh);
  738.                 t := F.trailer; ph := F.H - F.top;
  739.                 WHILE t.next # F.trailer DO t := t.next; ph := ph - t.h END;
  740.                 Complete(F, F.trailer, t, F.trailer.org, ph); ShowLines(F, F.bot, ph, FALSE)
  741.             ELSE dh := oldH - h;    (* reduce *)
  742.                 IF y + h # oldY + oldH THEN
  743.                     Display.CopyBlock(x, oldY + dh, w, h, x, y, Display.replace);
  744.                     ShiftSubFrames(F, oldY + dh, y, h)
  745.                 END;
  746.                 t := F.trailer; ph := F.H - F.top;
  747.                 WHILE (t.next # F.trailer) & (ph - t.next.h >= F.bot) DO t := t.next; DEC(ph, t.h) END;
  748.                 IF t = F.trailer THEN t.org := F.org; t.span := 0 END;
  749.                 Complete(F, F.trailer, t, t.org + t.span, ph);
  750.                 EraseRect(F, x + F.left, y, w - F.left, ph);
  751.                 InvalSubFrames(F, x, oldY, w, y - oldY); InvalSubFrames(F, x, y + h, w, dh - (y - oldY))
  752.             END;
  753.             ShowTick(F)
  754.         ELSE F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL; Show(F, F.org)
  755.         END
  756.     END Resize;
  757.     (** Contents Update **)
  758.     PROCEDURE Update (F: Frame; VAR msg: UpdateMsg);    (** removes global marks as needed **)
  759.         VAR t: TextLine; org, d, Fbeg, Fend: LONGINT;
  760.             foc: Display.Frame; beg, end: LONGINT; ch: CHAR; r: Texts.Reader; loc: Location;
  761.         PROCEDURE Begin (VAR beg: LONGINT; VAR org0: LONGINT; VAR q: TextLine);
  762.             (* org0 = origin of first affected line; beg = pos of first modified character; q = first affected line (if line origin has not moved).*)
  763.             (* q = NIL => beg = org0; q # NIL => first (beg-org0) characters of q need not be redrawn *)
  764.             VAR trailer, t: TextLine;
  765.         BEGIN
  766.             trailer := F.trailer; t := trailer;
  767.             WHILE (t.next # trailer) & (beg >= t.next.org + t.next.span) & ~t.next.eot DO t := t.next END;
  768.             q := t.next;
  769.             IF (t # trailer) & (q # trailer) & (beg <= q.org + q.span) THEN
  770.                 Texts.OpenReader(R, F.text, t.org); Texts.Read(R, nextCh); org0 := t.org; NextLine(F.text, org0)
  771.             ELSE org0 := beg; BegOfLine(F.text, org0, TRUE)
  772.             END;
  773.             IF org0 # q.org THEN
  774.                 IF t = trailer THEN org0 := q.org ELSE org0 := t.org END;
  775.                 beg := org0; q := NIL
  776.             END
  777.         END Begin;
  778.         PROCEDURE Adjust (end, delta: LONGINT);
  779.             (* H1 = top of synchronization line in old frame *)
  780.             (* h0 = top of line that was modified *)
  781.             (* h1 = top of block in new frame that could be reused *)
  782.             (* h2 = bottom of last line in new frame *)
  783.             (* h1 - h2 = height of block that could be reused *)
  784.             VAR new, old, s, t, u, p, q: TextLine; bot: Location;
  785.                 org, org0, beg: LONGINT; ph, h0, h1, H1, h2, lm, dx, dy: INTEGER;
  786.         BEGIN
  787.             q := NIL; LocateLineTop(F, F.trailer, F.trailer.org, bot);
  788.             IF msg.beg < F.org THEN org0 := F.org; beg := org0 ELSE beg := msg.beg; Begin(beg, org0, q) END;
  789.             NEW(new); s := new; old := F.trailer; t := old; org := F.org; ph := F.H - F.top;
  790.             WHILE (t.next # old) & (t.next.org # org0) DO t := t.next;    (*transfer unchanged prefix*)
  791.                 s.next := t; s := t; DEC(ph, s.h); INC(org, s.span)
  792.             END;
  793.             h0 := ph; H1 := h0; t := t.next; p := s;
  794.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);    (*rebuild at least one line descriptor*)
  795.             LOOP NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  796.                 IF ph - u.h < F.bot THEN h1 := ph; h2 := h1; EXIT END;
  797.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span);
  798.                 IF R.eot THEN h1 := ph; h2 := h1; EXIT END;
  799.                 IF org > end THEN
  800.                     WHILE (t # old) & (org > t.org + delta) DO DEC(H1, t.h); t := t.next END;
  801.                     IF (org = t.org + delta) & (P = t.P) THEN h1 := ph;    (*resynchronized*)
  802.                         WHILE (t # old) & (ph - t.h >= F.bot) DO    (*transfer unchanged suffix*)
  803.                             s.next := t; s := t; s.org := org; ParcBefore(F.text, s.org, s.P, s.pbeg);
  804.                             DEC(ph, s.h); INC(org, s.span); t := t.next
  805.                         END;
  806.                         h2 := ph; EXIT
  807.                     END
  808.                 END
  809.             END;
  810.             Shift(F, F.Y + H1 - (h1 - h2), F.Y + h2, h1 - h2);
  811.             Complete(F, new, s, org, ph); F.trailer := new; t := p.next;
  812.             IF (q # NIL) & (t # F.trailer) & (q.h = t.h) & (q.dsr = t.dsr) & (q.org = t.org) & (q.P = t.P) & (end <= t.org + t.span) THEN
  813.                 P := t.P; pbeg := t.pbeg;
  814.                 IF (P.opts * AdjMask = {leftAdj}) OR (P.opts * AdjMask = AdjMask) & (q.nob = 0) & (t.nob = 0) THEN
  815.                     Width(F, t, beg, lm, dx, dy);    (*preserve prefix of first affected line*)
  816.                     DEC(h0, t.h); Erase(F, F.X + lm, F.Y + h0, F.W - lm, t.h);
  817.                     ShowLine(F, t, F.X + lm, F.X + F.W - F.right, F.Y + h0)
  818.                 END
  819.             END;
  820.             ShowLines(F, h1, h0, TRUE);
  821.             Erase(F, F.X + F.left, bot.y, F.W - F.left, h2 - (bot.y - F.Y)); ShowLines(F, F.bot, h2, FALSE)
  822.         END Adjust;
  823.     BEGIN
  824.         foc := F.focus; beg := msg.beg; end := msg.end; 
  825.         F.handle(F, neutralize); MarkMenu(F); Fbeg := F.org; Fend := F.trailer.org;
  826.         IF (msg.id = Texts.insert) & (msg.beg < F.org) THEN t := F.trailer; d := msg.end - msg.beg; INC(F.org, d);
  827.             REPEAT INC(t.org, d); t := t.next UNTIL t = F.trailer
  828.         ELSIF msg.id = Texts.delete THEN
  829.             IF msg.end <= F.org THEN t := F.trailer; d := msg.end - msg.beg; DEC(F.org, d);
  830.                 REPEAT DEC(t.org, d); t := t.next UNTIL t = F.trailer
  831.             ELSIF msg.beg < F.org THEN F.org := msg.beg
  832.             END
  833.         END;
  834.         org := F.org;
  835.         IF msg.beg <= Fbeg + AdjustSpan THEN BegOfLine(F.text, org, TRUE) END;
  836.         ParcBefore(F.text, org, P, d);
  837.         IF (org # F.org) OR (P # F.trailer.next.P) THEN
  838.             F.trailer := NIL; Show(F, F.org)
  839.         ELSIF (msg.end > Fbeg) & (msg.beg < Fend + AdjustSpan) THEN
  840.             IF msg.id = Texts.replace THEN Adjust(msg.end, 0);
  841.                 (* refocus element if necessary *)
  842.                 IF (foc # NIL) & (end-beg = 1) THEN
  843.                     Texts.OpenReader(r, F.text, beg); Texts.Read(r, ch);
  844.                     IF r.elem # NIL THEN
  845.                         LocatePos(F, beg, loc); foc := ThisSubFrame(F, loc.x, loc.y); PassSubFocus(F, foc);
  846.                     END
  847.                 END
  848.             ELSIF msg.id = Texts.insert THEN Adjust(msg.end, msg.end - msg.beg)
  849.             ELSIF msg.id = Texts.delete THEN Adjust(msg.beg, msg.beg - msg.end)
  850.             END
  851.         END;
  852.         ShowTick(F)
  853.     END Update;
  854.     (** User Interface **)
  855.     PROCEDURE Back (F: Frame; dY: INTEGER; (*inout*) VAR org: LONGINT);    (* mh 10.10.92 *)
  856.         (* computes new org such that old org is (at most) dY pixels below new org *)
  857.         VAR H: INTEGER; oldOrg: LONGINT;
  858.         PROCEDURE TotalHeight (org1, org2: LONGINT): INTEGER;
  859.             (* measures total height of text-lines starting at org1 and ending at the line before the line containing org2 *) 
  860.             VAR h: INTEGER; line: TextLine;
  861.         BEGIN
  862.             Texts.OpenReader(R, F.text, org1); Texts.Read(R, nextCh); NEW(line); h := 0;
  863.             LOOP line.org := org1;
  864.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org1, line.span);
  865.                 IF Texts.Pos(R)-1 > org2 THEN EXIT END;
  866.                 INC(h, line.h);
  867.                 IF R.eot THEN EXIT END;
  868.             END;
  869.             RETURN h
  870.         END TotalHeight;
  871.         PROCEDURE Forward (h: INTEGER);
  872.             (* increase org by n text-lines such that the sum of the n line-heights > h *)
  873.             VAR line: TextLine;
  874.         BEGIN
  875.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); NEW(line);
  876.             WHILE h > 0 DO line.org := org;
  877.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org, line.span); DEC(h, line.h);
  878.             END;
  879.             org := Texts.Pos(R)-1;
  880.         END Forward;
  881.     BEGIN H := 0;
  882.         LOOP oldOrg := org;
  883.             IF org = 0 THEN EXIT END;
  884.             DEC(org, 800); BegOfLine(F.text, org, FALSE);
  885.             INC(H, TotalHeight(org, oldOrg));
  886.             IF H > dY THEN EXIT END;
  887.         END;
  888.         Forward(H - dY);
  889.     END Back;
  890.     PROCEDURE TrackLine* (F: Frame; VAR x, y: INTEGER; VAR org: LONGINT; VAR keysum: SET);
  891.         VAR keys: SET; new, old: Location;
  892.     BEGIN
  893.         LocateLine(F, y, old); InvertRect(F, old.x, old.y, old.dx + 4, 2); keysum := {};
  894.         REPEAT TrackMouse(x, y, keys, keysum); LocateLine(F, y, new);
  895.             IF new.org # old.org THEN
  896.                 InvertRect(F, new.x, new.y, new.dx + 4, 2); InvertRect(F, old.x, old.y, old.dx + 4, 2); old := new
  897.             END
  898.         UNTIL keys = {};
  899.         InvertRect(F, new.x, new.y, new.dx + 4, 2); org := new.org
  900.     END TrackLine;
  901.     PROCEDURE TrackWord* (F: Frame; VAR x, y: INTEGER; VAR pos: LONGINT; VAR keysum: SET);
  902.         VAR keys: SET; new, old: Location;
  903.     BEGIN 
  904.         LocateWord(F, x, y, old); InvertRect(F, old.x, old.y, old.dx, 2); keysum := {};
  905.         REPEAT TrackMouse(x, y, keys, keysum); LocateWord(F, x, y, new);
  906.             IF new.pos # old.pos THEN
  907.                 InvertRect(F, new.x, new.y, new.dx, 2); InvertRect(F, old.x, old.y, old.dx, 2); old := new
  908.             END
  909.         UNTIL keys = {};
  910.         InvertRect(F, new.x, new.y, new.dx, 2); pos := new.pos
  911.     END TrackWord;
  912.     PROCEDURE TrackCaret* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  913.         VAR keys: SET;
  914.     BEGIN keysum := {};
  915.         REPEAT TrackMouse(x, y, keys, keysum); SetCaret(F, Pos(F, x, y)) UNTIL keys = {}
  916.     END TrackCaret;
  917.     PROCEDURE TrackSelection* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  918.         VAR keys: SET; pos: LONGINT; V: Viewers.Viewer; f: Frame;
  919.     BEGIN
  920.         V := Viewers.This(F.X, F.Y); V := V.next(Viewers.Viewer);
  921.         IF (V.dsc # NIL) & (V.dsc.next # NIL) & (V.dsc.next IS Frame) THEN f := V.dsc.next(Frame);
  922.             IF f.hasSel & (f.text = F.text) THEN
  923.                 IF (f.selbeg.pos < f.trailer.org) & (f.org < f.selend.pos) & (f.selbeg.pos <= Pos(F, x, y)) THEN
  924.                     SetSelection(F, f.selbeg.pos, Pos(F, x, y) + 1)
  925.                 ELSE RemoveSelection(f); f := NIL
  926.                 END
  927.             ELSE f := NIL
  928.             END
  929.         ELSE f := NIL
  930.         END;
  931.         IF f = NIL THEN
  932.             IF F.hasSel & (F.selbeg.pos + 1 = F.selend.pos) & (Pos(F, x, y) = F.selbeg.pos) THEN
  933.                 SetSelection(F, F.selbeg.org, Pos(F, x, y) + 1)
  934.             ELSE SetSelection(F, Pos(F, x, y), Pos(F, x, y) + 1)
  935.             END
  936.         END;
  937.         keysum := {};
  938.         REPEAT TrackMouse(x, y, keys, keysum);
  939.             IF F.hasSel THEN
  940.                 pos := Pos(F, x, Min(y, F.selbeg.y)) + 1;
  941.                 IF pos <= F.selbeg.pos THEN pos := F.selbeg.pos + 1 END;
  942.                 SetSelection(F, F.selbeg.pos, pos);
  943.                 IF f # NIL THEN SetSelection(f, f.selbeg.pos, pos); f.selend.pos := F.selend.pos END
  944.             ELSE SetSelection(F, Pos(F, x, y), Pos(F, x, y) + 1)
  945.             END
  946.         UNTIL keys = {};
  947.         IF f # NIL THEN F.selbeg.pos := f.selbeg.pos END
  948.     END TrackSelection;
  949.     PROCEDURE^ NewText* (T: Texts.Text; pos: LONGINT): Frame;
  950.     PROCEDURE^ NewMenu* (name, commands: ARRAY OF CHAR): Frame;
  951.     PROCEDURE^ Text* (name: ARRAY OF CHAR): Texts.Text;
  952.     PROCEDURE Call (F: Frame; pos: LONGINT; keysum: SET);
  953.         VAR S: Texts.Scanner; res, i, j, X, Y: INTEGER; v: Viewers.Viewer;
  954.     BEGIN
  955.         Texts.OpenScanner(S, F.text, pos); Texts.Scan(S);
  956.         IF (S.class = Texts.Name) & (S.line = 0) THEN
  957.             IF rightKey IN keysum THEN  (*open text viewer*)
  958.                 S.s := "Edit.Open"; par.pos := pos
  959.             ELSE par.pos := pos + S.len
  960.             END ;
  961.             par.vwr := Viewers.This(F.X, F.Y);
  962.             par.frame := F; par.text := F.text;
  963.             Oberon.Call(S.s, par, keysum = {middleKey, leftKey}, res);
  964.             IF res > 0 THEN
  965.                 Texts.WriteString(W0, "Call error: "); Texts.WriteString(W0, Modules.importing);
  966.                 IF res = 1 THEN Texts.WriteString(W0, " not found")
  967.                 ELSIF res = 2 THEN Texts.WriteString(W0, " not an obj-file")
  968.                 ELSIF res = 3 THEN Texts.WriteString(W0, " imports ");
  969.                     Texts.WriteString(W0, Modules.imported); Texts.WriteString(W0, " with bad key");
  970.                 ELSIF res = 4 THEN Texts.WriteString(W0, " corrupted obj file")
  971.                 ELSIF res = 6 THEN Texts.WriteString(W0, " has too many imports")
  972.                 ELSIF res = 7 THEN Texts.WriteString(W0, " not enough space")
  973.                 END
  974.             ELSIF res < 0 THEN
  975.                 INC(i); WHILE i < S.len DO Texts.Write(W0, S.s[i]); INC(i) END;
  976.                 Texts.WriteString(W0, " not found")
  977.             END;
  978.             IF res # 0 THEN Texts.WriteLn(W0); Texts.Append(Oberon.Log, W0.buf) END
  979.         END
  980.     END Call;
  981.     PROCEDURE PickAttributes (VAR W: Texts.Writer; T: Texts.Text; pos: LONGINT; font: Fonts.Font; col, voff: SHORTINT);
  982.         VAR R: Texts.Reader; ch: CHAR;
  983.     BEGIN
  984.         IF T.len > 0 THEN
  985.             IF pos < T.len THEN Texts.OpenReader(R, T, pos); Texts.Read(R, ch) END;
  986.             IF (pos > 0) & ((pos = T.len) OR (ch <= " ")) THEN
  987.                 Texts.OpenReader(R, T, pos - 1); Texts.Read(R, ch)
  988.             END;
  989.             Texts.SetFont(W, R.fnt); Texts.SetColor(W, R.col);
  990.             IF (ch = CR) OR (ch = TAB) OR (ch = LF) THEN Texts.SetOffset(W, voff) ELSE Texts.SetOffset(W, R.voff) END
  991.         ELSE Texts.SetFont(W, font); Texts.SetColor(W, col); Texts.SetOffset(W, voff)
  992.         END
  993.     END PickAttributes;
  994.     PROCEDURE ShiftBlock (F: Frame; delta: INTEGER);    (* shift selected lines to left or right *)
  995.         VAR text: Texts.Text; pos, beg, end, time: LONGINT; select: SelectMsg; ch: CHAR;
  996.     BEGIN
  997.         Oberon.GetSelection(text, beg, end, time);
  998.         IF (time >= 0) & (text = F.text) THEN BegOfLine(F.text, beg, FALSE); pos := beg;
  999.             WHILE pos < end DO Texts.OpenReader(R, F.text, pos); Texts.Read(R, ch);
  1000.                 WHILE (R.elem # NIL) & (R.elem IS Parc) & (pos < end) DO Texts.Read(R, ch); INC(pos) END;
  1001.                 IF pos < end THEN
  1002.                     IF delta < 0 THEN
  1003.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN
  1004.                             Texts.Delete(F.text, pos, pos + 1); DEC(end)
  1005.                         END
  1006.                     ELSE
  1007.                         PickAttributes(W, text, pos, Oberon.CurFnt, Oberon.CurCol, Oberon.CurOff);
  1008.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN Texts.Write(W, ch)    (* first char extension *)
  1009.                         ELSE Texts.Write(W, TAB)
  1010.                         END;
  1011.                         Texts.Insert(F.text, pos, W.buf); INC(end); INC(pos)
  1012.                     END;
  1013.                     Texts.OpenReader(R, F.text, pos);
  1014.                     REPEAT Texts.Read(R, ch) UNTIL R.eot OR (ch = CR);
  1015.                     pos := Texts.Pos(R)
  1016.                 END
  1017.             END;
  1018.             select.text := F.text; select.beg := beg; select.end := pos; select.time := Oberon.Time();
  1019.             Viewers.Broadcast(select)
  1020.         END
  1021.     END ShiftBlock;
  1022.     PROCEDURE Write (F: Frame; ch: CHAR; fnt: Fonts.Font; col, voff: SHORTINT);
  1023.         VAR loc: Location; parc: Parc; org, pos, pbeg: LONGINT; i: INTEGER; ch0: CHAR;
  1024.             buf: ARRAY 32 OF CHAR;
  1025.             copy: Texts.CopyMsg; input: Oberon.InputMsg;
  1026.         PROCEDURE Visible(ch: CHAR): BOOLEAN;
  1027.             VAR pat: Display.Pattern; dx, x, y, w, h: INTEGER;
  1028.         BEGIN Display.GetChar(W.fnt.raster, ch, dx, x, y, w, h, pat); RETURN dx > 0
  1029.         END Visible;
  1030.         PROCEDURE InsertBuffer;
  1031.             VAR i, j: INTEGER; ch: CHAR;
  1032.         BEGIN i := 0; j := 0; ch := buf[i];
  1033.             WHILE ch # 0X DO
  1034.                 IF (ch = TAB) OR (ch = CR) OR (ch = " ") OR Visible(ch) THEN Texts.Write(W, ch); INC(j) END;
  1035.                 INC(i); ch := buf[i]
  1036.             END; 
  1037.             IF j > 0 THEN Texts.Insert(F.text, pos, W.buf); INC(pos, LONG(j)) END
  1038.         END InsertBuffer;
  1039.         PROCEDURE Flush;
  1040.             VAR ch: CHAR;
  1041.         BEGIN
  1042.             WHILE Input.Available() > 0 DO Input.Read(ch) END
  1043.         END Flush;
  1044.     BEGIN
  1045.         IF F.hasSel & (ch = CRSL) THEN ShiftBlock(F, -1)
  1046.         ELSIF F.hasSel & (ch = CRSR) THEN ShiftBlock(F, 1)
  1047.         ELSIF F.hasCar THEN pos := F.carloc.pos;
  1048.             IF (ch = DEL) & (pos > F.org) THEN DEC(pos); Texts.Delete(F.text, pos, pos + 1); Flush
  1049.             ELSIF (ch = CRSL) & (pos > 0) THEN DEC(pos)
  1050.             ELSIF (ch = CRSR) & (pos < F.text.len) THEN INC(pos)
  1051.             ELSIF (ch = CRSU) & (pos > 0) THEN                                    (*<< mah cursor up *)
  1052.                 org:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h);
  1053.                 IF org=pos THEN Show (F, F.org-1) END;
  1054.                 pos:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h)
  1055.             ELSIF (ch = CRSD) & (pos < F.text.len) THEN                        (*<< mah cursor down *)
  1056.                 org:=Pos (F, F.carloc.x+1, F.carloc.y-F.carloc.line.next.h);
  1057.                 IF (org=pos) & (F.trailer.org+F.trailer.len#F.text.len) THEN Show (F, F.trailer.next.next.org) END;
  1058.                 LocatePos (F, pos, loc);
  1059.                 pos:=Pos (F, F.carloc.x+1, loc.y-loc.line.next.h)
  1060.             ELSIF (ch = BRK) OR (ch = ShiftBRK) THEN
  1061.                 ParcBefore(F.text, pos, P, pbeg); P.handle(P, copy); parc := copy.e(Parc);
  1062.                 IF ch = BRK THEN EXCL(parc.opts, pageBreak) ELSE INCL(parc.opts, pageBreak) END;
  1063.                 PickAttributes(W, F.text, pos, fnt, col, voff);
  1064.                 Texts.WriteElem(W, parc); Texts.Insert(F.text, pos, W.buf); INC(pos)
  1065.             ELSIF (ch = TAB) OR (ch = LF) OR (ch = CR) OR (ch >= " ") THEN
  1066.                 PickAttributes(W, F.text, pos, fnt, col, voff);
  1067.                 IF ch = LF THEN buf[0] := CR; i := 1; org := F.carloc.org; BegOfLine(F.text, org, FALSE);
  1068.                     Texts.OpenReader(R, F.text, org);
  1069.                     REPEAT Texts.Read(R, ch) UNTIL (R.elem = NIL) OR ~(R.elem IS Parc);
  1070.                     WHILE (Texts.Pos(R) <= pos) & (ch <= " ") & (ch # Texts.ElemChar) & (i < 31) DO
  1071.                         buf[i] := ch; INC(i); Texts.Read(R, ch)
  1072.                     END
  1073.                 ELSE buf[0] := ch; i := 1
  1074.                 END;
  1075.                 WHILE (Input.Available() > 0) & (i < 31) & (ch >= " ") & (ch < DEL) DO Input.Read(buf[i]); INC(i) END;
  1076.                 buf[i] := 0X; InsertBuffer
  1077.             END;
  1078.             IF pos < F.org THEN Show(F, F.org - 1)
  1079.             ELSIF pos < F.text.len THEN org := -1;
  1080.                 WHILE (pos >= F.trailer.org) & (pos > F.org) DO
  1081.                     org := F.trailer.next.next.org; IF org = F.org THEN INC(org) END;
  1082.                     ShowFrom(F, org); Flush
  1083.                 END
  1084.             ELSE LocatePos(F, pos, loc); LocateChar(F, loc.x + 1, loc.y, loc);
  1085.                 IF pos # loc.pos THEN Show(F, F.trailer.next.next.org); Flush END
  1086.             END;
  1087.             SetCaret(F, pos)
  1088.         ELSIF F.focus # NIL THEN input.id := Oberon.consume; input.ch := ch;
  1089.             input.fnt := fnt; input.col := col; input.voff := voff; F.focus.handle(F.focus, input)
  1090.         END
  1091.     END Write;
  1092.     PROCEDURE TouchElem (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  1093.         VAR loc: Location; e: Texts.Elem; pbeg: LONGINT; y0: INTEGER;
  1094.             track: TrackMsg;
  1095.     BEGIN
  1096.         LocateChar(F, x, y, loc); e := R.elem;
  1097.         IF (e # NIL) & (loc.x + e.W DIV Unit <= F.X + F.W - F.right) THEN
  1098.             ParcBefore(F.text, loc.pos, P, pbeg); y0 := loc.y + loc.line.dsr - SHORT(P.dsr DIV Unit) + loc.dy;
  1099.             IF (loc.x <= x) & (x < loc.x + e.W DIV Unit) & (keysum= {middleKey}) THEN
  1100.                 track.X := x; track.Y := y; track.keys := keysum;
  1101.                 track.fnt := R.fnt; track.col := R.col; track.pos := Texts.Pos(R) - 1;
  1102.                 track.frame := F; track.X0 := loc.x; track.Y0 := y0;
  1103.                 e.handle(e, track); keysum := {}
  1104.             END
  1105.         END
  1106.     END TouchElem;
  1107.     PROCEDURE Edit (F: Frame; x, y: INTEGER; keysum: SET);
  1108.         VAR ef: Display.Frame; text: Texts.Text; beg, end, time, pos, oldpos: LONGINT; keys: SET; ch: CHAR;
  1109.             loc: Location; delta: INTEGER; copyover: Oberon.CopyOverMsg; input: Oberon.InputMsg;
  1110.     BEGIN
  1111.         IF x < F.X + F.barW THEN pos := F.org;    (* scroll bar *)
  1112.             IF leftKey IN keysum THEN TrackLine(F, x, y, pos, keysum)
  1113.             ELSIF rightKey IN keysum THEN TrackLine(F, x, y, pos, keysum); LocateLine(F, y, loc);
  1114.                 pos := F.org; delta := loc.y - (F.Y + F.bot); Back(F, delta, pos)
  1115.             ELSIF middleKey IN keysum THEN
  1116.                 (* heavy dragging *)
  1117.                 IF (F.Y + F.markH - 1 <= y) & (y < F.Y + F.markH + 4) THEN
  1118.                     oldpos := F.org;
  1119.                     REPEAT
  1120.                         TrackMouse(x, y, keys, keysum);
  1121.                         IF (F.Y <= y) & (y <= F.Y + F.H) THEN Show(F, CoordToPos(F, y - F.Y)); pos := F.org END
  1122.                     UNTIL keys # {middleKey};
  1123.                     REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1124.                     IF keysum = cancel THEN ShowFrom(F, oldpos) END
  1125.                 END;
  1126.                 REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1127.                 IF keysum = {middleKey, leftKey} THEN pos := F.text.len; (*BegOfLine(F.text, pos, TRUE);*)
  1128.                     Back(F, F.H - F.bot - F.top - 30 (*heuristic*), pos);
  1129.                 ELSIF keysum = {middleKey, rightKey} THEN pos := 0
  1130.                 ELSIF (F.Y <= y) & (y <= F.Y + F.H) THEN pos := CoordToPos(F, y - F.Y); BegOfLine(F.text, pos, TRUE)
  1131.                 END
  1132.             ELSE DrawCursor(x, y); keysum := cancel
  1133.             END;
  1134.             IF keysum # cancel THEN ShowFrom(F, pos) END
  1135.         ELSE    (* text area *)
  1136.             ef := ThisSubFrame(F, x, y);
  1137.             IF ef # NIL THEN    (* within sub-frame *)
  1138.                 IF (F.focus # ef) & (keysum = {leftKey}) THEN
  1139.                     REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1140.                     IF keysum = {leftKey} THEN RemoveSelection(F); RemoveCaret(F); PassSubFocus(F, ef); RETURN END
  1141.                 ELSIF F.focus = ef THEN input.id := Oberon.track; input.keys := keysum; input.X := x; input.Y := y;
  1142.                     ef.handle(ef, input); RETURN
  1143.                 END
  1144.             END;
  1145.             IF keysum # {} THEN
  1146.                 TouchElem(F, x, y, keysum);
  1147.                 IF keysum = {} THEN RETURN END
  1148.             END;
  1149.             IF leftKey IN keysum THEN Oberon.PassFocus(Viewers.This(F.X, F.Y)); TrackCaret(F, x, y, keysum);
  1150.                 IF (keysum = {leftKey, middleKey}) & F.hasCar THEN Oberon.GetSelection(text, beg, end, time);
  1151.                     IF time >= 0 THEN Texts.Save(text, beg, end, B);
  1152.                         Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (end - beg))
  1153.                     END
  1154.                 ELSIF (keysum = {leftKey, rightKey}) & F.hasCar & (F.carloc.pos < F.text.len) THEN
  1155.                     Oberon.GetSelection(text, beg, end, time);
  1156.                     IF time >= 0 THEN Texts.OpenReader(R, F.text, F.carloc.pos); Texts.Read(R, ch);
  1157.                         Texts.ChangeLooks(text, beg, end, {0, 1, 2}, R.fnt, R.col, R.voff)
  1158.                     END
  1159.                 END
  1160.             ELSIF middleKey IN keysum THEN TrackWord(F, x, y, pos, keysum);
  1161.                 IF keysum # cancel THEN Call(F, pos, keysum) END
  1162.             ELSIF rightKey IN keysum THEN TrackSelection(F, x, y, keysum);
  1163.                 IF (keysum = {rightKey, middleKey}) & F.hasSel THEN
  1164.                     copyover.text := F.text; copyover.beg := F.selbeg.pos; copyover.end := F.selend.pos;
  1165.                     Oberon.FocusViewer.handle(Oberon.FocusViewer, copyover)
  1166.                 ELSIF (keysum = {rightKey, leftKey}) & F.hasSel THEN Oberon.PassFocus(Viewers.This(F.X, F.Y));
  1167.                     Texts.Delete(F.text, F.selbeg.pos, F.selend.pos); SetCaret(F, F.selbeg.pos)
  1168.                 END
  1169.             ELSE DrawCursor(x, y)
  1170.             END
  1171.         END
  1172.     END Edit;
  1173.     (** General **)
  1174.     PROCEDURE Copy (SF, DF: Frame);
  1175.     BEGIN
  1176.         DF.handle := SF.handle; DF.text := SF.text; DF.org := SF.org;
  1177.         DF.col := SF.col; DF.left := SF.left; DF.right := SF.right; DF.top := SF.top; DF.bot := SF.bot;
  1178.         DF.barW := SF.barW; DF.hasCar := FALSE; DF.hasSel := FALSE; DF.showsParcs := SF.showsParcs;
  1179.         DF.focus := NIL; DF.trailer := NIL
  1180.     END Copy;
  1181.     PROCEDURE Handle* (f: Display.Frame; VAR msg: Display.FrameMsg);
  1182.         VAR F, F1: Frame; pos: LONGINT;
  1183.     BEGIN F := f(Frame);
  1184.         IF msg IS Oberon.InputMsg THEN
  1185.             WITH msg: Oberon.InputMsg DO
  1186.                 IF msg.id = Oberon.consume THEN Write(F, msg.ch, msg.fnt, msg.col, msg.voff)
  1187.                 ELSIF msg.id = Oberon.track THEN Edit(F, msg.X, msg.Y, msg.keys)
  1188.                 END
  1189.             END
  1190.         ELSIF msg IS Oberon.ControlMsg THEN
  1191.             WITH msg: Oberon.ControlMsg DO
  1192.                 IF msg.id = Oberon.defocus THEN RemoveCaret(F)
  1193.                 ELSIF msg.id = Oberon.neutralize THEN
  1194.                     RemoveCaret(F); RemoveSelection(F); PassSubFocus(F, NIL); NotifySubFrames(F, msg)
  1195.                 ELSE NotifySubFrames(F, msg)
  1196.                 END
  1197.             END
  1198.         ELSIF msg IS Oberon.CopyMsg THEN
  1199.             WITH msg: Oberon.CopyMsg DO
  1200.                 IF msg.F = NIL THEN NEW(F1); msg.F := F1 END;
  1201.                 Copy(F, msg.F(Frame))
  1202.             END
  1203.         ELSIF msg IS UpdateMsg THEN NotifySubFrames(F, msg);
  1204.             WITH msg: UpdateMsg DO
  1205.                 IF msg.text = F.text THEN Update(F, msg) END
  1206.             END
  1207.         ELSIF msg IS InsertElemMsg THEN
  1208.             IF F.hasCar THEN pos := F.carloc.pos;
  1209.                 PickAttributes(W, F.text, pos, Oberon.CurFnt, Oberon.CurCol, Oberon.CurOff);
  1210.                 Texts.WriteElem(W, msg(InsertElemMsg).e);
  1211.                 Texts.Insert(F.text, pos, W.buf);
  1212.                 SetCaret(F, pos + 1)
  1213.             END
  1214.         ELSIF msg IS Oberon.SelectionMsg THEN NotifySubFrames(F, msg);
  1215.             WITH msg: Oberon.SelectionMsg DO
  1216.                 IF F.hasSel & (F.time > msg.time) THEN
  1217.                     msg.text := F.text; msg.beg := F.selbeg.pos; msg.end := F.selend.pos; msg.time := F.time
  1218.                 END
  1219.             END
  1220.         ELSIF msg IS Oberon.CopyOverMsg THEN NotifySubFrames(F, msg);
  1221.             WITH msg: Oberon.CopyOverMsg DO
  1222.                 IF F.hasCar THEN Texts.Save(msg.text, msg.beg, msg.end, B);
  1223.                     Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (msg.end - msg.beg))
  1224.                 END
  1225.             END
  1226.         ELSIF msg IS MenuViewers.ModifyMsg THEN
  1227.             WITH msg: MenuViewers.ModifyMsg DO
  1228.                 F.handle(F, neutralize); Resize(F, F.X, msg.Y, F.W, msg.H)
  1229.             END
  1230.         ELSIF msg IS SelectMsg THEN NotifySubFrames(F, msg);
  1231.             WITH msg: SelectMsg DO
  1232.                 IF (msg.text = F.text) & ~F.hasSel THEN Oberon.RemoveMarks(F.X, F.Y, F.W, F.H);
  1233.                     F.handle(F, neutralize);
  1234.                     SetSelection(F, msg.beg, msg.end); F.time := msg.time;
  1235.                     IF F.hasSel THEN F.selbeg.pos := msg.beg; F.selend.pos := msg.end END
  1236.                 END
  1237.             END
  1238.         ELSE NotifySubFrames(F, msg)
  1239.         END
  1240.     END Handle;
  1241.     PROCEDURE Open* (F: Frame; T: Texts.Text; pos: LONGINT);
  1242.     BEGIN
  1243.         F.handle := Handle; F.text := T; F.org := pos; F.col := Display.black;
  1244.         F.left := left; F.right := right; F.top := top; F.bot := bot;
  1245.         F.barW := barW; F.hasCar := FALSE; F.hasSel := FALSE; F.showsParcs := FALSE; F.trailer := NIL
  1246.     END Open;
  1247.     PROCEDURE NotifyDisplay* (T: Texts.Text; op: INTEGER; beg, end: LONGINT);
  1248.         VAR msg: UpdateMsg;
  1249.     BEGIN
  1250.         msg.text := T; msg.id := op; msg.beg := beg; msg.end := end; Viewers.Broadcast(msg)
  1251.     END NotifyDisplay;
  1252.     PROCEDURE Text* (name: ARRAY OF CHAR): Texts.Text;
  1253.         VAR text: Texts.Text;
  1254.     BEGIN
  1255.         NEW(text); Texts.Open(text, name); text.notify := NotifyDisplay; RETURN text
  1256.     END Text;
  1257.     PROCEDURE NewText* (T: Texts.Text; pos: LONGINT): Frame;
  1258.         VAR frame: Frame;
  1259.     BEGIN
  1260.         NEW(frame); Open(frame, T, pos);
  1261.         RETURN frame
  1262.     END NewText;
  1263.     PROCEDURE NewMenu* (name, commands: ARRAY OF CHAR): Frame;
  1264.         VAR T, T1: Texts.Text;
  1265.             buf: Texts.Buffer;
  1266.             frame: Frame;
  1267.             fn: ARRAY 32 OF CHAR;
  1268.             i: INTEGER;
  1269.     BEGIN i := 0; T := Text("");
  1270.         Texts.WriteString(W0, name); Texts.WriteString(W0, " | "); Texts.Append(T, W0.buf);
  1271.         IF commands[0] = "^" THEN
  1272.             WHILE commands[i+1] > " " DO fn[i] := commands[i+1]; INC(i) END ;
  1273.             fn[i] := 0X;
  1274.             IF Files.Old(fn) = NIL THEN
  1275.                 IF commands[i+1] = " " THEN INC(i, 2);
  1276.                     WHILE commands[i] > 0X DO Texts.Write(W0, commands[i]); INC(i) END ;
  1277.                     Texts.Append(T, W0.buf)
  1278.                 END
  1279.             ELSE NEW(T1); Texts.Open(T1, fn);
  1280.                 NEW(buf); Texts.OpenBuf(buf); Texts.Save(T1, 0, T1.len, buf); Texts.Append(T, buf)
  1281.             END
  1282.         ELSE Texts.WriteString(W0, commands); Texts.Append(T, W0.buf)
  1283.         END;
  1284.         NEW(frame); Open(frame, T, 0);
  1285.         frame.col := Display.white; frame.left := 6; frame.top := 0; frame.bot := 0; frame.barW := 0;
  1286.         RETURN frame
  1287.     END NewMenu;
  1288. BEGIN
  1289.     Texts.OpenWriter(W); Texts.OpenWriter(W0);
  1290.     Texts.SetFont(W0, Fonts.Default); Texts.SetColor(W0, Display.white); Texts.SetOffset(W0, 0);
  1291.     neutralize.id := Oberon.neutralize;
  1292.     NEW(par);
  1293.     NEW(B); Texts.OpenBuf(B);
  1294.     menuH := Fonts.Default.height + 2;
  1295.     barW := 14; left := barW + 6; right := 8; top := 6; bot := 6;
  1296.     InitDefParc
  1297. END TextFrames.
  1298.