-- File: SVViewerToolsImpl.mesa
-- Author: Eric Bier on December 18, 1982 1:40 am
-- Last edited by Eric Bier on March 23, 1983 2:59 pm
-- Contents: Basic functions for retrieving values from and writing values to text viewers

DIRECTORY
 GraphicsColor,
IO,
 MessageWindow,
 Rope,
 SVViewerTools,
 TFI3d,
 ViewerClasses,
 ViewerTools;

SVViewerToolsImpl: PROGRAM
IMPORTS GraphicsColor, IO, MessageWindow, Rope, TFI3d, ViewerTools
EXPORTS SVViewerTools =
BEGIN

Color: TYPE = GraphicsColor.Color;
Viewer: TYPE = ViewerClasses.Viewer;

GetReal: PUBLIC PROC [textViewer: Viewer, default: REAL] RETURNS [r: REAL] = {
 rRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
IF Rope.Size[rRope] = 0 THEN r ← default ELSE {
  IF Rope.Find[rRope, ".", 0, FALSE] = -1 THEN rRope ← Rope.Concat[rRope, ".0"];
  r ← IO.GetReal[IO.RIS[rRope]];
  };
 };

GetBool: PUBLIC PROC [textViewer: Viewer] RETURNS [b: BOOL] = {
 bRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
 b ← IF Rope.Equal[bRope, "TRUE"] THEN TRUE ELSE FALSE;
 };

GetColor: PUBLIC PROC [textViewer: Viewer] RETURNS [color: GraphicsColor.Color, success: BOOL] = {
 r, g, b: REAL;
 success ← TRUE;
 [r, g, b] ← GetThreeReals[textViewer];
IF r > 1 OR r < 0 THEN {MessageWindow.Append["r value must be between 0 and 1"]; MessageWindow.Blink[]; success ← FALSE};
IF g > 1 OR g < 0 THEN {MessageWindow.Append["g value must be between 0 and 1"]; MessageWindow.Blink[]; success ← FALSE};
IF b > 1 OR b < 0 THEN {MessageWindow.Append["b value must be between 0 and 1"]; MessageWindow.Blink[]; success ← FALSE};
IF success THEN color ← GraphicsColor.RGBToColor [r, g, b];
 };

GetThreeReals: PUBLIC PROC [textViewer: Viewer] RETURNS [x, y, z: REAL] = {
 wholeRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
 wholeStream: IO.STREAMIO.RIS[wholeRope];
 threeNumsThere: BOOLTRUE;
 TFI3d.ReadBlank[wholeStream];
 x ← TFI3d.ReadReal[wholeStream];
 TFI3d.ReadBlankAndRope[wholeStream, ","
  !TFI3d.RopeNotOnTop => {threeNumsThere ← FALSE; CONTINUE}];
IF NOT threeNumsThere THEN y ← x
ELSE {TFI3d.ReadBlank[wholeStream]; y ← TFI3d.ReadReal[wholeStream]};
 TFI3d.ReadBlankAndRope[wholeStream, ","
  !TFI3d.RopeNotOnTop => {threeNumsThere ← FALSE; CONTINUE}];
IF NOT threeNumsThere THEN z ← y
ELSE {TFI3d.ReadBlank[wholeStream]; z ← TFI3d.ReadReal[wholeStream]};
 };

SetReal: PUBLIC PROC [viewer: Viewer, real: REAL] = {
 s: IO.STREAM;
 realRope: Rope.ROPE;
 s ← IO.CreateOutputStreamToRope[];
 s.PutF["%g", [real[real]]];
 realRope ← IO.GetOutputStreamRope[s];
 ViewerTools.SetContents[viewer, realRope];
 };

SetBool: PUBLIC PROC [viewer: Viewer, bool: BOOL] = {
 s: IO.STREAM;
 boolRope: Rope.ROPE;
 s ← IO.CreateOutputStreamToRope[];
 s.PutF["%g", [boolean[bool]]];
 boolRope ← IO.GetOutputStreamRope[s];
 ViewerTools.SetContents[viewer, boolRope];
 };

SetColor: PUBLIC PROC [viewer: Viewer, color: Color] = {
 r,g,b: REAL;
 [r,g,b] ← GraphicsColor.ColorToRGB[color];
 SetThreeReals[viewer, r, g, b];
 };

SetThreeReals: PUBLIC PROC [viewer: Viewer, a, b, c: REAL] = {
 s: IO.STREAM;
 realsRope: Rope.ROPE;
 s ← IO.CreateOutputStreamToRope[];
 s.PutF["%g, %g, %g", [real[a]], [real[b]], [real[c]]];
 realsRope ← IO.GetOutputStreamRope[s];
 ViewerTools.SetContents[viewer, realsRope];
 };

END.