-- TiogaArtworkViewerImpl.mesa
-- Rick Beach, July 3, 1982 3:05 pm
-- Maureen Stone December 10, 1982 11:55 am

DIRECTORY
 Buttons,
 Containers,
 GriffinToTA USING [ConvertFile, ConvertFileToJaM, ViewType],
 Menus,
 Rope,
 SirPress USING [NewPressHandle, PressHandle, ClosePress],
 TACallig USING [ForgetThePressHandle, UseThisPressHandle],
 TAPrivate USING [FixFileName, InitJaMGraphics, NoJaMGraphicsViewer],
 TARender USING [DocumentNotFound, NotATiogaArtworkDocument, RenderDocument],
 TextNode,
 TiogaArtworkViewer,
 VFonts USING [StringWidth],
 ViewerClasses,
 ViewerOps,
 ViewerTools,
 TATypeSetter;

TiogaArtworkViewerImpl: PROGRAM
IMPORTS Buttons, Containers, GriffinToTA, Menus, Rope, SirPress, TACallig, TAPrivate, TARender, VFonts, ViewerOps, ViewerTools, TATypeSetter
EXPORTS TiogaArtworkViewer = {

ROPE: TYPE = Rope.ROPE;

griffinView: GriffinToTA.ViewType ← main;
tAContainer: Containers.Container;
griffinButton: Buttons.Button;
pressButton: Buttons.Button;
jamButton: Buttons.Button;
graphicsButton: Buttons.Button;
fileNameBox: ViewerClasses.Viewer;
messageBox: ViewerClasses.Viewer;
savedFileName: ROPENIL;
griffinToTiogaRope: ROPE = "Griffin -> TiogaArtwork!";
tiogaToPressRope: ROPE = "TiogaArtwork -> Press!";
tiogaToJaMGraphicsRope: ROPE = "TiogaArtwork -> JaMGraphics!";
griffinToJaMRope: ROPE = "Griffin -> JaM!";


leftBorder: INTEGER = 10;
gapBetween: INTEGER = 10;
smallGap: INTEGER = 5;
heightBetween: INTEGER = 18;
verticalGap: INTEGER = 5;

Line: PROC [line: INTEGER] RETURNS [INTEGER] = INLINE{
RETURN[verticalGap+(line-1)*heightBetween]};

CreateViewer: PUBLIC PROCEDURE = {
 menu: Menus.Menu ← Menus.CreateMenu[];
 longestButton: INTEGER = MAX[
  VFonts.StringWidth[griffinToTiogaRope],
  VFonts.StringWidth[tiogaToPressRope],
  VFonts.StringWidth[tiogaToJaMGraphicsRope]];
 tAContainer ← Containers.Create[
  info: [
   name: "Tioga Artwork Viewer",
   column: right,
   openHeight: Line[8]]];
 griffinButton ← Buttons.Create[
  info: [
   name: griffinToTiogaRope,
   wx: leftBorder,
   wy: Line[1],
   parent: tAContainer ],
  proc: GriffinToTiogaOp,
  fork: TRUE,
  documentation: "Convert the file from Griffin format to Tioga Artwork"];
 jamButton ← Buttons.Create[
  info: [
   name: griffinToJaMRope,
   wx: leftBorder,
   wy: Line[2],
   parent: tAContainer ],
  proc: GriffinToJaMOp,
  fork: TRUE,
  documentation: "Convert the file from Griffin format to a JaM script"];
 graphicsButton ← Buttons.Create[
  info: [
   name: tiogaToJaMGraphicsRope,
   wx: leftBorder,
   wy: Line[3],
   parent: tAContainer ],
  proc: TiogaToJaMGraphicsOp,
  fork: TRUE,
  documentation: "Display the Tioga Artwork file in the JaMGraphics viewer"];
 fileNameBox ← ViewerTools.MakeNewTextViewer[
  info:[
   wx: leftBorder+gapBetween+longestButton,
   wy: Line[1],
   ww: 4*72,
   wh: heightBetween,
   parent: tAContainer,
   scrollable: FALSE],
  paint: FALSE];
 Containers.ChildXBound[tAContainer, fileNameBox];
 messageBox ← ViewerTools.MakeNewTextViewer[
  info: [
   wx: leftBorder,
   wy: Line[4]+heightBetween/2,
   ww: 4*72,
   wh: Line[2],
   parent: tAContainer,
   scrollable: FALSE,
   border: FALSE],
  paint: FALSE];
 Containers.ChildXBound[tAContainer, messageBox];
 ViewerTools.InhibitUserEdits[messageBox];
 ViewerTools.SetContents[messageBox, "Enter filename and bug an operation"];
 Menus.InsertMenuEntry[menu: menu,
  entry: Menus.CreateEntry[name: "GetSelection", proc: GetSelectionButton, fork: FALSE,
  documentation: "Stuff the selection (or the name of the selected viewer) into the file name box"]];
 Menus.InsertMenuEntry[menu: menu,
  entry: Menus.CreateEntry[name: "HookUpTSetter", proc: TSetterButton, fork: FALSE,
  documentation: "Sets up the callback procs for the TSetter. Run TSetter first"]];
 ViewerOps.SetMenu[tAContainer, menu];
 };

GriffinToTiogaOp: Buttons.ButtonProc = {
 fileName: ROPE;
IF ~FileNameBoxOk[] THEN RETURN;
 fileName ← TAPrivate.FixFileName[ViewerTools.GetContents[fileNameBox], ".Griffin"];
 Working[griffinButton, "Converting from ", fileName, " to Tioga Artwork."];
 GriffinToTA.ConvertFile[fileName, griffinView];
 AllDone[griffinButton, "Completed the Tioga Artwork file: ", fileName];

 };

GriffinToJaMOp: Buttons.ButtonProc = {
 fileName: ROPE;
IF ~FileNameBoxOk[] THEN RETURN;
 fileName ← TAPrivate.FixFileName[ViewerTools.GetContents[fileNameBox], ".Griffin"];
 Working[jamButton, "Converting from ", fileName, " to JaM script."];
 GriffinToTA.ConvertFileToJaM[fileName, griffinView];
 fileName ← TAPrivate.FixFileName[ViewerTools.GetContents[fileNameBox], ".JaM"];
 AllDone[jamButton, "Completed the JaM file: ", fileName];

 };

TiogaToPressOp: Buttons.ButtonProc = {
 artFileName, pressFileName: ROPE;
 pressHandle: SirPress.PressHandle;
IF ~FileNameBoxOk[] THEN RETURN;
 artFileName ← TAPrivate.FixFileName[ViewerTools.GetContents[fileNameBox], ".Artwork"];
 pressFileName ← TAPrivate.FixFileName[ViewerTools.GetContents[fileNameBox], ".Press"];
 Working[pressButton, "Converting from ", artFileName, " to a press file."];
 TAPrivate.InitJaMGraphics[ ! TAPrivate.NoJaMGraphicsViewer => GOTO NoJaMGraphics];
 pressHandle ← SirPress.NewPressHandle[pressFileName];
 TACallig.UseThisPressHandle[pressHandle];
 { ENABLE UNWIND => TACallig.ForgetThePressHandle[];
  TARender.RenderDocument[handle: NIL, fileName: artFileName, paint: TRUE !
   TARender.DocumentNotFound => {savedFileName ← fileName; GOTO NoFile};
   TARender.NotATiogaArtworkDocument => {savedFileName ← fileName; GOTO NotArtwork}];
  TACallig.ForgetThePressHandle[];
  };
 SirPress.ClosePress[pressHandle];
 AllDone[pressButton, "Completed the press file: ", pressFileName];
EXITS
  NoFile => AllDone[pressButton, "The file ", savedFileName, " could not be opened."];
  NotArtwork=> AllDone[pressButton, "The file ", savedFileName, " does not have Artwork=TRUE property on the root."];
  NoJaMGraphics => AllDone[pressButton, "Either no JaM typescript or TJaMGraphicsPackage could not be loaded."];
 };

TiogaToJaMGraphicsOp: Buttons.ButtonProc = {
 fileName: ROPE;
IF ~FileNameBoxOk[] THEN RETURN;
 fileName ← TAPrivate.FixFileName[ViewerTools.GetContents[fileNameBox], ".Artwork"];
 Working[graphicsButton, "Rendering Tioga Artwork in ", fileName];
 TAPrivate.InitJaMGraphics[ ! TAPrivate.NoJaMGraphicsViewer => GOTO NoJaMGraphics];
 TARender.RenderDocument[handle: NIL, fileName: fileName, paint: TRUE !
  TARender.DocumentNotFound => {savedFileName ← fileName; GOTO NoFile};
  TARender.NotATiogaArtworkDocument => {savedFileName ← fileName; GOTO NotArtwork}];
 AllDone[graphicsButton, "Completed drawing Tioga Artwork figure."];
EXITS
  NoFile => AllDone[graphicsButton, "The file ", savedFileName, " could not be opened."];
  NotArtwork=> AllDone[graphicsButton, "The file ", savedFileName, " does not have Artwork=TRUE property on the root."];
  NoJaMGraphics => AllDone[graphicsButton, "Either no JaM typescript or TJaMGraphicsPackage could not be loaded."];
 };

FileNameBoxOk: PROC RETURNS[BOOLEAN] = {
 ViewerTools.SetContents[messageBox, ""];
IF Rope.Size[ViewerTools.GetContents[fileNameBox]] = 0 THEN {
  ViewerTools.SetContents[messageBox, "Enter the filename in the box first."];
  RETURN[FALSE]};
RETURN[TRUE];
 };

GetSelectionButton: Menus.MenuProc = {
 selectedViewer: ViewerClasses.Viewer ← ViewerTools.GetSelectedViewer[];
 sourceName: ROPE
  IF selectedViewer = NIL OR selectedViewer.class.get = NIL THEN NIL
  ELSE NARROW[selectedViewer.class.get[selectedViewer, $SelChars]];
IF sourceName.Length[] <=1 THEN {
  IF (selectedViewer = NIL) THEN {
   ViewerTools.SetContents[messageBox, "Selection not in text viewer"];
   sourceName ← NIL}
  ELSE sourceName ← selectedViewer.name;
  };
 ViewerTools.SetContents[fileNameBox, sourceName];
 };

TSetterButton: Menus.MenuProc = {
 TATypeSetter.RegisterProcs[];
 };


Working: PROCEDURE[button: Buttons.Button, r1, r2, r3, r4, r5: ROPENIL] = {
 ViewerTools.SetContents[messageBox, Rope.Cat[r1, r2, r3, r4, r5]];
 Buttons.SetDisplayStyle[button, $BlackOnGrey, TRUE];
 };

AllDone: PROCEDURE[button: Buttons.Button, r1, r2, r3, r4, r5: ROPENIL] = {
 ViewerTools.SetContents[messageBox, Rope.Cat[r1, r2, r3, r4, r5]];
 Buttons.SetDisplayStyle[button, $BlackOnWhite, TRUE];
 };

CreateViewer[];

}.