-- LoomSetup.mesa Edited by Sweet on June 13, 1979  10:14 AM

DIRECTORY
  DisplayDefs: FROM "displaydefs" USING [Dimension],
  ImageDefs: FROM "imagedefs" USING [StopMesa],
  IODefs: FROM "IODefs" USING [BS, ControlA, ControlW, DEL, SP],
  LoomDefs: FROM "loomdefs",
  MenuDefs: FROM "MenuDefs" USING [InstantiateMenuWithSubwindow,
    MenuHandle, MenuItemObject, CreateMenu, UninstantiateMenuFromSubwindow,
    DestroyMenu, MenuItemIndex],
  ParameterDefs: FROM "ParameterDefs" USING [
    CreateParameterSubwindow, defaultBoxWidth,
    DisplayParameterItem, EnumeratedRecord,
    line0, line1, line2, line3, line4, line5, line6, line7, line8,
    ParameterItemDescriptor,
    ParameterItemHandle, ParameterItemObject, ParameterProcType,
    ParameterStringEditProcType, VanillaEditStringProc,
    VanillaFeedbackStringProc, VanillaMenuProc, VanillaNotifyProc],
  SelectionDefs: FROM "SelectionDefs" USING [SelectionTargetType],
  StringDefs: FROM "StringDefs" USING [AppendChar, AppendDecimal,
    AppendString],
  SystemDefs: FROM "SystemDefs" USING [
    AllocateHeapNode, AllocateHeapString, FreeHeapNode, FreeHeapString],
  ToolsDefs: FROM "ToolsDefs" USING [ MakeHeapString],
  WindowDefs: FROM "WindowDefs" USING [
    CreateSubwindow, MakeWindowSizeTiny,
    DestroyWindowEtc,
    PaintWindow,
    SubwindowHandle, SubwindowPlace,
    WindowHandle];

LoomSetup: PROGRAM
  IMPORTS ImageDefs, LoomDefs, MenuDefs, ParameterDefs,
    StringDefs, SystemDefs, ToolsDefs, WindowDefs
  EXPORTS LoomDefs =
    
BEGIN OPEN DisplayDefs, MenuDefs, ParameterDefs, WindowDefs, LoomDefs;

Quit: PUBLIC PROCEDURE [
  sw: SubwindowHandle, items: ParameterItemDescriptor ← NULL, index: CARDINAL ← NULL] =
  BEGIN
  ImageDefs.StopMesa[];
  END;

  
DestroyThisTool: PUBLIC PROCEDURE =
  -- This procedure will be called just prior to this Tools destruction.
  BEGIN 
  IF msgSW # NIL THEN MakeToolInactive[];
  DestroyWindowEtc[controlWindow];
  END;


menu: ARRAY PaintAction OF MenuDefs.MenuHandle;
mi: ARRAY PaintAction OF DESCRIPTOR FOR ARRAY OF MenuDefs.MenuItemObject;
currentMenu: PaintAction ← stop;

MakeMyMenu: PUBLIC PROCEDURE =
  BEGIN OPEN MenuDefs, ToolsDefs;
  a: PaintAction;
  mi[stop] ← DESCRIPTOR[SystemDefs.AllocateHeapNode[3*SIZE[MenuItemObject]], 3];
  mi[run] ← DESCRIPTOR[SystemDefs.AllocateHeapNode[3*SIZE[MenuItemObject]], 3];
  mi[pause] ← DESCRIPTOR[SystemDefs.AllocateHeapNode[4*SIZE[MenuItemObject]], 4];
  mi[stop][0] ← mi[run][0] ← mi[pause][0] ← [MakeHeapString["Press"L], MenuProc];
  mi[stop][1] ← [MakeHeapString["Repaint"L], MenuProc];
  mi[stop][2] ← mi[run][2] ← mi[pause][3] ← [MakeHeapString["Quit"L], MenuProc];
  mi[run][1] ← [MakeHeapString["Pause"L], MenuProc];
  mi[pause][1] ← [MakeHeapString["Resume"L], MenuProc];
  mi[pause][2] ← [MakeHeapString["Cancel"L], MenuProc];
  FOR a IN PaintAction DO
    menu[a] ← CreateMenu[mi[a], "LoomTool"L];
    ENDLOOP;
  MenuDefs.InstantiateMenuWithSubwindow[menu[stop],
    weaveWindow.subwindowChain];
  currentMenu ← stop;
  END;
  
FreeMyMenu: PUBLIC PROCEDURE =
  BEGIN OPEN SystemDefs, MenuDefs;
  i: CARDINAL; a: PaintAction;
  UninstantiateMenuFromSubwindow[menu[currentMenu], weaveWindow.subwindowChain];
  FOR a IN PaintAction DO
    DestroyMenu[menu[a]];
    FOR i IN [0..LENGTH[mi[a]]) DO
      FreeHeapString[mi[a][i].keyword];
      ENDLOOP;
    FreeHeapNode[BASE[mi[a]]];
    ENDLOOP;
  END;


MenuProc: PROCEDURE [sw: SubwindowHandle, menu: MenuDefs.MenuHandle, index: MenuDefs.MenuItemIndex] =
  BEGIN
  SELECT currentMenu FROM
    stop => SELECT index FROM
      0 => Press[sw];
      1 => Repaint[sw];
      2 => Quit[sw];
      ENDCASE;
    run => SELECT index FROM
      0 => Press[sw];
      1 => PausePainting[sw];
      2 => Quit[sw];
      ENDCASE;
    pause => SELECT index FROM
      0 => Press[sw];
      1 => ResumePainting[sw];
      2 => CancelPainting[sw];
      3 => Quit[sw];
      ENDCASE;
    ENDCASE;
  END;

  PaintCommands: PUBLIC PROCEDURE [action: PaintAction] =
    BEGIN
    parameterItems[flex1parm] ← fp1[action];
    parameterItems[flex2parm] ← fp2[action];
    [] ← DisplayParameterItem[parameterSW, flex1parm];
    [] ← DisplayParameterItem[parameterSW, flex2parm];
    IF action # currentMenu THEN
      BEGIN
      UninstantiateMenuFromSubwindow[menu[currentMenu],
	weaveWindow.subwindowChain];
      InstantiateMenuWithSubwindow[menu[action],
	weaveWindow.subwindowChain];
      currentMenu ← action;
      END;
    END;

flex1parm, flex2parm: CARDINAL;
fp1, fp2: ARRAY PaintAction OF ParameterItemHandle;
maxParams: CARDINAL = 50;
parameterItems: PUBLIC DESCRIPTOR FOR ARRAY OF ParameterItemHandle;

MakeMeAParameterSubwindow: PUBLIC PROCEDURE =
  BEGIN OPEN SystemDefs; 
  piBase: POINTER;
  np: INTEGER ← -1;

  parameterSW ← CreateSubwindow[controlWindow, [[0,0], [10, 10]], NIL];
  input ← AllocateHeapString[20];
  output ← AllocateHeapString[20];
  titleString ← AllocateHeapString[40];
  woString ← AllocateHeapString[100];
  poString ← AllocateHeapString[100];
  warpSizeString ← AllocateHeapString[4];
  weftSizeString ← AllocateHeapString[4];
  warpColorString ← AllocateHeapString[30];
  weftColorString ← AllocateHeapString[30];
  overlapString ← AllocateHeapString[30];
  StringDefs.AppendDecimal[warpSizeString, warpSize];
  StringDefs.AppendDecimal[weftSizeString, weftSize];
  BEGIN
  p: Pedal;
  FOR p IN Pedal DO tieup[p] ← AllocateHeapString[8]; ENDLOOP;
  END;
  piBase ← AllocateHeapNode[maxParams];
  parameterItems ← DESCRIPTOR[piBase, maxParams];
  parameterItems[np←np+1] ← MakeStringItem["Warp order"L, [1*7, line0], @woString, D1to8dash];
  parameterItems[np←np+1] ← MakeStringItem["P1"L, [0, line1], @tieup[0], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P2"L, [13*7, line1], @tieup[1], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P3"L, [26*7, line1], @tieup[2], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P4"L, [39*7, line1], @tieup[3], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P5"L, [0, line2], @tieup[4], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P6"L, [13*7, line2], @tieup[5], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P7"L, [26*7, line2], @tieup[6], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["P8"L, [39*7, line2], @tieup[7], D1to8, 8*7];
  parameterItems[np←np+1] ← MakeStringItem["Warp"L, [0, line3], @warpSizeString, D0to9];
  parameterItems[np←np+1] ← MakeStringItem[",color"L, [9*7, line3], @warpColorString, EditColors, 20*7];
  parameterItems[np←np+1] ← MakeStringItem["Overlap"L, [38*7, line3], @overlapString, EditOverlap];
  parameterItems[np←np+1] ← MakeStringItem["Weft"L, [0, line4], @weftSizeString, D0to9];
  parameterItems[np←np+1] ← MakeStringItem[",color"L, [9*7, line4], @weftColorString, EditColors, 20*7];
  parameterItems[np←np+1] ← MakeStringItem["Title"L, [0,line5], @titleString];
  parameterItems[np←np+1] ← MakeCommandItem["Load"L, [0, line6], Load];
  parameterItems[np←np+1] ← MakeStringItem["from"L, [6*7,line6], @input];
  parameterItems[np←np+1] ← MakeCommandItem["Dump"L, [31*7, line6], Dump];
  parameterItems[np←np+1] ← MakeCommandItem["Press"L, [37*7, line6], Press];
  parameterItems[np←np+1] ← MakeStringItem["to"L, [44*7,line6], @output];
  parameterItems[np←np+1] ← MakeBoolItem["AUTO"L, [1*7, line7], @autoRepaint];
  parameterItems[flex1parm ← (np←np+1)] ← fp1[stop] ← 
    MakeCommandItem["Repaint"L, [6*7, line7], Repaint];
  parameterItems[flex2parm ← (np←np+1)] ← fp2[stop] ← fp2[run] ← 
    MakeTagItem[""L, [15*7, line7]];
  fp1[run] ← MakeCommandItem["Pause"L, [6*7, line7], PausePainting];
  fp1[pause] ← MakeCommandItem["Resume"L, [6*7, line7], ResumePainting];
  fp2[pause] ← MakeCommandItem["Cancel"L, [15*7, line7], CancelPainting];
  parameterItems[np←np+1] ← MakeCommandItem["StdPedal"L, [23*7, line7], StdPedal];
  parameterItems[np←np+1] ← MakeCommandItem["Up&Down"L, [33*7, line7], UpDown];
  parameterItems[np←np+1] ← MakeCommandItem["Quit"L, [42*7, line7], Quit];
  parameterItems[poParmNum ← (np←np+1)] ← MakeStringItem["Pedal order"L, [0,line8], @poString, D1to8dash];
  nParams ← np+1;
  parameterItems ← DESCRIPTOR[piBase, nParams];
  CreateParameterSubwindow[parameterItems, parameterSW,
    [fixed, TRUE, TRUE, TRUE], ConvertSelection];
  END;
  
D1to8: PROCEDURE [item: POINTER TO string ParameterItemObject, edits: STRING] RETURNS [pos: CARDINAL] =
    BEGIN OPEN IODefs;
    filtered: STRING ← SystemDefs.AllocateHeapString[edits.length];
    i: CARDINAL; c: CHARACTER;

    FOR i IN [0..edits.length) DO
      SELECT (c ← edits[i]) FROM
	IN ['1..'8], BS, ControlA, ControlW, DEL =>
	  StringDefs.AppendChar[filtered, c];
	ENDCASE;
      ENDLOOP;
    pos ← VanillaEditStringProc[item, filtered];
    SystemDefs.FreeHeapString[filtered];
    RETURN 
    END;

D1to8dash: PROCEDURE [item: POINTER TO string ParameterItemObject, edits: STRING] RETURNS [pos: CARDINAL] =
    BEGIN OPEN IODefs;
    filtered: STRING ← SystemDefs.AllocateHeapString[edits.length];
    i: CARDINAL; c: CHARACTER;

    FOR i IN [0..edits.length) DO
      SELECT (c ← edits[i]) FROM
	IN ['1..'8], '-, BS, ControlA, ControlW, DEL, SP =>
	  StringDefs.AppendChar[filtered, c];
	ENDCASE;
      ENDLOOP;
    pos ← VanillaEditStringProc[item, filtered];
    SystemDefs.FreeHeapString[filtered];
    RETURN 
    END;

D0to9: PROCEDURE [item: POINTER TO string ParameterItemObject, edits: STRING] RETURNS [pos: CARDINAL] =
    BEGIN OPEN IODefs;
    filtered: STRING ← SystemDefs.AllocateHeapString[edits.length];
    i: CARDINAL; c: CHARACTER;

    FOR i IN [0..edits.length) DO
      SELECT (c ← edits[i]) FROM
	IN ['0..'9], BS, ControlA, ControlW, DEL =>
	  StringDefs.AppendChar[filtered, c];
	ENDCASE;
      ENDLOOP;
    pos ← VanillaEditStringProc[item, filtered];
    SystemDefs.FreeHeapString[filtered];
    RETURN 
    END;

EditColors: PROCEDURE [item: POINTER TO string ParameterItemObject, edits: STRING] RETURNS [pos: CARDINAL] =
    BEGIN OPEN IODefs;
    filtered: STRING ← SystemDefs.AllocateHeapString[edits.length];
    i: CARDINAL; c: CHARACTER;

    FOR i IN [0..edits.length) DO
      SELECT (c ← edits[i]) FROM
	IN ['0..'9], BS, ControlA, ControlW, DEL, SP, IN ColorName, 'r =>
	  StringDefs.AppendChar[filtered, c];
	ENDCASE;
      ENDLOOP;
    pos ← VanillaEditStringProc[item, filtered];
    SystemDefs.FreeHeapString[filtered];
    RETURN 
    END;

EditOverlap: PROCEDURE [item: POINTER TO string ParameterItemObject, edits: STRING] RETURNS [pos: CARDINAL] =
    BEGIN OPEN IODefs;
    filtered: STRING ← SystemDefs.AllocateHeapString[edits.length];
    i: CARDINAL; c: CHARACTER;

    FOR i IN [0..edits.length) DO
      SELECT (c ← edits[i]) FROM
	IN ['0..'9], BS, ControlA, ControlW, DEL, SP, IN ColorName, '/, '= =>
	  StringDefs.AppendChar[filtered, c];
	ENDCASE;
      ENDLOOP;
    pos ← VanillaEditStringProc[item, filtered];
    SystemDefs.FreeHeapString[filtered];
    RETURN 
    END;



-- Make Standard Tools Facilities Utilities

ConvertSelection: PROCEDURE [
  data: POINTER, targetType: SelectionDefs.SelectionTargetType]
  RETURNS [POINTER] =
  BEGIN 
  RETURN [NIL]; 
  END;
  
MakeCommandItem: PROCEDURE [
  tag: STRING, place: SubwindowPlace, proc: ParameterProcType]
  RETURNS [p: POINTER TO command ParameterItemObject] =
  BEGIN 
  s: STRING ← SystemDefs.AllocateHeapString[tag.length];
  StringDefs.AppendString[s, tag];
  p ← SystemDefs.AllocateHeapNode[SIZE[ParameterItemObject]];
  p↑ ← ParameterItemObject [s, place, command[proc: proc]];
  END;
  
MakeTagItem: PROCEDURE [
  tag: STRING, place: SubwindowPlace, other: POINTER ← NIL]
  RETURNS [p: POINTER TO tagOnly ParameterItemObject] =
  BEGIN 
  s: STRING ← SystemDefs.AllocateHeapString[tag.length];
  StringDefs.AppendString[s, tag];
  p ← SystemDefs.AllocateHeapNode[SIZE[ParameterItemObject]];
  p↑ ← ParameterItemObject [s, place, tagOnly[otherItem: other]];
  END;
  
MakeStringItem: PROCEDURE [
  tag: STRING, place: SubwindowPlace, sp: POINTER TO STRING,
  eproc: ParameterStringEditProcType ← VanillaEditStringProc, bw: Dimension ← defaultBoxWidth]
  RETURNS [p: POINTER TO string ParameterItemObject] =
  BEGIN OPEN SystemDefs; 
  s: STRING ← AllocateHeapString[tag.length];
  StringDefs.AppendString[s, tag];
  p ← AllocateHeapNode[SIZE[ParameterItemObject]];
  p↑ ← ParameterItemObject [s, place, string[
    boxWidth: bw,
    string: sp,
    editProc: eproc,
    feedbackProc: VanillaFeedbackStringProc,
    menuProc: VanillaMenuProc]];
  END;
  
MakeBoolItem: PROCEDURE [
  tag: STRING, place: SubwindowPlace, bp: POINTER TO BOOLEAN]
  RETURNS [p: POINTER TO boolean ParameterItemObject] =
  BEGIN OPEN SystemDefs; 
  s: STRING ← AllocateHeapString[tag.length];
  StringDefs.AppendString[s, tag];
  p ← AllocateHeapNode[SIZE[ParameterItemObject]];
  p↑ ← ParameterItemObject [s, place, boolean[
    feedback: video,
    switch: bp,
    proc: VanillaNotifyProc]];
  END;
  
MakeEnumItem: PROCEDURE [
  tag: STRING, place: SubwindowPlace, ep: POINTER,
  ea: DESCRIPTOR FOR ARRAY OF EnumeratedRecord]
  RETURNS [p: POINTER TO enumerated ParameterItemObject] =
  BEGIN OPEN SystemDefs; 
  s: STRING ← AllocateHeapString[tag.length];
  StringDefs.AppendString[s, tag];
  p ← AllocateHeapNode[SIZE[ParameterItemObject]];
  p↑ ← ParameterItemObject [s, place, enumerated[
    feedback: all,
    value: ep,
    proc: VanillaNotifyProc,
    enumeratedRecords: ea]];
  END;
  
-- Mainline code

MenuDefs.InstantiateMenuWithSubwindow[NIL, controlWindow.subwindowChain];
MakeWindowSizeTiny[controlWindow];
PaintWindow[controlWindow];


END. -- of LoomSetup