-- compiler FigureOps/n
--m.stone November 6, 1980  11:03 AM
--Tiberi November 19, 1979  5:48 PM

DIRECTORY
	GriffinDefs: FROM "GriffinDefs",
	GriffinStartDefs: FROM "GriffinStartDefs",
	MenuDefs: FROM "MenuDefs",
	PointDefs: FROM "PointDefs",
	ObjectDefs: FROM "ObjectDefs",
	ImageDefs: FROM "ImageDefs",
	GriffinFileDefs: FROM "GriffinFileDefs",
	AltoDisplay: FROM "AltoDisplay",
	DirectoryDefs: FROM "DirectoryDefs"
		USING [EnumerateDirectory],
	RefreshDefs: FROM "RefreshDefs",
	ScreenDefs: FROM "ScreenDefs";

FigureOps: PROGRAM
	IMPORTS GriffinDefs, MenuDefs, ScreenDefs, ObjectDefs,
		GriffinFileDefs, DirectoryDefs,RefreshDefs
	EXPORTS GriffinDefs, GriffinStartDefs = 
BEGIN
gridX: INTEGER ← 8; gridY: INTEGER ← 8;
gridOn: BOOLEAN ← FALSE;
X: CARDINAL = PointDefs.X;
Y: CARDINAL = PointDefs.Y;


Refresh: MenuDefs.MenuProc =
BEGIN
ScreenDefs.ClearScreen[];
ObjectDefs.ReplotAllObjects[];
END;

Press: MenuDefs.MenuProc =
BEGIN OPEN ObjectDefs,AltoDisplay;
name: STRING;
newfile: BOOLEAN;
[name,newfile] ← GetName[];
IF name.length=0 THEN RETURN;
ExpungeObjects[];
ObjectDefs.PressAllObjects[name];
END;

saveFileName: STRING;

Save: MenuDefs.MenuProc =
BEGIN OPEN ObjectDefs;
newfile: BOOLEAN;
[saveFileName,newfile] ← GetName[];
IF saveFileName.length=0 THEN RETURN;
IF newfile THEN BEGIN
	ExpungeObjects[];
	[] ← GriffinFileDefs.OpenPortfolio [saveFileName, TRUE];
	[] ← GriffinFileDefs.AddFigure [1];
	GriffinFileDefs.ClosePortfolio;
	END
ELSE {MenuDefs.ShowMenu[filewriteMenu]; RefreshDefs.RestoreScreen[]};
END;

YesFile: MenuDefs.MenuProc =
BEGIN
ObjectDefs.ExpungeObjects[];
[] ← GriffinFileDefs.OpenPortfolio [saveFileName, TRUE];
[] ← GriffinFileDefs.ReplaceFigure [1];
GriffinFileDefs.ClosePortfolio;
MenuDefs.HideMenu[filewriteMenu];
RefreshDefs.RestoreScreen[];
END;

NoFile: MenuDefs.MenuProc =
BEGIN
MenuDefs.HideMenu[filewriteMenu];
RefreshDefs.RestoreScreen[];
END;

Restore: MenuDefs.MenuProc =
BEGIN OPEN ObjectDefs;
name: STRING;
newfile: BOOLEAN;
[name,newfile] ← GetName[];
IF name.length=0 THEN RETURN;
ExpungeObjects[];
IF  GriffinFileDefs.OpenPortfolio [name, FALSE] THEN
	BEGIN
	GriffinFileDefs.ReadFigure [1];
	GriffinFileDefs.ClosePortfolio;
	ObjectDefs.ReplotAllObjects[];
	END;
END;

TypeIntoFileName: MenuDefs.MenuProc =
BEGIN
MenuDefs.SelectOnly[item];
GriffinDefs.TypeIntoMenuItem[item];
END;

GetName: PROCEDURE RETURNS[string: STRING, newname: BOOLEAN]=
BEGIN OPEN MenuDefs;
nameItem: MenuItemHandle ← NIL;
FindLastSelected: MenuProc =
	BEGIN
	IF IsSelected[item] THEN nameItem ← item;
	END;
ForAllMenuItems[filesMenu, FindLastSelected];
IF nameItem = fileName THEN BEGIN
	nameItem ← AddPortfolioName[MenuString[fileName]];
	RETURN[MenuString[nameItem], TRUE];
	END
ELSE  RETURN[MenuString[nameItem], FALSE];
END;

AddPortfolioName: PROCEDURE[string: STRING]
	RETURNS [item: MenuDefs.MenuItemHandle] =
BEGIN OPEN MenuDefs, GriffinDefs;
i: CARDINAL;
IF string.length = 0
	THEN UserMessage["First select or type a file name."];
FOR i IN [0..string.length) DO
   IF string[i] = '   THEN UserMessage["Invalid file name."];
ENDLOOP;
item ← AddMenuItem[filesMenu, string, SelectOnly];
SetMenuString[fileName, ""];
SelectOnly[item];
RefreshDefs.RestoreScreen[];
END;

GridMenuProc: MenuDefs.MenuProc =
BEGIN
gridOn ← ~gridOn;
IF ~gridOn THEN BEGIN
	MenuDefs.Deselect[item];
	ObjectDefs.GridOff[];
	END
ELSE BEGIN
	MenuDefs.Select[item];
	ObjectDefs.GridOn[];
	END;
END;

Grid: PUBLIC PROCEDURE[pt: PointDefs.ScrPt] RETURNS[PointDefs.ScrPt]=
BEGIN
dx,dy: INTEGER;
IF NOT gridOn THEN RETURN[pt];
--will be unconvincing if grid < 2;
dx ← pt[X] MOD gridX;
pt[X] ← pt[X]-dx;
IF dx > gridX/2 THEN pt[X] ← pt[X] +gridX;
dy ← pt[Y] MOD gridY;
pt[Y] ← pt[Y]-dy;
IF dy > gridY/2 THEN pt[Y] ← pt[Y] +gridY;
RETURN[pt];
END;

Merge: MenuDefs.MenuProc =
BEGIN OPEN ObjectDefs;
view: View ←  viewMenu.view;
Mark: ObjectProc = BEGIN
	IF obj.view=view THEN RETURN;
	obj.view ← view;
	RefreshDefs.MarkBox[obj.tl,obj.br,obj];
	END;
ForAllObjects[Mark];
RefreshDefs.RestoreScreen[];
END;

Switch: MenuDefs.MenuProc =
BEGIN OPEN ObjectDefs;
newView: View ← IF viewMenu.view=main THEN alternate ELSE main;
Mark: ObjectProc = BEGIN
	WITH foo: obj SELECT FROM
	menu => obj.view ← newView;
	token => IF foo.tokenType#selected AND foo.tokenType#open THEN obj.view ← newView;
	ENDCASE;
	END;
ForAllObjects[Mark];
SetCurrentView[newView];
ScreenDefs.ClearScreen[];
ObjectDefs.ReplotAllObjects[];
END;

EachFile: PROCEDURE [fp: POINTER, s: STRING]
	RETURNS [BOOLEAN] =
BEGIN
s.length ← GriffinFileDefs.IsTail[s, ".Griffin."];
IF s.length # 0
	THEN []←AddPortfolioName[s];
RETURN[FALSE];
END;

promptMenu: MenuDefs.MenuHandle←NIL;
displayMenu,filesMenu,viewMenu, filewriteMenu: PUBLIC MenuDefs.MenuHandle←NIL;

fileName: MenuDefs.MenuItemHandle;

promptMenu ← MenuDefs.CreateMenu[horizontal, [60,70], "Type File Name >"];

viewMenu ← MenuDefs.CreateMenu[horizontal, [100,50], NIL];
[]←MenuDefs.AddMenuItem[viewMenu,"Refresh",Refresh];
[]←MenuDefs.AddMenuItem[viewMenu,"Grid",GridMenuProc];
[]←MenuDefs.AddMenuItem[viewMenu,"Merge",Merge];
[]←MenuDefs.AddMenuItem[viewMenu,"Switch",Switch];

filesMenu ← MenuDefs.CreateMenu[vertical, [100,75], NIL];
[]←MenuDefs.AddMenuItem[filesMenu,"Save", Save];
[]←MenuDefs.AddMenuItem[filesMenu,"Restore", Restore];
[]←MenuDefs.AddMenuItem[filesMenu,"Press", Press];
fileName ← MenuDefs.AddMenuItem[filesMenu, "", TypeIntoFileName];

filewriteMenu ← MenuDefs.CreateMenu[horizontal, [100,75], "Overwrite Exisiting File? [Confirm]"];
[]←MenuDefs.AddMenuItem[filewriteMenu,"Yes",YesFile];
[]←MenuDefs.AddMenuItem[filewriteMenu,"No",NoFile];

DirectoryDefs.EnumerateDirectory [EachFile];
MenuDefs.SelectOnly[fileName];

END.